Skip to content

Commit c78ce68

Browse files
committed
Add shared_repo relative dir for files entitlement
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 e608442 commit c78ce68

File tree

9 files changed

+37
-21
lines changed

9 files changed

+37
-21
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,
@@ -52,11 +52,11 @@ public record BootstrapArgs(
5252
requireNonNull(pluginResolver);
5353
requireNonNull(settingResolver);
5454
requireNonNull(settingGlobResolver);
55-
requireNonNull(repoDirResolver);
5655
requireNonNull(dataDirs);
5756
if (dataDirs.length == 0) {
5857
throw new IllegalArgumentException("must provide at least one data directory");
5958
}
59+
requireNonNull(sharedRepoDirs);
6060
requireNonNull(configDir);
6161
requireNonNull(libDir);
6262
requireNonNull(logsDir);
@@ -78,8 +78,8 @@ public static BootstrapArgs bootstrapArgs() {
7878
* @param pluginResolver a functor to map a Java Class to the plugin it belongs to (the plugin name).
7979
* @param settingResolver a functor to resolve the value of an Elasticsearch setting.
8080
* @param settingGlobResolver a functor to resolve a glob expression for one or more Elasticsearch settings.
81-
* @param repoDirResolver a functor to map a repository location to its Elasticsearch path.
8281
* @param dataDirs data directories for Elasticsearch
82+
* @param sharedRepoDirs shared repository directories for Elasticsearch
8383
* @param configDir the config directory for Elasticsearch
8484
* @param libDir the lib directory for Elasticsearch
8585
* @param tempDir the temp directory for Elasticsearch
@@ -90,8 +90,8 @@ public static void bootstrap(
9090
Function<Class<?>, String> pluginResolver,
9191
Function<String, String> settingResolver,
9292
Function<String, Stream<String>> settingGlobResolver,
93-
Function<String, Path> repoDirResolver,
9493
Path[] dataDirs,
94+
Path[] sharedRepoDirs,
9595
Path configDir,
9696
Path libDir,
9797
Path logsDir,
@@ -107,8 +107,8 @@ public static void bootstrap(
107107
pluginResolver,
108108
settingResolver,
109109
settingGlobResolver,
110-
repoDirResolver,
111110
dataDirs,
111+
sharedRepoDirs,
112112
configDir,
113113
libDir,
114114
logsDir,

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

Lines changed: 10 additions & 6 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
import static org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.Platform.LINUX;
@@ -139,6 +141,7 @@ private static PolicyManager createPolicyManager() {
139141
getUserHome(),
140142
bootstrapArgs.configDir(),
141143
bootstrapArgs.dataDirs(),
144+
bootstrapArgs.sharedRepoDirs(),
142145
bootstrapArgs.tempDir(),
143146
bootstrapArgs.settingResolver(),
144147
bootstrapArgs.settingGlobResolver()
@@ -152,7 +155,7 @@ private static PolicyManager createPolicyManager() {
152155
FileData.ofPath(bootstrapArgs.configDir(), READ),
153156
FileData.ofPath(bootstrapArgs.logsDir(), READ_WRITE),
154157
FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE),
155-
FileData.ofPath(bootstrapArgs.repoDirResolver().apply(""), READ_WRITE),
158+
FileData.ofRelativePath(Path.of(""), SHARED_REPO, READ_WRITE),
156159

157160
// OS release on Linux
158161
FileData.ofPath(Path.of("/etc/os-release"), READ).withPlatform(LINUX),
@@ -184,8 +187,9 @@ private static PolicyManager createPolicyManager() {
184187
new CreateClassLoaderEntitlement(),
185188
new FilesEntitlement(
186189
List.of(
187-
FileData.ofPath(bootstrapArgs.repoDirResolver().apply(""), READ_WRITE),
188-
FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE)
190+
// TODO: what in es.base is accessing shared repo?
191+
FileData.ofRelativePath(Path.of(""), SHARED_REPO, READ_WRITE),
192+
FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE)
189193
)
190194
)
191195
)
@@ -214,14 +218,14 @@ private static PolicyManager createPolicyManager() {
214218
new FilesEntitlement(
215219
List.of(
216220
FileData.ofPath(bootstrapArgs.configDir(), READ),
217-
FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE)
221+
FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE)
218222
)
219223
)
220224
)
221225
),
222226
new Scope(
223227
"org.apache.lucene.misc",
224-
List.of(new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE))))
228+
List.of(new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE))))
225229
),
226230
new Scope(
227231
"org.apache.logging.log4j.core",
@@ -231,7 +235,7 @@ private static PolicyManager createPolicyManager() {
231235
"org.elasticsearch.nativeaccess",
232236
List.of(
233237
new LoadNativeLibrariesEntitlement(),
234-
new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE)))
238+
new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE)))
235239
)
236240
)
237241
);

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
@@ -36,6 +36,7 @@ public enum Mode {
3636
public enum BaseDir {
3737
CONFIG,
3838
DATA,
39+
SHARED_REPO,
3940
HOME
4041
}
4142

@@ -104,14 +105,9 @@ default Stream<Path> resolvePaths(PathLookup pathLookup) {
104105
case CONFIG:
105106
return relativePaths.map(relativePath -> pathLookup.configDir().resolve(relativePath));
106107
case DATA:
107-
// multiple data dirs are a pain...we need the combination of relative paths and data dirs
108-
List<Path> paths = new ArrayList<>();
109-
for (var relativePath : relativePaths.toList()) {
110-
for (var dataDir : pathLookup.dataDirs()) {
111-
paths.add(dataDir.resolve(relativePath));
112-
}
113-
}
114-
return paths.stream();
108+
return relativePathsCombination(pathLookup.dataDirs(), relativePaths);
109+
case SHARED_REPO:
110+
return relativePathsCombination(pathLookup.sharedRepoDirs(), relativePaths);
115111
case HOME:
116112
return relativePaths.map(relativePath -> pathLookup.homeDir().resolve(relativePath));
117113
default:
@@ -120,6 +116,17 @@ default Stream<Path> resolvePaths(PathLookup pathLookup) {
120116
}
121117
}
122118

119+
private static Stream<Path> relativePathsCombination(Path[] baseDirs, Stream<Path> relativePaths) {
120+
// multiple base dirs are a pain...we need the combination of the base dirs and relative paths
121+
List<Path> paths = new ArrayList<>();
122+
for (var relativePath : relativePaths.toList()) {
123+
for (var dataDir : baseDirs) {
124+
paths.add(dataDir.resolve(relativePath));
125+
}
126+
}
127+
return paths.stream();
128+
}
129+
123130
private record AbsolutePathFileData(Path path, Mode mode, Platform platform) implements FileData {
124131
@Override
125132
public Stream<Path> resolvePaths(PathLookup pathLookup) {
@@ -221,6 +228,7 @@ private static BaseDir parseBaseDir(String baseDir) {
221228
case "config" -> BaseDir.CONFIG;
222229
case "data" -> BaseDir.DATA;
223230
case "home" -> BaseDir.HOME;
231+
// NOTE: shared_repo is _not_ accessible to policy files, only internally
224232
default -> throw new PolicyValidationException(
225233
"invalid relative directory: " + baseDir + ", valid values: [config, data, home]"
226234
);

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
@@ -43,6 +43,7 @@ private static Path path(String s) {
4343
Path.of("/home"),
4444
Path.of("/config"),
4545
new Path[] { Path.of("/data1"), Path.of("/data2") },
46+
new Path[] { Path.of("/shared1"), Path.of("/shared2") },
4647
Path.of("/tmp"),
4748
setting -> settings.get(setting),
4849
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)