9
9
10
10
package org .elasticsearch .entitlement .bootstrap ;
11
11
12
- import org .elasticsearch .entitlement .initialization .TestEntitlementInitialization ;
12
+ import org .elasticsearch .bootstrap .TestBuildInfo ;
13
+ import org .elasticsearch .bootstrap .TestBuildInfoParser ;
14
+ import org .elasticsearch .bootstrap .TestScopeResolver ;
15
+ import org .elasticsearch .core .Strings ;
16
+ import org .elasticsearch .core .SuppressForbidden ;
17
+ import org .elasticsearch .entitlement .initialization .EntitlementInitialization ;
13
18
import org .elasticsearch .entitlement .runtime .policy .PathLookup ;
19
+ import org .elasticsearch .entitlement .runtime .policy .Policy ;
20
+ import org .elasticsearch .entitlement .runtime .policy .PolicyManager ;
21
+ import org .elasticsearch .entitlement .runtime .policy .PolicyParser ;
22
+ import org .elasticsearch .entitlement .runtime .policy .TestPolicyManager ;
14
23
import org .elasticsearch .logging .LogManager ;
15
24
import org .elasticsearch .logging .Logger ;
25
+ import org .elasticsearch .plugins .PluginDescriptor ;
16
26
27
+ import java .io .IOException ;
28
+ import java .io .InputStream ;
29
+ import java .net .URL ;
17
30
import java .nio .file .Path ;
31
+ import java .util .ArrayList ;
32
+ import java .util .HashMap ;
33
+ import java .util .List ;
34
+ import java .util .Map ;
35
+ import java .util .Set ;
18
36
import java .util .stream .Stream ;
19
37
20
38
public class TestEntitlementBootstrap {
@@ -24,10 +42,15 @@ public class TestEntitlementBootstrap {
24
42
/**
25
43
* Activates entitlement checking in tests.
26
44
*/
27
- public static void bootstrap () {
28
- TestEntitlementInitialization .initializeArgs = new TestEntitlementInitialization .InitializeArgs (new TestPathLookup ());
45
+ public static void bootstrap () throws IOException {
46
+ TestPathLookup pathLookup = new TestPathLookup ();
47
+ EntitlementInitialization .initializeArgs = new EntitlementInitialization .InitializeArgs (
48
+ pathLookup ,
49
+ Set .of (),
50
+ createPolicyManager (pathLookup )
51
+ );
29
52
logger .debug ("Loading entitlement agent" );
30
- EntitlementBootstrap .loadAgent (EntitlementBootstrap .findAgentJar (), TestEntitlementInitialization .class .getName ());
53
+ EntitlementBootstrap .loadAgent (EntitlementBootstrap .findAgentJar (), EntitlementInitialization .class .getName ());
31
54
}
32
55
33
56
private record TestPathLookup () implements PathLookup {
@@ -47,4 +70,71 @@ public Stream<Path> resolveSettingPaths(BaseDir baseDir, String settingName) {
47
70
}
48
71
49
72
}
73
+
74
+ private static PolicyManager createPolicyManager (PathLookup pathLookup ) throws IOException {
75
+
76
+ var pluginsTestBuildInfo = TestBuildInfoParser .parseAllPluginTestBuildInfo ();
77
+ var serverTestBuildInfo = TestBuildInfoParser .parseServerTestBuildInfo ();
78
+ var scopeResolver = TestScopeResolver .createScopeResolver (serverTestBuildInfo , pluginsTestBuildInfo );
79
+ List <String > pluginNames = pluginsTestBuildInfo .stream ().map (TestBuildInfo ::component ).toList ();
80
+
81
+ var pluginDescriptors = parsePluginsDescriptors (pluginNames );
82
+ var pluginsData = pluginDescriptors .stream ()
83
+ .map (descriptor -> new TestPluginData (descriptor .getName (), descriptor .isModular (), false ))
84
+ .toList ();
85
+ Map <String , Policy > pluginPolicies = parsePluginsPolicies (pluginsData );
86
+
87
+ FilesEntitlementsValidation .validate (pluginPolicies , pathLookup );
88
+
89
+ return new TestPolicyManager (
90
+ HardcodedEntitlements .serverPolicy (null , null ),
91
+ HardcodedEntitlements .agentEntitlements (),
92
+ pluginPolicies ,
93
+ scopeResolver ,
94
+ Map .of (),
95
+ pathLookup
96
+ );
97
+ }
98
+
99
+ private record TestPluginData (String pluginName , boolean isModular , boolean isExternalPlugin ) {}
100
+
101
+ private static Map <String , Policy > parsePluginsPolicies (List <TestPluginData > pluginsData ) {
102
+ Map <String , Policy > policies = new HashMap <>();
103
+ for (var pluginData : pluginsData ) {
104
+ String pluginName = pluginData .pluginName ();
105
+ var resourceName = Strings .format ("META-INF/es-plugins/%s/entitlement-policy.yaml" , pluginName );
106
+
107
+ var resource = EntitlementInitialization .class .getClassLoader ().getResource (resourceName );
108
+ if (resource != null ) {
109
+ try (var inputStream = getStream (resource )) {
110
+ policies .put (pluginName , new PolicyParser (inputStream , pluginName , pluginData .isExternalPlugin ()).parsePolicy ());
111
+ } catch (IOException e ) {
112
+ throw new IllegalArgumentException (Strings .format ("Cannot read policy for plugin [%s]" , pluginName ), e );
113
+ }
114
+ }
115
+ }
116
+ return policies ;
117
+ }
118
+
119
+ private static List <PluginDescriptor > parsePluginsDescriptors (List <String > pluginNames ) {
120
+ List <PluginDescriptor > descriptors = new ArrayList <>();
121
+ for (var pluginName : pluginNames ) {
122
+ var resourceName = Strings .format ("META-INF/es-plugins/%s/plugin-descriptor.properties" , pluginName );
123
+ var resource = EntitlementInitialization .class .getClassLoader ().getResource (resourceName );
124
+ if (resource != null ) {
125
+ try (var inputStream = getStream (resource )) {
126
+ descriptors .add (PluginDescriptor .readInternalDescriptorFromStream (inputStream ));
127
+ } catch (IOException e ) {
128
+ throw new IllegalArgumentException (Strings .format ("Cannot read descriptor for plugin [%s]" , pluginName ), e );
129
+ }
130
+ }
131
+ }
132
+ return descriptors ;
133
+ }
134
+
135
+ @ SuppressForbidden (reason = "URLs from class loader" )
136
+ private static InputStream getStream (URL resource ) throws IOException {
137
+ return resource .openStream ();
138
+ }
139
+
50
140
}
0 commit comments