Improve intelligence around @EnableClusterAware to be Environment-aware.

One example is when running your Spring Boot app in Pivotal Platfom using Pivotal Cloud Cache (PCC), which is secure by default and requires HTTPS in addition to Auth.

Resolves gh-56.
This commit is contained in:
John Blum
2019-09-20 13:26:09 -07:00
parent 0094fd6ea4
commit 7d80bfec9f
5 changed files with 203 additions and 2 deletions

View File

@@ -105,6 +105,7 @@ public class ClientSecurityAutoConfiguration {
private static final String MANAGEMENT_HTTP_HOST_PROPERTY = "spring.data.gemfire.management.http.host";
private static final String MANAGEMENT_HTTP_PORT_PROPERTY = "spring.data.gemfire.management.http.port";
private static final String MANAGEMENT_REQUIRE_HTTPS_PROPERTY = "spring.data.gemfire.management.require-https";
private static final String MANAGEMENT_USE_HTTP_PROPERTY = "spring.data.gemfire.management.use-http";
private static final String POOL_LOCATORS_PROPERTY = "spring.data.gemfire.pool.locators";
@@ -206,6 +207,7 @@ public class ClientSecurityAutoConfiguration {
cloudCacheService.getGfshUrl().ifPresent(url -> {
cloudCacheProperties.setProperty(MANAGEMENT_USE_HTTP_PROPERTY, Boolean.TRUE.toString());
cloudCacheProperties.setProperty(MANAGEMENT_HTTP_HOST_PROPERTY, url.getHost());
cloudCacheProperties.setProperty(MANAGEMENT_REQUIRE_HTTPS_PROPERTY, Boolean.TRUE.toString());
cloudCacheProperties.setProperty(MANAGEMENT_HTTP_PORT_PROPERTY, String.valueOf(url.getPort()));
});
}

View File

@@ -114,9 +114,9 @@ public class ClusterConfigurationWithAuthenticationIntegrationTests extends Fork
}
@SpringBootApplication
@EnableClusterConfiguration(useHttp = true)
@EnableLogging(logLevel = GEMFIRE_LOG_LEVEL)
@EnableEntityDefinedRegions(basePackageClasses = Book.class)
@EnableClusterConfiguration(useHttp = true)
static class GeodeClientConfiguration { }
@SpringBootApplication

View File

@@ -0,0 +1,84 @@
/*
* Copyright 2019 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.cluster;
import static org.assertj.core.api.Assertions.assertThat;
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.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.data.gemfire.config.annotation.ClusterConfigurationConfiguration;
import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.geode.config.annotation.EnableClusterAware;
import org.springframework.geode.core.util.ObjectUtils;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration Tests for testing the {@link EnableClusterAware} annotation and expected configuration applied by SBDG
* when the Apache Geode cluster is NOT secure.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.boot.autoconfigure.SpringBootApplication
* @see org.springframework.boot.test.context.SpringBootTest
* @see org.springframework.core.env.Environment
* @see org.springframework.data.gemfire.config.annotation.ClusterConfigurationConfiguration
* @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.geode.config.annotation.EnableClusterAware
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.2.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = { "spring.boot.data.gemfire.cluster.condition.match=true" }
)
@SuppressWarnings("unused")
public class ClusterConfigurationWithClusterAwareWhenNonSecureClusterAvailableIntegrationTests
extends IntegrationTestsSupport {
@Autowired
private Environment environment;
@Autowired
private ClusterConfigurationConfiguration configuration;
@Test
public void configurationStatesManagementRestApiRequiresHttps() {
boolean configurationRequiresHttps =
ObjectUtils.invoke(this.configuration, "resolveManagementRequireHttps");
assertThat(configurationRequiresHttps).isFalse();
}
@Test
public void environmentStatesManagementRestApiRequiresHttps() {
assertThat(this.environment.containsProperty("spring.data.gemfire.management.require-https")).isFalse();
}
@SpringBootApplication
@EnableClusterAware
@EnableGemFireMockObjects
static class TestConfiguration { }
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2019 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.cluster;
import static org.assertj.core.api.Assertions.assertThat;
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.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.data.gemfire.config.annotation.ClusterConfigurationConfiguration;
import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.geode.config.annotation.EnableClusterAware;
import org.springframework.geode.core.util.ObjectUtils;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration Tests for testing the {@link EnableClusterAware} annotation and expected configuration applied by SBDG
* when the Apache Geode cluster is secure.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.boot.autoconfigure.SpringBootApplication
* @see org.springframework.boot.test.context.SpringBootTest
* @see org.springframework.core.env.Environment
* @see org.springframework.data.gemfire.config.annotation.ClusterConfigurationConfiguration
* @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.geode.config.annotation.EnableClusterAware
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.2.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = {
"VCAP_APPLICATION={ \"name\" : \"ClusterConfigurationWithClusterAwareWhenSecureClusterAvailableIntegrationTests\", \"uris\" : \"myapp.app.cloud.skullbox.com\"}",
"VCAP_SERVICES={ \"test-pcc\" : [{ \"name\" : \"test-pcc\", \"tags\" : \"cloudcache,database,gemfire,pivotal\", \"credentials\" : { \"urls\" : { \"gfsh\" : \"https://myapp.app.cloud.skullbox.com/gemfire/v1\" }}}]}",
"spring.boot.data.gemfire.cluster.condition.match=true"
}
)
@SuppressWarnings("unused")
public class ClusterConfigurationWithClusterAwareWhenSecureClusterAvailableIntegrationTests
extends IntegrationTestsSupport {
@Autowired
private Environment environment;
@Autowired
private ClusterConfigurationConfiguration configuration;
@Test
public void configurationStatesManagementRestApiRequiresHttps() {
boolean configurationRequiresHttps =
ObjectUtils.invoke(this.configuration, "resolveManagementRequireHttps");
assertThat(configurationRequiresHttps).isTrue();
}
@Test
public void environmentStatesManagementRestApiRequiresHttps() {
assertThat(this.environment.getProperty("spring.data.gemfire.management.require-https", Boolean.class))
.isTrue();
}
@SpringBootApplication
@EnableClusterAware
@EnableGemFireMockObjects
static class TestConfiguration { }
}

View File

@@ -141,7 +141,7 @@ public class ClientSecurityAutoConfigurationUnitTests {
}
@Test
public void clientSecurityIsDisabledWhenEnablePropertyIsTrueAndCloudFoundryIsInactive() {
public void clientSecurityIsDisabledWhenEnablePropertyIsTrueAndCloudFoundryIsNotActive() {
AutoConfiguredCloudSecurityEnvironmentPostProcessor environmentPostProcessor =
spy(new AutoConfiguredCloudSecurityEnvironmentPostProcessor());
@@ -167,6 +167,30 @@ public class ClientSecurityAutoConfigurationUnitTests {
verify(environmentPostProcessor, never()).configureSecurityContext(eq(mockEnvironment));
}
@Test
public void clientSecurityIsDisabledWhenEnablePropertyIsUnsetAndCloudFoundryIsNotActive() {
AutoConfiguredCloudSecurityEnvironmentPostProcessor environmentPostProcessor =
spy(new AutoConfiguredCloudSecurityEnvironmentPostProcessor());
doNothing().when(environmentPostProcessor).configureSecurityContext(any(ConfigurableEnvironment.class));
ConfigurableEnvironment mockEnvironment = spy(new StandardEnvironment());
doReturn(false).when(mockEnvironment).containsProperty(eq("VCAP_APPLICATION"));
doReturn(false).when(mockEnvironment).containsProperty(eq("VCAP_SERVICES"));
environmentPostProcessor.postProcessEnvironment(mockEnvironment, null);
verify(mockEnvironment, times(1))
.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
public void configuresSecurityContext() {
@@ -214,6 +238,7 @@ public class ClientSecurityAutoConfigurationUnitTests {
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.require-https")).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");
}
@@ -266,6 +291,7 @@ public class ClientSecurityAutoConfigurationUnitTests {
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.require-https")).isFalse();
assertThat(propertySource.containsProperty("spring.data.gemfire.management.http.host")).isFalse();
assertThat(propertySource.containsProperty("spring.data.gemfire.management.http.port")).isFalse();
}