|
34 | 34 | import java.lang.StackWalker.StackFrame; |
35 | 35 | import java.lang.module.ModuleFinder; |
36 | 36 | import java.lang.module.ModuleReference; |
| 37 | +import java.lang.module.ResolvedModule; |
37 | 38 | import java.nio.file.Path; |
38 | 39 | import java.util.ArrayList; |
39 | 40 | import java.util.HashSet; |
@@ -123,20 +124,10 @@ ModuleEntitlements policyEntitlements(String componentName, String moduleName, L |
123 | 124 |
|
124 | 125 | public static final String ALL_UNNAMED = "ALL-UNNAMED"; |
125 | 126 |
|
126 | | - private static final Set<Module> systemModules = findSystemModules(); |
| 127 | + private static final Set<ModuleReference> systemModules = findSystemModules(); |
127 | 128 |
|
128 | | - private static Set<Module> findSystemModules() { |
129 | | - var systemModulesDescriptors = ModuleFinder.ofSystem() |
130 | | - .findAll() |
131 | | - .stream() |
132 | | - .map(ModuleReference::descriptor) |
133 | | - .collect(Collectors.toUnmodifiableSet()); |
134 | | - return Stream.concat( |
135 | | - // entitlements is a "system" module, we can do anything from it |
136 | | - Stream.of(PolicyManager.class.getModule()), |
137 | | - // anything in the boot layer is also part of the system |
138 | | - ModuleLayer.boot().modules().stream().filter(m -> systemModulesDescriptors.contains(m.getDescriptor())) |
139 | | - ).collect(Collectors.toUnmodifiableSet()); |
| 129 | + private static Set<ModuleReference> findSystemModules() { |
| 130 | + return ModuleFinder.ofSystem().findAll().stream().collect(Collectors.toUnmodifiableSet()); |
140 | 131 | } |
141 | 132 |
|
142 | 133 | /** |
@@ -615,14 +606,31 @@ private static boolean isTriviallyAllowed(Class<?> requestingClass) { |
615 | 606 | logger.debug("Entitlement trivially allowed: no caller frames outside the entitlement library"); |
616 | 607 | return true; |
617 | 608 | } |
618 | | - if (systemModules.contains(requestingClass.getModule())) { |
| 609 | + if (isSystemModule(requestingClass.getModule())) { |
619 | 610 | logger.debug("Entitlement trivially allowed from system module [{}]", requestingClass.getModule().getName()); |
620 | 611 | return true; |
621 | 612 | } |
622 | 613 | logger.trace("Entitlement not trivially allowed"); |
623 | 614 | return false; |
624 | 615 | } |
625 | 616 |
|
| 617 | + /** |
| 618 | + * Determines if the given {@link Module} is a system module. That is, a module that is included as part of the Java runtime. |
| 619 | + */ |
| 620 | + private static boolean isSystemModule(Module module) { |
| 621 | + if (module.getLayer() == null) { |
| 622 | + // This is an unnamed module, so not a system module |
| 623 | + return false; |
| 624 | + } |
| 625 | + |
| 626 | + ModuleReference moduleReference = module.getLayer() |
| 627 | + .configuration() |
| 628 | + .findModule(module.getName()) |
| 629 | + .map(ResolvedModule::reference) |
| 630 | + .orElse(null); |
| 631 | + return module == PolicyManager.class.getModule() || (moduleReference != null && systemModules.contains(moduleReference)); |
| 632 | + } |
| 633 | + |
626 | 634 | @Override |
627 | 635 | public String toString() { |
628 | 636 | return "PolicyManager{" + "serverEntitlements=" + serverEntitlements + ", pluginsEntitlements=" + pluginsEntitlements + '}'; |
|
0 commit comments