Skip to content

Commit 453f966

Browse files
docs(samples): add sample to filter certificates (#160)
docs(samples): add sample to undelete certificate authority * samples(security): add filter/undelete certs * add fixture for a deleted CA Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent a14b962 commit 453f966

5 files changed

+148
-9
lines changed

privateca/snippets/conftest.py

+13
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,16 @@ def certificate_authority(ca_pool):
5656
yield ca_pool, CA_NAME
5757

5858
delete_certificate_authority(PROJECT, LOCATION, ca_pool, CA_NAME)
59+
60+
61+
@pytest.fixture
62+
def deleted_certificate_authority(ca_pool):
63+
CA_NAME = generate_name()
64+
65+
create_certificate_authority(
66+
PROJECT, LOCATION, ca_pool, CA_NAME, COMMON_NAME, ORGANIZATION, CA_DURATION
67+
)
68+
69+
delete_certificate_authority(PROJECT, LOCATION, ca_pool, CA_NAME)
70+
71+
yield ca_pool, CA_NAME
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_filter_certificate]
18+
import google.cloud.security.privateca_v1 as privateca_v1
19+
20+
21+
def filter_certificates(
22+
project_id: str, location: str, ca_pool_name: str, filter_condition: str
23+
) -> None:
24+
"""
25+
Filter certificates based on a condition and list them.
26+
27+
Args:
28+
project_id: project ID or project number of the Cloud project you want to use.
29+
location: location you want to use. For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
30+
ca_pool_name: name of the CA pool which contains the certificates to be listed.
31+
"""
32+
33+
caServiceClient = privateca_v1.CertificateAuthorityServiceClient()
34+
35+
ca_pool_path = caServiceClient.ca_pool_path(project_id, location, ca_pool_name)
36+
37+
# Create the certificate request and set the filter condition.
38+
request = privateca_v1.ListCertificatesRequest(
39+
parent=ca_pool_path, filter=filter_condition,
40+
)
41+
42+
# Retrieve and print the certificate names.
43+
print("Available certificates: ")
44+
for cert in caServiceClient.list_certificates(request=request):
45+
print(f"- {cert.name}")
46+
47+
48+
# [END privateca_filter_certificate]

privateca/snippets/test_certificate_authorities.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from delete_certificate_authority import delete_certificate_authority
2525
from disable_certificate_authority import disable_certificate_authority
2626
from enable_certificate_authority import enable_certificate_authority
27+
from undelete_certificate_authority import undelete_certificate_authority
2728

2829

2930
PROJECT = google.auth.default()[1]
@@ -71,17 +72,15 @@ def test_enable_and_disable_certificate_authority(
7172
assert re.search(f"Disabled Certificate Authority: {CA_NAME}", out,)
7273

7374

74-
def test_delete_certificate_authority(capsys: typing.Any) -> None:
75-
CA_POOL_NAME = generate_name()
76-
CA_NAME = generate_name()
75+
def test_undelete_certificate_authority(
76+
deleted_certificate_authority, capsys: typing.Any
77+
) -> None:
78+
CA_POOL_NAME, CA_NAME = deleted_certificate_authority
7779

78-
create_ca_pool(PROJECT, LOCATION, CA_POOL_NAME)
79-
create_certificate_authority(
80-
PROJECT, LOCATION, CA_POOL_NAME, CA_NAME, COMMON_NAME, ORGANIZATION, CA_DURATION
81-
)
80+
undelete_certificate_authority(PROJECT, LOCATION, CA_POOL_NAME, CA_NAME)
8281
delete_certificate_authority(PROJECT, LOCATION, CA_POOL_NAME, CA_NAME)
8382
delete_ca_pool(PROJECT, LOCATION, CA_POOL_NAME)
8483

8584
out, _ = capsys.readouterr()
86-
85+
assert re.search(f"Successfully undeleted Certificate Authority: {CA_NAME}", out,)
8786
assert re.search(f"Successfully deleted Certificate Authority: {CA_NAME}", out,)

privateca/snippets/test_certificates.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515

16+
import re
1617
import time
1718
import typing
1819
import uuid
@@ -28,6 +29,7 @@
2829
from create_certificate import create_certificate
2930
from disable_certificate_authority import disable_certificate_authority
3031
from enable_certificate_authority import enable_certificate_authority
32+
from filter_certificates import filter_certificates
3133
from revoke_certificate import revoke_certificate
3234

3335

@@ -74,13 +76,22 @@ def test_create_and_revoke_certificate_authority(
7476
public_key_bytes,
7577
)
7678

79+
FILTER_CONDITION = (
80+
f"certificate_description.subject_description.subject.common_name={COMMON_NAME}"
81+
)
82+
filter_certificates(PROJECT, LOCATION, CA_POOL_NAME, FILTER_CONDITION)
83+
7784
revoke_certificate(
7885
PROJECT, LOCATION, CA_POOL_NAME, CERT_NAME,
7986
)
8087

8188
disable_certificate_authority(PROJECT, LOCATION, CA_POOL_NAME, CA_NAME)
8289

8390
out, _ = capsys.readouterr()
84-
8591
assert "Certificate creation result:" in out
92+
assert "Available certificates:" in out
93+
assert re.search(
94+
f"- projects/.*/locations/{LOCATION}/caPools/{CA_POOL_NAME}/certificates/{CERT_NAME}",
95+
out,
96+
)
8697
assert "Certificate revoke result:" in out
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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_undelete_ca]
18+
import google.cloud.security.privateca_v1 as privateca_v1
19+
20+
21+
def undelete_certificate_authority(
22+
project_id: str, location: str, ca_pool_name: str, ca_name: str
23+
) -> None:
24+
"""
25+
Restore a deleted CA, if still within the grace period of 30 days.
26+
27+
Args:
28+
project_id: project ID or project number of the Cloud project you want to use.
29+
location: location you want to use. For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
30+
ca_pool_name: the name of the CA pool under which the deleted CA is present.
31+
ca_name: the name of the CA to be restored (undeleted).
32+
"""
33+
34+
caServiceClient = privateca_v1.CertificateAuthorityServiceClient()
35+
ca_path = caServiceClient.certificate_authority_path(
36+
project_id, location, ca_pool_name, ca_name
37+
)
38+
39+
# Confirm if the CA is in DELETED stage.
40+
ca_state = caServiceClient.get_certificate_authority(name=ca_path).state
41+
if ca_state != privateca_v1.CertificateAuthority.State.DELETED:
42+
print("CA is not deleted !")
43+
return
44+
45+
# Create the Request.
46+
request = privateca_v1.UndeleteCertificateAuthorityRequest(name=ca_path)
47+
48+
# Undelete the CA.
49+
operation = caServiceClient.undelete_certificate_authority(request=request)
50+
result = operation.result()
51+
52+
print("Operation result", result)
53+
54+
# Get the current CA state.
55+
ca_state = caServiceClient.get_certificate_authority(name=ca_path).state
56+
57+
# CA state changes from DELETED to DISABLED if successfully restored.
58+
# Confirm if the CA is DISABLED.
59+
if ca_state == privateca_v1.CertificateAuthority.State.DISABLED:
60+
print("Successfully undeleted Certificate Authority:", ca_name)
61+
else:
62+
print(
63+
"Unable to restore the Certificate Authority! Please try again! Current state:",
64+
ca_state,
65+
)
66+
67+
68+
# [END privateca_undelete_ca]

0 commit comments

Comments
 (0)