Bylaw

Gallery › Key Vault › Azure Community Policy

Deploy diagnostic setting for key vault to stream to event hub

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode all Version 1.0.0

Automatically configure a diagnostic setting for key vault resources which will stream to a specified event hub namespace.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONazurepolicy.json · 123 lines
{
  "name": "87837b80-65c9-4c67-9b49-097ea38c9510",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy diagnostic setting for key vault to stream to event hub",
    "description": "Automatically configure a diagnostic setting for key vault resources which will stream to a specified event hub namespace.",
    "metadata": {
      "version": "1.0.0",
      "category": "Key Vault"
    },
    "mode": "all",
    "parameters": {
      "diagnosticsSettingName": {
        "type": "string",
        "metadata": {
          "description": "Name for the diagnostic setting resource (e.g. 'SecOpsSendToEventHub')"
        }
      },
      "eventHubAuthorizationRuleId": {
        "type": "string",
        "metadata": {
          "description": "Resource ID of the event hub authorization rule for the Event Hubs namespace in which the event hubs should be created and/or streamed to."
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.KeyVault/vaults"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/diagnosticSettings",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "existenceCondition": {
            "field": "Microsoft.Insights/diagnosticSettings/eventHubAuthorizationRuleId",
            "equals": "[parameters('eventHubAuthorizationRuleId')]"
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "diagnosticsSettingName": {
                    "type": "string"
                  },
                  "eventHubAuthorizationRuleId": {
                    "type": "string"
                  },
                  "keyVaultName": {
                    "type": "string"
                  }
                },
                "variables": {},
                "functions": [],
                "resources": [
                  {
                    "type": "Microsoft.KeyVault/vaults/providers/diagnosticSettings",
                    "name": "[concat(parameters('keyVaultName'),'/Microsoft.Insights/', parameters('diagnosticsSettingName'))]",
                    "apiVersion": "2017-05-01-preview",
                    "properties": {
                      "eventHubAuthorizationRuleId": "[parameters('eventHubAuthorizationRuleId')]",
                      "logs": [
                        {
                          "category": "AuditEvent",
                          "enabled": true,
                          "retentionPolicy": {
                            "enabled": false,
                            "days": 0
                          }
                        }
                      ],
                      "metrics": [
                        {
                          "category": "AllMetrics",
                          "enabled": true,
                          "retentionPolicy": {
                            "enabled": false,
                            "days": 0
                          }
                        }
                      ]
                    }
                  }
                ],
                "outputs": {}
              },
              "parameters": {
                "diagnosticsSettingName": {
                  "value": "[parameters('diagnosticsSettingName')]"
                },
                "eventHubAuthorizationRuleId": {
                  "value": "[parameters('eventHubAuthorizationRuleId')]"
                },
                "keyVaultName": {
                  "value": "[field('name')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw87837b80-65c9-4c67-9b49-097ea38c9510.bylaw.ps1 · 39 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '87837b80-65c9-4c67-9b49-097ea38c9510' {

    Properties @{
        DisplayName = 'Deploy diagnostic setting for key vault to stream to event hub'
        Description = 'Automatically configure a diagnostic setting for key vault resources which will stream to a specified event hub namespace.'
        Version     = '1.0.0'
        Category    = 'Key Vault'
        Mode        = 'All'
    }

    Parameter DiagnosticsSettingName @{
        Type        = 'string'
        Description = 'Name for the diagnostic setting resource (e.g. ''SecOpsSendToEventHub'')'
    }
    Parameter EventHubAuthorizationRuleId @{
        Type        = 'string'
        Description = 'Resource ID of the event hub authorization rule for the Event Hubs namespace in which the event hubs should be created and/or streamed to.'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.KeyVault/vaults'

    DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Contributor' -Effect (Param Effect) {
        Require 'eventHubAuthorizationRuleId is the EventHubAuthorizationRuleId parameter' {
            Field eventHubAuthorizationRuleId | Must -Be (Param EventHubAuthorizationRuleId)
        }
        Deployment -TemplateFile './templates/87837b80-65c9-4c67-9b49-097ea38c9510.json' -Parameters ([ordered]@{
                diagnosticsSettingName      = (Param DiagnosticsSettingName)
                eventHubAuthorizationRuleId = (Param EventHubAuthorizationRuleId)
                keyVaultName                = (Arm '[field(''name'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Key Vault/deploy-diagnostic-setting-for-key-vault-to-stream-to-event-hub/azurepolicy.json in Azure/Community-Policy, under the MIT licence.