|
5 | 5 | */
|
6 | 6 | package org.elasticsearch.xpack.core.ssl;
|
7 | 7 |
|
| 8 | +import org.elasticsearch.bootstrap.BootstrapCheck; |
| 9 | +import org.elasticsearch.bootstrap.BootstrapContext; |
8 | 10 | import org.elasticsearch.cluster.metadata.MetaData;
|
9 | 11 | import org.elasticsearch.common.settings.Settings;
|
10 | 12 | import org.elasticsearch.common.unit.TimeValue;
|
11 | 13 | import org.elasticsearch.license.License;
|
| 14 | +import org.elasticsearch.license.License.OperationMode; |
12 | 15 | import org.elasticsearch.license.TestUtils;
|
13 | 16 | import org.elasticsearch.test.AbstractBootstrapCheckTestCase;
|
14 | 17 |
|
15 |
| -import java.util.EnumSet; |
16 |
| - |
17 | 18 | public class TLSLicenseBootstrapCheckTests extends AbstractBootstrapCheckTestCase {
|
18 |
| - public void testBootstrapCheck() throws Exception { |
| 19 | + public void testBootstrapCheckOnEmptyMetadata() { |
19 | 20 | assertTrue(new TLSLicenseBootstrapCheck().check(emptyContext).isSuccess());
|
20 | 21 | assertTrue(new TLSLicenseBootstrapCheck().check(createTestContext(Settings.builder().put("xpack.security.transport.ssl.enabled"
|
21 |
| - , randomBoolean()).build(), MetaData.EMPTY_META_DATA)).isSuccess()); |
22 |
| - int numIters = randomIntBetween(1,10); |
23 |
| - for (int i = 0; i < numIters; i++) { |
24 |
| - License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(24)); |
25 |
| - EnumSet<License.OperationMode> productionModes = EnumSet.of(License.OperationMode.GOLD, License.OperationMode.PLATINUM, |
26 |
| - License.OperationMode.STANDARD); |
27 |
| - MetaData.Builder builder = MetaData.builder(); |
28 |
| - TestUtils.putLicense(builder, license); |
29 |
| - MetaData build = builder.build(); |
30 |
| - if (productionModes.contains(license.operationMode()) == false) { |
31 |
| - assertTrue(new TLSLicenseBootstrapCheck().check(createTestContext( |
32 |
| - Settings.builder().put("xpack.security.transport.ssl.enabled", true).build(), build)).isSuccess()); |
33 |
| - } else { |
34 |
| - assertTrue(new TLSLicenseBootstrapCheck().check(createTestContext( |
35 |
| - Settings.builder().put("xpack.security.transport.ssl.enabled", false).build(), build)).isFailure()); |
36 |
| - assertEquals("Transport SSL must be enabled for setups with production licenses. Please set " + |
37 |
| - "[xpack.security.transport.ssl.enabled] to [true] or disable security by setting " + |
38 |
| - "[xpack.security.enabled] to [false]", |
39 |
| - new TLSLicenseBootstrapCheck().check(createTestContext( |
40 |
| - Settings.builder().put("xpack.security.transport.ssl.enabled", false).build(), build)).getMessage()); |
41 |
| - } |
| 22 | + , randomBoolean()).build(), MetaData.EMPTY_META_DATA)).isSuccess()); |
| 23 | + } |
| 24 | + |
| 25 | + public void testBootstrapCheckFailureOnPremiumLicense() throws Exception { |
| 26 | + final OperationMode mode = randomFrom(OperationMode.PLATINUM, OperationMode.GOLD, OperationMode.STANDARD); |
| 27 | + final Settings.Builder settings = Settings.builder(); |
| 28 | + if (randomBoolean()) { |
| 29 | + // randomise between default-false & explicit-false |
| 30 | + settings.put("xpack.security.transport.ssl.enabled", false); |
| 31 | + } |
| 32 | + if (randomBoolean()) { |
| 33 | + // randomise between default-true & explicit-true |
| 34 | + settings.put("xpack.security.enabled", true); |
| 35 | + } |
| 36 | + |
| 37 | + final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(mode, settings); |
| 38 | + assertTrue("Expected bootstrap failure", result.isFailure()); |
| 39 | + assertEquals("Transport SSL must be enabled if security is enabled on a [" + mode.description() + "] license. Please set " + |
| 40 | + "[xpack.security.transport.ssl.enabled] to [true] or disable security by setting " + |
| 41 | + "[xpack.security.enabled] to [false]", |
| 42 | + result.getMessage()); |
| 43 | + } |
| 44 | + |
| 45 | + public void testBootstrapCheckSucceedsWithTlsEnabledOnPremiumLicense() throws Exception { |
| 46 | + final OperationMode mode = randomFrom(OperationMode.PLATINUM, OperationMode.GOLD, OperationMode.STANDARD); |
| 47 | + final Settings.Builder settings = Settings.builder().put("xpack.security.transport.ssl.enabled", true); |
| 48 | + final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(mode, settings); |
| 49 | + assertSuccess(result); |
| 50 | + } |
| 51 | + |
| 52 | + public void testBootstrapCheckFailureOnBasicLicense() throws Exception { |
| 53 | + final Settings.Builder settings = Settings.builder().put("xpack.security.enabled", true); |
| 54 | + if (randomBoolean()) { |
| 55 | + // randomise between default-false & explicit-false |
| 56 | + settings.put("xpack.security.transport.ssl.enabled", false); |
| 57 | + } |
| 58 | + final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(OperationMode.BASIC, settings); |
| 59 | + assertTrue("Expected bootstrap failure", result.isFailure()); |
| 60 | + assertEquals("Transport SSL must be enabled if security is enabled on a [basic] license. Please set " + |
| 61 | + "[xpack.security.transport.ssl.enabled] to [true] or disable security by setting " + |
| 62 | + "[xpack.security.enabled] to [false]", |
| 63 | + result.getMessage()); |
| 64 | + } |
| 65 | + |
| 66 | + public void testBootstrapSucceedsIfSecurityIsNotEnabledOnBasicLicense() throws Exception { |
| 67 | + final Settings.Builder settings = Settings.builder(); |
| 68 | + if (randomBoolean()) { |
| 69 | + // randomise between default-false & explicit-false |
| 70 | + settings.put("xpack.security.enabled", false); |
| 71 | + } |
| 72 | + if (randomBoolean()) { |
| 73 | + // it does not matter whether or not this is set, as security is not enabled. |
| 74 | + settings.put("xpack.security.transport.ssl.enabled", randomBoolean()); |
42 | 75 | }
|
| 76 | + final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(OperationMode.BASIC, settings); |
| 77 | + assertSuccess(result); |
43 | 78 | }
|
| 79 | + |
| 80 | + public void testBootstrapSucceedsIfTlsIsEnabledOnBasicLicense() throws Exception { |
| 81 | + final Settings.Builder settings = Settings.builder().put("xpack.security.transport.ssl.enabled", true); |
| 82 | + if (randomBoolean()) { |
| 83 | + // it does not matter whether or not this is set, as TLS is enabled. |
| 84 | + settings.put("xpack.security.enabled", randomBoolean()); |
| 85 | + } |
| 86 | + final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(OperationMode.BASIC, settings); |
| 87 | + assertSuccess(result); |
| 88 | + } |
| 89 | + |
| 90 | + public void testBootstrapCheckAlwaysSucceedsOnTrialLicense() throws Exception { |
| 91 | + final Settings.Builder settings = Settings.builder(); |
| 92 | + if (randomBoolean()) { |
| 93 | + // it does not matter whether this is set, or to which value. |
| 94 | + settings.put("xpack.security.enabled", randomBoolean()); |
| 95 | + } |
| 96 | + if (randomBoolean()) { |
| 97 | + // it does not matter whether this is set, or to which value. |
| 98 | + settings.put("xpack.security.transport.ssl.enabled", randomBoolean()); |
| 99 | + } |
| 100 | + final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(OperationMode.TRIAL, settings); |
| 101 | + assertSuccess(result); |
| 102 | + } |
| 103 | + |
| 104 | + public BootstrapCheck.BootstrapCheckResult runBootstrapCheck(OperationMode mode, Settings.Builder settings) throws Exception { |
| 105 | + final License license = TestUtils.generateSignedLicense(mode.description(), TimeValue.timeValueHours(24)); |
| 106 | + MetaData.Builder builder = MetaData.builder(); |
| 107 | + TestUtils.putLicense(builder, license); |
| 108 | + MetaData metaData = builder.build(); |
| 109 | + final BootstrapContext context = createTestContext(settings.build(), metaData); |
| 110 | + return new TLSLicenseBootstrapCheck().check(context); |
| 111 | + } |
| 112 | + |
| 113 | + public void assertSuccess(BootstrapCheck.BootstrapCheckResult result) { |
| 114 | + if (result.isFailure()) { |
| 115 | + fail("Bootstrap check failed unexpectedly: " + result.getMessage()); |
| 116 | + } |
| 117 | + } |
| 118 | + |
44 | 119 | }
|
0 commit comments