-
Notifications
You must be signed in to change notification settings - Fork 12
docs(samples): add template/monitoring samples #174
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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] | ||
| 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. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC, project ID and project number are different entities. Does this function accept either or these? If not, please specify the correct one, project ID OR project number. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great catch - they are different entities
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have double checked with a To be on the safe side we should test this sample with ID and number as well.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed that this is a bit strange behavior |
||
| 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] | ||
| 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. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC, project ID and project number are different entities. Does this function accept either or these? If not, please specify the correct one, project ID OR project number. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great catch - they are different entities |
||
| 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), | ||
| ) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't we supposed to print an error message, in case of an error?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess here we have either exception itself or printed list of templates |
||
| print("Available certificate templates:") | ||
| for certificate_template in caServiceClient.list_certificate_templates( | ||
| request=request | ||
| ): | ||
| print(certificate_template.name) | ||
|
|
||
|
|
||
| # [END privateca_list_certificate_template] | ||
| 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. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC, project ID and project number are different entities. Does this function accept either or these? If not, please specify the correct one, project ID OR project number. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great catch - they are different entities
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For python-compute we use the same description: https://github.com/googleapis/python-compute/blob/main/samples/snippets/sample_create_vm.py#L167 Here we accept both entities as an input parameter: project ID and project number
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @leahecole @savijatv WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think that they're different. The Project ID starts with a letter whereas Project number is all numeric. See https://cloud.google.com/resource-manager/docs/creating-managing-projects.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, have just checked, the code accepts both |
||
| """ | ||
|
|
||
| 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": "[email protected]"}, | ||
| ) | ||
| 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] | ||
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, project ID and project number are different entities. Does this function accept either or these? If not, please specify the correct one, project ID OR project number.