2020import java .util .Map ;
2121
2222import static org .elasticsearch .core .PathUtils .getDefaultFileSystem ;
23+ import static org .hamcrest .Matchers .equalTo ;
2324import static org .hamcrest .Matchers .is ;
2425
2526public class FileAccessTreeTests extends ESTestCase {
@@ -36,6 +37,7 @@ private static Path path(String s) {
3637 }
3738
3839 private static final PathLookup TEST_PATH_LOOKUP = new PathLookup (
40+ Path .of ("/home" ),
3941 Path .of ("/config" ),
4042 new Path [] { Path .of ("/data1" ), Path .of ("/data2" ) },
4143 Path .of ("/tmp" )
@@ -91,32 +93,36 @@ public void testReadWriteUnderRead() {
9193 }
9294
9395 public void testReadWithRelativePath () {
94- var tree = accessTree (entitlement (Map .of ("relative_path" , "foo" , "mode" , "read" , "relative_to" , "config" )));
95- assertThat (tree .canRead (path ("foo" )), is (false ));
96+ for (var dir : List .of ("config" , "home" )) {
97+ var tree = accessTree (entitlement (Map .of ("relative_path" , "foo" , "mode" , "read" , "relative_to" , dir )));
98+ assertThat (tree .canRead (path ("foo" )), is (false ));
9699
97- assertThat (tree .canRead (path ("/config /foo" )), is (true ));
100+ assertThat (tree .canRead (path ("/" + dir + " /foo" )), is (true ));
98101
99- assertThat (tree .canRead (path ("/config /foo/subdir" )), is (true ));
100- assertThat (tree .canRead (path ("/config /food" )), is (false ));
101- assertThat (tree .canWrite (path ("/config /foo" )), is (false ));
102+ assertThat (tree .canRead (path ("/" + dir + " /foo/subdir" )), is (true ));
103+ assertThat (tree .canRead (path ("/" + dir + " /food" )), is (false ));
104+ assertThat (tree .canWrite (path ("/" + dir + " /foo" )), is (false ));
102105
103- assertThat (tree .canRead (path ("/config" )), is (false ));
104- assertThat (tree .canRead (path ("/config/before" )), is (false ));
105- assertThat (tree .canRead (path ("/config/later" )), is (false ));
106+ assertThat (tree .canRead (path ("/" + dir )), is (false ));
107+ assertThat (tree .canRead (path ("/" + dir + "/before" )), is (false ));
108+ assertThat (tree .canRead (path ("/" + dir + "/later" )), is (false ));
109+ }
106110 }
107111
108112 public void testWriteWithRelativePath () {
109- var tree = accessTree (entitlement (Map .of ("relative_path" , "foo" , "mode" , "read_write" , "relative_to" , "config" )));
110- assertThat (tree .canWrite (path ("/config/foo" )), is (true ));
111- assertThat (tree .canWrite (path ("/config/foo/subdir" )), is (true ));
112- assertThat (tree .canWrite (path ("foo" )), is (false ));
113- assertThat (tree .canWrite (path ("/config/food" )), is (false ));
114- assertThat (tree .canRead (path ("/config/foo" )), is (true ));
115- assertThat (tree .canRead (path ("foo" )), is (false ));
116-
117- assertThat (tree .canWrite (path ("/config" )), is (false ));
118- assertThat (tree .canWrite (path ("/config/before" )), is (false ));
119- assertThat (tree .canWrite (path ("/config/later" )), is (false ));
113+ for (var dir : List .of ("config" , "home" )) {
114+ var tree = accessTree (entitlement (Map .of ("relative_path" , "foo" , "mode" , "read_write" , "relative_to" , dir )));
115+ assertThat (tree .canWrite (path ("/" + dir + "/foo" )), is (true ));
116+ assertThat (tree .canWrite (path ("/" + dir + "/foo/subdir" )), is (true ));
117+ assertThat (tree .canWrite (path ("/" + dir )), is (false ));
118+ assertThat (tree .canWrite (path ("/" + dir + "/food" )), is (false ));
119+ assertThat (tree .canRead (path ("/" + dir + "/foo" )), is (true ));
120+ assertThat (tree .canRead (path ("/" + dir )), is (false ));
121+
122+ assertThat (tree .canWrite (path ("/" + dir )), is (false ));
123+ assertThat (tree .canWrite (path ("/" + dir + "/before" )), is (false ));
124+ assertThat (tree .canWrite (path ("/" + dir + "/later" )), is (false ));
125+ }
120126 }
121127
122128 public void testMultipleDataDirs () {
@@ -161,12 +167,20 @@ public void testTempDirAccess() {
161167 Path tempDir = createTempDir ();
162168 var tree = FileAccessTree .of (
163169 FilesEntitlement .EMPTY ,
164- new PathLookup (Path .of ("/config" ), new Path [] { Path .of ("/data1" ), Path .of ("/data2" ) }, tempDir )
170+ new PathLookup (Path .of ("/home" ), Path . of ( "/ config" ), new Path [] { Path .of ("/data1" ), Path .of ("/data2" ) }, tempDir )
165171 );
166172 assertThat (tree .canRead (tempDir ), is (true ));
167173 assertThat (tree .canWrite (tempDir ), is (true ));
168174 }
169175
176+ public void testUndefinedHome () {
177+ var tree = FileAccessTree .of (
178+ entitlement (Map .of ("relative_path" , "foo" , "mode" , "read" , "relative_to" , "home" )),
179+ new PathLookup (null , TEST_PATH_LOOKUP .configDir (), TEST_PATH_LOOKUP .dataDirs (), TEST_PATH_LOOKUP .tempDir ())
180+ );
181+ assertThat (tree , equalTo (FileAccessTree .of (FilesEntitlement .EMPTY , TEST_PATH_LOOKUP )));
182+ }
183+
170184 FileAccessTree accessTree (FilesEntitlement entitlement ) {
171185 return FileAccessTree .of (entitlement , TEST_PATH_LOOKUP );
172186 }
0 commit comments