1010package org .elasticsearch .entitlement .runtime .policy ;
1111
1212import org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement ;
13+ import org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .Mode ;
1314import org .elasticsearch .logging .LogManager ;
1415import org .elasticsearch .logging .Logger ;
1516
17+ import java .io .IOException ;
18+ import java .io .UncheckedIOException ;
19+ import java .nio .file .Files ;
1620import java .nio .file .Path ;
1721import java .util .ArrayList ;
1822import java .util .Arrays ;
1923import java .util .List ;
2024import java .util .Objects ;
25+ import java .util .function .BiConsumer ;
26+ import java .util .function .Consumer ;
2127
2228import 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