@@ -118,15 +118,16 @@ public void getUsers(String[] userNames, final ActionListener<Collection<User>>
118
118
}
119
119
};
120
120
121
- if (securityIndex .isAvailable () == false ) {
121
+ if (securityIndex .indexExists () == false ) {
122
+ // TODO remove this short circuiting and fix tests that fail without this!
122
123
listener .onResponse (Collections .emptyList ());
123
124
} else if (userNames .length == 1 ) { // optimization for single user lookup
124
125
final String username = userNames [0 ];
125
126
getUserAndPassword (username , ActionListener .wrap (
126
127
(uap ) -> listener .onResponse (uap == null ? Collections .emptyList () : Collections .singletonList (uap .user ())),
127
128
handleException ));
128
129
} else {
129
- securityIndex .checkIndexVersionThenExecute (listener ::onFailure , () -> {
130
+ securityIndex .prepareIndexIfNeededThenExecute (listener ::onFailure , () -> {
130
131
final QueryBuilder query ;
131
132
if (userNames == null || userNames .length == 0 ) {
132
133
query = QueryBuilders .termQuery (Fields .TYPE .getPreferredName (), USER_DOC_TYPE );
@@ -154,10 +155,10 @@ public void getUsers(String[] userNames, final ActionListener<Collection<User>>
154
155
}
155
156
156
157
void getUserCount (final ActionListener <Long > listener ) {
157
- if (securityIndex .isAvailable () == false ) {
158
+ if (securityIndex .indexExists () == false ) {
158
159
listener .onResponse (0L );
159
160
} else {
160
- securityIndex .checkIndexVersionThenExecute (listener ::onFailure , () ->
161
+ securityIndex .prepareIndexIfNeededThenExecute (listener ::onFailure , () ->
161
162
executeAsyncWithOrigin (client .threadPool ().getThreadContext (), SECURITY_ORIGIN ,
162
163
client .prepareSearch (SECURITY_INDEX_NAME )
163
164
.setQuery (QueryBuilders .termQuery (Fields .TYPE .getPreferredName (), USER_DOC_TYPE ))
@@ -181,10 +182,11 @@ public void onFailure(Exception e) {
181
182
* Async method to retrieve a user and their password
182
183
*/
183
184
private void getUserAndPassword (final String user , final ActionListener <UserAndPassword > listener ) {
184
- if (securityIndex .isAvailable () == false ) {
185
+ if (securityIndex .indexExists () == false ) {
186
+ // TODO remove this short circuiting and fix tests that fail without this!
185
187
listener .onResponse (null );
186
188
} else {
187
- securityIndex .checkIndexVersionThenExecute (listener ::onFailure , () ->
189
+ securityIndex .prepareIndexIfNeededThenExecute (listener ::onFailure , () ->
188
190
executeAsyncWithOrigin (client .threadPool ().getThreadContext (), SECURITY_ORIGIN ,
189
191
client .prepareGet (SECURITY_INDEX_NAME ,
190
192
INDEX_TYPE , getIdForUser (USER_DOC_TYPE , user )).request (),
@@ -457,28 +459,24 @@ public void onFailure(Exception e) {
457
459
}
458
460
459
461
public void deleteUser (final DeleteUserRequest deleteUserRequest , final ActionListener <Boolean > listener ) {
460
- if (securityIndex .isAvailable () == false ) {
461
- listener .onResponse (false );
462
- } else {
463
- securityIndex .checkIndexVersionThenExecute (listener ::onFailure , () -> {
464
- DeleteRequest request = client .prepareDelete (SECURITY_INDEX_NAME ,
462
+ securityIndex .prepareIndexIfNeededThenExecute (listener ::onFailure , () -> {
463
+ DeleteRequest request = client .prepareDelete (SECURITY_INDEX_NAME ,
465
464
INDEX_TYPE , getIdForUser (USER_DOC_TYPE , deleteUserRequest .username ())).request ();
466
- request .setRefreshPolicy (deleteUserRequest .getRefreshPolicy ());
467
- executeAsyncWithOrigin (client .threadPool ().getThreadContext (), SECURITY_ORIGIN , request ,
465
+ request .setRefreshPolicy (deleteUserRequest .getRefreshPolicy ());
466
+ executeAsyncWithOrigin (client .threadPool ().getThreadContext (), SECURITY_ORIGIN , request ,
468
467
new ActionListener <DeleteResponse >() {
469
468
@ Override
470
469
public void onResponse (DeleteResponse deleteResponse ) {
471
470
clearRealmCache (deleteUserRequest .username (), listener ,
472
- deleteResponse .getResult () == DocWriteResponse .Result .DELETED );
471
+ deleteResponse .getResult () == DocWriteResponse .Result .DELETED );
473
472
}
474
473
475
474
@ Override
476
475
public void onFailure (Exception e ) {
477
476
listener .onFailure (e );
478
477
}
479
478
}, client ::delete );
480
- });
481
- }
479
+ });
482
480
}
483
481
484
482
/**
@@ -500,10 +498,11 @@ void verifyPassword(String username, final SecureString password, ActionListener
500
498
}
501
499
502
500
void getReservedUserInfo (String username , ActionListener <ReservedUserInfo > listener ) {
503
- if (securityIndex .isAvailable () == false ) {
501
+ if (securityIndex .indexExists () == false ) {
502
+ // TODO remove this short circuiting and fix tests that fail without this!
504
503
listener .onResponse (null );
505
504
} else {
506
- securityIndex .checkIndexVersionThenExecute (listener ::onFailure , () ->
505
+ securityIndex .prepareIndexIfNeededThenExecute (listener ::onFailure , () ->
507
506
executeAsyncWithOrigin (client .threadPool ().getThreadContext (), SECURITY_ORIGIN ,
508
507
client .prepareGet (SECURITY_INDEX_NAME , INDEX_TYPE ,
509
508
getIdForUser (RESERVED_USER_TYPE , username )).request (),
@@ -542,53 +541,49 @@ public void onFailure(Exception e) {
542
541
}
543
542
544
543
void getAllReservedUserInfo (ActionListener <Map <String , ReservedUserInfo >> listener ) {
545
- if (securityIndex .isAvailable () == false ) {
546
- listener .onResponse (Collections .emptyMap ());
547
- } else {
548
- securityIndex .checkIndexVersionThenExecute (listener ::onFailure , () ->
549
- executeAsyncWithOrigin (client .threadPool ().getThreadContext (), SECURITY_ORIGIN ,
550
- client .prepareSearch (SECURITY_INDEX_NAME )
544
+ securityIndex .prepareIndexIfNeededThenExecute (listener ::onFailure , () ->
545
+ executeAsyncWithOrigin (client .threadPool ().getThreadContext (), SECURITY_ORIGIN ,
546
+ client .prepareSearch (SECURITY_INDEX_NAME )
551
547
.setQuery (QueryBuilders .termQuery (Fields .TYPE .getPreferredName (), RESERVED_USER_TYPE ))
552
548
.setFetchSource (true ).request (),
553
- new ActionListener <SearchResponse >() {
554
- @ Override
555
- public void onResponse (SearchResponse searchResponse ) {
556
- Map <String , ReservedUserInfo > userInfos = new HashMap <>();
557
- assert searchResponse .getHits ().getTotalHits () <= 10 :
549
+ new ActionListener <SearchResponse >() {
550
+ @ Override
551
+ public void onResponse (SearchResponse searchResponse ) {
552
+ Map <String , ReservedUserInfo > userInfos = new HashMap <>();
553
+ assert searchResponse .getHits ().getTotalHits () <= 10 :
558
554
"there are more than 10 reserved users we need to change this to retrieve them all!" ;
559
- for (SearchHit searchHit : searchResponse .getHits ().getHits ()) {
560
- Map <String , Object > sourceMap = searchHit .getSourceAsMap ();
561
- String password = (String ) sourceMap .get (Fields .PASSWORD .getPreferredName ());
562
- Boolean enabled = (Boolean ) sourceMap .get (Fields .ENABLED .getPreferredName ());
563
- final String id = searchHit .getId ();
564
- assert id != null && id .startsWith (RESERVED_USER_TYPE ) :
555
+ for (SearchHit searchHit : searchResponse .getHits ().getHits ()) {
556
+ Map <String , Object > sourceMap = searchHit .getSourceAsMap ();
557
+ String password = (String ) sourceMap .get (Fields .PASSWORD .getPreferredName ());
558
+ Boolean enabled = (Boolean ) sourceMap .get (Fields .ENABLED .getPreferredName ());
559
+ final String id = searchHit .getId ();
560
+ assert id != null && id .startsWith (RESERVED_USER_TYPE ) :
565
561
"id [" + id + "] does not start with reserved-user prefix" ;
566
- final String username = id .substring (RESERVED_USER_TYPE .length () + 1 );
567
- if (password == null ) {
568
- listener .onFailure (new IllegalStateException ("password hash must not be null!" ));
569
- return ;
570
- } else if (enabled == null ) {
571
- listener .onFailure (new IllegalStateException ("enabled must not be null!" ));
572
- return ;
573
- } else {
574
- userInfos .put (username , new ReservedUserInfo (password .toCharArray (), enabled , false ));
575
- }
562
+ final String username = id .substring (RESERVED_USER_TYPE .length () + 1 );
563
+ if (password == null ) {
564
+ listener .onFailure (new IllegalStateException ("password hash must not be null!" ));
565
+ return ;
566
+ } else if (enabled == null ) {
567
+ listener .onFailure (new IllegalStateException ("enabled must not be null!" ));
568
+ return ;
569
+ } else {
570
+ userInfos .put (username , new ReservedUserInfo (password .toCharArray (), enabled , false ));
576
571
}
577
- listener .onResponse (userInfos );
578
572
}
573
+ listener .onResponse (userInfos );
574
+ }
579
575
580
- @ Override
581
- public void onFailure (Exception e ) {
582
- if (e instanceof IndexNotFoundException ) {
583
- logger .trace ("could not retrieve built in users since security index does not exist" , e );
584
- listener .onResponse (Collections .emptyMap ());
585
- } else {
586
- logger .error ("failed to retrieve built in users" , e );
587
- listener .onFailure (e );
588
- }
576
+ @ Override
577
+ public void onFailure (Exception e ) {
578
+ if (e instanceof IndexNotFoundException ) {
579
+ logger .trace ("could not retrieve built in users since security index does not exist" , e );
580
+ listener .onResponse (Collections .emptyMap ());
581
+ } else {
582
+ logger .error ("failed to retrieve built in users" , e );
583
+ listener .onFailure (e );
589
584
}
590
- }, client :: search ));
591
- }
585
+ }
586
+ }, client :: search ));
592
587
}
593
588
594
589
private <Response > void clearRealmCache (String username , ActionListener <Response > listener , Response response ) {
0 commit comments