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
@@ -165,27 +169,20 @@ private static Class<?>[] findClassesToRetransform(Class<?>[] loadedClasses, Set
165169    private  static  PolicyManager  createPolicyManager () {
166170        EntitlementBootstrap .BootstrapArgs  bootstrapArgs  = EntitlementBootstrap .bootstrapArgs ();
167171        Map <String , Policy > pluginPolicies  = bootstrapArgs .pluginPolicies ();
168-         var  pathLookup  = new  PathLookup (
169-             getUserHome (),
170-             bootstrapArgs .configDir (),
171-             bootstrapArgs .dataDirs (),
172-             bootstrapArgs .sharedRepoDirs (),
173-             bootstrapArgs .tempDir (),
174-             bootstrapArgs .settingResolver ()
175-         );
172+         PathLookup  pathLookup  = bootstrapArgs .pathLookup ();
176173
177174        List <Scope > serverScopes  = new  ArrayList <>();
178175        List <FileData > serverModuleFileDatas  = new  ArrayList <>();
179176        Collections .addAll (
180177            serverModuleFileDatas ,
181178            // Base ES directories 
182-             FileData .ofPath ( bootstrapArgs . pluginsDir () , READ ),
183-             FileData .ofPath ( bootstrapArgs . modulesDir () , READ ),
184-             FileData .ofPath ( bootstrapArgs . configDir () , READ ),
185-             FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ),
186-             FileData .ofPath ( bootstrapArgs . libDir () , READ ),
187-             FileData .ofRelativePath ( Path . of ( "" ),  DATA , READ_WRITE ),
188-             FileData .ofRelativePath ( Path . of ( "" ),  SHARED_REPO , READ_WRITE ),
179+             FileData .ofBaseDirPath ( PLUGINS , READ ),
180+             FileData .ofBaseDirPath ( MODULES , READ ),
181+             FileData .ofBaseDirPath ( CONFIG , READ ),
182+             FileData .ofBaseDirPath ( LOGS , READ_WRITE ),
183+             FileData .ofBaseDirPath ( LIB , READ ),
184+             FileData .ofBaseDirPath ( DATA , READ_WRITE ),
185+             FileData .ofBaseDirPath ( SHARED_REPO , READ_WRITE ),
189186            // exclusive settings file 
190187            FileData .ofRelativePath (Path .of ("operator/settings.json" ), CONFIG , READ_WRITE ).withExclusive (true ),
191188
@@ -207,8 +204,8 @@ private static PolicyManager createPolicyManager() {
207204            FileData .ofPath (Path .of ("/proc/self/mountinfo" ), READ ).withPlatform (LINUX ),
208205            FileData .ofPath (Path .of ("/proc/diskstats" ), READ ).withPlatform (LINUX )
209206        );
210-         if  (bootstrapArgs .pidFile () != null ) {
211-             serverModuleFileDatas .add (FileData .ofPath (bootstrapArgs .pidFile (), READ_WRITE ));
207+         if  (pathLookup .pidFile () != null ) {
208+             serverModuleFileDatas .add (FileData .ofPath (pathLookup .pidFile (), READ_WRITE ));
212209        }
213210
214211        Collections .addAll (
@@ -220,8 +217,8 @@ private static PolicyManager createPolicyManager() {
220217                    new  FilesEntitlement (
221218                        List .of (
222219                            // TODO: what in es.base is accessing shared repo? 
223-                             FileData .ofRelativePath ( Path . of ( "" ),  SHARED_REPO , READ_WRITE ),
224-                             FileData .ofRelativePath ( Path . of ( "" ),  DATA , READ_WRITE )
220+                             FileData .ofBaseDirPath ( SHARED_REPO , READ_WRITE ),
221+                             FileData .ofBaseDirPath ( DATA , READ_WRITE )
225222                        )
226223                    )
227224                )
@@ -246,25 +243,17 @@ private static PolicyManager createPolicyManager() {
246243                List .of (
247244                    new  LoadNativeLibrariesEntitlement (),
248245                    new  ManageThreadsEntitlement (),
249-                     new  FilesEntitlement (
250-                         List .of (FileData .ofPath (bootstrapArgs .configDir (), READ ), FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE ))
251-                     )
246+                     new  FilesEntitlement (List .of (FileData .ofBaseDirPath (CONFIG , READ ), FileData .ofBaseDirPath (DATA , READ_WRITE )))
252247                )
253248            ),
254-             new  Scope (
255-                 "org.apache.lucene.misc" ,
256-                 List .of (new  FilesEntitlement (List .of (FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE ))))
257-             ),
249+             new  Scope ("org.apache.lucene.misc" , List .of (new  FilesEntitlement (List .of (FileData .ofBaseDirPath (DATA , READ_WRITE ))))),
258250            new  Scope (
259251                "org.apache.logging.log4j.core" ,
260-                 List .of (new  ManageThreadsEntitlement (), new  FilesEntitlement (List .of (FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ))))
252+                 List .of (new  ManageThreadsEntitlement (), new  FilesEntitlement (List .of (FileData .ofBaseDirPath ( LOGS , READ_WRITE ))))
261253            ),
262254            new  Scope (
263255                "org.elasticsearch.nativeaccess" ,
264-                 List .of (
265-                     new  LoadNativeLibrariesEntitlement (),
266-                     new  FilesEntitlement (List .of (FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE )))
267-                 )
256+                 List .of (new  LoadNativeLibrariesEntitlement (), new  FilesEntitlement (List .of (FileData .ofBaseDirPath (DATA , READ_WRITE ))))
268257            )
269258        );
270259
@@ -289,7 +278,7 @@ private static PolicyManager createPolicyManager() {
289278                new  Scope (
290279                    "org.bouncycastle.fips.core" ,
291280                    // read to lib dir is required for checksum validation 
292-                     List .of (new  FilesEntitlement (List .of (FileData .ofPath ( bootstrapArgs . libDir () , READ ))), new  ManageThreadsEntitlement ())
281+                     List .of (new  FilesEntitlement (List .of (FileData .ofBaseDirPath ( LIB , READ ))), new  ManageThreadsEntitlement ())
293282                )
294283            );
295284        }
@@ -313,21 +302,14 @@ private static PolicyManager createPolicyManager() {
313302            new  LoadNativeLibrariesEntitlement (),
314303            new  FilesEntitlement (
315304                List .of (
316-                     FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ),
305+                     FileData .ofBaseDirPath ( LOGS , READ_WRITE ),
317306                    FileData .ofPath (Path .of ("/proc/meminfo" ), READ ),
318307                    FileData .ofPath (Path .of ("/sys/fs/cgroup/" ), READ )
319308                )
320309            )
321310        );
322311
323-         validateFilesEntitlements (
324-             pluginPolicies ,
325-             pathLookup ,
326-             bootstrapArgs .configDir (),
327-             bootstrapArgs .pluginsDir (),
328-             bootstrapArgs .modulesDir (),
329-             bootstrapArgs .libDir ()
330-         );
312+         validateFilesEntitlements (pluginPolicies , pathLookup );
331313
332314        return  new  PolicyManager (
333315            serverPolicy ,
@@ -342,21 +324,14 @@ private static PolicyManager createPolicyManager() {
342324        );
343325    }
344326
345-     private  static  Set <Path > pathSet (Path ... paths ) {
346-         return  Arrays .stream (paths ).map (x  -> x .toAbsolutePath ().normalize ()).collect (Collectors .toUnmodifiableSet ());
347-     }
348- 
349327    // package visible for tests 
350-     static  void  validateFilesEntitlements (
351-         Map <String , Policy > pluginPolicies ,
352-         PathLookup  pathLookup ,
353-         Path  configDir ,
354-         Path  pluginsDir ,
355-         Path  modulesDir ,
356-         Path  libDir 
357-     ) {
358-         var  readAccessForbidden  = pathSet (pluginsDir , modulesDir , libDir );
359-         var  writeAccessForbidden  = pathSet (configDir );
328+     static  void  validateFilesEntitlements (Map <String , Policy > pluginPolicies , PathLookup  pathLookup ) {
329+         Set <Path > readAccessForbidden  = new  HashSet <>();
330+         pathLookup .getBaseDirPaths (PLUGINS ).forEach (p  -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
331+         pathLookup .getBaseDirPaths (MODULES ).forEach (p  -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
332+         pathLookup .getBaseDirPaths (LIB ).forEach (p  -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
333+         Set <Path > writeAccessForbidden  = new  HashSet <>();
334+         pathLookup .getBaseDirPaths (CONFIG ).forEach (p  -> writeAccessForbidden .add (p .toAbsolutePath ().normalize ()));
360335        for  (var  pluginPolicy  : pluginPolicies .entrySet ()) {
361336            for  (var  scope  : pluginPolicy .getValue ().scopes ()) {
362337                var  filesEntitlement  = scope .entitlements ()
0 commit comments