Inherit from AbstractAutoConfiguredSecurityContextIntegrationTests.

Edit Javadoc.
This commit is contained in:
John Blum
2018-05-28 14:27:11 -07:00
parent b3d1f6cfd8
commit 12a11de856
2 changed files with 21 additions and 147 deletions

View File

@@ -16,51 +16,23 @@
package org.springframework.boot.data.geode.security.auth.local;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.data.gemfire.config.annotation.support.AutoConfiguredAuthenticationInitializer.SDG_SECURITY_PASSWORD_PROPERTY;
import static org.springframework.data.gemfire.config.annotation.support.AutoConfiguredAuthenticationInitializer.SDG_SECURITY_USERNAME_PROPERTY;
import static org.springframework.data.gemfire.config.annotation.support.AutoConfiguredAuthenticationInitializer.SECURITY_PASSWORD_PROPERTY;
import static org.springframework.data.gemfire.config.annotation.support.AutoConfiguredAuthenticationInitializer.SECURITY_USERNAME_PROPERTY;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import java.io.IOException;
import java.io.Serializable;
import java.security.Principal;
import java.util.Optional;
import java.util.Properties;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.apache.geode.security.AuthenticationFailedException;
import org.apache.geode.security.ResourcePermission;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.data.geode.autoconfigure.SslAutoConfiguration;
import org.springframework.boot.data.geode.security.auth.AbstractAutoConfiguredSecurityContextIntegrationTests;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.gemfire.config.annotation.CacheServerApplication;
import org.springframework.data.gemfire.config.annotation.EnableLogging;
import org.springframework.data.gemfire.config.annotation.EnablePdx;
import org.springframework.data.gemfire.config.annotation.EnableSecurity;
import org.springframework.data.gemfire.tests.integration.ForkingClientServerIntegrationTestsSupport;
import org.springframework.data.gemfire.tests.integration.config.ClientServerIntegrationTestsConfiguration;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;
import example.geode.cache.EchoCacheLoader;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* Integration test testing the auto-configuration of Apache Geode/Pivotal GemFire Security
@@ -71,12 +43,14 @@ import lombok.RequiredArgsConstructor;
* @see org.junit.Test
* @see org.apache.geode.cache.GemFireCache
* @see org.springframework.boot.autoconfigure.SpringBootApplication
* @see org.springframework.boot.data.geode.autoconfigure.SecurityAutoConfiguration
* @see org.springframework.boot.data.geode.security.auth.AbstractAutoConfiguredSecurityContextIntegrationTests
* @see org.springframework.boot.test.context.SpringBootTest
* @see org.springframework.context.annotation.AnnotationConfigApplicationContext
* @see org.springframework.context.annotation.Bean
* @see org.springframework.context.annotation.Import
* @see org.springframework.data.gemfire.config.annotation.CacheServerApplication
* @see org.springframework.data.gemfire.config.annotation.EnableSecurity
* @see org.springframework.data.gemfire.tests.integration.ForkingClientServerIntegrationTestsSupport
* @see org.springframework.data.gemfire.tests.integration.config.ClientServerIntegrationTestsConfiguration
* @see org.springframework.test.context.ActiveProfiles
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.0.0
@@ -86,53 +60,26 @@ import lombok.RequiredArgsConstructor;
@SpringBootTest(classes = AutoConfiguredLocalSecurityContextIntegrationTests.GemFireClientConfiguration.class,
webEnvironment = SpringBootTest.WebEnvironment.NONE)
@SuppressWarnings("unused")
public class AutoConfiguredLocalSecurityContextIntegrationTests extends ForkingClientServerIntegrationTestsSupport {
public class AutoConfiguredLocalSecurityContextIntegrationTests
extends AbstractAutoConfiguredSecurityContextIntegrationTests {
private static final String GEMFIRE_LOG_LEVEL = "error";
@BeforeClass
public static void startGemFireServer() throws IOException {
startGemFireServer(GemFireServerConfiguration.class);
}
@Autowired
private GemfireTemplate echoTemplate;
@Test
public void clientServerAuthIsSuccessful() {
assertThat(this.echoTemplate.<String, String>get("Hello")).isEqualTo("Hello");
assertThat(this.echoTemplate.<String, String>get("Test")).isEqualTo("Test");
assertThat(this.echoTemplate.<String, String>get("Good-Bye")).isEqualTo("Good-Bye");
startGemFireServer(GemFireServerConfiguration.class,"-Dspring.profiles.active=security-local");
}
@EnableLogging(logLevel = GEMFIRE_LOG_LEVEL)
@Import(ClientServerIntegrationTestsConfiguration.class)
@SpringBootApplication(exclude = SslAutoConfiguration.class)
static class GemFireClientConfiguration extends ClientServerIntegrationTestsConfiguration {
static class GemFireClientConfiguration extends BaseGemFireClientConfiguration { }
@Bean("Echo")
public ClientRegionFactoryBean<String, String> echoRegion(GemFireCache gemfireCache) {
ClientRegionFactoryBean<String, String> echoRegion = new ClientRegionFactoryBean<>();
echoRegion.setCache(gemfireCache);
echoRegion.setClose(false);
echoRegion.setShortcut(ClientRegionShortcut.PROXY);
return echoRegion;
}
@Bean
GemfireTemplate echoTemplate(GemFireCache gemfireCache) {
return new GemfireTemplate(gemfireCache.getRegion("/Echo"));
}
}
@EnablePdx
@EnableSecurity(securityManagerClassName =
"org.springframework.boot.data.geode.security.auth.local.AutoConfiguredLocalSecurityContextIntegrationTests$TestSecurityManager")
@CacheServerApplication(name = "AutoConfiguredLocalSecurityContextIntegrationTests", logLevel = GEMFIRE_LOG_LEVEL)
static class GemFireServerConfiguration {
@CacheServerApplication(name = "AutoConfiguredLocalSecurityContextIntegrationTests", logLevel = GEMFIRE_LOG_LEVEL,
useBeanFactoryLocator = true)
@EnableSecurity(securityManagerClassName = "org.springframework.boot.data.geode.security.support.SecurityManagerProxy")
@PropertySource("application-security-local.properties")
static class GemFireServerConfiguration extends BaseGemFireServerConfiguration {
public static void main(String[] args) {
@@ -141,81 +88,5 @@ public class AutoConfiguredLocalSecurityContextIntegrationTests extends ForkingC
applicationContext.registerShutdownHook();
}
@Bean("Echo")
public PartitionedRegionFactoryBean<String, String> echoRegion(GemFireCache gemfireCache) {
PartitionedRegionFactoryBean<String, String> echoRegion = new PartitionedRegionFactoryBean<>();
echoRegion.setCache(gemfireCache);
echoRegion.setCacheLoader(EchoCacheLoader.INSTANCE);
echoRegion.setClose(false);
echoRegion.setPersistent(false);
return echoRegion;
}
}
public static class TestSecurityManager implements org.apache.geode.security.SecurityManager {
private final String username;
private final String password;
public TestSecurityManager() throws IOException {
Properties securityProperties = new Properties();
securityProperties.load(new ClassPathResource("application-security-local.properties").getInputStream());
this.username = Optional.ofNullable(securityProperties.getProperty(SDG_SECURITY_USERNAME_PROPERTY))
.filter(StringUtils::hasText)
.orElseThrow(() -> newIllegalArgumentException("Username is required"));
this.password = Optional.ofNullable(securityProperties.getProperty(SDG_SECURITY_PASSWORD_PROPERTY))
.filter(StringUtils::hasText)
.orElseThrow(() -> newIllegalArgumentException("Password is required"));
}
@Override
public Object authenticate(Properties credentials) throws AuthenticationFailedException {
String username = credentials.getProperty(SECURITY_USERNAME_PROPERTY);
String password = credentials.getProperty(SECURITY_PASSWORD_PROPERTY);
if (!(this.username.equals(username) && this.password.equals(password))) {
throw new AuthenticationFailedException(String.format("Failed to authenticate user [%s]", username));
}
return User.with(username).having(password);
}
/**
* @inheritDoc
*/
@Override
public boolean authorize(Object principal, ResourcePermission permission) {
return true;
}
}
@Data
@EqualsAndHashCode(of = "name")
@RequiredArgsConstructor(staticName = "with")
public static class User implements Principal, Serializable {
@NonNull
private String name;
private String password;
User having(String password) {
setPassword(password);
return this;
}
@Override
public String toString() {
return getName();
}
}
}

View File

@@ -1,3 +1,6 @@
# Spring Boot application.properties for Local Security Context testing
# Spring Boot application.properties for testing security in a local context.
spring.data.gemfire.security.username=ghostrider
spring.data.gemfire.security.password=p@55w0rd
security.context.username.property=${spring.data.gemfire.security.username}
security.context.password.property=${spring.data.gemfire.security.password}