-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Startup check for security implicit behavior change #76879
Changes from all commits
f2bbff6
1ced464
0010034
b4b713c
cc472a2
b890c25
66d09eb
a8297da
a7f6421
2eba1d7
28491b6
79da522
1180f45
399fc96
80f893f
2acbd38
12bcde6
fa33a56
7bcec6c
1006559
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.security; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.bootstrap.BootstrapCheck; | ||
import org.elasticsearch.bootstrap.BootstrapContext; | ||
import org.elasticsearch.env.NodeMetadata; | ||
import org.elasticsearch.license.License; | ||
import org.elasticsearch.license.LicenseService; | ||
import org.elasticsearch.xpack.core.XPackSettings; | ||
|
||
public class SecurityImplicitBehaviorBootstrapCheck implements BootstrapCheck { | ||
|
||
private final NodeMetadata nodeMetadata; | ||
|
||
public SecurityImplicitBehaviorBootstrapCheck(NodeMetadata nodeMetadata) { | ||
this.nodeMetadata = nodeMetadata; | ||
} | ||
|
||
@Override | ||
public BootstrapCheckResult check(BootstrapContext context) { | ||
if (nodeMetadata == null) { | ||
return BootstrapCheckResult.success(); | ||
} | ||
final License license = LicenseService.getLicense(context.metadata()); | ||
final Version lastKnownVersion = nodeMetadata.previousNodeVersion(); | ||
// pre v7.2.0 nodes have Version.EMPTY and its id is 0, so Version#before handles this successfully | ||
if (lastKnownVersion.before(Version.V_8_0_0) | ||
&& XPackSettings.SECURITY_ENABLED.exists(context.settings()) == false | ||
&& (license.operationMode() == License.OperationMode.BASIC || license.operationMode() == License.OperationMode.TRIAL)) { | ||
return BootstrapCheckResult.failure( | ||
"The default value for [" | ||
+ XPackSettings.SECURITY_ENABLED.getKey() | ||
+ "] has changed in the current version. " | ||
+ " Security features were implicitly disabled for this node but they would now be enabled, possibly" | ||
+ " preventing access to the node. " | ||
+ "See https://www.elastic.co/guide/en/elasticsearch/reference/" | ||
+ Version.CURRENT.major | ||
+ "." | ||
+ Version.CURRENT.minor | ||
+ "/security-minimal-setup.html to configure security, or explicitly disable security by " | ||
+ "setting [xpack.security.enabled] to \"false\" in elasticsearch.yml before restarting the node." | ||
); | ||
} else { | ||
return BootstrapCheckResult.success(); | ||
} | ||
} | ||
|
||
public boolean alwaysEnforce() { | ||
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. I don't love this always enabled Bootstrap check, but this is currently the only way for us to make a check on node startup that has a view ( albeit limited ) to the restored cluster state ( via the BootstrapContext ) 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. I'd forgotten that we expose the metadata read from disk like this, but I think this is fine - at least it's no worse than any of the other places that make decisions based on the contents of the on-disk cluster state despite the fact that this could be stale or even uncommitted. |
||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.security; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.bootstrap.BootstrapCheck; | ||
import org.elasticsearch.cluster.metadata.Metadata; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.core.TimeValue; | ||
import org.elasticsearch.env.NodeMetadata; | ||
import org.elasticsearch.license.License; | ||
import org.elasticsearch.license.LicensesMetadata; | ||
import org.elasticsearch.license.TestUtils; | ||
import org.elasticsearch.test.AbstractBootstrapCheckTestCase; | ||
import org.elasticsearch.test.VersionUtils; | ||
import org.elasticsearch.xpack.core.XPackSettings; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class SecurityImplicitBehaviorBootstrapCheckTests extends AbstractBootstrapCheckTestCase { | ||
|
||
public void testFailureUpgradeFrom7xWithImplicitSecuritySettings() throws Exception { | ||
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. I think we need 2 methods:
The 2nd one seems to be missing. 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. makes sense, will add now 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. added in 2acbd38 |
||
final Version previousVersion = VersionUtils.randomVersionBetween(random(), Version.V_7_2_0, Version.V_7_16_0); | ||
NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion); | ||
nodeMetadata = nodeMetadata.upgradeToCurrentVersion(); | ||
BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata).check( | ||
createTestContext(Settings.EMPTY, createLicensesMetadata(previousVersion, randomFrom("basic", "trial"))) | ||
); | ||
assertThat(result.isFailure(), is(true)); | ||
assertThat( | ||
result.getMessage(), | ||
equalTo( | ||
"The default value for [" | ||
+ XPackSettings.SECURITY_ENABLED.getKey() | ||
+ "] has changed in the current version. " | ||
+ " Security features were implicitly disabled for this node but they would now be enabled, possibly" | ||
+ " preventing access to the node. " | ||
+ "See https://www.elastic.co/guide/en/elasticsearch/reference/" | ||
+ Version.CURRENT.major | ||
+ "." | ||
+ Version.CURRENT.minor | ||
+ "/security-minimal-setup.html to configure security, or explicitly disable security by " | ||
+ "setting [xpack.security.enabled] to \"false\" in elasticsearch.yml before restarting the node." | ||
) | ||
); | ||
} | ||
|
||
public void testUpgradeFrom7xWithImplicitSecuritySettingsOnGoldPlus() throws Exception { | ||
final Version previousVersion = VersionUtils.randomVersionBetween(random(), Version.V_7_2_0, Version.V_7_16_0); | ||
NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion); | ||
nodeMetadata = nodeMetadata.upgradeToCurrentVersion(); | ||
BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata).check( | ||
createTestContext(Settings.EMPTY, createLicensesMetadata(previousVersion, randomFrom("gold", "platinum"))) | ||
); | ||
assertThat(result.isSuccess(), is(true)); | ||
} | ||
|
||
public void testUpgradeFrom7xWithExplicitSecuritySettings() throws Exception { | ||
final Version previousVersion = VersionUtils.randomVersionBetween(random(), Version.V_7_2_0, Version.V_7_16_0); | ||
NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion); | ||
nodeMetadata = nodeMetadata.upgradeToCurrentVersion(); | ||
BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata).check( | ||
createTestContext( | ||
Settings.builder().put(XPackSettings.SECURITY_ENABLED.getKey(), true).build(), | ||
createLicensesMetadata(previousVersion, randomFrom("basic", "trial")) | ||
) | ||
); | ||
assertThat(result.isSuccess(), is(true)); | ||
} | ||
|
||
public void testUpgradeFrom8xWithImplicitSecuritySettings() throws Exception { | ||
final Version previousVersion = VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, null); | ||
NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion); | ||
nodeMetadata = nodeMetadata.upgradeToCurrentVersion(); | ||
BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata).check( | ||
createTestContext(Settings.EMPTY, createLicensesMetadata(previousVersion, randomFrom("basic", "trial"))) | ||
); | ||
assertThat(result.isSuccess(), is(true)); | ||
} | ||
|
||
public void testUpgradeFrom8xWithExplicitSecuritySettings() throws Exception { | ||
final Version previousVersion = VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, null); | ||
NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion); | ||
nodeMetadata = nodeMetadata.upgradeToCurrentVersion(); | ||
BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata).check( | ||
createTestContext( | ||
Settings.builder().put(XPackSettings.SECURITY_ENABLED.getKey(), true).build(), | ||
createLicensesMetadata(previousVersion, randomFrom("basic", "trial")) | ||
) | ||
); | ||
assertThat(result.isSuccess(), is(true)); | ||
} | ||
|
||
private Metadata createLicensesMetadata(Version version, String licenseMode) throws Exception { | ||
License license = TestUtils.generateSignedLicense(licenseMode, TimeValue.timeValueHours(2)); | ||
return Metadata.builder().putCustom(LicensesMetadata.TYPE, new LicensesMetadata(license, version)).build(); | ||
} | ||
} |
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 know I'm fighting against the existing conventions of this class, but is it possible to get some sort of javadoc here?
What does
previous
mean exactly? I think it's "last time the node started" (or more accurately "the version of the metadata that was read from disk") ... but I'm sure there could be all sorts of nuace in rolling upgrades, master elections, etc, and I'd like to be able to consult javadocs so I can know how to reason about that.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.
added in 80f893f, @DaveCTurner can keep me honest or suggest enhancements
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.
Docs LGTM 👍