Skip to content

Commit 06e462e

Browse files
committed
Only enforce password hashing check if FIPS enabled (#32383)
This commit modifies the FIPS password hashing algorithm check to only be executed if FIPS mode is enabled.
1 parent 422da32 commit 06e462e

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class FIPS140PasswordHashingAlgorithmBootstrapCheck implements BootstrapC
1616

1717
private final boolean fipsModeEnabled;
1818

19-
FIPS140PasswordHashingAlgorithmBootstrapCheck(Settings settings) {
19+
FIPS140PasswordHashingAlgorithmBootstrapCheck(final Settings settings) {
2020
this.fipsModeEnabled = Security.FIPS_MODE_ENABLED.get(settings);
2121
}
2222

@@ -27,17 +27,15 @@ public class FIPS140PasswordHashingAlgorithmBootstrapCheck implements BootstrapC
2727
* @return the result of the bootstrap check
2828
*/
2929
@Override
30-
public BootstrapCheckResult check(BootstrapContext context) {
31-
final String selectedAlgorithm = XPackSettings.PASSWORD_HASHING_ALGORITHM.get(context.settings);
32-
if (selectedAlgorithm.toLowerCase(Locale.ROOT).startsWith("pbkdf2") == false) {
33-
return BootstrapCheckResult.failure("Only PBKDF2 is allowed for password hashing in a FIPS-140 JVM. Please set the " +
34-
"appropriate value for [ " + XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey() + " ] setting.");
30+
public BootstrapCheckResult check(final BootstrapContext context) {
31+
if (fipsModeEnabled) {
32+
final String selectedAlgorithm = XPackSettings.PASSWORD_HASHING_ALGORITHM.get(context.settings);
33+
if (selectedAlgorithm.toLowerCase(Locale.ROOT).startsWith("pbkdf2") == false) {
34+
return BootstrapCheckResult.failure("Only PBKDF2 is allowed for password hashing in a FIPS-140 JVM. Please set the " +
35+
"appropriate value for [ " + XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey() + " ] setting.");
36+
}
3537
}
3638
return BootstrapCheckResult.success();
3739
}
3840

39-
@Override
40-
public boolean alwaysEnforce() {
41-
return fipsModeEnabled;
42-
}
4341
}

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

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,60 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6+
67
package org.elasticsearch.xpack.security;
78

9+
import org.elasticsearch.bootstrap.BootstrapCheck;
810
import org.elasticsearch.bootstrap.BootstrapContext;
911
import org.elasticsearch.common.settings.Settings;
1012
import org.elasticsearch.test.ESTestCase;
1113
import org.elasticsearch.xpack.core.XPackSettings;
1214

15+
import java.util.Arrays;
16+
17+
import static org.hamcrest.Matchers.equalTo;
18+
1319
public class FIPS140PasswordHashingAlgorithmBootstrapCheckTests extends ESTestCase {
1420

1521
public void testPBKDF2AlgorithmIsAllowed() {
16-
Settings settings = Settings.builder().put("xpack.security.fips_mode.enabled", "true").build();
22+
{
23+
final Settings settings = Settings.builder()
24+
.put(Security.FIPS_MODE_ENABLED.getKey(), true)
25+
.put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), "PBKDF2_10000")
26+
.build();
27+
final BootstrapCheck.BootstrapCheckResult result =
28+
new FIPS140PasswordHashingAlgorithmBootstrapCheck(settings).check(new BootstrapContext(settings, null));
29+
assertFalse(result.isFailure());
30+
}
1731

18-
settings = Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), "PBKDF2_10000").build();
19-
assertFalse(new FIPS140PasswordHashingAlgorithmBootstrapCheck(settings).check(new BootstrapContext(settings, null)).isFailure());
20-
21-
settings = Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), "PBKDF2").build();
22-
assertFalse(new FIPS140PasswordHashingAlgorithmBootstrapCheck(settings).check(new BootstrapContext(settings, null)).isFailure());
32+
{
33+
final Settings settings = Settings.builder()
34+
.put(Security.FIPS_MODE_ENABLED.getKey(), true)
35+
.put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), "PBKDF2")
36+
.build();
37+
final BootstrapCheck.BootstrapCheckResult result =
38+
new FIPS140PasswordHashingAlgorithmBootstrapCheck(settings).check(new BootstrapContext(settings, null));
39+
assertFalse(result.isFailure());
40+
}
2341
}
2442

25-
public void testBCRYPTAlgorithmIsNotAllowed() {
26-
Settings settings = Settings.builder().put("xpack.security.fips_mode.enabled", "true").build();
27-
assertTrue(new FIPS140PasswordHashingAlgorithmBootstrapCheck(settings).check(new BootstrapContext(settings, null)).isFailure());
28-
settings = Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), "BCRYPT").build();
29-
assertTrue(new FIPS140PasswordHashingAlgorithmBootstrapCheck(settings).check(new BootstrapContext(settings, null)).isFailure());
43+
public void testBCRYPTAlgorithmDependsOnFipsMode() {
44+
for (final Boolean fipsModeEnabled : Arrays.asList(true, false)) {
45+
for (final String passwordHashingAlgorithm : Arrays.asList(null, "BCRYPT", "BCRYPT11")) {
46+
runBCRYPTTest(fipsModeEnabled, passwordHashingAlgorithm);
47+
}
48+
}
49+
}
3050

31-
settings = Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), "BCRYPT11").build();
32-
assertTrue(new FIPS140PasswordHashingAlgorithmBootstrapCheck(settings).check(new BootstrapContext(settings, null)).isFailure());
51+
private void runBCRYPTTest(final boolean fipsModeEnabled, final String passwordHashingAlgorithm) {
52+
final Settings.Builder builder = Settings.builder().put(Security.FIPS_MODE_ENABLED.getKey(), fipsModeEnabled);
53+
if (passwordHashingAlgorithm != null) {
54+
builder.put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), passwordHashingAlgorithm);
55+
}
56+
final Settings settings = builder.build();
57+
final BootstrapCheck.BootstrapCheckResult result =
58+
new FIPS140PasswordHashingAlgorithmBootstrapCheck(settings).check(new BootstrapContext(settings, null));
59+
assertThat(result.isFailure(), equalTo(fipsModeEnabled));
3360
}
61+
3462
}

0 commit comments

Comments
 (0)