cloud-custodian / cloud-custodian

Rules engine for cloud security, cost optimization, and governance, DSL in yaml for policies to query, filter, and take actions on resources
https://cloudcustodian.io
Apache License 2.0
5.37k stars 1.46k forks source link

Azure - Event Grid Functions using Management groups sends multiple emails #5869

Open justinhauer opened 4 years ago

justinhauer commented 4 years ago

The bug:

A policy using an event grid function in Azure + the management group environment variable appears to send multiple emails (one email for every subscription within the management group) to the Storage account queue for sendgrid to pick up

Steps to reproduce the behavior:

Create the following policy:

vars:
  email-notify: &email-notify
  - type: notify
    template: default.html
    priority_header: '2'
    to:
      - myEmail@myCompany.com
    transport:
      type: asq
      queue: https://myEmailQueue.queue.core.windows.net/taskqueue

policies:
- name: vms-with-public-ip
  description: Finds new Virtual Machines with public IP addresses
  resource: azure.vm
  mode:
    type: azure-event-grid
    events:
      - resourceProvider: Microsoft.Compute/virtualMachines
        event: write
  filters:
  - type: network-interface
    key: 'properties.ipConfigurations[].properties.publicIPAddress.id'
    value: not-null
  actions:
  - <<: *email-notify
    subject: Custodian - New Virtual Machine Resource found with public IP
    violation_desc: 'Notification - The following VM was created with a public IP'
    to:
    - myEmail@myCompany.com

Expected behavior:

One email is received, not multiple

Background :

kapilt commented 4 years ago

yeah.. i'm a little confused by what the ms team did here for management group functions, there's a separate copy of the function for each subscription. which seems odd, i've got some questions about this approach out to them, as it seems like it should not be doing that for mgmt events, and that even for subscription events, and for periodic it should be done on a basis of runtime querying of the subscriptions in the mgmt group.