Workaround issue caused by org.springframework.cloud:spring-cloud-services-starter-service-registry.

Resolves gh-21.
This commit is contained in:
John Blum
2019-01-29 10:29:22 -08:00
parent 795da35cde
commit 2e263257f2
2 changed files with 204 additions and 43 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.geode.boot.autoconfigure.security.auth;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
@@ -27,11 +28,16 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Properties;
import org.junit.Test;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.geode.boot.autoconfigure.ClientSecurityAutoConfiguration;
import org.springframework.geode.boot.autoconfigure.ClientSecurityAutoConfiguration.AutoConfiguredCloudSecurityEnvironmentPostProcessor;
import org.springframework.mock.env.MockEnvironment;
/**
* Unit Tests for {@link ClientSecurityAutoConfiguration}.
@@ -55,15 +61,15 @@ public class ClientSecurityAutoConfigurationUnitTests {
ConfigurableEnvironment mockEnvironment = mock(ConfigurableEnvironment.class);
when(mockEnvironment.getProperty(eq(ClientSecurityAutoConfiguration.SECURITY_CLOUD_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
eq(Boolean.class), eq(true))).thenReturn(true);
when(mockEnvironment.containsProperty(eq("VCAP_APPLICATION"))).thenReturn(true);
when(mockEnvironment.getProperty(eq(ClientSecurityAutoConfiguration.CLOUD_SECURITY_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
eq(Boolean.class), eq(true))).thenReturn(true);
environmentPostProcessor.postProcessEnvironment(mockEnvironment, null);
verify(mockEnvironment, times(1))
.getProperty(eq(ClientSecurityAutoConfiguration.SECURITY_CLOUD_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
.getProperty(eq(ClientSecurityAutoConfiguration.CLOUD_SECURITY_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
eq(Boolean.class), eq(true));
verify(mockEnvironment, times(1)).containsProperty(eq("VCAP_APPLICATION"));
@@ -82,21 +88,20 @@ public class ClientSecurityAutoConfigurationUnitTests {
doNothing().when(environmentPostProcessor).configureSecurityContext(any(ConfigurableEnvironment.class));
ConfigurableEnvironment environment = spy(new StandardEnvironment());
ConfigurableEnvironment mockEnvironment = spy(new MockEnvironment());
doReturn(true).when(environment).containsProperty(eq("VCAP_SERVICES"));
doReturn(true).when(mockEnvironment).containsProperty(eq("VCAP_SERVICES"));
environmentPostProcessor.postProcessEnvironment(environment, null);
environmentPostProcessor.postProcessEnvironment(mockEnvironment, null);
verify(environment, times(1))
.getProperty(eq(ClientSecurityAutoConfiguration.SECURITY_CLOUD_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
verify(mockEnvironment, times(1))
.getProperty(eq(ClientSecurityAutoConfiguration.CLOUD_SECURITY_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
eq(Boolean.class), eq(true));
verify(environment, times(1)).containsProperty(eq("VCAP_APPLICATION"));
verify(environment, times(1)).containsProperty(eq("VCAP_SERVICES"));
verify(mockEnvironment, times(1)).containsProperty(eq("VCAP_APPLICATION"));
verify(mockEnvironment, times(1)).containsProperty(eq("VCAP_SERVICES"));
verify(environmentPostProcessor, times(1))
.configureSecurityContext(eq(environment));
verify(environmentPostProcessor, times(1)).configureSecurityContext(eq(mockEnvironment));
}
@Test
@@ -109,16 +114,16 @@ public class ClientSecurityAutoConfigurationUnitTests {
ConfigurableEnvironment mockEnvironment = mock(ConfigurableEnvironment.class);
when(mockEnvironment.getProperty(eq(ClientSecurityAutoConfiguration.SECURITY_CLOUD_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
eq(Boolean.class), eq(true))).thenReturn(false);
when(mockEnvironment.containsProperty(eq("VCAP_APPLICATION"))).thenReturn(true);
when(mockEnvironment.containsProperty(eq("VCAP_SERVICES"))).thenReturn(true);
when(mockEnvironment.getProperty(eq(ClientSecurityAutoConfiguration.CLOUD_SECURITY_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
eq(Boolean.class), eq(true))).thenReturn(false);
environmentPostProcessor.postProcessEnvironment(mockEnvironment, null);
verify(mockEnvironment, times(1))
.getProperty(eq(ClientSecurityAutoConfiguration.SECURITY_CLOUD_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
.getProperty(eq(ClientSecurityAutoConfiguration.CLOUD_SECURITY_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
eq(Boolean.class), eq(true));
verify(mockEnvironment, never()).containsProperty(eq("VCAP_APPLICATION"));
@@ -136,20 +141,124 @@ public class ClientSecurityAutoConfigurationUnitTests {
ConfigurableEnvironment mockEnvironment = mock(ConfigurableEnvironment.class);
when(mockEnvironment.getProperty(eq(ClientSecurityAutoConfiguration.SECURITY_CLOUD_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
eq(Boolean.class), eq(true))).thenReturn(true);
when(mockEnvironment.containsProperty(eq("VCAP_APPLICATION"))).thenReturn(false);
when(mockEnvironment.containsProperty(eq("VCAP_SERVICES"))).thenReturn(false);
when(mockEnvironment.getProperty(eq(ClientSecurityAutoConfiguration.CLOUD_SECURITY_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
eq(Boolean.class), eq(true))).thenReturn(true);
environmentPostProcessor.postProcessEnvironment(mockEnvironment, null);
verify(mockEnvironment, times(1))
.getProperty(eq(ClientSecurityAutoConfiguration.SECURITY_CLOUD_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
.getProperty(eq(ClientSecurityAutoConfiguration.CLOUD_SECURITY_ENVIRONMENT_POST_PROCESSOR_ENABLED_PROPERTY),
eq(Boolean.class), eq(true));
verify(mockEnvironment, times(1)).containsProperty(eq("VCAP_APPLICATION"));
verify(mockEnvironment, times(1)).containsProperty(eq("VCAP_SERVICES"));
verify(environmentPostProcessor, never()).configureSecurityContext(eq(mockEnvironment));
}
@Test
@SuppressWarnings("unchecked")
public void configuresSecurityContext() {
ConfigurableEnvironment mockEnvironment = mock(ConfigurableEnvironment.class);
Properties vcapProperties = new Properties();
vcapProperties.setProperty("vcap.application.name", "TestApp");
vcapProperties.setProperty("vcap.application.uris", "test-app.apps.cloud.skullbox.com");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.locators", "boombox[10334],skullbox[10334]");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.urls.gfsh", "https://cloud.skullbox.com:8080/gemfire/v1");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.urls.pulse", "https://cloud.skullbox.com:8080/pulse");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].username", "Abuser");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].password", "p@55w0rd");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].roles", "cluster_developer");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].username", "Master");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].password", "p@$$w0rd");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].roles", "cluster_operator");
vcapProperties.setProperty("vcap.services.test-pcc.tags", "gemfire,cloudcache,test,geode");
PropertySource vcapPropertySource = new PropertiesPropertySource("vcap", vcapProperties);
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(vcapPropertySource);
when(mockEnvironment.getPropertySources()).thenReturn(propertySources);
AutoConfiguredCloudSecurityEnvironmentPostProcessor environmentPostProcessor =
spy(new AutoConfiguredCloudSecurityEnvironmentPostProcessor());
environmentPostProcessor.configureSecurityContext(mockEnvironment);
verify(mockEnvironment, times(2)).getPropertySources();
assertThat(propertySources.contains("boot.data.gemfire.cloudcache")).isTrue();
PropertySource propertySource = propertySources.get("boot.data.gemfire.cloudcache");
assertThat(propertySource).isNotNull();
assertThat(propertySource.getName()).isEqualTo("boot.data.gemfire.cloudcache");
assertThat(propertySource.getProperty("spring.data.gemfire.security.username")).isEqualTo("Master");
assertThat(propertySource.getProperty("spring.data.gemfire.security.password")).isEqualTo("p@$$w0rd");
assertThat(propertySource.getProperty("spring.data.gemfire.pool.locators"))
.isEqualTo("boombox[10334],skullbox[10334]");
assertThat(propertySource.getProperty("spring.data.gemfire.management.use-http")).isEqualTo("true");
assertThat(propertySource.getProperty("spring.data.gemfire.management.http.host")).isEqualTo("cloud.skullbox.com");
assertThat(propertySource.getProperty("spring.data.gemfire.management.http.port")).isEqualTo("8080");
}
@Test
@SuppressWarnings("unchecked")
public void configuresSecurityContextWithLocatorsOnly() {
ConfigurableEnvironment mockEnvironment = mock(ConfigurableEnvironment.class);
when(mockEnvironment.containsProperty("spring.data.gemfire.security.username")).thenReturn(true);
when(mockEnvironment.containsProperty("spring.data.gemfire.security.password")).thenReturn(true);
Properties vcapProperties = new Properties();
vcapProperties.setProperty("vcap.application.name", "TestApp");
vcapProperties.setProperty("vcap.application.uris", "test-app.apps.cloud.skullbox.com");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.locators", "boombox[10334],skullbox[10334]");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.urls.pulse", "https://cloud.skullbox.com:8080/pulse");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].username", "Abuser");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].password", "p@55w0rd");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].roles", "cluster_developer");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].username", "Master");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].password", "p@$$w0rd");
vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].roles", "cluster_operator");
vcapProperties.setProperty("vcap.services.test-pcc.tags", "gemfire,cloudcache,test");
PropertySource vcapPropertySource = new PropertiesPropertySource("vcap", vcapProperties);
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(vcapPropertySource);
when(mockEnvironment.getPropertySources()).thenReturn(propertySources);
AutoConfiguredCloudSecurityEnvironmentPostProcessor environmentPostProcessor =
spy(new AutoConfiguredCloudSecurityEnvironmentPostProcessor());
environmentPostProcessor.configureSecurityContext(mockEnvironment);
verify(mockEnvironment, times(2)).getPropertySources();
assertThat(propertySources.contains("boot.data.gemfire.cloudcache")).isTrue();
PropertySource propertySource = propertySources.get("boot.data.gemfire.cloudcache");
assertThat(propertySource).isNotNull();
assertThat(propertySource.getName()).isEqualTo("boot.data.gemfire.cloudcache");
assertThat(propertySource.containsProperty("spring.data.gemfire.security.username")).isFalse();
assertThat(propertySource.containsProperty("spring.data.gemfire.security.password")).isFalse();
assertThat(propertySource.getProperty("spring.data.gemfire.pool.locators"))
.isEqualTo("boombox[10334],skullbox[10334]");
assertThat(propertySource.containsProperty("spring.data.gemfire.management.use-http")).isFalse();
assertThat(propertySource.containsProperty("spring.data.gemfire.management.http.host")).isFalse();
assertThat(propertySource.containsProperty("spring.data.gemfire.management.http.port")).isFalse();
}
}