-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathmonitor_certificate_authority.py
86 lines (72 loc) · 3.21 KB
/
monitor_certificate_authority.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/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": "[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]
return policy.name
def delete_ca_monitor_policy(policy_name: str) -> None:
"""Deletes a named policy in the project
Args:
policy_name: fully qualified name of a policy
"""
alert_policy_client = monitoring_v3.AlertPolicyServiceClient()
alert_policy_client.delete_alert_policy(name=policy_name)