Class AccessController
java.lang.Object
org.opensearch.secure_sm.AccessController
A utility class that provides methods to perform actions in a privileged context.
This class is a replacement for Java's
java.security.AccessController functionality which is marked for
removal. All new code should use this class instead of the JDK's AccessController.
Running code in a privileged context will ensure that the code has the necessary permissions
without traversing through the entire call stack. See org.opensearch.javaagent.StackCallerProtectionDomainChainExtractor
Example usages:
AccessController.doPrivileged(() -> {
// code that requires privileges
});
Example usage with a return value and checked exception:
T something = AccessController.doPrivilegedChecked(() -> {
// code that requires privileges and may throw a checked exception
return something;
// or
throw new Exception();
});
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAccessController.CheckedRunnable<E extends Exception>A functional interface that represents a runnable action that can throw a checked exception. -
Method Summary
Modifier and TypeMethodDescriptionstatic voiddoPrivileged(Runnable action) Performs the specified action in a privileged block.static <T> TdoPrivileged(Supplier<T> action) Performs the specified action.static <T> TdoPrivilegedChecked(Callable<T> action) Performs the specified action.static <T extends Exception>
voidPerforms the specified action in a privileged block.
-
Method Details
-
doPrivileged
Performs the specified action in a privileged block.If the action's
runmethod throws an (unchecked) exception, it will propagate through this method.- Parameters:
action- the action to be performed
-
doPrivileged
Performs the specified action.If the action's
runmethod throws an unchecked exception, it will propagate through this method.- Type Parameters:
T- the type of the value returned by the PrivilegedExceptionAction'srunmethod- Parameters:
action- the action to be performed- Returns:
- the value returned by the action's
runmethod
-
doPrivilegedChecked
Performs the specified action.If the action's
runmethod throws an unchecked exception, it will propagate through this method.- Type Parameters:
T- the type of the value returned by the PrivilegedExceptionAction'srunmethod- Parameters:
action- the action to be performed- Returns:
- the value returned by the action's
runmethod - Throws:
Exception- if the specified action'scallmethod threw a checked exception
-
doPrivilegedChecked
public static <T extends Exception> void doPrivilegedChecked(AccessController.CheckedRunnable<T> action) throws T Performs the specified action in a privileged block.If the action's
runmethod throws an (unchecked) exception, it will propagate through this method.- Parameters:
action- the action to be performed- Throws:
T- if the specified action'scallmethod threw a checked exception
-