Skip to content

Commit 3ffd881

Browse files
committed
Polish "Add support for Redis sentinel username"
See gh-29661
1 parent 202a426 commit 3ffd881

File tree

3 files changed

+45
-41
lines changed

3 files changed

+45
-41
lines changed

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

Lines changed: 1 addition & 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.

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

Lines changed: 13 additions & 13 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.
@@ -373,14 +373,14 @@ public static class Sentinel {
373373
private List<String> nodes;
374374

375375
/**
376-
* Password for authenticating with sentinel(s).
376+
* Login username for authenticating with sentinel(s).
377377
*/
378-
private String password;
378+
private String username;
379379

380380
/**
381-
* Login username for authenticating with sentinel(s).
381+
* Password for authenticating with sentinel(s).
382382
*/
383-
private String username;
383+
private String password;
384384

385385
public String getMaster() {
386386
return this.master;
@@ -398,14 +398,6 @@ public void setNodes(List<String> nodes) {
398398
this.nodes = nodes;
399399
}
400400

401-
public String getPassword() {
402-
return this.password;
403-
}
404-
405-
public void setPassword(String password) {
406-
this.password = password;
407-
}
408-
409401
public String getUsername() {
410402
return this.username;
411403
}
@@ -414,6 +406,14 @@ public void setUsername(String username) {
414406
this.username = username;
415407
}
416408

409+
public String getPassword() {
410+
return this.password;
411+
}
412+
413+
public void setPassword(String password) {
414+
this.password = password;
415+
}
416+
417417
}
418418

419419
/**

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

Lines changed: 31 additions & 27 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,53 +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) -> {
318314
assertThat(sentinelConfiguration.getSentinelUsername()).isNull();
319315
assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret");
320316
Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
321317
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
322318
.contains("127.0.0.1:26379", "127.0.0.1:26380");
323-
});
319+
}));
324320
}
325321

326322
@Test
327323
void testRedisConfigurationWithSentinelAuthenticationAndDataNodeAuthentication() {
328-
this.contextRunner.withPropertyValues("spring.redis.username=username", "spring.redis.password=password",
329-
"spring.redis.sentinel.username=sentinel", "spring.redis.sentinel.password=secret",
330-
"spring.redis.sentinel.master:mymaster",
331-
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> {
332-
LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class);
333-
assertThat(getUserName(connectionFactory)).isEqualTo("username");
334-
assertThat(connectionFactory.getPassword()).isEqualTo("password");
335-
RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration();
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) -> {
336330
assertThat(sentinelConfiguration.getSentinelUsername()).isEqualTo("sentinel");
337331
assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret");
338332
Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
339333
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
340334
.contains("127.0.0.1:26379", "127.0.0.1:26380");
341-
});
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+
};
342346
}
343347

344348
@Test

0 commit comments

Comments
 (0)