diff --git a/spring-cloud-cloudfoundry-connector/src/main/java/org/springframework/cloud/cloudfoundry/RedisServiceInfoCreator.java b/spring-cloud-cloudfoundry-connector/src/main/java/org/springframework/cloud/cloudfoundry/RedisServiceInfoCreator.java index 7efdf790..6a53a68f 100644 --- a/spring-cloud-cloudfoundry-connector/src/main/java/org/springframework/cloud/cloudfoundry/RedisServiceInfoCreator.java +++ b/spring-cloud-cloudfoundry-connector/src/main/java/org/springframework/cloud/cloudfoundry/RedisServiceInfoCreator.java @@ -7,13 +7,14 @@ /** * * @author Ramnivas Laddad + * @author Scott Frederick * */ public class RedisServiceInfoCreator extends CloudFoundryServiceInfoCreator { 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 serviceData) { @@ -24,7 +25,7 @@ public RedisServiceInfo createServiceInfo(Map 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); diff --git a/spring-cloud-cloudfoundry-connector/src/test/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnectorRedisServiceTest.java b/spring-cloud-cloudfoundry-connector/src/test/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnectorRedisServiceTest.java index 1be5666a..1131cfbc 100644 --- a/spring-cloud-cloudfoundry-connector/src/test/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnectorRedisServiceTest.java +++ b/spring-cloud-cloudfoundry-connector/src/test/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnectorRedisServiceTest.java @@ -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, diff --git a/spring-cloud-cloudfoundry-connector/src/test/resources/org/springframework/cloud/cloudfoundry/test-redis-info-no-label-no-tags-secure.json b/spring-cloud-cloudfoundry-connector/src/test/resources/org/springframework/cloud/cloudfoundry/test-redis-info-no-label-no-tags-secure.json new file mode 100644 index 00000000..2ffca472 --- /dev/null +++ b/spring-cloud-cloudfoundry-connector/src/test/resources/org/springframework/cloud/cloudfoundry/test-redis-info-no-label-no-tags-secure.json @@ -0,0 +1,6 @@ +{ + "name": "$serviceName", + "credentials": { + "uri": "rediss://$username:$password@$hostname:$port" + } +} \ No newline at end of file diff --git a/spring-cloud-cloudfoundry-connector/src/test/resources/org/springframework/cloud/cloudfoundry/test-redis-info-no-label-no-tags.json b/spring-cloud-cloudfoundry-connector/src/test/resources/org/springframework/cloud/cloudfoundry/test-redis-info-no-label-no-tags.json deleted file mode 100644 index 923c78df..00000000 --- a/spring-cloud-cloudfoundry-connector/src/test/resources/org/springframework/cloud/cloudfoundry/test-redis-info-no-label-no-tags.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "$serviceName", - "credentials": { - "uri": "redis://$username:$password@$hostname:$port" - } -} \ No newline at end of file diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/RedisServiceInfo.java b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/RedisServiceInfo.java index e5e2605c..7019ddd4 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/RedisServiceInfo.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/RedisServiceInfo.java @@ -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); diff --git a/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/keyval/RedisConnectionFactoryCreator.java b/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/keyval/RedisConnectionFactoryCreator.java index cb1a720c..c2a288e8 100644 --- a/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/keyval/RedisConnectionFactoryCreator.java +++ b/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/keyval/RedisConnectionFactoryCreator.java @@ -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; @@ -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); @@ -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()); + } } diff --git a/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/redis/RedisServiceConnectorCreatorTest.java b/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/redis/RedisServiceConnectorCreatorTest.java index 227d6381..37b850e9 100644 --- a/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/redis/RedisServiceConnectorCreatorTest.java +++ b/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/redis/RedisServiceConnectorCreatorTest.java @@ -18,6 +18,7 @@ /** * * @author Ramnivas Laddad + * @author Scott Frederick * */ public class RedisServiceConnectorCreatorTest { @@ -36,15 +37,25 @@ 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); @@ -52,7 +63,8 @@ public RedisServiceInfo createServiceInfo() { 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 @@ private void assertConnectorProperties(RedisServiceInfo serviceInfo, RedisConnec 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() +