5858import java .nio .file .attribute .FileAttribute ;
5959import java .nio .file .spi .FileSystemProvider ;
6060import java .util .ArrayList ;
61- import java .util .Arrays ;
6261import java .util .Collections ;
6362import java .util .HashMap ;
63+ import java .util .HashSet ;
6464import java .util .List ;
6565import java .util .Map ;
6666import java .util .Set ;
7070import java .util .stream .Stream ;
7171import java .util .stream .StreamSupport ;
7272
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 ;
7380import 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 ;
7781import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .Mode .READ ;
7882import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .Mode .READ_WRITE ;
7983
@@ -200,27 +204,20 @@ private static Class<?>[] findClassesToRetransform(Class<?>[] loadedClasses, Set
200204 private static PolicyManager createPolicyManager () {
201205 EntitlementBootstrap .BootstrapArgs bootstrapArgs = EntitlementBootstrap .bootstrapArgs ();
202206 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 ();
211208
212209 List <Scope > serverScopes = new ArrayList <>();
213210 List <FileData > serverModuleFileDatas = new ArrayList <>();
214211 Collections .addAll (
215212 serverModuleFileDatas ,
216213 // 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 ),
224221 // exclusive settings file
225222 FileData .ofRelativePath (Path .of ("operator/settings.json" ), CONFIG , READ_WRITE ).withExclusive (true ),
226223
@@ -242,8 +239,8 @@ private static PolicyManager createPolicyManager() {
242239 FileData .ofPath (Path .of ("/proc/self/mountinfo" ), READ ).withPlatform (LINUX ),
243240 FileData .ofPath (Path .of ("/proc/diskstats" ), READ ).withPlatform (LINUX )
244241 );
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 ));
247244 }
248245
249246 Collections .addAll (
@@ -255,8 +252,8 @@ private static PolicyManager createPolicyManager() {
255252 new FilesEntitlement (
256253 List .of (
257254 // 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 )
260257 )
261258 )
262259 )
@@ -281,25 +278,17 @@ private static PolicyManager createPolicyManager() {
281278 List .of (
282279 new LoadNativeLibrariesEntitlement (),
283280 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 )))
287282 )
288283 ),
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 ))))),
293285 new Scope (
294286 "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 ))))
296288 ),
297289 new Scope (
298290 "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 ))))
303292 )
304293 );
305294
@@ -324,7 +313,7 @@ private static PolicyManager createPolicyManager() {
324313 new Scope (
325314 "org.bouncycastle.fips.core" ,
326315 // 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 ())
328317 )
329318 );
330319 }
@@ -348,21 +337,14 @@ private static PolicyManager createPolicyManager() {
348337 new LoadNativeLibrariesEntitlement (),
349338 new FilesEntitlement (
350339 List .of (
351- FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ),
340+ FileData .ofBaseDirPath ( LOGS , READ_WRITE ),
352341 FileData .ofPath (Path .of ("/proc/meminfo" ), READ ),
353342 FileData .ofPath (Path .of ("/sys/fs/cgroup/" ), READ )
354343 )
355344 )
356345 );
357346
358- validateFilesEntitlements (
359- pluginPolicies ,
360- pathLookup ,
361- bootstrapArgs .configDir (),
362- bootstrapArgs .pluginsDir (),
363- bootstrapArgs .modulesDir (),
364- bootstrapArgs .libDir ()
365- );
347+ validateFilesEntitlements (pluginPolicies , pathLookup );
366348
367349 return new PolicyManager (
368350 serverPolicy ,
@@ -377,21 +359,14 @@ private static PolicyManager createPolicyManager() {
377359 );
378360 }
379361
380- private static Set <Path > pathSet (Path ... paths ) {
381- return Arrays .stream (paths ).map (x -> x .toAbsolutePath ().normalize ()).collect (Collectors .toUnmodifiableSet ());
382- }
383-
384362 // 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 ()));
395370 for (var pluginPolicy : pluginPolicies .entrySet ()) {
396371 for (var scope : pluginPolicy .getValue ().scopes ()) {
397372 var filesEntitlement = scope .entitlements ()
0 commit comments