Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 77832 invoked from network); 24 Jun 2010 11:17:06 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 24 Jun 2010 11:17:06 -0000 Received: (qmail 50692 invoked by uid 500); 24 Jun 2010 11:17:06 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 50621 invoked by uid 500); 24 Jun 2010 11:17:03 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 50610 invoked by uid 99); 24 Jun 2010 11:17:02 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Jun 2010 11:17:02 +0000 X-ASF-Spam-Status: No, hits=-1538.2 required=10.0 tests=ALL_TRUSTED,AWL X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Jun 2010 11:17:01 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id A0CC323889D2; Thu, 24 Jun 2010 11:16:09 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r957509 - in /activemq/trunk/activemq-core/src: main/java/org/apache/activemq/security/ test/java/org/apache/activemq/security/ test/resources/org/apache/activemq/security/ Date: Thu, 24 Jun 2010 11:16:09 -0000 To: commits@activemq.apache.org From: dejanb@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100624111609.A0CC323889D2@eris.apache.org> Author: dejanb Date: Thu Jun 24 11:16:09 2010 New Revision: 957509 URL: http://svn.apache.org/viewvc?rev=957509&view=rev Log: https://issues.apache.org/activemq/browse/AMQ-2081 - anonymous access Added: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/security/SimpleAnonymousPluginTest.java activemq/trunk/activemq-core/src/test/resources/org/apache/activemq/security/simple-anonymous-broker.xml Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationBroker.java activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationPlugin.java Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationBroker.java URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationBroker.java?rev=957509&r1=957508&r2=957509&view=diff ============================================================================== --- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationBroker.java (original) +++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationBroker.java Thu Jun 24 11:16:09 2010 @@ -16,6 +16,7 @@ */ package org.apache.activemq.security; +import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -25,6 +26,7 @@ import org.apache.activemq.broker.Broker import org.apache.activemq.broker.BrokerFilter; import org.apache.activemq.broker.ConnectionContext; import org.apache.activemq.command.ConnectionInfo; +import org.apache.activemq.jaas.GroupPrincipal; /** * Handles authenticating a users against a simple user name/password map. @@ -33,6 +35,9 @@ import org.apache.activemq.command.Conne */ public class SimpleAuthenticationBroker extends BrokerFilter { + private boolean anonymousAccessAllowed = false; + private String anonymousUser; + private String anonymousGroup; private final Map userPasswords; private final Map userGroups; private final CopyOnWriteArrayList securityContexts = new CopyOnWriteArrayList(); @@ -42,22 +47,47 @@ public class SimpleAuthenticationBroker this.userPasswords = userPasswords; this.userGroups = userGroups; } + + public void setAnonymousAccessAllowed(boolean anonymousAccessAllowed) { + this.anonymousAccessAllowed = anonymousAccessAllowed; + } + + public void setAnonymousUser(String anonymousUser) { + this.anonymousUser = anonymousUser; + } + + public void setAnonymousGroup(String anonymousGroup) { + this.anonymousGroup = anonymousGroup; + } public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception { - if (context.getSecurityContext() == null) { + SecurityContext s = context.getSecurityContext(); + if (s == null) { // Check the username and password. - String pw = (String)userPasswords.get(info.getUserName()); - if (pw == null || !pw.equals(info.getPassword())) { - throw new SecurityException("User name or password is invalid."); - } - - final Set groups = (Set)userGroups.get(info.getUserName()); - SecurityContext s = new SecurityContext(info.getUserName()) { - public Set getPrincipals() { - return groups; + if (anonymousAccessAllowed && info.getUserName() == null && info.getPassword() == null) { + info.setUserName(anonymousUser); + s = new SecurityContext(info.getUserName()) { + public Set getPrincipals() { + Set groups = new HashSet(); + groups.add(new GroupPrincipal(anonymousGroup)); + return groups; + } + }; + } else { + String pw = (String) userPasswords.get(info.getUserName()); + if (pw == null || !pw.equals(info.getPassword())) { + throw new SecurityException( + "User name or password is invalid."); } - }; + + final Set groups = (Set) userGroups.get(info.getUserName()); + s = new SecurityContext(info.getUserName()) { + public Set getPrincipals() { + return groups; + } + }; + } context.setSecurityContext(s); securityContexts.add(s); Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationPlugin.java URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationPlugin.java?rev=957509&r1=957508&r2=957509&view=diff ============================================================================== --- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationPlugin.java (original) +++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/SimpleAuthenticationPlugin.java Thu Jun 24 11:16:09 2010 @@ -41,6 +41,11 @@ import org.apache.activemq.jaas.GroupPri public class SimpleAuthenticationPlugin implements BrokerPlugin { private Map userPasswords; private Map> userGroups; + private static final String DEFAULT_ANONYMOUS_USER = "anonymous"; + private static final String DEFAULT_ANONYMOUS_GROUP = "anonymous"; + private String anonymousUser = DEFAULT_ANONYMOUS_USER; + private String anonymousGroup = DEFAULT_ANONYMOUS_GROUP; + private boolean anonymousAccessAllowed = false; public SimpleAuthenticationPlugin() { } @@ -49,8 +54,12 @@ public class SimpleAuthenticationPlugin setUsers(users); } - public Broker installPlugin(Broker broker) { - return new SimpleAuthenticationBroker(broker, userPasswords, userGroups); + public Broker installPlugin(Broker parent) { + SimpleAuthenticationBroker broker = new SimpleAuthenticationBroker(parent, userPasswords, userGroups); + broker.setAnonymousAccessAllowed(anonymousAccessAllowed); + broker.setAnonymousUser(anonymousUser); + broker.setAnonymousGroup(anonymousGroup); + return broker; } public Map> getUserGroups() { @@ -77,6 +86,19 @@ public class SimpleAuthenticationPlugin userGroups.put(user.getUsername(), groups); } } + + + public void setAnonymousAccessAllowed(boolean anonymousAccessAllowed) { + this.anonymousAccessAllowed = anonymousAccessAllowed; + } + + public void setAnonymousUser(String anonymousUser) { + this.anonymousUser = anonymousUser; + } + + public void setAnonymousGroup(String anonymousGroup) { + this.anonymousGroup = anonymousGroup; + } /** * Sets the groups a user is in. The key is the user name and the value is a Added: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/security/SimpleAnonymousPluginTest.java URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/security/SimpleAnonymousPluginTest.java?rev=957509&view=auto ============================================================================== --- activemq/trunk/activemq-core/src/test/java/org/apache/activemq/security/SimpleAnonymousPluginTest.java (added) +++ activemq/trunk/activemq-core/src/test/java/org/apache/activemq/security/SimpleAnonymousPluginTest.java Thu Jun 24 11:16:09 2010 @@ -0,0 +1,121 @@ +/** + * 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.activemq.security; + +import javax.jms.Connection; +import javax.jms.JMSException; + +import junit.framework.Test; + +import org.apache.activemq.CombinationTestSupport; +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.command.ActiveMQQueue; +import org.apache.activemq.command.ActiveMQTopic; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class SimpleAnonymousPluginTest extends SimpleAuthenticationPluginTest { + + private static final Log LOG = LogFactory.getLog(SimpleAnonymousPluginTest.class); + + public static Test suite() { + return suite(SimpleAnonymousPluginTest.class); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + protected BrokerService createBroker() throws Exception { + return createBroker("org/apache/activemq/security/simple-anonymous-broker.xml"); + } + + public void testInvalidAuthentication() throws JMSException { + + try { + // Bad password + Connection c = factory.createConnection("user", "krap"); + connections.add(c); + c.start(); + fail("Expected exception."); + } catch (JMSException e) { + } + + try { + // Bad userid + Connection c = factory.createConnection("userkrap", null); + connections.add(c); + c.start(); + fail("Expected exception."); + } catch (JMSException e) { + } + } + + + public void testAnonymousReceiveSucceeds() throws JMSException { + doReceive(false); + } + + public void testAnonymousReceiveFails() throws JMSException { + doReceive(true); + } + + public void testAnonymousSendFails() throws JMSException { + doSend(true); + } + + public void testAnonymousSendSucceeds() throws JMSException { + doSend(false); + } + + /** + * @see {@link CombinationTestSupport} + */ + public void initCombosForTestAnonymousReceiveSucceeds() { + addCombinationValues("userName", new Object[] {}); + addCombinationValues("password", new Object[] {}); + addCombinationValues("destination", new Object[] {new ActiveMQQueue("GUEST.BAR"), new ActiveMQTopic("GUEST.BAR")}); + } + + /** + * @see {@link CombinationTestSupport} + */ + public void initCombosForTestAnonymousReceiveFails() { + addCombinationValues("userName", new Object[] {}); + addCombinationValues("password", new Object[] {}); + addCombinationValues("destination", new Object[] {new ActiveMQQueue("TEST"), new ActiveMQTopic("TEST"), new ActiveMQQueue("USERS.FOO"), new ActiveMQTopic("USERS.FOO") }); + } + + /** + * @see {@link CombinationTestSupport} + */ + public void initCombosForTestAnonymousSendFails() { + addCombinationValues("userName", new Object[] {}); + addCombinationValues("password", new Object[] {}); + addCombinationValues("destination", new Object[] {new ActiveMQQueue("TEST"), new ActiveMQTopic("TEST"), new ActiveMQQueue("USERS.FOO"), new ActiveMQTopic("USERS.FOO")}); + } + + /** + * @see {@link CombinationTestSupport} + */ + public void initCombosForTestAnonymousSendSucceeds() { + addCombinationValues("userName", new Object[] {}); + addCombinationValues("password", new Object[] {}); + addCombinationValues("destination", new Object[] {new ActiveMQQueue("GUEST.BAR"), new ActiveMQTopic("GUEST.BAR")}); + } + +} Added: activemq/trunk/activemq-core/src/test/resources/org/apache/activemq/security/simple-anonymous-broker.xml URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/resources/org/apache/activemq/security/simple-anonymous-broker.xml?rev=957509&view=auto ============================================================================== --- activemq/trunk/activemq-core/src/test/resources/org/apache/activemq/security/simple-anonymous-broker.xml (added) +++ activemq/trunk/activemq-core/src/test/resources/org/apache/activemq/security/simple-anonymous-broker.xml Thu Jun 24 11:16:09 2010 @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +