Skip to content

Commit

Permalink
Merge branch '8.x' into backport/8.x/pr-118353
Browse files Browse the repository at this point in the history
  • Loading branch information
john-wagster authored Dec 19, 2024
2 parents 310a563 + d9578c5 commit 600c6d2
Show file tree
Hide file tree
Showing 38 changed files with 933 additions and 395 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/118931.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 118931
summary: Add a `LicenseAware` interface for licensed Nodes
area: ES|QL
type: enhancement
issues:
- 117405
6 changes: 6 additions & 0 deletions docs/changelog/119097.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 119097
summary: "[8.x] Update data stream deprecations warnings to new format and filter\
\ sea…"
area: Data streams
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.net.URL;
import java.net.URLStreamHandlerFactory;
import java.util.List;

public interface EntitlementChecker {

Expand All @@ -29,4 +30,10 @@ public interface EntitlementChecker {
void check$java_net_URLClassLoader$(Class<?> callerClass, String name, URL[] urls, ClassLoader parent);

void check$java_net_URLClassLoader$(Class<?> callerClass, String name, URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory);

// Process creation
void check$$start(Class<?> callerClass, ProcessBuilder that, ProcessBuilder.Redirect[] redirects);

void check$java_lang_ProcessBuilder$startPipeline(Class<?> callerClass, List<ProcessBuilder> builders);

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,58 +29,74 @@
import java.util.stream.Collectors;

import static java.util.Map.entry;
import static org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction.CheckAction.deniedToPlugins;
import static org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction.CheckAction.forPlugins;
import static org.elasticsearch.rest.RestRequest.Method.GET;

public class RestEntitlementsCheckAction extends BaseRestHandler {
private static final Logger logger = LogManager.getLogger(RestEntitlementsCheckAction.class);
private final String prefix;

private record CheckAction(Runnable action, boolean isServerOnly) {

static CheckAction serverOnly(Runnable action) {
record CheckAction(Runnable action, boolean isAlwaysDeniedToPlugins) {
/**
* These cannot be granted to plugins, so our test plugins cannot test the "allowed" case.
* Used both for always-denied entitlements as well as those granted only to the server itself.
*/
static CheckAction deniedToPlugins(Runnable action) {
return new CheckAction(action, true);
}

static CheckAction serverAndPlugin(Runnable action) {
static CheckAction forPlugins(Runnable action) {
return new CheckAction(action, false);
}
}

private static final Map<String, CheckAction> checkActions = Map.ofEntries(
entry("runtime_exit", CheckAction.serverOnly(RestEntitlementsCheckAction::runtimeExit)),
entry("runtime_halt", CheckAction.serverOnly(RestEntitlementsCheckAction::runtimeHalt)),
entry("create_classloader", CheckAction.serverAndPlugin(RestEntitlementsCheckAction::createClassLoader))
entry("runtime_exit", deniedToPlugins(RestEntitlementsCheckAction::runtimeExit)),
entry("runtime_halt", deniedToPlugins(RestEntitlementsCheckAction::runtimeHalt)),
entry("create_classloader", forPlugins(RestEntitlementsCheckAction::createClassLoader)),
// entry("processBuilder_start", deniedToPlugins(RestEntitlementsCheckAction::processBuilder_start)),
entry("processBuilder_startPipeline", deniedToPlugins(RestEntitlementsCheckAction::processBuilder_startPipeline))
);

@SuppressForbidden(reason = "Specifically testing Runtime.exit")
private static void runtimeExit() {
logger.info("Calling Runtime.exit;");
Runtime.getRuntime().exit(123);
}

@SuppressForbidden(reason = "Specifically testing Runtime.halt")
private static void runtimeHalt() {
logger.info("Calling Runtime.halt;");
Runtime.getRuntime().halt(123);
}

private static void createClassLoader() {
logger.info("Calling new URLClassLoader");
try (var classLoader = new URLClassLoader("test", new URL[0], RestEntitlementsCheckAction.class.getClassLoader())) {
logger.info("Created URLClassLoader [{}]", classLoader.getName());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static void processBuilder_start() {
// TODO: processBuilder().start();
}

private static void processBuilder_startPipeline() {
try {
ProcessBuilder.startPipeline(List.of());
} catch (IOException e) {
throw new IllegalStateException(e);
}
}

public RestEntitlementsCheckAction(String prefix) {
this.prefix = prefix;
}

public static Set<String> getServerAndPluginsCheckActions() {
return checkActions.entrySet()
.stream()
.filter(kv -> kv.getValue().isServerOnly() == false)
.filter(kv -> kv.getValue().isAlwaysDeniedToPlugins() == false)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}
Expand Down Expand Up @@ -112,6 +128,7 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
}

return channel -> {
logger.info("Calling check action [{}]", actionName);
checkAction.action().run();
channel.sendResponse(new RestResponse(RestStatus.OK, Strings.format("Succesfully executed action [%s]", actionName)));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.function.Supplier;

public class EntitlementAllowedNonModularPlugin extends Plugin implements ActionPlugin {

@Override
public List<RestHandler> getRestHandlers(
final Settings settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.function.Supplier;

public class EntitlementAllowedPlugin extends Plugin implements ActionPlugin {

@Override
public List<RestHandler> getRestHandlers(
final Settings settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.function.Supplier;

public class EntitlementDeniedNonModularPlugin extends Plugin implements ActionPlugin {

@Override
public List<RestHandler> getRestHandlers(
final Settings settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.function.Supplier;

public class EntitlementDeniedPlugin extends Plugin implements ActionPlugin {

@Override
public List<RestHandler> getRestHandlers(
final Settings settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.net.URL;
import java.net.URLStreamHandlerFactory;
import java.util.List;

/**
* Implementation of the {@link EntitlementChecker} interface, providing additional
Expand Down Expand Up @@ -67,4 +68,14 @@ public ElasticsearchEntitlementChecker(PolicyManager policyManager) {
) {
policyManager.checkCreateClassLoader(callerClass);
}

@Override
public void check$$start(Class<?> callerClass, ProcessBuilder processBuilder, ProcessBuilder.Redirect[] redirects) {
policyManager.checkStartProcess(callerClass);
}

@Override
public void check$java_lang_ProcessBuilder$startPipeline(Class<?> callerClass, List<ProcessBuilder> builders) {
policyManager.checkStartProcess(callerClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ private static Map<String, List<Entitlement>> buildScopeEntitlementsMap(Policy p
return policy.scopes.stream().collect(Collectors.toUnmodifiableMap(scope -> scope.name, scope -> scope.entitlements));
}

public void checkStartProcess(Class<?> callerClass) {
neverEntitled(callerClass, "start process");
}

private void neverEntitled(Class<?> callerClass, String operationDescription) {
var requestingModule = requestingModule(callerClass);
if (isTriviallyAllowed(requestingModule)) {
return;
}

throw new NotEntitledException(
Strings.format(
"Not entitled: caller [%s], module [%s], operation [%s]",
callerClass,
requestingModule.getName(),
operationDescription
)
);
}

public void checkExitVM(Class<?> callerClass) {
checkEntitlementPresent(callerClass, ExitVMEntitlement.class);
}
Expand Down
Loading

0 comments on commit 600c6d2

Please sign in to comment.