Search in sources :

Example 1 with CertificateStatus

use of com.venafi.vcert.sdk.certificate.CertificateStatus in project vcert-java by Venafi.

the class CloudConnectorTest method renewCertificate.

@Test
@DisplayName("Renew a certificate with fingerprint")
void renewCertificate() throws VCertException {
    final String apiKey = "12345678-1234-1234-1234-123456789012";
    final Authentication auth = new Authentication(null, null, apiKey);
    String requestId = "request_1";
    final String thumbprint = "52030990E3DC44199DA11C2D73E41EF8EAD8A4E1";
    final RenewalRequest renewalRequest = new RenewalRequest();
    CertificateRequest request = mock(CertificateRequest.class);
    renewalRequest.request(request);
    final Cloud.CertificateSearchResponse searchResponse = mock(Cloud.CertificateSearchResponse.class);
    final CertificateStatus certificateStatus = mock(CertificateStatus.class);
    renewalRequest.thumbprint(thumbprint);
    final Cloud.Certificate certificate1 = new Cloud.Certificate();
    certificate1.certificateRequestId(requestId);
    final CloudConnector.CertificateRequestsResponse requestsResponse = mock(CloudConnector.CertificateRequestsResponse.class);
    final CloudConnector.CertificateRequestsResponseData requestsResponseData = mock(CloudConnector.CertificateRequestsResponseData.class);
    // CertificateDetails certDetails = cloud.certificateDetails(certificateId, auth.apiKey());
    CertificateDetails certDetails = new CertificateDetails();
    certDetails.id("007");
    certDetails.certificateRequestId(requestId);
    List<String> list = new ArrayList<String>();
    list.add(requestId);
    when(cloud.certificateDetails(eq(requestId), eq(apiKey))).thenReturn(certDetails);
    when(cloud.searchCertificates(eq(apiKey), searchRequestArgumentCaptor.capture())).thenReturn(searchResponse);
    when(searchResponse.certificates()).thenReturn(singletonList(certificate1));
    when(cloud.certificateStatus(requestId, apiKey)).thenReturn(certificateStatus);
    when(certificateStatus.certificateIds()).thenReturn(list);
    when(cloud.certificateRequest(eq(apiKey), any(CloudConnector.CertificateRequestsPayload.class))).thenReturn(requestsResponse);
    when(requestsResponse.certificateRequests()).thenReturn(singletonList(requestsResponseData));
    when(requestsResponseData.id()).thenReturn("certificate_result");
    String fakeCSR = "fake csr";
    byte[] bytes = fakeCSR.getBytes();
    when(renewalRequest.request().csr()).thenReturn(bytes);
    classUnderTest.authenticate(auth);
    assertThat(classUnderTest.renewCertificate(renewalRequest)).isEqualTo("certificate_result");
}
Also used : CertificateStatus(com.venafi.vcert.sdk.certificate.CertificateStatus) CertificateDetails(com.venafi.vcert.sdk.connectors.cloud.domain.CertificateDetails) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RenewalRequest(com.venafi.vcert.sdk.certificate.RenewalRequest) Authentication(com.venafi.vcert.sdk.endpoint.Authentication) CertificateRequest(com.venafi.vcert.sdk.certificate.CertificateRequest) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with CertificateStatus

use of com.venafi.vcert.sdk.certificate.CertificateStatus in project vcert-java by Venafi.

the class CloudConnector method getCertificateIdFromPickupId.

private String getCertificateIdFromPickupId(CertificateRequest request) throws VCertException {
    CertificateStatus certificateStatus = null;
    if (user == null || user.company() == null) {
        throw new UserNotAuthenticatedException("Must be authenticated to retrieve certificate");
    }
    Instant startTime = Instant.now();
    while (true) {
        certificateStatus = getCertificateStatus(request.pickupId());
        if (certificateStatus == null) {
            throw new FailedToRetrieveCertificateStatusException(request.pickupId());
        }
        if ("ISSUED".equals(certificateStatus.status())) {
            break;
        } else if ("FAILED".equals(certificateStatus.status())) {
            throw new CertificateStatusFailedException(certificateStatus.toString());
        }
        // Status either REQUESTED or PENDING
        if (ZERO.equals(request.timeout())) {
            throw new CertificatePendingException(request.pickupId());
        }
        if (Instant.now().isAfter(startTime.plus(request.timeout()))) {
            throw new RetrieveCertificateTimeoutException(request.pickupId());
        }
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            // Restore interrupted state...
            Thread.currentThread().interrupt();
            throw new AttemptToRetryException(e);
        }
    }
    return certificateStatus.certificateIds().get(0);
}
Also used : CertificateStatus(com.venafi.vcert.sdk.certificate.CertificateStatus) Instant(java.time.Instant)

Example 3 with CertificateStatus

use of com.venafi.vcert.sdk.certificate.CertificateStatus 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 4 with CertificateStatus

use of com.venafi.vcert.sdk.certificate.CertificateStatus in project vcert-java by Venafi.

the class CloudConnectorTest method retrieveCertificate.

@Test
void retrieveCertificate() throws VCertException, IOException {
    Security.addProvider(new BouncyCastleProvider());
    String apiKey = "12345678-1234-1234-1234-123456789012";
    final Authentication auth = new Authentication(null, null, apiKey);
    classUnderTest.authenticate(auth);
    String body = readResourceAsString("certificates/certWithKey.pem");
    PEMCollection pemCollection = PEMCollection.fromStringPEMCollection(body, ChainOption.ChainOptionIgnore, null, null);
    CertificateRequest request = new CertificateRequest().subject(new CertificateRequest.PKIXName().commonName("random name").organization(singletonList("Venafi, Inc.")).organizationalUnit(singletonList("Automated Tests")));
    request.pickupId("jackpot").keyType(KeyType.RSA).keyPair(new KeyPair(pemCollection.certificate().getPublicKey(), pemCollection.privateKey())).keyPassword(KEY_SECRET);
    List<String> list = new ArrayList<String>();
    list.add("jackpot");
    CertificateStatus status = new CertificateStatus().status("ISSUED").certificateIds(list);
    CertificateDetails certificateDetails = new CertificateDetails().dekHash("12345");
    EdgeEncryptionKey edgeEncryptionKey = new EdgeEncryptionKey();
    cloud.certificateDetails(eq("jackpot"), eq(apiKey));
    when(cloud.certificateStatus(eq("jackpot"), eq(apiKey))).thenReturn(status);
    when(cloud.retrieveCertificate(eq("jackpot"), eq(apiKey), eq("ROOT_FIRST"))).thenReturn(Response.builder().request(Request.create(Request.HttpMethod.GET, "http://localhost", new HashMap<String, Collection<String>>(), null, null)).status(200).body(body, Charset.forName("UTF-8")).build());
    when(cloud.certificateDetails(eq("jackpot"), eq(apiKey))).thenReturn(certificateDetails);
    when(cloud.retrieveEdgeEncryptionKey(eq("12345"), eq(apiKey))).thenReturn(edgeEncryptionKey);
    PEMCollection pemCollection2 = classUnderTest.retrieveCertificate(request);
    assertThat(pemCollection2).isNotNull();
    assertThat(pemCollection2.certificate()).isNotNull();
    assertThat(pemCollection2.privateKey()).isNotNull();
    assertThat(pemCollection2.privateKeyPassword()).isEqualTo(KEY_SECRET);
}
Also used : KeyPair(java.security.KeyPair) HashMap(java.util.HashMap) CertificateStatus(com.venafi.vcert.sdk.certificate.CertificateStatus) CertificateDetails(com.venafi.vcert.sdk.connectors.cloud.domain.CertificateDetails) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PEMCollection(com.venafi.vcert.sdk.certificate.PEMCollection) Authentication(com.venafi.vcert.sdk.endpoint.Authentication) CertificateRequest(com.venafi.vcert.sdk.certificate.CertificateRequest) EdgeEncryptionKey(com.venafi.vcert.sdk.connectors.cloud.domain.EdgeEncryptionKey) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) Test(org.junit.jupiter.api.Test)

Aggregations

CertificateStatus (com.venafi.vcert.sdk.certificate.CertificateStatus)4 CertificateRequest (com.venafi.vcert.sdk.certificate.CertificateRequest)3 Authentication (com.venafi.vcert.sdk.endpoint.Authentication)3 ArrayList (java.util.ArrayList)3 PEMCollection (com.venafi.vcert.sdk.certificate.PEMCollection)2 RenewalRequest (com.venafi.vcert.sdk.certificate.RenewalRequest)2 CertificateDetails (com.venafi.vcert.sdk.connectors.cloud.domain.CertificateDetails)2 Instant (java.time.Instant)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 RevocationRequest (com.venafi.vcert.sdk.certificate.RevocationRequest)1 SshCaTemplateRequest (com.venafi.vcert.sdk.certificate.SshCaTemplateRequest)1 SshCertRetrieveDetails (com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails)1 SshCertificateRequest (com.venafi.vcert.sdk.certificate.SshCertificateRequest)1 SshConfig (com.venafi.vcert.sdk.certificate.SshConfig)1 Connector (com.venafi.vcert.sdk.connectors.Connector)1