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