Add getByNameWithHistory operation that accepts a version parameter

Fixes #34
This commit is contained in:
Scott Frederick
2018-10-15 17:07:50 -05:00
parent 7e14e58d24
commit e61739c031
10 changed files with 129 additions and 24 deletions

View File

@@ -55,7 +55,7 @@ public interface CredHubOperations {
* @param <P> the credential parameter implementation type
* @return the details of the generated credential
*/
<T, P> CredentialDetails<T> generate(ParametersRequest<P> parametersRequest);
<T, P> CredentialDetails<T> generate(final ParametersRequest<P> parametersRequest);
/**
* Regenerate a credential in CredHub. Only credentials that were previously generated can be
@@ -65,7 +65,7 @@ public interface CredHubOperations {
* @param <T> the credential implementation type
* @return the details of the regenerated credential
*/
<T> CredentialDetails<T> regenerate(CredentialName name);
<T> CredentialDetails<T> regenerate(final CredentialName name);
/**
* Retrieve a credential using its ID, as returned in a write request.
@@ -75,7 +75,7 @@ public interface CredHubOperations {
* @param <T> the credential implementation type
* @return the details of the retrieved credential
*/
<T> CredentialDetails<T> getById(final String id, Class<T> credentialType);
<T> CredentialDetails<T> getById(final String id, final Class<T> credentialType);
/**
* Retrieve a credential using its name, as passed to a write request.
@@ -86,7 +86,7 @@ public interface CredHubOperations {
* @param <T> the credential implementation type
* @return the details of the retrieved credential
*/
<T> CredentialDetails<T> getByName(final CredentialName name, Class<T> credentialType);
<T> CredentialDetails<T> getByName(final CredentialName name, final Class<T> credentialType);
/**
* Retrieve a credential using its name, as passed to a write request.
@@ -98,15 +98,29 @@ public interface CredHubOperations {
* @param <T> the credential implementation type
* @return the details of the retrieved credential, including history
*/
<T> List<CredentialDetails<T>> getByNameWithHistory(CredentialName name, Class<T> credentialType);
<T> List<CredentialDetails<T>> getByNameWithHistory(final CredentialName name, final Class<T> 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 <T> the credential implementation type
* @return the details of the retrieved credential, including history
*/
List<CredentialSummary> findByName(CredentialName name);
<T> List<CredentialDetails<T>> getByNameWithHistory(final CredentialName name, int versions,
final Class<T> 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<CredentialSummary> 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<CredentialSummary> findByPath(String path);
List<CredentialSummary> 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<CredentialPermission> getPermissions(CredentialName name);
List<CredentialPermission> 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<CredentialPermission> addPermissions(CredentialName name, CredentialPermission... permissions);
List<CredentialPermission> 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

View File

@@ -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 + "&current=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<T> doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialDetails<T>> response =
restOperations.exchange(BASE_URL_PATH, PUT,
new HttpEntity<CredentialRequest<T>>(credentialRequest), ref);
new HttpEntity<>(credentialRequest), ref);
throwExceptionOnError(response);
@@ -130,7 +131,7 @@ public class CredHubTemplate implements CredHubOperations {
public CredentialDetails<T> doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialDetails<T>> response =
restOperations.exchange(BASE_URL_PATH, POST,
new HttpEntity<ParametersRequest<P>>(parametersRequest), ref);
new HttpEntity<>(parametersRequest), ref);
throwExceptionOnError(response);
@@ -149,12 +150,12 @@ public class CredHubTemplate implements CredHubOperations {
return doWithRest(new RestOperationsCallback<CredentialDetails<T>>() {
@Override
public CredentialDetails<T> doWithRestOperations(RestOperations restOperations) {
Map<String, Object> request = new HashMap<String, Object>(1);
Map<String, Object> request = new HashMap<>(1);
request.put("name", name.getName());
ResponseEntity<CredentialDetails<T>> response =
restOperations.exchange(REGENERATE_URL_PATH, POST,
new HttpEntity<Map<String, Object>>(request), ref);
new HttpEntity<>(request), ref);
throwExceptionOnError(response);
@@ -164,7 +165,7 @@ public class CredHubTemplate implements CredHubOperations {
}
@Override
public <T> CredentialDetails<T> getById(final String id, Class<T> credentialType) {
public <T> CredentialDetails<T> getById(final String id, final Class<T> 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 <T> CredentialDetails<T> getByName(final CredentialName name, Class<T> credentialType) {
public <T> CredentialDetails<T> getByName(final CredentialName name, final Class<T> 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 <T> List<CredentialDetails<T>> getByNameWithHistory(final CredentialName name, Class<T> credentialType) {
public <T> List<CredentialDetails<T>> getByNameWithHistory(final CredentialName name, final Class<T> 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 <T> List<CredentialDetails<T>> getByNameWithHistory(final CredentialName name, final int versions,
final Class<T> credentialType) {
Assert.notNull(name, "credential name must not be null");
Assert.notNull(credentialType, "credential type must not be null");
final ParameterizedTypeReference<CredentialDetailsData<T>> ref =
new ParameterizedTypeReference<CredentialDetailsData<T>>() {};
return doWithRest(new RestOperationsCallback<List<CredentialDetails<T>>>() {
@Override
public List<CredentialDetails<T>> doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialDetailsData<T>> response =
restOperations.exchange(NAME_URL_QUERY_VERSIONS, GET, null, ref,
name.getName(), versions);
throwExceptionOnError(response);
return response.getBody().getData();
}
});
}
@Override
public List<CredentialSummary> 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<CredentialPermission> addPermissions(final CredentialName name, CredentialPermission... permissions) {
public List<CredentialPermission> 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<CredentialPermission> doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialPermissions> response =
restOperations.exchange(PERMISSIONS_URL_PATH, POST,
new HttpEntity<CredentialPermissions>(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<ServicesData> response = restOperations
.exchange(INTERPOLATE_URL_PATH, POST,
new HttpEntity<ServicesData>(serviceData), ServicesData.class);
new HttpEntity<>(serviceData), ServicesData.class);
throwExceptionOnError(response);

View File

@@ -111,4 +111,10 @@ public class CredHubTemplateDetailCertificateUnitTests
ResponseEntity<CredentialDetailsData<CertificateCredential>> expectedResponse) {
verifyGetByNameWithHistory(expectedResponse);
}
@Theory
public void getByNameWithVersions(@FromDataPoints("data-responses")
ResponseEntity<CredentialDetailsData<CertificateCredential>> expectedResponse) {
verifyGetByNameWithVersions(expectedResponse);
}
}

View File

@@ -88,4 +88,10 @@ public class CredHubTemplateDetailJsonUnitTests
ResponseEntity<CredentialDetailsData<JsonCredential>> expectedResponse) {
verifyGetByNameWithHistory(expectedResponse);
}
@Theory
public void getByNameWithVersions(@FromDataPoints("data-responses")
ResponseEntity<CredentialDetailsData<JsonCredential>> expectedResponse) {
verifyGetByNameWithVersions(expectedResponse);
}
}

View File

@@ -107,4 +107,10 @@ public class CredHubTemplateDetailPasswordUnitTests
ResponseEntity<CredentialDetailsData<PasswordCredential>> expectedResponse) {
verifyGetByNameWithHistory(expectedResponse);
}
@Theory
public void getByNameWithVersions(@FromDataPoints("data-responses")
ResponseEntity<CredentialDetailsData<PasswordCredential>> expectedResponse) {
verifyGetByNameWithVersions(expectedResponse);
}
}

View File

@@ -108,4 +108,10 @@ public class CredHubTemplateDetailRsaUnitTests
ResponseEntity<CredentialDetailsData<RsaCredential>> expectedResponse) {
verifyGetByNameWithHistory(expectedResponse);
}
@Theory
public void getByNameWithVersions(@FromDataPoints("data-responses")
ResponseEntity<CredentialDetailsData<RsaCredential>> expectedResponse) {
verifyGetByNameWithVersions(expectedResponse);
}
}

View File

@@ -108,4 +108,10 @@ public class CredHubTemplateDetailSshUnitTests
ResponseEntity<CredentialDetailsData<SshCredential>> expectedResponse) {
verifyGetByNameWithHistory(expectedResponse);
}
@Theory
public void getByNameWithVersions(@FromDataPoints("data-responses")
ResponseEntity<CredentialDetailsData<SshCredential>> expectedResponse) {
verifyGetByNameWithVersions(expectedResponse);
}
}

View File

@@ -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<T, P> extends CredHubTe
}
}
@SuppressWarnings("deprecation")
void verifyGetByNameWithVersions(ResponseEntity<CredentialDetailsData<T>> 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<CredentialDetails<T>> response = credHubTemplate.getByNameWithHistory(NAME, 5, getType());
assertDataResponseContainsExpectedCredentials(expectedResponse, response);
}
}
private void assertDataResponseContainsExpectedCredentials(
ResponseEntity<CredentialDetailsData<T>> expectedResponse,
List<CredentialDetails<T>> response) {

View File

@@ -83,4 +83,10 @@ public class CredHubTemplateDetailUserUnitTests
ResponseEntity<CredentialDetailsData<UserCredential>> expectedResponse) {
verifyGetByNameWithHistory(expectedResponse);
}
@Theory
public void getByNameWithVersions(@FromDataPoints("data-responses")
ResponseEntity<CredentialDetailsData<UserCredential>> expectedResponse) {
verifyGetByNameWithVersions(expectedResponse);
}
}

View File

@@ -84,4 +84,10 @@ public class CredHubTemplateDetailValueUnitTests
ResponseEntity<CredentialDetailsData<ValueCredential>> expectedResponse) {
verifyGetByNameWithHistory(expectedResponse);
}
@Theory
public void getByNameWithVersions(@FromDataPoints("data-responses")
ResponseEntity<CredentialDetailsData<ValueCredential>> expectedResponse) {
verifyGetByNameWithVersions(expectedResponse);
}
}