-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
base: main
Are you sure you want to change the base?
Conversation
…oller Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Craig Perkins <[email protected]>
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.javaagent.bootstrap; |
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 -javaagent
are available on the classpath, so the compileOnly dependency is making the assumption this will be provided at runtime via a -javaagent
.
Signed-off-by: Craig Perkins <[email protected]>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #18346 +/- ##
============================================
+ Coverage 72.60% 72.67% +0.07%
- Complexity 67682 67748 +66
============================================
Files 5497 5498 +1
Lines 311819 311825 +6
Branches 45265 45265
============================================
+ Hits 226409 226633 +224
+ Misses 66941 66771 -170
+ Partials 18469 18421 -48 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/AccessController.java
Outdated
Show resolved
Hide resolved
libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/AccessController.java
Outdated
Show resolved
Hide resolved
libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/AccessController.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Craig Perkins <[email protected]>
❕ Gradle check result for 53be672: UNSTABLE Please review all flaky tests that succeeded after retry and create an issue if one does not already exist to track the flaky failure. |
* | ||
* @return the value returned by the action's {@code run} method | ||
*/ | ||
public static <T> T doPrivileged(PrivilegedAction<T> action) { |
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 think we should improve on this interface. Because these two method only differ in the exceptions that are thrown, the compiler can't disambiguate which overload is being called if you attempt to pass a lambda. The solution is to either make an anonymous class or explicitly add a cast, both of which are unnecessarily verbose. I suggest something like the following:
public static <T> T doPrivileged(PrivilegedAction<T> action) {
return action.run();
}
public static void doPrivileged(Runnable action) {
action.run();
}
public static <T> T doPrivilegedChecked(PrivilegedExceptionAction<T> action) throws Exception {
return action.run();
}
public static void doPrivilegedChecked(PrivilegedExceptionRunnable action) throws Exception {
action.run();
}
public interface PrivilegedExceptionRunnable {
void run() throws Exception;
}
The basic idea is to use a different method name for the checked variants, and also provide variants that do not return anything. This will allow for code like:
String foo = AccessController.doPrivilegedChecked(() -> Files.readString(Paths.get("/tmp/foo")));
and
AccessController.doPrivilegedChecked(() -> Files.delete(Paths.get("/tmp/foo")));
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.
The difference is that the arg in the unchecked doPrivileged
is PrivilegedAction
vs the arg in the checked doPrivileged
is PrivilegedExceptionAction
which is how java knows which version of the method you are using.
I like your suggestion though and think this could be an opportunity to remove PrivilegedExceptionAction
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.
But the compiler can't figure it out implicitly if you pass a lambda so you're forced to define a type with a cast. Basically I'd love to be able to change this code:
Lines 190 to 193 in abe2333
AccessController.doPrivileged((PrivilegedAction<Void>) () -> { | |
boundAddresses.add(bindAddress(address, port)); | |
return null; | |
}); |
to:
AccessController.doPrivileged(() -> boundAddresses.add(bindAddress(address, port)));
Note the runnable variant is needed here as well to avoid the return null
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.
wdyt about this interface?
public static void doPrivileged(Runnable action) {
action.run();
}
public static <T> T doPrivileged(Callable<T> action) throws Exception {
return action.call();
}
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 think we'd want all four variants: the void and return type for both checked and unchecked exceptions:
public static <T> T doPrivileged(Supplier<T> action) {
return action.get();
}
public static void doPrivileged(Runnable action) {
action.run();
}
public static <T> T doPrivilegedChecked(Callable<T> action) throws Exception {
return action.call();
}
public static <T extends Exception> void doPrivilegedChecked(CheckedRunnable<T> action) throws T {
action.run();
}
public interface CheckedRunnable<E extends Exception> {
void run() throws E;
}
To avoid ambiguity with lambdas you still have to do the slightly clunky thing of giving a different name to the checked variants, but I think that's preferable compared to requiring a cast.
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.
Added the checked variants to this interface. I think this is a much cleaner and modern interface now.
Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Craig Perkins <[email protected]>
❌ Gradle check result for 00c22c7: null Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change? |
Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Craig Perkins <[email protected]>
❌ Gradle check result for d79bdc1: FAILURE Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change? |
Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Craig Perkins <[email protected]>
@@ -51,11 +51,10 @@ | |||
import org.opensearch.ingest.IngestDocument; | |||
import org.opensearch.ingest.Processor; | |||
import org.opensearch.ingest.geoip.IngestGeoIpModulePlugin.GeoIpCache; | |||
import org.opensearch.javaagent.bootstrap.AccessController; |
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.
Testing the replacement in the ingest-geoip
module after switching :libs:agent-sm:bootstrap
from compileOnly
to compileOnlyApi
in the server.
❌ Gradle check result for 9cfa314: FAILURE Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change? |
❌ Gradle check result for 9cfa314: FAILURE Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change? |
import java.util.function.Supplier; | ||
|
||
/** | ||
* Utility class to run code in a privileged block. |
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.
Let's expand on this by including some text about when and why this is needed.
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.
Expanded on this javadoc with example usages and more explanation. It doesn't quite get into how it relates to the policy files, but perhaps we can link to opensearch-project/project-website#3784 once that's published?
Signed-off-by: Craig Perkins <[email protected]>
❌ Gradle check result for 995c66c: FAILURE Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change? |
Signed-off-by: Craig Perkins <[email protected]>
Description
The classes in this PR were on a former iteration of #17894
This PR creates replacements for JSM's AccessController which is marked for removal from the JDK. While JSM was replaced with the java agent in 3.0.0, the logic to extract the ProtectionDomains from the call stack relies on the AccessController to limit the frames when examining the stack. The java agent needs to retain this code marker to know when to stop walking the stack and this PR creates OpenSearch equivalents to the AccessController which is a simple wrapper around a runnable block of code.
Related Issues
Resolves #18339
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.