1 18 package org.apache.activemq.security; 19 20 import org.apache.activemq.CombinationTestSupport; 21 import org.apache.activemq.broker.Broker; 22 import org.apache.activemq.broker.BrokerPlugin; 23 import org.apache.activemq.broker.BrokerService; 24 import org.apache.activemq.command.ActiveMQQueue; 25 import org.apache.activemq.command.ActiveMQTopic; 26 import org.apache.activemq.filter.DestinationMap; 27 import org.apache.activemq.jaas.GroupPrincipal; 28 29 import java.net.URL ; 30 import java.util.Arrays ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.Set ; 34 35 import junit.framework.Test; 36 37 43 public class SimpleSecurityBrokerSystemTest extends SecurityTestSupport { 44 45 static final GroupPrincipal guests = new GroupPrincipal("guests"); 46 static final GroupPrincipal users = new GroupPrincipal("users"); 47 static final GroupPrincipal admins = new GroupPrincipal("admins"); 48 49 public BrokerPlugin authorizationPlugin; 50 public BrokerPlugin authenticationPlugin; 51 52 static { 53 String path = System.getProperty("java.security.auth.login.config"); 54 if (path == null) { 55 URL resource = SimpleSecurityBrokerSystemTest.class.getClassLoader().getResource("login.config"); 56 if (resource != null) { 57 path = resource.getFile(); 58 System.setProperty("java.security.auth.login.config", path); 59 } 60 } 61 log.info("Path to login config: " + path); 62 } 63 64 public static Test suite() { 65 return suite(SimpleSecurityBrokerSystemTest.class); 66 } 67 68 public static void main(String [] args) { 69 junit.textui.TestRunner.run(suite()); 70 } 71 72 public static AuthorizationMap createAuthorizationMap() { 73 DestinationMap readAccess = new DestinationMap(); 74 readAccess.put(new ActiveMQQueue(">"), admins); 75 readAccess.put(new ActiveMQQueue("USERS.>"), users); 76 readAccess.put(new ActiveMQQueue("GUEST.>"), guests); 77 readAccess.put(new ActiveMQTopic(">"), admins); 78 readAccess.put(new ActiveMQTopic("USERS.>"), users); 79 readAccess.put(new ActiveMQTopic("GUEST.>"), guests); 80 81 DestinationMap writeAccess = new DestinationMap(); 82 writeAccess.put(new ActiveMQQueue(">"), admins); 83 writeAccess.put(new ActiveMQQueue("USERS.>"), users); 84 writeAccess.put(new ActiveMQQueue("GUEST.>"), users); 85 writeAccess.put(new ActiveMQQueue("GUEST.>"), guests); 86 writeAccess.put(new ActiveMQTopic(">"), admins); 87 writeAccess.put(new ActiveMQTopic("USERS.>"), users); 88 writeAccess.put(new ActiveMQTopic("GUEST.>"), users); 89 writeAccess.put(new ActiveMQTopic("GUEST.>"), guests); 90 91 readAccess.put(new ActiveMQTopic("ActiveMQ.Advisory.>"), guests); 92 readAccess.put(new ActiveMQTopic("ActiveMQ.Advisory.>"), users); 93 writeAccess.put(new ActiveMQTopic("ActiveMQ.Advisory.>"), guests); 94 writeAccess.put(new ActiveMQTopic("ActiveMQ.Advisory.>"), users); 95 96 DestinationMap adminAccess = new DestinationMap(); 97 adminAccess.put(new ActiveMQTopic(">"), admins); 98 adminAccess.put(new ActiveMQTopic(">"), users); 99 adminAccess.put(new ActiveMQTopic(">"), guests); 100 adminAccess.put(new ActiveMQQueue(">"), admins); 101 adminAccess.put(new ActiveMQQueue(">"), users); 102 adminAccess.put(new ActiveMQQueue(">"), guests); 103 104 return new SimpleAuthorizationMap(writeAccess, readAccess, adminAccess); 105 } 106 107 class SimpleAuthenticationFactory implements BrokerPlugin { 108 public Broker installPlugin(Broker broker) { 109 110 HashMap u = new HashMap (); 111 u.put("system", "manager"); 112 u.put("user", "password"); 113 u.put("guest", "password"); 114 115 HashMap groups = new HashMap (); 116 groups.put("system", new HashSet (Arrays.asList(new Object [] { admins, users }))); 117 groups.put("user", new HashSet (Arrays.asList(new Object [] { users }))); 118 groups.put("guest", new HashSet (Arrays.asList(new Object [] { guests }))); 119 120 return new SimpleAuthenticationBroker(broker, u, groups); 121 } 122 123 public String toString() { 124 return "SimpleAuthenticationBroker"; 125 } 126 } 127 128 131 public void initCombos() { 132 addCombinationValues("authorizationPlugin", new Object [] { new AuthorizationPlugin(createAuthorizationMap()), }); 133 addCombinationValues("authenticationPlugin", new Object [] { new SimpleAuthenticationFactory(), new JaasAuthenticationPlugin(), }); 134 } 135 136 protected BrokerService createBroker() throws Exception { 137 BrokerService broker = super.createBroker(); 138 broker.setPlugins(new BrokerPlugin[] { authorizationPlugin, authenticationPlugin }); 139 broker.setPersistent(false); 140 return broker; 141 } 142 143 144 } 145 | Popular Tags |