5858import java .nio .file .attribute .FileAttribute ;
5959import java .nio .file .spi .FileSystemProvider ;
6060import java .util .ArrayList ;
61- import java .util .Arrays ;
6261import java .util .Collections ;
6362import java .util .HashMap ;
63+ import java .util .HashSet ;
6464import java .util .List ;
6565import java .util .Map ;
6666import java .util .Set ;
7070import java .util .stream .Stream ;
7171import java .util .stream .StreamSupport ;
7272
73+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .CONFIG ;
74+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .DATA ;
75+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .LIB ;
76+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .LOGS ;
77+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .MODULES ;
78+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .PLUGINS ;
79+ import static org .elasticsearch .entitlement .runtime .policy .PathLookup .BaseDir .SHARED_REPO ;
7380import static org .elasticsearch .entitlement .runtime .policy .Platform .LINUX ;
74- import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .BaseDir .CONFIG ;
75- import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .BaseDir .DATA ;
76- import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .BaseDir .SHARED_REPO ;
7781import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .Mode .READ ;
7882import static org .elasticsearch .entitlement .runtime .policy .entitlements .FilesEntitlement .Mode .READ_WRITE ;
7983
@@ -165,27 +169,20 @@ private static Class<?>[] findClassesToRetransform(Class<?>[] loadedClasses, Set
165169 private static PolicyManager createPolicyManager () {
166170 EntitlementBootstrap .BootstrapArgs bootstrapArgs = EntitlementBootstrap .bootstrapArgs ();
167171 Map <String , Policy > pluginPolicies = bootstrapArgs .pluginPolicies ();
168- var pathLookup = new PathLookup (
169- getUserHome (),
170- bootstrapArgs .configDir (),
171- bootstrapArgs .dataDirs (),
172- bootstrapArgs .sharedRepoDirs (),
173- bootstrapArgs .tempDir (),
174- bootstrapArgs .settingResolver ()
175- );
172+ PathLookup pathLookup = bootstrapArgs .pathLookup ();
176173
177174 List <Scope > serverScopes = new ArrayList <>();
178175 List <FileData > serverModuleFileDatas = new ArrayList <>();
179176 Collections .addAll (
180177 serverModuleFileDatas ,
181178 // Base ES directories
182- FileData .ofPath ( bootstrapArgs . pluginsDir () , READ ),
183- FileData .ofPath ( bootstrapArgs . modulesDir () , READ ),
184- FileData .ofPath ( bootstrapArgs . configDir () , READ ),
185- FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ),
186- FileData .ofPath ( bootstrapArgs . libDir () , READ ),
187- FileData .ofRelativePath ( Path . of ( "" ), DATA , READ_WRITE ),
188- FileData .ofRelativePath ( Path . of ( "" ), SHARED_REPO , READ_WRITE ),
179+ FileData .ofBaseDirPath ( PLUGINS , READ ),
180+ FileData .ofBaseDirPath ( MODULES , READ ),
181+ FileData .ofBaseDirPath ( CONFIG , READ ),
182+ FileData .ofBaseDirPath ( LOGS , READ_WRITE ),
183+ FileData .ofBaseDirPath ( LIB , READ ),
184+ FileData .ofBaseDirPath ( DATA , READ_WRITE ),
185+ FileData .ofBaseDirPath ( SHARED_REPO , READ_WRITE ),
189186 // exclusive settings file
190187 FileData .ofRelativePath (Path .of ("operator/settings.json" ), CONFIG , READ_WRITE ).withExclusive (true ),
191188 // OS release on Linux
@@ -206,8 +203,8 @@ private static PolicyManager createPolicyManager() {
206203 FileData .ofPath (Path .of ("/proc/self/mountinfo" ), READ ).withPlatform (LINUX ),
207204 FileData .ofPath (Path .of ("/proc/diskstats" ), READ ).withPlatform (LINUX )
208205 );
209- if (bootstrapArgs .pidFile () != null ) {
210- serverModuleFileDatas .add (FileData .ofPath (bootstrapArgs .pidFile (), READ_WRITE ));
206+ if (pathLookup .pidFile () != null ) {
207+ serverModuleFileDatas .add (FileData .ofPath (pathLookup .pidFile (), READ_WRITE ));
211208 }
212209
213210 Collections .addAll (
@@ -219,8 +216,8 @@ private static PolicyManager createPolicyManager() {
219216 new FilesEntitlement (
220217 List .of (
221218 // TODO: what in es.base is accessing shared repo?
222- FileData .ofRelativePath ( Path . of ( "" ), SHARED_REPO , READ_WRITE ),
223- FileData .ofRelativePath ( Path . of ( "" ), DATA , READ_WRITE )
219+ FileData .ofBaseDirPath ( SHARED_REPO , READ_WRITE ),
220+ FileData .ofBaseDirPath ( DATA , READ_WRITE )
224221 )
225222 )
226223 )
@@ -245,25 +242,17 @@ private static PolicyManager createPolicyManager() {
245242 List .of (
246243 new LoadNativeLibrariesEntitlement (),
247244 new ManageThreadsEntitlement (),
248- new FilesEntitlement (
249- List .of (FileData .ofPath (bootstrapArgs .configDir (), READ ), FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE ))
250- )
245+ new FilesEntitlement (List .of (FileData .ofBaseDirPath (CONFIG , READ ), FileData .ofBaseDirPath (DATA , READ_WRITE )))
251246 )
252247 ),
253- new Scope (
254- "org.apache.lucene.misc" ,
255- List .of (new FilesEntitlement (List .of (FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE ))))
256- ),
248+ new Scope ("org.apache.lucene.misc" , List .of (new FilesEntitlement (List .of (FileData .ofBaseDirPath (DATA , READ_WRITE ))))),
257249 new Scope (
258250 "org.apache.logging.log4j.core" ,
259- List .of (new ManageThreadsEntitlement (), new FilesEntitlement (List .of (FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ))))
251+ List .of (new ManageThreadsEntitlement (), new FilesEntitlement (List .of (FileData .ofBaseDirPath ( LOGS , READ_WRITE ))))
260252 ),
261253 new Scope (
262254 "org.elasticsearch.nativeaccess" ,
263- List .of (
264- new LoadNativeLibrariesEntitlement (),
265- new FilesEntitlement (List .of (FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE )))
266- )
255+ List .of (new LoadNativeLibrariesEntitlement (), new FilesEntitlement (List .of (FileData .ofBaseDirPath (DATA , READ_WRITE ))))
267256 )
268257 );
269258
@@ -288,7 +277,7 @@ private static PolicyManager createPolicyManager() {
288277 new Scope (
289278 "org.bouncycastle.fips.core" ,
290279 // read to lib dir is required for checksum validation
291- List .of (new FilesEntitlement (List .of (FileData .ofPath ( bootstrapArgs . libDir () , READ ))), new ManageThreadsEntitlement ())
280+ List .of (new FilesEntitlement (List .of (FileData .ofBaseDirPath ( LIB , READ ))), new ManageThreadsEntitlement ())
292281 )
293282 );
294283 }
@@ -312,21 +301,14 @@ private static PolicyManager createPolicyManager() {
312301 new LoadNativeLibrariesEntitlement (),
313302 new FilesEntitlement (
314303 List .of (
315- FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ),
304+ FileData .ofBaseDirPath ( LOGS , READ_WRITE ),
316305 FileData .ofPath (Path .of ("/proc/meminfo" ), READ ),
317306 FileData .ofPath (Path .of ("/sys/fs/cgroup/" ), READ )
318307 )
319308 )
320309 );
321310
322- validateFilesEntitlements (
323- pluginPolicies ,
324- pathLookup ,
325- bootstrapArgs .configDir (),
326- bootstrapArgs .pluginsDir (),
327- bootstrapArgs .modulesDir (),
328- bootstrapArgs .libDir ()
329- );
311+ validateFilesEntitlements (pluginPolicies , pathLookup );
330312
331313 return new PolicyManager (
332314 serverPolicy ,
@@ -341,21 +323,14 @@ private static PolicyManager createPolicyManager() {
341323 );
342324 }
343325
344- private static Set <Path > pathSet (Path ... paths ) {
345- return Arrays .stream (paths ).map (x -> x .toAbsolutePath ().normalize ()).collect (Collectors .toUnmodifiableSet ());
346- }
347-
348326 // package visible for tests
349- static void validateFilesEntitlements (
350- Map <String , Policy > pluginPolicies ,
351- PathLookup pathLookup ,
352- Path configDir ,
353- Path pluginsDir ,
354- Path modulesDir ,
355- Path libDir
356- ) {
357- var readAccessForbidden = pathSet (pluginsDir , modulesDir , libDir );
358- var writeAccessForbidden = pathSet (configDir );
327+ static void validateFilesEntitlements (Map <String , Policy > pluginPolicies , PathLookup pathLookup ) {
328+ Set <Path > readAccessForbidden = new HashSet <>();
329+ pathLookup .getBaseDirPaths (PLUGINS ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
330+ pathLookup .getBaseDirPaths (MODULES ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
331+ pathLookup .getBaseDirPaths (LIB ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
332+ Set <Path > writeAccessForbidden = new HashSet <>();
333+ pathLookup .getBaseDirPaths (CONFIG ).forEach (p -> writeAccessForbidden .add (p .toAbsolutePath ().normalize ()));
359334 for (var pluginPolicy : pluginPolicies .entrySet ()) {
360335 for (var scope : pluginPolicy .getValue ().scopes ()) {
361336 var filesEntitlement = scope .entitlements ()
0 commit comments