KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > security > SimpleSecurityBrokerSystemTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import junit.framework.Test;
36
37 /**
38  * Tests that the broker allows/fails access to destinations based on the
39  * security policy installed on the broker.
40  *
41  * @version $Revision$
42  */

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 JavaDoc path = System.getProperty("java.security.auth.login.config");
54         if (path == null) {
55             URL JavaDoc 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 JavaDoc[] 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 JavaDoc u = new HashMap JavaDoc();
111             u.put("system", "manager");
112             u.put("user", "password");
113             u.put("guest", "password");
114
115             HashMap JavaDoc groups = new HashMap JavaDoc();
116             groups.put("system", new HashSet JavaDoc(Arrays.asList(new Object JavaDoc[] { admins, users })));
117             groups.put("user", new HashSet JavaDoc(Arrays.asList(new Object JavaDoc[] { users })));
118             groups.put("guest", new HashSet JavaDoc(Arrays.asList(new Object JavaDoc[] { guests })));
119
120             return new SimpleAuthenticationBroker(broker, u, groups);
121         }
122
123         public String JavaDoc toString() {
124             return "SimpleAuthenticationBroker";
125         }
126     }
127
128     /**
129      * @see {@link CombinationTestSupport}
130      */

131     public void initCombos() {
132         addCombinationValues("authorizationPlugin", new Object JavaDoc[] { new AuthorizationPlugin(createAuthorizationMap()), });
133         addCombinationValues("authenticationPlugin", new Object JavaDoc[] { new SimpleAuthenticationFactory(), new JaasAuthenticationPlugin(), });
134     }
135
136     protected BrokerService createBroker() throws Exception JavaDoc {
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