Skip to content

Commit

Permalink
GEODE-9811: Turn UnavailableSecurityManagerException into CacheClosed…
Browse files Browse the repository at this point in the history
…Exception (apache#7148)
  • Loading branch information
jinmeiliao authored Nov 30, 2021
1 parent 2b523e1 commit 607c30c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.ShiroException;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.SubjectThreadState;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.util.ThreadState;

import org.apache.geode.GemFireIOException;
import org.apache.geode.annotations.VisibleForTesting;
import org.apache.geode.cache.CacheClosedException;
import org.apache.geode.internal.cache.EntryEventImpl;
import org.apache.geode.internal.security.shiro.GeodeAuthenticationToken;
import org.apache.geode.internal.security.shiro.SecurityManagerProvider;
Expand Down Expand Up @@ -117,8 +120,12 @@ public Subject getSubject() {
}
}

// in other cases like rest call, client operations, we get it from the current thread
currentUser = SecurityUtils.getSubject();
try {
// in other cases like rest call, client operations, we get it from the current thread
currentUser = getCurrentUser();
} catch (UnavailableSecurityManagerException e) {
throw new CacheClosedException("Cache is closed.", e);
}

if (currentUser == null || currentUser.getPrincipal() == null) {
throw new AuthenticationRequiredException("Failed to find the authenticated user.");
Expand All @@ -127,6 +134,11 @@ public Subject getSubject() {
return currentUser;
}

@VisibleForTesting
Subject getCurrentUser() {
return SecurityUtils.getSubject();
}

/**
* Returns the current principal if one exists or null.
*
Expand All @@ -153,7 +165,7 @@ public Subject login(final Properties credentials) {
// this makes sure it starts with a clean user object
ThreadContext.remove();

Subject currentUser = SecurityUtils.getSubject();
Subject currentUser = getCurrentUser();
GeodeAuthenticationToken token = new GeodeAuthenticationToken(credentials);
try {
logger.debug("Logging in " + token.getPrincipal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.util.Properties;

import org.apache.shiro.ShiroException;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.SubjectContext;
Expand All @@ -34,6 +36,7 @@
import org.junit.Before;
import org.junit.Test;

import org.apache.geode.cache.CacheClosedException;
import org.apache.geode.internal.security.shiro.GeodeAuthenticationToken;
import org.apache.geode.internal.security.shiro.SecurityManagerProvider;
import org.apache.geode.security.AuthenticationExpiredException;
Expand Down Expand Up @@ -219,4 +222,13 @@ public void login_when_ShiroException_causedBy_OtherExceptions() throws Exceptio
.hasCauseInstanceOf(RuntimeException.class)
.hasMessageContaining("Authentication error. Please check your credentials.");
}

@Test
public void getSubjectShouldThrowCacheClosedExceptionIfSecurityManagerUnavailable() {
IntegratedSecurityService spy = spy(securityService);
doThrow(new UnavailableSecurityManagerException("test")).when(spy).getCurrentUser();
assertThatThrownBy(() -> spy.getSubject())
.isInstanceOf(CacheClosedException.class)
.hasMessageContaining("Cache is closed");
}
}

0 comments on commit 607c30c

Please sign in to comment.