Skip to content

Commit f9e6a58

Browse files
committed
Added initial version for key vault secrets expiry alerts.
1 parent db6eca7 commit f9e6a58

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Secret Nearing Expiration Alert for Key Vault
2+
resource "azurerm_monitor_metric_alert" "kv_secret_near_expiry" {
3+
count = var.enable_alerting == true ? 1 : 0
4+
5+
name = "${azurerm_key_vault.keyvault.name}-secret-near-expiry"
6+
resource_group_name = var.resource_group_name_monitoring != null ? var.resource_group_name_monitoring : var.resource_group_name
7+
scopes = [azurerm_key_vault.keyvault.id] # Point to your key vault
8+
description = "Action will be triggered when any Key Vault secret is nearing expiration."
9+
window_size = var.alert_window_size
10+
frequency = local.alert_frequency
11+
severity = 2 # Warning
12+
13+
criteria {
14+
metric_namespace = "Microsoft.KeyVault/vaults"
15+
metric_name = "Secret Nearing Expiration"
16+
aggregation = "Total"
17+
operator = "GreaterThan"
18+
threshold = var.alert_secret_expiry_threshold
19+
20+
dimension {
21+
name = "SecretName"
22+
operator = "Include"
23+
values = ["*"]
24+
}
25+
}
26+
27+
action {
28+
action_group_id = var.action_group_id
29+
}
30+
31+
lifecycle {
32+
ignore_changes = [
33+
tags
34+
]
35+
}
36+
}
37+
38+
# Secret Expired Alert for Key Vault
39+
resource "azurerm_monitor_metric_alert" "kv_secret_expired" {
40+
count = var.enable_alerting == true ? 1 : 0
41+
42+
name = "${azurerm_key_vault.keyvault.name}-secret-expired"
43+
resource_group_name = var.resource_group_name_monitoring != null ? var.resource_group_name_monitoring : var.resource_group_name
44+
scopes = [azurerm_key_vault.keyvault.id] # Point to your key vault
45+
description = "Action will be triggered when any Key Vault secret is nearing expiration."
46+
window_size = var.alert_window_size
47+
frequency = local.alert_frequency
48+
severity = 2 # Warning
49+
50+
criteria {
51+
metric_namespace = "Microsoft.KeyVault/vaults"
52+
metric_name = "Secret Expired"
53+
aggregation = "Total"
54+
operator = "GreaterThan"
55+
threshold = var.alert_secret_expiry_threshold
56+
57+
dimension {
58+
name = "SecretName"
59+
operator = "Include"
60+
values = ["*"]
61+
}
62+
}
63+
64+
action {
65+
action_group_id = var.action_group_id
66+
}
67+
68+
lifecycle {
69+
ignore_changes = [
70+
tags
71+
]
72+
}
73+
}

infrastructure/modules/key-vault/variables.tf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ variable "disk_encryption" {
1414
default = true
1515
}
1616

17+
/* --------------------------------------------------------------------------------------------------
18+
Monitoring and Diagnostics Variables
19+
-------------------------------------------------------------------------------------------------- */
20+
1721
variable "log_analytics_workspace_id" {
1822
type = string
1923
description = "id of the log analytics workspace to send resource logging to via diagnostic settings"
@@ -35,6 +39,41 @@ variable "monitor_diagnostic_setting_keyvault_metrics" {
3539
description = "Controls what metrics will be enabled for the keyvault"
3640
}
3741

42+
variable "resource_group_name_monitoring" {
43+
type = string
44+
description = "The name of the resource group in which to create the Monitoring resources for the Key Vault. Changing this forces a new resource to be created."
45+
default = null
46+
}
47+
48+
variable "action_group_id" {
49+
type = string
50+
description = "The ID of the Action Group to use for alerts."
51+
default = null
52+
}
53+
54+
variable "enable_alerting" {
55+
description = "Whether monitoring and alerting is enabled for the Key Vault."
56+
type = bool
57+
default = false
58+
}
59+
60+
variable "alert_window_size" {
61+
type = string
62+
nullable = false
63+
default = "PT5M"
64+
validation {
65+
condition = contains(["PT1M", "PT5M", "PT15M", "PT30M", "PT1H", "PT6H", "PT12H"], var.alert_window_size)
66+
error_message = "The alert_window_size must be one of: PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H"
67+
}
68+
description = "The period of time that is used to monitor alert activity e.g. PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H. The interval between checks is adjusted accordingly."
69+
}
70+
71+
variable "alert_secret_expiry_threshold" {
72+
type = number
73+
description = "The threshold for secrets to trigger the alert."
74+
default = 10
75+
}
76+
3877
variable "name" {
3978
description = "The name of the Key Vault."
4079
type = string
@@ -107,3 +146,15 @@ variable "tags" {
107146
description = "Resource tags to be applied throughout the deployment."
108147
default = {}
109148
}
149+
150+
locals {
151+
alert_frequency_map = {
152+
PT5M = "PT1M"
153+
PT15M = "PT1M"
154+
PT30M = "PT1M"
155+
PT1H = "PT1M"
156+
PT6H = "PT5M"
157+
PT12H = "PT5M"
158+
}
159+
alert_frequency = local.alert_frequency_map[var.alert_window_size]
160+
}

0 commit comments

Comments
 (0)