-
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
Changes from 7 commits
5beafae
e8248de
9afced3
a0a0d9f
cc5a240
a172f67
366406f
53be672
18ccef4
44eb148
00c22c7
3678956
0d6e1b3
d79bdc1
71ba997
9cfa314
435fe93
995c66c
5b04b59
f054131
7e2a98d
cca10f5
1348eeb
9c44efb
e81fdf5
e735773
293fd83
2c8e511
9dc5780
fc6a21b
dc7eafd
9a3f3f2
8950858
c6a61fc
5c32ba2
235bd69
e7270f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * 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.javaagent.bootstrap; | ||
|
|
||
| /** | ||
| * Utility class to run code in a privileged block. | ||
cwperks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| 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 <T> the type of the value returned by the PrivilegedAction'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(PrivilegedAction<T> action) { | ||
cwperks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| T result = action.run(); | ||
| return result; | ||
|
Check warning on line 35 in libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/AccessController.java
|
||
cwperks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private static PrivilegedActionException wrapException(Exception e) { | ||
| return new PrivilegedActionException(e); | ||
|
Check warning on line 39 in libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/AccessController.java
|
||
| } | ||
|
|
||
| /** | ||
| * 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 PrivilegedActionException if the specified action's | ||
| * {@code run} method threw a <i>checked</i> exception | ||
| */ | ||
| public static <T> T doPrivileged(PrivilegedExceptionAction<T> action) throws PrivilegedActionException { | ||
| try { | ||
| T result = action.run(); | ||
| return result; | ||
cwperks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (RuntimeException e) { | ||
| throw e; | ||
| } catch (Exception e) { | ||
| throw wrapException(e); | ||
|
Check warning on line 65 in libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/AccessController.java
|
||
andrross marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * 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.javaagent.bootstrap; | ||
|
|
||
| /** | ||
| * A computation to be performed by invoking | ||
| * {@code AccessController.doPrivileged} on the | ||
| * {@code PrivilegedAction} object. This interface is used only for | ||
| * computations that do not throw checked exceptions; computations that | ||
| * throw checked exceptions must use {@code PrivilegedExceptionAction} | ||
| * instead. | ||
| * @param <T> the type of the result of running the computation | ||
| * | ||
| * @see AccessController | ||
| * @see AccessController#doPrivileged(PrivilegedAction) | ||
| * @see PrivilegedExceptionAction | ||
| */ | ||
| @FunctionalInterface | ||
| public interface PrivilegedAction<T> { | ||
| /** | ||
| * Performs the computation. This method will be called by | ||
| * {@code AccessController.doPrivileged}. | ||
| * | ||
| * @return a class-dependent value that may represent the results of the | ||
| * computation. | ||
| * @see AccessController#doPrivileged(PrivilegedAction) | ||
| */ | ||
| T run(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * 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.javaagent.bootstrap; | ||
|
|
||
| /** | ||
| * This exception is thrown by | ||
| * {@code doPrivileged(PrivilegedExceptionAction)} to indicate | ||
| * that the action being performed threw a checked exception. The exception | ||
| * thrown by the action can be obtained by calling the | ||
| * {@code getException} method. In effect, an | ||
| * {@code PrivilegedActionException} is a "wrapper" | ||
| * for an exception thrown by a privileged action. | ||
| * | ||
| * @see PrivilegedExceptionAction | ||
| * @see AccessController#doPrivileged(PrivilegedExceptionAction) | ||
| */ | ||
| public class PrivilegedActionException extends Exception { | ||
| /** | ||
| * Constructs a new {@code PrivilegedActionException} "wrapping" | ||
| * the specific Exception. | ||
| * | ||
| * @param exception The exception thrown | ||
| */ | ||
| public PrivilegedActionException(Exception exception) { | ||
| super(null, exception); // Disallow initCause | ||
| } | ||
|
Check warning on line 32 in libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/PrivilegedActionException.java
|
||
|
|
||
| /** | ||
| * Returns a string representation of this exception. | ||
| * | ||
| * @return a string representation of this exception. | ||
| */ | ||
| @Override | ||
| public String toString() { | ||
| String s = getClass().getName(); | ||
| Throwable cause = super.getCause(); | ||
|
Check warning on line 42 in libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/PrivilegedActionException.java
|
||
| return (cause != null) ? (s + ": " + cause.toString()) : s; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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.javaagent.bootstrap; | ||
|
|
||
| import java.security.PrivilegedAction; | ||
|
|
||
| /** | ||
| * A computation to be performed with privileges, that throws one or | ||
| * more checked exceptions. The computation is performed by invoking | ||
| * {@code AccessController.doPrivileged} on the | ||
| * {@code PrivilegedExceptionAction} object. This interface is | ||
| * used only for computations that throw checked exceptions; | ||
| * computations that do not throw | ||
| * checked exceptions should use {@code PrivilegedAction} instead. | ||
| * @param <T> the type of the result of running the computation | ||
| * | ||
| * @see PrivilegedAction | ||
| */ | ||
| @FunctionalInterface | ||
| public interface PrivilegedExceptionAction<T> { | ||
| /** | ||
| * Performs the computation. This method will be called by | ||
| * {@code AccessController.doPrivileged}. | ||
| * | ||
| * @return a class-dependent value that may represent the results of the | ||
| * computation. | ||
| * @throws Exception an exceptional condition has occurred. Each class | ||
| * that implements {@code PrivilegedExceptionAction} should | ||
| * document the exceptions that its run method can throw. | ||
| */ | ||
|
|
||
| T run() throws Exception; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know the right module for this code. The server has a dependency on this lib, but its marked as
compileOnly. How are the other classes in this module (like AgentPolicy) available at runtime?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe jars passed to the JVM via
-javaagentare available on the classpath, so the compileOnly dependency is making the assumption this will be provided at runtime via a-javaagent.