hwanju commented on a change in pull request #14499: URL: https://github.com/apache/flink/pull/14499#discussion_r555767838
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/security/FlinkSecurityManagerTest.java ########## @@ -0,0 +1,415 @@ +/* + * 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.runtime.security; + +import org.apache.flink.configuration.ClusterOptions; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.IllegalConfigurationException; +import org.apache.flink.core.testutils.CheckedThread; +import org.apache.flink.runtime.UserSystemExitException; +import org.apache.flink.util.TestLogger; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.security.Permission; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.apache.flink.core.testutils.CommonTestUtils.assertThrows; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** Tests for {@code FlinkUserSecurityManager}. */ +public class FlinkSecurityManagerTest extends TestLogger { + + private static final int TEST_EXIT_CODE = 123; + SecurityManager originalSecurityManager; + FlinkSecurityManager flinkSecurityManager; + + @Before + public void setUp() { + originalSecurityManager = System.getSecurityManager(); + } + + @After + public void tearDown() { + System.setSecurityManager(originalSecurityManager); + } + + @Test(expected = UserSystemExitException.class) + public void testThrowUserExit() { + flinkSecurityManager = + new FlinkSecurityManager(ClusterOptions.UserSystemExitMode.THROW, null); + flinkSecurityManager.monitorUserSystemExit(); + flinkSecurityManager.checkExit(TEST_EXIT_CODE); + } + + @Test + public void testToggleUserExit() { + flinkSecurityManager = + new FlinkSecurityManager(ClusterOptions.UserSystemExitMode.THROW, null); + flinkSecurityManager.checkExit(TEST_EXIT_CODE); + flinkSecurityManager.monitorUserSystemExit(); + try { + flinkSecurityManager.checkExit(TEST_EXIT_CODE); + fail(); + } catch (UserSystemExitException ignored) { + } + flinkSecurityManager.unmonitorUserSystemExit(); + flinkSecurityManager.checkExit(TEST_EXIT_CODE); + } + + @Test + public void testPerThreadThrowUserExit() throws Exception { + flinkSecurityManager = + new FlinkSecurityManager(ClusterOptions.UserSystemExitMode.THROW, null); + ExecutorService executorService = Executors.newSingleThreadExecutor(); + // Async thread test before enabling monitoring ensures it does not throw while prestarting + // worker thread, which is to be unmonitored and tested after enabling monitoring enabled. + CompletableFuture<Void> future = + CompletableFuture.runAsync( + () -> flinkSecurityManager.checkExit(TEST_EXIT_CODE), executorService); + future.get(); + flinkSecurityManager.monitorUserSystemExit(); + try { + flinkSecurityManager.checkExit(TEST_EXIT_CODE); + fail(); + } catch (UserSystemExitException ignored) { + } + // This threaded exit should be allowed as thread is not spawned while monitor is enabled. + future = + CompletableFuture.runAsync( + () -> flinkSecurityManager.checkExit(TEST_EXIT_CODE), executorService); + future.get(); + } + + @Test + public void testInheritedThrowUserExit() throws Exception { + flinkSecurityManager = + new FlinkSecurityManager(ClusterOptions.UserSystemExitMode.THROW, null); + flinkSecurityManager.monitorUserSystemExit(); + try { + flinkSecurityManager.checkExit(TEST_EXIT_CODE); + fail(); + } catch (UserSystemExitException ignored) { + } + CheckedThread thread = + new CheckedThread() { + @Override + public void go() { + try { + flinkSecurityManager.checkExit(TEST_EXIT_CODE); + fail(); + } catch (UserSystemExitException ignored) { + } catch (Throwable t) { + fail(); + } + } + }; + thread.start(); + thread.sync(); + } + + @Test + public void testLogUserExit() { Review comment: You can see some difference. `test*Configuration` is a little more end-to-end via configuration setting (testing `fromConfiguration`), while others are not using `Configuration` rather directly using constructor. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org