9
9
10
10
package org .elasticsearch .bootstrap ;
11
11
12
- import org .elasticsearch .core .Nullable ;
13
12
import org .elasticsearch .core .SuppressForbidden ;
14
- import org .elasticsearch .entitlement .runtime .policy .PolicyManager . PolicyScope ;
13
+ import org .elasticsearch .entitlement .runtime .policy .PolicyManager ;
15
14
import org .elasticsearch .logging .LogManager ;
16
15
import org .elasticsearch .logging .Logger ;
17
16
18
- import java .lang .module .ModuleDescriptor ;
19
- import java .lang .module .ModuleFinder ;
20
17
import java .net .MalformedURLException ;
21
18
import java .net .URL ;
22
19
import java .util .List ;
25
22
import java .util .TreeMap ;
26
23
import java .util .function .Function ;
27
24
28
- import static java .util .Objects .requireNonNull ;
29
- import static java .util .stream .Collectors .toSet ;
30
25
import static org .elasticsearch .entitlement .runtime .policy .PolicyManager .ALL_UNNAMED ;
31
26
import static org .elasticsearch .entitlement .runtime .policy .PolicyManager .ComponentKind .PLUGIN ;
32
- import static org .elasticsearch .entitlement .runtime .policy .PolicyManager .ComponentKind .SERVER ;
33
- import static org .elasticsearch .entitlement .runtime .policy .PolicyManager .MODULES_EXCLUDED_FROM_SYSTEM_MODULES ;
34
27
35
- public final class TestScopeResolver {
28
+ public record TestScopeResolver ( Map < String , PolicyManager . PolicyScope > scopeMap ) {
36
29
37
30
private static final Logger logger = LogManager .getLogger (TestScopeResolver .class );
38
- private final Map <String , PolicyScope > scopeMap ;
39
- private static final Map <String , PolicyScope > excludedSystemPackageScopes = computeExcludedSystemPackageScopes ();
40
31
41
- public TestScopeResolver (Map <String , PolicyScope > scopeMap ) {
42
- this .scopeMap = scopeMap ;
43
- }
44
-
45
- private static Map <String , PolicyScope > computeExcludedSystemPackageScopes () {
46
- // Within any one module layer, module names are unique, so we just need the names
47
- Set <String > systemModuleNames = ModuleFinder .ofSystem ()
48
- .findAll ()
49
- .stream ()
50
- .map (ref -> ref .descriptor ().name ())
51
- .filter (MODULES_EXCLUDED_FROM_SYSTEM_MODULES ::contains )
52
- .collect (toSet ());
53
-
54
- Map <String , PolicyScope > result = new TreeMap <>();
55
- ModuleLayer .boot ().modules ().stream ().filter (m -> systemModuleNames .contains (m .getName ())).forEach (m -> {
56
- ModuleDescriptor desc = m .getDescriptor ();
57
- if (desc != null ) {
58
- desc .packages ().forEach (pkg ->
59
- // Our component identification logic returns SERVER for these
60
- result .put (pkg , new PolicyScope (SERVER , SERVER .componentName , m .getName ())));
61
- }
62
- });
63
- return result ;
64
- }
65
-
66
- public static @ Nullable PolicyScope getExcludedSystemPackageScope (Class <?> callerClass ) {
67
- return excludedSystemPackageScopes .get (callerClass .getPackageName ());
68
- }
69
-
70
- PolicyScope getScope (Class <?> callerClass ) {
32
+ PolicyManager .PolicyScope getScope (Class <?> callerClass ) {
71
33
var callerCodeSource = callerClass .getProtectionDomain ().getCodeSource ();
72
- if (callerCodeSource == null ) {
73
- // This only happens for JDK classes. Furthermore, for trivially allowed modules, we shouldn't even get here.
74
- // Hence, this must be an excluded system module, so check for that.
75
- return requireNonNull (getExcludedSystemPackageScope (callerClass ));
76
- }
34
+ assert callerCodeSource != null ;
77
35
78
36
var location = callerCodeSource .getLocation ().toString ();
79
37
var scope = scopeMap .get (location );
80
38
if (scope == null ) {
81
39
// Special cases for libraries not handled by our automatically-generated scopeMap
82
40
if (callerClass .getPackageName ().startsWith ("org.bouncycastle" )) {
83
- scope = new PolicyScope (PLUGIN , "security" , ALL_UNNAMED );
41
+ scope = new PolicyManager . PolicyScope (PLUGIN , "security" , ALL_UNNAMED );
84
42
logger .debug ("Assuming bouncycastle is part of the security plugin" );
85
43
}
86
44
}
87
45
if (scope == null ) {
88
46
logger .warn ("Cannot identify a scope for class [{}], location [{}]" , callerClass .getName (), location );
89
- return PolicyScope .unknown (location );
47
+ return PolicyManager . PolicyScope .unknown (location );
90
48
}
91
49
return scope ;
92
50
}
93
51
94
- public static Function <Class <?>, PolicyScope > createScopeResolver (
52
+ public static Function <Class <?>, PolicyManager . PolicyScope > createScopeResolver (
95
53
TestBuildInfo serverBuildInfo ,
96
54
List <TestBuildInfo > pluginsBuildInfo ,
97
55
Set <String > modularPlugins
98
56
) {
99
- Map <String , PolicyScope > scopeMap = new TreeMap <>(); // Sorted to make it easier to read during debugging
57
+ Map <String , PolicyManager . PolicyScope > scopeMap = new TreeMap <>(); // Sorted to make it easier to read during debugging
100
58
for (var pluginBuildInfo : pluginsBuildInfo ) {
101
59
boolean isModular = modularPlugins .contains (pluginBuildInfo .component ());
102
60
for (var location : pluginBuildInfo .locations ()) {
@@ -108,7 +66,7 @@ public static Function<Class<?>, PolicyScope> createScopeResolver(
108
66
String module = isModular ? location .module () : ALL_UNNAMED ;
109
67
scopeMap .put (
110
68
getCodeSource (codeSource , location .representativeClass ()),
111
- PolicyScope .plugin (pluginBuildInfo .component (), module )
69
+ PolicyManager . PolicyScope .plugin (pluginBuildInfo .component (), module )
112
70
);
113
71
} catch (MalformedURLException e ) {
114
72
throw new IllegalArgumentException ("Cannot locate class [" + location .representativeClass () + "]" , e );
@@ -123,7 +81,7 @@ public static Function<Class<?>, PolicyScope> createScopeResolver(
123
81
continue ;
124
82
}
125
83
try {
126
- scopeMap .put (getCodeSource (classUrl , location .representativeClass ()), PolicyScope .server (location .module ()));
84
+ scopeMap .put (getCodeSource (classUrl , location .representativeClass ()), PolicyManager . PolicyScope .server (location .module ()));
127
85
} catch (MalformedURLException e ) {
128
86
throw new IllegalArgumentException ("Cannot locate class [" + location .representativeClass () + "]" , e );
129
87
}
0 commit comments