+ * Resources (e.g. jar files and directories) listed in {@code codebases} location
+ * will be provided to the policy file via a system property of the short name:
+ * e.g.
- * As an approximation, we just exclude es/test/framework classes,
- * because they will be present in stacks and fail tests for the
- * simple case where an AccessController block is missing, because
- * java security checks every codebase in the stacktrace, and we
- * are sure to pollute it.
- */
-final class MockPluginPolicy extends Policy {
- final ESPolicy standardPolicy;
- final PermissionCollection extraPermissions;
- final Set${codebase.joda-convert-1.2.jar}
would map to full URL.
+ */
+ @SuppressForbidden(reason = "accesses fully qualified URLs to configure security")
+ static Policy readPolicy(URL policyFile, URL codebases[]) {
+ try {
+ try {
+ // set codebase properties
+ for (URL url : codebases) {
+ String shortName = PathUtils.get(url.toURI()).getFileName().toString();
+ System.setProperty("codebase." + shortName, url.toString());
+ }
+ return Policy.getInstance("JavaPolicy", new URIParameter(policyFile.toURI()));
+ } finally {
+ // clear codebase properties
+ for (URL url : codebases) {
+ String shortName = PathUtils.get(url.toURI()).getFileName().toString();
+ System.clearProperty("codebase." + shortName);
+ }
+ }
+ } catch (NoSuchAlgorithmException | URISyntaxException e) {
+ throw new IllegalArgumentException("unable to parse policy file `" + policyFile + "`", e);
+ }
+ }
+
/** returns dynamic Permissions to configured paths */
static Permissions createPermissions(Environment environment) throws IOException {
Permissions policy = new Permissions();
diff --git a/core/src/main/resources/org/elasticsearch/bootstrap/security.policy b/core/src/main/resources/org/elasticsearch/bootstrap/security.policy
index 7e7f347ce1b..244d5be6511 100644
--- a/core/src/main/resources/org/elasticsearch/bootstrap/security.policy
+++ b/core/src/main/resources/org/elasticsearch/bootstrap/security.policy
@@ -31,46 +31,12 @@ grant codeBase "file:${{java.ext.dirs}}/*" {
//// Very special jar permissions:
//// These are dangerous permissions that we don't want to grant to everything.
-grant codeBase "${es.security.jar.lucene.core}" {
+grant codeBase "${codebase.lucene-core-5.4.0-snapshot-1708254.jar}" {
// needed to allow MMapDirectory's "unmap hack"
permission java.lang.RuntimePermission "accessClassInPackage.sun.misc";
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
};
-//// test framework permissions.
-//// These are mock objects and test management that we allow test framework libs
-//// to provide on our behalf. But tests themselves cannot do this stuff!
-
-grant codeBase "${es.security.jar.elasticsearch.securemock}" {
- // needed to access ReflectionFactory (see below)
- permission java.lang.RuntimePermission "accessClassInPackage.sun.reflect";
- // needed to support creation of mocks
- permission java.lang.RuntimePermission "reflectionFactoryAccess";
- // needed for spy interception, etc
- permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
-};
-
-grant codeBase "${es.security.jar.lucene.testframework}" {
- // needed by RamUsageTester
- permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
-};
-
-grant codeBase "${es.security.jar.randomizedtesting.runner}" {
- // optionally needed for access to private test methods (e.g. beforeClass)
- permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
-
- // needed for top threads handling
- permission java.lang.RuntimePermission "modifyThreadGroup";
-};
-
-grant codeBase "${es.security.jar.randomizedtesting.junit4}" {
- // needed for gson serialization
- permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
-
- // needed for stream redirection
- permission java.lang.RuntimePermission "setIO";
-};
-
//// Everything else:
grant {
@@ -126,10 +92,4 @@ grant {
// needed by JDKESLoggerTests
permission java.util.logging.LoggingPermission "control";
-
- // needed to install SSLFactories, advanced SSL configuration, etc.
- permission java.lang.RuntimePermission "setFactory";
-
- // needed to allow installation of bouncycastle crypto provider
- permission java.security.SecurityPermission "putProviderProperty.BC";
};
diff --git a/core/src/main/resources/org/elasticsearch/bootstrap/test-framework.policy b/core/src/main/resources/org/elasticsearch/bootstrap/test-framework.policy
new file mode 100644
index 00000000000..f038c51c596
--- /dev/null
+++ b/core/src/main/resources/org/elasticsearch/bootstrap/test-framework.policy
@@ -0,0 +1,52 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+//// additional test framework permissions.
+//// These are mock objects and test management that we allow test framework libs
+//// to provide on our behalf. But tests themselves cannot do this stuff!
+
+grant codeBase "${codebase.securemock-1.1.jar}" {
+ // needed to access ReflectionFactory (see below)
+ permission java.lang.RuntimePermission "accessClassInPackage.sun.reflect";
+ // needed to support creation of mocks
+ permission java.lang.RuntimePermission "reflectionFactoryAccess";
+ // needed for spy interception, etc
+ permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
+};
+
+grant codeBase "${codebase.lucene-test-framework-5.4.0-snapshot-1708254.jar}" {
+ // needed by RamUsageTester
+ permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
+};
+
+grant codeBase "${codebase.randomizedtesting-runner-2.1.17.jar}" {
+ // optionally needed for access to private test methods (e.g. beforeClass)
+ permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
+
+ // needed for top threads handling
+ permission java.lang.RuntimePermission "modifyThreadGroup";
+};
+
+grant codeBase "${codebase.junit4-ant-2.1.17.jar}" {
+ // needed for gson serialization
+ permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
+
+ // needed for stream redirection
+ permission java.lang.RuntimePermission "setIO";
+};
diff --git a/core/src/test/java/org/elasticsearch/bootstrap/BootstrapForTesting.java b/core/src/test/java/org/elasticsearch/bootstrap/BootstrapForTesting.java
index 3d19c5fb296..2c195a9a014 100644
--- a/core/src/test/java/org/elasticsearch/bootstrap/BootstrapForTesting.java
+++ b/core/src/test/java/org/elasticsearch/bootstrap/BootstrapForTesting.java
@@ -19,28 +19,37 @@
package org.elasticsearch.bootstrap;
+import com.carrotsearch.randomizedtesting.RandomizedRunner;
+
+import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestSecurityManager;
import org.elasticsearch.bootstrap.Bootstrap;
import org.elasticsearch.bootstrap.ESPolicy;
import org.elasticsearch.bootstrap.Security;
import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.plugins.PluginInfo;
+import org.junit.Assert;
import java.io.FilePermission;
import java.io.InputStream;
-import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.security.Permission;
-import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
-import java.security.URIParameter;
+import java.security.ProtectionDomain;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
+import java.util.Map;
import java.util.Objects;
import java.util.Properties;
+import java.util.Set;
import static com.carrotsearch.randomizedtesting.RandomizedTest.systemPropertyAsBoolean;
@@ -83,7 +92,6 @@ public class BootstrapForTesting {
// install security manager if requested
if (systemPropertyAsBoolean("tests.security.manager", true)) {
try {
- Security.setCodebaseProperties();
// initialize paths the same exact way as bootstrap
Permissions perms = new Permissions();
// add permissions to everything in classpath
@@ -120,31 +128,17 @@ public class BootstrapForTesting {
if (System.getProperty("tests.maven") == null) {
perms.add(new RuntimePermission("setIO"));
}
-
- final Policy policy;
- // if its a plugin with special permissions, we use a wrapper policy impl to try
- // to simulate what happens with a real distribution
- List