-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): add template/monitoring samples (#174)
- Loading branch information
1 parent
b07eebb
commit 6b83ff4
Showing
12 changed files
with
605 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 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_template] | ||
import google.cloud.security.privateca_v1 as privateca_v1 | ||
from google.type import expr_pb2 | ||
|
||
|
||
def create_certificate_template( | ||
project_id: str, location: str, certificate_template_id: str, | ||
) -> None: | ||
""" | ||
Create a Certificate template. These templates can be reused for common | ||
certificate issuance scenarios. | ||
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. | ||
certificate_template_id: set a unique name for the certificate template. | ||
""" | ||
|
||
caServiceClient = privateca_v1.CertificateAuthorityServiceClient() | ||
|
||
# Describes any predefined X.509 values set by this template. | ||
# The provided extensions are copied over to certificate requests that use this template. | ||
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, | ||
), | ||
), | ||
ca_options=privateca_v1.X509Parameters.CaOptions(is_ca=False,), | ||
) | ||
|
||
# CEL expression that is evaluated against the Subject and | ||
# Subject Alternative Name of the certificate before it is issued. | ||
expr = expr_pb2.Expr(expression="subject_alt_names.all(san, san.type == DNS)") | ||
|
||
# Set the certificate issuance schema. | ||
certificate_template = privateca_v1.CertificateTemplate( | ||
predefined_values=x509_parameters, | ||
identity_constraints=privateca_v1.CertificateIdentityConstraints( | ||
cel_expression=expr, | ||
allow_subject_passthrough=False, | ||
allow_subject_alt_names_passthrough=False, | ||
), | ||
) | ||
|
||
# Request to create a certificate template. | ||
request = privateca_v1.CreateCertificateTemplateRequest( | ||
parent=caServiceClient.common_location_path(project_id, location), | ||
certificate_template=certificate_template, | ||
certificate_template_id=certificate_template_id, | ||
) | ||
operation = caServiceClient.create_certificate_template(request=request) | ||
result = operation.result() | ||
|
||
print("Operation result:", result) | ||
|
||
|
||
# [END privateca_create_certificate_template] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 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_delete_certificate_template] | ||
import google.cloud.security.privateca_v1 as privateca_v1 | ||
|
||
|
||
def delete_certificate_template( | ||
project_id: str, location: str, certificate_template_id: str, | ||
) -> None: | ||
""" | ||
Delete the certificate template present in the given project and location. | ||
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. | ||
certificate_template_id: set a unique name for the certificate template. | ||
""" | ||
|
||
caServiceClient = privateca_v1.CertificateAuthorityServiceClient() | ||
|
||
# Request to delete a certificate template. | ||
request = privateca_v1.DeleteCertificateTemplateRequest( | ||
name=caServiceClient.certificate_template_path( | ||
project_id, location, certificate_template_id, | ||
) | ||
) | ||
operation = caServiceClient.delete_certificate_template(request=request) | ||
result = operation.result() | ||
|
||
print("Operation result", result) | ||
print("Deleted certificate template:", certificate_template_id) | ||
|
||
|
||
# [END privateca_delete_certificate_template] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 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_list_certificate_template] | ||
import google.cloud.security.privateca_v1 as privateca_v1 | ||
|
||
|
||
def list_certificate_templates(project_id: str, location: str) -> None: | ||
""" | ||
List the certificate templates present in the given project and location. | ||
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. | ||
""" | ||
|
||
caServiceClient = privateca_v1.CertificateAuthorityServiceClient() | ||
|
||
# List Templates Request. | ||
request = privateca_v1.ListCertificateTemplatesRequest( | ||
parent=caServiceClient.common_location_path(project_id, location), | ||
) | ||
|
||
print("Available certificate templates:") | ||
for certificate_template in caServiceClient.list_certificate_templates( | ||
request=request | ||
): | ||
print(certificate_template.name) | ||
|
||
|
||
# [END privateca_list_certificate_template] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 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_monitor_ca_expiry] | ||
import google.cloud.monitoring_v3 as monitoring_v3 | ||
|
||
|
||
def create_ca_monitor_policy(project_id: str) -> None: | ||
""" | ||
Create a monitoring policy that notifies you 30 days before a managed CA expires. | ||
Args: | ||
project_id: project ID or project number of the Cloud project you want to use. | ||
""" | ||
|
||
alertPolicyServiceClient = monitoring_v3.AlertPolicyServiceClient() | ||
notificationChannelServiceClient = monitoring_v3.NotificationChannelServiceClient() | ||
|
||
# Query which indicates the resource to monitor and the constraints. | ||
# Here, the alert policy notifies you 30 days before a managed CA expires. | ||
# For more information on creating queries, see: https://cloud.google.com/monitoring/mql/alerts | ||
query = ( | ||
"fetch privateca.googleapis.com/CertificateAuthority" | ||
"| metric 'privateca.googleapis.com/ca/cert_chain_expiration'" | ||
"| group_by 5m," | ||
"[value_cert_chain_expiration_mean: mean(value.cert_chain_expiration)]" | ||
"| every 5m" | ||
"| condition val() < 2.592e+06 's'" | ||
) | ||
|
||
# Create a notification channel. | ||
notification_channel = monitoring_v3.NotificationChannel( | ||
type_="email", | ||
labels={"email_address": "python-docs-samples-testing@google.com"}, | ||
) | ||
channel = notificationChannelServiceClient.create_notification_channel( | ||
name=notificationChannelServiceClient.common_project_path(project_id), | ||
notification_channel=notification_channel, | ||
) | ||
|
||
# Set the query and notification channel. | ||
alert_policy = monitoring_v3.AlertPolicy( | ||
display_name="policy-name", | ||
conditions=[ | ||
monitoring_v3.AlertPolicy.Condition( | ||
display_name="ca-cert-chain-expiration", | ||
condition_monitoring_query_language=monitoring_v3.AlertPolicy.Condition.MonitoringQueryLanguageCondition( | ||
query=query, | ||
), | ||
) | ||
], | ||
combiner=monitoring_v3.AlertPolicy.ConditionCombinerType.AND, | ||
notification_channels=[channel.name], | ||
) | ||
|
||
policy = alertPolicyServiceClient.create_alert_policy( | ||
name=notificationChannelServiceClient.common_project_path(project_id), | ||
alert_policy=alert_policy, | ||
) | ||
|
||
print("Monitoring policy successfully created!", policy.name) | ||
|
||
|
||
# [END privateca_monitor_ca_expiry] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
google-cloud-private-ca==1.2.1 | ||
google-cloud-kms==2.10.1 | ||
google-cloud-monitoring==2.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.