58
58
import java .nio .file .attribute .FileAttribute ;
59
59
import java .nio .file .spi .FileSystemProvider ;
60
60
import java .util .ArrayList ;
61
- import java .util .Arrays ;
62
61
import java .util .Collections ;
63
62
import java .util .HashMap ;
63
+ import java .util .HashSet ;
64
64
import java .util .List ;
65
65
import java .util .Map ;
66
66
import java .util .Set ;
70
70
import java .util .stream .Stream ;
71
71
import java .util .stream .StreamSupport ;
72
72
73
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .CONFIG ;
74
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .DATA ;
75
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .LIB ;
76
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .LOGS ;
77
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .MODULES ;
78
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .PLUGINS ;
79
+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .SHARED_REPO ;
73
80
import static org .elasticsearch .entitlement .runtime .policy .Platform .LINUX ;
74
- import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .BaseDir .CONFIG ;
75
- import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .BaseDir .DATA ;
76
- import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .BaseDir .SHARED_REPO ;
77
81
import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .Mode .READ ;
78
82
import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .Mode .READ_WRITE ;
79
83
@@ -165,27 +169,20 @@ private static Class<?>[] findClassesToRetransform(Class<?>[] loadedClasses, Set
165
169
private static PolicyManager createPolicyManager () {
166
170
EntitlementBootstrap .BootstrapArgs bootstrapArgs = EntitlementBootstrap .bootstrapArgs ();
167
171
Map <String , Policy > pluginPolicies = bootstrapArgs .pluginPolicies ();
168
- var pathLookup = new PathLookup (
169
- getUserHome (),
170
- bootstrapArgs .configDir (),
171
- bootstrapArgs .dataDirs (),
172
- bootstrapArgs .sharedRepoDirs (),
173
- bootstrapArgs .tempDir (),
174
- bootstrapArgs .settingResolver ()
175
- );
172
+ PathLookup pathLookup = bootstrapArgs .pathLookup ();
176
173
177
174
List <Scope > serverScopes = new ArrayList <>();
178
175
List <FileData > serverModuleFileDatas = new ArrayList <>();
179
176
Collections .addAll (
180
177
serverModuleFileDatas ,
181
178
// Base ES directories
182
- FileData .ofPath ( bootstrapArgs . pluginsDir () , READ ),
183
- FileData .ofPath ( bootstrapArgs . modulesDir () , READ ),
184
- FileData .ofPath ( bootstrapArgs . configDir () , READ ),
185
- FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ),
186
- FileData .ofPath ( bootstrapArgs . libDir () , READ ),
187
- FileData .ofRelativePath ( Path . of ( "" ), DATA , READ_WRITE ),
188
- FileData .ofRelativePath ( Path . of ( "" ), SHARED_REPO , READ_WRITE ),
179
+ FileData .ofBaseDirPath ( PLUGINS , READ ),
180
+ FileData .ofBaseDirPath ( MODULES , READ ),
181
+ FileData .ofBaseDirPath ( CONFIG , READ ),
182
+ FileData .ofBaseDirPath ( LOGS , READ_WRITE ),
183
+ FileData .ofBaseDirPath ( LIB , READ ),
184
+ FileData .ofBaseDirPath ( DATA , READ_WRITE ),
185
+ FileData .ofBaseDirPath ( SHARED_REPO , READ_WRITE ),
189
186
// exclusive settings file
190
187
FileData .ofRelativePath (Path .of ("operator/settings.json" ), CONFIG , READ_WRITE ).withExclusive (true ),
191
188
// OS release on Linux
@@ -206,8 +203,8 @@ private static PolicyManager createPolicyManager() {
206
203
FileData .ofPath (Path .of ("/proc/self/mountinfo" ), READ ).withPlatform (LINUX ),
207
204
FileData .ofPath (Path .of ("/proc/diskstats" ), READ ).withPlatform (LINUX )
208
205
);
209
- if (bootstrapArgs .pidFile () != null ) {
210
- serverModuleFileDatas .add (FileData .ofPath (bootstrapArgs .pidFile (), READ_WRITE ));
206
+ if (pathLookup .pidFile () != null ) {
207
+ serverModuleFileDatas .add (FileData .ofPath (pathLookup .pidFile (), READ_WRITE ));
211
208
}
212
209
213
210
Collections .addAll (
@@ -219,8 +216,8 @@ private static PolicyManager createPolicyManager() {
219
216
new FilesEntitlement (
220
217
List .of (
221
218
// TODO: what in es.base is accessing shared repo?
222
- FileData .ofRelativePath ( Path . of ( "" ), SHARED_REPO , READ_WRITE ),
223
- FileData .ofRelativePath ( Path . of ( "" ), DATA , READ_WRITE )
219
+ FileData .ofBaseDirPath ( SHARED_REPO , READ_WRITE ),
220
+ FileData .ofBaseDirPath ( DATA , READ_WRITE )
224
221
)
225
222
)
226
223
)
@@ -245,25 +242,17 @@ private static PolicyManager createPolicyManager() {
245
242
List .of (
246
243
new LoadNativeLibrariesEntitlement (),
247
244
new ManageThreadsEntitlement (),
248
- new FilesEntitlement (
249
- List .of (FileData .ofPath (bootstrapArgs .configDir (), READ ), FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE ))
250
- )
245
+ new FilesEntitlement (List .of (FileData .ofBaseDirPath (CONFIG , READ ), FileData .ofBaseDirPath (DATA , READ_WRITE )))
251
246
)
252
247
),
253
- new Scope (
254
- "org.apache.lucene.misc" ,
255
- List .of (new FilesEntitlement (List .of (FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE ))))
256
- ),
248
+ new Scope ("org.apache.lucene.misc" , List .of (new FilesEntitlement (List .of (FileData .ofBaseDirPath (DATA , READ_WRITE ))))),
257
249
new Scope (
258
250
"org.apache.logging.log4j.core" ,
259
- List .of (new ManageThreadsEntitlement (), new FilesEntitlement (List .of (FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ))))
251
+ List .of (new ManageThreadsEntitlement (), new FilesEntitlement (List .of (FileData .ofBaseDirPath ( LOGS , READ_WRITE ))))
260
252
),
261
253
new Scope (
262
254
"org.elasticsearch.nativeaccess" ,
263
- List .of (
264
- new LoadNativeLibrariesEntitlement (),
265
- new FilesEntitlement (List .of (FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE )))
266
- )
255
+ List .of (new LoadNativeLibrariesEntitlement (), new FilesEntitlement (List .of (FileData .ofBaseDirPath (DATA , READ_WRITE ))))
267
256
)
268
257
);
269
258
@@ -288,7 +277,7 @@ private static PolicyManager createPolicyManager() {
288
277
new Scope (
289
278
"org.bouncycastle.fips.core" ,
290
279
// read to lib dir is required for checksum validation
291
- List .of (new FilesEntitlement (List .of (FileData .ofPath ( bootstrapArgs . libDir () , READ ))), new ManageThreadsEntitlement ())
280
+ List .of (new FilesEntitlement (List .of (FileData .ofBaseDirPath ( LIB , READ ))), new ManageThreadsEntitlement ())
292
281
)
293
282
);
294
283
}
@@ -312,21 +301,14 @@ private static PolicyManager createPolicyManager() {
312
301
new LoadNativeLibrariesEntitlement (),
313
302
new FilesEntitlement (
314
303
List .of (
315
- FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ),
304
+ FileData .ofBaseDirPath ( LOGS , READ_WRITE ),
316
305
FileData .ofPath (Path .of ("/proc/meminfo" ), READ ),
317
306
FileData .ofPath (Path .of ("/sys/fs/cgroup/" ), READ )
318
307
)
319
308
)
320
309
);
321
310
322
- validateFilesEntitlements (
323
- pluginPolicies ,
324
- pathLookup ,
325
- bootstrapArgs .configDir (),
326
- bootstrapArgs .pluginsDir (),
327
- bootstrapArgs .modulesDir (),
328
- bootstrapArgs .libDir ()
329
- );
311
+ validateFilesEntitlements (pluginPolicies , pathLookup );
330
312
331
313
return new PolicyManager (
332
314
serverPolicy ,
@@ -341,21 +323,14 @@ private static PolicyManager createPolicyManager() {
341
323
);
342
324
}
343
325
344
- private static Set <Path > pathSet (Path ... paths ) {
345
- return Arrays .stream (paths ).map (x -> x .toAbsolutePath ().normalize ()).collect (Collectors .toUnmodifiableSet ());
346
- }
347
-
348
326
// package visible for tests
349
- static void validateFilesEntitlements (
350
- Map <String , Policy > pluginPolicies ,
351
- PathLookup pathLookup ,
352
- Path configDir ,
353
- Path pluginsDir ,
354
- Path modulesDir ,
355
- Path libDir
356
- ) {
357
- var readAccessForbidden = pathSet (pluginsDir , modulesDir , libDir );
358
- var writeAccessForbidden = pathSet (configDir );
327
+ static void validateFilesEntitlements (Map <String , Policy > pluginPolicies , PathLookup pathLookup ) {
328
+ Set <Path > readAccessForbidden = new HashSet <>();
329
+ pathLookup .getBaseDirPaths (PLUGINS ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
330
+ pathLookup .getBaseDirPaths (MODULES ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
331
+ pathLookup .getBaseDirPaths (LIB ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
332
+ Set <Path > writeAccessForbidden = new HashSet <>();
333
+ pathLookup .getBaseDirPaths (CONFIG ).forEach (p -> writeAccessForbidden .add (p .toAbsolutePath ().normalize ()));
359
334
for (var pluginPolicy : pluginPolicies .entrySet ()) {
360
335
for (var scope : pluginPolicy .getValue ().scopes ()) {
361
336
var filesEntitlement = scope .entitlements ()
0 commit comments