1010package  org .elasticsearch .entitlement .initialization ;
1111
1212import  org .elasticsearch .core .Booleans ;
13- import  org .elasticsearch .core .Strings ;
1413import  org .elasticsearch .entitlement .bootstrap .EntitlementBootstrap ;
1514import  org .elasticsearch .entitlement .bridge .EntitlementChecker ;
1615import  org .elasticsearch .entitlement .runtime .api .ElasticsearchEntitlementChecker ;
17- import  org .elasticsearch .entitlement .runtime .policy .FileAccessTree ;
1816import  org .elasticsearch .entitlement .runtime .policy .PathLookup ;
1917import  org .elasticsearch .entitlement .runtime .policy .Policy ;
2018import  org .elasticsearch .entitlement .runtime .policy .PolicyManager ;
3937import  java .nio .file .Path ;
4038import  java .util .ArrayList ;
4139import  java .util .Collections ;
42- import  java .util .HashSet ;
4340import  java .util .List ;
4441import  java .util .Map ;
4542import  java .util .Set ;
@@ -248,7 +245,7 @@ private static PolicyManager createPolicyManager() {
248245            )
249246        );
250247
251-         validateFilesEntitlements (pluginPolicies , pathLookup );
248+         FilesEntitlementsValidation . validate (pluginPolicies , pathLookup );
252249
253250        return  new  PolicyManager (
254251            serverPolicy ,
@@ -262,74 +259,6 @@ private static PolicyManager createPolicyManager() {
262259        );
263260    }
264261
265-     // package visible for tests 
266-     static  void  validateFilesEntitlements (Map <String , Policy > pluginPolicies , PathLookup  pathLookup ) {
267-         Set <Path > readAccessForbidden  = new  HashSet <>();
268-         pathLookup .getBaseDirPaths (PLUGINS ).forEach (p  -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
269-         pathLookup .getBaseDirPaths (MODULES ).forEach (p  -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
270-         pathLookup .getBaseDirPaths (LIB ).forEach (p  -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
271-         Set <Path > writeAccessForbidden  = new  HashSet <>();
272-         pathLookup .getBaseDirPaths (CONFIG ).forEach (p  -> writeAccessForbidden .add (p .toAbsolutePath ().normalize ()));
273-         for  (var  pluginPolicy  : pluginPolicies .entrySet ()) {
274-             for  (var  scope  : pluginPolicy .getValue ().scopes ()) {
275-                 var  filesEntitlement  = scope .entitlements ()
276-                     .stream ()
277-                     .filter (x  -> x  instanceof  FilesEntitlement )
278-                     .map (x  -> ((FilesEntitlement ) x ))
279-                     .findFirst ();
280-                 if  (filesEntitlement .isPresent ()) {
281-                     var  fileAccessTree  = FileAccessTree .withoutExclusivePaths (filesEntitlement .get (), pathLookup , null );
282-                     validateReadFilesEntitlements (pluginPolicy .getKey (), scope .moduleName (), fileAccessTree , readAccessForbidden );
283-                     validateWriteFilesEntitlements (pluginPolicy .getKey (), scope .moduleName (), fileAccessTree , writeAccessForbidden );
284-                 }
285-             }
286-         }
287-     }
288- 
289-     private  static  IllegalArgumentException  buildValidationException (
290-         String  componentName ,
291-         String  moduleName ,
292-         Path  forbiddenPath ,
293-         FilesEntitlement .Mode  mode 
294-     ) {
295-         return  new  IllegalArgumentException (
296-             Strings .format (
297-                 "policy for module [%s] in [%s] has an invalid file entitlement. Any path under [%s] is forbidden for mode [%s]." ,
298-                 moduleName ,
299-                 componentName ,
300-                 forbiddenPath ,
301-                 mode 
302-             )
303-         );
304-     }
305- 
306-     private  static  void  validateReadFilesEntitlements (
307-         String  componentName ,
308-         String  moduleName ,
309-         FileAccessTree  fileAccessTree ,
310-         Set <Path > readForbiddenPaths 
311-     ) {
312- 
313-         for  (Path  forbiddenPath  : readForbiddenPaths ) {
314-             if  (fileAccessTree .canRead (forbiddenPath )) {
315-                 throw  buildValidationException (componentName , moduleName , forbiddenPath , READ );
316-             }
317-         }
318-     }
319- 
320-     private  static  void  validateWriteFilesEntitlements (
321-         String  componentName ,
322-         String  moduleName ,
323-         FileAccessTree  fileAccessTree ,
324-         Set <Path > writeForbiddenPaths 
325-     ) {
326-         for  (Path  forbiddenPath  : writeForbiddenPaths ) {
327-             if  (fileAccessTree .canWrite (forbiddenPath )) {
328-                 throw  buildValidationException (componentName , moduleName , forbiddenPath , READ_WRITE );
329-             }
330-         }
331-     }
332- 
333262    /** 
334263     * If bytecode verification is enabled, ensure these classes get loaded before transforming/retransforming them. 
335264     * For these classes, the order in which we transform and verify them matters. Verification during class transformation is at least an 
0 commit comments