Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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 Apr 22, 2025
e8248de
Fix javadoc
cwperks Apr 22, 2025
9afced3
Remove getException
cwperks Apr 22, 2025
a0a0d9f
Remove other instance of apiNote
cwperks Apr 22, 2025
cc5a240
Modify javadoc and restart stuck CI checks
cwperks Apr 22, 2025
a172f67
Remove mistakenly added line
cwperks May 20, 2025
366406f
Add to CHANGELOG
cwperks May 20, 2025
53be672
Address code review feedback
cwperks May 21, 2025
18ccef4
Use callable and runnable
cwperks May 22, 2025
44eb148
Use Callable
cwperks May 22, 2025
00c22c7
Merge branch 'main' into access-controller
cwperks May 22, 2025
3678956
Add checked equivalents to interface
cwperks May 23, 2025
0d6e1b3
Add throws IllegalArgumentException
cwperks May 23, 2025
d79bdc1
Merge branch 'main' into access-controller
cwperks May 23, 2025
71ba997
Fix precommit
cwperks May 23, 2025
9cfa314
Show example of replacement in a module
cwperks May 23, 2025
435fe93
Merge branch 'main' into access-controller
cwperks May 29, 2025
995c66c
Address code review comments
cwperks May 29, 2025
5b04b59
Fix precommit
cwperks May 29, 2025
f054131
Merge branch 'main' into access-controller
cwperks Jun 2, 2025
7e2a98d
Merge branch 'main' into access-controller
cwperks Jun 6, 2025
cca10f5
Merge branch 'main' into access-controller
cwperks Jun 10, 2025
1348eeb
Address code review comments
cwperks Jun 10, 2025
9c44efb
Merge branch 'access-controller' of https://github.com/cwperks/OpenSe…
cwperks Jun 10, 2025
e81fdf5
Merge branch 'main' into access-controller
cwperks Jun 10, 2025
e735773
Merge branch 'main' into access-controller
cwperks Jun 11, 2025
293fd83
Merge branch 'main' into access-controller
cwperks Jun 13, 2025
2c8e511
Merge branch 'main' into access-controller
cwperks Jun 17, 2025
9dc5780
Merge branch 'access-controller' of https://github.com/cwperks/OpenSe…
cwperks Jun 17, 2025
fc6a21b
Merge branch 'main' into access-controller
cwperks Jun 18, 2025
dc7eafd
Create separate agent-api lib and remove compileOnlyApi
cwperks Jun 19, 2025
9a3f3f2
Re-use agent-policy lib
cwperks Jun 19, 2025
8950858
Address review comments
cwperks Jun 20, 2025
c6a61fc
Move to secure_sm package
cwperks Jun 20, 2025
5c32ba2
Merge branch 'main' into access-controller
cwperks Jun 20, 2025
235bd69
Merge branch 'main' into access-controller
cwperks Jun 21, 2025
e7270f7
Fix conflicts in CHANGELOG
cwperks Jun 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Use QueryCoordinatorContext for the rewrite in validate API. ([#18272](https://github.com/opensearch-project/OpenSearch/pull/18272))
- Upgrade crypto kms plugin dependencies for AWS SDK v2.x. ([#18268](https://github.com/opensearch-project/OpenSearch/pull/18268))
- Add support for `matched_fields` with the unified highlighter ([#18164](https://github.com/opensearch-project/OpenSearch/issues/18164))
- Create equivalents of JSM's AccessController in the java agent ([#18346](https://github.com/opensearch-project/OpenSearch/issues/18346))

### Changed
- Create generic DocRequest to better categorize ActionRequests ([#18269](https://github.com/opensearch-project/OpenSearch/pull/18269)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ private StackCallerProtectionDomainChainExtractor() {}
@Override
public Collection<ProtectionDomain> apply(Stream<StackFrame> frames) {
return frames.takeWhile(
frame -> !(frame.getClassName().equals("java.security.AccessController") && frame.getMethodName().equals("doPrivileged"))
frame -> !((frame.getClassName().equals("java.security.AccessController")
|| frame.getClassName().equals("org.opensearch.javaagent.bootstrap.AccessController"))
&& frame.getMethodName().equals("doPrivileged"))
)
.map(StackFrame::getDeclaringClass)
.map(Class::getProtectionDomain)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,76 @@ public Void run() {
}
});
}

@Test
public void testStackTruncationWithOpenSearchAccessController() throws Exception {
org.opensearch.javaagent.bootstrap.AccessController.doPrivileged(new org.opensearch.javaagent.bootstrap.PrivilegedAction<Void>() {
@Override
public Void run() {
StackCallerProtectionDomainChainExtractor extractor = StackCallerProtectionDomainChainExtractor.INSTANCE;
Set<ProtectionDomain> protectionDomains = (Set<ProtectionDomain>) extractor.apply(captureStackFrames().stream());
assertEquals(1, protectionDomains.size());
List<String> simpleNames = protectionDomains.stream().map(pd -> {
try {
return pd.getCodeSource().getLocation().toURI();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
})
.map(URI::getPath)
.map(Paths::get)
.map(Path::getFileName)
.map(Path::toString)
// strip trailing “-VERSION.jar” if present
.map(name -> name.replaceFirst("-\\d[\\d\\.]*\\.jar$", ""))
// otherwise strip “.jar”
.map(name -> name.replaceFirst("\\.jar$", ""))
.toList();
assertThat(
simpleNames,
containsInAnyOrder(
"test" // from the build/classes/java/test directory
)
);
return null;
}
});
}

@Test
public void testStackTruncationWithOpenSearchAccessControllerWithCheckedException() throws Exception {
org.opensearch.javaagent.bootstrap.AccessController.doPrivileged(
new org.opensearch.javaagent.bootstrap.PrivilegedExceptionAction<Void>() {
@Override
public Void run() {
StackCallerProtectionDomainChainExtractor extractor = StackCallerProtectionDomainChainExtractor.INSTANCE;
Set<ProtectionDomain> protectionDomains = (Set<ProtectionDomain>) extractor.apply(captureStackFrames().stream());
assertEquals(1, protectionDomains.size());
List<String> simpleNames = protectionDomains.stream().map(pd -> {
try {
return pd.getCodeSource().getLocation().toURI();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
})
.map(URI::getPath)
.map(Paths::get)
.map(Path::getFileName)
.map(Path::toString)
// strip trailing “-VERSION.jar” if present
.map(name -> name.replaceFirst("-\\d[\\d\\.]*\\.jar$", ""))
// otherwise strip “.jar”
.map(name -> name.replaceFirst("\\.jar$", ""))
.toList();
assertThat(
simpleNames,
containsInAnyOrder(
"test" // from the build/classes/java/test directory
)
);
return null;
}
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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;
Copy link
Member Author

@cwperks cwperks May 20, 2025

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?

Copy link
Member

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.


/**
* Utility class to run code in a privileged block.
*/
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) {
return action.run();

Check warning on line 34 in libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/AccessController.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/AccessController.java#L34

Added line #L34 was not covered by tests
}

/**
* 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 {
return action.run();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new PrivilegedActionException(e);

Check warning on line 59 in libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/AccessController.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/AccessController.java#L55-L59

Added lines #L55 - L59 were not covered by tests
}
}
}
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} &quot;wrapping&quot;
* 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

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/PrivilegedActionException.java#L31-L32

Added lines #L31 - L32 were not covered by tests

/**
* 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

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/bootstrap/src/main/java/org/opensearch/javaagent/bootstrap/PrivilegedActionException.java#L41-L42

Added lines #L41 - L42 were not covered by tests
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;
}
Loading