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
8690 */
8791public class EntitlementInitialization {
8892
89- private static final String AGENTS_PACKAGE_NAME = "co.elastic.apm.agent" ;
9093 private static final Module ENTITLEMENTS_MODULE = PolicyManager .class .getModule ();
9194
9295 private static ElasticsearchEntitlementChecker manager ;
@@ -165,27 +168,20 @@ private static Class<?>[] findClassesToRetransform(Class<?>[] loadedClasses, Set
165168 private static PolicyManager createPolicyManager () {
166169 EntitlementBootstrap .BootstrapArgs bootstrapArgs = EntitlementBootstrap .bootstrapArgs ();
167170 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- );
171+ PathLookup pathLookup = bootstrapArgs .pathLookup ();
176172
177173 List <Scope > serverScopes = new ArrayList <>();
178174 List <FileData > serverModuleFileDatas = new ArrayList <>();
179175 Collections .addAll (
180176 serverModuleFileDatas ,
181177 // 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 ),
178+ FileData .ofBaseDirPath ( PLUGINS , READ ),
179+ FileData .ofBaseDirPath ( MODULES , READ ),
180+ FileData .ofBaseDirPath ( CONFIG , READ ),
181+ FileData .ofBaseDirPath ( LOGS , READ_WRITE ),
182+ FileData .ofBaseDirPath ( LIB , READ ),
183+ FileData .ofBaseDirPath ( DATA , READ_WRITE ),
184+ FileData .ofBaseDirPath ( SHARED_REPO , READ_WRITE ),
189185 // exclusive settings file
190186 FileData .ofRelativePath (Path .of ("operator/settings.json" ), CONFIG , READ_WRITE ).withExclusive (true ),
191187 // OS release on Linux
@@ -206,8 +202,8 @@ private static PolicyManager createPolicyManager() {
206202 FileData .ofPath (Path .of ("/proc/self/mountinfo" ), READ ).withPlatform (LINUX ),
207203 FileData .ofPath (Path .of ("/proc/diskstats" ), READ ).withPlatform (LINUX )
208204 );
209- if (bootstrapArgs .pidFile () != null ) {
210- serverModuleFileDatas .add (FileData .ofPath (bootstrapArgs .pidFile (), READ_WRITE ));
205+ if (pathLookup .pidFile () != null ) {
206+ serverModuleFileDatas .add (FileData .ofPath (pathLookup .pidFile (), READ_WRITE ));
211207 }
212208
213209 Collections .addAll (
@@ -219,8 +215,8 @@ private static PolicyManager createPolicyManager() {
219215 new FilesEntitlement (
220216 List .of (
221217 // 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 )
218+ FileData .ofBaseDirPath ( SHARED_REPO , READ_WRITE ),
219+ FileData .ofBaseDirPath ( DATA , READ_WRITE )
224220 )
225221 )
226222 )
@@ -245,25 +241,17 @@ private static PolicyManager createPolicyManager() {
245241 List .of (
246242 new LoadNativeLibrariesEntitlement (),
247243 new ManageThreadsEntitlement (),
248- new FilesEntitlement (
249- List .of (FileData .ofPath (bootstrapArgs .configDir (), READ ), FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE ))
250- )
244+ new FilesEntitlement (List .of (FileData .ofBaseDirPath (CONFIG , READ ), FileData .ofBaseDirPath (DATA , READ_WRITE )))
251245 )
252246 ),
253- new Scope (
254- "org.apache.lucene.misc" ,
255- List .of (new FilesEntitlement (List .of (FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE ))))
256- ),
247+ new Scope ("org.apache.lucene.misc" , List .of (new FilesEntitlement (List .of (FileData .ofBaseDirPath (DATA , READ_WRITE ))))),
257248 new Scope (
258249 "org.apache.logging.log4j.core" ,
259- List .of (new ManageThreadsEntitlement (), new FilesEntitlement (List .of (FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ))))
250+ List .of (new ManageThreadsEntitlement (), new FilesEntitlement (List .of (FileData .ofBaseDirPath ( LOGS , READ_WRITE ))))
260251 ),
261252 new Scope (
262253 "org.elasticsearch.nativeaccess" ,
263- List .of (
264- new LoadNativeLibrariesEntitlement (),
265- new FilesEntitlement (List .of (FileData .ofRelativePath (Path .of ("" ), DATA , READ_WRITE )))
266- )
254+ List .of (new LoadNativeLibrariesEntitlement (), new FilesEntitlement (List .of (FileData .ofBaseDirPath (DATA , READ_WRITE ))))
267255 )
268256 );
269257
@@ -288,7 +276,7 @@ private static PolicyManager createPolicyManager() {
288276 new Scope (
289277 "org.bouncycastle.fips.core" ,
290278 // read to lib dir is required for checksum validation
291- List .of (new FilesEntitlement (List .of (FileData .ofPath ( bootstrapArgs . libDir () , READ ))), new ManageThreadsEntitlement ())
279+ List .of (new FilesEntitlement (List .of (FileData .ofBaseDirPath ( LIB , READ ))), new ManageThreadsEntitlement ())
292280 )
293281 );
294282 }
@@ -312,50 +300,35 @@ private static PolicyManager createPolicyManager() {
312300 new LoadNativeLibrariesEntitlement (),
313301 new FilesEntitlement (
314302 List .of (
315- FileData .ofPath ( bootstrapArgs . logsDir () , READ_WRITE ),
303+ FileData .ofBaseDirPath ( LOGS , READ_WRITE ),
316304 FileData .ofPath (Path .of ("/proc/meminfo" ), READ ),
317305 FileData .ofPath (Path .of ("/sys/fs/cgroup/" ), READ )
318306 )
319307 )
320308 );
321309
322- validateFilesEntitlements (
323- pluginPolicies ,
324- pathLookup ,
325- bootstrapArgs .configDir (),
326- bootstrapArgs .pluginsDir (),
327- bootstrapArgs .modulesDir (),
328- bootstrapArgs .libDir ()
329- );
310+ validateFilesEntitlements (pluginPolicies , pathLookup );
330311
331312 return new PolicyManager (
332313 serverPolicy ,
333314 agentEntitlements ,
334315 pluginPolicies ,
335- EntitlementBootstrap .bootstrapArgs ().pluginResolver (),
316+ EntitlementBootstrap .bootstrapArgs ().scopeResolver (),
336317 EntitlementBootstrap .bootstrapArgs ().sourcePaths (),
337- AGENTS_PACKAGE_NAME ,
338318 ENTITLEMENTS_MODULE ,
339319 pathLookup ,
340320 bootstrapArgs .suppressFailureLogClasses ()
341321 );
342322 }
343323
344- private static Set <Path > pathSet (Path ... paths ) {
345- return Arrays .stream (paths ).map (x -> x .toAbsolutePath ().normalize ()).collect (Collectors .toUnmodifiableSet ());
346- }
347-
348324 // 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 );
325+ static void validateFilesEntitlements (Map <String , Policy > pluginPolicies , PathLookup pathLookup ) {
326+ Set <Path > readAccessForbidden = new HashSet <>();
327+ pathLookup .getBaseDirPaths (PLUGINS ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
328+ pathLookup .getBaseDirPaths (MODULES ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
329+ pathLookup .getBaseDirPaths (LIB ).forEach (p -> readAccessForbidden .add (p .toAbsolutePath ().normalize ()));
330+ Set <Path > writeAccessForbidden = new HashSet <>();
331+ pathLookup .getBaseDirPaths (CONFIG ).forEach (p -> writeAccessForbidden .add (p .toAbsolutePath ().normalize ()));
359332 for (var pluginPolicy : pluginPolicies .entrySet ()) {
360333 for (var scope : pluginPolicy .getValue ().scopes ()) {
361334 var filesEntitlement = scope .entitlements ()
0 commit comments