35
35
import org .elasticsearch .xpack .core .security .action .user .ChangePasswordResponse ;
36
36
import org .elasticsearch .xpack .core .security .action .user .DeleteUserResponse ;
37
37
import org .elasticsearch .xpack .core .security .action .user .GetUsersResponse ;
38
+ import org .elasticsearch .xpack .core .security .authc .support .Hasher ;
38
39
import org .elasticsearch .xpack .core .security .authz .RoleDescriptor ;
39
40
import org .elasticsearch .xpack .core .security .authz .permission .Role ;
40
41
import org .elasticsearch .xpack .core .security .authz .store .ReservedRolesStore ;
72
73
public class NativeRealmIntegTests extends NativeRealmIntegTestCase {
73
74
74
75
private static boolean anonymousEnabled ;
76
+ private static Hasher hasher ;
75
77
76
78
private boolean roleExists ;
77
79
78
80
@ BeforeClass
79
81
public static void init () {
80
82
anonymousEnabled = randomBoolean ();
83
+ hasher = getFastStoredHashAlgoForTests ();
81
84
}
82
85
83
86
@ Override
84
87
public Settings nodeSettings (int nodeOrdinal ) {
88
+ Settings .Builder builder = Settings .builder ().put (super .nodeSettings (nodeOrdinal ))
89
+ .put ("xpack.security.authc.password_hashing.algorithm" , hasher .name ());
85
90
if (anonymousEnabled ) {
86
- return Settings .builder ().put (super .nodeSettings (nodeOrdinal ))
87
- .put (AnonymousUser .ROLES_SETTING .getKey (), "native_anonymous" )
88
- .build ();
91
+ builder .put (AnonymousUser .ROLES_SETTING .getKey (), "native_anonymous" );
89
92
}
90
- return super . nodeSettings ( nodeOrdinal );
93
+ return builder . build ( );
91
94
}
92
95
93
96
@ Before
@@ -111,7 +114,7 @@ public void setupAnonymousRoleIfNecessary() throws Exception {
111
114
public void testDeletingNonexistingUserAndRole () throws Exception {
112
115
SecurityClient c = securityClient ();
113
116
// first create the index so it exists
114
- c .preparePutUser ("joe" , "s3kirt" .toCharArray (), getFastStoredHashAlgoForTests () , "role1" , "user" ).get ();
117
+ c .preparePutUser ("joe" , "s3kirt" .toCharArray (), hasher , "role1" , "user" ).get ();
115
118
DeleteUserResponse resp = c .prepareDeleteUser ("missing" ).get ();
116
119
assertFalse ("user shouldn't be found" , resp .found ());
117
120
DeleteRoleResponse resp2 = c .prepareDeleteRole ("role" ).get ();
@@ -131,7 +134,7 @@ public void testAddAndGetUser() throws Exception {
131
134
final List <User > existingUsers = Arrays .asList (c .prepareGetUsers ().get ().users ());
132
135
final int existing = existingUsers .size ();
133
136
logger .error ("--> creating user" );
134
- c .preparePutUser ("joe" , "s3kirt" .toCharArray (), getFastStoredHashAlgoForTests () , "role1" , "user" ).get ();
137
+ c .preparePutUser ("joe" , "s3kirt" .toCharArray (), hasher , "role1" , "user" ).get ();
135
138
logger .error ("--> waiting for .security index" );
136
139
ensureGreen (SECURITY_INDEX_NAME );
137
140
logger .info ("--> retrieving user" );
@@ -142,8 +145,8 @@ public void testAddAndGetUser() throws Exception {
142
145
assertArrayEquals (joe .roles (), new String []{"role1" , "user" });
143
146
144
147
logger .info ("--> adding two more users" );
145
- c .preparePutUser ("joe2" , "s3kirt2" .toCharArray (), getFastStoredHashAlgoForTests () , "role2" , "user" ).get ();
146
- c .preparePutUser ("joe3" , "s3kirt3" .toCharArray (), getFastStoredHashAlgoForTests () , "role3" , "user" ).get ();
148
+ c .preparePutUser ("joe2" , "s3kirt2" .toCharArray (), hasher , "role2" , "user" ).get ();
149
+ c .preparePutUser ("joe3" , "s3kirt3" .toCharArray (), hasher , "role3" , "user" ).get ();
147
150
GetUsersResponse allUsersResp = c .prepareGetUsers ().get ();
148
151
assertTrue ("users should exist" , allUsersResp .hasUsers ());
149
152
assertEquals ("should be " + (3 + existing ) + " users total" , 3 + existing , allUsersResp .users ().length );
@@ -237,7 +240,7 @@ public void testAddUserAndRoleThenAuth() throws Exception {
237
240
new BytesArray ("{\" match_all\" : {}}" ))
238
241
.get ();
239
242
logger .error ("--> creating user" );
240
- c .preparePutUser ("joe" , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () , "test_role" ).get ();
243
+ c .preparePutUser ("joe" , "s3krit" .toCharArray (), hasher , "test_role" ).get ();
241
244
logger .error ("--> waiting for .security index" );
242
245
ensureGreen (SECURITY_INDEX_NAME );
243
246
logger .info ("--> retrieving user" );
@@ -258,7 +261,7 @@ public void testAddUserAndRoleThenAuth() throws Exception {
258
261
public void testUpdatingUserAndAuthentication () throws Exception {
259
262
SecurityClient c = securityClient ();
260
263
logger .error ("--> creating user" );
261
- c .preparePutUser ("joe" , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () , SecuritySettingsSource .TEST_ROLE ).get ();
264
+ c .preparePutUser ("joe" , "s3krit" .toCharArray (), hasher , SecuritySettingsSource .TEST_ROLE ).get ();
262
265
logger .error ("--> waiting for .security index" );
263
266
ensureGreen (SECURITY_INDEX_NAME );
264
267
logger .info ("--> retrieving user" );
@@ -275,7 +278,7 @@ public void testUpdatingUserAndAuthentication() throws Exception {
275
278
276
279
assertEquals (1L , searchResp .getHits ().getTotalHits ());
277
280
278
- c .preparePutUser ("joe" , "s3krit2" .toCharArray (), getFastStoredHashAlgoForTests () , SecuritySettingsSource .TEST_ROLE ).get ();
281
+ c .preparePutUser ("joe" , "s3krit2" .toCharArray (), hasher , SecuritySettingsSource .TEST_ROLE ).get ();
279
282
280
283
try {
281
284
client ().filterWithHeader (Collections .singletonMap ("Authorization" , token )).prepareSearch ("idx" ).get ();
@@ -293,7 +296,7 @@ public void testUpdatingUserAndAuthentication() throws Exception {
293
296
public void testCreateDeleteAuthenticate () {
294
297
SecurityClient c = securityClient ();
295
298
logger .error ("--> creating user" );
296
- c .preparePutUser ("joe" , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () ,
299
+ c .preparePutUser ("joe" , "s3krit" .toCharArray (), hasher ,
297
300
SecuritySettingsSource .TEST_ROLE ).get ();
298
301
logger .error ("--> waiting for .security index" );
299
302
ensureGreen (SECURITY_INDEX_NAME );
@@ -332,7 +335,7 @@ public void testCreateAndUpdateRole() {
332
335
new BytesArray ("{\" match_all\" : {}}" ))
333
336
.get ();
334
337
logger .error ("--> creating user" );
335
- c .preparePutUser ("joe" , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () , "test_role" ).get ();
338
+ c .preparePutUser ("joe" , "s3krit" .toCharArray (), hasher , "test_role" ).get ();
336
339
logger .error ("--> waiting for .security index" );
337
340
ensureGreen (SECURITY_INDEX_NAME );
338
341
@@ -381,7 +384,7 @@ public void testAuthenticateWithDeletedRole() {
381
384
.addIndices (new String []{"*" }, new String []{"read" }, new String []{"body" , "title" }, null ,
382
385
new BytesArray ("{\" match_all\" : {}}" ))
383
386
.get ();
384
- c .preparePutUser ("joe" , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () , "test_role" ).get ();
387
+ c .preparePutUser ("joe" , "s3krit" .toCharArray (), hasher , "test_role" ).get ();
385
388
logger .error ("--> waiting for .security index" );
386
389
ensureGreen (SECURITY_INDEX_NAME );
387
390
@@ -415,7 +418,7 @@ public void testPutUserWithoutPassword() {
415
418
assertThat (client .prepareGetUsers ("joes" ).get ().hasUsers (), is (false ));
416
419
// check that putting a user without a password fails if the user doesn't exist
417
420
try {
418
- client .preparePutUser ("joe" , null , getFastStoredHashAlgoForTests () , "admin_role" ).get ();
421
+ client .preparePutUser ("joe" , null , hasher , "admin_role" ).get ();
419
422
fail ("cannot create a user without a password" );
420
423
} catch (IllegalArgumentException e ) {
421
424
assertThat (e .getMessage (), containsString ("password must be specified" ));
@@ -425,15 +428,15 @@ public void testPutUserWithoutPassword() {
425
428
426
429
// create joe with a password and verify the user works
427
430
client .preparePutUser ("joe" , SecuritySettingsSourceField .TEST_PASSWORD .toCharArray (),
428
- getFastStoredHashAlgoForTests () , "admin_role" ).get ();
431
+ hasher , "admin_role" ).get ();
429
432
assertThat (client .prepareGetUsers ("joe" ).get ().hasUsers (), is (true ));
430
433
final String token = basicAuthHeaderValue ("joe" , SecuritySettingsSourceField .TEST_PASSWORD_SECURE_STRING );
431
434
ClusterHealthResponse response = client ().filterWithHeader (Collections .singletonMap ("Authorization" , token )).admin ().cluster ()
432
435
.prepareHealth ().get ();
433
436
assertFalse (response .isTimedOut ());
434
437
435
438
// modify joe without sending the password
436
- client .preparePutUser ("joe" , null , getFastStoredHashAlgoForTests () , "read_role" ).fullName ("Joe Smith" ).get ();
439
+ client .preparePutUser ("joe" , null , hasher , "read_role" ).fullName ("Joe Smith" ).get ();
437
440
GetUsersResponse getUsersResponse = client .prepareGetUsers ("joe" ).get ();
438
441
assertThat (getUsersResponse .hasUsers (), is (true ));
439
442
assertThat (getUsersResponse .users ().length , is (1 ));
@@ -454,7 +457,7 @@ public void testPutUserWithoutPassword() {
454
457
455
458
// update the user with password and admin role again
456
459
String secondPassword = SecuritySettingsSourceField .TEST_PASSWORD + "2" ;
457
- client .preparePutUser ("joe" , secondPassword .toCharArray (), getFastStoredHashAlgoForTests () , "admin_role" ).
460
+ client .preparePutUser ("joe" , secondPassword .toCharArray (), hasher , "admin_role" ).
458
461
fullName ("Joe Smith" ).get ();
459
462
getUsersResponse = client .prepareGetUsers ("joe" ).get ();
460
463
assertThat (getUsersResponse .hasUsers (), is (true ));
@@ -483,7 +486,7 @@ public void testPutUserWithoutPassword() {
483
486
public void testCannotCreateUserWithShortPassword () throws Exception {
484
487
SecurityClient client = securityClient ();
485
488
try {
486
- client .preparePutUser ("joe" , randomAlphaOfLengthBetween (0 , 5 ).toCharArray (), getFastStoredHashAlgoForTests () ,
489
+ client .preparePutUser ("joe" , randomAlphaOfLengthBetween (0 , 5 ).toCharArray (), hasher ,
487
490
"admin_role" ).get ();
488
491
fail ("cannot create a user without a password < 6 characters" );
489
492
} catch (ValidationException v ) {
@@ -494,7 +497,7 @@ public void testCannotCreateUserWithShortPassword() throws Exception {
494
497
public void testCannotCreateUserWithInvalidCharactersInName () throws Exception {
495
498
SecurityClient client = securityClient ();
496
499
ValidationException v = expectThrows (ValidationException .class ,
497
- () -> client .preparePutUser ("fóóbár" , "my-am@zing-password" .toCharArray (), getFastStoredHashAlgoForTests () ,
500
+ () -> client .preparePutUser ("fóóbár" , "my-am@zing-password" .toCharArray (), hasher ,
498
501
"admin_role" ).get ()
499
502
);
500
503
assertThat (v .getMessage (), containsString ("names must be" ));
@@ -505,7 +508,7 @@ public void testUsersAndRolesDoNotInterfereWithIndicesStats() throws Exception {
505
508
506
509
SecurityClient client = securityClient ();
507
510
if (randomBoolean ()) {
508
- client .preparePutUser ("joe" , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () ,
511
+ client .preparePutUser ("joe" , "s3krit" .toCharArray (), hasher ,
509
512
SecuritySettingsSource .TEST_ROLE ).get ();
510
513
} else {
511
514
client .preparePutRole ("read_role" )
@@ -526,7 +529,7 @@ public void testOperationsOnReservedUsers() throws Exception {
526
529
final String username = randomFrom (ElasticUser .NAME , KibanaUser .NAME );
527
530
IllegalArgumentException exception = expectThrows (IllegalArgumentException .class ,
528
531
() -> securityClient ().preparePutUser (username , randomBoolean () ? SecuritySettingsSourceField .TEST_PASSWORD .toCharArray ()
529
- : null , getFastStoredHashAlgoForTests () , "admin" ).get ());
532
+ : null , hasher , "admin" ).get ());
530
533
assertThat (exception .getMessage (), containsString ("Username [" + username + "] is reserved" ));
531
534
532
535
exception = expectThrows (IllegalArgumentException .class ,
@@ -539,21 +542,21 @@ public void testOperationsOnReservedUsers() throws Exception {
539
542
540
543
exception = expectThrows (IllegalArgumentException .class ,
541
544
() -> securityClient ().prepareChangePassword (AnonymousUser .DEFAULT_ANONYMOUS_USERNAME , "foobar" .toCharArray (),
542
- getFastStoredHashAlgoForTests () ).get ());
545
+ hasher ).get ());
543
546
assertThat (exception .getMessage (), containsString ("user [" + AnonymousUser .DEFAULT_ANONYMOUS_USERNAME + "] is anonymous" ));
544
547
545
548
exception = expectThrows (IllegalArgumentException .class ,
546
549
() -> securityClient ().preparePutUser (AnonymousUser .DEFAULT_ANONYMOUS_USERNAME , "foobar" .toCharArray (),
547
- getFastStoredHashAlgoForTests () ).get ());
550
+ hasher ).get ());
548
551
assertThat (exception .getMessage (), containsString ("Username [" + AnonymousUser .DEFAULT_ANONYMOUS_USERNAME + "] is reserved" ));
549
552
550
553
exception = expectThrows (IllegalArgumentException .class ,
551
- () -> securityClient ().preparePutUser (SystemUser .NAME , "foobar" .toCharArray (), getFastStoredHashAlgoForTests () ).get ());
554
+ () -> securityClient ().preparePutUser (SystemUser .NAME , "foobar" .toCharArray (), hasher ).get ());
552
555
assertThat (exception .getMessage (), containsString ("user [" + SystemUser .NAME + "] is internal" ));
553
556
554
557
exception = expectThrows (IllegalArgumentException .class ,
555
558
() -> securityClient ().prepareChangePassword (SystemUser .NAME , "foobar" .toCharArray (),
556
- getFastStoredHashAlgoForTests () ).get ());
559
+ hasher ).get ());
557
560
assertThat (exception .getMessage (), containsString ("user [" + SystemUser .NAME + "] is internal" ));
558
561
559
562
exception = expectThrows (IllegalArgumentException .class ,
@@ -592,7 +595,7 @@ public void testOperationsOnReservedRoles() throws Exception {
592
595
593
596
@ AwaitsFix (bugUrl = "https://github.com/elastic/elasticsearch/issues/31670" )
594
597
public void testCreateAndChangePassword () throws Exception {
595
- securityClient ().preparePutUser ("joe" , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () ,
598
+ securityClient ().preparePutUser ("joe" , "s3krit" .toCharArray (), hasher ,
596
599
SecuritySettingsSource .TEST_ROLE ).get ();
597
600
final String token = basicAuthHeaderValue ("joe" , new SecureString ("s3krit" .toCharArray ()));
598
601
ClusterHealthResponse response = client ().filterWithHeader (Collections .singletonMap ("Authorization" , token ))
@@ -601,7 +604,7 @@ public void testCreateAndChangePassword() throws Exception {
601
604
602
605
ChangePasswordResponse passwordResponse = securityClient (
603
606
client ().filterWithHeader (Collections .singletonMap ("Authorization" , token )))
604
- .prepareChangePassword ("joe" , SecuritySettingsSourceField .TEST_PASSWORD .toCharArray (), getFastStoredHashAlgoForTests () ).get ();
607
+ .prepareChangePassword ("joe" , SecuritySettingsSourceField .TEST_PASSWORD .toCharArray (), hasher ).get ();
605
608
assertThat (passwordResponse , notNullValue ());
606
609
607
610
@@ -681,7 +684,7 @@ public void testRealmUsageStats() {
681
684
final int numNativeUsers = scaledRandomIntBetween (1 , 32 );
682
685
SecurityClient securityClient = new SecurityClient (client ());
683
686
for (int i = 0 ; i < numNativeUsers ; i ++) {
684
- securityClient .preparePutUser ("joe" + i , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () ,
687
+ securityClient .preparePutUser ("joe" + i , "s3krit" .toCharArray (), hasher ,
685
688
"superuser" ).get ();
686
689
}
687
690
@@ -702,7 +705,7 @@ public void testRealmUsageStats() {
702
705
703
706
public void testSetEnabled () throws Exception {
704
707
705
- securityClient ().preparePutUser ("joe" , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () ,
708
+ securityClient ().preparePutUser ("joe" , "s3krit" .toCharArray (), hasher ,
706
709
SecuritySettingsSource .TEST_ROLE ).get ();
707
710
final String token = basicAuthHeaderValue ("joe" , new SecureString ("s3krit" .toCharArray ()));
708
711
ClusterHealthResponse response = client ().filterWithHeader (Collections .singletonMap ("Authorization" , token ))
@@ -727,7 +730,7 @@ public void testSetEnabled() throws Exception {
727
730
728
731
public void testNegativeLookupsThenCreateRole () throws Exception {
729
732
SecurityClient securityClient = new SecurityClient (client ());
730
- securityClient .preparePutUser ("joe" , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () , "unknown_role" ).get ();
733
+ securityClient .preparePutUser ("joe" , "s3krit" .toCharArray (), hasher , "unknown_role" ).get ();
731
734
732
735
final int negativeLookups = scaledRandomIntBetween (1 , 10 );
733
736
for (int i = 0 ; i < negativeLookups ; i ++) {
@@ -763,9 +766,9 @@ public void testNegativeLookupsThenCreateRole() throws Exception {
763
766
* the loader returned a null value, while the other caller(s) would get a null value unexpectedly
764
767
*/
765
768
public void testConcurrentRunAs () throws Exception {
766
- securityClient ().preparePutUser ("joe" , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () , SecuritySettingsSource
769
+ securityClient ().preparePutUser ("joe" , "s3krit" .toCharArray (), hasher , SecuritySettingsSource
767
770
.TEST_ROLE ).get ();
768
- securityClient ().preparePutUser ("executor" , "s3krit" .toCharArray (), getFastStoredHashAlgoForTests () , "superuser" ).get ();
771
+ securityClient ().preparePutUser ("executor" , "s3krit" .toCharArray (), hasher , "superuser" ).get ();
769
772
final String token = basicAuthHeaderValue ("executor" , new SecureString ("s3krit" .toCharArray ()));
770
773
final Client client = client ().filterWithHeader (MapBuilder .<String , String >newMapBuilder ()
771
774
.put ("Authorization" , token )
0 commit comments