Skip to content

Commit

Permalink
Add ANONYMOUS SASL mechanism
Browse files Browse the repository at this point in the history
Fixes #1405
  • Loading branch information
acogoluegnes committed Sep 9, 2024
1 parent 911799c commit c2849c2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/rabbitmq/client/DefaultSaslConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.rabbitmq.client;

import com.rabbitmq.client.impl.AnonymousMechanism;
import com.rabbitmq.client.impl.ExternalMechanism;
import com.rabbitmq.client.impl.PlainMechanism;

Expand All @@ -30,6 +31,7 @@ public class DefaultSaslConfig implements SaslConfig {

public static final DefaultSaslConfig PLAIN = new DefaultSaslConfig("PLAIN");
public static final DefaultSaslConfig EXTERNAL = new DefaultSaslConfig("EXTERNAL");
public static final DefaultSaslConfig ANONYMOUS = new DefaultSaslConfig("ANONYMOUS");

/**
* Create a DefaultSaslConfig with an explicit mechanism to use.
Expand All @@ -50,6 +52,8 @@ public SaslMechanism getSaslMechanism(String[] serverMechanisms) {
}
else if (mechanism.equals("EXTERNAL")) {
return new ExternalMechanism();
} else if (mechanism.equals("ANONYMOUS")) {
return new AnonymousMechanism();
}
}
return null;
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/rabbitmq/client/impl/AnonymousMechanism.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2007-2023 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
// [email protected].

package com.rabbitmq.client.impl;

import com.rabbitmq.client.LongString;
import com.rabbitmq.client.SaslMechanism;

/**
* The ANONYMOUS auth mechanism
*/
public class AnonymousMechanism implements SaslMechanism {
@Override
public String getName() {
return "ANONYMOUS";
}

@Override
public LongString handleChallenge(LongString challenge, String username, String password) {
return LongStringHelper.asLongString("");
}
}
3 changes: 2 additions & 1 deletion src/test/java/com/rabbitmq/client/test/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@ private BrokerVersionAtLeast310Condition() {

public enum BrokerVersion {
RABBITMQ_3_8("3.8.0"),
RABBITMQ_3_10("3.10.0");
RABBITMQ_3_10("3.10.0"),
RABBITMQ_4_0("4.0.0");

final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@
import java.util.Map;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.*;
import com.rabbitmq.client.test.TestUtils;
import org.junit.jupiter.api.Test;

import com.rabbitmq.client.AuthenticationFailureException;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.LongString;
import com.rabbitmq.client.PossibleAuthenticationFailureException;
import com.rabbitmq.client.SaslConfig;
import com.rabbitmq.client.SaslMechanism;
import com.rabbitmq.client.impl.AMQConnection;
import com.rabbitmq.client.impl.LongStringHelper;
import com.rabbitmq.client.test.BrokerTestCase;
Expand Down Expand Up @@ -105,6 +99,15 @@ public SaslMechanism getSaslMechanism(String[] mechanisms) {
connectionCloseAuthFailure(connectionFactory.getUsername(), "incorrect-password");
}

@Test
@TestUtils.BrokerVersionAtLeast(TestUtils.BrokerVersion.RABBITMQ_4_0)
public void anonymousShouldSucceed() throws Exception {
ConnectionFactory factory = TestUtils.connectionFactory();
factory.setSaslConfig(DefaultSaslConfig.ANONYMOUS);
Connection connection = factory.newConnection();
connection.close();
}

public void connectionCloseAuthFailure(String username, String password) throws IOException, TimeoutException {
String failDetail = "for username " + username + " and password " + password;
try {
Expand Down

0 comments on commit c2849c2

Please sign in to comment.