zentol commented on a change in pull request #17916: URL: https://github.com/apache/flink/pull/17916#discussion_r810917079
########## File path: flink-core/src/test/java/org/apache/flink/core/fs/SafeFileSystemFactoryTest.java ########## @@ -0,0 +1,183 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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. + */ + +package org.apache.flink.core.fs; + +import org.apache.flink.util.ChildFirstClassLoader; +import org.apache.flink.util.TemporaryClassLoaderContext; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URI; +import java.net.URL; +import java.security.AccessControlContext; +import java.security.AccessController; +import java.security.ProtectionDomain; +import java.util.List; +import java.util.Vector; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** Tests for the {@link SafeFileSystemFactory}. */ +public class SafeFileSystemFactoryTest { + + /** + * We need to load this class in separate classloader to leak the classloader through the {@link + * ProtectionDomain}. + */ + private static class ProtectionDomainLeakTest { + + ProtectionDomainLeakTest(FileSystemFactory factory) throws Exception { + factory.create(URI.create("test://filesystem")); + } + } + + private abstract static class TestFileSystemFactory implements FileSystemFactory { + + @Override + public String getScheme() { + throw new UnsupportedOperationException("Test."); + } + } + + private static Stream<Class<?>> getLoadedClasses(ClassLoader classLoader) throws Exception { + final Field field = ClassLoader.class.getDeclaredField("classes"); + field.setAccessible(true); + @SuppressWarnings("unchecked") + final Vector<Class<?>> classes = (Vector<Class<?>>) field.get(classLoader); + return classes.stream(); + } + + private static List<Class<?>> getLoadedFlinkClasses(ClassLoader classLoader) throws Exception { + return getLoadedClasses(classLoader) + .filter(c -> c.getName().startsWith("org.apache.flink")) + .collect(Collectors.toList()); + } + + private static ProtectionDomain[] getProtectionDomains() { + try { + final AccessControlContext context = AccessController.getContext(); + final Method domainResolver = context.getClass().getDeclaredMethod("getContext"); + domainResolver.setAccessible(true); + return (ProtectionDomain[]) domainResolver.invoke(context); + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + @Test + void testProtectionDomainLeaksWithRegularFileSystemFactory() throws Exception { + testProtectionDomainLeak( + new TestFileSystemFactory() { + + @Override + public FileSystem create(URI fsUri) { + boolean leak = false; + for (ProtectionDomain protectionDomain : getProtectionDomains()) { + if (protectionDomain.getClassLoader() + instanceof ChildFirstClassLoader) { + leak = true; + break; + } + } + Assertions.assertTrue(leak); + return null; + } + }); + } + + @Test + void testProtectionDomainDoesNotLeakWithWrappedFileSystemFactory() throws Exception { + testProtectionDomainLeak( + SafeFileSystemFactory.of( + new TestFileSystemFactory() { + + @Override + public FileSystem create(URI fsUri) { + for (ProtectionDomain protectionDomain : getProtectionDomains()) { + Assertions.assertFalse( + protectionDomain.getClassLoader() + instanceof ChildFirstClassLoader, + "ClassLoader leaked."); + } + return null; + } + })); + } + + private void testProtectionDomainLeak(FileSystemFactory factory) throws Exception { + final URL testClassLocation = + getClass().getProtectionDomain().getCodeSource().getLocation(); + final ChildFirstClassLoader leakyClassLoader = + new ChildFirstClassLoader( + new URL[] {testClassLocation}, + Thread.currentThread().getContextClassLoader(), + new String[] {"java."}, + error -> { + // No-op. + }); + try (TemporaryClassLoaderContext ignored = + TemporaryClassLoaderContext.of(leakyClassLoader)) { + final Class<?> clazz = + Class.forName(ProtectionDomainLeakTest.class.getName(), true, leakyClassLoader); + final Constructor<?> declaredConstructor = + clazz.getDeclaredConstructor(FileSystemFactory.class); + declaredConstructor.setAccessible(true); + declaredConstructor.newInstance(factory); + } + Assertions.assertEquals(1, getLoadedFlinkClasses(leakyClassLoader).size()); + Assertions.assertEquals( + ProtectionDomainLeakTest.class.getName(), + getLoadedFlinkClasses(leakyClassLoader).get(0).getName()); Review comment: PD=ProtectionDomain -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
