From ef9967e66bfa3f10f3332dc0be54f1200ae64fc9 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Wed, 27 Nov 2019 11:14:57 -0600 Subject: [PATCH] Remove spring-credhub-cloud-connector module. --- spring-credhub-cloud-connector/build.gradle | 36 ----- ...InterpolationServiceDataPostProcessor.java | 136 ----------------- .../credhub/cloud/package-info.java | 20 --- ...loud.cloudfoundry.ServiceDataPostProcessor | 1 - ...polationServiceDataPostProcessorTests.java | 141 ------------------ 5 files changed, 334 deletions(-) delete mode 100644 spring-credhub-cloud-connector/build.gradle delete mode 100644 spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessor.java delete mode 100644 spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/package-info.java delete mode 100644 spring-credhub-cloud-connector/src/main/resources/META-INF/services/org.springframework.cloud.cloudfoundry.ServiceDataPostProcessor delete mode 100644 spring-credhub-cloud-connector/src/test/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessorTests.java diff --git a/spring-credhub-cloud-connector/build.gradle b/spring-credhub-cloud-connector/build.gradle deleted file mode 100644 index 0a2a905..0000000 --- a/spring-credhub-cloud-connector/build.gradle +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016-2017 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. - */ - -dependencyManagement { - imports { - mavenBom "org.springframework:spring-framework-bom:${springVersion}" - mavenBom "io.projectreactor:reactor-bom:${reactorVersion}" - } -} - -description = 'Spring CredHub Cloud Connector' - -dependencies { - compile project(':spring-credhub-core') - - compile "org.springframework:spring-core" - compile "org.springframework.cloud:spring-cloud-cloudfoundry-connector:${springCloudConnectorsVersion}" - - testImplementation("org.springframework:spring-test") - testImplementation("junit:junit") - testImplementation("org.mockito:mockito-core") - testImplementation("org.assertj:assertj-core:${assertJVersion}") -} diff --git a/spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessor.java b/spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessor.java deleted file mode 100644 index 9779b55..0000000 --- a/spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessor.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright 2016-2017 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.credhub.cloud; - -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.springframework.cloud.cloudfoundry.CloudFoundryRawServiceData; -import org.springframework.cloud.cloudfoundry.ServiceDataPostProcessor; -import org.springframework.credhub.configuration.CredHubTemplateFactory; -import org.springframework.credhub.core.interpolation.CredHubInterpolationOperations; -import org.springframework.credhub.core.CredHubOperations; -import org.springframework.credhub.core.CredHubProperties; -import org.springframework.credhub.support.ClientOptions; -import org.springframework.credhub.support.ServicesData; - -/** - * A Spring Cloud Connectors {@link ServiceDataPostProcessor} that post-processes service - * data from {@literal VCAP_SERVICES} using the CredHub interpolation API. - * - * @author Scott Frederick - * @author Daniel Lavoie - */ -public class CredHubInterpolationServiceDataPostProcessor - implements ServiceDataPostProcessor { - private Logger logger = Logger - .getLogger(CredHubInterpolationServiceDataPostProcessor.class.getName()); - - private CredHubInterpolationOperations credHubOperations; - - /** - * Initialize the service data post-processor. - */ - public CredHubInterpolationServiceDataPostProcessor() { - try { - CredHubTemplateFactory credHubTemplateFactory = new CredHubTemplateFactory(); - CredHubProperties credHubProperties = new CredHubProperties(); - - credHubProperties.setUrl(System.getProperty("spring.credhub.url")); - - if (credHubProperties.getUrl() != null - && !credHubProperties.getUrl().isEmpty()) { - credHubOperations = credHubTemplateFactory.credHubTemplate( - credHubProperties, - new ClientOptions()) - .interpolation(); - } - else { - logger.log(Level.WARNING, - "System property spring.credhub.url is undefined. CredHubOperations cannot be initialized, disabling processing of service data"); - } - } - catch (Exception e) { - logger.log(Level.WARNING, "CredHubOperations cannot be initialized, " - + "disabling processing of service data", e); - } - } - - /** - * Initialize the service data post-processor using the provided - * {@link CredHubOperations}. Intended for internal use. - * - * @param credHubOperations the CredHubOperations to use - */ - CredHubInterpolationServiceDataPostProcessor(CredHubInterpolationOperations credHubOperations) { - this.credHubOperations = credHubOperations; - } - - /** - * Process the provided {@literal serviceData} parsed from {@literal VCAP_SERVICES} by - * Spring Cloud Connectors using the - * {@link CredHubInterpolationOperations#interpolateServiceData(ServicesData)} API. - * - * @param serviceData raw service data parsed from {@literal VCAP_SERVICES} - * @return serviceData with CredHub references replaced by stored credentials - */ - @Override - public CloudFoundryRawServiceData process(CloudFoundryRawServiceData serviceData) { - if (credHubOperations == null) { - return serviceData; - } - - try { - ServicesData interpolatedData = credHubOperations - .interpolateServiceData(connectorsToCredHub(serviceData)); - - return credHubToConnectors(interpolatedData); - } - catch (Exception e) { - logger.log(Level.WARNING, "Error interpolating service data from CredHub.", - e); - return serviceData; - } - } - - /** - * Convert from the Spring Cloud Connectors service data structure to the Spring - * Credhub data structure. - * - * @param rawServiceData the Spring Cloud Connectors data structure - * @return the equivalent Spring CredHub data structure - */ - private ServicesData connectorsToCredHub(CloudFoundryRawServiceData rawServiceData) { - ServicesData servicesData = new ServicesData(); - servicesData.putAll(rawServiceData); - return servicesData; - } - - /** - * Convert from the Spring Credhub service data structure to the Spring Cloud - * Connectors data structure. - * - * @param interpolatedData the Spring CredHub data structure - * @return the equivalent Spring Cloud Connectors data structure - */ - private CloudFoundryRawServiceData credHubToConnectors( - ServicesData interpolatedData) { - CloudFoundryRawServiceData rawServicesData = new CloudFoundryRawServiceData(); - rawServicesData.putAll(interpolatedData); - return rawServicesData; - } -} diff --git a/spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/package-info.java b/spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/package-info.java deleted file mode 100644 index 43a3779..0000000 --- a/spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2016-2017 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. - */ - -/** - * Spring Cloud Connectors support for interpolating credentials from CredHub. - */ -package org.springframework.credhub.cloud; \ No newline at end of file diff --git a/spring-credhub-cloud-connector/src/main/resources/META-INF/services/org.springframework.cloud.cloudfoundry.ServiceDataPostProcessor b/spring-credhub-cloud-connector/src/main/resources/META-INF/services/org.springframework.cloud.cloudfoundry.ServiceDataPostProcessor deleted file mode 100644 index 020ef8c..0000000 --- a/spring-credhub-cloud-connector/src/main/resources/META-INF/services/org.springframework.cloud.cloudfoundry.ServiceDataPostProcessor +++ /dev/null @@ -1 +0,0 @@ -org.springframework.credhub.cloud.CredHubInterpolationServiceDataPostProcessor \ No newline at end of file diff --git a/spring-credhub-cloud-connector/src/test/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessorTests.java b/spring-credhub-cloud-connector/src/test/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessorTests.java deleted file mode 100644 index ee32778..0000000 --- a/spring-credhub-cloud-connector/src/test/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessorTests.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright 2016-2017 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.credhub.cloud; - -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentMatcher; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -import org.springframework.cloud.cloudfoundry.CloudFoundryRawServiceData; -import org.springframework.credhub.core.CredHubException; -import org.springframework.credhub.core.interpolation.CredHubInterpolationOperations; -import org.springframework.credhub.support.ServicesData; -import org.springframework.http.HttpStatus; - -import static org.mockito.ArgumentMatchers.argThat; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(MockitoJUnitRunner.class) -public class CredHubInterpolationServiceDataPostProcessorTests { - @Mock - private CredHubInterpolationOperations credHubOperations; - - @Test - public void processServiceData() { - CloudFoundryRawServiceData rawServiceData = buildRawServiceData(); - ServicesData interpolatedServiceData = buildInterpolatedServiceData(); - - when(credHubOperations.interpolateServiceData(argThat(matchesContent(rawServiceData)))) - .thenReturn(interpolatedServiceData); - - CredHubInterpolationServiceDataPostProcessor processor = - new CredHubInterpolationServiceDataPostProcessor(credHubOperations); - - CloudFoundryRawServiceData actual = processor.process(rawServiceData); - assertThat(actual).isEqualTo(interpolatedServiceData); - } - - @Test - public void processServiceDataWithCredHubError() { - CloudFoundryRawServiceData rawServiceData = buildRawServiceData(); - - when(credHubOperations.interpolateServiceData(argThat(matchesContent(rawServiceData)))) - .thenThrow(new CredHubException(HttpStatus.UNAUTHORIZED)); - - CredHubInterpolationServiceDataPostProcessor processor = - new CredHubInterpolationServiceDataPostProcessor(credHubOperations); - - CloudFoundryRawServiceData actual = processor.process(rawServiceData); - assertThat(actual).isEqualTo(rawServiceData); - } - - @Test - public void processServiceDataWithInitializationError() { - CredHubInterpolationServiceDataPostProcessor processor = - new CredHubInterpolationServiceDataPostProcessor(null); - - processor.process(new CloudFoundryRawServiceData()); - - verifyZeroInteractions(credHubOperations); - } - - private ArgumentMatcher matchesContent(final CloudFoundryRawServiceData expected) { - return new ArgumentMatcher() { - @Override - public boolean matches(ServicesData actual) { - return mapsAreEquivalent(actual, expected); - } - }; - } - - private boolean mapsAreEquivalent(Map actual, Map expected) { - return expected.equals(actual); - } - - private CloudFoundryRawServiceData buildRawServiceData() { - HashMap credentials = new HashMap() { - { - put("credhub-ref", - "((/c/service-broker/service-offering/1111-2222-3333-4444/credentials))"); - } - }; - - HashMap>> rawServiceData = buildRawServiceData(credentials); - - return new CloudFoundryRawServiceData(rawServiceData); - } - - private ServicesData buildInterpolatedServiceData() { - HashMap credentials = new HashMap() { - { - put("uri", "https://example.com"); - put("username", "user"); - put("password", "secret"); - } - }; - - HashMap>> rawServiceData = buildRawServiceData(credentials); - - return new ServicesData(rawServiceData); - } - - private HashMap>> buildRawServiceData(final HashMap credentials) { - return new HashMap>>() { - { - put("service-offering", Collections.> singletonList( - new HashMap() { - { - put("credentials", credentials); - put("label", "service-offering"); - put("name", "service-instance"); - put("plan", "standard"); - } - })); - } - }; - } - -} \ No newline at end of file