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

Commit e79724d

Browse files
docs(samples): added samples for issuance policy and certificate templates (#264)
* docs(samples): init commit - set issuance policy * docs(samples): added certificate template CRUD samples * refactor(samples): modified the samples for test coherence * test(samples): Added tests for issuance policy and certificate templates. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * refactor(samples): included filter condition and comments * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * refactor(samples): included review comments * 🦉 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 fa2df57 commit e79724d

File tree

10 files changed

+624
-27
lines changed

10 files changed

+624
-27
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,23 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-security-priv
8080
| 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) |
8181
| 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) |
8282
| 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 Template | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/CreateCertificateTemplate.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/CreateCertificateTemplate.java) |
8384
| 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) |
8485
| 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) |
8586
| 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) |
8687
| 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) |
88+
| Delete Certificate Template | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/DeleteCertificateTemplate.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/DeleteCertificateTemplate.java) |
8789
| 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) |
8890
| 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) |
8991
| 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) |
9092
| 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) |
9193
| 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) |
94+
| List Certificate Templates | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/ListCertificateTemplates.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/ListCertificateTemplates.java) |
9295
| 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) |
9396
| 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) |
9497
| 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) |
98+
| Update Ca Pool_Issuance Policy | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/UpdateCaPool_IssuancePolicy.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/UpdateCaPool_IssuancePolicy.java) |
99+
| Update Certificate Template | [source code](https://github.com/googleapis/java-security-private-ca/blob/master/samples/snippets/cloud-client/src/main/java/privateca/UpdateCertificateTemplate.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/UpdateCertificateTemplate.java) |
95100

96101

97102

samples/snippets/cloud-client/src/main/java/privateca/CreateCertificate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static void createCertificate(
8585
// certificateLifetime: The validity of the certificate in seconds.
8686
String commonName = "common-name";
8787
String orgName = "org-name";
88-
String domainName = "dnsname.com";
88+
String domainName = "dns.your-domain.com";
8989
long certificateLifetime = 1000L;
9090

9191
// Set the Public Key and its format.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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_template]
19+
20+
import com.google.api.core.ApiFuture;
21+
import com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient;
22+
import com.google.cloud.security.privateca.v1.CertificateIdentityConstraints;
23+
import com.google.cloud.security.privateca.v1.CertificateTemplate;
24+
import com.google.cloud.security.privateca.v1.CreateCertificateTemplateRequest;
25+
import com.google.cloud.security.privateca.v1.KeyUsage;
26+
import com.google.cloud.security.privateca.v1.KeyUsage.ExtendedKeyUsageOptions;
27+
import com.google.cloud.security.privateca.v1.KeyUsage.KeyUsageOptions;
28+
import com.google.cloud.security.privateca.v1.LocationName;
29+
import com.google.cloud.security.privateca.v1.X509Parameters;
30+
import com.google.cloud.security.privateca.v1.X509Parameters.CaOptions;
31+
import com.google.longrunning.Operation;
32+
import com.google.type.Expr;
33+
import java.io.IOException;
34+
import java.util.concurrent.ExecutionException;
35+
import java.util.concurrent.TimeUnit;
36+
import java.util.concurrent.TimeoutException;
37+
38+
public class CreateCertificateTemplate {
39+
40+
public static void main(String[] args)
41+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
42+
/* TODO(developer): Replace these variables before running the sample.
43+
location: For a list of locations, see:
44+
https://cloud.google.com/certificate-authority-service/docs/locations */
45+
String project = "your-project-id";
46+
String location = "ca-location";
47+
String certificateTemplateId = "certificate-template-id";
48+
49+
createCertificateTemplate(project, location, certificateTemplateId);
50+
}
51+
52+
/* Creates a Certificate template. These templates can be reused for common
53+
certificate issuance scenarios. */
54+
public static void createCertificateTemplate(
55+
String project, String location, String certificateTemplateId)
56+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
57+
/* Initialize client that will be used to send requests. This client only needs to be created
58+
once, and can be reused for multiple requests. After completing all of your requests, call
59+
the `certificateAuthorityServiceClient.close()` method on the client to safely
60+
clean up any remaining background resources. */
61+
try (CertificateAuthorityServiceClient certificateAuthorityServiceClient =
62+
CertificateAuthorityServiceClient.create()) {
63+
64+
/* Describes any predefined X.509 values set by this template.
65+
The provided extensions are copied over to certificate requests that use this template.*/
66+
KeyUsage keyUsage =
67+
KeyUsage.newBuilder()
68+
.setBaseKeyUsage(
69+
KeyUsageOptions.newBuilder()
70+
.setDigitalSignature(true)
71+
.setKeyEncipherment(true)
72+
.build())
73+
.setExtendedKeyUsage(ExtendedKeyUsageOptions.newBuilder().setServerAuth(true).build())
74+
.build();
75+
76+
CaOptions caOptions = CaOptions.newBuilder().setIsCa(false).build();
77+
78+
/* CEL expression that is evaluated against the Subject and
79+
Subject Alternative Name of the certificate before it is issued. */
80+
Expr expr =
81+
Expr.newBuilder().setExpression("subject_alt_names.all(san, san.type == DNS)").build();
82+
83+
// Set the certificate issuance schema.
84+
CertificateTemplate certificateTemplate =
85+
CertificateTemplate.newBuilder()
86+
.setPredefinedValues(
87+
X509Parameters.newBuilder().setKeyUsage(keyUsage).setCaOptions(caOptions).build())
88+
.setIdentityConstraints(
89+
CertificateIdentityConstraints.newBuilder()
90+
.setCelExpression(expr)
91+
.setAllowSubjectPassthrough(false)
92+
.setAllowSubjectAltNamesPassthrough(false)
93+
.build())
94+
.build();
95+
96+
// Set the parent and certificate template properties.
97+
CreateCertificateTemplateRequest certificateTemplateRequest =
98+
CreateCertificateTemplateRequest.newBuilder()
99+
.setParent(LocationName.of(project, location).toString())
100+
.setCertificateTemplate(certificateTemplate)
101+
.setCertificateTemplateId(certificateTemplateId)
102+
.build();
103+
104+
// Create Template request.
105+
ApiFuture<Operation> futureCall =
106+
certificateAuthorityServiceClient
107+
.createCertificateTemplateCallable()
108+
.futureCall(certificateTemplateRequest);
109+
110+
Operation response = futureCall.get(60, TimeUnit.SECONDS);
111+
112+
if (response.hasError()) {
113+
System.out.println("Error creating certificate template ! " + response.getError());
114+
return;
115+
}
116+
117+
System.out.println("Successfully created certificate template ! " + response.getName());
118+
}
119+
}
120+
}
121+
// [END privateca_create_certificate_template]

samples/snippets/cloud-client/src/main/java/privateca/CreateSubordinateCa.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.cloud.security.privateca.v1.KeyUsage;
3030
import com.google.cloud.security.privateca.v1.KeyUsage.KeyUsageOptions;
3131
import com.google.cloud.security.privateca.v1.Subject;
32+
import com.google.cloud.security.privateca.v1.SubjectAltNames;
3233
import com.google.cloud.security.privateca.v1.X509Parameters;
3334
import com.google.cloud.security.privateca.v1.X509Parameters.CaOptions;
3435
import com.google.longrunning.Operation;
@@ -65,6 +66,7 @@ public static void createSubordinateCertificateAuthority(
6566

6667
String commonName = "common-name";
6768
String orgName = "csr-org-name";
69+
String domainName = "dns.your-domain.com";
6870
int caDuration = 100000; // Validity of this CA in seconds.
6971

7072
// Set the type of Algorithm.
@@ -76,6 +78,8 @@ public static void createSubordinateCertificateAuthority(
7678
SubjectConfig.newBuilder()
7779
.setSubject(
7880
Subject.newBuilder().setCommonName(commonName).setOrganization(orgName).build())
81+
// Set the fully qualified domain name.
82+
.setSubjectAltName(SubjectAltNames.newBuilder().addDnsNames(domainName).build())
7983
.build();
8084

8185
// Set the key usage options for X.509 fields.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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_delete_certificate_template]
19+
20+
import com.google.api.core.ApiFuture;
21+
import com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient;
22+
import com.google.cloud.security.privateca.v1.CertificateTemplateName;
23+
import com.google.cloud.security.privateca.v1.DeleteCertificateTemplateRequest;
24+
import com.google.longrunning.Operation;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.TimeoutException;
29+
30+
public class DeleteCertificateTemplate {
31+
32+
public static void main(String[] args)
33+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
34+
/* TODO(developer): Replace these variables before running the sample.
35+
location: For a list of locations, see:
36+
https://cloud.google.com/certificate-authority-service/docs/locations
37+
certificateTemplateId: Id of the certificate template to delete. */
38+
String project = "your-project-id";
39+
String location = "ca-location";
40+
String certificateTemplateId = "certificate-template-id";
41+
42+
deleteCertificateTemplate(project, location, certificateTemplateId);
43+
}
44+
45+
// Deletes the certificate template present in the given project and location.
46+
public static void deleteCertificateTemplate(
47+
String project, String location, String certificateTemplateId)
48+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
49+
/* Initialize client that will be used to send requests. This client only needs to be created
50+
once, and can be reused for multiple requests. After completing all of your requests, call
51+
the `certificateAuthorityServiceClient.close()` method on the client to safely
52+
clean up any remaining background resources. */
53+
try (CertificateAuthorityServiceClient certificateAuthorityServiceClient =
54+
CertificateAuthorityServiceClient.create()) {
55+
56+
// Set the parent name of the certificate template to be deleted.
57+
DeleteCertificateTemplateRequest request =
58+
DeleteCertificateTemplateRequest.newBuilder()
59+
.setName(
60+
CertificateTemplateName.of(project, location, certificateTemplateId).toString())
61+
.build();
62+
63+
ApiFuture<Operation> futureCall =
64+
certificateAuthorityServiceClient.deleteCertificateTemplateCallable().futureCall(request);
65+
66+
Operation response = futureCall.get(60, TimeUnit.SECONDS);
67+
68+
// Check for errors.
69+
if (response.hasError()) {
70+
System.out.println("Error deleting the certificate template ! " + response.getError());
71+
return;
72+
}
73+
74+
System.out.println("Successfully created certificate template ! " + response.getName());
75+
}
76+
}
77+
}
78+
// [END privateca_delete_certificate_template]

0 commit comments

Comments
 (0)