Search in sources :

Example 26 with CertificateAuthorityServiceClient

use of com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient in project java-security-private-ca by googleapis.

the class UpdateCertificateAuthority method updateCaLabel.

// Updates the labels in a certificate authority.
public static void updateCaLabel(String project, String location, String pool_Id, String certificateAuthorityName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    /* Initialize client that will be used to send requests. This client only needs to be created
    once, and can be reused for multiple requests. After completing all of your requests, call
    the `certificateAuthorityServiceClient.close()` method on the client to safely
    clean up any remaining background resources. */
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        // Set the parent path and the new labels.
        String certificateAuthorityParent = CertificateAuthorityName.of(project, location, pool_Id, certificateAuthorityName).toString();
        CertificateAuthority certificateAuthority = CertificateAuthority.newBuilder().setName(certificateAuthorityParent).putLabels("env", "test").build();
        // Create a request to update the CA.
        UpdateCertificateAuthorityRequest request = UpdateCertificateAuthorityRequest.newBuilder().setCertificateAuthority(certificateAuthority).setUpdateMask(FieldMask.newBuilder().addPaths("labels").build()).build();
        // Update the CA and wait for the operation to complete.
        ApiFuture<Operation> futureCall = certificateAuthorityServiceClient.updateCertificateAuthorityCallable().futureCall(request);
        Operation operation = futureCall.get(60, TimeUnit.SECONDS);
        // Check for errors.
        if (operation.hasError()) {
            System.out.println("Error in updating labels ! " + operation.getError());
        }
        // Get the updated CA and check if it contains the new label.
        CertificateAuthority response = certificateAuthorityServiceClient.getCertificateAuthority(certificateAuthorityParent);
        if (response.getLabelsMap().containsKey("env") && response.getLabelsMap().get("env").equalsIgnoreCase("test")) {
            System.out.println("Successfully updated the labels ! ");
        }
    }
}
Also used : CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CertificateAuthority(com.google.cloud.security.privateca.v1.CertificateAuthority) UpdateCertificateAuthorityRequest(com.google.cloud.security.privateca.v1.UpdateCertificateAuthorityRequest) Operation(com.google.longrunning.Operation)

Example 27 with CertificateAuthorityServiceClient

use of com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient in project java-security-private-ca by googleapis.

the class SnippetsIT method testCreateCertificate.

@Test
public void testCreateCertificate() throws IOException {
    // Check if the certificate created during setup is successful.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        CertificateName certificateName = CertificateName.of(PROJECT_ID, LOCATION, CA_POOL_ID, CERTIFICATE_NAME);
        Certificate certificate = certificateAuthorityServiceClient.getCertificate(certificateName);
        assertThat(certificate.getName()).contains(CERTIFICATE_NAME);
    }
}
Also used : CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CertificateName(com.google.cloud.security.privateca.v1.CertificateName) Certificate(com.google.cloud.security.privateca.v1.Certificate) Test(org.junit.Test)

Example 28 with CertificateAuthorityServiceClient

use of com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient in project java-security-private-ca by googleapis.

the class SnippetsIT method testCreateCertificateTemplate.

@Test
public void testCreateCertificateTemplate() throws IOException {
    // Check that the Certificate template has been created as part of the setup.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        String certificateTemplate = certificateAuthorityServiceClient.getCertificateTemplate(CertificateTemplateName.of(PROJECT_ID, LOCATION, CERTIFICATE_TEMPLATE_NAME).toString()).getName();
        assertThat(certificateTemplate).contains(String.format("projects/%s/locations/%s/", PROJECT_ID, LOCATION));
    }
}
Also used : CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 29 with CertificateAuthorityServiceClient

use of com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient in project java-security-private-ca by googleapis.

the class SnippetsIT method testCreateCAPool.

@Test
public void testCreateCAPool() throws IOException {
    // Check if the CA pool created during setup is successful.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        String caPoolName = certificateAuthorityServiceClient.getCaPool(CaPoolName.of(PROJECT_ID, LOCATION, CA_POOL_ID).toString()).getName();
        assertThat(caPoolName).contains(String.format("projects/%s/locations/%s/caPools/%s", PROJECT_ID, LOCATION, CA_POOL_ID));
    }
}
Also used : CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 30 with CertificateAuthorityServiceClient

use of com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient in project java-security-private-ca by googleapis.

the class SnippetsIT method testCreateCertificateWithCSR.

@Test
public void testCreateCertificateWithCSR() throws IOException {
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        Certificate response = certificateAuthorityServiceClient.getCertificate(CertificateName.of(PROJECT_ID, LOCATION, CA_POOL_ID, CSR_CERTIFICATE_NAME).toString());
        Assert.assertTrue(response.hasCreateTime());
    }
}
Also used : CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) Certificate(com.google.cloud.security.privateca.v1.Certificate) Test(org.junit.Test)

Aggregations

CertificateAuthorityServiceClient (com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient)31 Operation (com.google.longrunning.Operation)14 Test (org.junit.Test)9 Certificate (com.google.cloud.security.privateca.v1.Certificate)8 ByteString (com.google.protobuf.ByteString)6 CertificateAuthority (com.google.cloud.security.privateca.v1.CertificateAuthority)5 CaPoolName (com.google.cloud.security.privateca.v1.CaPoolName)3 State (com.google.cloud.security.privateca.v1.CertificateAuthority.State)3 CertificateAuthorityName (com.google.cloud.security.privateca.v1.CertificateAuthorityName)3 SubjectConfig (com.google.cloud.security.privateca.v1.CertificateConfig.SubjectConfig)3 CertificateName (com.google.cloud.security.privateca.v1.CertificateName)3 CertificateTemplate (com.google.cloud.security.privateca.v1.CertificateTemplate)3 X509Parameters (com.google.cloud.security.privateca.v1.X509Parameters)3 CaPool (com.google.cloud.security.privateca.v1.CaPool)2 IssuancePolicy (com.google.cloud.security.privateca.v1.CaPool.IssuancePolicy)2 KeyVersionSpec (com.google.cloud.security.privateca.v1.CertificateAuthority.KeyVersionSpec)2 CreateCertificateAuthorityRequest (com.google.cloud.security.privateca.v1.CreateCertificateAuthorityRequest)2 CreateCertificateRequest (com.google.cloud.security.privateca.v1.CreateCertificateRequest)2 ActivateCertificateAuthorityRequest (com.google.cloud.security.privateca.v1.ActivateCertificateAuthorityRequest)1 CertificateIdentityConstraints (com.google.cloud.security.privateca.v1.CertificateIdentityConstraints)1