diff --git a/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/SslAutoConfiguration.java b/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/SslAutoConfiguration.java index 13073733..57152b9a 100644 --- a/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/SslAutoConfiguration.java +++ b/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/SslAutoConfiguration.java @@ -27,9 +27,6 @@ import java.util.Properties; import org.apache.geode.cache.GemFireCache; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -56,6 +53,9 @@ import org.springframework.util.FileCopyUtils; import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Spring Boot {@link EnableAutoConfiguration auto-configuration} enabling Apache Geode's SSL transport * between client and servers when using the client/server topology. @@ -102,8 +102,10 @@ public class SslAutoConfiguration { private static final String GEMFIRE_SSL_KEYSTORE_PROPERTY = "gemfire.ssl-keystore"; private static final String GEMFIRE_SSL_PROPERTY_SOURCE_NAME = "gemfire-ssl"; private static final String GEMFIRE_SSL_TRUSTSTORE_PROPERTY = "gemfire.ssl-truststore"; - private static final String SECURITY_SSL_KEYSTORE_PROPERTY = "spring.data.gemfire.security.ssl.keystore"; - private static final String SECURITY_SSL_TRUSTSTORE_PROPERTY = "spring.data.gemfire.security.ssl.truststore"; + private static final String SECURITY_SSL_PROPERTY_PREFIX = "spring.data.gemfire.security.ssl"; + private static final String SECURITY_SSL_KEYSTORE_PROPERTY = SECURITY_SSL_PROPERTY_PREFIX + ".keystore"; + private static final String SECURITY_SSL_TRUSTSTORE_PROPERTY = SECURITY_SSL_PROPERTY_PREFIX + ".truststore"; + private static final String SECURITY_SSL_USE_DEFAULT_CONTEXT = SECURITY_SSL_PROPERTY_PREFIX + ".use-default-context"; private static final String TRUSTED_KEYSTORE_FILENAME = "trusted.keystore"; private static final String TRUSTED_KEYSTORE_FILENAME_PROPERTY = "spring.boot.data.gemfire.security.ssl.keystore.name"; private static final String USER_HOME_DIRECTORY = System.getProperty("user.home"); @@ -312,9 +314,12 @@ public class SslAutoConfiguration { @Conditional(TrustedKeyStoreIsPresentCondition.class) static class TrustedKeyStoreCondition { } - @ConditionalOnProperty(prefix = "spring.data.gemfire.security.ssl", name = { "keystore", "truststore" }) + @ConditionalOnProperty(prefix = SECURITY_SSL_PROPERTY_PREFIX, name = { "keystore", "truststore" }) static class SpringDataGemFireSecuritySslKeyStoreAndTruststorePropertiesSet { } + @ConditionalOnProperty(SECURITY_SSL_USE_DEFAULT_CONTEXT) + static class SpringDataGeodeSslUseDefaultContextPropertySet { } + @ConditionalOnProperty({ GEMFIRE_SSL_KEYSTORE_PROPERTY, GEMFIRE_SSL_TRUSTSTORE_PROPERTY }) static class ApacheGeodeSslKeyStoreAndTruststorePropertiesSet { } diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/tls/AbstractTlsEnabledAutoConfigurationIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/tls/AbstractTlsEnabledAutoConfigurationIntegrationTests.java new file mode 100644 index 00000000..3b1e5838 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/tls/AbstractTlsEnabledAutoConfigurationIntegrationTests.java @@ -0,0 +1,75 @@ +/* + * Copyright 2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package org.springframework.geode.boot.autoconfigure.security.tls; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Before; +import org.junit.Test; + +import org.apache.geode.cache.client.ClientCache; +import org.apache.geode.distributed.ConfigurationProperties; +import org.apache.geode.distributed.DistributedSystem; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport; + +/** + * Abstract Integration Test base class containing tests for TLS configuration in a Cloud Platform Environment/Context. + * + * @author John Blum + * @see org.apache.geode.cache.client.ClientCache + * @see org.apache.geode.distributed.ConfigurationProperties + * @see org.apache.geode.distributed.DistributedSystem + * @see org.springframework.core.env.Environment + * @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport + * @since 1.3.0 + */ +@SuppressWarnings("unused") +public abstract class AbstractTlsEnabledAutoConfigurationIntegrationTests extends IntegrationTestsSupport { + + @Autowired + protected ClientCache clientCache; + + @Autowired + protected Environment environment; + + @Before + public void setup() { + + assertThat(this.clientCache).describedAs("ClientCache was not configured").isNotNull(); + assertThat(this.environment).describedAs("Environment was not configured").isNotNull(); + } + + @Test + public void environmentContainsSpringDataGeodeSecuritySslUseDefaultContextProperty() { + + assertThat(this.environment.getProperty("spring.data.gemfire.security.ssl.use-default-context", + Boolean.class, false)).isTrue(); + } + + @Test + public void geodeClientIsConfiguredWithSslUsingDefaultContext() { + + DistributedSystem distributedSystem = this.clientCache.getDistributedSystem(); + + assertThat(distributedSystem).isNotNull(); + assertThat(distributedSystem.getProperties()).isNotNull(); + assertThat(distributedSystem.getProperties().getProperty(ConfigurationProperties.SSL_USE_DEFAULT_CONTEXT, "false")) + .isEqualTo("true"); + } +} diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/tls/MockedTlsEnabledAutoConfigurationIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/tls/MockedTlsEnabledAutoConfigurationIntegrationTests.java new file mode 100644 index 00000000..bac811b8 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/tls/MockedTlsEnabledAutoConfigurationIntegrationTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package org.springframework.geode.boot.autoconfigure.security.tls; + +import org.junit.Before; +import org.junit.runner.RunWith; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Profile; +import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects; +import org.springframework.geode.util.GeodeAssertions; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Integration Tests testing the configuration of TLS (e.g. SSL) in a Cloud Platform Environment/Context (e.g. PCF) + * using GemFire Mock Objects. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.boot.autoconfigure.SpringBootApplication + * @see org.springframework.boot.test.context.SpringBootTest + * @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects + * @see org.springframework.geode.boot.autoconfigure.security.tls.AbstractTlsEnabledAutoConfigurationIntegrationTests + * @see org.springframework.test.context.junit4.SpringRunner + * @since 1.3.0 + */ +@ActiveProfiles("mock-tls") +@RunWith(SpringRunner.class) +@SpringBootTest(classes = MockedTlsEnabledAutoConfigurationIntegrationTests.TestConfiguration.class, + properties = { + "VCAP_APPLICATION={ \"name\": \"MockedTlsEnabledAutoConfigurationIntegrationTests\", \"uris\": [] }", + "VCAP_SERVICES={ \"p-cloudcache\": [{ \"credentials\": { \"tls-enabled\": \"true\" }, \"name\": \"jblum-pcc\", \"tags\": [ \"gemfire\", \"cloudcache\", \"database\", \"pivotal\" ]}]}" + } +) +@SuppressWarnings("unused") +public class MockedTlsEnabledAutoConfigurationIntegrationTests + extends AbstractTlsEnabledAutoConfigurationIntegrationTests { + + @Before + public void testWithMockGemFireObjects() { + GeodeAssertions.assertThat(this.clientCache).isNotInstanceOfGemFireCacheImpl(); + GeodeAssertions.assertThat(this.clientCache.getDistributedSystem()).isNotInstanceOfInternalDistributedSystem(); + } + + @SpringBootApplication + @EnableGemFireMockObjects + @Profile("mock-tls") + static class TestConfiguration { } + +} diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/tls/TlsEnabledAutoConfigurationIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/tls/TlsEnabledAutoConfigurationIntegrationTests.java new file mode 100644 index 00000000..6317ff60 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/tls/TlsEnabledAutoConfigurationIntegrationTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package org.springframework.geode.boot.autoconfigure.security.tls; + +import org.junit.Before; +import org.junit.runner.RunWith; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Profile; +import org.springframework.geode.util.GeodeAssertions; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Integration Tests testing the configuration of TLS (e.g. SSL) in a Cloud Platform Environment/Context (e.g. PCF) + * using Live GemFire Objects. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.boot.autoconfigure.SpringBootApplication + * @see org.springframework.boot.test.context.SpringBootTest + * @see org.springframework.geode.boot.autoconfigure.security.tls.AbstractTlsEnabledAutoConfigurationIntegrationTests + * @see org.springframework.test.context.junit4.SpringRunner + * @since 1.3.0 + */ +@ActiveProfiles("tls") +@RunWith(SpringRunner.class) +@SpringBootTest(classes = TlsEnabledAutoConfigurationIntegrationTests.TestConfiguration.class, + properties = { + "VCAP_APPLICATION={ \"name\": \"MockedTlsEnabledAutoConfigurationIntegrationTests\", \"uris\": [] }", + "VCAP_SERVICES={ \"p-cloudcache\": [{ \"credentials\": { \"tls-enabled\": \"true\" }, \"name\": \"jblum-pcc\", \"tags\": [ \"gemfire\", \"cloudcache\", \"database\", \"pivotal\" ]}]}" + } +) +@SuppressWarnings("unused") +public class TlsEnabledAutoConfigurationIntegrationTests extends AbstractTlsEnabledAutoConfigurationIntegrationTests { + + @Before + public void testWithLiveGeodeObjects() { + GeodeAssertions.assertThat(this.clientCache).isInstanceOfGemFireCacheImpl(); + GeodeAssertions.assertThat(this.clientCache.getDistributedSystem()).isInstanceOfInternalDistributedSystem(); + } + + @SpringBootApplication + @Profile("tls") + static class TestConfiguration { } + +}