id: azure-db-mysql-delete-unalerted
info:
  name: Azure MySQL Database Delete Alert Not Configured
  author: princechaddha
  severity: high
  description: |
    Ensure that a Microsoft Azure activity log alert is fired whenever a "Delete MySQL Database" event is triggered within your cloud account. An Azure activity log alert fires each time the action event that matches the condition specified in the alert configuration is triggered. The alert condition that this conformity rule checks for is "Whenever the Activity Log has an event with Category='Administrative', Signal name='Delete MySQL Database (servers/databases)'"
  impact: |
    Failing to configure alerts for "Delete MySQL Database" events can allow unauthorized or accidental deletions to go unnoticed, potentially leading to significant data loss and compliance issues.
  remediation: |
    Configure an activity log alert to fire on "Delete MySQL Database" events with the condition set to "Microsoft.DBforMySQL/servers/databases/delete" and ensure that an action group is attached to manage notifications.
  reference:
    - https://docs.microsoft.com/en-us/azure/azure-monitor/platform/alerts-activity-log
  tags: cloud,devops,azure,microsoft,mysql,azure-cloud-config

flow: |
  code(1);
  for (let AlertData of iterate(template.alertList)) {
    set("id", AlertData);
    code(2);
  }

self-contained: true
code:
  - engine:
      - sh
      - bash
    source: |
      az monitor activity-log alert list --output json --query '[?(enabled==`true`)].id'

    extractors:
      - type: json
        name: alertList
        internal: true
        json:
          - '.[]'

  - engine:
      - sh
      - bash
    source: |
      az monitor activity-log alert show --ids "$id" --query 'condition'

    matchers-condition: and
    matchers:
      - type: word
        words:
          - '"field": "operationName"'

      - type: word
        words:
          - "Microsoft.DBforMySQL/servers/databases/delete"
        negative: true

    extractors:
      - type: dsl
        dsl:
          - 'id + " does not have the correct alert configuration for Delete MySQL Database events"'
# digest: 4a0a0047304502203891a70a07899ac1b60f3f78e68d07e6f8ef8a0220242c448e45b84c8a823aac022100d943c9e0464edb428153c0461b4c101aebd033ada992d887908b2128f0b74649:922c64590222798bb761d5b6d8e72950