Bylaw

Gallery › Monitoring › Azure Community Policy

Apply Diagnostic Settings for Azure Key Vault to a regional Event Hub

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

This policy automatically deploys diagnostic settings for Azure Key Vault to a regional Event Hub.

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

Azure Policy JSONazurepolicy.json · 211 lines
{
  "name": "ebe03a92-c4ce-4289-a0da-b0ac4b2c681e",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Apply Diagnostic Settings for Azure Key Vault to a regional Event Hub",
    "description": "This policy automatically deploys diagnostic settings for Azure Key Vault to a regional Event Hub.",
    "metadata": {
      "category": "Monitoring",
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "profileName": {
        "type": "String",
        "metadata": {
          "displayName": "Profile Name for Config",
          "description": "The profile name Azure Diagnostics"
        }
      },
      "eventHubName": {
        "type": "String",
        "metadata": {
          "displayName": "EventHub Name",
          "description": "The event hub for Azure Diagnostics"
        }
      },
      "eventHubRuleID": {
        "type": "String",
        "metadata": {
          "displayName": "EventHubRuleID",
          "description": "The event hub RuleID for Azure Diagnostics"
        }
      },
      "azureRegions": {
        "type": "Array",
        "metadata": {
          "displayName": "Allowed Locations",
          "description": "The list of locations that can be specified when deploying resources",
          "strongType": "location"
        }
      },
      "metricsEnabled": {
        "type": "String",
        "metadata": {
          "displayName": "Enable Metrics",
          "description": "Enable Metrics - True or False"
        },
        "allowedValues": [
          "True",
          "False"
        ],
        "defaultValue": "False"
      },
      "logsEnabled": {
        "type": "String",
        "metadata": {
          "displayName": "Enable Logs",
          "description": "Enable Logs - True or False"
        },
        "allowedValues": [
          "True",
          "False"
        ],
        "defaultValue": "True"
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.KeyVault/vaults"
          },
          {
            "field": "location",
            "in": "[parameters('azureRegions')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/diagnosticSettings",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
                "equals": "[parameters('logsEnabled')]"
              },
              {
                "field": "Microsoft.Insights/diagnosticSettings/metrics.enabled",
                "equals": "[parameters('metricsEnabled')]"
              },
              {
                "field": "Microsoft.Insights/diagnosticSettings/eventHubName",
                "equals": "[parameters('eventHubName')]"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "vaultName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "eventHubName": {
                    "type": "string"
                  },
                  "eventHubRuleId": {
                    "type": "string"
                  },
                  "metricsEnabled": {
                    "type": "string"
                  },
                  "logsEnabled": {
                    "type": "string"
                  },
                  "profileName": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.KeyVault/vaults/providers/diagnosticSettings",
                    "apiVersion": "2017-05-01-preview",
                    "name": "[concat(parameters('vaultName'), '/', 'Microsoft.Insights/', parameters('profileName'))]",
                    "location": "[parameters('location')]",
                    "dependsOn": [],
                    "properties": {
                      "eventHubName": "[parameters('eventHubName')]",
                      "eventHubAuthorizationRuleId": "[parameters('eventHubRuleId')]",
                      "metrics": [
                        {
                          "category": "AllMetrics",
                          "enabled": "[parameters('metricsEnabled')]",
                          "retentionPolicy": {
                            "enabled": false,
                            "days": 0
                          }
                        }
                      ],
                      "logs": [
                        {
                          "category": "AuditEvent",
                          "enabled": "[parameters('logsEnabled')]"
                        }
                      ]
                    }
                  }
                ],
                "outputs": {
                  "policy": {
                    "type": "string",
                    "value": "[concat(parameters('eventHubName'), 'configured for diagnostic logs for ', ': ', parameters('name'))]"
                  }
                }
              },
              "parameters": {
                "eventHubName": {
                  "value": "[parameters('eventHubName')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "vaultName": {
                  "value": "[field('name')]"
                },
                "eventHubRuleId": {
                  "value": "[parameters('eventHubRuleId')]"
                },
                "metricsEnabled": {
                  "value": "[parameters('metricsEnabled')]"
                },
                "logsEnabled": {
                  "value": "[parameters('logsEnabled')]"
                },
                "profileName": {
                  "value": "[parameters('profileName')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylawebe03a92-c4ce-4289-a0da-b0ac4b2c681e.bylaw.ps1 · 75 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'ebe03a92-c4ce-4289-a0da-b0ac4b2c681e' {

    Properties @{
        DisplayName = 'Apply Diagnostic Settings for Azure Key Vault to a regional Event Hub'
        Description = 'This policy automatically deploys diagnostic settings for Azure Key Vault to a regional Event Hub.'
        Category    = 'Monitoring'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Parameter ProfileName @{
        Type        = 'String'
        DisplayName = 'Profile Name for Config'
        Description = 'The profile name Azure Diagnostics'
    }
    Parameter EventHubName @{
        Type        = 'String'
        DisplayName = 'EventHub Name'
        Description = 'The event hub for Azure Diagnostics'
    }
    Parameter EventHubRuleID @{
        Type        = 'String'
        DisplayName = 'EventHubRuleID'
        Description = 'The event hub RuleID for Azure Diagnostics'
    }
    Parameter AzureRegions @{
        Type        = 'Array'
        DisplayName = 'Allowed Locations'
        Description = 'The list of locations that can be specified when deploying resources'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter MetricsEnabled @{
        AllowedValues = @('True', 'False')
        Default       = 'False'
        DisplayName   = 'Enable Metrics'
        Description   = 'Enable Metrics - True or False'
    }
    Parameter LogsEnabled @{
        AllowedValues = @('True', 'False')
        Default       = 'True'
        DisplayName   = 'Enable Logs'
        Description   = 'Enable Logs - True or False'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.KeyVault/vaults' {

        Require 'location is not one of the AzureRegions parameter' {
            Field location | Must -Not -BeIn (Param AzureRegions) -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Contributor' -Effect (Param Effect) {
        Require 'logs.enabled, metrics.enabled and eventHubName are as required' {
            Field logs.enabled | Must -Be (Param LogsEnabled)
            Field metrics.enabled | Must -Be (Param MetricsEnabled)
            Field eventHubName | Must -Be (Param EventHubName)
        }
        Deployment -TemplateFile './templates/ebe03a92-c4ce-4289-a0da-b0ac4b2c681e.json' -Parameters ([ordered]@{
                eventHubName   = (Param EventHubName)
                location       = (Arm '[field(''location'')]')
                vaultName      = (Arm '[field(''name'')]')
                eventHubRuleId = (Arm '[parameters(''eventHubRuleId'')]')
                metricsEnabled = (Param MetricsEnabled)
                logsEnabled    = (Param LogsEnabled)
                profileName    = (Param ProfileName)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Monitoring/apply-diagnostic-settings-for-azure-key-vault-to-a-regional-event-hub/azurepolicy.json in Azure/Community-Policy, under the MIT licence.