summaryrefslogtreecommitdiff
path: root/java/broker/src/test/java/org/apache/qpid/server/security
diff options
context:
space:
mode:
Diffstat (limited to 'java/broker/src/test/java/org/apache/qpid/server/security')
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/SubjectCreatorTest.java138
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticatedPrincipalTest.java147
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticatedPrincipalTestHelper.java54
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticationResultTest.java112
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/TestPrincipalUtils.java (renamed from java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/TestPrincipalUtils.java)15
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/UsernamePrincipalTest.java (renamed from java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/UsernamePrincipalTest.java)55
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabaseTest.java2
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/database/PlainPasswordFilePrincipalDatabaseTest.java2
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/database/PropertiesPrincipalDatabase.java158
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AnonymousAuthenticationManagerTest.java47
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationManagerRegistryTest.java304
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/Base64MD5PasswordFileAuthenticationManagerFactoryTest.java111
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/ExternalAuthenticationManagerTest.java58
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactoryTest.java111
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManagerTest.java364
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactoryTest.java48
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticatorTest.java183
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/TestPrincipalDatabase.java6
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupDatabaseTest.java456
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerFactoryTest.java77
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerTest.java197
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/group/GroupPrincipalAccessorTest.java80
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/group/GroupPrincipalTest.java (renamed from java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/GroupPrincipalTest.java)14
23 files changed, 1986 insertions, 753 deletions
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/SubjectCreatorTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/SubjectCreatorTest.java
new file mode 100644
index 0000000000..b1bc9bea68
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/SubjectCreatorTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.qpid.server.security;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.security.Principal;
+import java.util.Arrays;
+import java.util.HashSet;
+
+import javax.security.auth.Subject;
+import javax.security.sasl.SaslServer;
+
+import junit.framework.TestCase;
+
+import org.apache.qpid.server.security.auth.AuthenticatedPrincipal;
+import org.apache.qpid.server.security.auth.AuthenticationResult;
+import org.apache.qpid.server.security.auth.AuthenticationResult.AuthenticationStatus;
+import org.apache.qpid.server.security.auth.SubjectAuthenticationResult;
+import org.apache.qpid.server.security.auth.manager.AuthenticationManager;
+import org.apache.qpid.server.security.group.GroupPrincipalAccessor;
+
+public class SubjectCreatorTest extends TestCase
+{
+ private static final String USERNAME = "username";
+ private static final String PASSWORD = "password";
+
+ private AuthenticationManager _authenticationManager = mock(AuthenticationManager.class);
+ private GroupPrincipalAccessor _groupPrincipalAccessor = mock(GroupPrincipalAccessor.class);
+ private SubjectCreator _subjectCreator = new SubjectCreator(_authenticationManager, _groupPrincipalAccessor);
+
+ private Principal _userPrincipal = mock(Principal.class);
+ private Principal _group1 = mock(Principal.class);
+ private Principal _group2 = mock(Principal.class);
+
+ private AuthenticationResult _authenticationResult;
+ private SaslServer _testSaslServer = mock(SaslServer.class);
+ private byte[] _saslResponseBytes = PASSWORD.getBytes();
+
+ @Override
+ public void setUp()
+ {
+ _authenticationResult = new AuthenticationResult(_userPrincipal);
+ when(_authenticationManager.authenticate(USERNAME, PASSWORD)).thenReturn(_authenticationResult);
+
+ when(_groupPrincipalAccessor.getGroupPrincipals(USERNAME))
+ .thenReturn(new HashSet<Principal>(Arrays.asList(_group1, _group2)));
+ }
+
+ public void testAuthenticateUsernameAndPasswordReturnsSubjectWithUserAndGroupPrincipals()
+ {
+ final SubjectAuthenticationResult actualResult = _subjectCreator.authenticate(USERNAME, PASSWORD);
+
+ assertEquals(AuthenticationStatus.SUCCESS, actualResult.getStatus());
+
+ final Subject actualSubject = actualResult.getSubject();
+
+ assertEquals("Should contain one user principal and two groups ", 3, actualSubject.getPrincipals().size());
+
+ assertTrue(actualSubject.getPrincipals().contains(new AuthenticatedPrincipal(_userPrincipal)));
+ assertTrue(actualSubject.getPrincipals().contains(_group1));
+ assertTrue(actualSubject.getPrincipals().contains(_group2));
+
+ assertTrue(actualSubject.isReadOnly());
+ }
+
+ public void testSaslAuthenticationSuccessReturnsSubjectWithUserAndGroupPrincipals() throws Exception
+ {
+ when(_authenticationManager.authenticate(_testSaslServer, _saslResponseBytes)).thenReturn(_authenticationResult);
+ when(_testSaslServer.isComplete()).thenReturn(true);
+ when(_testSaslServer.getAuthorizationID()).thenReturn(USERNAME);
+
+ SubjectAuthenticationResult result = _subjectCreator.authenticate(_testSaslServer, _saslResponseBytes);
+
+ final Subject actualSubject = result.getSubject();
+ assertEquals("Should contain one user principal and two groups ", 3, actualSubject.getPrincipals().size());
+
+ assertTrue(actualSubject.getPrincipals().contains(new AuthenticatedPrincipal(_userPrincipal)));
+ assertTrue(actualSubject.getPrincipals().contains(_group1));
+ assertTrue(actualSubject.getPrincipals().contains(_group2));
+
+ assertTrue(actualSubject.isReadOnly());
+ }
+
+ public void testAuthenticateUnsuccessfulWithUsernameReturnsNullSubjectAndCorrectStatus()
+ {
+ testUnsuccessfulAuthentication(AuthenticationResult.AuthenticationStatus.CONTINUE);
+ testUnsuccessfulAuthentication(AuthenticationResult.AuthenticationStatus.ERROR);
+ }
+
+ private void testUnsuccessfulAuthentication(AuthenticationStatus expectedStatus)
+ {
+ AuthenticationResult failedAuthenticationResult = new AuthenticationResult(expectedStatus);
+
+ when(_authenticationManager.authenticate(USERNAME, PASSWORD)).thenReturn(failedAuthenticationResult);
+
+ SubjectAuthenticationResult subjectAuthenticationResult = _subjectCreator.authenticate(USERNAME, PASSWORD);
+
+ assertSame(expectedStatus, subjectAuthenticationResult.getStatus());
+ assertNull(subjectAuthenticationResult.getSubject());
+ }
+
+ public void testAuthenticateUnsuccessfulWithSaslServerReturnsNullSubjectAndCorrectStatus()
+ {
+ testUnsuccessfulAuthenticationWithSaslServer(AuthenticationResult.AuthenticationStatus.CONTINUE);
+ testUnsuccessfulAuthenticationWithSaslServer(AuthenticationResult.AuthenticationStatus.ERROR);
+ }
+
+ private void testUnsuccessfulAuthenticationWithSaslServer(AuthenticationStatus expectedStatus)
+ {
+ AuthenticationResult failedAuthenticationResult = new AuthenticationResult(expectedStatus);
+
+ when(_authenticationManager.authenticate(_testSaslServer, _saslResponseBytes)).thenReturn(failedAuthenticationResult);
+ when(_testSaslServer.isComplete()).thenReturn(false);
+
+ SubjectAuthenticationResult subjectAuthenticationResult = _subjectCreator.authenticate(_testSaslServer, _saslResponseBytes);
+
+ assertSame(expectedStatus, subjectAuthenticationResult.getStatus());
+ assertNull(subjectAuthenticationResult.getSubject());
+ }
+}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticatedPrincipalTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticatedPrincipalTest.java
new file mode 100644
index 0000000000..cd5791952f
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticatedPrincipalTest.java
@@ -0,0 +1,147 @@
+/*
+ * 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.qpid.server.security.auth;
+
+import java.security.Principal;
+
+import javax.security.auth.Subject;
+
+import org.apache.qpid.server.security.auth.UsernamePrincipal;
+
+import junit.framework.TestCase;
+
+public class AuthenticatedPrincipalTest extends TestCase
+{
+
+ private AuthenticatedPrincipal _authenticatedPrincipal = new AuthenticatedPrincipal(new UsernamePrincipal("name"));
+
+ public void testGetAuthenticatedPrincipalFromSubject()
+ {
+ final Subject subject = createSubjectContainingAuthenticatedPrincipal();
+ final AuthenticatedPrincipal actual = AuthenticatedPrincipal.getAuthenticatedPrincipalFromSubject(subject);
+ assertSame(_authenticatedPrincipal, actual);
+ }
+
+ public void testAuthenticatedPrincipalNotInSubject()
+ {
+ try
+ {
+ AuthenticatedPrincipal.getAuthenticatedPrincipalFromSubject(new Subject());
+ fail("Exception not thrown");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ // PASS
+ }
+ }
+
+ public void testGetOptionalAuthenticatedPrincipalFromSubject()
+ {
+ final Subject subject = createSubjectContainingAuthenticatedPrincipal();
+ final AuthenticatedPrincipal actual = AuthenticatedPrincipal.getOptionalAuthenticatedPrincipalFromSubject(subject);
+ assertSame(_authenticatedPrincipal, actual);
+ }
+
+ public void testGetOptionalAuthenticatedPrincipalFromSubjectReturnsNullIfMissing()
+ {
+ Subject subjectWithNoPrincipals = new Subject();
+ assertNull(AuthenticatedPrincipal.getOptionalAuthenticatedPrincipalFromSubject(subjectWithNoPrincipals));
+
+ Subject subjectWithoutAuthenticatedPrincipal = new Subject();
+ subjectWithoutAuthenticatedPrincipal.getPrincipals().add(new UsernamePrincipal("name1"));
+ assertNull("Should return null for a subject containing a principal that isn't an AuthenticatedPrincipal",
+ AuthenticatedPrincipal.getOptionalAuthenticatedPrincipalFromSubject(subjectWithoutAuthenticatedPrincipal));
+ }
+
+ public void testTooManyAuthenticatedPrincipalsInSubject()
+ {
+ final Subject subject = new Subject();
+ subject.getPrincipals().add(new AuthenticatedPrincipal(new UsernamePrincipal("name1")));
+ subject.getPrincipals().add(new AuthenticatedPrincipal(new UsernamePrincipal("name2")));
+
+ try
+ {
+ AuthenticatedPrincipal.getAuthenticatedPrincipalFromSubject(subject);
+ fail("Exception not thrown");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ // PASS
+ }
+ }
+
+ private Subject createSubjectContainingAuthenticatedPrincipal()
+ {
+ final Principal other = new Principal()
+ {
+ public String getName()
+ {
+ return "otherprincipal";
+ }
+ };
+
+ final Subject subject = new Subject();
+ subject.getPrincipals().add(_authenticatedPrincipal);
+ subject.getPrincipals().add(other);
+ return subject;
+ }
+
+ public void testEqualsAndHashcode()
+ {
+ AuthenticatedPrincipal user1principal1 = new AuthenticatedPrincipal(new UsernamePrincipal("user1"));
+ AuthenticatedPrincipal user1principal2 = new AuthenticatedPrincipal(new UsernamePrincipal("user1"));
+
+ assertTrue(user1principal1.equals(user1principal1));
+ assertTrue(user1principal1.equals(user1principal2));
+ assertTrue(user1principal2.equals(user1principal1));
+
+ assertEquals(user1principal1.hashCode(), user1principal2.hashCode());
+ }
+
+ public void testEqualsAndHashcodeWithSameWrappedObject()
+ {
+ UsernamePrincipal wrappedPrincipal = new UsernamePrincipal("user1");
+ AuthenticatedPrincipal user1principal1 = new AuthenticatedPrincipal(wrappedPrincipal);
+ AuthenticatedPrincipal user1principal2 = new AuthenticatedPrincipal(wrappedPrincipal);
+
+ assertTrue(user1principal1.equals(user1principal1));
+ assertTrue(user1principal1.equals(user1principal2));
+ assertTrue(user1principal2.equals(user1principal1));
+
+ assertEquals(user1principal1.hashCode(), user1principal2.hashCode());
+ }
+
+ public void testEqualsWithDifferentUsernames()
+ {
+ AuthenticatedPrincipal user1principal1 = new AuthenticatedPrincipal(new UsernamePrincipal("user1"));
+ AuthenticatedPrincipal user1principal2 = new AuthenticatedPrincipal(new UsernamePrincipal("user2"));
+
+ assertFalse(user1principal1.equals(user1principal2));
+ assertFalse(user1principal2.equals(user1principal1));
+ }
+
+ public void testEqualsWithDisimilarObjects()
+ {
+ UsernamePrincipal wrappedPrincipal = new UsernamePrincipal("user1");
+ AuthenticatedPrincipal authenticatedPrincipal = new AuthenticatedPrincipal(wrappedPrincipal);
+
+ assertFalse(authenticatedPrincipal.equals(wrappedPrincipal));
+ assertFalse(wrappedPrincipal.equals(authenticatedPrincipal));
+ }
+}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticatedPrincipalTestHelper.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticatedPrincipalTestHelper.java
new file mode 100644
index 0000000000..e9d8d16fce
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticatedPrincipalTestHelper.java
@@ -0,0 +1,54 @@
+/*
+ * 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.qpid.server.security.auth;
+
+import java.security.Principal;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import junit.framework.Assert;
+
+/**
+ * Helper class for testing that sets of principals contain {@link AuthenticatedPrincipal}'s that wrap
+ * expected {@link Principal}'s.
+ */
+public class AuthenticatedPrincipalTestHelper
+{
+ public static void assertOnlyContainsWrapped(Principal wrappedPrincipal, Set<Principal> principals)
+ {
+ assertOnlyContainsWrappedAndSecondaryPrincipals(wrappedPrincipal, Collections.<Principal>emptySet(), principals);
+ }
+
+
+ public static void assertOnlyContainsWrappedAndSecondaryPrincipals(
+ Principal expectedWrappedPrincipal,
+ Set<Principal> expectedSecondaryPrincipals,
+ Set<Principal> actualPrincipals)
+ {
+ Assert.assertEquals("Principal set should contain one principal " + "but the principal set is: " + actualPrincipals,
+ 1 + expectedSecondaryPrincipals.size(),
+ actualPrincipals.size());
+
+ Set<Principal> expectedSet = new HashSet<Principal>(expectedSecondaryPrincipals);
+ expectedSet.add(new AuthenticatedPrincipal(expectedWrappedPrincipal));
+
+ Assert.assertEquals(expectedSet, actualPrincipals);
+ }
+}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticationResultTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticationResultTest.java
new file mode 100644
index 0000000000..a023cbdbb2
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/AuthenticationResultTest.java
@@ -0,0 +1,112 @@
+/*
+ * 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.qpid.server.security.auth;
+
+import static org.apache.qpid.server.security.auth.AuthenticatedPrincipalTestHelper.assertOnlyContainsWrapped;
+import static org.apache.qpid.server.security.auth.AuthenticatedPrincipalTestHelper.assertOnlyContainsWrappedAndSecondaryPrincipals;
+import static org.mockito.Mockito.mock;
+
+import java.security.Principal;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+public class AuthenticationResultTest extends TestCase
+{
+ public void testConstructWithAuthenticationStatusContinue()
+ {
+ AuthenticationResult authenticationResult = new AuthenticationResult(AuthenticationResult.AuthenticationStatus.CONTINUE);
+ assertSame(AuthenticationResult.AuthenticationStatus.CONTINUE, authenticationResult.getStatus());
+ assertTrue(authenticationResult.getPrincipals().isEmpty());
+ }
+
+ public void testConstructWithAuthenticationStatusError()
+ {
+ AuthenticationResult authenticationResult = new AuthenticationResult(AuthenticationResult.AuthenticationStatus.ERROR);
+ assertSame(AuthenticationResult.AuthenticationStatus.ERROR, authenticationResult.getStatus());
+ assertTrue(authenticationResult.getPrincipals().isEmpty());
+ }
+
+ public void testConstructWithAuthenticationStatusSuccessThrowsException()
+ {
+ try
+ {
+ new AuthenticationResult(AuthenticationResult.AuthenticationStatus.SUCCESS);
+ fail("Exception not thrown");
+ }
+ catch(IllegalArgumentException e)
+ {
+ // PASS
+ }
+ }
+
+ public void testConstructWithPrincipal()
+ {
+ Principal mainPrincipal = mock(Principal.class);
+ AuthenticationResult authenticationResult = new AuthenticationResult(mainPrincipal);
+
+ assertOnlyContainsWrapped(mainPrincipal, authenticationResult.getPrincipals());
+ assertSame(AuthenticationResult.AuthenticationStatus.SUCCESS, authenticationResult.getStatus());
+ }
+
+ public void testConstructWithNullPrincipalThrowsException()
+ {
+ try
+ {
+ new AuthenticationResult((Principal)null);
+ fail("Exception not thrown");
+ }
+ catch(IllegalArgumentException e)
+ {
+ // pass
+ }
+ }
+
+ public void testConstructWithSetOfPrincipals()
+ {
+ Principal mainPrincipal = mock(Principal.class);
+ Principal secondaryPrincipal = mock(Principal.class);
+ Set<Principal> secondaryPrincipals = Collections.singleton(secondaryPrincipal);
+
+ AuthenticationResult authenticationResult = new AuthenticationResult(mainPrincipal, secondaryPrincipals);
+
+ assertOnlyContainsWrappedAndSecondaryPrincipals(mainPrincipal, secondaryPrincipals, authenticationResult.getPrincipals());
+ assertSame(AuthenticationResult.AuthenticationStatus.SUCCESS, authenticationResult.getStatus());
+ }
+
+ public void testConstructWithSetOfPrincipalsDeDuplicatesMainPrincipal()
+ {
+ Principal mainPrincipal = mock(Principal.class);
+ Principal secondaryPrincipal = mock(Principal.class);
+
+ Set<Principal> secondaryPrincipalsContainingDuplicateOfMainPrincipal = new HashSet<Principal>(
+ Arrays.asList(secondaryPrincipal, mainPrincipal));
+ Set<Principal> deDuplicatedSecondaryPrincipals = Collections.singleton(secondaryPrincipal);
+
+ AuthenticationResult authenticationResult = new AuthenticationResult(
+ mainPrincipal, secondaryPrincipalsContainingDuplicateOfMainPrincipal);
+
+ assertOnlyContainsWrappedAndSecondaryPrincipals(mainPrincipal, deDuplicatedSecondaryPrincipals, authenticationResult.getPrincipals());
+
+ assertSame(AuthenticationResult.AuthenticationStatus.SUCCESS, authenticationResult.getStatus());
+ }
+}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/TestPrincipalUtils.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/TestPrincipalUtils.java
index 7ce03eaa79..ea6b40e3de 100644
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/TestPrincipalUtils.java
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/TestPrincipalUtils.java
@@ -18,9 +18,12 @@
* under the License.
*
*/
-package org.apache.qpid.server.security.auth.sasl;
+package org.apache.qpid.server.security.auth;
import javax.security.auth.Subject;
+
+import org.apache.qpid.server.security.group.GroupPrincipal;
+
import java.security.Principal;
import java.util.Collections;
import java.util.HashSet;
@@ -28,21 +31,19 @@ import java.util.Set;
public class TestPrincipalUtils
{
-
/**
- * Creates a test subject, with exactly one UsernamePrincipal and zero or more GroupPrincipals.
+ * Creates a test subject, with exactly one {@link AuthenticatedPrincipal} and zero or more GroupPrincipals.
*/
public static Subject createTestSubject(final String username, final String... groups)
{
final Set<Principal> principals = new HashSet<Principal>(1 + groups.length);
- principals.add(new UsernamePrincipal(username));
+ principals.add(new AuthenticatedPrincipal(username));
for (String group : groups)
{
principals.add(new GroupPrincipal(group));
}
-
- final Subject subject = new Subject(true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET);
- return subject;
+
+ return new Subject(true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET);
}
}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/UsernamePrincipalTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/UsernamePrincipalTest.java
index 75bc76c688..5e025d3ca8 100644
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/UsernamePrincipalTest.java
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/UsernamePrincipalTest.java
@@ -18,13 +18,10 @@
* under the License.
*
*/
-package org.apache.qpid.server.security.auth.sasl;
+package org.apache.qpid.server.security.auth;
import junit.framework.TestCase;
-import javax.security.auth.Subject;
-import java.security.Principal;
-
/**
* Tests the UsernamePrincipal.
*
@@ -70,54 +67,4 @@ public class UsernamePrincipalTest extends TestCase
UsernamePrincipal principal = new UsernamePrincipal("string");
assertFalse(principal.equals(null));
}
-
- public void testGetUsernamePrincipalFromSubject()
- {
- final UsernamePrincipal expected = new UsernamePrincipal("name");
- final Principal other = new Principal()
- {
- public String getName()
- {
- return "otherprincipal";
- }
- };
-
- final Subject subject = new Subject();
- subject.getPrincipals().add(expected);
- subject.getPrincipals().add(other);
-
- final UsernamePrincipal actual = UsernamePrincipal.getUsernamePrincipalFromSubject(subject);
- assertSame(expected, actual);
- }
-
- public void testUsernamePrincipalNotInSubject()
- {
- try
- {
- UsernamePrincipal.getUsernamePrincipalFromSubject(new Subject());
- fail("Exception not thrown");
- }
- catch (IllegalArgumentException iae)
- {
- // PASS
- }
- }
-
- public void testTooManyUsernamePrincipalInSubject()
- {
- final Subject subject = new Subject();
- subject.getPrincipals().add(new UsernamePrincipal("name1"));
- subject.getPrincipals().add(new UsernamePrincipal("name2"));
- try
- {
-
- UsernamePrincipal.getUsernamePrincipalFromSubject(subject);
- fail("Exception not thrown");
- }
- catch (IllegalArgumentException iae)
- {
- // PASS
- }
- }
-
}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabaseTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabaseTest.java
index 33740af1e7..7b244e219e 100644
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabaseTest.java
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabaseTest.java
@@ -23,7 +23,7 @@ package org.apache.qpid.server.security.auth.database;
import junit.framework.TestCase;
import org.apache.commons.codec.binary.Base64;
-import org.apache.qpid.server.security.auth.sasl.UsernamePrincipal;
+import org.apache.qpid.server.security.auth.UsernamePrincipal;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.login.AccountNotFoundException;
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/PlainPasswordFilePrincipalDatabaseTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/PlainPasswordFilePrincipalDatabaseTest.java
index b8601f0e5c..8e62324f7d 100644
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/PlainPasswordFilePrincipalDatabaseTest.java
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/PlainPasswordFilePrincipalDatabaseTest.java
@@ -22,7 +22,7 @@ package org.apache.qpid.server.security.auth.database;
import junit.framework.TestCase;
-import org.apache.qpid.server.security.auth.sasl.UsernamePrincipal;
+import org.apache.qpid.server.security.auth.UsernamePrincipal;
import javax.security.auth.login.AccountNotFoundException;
import java.io.BufferedReader;
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/PropertiesPrincipalDatabase.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/PropertiesPrincipalDatabase.java
new file mode 100644
index 0000000000..f670d80ae8
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/PropertiesPrincipalDatabase.java
@@ -0,0 +1,158 @@
+/*
+ * 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.qpid.server.security.auth.database;
+
+import org.apache.qpid.server.security.auth.sasl.AuthenticationProviderInitialiser;
+import org.apache.qpid.server.security.auth.UsernamePrincipal;
+import org.apache.qpid.server.security.auth.sasl.crammd5.CRAMMD5Initialiser;
+import org.apache.qpid.server.security.auth.sasl.plain.PlainInitialiser;
+
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.login.AccountNotFoundException;
+import java.io.IOException;
+import java.security.Principal;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+public class PropertiesPrincipalDatabase implements PrincipalDatabase
+{
+ private Properties _users;
+
+ private Map<String, AuthenticationProviderInitialiser> _saslServers;
+
+ public PropertiesPrincipalDatabase(Properties users)
+ {
+ _users = users;
+
+ _saslServers = new HashMap<String, AuthenticationProviderInitialiser>();
+
+ /**
+ * Create Authenticators for Properties Principal Database.
+ */
+
+ // Accept MD5 incomming and use plain comparison with the file
+ PlainInitialiser cram = new PlainInitialiser();
+ cram.initialise(this);
+ // Accept Plain incomming and hash it for comparison to the file.
+ CRAMMD5Initialiser plain = new CRAMMD5Initialiser();
+ plain.initialise(this, CRAMMD5Initialiser.HashDirection.INCOMMING);
+
+ _saslServers.put(plain.getMechanismName(), cram);
+ _saslServers.put(cram.getMechanismName(), plain);
+ }
+
+ public void setPassword(Principal principal, PasswordCallback callback) throws IOException, AccountNotFoundException
+ {
+ if (principal == null)
+ {
+ throw new IllegalArgumentException("principal must not be null");
+ }
+
+
+
+ final String pwd = _users.getProperty(principal.getName());
+
+ if (pwd != null)
+ {
+ callback.setPassword(pwd.toCharArray());
+ }
+ else
+ {
+ throw new AccountNotFoundException("No account found for principal " + principal);
+ }
+ }
+
+ public boolean verifyPassword(String principal, char[] password) throws AccountNotFoundException
+ {
+ //fixme this is not correct as toCharArray is not safe based on the type of string.
+ char[] pwd = _users.getProperty(principal).toCharArray();
+
+ return compareCharArray(pwd, password);
+ }
+
+ public boolean updatePassword(Principal principal, char[] password) throws AccountNotFoundException
+ {
+ return false; // updates denied
+ }
+
+ public boolean createPrincipal(Principal principal, char[] password)
+ {
+ return false; // updates denied
+ }
+
+ public boolean deletePrincipal(Principal principal) throws AccountNotFoundException
+ {
+ return false; // updates denied
+ }
+
+ private boolean compareCharArray(char[] a, char[] b)
+ {
+ boolean equal = false;
+ if (a.length == b.length)
+ {
+ equal = true;
+ int index = 0;
+ while (equal && index < a.length)
+ {
+ equal = a[index] == b[index];
+ index++;
+ }
+ }
+ return equal;
+ }
+
+
+ public Map<String, AuthenticationProviderInitialiser> getMechanisms()
+ {
+ return _saslServers;
+ }
+
+ public List<Principal> getUsers()
+ {
+ return new LinkedList<Principal>(); //todo
+ }
+
+ public Principal getUser(String username)
+ {
+ if (_users.getProperty(username) != null)
+ {
+ return new UsernamePrincipal(username);
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ public void reload() throws IOException
+ {
+ //No file to update from, so do nothing.
+ }
+
+ @Override
+ public void setPasswordFile(String passwordFile)
+ {
+ throw new UnsupportedOperationException();
+ }
+}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AnonymousAuthenticationManagerTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AnonymousAuthenticationManagerTest.java
index 9dcd22c088..cfeb7c525b 100644
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AnonymousAuthenticationManagerTest.java
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AnonymousAuthenticationManagerTest.java
@@ -20,26 +20,17 @@
*/
package org.apache.qpid.server.security.auth.manager;
+import static org.apache.qpid.server.security.auth.AuthenticatedPrincipalTestHelper.assertOnlyContainsWrapped;
+
import javax.security.sasl.SaslException;
import javax.security.sasl.SaslServer;
-import org.apache.commons.configuration.CompositeConfiguration;
-import org.apache.commons.configuration.ConfigurationException;
-import org.apache.commons.configuration.XMLConfiguration;
-import org.apache.qpid.server.configuration.plugins.ConfigurationPlugin;
+
import org.apache.qpid.server.security.auth.AuthenticationResult;
-import org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase;
-import org.apache.qpid.server.util.InternalBrokerBaseCase;
+import org.apache.qpid.test.utils.QpidTestCase;
-public class AnonymousAuthenticationManagerTest extends InternalBrokerBaseCase
+public class AnonymousAuthenticationManagerTest extends QpidTestCase
{
-
- private AuthenticationManager _manager = null;
-
- public void setUp() throws Exception
- {
- _manager = AnonymousAuthenticationManager.INSTANCE;
- }
-
+ private AuthenticationManager _manager = new AnonymousAuthenticationManager();
public void tearDown() throws Exception
{
@@ -49,29 +40,6 @@ public class AnonymousAuthenticationManagerTest extends InternalBrokerBaseCase
}
}
- private ConfigurationPlugin getPlainDatabaseConfig() throws ConfigurationException
- {
- final ConfigurationPlugin config = new PrincipalDatabaseAuthenticationManager.PrincipalDatabaseAuthenticationManagerConfiguration();
-
- XMLConfiguration xmlconfig = new XMLConfiguration();
- xmlconfig.addProperty("pd-auth-manager.principal-database.class", PlainPasswordFilePrincipalDatabase.class.getName());
-
- // Create a CompositeConfiguration as this is what the broker uses
- CompositeConfiguration composite = new CompositeConfiguration();
- composite.addConfiguration(xmlconfig);
- config.setConfiguration("security", xmlconfig);
- return config;
- }
-
-
- public void testConfiguration() throws Exception
- {
- AuthenticationManager authenticationManager =
- AnonymousAuthenticationManager.FACTORY.newInstance(getPlainDatabaseConfig());
-
- assertNull("AnonymousAuthenticationManager unexpectedly created when not in config", authenticationManager);
- }
-
public void testGetMechanisms() throws Exception
{
assertEquals("ANONYMOUS", _manager.getMechanisms());
@@ -102,7 +70,8 @@ public class AnonymousAuthenticationManagerTest extends InternalBrokerBaseCase
assertEquals("Expected authentication to be successful",
AuthenticationResult.AuthenticationStatus.SUCCESS,
result.getStatus());
- assertNotNull("Subject should not be null", result.getSubject());
+
+ assertOnlyContainsWrapped(AnonymousAuthenticationManager.ANONYMOUS_PRINCIPAL, result.getPrincipals());
}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationManagerRegistryTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationManagerRegistryTest.java
deleted file mode 100644
index efb8df3a38..0000000000
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationManagerRegistryTest.java
+++ /dev/null
@@ -1,304 +0,0 @@
-/*
- * 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.qpid.server.security.auth.manager;
-
-import static org.mockito.Mockito.*;
-
-import java.net.InetSocketAddress;
-import java.net.SocketAddress;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.commons.configuration.ConfigurationException;
-import org.apache.qpid.server.configuration.ServerConfiguration;
-import org.apache.qpid.server.plugins.Plugin;
-import org.apache.qpid.server.plugins.PluginManager;
-import org.apache.qpid.server.security.SecurityManager.SecurityConfiguration;
-import org.mockito.Mockito;
-
-import junit.framework.TestCase;
-
-public class AuthenticationManagerRegistryTest extends TestCase
-{
- private static final Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> EMPTY_PLUGINMAP = Collections.emptyMap();
-
- private PluginManager _pluginManager = Mockito.mock(PluginManager.class);
- private ServerConfiguration _serverConfiguration = Mockito.mock(ServerConfiguration.class);
- private SecurityConfiguration _securityConfiguration = Mockito.mock(SecurityConfiguration.class);
-
- private List<AuthenticationManager> _allCreatedAuthManagers = new ArrayList<AuthenticationManager>();
-
- @Override
- protected void setUp() throws Exception
- {
- super.setUp();
-
- // Setup server configuration to return mock security config.
- when(_serverConfiguration.getConfiguration(SecurityConfiguration.class.getName())).thenReturn(_securityConfiguration);
- }
-
- @Override
- protected void tearDown() throws Exception
- {
- try
- {
- verifyAllCreatedAuthManagersClosed();
- }
- finally
- {
- super.tearDown();
- }
- }
-
- public void testNoAuthenticationManagerFactoryPluginsFound() throws Exception
- {
- when(_pluginManager.getAuthenticationManagerPlugins()).thenReturn(EMPTY_PLUGINMAP);
- try
- {
- new AuthenticationManagerRegistry(_serverConfiguration, _pluginManager);
- fail("Exception not thrown");
- }
- catch (ConfigurationException ce)
- {
- // PASS
- assertEquals("No authentication manager factory plugins found. Check the desired authentication manager plugin has been placed in the plugins directory.",
- ce.getMessage());
- }
- }
-
- public void testSameAuthenticationManagerSpecifiedTwice() throws Exception
- {
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager1.class);
-
- Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> pluginMap = createPluginMap(myAuthManagerFactory, myAuthManagerFactory);
-
- when(_pluginManager.getAuthenticationManagerPlugins()).thenReturn(pluginMap);
-
- try
- {
- new AuthenticationManagerRegistry(_serverConfiguration, _pluginManager);
- fail("Exception not thrown");
- }
- catch (ConfigurationException ce)
- {
- // PASS
- assertEquals("Cannot configure more than one authentication manager of type " + myAuthManagerFactory.getPluginClass().getSimpleName() + ". Remove configuration for one of the authentication managers.",
- ce.getMessage());
- }
- }
-
- public void testMultipleAuthenticationManagersSpecifiedButNoDefaultSpecified() throws Exception
- {
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory1 = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager1.class);
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory2 = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager2.class);
-
- Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> pluginMap = createPluginMap(myAuthManagerFactory1, myAuthManagerFactory2);
-
- when(_pluginManager.getAuthenticationManagerPlugins()).thenReturn(pluginMap);
- when(_serverConfiguration.getDefaultAuthenticationManager()).thenReturn(null);
-
- try
- {
- new AuthenticationManagerRegistry(_serverConfiguration, _pluginManager);
- fail("Exception not thrown");
- }
- catch (ConfigurationException ce)
- {
- // PASS
- assertEquals("If more than one authentication manager is configured a default MUST be specified.",
- ce.getMessage());
- }
- }
-
- public void testDefaultAuthenticationManagerNotKnown() throws Exception
- {
- String myDefaultAuthManagerSimpleClassName = "UnknownAuthenticationManager";
-
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory1 = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager1.class);
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory2 = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager2.class);
-
- Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> pluginMap = createPluginMap(myAuthManagerFactory1, myAuthManagerFactory2);
-
- when(_pluginManager.getAuthenticationManagerPlugins()).thenReturn(pluginMap);
- when(_serverConfiguration.getDefaultAuthenticationManager()).thenReturn(myDefaultAuthManagerSimpleClassName);
-
- try
- {
- new AuthenticationManagerRegistry(_serverConfiguration, _pluginManager);
- fail("Exception not thrown");
- }
- catch (ConfigurationException ce)
- {
- // PASS
- assertTrue("Unexpected message " + ce.getMessage(),
- ce.getMessage().startsWith("No authentication managers configured of type " + myDefaultAuthManagerSimpleClassName + " which is specified as the default"));
- }
- }
-
- public void testPortMappedToUnknownAuthenticationManager() throws Exception
- {
- String myDefaultAuthManagerSimpleClassName = "UnknownAuthenticationManager";
- int portNumber = 1234;
-
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory1 = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager1.class);
-
- Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> pluginMap = createPluginMap(myAuthManagerFactory1);
-
- when(_pluginManager.getAuthenticationManagerPlugins()).thenReturn(pluginMap);
- when(_serverConfiguration.getPortAuthenticationMappings()).thenReturn(Collections.singletonMap(portNumber, myDefaultAuthManagerSimpleClassName));
-
- try
- {
- new AuthenticationManagerRegistry(_serverConfiguration, _pluginManager);
- fail("Exception not thrown");
- }
- catch (ConfigurationException ce)
- {
- // PASS
- assertEquals("Unknown authentication manager class " + myDefaultAuthManagerSimpleClassName + " configured for port " + portNumber, ce.getMessage());
- }
- }
-
- public void testGetAuthenticationManagerForInetSocketAddress() throws Exception
- {
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory1 = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager1.class);
- Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> pluginMap = createPluginMap(myAuthManagerFactory1);
-
- when(_pluginManager.getAuthenticationManagerPlugins()).thenReturn(pluginMap);
-
- AuthenticationManagerRegistry registry = new AuthenticationManagerRegistry(_serverConfiguration, _pluginManager);
-
- AuthenticationManager authenticationManager = registry.getAuthenticationManager(new InetSocketAddress(1234));
- assertEquals("TestAuthenticationManager1", authenticationManager.getMechanisms());
-
- registry.close();
- }
-
- public void testGetAuthenticationManagerForNonInetSocketAddress() throws Exception
- {
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory1 = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager1.class);
- Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> pluginMap = createPluginMap(myAuthManagerFactory1);
-
- when(_pluginManager.getAuthenticationManagerPlugins()).thenReturn(pluginMap);
-
- AuthenticationManagerRegistry registry = new AuthenticationManagerRegistry(_serverConfiguration, _pluginManager);
-
- AuthenticationManager authenticationManager = registry.getAuthenticationManager(mock(SocketAddress.class));
- assertEquals("TestAuthenticationManager1", authenticationManager.getMechanisms());
-
- registry.close();
- }
-
- public void testGetAuthenticationManagerWithMultipleAuthenticationManager() throws Exception
- {
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory1 = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager1.class);
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory2 = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager2.class);
- Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> pluginMap = createPluginMap(myAuthManagerFactory1, myAuthManagerFactory2);
-
- String defaultAuthManger = myAuthManagerFactory1.getPluginName();
- int unmappedPortNumber = 1234;
- int mappedPortNumber = 1235;
- String mappedAuthManager = myAuthManagerFactory2.getPluginName();
-
- when(_pluginManager.getAuthenticationManagerPlugins()).thenReturn(pluginMap);
- when(_serverConfiguration.getDefaultAuthenticationManager()).thenReturn(defaultAuthManger);
- when(_serverConfiguration.getPortAuthenticationMappings()).thenReturn(Collections.singletonMap(mappedPortNumber, mappedAuthManager));
-
- AuthenticationManagerRegistry registry = new AuthenticationManagerRegistry(_serverConfiguration, _pluginManager);
-
- AuthenticationManager authenticationManager1 = registry.getAuthenticationManager(new InetSocketAddress(unmappedPortNumber));
- assertEquals("TestAuthenticationManager1", authenticationManager1.getMechanisms());
-
- AuthenticationManager authenticationManager2 = registry.getAuthenticationManager(new InetSocketAddress(mappedPortNumber));
- assertEquals("TestAuthenticationManager2", authenticationManager2.getMechanisms());
-
- registry.close();
- }
-
- public void testAuthenticationManagersAreClosed() throws Exception
- {
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory1 = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager1.class);
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory2 = newMockFactoryProducingMockAuthManagerImplementing(TestAuthenticationManager2.class);
- Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> pluginMap = createPluginMap(myAuthManagerFactory1, myAuthManagerFactory2);
-
- String defaultAuthManger = myAuthManagerFactory1.getPluginName();
- when(_pluginManager.getAuthenticationManagerPlugins()).thenReturn(pluginMap);
- when(_serverConfiguration.getDefaultAuthenticationManager()).thenReturn(defaultAuthManger);
-
- AuthenticationManagerRegistry registry = new AuthenticationManagerRegistry(_serverConfiguration, _pluginManager);
-
- registry.close();
- }
-
- private AuthenticationManagerPluginFactory<? extends Plugin> newMockFactoryProducingMockAuthManagerImplementing(Class<? extends AuthenticationManager> authManagerClazz)
- throws ConfigurationException
- {
- AuthenticationManager myAuthManager = mock(authManagerClazz);
- when(myAuthManager.getMechanisms()).thenReturn(authManagerClazz.getSimpleName()); // used to verify the getAuthenticationManagerFor returns expected impl.
-
- AuthenticationManagerPluginFactory myAuthManagerFactory = mock(AuthenticationManagerPluginFactory.class);
- when(myAuthManagerFactory.getPluginClass()).thenReturn(myAuthManager.getClass());
- when(myAuthManagerFactory.getPluginName()).thenReturn(myAuthManager.getClass().getSimpleName());
- when(myAuthManagerFactory.newInstance(_securityConfiguration)).thenReturn(myAuthManager);
-
- _allCreatedAuthManagers.add(myAuthManager);
- return myAuthManagerFactory;
- }
-
- private Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> createPluginMap(
- AuthenticationManagerPluginFactory<? extends Plugin> myAuthManagerFactory)
- {
- return createPluginMap(myAuthManagerFactory, null);
- }
-
- private Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> createPluginMap(
- AuthenticationManagerPluginFactory<? extends Plugin> authManagerFactory1,
- AuthenticationManagerPluginFactory<? extends Plugin> authManagerFactory2)
- {
- Map<String, AuthenticationManagerPluginFactory<? extends Plugin>> pluginMap = new HashMap<String, AuthenticationManagerPluginFactory<? extends Plugin>>();
- pluginMap.put("config.path.unused1", authManagerFactory1);
- if (authManagerFactory2 != null)
- {
- pluginMap.put("config.path.unused2", authManagerFactory2);
- }
- return pluginMap;
- }
-
- private void verifyAllCreatedAuthManagersClosed()
- {
- for (Iterator<AuthenticationManager> iterator = _allCreatedAuthManagers.iterator(); iterator.hasNext();)
- {
- AuthenticationManager authenticationManager = (AuthenticationManager) iterator.next();
- verify(authenticationManager).close();
- }
- }
-
- private interface TestAuthenticationManager1 extends AuthenticationManager
- {
- }
-
- private interface TestAuthenticationManager2 extends AuthenticationManager
- {
- }
-}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/Base64MD5PasswordFileAuthenticationManagerFactoryTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/Base64MD5PasswordFileAuthenticationManagerFactoryTest.java
new file mode 100644
index 0000000000..04e09e073f
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/Base64MD5PasswordFileAuthenticationManagerFactoryTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.qpid.server.security.auth.manager;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.qpid.server.plugin.AuthenticationManagerFactory;
+import org.apache.qpid.server.security.auth.database.Base64MD5PasswordFilePrincipalDatabase;
+
+public class Base64MD5PasswordFileAuthenticationManagerFactoryTest extends TestCase
+{
+ AuthenticationManagerFactory _factory = new Base64MD5PasswordFileAuthenticationManagerFactory();
+ private Map<String, Object> _configuration = new HashMap<String, Object>();
+ private File _emptyPasswordFile;
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ _emptyPasswordFile = File.createTempFile(getName(), "passwd");
+ _emptyPasswordFile.deleteOnExit();
+ }
+
+ public void testBase64MD5InstanceCreated() throws Exception
+ {
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, Base64MD5PasswordFileAuthenticationManagerFactory.PROVIDER_TYPE);
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_PATH, _emptyPasswordFile.getAbsolutePath());
+
+ AuthenticationManager manager = _factory.createInstance(_configuration);
+ assertNotNull(manager);
+ assertTrue(manager instanceof PrincipalDatabaseAuthenticationManager);
+ assertTrue(((PrincipalDatabaseAuthenticationManager)manager).getPrincipalDatabase() instanceof Base64MD5PasswordFilePrincipalDatabase);
+ }
+
+ public void testPasswordFileNotFound() throws Exception
+ {
+ //delete the file
+ _emptyPasswordFile.delete();
+
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, Base64MD5PasswordFileAuthenticationManagerFactory.PROVIDER_TYPE);
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_PATH, _emptyPasswordFile.getAbsolutePath());
+
+ try
+ {
+ _factory.createInstance(_configuration);
+ }
+ catch (RuntimeException re)
+ {
+ assertTrue(re.getCause() instanceof FileNotFoundException);
+ }
+ }
+
+ public void testReturnsNullWhenNoConfig() throws Exception
+ {
+ AuthenticationManager manager = _factory.createInstance(_configuration);
+ assertNull(manager);
+ }
+
+ public void testReturnsNullWhenConfigForOtherAuthManagerType() throws Exception
+ {
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, "other-auth-manager");
+ AuthenticationManager manager = _factory.createInstance(_configuration);
+ assertNull(manager);
+ }
+
+ public void testReturnsNullWhenConfigForPlainPDImplementationNoPasswordFileValueSpecified() throws Exception
+ {
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, Base64MD5PasswordFileAuthenticationManagerFactory.PROVIDER_TYPE);
+
+ AuthenticationManager manager = _factory.createInstance(_configuration);
+ assertNull(manager);
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ try
+ {
+ if (_emptyPasswordFile == null && _emptyPasswordFile.exists())
+ {
+ _emptyPasswordFile.delete();
+ }
+ }
+ finally
+ {
+ super.tearDown();
+ }
+ }
+} \ No newline at end of file
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/ExternalAuthenticationManagerTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/ExternalAuthenticationManagerTest.java
index c1a55ef2ad..a66d73c47d 100644
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/ExternalAuthenticationManagerTest.java
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/ExternalAuthenticationManagerTest.java
@@ -18,58 +18,18 @@
*/
package org.apache.qpid.server.security.auth.manager;
+import static org.apache.qpid.server.security.auth.AuthenticatedPrincipalTestHelper.assertOnlyContainsWrapped;
+
import javax.security.auth.x500.X500Principal;
import javax.security.sasl.SaslException;
import javax.security.sasl.SaslServer;
-import org.apache.commons.configuration.CompositeConfiguration;
-import org.apache.commons.configuration.ConfigurationException;
-import org.apache.commons.configuration.XMLConfiguration;
-import org.apache.qpid.server.configuration.plugins.ConfigurationPlugin;
+
import org.apache.qpid.server.security.auth.AuthenticationResult;
-import org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase;
-import org.apache.qpid.server.util.InternalBrokerBaseCase;
+import org.apache.qpid.test.utils.QpidTestCase;
-public class ExternalAuthenticationManagerTest extends InternalBrokerBaseCase
+public class ExternalAuthenticationManagerTest extends QpidTestCase
{
-
- private AuthenticationManager _manager = null;
-
- public void setUp() throws Exception
- {
- _manager = ExternalAuthenticationManager.INSTANCE;
- }
-
-
- public void tearDown() throws Exception
- {
- if(_manager != null)
- {
- _manager = null;
- }
- }
-
- private ConfigurationPlugin getPlainDatabaseConfig() throws ConfigurationException
- {
- final ConfigurationPlugin config = new PrincipalDatabaseAuthenticationManager.PrincipalDatabaseAuthenticationManagerConfiguration();
-
- XMLConfiguration xmlconfig = new XMLConfiguration();
- xmlconfig.addProperty("pd-auth-manager.principal-database.class", PlainPasswordFilePrincipalDatabase.class.getName());
-
- // Create a CompositeConfiguration as this is what the broker uses
- CompositeConfiguration composite = new CompositeConfiguration();
- composite.addConfiguration(xmlconfig);
- config.setConfiguration("security", xmlconfig);
- return config;
- }
-
-
- public void testConfiguration() throws Exception
- {
- AuthenticationManager authenticationManager =
- ExternalAuthenticationManager.FACTORY.newInstance(getPlainDatabaseConfig());
-
- assertNull("ExternalAuthenticationManager unexpectedly created when not in config", authenticationManager);
- }
+ private AuthenticationManager _manager = new ExternalAuthenticationManager();
public void testGetMechanisms() throws Exception
{
@@ -103,12 +63,12 @@ public class ExternalAuthenticationManagerTest extends InternalBrokerBaseCase
assertEquals("Expected authentication to be successful",
AuthenticationResult.AuthenticationStatus.SUCCESS,
result.getStatus());
- assertEquals("Expected principal to be unchanged",
- principal,
- result.getSubject().getPrincipals().iterator().next());
+
+ assertOnlyContainsWrapped(principal, result.getPrincipals());
saslServer = _manager.createSaslServer("EXTERNAL", "example.example.com", null);
result = _manager.authenticate(saslServer, new byte[0]);
+
assertNotNull(result);
assertEquals("Expected authentication to be unsuccessful",
AuthenticationResult.AuthenticationStatus.ERROR,
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactoryTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactoryTest.java
new file mode 100644
index 0000000000..d428f8b211
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactoryTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.qpid.server.security.auth.manager;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.qpid.server.plugin.AuthenticationManagerFactory;
+import org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase;
+
+public class PlainPasswordFileAuthenticationManagerFactoryTest extends TestCase
+{
+ AuthenticationManagerFactory _factory = new PlainPasswordFileAuthenticationManagerFactory();
+ private Map<String, Object> _configuration = new HashMap<String, Object>();
+ private File _emptyPasswordFile;
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ _emptyPasswordFile = File.createTempFile(getName(), "passwd");
+ _emptyPasswordFile.deleteOnExit();
+ }
+
+ public void testPlainInstanceCreated() throws Exception
+ {
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, PlainPasswordFileAuthenticationManagerFactory.PROVIDER_TYPE);
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_PATH, _emptyPasswordFile.getAbsolutePath());
+
+ AuthenticationManager manager = _factory.createInstance(_configuration);
+ assertNotNull(manager);
+ assertTrue(manager instanceof PrincipalDatabaseAuthenticationManager);
+ assertTrue(((PrincipalDatabaseAuthenticationManager)manager).getPrincipalDatabase() instanceof PlainPasswordFilePrincipalDatabase);
+ }
+
+ public void testPasswordFileNotFound() throws Exception
+ {
+ //delete the file
+ _emptyPasswordFile.delete();
+
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, PlainPasswordFileAuthenticationManagerFactory.PROVIDER_TYPE);
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_PATH, _emptyPasswordFile.getAbsolutePath());
+
+ try
+ {
+ _factory.createInstance(_configuration);
+ }
+ catch (RuntimeException re)
+ {
+ assertTrue(re.getCause() instanceof FileNotFoundException);
+ }
+ }
+
+ public void testReturnsNullWhenNoConfig() throws Exception
+ {
+ AuthenticationManager manager = _factory.createInstance(_configuration);
+ assertNull(manager);
+ }
+
+ public void testReturnsNullWhenConfigForOtherAuthManagerType() throws Exception
+ {
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, "other-auth-manager");
+ AuthenticationManager manager = _factory.createInstance(_configuration);
+ assertNull(manager);
+ }
+
+ public void testReturnsNullWhenConfigForPlainPDImplementationNoPasswordFileValueSpecified() throws Exception
+ {
+ _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, PlainPasswordFileAuthenticationManagerFactory.PROVIDER_TYPE);
+
+ AuthenticationManager manager = _factory.createInstance(_configuration);
+ assertNull(manager);
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ try
+ {
+ if (_emptyPasswordFile == null && _emptyPasswordFile.exists())
+ {
+ _emptyPasswordFile.delete();
+ }
+ }
+ finally
+ {
+ super.tearDown();
+ }
+ }
+}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManagerTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManagerTest.java
index 47c189e4fa..1ae667804a 100644
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManagerTest.java
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManagerTest.java
@@ -20,132 +20,92 @@
*/
package org.apache.qpid.server.security.auth.manager;
-import org.apache.commons.configuration.CompositeConfiguration;
-import org.apache.commons.configuration.ConfigurationException;
-import org.apache.commons.configuration.XMLConfiguration;
+import static org.apache.qpid.server.security.auth.AuthenticatedPrincipalTestHelper.assertOnlyContainsWrapped;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
-import org.apache.qpid.server.configuration.plugins.ConfigurationPlugin;
-import org.apache.qpid.server.security.auth.AuthenticationResult;
-import org.apache.qpid.server.security.auth.AuthenticationResult.AuthenticationStatus;
-import org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase;
-import org.apache.qpid.server.security.auth.sasl.UsernamePrincipal;
-import org.apache.qpid.server.util.InternalBrokerBaseCase;
+import java.security.Provider;
+import java.security.Security;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
-import javax.security.auth.Subject;
+import javax.security.auth.callback.CallbackHandler;
import javax.security.sasl.SaslException;
import javax.security.sasl.SaslServer;
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileWriter;
-import java.security.Provider;
-import java.security.Security;
+import javax.security.sasl.SaslServerFactory;
+
+import org.apache.qpid.server.security.auth.AuthenticationResult;
+import org.apache.qpid.server.security.auth.AuthenticationResult.AuthenticationStatus;
+import org.apache.qpid.server.security.auth.UsernamePrincipal;
+import org.apache.qpid.server.security.auth.database.PrincipalDatabase;
+import org.apache.qpid.server.security.auth.sasl.AuthenticationProviderInitialiser;
+import org.apache.qpid.server.security.auth.sasl.UsernamePasswordInitialiser;
+import org.apache.qpid.test.utils.QpidTestCase;
/**
- *
* Tests the public methods of PrincipalDatabaseAuthenticationManager.
*
*/
-public class PrincipalDatabaseAuthenticationManagerTest extends InternalBrokerBaseCase
+public class PrincipalDatabaseAuthenticationManagerTest extends QpidTestCase
{
+ private static final String MOCK_MECH_NAME = "MOCK-MECH-NAME";
+ private static final UsernamePrincipal PRINCIPAL = new UsernamePrincipal("guest");
+
private AuthenticationManager _manager = null; // Class under test
- private String TEST_USERNAME = "guest";
- private String TEST_PASSWORD = "guest";
+ private PrincipalDatabase _principalDatabase;
- /**
- * @see org.apache.qpid.server.util.InternalBrokerBaseCase#tearDown()
- */
@Override
public void tearDown() throws Exception
{
- super.tearDown();
if (_manager != null)
{
_manager.close();
}
+ super.tearDown();
}
- /**
- * @see org.apache.qpid.server.util.InternalBrokerBaseCase#setUp()
- */
- @Override
- public void setUp() throws Exception
+ private void setupMocks() throws Exception
{
- super.setUp();
-
- final String passwdFilename = createPasswordFile().getCanonicalPath();
- final ConfigurationPlugin config = getConfig(PlainPasswordFilePrincipalDatabase.class.getName(),
- "passwordFile", passwdFilename);
+ _principalDatabase = mock(PrincipalDatabase.class);
- _manager = PrincipalDatabaseAuthenticationManager.FACTORY.newInstance(config);
- }
+ AuthenticationProviderInitialiser _mockMechInitialiser = mock(AuthenticationProviderInitialiser.class);
+ Map<String, AuthenticationProviderInitialiser> _initialisers = Collections.singletonMap(MOCK_MECH_NAME, _mockMechInitialiser);
- /**
- * Tests where the case where the config specifies a PD implementation
- * that is not found.
- */
- public void testPrincipalDatabaseImplementationNotFound() throws Exception
- {
- try
- {
- _manager = PrincipalDatabaseAuthenticationManager.FACTORY.newInstance(getConfig("not.Found", null, null));
- fail("Exception not thrown");
- }
- catch (ConfigurationException ce)
- {
- // PASS
- }
- }
+ when(_principalDatabase.getMechanisms()).thenReturn(_initialisers);
- /**
- * Tests where the case where the config specifies a PD implementation
- * of the wrong type.
- */
- public void testPrincipalDatabaseImplementationWrongType() throws Exception
- {
- try
- {
- _manager = PrincipalDatabaseAuthenticationManager.FACTORY.newInstance(getConfig(String.class.getName(), null, null)); // Not a PrincipalDatabase implementation
- fail("Exception not thrown");
- }
- catch (ConfigurationException ce)
- {
- // PASS
- }
+ _manager = new PrincipalDatabaseAuthenticationManager(_principalDatabase);
+ _manager.initialise();
}
- /**
- * Tests the case where a setter with the desired name cannot be found.
- */
- public void testPrincipalDatabaseSetterNotFound() throws Exception
+ private void setupMocksWithInitialiser() throws Exception
{
- try
- {
- _manager = PrincipalDatabaseAuthenticationManager.FACTORY.newInstance(getConfig(PlainPasswordFilePrincipalDatabase.class.getName(), "noMethod", "test"));
- fail("Exception not thrown");
- }
- catch (ConfigurationException ce)
- {
- // PASS
- }
- }
+ _principalDatabase = mock(PrincipalDatabase.class);
- /**
- * QPID-1347. Make sure the exception message and stack trace is reasonable for an absent password file.
- */
- public void testPrincipalDatabaseThrowsSetterFileNotFound() throws Exception
- {
- try
- {
- _manager = PrincipalDatabaseAuthenticationManager.FACTORY.newInstance(getConfig(PlainPasswordFilePrincipalDatabase.class.getName(), "passwordFile", "/not/found"));
- fail("Exception not thrown");
- }
- catch (ConfigurationException ce)
+ UsernamePasswordInitialiser usernamePasswordInitialiser = new UsernamePasswordInitialiser()
{
- // PASS
- assertNotNull("Expected an underlying cause", ce.getCause());
- assertEquals(FileNotFoundException.class, ce.getCause().getClass());
- }
+ @Override
+ public Class<? extends SaslServerFactory> getServerFactoryClassForJCARegistration()
+ {
+ return MySaslServerFactory.class;
+ }
+
+ @Override
+ public String getMechanismName()
+ {
+ return MOCK_MECH_NAME;
+ }
+ };
+
+ Map<String,AuthenticationProviderInitialiser> initialisers = new HashMap<String, AuthenticationProviderInitialiser>();
+ initialisers.put(MOCK_MECH_NAME, usernamePasswordInitialiser);
+
+ when(_principalDatabase.getMechanisms()).thenReturn(initialisers);
+
+ usernamePasswordInitialiser.initialise(_principalDatabase);
+
+ _manager = new PrincipalDatabaseAuthenticationManager(_principalDatabase);
+ _manager.initialise();
}
/**
@@ -153,11 +113,16 @@ public class PrincipalDatabaseAuthenticationManagerTest extends InternalBrokerBa
*/
public void testRegisteredMechanisms() throws Exception
{
+ //Ensure we haven't registered anything yet (though this would really indicate a prior test failure!)
+ Provider qpidProvider = Security.getProvider(AuthenticationManager.PROVIDER_NAME);
+ assertNull(qpidProvider);
+
+ setupMocksWithInitialiser();
+
assertNotNull(_manager.getMechanisms());
- // relies on those mechanisms attached to PropertiesPrincipalDatabaseManager
- assertEquals("AMQPLAIN PLAIN CRAM-MD5", _manager.getMechanisms());
+ assertEquals(MOCK_MECH_NAME, _manager.getMechanisms());
- Provider qpidProvider = Security.getProvider(PrincipalDatabaseAuthenticationManager.PROVIDER_NAME);
+ qpidProvider = Security.getProvider(AuthenticationManager.PROVIDER_NAME);
assertNotNull(qpidProvider);
}
@@ -167,96 +132,103 @@ public class PrincipalDatabaseAuthenticationManagerTest extends InternalBrokerBa
*/
public void testSaslMechanismCreation() throws Exception
{
- SaslServer server = _manager.createSaslServer("CRAM-MD5", "localhost", null);
+ setupMocksWithInitialiser();
+
+ SaslServer server = _manager.createSaslServer(MOCK_MECH_NAME, "localhost", null);
assertNotNull(server);
// Merely tests the creation of the mechanism. Mechanisms themselves are tested
// by their own tests.
}
-
+
/**
* Tests that the authenticate method correctly interprets an
* authentication success.
- *
+ *
*/
public void testSaslAuthenticationSuccess() throws Exception
{
+ setupMocks();
+
SaslServer testServer = createTestSaslServer(true, false);
-
+
AuthenticationResult result = _manager.authenticate(testServer, "12345".getBytes());
- final Subject subject = result.getSubject();
- assertTrue(subject.getPrincipals().contains(new UsernamePrincipal("guest")));
+
+ assertOnlyContainsWrapped(PRINCIPAL, result.getPrincipals());
assertEquals(AuthenticationStatus.SUCCESS, result.getStatus());
}
/**
- *
+ *
* Tests that the authenticate method correctly interprets an
* authentication not complete.
- *
+ *
*/
public void testSaslAuthenticationNotCompleted() throws Exception
{
+ setupMocks();
+
SaslServer testServer = createTestSaslServer(false, false);
-
+
AuthenticationResult result = _manager.authenticate(testServer, "12345".getBytes());
- assertNull(result.getSubject());
+ assertEquals("Principals was not expected size", 0, result.getPrincipals().size());
+
assertEquals(AuthenticationStatus.CONTINUE, result.getStatus());
}
/**
- *
+ *
* Tests that the authenticate method correctly interprets an
* authentication error.
- *
+ *
*/
public void testSaslAuthenticationError() throws Exception
{
+ setupMocks();
+
SaslServer testServer = createTestSaslServer(false, true);
-
+
AuthenticationResult result = _manager.authenticate(testServer, "12345".getBytes());
- assertNull(result.getSubject());
+ assertEquals("Principals was not expected size", 0, result.getPrincipals().size());
assertEquals(AuthenticationStatus.ERROR, result.getStatus());
}
- /**
- * Tests that the authenticate method correctly interprets an
- * authentication success.
- *
- */
public void testNonSaslAuthenticationSuccess() throws Exception
{
+ setupMocks();
+
+ when(_principalDatabase.verifyPassword("guest", "guest".toCharArray())).thenReturn(true);
+
AuthenticationResult result = _manager.authenticate("guest", "guest");
- final Subject subject = result.getSubject();
- assertFalse("Subject should not be set read-only", subject.isReadOnly());
- assertTrue(subject.getPrincipals().contains(new UsernamePrincipal("guest")));
+ assertOnlyContainsWrapped(PRINCIPAL, result.getPrincipals());
assertEquals(AuthenticationStatus.SUCCESS, result.getStatus());
}
- /**
- * Tests that the authenticate method correctly interprets an
- * authentication success.
- *
- */
public void testNonSaslAuthenticationNotCompleted() throws Exception
{
+ setupMocks();
+
+ when(_principalDatabase.verifyPassword("guest", "wrongpassword".toCharArray())).thenReturn(false);
+
AuthenticationResult result = _manager.authenticate("guest", "wrongpassword");
- assertNull(result.getSubject());
+ assertEquals("Principals was not expected size", 0, result.getPrincipals().size());
assertEquals(AuthenticationStatus.CONTINUE, result.getStatus());
}
-
+
/**
* Tests the ability to de-register the provider.
*/
public void testClose() throws Exception
{
- assertEquals("AMQPLAIN PLAIN CRAM-MD5", _manager.getMechanisms());
- assertNotNull(Security.getProvider(PrincipalDatabaseAuthenticationManager.PROVIDER_NAME));
+ setupMocksWithInitialiser();
+
+ assertEquals(MOCK_MECH_NAME, _manager.getMechanisms());
+ assertNotNull(Security.getProvider(AuthenticationManager.PROVIDER_NAME));
_manager.close();
// Check provider has been removed.
assertNull(_manager.getMechanisms());
- assertNull(Security.getProvider(PrincipalDatabaseAuthenticationManager.PROVIDER_NAME));
+ assertNull(Security.getProvider(AuthenticationManager.PROVIDER_NAME));
_manager = null;
}
@@ -265,94 +237,90 @@ public class PrincipalDatabaseAuthenticationManagerTest extends InternalBrokerBa
*/
private SaslServer createTestSaslServer(final boolean complete, final boolean throwSaslException)
{
- return new SaslServer()
- {
- public String getMechanismName()
- {
- return null;
- }
+ return new MySaslServer(throwSaslException, complete);
+ }
- public byte[] evaluateResponse(byte[] response) throws SaslException
- {
- if (throwSaslException)
- {
- throw new SaslException("Mocked exception");
- }
- return null;
- }
+ public static final class MySaslServer implements SaslServer
+ {
+ private final boolean _throwSaslException;
+ private final boolean _complete;
- public boolean isComplete()
- {
- return complete;
- }
+ public MySaslServer()
+ {
+ this(false, true);
+ }
- public String getAuthorizationID()
- {
- return complete ? "guest" : null;
- }
+ private MySaslServer(boolean throwSaslException, boolean complete)
+ {
+ _throwSaslException = throwSaslException;
+ _complete = complete;
+ }
- public byte[] unwrap(byte[] incoming, int offset, int len) throws SaslException
- {
- return null;
- }
+ public String getMechanismName()
+ {
+ return null;
+ }
- public byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException
+ public byte[] evaluateResponse(byte[] response) throws SaslException
+ {
+ if (_throwSaslException)
{
- return null;
+ throw new SaslException("Mocked exception");
}
+ return null;
+ }
- public Object getNegotiatedProperty(String propName)
- {
- return null;
- }
+ public boolean isComplete()
+ {
+ return _complete;
+ }
- public void dispose() throws SaslException
- {
- }
- };
- }
+ public String getAuthorizationID()
+ {
+ return _complete ? "guest" : null;
+ }
- private ConfigurationPlugin getConfig(final String clazz, final String argName, final String argValue) throws Exception
- {
- final ConfigurationPlugin config = new PrincipalDatabaseAuthenticationManager.PrincipalDatabaseAuthenticationManagerConfiguration();
+ public byte[] unwrap(byte[] incoming, int offset, int len) throws SaslException
+ {
+ return null;
+ }
- XMLConfiguration xmlconfig = new XMLConfiguration();
- xmlconfig.addProperty("pd-auth-manager.principal-database.class", clazz);
+ public byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException
+ {
+ return null;
+ }
- if (argName != null)
+ public Object getNegotiatedProperty(String propName)
{
- xmlconfig.addProperty("pd-auth-manager.principal-database.attributes.attribute.name", argName);
- xmlconfig.addProperty("pd-auth-manager.principal-database.attributes.attribute.value", argValue);
+ return null;
}
- // Create a CompositeConfiguration as this is what the broker uses
- CompositeConfiguration composite = new CompositeConfiguration();
- composite.addConfiguration(xmlconfig);
- config.setConfiguration("security", xmlconfig);
- return config;
+ public void dispose() throws SaslException
+ {
+ }
}
- private File createPasswordFile() throws Exception
+ public static class MySaslServerFactory implements SaslServerFactory
{
- BufferedWriter writer = null;
- try
- {
- File testFile = File.createTempFile(this.getClass().getName(),"tmp");
- testFile.deleteOnExit();
-
- writer = new BufferedWriter(new FileWriter(testFile));
- writer.write(TEST_USERNAME + ":" + TEST_PASSWORD);
- writer.newLine();
-
- return testFile;
-
- }
- finally
+ @Override
+ public SaslServer createSaslServer(String mechanism, String protocol,
+ String serverName, Map<String, ?> props, CallbackHandler cbh)
+ throws SaslException
{
- if (writer != null)
+ if (MOCK_MECH_NAME.equals(mechanism))
{
- writer.close();
+ return new MySaslServer();
}
+ else
+ {
+ return null;
+ }
+ }
+
+ @Override
+ public String[] getMechanismNames(Map<String, ?> props)
+ {
+ return new String[]{MOCK_MECH_NAME};
}
}
}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactoryTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactoryTest.java
new file mode 100644
index 0000000000..1424bee611
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactoryTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.qpid.server.security.auth.manager;
+
+import java.util.HashMap;
+import java.util.Map;
+
+
+import junit.framework.TestCase;
+
+public class SimpleLDAPAuthenticationManagerFactoryTest extends TestCase
+{
+ private SimpleLDAPAuthenticationManagerFactory _factory = new SimpleLDAPAuthenticationManagerFactory();
+ private Map<String, Object> _configuration = new HashMap<String, Object>();
+
+ public void testInstanceCreated() throws Exception
+ {
+ _configuration.put(SimpleLDAPAuthenticationManagerFactory.ATTRIBUTE_TYPE, SimpleLDAPAuthenticationManagerFactory.PROVIDER_TYPE);
+ _configuration.put("providerUrl", "ldaps://example.com:636/");
+ _configuration.put("searchContext", "dc=example");
+
+ AuthenticationManager manager = _factory.createInstance(_configuration);
+ assertNotNull(manager);
+ }
+
+ public void testReturnsNullWhenNoConfig() throws Exception
+ {
+ AuthenticationManager manager = _factory.createInstance(_configuration);
+ assertNull(manager);
+ }
+}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticatorTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticatorTest.java
index c0c55de92a..52b525dd80 100644
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticatorTest.java
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticatorTest.java
@@ -20,20 +20,26 @@
*/
package org.apache.qpid.server.security.auth.rmi;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
import java.security.Principal;
+import java.util.regex.Pattern;
+
+import javax.security.auth.Subject;
+
import junit.framework.TestCase;
-import org.apache.qpid.server.configuration.plugins.ConfigurationPlugin;
+import org.apache.qpid.server.model.Broker;
+import org.apache.qpid.server.security.SubjectCreator;
import org.apache.qpid.server.security.auth.AuthenticationResult;
import org.apache.qpid.server.security.auth.AuthenticationResult.AuthenticationStatus;
-import org.apache.qpid.server.security.auth.manager.AuthenticationManager;
-
-import javax.management.remote.JMXPrincipal;
-import javax.security.auth.Subject;
-import javax.security.sasl.SaslException;
-import javax.security.sasl.SaslServer;
-import java.net.InetSocketAddress;
-import java.util.Collections;
+import org.apache.qpid.server.security.auth.SubjectAuthenticationResult;
+import org.apache.qpid.server.security.SecurityManager;
/**
* Tests the RMIPasswordAuthenticator and its collaboration with the AuthenticationManager.
@@ -41,36 +47,35 @@ import java.util.Collections;
*/
public class RMIPasswordAuthenticatorTest extends TestCase
{
- private final String USERNAME = "guest";
- private final String PASSWORD = "guest";
+ private static final String USERNAME = "guest";
+ private static final String PASSWORD = "password";
+
+ private final Broker _broker = mock(Broker.class);
+ private final SecurityManager _securityManager = mock(SecurityManager.class);
+ private final Subject _loginSubject = new Subject();
+ private final String[] _credentials = new String[] {USERNAME, PASSWORD};
+
private RMIPasswordAuthenticator _rmipa;
- private String[] _credentials;
+
+ private SubjectCreator _usernamePasswordOkaySuvjectCreator = createMockSubjectCreator(true, null);
+ private SubjectCreator _badPasswordSubjectCreator = createMockSubjectCreator(false, null);
protected void setUp() throws Exception
{
- _rmipa = new RMIPasswordAuthenticator(new InetSocketAddress(5672));
-
- _credentials = new String[] {USERNAME, PASSWORD};
+ when(_broker.getSecurityManager()).thenReturn(_securityManager);
+ _rmipa = new RMIPasswordAuthenticator(_broker, new InetSocketAddress(8999));
}
/**
- * Tests a successful authentication. Ensures that a populated read-only subject it returned.
+ * Tests a successful authentication. Ensures that the expected subject is returned.
*/
public void testAuthenticationSuccess()
{
- final Subject expectedSubject = new Subject(true,
- Collections.singleton(new JMXPrincipal(USERNAME)),
- Collections.EMPTY_SET,
- Collections.EMPTY_SET);
-
- _rmipa.setAuthenticationManager(createTestAuthenticationManager(true, null));
-
+ when(_broker.getSubjectCreator(any(SocketAddress.class))).thenReturn(_usernamePasswordOkaySuvjectCreator);
+ when(_securityManager.accessManagement()).thenReturn(true);
Subject newSubject = _rmipa.authenticate(_credentials);
- assertTrue("Subject must be readonly", newSubject.isReadOnly());
- assertTrue("Returned subject does not equal expected value",
- newSubject.equals(expectedSubject));
-
+ assertSame("Subject must be unchanged", _loginSubject, newSubject);
}
/**
@@ -78,7 +83,7 @@ public class RMIPasswordAuthenticatorTest extends TestCase
*/
public void testUsernameOrPasswordInvalid()
{
- _rmipa.setAuthenticationManager(createTestAuthenticationManager(false, null));
+ when(_broker.getSubjectCreator(any(SocketAddress.class))).thenReturn(_badPasswordSubjectCreator);
try
{
@@ -89,17 +94,31 @@ public class RMIPasswordAuthenticatorTest extends TestCase
{
assertEquals("Unexpected exception message",
RMIPasswordAuthenticator.INVALID_CREDENTIALS, se.getMessage());
+ }
+ }
+
+ public void testAuthorisationFailure()
+ {
+ when(_broker.getSubjectCreator(any(SocketAddress.class))).thenReturn(_usernamePasswordOkaySuvjectCreator);
+ when(_securityManager.accessManagement()).thenReturn(false);
+ try
+ {
+ _rmipa.authenticate(_credentials);
+ fail("Exception not thrown");
+ }
+ catch (SecurityException se)
+ {
+ assertEquals("Unexpected exception message",
+ RMIPasswordAuthenticator.USER_NOT_AUTHORISED_FOR_MANAGEMENT, se.getMessage());
}
}
- /**
- * Tests case where authentication system itself fails.
- */
- public void testAuthenticationFailure()
+ public void testSubjectCreatorInternalFailure()
{
final Exception mockAuthException = new Exception("Mock Auth system failure");
- _rmipa.setAuthenticationManager(createTestAuthenticationManager(false, mockAuthException));
+ SubjectCreator subjectCreator = createMockSubjectCreator(false, mockAuthException);
+ when(_broker.getSubjectCreator(any(SocketAddress.class))).thenReturn(subjectCreator);
try
{
@@ -112,13 +131,13 @@ public class RMIPasswordAuthenticatorTest extends TestCase
}
}
-
/**
* Tests case where authentication manager is not set.
*/
- public void testNullAuthenticationManager() throws Exception
+ public void testNullSubjectCreator() throws Exception
{
- _rmipa.setAuthenticationManager(null);
+ when(_broker.getSubjectCreator(any(SocketAddress.class))).thenReturn(null);
+
try
{
_rmipa.authenticate(_credentials);
@@ -126,8 +145,7 @@ public class RMIPasswordAuthenticatorTest extends TestCase
}
catch (SecurityException se)
{
- assertEquals("Unexpected exception message",
- RMIPasswordAuthenticator.UNABLE_TO_LOOKUP, se.getMessage());
+ assertTrue("Unexpected exception message", Pattern.matches("Can't get subject creator for .*:8999", se.getMessage()));
}
}
@@ -155,11 +173,13 @@ public class RMIPasswordAuthenticatorTest extends TestCase
*/
public void testWithIllegalNumberOfArguments()
{
+ String[] credentials;
+
// Test handling of incorrect number of credentials
try
{
- _credentials = new String[]{USERNAME, PASSWORD, PASSWORD};
- _rmipa.authenticate(_credentials);
+ credentials = new String[]{USERNAME, PASSWORD, PASSWORD};
+ _rmipa.authenticate(credentials);
fail("SecurityException expected due to supplying wrong number of credentials");
}
catch (SecurityException se)
@@ -172,8 +192,8 @@ public class RMIPasswordAuthenticatorTest extends TestCase
try
{
//send a null array
- _credentials = null;
- _rmipa.authenticate(_credentials);
+ credentials = null;
+ _rmipa.authenticate(credentials);
fail("SecurityException expected due to not supplying an array of credentials");
}
catch (SecurityException se)
@@ -185,8 +205,8 @@ public class RMIPasswordAuthenticatorTest extends TestCase
try
{
//send a null password
- _credentials = new String[]{USERNAME, null};
- _rmipa.authenticate(_credentials);
+ credentials = new String[]{USERNAME, null};
+ _rmipa.authenticate(credentials);
fail("SecurityException expected due to sending a null password");
}
catch (SecurityException se)
@@ -198,8 +218,8 @@ public class RMIPasswordAuthenticatorTest extends TestCase
try
{
//send a null username
- _credentials = new String[]{null, PASSWORD};
- _rmipa.authenticate(_credentials);
+ credentials = new String[]{null, PASSWORD};
+ _rmipa.authenticate(credentials);
fail("SecurityException expected due to sending a null username");
}
catch (SecurityException se)
@@ -209,55 +229,30 @@ public class RMIPasswordAuthenticatorTest extends TestCase
}
}
- private AuthenticationManager createTestAuthenticationManager(final boolean successfulAuth, final Exception exception)
+ private SubjectCreator createMockSubjectCreator(final boolean successfulAuth, final Exception exception)
{
- return new AuthenticationManager()
+ SubjectCreator subjectCreator = mock(SubjectCreator.class);
+
+ SubjectAuthenticationResult subjectAuthenticationResult;
+
+ if (exception != null) {
+
+ subjectAuthenticationResult = new SubjectAuthenticationResult(
+ new AuthenticationResult(AuthenticationStatus.ERROR, exception));
+ }
+ else if (successfulAuth)
{
- public void configure(ConfigurationPlugin config)
- {
- throw new UnsupportedOperationException();
- }
-
- public void initialise()
- {
- throw new UnsupportedOperationException();
- }
-
- public void close()
- {
- throw new UnsupportedOperationException();
- }
-
- public String getMechanisms()
- {
- throw new UnsupportedOperationException();
- }
-
- public SaslServer createSaslServer(String mechanism, String localFQDN, Principal externalPrincipal) throws SaslException
- {
- throw new UnsupportedOperationException();
- }
-
- public AuthenticationResult authenticate(SaslServer server, byte[] response)
- {
- throw new UnsupportedOperationException();
- }
-
- public AuthenticationResult authenticate(String username, String password)
- {
- if (exception != null) {
- return new AuthenticationResult(AuthenticationStatus.ERROR, exception);
- }
- else if (successfulAuth)
- {
- return new AuthenticationResult(new Subject());
- }
- else
- {
- return new AuthenticationResult(AuthenticationStatus.CONTINUE);
- }
- }
-
- };
+
+ subjectAuthenticationResult = new SubjectAuthenticationResult(
+ new AuthenticationResult(mock(Principal.class)), _loginSubject);
+ }
+ else
+ {
+ subjectAuthenticationResult = new SubjectAuthenticationResult(new AuthenticationResult(AuthenticationStatus.CONTINUE));
+ }
+
+ when(subjectCreator.authenticate(anyString(), anyString())).thenReturn(subjectAuthenticationResult);
+
+ return subjectCreator;
}
}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/TestPrincipalDatabase.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/TestPrincipalDatabase.java
index 8c7f3ad6ef..f94d8ddfc3 100644
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/TestPrincipalDatabase.java
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/TestPrincipalDatabase.java
@@ -86,4 +86,10 @@ public class TestPrincipalDatabase implements PrincipalDatabase
// TODO Auto-generated method stub
}
+ @Override
+ public void setPasswordFile(String passwordFile) throws IOException
+ {
+ // TODO Auto-generated method stub
+ }
+
}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupDatabaseTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupDatabaseTest.java
new file mode 100644
index 0000000000..b020c1655a
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupDatabaseTest.java
@@ -0,0 +1,456 @@
+/*
+ * 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.qpid.server.security.group;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.qpid.server.security.group.FileGroupDatabase;
+
+import junit.framework.TestCase;
+
+public class FileGroupDatabaseTest extends TestCase
+{
+ private static final String USER1 = "user1";
+ private static final String USER2 = "user2";
+ private static final String USER3 = "user3";
+
+ private static final String MY_GROUP = "myGroup";
+ private static final String MY_GROUP2 = "myGroup2";
+ private static final String MY_GROUP1 = "myGroup1";
+
+ private FileGroupDatabase _groupDatabase = new FileGroupDatabase();
+ private String _groupFile;
+
+ public void testGetAllGroups() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", USER1);
+
+ Set<String> groups = _groupDatabase.getAllGroups();
+ assertEquals(1, groups.size());
+ assertTrue(groups.contains(MY_GROUP));
+ }
+
+ public void testGetAllGroupsWhenGroupFileEmpty() throws Exception
+ {
+ _groupDatabase.setGroupFile(_groupFile);
+
+ Set<String> groups = _groupDatabase.getAllGroups();
+ assertEquals(0, groups.size());
+ }
+
+ public void testMissingGroupFile() throws Exception
+ {
+ try
+ {
+ _groupDatabase.setGroupFile("/not/a/file");
+ fail("Exception not thrown");
+ }
+ catch (FileNotFoundException fnfe)
+ {
+ // PASS
+ }
+ }
+
+ public void testInvalidFormat() throws Exception
+ {
+ writeGroupFile("name.notvalid", USER1);
+
+ try
+ {
+ _groupDatabase.setGroupFile(_groupFile);
+ fail("Exception not thrown");
+ }
+ catch (IllegalArgumentException gde)
+ {
+ // PASS
+ }
+ }
+
+ public void testGetUsersInGroup() throws Exception
+ {
+ writeGroupFile("myGroup.users", "user1,user2,user3");
+
+ _groupDatabase.setGroupFile(_groupFile);
+
+ Set<String> users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertNotNull(users);
+ assertEquals(3, users.size());
+ }
+
+ public void testDuplicateUsersInGroupAreConflated() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", "user1,user1,user3,user1");
+
+ Set<String> users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertNotNull(users);
+ assertEquals(2, users.size());
+ }
+
+ public void testGetUsersWithEmptyGroup() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", "");
+
+ Set<String> users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertNotNull(users);
+ assertTrue(users.isEmpty());
+ }
+
+ public void testGetUsersInNonExistentGroup() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", "user1,user2,user3");
+
+ Set<String> users = _groupDatabase.getUsersInGroup("groupDoesntExist");
+ assertNotNull(users);
+ assertTrue(users.isEmpty());
+ }
+
+ public void testGetUsersInNullGroup() throws Exception
+ {
+ writeAndSetGroupFile();
+ assertTrue(_groupDatabase.getUsersInGroup(null).isEmpty());
+ }
+
+ public void testGetGroupPrincipalsForUserWhenUserBelongsToOneGroup() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", "user1,user2");
+ Set<String> groups = _groupDatabase.getGroupsForUser(USER1);
+ assertEquals(1, groups.size());
+ assertTrue(groups.contains(MY_GROUP));
+ }
+
+ public void testGetGroupPrincipalsForUserWhenUserBelongsToTwoGroup() throws Exception
+ {
+ writeAndSetGroupFile("myGroup1.users", "user1,user2",
+ "myGroup2.users", "user1,user3");
+ Set<String> groups = _groupDatabase.getGroupsForUser(USER1);
+ assertEquals(2, groups.size());
+ assertTrue(groups.contains(MY_GROUP1));
+ assertTrue(groups.contains(MY_GROUP2));
+ }
+
+ public void testGetGroupPrincipalsForUserWhenUserAddedToGroup() throws Exception
+ {
+ writeAndSetGroupFile("myGroup1.users", "user1,user2",
+ "myGroup2.users", USER2);
+ Set<String> groups = _groupDatabase.getGroupsForUser(USER1);
+ assertEquals(1, groups.size());
+ assertTrue(groups.contains(MY_GROUP1));
+
+ _groupDatabase.addUserToGroup(USER1, MY_GROUP2);
+
+ groups = _groupDatabase.getGroupsForUser(USER1);
+ assertEquals(2, groups.size());
+ assertTrue(groups.contains(MY_GROUP1));
+ assertTrue(groups.contains(MY_GROUP2));
+
+ Set<String> users = _groupDatabase.getUsersInGroup(MY_GROUP2);
+ assertEquals(2, users.size());
+ assertTrue(users.contains(USER1));
+ assertTrue(users.contains(USER2));
+ }
+
+ public void testGetGroupPrincipalsForUserWhenUserRemovedFromGroup() throws Exception
+ {
+ writeAndSetGroupFile("myGroup1.users", "user1,user2",
+ "myGroup2.users", "user1,user2");
+ Set<String> groups = _groupDatabase.getGroupsForUser(USER1);
+ assertEquals(2, groups.size());
+ assertTrue(groups.contains(MY_GROUP1));
+ assertTrue(groups.contains(MY_GROUP2));
+
+ _groupDatabase.removeUserFromGroup(USER1, MY_GROUP2);
+
+ groups = _groupDatabase.getGroupsForUser(USER1);
+ assertEquals(1, groups.size());
+ assertTrue(groups.contains(MY_GROUP1));
+ }
+
+ public void testGetGroupPrincipalsForUserWhenUserAdddedToGroupTheyAreAlreadyIn() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", USER1);
+ _groupDatabase.addUserToGroup(USER1, MY_GROUP);
+
+ Set<String> groups = _groupDatabase.getGroupsForUser(USER1);
+
+ assertEquals(1, groups.size());
+ assertTrue(groups.contains(MY_GROUP));
+
+ Set<String> users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertEquals(1, users.size());
+ assertTrue(users.contains(USER1));
+ }
+
+ public void testGetGroupPrincipalsForUserWhenUserNotKnown() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", "user1,user2");
+ Set<String> groups = _groupDatabase.getGroupsForUser(USER3);
+ assertEquals(0, groups.size());
+ }
+
+ public void testGetGroupPrincipalsForNullUser() throws Exception
+ {
+ writeAndSetGroupFile();
+ assertTrue(_groupDatabase.getGroupsForUser(null).isEmpty());
+ }
+
+ public void testAddUserToExistingGroup() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", "user1,user2");
+
+ Set<String> users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertNotNull(users);
+ assertEquals(2, users.size());
+
+ _groupDatabase.addUserToGroup(USER3, MY_GROUP);
+
+ users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertNotNull(users);
+ assertEquals(3, users.size());
+ }
+
+ public void testAddUserToEmptyGroup() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", "");
+
+ Set<String> users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertNotNull(users);
+ assertEquals(0, users.size());
+
+ _groupDatabase.addUserToGroup(USER3, MY_GROUP);
+
+ users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertNotNull(users);
+ assertEquals(1, users.size());
+ }
+
+ public void testAddUserToNonExistentGroup() throws Exception
+ {
+ writeAndSetGroupFile();
+
+ Set<String> users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertNotNull(users);
+ assertEquals(0, users.size());
+
+ try
+ {
+ _groupDatabase.addUserToGroup(USER3, MY_GROUP);
+ fail("Expected exception not thrown");
+ }
+ catch(IllegalArgumentException e)
+ {
+ // pass
+ }
+
+ users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertNotNull(users);
+ assertEquals(0, users.size());
+ }
+
+ public void testRemoveUserFromExistingGroup() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", "user1,user2");
+
+ Set<String> users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertNotNull(users);
+ assertEquals(2, users.size());
+
+ _groupDatabase.removeUserFromGroup(USER2, MY_GROUP);
+
+ users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertNotNull(users);
+ assertEquals(1, users.size());
+ }
+
+ public void testRemoveUserFromNonexistentGroup() throws Exception
+ {
+ writeAndSetGroupFile();
+
+ try
+ {
+ _groupDatabase.removeUserFromGroup(USER1, MY_GROUP);
+ fail("Expected exception not thrown");
+ }
+ catch(IllegalArgumentException e)
+ {
+ // pass
+ }
+
+ assertTrue(_groupDatabase.getUsersInGroup(MY_GROUP).isEmpty());
+ }
+
+ public void testRemoveUserFromGroupTwice() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", USER1);
+ assertTrue(_groupDatabase.getUsersInGroup(MY_GROUP).contains(USER1));
+
+ _groupDatabase.removeUserFromGroup(USER1, MY_GROUP);
+ assertTrue(_groupDatabase.getUsersInGroup(MY_GROUP).isEmpty());
+
+ _groupDatabase.removeUserFromGroup(USER1, MY_GROUP);
+ assertTrue(_groupDatabase.getUsersInGroup(MY_GROUP).isEmpty());
+ }
+
+ public void testAddUserPersistedToFile() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", "user1,user2");
+
+ Set<String> users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertEquals(2, users.size());
+
+ _groupDatabase.addUserToGroup(USER3, MY_GROUP);
+ assertEquals(3, users.size());
+
+ FileGroupDatabase newGroupDatabase = new FileGroupDatabase();
+ newGroupDatabase.setGroupFile(_groupFile);
+
+ Set<String> newUsers = newGroupDatabase.getUsersInGroup(MY_GROUP);
+ assertEquals(users.size(), newUsers.size());
+ }
+
+ public void testRemoveUserPersistedToFile() throws Exception
+ {
+ writeAndSetGroupFile("myGroup.users", "user1,user2");
+
+ Set<String> users = _groupDatabase.getUsersInGroup(MY_GROUP);
+ assertEquals(2, users.size());
+
+ _groupDatabase.removeUserFromGroup(USER2, MY_GROUP);
+ assertEquals(1, users.size());
+
+ FileGroupDatabase newGroupDatabase = new FileGroupDatabase();
+ newGroupDatabase.setGroupFile(_groupFile);
+
+ Set<String> newUsers = newGroupDatabase.getUsersInGroup(MY_GROUP);
+ assertEquals(users.size(), newUsers.size());
+ }
+
+ public void testCreateGroupPersistedToFile() throws Exception
+ {
+ writeAndSetGroupFile();
+
+ Set<String> groups = _groupDatabase.getAllGroups();
+ assertEquals(0, groups.size());
+
+ _groupDatabase.createGroup(MY_GROUP);
+
+ groups = _groupDatabase.getAllGroups();
+ assertEquals(1, groups.size());
+ assertTrue(groups.contains(MY_GROUP));
+
+ FileGroupDatabase newGroupDatabase = new FileGroupDatabase();
+ newGroupDatabase.setGroupFile(_groupFile);
+
+ Set<String> newGroups = newGroupDatabase.getAllGroups();
+ assertEquals(1, newGroups.size());
+ assertTrue(newGroups.contains(MY_GROUP));
+ }
+
+ public void testRemoveGroupPersistedToFile() throws Exception
+ {
+ writeAndSetGroupFile("myGroup1.users", "user1,user2",
+ "myGroup2.users", "user1,user2");
+
+ Set<String> groups = _groupDatabase.getAllGroups();
+ assertEquals(2, groups.size());
+
+ Set<String> groupsForUser1 = _groupDatabase.getGroupsForUser(USER1);
+ assertEquals(2, groupsForUser1.size());
+
+ _groupDatabase.removeGroup(MY_GROUP1);
+
+ groups = _groupDatabase.getAllGroups();
+ assertEquals(1, groups.size());
+ assertTrue(groups.contains(MY_GROUP2));
+
+ groupsForUser1 = _groupDatabase.getGroupsForUser(USER1);
+ assertEquals(1, groupsForUser1.size());
+
+ FileGroupDatabase newGroupDatabase = new FileGroupDatabase();
+ newGroupDatabase.setGroupFile(_groupFile);
+
+ Set<String> newGroups = newGroupDatabase.getAllGroups();
+ assertEquals(1, newGroups.size());
+ assertTrue(newGroups.contains(MY_GROUP2));
+
+ Set<String> newGroupsForUser1 = newGroupDatabase.getGroupsForUser(USER1);
+ assertEquals(1, newGroupsForUser1.size());
+ assertTrue(newGroupsForUser1.contains(MY_GROUP2));
+}
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ _groupFile = createEmptyTestGroupFile();
+ }
+
+ private void writeAndSetGroupFile(String... groupAndUsers) throws Exception
+ {
+ writeGroupFile(groupAndUsers);
+ _groupDatabase.setGroupFile(_groupFile);
+ }
+
+ private void writeGroupFile(String... groupAndUsers) throws Exception
+ {
+ if (groupAndUsers.length % 2 != 0)
+ {
+ throw new IllegalArgumentException("Number of groupAndUsers must be even");
+ }
+
+ Properties props = new Properties();
+ for (int i = 0 ; i < groupAndUsers.length; i=i+2)
+ {
+ String group = groupAndUsers[i];
+ String users = groupAndUsers[i+1];
+ props.put(group, users);
+ }
+
+ props.store(new FileOutputStream(_groupFile), "test group file");
+ }
+
+ private String createEmptyTestGroupFile() throws IOException
+ {
+ File tmpGroupFile = File.createTempFile("groups", "grp");
+ tmpGroupFile.deleteOnExit();
+
+ return tmpGroupFile.getAbsolutePath();
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+
+ if (_groupFile != null)
+ {
+ File groupFile = new File(_groupFile);
+ if (groupFile.exists())
+ {
+ groupFile.delete();
+ }
+ }
+ }
+
+}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerFactoryTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerFactoryTest.java
new file mode 100644
index 0000000000..934c0082ea
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerFactoryTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.qpid.server.security.group;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.qpid.server.model.GroupProvider;
+import org.apache.qpid.test.utils.TestFileUtils;
+
+public class FileGroupManagerFactoryTest extends TestCase
+{
+
+ private FileGroupManagerFactory _factory = new FileGroupManagerFactory();
+ private Map<String, Object> _configuration = new HashMap<String, Object>();
+ private String _emptyButValidGroupFile = TestFileUtils.createTempFile(this).getAbsolutePath();
+
+ public void testInstanceCreated() throws Exception
+ {
+ _configuration.put(GroupProvider.TYPE, FileGroupManagerFactory.FILE_GROUP_MANAGER_TYPE);
+ _configuration.put(FileGroupManagerFactory.FILE, _emptyButValidGroupFile);
+
+ GroupManager manager = _factory.createInstance(_configuration);
+ assertNotNull(manager);
+ assertTrue(manager instanceof FileGroupManager);
+ }
+
+ public void testReturnsNullWhenNoConfig() throws Exception
+ {
+ GroupManager manager = _factory.createInstance(_configuration);
+ assertNull(manager);
+ }
+
+ public void testReturnsNullWhenConfigNotForThisPlugin() throws Exception
+ {
+ _configuration.put(GroupProvider.TYPE, "other-group-manager");
+
+ GroupManager manager = _factory.createInstance(_configuration);
+ assertNull(manager);
+ }
+
+
+ public void testRejectsConfigThatIsMissingAttributeValue() throws Exception
+ {
+ _configuration.put(GroupProvider.TYPE, FileGroupManagerFactory.FILE_GROUP_MANAGER_TYPE);
+ _configuration.put(FileGroupManagerFactory.FILE, null);
+
+ try
+ {
+ _factory.createInstance(_configuration);
+ fail("Exception not thrown");
+ }
+ catch (RuntimeException re)
+ {
+ // PASS
+ }
+ }
+
+}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerTest.java
new file mode 100644
index 0000000000..b83d25b206
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerTest.java
@@ -0,0 +1,197 @@
+/*
+ * 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.qpid.server.security.group;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.security.Principal;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.qpid.server.configuration.IllegalConfigurationException;
+import org.apache.qpid.server.security.auth.UsernamePrincipal;
+import org.apache.qpid.test.utils.QpidTestCase;
+
+public class FileGroupManagerTest extends QpidTestCase
+{
+ private static final String MYGROUP_USERS = "user1";
+ private static final String MY_GROUP = "myGroup.users";
+ private static final String MY_GROUP2 = "myGroup2.users";
+ private File _tmpGroupFile;
+ private FileGroupManager _manager;
+
+ @Override
+ public void tearDown() throws Exception
+ {
+ super.tearDown();
+
+ if (_tmpGroupFile != null)
+ {
+ if (_tmpGroupFile.exists())
+ {
+ _tmpGroupFile.delete();
+ }
+ }
+ }
+
+ public void testValidGroupFile() throws Exception
+ {
+ final String groupFileName = writeGroupFile();
+
+ _manager = new FileGroupManager(groupFileName);
+ assertNotNull(_manager);
+ }
+
+ public void testNonExistentGroupFile() throws Exception
+ {
+ final String filePath = "/does.not.exist/";
+
+ try
+ {
+ _manager = new FileGroupManager(filePath);
+ fail("expected exception was not thrown");
+ }
+ catch(IllegalConfigurationException ce)
+ {
+ assertNotNull(ce.getCause());
+ assertTrue(ce.getCause() instanceof FileNotFoundException);
+ }
+ }
+
+ public void testGetGroupPrincipalsForUser() throws Exception
+ {
+ final String groupFileName = writeGroupFile();
+ _manager = new FileGroupManager(groupFileName);
+
+ Set<Principal> principals = _manager.getGroupPrincipalsForUser("user1");
+ assertEquals(1, principals.size());
+ assertTrue(principals.contains(new GroupPrincipal("myGroup")));
+ }
+
+ public void testGetUserPrincipalsForGroup() throws Exception
+ {
+ final String groupFileName = writeGroupFile();
+ _manager = new FileGroupManager(groupFileName);
+
+ Set<Principal> principals = _manager.getUserPrincipalsForGroup("myGroup");
+ assertEquals(1, principals.size());
+ assertTrue(principals.contains(new UsernamePrincipal("user1")));
+ }
+
+ public void testGetGroupPrincipals() throws Exception
+ {
+ final String groupFileName = writeGroupFile(MY_GROUP, MYGROUP_USERS, MY_GROUP2, MYGROUP_USERS);
+ _manager = new FileGroupManager(groupFileName);
+
+ Set<Principal> principals = _manager.getGroupPrincipals();
+ assertEquals(2, principals.size());
+ assertTrue(principals.contains(new GroupPrincipal("myGroup")));
+ assertTrue(principals.contains(new GroupPrincipal("myGroup2")));
+ }
+
+ public void testCreateGroup() throws Exception
+ {
+ final String groupFileName = writeGroupFile();
+ _manager = new FileGroupManager(groupFileName);
+
+ Set<Principal> principals = _manager.getGroupPrincipals();
+ assertEquals(1, principals.size());
+
+ _manager.createGroup("myGroup2");
+
+ principals = _manager.getGroupPrincipals();
+ assertEquals(2, principals.size());
+ assertTrue(principals.contains(new GroupPrincipal("myGroup2")));
+ }
+
+ public void testRemoveGroup() throws Exception
+ {
+ final String groupFileName = writeGroupFile(MY_GROUP, MYGROUP_USERS);
+ _manager = new FileGroupManager(groupFileName);
+
+ Set<Principal> principals = _manager.getGroupPrincipals();
+ assertEquals(1, principals.size());
+
+ _manager.removeGroup("myGroup");
+
+ principals = _manager.getGroupPrincipals();
+ assertEquals(0, principals.size());
+ }
+
+ public void testAddUserToGroup() throws Exception
+ {
+ final String groupFileName = writeGroupFile(MY_GROUP, MYGROUP_USERS);
+ _manager = new FileGroupManager(groupFileName);
+
+ Set<Principal> principals = _manager.getUserPrincipalsForGroup("myGroup");
+ assertEquals(1, principals.size());
+ assertFalse(principals.contains(new UsernamePrincipal("user2")));
+
+ _manager.addUserToGroup("user2", "myGroup");
+
+ principals = _manager.getUserPrincipalsForGroup("myGroup");
+ assertEquals(2, principals.size());
+ assertTrue(principals.contains(new UsernamePrincipal("user2")));
+ }
+
+ public void testRemoveUserInGroup() throws Exception
+ {
+ final String groupFileName = writeGroupFile(MY_GROUP, MYGROUP_USERS);
+ _manager = new FileGroupManager(groupFileName);
+
+ Set<Principal> principals = _manager.getUserPrincipalsForGroup("myGroup");
+ assertEquals(1, principals.size());
+ assertTrue(principals.contains(new UsernamePrincipal("user1")));
+
+ _manager.removeUserFromGroup("user1", "myGroup");
+
+ principals = _manager.getUserPrincipalsForGroup("myGroup");
+ assertEquals(0, principals.size());
+ }
+
+ private String writeGroupFile() throws Exception
+ {
+ return writeGroupFile(MY_GROUP, MYGROUP_USERS);
+ }
+
+ private String writeGroupFile(String... groupAndUsers) throws Exception
+ {
+ if (groupAndUsers.length % 2 != 0)
+ {
+ throw new IllegalArgumentException("Number of groupAndUsers must be even");
+ }
+
+ _tmpGroupFile = File.createTempFile("groups", "grp");
+ _tmpGroupFile.deleteOnExit();
+
+ Properties props = new Properties();
+ for (int i = 0 ; i < groupAndUsers.length; i=i+2)
+ {
+ String group = groupAndUsers[i];
+ String users = groupAndUsers[i+1];
+ props.put(group, users);
+ }
+
+ props.store(new FileOutputStream(_tmpGroupFile), "test group file");
+
+ return _tmpGroupFile.getCanonicalPath();
+ }
+}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/group/GroupPrincipalAccessorTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/group/GroupPrincipalAccessorTest.java
new file mode 100644
index 0000000000..e58a1a01f8
--- /dev/null
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/group/GroupPrincipalAccessorTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.qpid.server.security.group;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.security.Principal;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.apache.qpid.server.model.GroupProvider;
+
+public class GroupPrincipalAccessorTest extends TestCase
+{
+ private static final String USERNAME = "username";
+
+ private GroupProvider _groupManager1 = mock(GroupProvider.class);
+ private GroupProvider _groupManager2 = mock(GroupProvider.class);
+
+ private Principal _group1 = mock(Principal.class);
+ private Principal _group2 = mock(Principal.class);
+
+ @Override
+ public void setUp()
+ {
+ when(_groupManager1.getGroupPrincipalsForUser(USERNAME)).thenReturn(Collections.singleton(_group1));
+ when(_groupManager2.getGroupPrincipalsForUser(USERNAME)).thenReturn(Collections.singleton(_group2));
+ }
+
+ public void testGetGroupPrincipals()
+ {
+ getAndAssertGroupPrincipals(_group1, _group2);
+ }
+
+ public void testGetGroupPrincipalsWhenAGroupManagerReturnsNull()
+ {
+ when(_groupManager1.getGroupPrincipalsForUser(USERNAME)).thenReturn(null);
+
+ getAndAssertGroupPrincipals(_group2);
+ }
+
+ public void testGetGroupPrincipalsWhenAGroupManagerReturnsEmptySet()
+ {
+ when(_groupManager2.getGroupPrincipalsForUser(USERNAME)).thenReturn(new HashSet<Principal>());
+
+ getAndAssertGroupPrincipals(_group1);
+ }
+
+ private void getAndAssertGroupPrincipals(Principal... expectedGroups)
+ {
+ GroupPrincipalAccessor groupPrincipalAccessor = new GroupPrincipalAccessor(Arrays.asList(_groupManager1, _groupManager2));
+
+ Set<Principal> actualGroupPrincipals = groupPrincipalAccessor.getGroupPrincipals(USERNAME);
+
+ Set<Principal> expectedGroupPrincipals = new HashSet<Principal>(Arrays.asList(expectedGroups));
+
+ assertEquals(expectedGroupPrincipals, actualGroupPrincipals);
+ }
+}
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/GroupPrincipalTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/group/GroupPrincipalTest.java
index 076b7c9248..d285a0797a 100644
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/sasl/GroupPrincipalTest.java
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/group/GroupPrincipalTest.java
@@ -7,9 +7,9 @@
* 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
@@ -18,7 +18,9 @@
* under the License.
*
*/
-package org.apache.qpid.server.security.auth.sasl;
+package org.apache.qpid.server.security.group;
+
+import org.apache.qpid.server.security.auth.UsernamePrincipal;
import junit.framework.TestCase;
@@ -34,7 +36,7 @@ public class GroupPrincipalTest extends TestCase
{
final GroupPrincipal principal = new GroupPrincipal("group");
final UsernamePrincipal user = new UsernamePrincipal("name");
-
+
try
{
principal.addMember(user);
@@ -45,7 +47,7 @@ public class GroupPrincipalTest extends TestCase
// PASS
}
}
-
+
public void testEqualitySameName()
{
final String string = "string";
@@ -80,7 +82,7 @@ public class GroupPrincipalTest extends TestCase
assertFalse(principal.equals(null));
}
-
+
}