From e61739c031c8d40f73de52a49c98e99534979923 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Mon, 15 Oct 2018 17:07:50 -0500 Subject: [PATCH] Add getByNameWithHistory operation that accepts a version parameter Fixes #34 --- .../credhub/core/CredHubOperations.java | 42 +++++++++++------ .../credhub/core/CredHubTemplate.java | 45 ++++++++++++++----- ...HubTemplateDetailCertificateUnitTests.java | 6 +++ .../CredHubTemplateDetailJsonUnitTests.java | 6 +++ ...redHubTemplateDetailPasswordUnitTests.java | 6 +++ .../CredHubTemplateDetailRsaUnitTests.java | 6 +++ .../CredHubTemplateDetailSshUnitTests.java | 6 +++ .../CredHubTemplateDetailUnitTestsBase.java | 24 ++++++++++ .../CredHubTemplateDetailUserUnitTests.java | 6 +++ .../CredHubTemplateDetailValueUnitTests.java | 6 +++ 10 files changed, 129 insertions(+), 24 deletions(-) diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java index e423774..9b243cd 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java @@ -55,7 +55,7 @@ public interface CredHubOperations { * @param

the credential parameter implementation type * @return the details of the generated credential */ - CredentialDetails generate(ParametersRequest

parametersRequest); + CredentialDetails generate(final ParametersRequest

parametersRequest); /** * Regenerate a credential in CredHub. Only credentials that were previously generated can be @@ -65,7 +65,7 @@ public interface CredHubOperations { * @param the credential implementation type * @return the details of the regenerated credential */ - CredentialDetails regenerate(CredentialName name); + CredentialDetails regenerate(final CredentialName name); /** * Retrieve a credential using its ID, as returned in a write request. @@ -75,7 +75,7 @@ public interface CredHubOperations { * @param the credential implementation type * @return the details of the retrieved credential */ - CredentialDetails getById(final String id, Class credentialType); + CredentialDetails getById(final String id, final Class credentialType); /** * Retrieve a credential using its name, as passed to a write request. @@ -86,7 +86,7 @@ public interface CredHubOperations { * @param the credential implementation type * @return the details of the retrieved credential */ - CredentialDetails getByName(final CredentialName name, Class credentialType); + CredentialDetails getByName(final CredentialName name, final Class credentialType); /** * Retrieve a credential using its name, as passed to a write request. @@ -98,15 +98,29 @@ public interface CredHubOperations { * @param the credential implementation type * @return the details of the retrieved credential, including history */ - List> getByNameWithHistory(CredentialName name, Class credentialType); + List> getByNameWithHistory(final CredentialName name, final Class credentialType); /** - * Find a credential using a full or partial name. + * Retrieve a credential using its name, as passed to a write request. + * A collection of stored values for the named credential will be returned, + * with the specified number of historical values. * * @param name the name of the credential; must not be {@literal null} - * @return a summary of the credential search results + * @param versions the number of historical versions to retrieve + * @param credentialType the type of credential expected to be returned + * @param the credential implementation type + * @return the details of the retrieved credential, including history */ - List findByName(CredentialName name); + List> getByNameWithHistory(final CredentialName name, int versions, + final Class credentialType); + + /** + * Find a credential using a full or partial name. + * + * @param name the name of the credential; must not be {@literal null} + * @return a summary of the credential search results + */ + List findByName(final CredentialName name); /** * Find a credential using a path. @@ -114,14 +128,14 @@ public interface CredHubOperations { * @param path the path to the credential; must not be {@literal null} * @return a summary of the credential search results */ - List findByPath(String path); + List findByPath(final String path); /** * Delete a credential by its full name. * * @param name the name of the credential; must not be {@literal null} */ - void deleteByName(CredentialName name); + void deleteByName(final CredentialName name); /** * Get the permissions associated with a credential. @@ -129,7 +143,7 @@ public interface CredHubOperations { * @param name the name of the credential; must not be {@literal null} * @return the collection of permissions associated with the credential */ - List getPermissions(CredentialName name); + List getPermissions(final CredentialName name); /** * Add permissions to an existing credential. @@ -138,7 +152,7 @@ public interface CredHubOperations { * @param permissions a collection of permissions to add * @return the collection of permissions associated with the credential */ - List addPermissions(CredentialName name, CredentialPermission... permissions); + List addPermissions(final CredentialName name, final CredentialPermission... permissions); /** * Delete a permission associated with a credential. @@ -146,7 +160,7 @@ public interface CredHubOperations { * @param name the name of the credential; must not be {@literal null} * @param actor the actor of the permission; must not be {@literal null} */ - void deletePermission(CredentialName name, Actor actor); + void deletePermission(final CredentialName name, final Actor actor); /** * Search the provided data structure of bound service credentials, looking for @@ -208,7 +222,7 @@ public interface CredHubOperations { * @return the serviceData structure with CredHub references replaced by stored * credential values */ - ServicesData interpolateServiceData(ServicesData serviceData); + ServicesData interpolateServiceData(final ServicesData serviceData); /** * Allow interaction with the configured {@link RestTemplate} not provided diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java index 2643f58..e9c6a94 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java @@ -56,6 +56,7 @@ public class CredHubTemplate implements CredHubOperations { static final String ID_URL_PATH = BASE_URL_PATH + "/{id}"; static final String NAME_URL_QUERY = BASE_URL_PATH + "?name={name}"; static final String NAME_URL_QUERY_CURRENT = NAME_URL_QUERY + "¤t=true"; + 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}"; @@ -109,7 +110,7 @@ public class CredHubTemplate implements CredHubOperations { public CredentialDetails doWithRestOperations(RestOperations restOperations) { ResponseEntity> response = restOperations.exchange(BASE_URL_PATH, PUT, - new HttpEntity>(credentialRequest), ref); + new HttpEntity<>(credentialRequest), ref); throwExceptionOnError(response); @@ -130,7 +131,7 @@ public class CredHubTemplate implements CredHubOperations { public CredentialDetails doWithRestOperations(RestOperations restOperations) { ResponseEntity> response = restOperations.exchange(BASE_URL_PATH, POST, - new HttpEntity>(parametersRequest), ref); + new HttpEntity<>(parametersRequest), ref); throwExceptionOnError(response); @@ -149,12 +150,12 @@ public class CredHubTemplate implements CredHubOperations { return doWithRest(new RestOperationsCallback>() { @Override public CredentialDetails doWithRestOperations(RestOperations restOperations) { - Map request = new HashMap(1); + Map request = new HashMap<>(1); request.put("name", name.getName()); ResponseEntity> response = restOperations.exchange(REGENERATE_URL_PATH, POST, - new HttpEntity>(request), ref); + new HttpEntity<>(request), ref); throwExceptionOnError(response); @@ -164,7 +165,7 @@ public class CredHubTemplate implements CredHubOperations { } @Override - public CredentialDetails getById(final String id, Class credentialType) { + public CredentialDetails getById(final String id, final Class credentialType) { Assert.notNull(id, "credential id must not be null"); Assert.notNull(credentialType, "credential type must not be null"); @@ -185,7 +186,7 @@ public class CredHubTemplate implements CredHubOperations { } @Override - public CredentialDetails getByName(final CredentialName name, Class credentialType) { + public CredentialDetails getByName(final CredentialName name, final Class credentialType) { Assert.notNull(name, "credential name must not be null"); Assert.notNull(credentialType, "credential type must not be null"); @@ -206,7 +207,7 @@ public class CredHubTemplate implements CredHubOperations { } @Override - public List> getByNameWithHistory(final CredentialName name, Class credentialType) { + public List> getByNameWithHistory(final CredentialName name, final Class credentialType) { Assert.notNull(name, "credential name must not be null"); Assert.notNull(credentialType, "credential type must not be null"); @@ -226,6 +227,29 @@ public class CredHubTemplate implements CredHubOperations { }); } + @Override + public List> getByNameWithHistory(final CredentialName name, final int versions, + final Class credentialType) { + Assert.notNull(name, "credential name must not be null"); + Assert.notNull(credentialType, "credential type must not be null"); + + final ParameterizedTypeReference> ref = + new ParameterizedTypeReference>() {}; + + return doWithRest(new RestOperationsCallback>>() { + @Override + public List> doWithRestOperations(RestOperations restOperations) { + ResponseEntity> response = + restOperations.exchange(NAME_URL_QUERY_VERSIONS, GET, null, ref, + name.getName(), versions); + + throwExceptionOnError(response); + + return response.getBody().getData(); + } + }); + } + @Override public List findByName(final CredentialName name) { Assert.notNull(name, "credential name must not be null"); @@ -296,7 +320,8 @@ public class CredHubTemplate implements CredHubOperations { } @Override - public List addPermissions(final CredentialName name, CredentialPermission... permissions) { + public List addPermissions(final CredentialName name, + final CredentialPermission... permissions) { Assert.notNull(name, "credential name must not be null"); final CredentialPermissions credentialPermissions = new CredentialPermissions(name, permissions); @@ -306,7 +331,7 @@ public class CredHubTemplate implements CredHubOperations { public List doWithRestOperations(RestOperations restOperations) { ResponseEntity response = restOperations.exchange(PERMISSIONS_URL_PATH, POST, - new HttpEntity(credentialPermissions), + new HttpEntity<>(credentialPermissions), CredentialPermissions.class); return response.getBody().getPermissions(); @@ -337,7 +362,7 @@ public class CredHubTemplate implements CredHubOperations { public ServicesData doWithRestOperations(RestOperations restOperations) { ResponseEntity response = restOperations .exchange(INTERPOLATE_URL_PATH, POST, - new HttpEntity(serviceData), ServicesData.class); + new HttpEntity<>(serviceData), ServicesData.class); throwExceptionOnError(response); diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailCertificateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailCertificateUnitTests.java index ed3502b..7c9b2f9 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailCertificateUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailCertificateUnitTests.java @@ -111,4 +111,10 @@ public class CredHubTemplateDetailCertificateUnitTests ResponseEntity> expectedResponse) { verifyGetByNameWithHistory(expectedResponse); } + + @Theory + public void getByNameWithVersions(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithVersions(expectedResponse); + } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailJsonUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailJsonUnitTests.java index 2f6db00..17efb0d 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailJsonUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailJsonUnitTests.java @@ -88,4 +88,10 @@ public class CredHubTemplateDetailJsonUnitTests ResponseEntity> expectedResponse) { verifyGetByNameWithHistory(expectedResponse); } + + @Theory + public void getByNameWithVersions(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithVersions(expectedResponse); + } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailPasswordUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailPasswordUnitTests.java index 5165277..09d44a4 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailPasswordUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailPasswordUnitTests.java @@ -107,4 +107,10 @@ public class CredHubTemplateDetailPasswordUnitTests ResponseEntity> expectedResponse) { verifyGetByNameWithHistory(expectedResponse); } + + @Theory + public void getByNameWithVersions(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithVersions(expectedResponse); + } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailRsaUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailRsaUnitTests.java index 612a797..5eac0f9 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailRsaUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailRsaUnitTests.java @@ -108,4 +108,10 @@ public class CredHubTemplateDetailRsaUnitTests ResponseEntity> expectedResponse) { verifyGetByNameWithHistory(expectedResponse); } + + @Theory + public void getByNameWithVersions(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithVersions(expectedResponse); + } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailSshUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailSshUnitTests.java index 4e5ac8c..5fbf463 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailSshUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailSshUnitTests.java @@ -108,4 +108,10 @@ public class CredHubTemplateDetailSshUnitTests ResponseEntity> expectedResponse) { verifyGetByNameWithHistory(expectedResponse); } + + @Theory + public void getByNameWithVersions(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithVersions(expectedResponse); + } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java index 48e027d..6141fd1 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java @@ -44,6 +44,7 @@ import static org.springframework.credhub.core.CredHubTemplate.BASE_URL_PATH; import static org.springframework.credhub.core.CredHubTemplate.ID_URL_PATH; import static org.springframework.credhub.core.CredHubTemplate.NAME_URL_QUERY; import static org.springframework.credhub.core.CredHubTemplate.NAME_URL_QUERY_CURRENT; +import static org.springframework.credhub.core.CredHubTemplate.NAME_URL_QUERY_VERSIONS; import static org.springframework.credhub.core.CredHubTemplate.REGENERATE_URL_PATH; import static org.springframework.http.HttpMethod.GET; import static org.springframework.http.HttpMethod.POST; @@ -223,6 +224,29 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubTe } } + @SuppressWarnings("deprecation") + void verifyGetByNameWithVersions(ResponseEntity> expectedResponse) { + when(restTemplate.exchange(eq(NAME_URL_QUERY_VERSIONS), eq(GET), isNull(HttpEntity.class), + isA(ParameterizedTypeReference.class), eq(NAME.getName()), eq(5))) + .thenReturn(expectedResponse); + + if (!expectedResponse.getStatusCode().equals(OK)) { + try { + credHubTemplate.getByNameWithHistory(NAME, 5, String.class); + fail("Exception should have been thrown"); + } + catch (CredHubException e) { + assertThat(e.getMessage(), + containsString(expectedResponse.getStatusCode().toString())); + } + } + else { + List> response = credHubTemplate.getByNameWithHistory(NAME, 5, getType()); + + assertDataResponseContainsExpectedCredentials(expectedResponse, response); + } + } + private void assertDataResponseContainsExpectedCredentials( ResponseEntity> expectedResponse, List> response) { diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUserUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUserUnitTests.java index bdb3b5a..79f9270 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUserUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUserUnitTests.java @@ -83,4 +83,10 @@ public class CredHubTemplateDetailUserUnitTests ResponseEntity> expectedResponse) { verifyGetByNameWithHistory(expectedResponse); } + + @Theory + public void getByNameWithVersions(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithVersions(expectedResponse); + } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailValueUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailValueUnitTests.java index 2f0a566..1622694 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailValueUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailValueUnitTests.java @@ -84,4 +84,10 @@ public class CredHubTemplateDetailValueUnitTests ResponseEntity> expectedResponse) { verifyGetByNameWithHistory(expectedResponse); } + + @Theory + public void getByNameWithVersions(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithVersions(expectedResponse); + } } \ No newline at end of file