|
@@ -91,12 +91,8 @@ public record FilesEntitlement(List<FileData> filesData) implements Entitlement
|
|
|
return new RelativePathFileData(relativePath, baseDir, mode, null, false);
|
|
|
}
|
|
|
|
|
|
- static FileData ofPathSetting(String setting, Mode mode, boolean ignoreUrl) {
|
|
|
- return new PathSettingFileData(setting, mode, ignoreUrl, null, false);
|
|
|
- }
|
|
|
-
|
|
|
- static FileData ofRelativePathSetting(String setting, BaseDir baseDir, Mode mode, boolean ignoreUrl) {
|
|
|
- return new RelativePathSettingFileData(setting, baseDir, mode, ignoreUrl, null, false);
|
|
|
+ static FileData ofPathSetting(String setting, BaseDir baseDir, Mode mode, boolean ignoreUrl) {
|
|
|
+ return new PathSettingFileData(setting, baseDir, mode, ignoreUrl, null, false);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -225,46 +221,28 @@ public record FilesEntitlement(List<FileData> filesData) implements Entitlement
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private record PathSettingFileData(String setting, Mode mode, boolean ignoreUrl, Platform platform, boolean exclusive)
|
|
|
+ private record PathSettingFileData(String setting, BaseDir baseDir, Mode mode, boolean ignoreUrl, Platform platform, boolean exclusive)
|
|
|
implements
|
|
|
- FileData {
|
|
|
+ RelativeFileData {
|
|
|
|
|
|
@Override
|
|
|
public PathSettingFileData withExclusive(boolean exclusive) {
|
|
|
- return new PathSettingFileData(setting, mode, ignoreUrl, platform, exclusive);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Stream<Path> resolvePaths(PathLookup pathLookup) {
|
|
|
- return resolvePathSettings(pathLookup, setting, ignoreUrl);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public FileData withPlatform(Platform platform) {
|
|
|
- if (platform == platform()) {
|
|
|
- return this;
|
|
|
- }
|
|
|
- return new PathSettingFileData(setting, mode, ignoreUrl, platform, exclusive);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private record RelativePathSettingFileData(
|
|
|
- String setting,
|
|
|
- BaseDir baseDir,
|
|
|
- Mode mode,
|
|
|
- boolean ignoreUrl,
|
|
|
- Platform platform,
|
|
|
- boolean exclusive
|
|
|
- ) implements FileData, RelativeFileData {
|
|
|
-
|
|
|
- @Override
|
|
|
- public RelativePathSettingFileData withExclusive(boolean exclusive) {
|
|
|
- return new RelativePathSettingFileData(setting, baseDir, mode, ignoreUrl, platform, exclusive);
|
|
|
+ return new PathSettingFileData(setting, baseDir, mode, ignoreUrl, platform, exclusive);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Stream<Path> resolveRelativePaths(PathLookup pathLookup) {
|
|
|
- return resolvePathSettings(pathLookup, setting, ignoreUrl);
|
|
|
+ Stream<String> result;
|
|
|
+ if (setting.contains("*")) {
|
|
|
+ result = pathLookup.settingGlobResolver().apply(setting);
|
|
|
+ } else {
|
|
|
+ String path = pathLookup.settingResolver().apply(setting);
|
|
|
+ result = path == null ? Stream.of() : Stream.of(path);
|
|
|
+ }
|
|
|
+ if (ignoreUrl) {
|
|
|
+ result = result.filter(s -> s.toLowerCase(Locale.ROOT).startsWith("https://") == false);
|
|
|
+ }
|
|
|
+ return result.map(pathLookup.configDir()::resolve);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -272,24 +250,10 @@ public record FilesEntitlement(List<FileData> filesData) implements Entitlement
|
|
|
if (platform == platform()) {
|
|
|
return this;
|
|
|
}
|
|
|
- return new RelativePathSettingFileData(setting, baseDir, mode, ignoreUrl, platform, exclusive);
|
|
|
+ return new PathSettingFileData(setting, baseDir, mode, ignoreUrl, platform, exclusive);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static Stream<Path> resolvePathSettings(PathLookup pathLookup, String setting, boolean ignoreUrl) {
|
|
|
- Stream<String> result;
|
|
|
- if (setting.contains("*")) {
|
|
|
- result = pathLookup.settingGlobResolver().apply(setting);
|
|
|
- } else {
|
|
|
- String path = pathLookup.settingResolver().apply(setting);
|
|
|
- result = path == null ? Stream.of() : Stream.of(path);
|
|
|
- }
|
|
|
- if (ignoreUrl) {
|
|
|
- result = result.filter(s -> s.toLowerCase(Locale.ROOT).startsWith("https://") == false);
|
|
|
- }
|
|
|
- return result.map(Path::of);
|
|
|
- }
|
|
|
-
|
|
|
private static Mode parseMode(String mode) {
|
|
|
if (mode.equals("read")) {
|
|
|
return Mode.READ;
|
|
@@ -371,7 +335,7 @@ public record FilesEntitlement(List<FileData> filesData) implements Entitlement
|
|
|
String relativePathAsString = checkString.apply(file, "relative_path");
|
|
|
String relativeTo = checkString.apply(file, "relative_to");
|
|
|
String pathSetting = checkString.apply(file, "path_setting");
|
|
|
- String relativePathSetting = checkString.apply(file, "relative_path_setting");
|
|
|
+ String settingBaseDirAsString = checkString.apply(file, "basedir_if_relative");
|
|
|
String modeAsString = checkString.apply(file, "mode");
|
|
|
String platformAsString = checkString.apply(file, "platform");
|
|
|
Boolean ignoreUrlAsStringBoolean = checkBoolean.apply(file, "ignore_url");
|
|
@@ -382,11 +346,10 @@ public record FilesEntitlement(List<FileData> filesData) implements Entitlement
|
|
|
if (file.isEmpty() == false) {
|
|
|
throw new PolicyValidationException("unknown key(s) [" + file + "] in a listed file for files entitlement");
|
|
|
}
|
|
|
- int foundKeys = (pathAsString != null ? 1 : 0) + (relativePathAsString != null ? 1 : 0) + (pathSetting != null ? 1 : 0)
|
|
|
- + (relativePathSetting != null ? 1 : 0);
|
|
|
+ int foundKeys = (pathAsString != null ? 1 : 0) + (relativePathAsString != null ? 1 : 0) + (pathSetting != null ? 1 : 0);
|
|
|
if (foundKeys != 1) {
|
|
|
throw new PolicyValidationException(
|
|
|
- "a files entitlement entry must contain one of " + "[path, relative_path, path_setting, relative_path_setting]"
|
|
|
+ "a files entitlement entry must contain one of " + "[path, relative_path, path_setting]"
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -399,20 +362,23 @@ public record FilesEntitlement(List<FileData> filesData) implements Entitlement
|
|
|
platform = parsePlatform(platformAsString);
|
|
|
}
|
|
|
|
|
|
- BaseDir baseDir = null;
|
|
|
- if (relativeTo != null) {
|
|
|
- baseDir = parseBaseDir(relativeTo);
|
|
|
+ if (relativeTo != null && relativePathAsString == null) {
|
|
|
+ throw new PolicyValidationException("'relative_to' may only be used with 'relative_path'");
|
|
|
}
|
|
|
|
|
|
- if (ignoreUrlAsStringBoolean != null && (relativePathAsString != null || pathAsString != null)) {
|
|
|
- throw new PolicyValidationException("'ignore_url' may only be used with `path_setting` or `relative_path_setting`");
|
|
|
+ if (ignoreUrlAsStringBoolean != null && pathSetting == null) {
|
|
|
+ throw new PolicyValidationException("'ignore_url' may only be used with 'path_setting'");
|
|
|
+ }
|
|
|
+ if (settingBaseDirAsString != null && pathSetting == null) {
|
|
|
+ throw new PolicyValidationException("'basedir_if_relative' may only be used with 'path_setting'");
|
|
|
}
|
|
|
|
|
|
final FileData fileData;
|
|
|
if (relativePathAsString != null) {
|
|
|
- if (baseDir == null) {
|
|
|
+ if (relativeTo == null) {
|
|
|
throw new PolicyValidationException("files entitlement with a 'relative_path' must specify 'relative_to'");
|
|
|
}
|
|
|
+ BaseDir baseDir = parseBaseDir(relativeTo);
|
|
|
Path relativePath = Path.of(relativePathAsString);
|
|
|
if (FileData.isAbsolutePath(relativePathAsString)) {
|
|
|
throw new PolicyValidationException("'relative_path' [" + relativePathAsString + "] must be relative");
|
|
@@ -425,12 +391,11 @@ public record FilesEntitlement(List<FileData> filesData) implements Entitlement
|
|
|
}
|
|
|
fileData = FileData.ofPath(path, mode);
|
|
|
} else if (pathSetting != null) {
|
|
|
- fileData = FileData.ofPathSetting(pathSetting, mode, ignoreUrlAsString);
|
|
|
- } else if (relativePathSetting != null) {
|
|
|
- if (baseDir == null) {
|
|
|
- throw new PolicyValidationException("files entitlement with a 'relative_path_setting' must specify 'relative_to'");
|
|
|
+ if (settingBaseDirAsString == null) {
|
|
|
+ throw new PolicyValidationException("files entitlement with a 'path_setting' must specify 'basedir_if_relative'");
|
|
|
}
|
|
|
- fileData = FileData.ofRelativePathSetting(relativePathSetting, baseDir, mode, ignoreUrlAsString);
|
|
|
+ BaseDir baseDir = parseBaseDir(settingBaseDirAsString);
|
|
|
+ fileData = FileData.ofPathSetting(pathSetting, baseDir, mode, ignoreUrlAsString);
|
|
|
} else {
|
|
|
throw new AssertionError("File entry validation error");
|
|
|
}
|