From 202a426bd6b0aa098b86aca72136ab00e23d908f Mon Sep 17 00:00:00 2001 From: mourezwell Date: Sat, 5 Feb 2022 17:36:09 +0100 Subject: [PATCH 1/2] Add support for Redis sentinel username See gh-29661 --- .../redis/RedisConnectionConfiguration.java | 1 + .../data/redis/RedisProperties.java | 13 +++++++++++++ .../redis/RedisAutoConfigurationTests.java | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java index b870e4a550..cee9c145f4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java @@ -99,6 +99,7 @@ abstract class RedisConnectionConfiguration { if (this.properties.getPassword() != null) { config.setPassword(RedisPassword.of(this.properties.getPassword())); } + config.setSentinelUsername(sentinelProperties.getUsername()); if (sentinelProperties.getPassword() != null) { config.setSentinelPassword(RedisPassword.of(sentinelProperties.getPassword())); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java index d61f578a9f..0afbb20b61 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java @@ -377,6 +377,11 @@ public class RedisProperties { */ private String password; + /** + * Login username for authenticating with sentinel(s). + */ + private String username; + public String getMaster() { return this.master; } @@ -401,6 +406,14 @@ public class RedisProperties { this.password = password; } + public String getUsername() { + return this.username; + } + + public void setUsername(String username) { + this.username = username; + } + } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java index cbc0855035..ce67723015 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java @@ -315,6 +315,25 @@ class RedisAutoConfigurationTests { assertThat(getUserName(connectionFactory)).isNull(); assertThat(connectionFactory.getPassword()).isEqualTo("password"); RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration(); + assertThat(sentinelConfiguration.getSentinelUsername()).isNull(); + assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret"); + Set sentinels = sentinelConfiguration.getSentinels(); + assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet())) + .contains("127.0.0.1:26379", "127.0.0.1:26380"); + }); + } + + @Test + void testRedisConfigurationWithSentinelAuthenticationAndDataNodeAuthentication() { + this.contextRunner.withPropertyValues("spring.redis.username=username", "spring.redis.password=password", + "spring.redis.sentinel.username=sentinel", "spring.redis.sentinel.password=secret", + "spring.redis.sentinel.master:mymaster", + "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> { + LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class); + assertThat(getUserName(connectionFactory)).isEqualTo("username"); + assertThat(connectionFactory.getPassword()).isEqualTo("password"); + RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration(); + assertThat(sentinelConfiguration.getSentinelUsername()).isEqualTo("sentinel"); assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret"); Set sentinels = sentinelConfiguration.getSentinels(); assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet())) From 3ffd88158e663e9241f35c6b707a246381a272f5 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 21 Feb 2022 09:52:29 +0100 Subject: [PATCH 2/2] Polish "Add support for Redis sentinel username" See gh-29661 --- .../redis/RedisConnectionConfiguration.java | 2 +- .../data/redis/RedisProperties.java | 26 ++++----- .../redis/RedisAutoConfigurationTests.java | 58 ++++++++++--------- 3 files changed, 45 insertions(+), 41 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java index cee9c145f4..12ef2a41c9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java index 0afbb20b61..89cb977a62 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -373,14 +373,14 @@ public class RedisProperties { private List nodes; /** - * Password for authenticating with sentinel(s). + * Login username for authenticating with sentinel(s). */ - private String password; + private String username; /** - * Login username for authenticating with sentinel(s). + * Password for authenticating with sentinel(s). */ - private String username; + private String password; public String getMaster() { return this.master; @@ -398,14 +398,6 @@ public class RedisProperties { this.nodes = nodes; } - public String getPassword() { - return this.password; - } - - public void setPassword(String password) { - this.password = password; - } - public String getUsername() { return this.username; } @@ -414,6 +406,14 @@ public class RedisProperties { this.username = username; } + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java index ce67723015..d2547a75a8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -292,53 +292,57 @@ class RedisAutoConfigurationTests { @Test void testRedisConfigurationWithSentinelAndAuthentication() { - this.contextRunner.withPropertyValues("spring.redis.username=user", "spring.redis.password=password", - "spring.redis.sentinel.master:mymaster", - "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> { - LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class); - assertThat(getUserName(connectionFactory)).isEqualTo("user"); - assertThat(connectionFactory.getPassword()).isEqualTo("password"); - RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration(); + this.contextRunner + .withPropertyValues("spring.redis.username=user", "spring.redis.password=password", + "spring.redis.sentinel.master:mymaster", + "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380") + .run(assertSentinelConfiguration("user", "password", (sentinelConfiguration) -> { assertThat(sentinelConfiguration.getSentinelPassword().isPresent()).isFalse(); - Set sentinels = connectionFactory.getSentinelConfiguration().getSentinels(); + Set sentinels = sentinelConfiguration.getSentinels(); assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet())) .contains("127.0.0.1:26379", "127.0.0.1:26380"); - }); + })); } @Test void testRedisConfigurationWithSentinelPasswordAndDataNodePassword() { - this.contextRunner.withPropertyValues("spring.redis.password=password", "spring.redis.sentinel.password=secret", - "spring.redis.sentinel.master:mymaster", - "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> { - LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class); - assertThat(getUserName(connectionFactory)).isNull(); - assertThat(connectionFactory.getPassword()).isEqualTo("password"); - RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration(); + this.contextRunner + .withPropertyValues("spring.redis.password=password", "spring.redis.sentinel.password=secret", + "spring.redis.sentinel.master:mymaster", + "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380") + .run(assertSentinelConfiguration(null, "password", (sentinelConfiguration) -> { assertThat(sentinelConfiguration.getSentinelUsername()).isNull(); assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret"); Set sentinels = sentinelConfiguration.getSentinels(); assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet())) .contains("127.0.0.1:26379", "127.0.0.1:26380"); - }); + })); } @Test void testRedisConfigurationWithSentinelAuthenticationAndDataNodeAuthentication() { - this.contextRunner.withPropertyValues("spring.redis.username=username", "spring.redis.password=password", - "spring.redis.sentinel.username=sentinel", "spring.redis.sentinel.password=secret", - "spring.redis.sentinel.master:mymaster", - "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> { - LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class); - assertThat(getUserName(connectionFactory)).isEqualTo("username"); - assertThat(connectionFactory.getPassword()).isEqualTo("password"); - RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration(); + this.contextRunner + .withPropertyValues("spring.redis.username=username", "spring.redis.password=password", + "spring.redis.sentinel.username=sentinel", "spring.redis.sentinel.password=secret", + "spring.redis.sentinel.master:mymaster", + "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380") + .run(assertSentinelConfiguration("username", "password", (sentinelConfiguration) -> { assertThat(sentinelConfiguration.getSentinelUsername()).isEqualTo("sentinel"); assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret"); Set sentinels = sentinelConfiguration.getSentinels(); assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet())) .contains("127.0.0.1:26379", "127.0.0.1:26380"); - }); + })); + } + + private ContextConsumer assertSentinelConfiguration(String userName, String password, + Consumer sentinelConfiguration) { + return (context) -> { + LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class); + assertThat(getUserName(connectionFactory)).isEqualTo(userName); + assertThat(connectionFactory.getPassword()).isEqualTo(password); + assertThat(connectionFactory.getSentinelConfiguration()).satisfies(sentinelConfiguration); + }; } @Test