Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: create object neary expiry alerts #97

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,55 @@ resource "azurerm_monitor_diagnostic_setting" "this" {
}
}
}

# Ref: https://learn.microsoft.com/en-us/azure/key-vault/general/alert#example-log-query-alert-for-near-expiry-certificates (2024-08-27)
resource "azurerm_monitor_scheduled_query_rules_alert_v2" "secret_near_expiry" {
name = "Secret near expiry - ${azurerm_key_vault.this.name}"
resource_group_name = var.resource_group_name
location = var.location
scopes = [azurerm_key_vault.this.id]
description = "" # TODO

criteria {
query = <<-QUERY
AzureDiagnostics
| where OperationName == 'SecretNearExpiryEventGridNotification'
| extend SecretExpire = unixtime_seconds_todatetime(eventGridEventProperties_data_EXP_d)
| extend DaysTillExpire = datetime_diff("Day", SecretExpire, now())
| project ResourceId, SecretName = eventGridEventProperties_subject_s, DaysTillExpire, SecretExpire
QUERY

time_aggregation_method = "Count" # TODO: aggregation granularity?
resource_id_column = "ResourceId"
operator = "GreaterThan"
threshold = 0

# Ensure unique SecretName + DaysTillExpire combinations will trigger separate alerts
dimension {
name = "SecretName"
operator = "Include"
values = ["*"]
}
dimension {
name = "DaysTillExpire"
operator = "Include"
values = ["*"]
}
}

# Configure alert to run once per day, and evaluate logs from the entire previous day
evaluation_frequency = "P1D" # TODO
window_duration = "P1D" # TODO

severity = 2 # Warning

# Sometimes fails during creation since no logs exist yet.
# Skip that validation.
skip_query_validation = true

action {
action_groups = [] # TODO
}

tags = var.tags
}