Skip to content

Commit

Permalink
Configure Redis connections to be secure when the rediss:// scheme is…
Browse files Browse the repository at this point in the history
… detected in the service info.
  • Loading branch information
scottfrederick committed Sep 10, 2018
1 parent b66b97a commit dbf8dfa
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
/**
*
* @author Ramnivas Laddad
* @author Scott Frederick
*
*/
public class RedisServiceInfoCreator extends CloudFoundryServiceInfoCreator<RedisServiceInfo> {

public RedisServiceInfoCreator() {
// the literal in the tag is CloudFoundry-specific
super(new Tags("redis"), RedisServiceInfo.REDIS_SCHEME);
super(new Tags("redis"), RedisServiceInfo.REDIS_SCHEME, RedisServiceInfo.REDISS_SCHEME);
}

public RedisServiceInfo createServiceInfo(Map<String,Object> serviceData) {
Expand All @@ -24,7 +25,7 @@ public RedisServiceInfo createServiceInfo(Map<String,Object> serviceData) {

if (uri == null) {
String host = getStringFromCredentials(credentials, "hostname", "host");
Integer port = getIntFromCredentials(credentials, "port");
int port = getIntFromCredentials(credentials, "port");
String password = (String) credentials.get("password");

return new RedisServiceInfo(id, host, port, password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private String getRedisServicePayload(String serviceName,
private String getRedisServicePayloadNoLabelNoTags(String serviceName,
String hostname, int port,
String password, String name) {
return getRedisServicePayload("test-redis-info-no-label-no-tags.json", serviceName, hostname, port, password, name);
return getRedisServicePayload("test-redis-info-no-label-no-tags-secure.json", serviceName, hostname, port, password, name);
}

private String getRedisServicePayload(String payloadFile, String serviceName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "$serviceName",
"credentials": {
"uri": "rediss://$username:$password@$hostname:$port"
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
/**
*
* @author Ramnivas Laddad
* @author Scott Frederick
*
*/
@ServiceLabel("redis")
public class RedisServiceInfo extends UriBasedServiceInfo {

public static final String REDIS_SCHEME = "redis";
public static final String REDISS_SCHEME = "rediss";

public RedisServiceInfo(String id, String host, int port, String password) {
super(id, REDIS_SCHEME, host, port, null, password, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public RedisConnectionFactory create(RedisServiceInfo serviceInfo, ServiceConnec
clientConfigurer.configure(builder, (PooledServiceConnectorConfig) serviceConnectorConfig);
}

if (connectionIsSecure(serviceInfo)) {
builder.useSsl();
}

JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration, builder.build());
connectionFactory.afterPropertiesSet();
return connectionFactory;
Expand All @@ -69,6 +73,10 @@ else if (hasClass(LETTUCE_CLASS_NAME)) {
builder = LettuceClientConfiguration.builder();
}

if (connectionIsSecure(serviceInfo)) {
builder.useSsl();
}

RedisLettuceClientConfigurer clientConfigurer = new RedisLettuceClientConfigurer();
if (serviceConnectorConfig instanceof RedisConnectionFactoryConfig) {
clientConfigurer.configure(builder, (RedisConnectionFactoryConfig) serviceConnectorConfig);
Expand All @@ -87,4 +95,8 @@ else if (hasClass(LETTUCE_CLASS_NAME)) {
serviceInfo.getId(), JEDIS_CLASS_NAME, LETTUCE_CLASS_NAME));
}
}

private boolean connectionIsSecure(RedisServiceInfo serviceInfo) {
return RedisServiceInfo.REDISS_SCHEME.equalsIgnoreCase(serviceInfo.getScheme());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/**
*
* @author Ramnivas Laddad
* @author Scott Frederick
*
*/
public class RedisServiceConnectorCreatorTest {
Expand All @@ -36,35 +37,48 @@ public void setup() {
}

@Test
public void cloudRedisCreationNoConfig() throws Exception {
RedisServiceInfo serviceInfo = createServiceInfo();
public void cloudRedisCreationNoConfig() {
RedisServiceInfo serviceInfo = createServiceInfo(RedisServiceInfo.REDIS_SCHEME);

RedisConnectionFactory dataSource = testCreator.create(serviceInfo, null);

assertConnectorProperties(serviceInfo, dataSource);
assertConnectorProperties(serviceInfo, dataSource, false);
}

public RedisServiceInfo createServiceInfo() {
@Test
public void cloudRedisCreationSecureConnection() {
RedisServiceInfo serviceInfo = createServiceInfo(RedisServiceInfo.REDISS_SCHEME);

RedisConnectionFactory dataSource = testCreator.create(serviceInfo, null);

assertConnectorProperties(serviceInfo, dataSource, true);
}

public RedisServiceInfo createServiceInfo(String scheme) {
when(mockRedisServiceInfo.getScheme()).thenReturn(scheme);
when(mockRedisServiceInfo.getHost()).thenReturn(TEST_HOST);
when(mockRedisServiceInfo.getPort()).thenReturn(TEST_PORT);
when(mockRedisServiceInfo.getPassword()).thenReturn(TEST_PASSWORD);

return mockRedisServiceInfo;
}

private void assertConnectorProperties(RedisServiceInfo serviceInfo, RedisConnectionFactory connector) {
private void assertConnectorProperties(RedisServiceInfo serviceInfo, RedisConnectionFactory connector,
boolean isSecure) {
assertNotNull(connector);

if (connector instanceof JedisConnectionFactory) {
JedisConnectionFactory connectionFactory = (JedisConnectionFactory) connector;
assertEquals(serviceInfo.getHost(), connectionFactory.getHostName());
assertEquals(serviceInfo.getPort(), connectionFactory.getPort());
assertEquals(serviceInfo.getPassword(), connectionFactory.getPassword());
assertEquals(isSecure, connectionFactory.isUseSsl());
} else if (connector instanceof LettuceConnectionFactory) {
LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) connector;
assertEquals(serviceInfo.getHost(), connectionFactory.getHostName());
assertEquals(serviceInfo.getPort(), connectionFactory.getPort());
assertEquals(serviceInfo.getPassword(), connectionFactory.getPassword());
assertEquals(isSecure, connectionFactory.isUseSsl());
} else {
fail("Expected RedisConnectionFactory of type " +
JedisConnectionFactory.class.getName() + " or " + LettuceConnectionFactory.class.getName() +
Expand Down

0 comments on commit dbf8dfa

Please sign in to comment.