99
1010package org .elasticsearch .entitlement .runtime .policy ;
1111
12+ import org .elasticsearch .core .Strings ;
1213import org .elasticsearch .entitlement .runtime .policy .entitlements .CreateClassLoaderEntitlement ;
1314import org .elasticsearch .entitlement .runtime .policy .entitlements .Entitlement ;
1415import org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement ;
1819import org .elasticsearch .entitlement .runtime .policy .entitlements .SetHttpsConnectionPropertiesEntitlement ;
1920import org .elasticsearch .entitlement .runtime .policy .entitlements .WriteSystemPropertiesEntitlement ;
2021import org .elasticsearch .test .ESTestCase ;
22+ import org .junit .BeforeClass ;
2123
2224import java .io .ByteArrayInputStream ;
2325import java .io .IOException ;
26+ import java .io .InputStream ;
2427import java .nio .charset .StandardCharsets ;
28+ import java .nio .file .Path ;
2529import java .util .List ;
2630import java .util .Map ;
2731import java .util .Set ;
2832
2933import static org .hamcrest .Matchers .equalTo ;
3034
35+ @ ESTestCase .WithoutSecurityManager
3136public class PolicyParserTests extends ESTestCase {
3237
38+ public static String TEST_ABSOLUTE_PATH_TO_FILE ;
39+
40+ @ BeforeClass
41+ public static void beforeClass () throws IOException {
42+ TEST_ABSOLUTE_PATH_TO_FILE = createTempFile ().toAbsolutePath ().toString ();
43+ }
44+
3345 private static class TestWrongEntitlementName implements Entitlement {}
3446
3547 public static class ManyConstructorsEntitlement implements Entitlement {
@@ -79,62 +91,65 @@ public void testGetEntitlementTypeName() {
7991 );
8092 }
8193
94+ private static InputStream createFilesTestPolicy () {
95+ return new ByteArrayInputStream (Strings .format ("""
96+ entitlement-module-name:
97+ - files:
98+ - path: '%s'
99+ mode: "read_write"
100+ """ , TEST_ABSOLUTE_PATH_TO_FILE ).getBytes (StandardCharsets .UTF_8 ));
101+ }
102+
82103 public void testPolicyBuilder () throws IOException {
83- Policy parsedPolicy = new PolicyParser (PolicyParserTests .class .getResourceAsStream ("test-policy.yaml" ), "test-policy.yaml" , false )
84- .parsePolicy ();
104+ Policy parsedPolicy = new PolicyParser (createFilesTestPolicy (), "test-policy.yaml" , false ).parsePolicy ();
85105 Policy expected = new Policy (
86106 "test-policy.yaml" ,
87107 List .of (
88108 new Scope (
89109 "entitlement-module-name" ,
90- List .of (FilesEntitlement .build (List .of (Map .of ("path" , "/test/path/to/file" , "mode" , "read_write" ))))
110+ List .of (FilesEntitlement .build (List .of (Map .of ("path" , TEST_ABSOLUTE_PATH_TO_FILE , "mode" , "read_write" ))))
91111 )
92112 )
93113 );
94114 assertEquals (expected , parsedPolicy );
95115 }
96116
97117 public void testPolicyBuilderOnExternalPlugin () throws IOException {
98- Policy parsedPolicy = new PolicyParser (PolicyParserTests .class .getResourceAsStream ("test-policy.yaml" ), "test-policy.yaml" , true )
99- .parsePolicy ();
118+ Policy parsedPolicy = new PolicyParser (createFilesTestPolicy (), "test-policy.yaml" , true ).parsePolicy ();
100119 Policy expected = new Policy (
101120 "test-policy.yaml" ,
102121 List .of (
103122 new Scope (
104123 "entitlement-module-name" ,
105- List .of (FilesEntitlement .build (List .of (Map .of ("path" , "/test/path/to/file" , "mode" , "read_write" ))))
124+ List .of (FilesEntitlement .build (List .of (Map .of ("path" , TEST_ABSOLUTE_PATH_TO_FILE , "mode" , "read_write" ))))
106125 )
107126 )
108127 );
109128 assertEquals (expected , parsedPolicy );
110129 }
111130
112131 public void testParseFiles () throws IOException {
113- Policy policyWithOnePath = new PolicyParser (new ByteArrayInputStream ("""
114- entitlement-module-name:
115- - files:
116- - path: "/test/path/to/file"
117- mode: "read_write"
118- """ .getBytes (StandardCharsets .UTF_8 )), "test-policy.yaml" , false ).parsePolicy ();
132+ Policy policyWithOnePath = new PolicyParser (createFilesTestPolicy (), "test-policy.yaml" , false ).parsePolicy ();
119133 Policy expected = new Policy (
120134 "test-policy.yaml" ,
121135 List .of (
122136 new Scope (
123137 "entitlement-module-name" ,
124- List .of (FilesEntitlement .build (List .of (Map .of ("path" , "/test/path/to/file" , "mode" , "read_write" ))))
138+ List .of (FilesEntitlement .build (List .of (Map .of ("path" , TEST_ABSOLUTE_PATH_TO_FILE , "mode" , "read_write" ))))
125139 )
126140 )
127141 );
128142 assertEquals (expected , policyWithOnePath );
129143
130- Policy policyWithTwoPaths = new PolicyParser (new ByteArrayInputStream ("""
144+ String testPathToReadDir = createTempDir ().toAbsolutePath ().toString ();
145+ Policy policyWithTwoPaths = new PolicyParser (new ByteArrayInputStream (Strings .format ("""
131146 entitlement-module-name:
132147 - files:
133- - path: "/test/path/to/file"
148+ - path: '%s'
134149 mode: "read_write"
135- - path: "/test/path/to/read-dir/"
150+ - path: '%s'
136151 mode: "read"
137- """ .getBytes (StandardCharsets .UTF_8 )), "test-policy.yaml" , false ).parsePolicy ();
152+ """ , TEST_ABSOLUTE_PATH_TO_FILE , testPathToReadDir ) .getBytes (StandardCharsets .UTF_8 )), "test-policy.yaml" , false ).parsePolicy ();
138153 expected = new Policy (
139154 "test-policy.yaml" ,
140155 List .of (
@@ -143,8 +158,8 @@ public void testParseFiles() throws IOException {
143158 List .of (
144159 FilesEntitlement .build (
145160 List .of (
146- Map .of ("path" , "/test/path/to/file" , "mode" , "read_write" ),
147- Map .of ("path" , "/test/path/to/read-dir/" , "mode" , "read" )
161+ Map .of ("path" , TEST_ABSOLUTE_PATH_TO_FILE , "mode" , "read_write" ),
162+ Map .of ("path" , testPathToReadDir , "mode" , "read" )
148163 )
149164 )
150165 )
@@ -153,18 +168,24 @@ public void testParseFiles() throws IOException {
153168 );
154169 assertEquals (expected , policyWithTwoPaths );
155170
156- Policy policyWithMultiplePathsAndBaseDir = new PolicyParser (new ByteArrayInputStream ("""
157- entitlement-module-name:
158- - files:
159- - relative_path: "test/path/to/file"
160- relative_to: "data"
161- mode: "read_write"
162- - relative_path: "test/path/to/read-dir/"
163- relative_to: "config"
164- mode: "read"
165- - path: "/path/to/file"
166- mode: "read_write"
167- """ .getBytes (StandardCharsets .UTF_8 )), "test-policy.yaml" , false ).parsePolicy ();
171+ String relativePathToFile = Path .of ("test/path/to/file" ).normalize ().toString ();
172+ String relativePathToDir = Path .of ("test/path/to/read-dir/" ).normalize ().toString ();
173+ Policy policyWithMultiplePathsAndBaseDir = new PolicyParser (
174+ new ByteArrayInputStream (Strings .format ("""
175+ entitlement-module-name:
176+ - files:
177+ - relative_path: '%s'
178+ relative_to: "data"
179+ mode: "read_write"
180+ - relative_path: '%s'
181+ relative_to: "config"
182+ mode: "read"
183+ - path: '%s'
184+ mode: "read_write"
185+ """ , relativePathToFile , relativePathToDir , TEST_ABSOLUTE_PATH_TO_FILE ).getBytes (StandardCharsets .UTF_8 )),
186+ "test-policy.yaml" ,
187+ false
188+ ).parsePolicy ();
168189 expected = new Policy (
169190 "test-policy.yaml" ,
170191 List .of (
@@ -173,9 +194,9 @@ public void testParseFiles() throws IOException {
173194 List .of (
174195 FilesEntitlement .build (
175196 List .of (
176- Map .of ("relative_path" , "test/path/to/file" , "mode" , "read_write" , "relative_to" , "data" ),
177- Map .of ("relative_path" , "test/path/to/read-dir/" , "mode" , "read" , "relative_to" , "config" ),
178- Map .of ("path" , "/path/to/file" , "mode" , "read_write" )
197+ Map .of ("relative_path" , relativePathToFile , "mode" , "read_write" , "relative_to" , "data" ),
198+ Map .of ("relative_path" , relativePathToDir , "mode" , "read" , "relative_to" , "config" ),
199+ Map .of ("path" , TEST_ABSOLUTE_PATH_TO_FILE , "mode" , "read_write" )
179200 )
180201 )
181202 )
0 commit comments