Skip to content

Commit c5dbb64

Browse files
committed
add policy manager tests for exclusive
1 parent b255a2c commit c5dbb64

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,24 @@ static void validateExclusivePaths(List<ExclusivePath> exclusivePaths) {
5555
ExclusivePath currentExclusivePath = exclusivePaths.get(0);
5656
for (int i = 1; i < exclusivePaths.size(); ++i) {
5757
ExclusivePath nextPath = exclusivePaths.get(i);
58-
if (isParent(currentExclusivePath.path(), nextPath.path())) {
58+
if (currentExclusivePath.path().equals(nextPath.path) || isParent(currentExclusivePath.path(), nextPath.path())) {
59+
throw new IllegalArgumentException(
60+
"duplicate/overlapping exclusive paths found in files entitlements: "
61+
+ "[["
62+
+ currentExclusivePath.componentName()
63+
+ "] ["
64+
+ currentExclusivePath.moduleName()
65+
+ "] ["
66+
+ currentExclusivePath.path()
67+
+ "]] and [["
5968

69+
+ nextPath.componentName()
70+
+ "] ["
71+
+ nextPath.moduleName()
72+
+ "] ["
73+
+ nextPath.path()
74+
+ "]]"
75+
);
6076
}
6177
currentExclusivePath = nextPath;
6278
}
@@ -89,7 +105,7 @@ private FileAccessTree(
89105
BiConsumer<Path, Mode> addPath = (path, mode) -> {
90106
var normalized = normalizePath(path);
91107
for (String exclusivePath : updatedExclusivePaths) {
92-
if (isParent(exclusivePath, normalized)) {
108+
if (exclusivePath.equals(normalized) || isParent(exclusivePath, normalized)) {
93109
throw new IllegalArgumentException(
94110
"[" + componentName + "] [" + moduleName + "] cannot use exclusive path [" + exclusivePath + "]"
95111
);

libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/PolicyManagerTests.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,115 @@ public void testDuplicateEntitlements() {
411411
);
412412
}
413413

414+
public void testFilesEntitlementsWithExclusive() {
415+
var iae = expectThrows(
416+
IllegalArgumentException.class,
417+
() -> new PolicyManager(
418+
createEmptyTestServerPolicy(),
419+
List.of(),
420+
Map.of(
421+
"plugin1",
422+
new Policy(
423+
"test",
424+
List.of(
425+
new Scope(
426+
"test",
427+
List.of(
428+
new FilesEntitlement(
429+
List.of(
430+
FilesEntitlement.FileData.ofPath(Path.of("/tmp/test"), FilesEntitlement.Mode.READ)
431+
.withExclusive(true)
432+
)
433+
)
434+
)
435+
)
436+
)
437+
),
438+
"plugin2",
439+
new Policy(
440+
"test",
441+
List.of(
442+
new Scope(
443+
"test",
444+
List.of(
445+
new FilesEntitlement(
446+
List.of(
447+
FilesEntitlement.FileData.ofPath(Path.of("/tmp/test"), FilesEntitlement.Mode.READ)
448+
.withExclusive(true)
449+
)
450+
)
451+
)
452+
)
453+
)
454+
)
455+
),
456+
c -> "",
457+
TEST_AGENTS_PACKAGE_NAME,
458+
NO_ENTITLEMENTS_MODULE,
459+
TEST_PATH_LOOKUP,
460+
Set.of()
461+
)
462+
);
463+
assertEquals(
464+
"duplicate/overlapping exclusive paths found in files entitlements: "
465+
+ "[[plugin1] [test] [/tmp/test]] and [[plugin2] [test] [/tmp/test]]",
466+
iae.getMessage()
467+
);
468+
469+
iae = expectThrows(
470+
IllegalArgumentException.class,
471+
() -> new PolicyManager(
472+
new Policy(
473+
"test",
474+
List.of(
475+
new Scope(
476+
"test",
477+
List.of(
478+
new FilesEntitlement(
479+
List.of(
480+
FilesEntitlement.FileData.ofPath(
481+
Path.of("/tmp/test/foo"), FilesEntitlement.Mode.READ).withExclusive(true),
482+
FilesEntitlement.FileData.ofPath(Path.of("/tmp/"), FilesEntitlement.Mode.READ)
483+
)
484+
)
485+
)
486+
)
487+
)
488+
),
489+
List.of(),
490+
Map.of(
491+
"plugin1",
492+
new Policy(
493+
"test",
494+
List.of(
495+
new Scope(
496+
"test",
497+
List.of(
498+
new FilesEntitlement(
499+
List.of(
500+
FilesEntitlement.FileData.ofPath(Path.of("/tmp/test"), FilesEntitlement.Mode.READ)
501+
.withExclusive(true)
502+
)
503+
)
504+
)
505+
)
506+
)
507+
)
508+
),
509+
c -> "",
510+
TEST_AGENTS_PACKAGE_NAME,
511+
NO_ENTITLEMENTS_MODULE,
512+
TEST_PATH_LOOKUP,
513+
Set.of()
514+
)
515+
);
516+
assertEquals(
517+
"duplicate/overlapping exclusive paths found in files entitlements: "
518+
+ "[[plugin1] [test] [/tmp/test]] and [[(server)] [test] [/tmp/test/foo]]",
519+
iae.getMessage()
520+
);
521+
}
522+
414523
/**
415524
* If the plugin resolver tells us a class is in a plugin, don't conclude that it's in an agent.
416525
*/

0 commit comments

Comments
 (0)