Configure Redis connections to be secure when the rediss:// scheme is detected in the service info.
This commit is contained in:
@@ -7,13 +7,14 @@ import org.springframework.cloud.service.common.RedisServiceInfo;
|
||||
/**
|
||||
*
|
||||
* @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) {
|
||||
@@ -24,7 +25,7 @@ public class RedisServiceInfoCreator extends CloudFoundryServiceInfoCreator<Redi
|
||||
|
||||
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);
|
||||
|
||||
@@ -47,7 +47,7 @@ public class CloudFoundryConnectorRedisServiceTest extends AbstractCloudFoundryC
|
||||
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,
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "$serviceName",
|
||||
"credentials": {
|
||||
"uri": "rediss://$username:$password@$hostname:$port"
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "$serviceName",
|
||||
"credentials": {
|
||||
"uri": "redis://$username:$password@$hostname:$port"
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,14 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel;
|
||||
/**
|
||||
*
|
||||
* @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);
|
||||
|
||||
@@ -51,6 +51,10 @@ public class RedisConnectionFactoryCreator extends AbstractServiceConnectorCreat
|
||||
clientConfigurer.configure(builder, (PooledServiceConnectorConfig) serviceConnectorConfig);
|
||||
}
|
||||
|
||||
if (connectionIsSecure(serviceInfo)) {
|
||||
builder.useSsl();
|
||||
}
|
||||
|
||||
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration, builder.build());
|
||||
connectionFactory.afterPropertiesSet();
|
||||
return connectionFactory;
|
||||
@@ -69,6 +73,10 @@ public class RedisConnectionFactoryCreator extends AbstractServiceConnectorCreat
|
||||
builder = LettuceClientConfiguration.builder();
|
||||
}
|
||||
|
||||
if (connectionIsSecure(serviceInfo)) {
|
||||
builder.useSsl();
|
||||
}
|
||||
|
||||
RedisLettuceClientConfigurer clientConfigurer = new RedisLettuceClientConfigurer();
|
||||
if (serviceConnectorConfig instanceof RedisConnectionFactoryConfig) {
|
||||
clientConfigurer.configure(builder, (RedisConnectionFactoryConfig) serviceConnectorConfig);
|
||||
@@ -87,4 +95,8 @@ public class RedisConnectionFactoryCreator extends AbstractServiceConnectorCreat
|
||||
serviceInfo.getId(), JEDIS_CLASS_NAME, LETTUCE_CLASS_NAME));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean connectionIsSecure(RedisServiceInfo serviceInfo) {
|
||||
return RedisServiceInfo.REDISS_SCHEME.equalsIgnoreCase(serviceInfo.getScheme());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactor
|
||||
/**
|
||||
*
|
||||
* @author Ramnivas Laddad
|
||||
* @author Scott Frederick
|
||||
*
|
||||
*/
|
||||
public class RedisServiceConnectorCreatorTest {
|
||||
@@ -36,15 +37,25 @@ public class RedisServiceConnectorCreatorTest {
|
||||
}
|
||||
|
||||
@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);
|
||||
@@ -52,7 +63,8 @@ public class RedisServiceConnectorCreatorTest {
|
||||
return mockRedisServiceInfo;
|
||||
}
|
||||
|
||||
private void assertConnectorProperties(RedisServiceInfo serviceInfo, RedisConnectionFactory connector) {
|
||||
private void assertConnectorProperties(RedisServiceInfo serviceInfo, RedisConnectionFactory connector,
|
||||
boolean isSecure) {
|
||||
assertNotNull(connector);
|
||||
|
||||
if (connector instanceof JedisConnectionFactory) {
|
||||
@@ -60,11 +72,13 @@ public class RedisServiceConnectorCreatorTest {
|
||||
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() +
|
||||
|
||||
Reference in New Issue
Block a user