Add regenerate method to certificate operations to bulk-regenerate certificates
Fixes #43
This commit is contained in:
@@ -56,6 +56,14 @@ public interface CredHubCertificateOperations {
|
||||
*/
|
||||
CertificateCredentialDetails regenerate(final String id, final boolean setAsTransitional);
|
||||
|
||||
/**
|
||||
* Regenerate all certificates in CredHub that were signed by the specified certificate.
|
||||
*
|
||||
* @param certificateName the name of the signing certificate credential; must not be {@literal null}
|
||||
* @return the names of all regenerated certificate credentials
|
||||
*/
|
||||
List<CredentialName> regenerate(CredentialName certificateName);
|
||||
|
||||
/**
|
||||
* Make the specified version of a certificate the {@literal transitional} version.
|
||||
*
|
||||
|
||||
@@ -36,7 +36,7 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* Implements the interactions with CredHub to retrieve, regenerate, and update
|
||||
* * certificates.
|
||||
* certificates.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@@ -45,9 +45,12 @@ public class CredHubCertificateTemplate implements CredHubCertificateOperations
|
||||
static final String NAME_URL_QUERY = BASE_URL_PATH + "?name={name}";
|
||||
static final String REGENERATE_URL_PATH = BASE_URL_PATH + "/{id}/regenerate";
|
||||
static final String UPDATE_TRANSITIONAL_URL_PATH = BASE_URL_PATH + "/{id}/update_transitional_version";
|
||||
static final String BULK_REGENERATE_URL_PATH = "/api/v1/bulk-regenerate";
|
||||
|
||||
static final String TRANSITIONAL_REQUEST_FIELD = "set_as_transitional";
|
||||
static final String VERSION_REQUEST_FIELD = "version";
|
||||
static final String SIGNED_BY_REQUEST_FIELD = "signed_by";
|
||||
static final String REGENERATED_CREDENTIALS_RESPONSE_FIELD = "regenerated_credentials";
|
||||
|
||||
private CredHubOperations credHubOperations;
|
||||
|
||||
@@ -60,11 +63,6 @@ public class CredHubCertificateTemplate implements CredHubCertificateOperations
|
||||
this.credHubOperations = credHubOperations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all certificates from CredHub.
|
||||
*
|
||||
* @return a collection of certificates
|
||||
*/
|
||||
@Override
|
||||
public List<CertificateSummary> getAll() {
|
||||
return credHubOperations.doWithRest(new RestOperationsCallback<List<CertificateSummary>>() {
|
||||
@@ -81,12 +79,6 @@ public class CredHubCertificateTemplate implements CredHubCertificateOperations
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a certificate using its name.
|
||||
*
|
||||
* @param name the name of the certificate credential; must not be {@literal null}
|
||||
* @return the details of the retrieved certificate credential
|
||||
*/
|
||||
@Override
|
||||
public CertificateSummary getByName(final CredentialName name) {
|
||||
Assert.notNull(name, "certificate name must not be null");
|
||||
@@ -105,14 +97,6 @@ public class CredHubCertificateTemplate implements CredHubCertificateOperations
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate a certificate.
|
||||
*
|
||||
* @param id the CredHub-generated ID of the certificate credential; must not be {@literal null}
|
||||
* @param setAsTransitional {@code true} to mark the certificate version transitional;
|
||||
* {@code false} otherwise
|
||||
* @return the details of the certificate credential
|
||||
*/
|
||||
@Override
|
||||
public CertificateCredentialDetails regenerate(final String id, final boolean setAsTransitional) {
|
||||
Assert.notNull(id, "credential ID must not be null");
|
||||
@@ -123,7 +107,7 @@ public class CredHubCertificateTemplate implements CredHubCertificateOperations
|
||||
return credHubOperations.doWithRest(new RestOperationsCallback<CertificateCredentialDetails>() {
|
||||
@Override
|
||||
public CertificateCredentialDetails doWithRestOperations(RestOperations restOperations) {
|
||||
Map<String, Boolean> request = new HashMap<>();
|
||||
Map<String, Boolean> request = new HashMap<>(1);
|
||||
request.put(TRANSITIONAL_REQUEST_FIELD, setAsTransitional);
|
||||
|
||||
ResponseEntity<CertificateCredentialDetails> response =
|
||||
@@ -137,16 +121,30 @@ public class CredHubCertificateTemplate implements CredHubCertificateOperations
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the specified version of a certificate the {@literal transitional} version.
|
||||
*
|
||||
* @param id the CredHub-generated ID of the certificate credential; must not be {@literal null}
|
||||
* and must be an ID returned by {@link #getAll()}
|
||||
* or {@link #getByName(CredentialName)}
|
||||
* @param versionId the CredHub-generated ID of the version of the certificate credential that should be
|
||||
* marked {@literal transitional}
|
||||
* @return the details of the certificate credential, including all versions
|
||||
*/
|
||||
@Override
|
||||
public List<CredentialName> regenerate(final CredentialName certificateName) {
|
||||
Assert.notNull(certificateName, "certificate name must not be null");
|
||||
|
||||
final ParameterizedTypeReference<Map<String, List<CredentialName>>> ref =
|
||||
new ParameterizedTypeReference<Map<String, List<CredentialName>>>() {};
|
||||
|
||||
return credHubOperations.doWithRest(new RestOperationsCallback<List<CredentialName>>() {
|
||||
@Override
|
||||
public List<CredentialName> doWithRestOperations(RestOperations restOperations) {
|
||||
Map<String, Object> request = new HashMap<>(1);
|
||||
request.put(SIGNED_BY_REQUEST_FIELD, certificateName.getName());
|
||||
|
||||
ResponseEntity<Map<String, List<CredentialName>>> response =
|
||||
restOperations.exchange(BULK_REGENERATE_URL_PATH, HttpMethod.POST,
|
||||
new HttpEntity<>(request), ref);
|
||||
|
||||
ExceptionUtils.throwExceptionOnError(response);
|
||||
|
||||
return response.getBody().get(REGENERATED_CREDENTIALS_RESPONSE_FIELD);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public List<CertificateCredentialDetails> updateTransitionalVersion(final String id,
|
||||
final String versionId) {
|
||||
Assert.notNull(id, "credential ID must not be null");
|
||||
@@ -157,7 +155,7 @@ public class CredHubCertificateTemplate implements CredHubCertificateOperations
|
||||
return credHubOperations.doWithRest(new RestOperationsCallback<List<CertificateCredentialDetails>>() {
|
||||
@Override
|
||||
public List<CertificateCredentialDetails> doWithRestOperations(RestOperations restOperations) {
|
||||
Map<String, String> request = new HashMap<>();
|
||||
Map<String, String> request = new HashMap<>(1);
|
||||
request.put(VERSION_REQUEST_FIELD, versionId);
|
||||
|
||||
ResponseEntity<List<CertificateCredentialDetails>> response =
|
||||
|
||||
@@ -56,6 +56,8 @@ public class CredHubCredentialTemplate implements CredHubCredentialOperations {
|
||||
static final String SHOW_ALL_URL_QUERY = BASE_URL_PATH + "?paths=true";
|
||||
static final String REGENERATE_URL_PATH = "/api/v1/regenerate";
|
||||
|
||||
static final String NAME_REQUEST_FIELD = "name";
|
||||
|
||||
private CredHubOperations credHubOperations;
|
||||
|
||||
/**
|
||||
@@ -121,7 +123,7 @@ public class CredHubCredentialTemplate implements CredHubCredentialOperations {
|
||||
@Override
|
||||
public CredentialDetails<T> doWithRestOperations(RestOperations restOperations) {
|
||||
Map<String, Object> request = new HashMap<>(1);
|
||||
request.put("name", name.getName());
|
||||
request.put(NAME_REQUEST_FIELD, name.getName());
|
||||
|
||||
ResponseEntity<CredentialDetails<T>> response =
|
||||
restOperations.exchange(REGENERATE_URL_PATH, HttpMethod.POST,
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.credhub.support.certificate;
|
||||
|
||||
import org.springframework.credhub.support.CredentialName;
|
||||
import org.springframework.credhub.support.KeyLength;
|
||||
import org.springframework.credhub.support.KeyParameters;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -43,6 +44,7 @@ public class CertificateParameters extends KeyParameters {
|
||||
/**
|
||||
* Create a {@link CertificateParameters} using defaults for all parameter values. Intended for internal use.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
private CertificateParameters() {
|
||||
this.commonName = null;
|
||||
this.alternativeNames = null;
|
||||
@@ -348,6 +350,18 @@ public class CertificateParameters extends KeyParameters {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of a certificate authority credential in CredHub to sign the generated certificate with.
|
||||
*
|
||||
* @param certificateAuthorityCredential the parameter value; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CertificateParametersBuilder certificateAuthorityCredential(CredentialName certificateAuthorityCredential) {
|
||||
Assert.notNull(certificateAuthorityCredential, "certificateAuthorityCredential must not be null");
|
||||
this.certificateAuthorityCredential = certificateAuthorityCredential.getName();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of the flag that indicates whether the generated certificate is a
|
||||
* certificate authority.
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.credhub.core.CredHubTemplate;
|
||||
import org.springframework.credhub.support.CredentialName;
|
||||
import org.springframework.credhub.support.certificate.CertificateSummary;
|
||||
import org.springframework.credhub.support.certificate.CertificateSummaryData;
|
||||
import org.springframework.credhub.support.CredentialType;
|
||||
@@ -35,6 +36,7 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -44,11 +46,15 @@ import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isA;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.BASE_URL_PATH;
|
||||
import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.BULK_REGENERATE_URL_PATH;
|
||||
import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.NAME_URL_QUERY;
|
||||
import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.REGENERATED_CREDENTIALS_RESPONSE_FIELD;
|
||||
import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.REGENERATE_URL_PATH;
|
||||
import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.SIGNED_BY_REQUEST_FIELD;
|
||||
import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.TRANSITIONAL_REQUEST_FIELD;
|
||||
import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.UPDATE_TRANSITIONAL_URL_PATH;
|
||||
import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.VERSION_REQUEST_FIELD;
|
||||
import static org.springframework.http.HttpMethod.POST;
|
||||
import static org.springframework.http.HttpStatus.OK;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@@ -124,6 +130,29 @@ public class CredHubCertificateTemplateUnitTests {
|
||||
assertThat(response.getValue().getPrivateKey()).isEqualTo("key");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void bulkRegenerate() {
|
||||
Map<String, List<CredentialName>> expectedResponse =
|
||||
Collections.singletonMap(REGENERATED_CREDENTIALS_RESPONSE_FIELD,
|
||||
Arrays.<CredentialName>asList(
|
||||
new SimpleCredentialName("example-certificate1"),
|
||||
new SimpleCredentialName("example-certificate2")));
|
||||
|
||||
Map<String, Object> request = new HashMap<String, Object>() {{
|
||||
put(SIGNED_BY_REQUEST_FIELD, NAME.getName());
|
||||
}};
|
||||
|
||||
when(restTemplate.exchange(eq(BULK_REGENERATE_URL_PATH), eq(POST),
|
||||
eq(new HttpEntity<>(request)), isA(ParameterizedTypeReference.class)))
|
||||
.thenReturn(new ResponseEntity<>(expectedResponse, OK));
|
||||
|
||||
List<CredentialName> response = credHubTemplate.regenerate(NAME);
|
||||
|
||||
assertThat(response).isNotNull();
|
||||
assertThat(response).isEqualTo(expectedResponse.get(REGENERATED_CREDENTIALS_RESPONSE_FIELD));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void updateTransitionalVersion() {
|
||||
|
||||
@@ -40,6 +40,7 @@ import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.BASE_URL_PATH;
|
||||
import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.ID_URL_PATH;
|
||||
import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.NAME_REQUEST_FIELD;
|
||||
import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.NAME_URL_QUERY;
|
||||
import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.NAME_URL_QUERY_CURRENT;
|
||||
import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.NAME_URL_QUERY_VERSIONS;
|
||||
@@ -130,7 +131,7 @@ public abstract class CredHubTemplateDetailUnitTestsBase<T, P> extends CredHubCr
|
||||
|
||||
void verifyRegenerate(ResponseEntity<CredentialDetails<T>> expectedResponse) {
|
||||
Map<String, Object> request = new HashMap<String, Object>() {{
|
||||
put("name", NAME.getName());
|
||||
put(NAME_REQUEST_FIELD, NAME.getName());
|
||||
}};
|
||||
|
||||
when(restTemplate.exchange(eq(REGENERATE_URL_PATH), eq(POST),
|
||||
|
||||
@@ -19,12 +19,11 @@ package org.springframework.credhub.integration;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.credhub.core.CredHubException;
|
||||
import org.springframework.credhub.core.certificate.CredHubCertificateOperations;
|
||||
import org.springframework.credhub.core.credential.CredHubCredentialOperations;
|
||||
import org.springframework.credhub.support.CredentialName;
|
||||
import org.springframework.credhub.support.certificate.CertificateSummary;
|
||||
import org.springframework.credhub.support.CredentialDetails;
|
||||
import org.springframework.credhub.support.CredentialSummary;
|
||||
import org.springframework.credhub.support.CredentialType;
|
||||
import org.springframework.credhub.support.SimpleCredentialName;
|
||||
import org.springframework.credhub.support.certificate.CertificateCredential;
|
||||
@@ -39,8 +38,10 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
|
||||
public class CertificateIntegrationTests extends CredHubIntegrationTests {
|
||||
private static final SimpleCredentialName CREDENTIAL_NAME =
|
||||
private static final SimpleCredentialName TEST_CERT_NAME =
|
||||
new SimpleCredentialName("spring-credhub", "integration-test", "test-certificate");
|
||||
private static final SimpleCredentialName ROOT_CERT_NAME =
|
||||
new SimpleCredentialName("spring-credhub", "integration-test", "root-certificate");
|
||||
|
||||
private CredHubCredentialOperations credentials;
|
||||
private CredHubCertificateOperations certificates;
|
||||
@@ -52,97 +53,101 @@ public class CertificateIntegrationTests extends CredHubIntegrationTests {
|
||||
credentials = operations.credentials();
|
||||
certificates = operations.certificates();
|
||||
|
||||
try {
|
||||
credentials.deleteByName(CREDENTIAL_NAME);
|
||||
} catch (CredHubException e) {
|
||||
// ignore failing deletes on cleanup
|
||||
}
|
||||
deleteCredentialIfExists(credentials, TEST_CERT_NAME);
|
||||
deleteCredentialIfExists(credentials, ROOT_CERT_NAME);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
credentials.deleteByName(CREDENTIAL_NAME);
|
||||
|
||||
List<CredentialSummary> afterDelete = credentials.findByName(CREDENTIAL_NAME);
|
||||
assertThat(afterDelete).hasSize(0);
|
||||
deleteCredentialIfExists(credentials, TEST_CERT_NAME);
|
||||
deleteCredentialIfExists(credentials, ROOT_CERT_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateCertificate() {
|
||||
CredentialDetails<CertificateCredential> certificate = credentials.generate(CertificateParametersRequest.builder()
|
||||
.name(CREDENTIAL_NAME)
|
||||
.parameters(CertificateParameters.builder()
|
||||
.commonName("example.com")
|
||||
.selfSign(true)
|
||||
.build())
|
||||
.build());
|
||||
assertThat(certificate.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
|
||||
CredentialDetails<CertificateCredential> certificate =
|
||||
credentials.generate(CertificateParametersRequest.builder()
|
||||
.name(TEST_CERT_NAME)
|
||||
.parameters(CertificateParameters.builder()
|
||||
.commonName("example.com")
|
||||
.selfSign(true)
|
||||
.build())
|
||||
.build());
|
||||
assertThat(certificate.getName().getName()).isEqualTo(TEST_CERT_NAME.getName());
|
||||
assertThat(certificate.getCredentialType()).isEqualTo(CredentialType.CERTIFICATE);
|
||||
assertThat(certificate.getId()).isNotNull();
|
||||
assertThat(certificate.getValue().getCertificate()).isNotNull();
|
||||
assertThat(certificate.getValue().getCertificateAuthority()).isNotNull();
|
||||
assertThat(certificate.getValue().getPrivateKey()).isNotNull();
|
||||
|
||||
CertificateSummary byName = certificates.getByName(CREDENTIAL_NAME);
|
||||
assertThat(byName.getName()).isEqualTo(CREDENTIAL_NAME.getName());
|
||||
CertificateSummary byName = certificates.getByName(TEST_CERT_NAME);
|
||||
assertThat(byName.getName()).isEqualTo(TEST_CERT_NAME.getName());
|
||||
assertThat(byName.getId()).isNotNull();
|
||||
|
||||
List<CertificateSummary> allCertificates = certificates.getAll();
|
||||
assertThat(allCertificates.size()).isGreaterThan(0);
|
||||
assertThat(allCertificates).extracting("name").contains(CREDENTIAL_NAME.getName());
|
||||
assertThat(allCertificates).extracting("name").contains(TEST_CERT_NAME.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void regenerateCertificate() {
|
||||
CredentialDetails<CertificateCredential> certificate = credentials.generate(CertificateParametersRequest.builder()
|
||||
.name(CREDENTIAL_NAME)
|
||||
.parameters(CertificateParameters.builder()
|
||||
.commonName("example.com")
|
||||
.selfSign(true)
|
||||
.build())
|
||||
.build());
|
||||
assertThat(certificate.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
|
||||
CredentialDetails<CertificateCredential> certificate =
|
||||
credentials.generate(CertificateParametersRequest.builder()
|
||||
.name(TEST_CERT_NAME)
|
||||
.parameters(CertificateParameters.builder()
|
||||
.commonName("example.com")
|
||||
.selfSign(true)
|
||||
.build())
|
||||
.build());
|
||||
assertThat(certificate.getName().getName()).isEqualTo(TEST_CERT_NAME.getName());
|
||||
|
||||
CertificateSummary byName = certificates.getByName(CREDENTIAL_NAME);
|
||||
CertificateSummary byName = certificates.getByName(TEST_CERT_NAME);
|
||||
|
||||
CertificateCredentialDetails regenerated = certificates.regenerate(byName.getId(), true);
|
||||
assertThat(regenerated.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
|
||||
assertThat(regenerated.getName().getName()).isEqualTo(TEST_CERT_NAME.getName());
|
||||
assertThat(regenerated.isTransitional()).isTrue();
|
||||
assertThat(regenerated.getValue().getCertificate()).isNotEqualTo(certificate.getValue().getCertificate());
|
||||
assertThat(regenerated.getValue().getCertificateAuthority()).isNotEqualTo(certificate.getValue().getCertificateAuthority());
|
||||
assertThat(regenerated.getValue().getPrivateKey()).isNotEqualTo(certificate.getValue().getPrivateKey());
|
||||
assertThat(regenerated.getValue().getCertificate())
|
||||
.isNotEqualTo(certificate.getValue().getCertificate());
|
||||
assertThat(regenerated.getValue().getCertificateAuthority())
|
||||
.isNotEqualTo(certificate.getValue().getCertificateAuthority());
|
||||
assertThat(regenerated.getValue().getPrivateKey())
|
||||
.isNotEqualTo(certificate.getValue().getPrivateKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rotateCertificate() {
|
||||
CredentialDetails<CertificateCredential> certificate = credentials.generate(CertificateParametersRequest.builder()
|
||||
.name(CREDENTIAL_NAME)
|
||||
.parameters(CertificateParameters.builder()
|
||||
.commonName("example.com")
|
||||
.selfSign(true)
|
||||
.build())
|
||||
.build());
|
||||
assertThat(certificate.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
|
||||
CredentialDetails<CertificateCredential> certificate =
|
||||
credentials.generate(CertificateParametersRequest.builder()
|
||||
.name(TEST_CERT_NAME)
|
||||
.parameters(CertificateParameters.builder()
|
||||
.commonName("example.com")
|
||||
.selfSign(true)
|
||||
.build())
|
||||
.build());
|
||||
assertThat(certificate.getName().getName()).isEqualTo(TEST_CERT_NAME.getName());
|
||||
|
||||
String credentialVersion0Id = certificate.getId();
|
||||
|
||||
List<CredentialDetails<CertificateCredential>> allVersions =
|
||||
credentials.getByNameWithHistory(CREDENTIAL_NAME, CertificateCredential.class);
|
||||
credentials.getByNameWithHistory(TEST_CERT_NAME, CertificateCredential.class);
|
||||
assertThat(allVersions).hasSize(1);
|
||||
assertThat(allVersions.get(0).getId()).isEqualTo(credentialVersion0Id);
|
||||
|
||||
CertificateSummary byName = certificates.getByName(CREDENTIAL_NAME);
|
||||
CertificateSummary byName = certificates.getByName(TEST_CERT_NAME);
|
||||
String certificateId = byName.getId();
|
||||
|
||||
CertificateCredentialDetails regenerated = certificates.regenerate(certificateId, true);
|
||||
assertThat(regenerated.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
|
||||
assertThat(regenerated.getValue().getCertificate()).isNotEqualTo(certificate.getValue().getCertificate());
|
||||
assertThat(regenerated.getValue().getCertificateAuthority()).isNotEqualTo(certificate.getValue().getCertificateAuthority());
|
||||
assertThat(regenerated.getValue().getPrivateKey()).isNotEqualTo(certificate.getValue().getPrivateKey());
|
||||
assertThat(regenerated.getName().getName()).isEqualTo(TEST_CERT_NAME.getName());
|
||||
assertThat(regenerated.getValue().getCertificate())
|
||||
.isNotEqualTo(certificate.getValue().getCertificate());
|
||||
assertThat(regenerated.getValue().getCertificateAuthority())
|
||||
.isNotEqualTo(certificate.getValue().getCertificateAuthority());
|
||||
assertThat(regenerated.getValue().getPrivateKey())
|
||||
.isNotEqualTo(certificate.getValue().getPrivateKey());
|
||||
|
||||
String credentialVersion1Id = regenerated.getId();
|
||||
|
||||
allVersions = credentials.getByNameWithHistory(CREDENTIAL_NAME, CertificateCredential.class);
|
||||
allVersions = credentials.getByNameWithHistory(TEST_CERT_NAME, CertificateCredential.class);
|
||||
assertThat(allVersions).hasSize(2);
|
||||
assertThat(allVersions).extracting("id").contains(credentialVersion1Id, credentialVersion0Id);
|
||||
|
||||
@@ -157,4 +162,37 @@ public class CertificateIntegrationTests extends CredHubIntegrationTests {
|
||||
assertThat(updatedCertificate).extracting("id").contains(credentialVersion1Id);
|
||||
assertThat(updatedCertificate).extracting("transitional").contains(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bulkRegenerateCertificates() {
|
||||
CredentialDetails<CertificateCredential> rootCertificate =
|
||||
credentials.generate(CertificateParametersRequest.builder()
|
||||
.name(ROOT_CERT_NAME)
|
||||
.parameters(CertificateParameters.builder()
|
||||
.commonName("example.com")
|
||||
.certificateAuthority(true)
|
||||
.selfSign(true)
|
||||
.build())
|
||||
.build());
|
||||
|
||||
CredentialDetails<CertificateCredential> signedCertificate =
|
||||
credentials.generate(CertificateParametersRequest.builder()
|
||||
.name(TEST_CERT_NAME)
|
||||
.parameters(CertificateParameters.builder()
|
||||
.commonName("example.com")
|
||||
.certificateAuthorityCredential(ROOT_CERT_NAME)
|
||||
.build())
|
||||
.build());
|
||||
assertThat(signedCertificate.getValue().getCertificateAuthority())
|
||||
.isEqualTo(rootCertificate.getValue().getCertificate());
|
||||
|
||||
List<CredentialDetails<CertificateCredential>> allVersions =
|
||||
credentials.getByNameWithHistory(TEST_CERT_NAME, CertificateCredential.class);
|
||||
assertThat(allVersions).hasSize(1);
|
||||
assertThat(allVersions.get(0).getId()).isEqualTo(signedCertificate.getId());
|
||||
|
||||
List<CredentialName> regeneratedNames = certificates.regenerate(ROOT_CERT_NAME);
|
||||
assertThat(regeneratedNames).hasSize(1);
|
||||
assertThat(regeneratedNames).contains(TEST_CERT_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,10 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.credhub.autoconfig.CredHubAutoConfiguration;
|
||||
import org.springframework.credhub.autoconfig.CredHubOAuth2TemplateAutoConfiguration;
|
||||
import org.springframework.credhub.autoconfig.CredHubTemplateAutoConfiguration;
|
||||
import org.springframework.credhub.core.CredHubException;
|
||||
import org.springframework.credhub.core.CredHubOperations;
|
||||
import org.springframework.credhub.core.credential.CredHubCredentialOperations;
|
||||
import org.springframework.credhub.support.SimpleCredentialName;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@@ -21,11 +24,19 @@ public abstract class CredHubIntegrationTests {
|
||||
@Autowired
|
||||
protected CredHubOperations operations;
|
||||
|
||||
protected boolean serverApiIsV1() {
|
||||
boolean serverApiIsV1() {
|
||||
return operations.info().version().isVersion1();
|
||||
}
|
||||
|
||||
protected boolean serverApiIsV2() {
|
||||
boolean serverApiIsV2() {
|
||||
return operations.info().version().isVersion2();
|
||||
}
|
||||
|
||||
void deleteCredentialIfExists(CredHubCredentialOperations credentialOperations, SimpleCredentialName credentialName) {
|
||||
try {
|
||||
credentialOperations.deleteByName(credentialName);
|
||||
} catch (CredHubException e) {
|
||||
// ignore failing deletes on cleanup
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.springframework.credhub.integration;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.credhub.core.CredHubException;
|
||||
import org.springframework.credhub.core.credential.CredHubCredentialOperations;
|
||||
import org.springframework.credhub.support.CredentialDetails;
|
||||
import org.springframework.credhub.support.CredentialSummary;
|
||||
@@ -57,16 +56,12 @@ public class CredentialIntegrationTests extends CredHubIntegrationTests {
|
||||
.excludeNumber(false)
|
||||
.includeSpecial(true);
|
||||
|
||||
try {
|
||||
credentials.deleteByName(CREDENTIAL_NAME);
|
||||
} catch (CredHubException e) {
|
||||
// ignore failing deletes on cleanup
|
||||
}
|
||||
deleteCredentialIfExists(credentials, CREDENTIAL_NAME);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
credentials.deleteByName(CREDENTIAL_NAME);
|
||||
deleteCredentialIfExists(credentials, CREDENTIAL_NAME);
|
||||
|
||||
List<CredentialSummary> afterDelete = credentials.findByName(CREDENTIAL_NAME);
|
||||
assertThat(afterDelete).hasSize(0);
|
||||
@@ -234,19 +229,24 @@ public class CredentialIntegrationTests extends CredHubIntegrationTests {
|
||||
.parameters(passwordParameters.build())
|
||||
.build());
|
||||
assertThat(convergeWithoutChanges.getValue().getUsername()).isEqualTo("test-user");
|
||||
assertThat(convergeWithoutChanges.getValue().getPassword()).isEqualTo(generated.getValue().getPassword());
|
||||
assertThat(convergeWithoutChanges.getValue().getPasswordHash()).isEqualTo(generated.getValue().getPasswordHash());
|
||||
assertThat(convergeWithoutChanges.getValue().getPassword())
|
||||
.isEqualTo(generated.getValue().getPassword());
|
||||
assertThat(convergeWithoutChanges.getValue().getPasswordHash())
|
||||
.isEqualTo(generated.getValue().getPasswordHash());
|
||||
|
||||
passwordParameters.includeSpecial(false);
|
||||
|
||||
CredentialDetails<UserCredential> convergeWithChanges = credentials.generate(UserParametersRequest.builder()
|
||||
.name(CREDENTIAL_NAME)
|
||||
.mode(WriteMode.CONVERGE)
|
||||
.username("test-user")
|
||||
.parameters(passwordParameters.build())
|
||||
.build());
|
||||
|
||||
CredentialDetails<UserCredential> convergeWithChanges =
|
||||
credentials.generate(UserParametersRequest.builder()
|
||||
.name(CREDENTIAL_NAME)
|
||||
.mode(WriteMode.CONVERGE)
|
||||
.username("test-user")
|
||||
.parameters(passwordParameters.build())
|
||||
.build());
|
||||
assertThat(convergeWithChanges.getValue().getUsername()).isEqualTo("test-user");
|
||||
assertThat(convergeWithChanges.getValue().getPassword()).isNotEqualTo(generated.getValue().getPassword());
|
||||
assertThat(convergeWithChanges.getValue().getPasswordHash()).isNotEqualTo(generated.getValue().getPasswordHash());
|
||||
assertThat(convergeWithChanges.getValue().getPassword())
|
||||
.isNotEqualTo(generated.getValue().getPassword());
|
||||
assertThat(convergeWithChanges.getValue().getPasswordHash())
|
||||
.isNotEqualTo(generated.getValue().getPasswordHash());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.credhub.integration;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.credhub.core.credential.CredHubCredentialOperations;
|
||||
@@ -42,6 +43,13 @@ public class PermissionIntegrationTests extends CredHubIntegrationTests {
|
||||
public void setUp() {
|
||||
credentials = operations.credentials();
|
||||
permissions = operations.permissions();
|
||||
|
||||
deleteCredentialIfExists(credentials, CREDENTIAL_NAME);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
deleteCredentialIfExists(credentials, CREDENTIAL_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,7 +88,5 @@ public class PermissionIntegrationTests extends CredHubIntegrationTests {
|
||||
|
||||
List<CredentialPermission> afterDelete = permissions.getPermissions(CREDENTIAL_NAME);
|
||||
assertThat(afterDelete).hasSize(0);
|
||||
|
||||
operations.credentials().deleteByName(CREDENTIAL_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user