diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateOperations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateOperations.java index e37e8e2..5aad021 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateOperations.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateOperations.java @@ -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 regenerate(CredentialName certificateName); + /** * Make the specified version of a certificate the {@literal transitional} version. * diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplate.java index aca4053..57d9950 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplate.java @@ -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 getAll() { return credHubOperations.doWithRest(new RestOperationsCallback>() { @@ -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() { @Override public CertificateCredentialDetails doWithRestOperations(RestOperations restOperations) { - Map request = new HashMap<>(); + Map request = new HashMap<>(1); request.put(TRANSITIONAL_REQUEST_FIELD, setAsTransitional); ResponseEntity 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 regenerate(final CredentialName certificateName) { + Assert.notNull(certificateName, "certificate name must not be null"); + + final ParameterizedTypeReference>> ref = + new ParameterizedTypeReference>>() {}; + + return credHubOperations.doWithRest(new RestOperationsCallback>() { + @Override + public List doWithRestOperations(RestOperations restOperations) { + Map request = new HashMap<>(1); + request.put(SIGNED_BY_REQUEST_FIELD, certificateName.getName()); + + ResponseEntity>> 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 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>() { @Override public List doWithRestOperations(RestOperations restOperations) { - Map request = new HashMap<>(); + Map request = new HashMap<>(1); request.put(VERSION_REQUEST_FIELD, versionId); ResponseEntity> response = diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/credential/CredHubCredentialTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/credential/CredHubCredentialTemplate.java index 72ea7fa..6242486 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/credential/CredHubCredentialTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/credential/CredHubCredentialTemplate.java @@ -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 doWithRestOperations(RestOperations restOperations) { Map request = new HashMap<>(1); - request.put("name", name.getName()); + request.put(NAME_REQUEST_FIELD, name.getName()); ResponseEntity> response = restOperations.exchange(REGENERATE_URL_PATH, HttpMethod.POST, diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/certificate/CertificateParameters.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/certificate/CertificateParameters.java index c8bd63e..10f01e5 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/certificate/CertificateParameters.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/certificate/CertificateParameters.java @@ -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. diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplateUnitTests.java index cdce257..7cedc54 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplateUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplateUnitTests.java @@ -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> expectedResponse = + Collections.singletonMap(REGENERATED_CREDENTIALS_RESPONSE_FIELD, + Arrays.asList( + new SimpleCredentialName("example-certificate1"), + new SimpleCredentialName("example-certificate2"))); + + Map request = new HashMap() {{ + 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 response = credHubTemplate.regenerate(NAME); + + assertThat(response).isNotNull(); + assertThat(response).isEqualTo(expectedResponse.get(REGENERATED_CREDENTIALS_RESPONSE_FIELD)); + } + @Test @SuppressWarnings("unchecked") public void updateTransitionalVersion() { diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailUnitTestsBase.java index e16b932..3ccfd85 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailUnitTestsBase.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailUnitTestsBase.java @@ -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 extends CredHubCr void verifyRegenerate(ResponseEntity> expectedResponse) { Map request = new HashMap() {{ - put("name", NAME.getName()); + put(NAME_REQUEST_FIELD, NAME.getName()); }}; when(restTemplate.exchange(eq(REGENERATE_URL_PATH), eq(POST), diff --git a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CertificateIntegrationTests.java b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CertificateIntegrationTests.java index fdd1a5d..7c679ba 100644 --- a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CertificateIntegrationTests.java +++ b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CertificateIntegrationTests.java @@ -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 afterDelete = credentials.findByName(CREDENTIAL_NAME); - assertThat(afterDelete).hasSize(0); + deleteCredentialIfExists(credentials, TEST_CERT_NAME); + deleteCredentialIfExists(credentials, ROOT_CERT_NAME); } @Test public void generateCertificate() { - CredentialDetails 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 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 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 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 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 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 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> 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 rootCertificate = + credentials.generate(CertificateParametersRequest.builder() + .name(ROOT_CERT_NAME) + .parameters(CertificateParameters.builder() + .commonName("example.com") + .certificateAuthority(true) + .selfSign(true) + .build()) + .build()); + + CredentialDetails 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> allVersions = + credentials.getByNameWithHistory(TEST_CERT_NAME, CertificateCredential.class); + assertThat(allVersions).hasSize(1); + assertThat(allVersions.get(0).getId()).isEqualTo(signedCertificate.getId()); + + List regeneratedNames = certificates.regenerate(ROOT_CERT_NAME); + assertThat(regeneratedNames).hasSize(1); + assertThat(regeneratedNames).contains(TEST_CERT_NAME); + } } diff --git a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredHubIntegrationTests.java b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredHubIntegrationTests.java index 0023385..9492c05 100644 --- a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredHubIntegrationTests.java +++ b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredHubIntegrationTests.java @@ -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 + } + } } diff --git a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredentialIntegrationTests.java b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredentialIntegrationTests.java index ca65be1..9f6e33f 100644 --- a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredentialIntegrationTests.java +++ b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredentialIntegrationTests.java @@ -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 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 convergeWithChanges = credentials.generate(UserParametersRequest.builder() - .name(CREDENTIAL_NAME) - .mode(WriteMode.CONVERGE) - .username("test-user") - .parameters(passwordParameters.build()) - .build()); + + CredentialDetails 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()); } } diff --git a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionIntegrationTests.java b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionIntegrationTests.java index 7c3ede7..42392cd 100644 --- a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionIntegrationTests.java +++ b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionIntegrationTests.java @@ -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 afterDelete = permissions.getPermissions(CREDENTIAL_NAME); assertThat(afterDelete).hasSize(0); - - operations.credentials().deleteByName(CREDENTIAL_NAME); } }