|
|
@@ -53,6 +53,7 @@ import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Properties;
|
|
|
import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.carrotsearch.randomizedtesting.RandomizedTest.systemPropertyAsBoolean;
|
|
|
import static org.elasticsearch.bootstrap.FilePermissionUtils.addDirectoryPath;
|
|
|
@@ -138,22 +139,12 @@ public class BootstrapForTesting {
|
|
|
// TODO: cut over all tests to bind to ephemeral ports
|
|
|
perms.add(new SocketPermission("localhost:1024-", "listen,resolve"));
|
|
|
|
|
|
- boolean inGradle = System.getProperty("tests.gradle") != null;
|
|
|
-
|
|
|
// read test-framework permissions
|
|
|
- Map<String, URL> codebases = PolicyUtil.getCodebaseJarMap(JarHell.parseClassPath());
|
|
|
- // when testing server, the main elasticsearch code is not yet in a jar, so we need to manually add it
|
|
|
- addClassCodebase(codebases,"elasticsearch", "org.elasticsearch.plugins.PluginsService");
|
|
|
- if (inGradle == false) {
|
|
|
- // intellij and eclipse don't package our internal libs, so we need to set the codebases for them manually
|
|
|
- addClassCodebase(codebases,"elasticsearch-plugin-classloader", "org.elasticsearch.plugins.ExtendedPluginsClassLoader");
|
|
|
- addClassCodebase(codebases,"elasticsearch-nio", "org.elasticsearch.nio.ChannelFactory");
|
|
|
- addClassCodebase(codebases, "elasticsearch-secure-sm", "org.elasticsearch.secure_sm.SecureSM");
|
|
|
- addClassCodebase(codebases, "elasticsearch-rest-client", "org.elasticsearch.client.RestClient");
|
|
|
- }
|
|
|
+ Map<String, URL> codebases = getCodebases();
|
|
|
+
|
|
|
final Policy testFramework = PolicyUtil.readPolicy(Bootstrap.class.getResource("test-framework.policy"), codebases);
|
|
|
final Policy runnerPolicy;
|
|
|
- if (inGradle) {
|
|
|
+ if (System.getProperty("tests.gradle") != null) {
|
|
|
runnerPolicy = PolicyUtil.readPolicy(Bootstrap.class.getResource("gradle.policy"), codebases);
|
|
|
} else {
|
|
|
runnerPolicy = PolicyUtil.readPolicy(Bootstrap.class.getResource("intellij.policy"), codebases);
|
|
|
@@ -192,9 +183,23 @@ public class BootstrapForTesting {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ static Map<String, URL> getCodebases() {
|
|
|
+ Map<String, URL> codebases = PolicyUtil.getCodebaseJarMap(JarHell.parseClassPath());
|
|
|
+ // when testing server, the main elasticsearch code is not yet in a jar, so we need to manually add it
|
|
|
+ addClassCodebase(codebases,"elasticsearch", "org.elasticsearch.plugins.PluginsService");
|
|
|
+ addClassCodebase(codebases,"elasticsearch-plugin-classloader", "org.elasticsearch.plugins.ExtendedPluginsClassLoader");
|
|
|
+ addClassCodebase(codebases,"elasticsearch-nio", "org.elasticsearch.nio.ChannelFactory");
|
|
|
+ addClassCodebase(codebases, "elasticsearch-secure-sm", "org.elasticsearch.secure_sm.SecureSM");
|
|
|
+ addClassCodebase(codebases, "elasticsearch-rest-client", "org.elasticsearch.client.RestClient");
|
|
|
+ return codebases;
|
|
|
+ }
|
|
|
+
|
|
|
/** Add the codebase url of the given classname to the codebases map, if the class exists. */
|
|
|
private static void addClassCodebase(Map<String, URL> codebases, String name, String classname) {
|
|
|
try {
|
|
|
+ if (codebases.containsKey(name)) {
|
|
|
+ return; // the codebase already exists, from the classpath
|
|
|
+ }
|
|
|
Class<?> clazz = BootstrapForTesting.class.getClassLoader().loadClass(classname);
|
|
|
URL location = clazz.getProtectionDomain().getCodeSource().getLocation();
|
|
|
if (location.toString().endsWith(".jar") == false) {
|
|
|
@@ -234,11 +239,30 @@ public class BootstrapForTesting {
|
|
|
Assert.class.getProtectionDomain().getCodeSource().getLocation()
|
|
|
));
|
|
|
codebases.removeAll(excluded);
|
|
|
+ final Map<String, URL> codebasesMap = PolicyUtil.getCodebaseJarMap(codebases);
|
|
|
|
|
|
// parse each policy file, with codebase substitution from the classpath
|
|
|
final List<Policy> policies = new ArrayList<>(pluginPolicies.size());
|
|
|
for (URL policyFile : pluginPolicies) {
|
|
|
- policies.add(PolicyUtil.readPolicy(policyFile, PolicyUtil.getCodebaseJarMap(codebases)));
|
|
|
+ Map<String, URL> policyCodebases = codebasesMap;
|
|
|
+
|
|
|
+ // if the codebases file is inside a jar, then we don't need to load it since the jar will
|
|
|
+ // have already been read from the classpath
|
|
|
+ if (policyFile.toString().contains(".jar!") == false) {
|
|
|
+ Path policyPath = PathUtils.get(policyFile.toURI());
|
|
|
+ Path codebasesPath = policyPath.getParent().resolve("plugin-security.codebases");
|
|
|
+
|
|
|
+ if (Files.exists(codebasesPath)) {
|
|
|
+ // load codebase to class map used for tests
|
|
|
+ policyCodebases = new HashMap<>(codebasesMap);
|
|
|
+ Map<String, String> codebasesProps = parsePropertiesFile(codebasesPath);
|
|
|
+ for (var entry : codebasesProps.entrySet()) {
|
|
|
+ addClassCodebase(policyCodebases, entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ policies.add(PolicyUtil.readPolicy(policyFile, policyCodebases));
|
|
|
}
|
|
|
|
|
|
// consult each policy file for those codebases
|
|
|
@@ -260,6 +284,14 @@ public class BootstrapForTesting {
|
|
|
return Collections.unmodifiableMap(map);
|
|
|
}
|
|
|
|
|
|
+ static Map<String, String> parsePropertiesFile(Path propertiesFile) throws Exception {
|
|
|
+ Properties props = new Properties();
|
|
|
+ try (InputStream is = Files.newInputStream(propertiesFile)) {
|
|
|
+ props.load(is);
|
|
|
+ }
|
|
|
+ return props.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString()));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* return parsed classpath, but with symlinks resolved to destination files for matching
|
|
|
* this is for matching the toRealPath() in the code where we have a proper plugin structure
|