Add support for enabling SSL on Spring Boot configured and bootstrapped Apache Geode/Pivotal GemFire Servers.

This commit is contained in:
John Blum
2018-05-28 19:47:40 -07:00
parent f96e7ab59b
commit bdb92980ab
2 changed files with 10 additions and 120 deletions

View File

@@ -26,7 +26,7 @@ import java.net.URL;
import java.util.Optional;
import java.util.Properties;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.GemFireCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
@@ -46,7 +46,7 @@ import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.data.gemfire.client.ClientCacheFactoryBean;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.config.annotation.EnableSsl;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
@@ -62,7 +62,7 @@ import org.springframework.util.StringUtils;
* @see java.net.URL
* @see java.util.Properties
* @see org.springframework.boot.SpringApplication
* @see org.apache.geode.cache.client.ClientCache
* @see org.apache.geode.cache.GemFireCache
* @see org.springframework.boot.autoconfigure.EnableAutoConfiguration
* @see org.springframework.boot.data.geode.autoconfigure.ClientCacheAutoConfiguration
* @see org.springframework.boot.env.EnvironmentPostProcessor
@@ -72,14 +72,14 @@ import org.springframework.util.StringUtils;
* @see org.springframework.core.env.ConfigurableEnvironment
* @see org.springframework.core.env.Environment
* @see org.springframework.core.io.Resource
* @see org.springframework.data.gemfire.client.ClientCacheFactoryBean
* @see org.springframework.data.gemfire.CacheFactoryBean
* @see org.springframework.data.gemfire.config.annotation.EnableSsl
* @since 1.0.0
*/
@Configuration
@AutoConfigureBefore(ClientCacheAutoConfiguration.class)
@Conditional(SslAutoConfiguration.EnableSslCondition.class)
@ConditionalOnClass({ ClientCacheFactoryBean.class, ClientCache.class })
@ConditionalOnClass({ CacheFactoryBean.class, GemFireCache.class })
@EnableSsl
@SuppressWarnings("unused")
public class SslAutoConfiguration {
@@ -271,6 +271,7 @@ public class SslAutoConfiguration {
@ConditionalOnProperty(prefix = "spring.data.gemfire.security.ssl", name = { "keystore", "truststore", })
static class SpringDataGeodeSslContextCondition {}
// TODO: ;-)
@ConditionalOnProperty({
GEMFIRE_SSL_KEYSTORE_PROPERTY,
GEMFIRE_SSL_TRUSTSTORE_PROPERTY,

View File

@@ -17,13 +17,9 @@
package org.springframework.boot.data.geode.security.ssl;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.geode.cache.GemFireCache;
@@ -33,28 +29,18 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.data.geode.core.util.ObjectUtils;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.data.gemfire.config.annotation.CacheServerApplication;
import org.springframework.data.gemfire.config.annotation.EnableLogging;
import org.springframework.data.gemfire.config.annotation.EnableSsl;
import org.springframework.data.gemfire.config.annotation.PeerCacheConfigurer;
import org.springframework.data.gemfire.tests.integration.ForkingClientServerIntegrationTestsSupport;
import org.springframework.data.gemfire.tests.integration.config.ClientServerIntegrationTestsConfiguration;
import org.springframework.data.gemfire.tests.util.FileSystemUtils;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import example.geode.cache.EchoCacheLoader;
@@ -88,89 +74,7 @@ public class AutoConfiguredSslIntegrationTests extends ForkingClientServerIntegr
@BeforeClass
public static void startGemFireServer() throws IOException {
startGemFireServer(GemFireServerConfiguration.class, javaxNetSslKeyStorePropertyFromClassPath());
}
private static String javaxNetSslKeyStorePropertyFromClassPath() {
return String.format("-Djavax.net.ssl.keyStore=%s", locateKeyStoreInClassPath()
.filter(StringUtils::hasText)
.orElseThrow(() -> newIllegalStateException("KeyStore file [%s] was not found",
TRUSTED_KEYSTORE_FILENAME)));
}
private static String javaxNetSslKeyStorePropertyFromFileSystem() {
return String.format("-Djavax.net.ssl.keyStore=%s", locateKeyStoreInFileSystem()
.map(File::getAbsolutePath)
.filter(StringUtils::hasText)
.orElseThrow(() -> newIllegalStateException("KeyStore file [%s] was not found",
TRUSTED_KEYSTORE_FILENAME)));
}
private static Optional<String> locateKeyStoreInClassPath() {
return locateKeyStoreInClassPath(TRUSTED_KEYSTORE_FILENAME);
}
@SuppressWarnings("all")
private static Optional<String> locateKeyStoreInClassPath(String keystoreName) {
/*
System.err.printf("KEYSTORE LOCATION [%s]%n", ObjectUtils.doOperationSafely(() ->
new File(new ClassPathResource(keystoreName).getURL().toURI())).getAbsolutePath());
*/
return Optional.of(new ClassPathResource(keystoreName))
.filter(Resource::exists)
.map(it -> ObjectUtils.doOperationSafely(() -> it.getURL()))
.map(url -> ObjectUtils.doOperationSafely(() -> url.toURI()))
.map(uri -> ObjectUtils.doOperationSafely(() -> new File(uri)))
.filter(File::isFile)
.map(File::getAbsolutePath);
}
private static Optional<File> locateKeyStoreInFileSystem() {
return locateKeyStoreInFileSystem(FileSystemUtils.WORKING_DIRECTORY);
}
private static Optional<File> locateKeyStoreInFileSystem(File directory) {
return locateKeyStoreInFileSystem(directory, TRUSTED_KEYSTORE_FILENAME);
}
private static Optional<File> locateKeyStoreInFileSystem(String keystoreFileName) {
return locateKeyStoreInFileSystem(FileSystemUtils.WORKING_DIRECTORY, keystoreFileName);
}
@SuppressWarnings("all")
private static Optional<File> locateKeyStoreInFileSystem(File directory, String keystoreFilename) {
Assert.isTrue(directory != null && directory.isDirectory(),
String.format("[%s] is not a valid directory", directory));
//System.err.printf("Searching [%s]...%n", directory);
for (File file : nullSafeArray(directory.listFiles(), File.class)) {
//System.err.printf("Testing [%s]...%n", file);
if (file.isDirectory()) {
Optional<File> theFile = locateKeyStoreInFileSystem(file, keystoreFilename);
if (theFile.isPresent()) {
return theFile;
}
else {
continue;
}
}
if (file.getName().equals(keystoreFilename)) {
return Optional.of(file);
}
}
return Optional.empty();
startGemFireServer(GemFireServerConfiguration.class, "-Dspring.profiles.active=ssl");
}
@AfterClass
@@ -216,27 +120,12 @@ public class AutoConfiguredSslIntegrationTests extends ForkingClientServerIntegr
}
}
@SpringBootApplication
@CacheServerApplication(name = "AutoConfiguredSslIntegrationTests", logLevel = GEMFIRE_LOG_LEVEL)
@PropertySource(name = "gemfire-ssl", value = "application-ssl.properties")
@EnableSsl
static class GemFireServerConfiguration {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext(GemFireServerConfiguration.class);
applicationContext.registerShutdownHook();
}
@Bean
PeerCacheConfigurer cacheServerSslConfigurer(
@Value("${javax.net.ssl.keyStore:trusted.keystore}") String keystoreLocation) {
return (beanName, bean) -> {
bean.getProperties().setProperty("ssl-keystore", keystoreLocation);
bean.getProperties().setProperty("ssl-truststore", keystoreLocation);
};
SpringApplication.run(GemFireServerConfiguration.class);
}
@Bean("Echo")