Skip to content

Commit eecf9fb

Browse files
rsamborskitelpirionkweinmeistergotochkinyucentao
authored
feat(fixit): Adding privateca/ca_pool samples (GoogleCloudPlatform#3068)
* feat(fixit): Adding privateca/ca_pool samples Co-authored-by: Eric Schmidt <erschmid@google.com> Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com> Co-authored-by: Gleb Otochkin <gleb.otochkin@gmail.com> Co-authored-by: yucentao <yucentao@google.com>
1 parent cadb0c5 commit eecf9fb

File tree

8 files changed

+500
-0
lines changed

8 files changed

+500
-0
lines changed

.github/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
/iam/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/dee-infra
6464
/iap/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/dee-infra
6565
/kms/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/dee-infra
66+
/privateca/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/dee-infra
6667
/securitycenter/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/dee-infra
6768
/secretmanager/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/dee-infra
6869
/mediacdn/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/dee-infra @justin-mp

.github/blunderbuss.yml

+22
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ assign_issues_by:
5353
- 'api: dlp'
5454
to:
5555
- GoogleCloudPlatform/googleapis-dlp
56+
- labels:
57+
- "api: batch"
58+
- "api: compute"
59+
- "api: cloudkms"
60+
- "api: iam"
61+
- "api: kms"
62+
- "api: privateca"
63+
- "api: secretmanager"
64+
- "api: securitycenter"
65+
to:
66+
- GoogleCloudPlatform/dee-infra
5667

5768
assign_prs_by:
5869
- labels:
@@ -74,3 +85,14 @@ assign_prs_by:
7485
- 'api: dlp'
7586
to:
7687
- GoogleCloudPlatform/googleapis-dlp
88+
- labels:
89+
- "api: batch"
90+
- "api: compute"
91+
- "api: cloudkms"
92+
- "api: iam"
93+
- "api: kms"
94+
- "api: privateca"
95+
- "api: secretmanager"
96+
- "api: securitycenter"
97+
to:
98+
- GoogleCloudPlatform/dee-infra

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@
3535
testing/kokoro/test-env.sh
3636
.envrc
3737
.DS_Store
38+
go.work
39+
go.work.sum

privateca/create_ca_pool.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package snippets
16+
17+
// [START privateca_create_ca_pool]
18+
import (
19+
"context"
20+
"fmt"
21+
"io"
22+
23+
privateca "cloud.google.com/go/security/privateca/apiv1"
24+
"cloud.google.com/go/security/privateca/apiv1/privatecapb"
25+
)
26+
27+
// Create a Certificate Authority pool. All certificates created under this CA pool will
28+
// follow the same issuance policy, IAM policies, etc.
29+
func createCaPool(w io.Writer, projectID string, location string, caPoolId string) error {
30+
// projectID := "your_project_id"
31+
// location := "us-central1" // For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
32+
// caPoolId := "ca-pool-id" // A unique id/name for the ca pool.
33+
34+
ctx := context.Background()
35+
caClient, err := privateca.NewCertificateAuthorityClient(ctx)
36+
if err != nil {
37+
return fmt.Errorf("NewCertificateAuthorityClient creation failed: %w", err)
38+
}
39+
defer caClient.Close()
40+
41+
caPool := &privatecapb.CaPool{
42+
// Set the tier (see: https://cloud.google.com/certificate-authority-service/docs/tiers).
43+
Tier: privatecapb.CaPool_ENTERPRISE,
44+
}
45+
46+
locationPath := fmt.Sprintf("projects/%s/locations/%s", projectID, location)
47+
48+
// See https://pkg.go.dev/cloud.google.com/go/security/privateca/apiv1/privatecapb#CreateCaPoolRequest.
49+
req := &privatecapb.CreateCaPoolRequest{
50+
Parent: locationPath,
51+
CaPoolId: caPoolId,
52+
CaPool: caPool,
53+
}
54+
55+
op, err := caClient.CreateCaPool(ctx, req)
56+
if err != nil {
57+
return fmt.Errorf("CreateCaPool failed: %w", err)
58+
}
59+
60+
if _, err = op.Wait(ctx); err != nil {
61+
return fmt.Errorf("CreateCaPool failed during wait: %w", err)
62+
}
63+
64+
fmt.Fprintf(w, "CA Pool created")
65+
66+
return nil
67+
}
68+
69+
// [END privateca_create_ca_pool]

privateca/delete_ca_pool.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package snippets
16+
17+
// [START privateca_delete_ca_pool]
18+
import (
19+
"context"
20+
"fmt"
21+
"io"
22+
23+
privateca "cloud.google.com/go/security/privateca/apiv1"
24+
"cloud.google.com/go/security/privateca/apiv1/privatecapb"
25+
)
26+
27+
// Delete the CA pool as mentioned by the ca_pool_name.
28+
// Before deleting the pool, all CAs in the pool MUST BE deleted.
29+
func deleteCaPool(w io.Writer, projectID string, location string, caPoolId string) error {
30+
// projectID := "your_project_id"
31+
// location := "us-central1" // For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
32+
// caPoolId := "ca-pool-id" // A unique id/name for the ca pool.
33+
34+
ctx := context.Background()
35+
caClient, err := privateca.NewCertificateAuthorityClient(ctx)
36+
if err != nil {
37+
return fmt.Errorf("NewCertificateAuthorityClient creation failed: %w", err)
38+
}
39+
defer caClient.Close()
40+
41+
fullCaPoolName := fmt.Sprintf("projects/%s/locations/%s/caPools/%s", projectID, location, caPoolId)
42+
43+
// See https://pkg.go.dev/cloud.google.com/go/security/privateca/apiv1/privatecapb#DeleteCaPoolRequest.
44+
req := &privatecapb.DeleteCaPoolRequest{
45+
Name: fullCaPoolName,
46+
}
47+
48+
op, err := caClient.DeleteCaPool(ctx, req)
49+
if err != nil {
50+
return fmt.Errorf("DeleteCaPool failed: %w", err)
51+
}
52+
53+
if err = op.Wait(ctx); err != nil {
54+
return fmt.Errorf("DeleteCaPool failed during wait: %w", err)
55+
}
56+
57+
fmt.Fprintf(w, "CA Pool deleted")
58+
59+
return nil
60+
}
61+
62+
// [END privateca_delete_ca_pool]

privateca/go.mod

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module github.com/GoogleCloudPlatform/golang-samples/privateca
2+
3+
go 1.19
4+
5+
require (
6+
cloud.google.com/go/security v1.14.1
7+
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20230517040748-5b807c48c3ca
8+
google.golang.org/api v0.122.0
9+
)
10+
11+
require (
12+
cloud.google.com/go v0.110.0 // indirect
13+
cloud.google.com/go/compute v1.19.0 // indirect
14+
cloud.google.com/go/compute/metadata v0.2.3 // indirect
15+
cloud.google.com/go/iam v0.13.0 // indirect
16+
cloud.google.com/go/longrunning v0.4.1 // indirect
17+
cloud.google.com/go/storage v1.30.1 // indirect
18+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
19+
github.com/golang/protobuf v1.5.3 // indirect
20+
github.com/google/go-cmp v0.5.9 // indirect
21+
github.com/google/s2a-go v0.1.3 // indirect
22+
github.com/google/uuid v1.3.0 // indirect
23+
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
24+
github.com/googleapis/gax-go/v2 v2.8.0 // indirect
25+
go.opencensus.io v0.24.0 // indirect
26+
golang.org/x/crypto v0.7.0 // indirect
27+
golang.org/x/net v0.9.0 // indirect
28+
golang.org/x/oauth2 v0.7.0 // indirect
29+
golang.org/x/sys v0.7.0 // indirect
30+
golang.org/x/text v0.9.0 // indirect
31+
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
32+
google.golang.org/appengine v1.6.7 // indirect
33+
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
34+
google.golang.org/grpc v1.55.0 // indirect
35+
google.golang.org/protobuf v1.30.0 // indirect
36+
)

0 commit comments

Comments
 (0)