geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From qih...@apache.org
Subject [1/3] incubator-geode git commit: GEODE-77: Implement Authenticator interface in class GMSAuthenticator with unit tests.
Date Thu, 13 Aug 2015 17:51:17 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-77 52f8ce6d1 -> 0a70d5140


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/0a70d514/gemfire-core/src/main/java/com/gemstone/gemfire/internal/i18n/LocalizedStrings.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/i18n/LocalizedStrings.java
b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/i18n/LocalizedStrings.java
index db27b95..c4d9c58 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/i18n/LocalizedStrings.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/i18n/LocalizedStrings.java
@@ -2129,7 +2129,12 @@ public class LocalizedStrings extends ParentLocalizedStrings {
 
   public static final StringId Network_partition_detected = new StringId(6607, "Exiting due
to possible network partition event due to loss of {0} cache processes: {1}");
   
-  
+  // GMSAuthenticator
+  public static final StringId AUTH_PEER_AUTHENTICATION_FAILED_WITH_EXCEPTION = new StringId(6608,
"Authentication failed for [{0}] using Authenticator [{1}]. {2}");
+  public static final StringId AUTH_PEER_AUTHENTICATION_FAILED = new StringId(6609, "Authentication
failed. See coordinator [{0}] logs for details.");
+  public static final StringId AUTH_PEER_AUTHENTICATION_MISSING_CREDENTIALS = new StringId(6610,
"Failed to find credentials from [{0}] using Authenticator [{1}]");
+  public static final StringId AUTH_FAILED_TO_ACQUIRE_AUTHINITIALIZE_INSTANCE = new StringId(6611,
"AuthInitialize instance could not be obtained");
+
   /** Testing strings, messageId 90000-99999 **/
   
   /** These are simple messages for testing, translated with Babelfish. **/
@@ -2140,4 +2145,6 @@ public class LocalizedStrings extends ParentLocalizedStrings {
   public static final StringId LISTENER_PREFIX = new StringId(90004, "Listener_");
   
   public static final StringId DistributedRegion_INITIALIZING_REGION_COMPLETED_0 = new StringId(90005,
"Initialization of region {0} completed");
+
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/0a70d514/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/auth/GMSAuthenticatorJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/auth/GMSAuthenticatorJUnitTest.java
b/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/auth/GMSAuthenticatorJUnitTest.java
new file mode 100644
index 0000000..8775d0f
--- /dev/null
+++ b/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/auth/GMSAuthenticatorJUnitTest.java
@@ -0,0 +1,300 @@
+package com.gemstone.gemfire.distributed.internal.membership.gms.auth;
+
+import com.gemstone.gemfire.LogWriter;
+import com.gemstone.gemfire.distributed.DistributedMember;
+import com.gemstone.gemfire.distributed.internal.membership.gms.Services;
+import com.gemstone.gemfire.internal.logging.InternalLogWriter;
+import com.gemstone.gemfire.security.AuthInitialize;
+import com.gemstone.gemfire.security.AuthenticationFailedException;
+import com.gemstone.gemfire.security.Authenticator;
+import com.gemstone.gemfire.security.GemFireSecurityException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+import java.security.Principal;
+import java.util.Properties;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.junit.Assert.*;
+
+@Category(UnitTest.class)
+public class GMSAuthenticatorJUnitTest {
+
+  static String prefix = "com.gemstone.gemfire.distributed.internal.membership.gms.auth.GMSAuthenticatorJUnitTest$";
+
+  Properties originalSystemProps = null;
+  Properties props = null;
+  Services services = null;
+  GMSAuthenticator authenticator = null;
+  DistributedMember member = null;
+
+  @Before
+  public void setUp() throws Exception {
+    originalSystemProps = System.getProperties();
+    props = new Properties();
+    authenticator = new GMSAuthenticator();
+    services = mock(Services.class);
+    InternalLogWriter securityLog = mock(InternalLogWriter.class);
+    when(services.getSecurityLogWriter()).thenReturn(securityLog);
+    authenticator.init(services);
+    member = mock(DistributedMember.class);
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    System.setProperties(originalSystemProps);
+  }
+
+  @Test
+  public void testGetSecurityProps() throws Exception {
+    props.setProperty("gemfire.sys.security-peer-auth-init", "dummy1");
+    props.setProperty("gemfire.sys.security-peer-authenticator", "dummy2");
+    props.setProperty("security-auth-init", "dummy3");
+    System.setProperties(props);
+    Properties secProps = authenticator.getSecurityProps();
+    assertEquals("wrong size", 2, secProps.size());
+    assertEquals("wrong value", "dummy1", secProps.getProperty("security-peer-auth-init"));
+    assertEquals("wrong value", "dummy2", secProps.getProperty("security-peer-authenticator"));
+  }
+
+  @Test
+  public void testGetCredentialNormal() throws Exception {
+    props.setProperty("security-peer-auth-init", prefix + "TestAuthInit2.create");
+    TestAuthInit2 auth = new TestAuthInit2();
+    assertFalse(auth.isClosed());
+    TestAuthInit2.setAuthInitialize(auth);
+    Properties credential = authenticator.getCredentials(member, props);
+    assertTrue(props == credential);
+    assertTrue(auth.isClosed());
+    assertTrue(TestAuthInit2.getCreateCount() == 1);
+  }
+
+  @Test
+  public void testGetCredentialWithNoAuth() throws Exception {
+    Properties credential = authenticator.getCredentials(member, props);
+    assertNull(credential);
+  }
+
+  @Test
+  public void testGetCredentialWithEmptyAuth() throws Exception {
+    props.setProperty("security-peer-auth-init", "");
+    Properties credential = authenticator.getCredentials(member, props);
+    assertNull(credential);
+  }
+
+  @Test
+  public void testGetCredentialWithNotExistAuth() throws Exception {
+    props.setProperty("security-peer-auth-init", prefix + "NotExistAuth.create");
+    verifyNegativeGetCredential(props, "Failed to acquire AuthInitialize method");
+  }
+
+  @Test
+  public void testGetCredentialWithNullAuth() throws Exception {
+    props.setProperty("security-peer-auth-init", prefix + "TestAuthInit1.create");
+    verifyNegativeGetCredential(props, "AuthInitialize instance could not be obtained");
+  }
+
+  @Test
+  public void testGetCredentialWithInitError() throws Exception {
+    props.setProperty("security-peer-auth-init", prefix + "TestAuthInit3.create");
+    verifyNegativeGetCredential(props, "expected init error");
+  }
+
+  @Test
+  public void testGetCredentialWithError() throws Exception {
+    props.setProperty("security-peer-auth-init", prefix + "TestAuthInit4.create");
+    verifyNegativeGetCredential(props, "expected get credential error");
+  }
+
+  void verifyNegativeGetCredential(Properties props, String expectedError) throws Exception
{
+    try {
+      authenticator.getCredentials(member, props);
+      fail("should catch: " + expectedError);
+    } catch (GemFireSecurityException e) {
+      assertTrue(e.getMessage().startsWith(expectedError));
+    }
+  }
+
+  @Test
+  public void testAuthenticatorNormal() throws Exception {
+    props.setProperty("security-peer-authenticator", prefix + "TestAuthenticator4.create");
+    TestAuthenticator4 auth = new TestAuthenticator4();
+    assertFalse(auth.isClosed());
+    TestAuthenticator4.setAuthenticator(auth);
+    String result = authenticator.authenticate(member, props, props, member);
+    assertNull(result);
+    assertTrue(auth.isClosed());
+    assertTrue(TestAuthenticator4.getCreateCount() == 1);
+  }
+
+  @Test
+  public void testAuthenticatorWithNoAuth() throws Exception {
+      String result = authenticator.authenticate(member, props, props, member);
+      assertNull(result);
+  }
+
+  @Test
+  public void testAuthenticatorWithEmptyAuth() throws Exception {
+    props.setProperty("security-peer-authenticator", "");
+    String result = authenticator.authenticate(member, props, props, member);
+    assertNull(result);
+  }
+
+  @Test
+  public void testAuthenticatorWithNotExistAuth() throws Exception {
+    props.setProperty("security-peer-authenticator", prefix + "NotExistAuth.create");
+    verifyNegativeAuthenticate(props, props, "Authentication failed. See coordinator");
+  }
+
+  @Test
+  public void testAuthenticatorWithNullAuth() throws Exception {
+    props.setProperty("security-peer-authenticator", prefix + "TestAuthenticator1.create");
+    verifyNegativeAuthenticate(props, props, "Authentication failed. See coordinator");
+  }
+
+  @Test
+  public void testAuthenticatorWithNullCredential() throws Exception {
+    props.setProperty("security-peer-authenticator", prefix + "TestAuthenticator1.create");
+    verifyNegativeAuthenticate(null, props, "Failed to find credentials from");
+  }
+
+  @Test
+  public void testAuthenticatorWithAuthInitFailure() throws Exception {
+    props.setProperty("security-peer-authenticator", prefix + "TestAuthenticator2.create");
+    verifyNegativeAuthenticate(props, props, "Authentication failed. See coordinator");
+  }
+
+  @Test
+  public void testAuthenticatorWithAuthFailure() throws Exception {
+    props.setProperty("security-peer-authenticator", prefix + "TestAuthenticator3.create");
+    verifyNegativeAuthenticate(props, props, "Authentication failed. See coordinator");
+  }
+
+  void verifyNegativeAuthenticate(Object credential, Properties props, String expectedError)
throws Exception {
+    String result = authenticator.authenticate(member, credential, props, member);
+    assertTrue(result, result.startsWith(expectedError));
+  }
+
+  // ----------------------------------------
+  //           Test AuthInitialize
+  // ----------------------------------------
+
+  public static class TestAuthInit1 implements AuthInitialize {
+    public static AuthInitialize create() {
+      return null;
+    }
+    public void init(LogWriter systemLogger, LogWriter securityLogger) throws AuthenticationFailedException
{
+    }
+    public Properties getCredentials(Properties props, DistributedMember server, boolean
isPeer)
+        throws AuthenticationFailedException {
+      throw new AuthenticationFailedException("expected get credential error");
+    }
+    public void close() {
+    }
+  }
+
+  public static class TestAuthInit2 extends TestAuthInit1 {
+    static TestAuthInit2 instance = null;
+    static int createCount = 0;
+    public static void setAuthInitialize(TestAuthInit2 auth) {
+      instance = auth;
+    }
+    public static AuthInitialize create() {
+      createCount ++;
+      return instance;
+    }
+    public Properties getCredentials(Properties props, DistributedMember server, boolean
isPeer)
+        throws AuthenticationFailedException {
+      return props;
+    }
+    boolean closed = false;
+    public void close() {
+      closed = true;
+    }
+    public boolean isClosed() {
+      return closed;
+    }
+    public static int getCreateCount() {
+      return createCount;
+    }
+  }
+
+  public static class TestAuthInit3 extends TestAuthInit1 {
+    public static AuthInitialize create() {
+      return new TestAuthInit3();
+    }
+    public void init(LogWriter systemLogger, LogWriter securityLogger) throws AuthenticationFailedException
{
+      throw new AuthenticationFailedException("expected init error");
+    }
+  }
+
+  public static class TestAuthInit4 extends TestAuthInit1 {
+    public static AuthInitialize create() {
+      return new TestAuthInit4();
+    }
+  }
+
+  // ----------------------------------------
+  //          Test Authenticator
+  // ----------------------------------------
+
+  public static class TestAuthenticator1 implements Authenticator {
+    public static Authenticator create() {
+      return null;
+    }
+    public void init(Properties securityProps, LogWriter systemLogger, LogWriter securityLogger)
throws AuthenticationFailedException {
+    }
+    public Principal authenticate(Properties props, DistributedMember member) throws AuthenticationFailedException
{
+      return null;
+    }
+    public void close() {
+    }
+  }
+
+  public static class TestAuthenticator2 extends TestAuthenticator1 {
+    public static Authenticator create() {
+      return new TestAuthenticator2();
+    }
+    public void init(Properties securityProps, LogWriter systemLogger, LogWriter securityLogger)
throws AuthenticationFailedException {
+      throw new AuthenticationFailedException("expected init error");
+    }
+  }
+
+  public static class TestAuthenticator3 extends TestAuthenticator1 {
+    public static Authenticator create() {
+      return new TestAuthenticator3();
+    }
+    public Principal authenticate(Properties props, DistributedMember member) throws AuthenticationFailedException
{
+      throw new AuthenticationFailedException("expected authenticate error");
+    }
+  }
+
+  public static class TestAuthenticator4 extends TestAuthenticator1 {
+    static Authenticator instance = null;
+    static int createCount = 0;
+    public static void setAuthenticator(Authenticator auth) {
+      instance = auth;
+    }
+    public static Authenticator create() {
+      createCount ++;
+      return instance;
+    }
+    public Principal authenticate(Properties props, DistributedMember member) throws AuthenticationFailedException
{
+      return null;
+    }
+    boolean closed = false;
+    public void close() {
+      closed = true;
+    }
+    public boolean isClosed() {
+      return closed;
+    }
+    public static int getCreateCount() {
+      return createCount;
+    }
+  }
+}


Mime
View raw message