Skip to content

Commit cefe334

Browse files
committed
Add anonymous SASL mechanism
1 parent fc388d2 commit cefe334

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2020-2023 Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
//
4+
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
5+
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
6+
// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
7+
// please see LICENSE-APACHE2.
8+
//
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
12+
//
13+
// If you have any questions regarding licensing, please contact us at
14+
15+
package com.rabbitmq.stream.sasl;
16+
17+
import java.nio.charset.StandardCharsets;
18+
19+
/** The <code>ANONYMOUS</code> {@link SaslMechanism}. */
20+
public final class AnonymousSaslMechanism implements SaslMechanism {
21+
22+
public static final SaslMechanism INSTANCE = new AnonymousSaslMechanism();
23+
24+
@Override
25+
public String getName() {
26+
return "ANONYMOUS";
27+
}
28+
29+
@Override
30+
public byte[] handleChallenge(byte[] challenge, CredentialsProvider credentialsProvider) {
31+
return "".getBytes(StandardCharsets.UTF_8);
32+
}
33+
}

src/main/java/com/rabbitmq/stream/sasl/DefaultSaslConfiguration.java

+3
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ public final class DefaultSaslConfiguration implements SaslConfiguration {
2828
new DefaultSaslConfiguration(PlainSaslMechanism.INSTANCE.getName());
2929
public static final SaslConfiguration EXTERNAL =
3030
new DefaultSaslConfiguration(ExternalSaslMechanism.INSTANCE.getName());
31+
public static final SaslConfiguration ANONYMOUS =
32+
new DefaultSaslConfiguration(AnonymousSaslMechanism.INSTANCE.getName());
3133

3234
private final Map<String, SaslMechanism> mechanisms =
3335
Collections.unmodifiableMap(
3436
new HashMap<String, SaslMechanism>() {
3537
{
3638
put(PlainSaslMechanism.INSTANCE.getName(), PlainSaslMechanism.INSTANCE);
3739
put(ExternalSaslMechanism.INSTANCE.getName(), ExternalSaslMechanism.INSTANCE);
40+
put(AnonymousSaslMechanism.INSTANCE.getName(), AnonymousSaslMechanism.INSTANCE);
3841
}
3942
});
4043
private final String mechanism;

src/test/java/com/rabbitmq/stream/impl/AuthenticationTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ void updateSecret() throws Exception {
147147
}
148148
}
149149

150+
@Test
151+
@TestUtils.BrokerVersionAtLeast(TestUtils.BrokerVersion.RABBITMQ_4_0_0)
152+
void anonymousAuthenticationShouldWork() {
153+
try (Client ignored =
154+
cf.get(
155+
new Client.ClientParameters().saslConfiguration(DefaultSaslConfiguration.ANONYMOUS))) {}
156+
}
157+
150158
private static CredentialsProvider credentialsProvider(String username, String password) {
151159
return new DefaultUsernamePasswordCredentialsProvider(username, password);
152160
}

src/test/java/com/rabbitmq/stream/impl/TestUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,8 @@ public enum BrokerVersion {
10341034
RABBITMQ_3_11_9("3.11.9"),
10351035
RABBITMQ_3_11_11("3.11.11"),
10361036
RABBITMQ_3_11_14("3.11.14"),
1037-
RABBITMQ_3_13_0("3.13.0");
1037+
RABBITMQ_3_13_0("3.13.0"),
1038+
RABBITMQ_4_0_0("4.0.0");
10381039

10391040
final String value;
10401041

0 commit comments

Comments
 (0)