-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Add runAs(Subject subject) to Client interface #16976
base: main
Are you sure you want to change the base?
Conversation
…ecuting actions Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Craig Perkins <[email protected]>
try (ThreadContext.StoredContext ctx = threadContext.stashContext()) { | ||
|
||
ActionListener<Response> wrappedListener = ActionListener.wrap(r -> { | ||
ctx.restore(); |
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.
This is the main reason for introducing this PR, to ensure that the original context is restored when an action is completed.
When the Security Plugin provides its implementation of a RunAsClient, it would inject a user corresponding to the plugin before doExecute
and restore the original context (including authenticated user info) before calling the original actionListener's onResponse or onFailure.
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 created a PR on my own fork of the security plugin to demonstrate how the changes would be integrated into a sample plugin: cwperks/security#40
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.
rather than ActionListener.wrap( ... restore; onResponse ... restore; onFailure )
why not just use ActionListener.runBefore(listener, () -> context.restore())
(or better yet ActionListener.runBefore(listener, context::restore)
)?
❌ Gradle check result for 2765e88: 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]>
❕ Gradle check result for 5415ab3: 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. |
Signed-off-by: Craig Perkins <[email protected]>
@nibix @dbwiddis @reta @derek-ho @DarshitChanpura Thank you all for engaging on this PR. I greatly simplified this now to keep the existing IdentityPlugin and IdentityAwarePlugin interfaces in tact. This now introduces a new method on Client called I think this both addresses @nibix' concerns about restoring the context after the action is completed and @reta's concerns about proliferation of clients. The caller of this method can pass the subject that is assigned to IdentityAwarePlugins from the IdentityAwarePlugin.assignPluginSubject call. |
Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Craig Perkins <[email protected]>
@cwperks I don't understand why we need that:
|
Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Craig Perkins <[email protected]>
Can you elaborate on injecting a wrapper? I opened this PR because of the concerns raised here about not restoring the original context on completion of a transport action. If a plugin needs to restore the context, they need to first call |
Sure, mentioned it here: #16976 (comment). The idea is to have the
The core, if |
But how does a plugin dev select which mode to use? i.e. run this TransportAction in the context of the authenticated user (w/o stashing the context) vs. running the transport action in the system (plugin-level) context that permits the plugin system index acces? |
One of the goals is to make this specific to each IdentityAwarePlugin installed in the cluster as well, there would need to be a FilterClient for each IdentityAwarePlugin. One of the goals of this change is better plugin isolation. PluginA should be able to access its own system indices, but not system indices of other plugins. For instance, other plugins of the cluster should not be able to delete the security index or write directly to it. |
It does not need to - it will have to: the call to access plugin system index should fail with an error that the call has to be executed with PluginSubject::runAs context. |
ok, I will close this PR then. I raised this PR because @nibix had some concerns about that model: opensearch-project/security#4896 (comment) and how providing a more convenient mechanism that restores the original context would be ideal. I think the changes in this PR would provide a nice plugin developer experience (while also supporting pluginSubject.runAs(() -> { ... })) as well if plugin devs would prefer to wrap a lot of action calls at once. With the changes in this PR, a plugin dev would just add on i.e.
|
If you refer to the sync I am quite sure that a solution to the problem needs to be aware of action listeners. |
@nibix I would argue this is not a leak, this is what "run as" means: execute any further actions as the |
Any further actions called by the caller of the action that executes the run as. This can cross plugin boundaries. |
For example:
|
This is the existing case with Edit: Are there instances of plugins calling transport actions that are defined in another plugin? |
Got it, thanks @nibix , that is again raises the question - sometimes this is desired behaviour, sometimes it is not, the caller could make this choice, for example, we could have
|
Sorry, can you rephrase the question?
Well, there is nothing that prevents this scenario. There's quite a lot of reusable functionality also organized as separate plugins (modules), for example the geospatial features.
For the call to the onSuccess/onFailure methods of the calling action listener, I would argue that it will be never desired behavior to keep the plugin thread context. The only cases where this would be the case when there shall be further transport actions to be executed by the plugin itself. However, this could be easily solved with the client solution. This would make it also very clear whether a transport action is executed in a "normal" thread context or in the plugin user thread context. |
So this is what should happen - the subject should never cross the thread boundaries by default and the core (with security plugin help) is in full control of it. |
From the example you posted, the same problem is the case if you replace i.e.
|
Yes, that's why it is essential to call ctx.restore() in the action listener. The API docs contain a warning about that OpenSearch/server/src/main/java/org/opensearch/common/util/concurrent/ThreadContext.java Lines 198 to 205 in f9c239d
But it is clear that is issue is non-obvious and often overlooked, despite the warning. As we are talking about security critical stuff, I would recommend to seek for solutions that are less easy to get wrong. |
Due to the async architecture, it must be able to cross thread boundaries. But it must not propagate back to a action listener onResult/onFailure call. sequenceDiagram
Plugin A->>Plugin B: "client.execute(PluginBAction.INSTANCE)"
Plugin B->>Core: "stash thread context, set as plugin subject<br>client.execute(SearchAction.INSTANCE)"
Core->>Plugin B: "actionListener.onSuccess()<br>// thread context is still set to subject of plugin B"
Plugin B->>Plugin A: "actionListener.onSuccess()<br>// Before calling onSuccess(), Plugin B MUST make sure that the original thread context is restored"
|
Sure, this is why we have a hierarchy of |
Description
Opening up this PR for discussion about a change in the interface that was introduced to formalize how plugins should interact with their own System Indices.
In the previous PR, there was a concept of a PluginSubject that was introduced that was assigned to IdentityAwarePlugins that could be used as a drop in replacement for
try (ThreadContext.StoredContext ctx = threadContext.stashContext()) { ... }
which is the pattern prevalently used for programmatic system index access.There was discussion on that PR against introducing a separate client to make calls that execute actions in the context of the plugin's identity vs the authenticated user context. i.e. stashContext is a method to effectively switch contexts where plugins behave as the system and run without authz checks which allows access to a system index. There is an effort to put stronger mechanisms in place to perform authz checks when plugins switch context to better sandbox plugins and empower system administrators with information at installation-time with access that a plugin needs to operate normally.
Opening up this PR in response to a review comment that brings up reasons to pursue a solution with a separate client. This PR creates a subclass of FilterClient (called
RunAsClient
) that stashes the context prior to execution and action and restoring the original context before delegating back to the corresponding ActionListener's onResponse or onFailure method.Related Issues
Related to opensearch-project/security#4439
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.