Search in sources :

Example 6 with RenewalRequest

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);
}
Also used : RenewalRequest(com.venafi.vcert.sdk.certificate.RenewalRequest) CertificateDNOrThumbprintWasNotProvidedException(com.venafi.vcert.sdk.connectors.ConnectorException.CertificateDNOrThumbprintWasNotProvidedException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 7 with RenewalRequest

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);
}
Also used : RenewalRequest(com.venafi.vcert.sdk.certificate.RenewalRequest) MoreThanOneCertificateWithSameThumbprintException(com.venafi.vcert.sdk.connectors.ConnectorException.MoreThanOneCertificateWithSameThumbprintException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 8 with RenewalRequest

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();
}
Also used : ImportResponse(com.venafi.vcert.sdk.certificate.ImportResponse) CertificateStatus(com.venafi.vcert.sdk.certificate.CertificateStatus) SshCertRetrieveDetails(com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails) StringUtils(org.apache.commons.lang3.StringUtils) CsrOriginOption(com.venafi.vcert.sdk.certificate.CsrOriginOption) CharStreams(com.google.common.io.CharStreams) com.venafi.vcert.sdk.connectors.cloud.domain(com.venafi.vcert.sdk.connectors.cloud.domain) SshCertificateRequest(com.venafi.vcert.sdk.certificate.SshCertificateRequest) SshConfig(com.venafi.vcert.sdk.certificate.SshConfig) ConnectorType(com.venafi.vcert.sdk.endpoint.ConnectorType) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) Instant(java.time.Instant) SerializedName(com.google.gson.annotations.SerializedName) Collectors(java.util.stream.Collectors) VCertException(com.venafi.vcert.sdk.VCertException) CertificateRequest(com.venafi.vcert.sdk.certificate.CertificateRequest) String.format(java.lang.String.format) CloudPolicy(com.venafi.vcert.sdk.policy.api.domain.CloudPolicy) Objects(java.util.Objects) PolicySpecification(com.venafi.vcert.sdk.policy.domain.PolicySpecification) Base64(java.util.Base64) List(java.util.List) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) OffsetDateTime(java.time.OffsetDateTime) ConnectorException(com.venafi.vcert.sdk.connectors.ConnectorException) ZERO(java.time.Duration.ZERO) Connector(com.venafi.vcert.sdk.connectors.Connector) Pattern(java.util.regex.Pattern) KeystoreRequest(com.venafi.vcert.sdk.connectors.cloud.endpoint.KeystoreRequest) Getter(lombok.Getter) RenewalRequest(com.venafi.vcert.sdk.certificate.RenewalRequest) ZoneConfiguration(com.venafi.vcert.sdk.connectors.ZoneConfiguration) PEMCollection(com.venafi.vcert.sdk.certificate.PEMCollection) ArrayList(java.util.ArrayList) SshCaTemplateRequest(com.venafi.vcert.sdk.certificate.SshCaTemplateRequest) Authentication(com.venafi.vcert.sdk.endpoint.Authentication) VCertUtils(com.venafi.vcert.sdk.utils.VCertUtils) Strings(org.bouncycastle.util.Strings) Response(feign.Response) CloudPolicySpecificationConverter(com.venafi.vcert.sdk.policy.converter.CloudPolicySpecificationConverter) Policy(com.venafi.vcert.sdk.connectors.Policy) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) ImportRequest(com.venafi.vcert.sdk.certificate.ImportRequest) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Data(lombok.Data) RevocationRequest(com.venafi.vcert.sdk.certificate.RevocationRequest) InputStream(java.io.InputStream) CertificateStatus(com.venafi.vcert.sdk.certificate.CertificateStatus) VCertException(com.venafi.vcert.sdk.VCertException)

Example 9 with RenewalRequest

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());
}
Also used : PEMCollection(com.venafi.vcert.sdk.certificate.PEMCollection) RenewalRequest(com.venafi.vcert.sdk.certificate.RenewalRequest) Authentication(com.venafi.vcert.sdk.endpoint.Authentication) ZoneConfiguration(com.venafi.vcert.sdk.connectors.ZoneConfiguration) CertificateRequest(com.venafi.vcert.sdk.certificate.CertificateRequest) X509Certificate(java.security.cert.X509Certificate)

Example 10 with RenewalRequest

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));
}
Also used : RenewalRequest(com.venafi.vcert.sdk.certificate.RenewalRequest) FeignException(feign.FeignException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

RenewalRequest (com.venafi.vcert.sdk.certificate.RenewalRequest)23 Test (org.junit.jupiter.api.Test)21 DisplayName (org.junit.jupiter.api.DisplayName)18 CertificateRequest (com.venafi.vcert.sdk.certificate.CertificateRequest)6 Authentication (com.venafi.vcert.sdk.endpoint.Authentication)6 PEMCollection (com.venafi.vcert.sdk.certificate.PEMCollection)5 ZoneConfiguration (com.venafi.vcert.sdk.connectors.ZoneConfiguration)5 X509Certificate (java.security.cert.X509Certificate)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 CertificateNotFoundByThumbprintException (com.venafi.vcert.sdk.connectors.ConnectorException.CertificateNotFoundByThumbprintException)3 CertificateStatus (com.venafi.vcert.sdk.certificate.CertificateStatus)2 CertificateDNOrThumbprintWasNotProvidedException (com.venafi.vcert.sdk.connectors.ConnectorException.CertificateDNOrThumbprintWasNotProvidedException)2 MoreThanOneCertificateWithSameThumbprintException (com.venafi.vcert.sdk.connectors.ConnectorException.MoreThanOneCertificateWithSameThumbprintException)2 FeignException (feign.FeignException)2 CharStreams (com.google.common.io.CharStreams)1 SerializedName (com.google.gson.annotations.SerializedName)1 VCertException (com.venafi.vcert.sdk.VCertException)1 CsrOriginOption (com.venafi.vcert.sdk.certificate.CsrOriginOption)1 ImportRequest (com.venafi.vcert.sdk.certificate.ImportRequest)1 ImportResponse (com.venafi.vcert.sdk.certificate.ImportResponse)1