Skip to content

Commit 2210236

Browse files
cbo-indeedsnicoll
authored andcommitted
Use ssl.enabled flag when RabbitMQ address has no protocol
See spring-projectsgh-19109
1 parent a38e6b4 commit 2210236

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void setAddresses(String addresses) {
188188
private List<Address> parseAddresses(String addresses) {
189189
List<Address> parsedAddresses = new ArrayList<>();
190190
for (String address : StringUtils.commaDelimitedListToStringArray(addresses)) {
191-
parsedAddresses.add(new Address(address));
191+
parsedAddresses.add(new Address(address, getSsl().isEnabled()));
192192
}
193193
return parsedAddresses;
194194
}
@@ -968,21 +968,24 @@ private static final class Address {
968968

969969
private boolean secureConnection;
970970

971-
private Address(String input) {
971+
private Address(String input, boolean sslEnabled) {
972972
input = input.trim();
973-
input = trimPrefix(input);
973+
input = trimPrefix(input, sslEnabled);
974974
input = parseUsernameAndPassword(input);
975975
input = parseVirtualHost(input);
976976
parseHostAndPort(input);
977977
}
978978

979-
private String trimPrefix(String input) {
979+
private String trimPrefix(String input, boolean sslEnabled) {
980980
if (input.startsWith(PREFIX_AMQP_SECURE)) {
981981
this.secureConnection = true;
982982
return input.substring(PREFIX_AMQP_SECURE.length());
983983
}
984984
if (input.startsWith(PREFIX_AMQP)) {
985-
input = input.substring(PREFIX_AMQP.length());
985+
return input.substring(PREFIX_AMQP.length());
986+
}
987+
if (sslEnabled) {
988+
this.secureConnection = true;
986989
}
987990
return input;
988991
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ public void determinePortUsingAmqpsReturnsPortOfFirstAddress() {
101101
assertThat(this.properties.determinePort()).isEqualTo(5671);
102102
}
103103

104+
@Test
105+
public void determinePortReturnsDefaultAmqpsPortWhenFirstAddressHasNoExplicitPort() {
106+
this.properties.setPort(1234);
107+
this.properties.setAddresses("rabbit1.example.com,rabbit2.example.com:2345");
108+
assertThat(this.properties.determinePort()).isEqualTo(5672);
109+
}
110+
111+
@Test
112+
public void determinePortReturnsDefaultAmqpsPortWhenFirstAddressHasNoExplicitPortButSslEnabled() {
113+
this.properties.getSsl().setEnabled(true);
114+
this.properties.setPort(1234);
115+
this.properties.setAddresses("rabbit1.example.com,rabbit2.example.com:2345");
116+
assertThat(this.properties.determinePort()).isEqualTo(5671);
117+
}
118+
104119
@Test
105120
public void virtualHostDefaultsToNull() {
106121
assertThat(this.properties.getVirtualHost()).isNull();
@@ -240,6 +255,20 @@ public void determineSslUsingAmqpsReturnsStateOfFirstAddress() {
240255
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
241256
}
242257

258+
@Test
259+
public void determineSslUsingAddressWithoutAmpqsDefersToSslFlagProperty() {
260+
this.properties.getSsl().setEnabled(true);
261+
this.properties.setAddresses("root:password@otherhost");
262+
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
263+
}
264+
265+
@Test
266+
public void determineSslUsingAddressWithoutAmpqDefersToSslFlagProperty() {
267+
this.properties.getSsl().setEnabled(false);
268+
this.properties.setAddresses("root:password@otherhost");
269+
assertThat(this.properties.getSsl().determineEnabled()).isFalse();
270+
}
271+
243272
@Test
244273
public void determineSslUsingAmqpReturnsStateOfFirstAddress() {
245274
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");

0 commit comments

Comments
 (0)