Skip to content

Commit

Permalink
Implement validity check to spring.rabbitmq.host config property
Browse files Browse the repository at this point in the history
As discussed on issue #35628, at some point the host property accepted
multiple comma-separated hosts. However, this was not intended, and for
better clarification, it was decided to implement a clearer error
message for this situation.

See gh-35684
  • Loading branch information
rafaelrc7 authored and mhalbritter committed Jun 5, 2023
1 parent 45ce18e commit ed47e09
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.ConfirmType;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
Expand All @@ -43,6 +44,7 @@
* @author Artsiom Yudovin
* @author Franjo Zilic
* @author Eddú Meléndez
* @author Rafael Carvalho
* @since 1.0.0
*/
@ConfigurationProperties(prefix = "spring.rabbitmq")
Expand Down Expand Up @@ -203,6 +205,10 @@ public String getAddresses() {
*/
public String determineAddresses() {
if (CollectionUtils.isEmpty(this.parsedAddresses)) {
if (this.host.contains(",")) {
throw new InvalidConfigurationPropertyValueException("spring.rabbitmq.host", this.host,
"Invalid character ','. Value must be a single host. For multiple hosts, use property 'spring.rabbitmq.addresses' instead.");
}
return this.host + ":" + determinePort();
}
List<String> addressStrings = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* Tests for {@link RabbitProperties}.
*
* @author Dave Syer
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Rafael Carvalho
*/
class RabbitPropertiesTests {

Expand Down Expand Up @@ -338,4 +341,13 @@ void determineUsernameWithoutPassword() {
assertThat(this.properties.determinePassword()).isEqualTo("guest");
}

@Test
void hostPropertyMustBeSingleHost() {
this.properties.setHost("my-rmq-host.net,my-rmq-host-2.net");
assertThat(this.properties.getHost()).isEqualTo("my-rmq-host.net,my-rmq-host-2.net");
assertThatThrownBy(this.properties::determineAddresses)
.isInstanceOf(InvalidConfigurationPropertyValueException.class)
.hasMessageContaining("spring.rabbitmq.host");
}

}

0 comments on commit ed47e09

Please sign in to comment.