Skip to content

Commit ac5ef8c

Browse files
authored
Security: remove password hash bootstrap check (#32440)
This change removes the PasswordHashingBootstrapCheck and replaces it with validation on the setting itself. This ensures we always get a valid value from the setting when it is used.
1 parent 7d8a64d commit ac5ef8c

File tree

6 files changed

+43
-89
lines changed

6 files changed

+43
-89
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.xpack.core.ssl.VerificationMode;
1515

1616
import javax.crypto.Cipher;
17+
import javax.crypto.SecretKeyFactory;
1718

1819
import java.security.NoSuchAlgorithmException;
1920
import java.util.ArrayList;
@@ -127,8 +128,16 @@ private XPackSettings() {
127128
public static final Setting<String> PASSWORD_HASHING_ALGORITHM = new Setting<>(
128129
"xpack.security.authc.password_hashing.algorithm", "bcrypt", Function.identity(), (v, s) -> {
129130
if (Hasher.getAvailableAlgoStoredHash().contains(v.toLowerCase(Locale.ROOT)) == false) {
130-
throw new IllegalArgumentException("Invalid algorithm: " + v + ". Only pbkdf2 or bcrypt family algorithms can be used for " +
131-
"password hashing.");
131+
throw new IllegalArgumentException("Invalid algorithm: " + v + ". Valid values for password hashing are " +
132+
Hasher.getAvailableAlgoStoredHash().toString());
133+
} else if (v.regionMatches(true, 0, "pbkdf2", 0, "pbkdf2".length())) {
134+
try {
135+
SecretKeyFactory.getInstance("PBKDF2withHMACSHA512");
136+
} catch (NoSuchAlgorithmException e) {
137+
throw new IllegalArgumentException(
138+
"Support for PBKDF2WithHMACSHA512 must be available in order to use any of the " +
139+
"PBKDF2 algorithms for the [xpack.security.authc.password_hashing.algorithm] setting.", e);
140+
}
132141
}
133142
}, Setting.Property.NodeScope);
134143

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/XPackSettingsTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
*/
66
package org.elasticsearch.xpack.core;
77

8+
import org.elasticsearch.common.settings.Settings;
89
import org.elasticsearch.test.ESTestCase;
910
import javax.crypto.Cipher;
11+
import javax.crypto.SecretKeyFactory;
1012

13+
import java.security.NoSuchAlgorithmException;
14+
15+
import static org.hamcrest.Matchers.containsString;
1116
import static org.hamcrest.Matchers.hasItem;
1217
import static org.hamcrest.Matchers.not;
1318

@@ -25,4 +30,30 @@ public void testDefaultSSLCiphers() throws Exception {
2530
assertThat(XPackSettings.DEFAULT_CIPHERS, not(hasItem("TLS_RSA_WITH_AES_256_CBC_SHA")));
2631
}
2732
}
33+
34+
public void testPasswordHashingAlgorithmSettingValidation() {
35+
final boolean isPBKDF2Available = isSecretkeyFactoryAlgoAvailable("PBKDF2WithHMACSHA512");
36+
final String pbkdf2Algo = randomFrom("PBKDF2_10000", "PBKDF2");
37+
final Settings settings = Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), pbkdf2Algo).build();
38+
if (isPBKDF2Available) {
39+
assertEquals(pbkdf2Algo, XPackSettings.PASSWORD_HASHING_ALGORITHM.get(settings));
40+
} else {
41+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
42+
() -> XPackSettings.PASSWORD_HASHING_ALGORITHM.get(settings));
43+
assertThat(e.getMessage(), containsString("Support for PBKDF2WithHMACSHA512 must be available"));
44+
}
45+
46+
final String bcryptAlgo = randomFrom("BCRYPT", "BCRYPT11");
47+
assertEquals(bcryptAlgo, XPackSettings.PASSWORD_HASHING_ALGORITHM.get(
48+
Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), bcryptAlgo).build()));
49+
}
50+
51+
private boolean isSecretkeyFactoryAlgoAvailable(String algorithmId) {
52+
try {
53+
SecretKeyFactory.getInstance(algorithmId);
54+
return true;
55+
} catch (NoSuchAlgorithmException e) {
56+
return false;
57+
}
58+
}
2859
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/PasswordHashingAlgorithmBootstrapCheck.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/Security.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ public Security(Settings settings, final Path configPath) {
300300
new TokenSSLBootstrapCheck(),
301301
new PkiRealmBootstrapCheck(getSslService()),
302302
new TLSLicenseBootstrapCheck(),
303-
new PasswordHashingAlgorithmBootstrapCheck(),
304303
new FIPS140SecureSettingsBootstrapCheck(settings, env),
305304
new FIPS140JKSKeystoreBootstrapCheck(settings),
306305
new FIPS140PasswordHashingAlgorithmBootstrapCheck(settings)));

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/PasswordHashingAlgorithmBootstrapCheckTests.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void testInvalidHashingAlgorithmFails() {
8383
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> new ReservedRealm(mock(Environment.class),
8484
invalidSettings, usersStore, new AnonymousUser(Settings.EMPTY), securityIndex, threadPool));
8585
assertThat(exception.getMessage(), containsString(invalidAlgoId));
86-
assertThat(exception.getMessage(), containsString("Only pbkdf2 or bcrypt family algorithms can be used for password hashing"));
86+
assertThat(exception.getMessage(), containsString("Invalid algorithm"));
8787
}
8888

8989
public void testReservedUserEmptyPasswordAuthenticationFails() throws Throwable {

0 commit comments

Comments
 (0)