Skip to content

Commit a81059a

Browse files
committed
Replace forbidden APIs
1 parent 0b4273a commit a81059a

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

test/framework/src/main/java/org/elasticsearch/bootstrap/BootstrapForTesting.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public class BootstrapForTesting {
8686
if (str == null) {
8787
return null;
8888
} else {
89-
return Path.of(str);
89+
return PathUtils.get(str);
9090
}
9191
}
9292

test/framework/src/main/java/org/elasticsearch/entitlement/bootstrap/TestEntitlementBootstrap.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import org.elasticsearch.bootstrap.TestBuildInfo;
1313
import org.elasticsearch.bootstrap.TestBuildInfoParser;
1414
import org.elasticsearch.bootstrap.TestScopeResolver;
15+
import org.elasticsearch.core.Booleans;
1516
import org.elasticsearch.core.Nullable;
17+
import org.elasticsearch.core.PathUtils;
1618
import org.elasticsearch.core.Strings;
1719
import org.elasticsearch.core.SuppressForbidden;
1820
import org.elasticsearch.entitlement.initialization.EntitlementInitialization;
@@ -25,7 +27,6 @@
2527
import org.elasticsearch.logging.Logger;
2628
import org.elasticsearch.plugins.PluginDescriptor;
2729

28-
import java.io.File;
2930
import java.io.IOException;
3031
import java.io.InputStream;
3132
import java.net.URL;
@@ -74,7 +75,7 @@ private static <T> List<T> zeroOrOne(T item) {
7475
}
7576

7677
public static boolean isEnabledForTest() {
77-
return Boolean.getBoolean("es.entitlement.enableForTests");
78+
return Booleans.parseBoolean("es.entitlement.enableForTests");
7879
}
7980

8081
public static void setActive(boolean newValue) {
@@ -107,17 +108,20 @@ private static TestPolicyManager createPolicyManager(PathLookup pathLookup) thro
107108
.toList();
108109
Map<String, Policy> pluginPolicies = parsePluginsPolicies(pluginsData);
109110

111+
String separator = System.getProperty("path.separator");
112+
110113
// In productions, plugins would have access to their respective bundle directories,
111114
// and so they'd be able to read from their jars. In testing, we approximate this
112115
// by considering the entire classpath to be "source paths" of all plugins. This
113116
// also has the effect of granting read access to everything on the test-only classpath,
114117
// which is fine, because any entitlement errors there could only be false positives.
115118
String classPathProperty = System.getProperty("java.class.path");
119+
116120
Set<Path> classPathEntries;
117121
if (classPathProperty == null) {
118122
classPathEntries = Set.of();
119123
} else {
120-
classPathEntries = Arrays.stream(classPathProperty.split(File.pathSeparator)).map(Path::of).collect(toCollection(TreeSet::new));
124+
classPathEntries = Arrays.stream(classPathProperty.split(separator)).map(PathUtils::get).collect(toCollection(TreeSet::new));
121125
}
122126
Map<String, Collection<Path>> pluginSourcePaths = pluginNames.stream().collect(toMap(n -> n, n -> classPathEntries));
123127

@@ -128,7 +132,7 @@ private static TestPolicyManager createPolicyManager(PathLookup pathLookup) thro
128132
if (testOnlyPathString == null) {
129133
testOnlyClassPath = Set.of();
130134
} else {
131-
testOnlyClassPath = Arrays.stream(testOnlyPathString.split(File.pathSeparator)).collect(toCollection(TreeSet::new));
135+
testOnlyClassPath = Arrays.stream(testOnlyPathString.split(separator)).collect(toCollection(TreeSet::new));
132136
}
133137

134138
return new TestPolicyManager(

test/framework/src/main/java/org/elasticsearch/entitlement/runtime/policy/TestPolicyManager.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.entitlement.runtime.policy.entitlements.Entitlement;
1313
import org.elasticsearch.test.ESTestCase;
1414

15+
import java.net.URISyntaxException;
1516
import java.nio.file.Path;
1617
import java.security.CodeSource;
1718
import java.security.ProtectionDomain;
@@ -134,7 +135,12 @@ private boolean isTestCode(Class<?> requestingClass) {
134135
// This can happen for JDK classes
135136
return false;
136137
}
137-
String needle = codeSource.getLocation().getPath();
138+
String needle;
139+
try {
140+
needle = codeSource.getLocation().toURI().getPath();
141+
} catch (URISyntaxException e) {
142+
throw new IllegalStateException(e);
143+
}
138144
if (needle.endsWith("/")) {
139145
needle = needle.substring(0, needle.length() - 1);
140146
}

0 commit comments

Comments
 (0)