Skip to content

Commit 400ec0e

Browse files
committed
follow links
1 parent 7ad07c0 commit 400ec0e

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@
1010
package org.elasticsearch.entitlement.runtime.policy;
1111

1212
import org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement;
13+
import org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.Mode;
1314
import org.elasticsearch.logging.LogManager;
1415
import org.elasticsearch.logging.Logger;
1516

17+
import java.io.IOException;
18+
import java.io.UncheckedIOException;
19+
import java.nio.file.Files;
1620
import java.nio.file.Path;
1721
import java.util.ArrayList;
1822
import java.util.Arrays;
1923
import java.util.List;
2024
import java.util.Objects;
25+
import java.util.function.BiConsumer;
26+
import java.util.function.Consumer;
2127

2228
import static org.elasticsearch.core.PathUtils.getDefaultFileSystem;
2329

@@ -32,6 +38,13 @@ public final class FileAccessTree {
3238
private FileAccessTree(FilesEntitlement filesEntitlement, PathLookup pathLookup) {
3339
List<String> readPaths = new ArrayList<>();
3440
List<String> writePaths = new ArrayList<>();
41+
BiConsumer<Path, Mode> addPath = (path, mode) -> {
42+
var normalized = normalizePath(path);
43+
if (mode == Mode.READ_WRITE) {
44+
writePaths.add(normalized);
45+
}
46+
readPaths.add(normalized);
47+
};
3548
for (FilesEntitlement.FileData fileData : filesEntitlement.filesData()) {
3649
var mode = fileData.mode();
3750
var paths = fileData.resolvePaths(pathLookup);
@@ -40,11 +53,18 @@ private FileAccessTree(FilesEntitlement filesEntitlement, PathLookup pathLookup)
4053
// TODO: null paths shouldn't be allowed, but they can occur due to repo paths
4154
return;
4255
}
43-
var normalized = normalizePath(path);
44-
if (mode == FilesEntitlement.Mode.READ_WRITE) {
45-
writePaths.add(normalized);
56+
addPath.accept(path, mode);
57+
// also try to follow symlinks. Lucene does this and writes to the target path.
58+
if (Files.exists(path)) {
59+
try {
60+
Path realPath = path.toRealPath();
61+
if (realPath.equals(path) == false) {
62+
addPath.accept(realPath, mode);
63+
}
64+
} catch (IOException e) {
65+
throw new UncheckedIOException(e);
66+
}
4667
}
47-
readPaths.add(normalized);
4868
});
4969
}
5070

0 commit comments

Comments
 (0)