Search in sources :

Example 1 with UserPrincipal

use of org.apache.activemq.jaas.UserPrincipal in project activemq-artemis by apache.

the class JaasDualAuthenticationBrokerTest method testInsecureConnector.

public void testInsecureConnector() {
    Connector connector = new TransportConnector(nonSslTransportServer);
    connectionContext.setConnector(connector);
    connectionInfo.setUserName(INSECURE_USERNAME);
    try {
        authBroker.addConnection(connectionContext, connectionInfo);
    } catch (Exception e) {
        fail("Call to addConnection failed: " + e.getMessage());
    }
    assertEquals("Number of addConnection calls to underlying Broker must match number of calls made to " + "AuthenticationBroker.", 1, receiveBroker.addConnectionData.size());
    ConnectionContext receivedContext = receiveBroker.addConnectionData.getFirst().connectionContext;
    assertEquals("The SecurityContext's userName must be set to that of the UserPrincipal.", INSECURE_USERNAME, receivedContext.getSecurityContext().getUserName());
    Set<Principal> receivedPrincipals = receivedContext.getSecurityContext().getPrincipals();
    assertEquals("2 Principals received", 2, receivedPrincipals.size());
    for (Iterator<Principal> iter = receivedPrincipals.iterator(); iter.hasNext(); ) {
        Principal currentPrincipal = iter.next();
        if (currentPrincipal instanceof UserPrincipal) {
            assertEquals("UserPrincipal is '" + INSECURE_USERNAME + "'", INSECURE_USERNAME, currentPrincipal.getName());
        } else if (currentPrincipal instanceof GroupPrincipal) {
            assertEquals("GroupPrincipal is '" + INSECURE_GROUP + "'", INSECURE_GROUP, currentPrincipal.getName());
        } else {
            fail("Unexpected Principal subclass found.");
        }
    }
    try {
        authBroker.removeConnection(connectionContext, connectionInfo, null);
    } catch (Exception e) {
        fail("Call to removeConnection failed: " + e.getMessage());
    }
    assertEquals("Number of removeConnection calls to underlying Broker must match number of calls made to " + "AuthenticationBroker.", 1, receiveBroker.removeConnectionData.size());
}
Also used : Connector(org.apache.activemq.broker.Connector) TransportConnector(org.apache.activemq.broker.TransportConnector) TransportConnector(org.apache.activemq.broker.TransportConnector) GroupPrincipal(org.apache.activemq.jaas.GroupPrincipal) ConnectionContext(org.apache.activemq.broker.ConnectionContext) UserPrincipal(org.apache.activemq.jaas.UserPrincipal) Principal(java.security.Principal) GroupPrincipal(org.apache.activemq.jaas.GroupPrincipal) UserPrincipal(org.apache.activemq.jaas.UserPrincipal)

Example 2 with UserPrincipal

use of org.apache.activemq.jaas.UserPrincipal in project activemq-artemis by apache.

the class JaasDualAuthenticationBrokerTest method testSecureConnector.

public void testSecureConnector() {
    Connector connector = new TransportConnector(sslTransportServer);
    connectionContext.setConnector(connector);
    connectionInfo.setTransportContext(new StubX509Certificate[] {});
    try {
        authBroker.addConnection(connectionContext, connectionInfo);
    } catch (Exception e) {
        fail("Call to addConnection failed: " + e.getMessage());
    }
    assertEquals("Number of addConnection calls to underlying Broker must match number of calls made to " + "AuthenticationBroker.", 1, receiveBroker.addConnectionData.size());
    ConnectionContext receivedContext = receiveBroker.addConnectionData.getFirst().connectionContext;
    assertEquals("The SecurityContext's userName must be set to that of the UserPrincipal.", DN_USERNAME, receivedContext.getSecurityContext().getUserName());
    Set<Principal> receivedPrincipals = receivedContext.getSecurityContext().getPrincipals();
    assertEquals("2 Principals received", 2, receivedPrincipals.size());
    for (Iterator<Principal> iter = receivedPrincipals.iterator(); iter.hasNext(); ) {
        Principal currentPrincipal = iter.next();
        if (currentPrincipal instanceof UserPrincipal) {
            assertEquals("UserPrincipal is '" + DN_USERNAME + "'", DN_USERNAME, currentPrincipal.getName());
        } else if (currentPrincipal instanceof GroupPrincipal) {
            assertEquals("GroupPrincipal is '" + DN_GROUP + "'", DN_GROUP, currentPrincipal.getName());
        } else {
            fail("Unexpected Principal subclass found.");
        }
    }
    try {
        authBroker.removeConnection(connectionContext, connectionInfo, null);
    } catch (Exception e) {
        fail("Call to removeConnection failed: " + e.getMessage());
    }
    assertEquals("Number of removeConnection calls to underlying Broker must match number of calls made to " + "AuthenticationBroker.", 1, receiveBroker.removeConnectionData.size());
}
Also used : Connector(org.apache.activemq.broker.Connector) TransportConnector(org.apache.activemq.broker.TransportConnector) TransportConnector(org.apache.activemq.broker.TransportConnector) GroupPrincipal(org.apache.activemq.jaas.GroupPrincipal) ConnectionContext(org.apache.activemq.broker.ConnectionContext) UserPrincipal(org.apache.activemq.jaas.UserPrincipal) Principal(java.security.Principal) GroupPrincipal(org.apache.activemq.jaas.GroupPrincipal) UserPrincipal(org.apache.activemq.jaas.UserPrincipal)

Example 3 with UserPrincipal

use of org.apache.activemq.jaas.UserPrincipal in project activemq-artemis by apache.

the class JaasCertificateAuthenticationBrokerTest method testAddConnectionSuccess.

public void testAddConnectionSuccess() {
    String dnUserName = "dnUserName";
    HashSet<String> userNames = new HashSet<>();
    userNames.add(dnUserName);
    HashSet<String> groupNames = new HashSet<>();
    groupNames.add("testGroup1");
    groupNames.add("testGroup2");
    groupNames.add("tesetGroup3");
    setConfiguration(userNames, groupNames, true);
    try {
        authBroker.addConnection(connectionContext, connectionInfo);
    } catch (Exception e) {
        fail("Call to addConnection failed: " + e.getMessage());
    }
    assertEquals("Number of addConnection calls to underlying Broker must match number of calls made to " + "AuthenticationBroker.", 1, receiveBroker.addConnectionData.size());
    ConnectionContext receivedContext = receiveBroker.addConnectionData.getFirst().connectionContext;
    assertEquals("The SecurityContext's userName must be set to that of the UserPrincipal.", dnUserName, receivedContext.getSecurityContext().getUserName());
    Set<Principal> receivedPrincipals = receivedContext.getSecurityContext().getPrincipals();
    for (Iterator<Principal> iter = receivedPrincipals.iterator(); iter.hasNext(); ) {
        Principal currentPrincipal = iter.next();
        if (currentPrincipal instanceof UserPrincipal) {
            if (userNames.remove(currentPrincipal.getName())) {
            // Nothing, we did good.
            } else {
                // Found an unknown userName.
                fail("Unknown UserPrincipal found");
            }
        } else if (currentPrincipal instanceof GroupPrincipal) {
            if (groupNames.remove(currentPrincipal.getName())) {
            // Nothing, we did good.
            } else {
                fail("Unknown GroupPrincipal found.");
            }
        } else {
            fail("Unexpected Principal subclass found.");
        }
    }
    if (!userNames.isEmpty()) {
        fail("Some usernames were not added as UserPrincipals");
    }
    if (!groupNames.isEmpty()) {
        fail("Some group names were not added as GroupPrincipals");
    }
}
Also used : GroupPrincipal(org.apache.activemq.jaas.GroupPrincipal) ConnectionContext(org.apache.activemq.broker.ConnectionContext) UserPrincipal(org.apache.activemq.jaas.UserPrincipal) Principal(java.security.Principal) GroupPrincipal(org.apache.activemq.jaas.GroupPrincipal) UserPrincipal(org.apache.activemq.jaas.UserPrincipal) HashSet(java.util.HashSet)

Aggregations

Principal (java.security.Principal)3 ConnectionContext (org.apache.activemq.broker.ConnectionContext)3 GroupPrincipal (org.apache.activemq.jaas.GroupPrincipal)3 UserPrincipal (org.apache.activemq.jaas.UserPrincipal)3 Connector (org.apache.activemq.broker.Connector)2 TransportConnector (org.apache.activemq.broker.TransportConnector)2 HashSet (java.util.HashSet)1