1515import org .elasticsearch .entitlement .bridge .EntitlementChecker ;
1616import org .elasticsearch .entitlement .instrumentation .InstrumentationService ;
1717import org .elasticsearch .entitlement .runtime .api .NotEntitledException ;
18+ import org .elasticsearch .entitlement .runtime .policy .FileAccessTree .ExclusiveFileEntitlement ;
19+ import org .elasticsearch .entitlement .runtime .policy .FileAccessTree .ExclusivePath ;
1820import org .elasticsearch .entitlement .runtime .policy .entitlements .CreateClassLoaderEntitlement ;
1921import org .elasticsearch .entitlement .runtime .policy .entitlements .Entitlement ;
2022import org .elasticsearch .entitlement .runtime .policy .entitlements .ExitVMEntitlement ;
3537import java .lang .module .ModuleReference ;
3638import java .nio .file .Path ;
3739import java .util .ArrayList ;
38- import java .util .Comparator ;
3940import java .util .HashSet ;
4041import java .util .List ;
4142import java .util .Map ;
@@ -172,29 +173,31 @@ public PolicyManager(
172173 this .pathLookup = requireNonNull (pathLookup );
173174 this .defaultFileAccess = FileAccessTree .of ("" , "" , FilesEntitlement .EMPTY , pathLookup , List .of ());
174175
175- List <ExclusivePath > exclusivePaths = new ArrayList <>();
176+ List <ExclusiveFileEntitlement > exclusiveFileEntitlements = new ArrayList <>();
176177 for (var e : serverEntitlements .entrySet ()) {
177- validateEntitlementsPerModule (SERVER_COMPONENT_NAME , e .getKey (), e .getValue ());
178- buildExclusivePathList (exclusivePaths , pathLookup , SERVER_COMPONENT_NAME , e .getKey (), e .getValue ());
178+ validateEntitlementsPerModule (SERVER_COMPONENT_NAME , e .getKey (), e .getValue (), exclusiveFileEntitlements );
179179 }
180- validateEntitlementsPerModule (APM_AGENT_COMPONENT_NAME , ALL_UNNAMED , apmAgentEntitlements );
181- buildExclusivePathList (exclusivePaths , pathLookup , APM_AGENT_COMPONENT_NAME , ALL_UNNAMED , apmAgentEntitlements );
180+ validateEntitlementsPerModule (APM_AGENT_COMPONENT_NAME , ALL_UNNAMED , apmAgentEntitlements , exclusiveFileEntitlements );
182181 for (var p : pluginsEntitlements .entrySet ()) {
183182 for (var m : p .getValue ().entrySet ()) {
184- validateEntitlementsPerModule (p .getKey (), m .getKey (), m .getValue ());
185- buildExclusivePathList (exclusivePaths , pathLookup , p .getKey (), m .getKey (), m .getValue ());
183+ validateEntitlementsPerModule (p .getKey (), m .getKey (), m .getValue (), exclusiveFileEntitlements );
186184 }
187185 }
188- exclusivePaths . sort ( Comparator . comparing ( ExclusivePath :: path ) );
189- validateExclusivePaths (exclusivePaths );
186+ List < ExclusivePath > exclusivePaths = FileAccessTree . buildExclusivePathList ( exclusiveFileEntitlements , pathLookup );
187+ FileAccessTree . validateExclusivePaths (exclusivePaths );
190188 this .exclusivePaths = exclusivePaths ;
191189 }
192190
193191 private static Map <String , List <Entitlement >> buildScopeEntitlementsMap (Policy policy ) {
194192 return policy .scopes ().stream ().collect (toUnmodifiableMap (Scope ::moduleName , Scope ::entitlements ));
195193 }
196194
197- private static void validateEntitlementsPerModule (String componentName , String moduleName , List <Entitlement > entitlements ) {
195+ private static void validateEntitlementsPerModule (
196+ String componentName ,
197+ String moduleName ,
198+ List <Entitlement > entitlements ,
199+ List <ExclusiveFileEntitlement > exclusiveFileEntitlements
200+ ) {
198201 Set <Class <? extends Entitlement >> found = new HashSet <>();
199202 for (var e : entitlements ) {
200203 if (found .contains (e .getClass ())) {
@@ -203,43 +206,8 @@ private static void validateEntitlementsPerModule(String componentName, String m
203206 );
204207 }
205208 found .add (e .getClass ());
206- }
207- }
208-
209- record ExclusivePath (String componentName , String moduleName , Path path ) {}
210-
211- private static void buildExclusivePathList (
212- List <ExclusivePath > exclusivePaths ,
213- PathLookup pathLookup ,
214- String componentName ,
215- String moduleName ,
216- List <Entitlement > entitlements
217- ) {
218- for (var e : entitlements ) {
219209 if (e instanceof FilesEntitlement fe ) {
220- for (FilesEntitlement .FileData fd : fe .filesData ()) {
221- if (fd .exclusive ()) {
222- List <Path > paths = fd .resolvePaths (pathLookup ).toList ();
223- for (Path path : paths ) {
224- exclusivePaths .add (new ExclusivePath (componentName , moduleName , path ));
225- }
226- }
227- }
228- }
229- }
230- }
231-
232- private static void validateExclusivePaths (List <ExclusivePath > exclusivePaths ) {
233- if (exclusivePaths .isEmpty () == false ) {
234- ExclusivePath currentExclusivePath = exclusivePaths .get (0 );
235- for (int i = 1 ; i < exclusivePaths .size (); ++i ) {
236- ExclusivePath nextPath = exclusivePaths .get (i );
237- if (nextPath .path ().equals (currentExclusivePath .path ())) {
238- // TODO: throw
239- } else if (nextPath .path ().startsWith (currentExclusivePath .path ())) {
240- // TODO: throw
241- }
242- currentExclusivePath = nextPath ;
210+ exclusiveFileEntitlements .add (new ExclusiveFileEntitlement (componentName , moduleName , fe ));
243211 }
244212 }
245213 }
0 commit comments