diff --git a/test/framework/src/main/java/org/elasticsearch/bootstrap/BootstrapForTesting.java b/test/framework/src/main/java/org/elasticsearch/bootstrap/BootstrapForTesting.java index 4776eaf1dc1bb..318f2ce863173 100644 --- a/test/framework/src/main/java/org/elasticsearch/bootstrap/BootstrapForTesting.java +++ b/test/framework/src/main/java/org/elasticsearch/bootstrap/BootstrapForTesting.java @@ -16,7 +16,6 @@ import org.elasticsearch.core.Booleans; import org.elasticsearch.core.PathUtils; import org.elasticsearch.jdk.JarHell; -import org.elasticsearch.test.mockito.SecureMockMaker; import java.io.IOException; import java.nio.file.Files; @@ -70,9 +69,6 @@ public class BootstrapForTesting { throw new RuntimeException("found jar hell in test classpath", e); } - // init mockito - SecureMockMaker.init(); - // Log ifconfig output before SecurityManager is installed IfConfig.logIfNecessary(); } diff --git a/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureAnnotationEngine.java b/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureAnnotationEngine.java deleted file mode 100644 index bc226b78ccdf2..0000000000000 --- a/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureAnnotationEngine.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -package org.elasticsearch.test.mockito; - -import org.mockito.internal.configuration.InjectingAnnotationEngine; -import org.mockito.plugins.AnnotationEngine; - -import static org.elasticsearch.test.mockito.SecureMockUtil.wrap; - -public class SecureAnnotationEngine implements AnnotationEngine { - private final AnnotationEngine delegate; - - public SecureAnnotationEngine() { - delegate = wrap(InjectingAnnotationEngine::new); - } - - @Override - public AutoCloseable process(Class clazz, Object testInstance) { - return wrap(() -> delegate.process(clazz, testInstance)); - } -} diff --git a/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureMockMaker.java b/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureMockMaker.java deleted file mode 100644 index a56a09a4ef08c..0000000000000 --- a/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureMockMaker.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -package org.elasticsearch.test.mockito; - -import org.mockito.MockedConstruction; -import org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker; -import org.mockito.internal.util.reflection.LenientCopyTool; -import org.mockito.invocation.MockHandler; -import org.mockito.mock.MockCreationSettings; -import org.mockito.plugins.MockMaker; - -import java.util.Optional; -import java.util.function.Function; - -import static org.elasticsearch.test.mockito.SecureMockUtil.wrap; - -/** - * A {@link MockMaker} that works with {@link SecurityManager}. - */ -public class SecureMockMaker implements MockMaker { - - // delegates to initializing util, which we don't want to have public - public static void init() { - SecureMockUtil.init(); - } - - // TODO: consider using InlineByteBuddyMockMaker, but this requires using a java agent for instrumentation - private final SubclassByteBuddyMockMaker delegate; - - public SecureMockMaker() { - delegate = wrap(SubclassByteBuddyMockMaker::new); - } - - @SuppressWarnings("rawtypes") - @Override - public T createMock(MockCreationSettings mockCreationSettings, MockHandler mockHandler) { - return wrap(() -> delegate.createMock(mockCreationSettings, mockHandler)); - } - - @SuppressWarnings("rawtypes") - @Override - public Optional createSpy(MockCreationSettings settings, MockHandler handler, T object) { - // spies are not implemented by the bytebuddy delegate implementation - return wrap(() -> { - T instance = delegate.createMock(settings, handler); - new LenientCopyTool().copyToMock(object, instance); - return Optional.of(instance); - }); - } - - @SuppressWarnings("rawtypes") - @Override - public MockHandler getHandler(Object o) { - return delegate.getHandler(o); - } - - @SuppressWarnings("rawtypes") - @Override - public void resetMock(Object o, MockHandler mockHandler, MockCreationSettings mockCreationSettings) { - wrap(() -> { - delegate.resetMock(o, mockHandler, mockCreationSettings); - return (Void) null; - }); - } - - @Override - public TypeMockability isTypeMockable(Class type) { - return delegate.isTypeMockable(type); - } - - @SuppressWarnings("rawtypes") - @Override - public StaticMockControl createStaticMock(Class type, MockCreationSettings settings, MockHandler handler) { - return delegate.createStaticMock(type, settings, handler); - } - - @Override - public ConstructionMockControl createConstructionMock( - Class type, - Function> settingsFactory, - Function> handlerFactory, - MockedConstruction.MockInitializer mockInitializer - ) { - return delegate.createConstructionMock(type, settingsFactory, handlerFactory, mockInitializer); - } - - @Override - public void clearAllCaches() { - delegate.clearAllCaches(); - } -} diff --git a/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureMockUtil.java b/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureMockUtil.java deleted file mode 100644 index dd97c2b30dc3b..0000000000000 --- a/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureMockUtil.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -package org.elasticsearch.test.mockito; - -import org.mockito.plugins.MockMaker; - -import java.security.AccessControlContext; -import java.security.AccessController; -import java.security.DomainCombiner; -import java.security.PrivilegedAction; -import java.security.ProtectionDomain; -import java.util.function.Supplier; - -class SecureMockUtil { - - // we use the protection domain of mockito for wrapped calls so that - // Elasticsearch server jar does not need additional permissions - private static final AccessControlContext context = getContext(); - - private static AccessControlContext getContext() { - ProtectionDomain[] pda = new ProtectionDomain[] { wrap(MockMaker.class::getProtectionDomain) }; - DomainCombiner combiner = (current, assigned) -> pda; - AccessControlContext acc = new AccessControlContext(AccessController.getContext(), combiner); - // getContext must be called with the new acc so that a combined context will be created - return AccessController.doPrivileged((PrivilegedAction) AccessController::getContext, acc); - } - - // forces static init to run - public static void init() {} - - // wrap the given call to play nice with SecurityManager - static T wrap(Supplier call) { - return AccessController.doPrivileged((PrivilegedAction) call::get, context); - } - - // no construction - private SecureMockUtil() {} -} diff --git a/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureObjectInstantiator.java b/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureObjectInstantiator.java deleted file mode 100644 index 4c3ef53ccce47..0000000000000 --- a/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureObjectInstantiator.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -package org.elasticsearch.test.mockito; - -import org.mockito.creation.instance.Instantiator; - -/** - * A wrapper for instantiating objects reflectively, but plays nice with SecurityManager. - */ -class SecureObjectInstantiator implements Instantiator { - private final Instantiator delegate; - - SecureObjectInstantiator(Instantiator delegate) { - this.delegate = delegate; - } - - @Override - public T newInstance(Class cls) { - return SecureMockUtil.wrap(() -> delegate.newInstance(cls)); - } -} diff --git a/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureObjectInstantiatorProvider.java b/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureObjectInstantiatorProvider.java deleted file mode 100644 index fb2798cdaf392..0000000000000 --- a/test/framework/src/main/java/org/elasticsearch/test/mockito/SecureObjectInstantiatorProvider.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -package org.elasticsearch.test.mockito; - -import org.mockito.creation.instance.Instantiator; -import org.mockito.internal.creation.instance.DefaultInstantiatorProvider; -import org.mockito.mock.MockCreationSettings; -import org.mockito.plugins.InstantiatorProvider2; - -/** - * A wrapper around the default provider which itself just wraps - * {@link Instantiator} instances to play nice with {@link SecurityManager}. - */ -public class SecureObjectInstantiatorProvider implements InstantiatorProvider2 { - private final DefaultInstantiatorProvider delegate; - - public SecureObjectInstantiatorProvider() { - delegate = new DefaultInstantiatorProvider(); - } - - @Override - public Instantiator getInstantiator(MockCreationSettings settings) { - return new SecureObjectInstantiator(delegate.getInstantiator(settings)); - } -} diff --git a/test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.AnnotationEngine b/test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.AnnotationEngine deleted file mode 100644 index be695fd30f509..0000000000000 --- a/test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.AnnotationEngine +++ /dev/null @@ -1 +0,0 @@ -org.elasticsearch.test.mockito.SecureAnnotationEngine diff --git a/test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.InstantiatorProvider2 b/test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.InstantiatorProvider2 deleted file mode 100644 index ba41d233143f9..0000000000000 --- a/test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.InstantiatorProvider2 +++ /dev/null @@ -1 +0,0 @@ -org.elasticsearch.test.mockito.SecureObjectInstantiatorProvider diff --git a/test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.MockMaker b/test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.MockMaker deleted file mode 100644 index e19b9d550f81b..0000000000000 --- a/test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.MockMaker +++ /dev/null @@ -1 +0,0 @@ -org.elasticsearch.test.mockito.SecureMockMaker