Skip to content

Commit aab5bb5

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 c2129ae commit aab5bb5

9 files changed

+619
-27
lines changed

privateca/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]

privateca/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]

privateca/cloud-client/src/main/java/privateca/FilterCertificates.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,16 @@ public static void main(String[] args) throws IOException {
3030
// location: For a list of locations, see:
3131
// https://cloud.google.com/certificate-authority-service/docs/locations
3232
// pool_Id: Id of the CA pool which contains the certificates to be listed.
33-
// filterCondition: Filter certificates based on the given condition.
34-
// For more info on conditions supported,
35-
// see:
36-
// https://cloud.google.com/certificate-authority-service/docs/sorting-filtering-certificates#filtering_support
3733
String project = "your-project-id";
3834
String location = "ca-location";
3935
String pool_Id = "ca-pool-id";
40-
String filterCondition = "filter-condition";
4136

42-
filterCertificates(project, location, pool_Id, filterCondition);
37+
filterCertificates(project, location, pool_Id);
4338
}
4439

4540
// Filter certificates based on a condition and list them.
46-
public static void filterCertificates(
47-
String project, String location, String pool_Id, String filterCondition) throws IOException {
41+
public static void filterCertificates(String project, String location, String pool_Id)
42+
throws IOException {
4843
// Initialize client that will be used to send requests. This client only needs to be created
4944
// once, and can be reused for multiple requests. After completing all of your requests, call
5045
// the `certificateAuthorityServiceClient.close()` method on the client to safely
@@ -63,8 +58,16 @@ public static void filterCertificates(
6358
ListCertificatesRequest listCertificatesRequest =
6459
ListCertificatesRequest.newBuilder()
6560
.setParent(caPool.toString())
66-
// Filter certificates according to the given condition.
67-
.setFilter(filterCondition)
61+
/* Filter certificates based on the given condition.
62+
For more info on conditions supported,
63+
see:
64+
https://cloud.google.com/certificate-authority-service/docs/sorting-filtering-certificates#filtering_support
65+
Few examples for constructing conditions:
66+
certificate_description.subject_description.not_after_time=timestamp(com.google.protobuf)
67+
certificate_description.subject_description.subject_alt_name.dns_names:my-dns
68+
Here, we are filtering certificates which has organization name = csr-org-name */
69+
.setFilter(
70+
"certificate_description.subject_description.subject.organization=csr-org-name")
6871
.build();
6972

7073
// Retrieve and print the certificate names.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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_list_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.CertificateTemplate;
23+
import com.google.cloud.security.privateca.v1.ListCertificateTemplatesRequest;
24+
import com.google.cloud.security.privateca.v1.ListCertificateTemplatesResponse;
25+
import com.google.cloud.security.privateca.v1.LocationName;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
import java.util.concurrent.TimeUnit;
29+
import java.util.concurrent.TimeoutException;
30+
31+
public class ListCertificateTemplates {
32+
33+
public static void main(String[] args)
34+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
35+
/* TODO(developer): Replace these variables before running the sample.
36+
location: For a list of locations, see:
37+
https://cloud.google.com/certificate-authority-service/docs/locations */
38+
String project = "your-project-id";
39+
String location = "ca-location";
40+
41+
listCertificateTemplates(project, location);
42+
}
43+
44+
// Lists the certificate templates present in the given project and location.
45+
public static void listCertificateTemplates(String project, String location)
46+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
47+
/* Initialize client that will be used to send requests. This client only needs to be created
48+
once, and can be reused for multiple requests. After completing all of your requests, call
49+
the `certificateAuthorityServiceClient.close()` method on the client to safely
50+
clean up any remaining background resources. */
51+
try (CertificateAuthorityServiceClient certificateAuthorityServiceClient =
52+
CertificateAuthorityServiceClient.create()) {
53+
54+
// Set the parent name to list the certificate templates.
55+
ListCertificateTemplatesRequest request =
56+
ListCertificateTemplatesRequest.newBuilder()
57+
.setParent(LocationName.of(project, location).toString())
58+
.build();
59+
60+
ApiFuture<ListCertificateTemplatesResponse> futureCall =
61+
certificateAuthorityServiceClient.listCertificateTemplatesCallable().futureCall(request);
62+
63+
// Get the response.
64+
ListCertificateTemplatesResponse response = futureCall.get(60, TimeUnit.SECONDS);
65+
66+
// List all templates.
67+
for (CertificateTemplate template : response.getCertificateTemplatesList()) {
68+
System.out.println(template.getName());
69+
}
70+
}
71+
}
72+
}
73+
// [END privateca_list_certificate_template]

0 commit comments

Comments
 (0)