1111
1212import org .elasticsearch .core .Booleans ;
1313import org .elasticsearch .core .PathUtils ;
14- import org .elasticsearch .core .Strings ;
1514import org .elasticsearch .core .internal .provider .ProviderLocator ;
1615import org .elasticsearch .entitlement .bootstrap .EntitlementBootstrap ;
1716import org .elasticsearch .entitlement .bridge .EntitlementChecker ;
2120import org .elasticsearch .entitlement .instrumentation .MethodKey ;
2221import org .elasticsearch .entitlement .instrumentation .Transformer ;
2322import org .elasticsearch .entitlement .runtime .api .ElasticsearchEntitlementChecker ;
24- import org .elasticsearch .entitlement .runtime .policy .FileAccessTree ;
2523import org .elasticsearch .entitlement .runtime .policy .PathLookup ;
2624import org .elasticsearch .entitlement .runtime .policy .Policy ;
2725import org .elasticsearch .entitlement .runtime .policy .PolicyManager ;
6058import java .util .ArrayList ;
6159import java .util .Collections ;
6260import java .util .HashMap ;
63- import java .util .HashSet ;
6461import java .util .List ;
6562import java .util .Map ;
6663import java .util .Set ;
@@ -345,7 +342,7 @@ private static PolicyManager createPolicyManager() {
345342 )
346343 );
347344
348- validateFilesEntitlements (pluginPolicies , pathLookup );
345+ FilesEntitlementsValidation . validate (pluginPolicies , pathLookup );
349346
350347 return new PolicyManager (
351348 serverPolicy ,
@@ -359,74 +356,6 @@ private static PolicyManager createPolicyManager() {
359356 );
360357 }
361358
362- // package visible for tests
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 ()));
370- for (var pluginPolicy : pluginPolicies .entrySet ()) {
371- for (var scope : pluginPolicy .getValue ().scopes ()) {
372- var filesEntitlement = scope .entitlements ()
373- .stream ()
374- .filter (x -> x instanceof FilesEntitlement )
375- .map (x -> ((FilesEntitlement ) x ))
376- .findFirst ();
377- if (filesEntitlement .isPresent ()) {
378- var fileAccessTree = FileAccessTree .withoutExclusivePaths (filesEntitlement .get (), pathLookup , null );
379- validateReadFilesEntitlements (pluginPolicy .getKey (), scope .moduleName (), fileAccessTree , readAccessForbidden );
380- validateWriteFilesEntitlements (pluginPolicy .getKey (), scope .moduleName (), fileAccessTree , writeAccessForbidden );
381- }
382- }
383- }
384- }
385-
386- private static IllegalArgumentException buildValidationException (
387- String componentName ,
388- String moduleName ,
389- Path forbiddenPath ,
390- FilesEntitlement .Mode mode
391- ) {
392- return new IllegalArgumentException (
393- Strings .format (
394- "policy for module [%s] in [%s] has an invalid file entitlement. Any path under [%s] is forbidden for mode [%s]." ,
395- moduleName ,
396- componentName ,
397- forbiddenPath ,
398- mode
399- )
400- );
401- }
402-
403- private static void validateReadFilesEntitlements (
404- String componentName ,
405- String moduleName ,
406- FileAccessTree fileAccessTree ,
407- Set <Path > readForbiddenPaths
408- ) {
409-
410- for (Path forbiddenPath : readForbiddenPaths ) {
411- if (fileAccessTree .canRead (forbiddenPath )) {
412- throw buildValidationException (componentName , moduleName , forbiddenPath , READ );
413- }
414- }
415- }
416-
417- private static void validateWriteFilesEntitlements (
418- String componentName ,
419- String moduleName ,
420- FileAccessTree fileAccessTree ,
421- Set <Path > writeForbiddenPaths
422- ) {
423- for (Path forbiddenPath : writeForbiddenPaths ) {
424- if (fileAccessTree .canWrite (forbiddenPath )) {
425- throw buildValidationException (componentName , moduleName , forbiddenPath , READ_WRITE );
426- }
427- }
428- }
429-
430359 private static Path getUserHome () {
431360 String userHome = System .getProperty ("user.home" );
432361 if (userHome == null ) {
0 commit comments