1010package org .elasticsearch .entitlement .runtime .policy .entitlements ;
1111
1212import org .elasticsearch .entitlement .runtime .policy .ExternalEntitlement ;
13+ import org .elasticsearch .entitlement .runtime .policy .PathLookup ;
1314import org .elasticsearch .entitlement .runtime .policy .PolicyValidationException ;
1415
1516import java .nio .file .Path ;
1617import java .util .ArrayList ;
18+ import java .util .Arrays ;
1719import java .util .HashMap ;
1820import java .util .List ;
1921import java .util .Map ;
2022import java .util .Objects ;
23+ import java .util .stream .Stream ;
2124
2225/**
2326 * Describes a file entitlement with a path and mode.
@@ -36,60 +39,99 @@ public enum BaseDir {
3639 DATA
3740 }
3841
39- public static final class FileData {
40- private final Path path ;
41- private final Mode mode ;
42- private final Path relativePath ;
43- private final BaseDir baseDir ;
44-
45- private FileData (Path path , Mode mode , Path relativePath , BaseDir baseDir ) {
46- this .path = path ;
47- this .mode = mode ;
48- this .relativePath = relativePath ;
49- this .baseDir = baseDir ;
50- }
42+ public interface FileData {
5143
52- public static FileData ofPath (Path path , Mode mode ) {
53- assert path .isAbsolute ();
54- return new FileData (path , mode , null , null );
55- }
44+ final class AbsolutePathFileData implements FileData {
45+ private final Path path ;
46+ private final Mode mode ;
5647
57- public static FileData ofRelativePath (Path relativePath , BaseDir baseDir , Mode mode ) {
58- assert relativePath . isAbsolute () == false ;
59- return new FileData ( null , mode , relativePath , baseDir ) ;
60- }
48+ private AbsolutePathFileData (Path path , Mode mode ) {
49+ this . path = path ;
50+ this . mode = mode ;
51+ }
6152
62- public Path path () {
63- return path ;
64- }
53+ @ Override
54+ public Stream <Path > resolvePaths (PathLookup pathLookup ) {
55+ return Stream .of (path );
56+ }
6557
66- public Mode mode () {
67- return mode ;
68- }
58+ @ Override
59+ public Mode mode () {
60+ return mode ;
61+ }
6962
70- public Path relativePath () {
71- return relativePath ;
63+ @ Override
64+ public boolean equals (Object obj ) {
65+ if (obj == this ) return true ;
66+ if (obj == null || obj .getClass () != this .getClass ()) return false ;
67+ var that = (AbsolutePathFileData ) obj ;
68+ return Objects .equals (this .path , that .path ) && Objects .equals (this .mode , that .mode );
69+ }
70+
71+ @ Override
72+ public int hashCode () {
73+ return Objects .hash (path , mode );
74+ }
7275 }
7376
74- public BaseDir baseDir () {
75- return baseDir ;
77+ final class RelativePathFileData implements FileData {
78+ private final Path relativePath ;
79+ private final BaseDir baseDir ;
80+ private final Mode mode ;
81+
82+ private RelativePathFileData (Path relativePath , BaseDir baseDir , Mode mode ) {
83+ this .relativePath = relativePath ;
84+ this .baseDir = baseDir ;
85+ this .mode = mode ;
86+ }
87+
88+ @ Override
89+ public Stream <Path > resolvePaths (PathLookup pathLookup ) {
90+ Objects .requireNonNull (pathLookup );
91+ switch (baseDir ) {
92+ case CONFIG :
93+ return Stream .of (pathLookup .configDir ().resolve (relativePath ));
94+ case DATA :
95+ return Arrays .stream (pathLookup .dataDirs ()).map (d -> d .resolve (relativePath ));
96+ default :
97+ throw new IllegalArgumentException ();
98+ }
99+ }
100+
101+ @ Override
102+ public Mode mode () {
103+ return mode ;
104+ }
105+
106+ @ Override
107+ public boolean equals (Object obj ) {
108+ if (obj == this ) return true ;
109+ if (obj == null || obj .getClass () != this .getClass ()) return false ;
110+ var that = (RelativePathFileData ) obj ;
111+ return Objects .equals (this .mode , that .mode )
112+ && Objects .equals (this .relativePath , that .relativePath )
113+ && Objects .equals (this .baseDir , that .baseDir );
114+ }
115+
116+ @ Override
117+ public int hashCode () {
118+ return Objects .hash (relativePath , baseDir , mode );
119+ }
76120 }
77121
78- @ Override
79- public boolean equals (Object obj ) {
80- if (obj == this ) return true ;
81- if (obj == null || obj .getClass () != this .getClass ()) return false ;
82- var that = (FileData ) obj ;
83- return Objects .equals (this .path , that .path )
84- && Objects .equals (this .mode , that .mode )
85- && Objects .equals (this .relativePath , that .relativePath )
86- && Objects .equals (this .baseDir , that .baseDir );
122+ static FileData ofPath (Path path , Mode mode ) {
123+ assert path .isAbsolute ();
124+ return new AbsolutePathFileData (path , mode );
87125 }
88126
89- @ Override
90- public int hashCode () {
91- return Objects . hash ( path , mode , relativePath , baseDir );
127+ static FileData ofRelativePath ( Path relativePath , BaseDir baseDir , Mode mode ) {
128+ assert relativePath . isAbsolute () == false ;
129+ return new RelativePathFileData ( relativePath , baseDir , mode );
92130 }
131+
132+ Stream <Path > resolvePaths (PathLookup pathLookup );
133+
134+ Mode mode ();
93135 }
94136
95137 private static Mode parseMode (String mode ) {
0 commit comments