From 4ea4f2647d6c94c8ac9af30450bd6a26156e7d85 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Wed, 17 Oct 2018 15:23:06 -0500 Subject: [PATCH] Add getAllPaths() to credentials operations. Fixes #38 --- .../core/CredHubCredentialsOperations.java | 8 ++ .../core/CredHubCredentialsTemplate.java | 19 +++++ .../credhub/support/CredentialPath.java | 77 +++++++++++++++++ .../credhub/support/CredentialPathData.java | 85 +++++++++++++++++++ .../support/CredentialSummaryData.java | 2 +- .../CredHubCredentialsTemplateUnitTests.java | 20 +++++ .../support/CredentialPathDataTests.java | 53 ++++++++++++ .../CredentialSummaryDataUnitTests.java | 9 +- 8 files changed, 266 insertions(+), 7 deletions(-) create mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPath.java create mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPathData.java create mode 100644 spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialPathDataTests.java diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsOperations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsOperations.java index 6325dcf..a07ba90 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsOperations.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsOperations.java @@ -18,6 +18,7 @@ package org.springframework.credhub.core; import org.springframework.credhub.support.CredentialDetails; import org.springframework.credhub.support.CredentialName; +import org.springframework.credhub.support.CredentialPath; import org.springframework.credhub.support.CredentialRequest; import org.springframework.credhub.support.CredentialSummary; import org.springframework.credhub.support.ParametersRequest; @@ -126,6 +127,13 @@ public interface CredHubCredentialsOperations { */ List findByPath(final String path); + /** + * Retrieve a collection of all paths that contain credentials. + * + * @return a collection of paths + */ + List getAllPaths(); + /** * Delete a credential by its full name. * diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsTemplate.java index 3c101a8..aa4c40d 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsTemplate.java @@ -20,6 +20,8 @@ import org.springframework.core.ParameterizedTypeReference; import org.springframework.credhub.support.CredentialDetails; import org.springframework.credhub.support.CredentialDetailsData; import org.springframework.credhub.support.CredentialName; +import org.springframework.credhub.support.CredentialPath; +import org.springframework.credhub.support.CredentialPathData; import org.springframework.credhub.support.CredentialRequest; import org.springframework.credhub.support.CredentialSummary; import org.springframework.credhub.support.CredentialSummaryData; @@ -53,6 +55,7 @@ public class CredHubCredentialsTemplate implements CredHubCredentialsOperations static final String NAME_URL_QUERY_VERSIONS = NAME_URL_QUERY + "&versions={versions}"; static final String NAME_LIKE_URL_QUERY = BASE_URL_PATH + "?name-like={name}"; static final String PATH_URL_QUERY = BASE_URL_PATH + "?path={path}"; + static final String SHOW_ALL_URL_QUERY = BASE_URL_PATH + "?paths=true"; static final String REGENERATE_URL_PATH = "/api/v1/regenerate"; private CredHubOperations credHubOperations; @@ -256,6 +259,22 @@ public class CredHubCredentialsTemplate implements CredHubCredentialsOperations }); } + @Override + public List getAllPaths() { + return credHubOperations.doWithRest(new RestOperationsCallback>() { + @Override + public List doWithRestOperations( + RestOperations restOperations) { + ResponseEntity response = restOperations + .getForEntity(SHOW_ALL_URL_QUERY, CredentialPathData.class); + + throwExceptionOnError(response); + + return response.getBody().getPaths(); + } + }); + } + @Override public void deleteByName(final CredentialName name) { Assert.notNull(name, "credential name must not be null"); diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPath.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPath.java new file mode 100644 index 0000000..68aa5bc --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPath.java @@ -0,0 +1,77 @@ +/* + * 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 + * + * http://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.support; + +/** + * The path to a credential that has been written to CredHub. Clients don't typically + * instantiate objects of this type, but will receive them in response to requests. + * + * @author Scott Frederick + */ +public class CredentialPath { + protected String path; + + /** + * Create a {@link CredentialPath}. Intended for internal use. + */ + CredentialPath() { + } + + /** + * Create a {@link CredentialPath} from the provided parameters. Intended for + * internal use. Clients will get {@link CredentialPath} objects populated from + * CredHub responses. + * + * @param path the name of the credential + */ + public CredentialPath(String path) { + this.path = path; + } + + /** + * Get the path to the credential. + * + * @return the credential path + */ + public String getPath() { + return this.path; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof CredentialPath)) + return false; + + CredentialPath that = (CredentialPath) o; + + return (path != null ? !path.equals(that.path) : that.path != null); + } + + @Override + public int hashCode() { + return path != null ? path.hashCode() : 0; + } + + @Override + public String toString() { + return "CredentialPath{" + + "path=" + path + + '}'; + } +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPathData.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPathData.java new file mode 100644 index 0000000..18e1b15 --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPathData.java @@ -0,0 +1,85 @@ +/* + * 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 + * + * http://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.support; + +import java.util.Arrays; +import java.util.List; + +/** + * A collection of {@link CredentialPath}s. Clients don't typically instantiate + * objects of this type, but will receive them in response to requests. + * + * @author Scott Frederick + */ +public class CredentialPathData { + private List paths; + + /** + * Create a {@link CredentialPathData}. + */ + CredentialPathData() { + } + + /** + * Create a {@link CredentialPathData} from the provided parameters. Intended for internal + * use. Clients will get {@link CredentialPathData} objects populated from + * CredHub responses. + * + * @param paths a collection of {@link CredentialPath}s + */ + public CredentialPathData(CredentialPath... paths) { + this.paths = Arrays.asList(paths); + } + + /** + * Get the collection of {@link CredentialPath}s. + * + * @return the collection of {@link CredentialPath}s + */ + public List getPaths() { + return this.paths; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof CredentialPathData)) + return false; + if (!super.equals(o)) + return false; + + CredentialPathData that = (CredentialPathData) o; + + return paths != null ? paths.equals(that.paths) + : that.paths == null; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (paths != null ? paths.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "CredentialPathData{" + + "paths=" + paths + + '}'; + } +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java index e88f46c..6bcbee4 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java @@ -79,7 +79,7 @@ public class CredentialSummaryData { @Override public String toString() { - return "CredentialSummaryResponse{" + return "CredentialSummaryData{" + "credentials=" + credentials + '}'; } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateUnitTests.java index f4195df..4c5155e 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateUnitTests.java @@ -20,12 +20,32 @@ package org.springframework.credhub.core; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.credhub.support.CredentialPath; +import org.springframework.credhub.support.CredentialPathData; +import org.springframework.http.ResponseEntity; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.springframework.credhub.core.CredHubCredentialsTemplate.NAME_URL_QUERY; +import static org.springframework.credhub.core.CredHubCredentialsTemplate.SHOW_ALL_URL_QUERY; +import static org.springframework.http.HttpStatus.OK; @RunWith(MockitoJUnitRunner.class) public class CredHubCredentialsTemplateUnitTests extends CredHubCredentialsTemplateUnitTestsBase { + @Test + public void getAllPaths() { + when(restTemplate.getForEntity(SHOW_ALL_URL_QUERY, CredentialPathData.class)) + .thenReturn(new ResponseEntity<>(new CredentialPathData(new CredentialPath("/path1"), new CredentialPath("/path2")), OK)); + + List paths = credHubTemplate.getAllPaths(); + + assertThat(paths.size()).isEqualTo(2); + assertThat(paths).extracting("path").contains("/path1", "/path2"); + } + @Test public void deleteByName() { credHubTemplate.deleteByName(NAME); diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialPathDataTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialPathDataTests.java new file mode 100644 index 0000000..d951d76 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialPathDataTests.java @@ -0,0 +1,53 @@ +package org.springframework.credhub.support; + +import org.junit.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CredentialPathDataTests extends JsonParsingUnitTestsBase { + @Test + public void deserializeWithPaths() throws IOException { + String json = "{\n" + + " \"paths\": [\n" + + " {\n" + + " \"path\": \"/\"\n" + + " },\n" + + " {\n" + + " \"path\": \"/director-name/\"\n" + + " },\n" + + " {\n" + + " \"path\": \"/director-name/deploy1/\"\n" + + " },\n" + + " {\n" + + " \"path\": \"/director-name/deploy2/\"\n" + + " },\n" + + " {\n" + + " \"path\": \"/director2/\"\n" + + " }\n" + + " ]\n" + + "}"; + + CredentialPathData paths = parseResponse(json); + + assertThat(paths.getPaths().size()).isEqualTo(5); + assertThat(paths.getPaths()).extracting("path") + .contains("/", "/director-name/", "/director-name/deploy1/", "/director-name/deploy2/", "/director2/"); + } + + @Test + public void deserializeWithNoPaths() throws IOException { + String json = "{\n" + + " \"paths\": []" + + "}"; + + CredentialPathData paths = parseResponse(json); + + assertThat(paths.getPaths().size()).isEqualTo(0); + } + + private CredentialPathData parseResponse(String json) throws java.io.IOException { + return objectMapper.readValue(json, CredentialPathData.class); + } +} \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialSummaryDataUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialSummaryDataUnitTests.java index 114bdbe..329d515 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialSummaryDataUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialSummaryDataUnitTests.java @@ -20,8 +20,6 @@ import java.util.List; import org.junit.Test; -import org.springframework.credhub.support.json.JsonCredential; - import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; @@ -46,7 +44,7 @@ public class CredentialSummaryDataUnitTests extends JsonParsingUnitTestsBase { " ]\n" + "}"; - CredentialSummaryData response = parseResponse(json, JsonCredential.class); + CredentialSummaryData response = parseResponse(json); assertThat(response.getCredentials().size(), equalTo(3)); @@ -71,14 +69,13 @@ public class CredentialSummaryDataUnitTests extends JsonParsingUnitTestsBase { " ]\n" + "}"; - CredentialSummaryData response = parseResponse(json, JsonCredential.class); + CredentialSummaryData response = parseResponse(json); assertThat(response.getCredentials(), notNullValue()); assertThat(response.getCredentials().size(), equalTo(0)); } - private CredentialSummaryData parseResponse(String json, Class jsonCredentialClass) - throws java.io.IOException { + private CredentialSummaryData parseResponse(String json) throws java.io.IOException { return objectMapper.readValue(json, CredentialSummaryData.class); } }