- 
                Notifications
    
You must be signed in to change notification settings  - Fork 25.6k
 
Improved PolicyManager error reporting #121824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      e95097e
              
                Report componentName in ModuleEntitlements.
              
              
                prdoyle a53369d
              
                Include component names in errors and logs
              
              
                prdoyle 4914936
              
                Name APM agent specifically.
              
              
                prdoyle 43c7f55
              
                Entitlement component names package private
              
              
                prdoyle 982430d
              
                [CI] Auto commit changes from spotless
              
              
                 4686965
              
                Merge branch 'main' into policymanager-error-reporting
              
              
                prdoyle File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -50,19 +50,35 @@ | |
| public class PolicyManager { | ||
| private static final Logger logger = LogManager.getLogger(PolicyManager.class); | ||
| 
     | 
||
| record ModuleEntitlements(Map<Class<? extends Entitlement>, List<Entitlement>> entitlementsByType, FileAccessTree fileAccess) { | ||
| public static final ModuleEntitlements NONE = new ModuleEntitlements(Map.of(), FileAccessTree.EMPTY); | ||
| public static final String UNKNOWN_COMPONENT_NAME = "(unknown)"; | ||
| public static final String SERVER_COMPONENT_NAME = "(server)"; | ||
| public static final String AGENT_COMPONENT_NAME = "(agent)"; | ||
                
       | 
||
| 
     | 
||
| /** | ||
| * @param componentName the plugin name; or else one of the special component names | ||
| * like {@link #SERVER_COMPONENT_NAME} or {@link #AGENT_COMPONENT_NAME}. | ||
| */ | ||
| record ModuleEntitlements( | ||
| String componentName, | ||
| Map<Class<? extends Entitlement>, List<Entitlement>> entitlementsByType, | ||
| FileAccessTree fileAccess | ||
| ) { | ||
| 
     | 
||
| ModuleEntitlements { | ||
| entitlementsByType = Map.copyOf(entitlementsByType); | ||
| } | ||
| 
     | 
||
| public static ModuleEntitlements from(List<Entitlement> entitlements) { | ||
| public static ModuleEntitlements none(String componentName) { | ||
| return new ModuleEntitlements(componentName, Map.of(), FileAccessTree.EMPTY); | ||
| } | ||
| 
     | 
||
| public static ModuleEntitlements from(String componentName, List<Entitlement> entitlements) { | ||
| var fileEntitlements = entitlements.stream() | ||
| .filter(e -> e.getClass().equals(FileEntitlement.class)) | ||
| .map(e -> (FileEntitlement) e) | ||
| .toList(); | ||
| return new ModuleEntitlements( | ||
| componentName, | ||
| entitlements.stream().collect(groupingBy(Entitlement::getClass)), | ||
| FileAccessTree.of(fileEntitlements) | ||
| ); | ||
| 
          
            
          
           | 
    @@ -184,9 +200,10 @@ private void neverEntitled(Class<?> callerClass, Supplier<String> operationDescr | |
| 
     | 
||
| throw new NotEntitledException( | ||
| Strings.format( | ||
| "Not entitled: caller [%s], module [%s], operation [%s]", | ||
| callerClass, | ||
| requestingClass.getModule() == null ? "<none>" : requestingClass.getModule().getName(), | ||
| "Not entitled: component [%s], module [%s], class [%s], operation [%s]", | ||
| getEntitlements(requestingClass).componentName(), | ||
| requestingClass.getModule().getName(), | ||
| requestingClass, | ||
| operationDescription.get() | ||
| ) | ||
| ); | ||
| 
          
            
          
           | 
    @@ -240,9 +257,10 @@ public void checkFileRead(Class<?> callerClass, Path path) { | |
| if (entitlements.fileAccess().canRead(path) == false) { | ||
| throw new NotEntitledException( | ||
| Strings.format( | ||
| "Not entitled: caller [%s], module [%s], entitlement [file], operation [read], path [%s]", | ||
| callerClass, | ||
| "Not entitled: component [%s], module [%s], class [%s], entitlement [file], operation [read], path [%s]", | ||
| entitlements.componentName(), | ||
| requestingClass.getModule(), | ||
| requestingClass, | ||
| path | ||
| ) | ||
| ); | ||
| 
        
          
        
         | 
    @@ -264,9 +282,10 @@ public void checkFileWrite(Class<?> callerClass, Path path) { | |
| if (entitlements.fileAccess().canWrite(path) == false) { | ||
| throw new NotEntitledException( | ||
| Strings.format( | ||
| "Not entitled: caller [%s], module [%s], entitlement [file], operation [write], path [%s]", | ||
| callerClass, | ||
| "Not entitled: component [%s], module [%s], class [%s], entitlement [file], operation [write], path [%s]", | ||
| entitlements.componentName(), | ||
| requestingClass.getModule(), | ||
| requestingClass, | ||
| path | ||
| ) | ||
| ); | ||
| 
          
            
          
           | 
    @@ -300,30 +319,33 @@ public void checkAllNetworkAccess(Class<?> callerClass) { | |
| } | ||
| 
     | 
||
| var classEntitlements = getEntitlements(requestingClass); | ||
| if (classEntitlements.hasEntitlement(InboundNetworkEntitlement.class) == false) { | ||
| throw new NotEntitledException( | ||
| Strings.format( | ||
| "Missing entitlement: class [%s], module [%s], entitlement [inbound_network]", | ||
| requestingClass, | ||
| requestingClass.getModule().getName() | ||
| ) | ||
| ); | ||
| } | ||
| checkFlagEntitlement(classEntitlements, InboundNetworkEntitlement.class, requestingClass); | ||
| checkFlagEntitlement(classEntitlements, OutboundNetworkEntitlement.class, requestingClass); | ||
| } | ||
| 
     | 
||
| if (classEntitlements.hasEntitlement(OutboundNetworkEntitlement.class) == false) { | ||
| private static void checkFlagEntitlement( | ||
| ModuleEntitlements classEntitlements, | ||
| Class<? extends Entitlement> entitlementClass, | ||
| Class<?> requestingClass | ||
| ) { | ||
| if (classEntitlements.hasEntitlement(entitlementClass) == false) { | ||
| throw new NotEntitledException( | ||
| Strings.format( | ||
| "Missing entitlement: class [%s], module [%s], entitlement [outbound_network]", | ||
| "Not entitled: component [%s], module [%s], class [%s], entitlement [%s]", | ||
| classEntitlements.componentName(), | ||
| requestingClass.getModule().getName(), | ||
| requestingClass, | ||
| requestingClass.getModule().getName() | ||
| PolicyParser.getEntitlementTypeName(entitlementClass) | ||
| ) | ||
| ); | ||
| } | ||
| logger.debug( | ||
| () -> Strings.format( | ||
| "Entitled: class [%s], module [%s], entitlements [inbound_network, outbound_network]", | ||
| "Entitled: component [%s], module [%s], class [%s], entitlement [%s]", | ||
| classEntitlements.componentName(), | ||
| requestingClass.getModule().getName(), | ||
| requestingClass, | ||
| requestingClass.getModule().getName() | ||
| PolicyParser.getEntitlementTypeName(entitlementClass) | ||
| ) | ||
| ); | ||
| } | ||
| 
        
          
        
         | 
    @@ -338,19 +360,21 @@ public void checkWriteProperty(Class<?> callerClass, String property) { | |
| if (entitlements.getEntitlements(WriteSystemPropertiesEntitlement.class).anyMatch(e -> e.properties().contains(property))) { | ||
| logger.debug( | ||
| () -> Strings.format( | ||
| "Entitled: class [%s], module [%s], entitlement [write_system_properties], property [%s]", | ||
| requestingClass, | ||
| "Entitled: component [%s], module [%s], class [%s], entitlement [write_system_properties], property [%s]", | ||
| entitlements.componentName(), | ||
| requestingClass.getModule().getName(), | ||
| requestingClass, | ||
| property | ||
| ) | ||
| ); | ||
| return; | ||
| } | ||
| throw new NotEntitledException( | ||
| Strings.format( | ||
| "Missing entitlement: class [%s], module [%s], entitlement [write_system_properties], property [%s]", | ||
| requestingClass, | ||
| "Not entitled: component [%s], module [%s], class [%s], entitlement [write_system_properties], property [%s]", | ||
| entitlements.componentName(), | ||
| requestingClass.getModule().getName(), | ||
| requestingClass, | ||
| property | ||
| ) | ||
| ); | ||
| 
        
          
        
         | 
    @@ -361,27 +385,7 @@ private void checkEntitlementPresent(Class<?> callerClass, Class<? extends Entit | |
| if (isTriviallyAllowed(requestingClass)) { | ||
| return; | ||
| } | ||
| 
     | 
||
| ModuleEntitlements entitlements = getEntitlements(requestingClass); | ||
| if (entitlements.hasEntitlement(entitlementClass)) { | ||
| logger.debug( | ||
| () -> Strings.format( | ||
| "Entitled: class [%s], module [%s], entitlement [%s]", | ||
| requestingClass, | ||
| requestingClass.getModule().getName(), | ||
| PolicyParser.getEntitlementTypeName(entitlementClass) | ||
| ) | ||
| ); | ||
| return; | ||
| } | ||
| throw new NotEntitledException( | ||
| Strings.format( | ||
| "Missing entitlement: class [%s], module [%s], entitlement [%s]", | ||
| requestingClass, | ||
| requestingClass.getModule().getName(), | ||
| PolicyParser.getEntitlementTypeName(entitlementClass) | ||
| ) | ||
| ); | ||
| checkFlagEntitlement(getEntitlements(requestingClass), entitlementClass, requestingClass); | ||
| } | ||
| 
     | 
||
| ModuleEntitlements getEntitlements(Class<?> requestingClass) { | ||
| 
        
          
        
         | 
    @@ -391,47 +395,44 @@ ModuleEntitlements getEntitlements(Class<?> requestingClass) { | |
| private ModuleEntitlements computeEntitlements(Class<?> requestingClass) { | ||
| Module requestingModule = requestingClass.getModule(); | ||
| if (isServerModule(requestingModule)) { | ||
| return getModuleScopeEntitlements(requestingClass, serverEntitlements, requestingModule.getName(), "server"); | ||
| return getModuleScopeEntitlements(serverEntitlements, requestingModule.getName(), SERVER_COMPONENT_NAME); | ||
| } | ||
| 
     | 
||
| // plugins | ||
| var pluginName = pluginResolver.apply(requestingClass); | ||
| if (pluginName != null) { | ||
| var pluginEntitlements = pluginsEntitlements.get(pluginName); | ||
| if (pluginEntitlements == null) { | ||
| return ModuleEntitlements.NONE; | ||
| return ModuleEntitlements.none(pluginName); | ||
| } else { | ||
| final String scopeName; | ||
| if (requestingModule.isNamed() == false) { | ||
| scopeName = ALL_UNNAMED; | ||
| } else { | ||
| scopeName = requestingModule.getName(); | ||
| } | ||
| return getModuleScopeEntitlements(requestingClass, pluginEntitlements, scopeName, pluginName); | ||
| return getModuleScopeEntitlements(pluginEntitlements, scopeName, pluginName); | ||
| } | ||
| } | ||
| 
     | 
||
| if (requestingModule.isNamed() == false && requestingClass.getPackageName().startsWith(agentsPackageName)) { | ||
| // agents are the only thing running non-modular in the system classloader | ||
| return ModuleEntitlements.from(agentEntitlements); | ||
| return ModuleEntitlements.from(AGENT_COMPONENT_NAME, agentEntitlements); | ||
| } | ||
| 
     | 
||
| logger.warn("No applicable entitlement policy for class [{}]", requestingClass.getName()); | ||
| return ModuleEntitlements.NONE; | ||
| return ModuleEntitlements.none(UNKNOWN_COMPONENT_NAME); | ||
| } | ||
| 
     | 
||
| private ModuleEntitlements getModuleScopeEntitlements( | ||
| Class<?> callerClass, | ||
| Map<String, List<Entitlement>> scopeEntitlements, | ||
| String moduleName, | ||
| String component | ||
| String componentName | ||
| ) { | ||
| var entitlements = scopeEntitlements.get(moduleName); | ||
| if (entitlements == null) { | ||
| logger.warn("No applicable entitlement policy for [{}], module [{}], class [{}]", component, moduleName, callerClass); | ||
| return ModuleEntitlements.NONE; | ||
| return ModuleEntitlements.none(componentName); | ||
| } | ||
| return ModuleEntitlements.from(entitlements); | ||
| return ModuleEntitlements.from(componentName, entitlements); | ||
| } | ||
| 
     | 
||
| private static boolean isServerModule(Module requestingModule) { | ||
| 
          
            
          
           | 
    ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can all be at most package private?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh true. I think they're public from a prior revision of this code.