Skip to content

Commit dd9de2c

Browse files
committed
Move normalization responsibility to FileEntitlement
1 parent ffc15a2 commit dd9de2c

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
import org.elasticsearch.entitlement.runtime.policy.entitlements.FileEntitlement;
1313

1414
import java.nio.file.Path;
15-
import java.nio.file.Paths;
1615
import java.util.ArrayList;
1716
import java.util.Arrays;
1817
import java.util.List;
1918
import java.util.Objects;
2019

20+
import static org.elasticsearch.entitlement.runtime.policy.entitlements.FileEntitlement.normalizePath;
21+
2122
public final class FileAccessTree {
2223
public static final FileAccessTree EMPTY = new FileAccessTree(List.of());
2324

@@ -28,7 +29,7 @@ private FileAccessTree(List<FileEntitlement> fileEntitlements) {
2829
List<String> readPaths = new ArrayList<>();
2930
List<String> writePaths = new ArrayList<>();
3031
for (FileEntitlement fileEntitlement : fileEntitlements) {
31-
String path = normalizedPath(fileEntitlement);
32+
String path = fileEntitlement.path();
3233
if (fileEntitlement.mode() == FileEntitlement.Mode.READ_WRITE) {
3334
writePaths.add(path);
3435
}
@@ -47,19 +48,11 @@ public static FileAccessTree of(List<FileEntitlement> fileEntitlements) {
4748
}
4849

4950
boolean canRead(Path path) {
50-
return checkPath(normalize(path), readPaths);
51+
return checkPath(normalizePath(path), readPaths);
5152
}
5253

5354
boolean canWrite(Path path) {
54-
return checkPath(normalize(path), writePaths);
55-
}
56-
57-
private static String normalizedPath(FileEntitlement fileEntitlement) {
58-
return normalize(Paths.get(fileEntitlement.path()));
59-
}
60-
61-
private static String normalize(Path path) {
62-
return path.toAbsolutePath().normalize().toString().replace('\\', '/');
55+
return checkPath(normalizePath(path), writePaths);
6356
}
6457

6558
private static boolean checkPath(String path, String[] paths) {

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
import org.elasticsearch.entitlement.runtime.policy.ExternalEntitlement;
1313
import org.elasticsearch.entitlement.runtime.policy.PolicyValidationException;
1414

15+
import java.nio.file.Path;
1516
import java.nio.file.Paths;
1617

1718
/**
18-
* Describes a file entitlement with a path and mode.
19+
* Describes entitlement to access files at a particular location.
20+
*
21+
* @param path the location of the files. Will be automatically {@link #normalizePath normalized}.
22+
* For directories, implicitly includes access to all contained files and (recursively) subdirectories.
23+
* @param mode the type of operation
1924
*/
2025
public record FileEntitlement(String path, Mode mode) implements Entitlement {
2126

@@ -25,11 +30,14 @@ public enum Mode {
2530
}
2631

2732
public FileEntitlement {
28-
path = normalizePath(path);
33+
path = normalizePath(Paths.get(path));
2934
}
3035

31-
private static String normalizePath(String path) {
32-
return Paths.get(path).toAbsolutePath().normalize().toString();
36+
/**
37+
* @return the "canonical" form of of the given {@code path}, to be used for entitlement checks.
38+
*/
39+
public static String normalizePath(Path path) {
40+
return path.toAbsolutePath().normalize().toString().replace('\\', '/');
3341
}
3442

3543
private static Mode parseMode(String mode) {

0 commit comments

Comments
 (0)