Review custom-role change logs and alerts

Track creation, modification, deletion and restoration of custom IAM roles.

Description

Monitor custom IAM roles when they are created, updated, deleted or restored.

Potential impact

A role change can grant excessive access or remove permissions that users need.

Remediation

To monitor organization-level roles, first route the organization’s audit logs to the project or log bucket where the metric is defined. Count CreateRole, UpdateRole, DeleteRole and UndeleteRole in the collected iam_role logs. Use the metric in an alert condition with a notification channel. Verify delivery with an authorized role change.

Examples

The first example has the wrong resource type and no alert condition. For the second, set var.audit_monitored_resource_type to a type reported by the metric and allow for ingestion delay in the evaluation window.

Before

hcl
resource "google_logging_metric" "custom_role_change" {
  name        = "custom_role_change"
  description = "Detects custom role changes"
  filter = <<-FILTER
    resource.type="Wrong_type"
    AND (protoPayload.methodName = "google.iam.admin.v1.CreateRole" OR
    protoPayload.methodName="google.iam.admin.v1.DeleteRole" OR
    protoPayload.methodName="google.iam.admin.v1.UpdateRole" OR
    protoPayload.methodName="google.iam.admin.v1.UndeleteRole")
  FILTER
}

resource "google_monitoring_alert_policy" "custom_role_alert" {
  display_name = "Custom Role Change Alert"
  combiner     = "OR"
}

After

hcl
resource "google_logging_metric" "custom_role_change" {
  name        = "custom_role_change"
  description = "Detects custom role changes"
  filter = <<-FILTER
    resource.type="iam_role"
    AND (protoPayload.methodName = "google.iam.admin.v1.CreateRole" OR
    protoPayload.methodName="google.iam.admin.v1.DeleteRole" OR
    protoPayload.methodName="google.iam.admin.v1.UpdateRole" OR
    protoPayload.methodName="google.iam.admin.v1.UndeleteRole")
  FILTER
}

resource "google_monitoring_alert_policy" "custom_role_alert" {
  display_name          = "Custom Role Change Alert"
  combiner              = "OR"
  notification_channels = [google_monitoring_notification_channel.security_ops.name]

  conditions {
    display_name = "Matching audit events"
    condition_threshold {
      filter = "metric.type=\"logging.googleapis.com/user/${google_logging_metric.custom_role_change.name}\" AND resource.type=\"${var.audit_monitored_resource_type}\""
      comparison      = "COMPARISON_GT"
      threshold_value = 0
      duration        = "0s"
      aggregations {
        alignment_period   = "600s"
        per_series_aligner = "ALIGN_SUM"
      }
    }
  }
}

References