-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Custom getCallerClass in entitlement bridge #125139
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
6 commits
Select commit
Hold shift + click to select a range
50be8ef
Custom getCallerClass in entitlement bridge
prdoyle b7df59c
Merge branch 'main' into caller-class
prdoyle aa7e633
Merge branch 'main' into caller-class
prdoyle 83bbefd
Merge branch 'main' into caller-class
prdoyle d4f3af0
Merge branch 'main' into caller-class
prdoyle e6763e6
Merge branch 'main' into caller-class
prdoyle 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
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
44 changes: 44 additions & 0 deletions
44
libs/entitlement/bridge/src/main/java/org/elasticsearch/entitlement/bridge/Util.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,44 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.entitlement.bridge; | ||
|
||
import java.util.Optional; | ||
|
||
import static java.lang.StackWalker.Option.RETAIN_CLASS_REFERENCE; | ||
|
||
public class Util { | ||
/** | ||
* A special value representing the case where a method <em>has no caller</em>. | ||
* This can occur if it's called directly from the JVM. | ||
* | ||
* @see StackWalker#getCallerClass() | ||
*/ | ||
public static final Class<?> NO_CLASS = new Object() { | ||
}.getClass(); | ||
|
||
/** | ||
* Why would we write this instead of using {@link StackWalker#getCallerClass()}? | ||
* Because that method throws {@link IllegalCallerException} if called from the "outermost frame", | ||
* which includes at least some cases of a method called from a native frame. | ||
* | ||
* @return the class that called the method which called this; or {@link #NO_CLASS} from the outermost frame. | ||
*/ | ||
@SuppressWarnings("unused") // Called reflectively from InstrumenterImpl | ||
public static Class<?> getCallerClass() { | ||
Optional<Class<?>> callerClassIfAny = StackWalker.getInstance(RETAIN_CLASS_REFERENCE) | ||
.walk( | ||
frames -> frames.skip(2) // Skip this method and its caller | ||
.findFirst() | ||
.map(StackWalker.StackFrame::getDeclaringClass) | ||
); | ||
return callerClassIfAny.orElse(NO_CLASS); | ||
} | ||
|
||
} |
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
33 changes: 33 additions & 0 deletions
33
libs/entitlement/src/test/java/org/elasticsearch/entitlement/bridge/UtilTests.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,33 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.entitlement.bridge; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import static org.elasticsearch.entitlement.bridge.UtilTests.MockSensitiveClass.mockSensitiveMethod; | ||
|
||
@ESTestCase.WithoutSecurityManager | ||
public class UtilTests extends ESTestCase { | ||
|
||
public void testCallerClass() { | ||
assertEquals(UtilTests.class, mockSensitiveMethod()); | ||
} | ||
|
||
/** | ||
* A separate class so the stack walk can discern the sensitive method's own class | ||
* from that of its caller. | ||
*/ | ||
static class MockSensitiveClass { | ||
public static Class<?> mockSensitiveMethod() { | ||
return Util.getCallerClass(); | ||
} | ||
} | ||
|
||
} |
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.