10
10
package org .elasticsearch .entitlement .initialization ;
11
11
12
12
import org .elasticsearch .core .Booleans ;
13
- import org .elasticsearch .core .Strings ;
14
13
import org .elasticsearch .entitlement .bootstrap .EntitlementBootstrap ;
15
14
import org .elasticsearch .entitlement .bridge .EntitlementChecker ;
16
15
import org .elasticsearch .entitlement .runtime .api .ElasticsearchEntitlementChecker ;
17
- import org .elasticsearch .entitlement .runtime .policy .FileAccessTree ;
18
16
import org .elasticsearch .entitlement .runtime .policy .PathLookup ;
19
17
import org .elasticsearch .entitlement .runtime .policy .Policy ;
20
18
import org .elasticsearch .entitlement .runtime .policy .PolicyManager ;
39
37
import java .nio .file .Path ;
40
38
import java .util .ArrayList ;
41
39
import java .util .Collections ;
42
- import java .util .HashSet ;
43
40
import java .util .List ;
44
41
import java .util .Map ;
45
42
import java .util .Set ;
@@ -248,7 +245,7 @@ private static PolicyManager createPolicyManager() {
248
245
)
249
246
);
250
247
251
- validateFilesEntitlements (pluginPolicies , pathLookup );
248
+ FilesEntitlementsValidation . validate (pluginPolicies , pathLookup );
252
249
253
250
return new PolicyManager (
254
251
serverPolicy ,
@@ -262,74 +259,6 @@ private static PolicyManager createPolicyManager() {
262
259
);
263
260
}
264
261
265
- // package visible for tests
266
- static void validateFilesEntitlements (Map <String , Policy > pluginPolicies , PathLookup pathLookup ) {
267
- Set <Path > readAccessForbidden = new HashSet <>();
268
- pathLookup .getBaseDirPaths (PLUGINS ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
269
- pathLookup .getBaseDirPaths (MODULES ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
270
- pathLookup .getBaseDirPaths (LIB ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
271
- Set <Path > writeAccessForbidden = new HashSet <>();
272
- pathLookup .getBaseDirPaths (CONFIG ).forEach (p -> writeAccessForbidden .add (p .toAbsolutePath ().normalize ()));
273
- for (var pluginPolicy : pluginPolicies .entrySet ()) {
274
- for (var scope : pluginPolicy .getValue ().scopes ()) {
275
- var filesEntitlement = scope .entitlements ()
276
- .stream ()
277
- .filter (x -> x instanceof FilesEntitlement )
278
- .map (x -> ((FilesEntitlement ) x ))
279
- .findFirst ();
280
- if (filesEntitlement .isPresent ()) {
281
- var fileAccessTree = FileAccessTree .withoutExclusivePaths (filesEntitlement .get (), pathLookup , null );
282
- validateReadFilesEntitlements (pluginPolicy .getKey (), scope .moduleName (), fileAccessTree , readAccessForbidden );
283
- validateWriteFilesEntitlements (pluginPolicy .getKey (), scope .moduleName (), fileAccessTree , writeAccessForbidden );
284
- }
285
- }
286
- }
287
- }
288
-
289
- private static IllegalArgumentException buildValidationException (
290
- String componentName ,
291
- String moduleName ,
292
- Path forbiddenPath ,
293
- FilesEntitlement .Mode mode
294
- ) {
295
- return new IllegalArgumentException (
296
- Strings .format (
297
- "policy for module [%s] in [%s] has an invalid file entitlement. Any path under [%s] is forbidden for mode [%s]." ,
298
- moduleName ,
299
- componentName ,
300
- forbiddenPath ,
301
- mode
302
- )
303
- );
304
- }
305
-
306
- private static void validateReadFilesEntitlements (
307
- String componentName ,
308
- String moduleName ,
309
- FileAccessTree fileAccessTree ,
310
- Set <Path > readForbiddenPaths
311
- ) {
312
-
313
- for (Path forbiddenPath : readForbiddenPaths ) {
314
- if (fileAccessTree .canRead (forbiddenPath )) {
315
- throw buildValidationException (componentName , moduleName , forbiddenPath , READ );
316
- }
317
- }
318
- }
319
-
320
- private static void validateWriteFilesEntitlements (
321
- String componentName ,
322
- String moduleName ,
323
- FileAccessTree fileAccessTree ,
324
- Set <Path > writeForbiddenPaths
325
- ) {
326
- for (Path forbiddenPath : writeForbiddenPaths ) {
327
- if (fileAccessTree .canWrite (forbiddenPath )) {
328
- throw buildValidationException (componentName , moduleName , forbiddenPath , READ_WRITE );
329
- }
330
- }
331
- }
332
-
333
262
/**
334
263
* If bytecode verification is enabled, ensure these classes get loaded before transforming/retransforming them.
335
264
* For these classes, the order in which we transform and verify them matters. Verification during class transformation is at least an
0 commit comments