@@ -91,12 +91,8 @@ static FileData ofRelativePath(Path relativePath, BaseDir baseDir, Mode mode) {
9191 return new RelativePathFileData (relativePath , baseDir , mode , null , false );
9292 }
9393
94- static FileData ofPathSetting (String setting , Mode mode , boolean ignoreUrl ) {
95- return new PathSettingFileData (setting , mode , ignoreUrl , null , false );
96- }
97-
98- static FileData ofRelativePathSetting (String setting , BaseDir baseDir , Mode mode , boolean ignoreUrl ) {
99- return new RelativePathSettingFileData (setting , baseDir , mode , ignoreUrl , null , false );
94+ static FileData ofPathSetting (String setting , BaseDir baseDir , Mode mode , boolean ignoreUrl ) {
95+ return new PathSettingFileData (setting , baseDir , mode , ignoreUrl , null , false );
10096 }
10197
10298 /**
@@ -225,71 +221,39 @@ public FileData withPlatform(Platform platform) {
225221 }
226222 }
227223
228- private record PathSettingFileData (String setting , Mode mode , boolean ignoreUrl , Platform platform , boolean exclusive )
224+ private record PathSettingFileData (String setting , BaseDir baseDir , Mode mode , boolean ignoreUrl , Platform platform , boolean exclusive )
229225 implements
230- FileData {
226+ RelativeFileData {
231227
232228 @ Override
233229 public PathSettingFileData withExclusive (boolean exclusive ) {
234- return new PathSettingFileData (setting , mode , ignoreUrl , platform , exclusive );
235- }
236-
237- @ Override
238- public Stream <Path > resolvePaths (PathLookup pathLookup ) {
239- return resolvePathSettings (pathLookup , setting , ignoreUrl );
240- }
241-
242- @ Override
243- public FileData withPlatform (Platform platform ) {
244- if (platform == platform ()) {
245- return this ;
246- }
247- return new PathSettingFileData (setting , mode , ignoreUrl , platform , exclusive );
248- }
249- }
250-
251- private record RelativePathSettingFileData (
252- String setting ,
253- BaseDir baseDir ,
254- Mode mode ,
255- boolean ignoreUrl ,
256- Platform platform ,
257- boolean exclusive
258- ) implements FileData , RelativeFileData {
259-
260- @ Override
261- public RelativePathSettingFileData withExclusive (boolean exclusive ) {
262- return new RelativePathSettingFileData (setting , baseDir , mode , ignoreUrl , platform , exclusive );
230+ return new PathSettingFileData (setting , baseDir , mode , ignoreUrl , platform , exclusive );
263231 }
264232
265233 @ Override
266234 public Stream <Path > resolveRelativePaths (PathLookup pathLookup ) {
267- return resolvePathSettings (pathLookup , setting , ignoreUrl );
235+ Stream <String > result ;
236+ if (setting .contains ("*" )) {
237+ result = pathLookup .settingGlobResolver ().apply (setting );
238+ } else {
239+ String path = pathLookup .settingResolver ().apply (setting );
240+ result = path == null ? Stream .of () : Stream .of (path );
241+ }
242+ if (ignoreUrl ) {
243+ result = result .filter (s -> s .toLowerCase (Locale .ROOT ).startsWith ("https://" ) == false );
244+ }
245+ return result .map (pathLookup .configDir ()::resolve );
268246 }
269247
270248 @ Override
271249 public FileData withPlatform (Platform platform ) {
272250 if (platform == platform ()) {
273251 return this ;
274252 }
275- return new RelativePathSettingFileData (setting , baseDir , mode , ignoreUrl , platform , exclusive );
253+ return new PathSettingFileData (setting , baseDir , mode , ignoreUrl , platform , exclusive );
276254 }
277255 }
278256
279- private static Stream <Path > resolvePathSettings (PathLookup pathLookup , String setting , boolean ignoreUrl ) {
280- Stream <String > result ;
281- if (setting .contains ("*" )) {
282- result = pathLookup .settingGlobResolver ().apply (setting );
283- } else {
284- String path = pathLookup .settingResolver ().apply (setting );
285- result = path == null ? Stream .of () : Stream .of (path );
286- }
287- if (ignoreUrl ) {
288- result = result .filter (s -> s .toLowerCase (Locale .ROOT ).startsWith ("https://" ) == false );
289- }
290- return result .map (Path ::of );
291- }
292-
293257 private static Mode parseMode (String mode ) {
294258 if (mode .equals ("read" )) {
295259 return Mode .READ ;
@@ -371,7 +335,7 @@ public static FilesEntitlement build(List<Object> paths) {
371335 String relativePathAsString = checkString .apply (file , "relative_path" );
372336 String relativeTo = checkString .apply (file , "relative_to" );
373337 String pathSetting = checkString .apply (file , "path_setting" );
374- String relativePathSetting = checkString .apply (file , "relative_path_setting " );
338+ String settingBaseDirAsString = checkString .apply (file , "basedir_if_relative " );
375339 String modeAsString = checkString .apply (file , "mode" );
376340 String platformAsString = checkString .apply (file , "platform" );
377341 Boolean ignoreUrlAsStringBoolean = checkBoolean .apply (file , "ignore_url" );
@@ -382,11 +346,10 @@ public static FilesEntitlement build(List<Object> paths) {
382346 if (file .isEmpty () == false ) {
383347 throw new PolicyValidationException ("unknown key(s) [" + file + "] in a listed file for files entitlement" );
384348 }
385- int foundKeys = (pathAsString != null ? 1 : 0 ) + (relativePathAsString != null ? 1 : 0 ) + (pathSetting != null ? 1 : 0 )
386- + (relativePathSetting != null ? 1 : 0 );
349+ int foundKeys = (pathAsString != null ? 1 : 0 ) + (relativePathAsString != null ? 1 : 0 ) + (pathSetting != null ? 1 : 0 );
387350 if (foundKeys != 1 ) {
388351 throw new PolicyValidationException (
389- "a files entitlement entry must contain one of " + "[path, relative_path, path_setting, relative_path_setting ]"
352+ "a files entitlement entry must contain one of " + "[path, relative_path, path_setting]"
390353 );
391354 }
392355
@@ -399,20 +362,23 @@ public static FilesEntitlement build(List<Object> paths) {
399362 platform = parsePlatform (platformAsString );
400363 }
401364
402- BaseDir baseDir = null ;
403- if (relativeTo != null ) {
404- baseDir = parseBaseDir (relativeTo );
365+ if (relativeTo != null && relativePathAsString == null ) {
366+ throw new PolicyValidationException ("'relative_to' may only be used with 'relative_path'" );
405367 }
406368
407- if (ignoreUrlAsStringBoolean != null && (relativePathAsString != null || pathAsString != null )) {
408- throw new PolicyValidationException ("'ignore_url' may only be used with `path_setting` or `relative_path_setting`" );
369+ if (ignoreUrlAsStringBoolean != null && pathSetting == null ) {
370+ throw new PolicyValidationException ("'ignore_url' may only be used with 'path_setting'" );
371+ }
372+ if (settingBaseDirAsString != null && pathSetting == null ) {
373+ throw new PolicyValidationException ("'basedir_if_relative' may only be used with 'path_setting'" );
409374 }
410375
411376 final FileData fileData ;
412377 if (relativePathAsString != null ) {
413- if (baseDir == null ) {
378+ if (relativeTo == null ) {
414379 throw new PolicyValidationException ("files entitlement with a 'relative_path' must specify 'relative_to'" );
415380 }
381+ BaseDir baseDir = parseBaseDir (relativeTo );
416382 Path relativePath = Path .of (relativePathAsString );
417383 if (FileData .isAbsolutePath (relativePathAsString )) {
418384 throw new PolicyValidationException ("'relative_path' [" + relativePathAsString + "] must be relative" );
@@ -425,12 +391,11 @@ public static FilesEntitlement build(List<Object> paths) {
425391 }
426392 fileData = FileData .ofPath (path , mode );
427393 } else if (pathSetting != null ) {
428- fileData = FileData .ofPathSetting (pathSetting , mode , ignoreUrlAsString );
429- } else if (relativePathSetting != null ) {
430- if (baseDir == null ) {
431- throw new PolicyValidationException ("files entitlement with a 'relative_path_setting' must specify 'relative_to'" );
394+ if (settingBaseDirAsString == null ) {
395+ throw new PolicyValidationException ("files entitlement with a 'path_setting' must specify 'basedir_if_relative'" );
432396 }
433- fileData = FileData .ofRelativePathSetting (relativePathSetting , baseDir , mode , ignoreUrlAsString );
397+ BaseDir baseDir = parseBaseDir (settingBaseDirAsString );
398+ fileData = FileData .ofPathSetting (pathSetting , baseDir , mode , ignoreUrlAsString );
434399 } else {
435400 throw new AssertionError ("File entry validation error" );
436401 }
0 commit comments