forked from opensearch-project/job-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use pluginSubject for system index interaction and remove usages of s…
…tashContext Signed-off-by: Craig Perkins <cwperx@amazon.com>
- Loading branch information
Showing
3 changed files
with
101 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 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
65 changes: 65 additions & 0 deletions
65
src/main/java/org/opensearch/jobscheduler/transport/RunAsSubjectClient.java
This file contains 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,65 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* 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.jobscheduler.transport; | ||
|
||
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.client.Client; | ||
import org.opensearch.client.FilterClient; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.core.action.ActionResponse; | ||
import org.opensearch.identity.Subject; | ||
|
||
/** | ||
* Implementation of client that will run transport actions in a stashed context and inject the name of the provided | ||
* subject into the context. | ||
*/ | ||
public class RunAsSubjectClient extends FilterClient { | ||
|
||
private static final Logger logger = LogManager.getLogger(RunAsSubjectClient.class); | ||
|
||
private Subject subject; | ||
|
||
public RunAsSubjectClient(Client delegate) { | ||
super(delegate); | ||
} | ||
|
||
public RunAsSubjectClient(Client delegate, Subject subject) { | ||
super(delegate); | ||
this.subject = subject; | ||
} | ||
|
||
public void setSubject(Subject subject) { | ||
this.subject = subject; | ||
} | ||
|
||
@Override | ||
protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute( | ||
ActionType<Response> action, | ||
Request request, | ||
ActionListener<Response> listener | ||
) { | ||
if (subject == null) { | ||
throw new IllegalStateException("RunAsSubjectClient is not initialized."); | ||
} | ||
try (ThreadContext.StoredContext ctx = threadPool().getThreadContext().newStoredContext(false)) { | ||
subject.runAs(() -> { | ||
logger.info("Running transport action with subject: {}", subject.getPrincipal().getName()); | ||
super.doExecute(action, request, ActionListener.runBefore(listener, ctx::restore)); | ||
return null; | ||
}); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |