Skip to content

Commit 8692d40

Browse files
committed
Merge branch 'main' into entitlements/file_read_methods
2 parents 4c50106 + 9141335 commit 8692d40

File tree

23 files changed

+488
-92
lines changed

23 files changed

+488
-92
lines changed

.buildkite/hooks/pre-command

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export GRADLE_BUILD_CACHE_USERNAME
4444
GRADLE_BUILD_CACHE_PASSWORD=$(vault read -field=password secret/ci/elastic-elasticsearch/migrated/gradle-build-cache)
4545
export GRADLE_BUILD_CACHE_PASSWORD
4646

47+
DEVELOCITY_ACCESS_KEY="gradle-enterprise.elastic.co=$(vault read -field=accesskey secret/ci/elastic-elasticsearch/migrated/gradle-build-cache)"
48+
export DEVELOCITY_ACCESS_KEY
49+
4750
BUILDKITE_API_TOKEN=$(vault read -field=token secret/ci/elastic-elasticsearch/buildkite-api-token)
4851
export BUILDKITE_API_TOKEN
4952

.ci/init.gradle

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1+
final String buildCacheUrl = System.getProperty('org.elasticsearch.build.cache.url')
2+
final boolean buildCachePush = Boolean.valueOf(System.getProperty('org.elasticsearch.build.cache.push', 'false'))
3+
14
gradle.settingsEvaluated { settings ->
25
settings.pluginManager.withPlugin("com.gradle.develocity") {
36
settings.develocity {
4-
server = 'https://gradle-enterprise.elastic.co'
7+
server = "https://gradle-enterprise.elastic.co"
58
}
6-
}
7-
}
8-
9-
final String buildCacheUrl = System.getProperty('org.elasticsearch.build.cache.url')
10-
final boolean buildCachePush = Boolean.valueOf(System.getProperty('org.elasticsearch.build.cache.push', 'false'))
11-
12-
if (buildCacheUrl) {
13-
gradle.settingsEvaluated { settings ->
14-
settings.buildCache {
15-
local {
16-
// Disable the local build cache in CI since we use ephemeral workers and it incurs an IO penalty
17-
enabled = false
18-
}
19-
remote(HttpBuildCache) {
20-
url = buildCacheUrl
21-
push = buildCachePush
22-
credentials {
23-
username = System.getenv("GRADLE_BUILD_CACHE_USERNAME")
24-
password = System.getenv("GRADLE_BUILD_CACHE_PASSWORD")
9+
if (buildCacheUrl) {
10+
settings.buildCache {
11+
local {
12+
// Disable the local build cache in CI since we use ephemeral workers and it incurs an IO penalty
13+
enabled = false
14+
}
15+
remote(settings.develocity.buildCache) {
16+
enabled = true
17+
push = buildCachePush
2518
}
2619
}
2720
}
2821
}
2922
}
23+
24+

build-conventions/settings.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
* your election, the "Elastic License 2.0", the "GNU Affero General Public
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
9+
10+
plugins {
11+
id "com.gradle.develocity" version "3.18.1"
12+
}
13+
914
rootProject.name = 'build-conventions'
1015

1116
dependencyResolutionManagement {

build-tools-internal/settings.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ pluginManagement {
88
includeBuild "../build-tools"
99
}
1010

11+
plugins {
12+
id "com.gradle.develocity" version "3.18.1"
13+
}
14+
1115
dependencyResolutionManagement {
1216
versionCatalogs {
1317
buildLibs {

build-tools/settings.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
pluginManagement {
1010
includeBuild "../build-conventions"
1111
}
12-
12+
plugins {
13+
id "com.gradle.develocity" version "3.18.1"
14+
}
1315
include 'reaper'
1416

1517
dependencyResolutionManagement {

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/LoadNativeLibrariesCheckActions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
class LoadNativeLibrariesCheckActions {
1313
static void runtimeLoad() {
1414
try {
15-
Runtime.getRuntime().load("libSomeLibFile.so");
15+
Runtime.getRuntime().load(FileCheckActions.readDir().resolve("libSomeLibFile.so").toString());
1616
} catch (UnsatisfiedLinkError ignored) {
1717
// The library does not exist, so we expect to fail loading it
1818
}
1919
}
2020

2121
static void systemLoad() {
2222
try {
23-
System.load("libSomeLibFile.so");
23+
System.load(FileCheckActions.readDir().resolve("libSomeLibFile.so").toString());
2424
} catch (UnsatisfiedLinkError ignored) {
2525
// The library does not exist, so we expect to fail loading it
2626
}

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/NativeActions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static void memorySegmentReinterpretWithSizeAndCleanup() {
113113
@EntitlementTest(expectedAccess = PLUGINS)
114114
static void symbolLookupWithPath() {
115115
try {
116-
SymbolLookup.libraryLookup(Path.of("/foo/bar/libFoo.so"), Arena.ofAuto());
116+
SymbolLookup.libraryLookup(FileCheckActions.readDir().resolve("libFoo.so"), Arena.ofAuto());
117117
} catch (IllegalArgumentException e) {
118118
// IllegalArgumentException is thrown if path does not point to a valid library (and it does not)
119119
}

libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.entitlement.instrumentation.MethodKey;
1919
import org.elasticsearch.entitlement.instrumentation.Transformer;
2020
import org.elasticsearch.entitlement.runtime.api.ElasticsearchEntitlementChecker;
21+
import org.elasticsearch.entitlement.runtime.policy.PathLookup;
2122
import org.elasticsearch.entitlement.runtime.policy.Policy;
2223
import org.elasticsearch.entitlement.runtime.policy.PolicyManager;
2324
import org.elasticsearch.entitlement.runtime.policy.Scope;
@@ -48,7 +49,6 @@
4849
import java.nio.file.attribute.FileAttribute;
4950
import java.nio.file.spi.FileSystemProvider;
5051
import java.util.ArrayList;
51-
import java.util.Arrays;
5252
import java.util.HashMap;
5353
import java.util.List;
5454
import java.util.Map;
@@ -126,9 +126,9 @@ private static Class<?>[] findClassesToRetransform(Class<?>[] loadedClasses, Set
126126
}
127127

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

133133
// TODO(ES-10031): Decide what goes in the elasticsearch default policy and extend it
134134
var serverPolicy = new Policy(
@@ -147,7 +147,7 @@ private static PolicyManager createPolicyManager() {
147147
new LoadNativeLibrariesEntitlement(),
148148
new ManageThreadsEntitlement(),
149149
new FilesEntitlement(
150-
List.of(new FilesEntitlement.FileData(EntitlementBootstrap.bootstrapArgs().tempDir().toString(), READ_WRITE))
150+
List.of(FilesEntitlement.FileData.ofPath(EntitlementBootstrap.bootstrapArgs().tempDir(), READ_WRITE))
151151
)
152152
)
153153
),
@@ -159,7 +159,7 @@ private static PolicyManager createPolicyManager() {
159159
"org.elasticsearch.nativeaccess",
160160
List.of(
161161
new LoadNativeLibrariesEntitlement(),
162-
new FilesEntitlement(Arrays.stream(dataDirs).map(d -> new FileData(d.toString(), READ_WRITE)).toList())
162+
new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE)))
163163
)
164164
)
165165
)
@@ -175,7 +175,7 @@ private static PolicyManager createPolicyManager() {
175175
resolver,
176176
AGENTS_PACKAGE_NAME,
177177
ENTITLEMENTS_MODULE,
178-
tempDir
178+
pathLookup
179179
);
180180
}
181181

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/api/ElasticsearchEntitlementChecker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ public void checkSelectorProviderInheritedChannel(Class<?> callerClass, Selector
838838

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

@@ -849,7 +849,7 @@ public void checkSelectorProviderInheritedChannel(Class<?> callerClass, Selector
849849

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

@@ -933,7 +933,7 @@ public void checkSelectorProviderInheritedChannel(Class<?> callerClass, Selector
933933

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

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,30 @@
2020
import static org.elasticsearch.core.PathUtils.getDefaultFileSystem;
2121

2222
public final class FileAccessTree {
23+
2324
private static final String FILE_SEPARATOR = getDefaultFileSystem().getSeparator();
2425

2526
private final String[] readPaths;
2627
private final String[] writePaths;
2728

28-
private FileAccessTree(FilesEntitlement filesEntitlement, Path tempDir) {
29+
private FileAccessTree(FilesEntitlement filesEntitlement, PathLookup pathLookup) {
2930
List<String> readPaths = new ArrayList<>();
3031
List<String> writePaths = new ArrayList<>();
3132
for (FilesEntitlement.FileData fileData : filesEntitlement.filesData()) {
32-
var path = normalizePath(Path.of(fileData.path()));
3333
var mode = fileData.mode();
34-
if (mode == FilesEntitlement.Mode.READ_WRITE) {
35-
writePaths.add(path);
36-
}
37-
readPaths.add(path);
34+
var paths = fileData.resolvePaths(pathLookup);
35+
paths.forEach(path -> {
36+
var normalized = normalizePath(path);
37+
if (mode == FilesEntitlement.Mode.READ_WRITE) {
38+
writePaths.add(normalized);
39+
}
40+
readPaths.add(normalized);
41+
});
3842
}
3943

4044
// everything has access to the temp dir
41-
readPaths.add(tempDir.toString());
42-
writePaths.add(tempDir.toString());
45+
readPaths.add(pathLookup.tempDir().toString());
46+
writePaths.add(pathLookup.tempDir().toString());
4347

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

51-
public static FileAccessTree of(FilesEntitlement filesEntitlement, Path tempDir) {
52-
return new FileAccessTree(filesEntitlement, tempDir);
55+
public static FileAccessTree of(FilesEntitlement filesEntitlement, PathLookup pathLookup) {
56+
return new FileAccessTree(filesEntitlement, pathLookup);
5357
}
5458

5559
boolean canRead(Path path) {

0 commit comments

Comments
 (0)