Skip to content

Commit 14c9147

Browse files
committed
Merge pull request #29661 from mourezwell
* pr/29661: Polish "Add support for Redis sentinel username" Add support for Redis sentinel username Closes gh-29661
2 parents ebbd167 + 3ffd881 commit 14c9147

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -99,6 +99,7 @@ protected final RedisSentinelConfiguration getSentinelConfig() {
9999
if (this.properties.getPassword() != null) {
100100
config.setPassword(RedisPassword.of(this.properties.getPassword()));
101101
}
102+
config.setSentinelUsername(sentinelProperties.getUsername());
102103
if (sentinelProperties.getPassword() != null) {
103104
config.setSentinelPassword(RedisPassword.of(sentinelProperties.getPassword()));
104105
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -372,6 +372,11 @@ public static class Sentinel {
372372
*/
373373
private List<String> nodes;
374374

375+
/**
376+
* Login username for authenticating with sentinel(s).
377+
*/
378+
private String username;
379+
375380
/**
376381
* Password for authenticating with sentinel(s).
377382
*/
@@ -393,6 +398,14 @@ public void setNodes(List<String> nodes) {
393398
this.nodes = nodes;
394399
}
395400

401+
public String getUsername() {
402+
return this.username;
403+
}
404+
405+
public void setUsername(String username) {
406+
this.username = username;
407+
}
408+
396409
public String getPassword() {
397410
return this.password;
398411
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -292,34 +292,57 @@ void testRedisConfigurationWithSentinelAndDatabase() {
292292

293293
@Test
294294
void testRedisConfigurationWithSentinelAndAuthentication() {
295-
this.contextRunner.withPropertyValues("spring.redis.username=user", "spring.redis.password=password",
296-
"spring.redis.sentinel.master:mymaster",
297-
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> {
298-
LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class);
299-
assertThat(getUserName(connectionFactory)).isEqualTo("user");
300-
assertThat(connectionFactory.getPassword()).isEqualTo("password");
301-
RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration();
295+
this.contextRunner
296+
.withPropertyValues("spring.redis.username=user", "spring.redis.password=password",
297+
"spring.redis.sentinel.master:mymaster",
298+
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380")
299+
.run(assertSentinelConfiguration("user", "password", (sentinelConfiguration) -> {
302300
assertThat(sentinelConfiguration.getSentinelPassword().isPresent()).isFalse();
303-
Set<RedisNode> sentinels = connectionFactory.getSentinelConfiguration().getSentinels();
301+
Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
304302
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
305303
.contains("127.0.0.1:26379", "127.0.0.1:26380");
306-
});
304+
}));
307305
}
308306

309307
@Test
310308
void testRedisConfigurationWithSentinelPasswordAndDataNodePassword() {
311-
this.contextRunner.withPropertyValues("spring.redis.password=password", "spring.redis.sentinel.password=secret",
312-
"spring.redis.sentinel.master:mymaster",
313-
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> {
314-
LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class);
315-
assertThat(getUserName(connectionFactory)).isNull();
316-
assertThat(connectionFactory.getPassword()).isEqualTo("password");
317-
RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration();
309+
this.contextRunner
310+
.withPropertyValues("spring.redis.password=password", "spring.redis.sentinel.password=secret",
311+
"spring.redis.sentinel.master:mymaster",
312+
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380")
313+
.run(assertSentinelConfiguration(null, "password", (sentinelConfiguration) -> {
314+
assertThat(sentinelConfiguration.getSentinelUsername()).isNull();
318315
assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret");
319316
Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
320317
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
321318
.contains("127.0.0.1:26379", "127.0.0.1:26380");
322-
});
319+
}));
320+
}
321+
322+
@Test
323+
void testRedisConfigurationWithSentinelAuthenticationAndDataNodeAuthentication() {
324+
this.contextRunner
325+
.withPropertyValues("spring.redis.username=username", "spring.redis.password=password",
326+
"spring.redis.sentinel.username=sentinel", "spring.redis.sentinel.password=secret",
327+
"spring.redis.sentinel.master:mymaster",
328+
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380")
329+
.run(assertSentinelConfiguration("username", "password", (sentinelConfiguration) -> {
330+
assertThat(sentinelConfiguration.getSentinelUsername()).isEqualTo("sentinel");
331+
assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret");
332+
Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
333+
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
334+
.contains("127.0.0.1:26379", "127.0.0.1:26380");
335+
}));
336+
}
337+
338+
private ContextConsumer<AssertableApplicationContext> assertSentinelConfiguration(String userName, String password,
339+
Consumer<RedisSentinelConfiguration> sentinelConfiguration) {
340+
return (context) -> {
341+
LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class);
342+
assertThat(getUserName(connectionFactory)).isEqualTo(userName);
343+
assertThat(connectionFactory.getPassword()).isEqualTo(password);
344+
assertThat(connectionFactory.getSentinelConfiguration()).satisfies(sentinelConfiguration);
345+
};
323346
}
324347

325348
@Test

0 commit comments

Comments
 (0)