3535import java .lang .module .ModuleFinder ;
3636import java .lang .module .ModuleReference ;
3737import java .nio .file .Path ;
38+ import java .nio .file .Paths ;
3839import java .util .ArrayList ;
3940import java .util .HashSet ;
4041import java .util .List ;
@@ -91,13 +92,17 @@ public <E extends Entitlement> Stream<E> getEntitlements(Class<E> entitlementCla
9192 }
9293 }
9394
95+ private FileAccessTree getDefaultFileAccess (String componentName , Path componentPath ) {
96+ return FileAccessTree .of (componentName , UNKNOWN_COMPONENT_NAME , FilesEntitlement .EMPTY , pathLookup , componentPath , List .of ());
97+ }
98+
9499 // pkg private for testing
95- ModuleEntitlements defaultEntitlements (String componentName ) {
96- return new ModuleEntitlements (componentName , Map .of (), defaultFileAccess );
100+ ModuleEntitlements defaultEntitlements (String componentName , Path componentPath ) {
101+ return new ModuleEntitlements (componentName , Map .of (), getDefaultFileAccess ( componentName , componentPath ) );
97102 }
98103
99104 // pkg private for testing
100- ModuleEntitlements policyEntitlements (String componentName , String moduleName , List <Entitlement > entitlements ) {
105+ ModuleEntitlements policyEntitlements (String componentName , Path componentPath , String moduleName , List <Entitlement > entitlements ) {
101106 FilesEntitlement filesEntitlement = FilesEntitlement .EMPTY ;
102107 for (Entitlement entitlement : entitlements ) {
103108 if (entitlement instanceof FilesEntitlement ) {
@@ -107,7 +112,7 @@ ModuleEntitlements policyEntitlements(String componentName, String moduleName, L
107112 return new ModuleEntitlements (
108113 componentName ,
109114 entitlements .stream ().collect (groupingBy (Entitlement ::getClass )),
110- FileAccessTree .of (componentName , moduleName , filesEntitlement , pathLookup , exclusivePaths )
115+ FileAccessTree .of (componentName , moduleName , filesEntitlement , pathLookup , componentPath , exclusivePaths )
111116 );
112117 }
113118
@@ -118,7 +123,6 @@ ModuleEntitlements policyEntitlements(String componentName, String moduleName, L
118123 private final Map <String , Map <String , List <Entitlement >>> pluginsEntitlements ;
119124 private final Function <Class <?>, String > pluginResolver ;
120125 private final PathLookup pathLookup ;
121- private final FileAccessTree defaultFileAccess ;
122126 private final Set <Class <?>> mutedClasses ;
123127
124128 public static final String ALL_UNNAMED = "ALL-UNNAMED" ;
@@ -139,6 +143,7 @@ private static Set<Module> findSystemModules() {
139143 ).collect (Collectors .toUnmodifiableSet ());
140144 }
141145
146+ private final Map <String , Path > sourcePaths ;
142147 /**
143148 * The package name containing classes from the APM agent.
144149 */
@@ -161,6 +166,7 @@ public PolicyManager(
161166 List <Entitlement > apmAgentEntitlements ,
162167 Map <String , Policy > pluginPolicies ,
163168 Function <Class <?>, String > pluginResolver ,
169+ Map <String , Path > sourcePaths ,
164170 String apmAgentPackageName ,
165171 Module entitlementsModule ,
166172 PathLookup pathLookup ,
@@ -172,16 +178,10 @@ public PolicyManager(
172178 .stream ()
173179 .collect (toUnmodifiableMap (Map .Entry ::getKey , e -> buildScopeEntitlementsMap (e .getValue ())));
174180 this .pluginResolver = pluginResolver ;
181+ this .sourcePaths = sourcePaths ;
175182 this .apmAgentPackageName = apmAgentPackageName ;
176183 this .entitlementsModule = entitlementsModule ;
177184 this .pathLookup = requireNonNull (pathLookup );
178- this .defaultFileAccess = FileAccessTree .of (
179- UNKNOWN_COMPONENT_NAME ,
180- UNKNOWN_COMPONENT_NAME ,
181- FilesEntitlement .EMPTY ,
182- pathLookup ,
183- List .of ()
184- );
185185 this .mutedClasses = suppressFailureLogClasses ;
186186
187187 List <ExclusiveFileEntitlement > exclusiveFileEntitlements = new ArrayList <>();
@@ -529,44 +529,81 @@ ModuleEntitlements getEntitlements(Class<?> requestingClass) {
529529 private ModuleEntitlements computeEntitlements (Class <?> requestingClass ) {
530530 Module requestingModule = requestingClass .getModule ();
531531 if (isServerModule (requestingModule )) {
532- return getModuleScopeEntitlements (serverEntitlements , requestingModule .getName (), SERVER_COMPONENT_NAME );
532+ return getModuleScopeEntitlements (
533+ serverEntitlements ,
534+ requestingModule .getName (),
535+ SERVER_COMPONENT_NAME ,
536+ getComponentPathFromClass (requestingClass )
537+ );
533538 }
534539
535540 // plugins
536541 var pluginName = pluginResolver .apply (requestingClass );
537542 if (pluginName != null ) {
538543 var pluginEntitlements = pluginsEntitlements .get (pluginName );
539544 if (pluginEntitlements == null ) {
540- return defaultEntitlements (pluginName );
545+ return defaultEntitlements (pluginName , sourcePaths . get ( pluginName ) );
541546 } else {
542- final String scopeName ;
543- if (requestingModule .isNamed () == false ) {
544- scopeName = ALL_UNNAMED ;
545- } else {
546- scopeName = requestingModule .getName ();
547- }
548- return getModuleScopeEntitlements (pluginEntitlements , scopeName , pluginName );
547+ return getModuleScopeEntitlements (
548+ pluginEntitlements ,
549+ getScopeName (requestingModule ),
550+ pluginName ,
551+ sourcePaths .get (pluginName )
552+ );
549553 }
550554 }
551555
552556 if (requestingModule .isNamed () == false && requestingClass .getPackageName ().startsWith (apmAgentPackageName )) {
553557 // The APM agent is the only thing running non-modular in the system classloader
554- return policyEntitlements (APM_AGENT_COMPONENT_NAME , ALL_UNNAMED , apmAgentEntitlements );
558+ return policyEntitlements (
559+ APM_AGENT_COMPONENT_NAME ,
560+ getComponentPathFromClass (requestingClass ),
561+ ALL_UNNAMED ,
562+ apmAgentEntitlements
563+ );
564+ }
565+
566+ return defaultEntitlements (UNKNOWN_COMPONENT_NAME , null );
567+ }
568+
569+ private static String getScopeName (Module requestingModule ) {
570+ if (requestingModule .isNamed () == false ) {
571+ return ALL_UNNAMED ;
572+ } else {
573+ return requestingModule .getName ();
555574 }
575+ }
556576
557- return defaultEntitlements (UNKNOWN_COMPONENT_NAME );
577+ // pkg private for testing
578+ static Path getComponentPathFromClass (Class <?> requestingClass ) {
579+ var codeSource = requestingClass .getProtectionDomain ().getCodeSource ();
580+ if (codeSource == null ) {
581+ return null ;
582+ }
583+ try {
584+ return Paths .get (codeSource .getLocation ().toURI ());
585+ } catch (Exception e ) {
586+ // If we get a URISyntaxException, or any other Exception due to an invalid URI, we return null to safely skip this location
587+ logger .info (
588+ "Cannot get component path for [{}]: [{}] cannot be converted to a valid Path" ,
589+ requestingClass .getName (),
590+ codeSource .getLocation ().toString ()
591+ );
592+ return null ;
593+ }
558594 }
559595
560596 private ModuleEntitlements getModuleScopeEntitlements (
561597 Map <String , List <Entitlement >> scopeEntitlements ,
562- String moduleName ,
563- String componentName
598+ String scopeName ,
599+ String componentName ,
600+ Path componentPath
564601 ) {
565- var entitlements = scopeEntitlements .get (moduleName );
602+ var entitlements = scopeEntitlements .get (scopeName );
566603 if (entitlements == null ) {
567- return defaultEntitlements (componentName );
604+ return defaultEntitlements (componentName , componentPath );
568605 }
569- return policyEntitlements (componentName , moduleName , entitlements );
606+ return policyEntitlements (componentName , componentPath , scopeName , entitlements );
570607 }
571608
572609 private static boolean isServerModule (Module requestingModule ) {
0 commit comments