Skip to content

Commit

Permalink
Merge branch 'main' into test/fix-bbq-bwc
Browse files Browse the repository at this point in the history
  • Loading branch information
benwtrent authored Feb 14, 2025
2 parents 2261655 + d59a0d9 commit 5a0b90b
Show file tree
Hide file tree
Showing 22 changed files with 524 additions and 80 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/122610.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 122610
summary: Canonicalize processor names and types in `IngestStats`
area: Ingest Node
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
class LoadNativeLibrariesCheckActions {
static void runtimeLoad() {
try {
Runtime.getRuntime().load("libSomeLibFile.so");
Runtime.getRuntime().load(FileCheckActions.readDir().resolve("libSomeLibFile.so").toString());
} catch (UnsatisfiedLinkError ignored) {
// The library does not exist, so we expect to fail loading it
}
}

static void systemLoad() {
try {
System.load("libSomeLibFile.so");
System.load(FileCheckActions.readDir().resolve("libSomeLibFile.so").toString());
} catch (UnsatisfiedLinkError ignored) {
// The library does not exist, so we expect to fail loading it
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static void memorySegmentReinterpretWithSizeAndCleanup() {
@EntitlementTest(expectedAccess = PLUGINS)
static void symbolLookupWithPath() {
try {
SymbolLookup.libraryLookup(Path.of("/foo/bar/libFoo.so"), Arena.ofAuto());
SymbolLookup.libraryLookup(FileCheckActions.readDir().resolve("libFoo.so"), Arena.ofAuto());
} catch (IllegalArgumentException e) {
// IllegalArgumentException is thrown if path does not point to a valid library (and it does not)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.elasticsearch.entitlement.instrumentation.MethodKey;
import org.elasticsearch.entitlement.instrumentation.Transformer;
import org.elasticsearch.entitlement.runtime.api.ElasticsearchEntitlementChecker;
import org.elasticsearch.entitlement.runtime.policy.PathLookup;
import org.elasticsearch.entitlement.runtime.policy.Policy;
import org.elasticsearch.entitlement.runtime.policy.PolicyManager;
import org.elasticsearch.entitlement.runtime.policy.Scope;
Expand Down Expand Up @@ -48,7 +49,6 @@
import java.nio.file.attribute.FileAttribute;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -126,9 +126,9 @@ private static Class<?>[] findClassesToRetransform(Class<?>[] loadedClasses, Set
}

private static PolicyManager createPolicyManager() {
Map<String, Policy> pluginPolicies = EntitlementBootstrap.bootstrapArgs().pluginPolicies();
Path[] dataDirs = EntitlementBootstrap.bootstrapArgs().dataDirs();
Path tempDir = EntitlementBootstrap.bootstrapArgs().tempDir();
EntitlementBootstrap.BootstrapArgs bootstrapArgs = EntitlementBootstrap.bootstrapArgs();
Map<String, Policy> pluginPolicies = bootstrapArgs.pluginPolicies();
var pathLookup = new PathLookup(bootstrapArgs.configDir(), bootstrapArgs.dataDirs(), bootstrapArgs.tempDir());

// TODO(ES-10031): Decide what goes in the elasticsearch default policy and extend it
var serverPolicy = new Policy(
Expand All @@ -147,7 +147,7 @@ private static PolicyManager createPolicyManager() {
new LoadNativeLibrariesEntitlement(),
new ManageThreadsEntitlement(),
new FilesEntitlement(
List.of(new FilesEntitlement.FileData(EntitlementBootstrap.bootstrapArgs().tempDir().toString(), READ_WRITE))
List.of(FilesEntitlement.FileData.ofPath(EntitlementBootstrap.bootstrapArgs().tempDir(), READ_WRITE))
)
)
),
Expand All @@ -159,7 +159,7 @@ private static PolicyManager createPolicyManager() {
"org.elasticsearch.nativeaccess",
List.of(
new LoadNativeLibrariesEntitlement(),
new FilesEntitlement(Arrays.stream(dataDirs).map(d -> new FileData(d.toString(), READ_WRITE)).toList())
new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE)))
)
)
)
Expand All @@ -175,7 +175,7 @@ private static PolicyManager createPolicyManager() {
resolver,
AGENTS_PACKAGE_NAME,
ENTITLEMENTS_MODULE,
tempDir
pathLookup
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ public void checkSelectorProviderInheritedChannel(Class<?> callerClass, Selector

@Override
public void check$java_lang_Runtime$load(Class<?> callerClass, Runtime that, String filename) {
// TODO: check filesystem entitlement READ
policyManager.checkFileRead(callerClass, Path.of(filename));
policyManager.checkLoadingNativeLibraries(callerClass);
}

Expand All @@ -847,7 +847,7 @@ public void checkSelectorProviderInheritedChannel(Class<?> callerClass, Selector

@Override
public void check$java_lang_System$$load(Class<?> callerClass, String filename) {
// TODO: check filesystem entitlement READ
policyManager.checkFileRead(callerClass, Path.of(filename));
policyManager.checkLoadingNativeLibraries(callerClass);
}

Expand Down Expand Up @@ -931,7 +931,7 @@ public void checkSelectorProviderInheritedChannel(Class<?> callerClass, Selector

@Override
public void check$java_lang_foreign_SymbolLookup$$libraryLookup(Class<?> callerClass, Path path, Arena arena) {
// TODO: check filesystem entitlement READ
policyManager.checkFileRead(callerClass, path);
policyManager.checkLoadingNativeLibraries(callerClass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,30 @@
import static org.elasticsearch.core.PathUtils.getDefaultFileSystem;

public final class FileAccessTree {

private static final String FILE_SEPARATOR = getDefaultFileSystem().getSeparator();

private final String[] readPaths;
private final String[] writePaths;

private FileAccessTree(FilesEntitlement filesEntitlement, Path tempDir) {
private FileAccessTree(FilesEntitlement filesEntitlement, PathLookup pathLookup) {
List<String> readPaths = new ArrayList<>();
List<String> writePaths = new ArrayList<>();
for (FilesEntitlement.FileData fileData : filesEntitlement.filesData()) {
var path = normalizePath(Path.of(fileData.path()));
var mode = fileData.mode();
if (mode == FilesEntitlement.Mode.READ_WRITE) {
writePaths.add(path);
}
readPaths.add(path);
var paths = fileData.resolvePaths(pathLookup);
paths.forEach(path -> {
var normalized = normalizePath(path);
if (mode == FilesEntitlement.Mode.READ_WRITE) {
writePaths.add(normalized);
}
readPaths.add(normalized);
});
}

// everything has access to the temp dir
readPaths.add(tempDir.toString());
writePaths.add(tempDir.toString());
readPaths.add(pathLookup.tempDir().toString());
writePaths.add(pathLookup.tempDir().toString());

readPaths.sort(String::compareTo);
writePaths.sort(String::compareTo);
Expand All @@ -48,8 +52,8 @@ private FileAccessTree(FilesEntitlement filesEntitlement, Path tempDir) {
this.writePaths = writePaths.toArray(new String[0]);
}

public static FileAccessTree of(FilesEntitlement filesEntitlement, Path tempDir) {
return new FileAccessTree(filesEntitlement, tempDir);
public static FileAccessTree of(FilesEntitlement filesEntitlement, PathLookup pathLookup) {
return new FileAccessTree(filesEntitlement, pathLookup);
}

boolean canRead(Path path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.entitlement.runtime.policy;

import java.nio.file.Path;

public record PathLookup(Path configDir, Path[] dataDirs, Path tempDir) {}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ ModuleEntitlements policyEntitlements(String componentName, List<Entitlement> en
return new ModuleEntitlements(
componentName,
entitlements.stream().collect(groupingBy(Entitlement::getClass)),
FileAccessTree.of(filesEntitlement, tempDir)
FileAccessTree.of(filesEntitlement, pathLookup)
);
}

Expand All @@ -109,7 +109,7 @@ ModuleEntitlements policyEntitlements(String componentName, List<Entitlement> en
private final List<Entitlement> apmAgentEntitlements;
private final Map<String, Map<String, List<Entitlement>>> pluginsEntitlements;
private final Function<Class<?>, String> pluginResolver;
private final Path tempDir;
private final PathLookup pathLookup;
private final FileAccessTree defaultFileAccess;

public static final String ALL_UNNAMED = "ALL-UNNAMED";
Expand Down Expand Up @@ -146,7 +146,7 @@ public PolicyManager(
Function<Class<?>, String> pluginResolver,
String apmAgentPackageName,
Module entitlementsModule,
Path tempDir
PathLookup pathLookup
) {
this.serverEntitlements = buildScopeEntitlementsMap(requireNonNull(serverPolicy));
this.apmAgentEntitlements = apmAgentEntitlements;
Expand All @@ -156,9 +156,8 @@ public PolicyManager(
this.pluginResolver = pluginResolver;
this.apmAgentPackageName = apmAgentPackageName;
this.entitlementsModule = entitlementsModule;
this.defaultFileAccess = FileAccessTree.of(FilesEntitlement.EMPTY, tempDir);

this.tempDir = tempDir;
this.pathLookup = requireNonNull(pathLookup);
this.defaultFileAccess = FileAccessTree.of(FilesEntitlement.EMPTY, pathLookup);

for (var e : serverEntitlements.entrySet()) {
validateEntitlementsPerModule(SERVER_COMPONENT_NAME, e.getKey(), e.getValue());
Expand Down
Loading

0 comments on commit 5a0b90b

Please sign in to comment.