Skip to content

Commit ef7ccd1

Browse files
authored
[TEST] Consistent algorithm usage (#32077)
Ensure that the same algorithm is used for settings and change password requests for consistency, even if we do not expext to reach the code where the algorithm is checked for now. Completes a7eaa40
1 parent a3b608d commit ef7ccd1

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportChangePasswordActionTests.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,16 @@ public class TransportChangePasswordActionTests extends ESTestCase {
4949

5050
public void testAnonymousUser() {
5151
final String hashingAlgorithm = randomFrom("pbkdf2", "pbkdf2_1000", "bcrypt", "bcrypt9");
52-
Settings settings = Settings.builder().put(AnonymousUser.ROLES_SETTING.getKey(), "superuser").build();
52+
Settings settings = Settings.builder().put(AnonymousUser.ROLES_SETTING.getKey(), "superuser")
53+
.put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hashingAlgorithm).build();
5354
AnonymousUser anonymousUser = new AnonymousUser(settings);
5455
NativeUsersStore usersStore = mock(NativeUsersStore.class);
55-
Settings passwordHashingSettings = Settings.builder().
56-
put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hashingAlgorithm).build();
57-
TransportService transportService = new TransportService(passwordHashingSettings, mock(Transport.class), null,
56+
TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null,
5857
TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
5958
TransportChangePasswordAction action = new TransportChangePasswordAction(settings, transportService,
6059
mock(ActionFilters.class), usersStore);
61-
62-
ChangePasswordRequest request = new ChangePasswordRequest();
6360
// Request will fail before the request hashing algorithm is checked, but we use the same algorithm as in settings for consistency
61+
ChangePasswordRequest request = new ChangePasswordRequest();
6462
request.username(anonymousUser.principal());
6563
request.passwordHash(Hasher.resolve(hashingAlgorithm).hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING));
6664

@@ -89,14 +87,13 @@ public void testInternalUsers() {
8987
NativeUsersStore usersStore = mock(NativeUsersStore.class);
9088
Settings passwordHashingSettings = Settings.builder().
9189
put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hashingAlgorithm).build();
92-
TransportService transportService = new TransportService(passwordHashingSettings, mock(Transport.class), null,
90+
TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null,
9391
TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
94-
TransportChangePasswordAction action = new TransportChangePasswordAction(Settings.EMPTY, transportService,
92+
TransportChangePasswordAction action = new TransportChangePasswordAction(passwordHashingSettings, transportService,
9593
mock(ActionFilters.class), usersStore);
96-
94+
// Request will fail before the request hashing algorithm is checked, but we use the same algorithm as in settings for consistency
9795
ChangePasswordRequest request = new ChangePasswordRequest();
9896
request.username(randomFrom(SystemUser.INSTANCE.principal(), XPackUser.INSTANCE.principal()));
99-
// Request will fail before the request hashing algorithm is checked, but we use the same algorithm as in settings for consistency
10097
request.passwordHash(Hasher.resolve(hashingAlgorithm).hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING));
10198

10299
final AtomicReference<Throwable> throwableRef = new AtomicReference<>();

0 commit comments

Comments
 (0)