Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.

Commit 7454cb0

Browse files
docs(samples): adding client library samples (#242)
* feat: add samples for creating and activating subordinate ca * feat: add sample for filtering certificate. * feat: add sample for undeleting CA. * fix: adding all pem certificates in chain. * docs: lint fix * refactor: filter conditions changed to arg * test: added test cases * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: region tag mismatch * fix: correct region tag mismatch * refactor: added comments for certificate chain setting * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs: lint fix * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 2f7e1a3 commit 7454cb0

7 files changed

Lines changed: 684 additions & 8 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,22 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-security-priv
7676

7777
| Sample | Source Code | Try it |
7878
| --------------------------- | --------------------------------- | ------ |
79+
| Activate Subordinate Ca | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/ActivateSubordinateCa.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/ActivateSubordinateCa.java) |
7980
| Create Ca Pool | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/CreateCaPool.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/CreateCaPool.java) |
8081
| Create Certificate | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/CreateCertificate.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/CreateCertificate.java) |
8182
| Create Certificate Authority | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/CreateCertificateAuthority.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/CreateCertificateAuthority.java) |
83+
| Create Certificate_CSR | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/CreateCertificate_CSR.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/CreateCertificate_CSR.java) |
84+
| Create Subordinate Ca | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/CreateSubordinateCa.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/CreateSubordinateCa.java) |
8285
| Delete Ca Pool | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/DeleteCaPool.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/DeleteCaPool.java) |
8386
| Delete Certificate Authority | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/DeleteCertificateAuthority.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/DeleteCertificateAuthority.java) |
8487
| Disable Certificate Authority | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/DisableCertificateAuthority.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/DisableCertificateAuthority.java) |
8588
| Enable Certificate Authority | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/EnableCertificateAuthority.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/EnableCertificateAuthority.java) |
89+
| Filter Certificates | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/FilterCertificates.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/FilterCertificates.java) |
8690
| List Ca Pools | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/ListCaPools.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/ListCaPools.java) |
8791
| List Certificate Authorities | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/ListCertificateAuthorities.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/ListCertificateAuthorities.java) |
8892
| List Certificates | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/ListCertificates.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/ListCertificates.java) |
8993
| Revoke Certificate | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/RevokeCertificate.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/RevokeCertificate.java) |
94+
| Undelete Certificate Authority | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/UndeleteCertificateAuthority.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-security-private-ca&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/privateca/UndeleteCertificateAuthority.java) |
9095

9196

9297

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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]
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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_create_certificate_csr]
19+
20+
import com.google.api.core.ApiFuture;
21+
import com.google.cloud.security.privateca.v1.CaPoolName;
22+
import com.google.cloud.security.privateca.v1.Certificate;
23+
import com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient;
24+
import com.google.cloud.security.privateca.v1.CreateCertificateRequest;
25+
import com.google.protobuf.Duration;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
29+
public class CreateCertificate_CSR {
30+
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
35+
// location: For a list of locations, see:
36+
// https://cloud.google.com/certificate-authority-service/docs/locations
37+
// pool_Id: Set a unique id for the CA pool.
38+
// certificateAuthorityName: The name of the certificate authority to sign the CSR.
39+
// certificateName: Set a unique name for the certificate.
40+
// pemCSR: Set the Certificate Issuing Request in the pem encoded format.
41+
String project = "your-project-id";
42+
String location = "ca-location";
43+
String pool_Id = "ca-pool-id";
44+
String certificateAuthorityName = "certificate-authority-name";
45+
String certificateName = "certificate-name";
46+
String pemCSR =
47+
"-----BEGIN CERTIFICATE REQUEST-----\n"
48+
+ "sample-pem-csr-format\n"
49+
+ "-----END CERTIFICATE REQUEST-----";
50+
51+
createCertificateWithCSR(
52+
project, location, pool_Id, certificateAuthorityName, certificateName, pemCSR);
53+
}
54+
55+
// Create a Certificate which is issued by the specified Certificate Authority.
56+
// The certificate details and the public key is provided as a CSR (Certificate Signing Request).
57+
public static void createCertificateWithCSR(
58+
String project,
59+
String location,
60+
String pool_Id,
61+
String certificateAuthorityName,
62+
String certificateName,
63+
String pemCSR)
64+
throws IOException, ExecutionException, InterruptedException {
65+
// Initialize client that will be used to send requests. This client only needs to be created
66+
// once, and can be reused for multiple requests. After completing all of your requests, call
67+
// the `certificateAuthorityServiceClient.close()` method on the client to safely
68+
// clean up any remaining background resources.
69+
try (CertificateAuthorityServiceClient certificateAuthorityServiceClient =
70+
CertificateAuthorityServiceClient.create()) {
71+
// certificateLifetime: The validity of the certificate in seconds.
72+
long certificateLifetime = 1000L;
73+
74+
// Create certificate with CSR.
75+
// The pemCSR contains the public key and the domain details required.
76+
Certificate certificate =
77+
Certificate.newBuilder()
78+
.setPemCsr(pemCSR)
79+
.setLifetime(Duration.newBuilder().setSeconds(certificateLifetime).build())
80+
.build();
81+
82+
// Create the Certificate Request.
83+
// Set the CA which is responsible for creating the certificate with the provided CSR.
84+
CreateCertificateRequest certificateRequest =
85+
CreateCertificateRequest.newBuilder()
86+
.setParent(CaPoolName.of(project, location, pool_Id).toString())
87+
.setIssuingCertificateAuthorityId(certificateAuthorityName)
88+
.setCertificateId(certificateName)
89+
.setCertificate(certificate)
90+
.build();
91+
92+
// Get the certificate response.
93+
ApiFuture<Certificate> future =
94+
certificateAuthorityServiceClient
95+
.createCertificateCallable()
96+
.futureCall(certificateRequest);
97+
98+
Certificate certificateResponse = future.get();
99+
100+
System.out.println("Certificate created successfully : " + certificateResponse.getName());
101+
102+
// Get the signed certificate and the issuer chain list.
103+
System.out.println("Signed certificate:\n " + certificateResponse.getPemCertificate());
104+
System.out.println("Issuer chain list:\n" + certificateResponse.getPemCertificateChainList());
105+
}
106+
}
107+
}
108+
// [END privateca_create_certificate_csr]

0 commit comments

Comments
 (0)