-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Create equivalents of JSM's AccessController in the java agent #18346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
5beafae
Create OpenSearch replacements for widely used methods in AccessContr…
cwperks e8248de
Fix javadoc
cwperks 9afced3
Remove getException
cwperks a0a0d9f
Remove other instance of apiNote
cwperks cc5a240
Modify javadoc and restart stuck CI checks
cwperks a172f67
Remove mistakenly added line
cwperks 366406f
Add to CHANGELOG
cwperks 53be672
Address code review feedback
cwperks 18ccef4
Use callable and runnable
cwperks 44eb148
Use Callable
cwperks 00c22c7
Merge branch 'main' into access-controller
cwperks 3678956
Add checked equivalents to interface
cwperks 0d6e1b3
Add throws IllegalArgumentException
cwperks d79bdc1
Merge branch 'main' into access-controller
cwperks 71ba997
Fix precommit
cwperks 9cfa314
Show example of replacement in a module
cwperks 435fe93
Merge branch 'main' into access-controller
cwperks 995c66c
Address code review comments
cwperks 5b04b59
Fix precommit
cwperks f054131
Merge branch 'main' into access-controller
cwperks 7e2a98d
Merge branch 'main' into access-controller
cwperks cca10f5
Merge branch 'main' into access-controller
cwperks 1348eeb
Address code review comments
cwperks 9c44efb
Merge branch 'access-controller' of https://github.com/cwperks/OpenSe…
cwperks e81fdf5
Merge branch 'main' into access-controller
cwperks e735773
Merge branch 'main' into access-controller
cwperks 293fd83
Merge branch 'main' into access-controller
cwperks 2c8e511
Merge branch 'main' into access-controller
cwperks 9dc5780
Merge branch 'access-controller' of https://github.com/cwperks/OpenSe…
cwperks fc6a21b
Merge branch 'main' into access-controller
cwperks dc7eafd
Create separate agent-api lib and remove compileOnlyApi
cwperks 9a3f3f2
Re-use agent-policy lib
cwperks 8950858
Address review comments
cwperks c6a61fc
Move to secure_sm package
cwperks 5c32ba2
Merge branch 'main' into access-controller
cwperks 235bd69
Merge branch 'main' into access-controller
cwperks e7270f7
Fix conflicts in CHANGELOG
cwperks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/AccessController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.secure_sm; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A utility class that provides methods to perform actions in a privileged context. | ||
* | ||
* This class is a replacement for Java's {@code java.security.AccessController} functionality which is marked for | ||
* removal. All new code should use this class instead of the JDK's {@code AccessController}. | ||
* | ||
* Running code in a privileged context will ensure that the code has the necessary permissions | ||
* without traversing through the entire call stack. See {@code org.opensearch.javaagent.StackCallerProtectionDomainChainExtractor} | ||
* | ||
* Example usages: | ||
* <pre> | ||
* {@code | ||
* AccessController.doPrivileged(() -> { | ||
* // code that requires privileges | ||
* }); | ||
* } | ||
* </pre> | ||
* | ||
* Example usage with a return value and checked exception: | ||
* | ||
* <pre> | ||
* {@code | ||
* T something = AccessController.doPrivilegedChecked(() -> { | ||
* // code that requires privileges and may throw a checked exception | ||
* return something; | ||
* // or | ||
* throw new Exception(); | ||
* }); | ||
* } | ||
* </pre> | ||
*/ | ||
public final class AccessController { | ||
/** | ||
* Don't allow instantiation an {@code AccessController} | ||
*/ | ||
private AccessController() {} | ||
|
||
/** | ||
* Performs the specified action in a privileged block. | ||
* | ||
* <p> If the action's {@code run} method throws an (unchecked) | ||
* exception, it will propagate through this method. | ||
* | ||
* @param action the action to be performed | ||
*/ | ||
public static void doPrivileged(Runnable action) { | ||
action.run(); | ||
} | ||
|
||
/** | ||
* Performs the specified action. | ||
* | ||
* <p> If the action's {@code run} method throws an <i>unchecked</i> | ||
* exception, it will propagate through this method. | ||
* | ||
* @param <T> the type of the value returned by the | ||
* PrivilegedExceptionAction's {@code run} method | ||
* | ||
* @param action the action to be performed | ||
* | ||
* @return the value returned by the action's {@code run} method | ||
*/ | ||
public static <T> T doPrivileged(Supplier<T> action) { | ||
return action.get(); | ||
} | ||
|
||
/** | ||
* Performs the specified action. | ||
* | ||
* <p> If the action's {@code run} method throws an <i>unchecked</i> | ||
* exception, it will propagate through this method. | ||
* | ||
* @param <T> the type of the value returned by the | ||
* PrivilegedExceptionAction's {@code run} method | ||
* | ||
* @param action the action to be performed | ||
* | ||
* @return the value returned by the action's {@code run} method | ||
* | ||
* @throws Exception if the specified action's | ||
* {@code call} method threw a <i>checked</i> exception | ||
*/ | ||
public static <T> T doPrivilegedChecked(Callable<T> action) throws Exception { | ||
return action.call(); | ||
} | ||
|
||
/** | ||
* Performs the specified action in a privileged block. | ||
* | ||
* <p> If the action's {@code run} method throws an (unchecked) | ||
* exception, it will propagate through this method. | ||
* | ||
* @param action the action to be performed | ||
* | ||
* @throws T if the specified action's | ||
* {@code call} method threw a <i>checked</i> exception | ||
*/ | ||
public static <T extends Exception> void doPrivilegedChecked(CheckedRunnable<T> action) throws T { | ||
action.run(); | ||
} | ||
|
||
/** | ||
* A functional interface that represents a runnable action that can throw a checked exception. | ||
* | ||
* @param <E> the type of the exception that can be thrown | ||
*/ | ||
public interface CheckedRunnable<E extends Exception> { | ||
|
||
/** | ||
* Executes the action. | ||
* | ||
* @throws E | ||
*/ | ||
void run() throws E; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/** | ||
* Classes for running code in a privileged context | ||
*/ | ||
package org.opensearch.secure_sm; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.