Skip to content

Commit d77c163

Browse files
committed
Using it in FilesEntitlement
1 parent b45071d commit d77c163

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/entitlements/FilesEntitlement.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,15 @@ public static FilesEntitlement build(List<Object> paths) {
237237
throw new PolicyValidationException("files entitlement with a 'relative_path' must specify 'relative_to'");
238238
}
239239

240-
Path relativePath = Path.of(relativePathAsString);
241-
if (relativePath.isAbsolute()) {
240+
if (FileData.isAbsolutePath(relativePathAsString)) {
242241
throw new PolicyValidationException("'relative_path' [" + relativePathAsString + "] must be relative");
243242
}
244-
filesData.add(FileData.ofRelativePath(relativePath, baseDir, mode));
243+
filesData.add(FileData.ofRelativePath(Path.of(relativePathAsString), baseDir, mode));
245244
} else if (pathAsString != null) {
246-
Path path = Path.of(pathAsString);
247-
if (path.isAbsolute() == false) {
245+
if (FileData.isAbsolutePath(pathAsString) == false) {
248246
throw new PolicyValidationException("'path' [" + pathAsString + "] must be absolute");
249247
}
250-
filesData.add(FileData.ofPath(path, mode));
248+
filesData.add(FileData.ofPath(Path.of(pathAsString), mode));
251249
} else if (pathSetting != null) {
252250
filesData.add(FileData.ofPathSetting(pathSetting, mode));
253251
} else if (relativePathSetting != null) {

libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/entitlements/FilesEntitlementTests.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,36 @@ public void testInvalidRelativeDirectory() {
6060
assertThat(ex.getMessage(), is("invalid relative directory: bar, valid values: [config, data, home]"));
6161
}
6262

63-
public void testFileDataRelativeWithEmptyDirectory() {
63+
public void testFileDataRelativeWithAbsoluteDirectoryFails() {
6464
var fileData = FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE);
6565
var dataDirs = fileData.resolvePaths(TEST_PATH_LOOKUP);
6666
assertThat(dataDirs.toList(), contains(Path.of("/data1/"), Path.of("/data2")));
6767
}
6868

69+
public void testFileDataAbsoluteWithRelativeDirectoryFails() {
70+
var ex = expectThrows(
71+
PolicyValidationException.class,
72+
() -> FilesEntitlement.build(List.of((Map.of("path", "foo", "mode", "read"))))
73+
);
74+
75+
assertThat(ex.getMessage(), is("'path' [foo] must be absolute"));
76+
}
77+
78+
public void testFileDataRelativeWithEmptyDirectory() {
79+
var ex = expectThrows(
80+
PolicyValidationException.class,
81+
() -> FilesEntitlement.build(List.of((Map.of("relative_path", "/foo", "mode", "read", "relative_to", "config"))))
82+
);
83+
84+
var ex2 = expectThrows(
85+
PolicyValidationException.class,
86+
() -> FilesEntitlement.build(List.of((Map.of("relative_path", "C:\\foo", "mode", "read", "relative_to", "config"))))
87+
);
88+
89+
assertThat(ex.getMessage(), is("'relative_path' [/foo] must be relative"));
90+
assertThat(ex2.getMessage(), is("'relative_path' [C:\\foo] must be relative"));
91+
}
92+
6993
public void testPathSettingResolve() {
7094
var entitlement = FilesEntitlement.build(List.of(Map.of("path_setting", "foo.bar", "mode", "read")));
7195
var filesData = entitlement.filesData();

0 commit comments

Comments
 (0)