You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/12/30 13:35:52 UTC

[GitHub] [flink] XComp commented on a change in pull request #14499: [FLINK-15156] Warn user if System.exit() is called in user code

XComp commented on a change in pull request #14499:
URL: https://github.com/apache/flink/pull/14499#discussion_r550073467



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for
+ * example, it can warn or prevent user code from terminating JVM by System.exit or halt by logging or throwing an exception.
+ * This does not necessarily prevent malicious users who try to tweak security manager on their own, but more for being dependable
+ * against user mistakes by gracefully handling them informing users rather than causing silent unavailability.
+ */
+public class FlinkUserSecurityManager extends SecurityManager {
+
+	/**
+	 * The mode of how to handle user code attempting to exit JVM.
+	 */
+	public enum CheckExitMode {
+		/** No check is enabled, that is allowing exit without any action. */
+		DISABLED,
+		/** Warn by logging but still allowing exit to be performed. */
+		WARN,
+		/** Throw exception when exit is attempted disallowing JVM termination. */
+		THROW,
+	}
+
+	static final Logger LOG = LoggerFactory.getLogger(FlinkUserSecurityManager.class);
+
+	private final SecurityManager originalSecurityManager = System.getSecurityManager();
+	private ThreadLocal<Boolean> monitorSystemExit = new InheritableThreadLocal<>();
+	private CheckExitMode checkExitMode;
+
+	public FlinkUserSecurityManager(CheckExitMode checkExitMode) {
+		super();
+		this.checkExitMode = checkExitMode;
+
+		LOG.info("FlinkUserSecurityManager is created with {} system exit check (previous security manager is {})",
+			this.checkExitMode, originalSecurityManager != null ? originalSecurityManager : "not existing");
+	}
+
+	/**
+	 * Instantiate FlinkUserSecurityManager from configuration. Return null if no security manager
+	 * check is needed, so that a caller can skip setting security manager avoiding runtime check cost,
+	 * if there is no security check set up already. Use {@link #setFromConfiguration} helper, which
+	 * handles disabled case.
+	 *
+	 * @param configuration to instantiate the security manager from
+	 *
+	 * @return FlinkUserSecurityManager instantiated baesd on configuration. Return null if disabled.
+	 */
+	public static FlinkUserSecurityManager fromConfiguration(Configuration configuration) {
+		final String checkExitModeConfig = configuration.getString(SecurityOptions.CHECK_SYSTEM_EXIT);
+		final CheckExitMode checkExitMode;
+
+		try {
+			checkExitMode = CheckExitMode.valueOf(checkExitModeConfig.toUpperCase());

Review comment:
       I'd suggest moving the parameter parsing into the `SecurityOptions` instead of dealing with it here. That could be achieved by making `SecurityOptions.CHECK_SYSTEM_EXIT` an enum-typed `ConfigOption`.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for
+ * example, it can warn or prevent user code from terminating JVM by System.exit or halt by logging or throwing an exception.
+ * This does not necessarily prevent malicious users who try to tweak security manager on their own, but more for being dependable
+ * against user mistakes by gracefully handling them informing users rather than causing silent unavailability.
+ */
+public class FlinkUserSecurityManager extends SecurityManager {
+
+	/**
+	 * The mode of how to handle user code attempting to exit JVM.
+	 */
+	public enum CheckExitMode {
+		/** No check is enabled, that is allowing exit without any action. */
+		DISABLED,
+		/** Warn by logging but still allowing exit to be performed. */
+		WARN,
+		/** Throw exception when exit is attempted disallowing JVM termination. */
+		THROW,
+	}
+
+	static final Logger LOG = LoggerFactory.getLogger(FlinkUserSecurityManager.class);
+
+	private final SecurityManager originalSecurityManager = System.getSecurityManager();
+	private ThreadLocal<Boolean> monitorSystemExit = new InheritableThreadLocal<>();
+	private CheckExitMode checkExitMode;
+
+	public FlinkUserSecurityManager(CheckExitMode checkExitMode) {
+		super();
+		this.checkExitMode = checkExitMode;
+
+		LOG.info("FlinkUserSecurityManager is created with {} system exit check (previous security manager is {})",
+			this.checkExitMode, originalSecurityManager != null ? originalSecurityManager : "not existing");
+	}
+
+	/**
+	 * Instantiate FlinkUserSecurityManager from configuration. Return null if no security manager
+	 * check is needed, so that a caller can skip setting security manager avoiding runtime check cost,
+	 * if there is no security check set up already. Use {@link #setFromConfiguration} helper, which
+	 * handles disabled case.
+	 *
+	 * @param configuration to instantiate the security manager from
+	 *
+	 * @return FlinkUserSecurityManager instantiated baesd on configuration. Return null if disabled.

Review comment:
       ```suggestion
   	 * @return FlinkUserSecurityManager instantiated based on configuration. Return null if disabled.
   ```

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for
+ * example, it can warn or prevent user code from terminating JVM by System.exit or halt by logging or throwing an exception.
+ * This does not necessarily prevent malicious users who try to tweak security manager on their own, but more for being dependable
+ * against user mistakes by gracefully handling them informing users rather than causing silent unavailability.
+ */
+public class FlinkUserSecurityManager extends SecurityManager {
+
+	/**
+	 * The mode of how to handle user code attempting to exit JVM.
+	 */
+	public enum CheckExitMode {
+		/** No check is enabled, that is allowing exit without any action. */
+		DISABLED,
+		/** Warn by logging but still allowing exit to be performed. */
+		WARN,

Review comment:
       I would suggest renaming `WARN` as it might be too generic. Something like `LOG` or `LOG_WARNING` would be more explicit in what channel is used for the warning.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for
+ * example, it can warn or prevent user code from terminating JVM by System.exit or halt by logging or throwing an exception.
+ * This does not necessarily prevent malicious users who try to tweak security manager on their own, but more for being dependable
+ * against user mistakes by gracefully handling them informing users rather than causing silent unavailability.
+ */
+public class FlinkUserSecurityManager extends SecurityManager {
+
+	/**
+	 * The mode of how to handle user code attempting to exit JVM.
+	 */
+	public enum CheckExitMode {
+		/** No check is enabled, that is allowing exit without any action. */
+		DISABLED,
+		/** Warn by logging but still allowing exit to be performed. */
+		WARN,
+		/** Throw exception when exit is attempted disallowing JVM termination. */
+		THROW,
+	}
+
+	static final Logger LOG = LoggerFactory.getLogger(FlinkUserSecurityManager.class);
+
+	private final SecurityManager originalSecurityManager = System.getSecurityManager();
+	private ThreadLocal<Boolean> monitorSystemExit = new InheritableThreadLocal<>();
+	private CheckExitMode checkExitMode;

Review comment:
       ```suggestion
   	private final ThreadLocal<Boolean> monitorSystemExit = new InheritableThreadLocal<>();
   	private final CheckExitMode checkExitMode;
   ```
   Both fields `monitorSystemExit` and `checkExitMode` can be marked as `final` as they are not changed anymore.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for

Review comment:
       ```suggestion
    * {@code FlinkUserSecurityManager} to control unexpected user behaviors that potentially impact cluster availability, for
   ```
   Minor detail: I suggest using the class name as is since we're referring to it here.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for
+ * example, it can warn or prevent user code from terminating JVM by System.exit or halt by logging or throwing an exception.
+ * This does not necessarily prevent malicious users who try to tweak security manager on their own, but more for being dependable
+ * against user mistakes by gracefully handling them informing users rather than causing silent unavailability.
+ */
+public class FlinkUserSecurityManager extends SecurityManager {
+
+	/**
+	 * The mode of how to handle user code attempting to exit JVM.
+	 */
+	public enum CheckExitMode {
+		/** No check is enabled, that is allowing exit without any action. */
+		DISABLED,
+		/** Warn by logging but still allowing exit to be performed. */
+		WARN,
+		/** Throw exception when exit is attempted disallowing JVM termination. */
+		THROW,
+	}
+
+	static final Logger LOG = LoggerFactory.getLogger(FlinkUserSecurityManager.class);
+
+	private final SecurityManager originalSecurityManager = System.getSecurityManager();
+	private ThreadLocal<Boolean> monitorSystemExit = new InheritableThreadLocal<>();
+	private CheckExitMode checkExitMode;
+
+	public FlinkUserSecurityManager(CheckExitMode checkExitMode) {
+		super();
+		this.checkExitMode = checkExitMode;
+
+		LOG.info("FlinkUserSecurityManager is created with {} system exit check (previous security manager is {})",
+			this.checkExitMode, originalSecurityManager != null ? originalSecurityManager : "not existing");
+	}
+
+	/**
+	 * Instantiate FlinkUserSecurityManager from configuration. Return null if no security manager
+	 * check is needed, so that a caller can skip setting security manager avoiding runtime check cost,
+	 * if there is no security check set up already. Use {@link #setFromConfiguration} helper, which
+	 * handles disabled case.
+	 *
+	 * @param configuration to instantiate the security manager from
+	 *
+	 * @return FlinkUserSecurityManager instantiated baesd on configuration. Return null if disabled.
+	 */
+	public static FlinkUserSecurityManager fromConfiguration(Configuration configuration) {
+		final String checkExitModeConfig = configuration.getString(SecurityOptions.CHECK_SYSTEM_EXIT);
+		final CheckExitMode checkExitMode;
+
+		try {
+			checkExitMode = CheckExitMode.valueOf(checkExitModeConfig.toUpperCase());
+		} catch (Exception ex) {
+			throw new IllegalConfigurationException(
+				String.format("%s is invalid configuration for %s.", checkExitModeConfig, SecurityOptions.CHECK_SYSTEM_EXIT.key()),
+				ex);
+		}
+
+		// If no check is enabled, return null so that caller can avoid setting security manager not to incur any runtime cost.
+		if (checkExitMode == CheckExitMode.DISABLED) {
+			return null;
+		}
+		// Add more configuration parameters that need user security manager (currently only for system exit).
+		return new FlinkUserSecurityManager(checkExitMode);
+	}
+
+	public static void setFromConfiguration(Configuration configuration) {
+		final FlinkUserSecurityManager flinkUserSecurityManager =
+			FlinkUserSecurityManager.fromConfiguration(configuration);
+		if (flinkUserSecurityManager != null) {
+			System.setSecurityManager(flinkUserSecurityManager);
+		}
+	}
+
+	public void checkPermission(Permission perm) {
+		if (originalSecurityManager != null) {
+			originalSecurityManager.checkPermission(perm);
+		}
+	}
+
+	public void checkPermission(Permission perm, Object context) {

Review comment:
       ```suggestion
     @Override
   	public void checkPermission(Permission perm, Object context) {
   ```

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for
+ * example, it can warn or prevent user code from terminating JVM by System.exit or halt by logging or throwing an exception.
+ * This does not necessarily prevent malicious users who try to tweak security manager on their own, but more for being dependable
+ * against user mistakes by gracefully handling them informing users rather than causing silent unavailability.
+ */
+public class FlinkUserSecurityManager extends SecurityManager {
+
+	/**
+	 * The mode of how to handle user code attempting to exit JVM.
+	 */
+	public enum CheckExitMode {
+		/** No check is enabled, that is allowing exit without any action. */
+		DISABLED,
+		/** Warn by logging but still allowing exit to be performed. */
+		WARN,
+		/** Throw exception when exit is attempted disallowing JVM termination. */
+		THROW,
+	}
+
+	static final Logger LOG = LoggerFactory.getLogger(FlinkUserSecurityManager.class);
+
+	private final SecurityManager originalSecurityManager = System.getSecurityManager();
+	private ThreadLocal<Boolean> monitorSystemExit = new InheritableThreadLocal<>();
+	private CheckExitMode checkExitMode;
+
+	public FlinkUserSecurityManager(CheckExitMode checkExitMode) {
+		super();
+		this.checkExitMode = checkExitMode;
+
+		LOG.info("FlinkUserSecurityManager is created with {} system exit check (previous security manager is {})",
+			this.checkExitMode, originalSecurityManager != null ? originalSecurityManager : "not existing");
+	}
+
+	/**
+	 * Instantiate FlinkUserSecurityManager from configuration. Return null if no security manager
+	 * check is needed, so that a caller can skip setting security manager avoiding runtime check cost,
+	 * if there is no security check set up already. Use {@link #setFromConfiguration} helper, which
+	 * handles disabled case.
+	 *
+	 * @param configuration to instantiate the security manager from
+	 *
+	 * @return FlinkUserSecurityManager instantiated baesd on configuration. Return null if disabled.
+	 */
+	public static FlinkUserSecurityManager fromConfiguration(Configuration configuration) {
+		final String checkExitModeConfig = configuration.getString(SecurityOptions.CHECK_SYSTEM_EXIT);
+		final CheckExitMode checkExitMode;
+
+		try {
+			checkExitMode = CheckExitMode.valueOf(checkExitModeConfig.toUpperCase());
+		} catch (Exception ex) {
+			throw new IllegalConfigurationException(
+				String.format("%s is invalid configuration for %s.", checkExitModeConfig, SecurityOptions.CHECK_SYSTEM_EXIT.key()),
+				ex);
+		}
+
+		// If no check is enabled, return null so that caller can avoid setting security manager not to incur any runtime cost.
+		if (checkExitMode == CheckExitMode.DISABLED) {
+			return null;
+		}
+		// Add more configuration parameters that need user security manager (currently only for system exit).
+		return new FlinkUserSecurityManager(checkExitMode);
+	}
+
+	public static void setFromConfiguration(Configuration configuration) {
+		final FlinkUserSecurityManager flinkUserSecurityManager =
+			FlinkUserSecurityManager.fromConfiguration(configuration);
+		if (flinkUserSecurityManager != null) {
+			System.setSecurityManager(flinkUserSecurityManager);
+		}
+	}
+
+	public void checkPermission(Permission perm) {

Review comment:
       ```suggestion
     @Override
   	public void checkPermission(Permission perm) {
   ```

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for
+ * example, it can warn or prevent user code from terminating JVM by System.exit or halt by logging or throwing an exception.
+ * This does not necessarily prevent malicious users who try to tweak security manager on their own, but more for being dependable
+ * against user mistakes by gracefully handling them informing users rather than causing silent unavailability.
+ */
+public class FlinkUserSecurityManager extends SecurityManager {
+
+	/**
+	 * The mode of how to handle user code attempting to exit JVM.
+	 */
+	public enum CheckExitMode {

Review comment:
       What about adding the description as a field to the enum. This way, you could even use it in the `SecurityOptions`' `ConfigOption` definition and we avoid having the same information available twice.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for
+ * example, it can warn or prevent user code from terminating JVM by System.exit or halt by logging or throwing an exception.
+ * This does not necessarily prevent malicious users who try to tweak security manager on their own, but more for being dependable
+ * against user mistakes by gracefully handling them informing users rather than causing silent unavailability.
+ */
+public class FlinkUserSecurityManager extends SecurityManager {
+
+	/**
+	 * The mode of how to handle user code attempting to exit JVM.
+	 */
+	public enum CheckExitMode {
+		/** No check is enabled, that is allowing exit without any action. */
+		DISABLED,
+		/** Warn by logging but still allowing exit to be performed. */
+		WARN,
+		/** Throw exception when exit is attempted disallowing JVM termination. */
+		THROW,
+	}
+
+	static final Logger LOG = LoggerFactory.getLogger(FlinkUserSecurityManager.class);
+
+	private final SecurityManager originalSecurityManager = System.getSecurityManager();
+	private ThreadLocal<Boolean> monitorSystemExit = new InheritableThreadLocal<>();
+	private CheckExitMode checkExitMode;
+
+	public FlinkUserSecurityManager(CheckExitMode checkExitMode) {
+		super();
+		this.checkExitMode = checkExitMode;
+
+		LOG.info("FlinkUserSecurityManager is created with {} system exit check (previous security manager is {})",
+			this.checkExitMode, originalSecurityManager != null ? originalSecurityManager : "not existing");
+	}
+
+	/**
+	 * Instantiate FlinkUserSecurityManager from configuration. Return null if no security manager
+	 * check is needed, so that a caller can skip setting security manager avoiding runtime check cost,
+	 * if there is no security check set up already. Use {@link #setFromConfiguration} helper, which
+	 * handles disabled case.
+	 *
+	 * @param configuration to instantiate the security manager from
+	 *
+	 * @return FlinkUserSecurityManager instantiated baesd on configuration. Return null if disabled.
+	 */
+	public static FlinkUserSecurityManager fromConfiguration(Configuration configuration) {
+		final String checkExitModeConfig = configuration.getString(SecurityOptions.CHECK_SYSTEM_EXIT);
+		final CheckExitMode checkExitMode;
+
+		try {
+			checkExitMode = CheckExitMode.valueOf(checkExitModeConfig.toUpperCase());
+		} catch (Exception ex) {
+			throw new IllegalConfigurationException(
+				String.format("%s is invalid configuration for %s.", checkExitModeConfig, SecurityOptions.CHECK_SYSTEM_EXIT.key()),
+				ex);
+		}
+
+		// If no check is enabled, return null so that caller can avoid setting security manager not to incur any runtime cost.
+		if (checkExitMode == CheckExitMode.DISABLED) {
+			return null;
+		}
+		// Add more configuration parameters that need user security manager (currently only for system exit).
+		return new FlinkUserSecurityManager(checkExitMode);
+	}
+
+	public static void setFromConfiguration(Configuration configuration) {
+		final FlinkUserSecurityManager flinkUserSecurityManager =
+			FlinkUserSecurityManager.fromConfiguration(configuration);
+		if (flinkUserSecurityManager != null) {
+			System.setSecurityManager(flinkUserSecurityManager);
+		}
+	}
+
+	public void checkPermission(Permission perm) {
+		if (originalSecurityManager != null) {
+			originalSecurityManager.checkPermission(perm);
+		}
+	}
+
+	public void checkPermission(Permission perm, Object context) {
+		if (originalSecurityManager != null) {
+			originalSecurityManager.checkPermission(perm, context);
+		}
+	}
+
+	public void checkExit(int status) {

Review comment:
       We're not calling `checkExit(int)` on the `originalSecurityManager` here. There's another `SecurityManager` implemented in the context of [FLINK-16510](https://issues.apache.org/jira/browse/FLINK-16510) called `ExitTrappingSecurityManager`. Its `checkExit` is not called if both `SecurityManager`s are configured, if I'm not mistaken.

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -705,9 +715,11 @@ public final void cancel() throws Exception {
 
         // the "cancel task" call must come first, but the cancelables must be
         // closed no matter what
+        FlinkUserSecurityManager.monitorSystemExitForCurrentThread();
         try {
             cancelTask();

Review comment:
       Just for documentation purposes: `cancelTask` indirectly calls `SourceFunction.cancel()`

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for
+ * example, it can warn or prevent user code from terminating JVM by System.exit or halt by logging or throwing an exception.
+ * This does not necessarily prevent malicious users who try to tweak security manager on their own, but more for being dependable
+ * against user mistakes by gracefully handling them informing users rather than causing silent unavailability.
+ */
+public class FlinkUserSecurityManager extends SecurityManager {
+
+	/**
+	 * The mode of how to handle user code attempting to exit JVM.
+	 */
+	public enum CheckExitMode {
+		/** No check is enabled, that is allowing exit without any action. */
+		DISABLED,
+		/** Warn by logging but still allowing exit to be performed. */
+		WARN,
+		/** Throw exception when exit is attempted disallowing JVM termination. */
+		THROW,
+	}
+
+	static final Logger LOG = LoggerFactory.getLogger(FlinkUserSecurityManager.class);
+
+	private final SecurityManager originalSecurityManager = System.getSecurityManager();
+	private ThreadLocal<Boolean> monitorSystemExit = new InheritableThreadLocal<>();
+	private CheckExitMode checkExitMode;
+
+	public FlinkUserSecurityManager(CheckExitMode checkExitMode) {
+		super();
+		this.checkExitMode = checkExitMode;
+
+		LOG.info("FlinkUserSecurityManager is created with {} system exit check (previous security manager is {})",
+			this.checkExitMode, originalSecurityManager != null ? originalSecurityManager : "not existing");
+	}
+
+	/**
+	 * Instantiate FlinkUserSecurityManager from configuration. Return null if no security manager
+	 * check is needed, so that a caller can skip setting security manager avoiding runtime check cost,
+	 * if there is no security check set up already. Use {@link #setFromConfiguration} helper, which
+	 * handles disabled case.
+	 *
+	 * @param configuration to instantiate the security manager from
+	 *
+	 * @return FlinkUserSecurityManager instantiated baesd on configuration. Return null if disabled.
+	 */
+	public static FlinkUserSecurityManager fromConfiguration(Configuration configuration) {
+		final String checkExitModeConfig = configuration.getString(SecurityOptions.CHECK_SYSTEM_EXIT);
+		final CheckExitMode checkExitMode;
+
+		try {
+			checkExitMode = CheckExitMode.valueOf(checkExitModeConfig.toUpperCase());
+		} catch (Exception ex) {
+			throw new IllegalConfigurationException(
+				String.format("%s is invalid configuration for %s.", checkExitModeConfig, SecurityOptions.CHECK_SYSTEM_EXIT.key()),
+				ex);
+		}
+
+		// If no check is enabled, return null so that caller can avoid setting security manager not to incur any runtime cost.
+		if (checkExitMode == CheckExitMode.DISABLED) {
+			return null;
+		}
+		// Add more configuration parameters that need user security manager (currently only for system exit).
+		return new FlinkUserSecurityManager(checkExitMode);
+	}
+
+	public static void setFromConfiguration(Configuration configuration) {
+		final FlinkUserSecurityManager flinkUserSecurityManager =
+			FlinkUserSecurityManager.fromConfiguration(configuration);
+		if (flinkUserSecurityManager != null) {
+			System.setSecurityManager(flinkUserSecurityManager);
+		}
+	}
+
+	public void checkPermission(Permission perm) {
+		if (originalSecurityManager != null) {
+			originalSecurityManager.checkPermission(perm);
+		}
+	}
+
+	public void checkPermission(Permission perm, Object context) {
+		if (originalSecurityManager != null) {
+			originalSecurityManager.checkPermission(perm, context);
+		}
+	}
+
+	public void checkExit(int status) {
+		super.checkExit(status);
+		if (!systemExitMonitored()) {
+			return;
+		}
+		switch (checkExitMode) {
+			case DISABLED:
+				break;
+			case WARN:
+				// Add exception trace log to help users to debug where exit came from.
+				LOG.warn("Exiting JVM with status {} is monitored, logging and exiting",
+					status, new UserSystemExitException());
+				break;
+			case THROW:
+				throw new UserSystemExitException();
+			default:
+				// Must not happen if exhaustively handling all modes above. Logging as being already at exit path.
+				LOG.warn("No valid check exit mode configured: {}", checkExitMode);
+		}
+	}
+
+	public SecurityManager getOriginalSecurityManager() {
+		return originalSecurityManager;
+	}
+
+	public void monitorSystemExit() {
+		monitorSystemExit.set(true);
+	}
+
+	public void unmonitorSystemExit() {
+		monitorSystemExit.set(false);
+	}
+
+	public boolean systemExitMonitored() {
+		return Boolean.TRUE.equals(monitorSystemExit.get());
+	}
+
+	public static void monitorSystemExitForCurrentThread() {
+		SecurityManager securityManager = System.getSecurityManager();
+		if (securityManager instanceof  FlinkUserSecurityManager) {
+			((FlinkUserSecurityManager) securityManager).monitorSystemExit();
+		}
+	}
+
+	public static void unmonitorSystemExitForCurrentThread() {
+		SecurityManager securityManager = System.getSecurityManager();

Review comment:
       Here we have to be careful considering that multiple `SecurityManagers` can be set wrapped up within each other. This code might not enabling/disabling might not work depending on the `SecurityManager` initialization order.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for
+ * example, it can warn or prevent user code from terminating JVM by System.exit or halt by logging or throwing an exception.
+ * This does not necessarily prevent malicious users who try to tweak security manager on their own, but more for being dependable
+ * against user mistakes by gracefully handling them informing users rather than causing silent unavailability.
+ */
+public class FlinkUserSecurityManager extends SecurityManager {
+
+	/**
+	 * The mode of how to handle user code attempting to exit JVM.
+	 */
+	public enum CheckExitMode {
+		/** No check is enabled, that is allowing exit without any action. */
+		DISABLED,
+		/** Warn by logging but still allowing exit to be performed. */
+		WARN,
+		/** Throw exception when exit is attempted disallowing JVM termination. */
+		THROW,
+	}
+
+	static final Logger LOG = LoggerFactory.getLogger(FlinkUserSecurityManager.class);
+
+	private final SecurityManager originalSecurityManager = System.getSecurityManager();
+	private ThreadLocal<Boolean> monitorSystemExit = new InheritableThreadLocal<>();
+	private CheckExitMode checkExitMode;
+
+	public FlinkUserSecurityManager(CheckExitMode checkExitMode) {
+		super();
+		this.checkExitMode = checkExitMode;
+
+		LOG.info("FlinkUserSecurityManager is created with {} system exit check (previous security manager is {})",
+			this.checkExitMode, originalSecurityManager != null ? originalSecurityManager : "not existing");
+	}
+
+	/**
+	 * Instantiate FlinkUserSecurityManager from configuration. Return null if no security manager
+	 * check is needed, so that a caller can skip setting security manager avoiding runtime check cost,
+	 * if there is no security check set up already. Use {@link #setFromConfiguration} helper, which
+	 * handles disabled case.
+	 *
+	 * @param configuration to instantiate the security manager from
+	 *
+	 * @return FlinkUserSecurityManager instantiated baesd on configuration. Return null if disabled.
+	 */
+	public static FlinkUserSecurityManager fromConfiguration(Configuration configuration) {
+		final String checkExitModeConfig = configuration.getString(SecurityOptions.CHECK_SYSTEM_EXIT);
+		final CheckExitMode checkExitMode;
+
+		try {
+			checkExitMode = CheckExitMode.valueOf(checkExitModeConfig.toUpperCase());
+		} catch (Exception ex) {
+			throw new IllegalConfigurationException(
+				String.format("%s is invalid configuration for %s.", checkExitModeConfig, SecurityOptions.CHECK_SYSTEM_EXIT.key()),
+				ex);
+		}
+
+		// If no check is enabled, return null so that caller can avoid setting security manager not to incur any runtime cost.
+		if (checkExitMode == CheckExitMode.DISABLED) {
+			return null;
+		}
+		// Add more configuration parameters that need user security manager (currently only for system exit).
+		return new FlinkUserSecurityManager(checkExitMode);
+	}
+
+	public static void setFromConfiguration(Configuration configuration) {
+		final FlinkUserSecurityManager flinkUserSecurityManager =
+			FlinkUserSecurityManager.fromConfiguration(configuration);
+		if (flinkUserSecurityManager != null) {
+			System.setSecurityManager(flinkUserSecurityManager);

Review comment:
       We might want to add more error handling here as well: Compare with `ExitTrappingSecurityManager.replaceGracefulExitWithHaltIfConfigured(..)`

##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/security/FlinkUserSecurityManagerTest.java
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Tests for {@code FlinkUserSecurityManager}.
+ */
+public class FlinkUserSecurityManagerTest {

Review comment:
       Does it make sense to check how the code behaves when there's already a `SecurityManager` initialized? ...considering that we have more than one `SecurityManager` that can be configured.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/entrypoint/ClusterEntrypoint.java
##########
@@ -220,6 +221,7 @@ private SecurityContext installSecurityContext(Configuration configuration) thro
     private void runCluster(Configuration configuration, PluginManager pluginManager)
             throws Exception {
         synchronized (lock) {
+            FlinkUserSecurityManager.setFromConfiguration(configuration);

Review comment:
       Here, we might want to collect all SecurityManager initialization calls into a single method to have it in the same place. `ExitTrappingSecurityManager` might be initialized within `ClusterEntrypoint.startCluster`. This is also the order I referred to in my comment in `FlinkUserSecurityManager. unmonitorSystemExitForCurrentThread`.

##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/security/FlinkUserSecurityManagerTest.java
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Tests for {@code FlinkUserSecurityManager}.
+ */
+public class FlinkUserSecurityManagerTest {
+
+	private static final int TEST_EXIT_CODE = 123;
+	private FlinkUserSecurityManager flinkUserSecurityManager;
+
+	@Before
+	public void setUp() {
+		flinkUserSecurityManager = new FlinkUserSecurityManager(FlinkUserSecurityManager.CheckExitMode.THROW);
+		System.setSecurityManager(flinkUserSecurityManager);
+	}
+
+	@After
+	public void tearDown() {
+		if (flinkUserSecurityManager != null) {
+			System.setSecurityManager(flinkUserSecurityManager.getOriginalSecurityManager());
+		}
+	}
+
+	@Test(expected = UserSystemExitException.class)
+	public void testThrowUserExit() {
+		flinkUserSecurityManager.monitorSystemExit();
+		flinkUserSecurityManager.checkExit(TEST_EXIT_CODE);
+	}
+
+	@Test
+	public void testToggleUserExit() {
+		flinkUserSecurityManager.checkExit(TEST_EXIT_CODE);
+		flinkUserSecurityManager.monitorSystemExit();
+		try {
+			flinkUserSecurityManager.checkExit(TEST_EXIT_CODE);
+			fail();
+		} catch (UserSystemExitException ignored) { }
+		flinkUserSecurityManager.unmonitorSystemExit();
+		flinkUserSecurityManager.checkExit(TEST_EXIT_CODE);
+	}
+
+	@Test
+	public void testPerThreadThrowUserExit() throws Exception {
+		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(() -> flinkUserSecurityManager.checkExit(TEST_EXIT_CODE),
+				executorService);
+		future.get();
+		flinkUserSecurityManager.monitorSystemExit();
+		try {
+			flinkUserSecurityManager.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(() -> flinkUserSecurityManager.checkExit(TEST_EXIT_CODE),
+				executorService);
+		future.get();
+	}
+
+	@Test
+	public void testInheritedThrowUserExit() throws Exception {
+		flinkUserSecurityManager.monitorSystemExit();
+		try {
+			flinkUserSecurityManager.checkExit(TEST_EXIT_CODE);
+			fail();
+		} catch (UserSystemExitException ignored) { }
+		Thread thread = new Thread(() -> {
+			try {
+				flinkUserSecurityManager.checkExit(TEST_EXIT_CODE);
+				fail();
+			} catch (UserSystemExitException ignored) {
+			} catch (Throwable t) {
+				fail();
+			}
+		});
+		thread.start();
+		thread.join();
+	}
+
+	@Test
+	public void testWarnUserExit() {
+		// Warn mode enables monitor but only logging allowing exit, hence not expecting exception.
+		// NOTE - Do not specifically test warning logging.
+		System.setSecurityManager(flinkUserSecurityManager.getOriginalSecurityManager());
+		flinkUserSecurityManager = new FlinkUserSecurityManager(FlinkUserSecurityManager.CheckExitMode.WARN);
+		System.setSecurityManager(flinkUserSecurityManager);
+		flinkUserSecurityManager.monitorSystemExit();
+		flinkUserSecurityManager.checkExit(TEST_EXIT_CODE);
+	}
+
+	@Test
+	public void testValidConfiguration() {

Review comment:
       Could we split this up into multiple test cases/methods to improve readability?

##########
File path: flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/tasks/StreamTaskSystemExitTest.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.streaming.runtime.tasks;
+
+import org.apache.flink.runtime.UserSystemExitException;
+import org.apache.flink.runtime.execution.Environment;
+import org.apache.flink.runtime.operators.testutils.MockEnvironmentBuilder;
+import org.apache.flink.runtime.security.FlinkUserSecurityManager;
+import org.apache.flink.streaming.api.operators.AbstractStreamOperator;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxDefaultAction;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+
+/**
+ * Tests for stream task where user-invokable codes try to exit JVM.
+ * Currently, monitoring system exit is enabled inside relevant methods that can call user-defined
+ * functions in {@code StreamTask}.
+ */
+public class StreamTaskSystemExitTest {

Review comment:
       ```suggestion
   public class StreamTaskSystemExitTest extend TestLogger {
   ```

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/security/FlinkUserSecurityManager.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.security.Permission;
+
+/**
+ * Flink user security manager to control unexpected user behaviors that potentially impact cluster availability, for
+ * example, it can warn or prevent user code from terminating JVM by System.exit or halt by logging or throwing an exception.
+ * This does not necessarily prevent malicious users who try to tweak security manager on their own, but more for being dependable
+ * against user mistakes by gracefully handling them informing users rather than causing silent unavailability.
+ */
+public class FlinkUserSecurityManager extends SecurityManager {
+
+	/**
+	 * The mode of how to handle user code attempting to exit JVM.
+	 */
+	public enum CheckExitMode {
+		/** No check is enabled, that is allowing exit without any action. */
+		DISABLED,
+		/** Warn by logging but still allowing exit to be performed. */
+		WARN,
+		/** Throw exception when exit is attempted disallowing JVM termination. */
+		THROW,
+	}
+
+	static final Logger LOG = LoggerFactory.getLogger(FlinkUserSecurityManager.class);
+
+	private final SecurityManager originalSecurityManager = System.getSecurityManager();
+	private ThreadLocal<Boolean> monitorSystemExit = new InheritableThreadLocal<>();
+	private CheckExitMode checkExitMode;
+
+	public FlinkUserSecurityManager(CheckExitMode checkExitMode) {
+		super();
+		this.checkExitMode = checkExitMode;
+
+		LOG.info("FlinkUserSecurityManager is created with {} system exit check (previous security manager is {})",
+			this.checkExitMode, originalSecurityManager != null ? originalSecurityManager : "not existing");
+	}
+
+	/**
+	 * Instantiate FlinkUserSecurityManager from configuration. Return null if no security manager
+	 * check is needed, so that a caller can skip setting security manager avoiding runtime check cost,
+	 * if there is no security check set up already. Use {@link #setFromConfiguration} helper, which
+	 * handles disabled case.
+	 *
+	 * @param configuration to instantiate the security manager from
+	 *
+	 * @return FlinkUserSecurityManager instantiated baesd on configuration. Return null if disabled.
+	 */
+	public static FlinkUserSecurityManager fromConfiguration(Configuration configuration) {
+		final String checkExitModeConfig = configuration.getString(SecurityOptions.CHECK_SYSTEM_EXIT);
+		final CheckExitMode checkExitMode;
+
+		try {
+			checkExitMode = CheckExitMode.valueOf(checkExitModeConfig.toUpperCase());
+		} catch (Exception ex) {
+			throw new IllegalConfigurationException(
+				String.format("%s is invalid configuration for %s.", checkExitModeConfig, SecurityOptions.CHECK_SYSTEM_EXIT.key()),
+				ex);
+		}
+
+		// If no check is enabled, return null so that caller can avoid setting security manager not to incur any runtime cost.
+		if (checkExitMode == CheckExitMode.DISABLED) {
+			return null;
+		}
+		// Add more configuration parameters that need user security manager (currently only for system exit).
+		return new FlinkUserSecurityManager(checkExitMode);
+	}
+
+	public static void setFromConfiguration(Configuration configuration) {
+		final FlinkUserSecurityManager flinkUserSecurityManager =
+			FlinkUserSecurityManager.fromConfiguration(configuration);
+		if (flinkUserSecurityManager != null) {
+			System.setSecurityManager(flinkUserSecurityManager);
+		}
+	}
+
+	public void checkPermission(Permission perm) {
+		if (originalSecurityManager != null) {
+			originalSecurityManager.checkPermission(perm);
+		}
+	}
+
+	public void checkPermission(Permission perm, Object context) {
+		if (originalSecurityManager != null) {
+			originalSecurityManager.checkPermission(perm, context);
+		}
+	}
+
+	public void checkExit(int status) {

Review comment:
       ```suggestion
     @Override
   	public void checkExit(int status) {
   ```

##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/security/FlinkUserSecurityManagerTest.java
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.runtime.UserSystemExitException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Tests for {@code FlinkUserSecurityManager}.
+ */
+public class FlinkUserSecurityManagerTest {

Review comment:
       ```suggestion
   public class FlinkUserSecurityManagerTest extend TestLogger {
   ```




----------------------------------------------------------------
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:
users@infra.apache.org