Browse Source

Remove SecureMockMaker (#127773)

The SecureMockMaker is a way for mockito to be allowed SM permissions
for proxying. Since the Security Manager is no longer used, secure mocks
are no longer needed. This commit removes them.
Ryan Ernst 5 months ago
parent
commit
1a1763c591

+ 0 - 4
test/framework/src/main/java/org/elasticsearch/bootstrap/BootstrapForTesting.java

@@ -16,7 +16,6 @@ import org.elasticsearch.common.settings.Settings;
 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();
     }

+ 0 - 28
test/framework/src/main/java/org/elasticsearch/test/mockito/SecureAnnotationEngine.java

@@ -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));
-    }
-}

+ 0 - 98
test/framework/src/main/java/org/elasticsearch/test/mockito/SecureMockMaker.java

@@ -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> T createMock(MockCreationSettings<T> mockCreationSettings, MockHandler mockHandler) {
-        return wrap(() -> delegate.createMock(mockCreationSettings, mockHandler));
-    }
-
-    @SuppressWarnings("rawtypes")
-    @Override
-    public <T> Optional<T> createSpy(MockCreationSettings<T> 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 <T> StaticMockControl<T> createStaticMock(Class<T> type, MockCreationSettings<T> settings, MockHandler handler) {
-        return delegate.createStaticMock(type, settings, handler);
-    }
-
-    @Override
-    public <T> ConstructionMockControl<T> createConstructionMock(
-        Class<T> type,
-        Function<MockedConstruction.Context, MockCreationSettings<T>> settingsFactory,
-        Function<MockedConstruction.Context, MockHandler<T>> handlerFactory,
-        MockedConstruction.MockInitializer<T> mockInitializer
-    ) {
-        return delegate.createConstructionMock(type, settingsFactory, handlerFactory, mockInitializer);
-    }
-
-    @Override
-    public void clearAllCaches() {
-        delegate.clearAllCaches();
-    }
-}

+ 0 - 45
test/framework/src/main/java/org/elasticsearch/test/mockito/SecureMockUtil.java

@@ -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<AccessControlContext>) AccessController::getContext, acc);
-    }
-
-    // forces static init to run
-    public static void init() {}
-
-    // wrap the given call to play nice with SecurityManager
-    static <T> T wrap(Supplier<T> call) {
-        return AccessController.doPrivileged((PrivilegedAction<T>) call::get, context);
-    }
-
-    // no construction
-    private SecureMockUtil() {}
-}

+ 0 - 28
test/framework/src/main/java/org/elasticsearch/test/mockito/SecureObjectInstantiator.java

@@ -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> T newInstance(Class<T> cls) {
-        return SecureMockUtil.wrap(() -> delegate.newInstance(cls));
-    }
-}

+ 0 - 32
test/framework/src/main/java/org/elasticsearch/test/mockito/SecureObjectInstantiatorProvider.java

@@ -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));
-    }
-}

+ 0 - 1
test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.AnnotationEngine

@@ -1 +0,0 @@
-org.elasticsearch.test.mockito.SecureAnnotationEngine

+ 0 - 1
test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.InstantiatorProvider2

@@ -1 +0,0 @@
-org.elasticsearch.test.mockito.SecureObjectInstantiatorProvider

+ 0 - 1
test/framework/src/main/resources/mockito-extensions/org.mockito.plugins.MockMaker

@@ -1 +0,0 @@
-org.elasticsearch.test.mockito.SecureMockMaker