|
3 | 3 | * or more contributor license agreements. Licensed under the Elastic License;
|
4 | 4 | * you may not use this file except in compliance with the Elastic License.
|
5 | 5 | */
|
| 6 | + |
6 | 7 | package org.elasticsearch.xpack.security;
|
7 | 8 |
|
| 9 | +import org.elasticsearch.bootstrap.BootstrapCheck; |
8 | 10 | import org.elasticsearch.bootstrap.BootstrapContext;
|
9 | 11 | import org.elasticsearch.common.settings.Settings;
|
10 | 12 | import org.elasticsearch.test.ESTestCase;
|
11 | 13 | import org.elasticsearch.xpack.core.XPackSettings;
|
12 | 14 |
|
| 15 | +import java.util.Arrays; |
| 16 | + |
| 17 | +import static org.hamcrest.Matchers.equalTo; |
| 18 | + |
13 | 19 | public class FIPS140PasswordHashingAlgorithmBootstrapCheckTests extends ESTestCase {
|
14 | 20 |
|
15 | 21 | 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 | + } |
17 | 31 |
|
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 | + } |
23 | 41 | }
|
24 | 42 |
|
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 | + } |
30 | 50 |
|
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)); |
33 | 60 | }
|
| 61 | + |
34 | 62 | }
|
0 commit comments