Skip to content

Commit abae8f2

Browse files
committed
Changing FileData to an interface
1 parent bac9eed commit abae8f2

File tree

2 files changed

+86
-63
lines changed

2 files changed

+86
-63
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.util.Arrays;
1717
import java.util.List;
1818
import java.util.Objects;
19-
import java.util.function.Consumer;
2019

2120
import static org.elasticsearch.core.PathUtils.getDefaultFileSystem;
2221

@@ -28,31 +27,13 @@ public final class FileAccessTree {
2827
private final String[] readPaths;
2928
private final String[] writePaths;
3029

31-
private static void resolvePath(FilesEntitlement.FileData fileData, PathLookup pathLookup, Consumer<Path> resolvedPathReceiver) {
32-
if (fileData.path() != null) {
33-
resolvedPathReceiver.accept(fileData.path());
34-
} else if (fileData.relativePath() != null && fileData.baseDir() != null && pathLookup != null) {
35-
switch (fileData.baseDir()) {
36-
case CONFIG:
37-
resolvedPathReceiver.accept(pathLookup.configDir().resolve(fileData.relativePath()));
38-
break;
39-
case DATA:
40-
Arrays.stream(pathLookup.dataDirs()).map(d -> d.resolve(fileData.relativePath())).forEach(resolvedPathReceiver::accept);
41-
break;
42-
default:
43-
throw new IllegalArgumentException();
44-
}
45-
} else {
46-
throw new IllegalArgumentException();
47-
}
48-
}
49-
5030
private FileAccessTree(FilesEntitlement filesEntitlement, PathLookup pathLookup) {
5131
List<String> readPaths = new ArrayList<>();
5232
List<String> writePaths = new ArrayList<>();
5333
for (FilesEntitlement.FileData fileData : filesEntitlement.filesData()) {
5434
var mode = fileData.mode();
55-
resolvePath(fileData, pathLookup, path -> {
35+
var paths = fileData.resolvePaths(pathLookup);
36+
paths.forEach(path -> {
5637
var normalized = normalizePath(path);
5738
if (mode == FilesEntitlement.Mode.READ_WRITE) {
5839
writePaths.add(normalized);

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/entitlements/FilesEntitlement.java

Lines changed: 84 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
package org.elasticsearch.entitlement.runtime.policy.entitlements;
1111

1212
import org.elasticsearch.entitlement.runtime.policy.ExternalEntitlement;
13+
import org.elasticsearch.entitlement.runtime.policy.PathLookup;
1314
import org.elasticsearch.entitlement.runtime.policy.PolicyValidationException;
1415

1516
import java.nio.file.Path;
1617
import java.util.ArrayList;
18+
import java.util.Arrays;
1719
import java.util.HashMap;
1820
import java.util.List;
1921
import java.util.Map;
2022
import java.util.Objects;
23+
import java.util.stream.Stream;
2124

2225
/**
2326
* Describes a file entitlement with a path and mode.
@@ -36,60 +39,99 @@ public enum BaseDir {
3639
DATA
3740
}
3841

39-
public static final class FileData {
40-
private final Path path;
41-
private final Mode mode;
42-
private final Path relativePath;
43-
private final BaseDir baseDir;
44-
45-
private FileData(Path path, Mode mode, Path relativePath, BaseDir baseDir) {
46-
this.path = path;
47-
this.mode = mode;
48-
this.relativePath = relativePath;
49-
this.baseDir = baseDir;
50-
}
42+
public interface FileData {
5143

52-
public static FileData ofPath(Path path, Mode mode) {
53-
assert path.isAbsolute();
54-
return new FileData(path, mode, null, null);
55-
}
44+
final class AbsolutePathFileData implements FileData {
45+
private final Path path;
46+
private final Mode mode;
5647

57-
public static FileData ofRelativePath(Path relativePath, BaseDir baseDir, Mode mode) {
58-
assert relativePath.isAbsolute() == false;
59-
return new FileData(null, mode, relativePath, baseDir);
60-
}
48+
private AbsolutePathFileData(Path path, Mode mode) {
49+
this.path = path;
50+
this.mode = mode;
51+
}
6152

62-
public Path path() {
63-
return path;
64-
}
53+
@Override
54+
public Stream<Path> resolvePaths(PathLookup pathLookup) {
55+
return Stream.of(path);
56+
}
6557

66-
public Mode mode() {
67-
return mode;
68-
}
58+
@Override
59+
public Mode mode() {
60+
return mode;
61+
}
6962

70-
public Path relativePath() {
71-
return relativePath;
63+
@Override
64+
public boolean equals(Object obj) {
65+
if (obj == this) return true;
66+
if (obj == null || obj.getClass() != this.getClass()) return false;
67+
var that = (AbsolutePathFileData) obj;
68+
return Objects.equals(this.path, that.path) && Objects.equals(this.mode, that.mode);
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return Objects.hash(path, mode);
74+
}
7275
}
7376

74-
public BaseDir baseDir() {
75-
return baseDir;
77+
final class RelativePathFileData implements FileData {
78+
private final Path relativePath;
79+
private final BaseDir baseDir;
80+
private final Mode mode;
81+
82+
private RelativePathFileData(Path relativePath, BaseDir baseDir, Mode mode) {
83+
this.relativePath = relativePath;
84+
this.baseDir = baseDir;
85+
this.mode = mode;
86+
}
87+
88+
@Override
89+
public Stream<Path> resolvePaths(PathLookup pathLookup) {
90+
Objects.requireNonNull(pathLookup);
91+
switch (baseDir) {
92+
case CONFIG:
93+
return Stream.of(pathLookup.configDir().resolve(relativePath));
94+
case DATA:
95+
return Arrays.stream(pathLookup.dataDirs()).map(d -> d.resolve(relativePath));
96+
default:
97+
throw new IllegalArgumentException();
98+
}
99+
}
100+
101+
@Override
102+
public Mode mode() {
103+
return mode;
104+
}
105+
106+
@Override
107+
public boolean equals(Object obj) {
108+
if (obj == this) return true;
109+
if (obj == null || obj.getClass() != this.getClass()) return false;
110+
var that = (RelativePathFileData) obj;
111+
return Objects.equals(this.mode, that.mode)
112+
&& Objects.equals(this.relativePath, that.relativePath)
113+
&& Objects.equals(this.baseDir, that.baseDir);
114+
}
115+
116+
@Override
117+
public int hashCode() {
118+
return Objects.hash(relativePath, baseDir, mode);
119+
}
76120
}
77121

78-
@Override
79-
public boolean equals(Object obj) {
80-
if (obj == this) return true;
81-
if (obj == null || obj.getClass() != this.getClass()) return false;
82-
var that = (FileData) obj;
83-
return Objects.equals(this.path, that.path)
84-
&& Objects.equals(this.mode, that.mode)
85-
&& Objects.equals(this.relativePath, that.relativePath)
86-
&& Objects.equals(this.baseDir, that.baseDir);
122+
static FileData ofPath(Path path, Mode mode) {
123+
assert path.isAbsolute();
124+
return new AbsolutePathFileData(path, mode);
87125
}
88126

89-
@Override
90-
public int hashCode() {
91-
return Objects.hash(path, mode, relativePath, baseDir);
127+
static FileData ofRelativePath(Path relativePath, BaseDir baseDir, Mode mode) {
128+
assert relativePath.isAbsolute() == false;
129+
return new RelativePathFileData(relativePath, baseDir, mode);
92130
}
131+
132+
Stream<Path> resolvePaths(PathLookup pathLookup);
133+
134+
Mode mode();
93135
}
94136

95137
private static Mode parseMode(String mode) {

0 commit comments

Comments
 (0)