Skip to content

Commit cd1bec3

Browse files
authored
[refactor] add Environment in BootstrapContext (#36573)
There are certain BootstrapCheck checks that may need access environment-specific values. Watcher's EncryptSensitiveDataBootstrapCheck passes in the node's environment via a constructor to bypass the shortcoming in BootstrapContext. This commit pulls in the node's environment into BootstrapContext. Another case is found in #36519, where it is useful to check the state of the data-path. Since PathUtils.get and Paths.get are forbidden APIs, we rely on the environment to retrieve references to things like node data paths. This means that the BootstrapContext will have the same Settings used in the Environment, which currently differs from the Node's settings.
1 parent d40037c commit cd1bec3

File tree

24 files changed

+201
-176
lines changed

24 files changed

+201
-176
lines changed

qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilBootstrapChecksTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121

2222
import org.apache.logging.log4j.Logger;
2323
import org.elasticsearch.common.SuppressForbidden;
24-
import org.elasticsearch.common.settings.Settings;
2524
import org.elasticsearch.node.NodeValidationException;
26-
import org.elasticsearch.test.ESTestCase;
25+
import org.elasticsearch.test.AbstractBootstrapCheckTestCase;
2726
import org.hamcrest.Matcher;
2827
import org.junit.After;
2928
import org.junit.Before;
@@ -40,7 +39,7 @@
4039
import static org.mockito.Mockito.verify;
4140
import static org.mockito.Mockito.verifyNoMoreInteractions;
4241

43-
public class EvilBootstrapChecksTests extends ESTestCase {
42+
public class EvilBootstrapChecksTests extends AbstractBootstrapCheckTestCase {
4443

4544
private String esEnforceBootstrapChecks = System.getProperty(ES_ENFORCE_BOOTSTRAP_CHECKS);
4645

@@ -65,7 +64,7 @@ public void testEnforceBootstrapChecks() throws NodeValidationException {
6564

6665
final NodeValidationException e = expectThrows(
6766
NodeValidationException.class,
68-
() -> BootstrapChecks.check(new BootstrapContext(Settings.EMPTY, null), false, checks, logger));
67+
() -> BootstrapChecks.check(emptyContext, false, checks, logger));
6968
final Matcher<String> allOf =
7069
allOf(containsString("bootstrap checks failed"), containsString("error"));
7170
assertThat(e, hasToString(allOf));
@@ -77,7 +76,7 @@ public void testNonEnforcedBootstrapChecks() throws NodeValidationException {
7776
setEsEnforceBootstrapChecks(null);
7877
final Logger logger = mock(Logger.class);
7978
// nothing should happen
80-
BootstrapChecks.check(new BootstrapContext(Settings.EMPTY, null), false, emptyList(), logger);
79+
BootstrapChecks.check(emptyContext, false, emptyList(), logger);
8180
verifyNoMoreInteractions(logger);
8281
}
8382

@@ -87,7 +86,7 @@ public void testInvalidValue() {
8786
final boolean enforceLimits = randomBoolean();
8887
final IllegalArgumentException e = expectThrows(
8988
IllegalArgumentException.class,
90-
() -> BootstrapChecks.check(new BootstrapContext(Settings.EMPTY, null), enforceLimits, emptyList()));
89+
() -> BootstrapChecks.check(emptyContext, enforceLimits, emptyList()));
9190
final Matcher<String> matcher = containsString(
9291
"[es.enforce.bootstrap.checks] must be [true] but was [" + value + "]");
9392
assertThat(e, hasToString(matcher));

server/src/main/java/org/elasticsearch/bootstrap/BootstrapChecks.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void check(final BootstrapContext context, final BoundTransportAddress bo
8181
final List<BootstrapCheck> combinedChecks = new ArrayList<>(builtInChecks);
8282
combinedChecks.addAll(additionalChecks);
8383
check( context,
84-
enforceLimits(boundTransportAddress, DiscoveryModule.DISCOVERY_TYPE_SETTING.get(context.settings)),
84+
enforceLimits(boundTransportAddress, DiscoveryModule.DISCOVERY_TYPE_SETTING.get(context.settings())),
8585
Collections.unmodifiableList(combinedChecks));
8686
}
8787

@@ -302,7 +302,7 @@ static class MlockallCheck implements BootstrapCheck {
302302

303303
@Override
304304
public BootstrapCheckResult check(BootstrapContext context) {
305-
if (BootstrapSettings.MEMORY_LOCK_SETTING.get(context.settings) && !isMemoryLocked()) {
305+
if (BootstrapSettings.MEMORY_LOCK_SETTING.get(context.settings()) && !isMemoryLocked()) {
306306
return BootstrapCheckResult.failure("memory locking requested for elasticsearch process but memory is not locked");
307307
} else {
308308
return BootstrapCheckResult.success();
@@ -408,7 +408,7 @@ static class MaxMapCountCheck implements BootstrapCheck {
408408
@Override
409409
public BootstrapCheckResult check(final BootstrapContext context) {
410410
// we only enforce the check if mmapfs is an allowed store type
411-
if (IndexModule.NODE_STORE_ALLOW_MMAPFS.get(context.settings)) {
411+
if (IndexModule.NODE_STORE_ALLOW_MMAPFS.get(context.settings())) {
412412
if (getMaxMapCount() != -1 && getMaxMapCount() < LIMIT) {
413413
final String message = String.format(
414414
Locale.ROOT,
@@ -525,7 +525,7 @@ static class SystemCallFilterCheck implements BootstrapCheck {
525525

526526
@Override
527527
public BootstrapCheckResult check(BootstrapContext context) {
528-
if (BootstrapSettings.SYSTEM_CALL_FILTER_SETTING.get(context.settings) && !isSystemCallFilterInstalled()) {
528+
if (BootstrapSettings.SYSTEM_CALL_FILTER_SETTING.get(context.settings()) && !isSystemCallFilterInstalled()) {
529529
final String message = "system call filters failed to install; " +
530530
"check the logs and fix your configuration or disable system call filters at your own risk";
531531
return BootstrapCheckResult.failure(message);
@@ -725,10 +725,10 @@ boolean isAllPermissionGranted() {
725725
static class DiscoveryConfiguredCheck implements BootstrapCheck {
726726
@Override
727727
public BootstrapCheckResult check(BootstrapContext context) {
728-
if (DiscoveryModule.ZEN2_DISCOVERY_TYPE.equals(DiscoveryModule.DISCOVERY_TYPE_SETTING.get(context.settings)) == false) {
728+
if (DiscoveryModule.ZEN2_DISCOVERY_TYPE.equals(DiscoveryModule.DISCOVERY_TYPE_SETTING.get(context.settings())) == false) {
729729
return BootstrapCheckResult.success();
730730
}
731-
if (ClusterBootstrapService.discoveryIsConfigured(context.settings)) {
731+
if (ClusterBootstrapService.discoveryIsConfigured(context.settings())) {
732732
return BootstrapCheckResult.success();
733733
}
734734

server/src/main/java/org/elasticsearch/bootstrap/BootstrapContext.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,36 @@
2020

2121
import org.elasticsearch.cluster.metadata.MetaData;
2222
import org.elasticsearch.common.settings.Settings;
23+
import org.elasticsearch.env.Environment;
2324

2425
/**
2526
* Context that is passed to every bootstrap check to make decisions on.
2627
*/
2728
public class BootstrapContext {
2829
/**
29-
* The nodes settings
30+
* The node's environment
3031
*/
31-
public final Settings settings;
32+
private final Environment environment;
33+
3234
/**
33-
* The nodes local state metadata loaded on startup
35+
* The node's local state metadata loaded on startup
3436
*/
35-
public final MetaData metaData;
37+
private final MetaData metaData;
3638

37-
public BootstrapContext(Settings settings, MetaData metaData) {
38-
this.settings = settings;
39+
public BootstrapContext(Environment environment, MetaData metaData) {
40+
this.environment = environment;
3941
this.metaData = metaData;
4042
}
43+
44+
public Environment environment() {
45+
return environment;
46+
}
47+
48+
public Settings settings() {
49+
return environment.settings();
50+
}
51+
52+
public MetaData metaData() {
53+
return metaData;
54+
}
4155
}

server/src/main/java/org/elasticsearch/node/Node.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ public Node start() throws NodeValidationException {
677677
onDiskMetadata = MetaData.EMPTY_META_DATA;
678678
}
679679
assert onDiskMetadata != null : "metadata is null but shouldn't"; // this is never null
680-
validateNodeBeforeAcceptingRequests(new BootstrapContext(settings, onDiskMetadata), transportService.boundAddress(), pluginsService
680+
validateNodeBeforeAcceptingRequests(new BootstrapContext(environment, onDiskMetadata), transportService.boundAddress(), pluginsService
681681
.filterPlugins(Plugin
682682
.class)
683683
.stream()

0 commit comments

Comments
 (0)