Skip to content

Commit

Permalink
Adjust Bootstrap and JVM options to ensure the SM is never used when …
Browse files Browse the repository at this point in the history
…entitlements are enabled (elastic#119689)
  • Loading branch information
ldematte authored Jan 8, 2025
1 parent 66f7c7b commit 3464adb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static List<String> systemJvmOptions(Settings nodeSettings, final Map<String, St
maybeSetActiveProcessorCount(nodeSettings),
maybeSetReplayFile(distroType, isHotspot),
maybeWorkaroundG1Bug(),
maybeAllowSecurityManager(),
maybeAllowSecurityManager(useEntitlements),
maybeAttachEntitlementAgent(useEntitlements)
).flatMap(s -> s).toList();
}
Expand Down Expand Up @@ -140,8 +140,8 @@ private static Stream<String> maybeWorkaroundG1Bug() {
}

@UpdateForV9(owner = UpdateForV9.Owner.CORE_INFRA)
private static Stream<String> maybeAllowSecurityManager() {
if (RuntimeVersionFeature.isSecurityManagerAvailable()) {
private static Stream<String> maybeAllowSecurityManager(boolean useEntitlements) {
if (useEntitlements == false && RuntimeVersionFeature.isSecurityManagerAvailable()) {
// Will become conditional on useEntitlements once entitlements can run without SM
return Stream.of("-Djava.security.manager=allow");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Bootstrap {

// arguments from the CLI process
private final ServerArgs args;
private final boolean useEntitlements;

// controller for spawning component subprocesses
private final Spawner spawner = new Spawner();
Expand All @@ -46,10 +47,11 @@ class Bootstrap {
// loads information about plugins required for entitlements in phase 2, used by plugins service in phase 3
private final SetOnce<PluginsLoader> pluginsLoader = new SetOnce<>();

Bootstrap(PrintStream out, PrintStream err, ServerArgs args) {
Bootstrap(PrintStream out, PrintStream err, ServerArgs args, boolean useEntitlements) {
this.out = out;
this.err = err;
this.args = args;
this.useEntitlements = useEntitlements;
}

ServerArgs args() {
Expand All @@ -60,6 +62,10 @@ Spawner spawner() {
return spawner;
}

public boolean useEntitlements() {
return useEntitlements;
}

void setSecureSettings(SecureSettings secureSettings) {
this.secureSettings.set(secureSettings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ static List<BootstrapCheck> checks() {
checks.add(new OnErrorCheck());
checks.add(new OnOutOfMemoryErrorCheck());
checks.add(new EarlyAccessCheck());
checks.add(new AllPermissionCheck());
checks.add(new DiscoveryConfiguredCheck());
checks.add(new ByteOrderCheck());
return Collections.unmodifiableList(checks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.nio.file.Path;
import java.security.Permission;
import java.security.Security;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -108,6 +109,7 @@ private static Bootstrap initPhase1() {
final PrintStream out = getStdout();
final PrintStream err = getStderr();
final ServerArgs args;
final boolean useEntitlements = Boolean.parseBoolean(System.getProperty("es.entitlements.enabled"));
try {
initSecurityProperties();

Expand All @@ -116,7 +118,7 @@ private static Bootstrap initPhase1() {
* the presence of a security manager or lack thereof act as if there is a security manager present (e.g., DNS cache policy).
* This forces such policies to take effect immediately.
*/
if (RuntimeVersionFeature.isSecurityManagerAvailable()) {
if (useEntitlements == false && RuntimeVersionFeature.isSecurityManagerAvailable()) {
org.elasticsearch.bootstrap.Security.setSecurityManager(new SecurityManager() {
@Override
public void checkPermission(Permission perm) {
Expand Down Expand Up @@ -149,7 +151,7 @@ public void checkPermission(Permission perm) {
return null; // unreachable, to satisfy compiler
}

return new Bootstrap(out, err, args);
return new Bootstrap(out, err, args, useEntitlements);
}

/**
Expand Down Expand Up @@ -214,7 +216,7 @@ private static void initPhase2(Bootstrap bootstrap) throws IOException {
var pluginsLoader = PluginsLoader.createPluginsLoader(nodeEnv.modulesFile(), nodeEnv.pluginsFile());
bootstrap.setPluginsLoader(pluginsLoader);

if (Boolean.parseBoolean(System.getProperty("es.entitlements.enabled"))) {
if (bootstrap.useEntitlements()) {
LogManager.getLogger(Elasticsearch.class).info("Bootstrapping Entitlements");

List<EntitlementBootstrap.PluginData> pluginData = Stream.concat(
Expand Down Expand Up @@ -280,7 +282,11 @@ protected void validateNodeBeforeAcceptingRequests(
final BoundTransportAddress boundTransportAddress,
List<BootstrapCheck> checks
) throws NodeValidationException {
BootstrapChecks.check(context, boundTransportAddress, checks);
var additionalChecks = new ArrayList<>(checks);
if (bootstrap.useEntitlements() == false) {
additionalChecks.add(new BootstrapChecks.AllPermissionCheck());
}
BootstrapChecks.check(context, boundTransportAddress, additionalChecks);
}
};
INSTANCE = new Elasticsearch(bootstrap.spawner(), node);
Expand Down

0 comments on commit 3464adb

Please sign in to comment.