From 1b327deba82d4ed3924d7ab569f93c7a210c9bbe Mon Sep 17 00:00:00 2001 From: John Blum Date: Tue, 30 Mar 2021 16:09:47 -0700 Subject: [PATCH] Randomize Locator network ports. --- ...dCloudSecurityContextIntegrationTests.java | 25 +++++++++---- ...HybridSecurityContextIntegrationTests.java | 36 +++++++++++++++---- ...stanceSecurityContextIntegrationTests.java | 24 +++++++++---- .../application-vcap-cloud.properties | 4 +-- .../application-vcap-hybrid.properties | 3 +- .../application-vcap-multi.properties | 2 +- 6 files changed, 70 insertions(+), 24 deletions(-) diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/cloud/AutoConfiguredCloudSecurityContextIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/cloud/AutoConfiguredCloudSecurityContextIntegrationTests.java index db688ffd..09291005 100644 --- a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/cloud/AutoConfiguredCloudSecurityContextIntegrationTests.java +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/cloud/AutoConfiguredCloudSecurityContextIntegrationTests.java @@ -38,7 +38,6 @@ import org.springframework.test.context.junit4.SpringRunner; * managed context (e.g. CloudFoundry). * * @author John Blum - * @see java.security.Principal * @see java.util.Properties * @see org.junit.Test * @see org.apache.geode.cache.GemFireCache @@ -65,6 +64,7 @@ import org.springframework.test.context.junit4.SpringRunner; public class AutoConfiguredCloudSecurityContextIntegrationTests extends AbstractAutoConfiguredSecurityContextIntegrationTests { + private static final String LOCATOR_PORT_PLACEHOLDER_REGEX = "%LOCATOR_PORT%"; private static final String VCAP_APPLICATION_PROPERTIES = "application-vcap-cloud.properties"; private static final Properties vcapApplicationProperties = new Properties(); @@ -72,17 +72,28 @@ public class AutoConfiguredCloudSecurityContextIntegrationTests @BeforeClass public static void startGemFireServer() throws IOException { - startGemFireServer(GemFireServerConfiguration.class, "-Dspring.profiles.active=security-cloud"); - loadVcapApplicationProperties(); + int locatorPort = findAvailablePort(); + + startGemFireServer(GemFireServerConfiguration.class, + String.format("-Dspring.data.gemfire.locator.port=%d", locatorPort), + "-Dspring.profiles.active=security-cloud"); + + loadVcapApplicationProperties(locatorPort); + unsetTestAutoConfiguredPoolServersPortSystemProperty(); } - private static void loadVcapApplicationProperties() throws IOException { + private static void loadVcapApplicationProperties(int locatorPort) throws IOException { vcapApplicationProperties.load(new ClassPathResource(VCAP_APPLICATION_PROPERTIES).getInputStream()); - vcapApplicationProperties.stringPropertyNames().forEach(property -> - System.setProperty(property, vcapApplicationProperties.getProperty(property))); + vcapApplicationProperties.stringPropertyNames().forEach(propertyName -> { + + String propertyValue = String.valueOf(vcapApplicationProperties.getProperty(propertyName)) + .replaceAll(LOCATOR_PORT_PLACEHOLDER_REGEX, String.valueOf(locatorPort)); + + System.setProperty(propertyName, propertyValue); + }); } private static void unsetTestAutoConfiguredPoolServersPortSystemProperty() { @@ -98,7 +109,7 @@ public class AutoConfiguredCloudSecurityContextIntegrationTests static class GemFireClientConfiguration extends BaseGemFireClientConfiguration { } @SpringBootApplication - @EnableLocator(port = 55221) + @EnableLocator @CacheServerApplication(name = "AutoConfiguredCloudSecurityContextIntegrationTestsServer") static class GemFireServerConfiguration extends BaseGemFireServerConfiguration { diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/hybrid/AutoConfiguredHybridSecurityContextIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/hybrid/AutoConfiguredHybridSecurityContextIntegrationTests.java index 056d2954..50d38bb7 100644 --- a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/hybrid/AutoConfiguredHybridSecurityContextIntegrationTests.java +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/hybrid/AutoConfiguredHybridSecurityContextIntegrationTests.java @@ -51,9 +51,11 @@ import org.springframework.test.context.junit4.SpringRunner; * @see org.springframework.boot.autoconfigure.SpringBootApplication * @see org.springframework.boot.builder.SpringApplicationBuilder * @see org.springframework.boot.test.context.SpringBootTest + * @see org.springframework.core.io.ClassPathResource * @see org.springframework.data.gemfire.config.annotation.CacheServerApplication * @see org.springframework.data.gemfire.config.annotation.EnableLocator * @see org.springframework.geode.boot.autoconfigure.ClientSecurityAutoConfiguration + * @see org.springframework.geode.boot.autoconfigure.PeerSecurityAutoConfiguration * @see org.springframework.geode.boot.autoconfigure.security.auth.AbstractAutoConfiguredSecurityContextIntegrationTests * @see org.springframework.test.annotation.DirtiesContext * @see org.springframework.test.context.junit4.SpringRunner @@ -64,7 +66,7 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest( classes = AutoConfiguredHybridSecurityContextIntegrationTests.GemFireClientConfiguration.class, properties = { - "spring.data.gemfire.pool.locators=localhost[54441]", + "spring.data.gemfire.pool.locators=localhost[${test.security.hybrid.gemfire.pool.locators.port:54441}]", "spring.data.gemfire.security.username=phantom", "spring.data.gemfire.security.password=s3cr3t" }, @@ -73,6 +75,7 @@ import org.springframework.test.context.junit4.SpringRunner; public class AutoConfiguredHybridSecurityContextIntegrationTests extends AbstractAutoConfiguredSecurityContextIntegrationTests { + private static final String LOCATOR_PORT_PLACEHOLDER_REGEX = "%LOCATOR_PORT%"; private static final String VCAP_APPLICATION_PROPERTIES = "application-vcap-hybrid.properties"; private static final Properties vcapApplicationProperties = new Properties(); @@ -80,17 +83,34 @@ public class AutoConfiguredHybridSecurityContextIntegrationTests @BeforeClass public static void startGemFireServer() throws IOException { - startGemFireServer(GemFireServerConfiguration.class,"-Dspring.profiles.active=security-hybrid"); - loadVcapApplicationProperties(); + int locatorPort = findAvailablePort(); + + startGemFireServer(GemFireServerConfiguration.class, + String.format("-Dspring.data.gemfire.locator.port=%d", locatorPort), + "-Dspring.profiles.active=security-hybrid"); + + loadVcapApplicationProperties(locatorPort); + + setTestSecuritySystemProperties(locatorPort); + unsetTestAutoConfiguredPoolServersPortSystemProperty(); } - private static void loadVcapApplicationProperties() throws IOException { + private static void loadVcapApplicationProperties(int locatorPort) throws IOException { vcapApplicationProperties.load(new ClassPathResource(VCAP_APPLICATION_PROPERTIES).getInputStream()); - vcapApplicationProperties.stringPropertyNames().forEach(property -> - System.setProperty(property, vcapApplicationProperties.getProperty(property))); + vcapApplicationProperties.stringPropertyNames().forEach(propertyName -> { + + String propertyValue = String.valueOf(vcapApplicationProperties.getProperty(propertyName)) + .replaceAll(LOCATOR_PORT_PLACEHOLDER_REGEX, String.valueOf(locatorPort)); + + System.setProperty(propertyName, propertyValue); + }); + } + + private static void setTestSecuritySystemProperties(int locatorPort) { + System.setProperty("test.security.hybrid.gemfire.pool.locators.port", String.valueOf(locatorPort)); } private static void unsetTestAutoConfiguredPoolServersPortSystemProperty() { @@ -99,14 +119,16 @@ public class AutoConfiguredHybridSecurityContextIntegrationTests @AfterClass public static void cleanUpUsedResources() { + vcapApplicationProperties.stringPropertyNames().forEach(System::clearProperty); + System.clearProperty("test.security.hybrid.gemfire.pool.locators.port"); } @SpringBootApplication static class GemFireClientConfiguration extends BaseGemFireClientConfiguration { } @SpringBootApplication - @EnableLocator(port = 54441) + @EnableLocator @CacheServerApplication(name = "AutoConfiguredHybridSecurityContextIntegrationTestsServer") static class GemFireServerConfiguration extends BaseGemFireServerConfiguration { diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/multi/AutoConfiguredMultiCloudCacheServiceInstanceSecurityContextIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/multi/AutoConfiguredMultiCloudCacheServiceInstanceSecurityContextIntegrationTests.java index df82c6e2..7a84e1f4 100644 --- a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/multi/AutoConfiguredMultiCloudCacheServiceInstanceSecurityContextIntegrationTests.java +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/security/auth/multi/AutoConfiguredMultiCloudCacheServiceInstanceSecurityContextIntegrationTests.java @@ -62,6 +62,7 @@ import org.springframework.test.context.junit4.SpringRunner; public class AutoConfiguredMultiCloudCacheServiceInstanceSecurityContextIntegrationTests extends AbstractAutoConfiguredSecurityContextIntegrationTests { + private static final String LOCATOR_PORT_PLACEHOLDER_REGEX = "%LOCATOR_PORT%"; private static final String VCAP_APPLICATION_PROPERTIES = "application-vcap-multi.properties"; private static final Properties vcapApplicationProperties = new Properties(); @@ -69,17 +70,28 @@ public class AutoConfiguredMultiCloudCacheServiceInstanceSecurityContextIntegrat @BeforeClass public static void startGemFireServer() throws IOException { - startGemFireServer(GemFireServerConfiguration.class, "-Dspring.profiles.active=security-multi"); - loadVcapApplicationProperties(); + int locatorPort = findAvailablePort(); + + startGemFireServer(AutoConfiguredMultiCloudCacheServiceInstanceSecurityContextIntegrationTests.GemFireServerConfiguration.class, + String.format("-Dspring.data.gemfire.locator.port=%d", locatorPort), + "-Dspring.profiles.active=security-multi"); + + loadVcapApplicationProperties(locatorPort); + unsetTestAutoConfiguredPoolServersPortSystemProperty(); } - private static void loadVcapApplicationProperties() throws IOException { + private static void loadVcapApplicationProperties(int locatorPort) throws IOException { vcapApplicationProperties.load(new ClassPathResource(VCAP_APPLICATION_PROPERTIES).getInputStream()); - vcapApplicationProperties.stringPropertyNames().forEach(property -> - System.setProperty(property, vcapApplicationProperties.getProperty(property))); + vcapApplicationProperties.stringPropertyNames().forEach(propertyName -> { + + String propertyValue = String.valueOf(vcapApplicationProperties.getProperty(propertyName)) + .replaceAll(LOCATOR_PORT_PLACEHOLDER_REGEX, String.valueOf(locatorPort)); + + System.setProperty(propertyName, propertyValue); + }); } private static void unsetTestAutoConfiguredPoolServersPortSystemProperty() { @@ -95,7 +107,7 @@ public class AutoConfiguredMultiCloudCacheServiceInstanceSecurityContextIntegrat static class GemFireClientConfiguration extends BaseGemFireClientConfiguration { } @SpringBootApplication - @EnableLocator(port = 56421) + @EnableLocator @CacheServerApplication(name = "AutoConfiguredMultiCloudCacheServiceInstanceSecurityContextIntegrationTestsServer") static class GemFireServerConfiguration extends BaseGemFireServerConfiguration { diff --git a/spring-geode-autoconfigure/src/test/resources/application-vcap-cloud.properties b/spring-geode-autoconfigure/src/test/resources/application-vcap-cloud.properties index 61e4b769..55777555 100644 --- a/spring-geode-autoconfigure/src/test/resources/application-vcap-cloud.properties +++ b/spring-geode-autoconfigure/src/test/resources/application-vcap-cloud.properties @@ -1,4 +1,4 @@ -# Spring Boot application.properties for testing security in a Pivotal CloudFoundry (PCF) context. +# Spring Boot application.properties for testing Apache Geode Security in a Pivotal CloudFoundry (PCF) context. VCAP_APPLICATION={"application_id":"c50bb519-2739-4fa3-8750-02c051e35735","application_name":"boot-test","application_uris":["boot-example.apps.tunis.cf-app.com"],"application_version":"d34bbbbd-c35c-4057-baf2-6300cb9aa2aa","cf_api":"https://api.sys.tunis.cf-app.com","host":"0.0.0.0","instance_id":"babcf301-3b34-4dcf-720e-ccfc","instance_index":0,"limits":{"disk":1024,"fds":16384,"mem":1024},"name":"boot-example","port":8080,"space_id":"271cf083-7855-4b5e-be19-65342d099099","space_name":"jblum-space","uris":["boot-example.apps.tunis.cf-app.com"],"version":"d34bbbbd-c35c-4057-baf2-6300cb9aa2aa"} -VCAP_SERVICES={"p-cloudcache":[{ "credentials": { "distributed_system_id": "0", "locators": [ "localhost[55221]" ], "urls": { "pulse": "https://cloudcache-9defb33a-6b8b-49f0-bd35-cf6f7b2f222f.sys.tunis.cf-app.com/pulse" }, "users": [ { "password": "vaxAi8UuJkBp9csgDvJ5YA", "roles": [ "cluster_operator" ], "username": "cluster_operator_CQhqoDaEIT1gobjLryfpBg" }, { "password": "egSyyyaM5Q5yUMOVZD6pXA", "roles": [ "developer" ], "username": "developer_krCFKddILf8EfWs0laUQ" } ], "wan": { "sender_credentials": { "active": { "password": "tYHFwByaMN675FuBWDZQiQ", "username": "gateway_sender_UJ0YO1pJBEnQP03yt7sVXQ" } } } }, "syslog_drain_url": null, "volume_mounts": [ ], "label": "p-cloudcache", "provider": null, "plan": "small", "name": "jblum-pcc", "tags": [ "gemfire", "cloudcache", "database", "pivotal" ] }]} +VCAP_SERVICES={"p-cloudcache":[{ "credentials": { "distributed_system_id": "0", "locators": [ "localhost[%LOCATOR_PORT%]" ], "urls": { "pulse": "https://cloudcache-9defb33a-6b8b-49f0-bd35-cf6f7b2f222f.sys.tunis.cf-app.com/pulse" }, "users": [ { "password": "vaxAi8UuJkBp9csgDvJ5YA", "roles": [ "cluster_operator" ], "username": "cluster_operator_CQhqoDaEIT1gobjLryfpBg" }, { "password": "egSyyyaM5Q5yUMOVZD6pXA", "roles": [ "developer" ], "username": "developer_krCFKddILf8EfWs0laUQ" } ], "wan": { "sender_credentials": { "active": { "password": "tYHFwByaMN675FuBWDZQiQ", "username": "gateway_sender_UJ0YO1pJBEnQP03yt7sVXQ" } } } }, "syslog_drain_url": null, "volume_mounts": [ ], "label": "p-cloudcache", "provider": null, "plan": "small", "name": "jblum-pcc", "tags": [ "gemfire", "cloudcache", "database", "pivotal" ] }]} diff --git a/spring-geode-autoconfigure/src/test/resources/application-vcap-hybrid.properties b/spring-geode-autoconfigure/src/test/resources/application-vcap-hybrid.properties index c2c90451..f0a090f4 100644 --- a/spring-geode-autoconfigure/src/test/resources/application-vcap-hybrid.properties +++ b/spring-geode-autoconfigure/src/test/resources/application-vcap-hybrid.properties @@ -1,4 +1,5 @@ -# Spring Boot application.properties for testing security in a Pivotal CloudFoundry (PCF) context without a Pivotal Cloud Cache (PCC) Service Instance +# Spring Boot application.properties for testing Apache Geode security in a Pivotal CloudFoundry (PCF) context +# without a Pivotal Cloud Cache (PCC) Service Instance. VCAP_APPLICATION={"application_id":"c50bb519-2739-4fa3-8750-02c051e35735","application_name":"boot-test","application_uris":["boot-example.apps.skullbox.cf-app.com"],"application_version":"d34bbbbd-c35c-4057-baf2-6300cb9aa2aa","cf_api":"https://api.sys.skullbox.cf-app.com","host":"0.0.0.0","instance_id":"babcf301-3b34-4dcf-720e-ccfc","instance_index":0,"limits":{"disk":1024,"fds":16384,"mem":1024},"name":"boot-example","port":8080,"space_id":"271cf083-7855-4b5e-be19-65342d099099","space_name":"jblum-space","uris":["boot-example.apps.skullbox.cf-app.com"],"version":"d34bbbbd-c35c-4057-baf2-6300cb9aa2aa"} VCAP_SERVICES={"p-cloudcache":[{ "credentials": { "distributed_system_id": "0", "locators": [ "localhost[54441]" ], "urls": { "pulse": "https://cloudcache-9defb33a-6b8b-49f0-bd35-cf6f7b2f222f.sys.skullbox.cf-app.com/pulse" }, "users": [ { "password": "s3cr3t", "roles": [ "cluster_operator" ], "username": "phantom" }, { "password": "egSyyyaM5Q5yUMOVZD6pXA", "roles": [ "developer" ], "username": "developer_krCFKddILf8EfWs0laUQ" } ], "wan": { "sender_credentials": { "active": { "password": "tYHFwByaMN675FuBWDZQiQ", "username": "gateway_sender_UJ0YO1pJBEnQP03yt7sVXQ" } } } }, "syslog_drain_url": null, "volume_mounts": [ ], "label": "p-cloudcache", "provider": null, "plan": "small", "name": "jblum-pcc", "tags": [ "database", "pivotal" ] }]} diff --git a/spring-geode-autoconfigure/src/test/resources/application-vcap-multi.properties b/spring-geode-autoconfigure/src/test/resources/application-vcap-multi.properties index e79130d7..e3624eae 100644 --- a/spring-geode-autoconfigure/src/test/resources/application-vcap-multi.properties +++ b/spring-geode-autoconfigure/src/test/resources/application-vcap-multi.properties @@ -1,4 +1,4 @@ # Spring Boot application.properties for testing security in a Pivotal CloudFoundry (PCF) context having multiple Pivotal Cloud Cache (PCC) service instances. VCAP_APPLICATION={"application_id":"c50bb519-2739-4fa3-8750-02c051e35735","application_name":"boot-test","application_uris":["boot-example.apps.tunis.cf-app.com"],"application_version":"d34bbbbd-c35c-4057-baf2-6300cb9aa2aa","cf_api":"https://api.skullbox.cf-app.com","host":"0.0.0.0","instance_id":"babcf301-3b34-4dcf-720e-ccfc","instance_index":0,"limits":{"disk":1024,"fds":16384,"mem":1024},"name":"boot-example","port":8080,"space_id":"271cf083-7855-4b5e-be19-65342d099099","space_name":"jblum-space","uris":["boot-example.apps.tunis.cf-app.com"],"version":"d34bbbbd-c35c-4057-baf2-6300cb9aa2aa"} -VCAP_SERVICES={"p-cloudcache":[{ "credentials": { "distributed_system_id": "0", "locators": [ "skullbox.cf-app.com[12345]" ], "urls": { "pulse": "https://cloudcache-9defb33a-6b8b-49f0-bd35-cf6f7b2f222f.skullbox.cf-app.com/pulse" }, "users": [{ "password": "vaxAi8UuJkBp9csgDvJ5YA", "roles": [ "cluster_operator" ], "username": "cluster_operator_CQhqoDaEIT1gobjLryfpBg" }, { "password": "egSyyyaM5Q5yUMOVZD6pXA", "roles": [ "developer" ], "username": "cluster_developer_krCFKddILf8EfWs0laUQ" }], "wan": { "sender_credentials": { "active": { "password": "tYHFwByaMN675FuBWDZQiQ", "username": "gateway_sender_UJ0YO1pJBEnQP03yt7sVXQ" } } } }, "syslog_drain_url": null, "volume_mounts": [ ], "label": "p-cloudcache", "provider": null, "plan": "small", "name": "jblum-pcc", "tags": [ "gemfire", "cloudcache", "database", "pivotal" ]}, { "credentials": { "distributed_system_id": "0", "locators": [ "localhost[56421]" ], "urls": { "pulse": "https://cloudcache-9defb33a-6b8b-49f0-bd35-cf6f7b2f222f.skullbox.cf-app.com/pulse" }, "users": [{ "password": "p@55w0rd", "roles": [ "cluster_operator" ], "username": "master" }, { "password": "egSyyyaM5Q5yUMOVZD6pXA", "roles": [ "developer" ], "username": "cluster_developer_krCFKddILf8EfWs0laUQ" }], "wan": { "sender_credentials": { "active": { "password": "tYHFwByaMN675FuBWDZQiQ", "username": "gateway_sender_UJ0YO1pJBEnQP03yt7sVXQ" } } } }, "syslog_drain_url": null, "volume_mounts": [ ], "label": "p-cloudcache", "provider": null, "plan": "small", "name": "test-pcc", "tags": [ "gemfire", "cloudcache", "database", "pivotal" ]}]} +VCAP_SERVICES={"p-cloudcache":[{ "credentials": { "distributed_system_id": "0", "locators": [ "skullbox.cf-app.com[12345]" ], "urls": { "pulse": "https://cloudcache-9defb33a-6b8b-49f0-bd35-cf6f7b2f222f.skullbox.cf-app.com/pulse" }, "users": [{ "password": "vaxAi8UuJkBp9csgDvJ5YA", "roles": [ "cluster_operator" ], "username": "cluster_operator_CQhqoDaEIT1gobjLryfpBg" }, { "password": "egSyyyaM5Q5yUMOVZD6pXA", "roles": [ "developer" ], "username": "cluster_developer_krCFKddILf8EfWs0laUQ" }], "wan": { "sender_credentials": { "active": { "password": "tYHFwByaMN675FuBWDZQiQ", "username": "gateway_sender_UJ0YO1pJBEnQP03yt7sVXQ" } } } }, "syslog_drain_url": null, "volume_mounts": [ ], "label": "p-cloudcache", "provider": null, "plan": "small", "name": "jblum-pcc", "tags": [ "gemfire", "cloudcache", "database", "pivotal" ]}, { "credentials": { "distributed_system_id": "0", "locators": [ "localhost[%LOCATOR_PORT%]" ], "urls": { "pulse": "https://cloudcache-9defb33a-6b8b-49f0-bd35-cf6f7b2f222f.skullbox.cf-app.com/pulse" }, "users": [{ "password": "p@55w0rd", "roles": [ "cluster_operator" ], "username": "master" }, { "password": "egSyyyaM5Q5yUMOVZD6pXA", "roles": [ "developer" ], "username": "cluster_developer_krCFKddILf8EfWs0laUQ" }], "wan": { "sender_credentials": { "active": { "password": "tYHFwByaMN675FuBWDZQiQ", "username": "gateway_sender_UJ0YO1pJBEnQP03yt7sVXQ" } } } }, "syslog_drain_url": null, "volume_mounts": [ ], "label": "p-cloudcache", "provider": null, "plan": "small", "name": "test-pcc", "tags": [ "gemfire", "cloudcache", "database", "pivotal" ]}]}