Skip to content

Commit ad99b0d

Browse files
authored
Add shared_repo relative dir for files entitlement (#123221)
This commit adds the shared repo path as a relative base dir. However, it does not make this available to policy files, only to server.
1 parent a792334 commit ad99b0d

File tree

9 files changed

+37
-22
lines changed

9 files changed

+37
-22
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/bootstrap/EntitlementBootstrap.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public record BootstrapArgs(
3939
Function<Class<?>, String> pluginResolver,
4040
Function<String, String> settingResolver,
4141
Function<String, Stream<String>> settingGlobResolver,
42-
Function<String, Path> repoDirResolver,
4342
Path[] dataDirs,
43+
Path[] sharedRepoDirs,
4444
Path configDir,
4545
Path libDir,
4646
Path logsDir,
@@ -51,11 +51,11 @@ public record BootstrapArgs(
5151
requireNonNull(pluginResolver);
5252
requireNonNull(settingResolver);
5353
requireNonNull(settingGlobResolver);
54-
requireNonNull(repoDirResolver);
5554
requireNonNull(dataDirs);
5655
if (dataDirs.length == 0) {
5756
throw new IllegalArgumentException("must provide at least one data directory");
5857
}
58+
requireNonNull(sharedRepoDirs);
5959
requireNonNull(configDir);
6060
requireNonNull(libDir);
6161
requireNonNull(logsDir);
@@ -77,8 +77,8 @@ public static BootstrapArgs bootstrapArgs() {
7777
* @param pluginResolver a functor to map a Java Class to the plugin it belongs to (the plugin name).
7878
* @param settingResolver a functor to resolve the value of an Elasticsearch setting.
7979
* @param settingGlobResolver a functor to resolve a glob expression for one or more Elasticsearch settings.
80-
* @param repoDirResolver a functor to map a repository location to its Elasticsearch path.
8180
* @param dataDirs data directories for Elasticsearch
81+
* @param sharedRepoDirs shared repository directories for Elasticsearch
8282
* @param configDir the config directory for Elasticsearch
8383
* @param libDir the lib directory for Elasticsearch
8484
* @param tempDir the temp directory for Elasticsearch
@@ -89,8 +89,8 @@ public static void bootstrap(
8989
Function<Class<?>, String> pluginResolver,
9090
Function<String, String> settingResolver,
9191
Function<String, Stream<String>> settingGlobResolver,
92-
Function<String, Path> repoDirResolver,
9392
Path[] dataDirs,
93+
Path[] sharedRepoDirs,
9494
Path configDir,
9595
Path libDir,
9696
Path logsDir,
@@ -105,8 +105,8 @@ public static void bootstrap(
105105
pluginResolver,
106106
settingResolver,
107107
settingGlobResolver,
108-
repoDirResolver,
109108
dataDirs,
109+
sharedRepoDirs,
110110
configDir,
111111
libDir,
112112
logsDir,

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
import java.util.stream.Stream;
6464
import java.util.stream.StreamSupport;
6565

66+
import static org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.BaseDir.DATA;
67+
import static org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.BaseDir.SHARED_REPO;
6668
import static org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.Mode.READ;
6769
import static org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.Mode.READ_WRITE;
6870

@@ -138,6 +140,7 @@ private static PolicyManager createPolicyManager() {
138140
getUserHome(),
139141
bootstrapArgs.configDir(),
140142
bootstrapArgs.dataDirs(),
143+
bootstrapArgs.sharedRepoDirs(),
141144
bootstrapArgs.tempDir(),
142145
bootstrapArgs.settingResolver(),
143146
bootstrapArgs.settingGlobResolver()
@@ -152,8 +155,8 @@ private static PolicyManager createPolicyManager() {
152155
new CreateClassLoaderEntitlement(),
153156
new FilesEntitlement(
154157
List.of(
155-
FileData.ofPath(bootstrapArgs.repoDirResolver().apply(""), READ_WRITE),
156-
FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE)
158+
FileData.ofRelativePath(Path.of(""), SHARED_REPO, READ_WRITE),
159+
FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE)
157160
)
158161
)
159162
)
@@ -175,8 +178,8 @@ private static PolicyManager createPolicyManager() {
175178
FileData.ofPath(bootstrapArgs.tempDir(), READ_WRITE),
176179
FileData.ofPath(bootstrapArgs.configDir(), READ),
177180
FileData.ofPath(bootstrapArgs.logsDir(), READ_WRITE),
178-
FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE),
179-
FileData.ofPath(bootstrapArgs.repoDirResolver().apply(""), READ_WRITE),
181+
FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE),
182+
FileData.ofRelativePath(Path.of(""), SHARED_REPO, READ_WRITE),
180183

181184
// OS release on Linux
182185
FileData.ofPath(Path.of("/etc/os-release"), READ),
@@ -210,21 +213,21 @@ private static PolicyManager createPolicyManager() {
210213
List.of(
211214
FileData.ofPath(bootstrapArgs.configDir(), READ),
212215
FileData.ofPath(bootstrapArgs.tempDir(), READ),
213-
FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE)
216+
FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE)
214217
)
215218
)
216219
)
217220
),
218221
new Scope(
219222
"org.apache.lucene.misc",
220-
List.of(new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE))))
223+
List.of(new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE))))
221224
),
222225
new Scope("org.apache.logging.log4j.core", List.of(new ManageThreadsEntitlement())),
223226
new Scope(
224227
"org.elasticsearch.nativeaccess",
225228
List.of(
226229
new LoadNativeLibrariesEntitlement(),
227-
new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE)))
230+
new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE)))
228231
)
229232
)
230233
);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public record PathLookup(
1717
Path homeDir,
1818
Path configDir,
1919
Path[] dataDirs,
20+
Path[] sharedRepoDirs,
2021
Path tempDir,
2122
Function<String, String> settingResolver,
2223
Function<String, Stream<String>> settingGlobResolver

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/entitlements/FilesEntitlement.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public enum Mode {
3838
public enum BaseDir {
3939
CONFIG,
4040
DATA,
41+
SHARED_REPO,
4142
HOME
4243
}
4344

@@ -122,14 +123,9 @@ default Stream<Path> resolvePaths(PathLookup pathLookup) {
122123
case CONFIG:
123124
return relativePaths.map(relativePath -> pathLookup.configDir().resolve(relativePath));
124125
case DATA:
125-
// multiple data dirs are a pain...we need the combination of relative paths and data dirs
126-
List<Path> paths = new ArrayList<>();
127-
for (var relativePath : relativePaths.toList()) {
128-
for (var dataDir : pathLookup.dataDirs()) {
129-
paths.add(dataDir.resolve(relativePath));
130-
}
131-
}
132-
return paths.stream();
126+
return relativePathsCombination(pathLookup.dataDirs(), relativePaths);
127+
case SHARED_REPO:
128+
return relativePathsCombination(pathLookup.sharedRepoDirs(), relativePaths);
133129
case HOME:
134130
return relativePaths.map(relativePath -> pathLookup.homeDir().resolve(relativePath));
135131
default:
@@ -138,6 +134,17 @@ default Stream<Path> resolvePaths(PathLookup pathLookup) {
138134
}
139135
}
140136

137+
private static Stream<Path> relativePathsCombination(Path[] baseDirs, Stream<Path> relativePaths) {
138+
// multiple base dirs are a pain...we need the combination of the base dirs and relative paths
139+
List<Path> paths = new ArrayList<>();
140+
for (var relativePath : relativePaths.toList()) {
141+
for (var dataDir : baseDirs) {
142+
paths.add(dataDir.resolve(relativePath));
143+
}
144+
}
145+
return paths.stream();
146+
}
147+
141148
private record AbsolutePathFileData(Path path, Mode mode) implements FileData {
142149
@Override
143150
public Stream<Path> resolvePaths(PathLookup pathLookup) {
@@ -189,6 +196,7 @@ private static BaseDir parseBaseDir(String baseDir) {
189196
case "config" -> BaseDir.CONFIG;
190197
case "data" -> BaseDir.DATA;
191198
case "home" -> BaseDir.HOME;
199+
// NOTE: shared_repo is _not_ accessible to policy files, only internally
192200
default -> throw new PolicyValidationException(
193201
"invalid relative directory: " + baseDir + ", valid values: [config, data, home]"
194202
);

libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTreeTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ private static Path path(String s) {
4242
Path.of("/home"),
4343
Path.of("/config"),
4444
new Path[] { Path.of("/data1"), Path.of("/data2") },
45+
new Path[] { Path.of("/shared1"), Path.of("/shared2") },
4546
Path.of("/tmp"),
4647
setting -> settings.get(setting),
4748
glob -> settings.getGlobValues(glob)

libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/PolicyManagerTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public static void beforeClass() {
6969
TEST_BASE_DIR.resolve("/user/home"),
7070
TEST_BASE_DIR.resolve("/config"),
7171
new Path[] { TEST_BASE_DIR.resolve("/data1/"), TEST_BASE_DIR.resolve("/data2") },
72+
new Path[] { TEST_BASE_DIR.resolve("/shared1"), TEST_BASE_DIR.resolve("/shared2") },
7273
TEST_BASE_DIR.resolve("/temp"),
7374
Settings.EMPTY::get,
7475
Settings.EMPTY::getGlobValues

libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/entitlements/FilesEntitlementTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static void setupRoot() {
4040
Path.of("home"),
4141
Path.of("/config"),
4242
new Path[] { Path.of("/data1"), Path.of("/data2") },
43+
new Path[] { Path.of("/shared1"), Path.of("/shared2") },
4344
Path.of("/tmp"),
4445
setting -> settings.get(setting),
4546
glob -> settings.getGlobValues(glob)

server/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ private static void initPhase2(Bootstrap bootstrap) throws IOException {
247247
pluginsResolver::resolveClassToPluginName,
248248
nodeEnv.settings()::get,
249249
nodeEnv.settings()::getGlobValues,
250-
nodeEnv::resolveRepoDir,
251250
nodeEnv.dataDirs(),
251+
nodeEnv.repoDirs(),
252252
nodeEnv.configDir(),
253253
nodeEnv.libDir(),
254254
nodeEnv.logsDir(),

server/src/main/java/org/elasticsearch/env/Environment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public static long getUsableSpace(Path path) throws IOException {
335335
*/
336336
public static void assertEquivalent(Environment actual, Environment expected) {
337337
assertEquals(actual.dataDirs(), expected.dataDirs(), "dataDirs");
338-
assertEquals(actual.repoDirs(), expected.repoDirs(), "repoDirs");
338+
assertEquals(actual.repoDirs(), expected.repoDirs(), "sharedRepoDirs");
339339
assertEquals(actual.configDir(), expected.configDir(), "configDir");
340340
assertEquals(actual.pluginsDir(), expected.pluginsDir(), "pluginsDir");
341341
assertEquals(actual.binDir(), expected.binDir(), "binDir");

0 commit comments

Comments
 (0)