Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.nio.charset.Charset;
import java.nio.file.LinkOption;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.attribute.UserPrincipal;
Expand Down Expand Up @@ -514,6 +515,8 @@ public interface EntitlementChecker {
void check$java_util_Scanner$(Class<?> callerClass, File source, Charset charset);

// nio
void check$java_nio_file_Files$$getOwner(Class<?> callerClass, Path path, LinkOption... options);

void check$java_nio_file_Files$$probeContentType(Class<?> callerClass, Path path);

void check$java_nio_file_Files$$setOwner(Class<?> callerClass, Path path, UserPrincipal principal);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ static void createFileOutputStreamFileWithAppend() throws IOException {
new FileOutputStream(readWriteFile().toFile(), false).close();
}

@EntitlementTest(expectedAccess = PLUGINS)
static void filesGetOwner() throws IOException {
Files.getOwner(readFile());
}

@EntitlementTest(expectedAccess = PLUGINS)
static void filesProbeContentType() throws IOException {
Files.probeContentType(readFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.entitlement.qa.EntitlementsTestRule.PolicyBuilder;
import org.elasticsearch.test.rest.ESRestTestCase;

import java.io.IOException;
Expand All @@ -22,7 +23,7 @@

public abstract class AbstractEntitlementsIT extends ESRestTestCase {

static final EntitlementsTestRule.PolicyBuilder ALLOWED_TEST_ENTITLEMENTS = (builder, tempDir) -> {
static final PolicyBuilder ALLOWED_TEST_ENTITLEMENTS = (builder, tempDir) -> {
builder.value("create_class_loader");
builder.value("set_https_connection_properties");
builder.value("inbound_network");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,27 @@
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

class EntitlementsTestRule implements TestRule {

// entitlements that test methods may use, see EntitledActions
private static final PolicyBuilder ENTITLED_POLICY = (builder, tempDir) -> {
builder.value(Map.of("write_system_properties", Map.of("properties", List.of("org.elasticsearch.entitlement.qa.selfTest"))));
builder.value(
Map.of(
"files",
List.of(
Map.of("path", tempDir.resolve("read_dir"), "mode", "read"),
Map.of("path", tempDir.resolve("read_write_dir"), "mode", "read_write"),
Map.of("path", tempDir.resolve("read_file"), "mode", "read"),
Map.of("path", tempDir.resolve("read_write_file"), "mode", "read_write")
)
)
);
};

interface PolicyBuilder {
void build(XContentBuilder builder, Path tempDir) throws IOException;
}
Expand All @@ -51,7 +69,7 @@ protected void before() throws Throwable {
}
};
cluster = ElasticsearchCluster.local()
.module("entitled")
.module("entitled", spec -> buildEntitlements(spec, "org.elasticsearch.entitlement.qa.entitled", ENTITLED_POLICY))
.module("entitlement-test-plugin", spec -> setupEntitlements(spec, modular, policyBuilder))
.systemProperty("es.entitlements.enabled", "true")
.systemProperty("es.entitlements.testdir", () -> testDir.getRoot().getAbsolutePath())
Expand All @@ -65,29 +83,30 @@ public Statement apply(Statement statement, Description description) {
return ruleChain.apply(statement, description);
}

private void setupEntitlements(PluginInstallSpec spec, boolean modular, PolicyBuilder policyBuilder) {
String moduleName = modular ? "org.elasticsearch.entitlement.qa.test" : "ALL-UNNAMED";
if (policyBuilder != null) {
spec.withEntitlementsOverride(old -> {
try {
try (var builder = YamlXContent.contentBuilder()) {
builder.startObject();
builder.field(moduleName);
builder.startArray();
private void buildEntitlements(PluginInstallSpec spec, String moduleName, PolicyBuilder policyBuilder) {
spec.withEntitlementsOverride(old -> {
try (var builder = YamlXContent.contentBuilder()) {
builder.startObject();
builder.field(moduleName);
builder.startArray();

policyBuilder.build(builder, testDir.getRoot().toPath());
builder.endArray();
builder.endObject();
policyBuilder.build(builder, testDir.getRoot().toPath());
builder.endArray();
builder.endObject();

String policy = Strings.toString(builder);
System.out.println("Using entitlement policy:\n" + policy);
return Resource.fromString(policy);
}
String policy = Strings.toString(builder);
System.out.println("Using entitlement policy for module " + moduleName + ":\n" + policy);
return Resource.fromString(policy);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}

} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
private void setupEntitlements(PluginInstallSpec spec, boolean modular, PolicyBuilder policyBuilder) {
Copy link
Contributor

@prdoyle prdoyle Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The distinction between the names setupEntitlements and buildEntitlements is a bit subtle... but these are private helpers, so it's not too hard to just look at them and see what they do.

String moduleName = modular ? "org.elasticsearch.entitlement.qa.test" : "ALL-UNNAMED";
if (policyBuilder != null) {
buildEntitlements(spec, moduleName, policyBuilder);
}

if (modular == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.nio.charset.Charset;
import java.nio.file.LinkOption;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.attribute.UserPrincipal;
Expand Down Expand Up @@ -976,6 +977,11 @@ public void checkSelectorProviderInheritedChannel(Class<?> callerClass, Selector

// nio

@Override
public void check$java_nio_file_Files$$getOwner(Class<?> callerClass, Path path, LinkOption... options) {
policyManager.checkFileRead(callerClass, path);
}

@Override
public void check$java_nio_file_Files$$probeContentType(Class<?> callerClass, Path path) {
policyManager.checkFileRead(callerClass, path);
Expand Down