Skip to content

Commit a7eaa40

Browse files
committed
Fix TransportChangePasswordActionTests
testIncorrectPasswordHashingAlgorithm is based on the assumption that the algorithm selected for the change password request is different than the one selected for the NativeUsersStore. pbkdf2_10000 is the same as pbkdf2 since 10000 is the default cost factor for pbkdf2 and thus should not be used as an option for the passwordHashingSettings. Also make sure that the same algorithm is used for settings and change password requests in other tests for consistency, even if we expect to not reach the code where the algorithm is checked for now. Resolves #31696 Reverts 1c4f480
1 parent 2971dd5 commit a7eaa40

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,21 @@
4747
public class TransportChangePasswordActionTests extends ESTestCase {
4848

4949
public void testAnonymousUser() {
50+
final String hashingAlgorithm = randomFrom("pbkdf2", "pbkdf2_1000", "bcrypt", "bcrypt9");
5051
Settings settings = Settings.builder().put(AnonymousUser.ROLES_SETTING.getKey(), "superuser").build();
5152
AnonymousUser anonymousUser = new AnonymousUser(settings);
5253
NativeUsersStore usersStore = mock(NativeUsersStore.class);
53-
TransportService transportService = new TransportService(Settings.EMPTY, null, null, TransportService.NOOP_TRANSPORT_INTERCEPTOR,
54+
Settings passwordHashingSettings = Settings.builder().
55+
put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hashingAlgorithm).build();
56+
TransportService transportService = new TransportService(passwordHashingSettings, null, null, TransportService.NOOP_TRANSPORT_INTERCEPTOR,
5457
x -> null, null, Collections.emptySet());
5558
TransportChangePasswordAction action = new TransportChangePasswordAction(settings, transportService,
5659
mock(ActionFilters.class), usersStore);
5760

5861
ChangePasswordRequest request = new ChangePasswordRequest();
62+
// Request will fail before the request hashing algorithm is checked, but we use the same algorithm as in settings for consistency
5963
request.username(anonymousUser.principal());
60-
request.passwordHash(Hasher.resolve(
61-
randomFrom("pbkdf2", "pbkdf2_1000", "bcrypt", "bcrypt9")).hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING));
64+
request.passwordHash(Hasher.resolve(hashingAlgorithm).hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING));
6265

6366
final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
6467
final AtomicReference<ChangePasswordResponse> responseRef = new AtomicReference<>();
@@ -81,16 +84,19 @@ public void onFailure(Exception e) {
8184
}
8285

8386
public void testInternalUsers() {
87+
final String hashingAlgorithm = randomFrom("pbkdf2", "pbkdf2_1000", "bcrypt", "bcrypt9");
8488
NativeUsersStore usersStore = mock(NativeUsersStore.class);
85-
TransportService transportService = new TransportService(Settings.EMPTY, null, null, TransportService.NOOP_TRANSPORT_INTERCEPTOR,
89+
Settings passwordHashingSettings = Settings.builder().
90+
put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hashingAlgorithm).build();
91+
TransportService transportService = new TransportService(passwordHashingSettings, null, null, TransportService.NOOP_TRANSPORT_INTERCEPTOR,
8692
x -> null, null, Collections.emptySet());
8793
TransportChangePasswordAction action = new TransportChangePasswordAction(Settings.EMPTY, transportService,
8894
mock(ActionFilters.class), usersStore);
8995

9096
ChangePasswordRequest request = new ChangePasswordRequest();
9197
request.username(randomFrom(SystemUser.INSTANCE.principal(), XPackUser.INSTANCE.principal()));
92-
request.passwordHash(Hasher.resolve(
93-
randomFrom("pbkdf2", "pbkdf2_1000", "bcrypt", "bcrypt9")).hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING));
98+
// Request will fail before the request hashing algorithm is checked, but we use the same algorithm as in settings for consistency
99+
request.passwordHash(Hasher.resolve(hashingAlgorithm).hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING));
94100

95101
final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
96102
final AtomicReference<ChangePasswordResponse> responseRef = new AtomicReference<>();
@@ -153,7 +159,6 @@ public void onFailure(Exception e) {
153159
verify(usersStore, times(1)).changePassword(eq(request), any(ActionListener.class));
154160
}
155161

156-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/31696")
157162
public void testIncorrectPasswordHashingAlgorithm() {
158163
final User user = randomFrom(new ElasticUser(true), new KibanaUser(true), new User("joe"));
159164
final Hasher hasher = Hasher.resolve(randomFrom("pbkdf2", "pbkdf2_1000", "bcrypt9", "bcrypt5"));
@@ -166,7 +171,7 @@ public void testIncorrectPasswordHashingAlgorithm() {
166171
TransportService transportService = new TransportService(Settings.EMPTY, null, null, TransportService.NOOP_TRANSPORT_INTERCEPTOR,
167172
x -> null, null, Collections.emptySet());
168173
Settings passwordHashingSettings = Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(),
169-
randomFrom("pbkdf2_50000", "pbkdf2_10000", "bcrypt11", "bcrypt8", "bcrypt")).build();
174+
randomFrom("pbkdf2_50000", "pbkdf2_100000", "bcrypt11", "bcrypt8", "bcrypt")).build();
170175
TransportChangePasswordAction action = new TransportChangePasswordAction(passwordHashingSettings, transportService,
171176
mock(ActionFilters.class), usersStore);
172177
action.doExecute(mock(Task.class), request, new ActionListener<ChangePasswordResponse>() {

0 commit comments

Comments
 (0)