Polish "Add support for Redis sentinel username"
See gh-29661
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
@@ -372,16 +372,16 @@ public class RedisProperties {
|
||||
*/
|
||||
private List<String> nodes;
|
||||
|
||||
/**
|
||||
* Password for authenticating with sentinel(s).
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* Login username for authenticating with sentinel(s).
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* Password for authenticating with sentinel(s).
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<RedisNode> sentinels = connectionFactory.getSentinelConfiguration().getSentinels();
|
||||
Set<RedisNode> 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<RedisNode> 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<RedisNode> 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<AssertableApplicationContext> assertSentinelConfiguration(String userName, String password,
|
||||
Consumer<RedisSentinelConfiguration> 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
|
||||
|
||||
Reference in New Issue
Block a user