-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce transport TLS on Basic with Security #42150
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import org.elasticsearch.bootstrap.BootstrapContext; | ||
import org.elasticsearch.license.License; | ||
import org.elasticsearch.license.LicenseService; | ||
import org.elasticsearch.license.XPackLicenseState; | ||
import org.elasticsearch.xpack.core.XPackSettings; | ||
|
||
/** | ||
|
@@ -19,10 +20,11 @@ public final class TLSLicenseBootstrapCheck implements BootstrapCheck { | |
public BootstrapCheckResult check(BootstrapContext context) { | ||
if (XPackSettings.TRANSPORT_SSL_ENABLED.get(context.settings()) == false) { | ||
License license = LicenseService.getLicense(context.metaData()); | ||
if (license != null && license.isProductionLicense()) { | ||
return BootstrapCheckResult.failure("Transport SSL must be enabled for setups with production licenses. Please set " + | ||
"[xpack.security.transport.ssl.enabled] to [true] or disable security by setting [xpack.security.enabled] " + | ||
"to [false]"); | ||
if (XPackLicenseState.isTransportTlsRequired(license, context.settings())) { | ||
return BootstrapCheckResult.failure("Transport SSL must be enabled if security is enabled on a [" + | ||
license.operationMode().description() + "] license. " + | ||
"Please set [xpack.security.transport.ssl.enabled] to [true] or disable security by setting " + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it also be enough to not explicitly set |
||
"[xpack.security.enabled] to [false]"); | ||
} | ||
} | ||
return BootstrapCheckResult.success(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,40 +5,115 @@ | |
*/ | ||
package org.elasticsearch.xpack.core.ssl; | ||
|
||
import org.elasticsearch.bootstrap.BootstrapCheck; | ||
import org.elasticsearch.bootstrap.BootstrapContext; | ||
import org.elasticsearch.cluster.metadata.MetaData; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.license.License; | ||
import org.elasticsearch.license.License.OperationMode; | ||
import org.elasticsearch.license.TestUtils; | ||
import org.elasticsearch.test.AbstractBootstrapCheckTestCase; | ||
|
||
import java.util.EnumSet; | ||
|
||
public class TLSLicenseBootstrapCheckTests extends AbstractBootstrapCheckTestCase { | ||
public void testBootstrapCheck() throws Exception { | ||
public void testBootstrapCheckOnEmptyMetadata() { | ||
assertTrue(new TLSLicenseBootstrapCheck().check(emptyContext).isSuccess()); | ||
assertTrue(new TLSLicenseBootstrapCheck().check(createTestContext(Settings.builder().put("xpack.security.transport.ssl.enabled" | ||
, randomBoolean()).build(), MetaData.EMPTY_META_DATA)).isSuccess()); | ||
int numIters = randomIntBetween(1,10); | ||
for (int i = 0; i < numIters; i++) { | ||
License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(24)); | ||
EnumSet<License.OperationMode> productionModes = EnumSet.of(License.OperationMode.GOLD, License.OperationMode.PLATINUM, | ||
License.OperationMode.STANDARD); | ||
MetaData.Builder builder = MetaData.builder(); | ||
TestUtils.putLicense(builder, license); | ||
MetaData build = builder.build(); | ||
if (productionModes.contains(license.operationMode()) == false) { | ||
assertTrue(new TLSLicenseBootstrapCheck().check(createTestContext( | ||
Settings.builder().put("xpack.security.transport.ssl.enabled", true).build(), build)).isSuccess()); | ||
} else { | ||
assertTrue(new TLSLicenseBootstrapCheck().check(createTestContext( | ||
Settings.builder().put("xpack.security.transport.ssl.enabled", false).build(), build)).isFailure()); | ||
assertEquals("Transport SSL must be enabled for setups with production licenses. Please set " + | ||
"[xpack.security.transport.ssl.enabled] to [true] or disable security by setting " + | ||
"[xpack.security.enabled] to [false]", | ||
new TLSLicenseBootstrapCheck().check(createTestContext( | ||
Settings.builder().put("xpack.security.transport.ssl.enabled", false).build(), build)).getMessage()); | ||
} | ||
, randomBoolean()).build(), MetaData.EMPTY_META_DATA)).isSuccess()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old method felt very much like it was using randomness to test a variety of scenarios, so I took the opportunity to split it out. |
||
|
||
public void testBootstrapCheckFailureOnPremiumLicense() throws Exception { | ||
final OperationMode mode = randomFrom(OperationMode.PLATINUM, OperationMode.GOLD, OperationMode.STANDARD); | ||
final Settings.Builder settings = Settings.builder(); | ||
if (randomBoolean()) { | ||
// randomise between default-false & explicit-false | ||
settings.put("xpack.security.transport.ssl.enabled", false); | ||
} | ||
if (randomBoolean()) { | ||
// randomise between default-true & explicit-true | ||
settings.put("xpack.security.enabled", true); | ||
} | ||
|
||
final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(mode, settings); | ||
assertTrue("Expected bootstrap failure", result.isFailure()); | ||
assertEquals("Transport SSL must be enabled if security is enabled on a [" + mode.description() + "] license. Please set " + | ||
"[xpack.security.transport.ssl.enabled] to [true] or disable security by setting " + | ||
"[xpack.security.enabled] to [false]", | ||
result.getMessage()); | ||
} | ||
|
||
public void testBootstrapCheckSucceedsWithTlsEnabledOnPremiumLicense() throws Exception { | ||
final OperationMode mode = randomFrom(OperationMode.PLATINUM, OperationMode.GOLD, OperationMode.STANDARD); | ||
final Settings.Builder settings = Settings.builder().put("xpack.security.transport.ssl.enabled", true); | ||
final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(mode, settings); | ||
assertSuccess(result); | ||
} | ||
|
||
public void testBootstrapCheckFailureOnBasicLicense() throws Exception { | ||
final Settings.Builder settings = Settings.builder().put("xpack.security.enabled", true); | ||
if (randomBoolean()) { | ||
// randomise between default-false & explicit-false | ||
settings.put("xpack.security.transport.ssl.enabled", false); | ||
} | ||
final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(OperationMode.BASIC, settings); | ||
assertTrue("Expected bootstrap failure", result.isFailure()); | ||
assertEquals("Transport SSL must be enabled if security is enabled on a [basic] license. Please set " + | ||
"[xpack.security.transport.ssl.enabled] to [true] or disable security by setting " + | ||
"[xpack.security.enabled] to [false]", | ||
result.getMessage()); | ||
} | ||
|
||
public void testBootstrapSucceedsIfSecurityIsNotEnabledOnBasicLicense() throws Exception { | ||
final Settings.Builder settings = Settings.builder(); | ||
if (randomBoolean()) { | ||
// randomise between default-false & explicit-false | ||
settings.put("xpack.security.enabled", false); | ||
} | ||
if (randomBoolean()) { | ||
// it does not matter whether or not this is set, as security is not enabled. | ||
settings.put("xpack.security.transport.ssl.enabled", randomBoolean()); | ||
} | ||
final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(OperationMode.BASIC, settings); | ||
assertSuccess(result); | ||
} | ||
|
||
public void testBootstrapSucceedsIfTlsIsEnabledOnBasicLicense() throws Exception { | ||
final Settings.Builder settings = Settings.builder().put("xpack.security.transport.ssl.enabled", true); | ||
if (randomBoolean()) { | ||
// it does not matter whether or not this is set, as TLS is enabled. | ||
settings.put("xpack.security.enabled", randomBoolean()); | ||
} | ||
final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(OperationMode.BASIC, settings); | ||
assertSuccess(result); | ||
} | ||
|
||
public void testBootstrapCheckAlwaysSucceedsOnTrialLicense() throws Exception { | ||
final Settings.Builder settings = Settings.builder(); | ||
if (randomBoolean()) { | ||
// it does not matter whether this is set, or to which value. | ||
settings.put("xpack.security.enabled", randomBoolean()); | ||
} | ||
if (randomBoolean()) { | ||
// it does not matter whether this is set, or to which value. | ||
settings.put("xpack.security.transport.ssl.enabled", randomBoolean()); | ||
} | ||
final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(OperationMode.TRIAL, settings); | ||
assertSuccess(result); | ||
} | ||
|
||
public BootstrapCheck.BootstrapCheckResult runBootstrapCheck(OperationMode mode, Settings.Builder settings) throws Exception { | ||
final License license = TestUtils.generateSignedLicense(mode.description(), TimeValue.timeValueHours(24)); | ||
MetaData.Builder builder = MetaData.builder(); | ||
TestUtils.putLicense(builder, license); | ||
MetaData metaData = builder.build(); | ||
final BootstrapContext context = createTestContext(settings.build(), metaData); | ||
return new TLSLicenseBootstrapCheck().check(context); | ||
} | ||
|
||
public void assertSuccess(BootstrapCheck.BootstrapCheckResult result) { | ||
if (result.isFailure()) { | ||
fail("Bootstrap check failed unexpectedly: " + result.getMessage()); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I replaced this with an explicit "is TLS required" method in XPackLicenseState.
I droppped the "production license" terminology, because it was a bit confusing (which is why this gap has only just been identified).
Why wasn't basic a production license before? Should it be one? It's perfectly fine to run basic in production, even though it doesn't have paid support.
I moved it from License to
XPackLicenseState
because