Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.pivotal.cfenv.spring.boot;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -52,15 +54,27 @@ public void process(CfCredentials cfCredentials, Map<String, Object> properties)
String uri = cfCredentials.getUri(redisSchemes);

if (uri == null) {
properties.put(PREFIX + ".host", cfCredentials.getHost());
properties.put(PREFIX + ".password", cfCredentials.getPassword());
if (configureSentinelSetup(cfCredentials.getMap())) {
properties.put(PREFIX + ".password", cfCredentials.getPassword());

Optional<String> tlsPort = Optional.ofNullable(cfCredentials.getString("tls_port"));
if (tlsPort.isPresent()) {
properties.put(PREFIX + ".port", tlsPort.get());
properties.put(PREFIX + ".ssl.enabled", Boolean.TRUE);
} else {
properties.put(PREFIX + ".port", cfCredentials.getPort());
// Sentinel configuration
properties.put(PREFIX + ".sentinel.master", cfCredentials.getString("master_name"));
properties.put(PREFIX + ".sentinel.nodes", extractSentinelNodes(cfCredentials.getMap()));
properties.put(PREFIX + ".sentinel.username", "");
properties.put(PREFIX + ".sentinel.password", cfCredentials.getString("sentinel_password"));
properties.put(PREFIX + ".ssl.enabled", useSentinelTls(cfCredentials.getMap()));
}
else {
properties.put(PREFIX + ".host", cfCredentials.getHost());
properties.put(PREFIX + ".password", cfCredentials.getPassword());

Optional<String> tlsPort = Optional.ofNullable(cfCredentials.getString("tls_port"));
if (tlsPort.isPresent()) {
properties.put(PREFIX + ".port", tlsPort.get());
properties.put(PREFIX + ".ssl.enabled", Boolean.TRUE);
} else {
properties.put(PREFIX + ".port", cfCredentials.getPort());
}
}
} else {
UriInfo uriInfo = new UriInfo(uri);
Expand All @@ -81,4 +95,40 @@ public CfEnvProcessorProperties getProperties() {
.build();
}

boolean configureSentinelSetup(Map<String, Object> credentialValues) {
return credentialValues.containsKey("sentinels");
}

boolean useSentinelTls(Map<String, Object> credentialValues) {
if (credentialValues.get("sentinels") instanceof List<?> sentinels) {
for (Object o : sentinels) {
Map<String, Object> sentinel = (Map<String, Object>) o;
if (sentinel.containsKey("tls_port")) {
return true;
}
}
}
return false;
}

private String extractSentinelNodes(Map<String, Object> credentialValues) {
List<String> nodes = new ArrayList<>();
if (credentialValues.containsKey("sentinels")) {
if (credentialValues.get("sentinels") instanceof List<?> sentinels) {
for (Object o : sentinels) {
Map<String, Object> sentinel = (Map<String, Object>) o;
String port;
if (sentinel.containsKey("tls_port")) {
port = sentinel.get("tls_port").toString();
}
else {
port = sentinel.get("port").toString();
}

nodes.add(sentinel.get("host") + ":" + port);
}
}
}
return String.join(",", nodes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package io.pivotal.cfenv.spring.boot;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import org.springframework.core.env.Environment;
Expand Down Expand Up @@ -83,6 +86,56 @@ public void testNoCredentials() {
assertThat(environment.getProperty(SPRING_DATA_REDIS + ".ssl")).isNull();
}

@Test
public void testRedisBootPropertiesSentinelSetup() {
String payload = new RedisFileSentinelPayloadBuilder("test-redis-info-sentinel.json")
.withServiceName("redis-ha-1")
.withName("redis-db")
.withPassword("password")
.withRedisPort(port)
.withSentinelMaster("my-master")
.withSentinelPassword("sentinel-password")
.withSentinel("sent1", 26379)
.withSentinel("sent2", 26380)
.payload();

mockVcapServices(getServicesPayload(payload));

Environment environment = getEnvironment();

assertThat(environment.getProperty(SPRING_DATA_REDIS + ".password")).isEqualTo("password");
assertThat(environment.getProperty(SPRING_DATA_REDIS + ".sentinel.master")).isEqualTo("my-master");
assertThat(environment.getProperty(SPRING_DATA_REDIS + ".sentinel.nodes")).isEqualTo("sent1:26379,sent2:26380");
assertThat(environment.getProperty(SPRING_DATA_REDIS + ".sentinel.username")).isEqualTo("");
assertThat(environment.getProperty(SPRING_DATA_REDIS + ".sentinel.password")).isEqualTo("sentinel-password");
assertThat(environment.getProperty(SPRING_DATA_REDIS + ".ssl.enabled")).isEqualTo("false");
}

@Test
public void testRedisBootPropertiesSentinelTlsSetup() {
String payload = new RedisFileSentinelPayloadBuilder("test-redis-info-sentinel-tls.json")
.withServiceName("redis-ha-1")
.withName("redis-db")
.withPassword("password")
.withRedisPort(port)
.withSentinelMaster("my-master")
.withSentinelPassword("sentinel-password")
.withSentinelTls("sent1", 26379, 26380)
.withSentinelTls("sent2", 26380, 26381)
.payload();

mockVcapServices(getServicesPayload(payload));

Environment environment = getEnvironment();

assertThat(environment.getProperty(SPRING_DATA_REDIS + ".password")).isEqualTo("password");
assertThat(environment.getProperty(SPRING_DATA_REDIS + ".sentinel.master")).isEqualTo("my-master");
assertThat(environment.getProperty(SPRING_DATA_REDIS + ".sentinel.nodes")).isEqualTo("sent1:26380,sent2:26381");
assertThat(environment.getProperty(SPRING_DATA_REDIS + ".sentinel.username")).isEqualTo("");
assertThat(environment.getProperty(SPRING_DATA_REDIS + ".sentinel.password")).isEqualTo("sentinel-password");
assertThat(environment.getProperty(SPRING_DATA_REDIS + ".ssl.enabled")).isEqualTo("true");
}

@Test
public void testGetProperties() {
assertThat(new RedisCfEnvProcessor().getProperties().getPropertyPrefixes()).isEqualTo(SPRING_DATA_REDIS);
Expand Down Expand Up @@ -149,6 +202,84 @@ String payload() {
.replace("$tls_port", String.valueOf(tlsPort))
.replace("$name", name);
}
}

private class RedisFileSentinelPayloadBuilder {

private String payload;
private String serviceName;
private String name;
private Integer redisPort;
private String password;
private String sentinelPassword;
private String sentinelMaster;
private List<SentinelDefinition> sentinels;

RedisFileSentinelPayloadBuilder(String filename) {
this.payload = readTestDataFile(filename);
this.sentinels = new ArrayList<>();
}

RedisFileSentinelPayloadBuilder withServiceName(String serviceName) {
this.serviceName = serviceName;
return this;
}

RedisFileSentinelPayloadBuilder withName(String name) {
this.name = name;
return this;
}

RedisFileSentinelPayloadBuilder withRedisPort(int redisPort) {
this.redisPort = redisPort;
return this;
}

RedisFileSentinelPayloadBuilder withPassword(String password) {
this.password = password;
return this;
}

RedisFileSentinelPayloadBuilder withSentinelPassword(String sentinelPassword) {
this.sentinelPassword = sentinelPassword;
return this;
}

RedisFileSentinelPayloadBuilder withSentinel(String host, int port) {
return withSentinelTls(host, port, -1);
}

RedisFileSentinelPayloadBuilder withSentinelTls(String host, int port, int tlsPort) {
this.sentinels.add(new SentinelDefinition(host, port, tlsPort));
return this;
}

RedisFileSentinelPayloadBuilder withSentinelMaster(String sentinelMaster) {
this.sentinelMaster = sentinelMaster;
return this;
}

String payload() {
var result = payload.replace("$serviceName", serviceName)
.replace("$redisPort", String.valueOf(redisPort))
.replace("$redisPassword", password)
.replace("$sentinelPassword", sentinelPassword)
.replace("$sentinelMaster", sentinelMaster)
.replace("$name", name);
for (int i = 0; i < this.sentinels.size(); i++) {
var sentinelDefinition = this.sentinels.get(i);

var hostKey = "$sentinel" + (i+1) + "-hostname";
var portKey = "$sentinel" + (i+1) + "-port";
var tlsPortKey = "$sentinel" + (i+1) + "-tlsPort";
result = result.replace(hostKey, sentinelDefinition.host);
result = result.replace(portKey, Integer.toString(sentinelDefinition.port));
result = result.replace(tlsPortKey, Integer.toString(sentinelDefinition.tlsPort));
}

return result;
}
}

record SentinelDefinition(String host, int port, int tlsPort) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "$serviceName",
"label": "rediscloud",
"plan": "ha",
"tags": [ "redis", "key-value" ],
"credentials": {
"master_name": "$sentinelMaster",
"password": "$redisPassword",
"port": "$redisPort",
"sentinel_password": "$sentinelPassword",
"sentinels": [
{
"host": "$sentinel1-hostname",
"port": "$sentinel1-port",
"tls_port": "$sentinel1-tlsPort"
},
{
"host": "$sentinel2-hostname",
"port": "$sentinel2-port",
"tls_port": "$sentinel2-tlsPort"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "$serviceName",
"label": "rediscloud",
"plan": "ha",
"tags": [ "redis", "key-value" ],
"credentials": {
"master_name": "$sentinelMaster",
"password": "$redisPassword",
"port": "$redisPort",
"sentinel_password": "$sentinelPassword",
"sentinels": [
{
"host": "$sentinel1-hostname",
"port": "$sentinel1-port"
},
{
"host": "$sentinel2-hostname",
"port": "$sentinel2-port"
}
]
}
}