@@ -17,7 +17,9 @@
|
||||
package org.springframework.credhub.core.certificate;
|
||||
|
||||
import org.springframework.credhub.support.CertificateSummary;
|
||||
import org.springframework.credhub.support.CredentialDetails;
|
||||
import org.springframework.credhub.support.CredentialName;
|
||||
import org.springframework.credhub.support.certificate.CertificateCredential;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -42,4 +44,13 @@ public interface CredHubCertificateOperations {
|
||||
* @return the details of the retrieved certificate credential
|
||||
*/
|
||||
CertificateSummary getByName(final CredentialName name);
|
||||
|
||||
/**
|
||||
* Regenerate a certificate.
|
||||
*
|
||||
* @param id the CredHub-generated ID of the certificate credential; must not be {@literal null}
|
||||
* @param setAsTransitional make the certificate version transitional or not
|
||||
* @return the details of the certificate credential
|
||||
*/
|
||||
CredentialDetails<CertificateCredential> regenerate(final String id, final boolean setAsTransitional);
|
||||
}
|
||||
|
||||
@@ -16,17 +16,25 @@
|
||||
|
||||
package org.springframework.credhub.core.certificate;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.credhub.core.CredHubOperations;
|
||||
import org.springframework.credhub.core.ExceptionUtils;
|
||||
import org.springframework.credhub.core.RestOperationsCallback;
|
||||
import org.springframework.credhub.support.CertificateSummary;
|
||||
import org.springframework.credhub.support.CertificateSummaryData;
|
||||
import org.springframework.credhub.support.CredentialDetails;
|
||||
import org.springframework.credhub.support.CredentialName;
|
||||
import org.springframework.credhub.support.certificate.CertificateCredential;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestOperations;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.http.HttpMethod.POST;
|
||||
|
||||
/**
|
||||
* Implements the interactions with CredHub to retrieve, regenerate, and update
|
||||
@@ -37,6 +45,8 @@ import java.util.List;
|
||||
public class CredHubCertificateTemplate implements CredHubCertificateOperations {
|
||||
static final String BASE_URL_PATH = "/api/v1/certificates";
|
||||
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 TRANSITIONAL_REQUEST_FIELD = "set_as_transitional";
|
||||
|
||||
private CredHubOperations credHubOperations;
|
||||
|
||||
@@ -49,6 +59,11 @@ 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>>() {
|
||||
@@ -65,6 +80,12 @@ 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");
|
||||
@@ -82,4 +103,34 @@ public class CredHubCertificateTemplate implements CredHubCertificateOperations
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate a certificate.
|
||||
*
|
||||
* @param id the CredHub-generated ID of the certificate credential; must not be {@literal null}
|
||||
* @return the details of the certificate credential
|
||||
*/
|
||||
@Override
|
||||
public CredentialDetails<CertificateCredential> regenerate(final String id, final boolean setAsTransitional) {
|
||||
Assert.notNull(id, "credential ID must not be null");
|
||||
|
||||
final ParameterizedTypeReference<CredentialDetails<CertificateCredential>> ref =
|
||||
new ParameterizedTypeReference<CredentialDetails<CertificateCredential>>() {};
|
||||
|
||||
return credHubOperations.doWithRest(new RestOperationsCallback<CredentialDetails<CertificateCredential>>() {
|
||||
@Override
|
||||
public CredentialDetails<CertificateCredential> doWithRestOperations(RestOperations restOperations) {
|
||||
Map<String, Boolean> request = new HashMap<>();
|
||||
request.put(TRANSITIONAL_REQUEST_FIELD, setAsTransitional);
|
||||
|
||||
ResponseEntity<CredentialDetails<CertificateCredential>> response =
|
||||
restOperations.exchange(REGENERATE_URL_PATH, POST,
|
||||
new HttpEntity<Object>(request), ref, id);
|
||||
|
||||
ExceptionUtils.throwExceptionOnError(response);
|
||||
|
||||
return response.getBody();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,19 +21,31 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.CertificateSummary;
|
||||
import org.springframework.credhub.support.CertificateSummaryData;
|
||||
import org.springframework.credhub.support.CredentialDetails;
|
||||
import org.springframework.credhub.support.CredentialType;
|
||||
import org.springframework.credhub.support.SimpleCredentialName;
|
||||
import org.springframework.credhub.support.certificate.CertificateCredential;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
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.NAME_URL_QUERY;
|
||||
import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.REGENERATE_URL_PATH;
|
||||
import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.TRANSITIONAL_REQUEST_FIELD;
|
||||
import static org.springframework.http.HttpStatus.OK;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@@ -83,4 +95,27 @@ public class CredHubCertificateTemplateUnitTests {
|
||||
assertThat(response.getId()).isEqualTo("id1");
|
||||
assertThat(response.getName()).isEqualTo("name1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void regenerate() {
|
||||
CredentialDetails<CertificateCredential> expectedCertificates =
|
||||
new CredentialDetails<>("id", NAME, CredentialType.CERTIFICATE,
|
||||
new CertificateCredential("cert", "authority", "key"));
|
||||
|
||||
Map<String, Boolean> request = new HashMap<>();
|
||||
request.put(TRANSITIONAL_REQUEST_FIELD, true);
|
||||
|
||||
when(restTemplate.exchange(eq(REGENERATE_URL_PATH), eq(HttpMethod.POST),
|
||||
eq(new HttpEntity<Object>(request)), isA(ParameterizedTypeReference.class), eq("id")))
|
||||
.thenReturn(new ResponseEntity<>(expectedCertificates, OK));
|
||||
|
||||
CredentialDetails<CertificateCredential> response = credHubTemplate.regenerate("id", true);
|
||||
|
||||
assertThat(response).isNotNull();
|
||||
assertThat(response.getId()).isEqualTo("id");
|
||||
assertThat(response.getCredentialType()).isEqualTo(CredentialType.CERTIFICATE);
|
||||
assertThat(response.getValue().getCertificate()).isEqualTo("cert");
|
||||
assertThat(response.getValue().getCertificateAuthority()).isEqualTo("authority");
|
||||
assertThat(response.getValue().getPrivateKey()).isEqualTo("key");
|
||||
}
|
||||
}
|
||||
@@ -87,4 +87,24 @@ public class CertificateIntegrationTests extends CredHubIntegrationTests {
|
||||
assertThat(allCertificates.size()).isGreaterThan(0);
|
||||
assertThat(allCertificates).extracting("name").contains(CREDENTIAL_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());
|
||||
|
||||
CertificateSummary byName = certificates.getByName(CREDENTIAL_NAME);
|
||||
|
||||
CredentialDetails<CertificateCredential> regenerated = certificates.regenerate(byName.getId(), 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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user