Skip to content

Commit

Permalink
Rename to getCurrentSubject
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Aug 19, 2024
1 parent 2c9ba56 commit 9c5b2c4
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.opensearch.env.Environment;
import org.opensearch.env.NodeEnvironment;
import org.opensearch.identity.PluginSubject;
import org.opensearch.identity.UserSubject;
import org.opensearch.identity.Subject;
import org.opensearch.identity.tokens.TokenManager;
import org.opensearch.plugins.IdentityPlugin;
import org.opensearch.plugins.Plugin;
Expand Down Expand Up @@ -86,7 +86,7 @@ public Collection<Object> createComponents(
* @return The current subject
*/
@Override
public UserSubject getUserSubject() {
public Subject getCurrentSubject() {
return new ShiroSubject(authTokenHandler, SecurityUtils.getSubject());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public Principal getPrincipal() {
@Override
public <T> T runAs(Callable<T> callable) throws Exception {
try (ThreadContext.StoredContext ctx = threadPool.getThreadContext().stashContext()) {
callable.call();
return callable.call();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public String executor() {
Map<String, List<String>> filteredHeaders = filterHeaders(headers, allowList, denyList);

TokenManager tokenManager = identityService.getTokenManager();
Subject subject = this.identityService.getUserSubject();
Subject subject = this.identityService.getCurrentSubject();
OnBehalfOfClaims claims = new OnBehalfOfClaims(discoveryExtensionNode.getId(), subject.getPrincipal().getName());

transportService.sendRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public IdentityService(final Settings settings, final ThreadPool threadPool, fin
}

/**
* Gets the current UserSubject
* Gets the current Subject
*/
public UserSubject getUserSubject() {
return identityPlugin.getUserSubject();
public Subject getCurrentSubject() {
return identityPlugin.getCurrentSubject();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions server/src/main/java/org/opensearch/identity/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public interface Subject {
* runAs allows the caller to run a callable function as this subject
*/
default <T> T runAs(Callable<T> callable) throws Exception {
callable.call();
return null;
return callable.call();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package org.opensearch.identity.noop;

import org.opensearch.identity.PluginSubject;
import org.opensearch.identity.UserSubject;
import org.opensearch.identity.Subject;
import org.opensearch.identity.tokens.TokenManager;
import org.opensearch.plugins.IdentityPlugin;
import org.opensearch.plugins.Plugin;
Expand All @@ -35,7 +35,7 @@ public NoopIdentityPlugin(ThreadPool threadPool) {
* @return Must never return null
*/
@Override
public UserSubject getUserSubject() {
public Subject getCurrentSubject() {
return new NoopSubject();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public Principal getPrincipal() {
@Override
public <T> T runAs(Callable<T> callable) throws Exception {
try (ThreadContext.StoredContext ctx = threadPool.getThreadContext().stashContext()) {
callable.call();
return callable.call();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.identity.PluginSubject;
import org.opensearch.identity.UserSubject;
import org.opensearch.identity.Subject;
import org.opensearch.identity.tokens.TokenManager;

/**
Expand All @@ -22,11 +22,11 @@
public interface IdentityPlugin {

/**
* Get the current user subject.
* Get the current subject.
*
* @return Should never return null
* */
UserSubject getUserSubject();
Subject getCurrentSubject();

/**
* Get the Identity Plugin's token manager implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ private boolean handleAuthenticateUser(final RestRequest request, final RestChan
// Authentication did not fail so return true. Authorization is handled at the action level.
return true;
}
final UserSubject currentSubject = identityService.getUserSubject();
final UserSubject currentSubject = (UserSubject) identityService.getCurrentSubject();
currentSubject.authenticate(token);
logger.debug("Logged in as user " + currentSubject);
} catch (final Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void testSingleIdentityPluginSucceeds() {
IdentityPlugin identityPlugin1 = new NoopIdentityPlugin(threadPool);
List<IdentityPlugin> pluginList1 = List.of(identityPlugin1);
IdentityService identityService1 = new IdentityService(Settings.EMPTY, threadPool, pluginList1);
assertTrue(identityService1.getUserSubject().getPrincipal().getName().equalsIgnoreCase("Unauthenticated"));
assertTrue(identityService1.getCurrentSubject().getPrincipal().getName().equalsIgnoreCase("Unauthenticated"));
assertThat(identityService1.getTokenManager(), is(instanceOf(NoopTokenManager.class)));
terminate(threadPool);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public void setUp() throws Exception {
extensionTokenProcessor = "placeholder_extension_token_processor";
identityService = new IdentityService(Settings.EMPTY, mock(ThreadPool.class), List.of());
TokenManager tokenManager = identityService.getTokenManager();
Subject subject = this.identityService.getUserSubject();
Subject subject = this.identityService.getCurrentSubject();
OnBehalfOfClaims claims = new OnBehalfOfClaims("testID", subject.getPrincipal().getName());
expectedRequestIssuerIdentity = identityService.getTokenManager()
.issueOnBehalfOfToken(identityService.getUserSubject(), claims)
.issueOnBehalfOfToken(identityService.getCurrentSubject(), claims)
.asAuthHeaderValue();
}

Expand Down

0 comments on commit 9c5b2c4

Please sign in to comment.