Skip to content

Commit

Permalink
Fixes test failures by creating a new shiro session if one is not ava…
Browse files Browse the repository at this point in the history
…ilable

Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Dec 3, 2022
1 parent 67843d9 commit e04e786
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion server/src/main/java/org/opensearch/rest/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.opensearch.OpenSearchException;
import org.opensearch.authn.tokens.AuthenticationToken;
Expand Down Expand Up @@ -622,7 +623,7 @@ private boolean authenticate(RestRequest request, RestChannel channel) throws IO
try {
headerToken = tokenType(authHeader.get());
subject = Identity.getAuthManager().getSubject();
subject.login(headerToken);
getShiroSessionAndLogin(subject, headerToken);
logger.info("Authentication successful");
return true;
} catch (final AuthenticationException ae) {
Expand Down Expand Up @@ -667,4 +668,21 @@ private AuthenticationToken tokenType(String authHeader) {
// support other type of header tokens
return null;
}

/**
* Get or create a shiro session before attempting login
*
* Should prevent "{@link org.apache.shiro.session.UnknownSessionException} There is no session with id '123'"
*
* @param subject Current shiro subject
* @param headerToken Attempt login using this token
*/
private static void getShiroSessionAndLogin(Subject subject, AuthenticationToken headerToken) {

// Get or create a new session for this subject when there is none to ensure passing tests.
// TODO: Ensure that shiro session is allowed to be created everytime
SecurityUtils.getSubject().getSession();

subject.login(headerToken);
}
}

0 comments on commit e04e786

Please sign in to comment.