use of com.venafi.vcert.sdk.certificate.RenewalRequest in project vcert-java by Venafi.
the class TppTokenConnectorTest method renewCertificateWithEmptyRequest.
@Test
@DisplayName("Renew Certificate with an empty request")
void renewCertificateWithEmptyRequest() throws VCertException {
final RenewalRequest renewalRequest = mock(RenewalRequest.class);
final Throwable throwable = assertThrows(VCertException.class, () -> classUnderTest.renewCertificate(renewalRequest));
assertThat(throwable instanceof CertificateDNOrThumbprintWasNotProvidedException);
}
use of com.venafi.vcert.sdk.certificate.RenewalRequest in project vcert-java by Venafi.
the class TppTokenConnectorTest method renewCertificateWithFingerPrintMultipleCertificates.
@Test
@DisplayName("Renew Certificate multiple certificates for the fingerprint")
void renewCertificateWithFingerPrintMultipleCertificates() throws VCertException {
final RenewalRequest renewalRequest = mock(RenewalRequest.class);
final Tpp.CertificateSearchResponse certificateSearchResponse = mock(Tpp.CertificateSearchResponse.class);
when(renewalRequest.thumbprint()).thenReturn("1111:1111:1111:1111");
when(tpp.searchCertificatesToken(any(), eq(HEADER_AUTHORIZATION))).thenReturn(certificateSearchResponse);
when(certificateSearchResponse.certificates()).thenReturn(Arrays.asList(new Tpp.Certificate(), new Tpp.Certificate()));
final Throwable throwable = assertThrows(VCertException.class, () -> classUnderTest.renewCertificate(renewalRequest));
// assertThat(throwable.getMessage()).contains("More than one certificate was found");
assertThat(throwable instanceof MoreThanOneCertificateWithSameThumbprintException);
}
use of com.venafi.vcert.sdk.certificate.RenewalRequest in project vcert-java by Venafi.
the class CloudConnector method renewCertificate.
@Override
public String renewCertificate(RenewalRequest request) throws VCertException {
String certificateRequestId = null;
if (isNotBlank(request.thumbprint())) {
Cloud.CertificateSearchResponse result = this.searchCertificatesByFingerprint(request.thumbprint());
Set<String> requestIds = result.certificates().stream().map(c -> c.certificateRequestId()).collect(Collectors.toSet());
if (requestIds.size() > 1) {
throw new MoreThanOneCertificateRequestIdException(request.thumbprint());
} else if (requestIds.size() == 0) {
throw new CertificateNotFoundByThumbprintException(request.thumbprint());
}
certificateRequestId = requestIds.iterator().next();
} else if (isNotBlank(request.certificateDN())) {
certificateRequestId = request.certificateDN();
} else {
throw new CertificateDNOrThumbprintWasNotProvidedException();
}
final CertificateStatus status = cloud.certificateStatus(certificateRequestId, auth.apiKey());
String certificateId = status.certificateIds().get(0);
CertificateDetails certDetails = cloud.certificateDetails(certificateId, auth.apiKey());
if (!certDetails.certificateRequestId().equals(certificateRequestId)) {
final StringBuilder errorStr = new StringBuilder();
errorStr.append("Certificate under requestId %s ");
errorStr.append(isNotBlank(request.thumbprint()) ? String.format("with thumbprint %s ", request.thumbprint()) : "");
errorStr.append("is not the latest under ManagedCertificateId %s. The latest request is %s. ");
errorStr.append("This error may happen when revoked certificate is requested to be renewed.");
throw new VCertException(String.format(errorStr.toString(), certificateRequestId, certDetails.id(), certDetails.certificateRequestId()));
}
final CertificateRequestsPayload certificateRequest = new CertificateRequestsPayload();
certificateRequest.existingCertificateId(certDetails.id());
certificateRequest.applicationId(status.applicationId());
certificateRequest.certificateIssuingTemplateId(status.certificateIssuingTemplateId());
// add client information
VCertUtils.addApiClientInformation(certificateRequest);
certificateRequest.reuseCSR(!(Objects.nonNull(request.request()) && request.request().csr().length > 0));
if (!certificateRequest.reuseCSR) {
certificateRequest.csr(Strings.fromByteArray(request.request().csr()));
} else {
throw new CSRNotProvidedException();
}
CertificateRequestsResponse response = cloud.certificateRequest(auth.apiKey(), certificateRequest);
return response.certificateRequests().get(0).id();
}
use of com.venafi.vcert.sdk.certificate.RenewalRequest in project vcert-java by Venafi.
the class Examples method main.
public static void main(String... args) throws VCertException, CertificateEncodingException {
final Config config = Config.builder().connectorType(ConnectorType.CLOUD).zone("Default").build();
final VCertClient client = new VCertClient(config);
final Authentication auth = Authentication.builder().apiKey("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").build();
client.authenticate(auth);
final ZoneConfiguration zoneConfiguration = client.readZoneConfiguration("My Project\\My Zone");
// Generate a certificate
CertificateRequest certificateRequest = new CertificateRequest().subject(new CertificateRequest.PKIXName().commonName("cert.test").organization(Collections.singletonList("Venafi, Inc.")).organizationalUnit(Arrays.asList("Engineering")).country(Collections.singletonList("US")).locality(Collections.singletonList("SLC")).province(Collections.singletonList("Utah"))).keyType(KeyType.RSA);
certificateRequest = client.generateRequest(zoneConfiguration, certificateRequest);
// Submit the certificate request
String newCertId = client.requestCertificate(certificateRequest, zoneConfiguration);
// Retrieve PEM collection from Venafi
final CertificateRequest pickupRequest = new CertificateRequest().pickupId(newCertId);
PEMCollection pemCollection = client.retrieveCertificate(pickupRequest);
System.out.println(pemCollection.certificate());
// Renew the certificate
X509Certificate cert = (X509Certificate) pemCollection.certificate();
String thumbprint = DigestUtils.sha1Hex(cert.getEncoded()).toUpperCase();
final CertificateRequest certificateRequestToRenew = new CertificateRequest().subject(new CertificateRequest.PKIXName().commonName("cert.test").organization(Collections.singletonList("Venafi, Inc.")).organizationalUnit(Arrays.asList("Engineering")).country(Collections.singletonList("US")).locality(Collections.singletonList("SLC")).province(Collections.singletonList("Utah")));
client.generateRequest(zoneConfiguration, certificateRequestToRenew);
final RenewalRequest renewalRequest = new RenewalRequest().thumbprint(thumbprint).request(certificateRequestToRenew);
final String renewedCertificate = client.renewCertificate(renewalRequest);
// Retrieve PEM collection from Venafi
final CertificateRequest renewPickupRequest = new CertificateRequest().pickupId(renewedCertificate);
PEMCollection pemCollectionRenewed = client.retrieveCertificate(pickupRequest);
System.out.println(pemCollectionRenewed.certificate());
}
use of com.venafi.vcert.sdk.certificate.RenewalRequest in project vcert-java by Venafi.
the class VCertClientTest method renewCertificateWithServerError.
@Test
@DisplayName("Renew certificate with server error")
void renewCertificateWithServerError() throws VCertException {
final RenewalRequest renewalRequest = mock(RenewalRequest.class);
doThrow(new FeignException.InternalServerError("Error", request, "".getBytes())).when(connector).renewCertificate(renewalRequest);
assertThrows(VCertException.class, () -> classUnderTest.renewCertificate(renewalRequest));
}
Aggregations