Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate code from googleapis/python-security-private-ca #9028

Merged
merged 91 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
b2b59f0
feat(samples): private CA python samples
FrodoTheTrue Jul 22, 2021
2d481cd
chore(deps): update dependency google-cloud-private-ca to v1 (#89)
renovate-bot Jul 28, 2021
c166568
chore(deps): update dependency google-cloud-kms to v2.4.1 (#88)
renovate-bot Jul 28, 2021
906d79e
chore(deps): update dependency google-cloud-kms to v2.4.2 (#93)
renovate-bot Jul 28, 2021
33fc434
chore(deps): update dependency google-auth to v1.33.1 (#87)
renovate-bot Jul 28, 2021
caf54ab
chore(deps): update dependency google-auth to v1.34.0 (#94)
renovate-bot Jul 29, 2021
0a9fd1b
chore(deps): update dependency google-cloud-kms to v2.4.3 (#96)
renovate-bot Jul 29, 2021
9033d16
chore(deps): update dependency google-cloud-private-ca to v1.0.3 (#97)
renovate-bot Jul 29, 2021
d6f48ab
feat(samples): add local generation for crypto keys (#98)
FrodoTheTrue Aug 3, 2021
15b1db7
chore(deps): update dependency google-cloud-kms to v2.5.0 (#101)
renovate-bot Aug 10, 2021
fcb65e3
chore: generate python samples templates in owlbot.py (#108)
busunkim96 Aug 18, 2021
695dcf7
chore(deps): update dependency cryptography to v3.4.8 (#109)
renovate-bot Aug 25, 2021
3a52c1a
chore(deps): update dependency google-auth to v2 (#107)
renovate-bot Aug 30, 2021
6334c61
chore(deps): update dependency google-cloud-private-ca to v1.0.4 (#103)
renovate-bot Aug 30, 2021
245e7bd
chore(deps): update dependency google-auth to v2.0.2 (#116)
renovate-bot Sep 1, 2021
c8acb2d
chore(deps): update dependency google-cloud-kms to v2.6.0 (#115)
renovate-bot Sep 1, 2021
6dcb975
chore(deps): update dependency pytest to v6.2.5 (#114)
renovate-bot Sep 1, 2021
c9e67d3
chore: blacken samples noxfile template (#121)
gcf-owl-bot[bot] Sep 16, 2021
7e2911b
chore: fail samples nox session if python version is missing (#128)
gcf-owl-bot[bot] Sep 30, 2021
8fe08dc
chore(python): Add kokoro configs for python 3.10 samples testing (#134)
gcf-owl-bot[bot] Oct 8, 2021
185d5d4
chore(deps): update dependency google-auth to v2.1.0 (#120)
renovate-bot Oct 27, 2021
1cc277d
chore(deps): update all dependencies (#144)
renovate-bot Oct 30, 2021
f671d4c
chore(deps): update all dependencies (#146)
renovate-bot Nov 2, 2021
ad4c1ff
chore(deps): update dependency google-cloud-private-ca to v1.2.1 (#147)
renovate-bot Nov 4, 2021
db9be66
chore(python): run blacken session for all directories with a noxfile…
gcf-owl-bot[bot] Nov 11, 2021
1ea16b7
chore(deps): update dependency cryptography to v36 (#154)
renovate-bot Nov 22, 2021
8dd8f69
feat(samples): add subordinate CA samples
FrodoTheTrue Nov 29, 2021
7fb6174
chore(deps): update dependency cryptography to v36.0.1 (#158)
renovate-bot Dec 24, 2021
a101e02
chore(samples): Add check for tests in directory (#164)
gcf-owl-bot[bot] Jan 11, 2022
a14b962
chore(python): Noxfile recognizes that tests can live in a folder (#169)
gcf-owl-bot[bot] Jan 19, 2022
453f966
docs(samples): add sample to filter certificates (#160)
FrodoTheTrue Jan 23, 2022
2b643ea
chore(deps): update dependency google-auth to v2.6.0 (#173)
renovate-bot Feb 3, 2022
a945636
docs(samples): add template/monitoring samples (#174)
FrodoTheTrue Feb 14, 2022
4b2d087
chore(deps): update all dependencies (#178)
renovate-bot Feb 26, 2022
8f0220b
chore(deps): update all dependencies (#186)
renovate-bot Mar 1, 2022
c6b7cf8
chore: Adding support for pytest-xdist and pytest-parallel (#193)
gcf-owl-bot[bot] Mar 4, 2022
4a37327
chore(deps): update all dependencies (#196)
renovate-bot Mar 7, 2022
c0c3a86
chore(deps): update dependency google-cloud-private-ca to v1.3.0 (#199)
renovate-bot Mar 12, 2022
698dbc1
chore(deps): update dependency pytest to v7.1.0 (#200)
renovate-bot Mar 13, 2022
7dfff56
chore(deps): update dependency cryptography to v36.0.2 (#201)
renovate-bot Mar 16, 2022
8f7813a
chore(deps): update all dependencies (#202)
renovate-bot Mar 19, 2022
42384bf
chore(python): use black==22.3.0 (#204)
gcf-owl-bot[bot] Mar 28, 2022
2f933f7
chore(deps): update dependency google-auth to v2.6.3 (#211)
renovate-bot Apr 7, 2022
81e9a54
chore(deps): update dependency google-auth to v2.6.4 (#215)
renovate-bot Apr 12, 2022
e259d72
chore(deps): update dependency google-auth to v2.6.5 (#217)
renovate-bot Apr 15, 2022
814c401
chore(python): add nox session to sort python imports (#218)
gcf-owl-bot[bot] Apr 21, 2022
09c0e7f
chore(deps): update dependency google-auth to v2.6.6 (#221)
renovate-bot Apr 22, 2022
6f53506
chore(deps): update dependency pytest to v7.1.2 (#222)
renovate-bot Apr 25, 2022
8a4c02e
chore(deps): update dependency cryptography to v37 (#223)
renovate-bot Apr 26, 2022
a14b4c2
chore(deps): update dependency cryptography to v37.0.1 (#225)
renovate-bot Apr 28, 2022
c3c2b6f
chore(deps): update dependency cryptography to v37.0.2 (#226)
renovate-bot May 5, 2022
b84346f
chore(deps): update dependency google-cloud-private-ca to v1.3.1 (#230)
renovate-bot May 19, 2022
d66afb1
fix: require python 3.7+ (#267)
gcf-owl-bot[bot] Jul 10, 2022
ea49721
chore(deps): update all dependencies (#262)
renovate-bot Jul 19, 2022
8c2e336
chore(deps): update all dependencies (#273)
renovate-bot Aug 2, 2022
343285b
chore(deps): update all dependencies (#275)
renovate-bot Aug 6, 2022
f5b4141
chore(deps): update all dependencies (#276)
renovate-bot Aug 9, 2022
f06041a
chore(deps): update dependency google-cloud-monitoring to v2.11.1 (#281)
renovate-bot Aug 17, 2022
77050aa
chore(deps): update all dependencies (#283)
renovate-bot Aug 19, 2022
388e65d
chore(deps): update dependency google-auth to v2.11.0 (#284)
renovate-bot Aug 23, 2022
fc2d7f8
chore(deps): update dependency pytest to v7.1.3 (#294)
renovate-bot Sep 6, 2022
4db0646
chore: Bump gapic-generator-python version to 1.3.0 (#295)
gcf-owl-bot[bot] Sep 7, 2022
08de6b7
chore: detect samples tests in nested directories (#299)
gcf-owl-bot[bot] Sep 13, 2022
052fd77
chore(deps): update dependency google-auth to v2.11.1 (#300)
renovate-bot Sep 20, 2022
6eb7bf5
chore(deps): update dependency google-auth to v2.12.0 (#302)
renovate-bot Oct 4, 2022
82f974c
chore(deps): update all dependencies (#304)
renovate-bot Oct 6, 2022
fa75df0
chore(deps): update all dependencies (#307)
renovate-bot Oct 18, 2022
5462d82
chore(deps): update dependency pytest to v7.2.0 (#308)
renovate-bot Oct 26, 2022
c78daf2
chore(deps): update dependency google-auth to v2.14.0 (#310)
renovate-bot Nov 1, 2022
10a3d35
chore(python): drop flake8-import-order in samples noxfile (#315)
gcf-owl-bot[bot] Nov 26, 2022
8002cc8
chore(deps): update all dependencies (#311)
renovate-bot Nov 26, 2022
063f976
chore(deps): update dependency cryptography to v38.0.4 (#317)
renovate-bot Nov 28, 2022
6c4d347
chore(deps): update dependency google-auth to v2.15.0 (#318)
renovate-bot Dec 15, 2022
09d35f7
chore(deps): update all dependencies (#321)
renovate-bot Dec 15, 2022
cf3dab9
chore(deps): update dependency google-cloud-private-ca to v1.5.0 (#322)
renovate-bot Dec 16, 2022
ab905cf
chore(deps): update dependency cryptography to v39 (#323)
renovate-bot Jan 4, 2023
5d1909c
chore(python): add support for python 3.11 (#324)
gcf-owl-bot[bot] Jan 6, 2023
9c1fc09
chore(deps): update dependency google-cloud-monitoring to v2.13.0 (#325)
renovate-bot Jan 9, 2023
1a0a4e2
chore(deps): update dependency google-auth to v2.16.0 (#326)
renovate-bot Jan 9, 2023
b8d644e
chore(deps): update all dependencies (#329)
renovate-bot Jan 11, 2023
e954605
chore(deps): update dependency pytest to v7.2.1 (#330)
renovate-bot Jan 14, 2023
0543acd
Merge remote-tracking branch 'migration/main' into python-security-pr…
rsamborski Jan 20, 2023
48249e4
Merge branch 'main' into python-security-private-ca-migration
kweinmeister Jan 20, 2023
e33f113
dee-migration: test fixes
rsamborski Jan 23, 2023
dce1074
dee-migration: add backoff to mitigate quota issue
rsamborski Jan 23, 2023
30c8db9
dee-migration: fixing tests
rsamborski Jan 23, 2023
2c77b23
Merge branch 'main' into python-security-private-ca-migration
rsamborski Jan 23, 2023
42c5884
Merge branch 'main' into python-security-private-ca-migration
nicain Jan 23, 2023
63473d9
Merge branch 'main' into python-security-private-ca-migration
rsamborski Jan 24, 2023
2d250c1
Merge branch 'main' into python-security-private-ca-migration
rsamborski Jan 25, 2023
379a957
Merge branch 'main' into python-security-private-ca-migration
kweinmeister Jan 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions privateca/snippets/activate_subordinate_ca.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START privateca_activate_subordinateca]
import google.cloud.security.privateca_v1 as privateca_v1


def activate_subordinate_ca(
project_id: str,
location: str,
ca_pool_name: str,
subordinate_ca_name: str,
pem_ca_certificate: str,
ca_name: str,
) -> None:
"""
Activate a subordinate Certificate Authority (CA).
*Prerequisite*: Get the Certificate Signing Resource (CSR) of the subordinate CA signed by another CA. Pass in the signed
certificate and (issuer CA's name or the issuer CA's Certificate chain).
*Post*: After activating the subordinate CA, it should be enabled before issuing certificates.
Args:
project_id: project ID or project number of the Cloud project you want to use.
location: location you want to use. For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
ca_pool_name: set it to the CA Pool under which the CA should be created.
pem_ca_certificate: the signed certificate, obtained by signing the CSR.
subordinate_ca_name: the CA to be activated.
ca_name: The name of the certificate authority which signed the CSR.
If an external CA (CA not present in Google Cloud) was used for signing,
then use the CA's issuerCertificateChain.
"""

ca_service_client = privateca_v1.CertificateAuthorityServiceClient()

subordinate_ca_path = ca_service_client.certificate_authority_path(
project_id, location, ca_pool_name, subordinate_ca_name
)
ca_path = ca_service_client.certificate_authority_path(
project_id, location, ca_pool_name, ca_name
)

# Set CA subordinate config.
subordinate_config = privateca_v1.SubordinateConfig(
# Follow one of the below methods:
# Method 1: If issuer CA is in Google Cloud, set the Certificate Authority Name.
certificate_authority=ca_path,
# Method 2: If issuer CA is external to Google Cloud, set the issuer's certificate chain.
# The certificate chain of the CA (which signed the CSR) from leaf to root.
# pem_issuer_chain=privateca_v1.SubordinateConfig.SubordinateConfigChain(
# pem_certificates=issuer_certificate_chain,
# )
)

# Construct the "Activate CA Request".
request = privateca_v1.ActivateCertificateAuthorityRequest(
name=subordinate_ca_path,
# The signed certificate.
pem_ca_certificate=pem_ca_certificate,
subordinate_config=subordinate_config,
)

# Activate the CA
operation = ca_service_client.activate_certificate_authority(request=request)
result = operation.result()

print("Operation result:", result)

# The current state will be STAGED.
# The Subordinate CA has to be ENABLED before issuing certificates.
print(
f"Current state: {ca_service_client.get_certificate_authority(name=subordinate_ca_path).state}"
)


# [END privateca_activate_subordinateca]
83 changes: 83 additions & 0 deletions privateca/snippets/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import uuid

import google.auth
import pytest

from create_ca_pool import create_ca_pool
from create_certificate_authority import create_certificate_authority
from create_certificate_template import create_certificate_template
from delete_ca_pool import delete_ca_pool
from delete_certificate_authority import delete_certificate_authority
from delete_certificate_template import delete_certificate_template

PROJECT = google.auth.default()[1]
LOCATION = "us-central1"
COMMON_NAME = "COMMON_NAME"
ORGANIZATION = "ORGANIZATION"
CA_DURATION = 1000000


def generate_name() -> str:
return "test-" + uuid.uuid4().hex[:10]


@pytest.fixture
def ca_pool():
CA_POOL_NAME = generate_name()

create_ca_pool(PROJECT, LOCATION, CA_POOL_NAME)

yield CA_POOL_NAME

delete_ca_pool(PROJECT, LOCATION, CA_POOL_NAME)


@pytest.fixture
def certificate_authority(ca_pool):
CA_NAME = generate_name()

create_certificate_authority(
PROJECT, LOCATION, ca_pool, CA_NAME, COMMON_NAME, ORGANIZATION, CA_DURATION
)

yield ca_pool, CA_NAME

delete_certificate_authority(PROJECT, LOCATION, ca_pool, CA_NAME)


@pytest.fixture
def deleted_certificate_authority(ca_pool):
CA_NAME = generate_name()

create_certificate_authority(
PROJECT, LOCATION, ca_pool, CA_NAME, COMMON_NAME, ORGANIZATION, CA_DURATION
)

delete_certificate_authority(PROJECT, LOCATION, ca_pool, CA_NAME)

yield ca_pool, CA_NAME


@pytest.fixture
def certificate_template():
TEMPLATE_NAME = generate_name()

create_certificate_template(PROJECT, LOCATION, TEMPLATE_NAME)

yield TEMPLATE_NAME

delete_certificate_template(PROJECT, LOCATION, TEMPLATE_NAME)
53 changes: 53 additions & 0 deletions privateca/snippets/create_ca_pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START privateca_create_ca_pool]
import google.cloud.security.privateca_v1 as privateca_v1


def create_ca_pool(project_id: str, location: str, ca_pool_name: str) -> None:
"""
Create a Certificate Authority pool. All certificates created under this CA pool will
follow the same issuance policy, IAM policies,etc.,

Args:
project_id: project ID or project number of the Cloud project you want to use.
location: location you want to use. For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
ca_pool_name: a unique name for the ca pool.
"""

caServiceClient = privateca_v1.CertificateAuthorityServiceClient()

ca_pool = privateca_v1.CaPool(
# Set the tier (see: https://cloud.google.com/certificate-authority-service/docs/tiers).
tier=privateca_v1.CaPool.Tier.ENTERPRISE,
)
location_path = caServiceClient.common_location_path(project_id, location)

# Create the pool request.
request = privateca_v1.CreateCaPoolRequest(
parent=location_path,
ca_pool_id=ca_pool_name,
ca_pool=ca_pool,
)

# Create the CA pool.
operation = caServiceClient.create_ca_pool(request=request)

print("Operation result:", operation.result())


# [END privateca_create_ca_pool]
102 changes: 102 additions & 0 deletions privateca/snippets/create_certificate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env python

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START privateca_create_certificate]
import google.cloud.security.privateca_v1 as privateca_v1
from google.protobuf import duration_pb2


def create_certificate(
project_id: str,
location: str,
ca_pool_name: str,
ca_name: str,
certificate_name: str,
common_name: str,
domain_name: str,
certificate_lifetime: int,
public_key_bytes: bytes,
) -> None:
"""
Create a Certificate which is issued by the Certificate Authority present in the CA Pool.
The key used to sign the certificate is created by the Cloud KMS.

Args:
project_id: project ID or project number of the Cloud project you want to use.
location: location you want to use. For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
ca_pool_name: set a unique name for the CA pool.
ca_name: the name of the certificate authority which issues the certificate.
certificate_name: set a unique name for the certificate.
common_name: a title for your certificate.
domain_name: fully qualified domain name for your certificate.
certificate_lifetime: the validity of the certificate in seconds.
public_key_bytes: public key used in signing the certificates.
"""

caServiceClient = privateca_v1.CertificateAuthorityServiceClient()

# The public key used to sign the certificate can be generated using any crypto library/framework.
# Also you can use Cloud KMS to retrieve an already created public key.
# For more info, see: https://cloud.google.com/kms/docs/retrieve-public-key.

# Set the Public Key and its format.
public_key = privateca_v1.PublicKey(
key=public_key_bytes,
format_=privateca_v1.PublicKey.KeyFormat.PEM,
)

subject_config = privateca_v1.CertificateConfig.SubjectConfig(
subject=privateca_v1.Subject(common_name=common_name),
subject_alt_name=privateca_v1.SubjectAltNames(dns_names=[domain_name]),
)

# Set the X.509 fields required for the certificate.
x509_parameters = privateca_v1.X509Parameters(
key_usage=privateca_v1.KeyUsage(
base_key_usage=privateca_v1.KeyUsage.KeyUsageOptions(
digital_signature=True,
key_encipherment=True,
),
extended_key_usage=privateca_v1.KeyUsage.ExtendedKeyUsageOptions(
server_auth=True,
client_auth=True,
),
),
)

# Create certificate.
certificate = privateca_v1.Certificate(
config=privateca_v1.CertificateConfig(
public_key=public_key,
subject_config=subject_config,
x509_config=x509_parameters,
),
lifetime=duration_pb2.Duration(seconds=certificate_lifetime),
)

# Create the Certificate Request.
request = privateca_v1.CreateCertificateRequest(
parent=caServiceClient.ca_pool_path(project_id, location, ca_pool_name),
certificate_id=certificate_name,
certificate=certificate,
issuing_certificate_authority_id=ca_name,
)
result = caServiceClient.create_certificate(request=request)

print("Certificate creation result:", result)


# [END privateca_create_certificate]
Loading