58
58
import java .nio .file .attribute .FileAttribute ;
59
59
import java .nio .file .spi .FileSystemProvider ;
60
60
import java .util .ArrayList ;
61
- import java .util .Arrays ;
62
61
import java .util .Collections ;
63
62
import java .util .HashMap ;
63
+ import java .util .HashSet ;
64
64
import java .util .List ;
65
65
import java .util .Map ;
66
66
import java .util .Set ;
70
70
import java .util .stream .Stream ;
71
71
import java .util .stream .StreamSupport ;
72
72
73
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .CONFIG ;
74
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .DATA ;
75
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .LIB ;
76
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .LOGS ;
77
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .MODULES ;
78
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .PLUGINS ;
79
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .SHARED_REPO ;
73
80
import static org .elasticsearch .entitlement .runtime .policy .Platform .LINUX ;
74
- import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .BaseDir .CONFIG ;
75
- import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .BaseDir .DATA ;
76
- import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .BaseDir .SHARED_REPO ;
77
81
import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .Mode .READ ;
78
82
import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .Mode .READ_WRITE ;
79
83
@@ -200,27 +204,20 @@ private static Class<?>[] findClassesToRetransform(Class<?>[] loadedClasses, Set
200
204
private static PolicyManager createPolicyManager () {
201
205
EntitlementBootstrap .BootstrapArgs bootstrapArgs = EntitlementBootstrap .bootstrapArgs ();
202
206
Map <String , Policy > pluginPolicies = bootstrapArgs .pluginPolicies ();
203
- var pathLookup = new PathLookup (
204
- getUserHome (),
205
- bootstrapArgs .configDir (),
206
- bootstrapArgs .dataDirs (),
207
- bootstrapArgs .sharedRepoDirs (),
208
- bootstrapArgs .tempDir (),
209
- bootstrapArgs .settingResolver ()
210
- );
207
+ PathLookup pathLookup = bootstrapArgs .pathLookup ();
211
208
212
209
List <Scope > serverScopes = new ArrayList <>();
213
210
List <FileData > serverModuleFileDatas = new ArrayList <>();
214
211
Collections .addAll (
215
212
serverModuleFileDatas ,
216
213
// Base ES directories
217
- FileData .ofPath ( bootstrapArgs . pluginsDir () , READ ),
218
- FileData .ofPath ( bootstrapArgs . modulesDir () , READ ),
219
- FileData .ofPath ( bootstrapArgs . configDir () , READ ),
220
- FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ),
221
- FileData .ofPath ( bootstrapArgs . libDir () , READ ),
222
- FileData .ofRelativePath ( Path . of ( "" ), DATA , READ_WRITE ),
223
- FileData .ofRelativePath ( Path . of ( "" ), SHARED_REPO , READ_WRITE ),
214
+ FileData .ofBaseDirPath ( PLUGINS , READ ),
215
+ FileData .ofBaseDirPath ( MODULES , READ ),
216
+ FileData .ofBaseDirPath ( CONFIG , READ ),
217
+ FileData .ofBaseDirPath ( LOGS , READ_WRITE ),
218
+ FileData .ofBaseDirPath ( LIB , READ ),
219
+ FileData .ofBaseDirPath ( DATA , READ_WRITE ),
220
+ FileData .ofBaseDirPath ( SHARED_REPO , READ_WRITE ),
224
221
// exclusive settings file
225
222
FileData .ofRelativePath (Path .of ("operator/settings.json" ), CONFIG , READ_WRITE ).withExclusive (true ),
226
223
@@ -242,8 +239,8 @@ private static PolicyManager createPolicyManager() {
242
239
FileData .ofPath (Path .of ("/proc/self/mountinfo" ), READ ).withPlatform (LINUX ),
243
240
FileData .ofPath (Path .of ("/proc/diskstats" ), READ ).withPlatform (LINUX )
244
241
);
245
- if (bootstrapArgs .pidFile () != null ) {
246
- serverModuleFileDatas .add (FileData .ofPath (bootstrapArgs .pidFile (), READ_WRITE ));
242
+ if (pathLookup .pidFile () != null ) {
243
+ serverModuleFileDatas .add (FileData .ofPath (pathLookup .pidFile (), READ_WRITE ));
247
244
}
248
245
249
246
Collections .addAll (
@@ -255,8 +252,8 @@ private static PolicyManager createPolicyManager() {
255
252
new FilesEntitlement (
256
253
List .of (
257
254
// TODO: what in es.base is accessing shared repo?
258
- FileData .ofRelativePath ( Path . of ( "" ), SHARED_REPO , READ_WRITE ),
259
- FileData .ofRelativePath ( Path . of ( "" ), DATA , READ_WRITE )
255
+ FileData .ofBaseDirPath ( SHARED_REPO , READ_WRITE ),
256
+ FileData .ofBaseDirPath ( DATA , READ_WRITE )
260
257
)
261
258
)
262
259
)
@@ -281,25 +278,17 @@ private static PolicyManager createPolicyManager() {
281
278
List .of (
282
279
new LoadNativeLibrariesEntitlement (),
283
280
new ManageThreadsEntitlement (),
284
- new FilesEntitlement (
285
- List .of (FileData .ofPath (bootstrapArgs .configDir (), READ ), FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE ))
286
- )
281
+ new FilesEntitlement (List .of (FileData .ofBaseDirPath (CONFIG , READ ), FileData .ofBaseDirPath (DATA , READ_WRITE )))
287
282
)
288
283
),
289
- new Scope (
290
- "org.apache.lucene.misc" ,
291
- List .of (new FilesEntitlement (List .of (FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE ))))
292
- ),
284
+ new Scope ("org.apache.lucene.misc" , List .of (new FilesEntitlement (List .of (FileData .ofBaseDirPath (DATA , READ_WRITE ))))),
293
285
new Scope (
294
286
"org.apache.logging.log4j.core" ,
295
- List .of (new ManageThreadsEntitlement (), new FilesEntitlement (List .of (FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ))))
287
+ List .of (new ManageThreadsEntitlement (), new FilesEntitlement (List .of (FileData .ofBaseDirPath ( LOGS , READ_WRITE ))))
296
288
),
297
289
new Scope (
298
290
"org.elasticsearch.nativeaccess" ,
299
- List .of (
300
- new LoadNativeLibrariesEntitlement (),
301
- new FilesEntitlement (List .of (FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE )))
302
- )
291
+ List .of (new LoadNativeLibrariesEntitlement (), new FilesEntitlement (List .of (FileData .ofBaseDirPath (DATA , READ_WRITE ))))
303
292
)
304
293
);
305
294
@@ -324,7 +313,7 @@ private static PolicyManager createPolicyManager() {
324
313
new Scope (
325
314
"org.bouncycastle.fips.core" ,
326
315
// read to lib dir is required for checksum validation
327
- List .of (new FilesEntitlement (List .of (FileData .ofPath ( bootstrapArgs . libDir () , READ ))), new ManageThreadsEntitlement ())
316
+ List .of (new FilesEntitlement (List .of (FileData .ofBaseDirPath ( LIB , READ ))), new ManageThreadsEntitlement ())
328
317
)
329
318
);
330
319
}
@@ -348,21 +337,14 @@ private static PolicyManager createPolicyManager() {
348
337
new LoadNativeLibrariesEntitlement (),
349
338
new FilesEntitlement (
350
339
List .of (
351
- FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ),
340
+ FileData .ofBaseDirPath ( LOGS , READ_WRITE ),
352
341
FileData .ofPath (Path .of ("/proc/meminfo" ), READ ),
353
342
FileData .ofPath (Path .of ("/sys/fs/cgroup/" ), READ )
354
343
)
355
344
)
356
345
);
357
346
358
- validateFilesEntitlements (
359
- pluginPolicies ,
360
- pathLookup ,
361
- bootstrapArgs .configDir (),
362
- bootstrapArgs .pluginsDir (),
363
- bootstrapArgs .modulesDir (),
364
- bootstrapArgs .libDir ()
365
- );
347
+ validateFilesEntitlements (pluginPolicies , pathLookup );
366
348
367
349
return new PolicyManager (
368
350
serverPolicy ,
@@ -377,21 +359,14 @@ private static PolicyManager createPolicyManager() {
377
359
);
378
360
}
379
361
380
- private static Set <Path > pathSet (Path ... paths ) {
381
- return Arrays .stream (paths ).map (x -> x .toAbsolutePath ().normalize ()).collect (Collectors .toUnmodifiableSet ());
382
- }
383
-
384
362
// package visible for tests
385
- static void validateFilesEntitlements (
386
- Map <String , Policy > pluginPolicies ,
387
- PathLookup pathLookup ,
388
- Path configDir ,
389
- Path pluginsDir ,
390
- Path modulesDir ,
391
- Path libDir
392
- ) {
393
- var readAccessForbidden = pathSet (pluginsDir , modulesDir , libDir );
394
- var writeAccessForbidden = pathSet (configDir );
363
+ static void validateFilesEntitlements (Map <String , Policy > pluginPolicies , PathLookup pathLookup ) {
364
+ Set <Path > readAccessForbidden = new HashSet <>();
365
+ pathLookup .getBaseDirPaths (PLUGINS ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
366
+ pathLookup .getBaseDirPaths (MODULES ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
367
+ pathLookup .getBaseDirPaths (LIB ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
368
+ Set <Path > writeAccessForbidden = new HashSet <>();
369
+ pathLookup .getBaseDirPaths (CONFIG ).forEach (p -> writeAccessForbidden .add (p .toAbsolutePath ().normalize ()));
395
370
for (var pluginPolicy : pluginPolicies .entrySet ()) {
396
371
for (var scope : pluginPolicy .getValue ().scopes ()) {
397
372
var filesEntitlement = scope .entitlements ()
0 commit comments