Skip to content

Commit 7116d06

Browse files
rsamborskitelpirion
authored andcommitted
migrate code from googleapis/python-security-private-ca (#9028)
- feat(samples): private CA python samples - chore(deps): update dependency google-cloud-private-ca to v1 (#89) - chore(deps): update dependency google-cloud-kms to v2.4.1 (#88) - chore(deps): update dependency google-cloud-kms to v2.4.2 (#93) - chore(deps): update dependency google-auth to v1.33.1 (#87) - chore(deps): update dependency google-auth to v1.34.0 (#94) - chore(deps): update dependency google-cloud-kms to v2.4.3 (#96) - chore(deps): update dependency google-cloud-private-ca to v1.0.3 (#97) - feat(samples): add local generation for crypto keys (#98) - chore(deps): update dependency google-cloud-kms to v2.5.0 (#101) - chore: generate python samples templates in owlbot.py (#108) - chore(deps): update dependency cryptography to v3.4.8 (#109) - chore(deps): update dependency google-auth to v2 (#107) - chore(deps): update dependency google-cloud-private-ca to v1.0.4 (#103) - chore(deps): update dependency google-auth to v2.0.2 (#116) - chore(deps): update dependency google-cloud-kms to v2.6.0 (#115) - chore(deps): update dependency pytest to v6.2.5 (#114) - chore: blacken samples noxfile template (#121) - chore: fail samples nox session if python version is missing (#128) - chore(python): Add kokoro configs for python 3.10 samples testing (#134) - chore(deps): update dependency google-auth to v2.1.0 (#120) - chore(deps): update all dependencies (#144) - chore(deps): update all dependencies (#146) - chore(deps): update dependency google-cloud-private-ca to v1.2.1 (#147) - chore(python): run blacken session for all directories with a noxfile (#150) - chore(deps): update dependency cryptography to v36 (#154) - feat(samples): add subordinate CA samples - chore(deps): update dependency cryptography to v36.0.1 (#158) - chore(samples): Add check for tests in directory (#164) - chore(python): Noxfile recognizes that tests can live in a folder (#169) - docs(samples): add sample to filter certificates (#160) - chore(deps): update dependency google-auth to v2.6.0 (#173) - docs(samples): add template/monitoring samples (#174) - chore(deps): update all dependencies (#178) - chore(deps): update all dependencies (#186) - chore: Adding support for pytest-xdist and pytest-parallel (#193) - chore(deps): update all dependencies (#196) - chore(deps): update dependency google-cloud-private-ca to v1.3.0 (#199) - chore(deps): update dependency pytest to v7.1.0 (#200) - chore(deps): update dependency cryptography to v36.0.2 (#201) - chore(deps): update all dependencies (#202) - chore(python): use black==22.3.0 (#204) - chore(deps): update dependency google-auth to v2.6.3 (#211) - chore(deps): update dependency google-auth to v2.6.4 (#215) - chore(deps): update dependency google-auth to v2.6.5 (#217) - chore(python): add nox session to sort python imports (#218) - chore(deps): update dependency google-auth to v2.6.6 (#221) - chore(deps): update dependency pytest to v7.1.2 (#222) - chore(deps): update dependency cryptography to v37 (#223) - chore(deps): update dependency cryptography to v37.0.1 (#225) - chore(deps): update dependency cryptography to v37.0.2 (#226) - chore(deps): update dependency google-cloud-private-ca to v1.3.1 (#230) - fix: require python 3.7+ (#267) - chore(deps): update all dependencies (#262) - chore(deps): update all dependencies (#273) - chore(deps): update all dependencies (#275) - chore(deps): update all dependencies (#276) - chore(deps): update dependency google-cloud-monitoring to v2.11.1 (#281) - chore(deps): update all dependencies (#283) - chore(deps): update dependency google-auth to v2.11.0 (#284) - chore(deps): update dependency pytest to v7.1.3 (#294) - chore: Bump gapic-generator-python version to 1.3.0 (#295) - chore: detect samples tests in nested directories (#299) - chore(deps): update dependency google-auth to v2.11.1 (#300) - chore(deps): update dependency google-auth to v2.12.0 (#302) - chore(deps): update all dependencies (#304) - chore(deps): update all dependencies (#307) - chore(deps): update dependency pytest to v7.2.0 (#308) - chore(deps): update dependency google-auth to v2.14.0 (#310) - chore(python): drop flake8-import-order in samples noxfile (#315) - chore(deps): update all dependencies (#311) - chore(deps): update dependency cryptography to v38.0.4 (#317) - chore(deps): update dependency google-auth to v2.15.0 (#318) - chore(deps): update all dependencies (#321) - chore(deps): update dependency google-cloud-private-ca to v1.5.0 (#322) - chore(deps): update dependency cryptography to v39 (#323) - chore(python): add support for python 3.11 (#324) - chore(deps): update dependency google-cloud-monitoring to v2.13.0 (#325) - chore(deps): update dependency google-auth to v2.16.0 (#326) - chore(deps): update all dependencies (#329) - chore(deps): update dependency pytest to v7.2.1 (#330) ## Description Sample Migration from googleapis/python-security-private-ca Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google. ## Checklist - [ ] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md) - [ ] README is updated to include [all relevant information](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#readme-file) - [ ] **Tests** pass: `nox -s py-3.9` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [ ] Please **merge** this PR for me once it is approved. - [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/CODEOWNERS) with the codeowners for this sample
1 parent 0013c10 commit 7116d06

32 files changed

+2196
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# [START privateca_activate_subordinateca]
18+
import google.cloud.security.privateca_v1 as privateca_v1
19+
20+
21+
def activate_subordinate_ca(
22+
project_id: str,
23+
location: str,
24+
ca_pool_name: str,
25+
subordinate_ca_name: str,
26+
pem_ca_certificate: str,
27+
ca_name: str,
28+
) -> None:
29+
"""
30+
Activate a subordinate Certificate Authority (CA).
31+
*Prerequisite*: Get the Certificate Signing Resource (CSR) of the subordinate CA signed by another CA. Pass in the signed
32+
certificate and (issuer CA's name or the issuer CA's Certificate chain).
33+
*Post*: After activating the subordinate CA, it should be enabled before issuing certificates.
34+
Args:
35+
project_id: project ID or project number of the Cloud project you want to use.
36+
location: location you want to use. For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
37+
ca_pool_name: set it to the CA Pool under which the CA should be created.
38+
pem_ca_certificate: the signed certificate, obtained by signing the CSR.
39+
subordinate_ca_name: the CA to be activated.
40+
ca_name: The name of the certificate authority which signed the CSR.
41+
If an external CA (CA not present in Google Cloud) was used for signing,
42+
then use the CA's issuerCertificateChain.
43+
"""
44+
45+
ca_service_client = privateca_v1.CertificateAuthorityServiceClient()
46+
47+
subordinate_ca_path = ca_service_client.certificate_authority_path(
48+
project_id, location, ca_pool_name, subordinate_ca_name
49+
)
50+
ca_path = ca_service_client.certificate_authority_path(
51+
project_id, location, ca_pool_name, ca_name
52+
)
53+
54+
# Set CA subordinate config.
55+
subordinate_config = privateca_v1.SubordinateConfig(
56+
# Follow one of the below methods:
57+
# Method 1: If issuer CA is in Google Cloud, set the Certificate Authority Name.
58+
certificate_authority=ca_path,
59+
# Method 2: If issuer CA is external to Google Cloud, set the issuer's certificate chain.
60+
# The certificate chain of the CA (which signed the CSR) from leaf to root.
61+
# pem_issuer_chain=privateca_v1.SubordinateConfig.SubordinateConfigChain(
62+
# pem_certificates=issuer_certificate_chain,
63+
# )
64+
)
65+
66+
# Construct the "Activate CA Request".
67+
request = privateca_v1.ActivateCertificateAuthorityRequest(
68+
name=subordinate_ca_path,
69+
# The signed certificate.
70+
pem_ca_certificate=pem_ca_certificate,
71+
subordinate_config=subordinate_config,
72+
)
73+
74+
# Activate the CA
75+
operation = ca_service_client.activate_certificate_authority(request=request)
76+
result = operation.result()
77+
78+
print("Operation result:", result)
79+
80+
# The current state will be STAGED.
81+
# The Subordinate CA has to be ENABLED before issuing certificates.
82+
print(
83+
f"Current state: {ca_service_client.get_certificate_authority(name=subordinate_ca_path).state}"
84+
)
85+
86+
87+
# [END privateca_activate_subordinateca]

privateca/snippets/conftest.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright 2021 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+
# http://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+
import uuid
16+
17+
import google.auth
18+
import pytest
19+
20+
from create_ca_pool import create_ca_pool
21+
from create_certificate_authority import create_certificate_authority
22+
from create_certificate_template import create_certificate_template
23+
from delete_ca_pool import delete_ca_pool
24+
from delete_certificate_authority import delete_certificate_authority
25+
from delete_certificate_template import delete_certificate_template
26+
27+
PROJECT = google.auth.default()[1]
28+
LOCATION = "us-central1"
29+
COMMON_NAME = "COMMON_NAME"
30+
ORGANIZATION = "ORGANIZATION"
31+
CA_DURATION = 1000000
32+
33+
34+
def generate_name() -> str:
35+
return "test-" + uuid.uuid4().hex[:10]
36+
37+
38+
@pytest.fixture
39+
def ca_pool():
40+
CA_POOL_NAME = generate_name()
41+
42+
create_ca_pool(PROJECT, LOCATION, CA_POOL_NAME)
43+
44+
yield CA_POOL_NAME
45+
46+
delete_ca_pool(PROJECT, LOCATION, CA_POOL_NAME)
47+
48+
49+
@pytest.fixture
50+
def certificate_authority(ca_pool):
51+
CA_NAME = generate_name()
52+
53+
create_certificate_authority(
54+
PROJECT, LOCATION, ca_pool, CA_NAME, COMMON_NAME, ORGANIZATION, CA_DURATION
55+
)
56+
57+
yield ca_pool, CA_NAME
58+
59+
delete_certificate_authority(PROJECT, LOCATION, ca_pool, CA_NAME)
60+
61+
62+
@pytest.fixture
63+
def deleted_certificate_authority(ca_pool):
64+
CA_NAME = generate_name()
65+
66+
create_certificate_authority(
67+
PROJECT, LOCATION, ca_pool, CA_NAME, COMMON_NAME, ORGANIZATION, CA_DURATION
68+
)
69+
70+
delete_certificate_authority(PROJECT, LOCATION, ca_pool, CA_NAME)
71+
72+
yield ca_pool, CA_NAME
73+
74+
75+
@pytest.fixture
76+
def certificate_template():
77+
TEMPLATE_NAME = generate_name()
78+
79+
create_certificate_template(PROJECT, LOCATION, TEMPLATE_NAME)
80+
81+
yield TEMPLATE_NAME
82+
83+
delete_certificate_template(PROJECT, LOCATION, TEMPLATE_NAME)

privateca/snippets/create_ca_pool.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# [START privateca_create_ca_pool]
18+
import google.cloud.security.privateca_v1 as privateca_v1
19+
20+
21+
def create_ca_pool(project_id: str, location: str, ca_pool_name: str) -> None:
22+
"""
23+
Create a Certificate Authority pool. All certificates created under this CA pool will
24+
follow the same issuance policy, IAM policies,etc.,
25+
26+
Args:
27+
project_id: project ID or project number of the Cloud project you want to use.
28+
location: location you want to use. For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
29+
ca_pool_name: a unique name for the ca pool.
30+
"""
31+
32+
caServiceClient = privateca_v1.CertificateAuthorityServiceClient()
33+
34+
ca_pool = privateca_v1.CaPool(
35+
# Set the tier (see: https://cloud.google.com/certificate-authority-service/docs/tiers).
36+
tier=privateca_v1.CaPool.Tier.ENTERPRISE,
37+
)
38+
location_path = caServiceClient.common_location_path(project_id, location)
39+
40+
# Create the pool request.
41+
request = privateca_v1.CreateCaPoolRequest(
42+
parent=location_path,
43+
ca_pool_id=ca_pool_name,
44+
ca_pool=ca_pool,
45+
)
46+
47+
# Create the CA pool.
48+
operation = caServiceClient.create_ca_pool(request=request)
49+
50+
print("Operation result:", operation.result())
51+
52+
53+
# [END privateca_create_ca_pool]
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# [START privateca_create_certificate]
18+
import google.cloud.security.privateca_v1 as privateca_v1
19+
from google.protobuf import duration_pb2
20+
21+
22+
def create_certificate(
23+
project_id: str,
24+
location: str,
25+
ca_pool_name: str,
26+
ca_name: str,
27+
certificate_name: str,
28+
common_name: str,
29+
domain_name: str,
30+
certificate_lifetime: int,
31+
public_key_bytes: bytes,
32+
) -> None:
33+
"""
34+
Create a Certificate which is issued by the Certificate Authority present in the CA Pool.
35+
The key used to sign the certificate is created by the Cloud KMS.
36+
37+
Args:
38+
project_id: project ID or project number of the Cloud project you want to use.
39+
location: location you want to use. For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
40+
ca_pool_name: set a unique name for the CA pool.
41+
ca_name: the name of the certificate authority which issues the certificate.
42+
certificate_name: set a unique name for the certificate.
43+
common_name: a title for your certificate.
44+
domain_name: fully qualified domain name for your certificate.
45+
certificate_lifetime: the validity of the certificate in seconds.
46+
public_key_bytes: public key used in signing the certificates.
47+
"""
48+
49+
caServiceClient = privateca_v1.CertificateAuthorityServiceClient()
50+
51+
# The public key used to sign the certificate can be generated using any crypto library/framework.
52+
# Also you can use Cloud KMS to retrieve an already created public key.
53+
# For more info, see: https://cloud.google.com/kms/docs/retrieve-public-key.
54+
55+
# Set the Public Key and its format.
56+
public_key = privateca_v1.PublicKey(
57+
key=public_key_bytes,
58+
format_=privateca_v1.PublicKey.KeyFormat.PEM,
59+
)
60+
61+
subject_config = privateca_v1.CertificateConfig.SubjectConfig(
62+
subject=privateca_v1.Subject(common_name=common_name),
63+
subject_alt_name=privateca_v1.SubjectAltNames(dns_names=[domain_name]),
64+
)
65+
66+
# Set the X.509 fields required for the certificate.
67+
x509_parameters = privateca_v1.X509Parameters(
68+
key_usage=privateca_v1.KeyUsage(
69+
base_key_usage=privateca_v1.KeyUsage.KeyUsageOptions(
70+
digital_signature=True,
71+
key_encipherment=True,
72+
),
73+
extended_key_usage=privateca_v1.KeyUsage.ExtendedKeyUsageOptions(
74+
server_auth=True,
75+
client_auth=True,
76+
),
77+
),
78+
)
79+
80+
# Create certificate.
81+
certificate = privateca_v1.Certificate(
82+
config=privateca_v1.CertificateConfig(
83+
public_key=public_key,
84+
subject_config=subject_config,
85+
x509_config=x509_parameters,
86+
),
87+
lifetime=duration_pb2.Duration(seconds=certificate_lifetime),
88+
)
89+
90+
# Create the Certificate Request.
91+
request = privateca_v1.CreateCertificateRequest(
92+
parent=caServiceClient.ca_pool_path(project_id, location, ca_pool_name),
93+
certificate_id=certificate_name,
94+
certificate=certificate,
95+
issuing_certificate_authority_id=ca_name,
96+
)
97+
result = caServiceClient.create_certificate(request=request)
98+
99+
print("Certificate creation result:", result)
100+
101+
102+
# [END privateca_create_certificate]

0 commit comments

Comments
 (0)