|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.secure_sm; |
| 10 | + |
| 11 | +import java.util.concurrent.Callable; |
| 12 | +import java.util.function.Supplier; |
| 13 | + |
| 14 | +/** |
| 15 | + * A utility class that provides methods to perform actions in a privileged context. |
| 16 | + * |
| 17 | + * This class is a replacement for Java's {@code java.security.AccessController} functionality which is marked for |
| 18 | + * removal. All new code should use this class instead of the JDK's {@code AccessController}. |
| 19 | + * |
| 20 | + * Running code in a privileged context will ensure that the code has the necessary permissions |
| 21 | + * without traversing through the entire call stack. See {@code org.opensearch.javaagent.StackCallerProtectionDomainChainExtractor} |
| 22 | + * |
| 23 | + * Example usages: |
| 24 | + * <pre> |
| 25 | + * {@code |
| 26 | + * AccessController.doPrivileged(() -> { |
| 27 | + * // code that requires privileges |
| 28 | + * }); |
| 29 | + * } |
| 30 | + * </pre> |
| 31 | + * |
| 32 | + * Example usage with a return value and checked exception: |
| 33 | + * |
| 34 | + * <pre> |
| 35 | + * {@code |
| 36 | + * T something = AccessController.doPrivilegedChecked(() -> { |
| 37 | + * // code that requires privileges and may throw a checked exception |
| 38 | + * return something; |
| 39 | + * // or |
| 40 | + * throw new Exception(); |
| 41 | + * }); |
| 42 | + * } |
| 43 | + * </pre> |
| 44 | + */ |
| 45 | +public final class AccessController { |
| 46 | + /** |
| 47 | + * Don't allow instantiation an {@code AccessController} |
| 48 | + */ |
| 49 | + private AccessController() {} |
| 50 | + |
| 51 | + /** |
| 52 | + * Performs the specified action in a privileged block. |
| 53 | + * |
| 54 | + * <p> If the action's {@code run} method throws an (unchecked) |
| 55 | + * exception, it will propagate through this method. |
| 56 | + * |
| 57 | + * @param action the action to be performed |
| 58 | + */ |
| 59 | + public static void doPrivileged(Runnable action) { |
| 60 | + action.run(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Performs the specified action. |
| 65 | + * |
| 66 | + * <p> If the action's {@code run} method throws an <i>unchecked</i> |
| 67 | + * exception, it will propagate through this method. |
| 68 | + * |
| 69 | + * @param <T> the type of the value returned by the |
| 70 | + * PrivilegedExceptionAction's {@code run} method |
| 71 | + * |
| 72 | + * @param action the action to be performed |
| 73 | + * |
| 74 | + * @return the value returned by the action's {@code run} method |
| 75 | + */ |
| 76 | + public static <T> T doPrivileged(Supplier<T> action) { |
| 77 | + return action.get(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Performs the specified action. |
| 82 | + * |
| 83 | + * <p> If the action's {@code run} method throws an <i>unchecked</i> |
| 84 | + * exception, it will propagate through this method. |
| 85 | + * |
| 86 | + * @param <T> the type of the value returned by the |
| 87 | + * PrivilegedExceptionAction's {@code run} method |
| 88 | + * |
| 89 | + * @param action the action to be performed |
| 90 | + * |
| 91 | + * @return the value returned by the action's {@code run} method |
| 92 | + * |
| 93 | + * @throws Exception if the specified action's |
| 94 | + * {@code call} method threw a <i>checked</i> exception |
| 95 | + */ |
| 96 | + public static <T> T doPrivilegedChecked(Callable<T> action) throws Exception { |
| 97 | + return action.call(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Performs the specified action in a privileged block. |
| 102 | + * |
| 103 | + * <p> If the action's {@code run} method throws an (unchecked) |
| 104 | + * exception, it will propagate through this method. |
| 105 | + * |
| 106 | + * @param action the action to be performed |
| 107 | + * |
| 108 | + * @throws T if the specified action's |
| 109 | + * {@code call} method threw a <i>checked</i> exception |
| 110 | + */ |
| 111 | + public static <T extends Exception> void doPrivilegedChecked(CheckedRunnable<T> action) throws T { |
| 112 | + action.run(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * A functional interface that represents a runnable action that can throw a checked exception. |
| 117 | + * |
| 118 | + * @param <E> the type of the exception that can be thrown |
| 119 | + */ |
| 120 | + public interface CheckedRunnable<E extends Exception> { |
| 121 | + |
| 122 | + /** |
| 123 | + * Executes the action. |
| 124 | + * |
| 125 | + * @throws E |
| 126 | + */ |
| 127 | + void run() throws E; |
| 128 | + } |
| 129 | +} |
0 commit comments