|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package privateca; |
| 17 | + |
| 18 | +// [START privateca_activate_subordinateca] |
| 19 | + |
| 20 | +import com.google.api.core.ApiFuture; |
| 21 | +import com.google.cloud.security.privateca.v1.ActivateCertificateAuthorityRequest; |
| 22 | +import com.google.cloud.security.privateca.v1.CertificateAuthorityName; |
| 23 | +import com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient; |
| 24 | +import com.google.cloud.security.privateca.v1.SubordinateConfig; |
| 25 | +import com.google.longrunning.Operation; |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.concurrent.ExecutionException; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | + |
| 30 | +public class ActivateSubordinateCa { |
| 31 | + |
| 32 | + public static void main(String[] args) |
| 33 | + throws InterruptedException, ExecutionException, IOException { |
| 34 | + // TODO(developer): Replace these variables before running the sample. |
| 35 | + |
| 36 | + // location: For a list of locations, see: |
| 37 | + // https://cloud.google.com/certificate-authority-service/docs/locations |
| 38 | + // pool_Id: Set a unique id for the CA pool. |
| 39 | + // subordinateCaName: The CA to be activated. |
| 40 | + // pemCACertificate: The signed certificate, obtained by signing the CSR. |
| 41 | + String project = "your-project-id"; |
| 42 | + String location = "ca-location"; |
| 43 | + String pool_Id = "ca-pool-id"; |
| 44 | + String subordinateCaName = "subordinate-certificate-authority-name"; |
| 45 | + String pemCACertificate = |
| 46 | + "-----BEGIN CERTIFICATE-----\n" + "sample-pem-certificate\n" + "-----END CERTIFICATE-----"; |
| 47 | + |
| 48 | + // certificateAuthorityName: The name of the certificate authority which signed the CSR. |
| 49 | + // If an external CA (CA not present in Google Cloud) was used for signing, |
| 50 | + // then use the CA's issuerCertificateChain. |
| 51 | + String certificateAuthorityName = "certificate-authority-name"; |
| 52 | + |
| 53 | + activateSubordinateCA( |
| 54 | + project, location, pool_Id, certificateAuthorityName, subordinateCaName, pemCACertificate); |
| 55 | + } |
| 56 | + |
| 57 | + // Activate a subordinate CA. |
| 58 | + // *Prerequisite*: Get the CSR of the subordinate CA signed by another CA. Pass in the signed |
| 59 | + // certificate and (issuer CA's name or the issuer CA's Certificate chain). |
| 60 | + // *Post*: After activating the subordinate CA, it should be enabled before issuing certificates. |
| 61 | + public static void activateSubordinateCA( |
| 62 | + String project, |
| 63 | + String location, |
| 64 | + String pool_Id, |
| 65 | + String certificateAuthorityName, |
| 66 | + String subordinateCaName, |
| 67 | + String pemCACertificate) |
| 68 | + throws ExecutionException, InterruptedException, IOException { |
| 69 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 70 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 71 | + // the `certificateAuthorityServiceClient.close()` method on the client to safely |
| 72 | + // clean up any remaining background resources. |
| 73 | + try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = |
| 74 | + CertificateAuthorityServiceClient.create()) { |
| 75 | + // Subordinate CA parent. |
| 76 | + String subordinateCaParent = |
| 77 | + CertificateAuthorityName.of(project, location, pool_Id, subordinateCaName).toString(); |
| 78 | + |
| 79 | + // Construct the "Activate CA Request". |
| 80 | + ActivateCertificateAuthorityRequest activateCertificateAuthorityRequest = |
| 81 | + ActivateCertificateAuthorityRequest.newBuilder() |
| 82 | + .setName(subordinateCaParent) |
| 83 | + // The signed certificate. |
| 84 | + .setPemCaCertificate(pemCACertificate) |
| 85 | + .setSubordinateConfig( |
| 86 | + SubordinateConfig.newBuilder() |
| 87 | + // Follow one of the below methods: |
| 88 | + |
| 89 | + // Method 1: If issuer CA is in Google Cloud, set the Certificate Authority |
| 90 | + // Name. |
| 91 | + .setCertificateAuthority( |
| 92 | + CertificateAuthorityName.of( |
| 93 | + project, location, pool_Id, certificateAuthorityName) |
| 94 | + .toString()) |
| 95 | + |
| 96 | + // Method 2: If issuer CA is external to Google Cloud, set the issuer's |
| 97 | + // certificate chain. |
| 98 | + // The certificate chain of the CA (which signed the CSR) from leaf to root. |
| 99 | + // .setPemIssuerChain( |
| 100 | + // SubordinateConfigChain.newBuilder() |
| 101 | + // .addAllPemCertificates(issuerCertificateChain) |
| 102 | + // .build()) |
| 103 | + |
| 104 | + .build()) |
| 105 | + .build(); |
| 106 | + |
| 107 | + // Activate the CA. |
| 108 | + ApiFuture<Operation> futureCall = |
| 109 | + certificateAuthorityServiceClient |
| 110 | + .activateCertificateAuthorityCallable() |
| 111 | + .futureCall(activateCertificateAuthorityRequest); |
| 112 | + |
| 113 | + Operation response = futureCall.get(); |
| 114 | + |
| 115 | + if (response.hasError()) { |
| 116 | + System.out.println("Error while activating the subordinate CA! " + response.getError()); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + System.out.println( |
| 121 | + "Subordinate Certificate Authority activated successfully ! !" + subordinateCaName); |
| 122 | + TimeUnit.SECONDS.sleep(3); |
| 123 | + // The current state will be STAGED. |
| 124 | + // The Subordinate CA has to be ENABLED before issuing certificates. |
| 125 | + System.out.println( |
| 126 | + "Current State: " |
| 127 | + + certificateAuthorityServiceClient |
| 128 | + .getCertificateAuthority(subordinateCaParent) |
| 129 | + .getState()); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | +// [END privateca_activate_subordinateca] |
0 commit comments