9
9
10
10
package org .elasticsearch .entitlement .bootstrap ;
11
11
12
- import org .apache .lucene .tests .mockfile .FilterPath ;
13
12
import org .elasticsearch .bootstrap .TestBuildInfo ;
14
13
import org .elasticsearch .bootstrap .TestBuildInfoParser ;
15
14
import org .elasticsearch .bootstrap .TestScopeResolver ;
16
15
import org .elasticsearch .common .Strings ;
17
- import org .elasticsearch .common .settings .Settings ;
18
16
import org .elasticsearch .core .Booleans ;
19
- import org .elasticsearch .core .Nullable ;
20
17
import org .elasticsearch .core .PathUtils ;
21
18
import org .elasticsearch .core .SuppressForbidden ;
22
19
import org .elasticsearch .entitlement .initialization .EntitlementInitialization ;
23
20
import org .elasticsearch .entitlement .runtime .policy .PathLookup ;
24
- import org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir ;
25
21
import org .elasticsearch .entitlement .runtime .policy .Policy ;
22
+ import org .elasticsearch .entitlement .runtime .policy .PolicyManager ;
26
23
import org .elasticsearch .entitlement .runtime .policy .PolicyParser ;
27
- import org .elasticsearch .entitlement .runtime .policy .TestPathLookup ;
28
24
import org .elasticsearch .entitlement .runtime .policy .TestPolicyManager ;
29
25
import org .elasticsearch .logging .LogManager ;
30
26
import org .elasticsearch .logging .Logger ;
35
31
import java .net .URI ;
36
32
import java .net .URL ;
37
33
import java .nio .file .Path ;
38
- import java .nio .file .Paths ;
39
34
import java .util .ArrayList ;
40
35
import java .util .Arrays ;
41
- import java .util .Collection ;
42
- import java .util .Collections ;
43
36
import java .util .HashMap ;
44
- import java .util .HashSet ;
45
37
import java .util .List ;
46
38
import java .util .Map ;
47
39
import java .util .Set ;
48
40
import java .util .TreeSet ;
49
- import java .util .concurrent .ConcurrentHashMap ;
50
- import java .util .function .BiFunction ;
51
- import java .util .function .Consumer ;
52
41
53
42
import static java .util .stream .Collectors .toCollection ;
54
43
import static java .util .stream .Collectors .toSet ;
55
- import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .TEMP ;
56
- import static org .elasticsearch .env .Environment .PATH_DATA_SETTING ;
57
- import static org .elasticsearch .env .Environment .PATH_HOME_SETTING ;
58
- import static org .elasticsearch .env .Environment .PATH_REPO_SETTING ;
59
- import static org .elasticsearch .env .Environment .PATH_SHARED_DATA_SETTING ;
60
44
61
45
public class TestEntitlementBootstrap {
62
-
63
46
private static final Logger logger = LogManager .getLogger (TestEntitlementBootstrap .class );
64
47
65
- private static Map < BaseDir , Collection < Path >> baseDirPaths = new ConcurrentHashMap <>() ;
66
- private static TestPolicyManager policyManager ;
48
+ private static TestPathLookup TEST_PATH_LOOKUP ;
49
+ private static TestPolicyManager POLICY_MANAGER ;
67
50
68
51
/**
69
52
* Activates entitlement checking in tests.
70
53
*/
71
- public static void bootstrap (@ Nullable Path tempDir ) throws IOException {
72
- if (isEnabledForTest () == false ) {
73
- return ;
74
- }
75
- var previousTempDir = baseDirPaths .put (TEMP , zeroOrOne (tempDir ));
76
- assert previousTempDir == null : "Test entitlement bootstrap called multiple times" ;
77
- TestPathLookup pathLookup = new TestPathLookup (baseDirPaths );
78
- policyManager = createPolicyManager (pathLookup );
79
- EntitlementInitialization .initializeArgs = new EntitlementInitialization .InitializeArgs (pathLookup , Set .of (), policyManager );
80
- logger .debug ("Loading entitlement agent" );
81
- EntitlementBootstrap .loadAgent (EntitlementBootstrap .findAgentJar (), EntitlementInitialization .class .getName ());
82
- }
83
-
84
- public static void registerNodeBaseDirs (Settings settings , Path configPath ) {
85
- if (policyManager == null ) {
86
- return ;
87
- }
88
-
89
- Path homeDir = homeDir (settings );
90
- Path configDir = configDir (configPath , homeDir );
91
- Collection <Path > dataDirs = dataDirs (settings , homeDir );
92
- Collection <Path > sharedDataDir = sharedDataDir (settings );
93
- Collection <Path > repoDirs = repoDirs (settings );
94
- logger .debug (
95
- "Registering node dirs: config [{}], dataDirs [{}], sharedDataDir [{}], repoDirs [{}]" ,
96
- configDir ,
97
- dataDirs ,
98
- sharedDataDir ,
99
- repoDirs
100
- );
101
- baseDirPaths .compute (BaseDir .CONFIG , baseDirModifier (paths -> paths .add (configDir )));
102
- baseDirPaths .compute (BaseDir .DATA , baseDirModifier (paths -> paths .addAll (dataDirs )));
103
- baseDirPaths .compute (BaseDir .SHARED_DATA , baseDirModifier (paths -> paths .addAll (sharedDataDir )));
104
- baseDirPaths .compute (BaseDir .SHARED_REPO , baseDirModifier (paths -> paths .addAll (repoDirs )));
105
- policyManager .clearModuleEntitlementsCache ();
106
- }
107
-
108
- public static void unregisterNodeBaseDirs (Settings settings , Path configPath ) {
109
- if (policyManager == null ) {
54
+ public static void bootstrap (Path tempDir ) throws IOException {
55
+ if (isEnabledForTests () == false ) {
110
56
return ;
111
57
}
112
-
113
- Path homeDir = homeDir (settings );
114
- Path configDir = configDir (configPath , homeDir );
115
- Collection <Path > dataDirs = dataDirs (settings , homeDir );
116
- Collection <Path > sharedDataDir = sharedDataDir (settings );
117
- Collection <Path > repoDirs = repoDirs (settings );
118
- logger .debug (
119
- "Unregistering node dirs: config [{}], dataDirs [{}], sharedDataDir [{}], repoDirs [{}]" ,
120
- configDir ,
121
- dataDirs ,
122
- sharedDataDir ,
123
- repoDirs
124
- );
125
- baseDirPaths .compute (BaseDir .CONFIG , baseDirModifier (paths -> paths .remove (configDir )));
126
- baseDirPaths .compute (BaseDir .DATA , baseDirModifier (paths -> paths .removeAll (dataDirs )));
127
- baseDirPaths .compute (BaseDir .SHARED_DATA , baseDirModifier (paths -> paths .removeAll (sharedDataDir )));
128
- baseDirPaths .compute (BaseDir .SHARED_REPO , baseDirModifier (paths -> paths .removeAll (repoDirs )));
129
- policyManager .clearModuleEntitlementsCache ();
130
- }
131
-
132
- private static Path homeDir (Settings settings ) {
133
- return absolutePath (PATH_HOME_SETTING .get (settings ));
134
- }
135
-
136
- private static Path configDir (Path configDir , Path homeDir ) {
137
- return configDir != null ? unwrapFilterPath (configDir ) : homeDir .resolve ("config" );
138
- }
139
-
140
- private static Collection <Path > dataDirs (Settings settings , Path homeDir ) {
141
- List <String > dataDirs = PATH_DATA_SETTING .get (settings );
142
- return dataDirs .isEmpty ()
143
- ? List .of (homeDir .resolve ("data" ))
144
- : dataDirs .stream ().map (TestEntitlementBootstrap ::absolutePath ).toList ();
58
+ assert POLICY_MANAGER == null && TEST_PATH_LOOKUP == null : "Test entitlement bootstrap called multiple times" ;
59
+ TEST_PATH_LOOKUP = new TestPathLookup (tempDir );
60
+ POLICY_MANAGER = createPolicyManager (TEST_PATH_LOOKUP );
61
+ loadAgent (POLICY_MANAGER , TEST_PATH_LOOKUP );
145
62
}
146
63
147
- private static Collection <Path > sharedDataDir (Settings settings ) {
148
- String sharedDataDir = PATH_SHARED_DATA_SETTING .get (settings );
149
- return Strings .hasText (sharedDataDir ) ? List .of (absolutePath (sharedDataDir )) : List .of ();
150
- }
151
-
152
- private static Collection <Path > repoDirs (Settings settings ) {
153
- return PATH_REPO_SETTING .get (settings ).stream ().map (TestEntitlementBootstrap ::absolutePath ).toList ();
154
- }
155
-
156
- private static BiFunction <BaseDir , Collection <Path >, Collection <Path >> baseDirModifier (Consumer <Collection <Path >> consumer ) {
157
- // always return a new unmodifiable copy
158
- return (BaseDir baseDir , Collection <Path > paths ) -> {
159
- paths = paths == null ? new HashSet <>() : new HashSet <>(paths );
160
- consumer .accept (paths );
161
- return Collections .unmodifiableCollection (paths );
162
- };
163
- }
164
-
165
- private static Path unwrapFilterPath (Path path ) {
166
- while (path instanceof FilterPath fPath ) {
167
- path = fPath .getDelegate ();
168
- }
169
- return path ;
170
- }
171
-
172
- @ SuppressForbidden (reason = "must be resolved using the default file system, rather then the mocked test file system" )
173
- private static Path absolutePath (String path ) {
174
- return Paths .get (path ).toAbsolutePath ().normalize ();
175
- }
176
-
177
- private static <T > List <T > zeroOrOne (T item ) {
178
- if (item == null ) {
179
- return List .of ();
180
- } else {
181
- return List .of (item );
182
- }
183
- }
184
-
185
- public static boolean isEnabledForTest () {
64
+ public static boolean isEnabledForTests () {
186
65
return Booleans .parseBoolean (System .getProperty ("es.entitlement.enableForTests" , "false" ));
187
66
}
188
67
189
- public static void setActive (boolean newValue ) {
190
- policyManager .setActive (newValue );
191
- }
192
-
193
- public static void setTriviallyAllowingTestCode (boolean newValue ) {
194
- policyManager .setTriviallyAllowingTestCode (newValue );
68
+ static TestPolicyManager testPolicyManager () {
69
+ return POLICY_MANAGER ;
195
70
}
196
71
197
- public static void setEntitledTestPackages ( String [] entitledTestPackages ) {
198
- policyManager . setEntitledTestPackages ( entitledTestPackages ) ;
72
+ static TestPathLookup testPathLookup ( ) {
73
+ return TEST_PATH_LOOKUP ;
199
74
}
200
75
201
- public static void resetAfterTest () {
202
- // reset all base dirs except TEMP, which is initialized just once statically
203
- baseDirPaths .keySet ().retainAll (List .of (TEMP ));
204
- if (policyManager != null ) {
205
- policyManager .resetAfterTest ();
206
- }
76
+ private static void loadAgent (PolicyManager policyManager , PathLookup pathLookup ) {
77
+ logger .debug ("Loading entitlement agent" );
78
+ EntitlementInitialization .initializeArgs = new EntitlementInitialization .InitializeArgs (pathLookup , Set .of (), policyManager );
79
+ EntitlementBootstrap .loadAgent (EntitlementBootstrap .findAgentJar (), EntitlementInitialization .class .getName ());
207
80
}
208
81
209
82
private static TestPolicyManager createPolicyManager (PathLookup pathLookup ) throws IOException {
@@ -224,7 +97,7 @@ private static TestPolicyManager createPolicyManager(PathLookup pathLookup) thro
224
97
225
98
String separator = System .getProperty ("path.separator" );
226
99
227
- // In productions , plugins would have access to their respective bundle directories,
100
+ // In production , plugins would have access to their respective bundle directories,
228
101
// and so they'd be able to read from their jars. In testing, we approximate this
229
102
// by considering the entire classpath to be "source paths" of all plugins. This
230
103
// also has the effect of granting read access to everything on the test-only classpath,
0 commit comments