Skip to content

Commit 5d8fe86

Browse files
committed
Polish "Use ssl.enabled flag when RabbitMQ address has no protocol"
There is a direct link between the sslEnabled flag and the default port that should be used by an address. The checks are currently set in two places: * Determine which port should be used * Determine if SSL should be enabled This commit polishes the initial proposal so that secureConnection is only set if a protocol is available. See gh-19109
1 parent 2210236 commit 5d8fe86

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public boolean determineEnabled() {
378378
return isEnabled();
379379
}
380380
Address address = RabbitProperties.this.parsedAddresses.get(0);
381-
return address.secureConnection;
381+
return address.determineSslEnabled(isEnabled());
382382
}
383383

384384
public void setEnabled(boolean enabled) {
@@ -966,27 +966,25 @@ private static final class Address {
966966

967967
private String virtualHost;
968968

969-
private boolean secureConnection;
969+
private Boolean secureConnection;
970970

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

979-
private String trimPrefix(String input, boolean sslEnabled) {
979+
private String trimPrefix(String input) {
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+
this.secureConnection = false;
985986
return input.substring(PREFIX_AMQP.length());
986987
}
987-
if (sslEnabled) {
988-
this.secureConnection = true;
989-
}
990988
return input;
991989
}
992990

@@ -1016,18 +1014,22 @@ private String parseVirtualHost(String input) {
10161014
return input;
10171015
}
10181016

1019-
private void parseHostAndPort(String input) {
1017+
private void parseHostAndPort(String input, boolean sslEnabled) {
10201018
int portIndex = input.indexOf(':');
10211019
if (portIndex == -1) {
10221020
this.host = input;
1023-
this.port = (this.secureConnection) ? DEFAULT_PORT_SECURE : DEFAULT_PORT;
1021+
this.port = (determineSslEnabled(sslEnabled)) ? DEFAULT_PORT_SECURE : DEFAULT_PORT;
10241022
}
10251023
else {
10261024
this.host = input.substring(0, portIndex);
10271025
this.port = Integer.valueOf(input.substring(portIndex + 1));
10281026
}
10291027
}
10301028

1029+
private boolean determineSslEnabled(boolean sslEnabled) {
1030+
return (this.secureConnection != null) ? this.secureConnection : sslEnabled;
1031+
}
1032+
10311033
}
10321034

10331035
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,6 @@ 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-
111104
@Test
112105
public void determinePortReturnsDefaultAmqpsPortWhenFirstAddressHasNoExplicitPortButSslEnabled() {
113106
this.properties.getSsl().setEnabled(true);
@@ -256,14 +249,14 @@ public void determineSslUsingAmqpsReturnsStateOfFirstAddress() {
256249
}
257250

258251
@Test
259-
public void determineSslUsingAddressWithoutAmpqsDefersToSslFlagProperty() {
252+
public void sslDetermineEnabledIsTrueWhenAddressHasNoProtocolAndSslIsEnabled() {
260253
this.properties.getSsl().setEnabled(true);
261254
this.properties.setAddresses("root:password@otherhost");
262255
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
263256
}
264257

265258
@Test
266-
public void determineSslUsingAddressWithoutAmpqDefersToSslFlagProperty() {
259+
public void sslDetermineEnabledIsFalseWhenAddressHasNoProtocolAndSslIsDisabled() {
267260
this.properties.getSsl().setEnabled(false);
268261
this.properties.setAddresses("root:password@otherhost");
269262
assertThat(this.properties.getSsl().determineEnabled()).isFalse();

0 commit comments

Comments
 (0)