Repository navigation
Add a shared PluginClient for running actions as a plugin's system subject #1023
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
cwperks
merged 4 commits into
opensearch-project:main
from
DarshitChanpura:centralize-plugin-client
Oct 29, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9203011
Add a shared PluginClient for running actions as a plugin's system su…
DarshitChanpura 50872ed
Restore the caller's context with core's ContextPreservingActionListener
DarshitChanpura 6a1eb36
Make PluginClient a per-plugin base class that behaves like the wrapp…
DarshitChanpura bf05321
Check PluginClient's subject in one atomic step and pin down two tests
DarshitChanpura 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
124 changes: 124 additions & 0 deletions
124
src/main/java/org/opensearch/commons/client/PluginClient.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,124 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.commons.client; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.opensearch.action.ActionRequest; | ||
| import org.opensearch.action.ActionType; | ||
| import org.opensearch.action.support.ContextPreservingActionListener; | ||
| import org.opensearch.core.action.ActionListener; | ||
| import org.opensearch.core.action.ActionResponse; | ||
| import org.opensearch.identity.PluginSubject; | ||
| import org.opensearch.transport.client.Client; | ||
| import org.opensearch.transport.client.FilterClient; | ||
|
|
||
| /** | ||
| * Runs transport actions as the consuming plugin's system subject rather than as the authenticated | ||
| * user, so the plugin can reach the system indices it registered. Use it instead of | ||
| * {@code ThreadContext.stashContext()}: a stashed context carries no user, and the security plugin | ||
| * lets a local request with no user through without evaluating privileges, whereas the plugin | ||
| * subject is authorized only for the plugin's own system indices and the permissions it declares. | ||
| * <p> | ||
| * Core assigns the subject through {@code IdentityAwarePlugin.assignSubject} after | ||
| * {@code createComponents}, with or without the security plugin installed. Build this client in | ||
| * {@code createComponents} and call {@link #setSubject(PluginSubject)} from {@code assignSubject}. | ||
| * Apart from the identity it runs as, it behaves like the client it wraps, including which failures | ||
| * are thrown and which reach the listener. | ||
| * <p> | ||
| * Each plugin declares its own subclass. Core binds every component returned from | ||
| * {@code createComponents} by its runtime class, and plugins sharing a classloader through | ||
| * {@code extendedPlugins} share this class, so a shared type would let one plugin inject another's | ||
| * client and run as its subject. | ||
| */ | ||
| public abstract class PluginClient extends FilterClient { | ||
|
|
||
| private static final Logger log = LogManager.getLogger(PluginClient.class); | ||
|
|
||
| // Set once, from assignSubject, on a different thread from the actions that read it. | ||
| private final AtomicReference<PluginSubject> subject = new AtomicReference<>(); | ||
|
|
||
| /** | ||
| * Wraps a client so its actions run as the plugin subject, which is assigned later. | ||
| * | ||
| * @param delegate the client whose actions this wraps | ||
| */ | ||
| protected PluginClient(Client delegate) { | ||
| super(delegate); | ||
| } | ||
|
|
||
| /** | ||
| * Wraps a client with its subject already known. | ||
| * | ||
| * @param delegate the client whose actions this wraps | ||
| * @param subject the plugin's system subject | ||
| * @throws NullPointerException if the subject is null | ||
| */ | ||
| protected PluginClient(Client delegate, PluginSubject subject) { | ||
| super(delegate); | ||
| setSubject(subject); | ||
| } | ||
|
|
||
| /** | ||
| * Assigns the subject every action runs as. It can be assigned once; assigning the same subject | ||
| * again is a no-op. | ||
| * | ||
| * @param subject the plugin's system subject, normally the one passed to {@code assignSubject} | ||
| * @throws NullPointerException if the subject is null | ||
| * @throws IllegalStateException if a different subject is already assigned | ||
| */ | ||
| public final void setSubject(PluginSubject subject) { | ||
| Objects.requireNonNull(subject, "subject"); | ||
| PluginSubject existing = this.subject.compareAndExchange(null, subject); | ||
| if (existing != null && existing != subject) { | ||
| throw new IllegalStateException("PluginClient already has a subject assigned."); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Not supported, because a plugin subject has no meaning on another cluster. | ||
| * | ||
| * @param clusterAlias the remote cluster that was requested | ||
| * @return never returns | ||
| * @throws UnsupportedOperationException always | ||
| */ | ||
| @Override | ||
| public final Client getRemoteClusterClient(String clusterAlias) { | ||
| throw new UnsupportedOperationException("PluginClient cannot run actions on a remote cluster."); | ||
| } | ||
|
|
||
| /** | ||
| * Runs the action as the assigned subject. | ||
| * | ||
| * @param action the action to run as the subject | ||
| * @param request the request to run it with | ||
| * @param listener notified with the caller's thread context in place rather than the subject's | ||
| * @throws IllegalStateException if no subject has been assigned yet, which is a wiring error | ||
| */ | ||
| @Override | ||
| protected final <Request extends ActionRequest, Response extends ActionResponse> void doExecute( | ||
| ActionType<Response> action, | ||
| Request request, | ||
| ActionListener<Response> listener | ||
| ) { | ||
| PluginSubject currentSubject = subject.get(); | ||
| if (currentSubject == null) { | ||
| throw new IllegalStateException("PluginClient is not initialized with a subject."); | ||
| } | ||
|
|
||
| // Captured before runAs stashes the context; core restores it around the callback, then puts | ||
| // the responding thread's own context back. | ||
| ActionListener<Response> callerContext = ContextPreservingActionListener | ||
| .wrapPreservingContext(listener, threadPool().getThreadContext()); | ||
| currentSubject.runAs(() -> { | ||
| log.debug("Running transport action as subject: {}", currentSubject.getPrincipal().getName()); | ||
| super.doExecute(action, request, callerContext); | ||
| }); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.