-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add support for setting based file entitlements #122656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bf48c0b
Add support for setting based file entitlements
rjernst b328123
add relative support
rjernst 20f20d3
[CI] Auto commit changes from spotless
809df1b
fix tests
rjernst 9494ef4
[CI] Auto commit changes from spotless
f3b88c9
Merge branch 'main' into entitlements/path_setting
rjernst 9a466a9
Merge branch 'main' into entitlements/path_setting
rjernst 338c1e9
Merge branch 'main' into entitlements/path_setting
rjernst 160b116
add back temp for now
rjernst a5b4f14
try normalizing temp
rjernst 35ca4a9
pass correct temp dir
rjernst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,33 @@ public enum BaseDir { | |
|
|
||
| public sealed interface FileData { | ||
|
|
||
| sealed interface RelativeFileData extends FileData { | ||
| BaseDir baseDir(); | ||
|
|
||
| Stream<Path> resolveRelativePaths(PathLookup pathLookup); | ||
|
|
||
| @Override | ||
| default Stream<Path> resolvePaths(PathLookup pathLookup) { | ||
| Objects.requireNonNull(pathLookup); | ||
| var relativePaths = resolveRelativePaths(pathLookup); | ||
| switch (baseDir()) { | ||
| case CONFIG: | ||
| return relativePaths.map(relativePath -> pathLookup.configDir().resolve(relativePath)); | ||
| case DATA: | ||
| // multiple data dirs are a pain...we need the combination of relative paths and data dirs | ||
| List<Path> paths = new ArrayList<>(); | ||
| for (var relativePath : relativePaths.toList()) { | ||
| for (var dataDir : pathLookup.dataDirs()) { | ||
| paths.add(dataDir.resolve(relativePath)); | ||
| } | ||
| } | ||
| return paths.stream(); | ||
| default: | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| final class AbsolutePathFileData implements FileData { | ||
|
||
| private final Path path; | ||
| private final Mode mode; | ||
|
|
@@ -119,6 +146,28 @@ public int hashCode() { | |
| } | ||
| } | ||
|
|
||
| record PathSettingFileData(String setting, Mode mode) implements FileData { | ||
| @Override | ||
| public Stream<Path> resolvePaths(PathLookup pathLookup) { | ||
| return FileData.resolvePathSettings(pathLookup, setting); | ||
| } | ||
| } | ||
|
|
||
| record RelativePathSettingFileData(String setting, BaseDir baseDir, Mode mode) implements FileData, RelativeFileData { | ||
| @Override | ||
| public Stream<Path> resolveRelativePaths(PathLookup pathLookup) { | ||
| return FileData.resolvePathSettings(pathLookup, setting); | ||
| } | ||
| } | ||
|
|
||
| private static Stream<Path> resolvePathSettings(PathLookup pathLookup, String setting) { | ||
| if (setting.contains("*")) { | ||
| return pathLookup.settingGlobResolver().apply(setting).map(Path::of); | ||
| } | ||
| String path = pathLookup.settingResolver().apply(setting); | ||
| return path == null ? Stream.of() : Stream.of(Path.of(path)); | ||
| } | ||
|
|
||
| static FileData ofPath(Path path, Mode mode) { | ||
| assert path.isAbsolute(); | ||
| return new AbsolutePathFileData(path, mode); | ||
|
|
@@ -129,6 +178,14 @@ static FileData ofRelativePath(Path relativePath, BaseDir baseDir, Mode mode) { | |
| return new RelativePathFileData(relativePath, baseDir, mode); | ||
| } | ||
|
|
||
| static FileData ofPathSetting(String setting, Mode mode) { | ||
| return new PathSettingFileData(setting, mode); | ||
| } | ||
|
|
||
| static FileData ofRelativePathSetting(String setting, BaseDir baseDir, Mode mode) { | ||
| return new RelativePathSettingFileData(setting, baseDir, mode); | ||
| } | ||
|
|
||
| Stream<Path> resolvePaths(PathLookup pathLookup); | ||
|
|
||
| Mode mode(); | ||
|
|
@@ -165,37 +222,56 @@ public static FilesEntitlement build(List<Object> paths) { | |
| String pathAsString = file.remove("path"); | ||
| String relativePathAsString = file.remove("relative_path"); | ||
| String relativeTo = file.remove("relative_to"); | ||
| String mode = file.remove("mode"); | ||
| String pathSetting = file.remove("path_setting"); | ||
| String relativePathSetting = file.remove("relative_path_setting"); | ||
| String modeAsString = file.remove("mode"); | ||
|
|
||
| if (file.isEmpty() == false) { | ||
| throw new PolicyValidationException("unknown key(s) [" + file + "] in a listed file for files entitlement"); | ||
| } | ||
| if (mode == null) { | ||
| int foundKeys = (pathAsString != null ? 1 : 0) + (relativePathAsString != null ? 1 : 0) + (pathSetting != null ? 1 : 0) | ||
| + (relativePathSetting != 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]" | ||
| ); | ||
| } | ||
|
|
||
| if (modeAsString == null) { | ||
| throw new PolicyValidationException("files entitlement must contain 'mode' for every listed file"); | ||
| } | ||
| if (pathAsString != null && relativePathAsString != null) { | ||
| throw new PolicyValidationException("a files entitlement entry cannot contain both 'path' and 'relative_path'"); | ||
| Mode mode = parseMode(modeAsString); | ||
|
|
||
| BaseDir baseDir = null; | ||
| if (relativeTo != null) { | ||
| baseDir = parseBaseDir(relativeTo); | ||
| } | ||
|
|
||
| if (relativePathAsString != null) { | ||
| if (relativeTo == null) { | ||
| if (baseDir == null) { | ||
| throw new PolicyValidationException("files entitlement with a 'relative_path' must specify 'relative_to'"); | ||
| } | ||
| final BaseDir baseDir = parseBaseDir(relativeTo); | ||
|
|
||
| Path relativePath = Path.of(relativePathAsString); | ||
| if (relativePath.isAbsolute()) { | ||
| throw new PolicyValidationException("'relative_path' [" + relativePathAsString + "] must be relative"); | ||
| } | ||
| filesData.add(FileData.ofRelativePath(relativePath, baseDir, parseMode(mode))); | ||
| filesData.add(FileData.ofRelativePath(relativePath, baseDir, mode)); | ||
| } else if (pathAsString != null) { | ||
| Path path = Path.of(pathAsString); | ||
| if (path.isAbsolute() == false) { | ||
| throw new PolicyValidationException("'path' [" + pathAsString + "] must be absolute"); | ||
| } | ||
| filesData.add(FileData.ofPath(path, parseMode(mode))); | ||
| filesData.add(FileData.ofPath(path, mode)); | ||
| } else if (pathSetting != null) { | ||
| filesData.add(FileData.ofPathSetting(pathSetting, mode)); | ||
| } else if (relativePathSetting != null) { | ||
| if (baseDir == null) { | ||
| throw new PolicyValidationException("files entitlement with a 'relative_path_setting' must specify 'relative_to'"); | ||
| } | ||
| filesData.add(FileData.ofRelativePathSetting(relativePathSetting, baseDir, mode)); | ||
| } else { | ||
| throw new PolicyValidationException("files entitlement must contain either 'path' or 'relative_path' for every entry"); | ||
| throw new AssertionError("File entry validation error"); | ||
| } | ||
| } | ||
| return new FilesEntitlement(filesData); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is why I think and interface is better than a record here; a record with 2 Function is just and interface in disguise. OK to keep *Dir() as simple getters though.
Not blocking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still in favor of the record here as there is only a single implementation. The resolvers are just necessary due to
Settingsnot being available in the entitlements libThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with Moritz, I prefer a record because it is "things" we are passing in. Creating an interface would require creating an implementation, but the record provides that.