Search in sources :

Example 11 with CertificateAuthorityServiceClient

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

the class DeleteCaPool method deleteCaPool.

// Delete the CA pool as mentioned by the pool_Id.
// Before deleting the pool, all CAs in the pool MUST BE deleted.
public static void deleteCaPool(String project, String location, String pool_Id) throws InterruptedException, ExecutionException, IOException {
    // clean up any remaining background resources.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        // Set the project, location and pool_Id to delete.
        CaPoolName caPool = CaPoolName.newBuilder().setProject(project).setLocation(location).setCaPool(pool_Id).build();
        // Create the Delete request.
        DeleteCaPoolRequest deleteCaPoolRequest = DeleteCaPoolRequest.newBuilder().setName(caPool.toString()).build();
        // Delete the CA Pool.
        ApiFuture<Operation> futureCall = certificateAuthorityServiceClient.deleteCaPoolCallable().futureCall(deleteCaPoolRequest);
        Operation response = futureCall.get();
        if (response.hasError()) {
            System.out.println("Error while deleting CA pool !" + response.getError());
            return;
        }
        System.out.println("Deleted CA Pool: " + pool_Id);
    }
}
Also used : DeleteCaPoolRequest(com.google.cloud.security.privateca.v1.DeleteCaPoolRequest) CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) Operation(com.google.longrunning.Operation) CaPoolName(com.google.cloud.security.privateca.v1.CaPoolName)

Example 12 with CertificateAuthorityServiceClient

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

the class DeleteCertificateAuthority method deleteCertificateAuthority.

// Delete the Certificate Authority from the specified CA pool.
// Before deletion, the CA must be disabled and must not contain any active certificates.
public static void deleteCertificateAuthority(String project, String location, String pool_Id, String certificateAuthorityName) throws IOException, ExecutionException, InterruptedException {
    // clean up any remaining background resources.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        // Create the Certificate Authority Name.
        CertificateAuthorityName certificateAuthorityNameParent = CertificateAuthorityName.newBuilder().setProject(project).setLocation(location).setCaPool(pool_Id).setCertificateAuthority(certificateAuthorityName).build();
        // Check if the CA is enabled.
        State caState = certificateAuthorityServiceClient.getCertificateAuthority(certificateAuthorityNameParent).getState();
        if (caState == State.ENABLED) {
            System.out.println("Please disable the Certificate Authority before deletion ! Current state: " + caState);
            return;
        }
        // Create the DeleteCertificateAuthorityRequest.
        // Setting the setIgnoreActiveCertificates() to true, will delete the CA
        // even if it contains active certificates. Care should be taken to re-anchor
        // the certificates to new CA before deleting.
        DeleteCertificateAuthorityRequest deleteCertificateAuthorityRequest = DeleteCertificateAuthorityRequest.newBuilder().setName(certificateAuthorityNameParent.toString()).setIgnoreActiveCertificates(false).build();
        // Delete the Certificate Authority.
        ApiFuture<Operation> futureCall = certificateAuthorityServiceClient.deleteCertificateAuthorityCallable().futureCall(deleteCertificateAuthorityRequest);
        Operation response = futureCall.get();
        if (response.hasError()) {
            System.out.println("Error while deleting Certificate Authority !" + response.getError());
            return;
        }
        // Check if the CA has been deleted.
        caState = certificateAuthorityServiceClient.getCertificateAuthority(certificateAuthorityNameParent).getState();
        if (caState == State.DELETED) {
            System.out.println("Successfully deleted Certificate Authority : " + certificateAuthorityName);
        } else {
            System.out.println("Unable to delete Certificate Authority. Please try again ! Current state: " + caState);
        }
    }
}
Also used : State(com.google.cloud.security.privateca.v1.CertificateAuthority.State) CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CertificateAuthorityName(com.google.cloud.security.privateca.v1.CertificateAuthorityName) Operation(com.google.longrunning.Operation) DeleteCertificateAuthorityRequest(com.google.cloud.security.privateca.v1.DeleteCertificateAuthorityRequest)

Example 13 with CertificateAuthorityServiceClient

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

the class EnableCertificateAuthority method enableCertificateAuthority.

// Enable the Certificate Authority present in the given ca pool.
// CA cannot be enabled if it has been already deleted.
public static void enableCertificateAuthority(String project, String location, String pool_Id, String certificateAuthorityName) throws IOException, ExecutionException, InterruptedException {
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        // Create the Certificate Authority Name.
        CertificateAuthorityName certificateAuthorityParent = CertificateAuthorityName.newBuilder().setProject(project).setLocation(location).setCaPool(pool_Id).setCertificateAuthority(certificateAuthorityName).build();
        // Create the Enable Certificate Authority Request.
        EnableCertificateAuthorityRequest enableCertificateAuthorityRequest = EnableCertificateAuthorityRequest.newBuilder().setName(certificateAuthorityParent.toString()).build();
        // Enable the Certificate Authority.
        ApiFuture<Operation> futureCall = certificateAuthorityServiceClient.enableCertificateAuthorityCallable().futureCall(enableCertificateAuthorityRequest);
        Operation response = futureCall.get();
        if (response.hasError()) {
            System.out.println("Error while enabling Certificate Authority !" + response.getError());
            return;
        }
        // Get the current CA state.
        State caState = certificateAuthorityServiceClient.getCertificateAuthority(certificateAuthorityParent).getState();
        // Check if the CA is enabled.
        if (caState == State.ENABLED) {
            System.out.println("Enabled Certificate Authority : " + certificateAuthorityName);
        } else {
            System.out.println("Cannot enable the Certificate Authority ! Current CA State: " + caState);
        }
    }
}
Also used : State(com.google.cloud.security.privateca.v1.CertificateAuthority.State) CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CertificateAuthorityName(com.google.cloud.security.privateca.v1.CertificateAuthorityName) EnableCertificateAuthorityRequest(com.google.cloud.security.privateca.v1.EnableCertificateAuthorityRequest) Operation(com.google.longrunning.Operation)

Example 14 with CertificateAuthorityServiceClient

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

the class ListCaPools method listCaPools.

// List all CA pools present in the given project and location.
public static void listCaPools(String project, String location) throws IOException {
    // clean up any remaining background resources.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        // Set the Location Name which contains project and location of the pool.
        LocationName locationName = LocationName.newBuilder().setProject(project).setLocation(location).build();
        String caPoolName = "";
        System.out.println("Available CA pools: ");
        // List the CA pools.
        for (CaPool caPool : certificateAuthorityServiceClient.listCaPools(locationName).iterateAll()) {
            caPoolName = caPool.getName();
            // caPoolName represents the full resource name of the
            // format 'projects/{project-id}/locations/{location}/ca-pools/{ca-pool-id}'.
            // Hence stripping it down to just CA pool id.
            System.out.println(caPoolName.substring(caPoolName.lastIndexOf("/") + 1) + " " + caPool.isInitialized());
        }
    }
}
Also used : CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CaPool(com.google.cloud.security.privateca.v1.CaPool) LocationName(com.google.cloud.security.privateca.v1.LocationName)

Example 15 with CertificateAuthorityServiceClient

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

the class RevokeCertificate method revokeCertificate.

// Revoke an issued certificate. Once revoked, the certificate will become invalid and will expire
// post its lifetime.
public static void revokeCertificate(String project, String location, String pool_Id, String certificateName) throws IOException, ExecutionException, InterruptedException {
    // clean up any remaining background resources.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        // Create Certificate Name.
        CertificateName certificateNameParent = CertificateName.newBuilder().setProject(project).setLocation(location).setCaPool(pool_Id).setCertificate(certificateName).build();
        // Create Revoke Certificate Request and specify the appropriate revocation reason.
        RevokeCertificateRequest revokeCertificateRequest = RevokeCertificateRequest.newBuilder().setName(certificateNameParent.toString()).setReason(RevocationReason.PRIVILEGE_WITHDRAWN).build();
        // Revoke certificate.
        ApiFuture<Certificate> response = certificateAuthorityServiceClient.revokeCertificateCallable().futureCall(revokeCertificateRequest);
        Certificate certificateResponse = response.get();
        System.out.println("Certificate Revoked: " + certificateResponse.getName());
    }
}
Also used : CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) RevokeCertificateRequest(com.google.cloud.security.privateca.v1.RevokeCertificateRequest) CertificateName(com.google.cloud.security.privateca.v1.CertificateName) Certificate(com.google.cloud.security.privateca.v1.Certificate)

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