You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@geode.apache.org by GitBox <gi...@apache.org> on 2021/10/01 21:07:37 UTC

[GitHub] [geode] DonalEvans commented on a change in pull request #6927: GEODE-9663: throw and handle AuthenticationExpiredException at login time

DonalEvans commented on a change in pull request #6927:
URL: https://github.com/apache/geode/pull/6927#discussion_r720527357



##########
File path: geode-apis-compatible-with-redis/src/test/java/org/apache/geode/redis/internal/executor/connection/AuthExecutorTest.java
##########
@@ -0,0 +1,70 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ *
+ */
+
+package org.apache.geode.redis.internal.executor.connection;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_AUTH_CALLED_WITHOUT_SECURITY_CONFIGURED;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_INVALID_USERNAME_OR_PASSWORD;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import java.util.Properties;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.geode.internal.security.SecurityService;
+import org.apache.geode.redis.internal.executor.RedisResponse;
+import org.apache.geode.redis.internal.netty.Command;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+import org.apache.geode.security.AuthenticationExpiredException;
+
+public class AuthExecutorTest {
+  private AuthExecutor executor;
+  private Command command;
+  private ExecutionHandlerContext context;
+  private SecurityService securityService;
+
+  @Before
+  public void before() throws Exception {

Review comment:
       `Exception` is never thrown from this method, or the other two in this class.

##########
File path: geode-core/src/test/java/org/apache/geode/cache/client/internal/AuthenticateUserOpTest.java
##########
@@ -82,4 +92,54 @@ public void constructorWithProperties() throws Exception {
     verify(impl).getCredentialBytes(eq(connection), captor.capture());
     assertThat(captor.getValue()).isEqualTo(properties);
   }
+
+  @Test
+  public void noAttempt_if_NotRequireCredentials() throws Exception {
+    when(server.getRequiresCredentials()).thenReturn(false);
+    impl.attempt(connection);
+    verify(impl, never()).parentAttempt(connection);
+  }
+
+  @Test
+  public void callPrentAttempt_IfRequireCredentials() throws Exception {
+    when(server.getRequiresCredentials()).thenReturn(true);
+    doReturn(null).when(impl).parentAttempt(connection);
+    impl.attempt(connection);
+    verify(impl).parentAttempt(connection);
+  }
+
+  @Test
+  public void whenParentAttemptThrowAuthenticationExpiredException() throws Exception {

Review comment:
       Could we also have a test case to cover when the first attempt results in an `AuthenticationExpiredException` but the second attempt is successful, just to cover all the bases?

##########
File path: geode-core/src/upgradeTest/java/org/apache/geode/security/AuthExpirationMultiServerDUnitTest.java
##########
@@ -253,6 +254,53 @@ public void registerInterestsWithMultiServers() throws Exception {
         .containsExactly("DATA:READ:partitionRegion:key0");
   }
 
+  @Test
+  public void consecutivePut() throws Exception {
+    int locatorPort = locator.getPort();
+    // do consecutive puts using a client
+    ClientVM client = cluster.startClientVM(3,
+        c -> c.withProperty(SECURITY_CLIENT_AUTH_INIT, UpdatableUserAuthInitialize.class.getName())
+            .withCacheSetup(ccf -> ccf.setPoolMaxConnections(2))
+            .withLocatorConnection(locatorPort));
+    AsyncInvocation invokePut = client.invokeAsync(() -> {
+      UpdatableUserAuthInitialize.setUser("user1");
+      Region<Object, Object> proxyRegion =
+          ClusterStartupRule.getClientCache()
+              .createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
+              .create(PARTITION_REGION);
+      IntStream.range(0, 1000).forEach(i -> proxyRegion.put("key" + i, "value" + i));
+    });
+
+    client.invoke(() -> {
+      // wait till at least 1/3 of the data is in the region to expire the user

Review comment:
       This comment seems inaccurate, since we're putting 1000 entries into the region but only waiting for 10 of them before expiring the user.

##########
File path: geode-old-client-support/src/main/java/com/gemstone/gemfire/OldClientSupportProvider.java
##########
@@ -127,11 +127,13 @@ public Throwable getThrowable(Throwable theThrowable, KnownVersion clientVersion
       return theThrowable;
     }
 
-    String className = theThrowable.getClass().getName();
-
     // backward compatibility for authentication expiration
     if (clientVersion.isOlderThan(ClientReAuthenticateMessage.RE_AUTHENTICATION_START_VERSION)) {
-      if (className.equals(AuthenticationExpiredException.class.getName())) {
+      if (theThrowable instanceof AuthenticationExpiredException) {
+        return new AuthenticationRequiredException(USER_NOT_FOUND);
+      }
+      Throwable cause = theThrowable.getCause();
+      if (cause != null && cause instanceof AuthenticationExpiredException) {

Review comment:
       This null check is redundant, as `instanceof` includes an implicit null check.

##########
File path: geode-core/src/test/java/org/apache/geode/internal/security/IntegratedSecurityServiceTest.java
##########
@@ -162,4 +172,51 @@ public void getPostProcessor_returnsNull() throws Exception {
     PostProcessor postProcessor = this.securityService.getPostProcessor();
     assertThat(postProcessor).isNull();
   }
+
+  @Test
+  public void login_when_credential_isNull() throws Exception {
+    assertThatThrownBy(() -> securityService.login(null))
+        .isInstanceOf(AuthenticationRequiredException.class)
+        .hasNoCause()
+        .hasMessageContaining("credentials are null");
+  }
+
+  @Test
+  public void login_when_ShiroException_hasNoCause() throws Exception {
+    doThrow(shiroException).when(mockSubject).login(any(GeodeAuthenticationToken.class));
+    assertThatThrownBy(() -> securityService.login(properties))
+        .isInstanceOf(AuthenticationFailedException.class)
+        .hasCauseInstanceOf(ShiroException.class)
+        .hasMessageContaining("Authentication error. Please check your credentials");
+  }
+
+  @Test
+  public void login_when_ShiroException_causdBy_AuthenticationFailed() throws Exception {

Review comment:
       Typo here, and in other test names. Should be "causedBy"

##########
File path: geode-core/src/upgradeTest/java/org/apache/geode/security/AuthExpirationMultiServerDUnitTest.java
##########
@@ -253,6 +254,53 @@ public void registerInterestsWithMultiServers() throws Exception {
         .containsExactly("DATA:READ:partitionRegion:key0");
   }
 
+  @Test
+  public void consecutivePut() throws Exception {
+    int locatorPort = locator.getPort();
+    // do consecutive puts using a client
+    ClientVM client = cluster.startClientVM(3,
+        c -> c.withProperty(SECURITY_CLIENT_AUTH_INIT, UpdatableUserAuthInitialize.class.getName())
+            .withCacheSetup(ccf -> ccf.setPoolMaxConnections(2))
+            .withLocatorConnection(locatorPort));
+    AsyncInvocation invokePut = client.invokeAsync(() -> {
+      UpdatableUserAuthInitialize.setUser("user1");
+      Region<Object, Object> proxyRegion =
+          ClusterStartupRule.getClientCache()
+              .createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
+              .create(PARTITION_REGION);
+      IntStream.range(0, 1000).forEach(i -> proxyRegion.put("key" + i, "value" + i));
+    });
+
+    client.invoke(() -> {
+      // wait till at least 1/3 of the data is in the region to expire the user
+      await().until(() -> getProxyRegion() != null);
+      await().until(() -> getProxyRegion().size() > 10);
+      UpdatableUserAuthInitialize.setUser("user2");
+    });
+
+    expireUserOnAllVms("user1");
+    invokePut.await();
+
+    ExpirableSecurityManager securityManager = collectSecurityManagers(server1, server2);
+    Map<String, List<String>> authorizedOps = securityManager.getAuthorizedOps();
+    if (authorizedOps.size() == 1) {
+      // in case user1 has finished putting all 1000 values in the region before we can expire it.
+      return;
+    }

Review comment:
       This case could potentially be avoided by having the client do put ops in a while loop, with some latch that can be toggled to exit the loop once we've finished with the `expireUserOnAllVms()` method. Without guaranteeing that the user was expired before finishing the puts, we can't guarantee that this test is actually testing anything.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@geode.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org