Reenable XTestSecurityManager

This commit is contained in:
Simon Willnauer 2015-09-10 10:31:58 +02:00
parent 8027b4a1b4
commit 7fff399834
3 changed files with 12 additions and 15 deletions

View File

@ -279,7 +279,7 @@
<include>rest-api-spec/**/*</include> <include>rest-api-spec/**/*</include>
<include>org/elasticsearch/test/**/*</include> <include>org/elasticsearch/test/**/*</include>
<include>org/elasticsearch/bootstrap/BootstrapForTesting.class</include> <include>org/elasticsearch/bootstrap/BootstrapForTesting.class</include>
<include>org/elasticsearch/bootstrap/XTestSecurityManager.class</include> <include>org/elasticsearch/bootstrap/XTestSecurityManager*.class</include>
<include>org/elasticsearch/common/cli/CliToolTestCase.class</include> <include>org/elasticsearch/common/cli/CliToolTestCase.class</include>
<include>org/elasticsearch/common/cli/CliToolTestCase$*.class</include> <include>org/elasticsearch/common/cli/CliToolTestCase$*.class</include>
<include>org/elasticsearch/cluster/MockInternalClusterInfoService.class</include> <include>org/elasticsearch/cluster/MockInternalClusterInfoService.class</include>

View File

@ -114,7 +114,7 @@ public class BootstrapForTesting {
perms.add(new FilePermission(coverageDir.resolve("jacoco-it.exec").toString(), "read,write")); perms.add(new FilePermission(coverageDir.resolve("jacoco-it.exec").toString(), "read,write"));
} }
Policy.setPolicy(new ESPolicy(perms)); Policy.setPolicy(new ESPolicy(perms));
System.setSecurityManager(new TestSecurityManager()); System.setSecurityManager(new XTestSecurityManager());
Security.selfTest(); Security.selfTest();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("unable to install test security manager", e); throw new RuntimeException("unable to install test security manager", e);

View File

@ -72,26 +72,24 @@ public final class XTestSecurityManager extends SecurityManager {
*/ */
@Override @Override
public void checkExit(final int status) { public void checkExit(final int status) {
AccessController.doPrivileged(new PrivilegedAction<Void>() { AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
@Override
public Void run() {
final String systemClassName = System.class.getName(), final String systemClassName = System.class.getName(),
runtimeClassName = Runtime.class.getName(); runtimeClassName = Runtime.class.getName();
String exitMethodHit = null; String exitMethodHit = null;
for (final StackTraceElement se : Thread.currentThread().getStackTrace()) { for (final StackTraceElement se : Thread.currentThread().getStackTrace()) {
final String className = se.getClassName(), methodName = se.getMethodName(); final String className = se.getClassName(), methodName = se.getMethodName();
if ( if (
("exit".equals(methodName) || "halt".equals(methodName)) && ("exit".equals(methodName) || "halt".equals(methodName)) &&
(systemClassName.equals(className) || runtimeClassName.equals(className)) (systemClassName.equals(className) || runtimeClassName.equals(className))
) { ) {
exitMethodHit = className + '#' + methodName + '(' + status + ')'; exitMethodHit = className + '#' + methodName + '(' + status + ')';
continue; continue;
} }
if (exitMethodHit != null) { if (exitMethodHit != null) {
if (className.startsWith(JUNIT4_TEST_RUNNER_PACKAGE) || if (className.startsWith(JUNIT4_TEST_RUNNER_PACKAGE) ||
className.startsWith(ECLIPSE_TEST_RUNNER_PACKAGE) || className.startsWith(ECLIPSE_TEST_RUNNER_PACKAGE) ||
className.startsWith(IDEA_TEST_RUNNER_PACKAGE)) { className.startsWith(IDEA_TEST_RUNNER_PACKAGE)) {
// this exit point is allowed, we return normally from closure: // this exit point is allowed, we return normally from closure:
return /*void*/ null; return /*void*/ null;
} else { } else {
@ -106,7 +104,6 @@ public final class XTestSecurityManager extends SecurityManager {
exitMethodHit = "JVM exit method"; exitMethodHit = "JVM exit method";
} }
throw new SecurityException(exitMethodHit + " calls are not allowed because they terminate the test runner's JVM."); throw new SecurityException(exitMethodHit + " calls are not allowed because they terminate the test runner's JVM.");
}
}); });
// we passed the stack check, delegate to super, so default policy can still deny permission: // we passed the stack check, delegate to super, so default policy can still deny permission: