Bylaw

Gallery › Key Vault › Azure Community Policy

Key Vault - Diagnostic Settings DINE

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

This Azure Policy creates a deployment to send all logs and metrics to a specified Log Analytics Workspace

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

Azure Policy JSONazurepolicy.json · 227 lines
{
  "name": "42308b1f-acbf-4a6c-a37e-f0184d0cad66",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Key Vault - Diagnostic Settings DINE",
    "description": "This Azure Policy creates a deployment to send all logs and metrics to a specified Log Analytics Workspace",
    "metadata": {
      "version": "1.0.0",
      "category": "Key Vault"
    },
    "mode": "All",
    "parameters": {
      "profileName": {
        "type": "String",
        "metadata": {
          "displayName": "Profile Name",
          "description": "The profile name of the Diagnostic Settings"
        }
      },
      "logAnalyticsWorkspace": {
        "type": "String",
        "metadata": {
          "displayName": "Log Analytics Workspace",
          "description": "The resource Id of the Log Analytics Workspace"
        }
      },
      "metricsEnabled": {
        "type": "String",
        "metadata": {
          "displayName": "Metrics Enabled",
          "description": "Whether the metrics will be send to the Log Analytics Workspace or not"
        },
        "allowedValues": [
          "True",
          "False"
        ]
      },
      "logsEnabled": {
        "type": "String",
        "metadata": {
          "displayName": "Logs Enabled",
          "description": "Whether the logs will be send to the Log Analytics Workspace or not"
        },
        "allowedValues": [
          "True",
          "False"
        ]
      },
      "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",
          "name": "[parameters('profileName')]",
          "evaluationDelay": "AfterProvisioning",
          "existenceCondition": {
            "allOf": [
              {
                "count": {
                  "field": "Microsoft.Insights/diagnosticSettings/logs[*]",
                  "where": {
                    "allOf": [
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
                        "equals": "[parameters('logsEnabled')]"
                      },
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
                        "equals": "AuditEvent"
                      }
                    ]
                  }
                },
                "equals": 1
              },
              {
                "count": {
                  "field": "Microsoft.Insights/diagnosticSettings/logs[*]",
                  "where": {
                    "allOf": [
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
                        "equals": "[parameters('logsEnabled')]"
                      },
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
                        "equals": "AzurePolicyEvaluationDetails"
                      }
                    ]
                  }
                },
                "equals": 1
              },
              {
                "count": {
                  "field": "Microsoft.Insights/diagnosticSettings/metrics[*]",
                  "where": {
                    "allOf": [
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/metrics[*].enabled",
                        "equals": "[parameters('metricsEnabled')]"
                      },
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/metrics[*].category",
                        "equals": "AllMetrics"
                      }
                    ]
                  }
                },
                "equals": 1
              },
              {
                "field": "Microsoft.Insights/diagnosticSettings/workspaceId",
                "equals": "[parameters('logAnalyticsWorkspace')]"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/749f88d5-cbae-40b8-bcfc-e573ddc772fa",
            "/providers/microsoft.authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "resourceName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "logAnalyticsWorkspace": {
                    "type": "string"
                  },
                  "profileName": {
                    "type": "string"
                  },
                  "metricsEnabled": {
                    "type": "string"
                  },
                  "logsEnabled": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.KeyVault/vaults/providers/diagnosticSettings",
                    "apiVersion": "2017-05-01-preview",
                    "name": "[concat(parameters('resourceName'), '/', 'Microsoft.Insights/', parameters('profileName'))]",
                    "location": "[parameters('location')]",
                    "dependsOn": [],
                    "properties": {
                      "workspaceId": "[parameters('logAnalyticsWorkspace')]",
                      "metrics": [
                        {
                          "category": "AllMetrics",
                          "enabled": "[parameters('metricsEnabled')]",
                          "retentionPolicy": {
                            "enabled": false,
                            "days": 0
                          }
                        }
                      ],
                      "logs": [
                        {
                          "category": "AuditEvent",
                          "enabled": "[parameters('logsEnabled')]"
                        },
                        {
                          "category": "AzurePolicyEvaluationDetails",
                          "enabled": "[parameters('logsEnabled')]"
                        }
                      ]
                    }
                  }
                ],
                "outputs": {}
              },
              "parameters": {
                "location": {
                  "value": "[field('location')]"
                },
                "resourceName": {
                  "value": "[field('name')]"
                },
                "logAnalyticsWorkspace": {
                  "value": "[parameters('logAnalyticsWorkspace')]"
                },
                "profileName": {
                  "value": "[parameters('profileName')]"
                },
                "metricsEnabled": {
                  "value": "[parameters('metricsEnabled')]"
                },
                "logsEnabled": {
                  "value": "[parameters('logsEnabled')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw42308b1f-acbf-4a6c-a37e-f0184d0cad66.bylaw.ps1 · 68 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '42308b1f-acbf-4a6c-a37e-f0184d0cad66' {

    Properties @{
        DisplayName = 'Key Vault - Diagnostic Settings DINE'
        Description = 'This Azure Policy creates a deployment to send all logs and metrics to a specified Log Analytics Workspace'
        Version     = '1.0.0'
        Category    = 'Key Vault'
        Mode        = 'All'
    }

    Parameter ProfileName @{
        Type        = 'String'
        DisplayName = 'Profile Name'
        Description = 'The profile name of the Diagnostic Settings'
    }
    Parameter LogAnalyticsWorkspace @{
        Type        = 'String'
        DisplayName = 'Log Analytics Workspace'
        Description = 'The resource Id of the Log Analytics Workspace'
    }
    Parameter MetricsEnabled @{
        Type          = 'String'
        AllowedValues = @('True', 'False')
        DisplayName   = 'Metrics Enabled'
        Description   = 'Whether the metrics will be send to the Log Analytics Workspace or not'
    }
    Parameter LogsEnabled @{
        Type          = 'String'
        AllowedValues = @('True', 'False')
        DisplayName   = 'Logs Enabled'
        Description   = 'Whether the logs will be send to the Log Analytics Workspace or not'
    }
    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 'Monitoring Contributor', 'Log Analytics Contributor' -Name (Param ProfileName) -EvaluationDelay AfterProvisioning -Effect (Param Effect) {
        Require 'logs, enabled, category and 2 more are as required' {
            Count 'logs[*]' -Where {
                Field enabled | Must -Be (Param LogsEnabled)
                Field category | Must -Be 'AuditEvent'
            } | Must -Be 1
            Count 'logs[*]' -Where {
                Field enabled | Must -Be (Param LogsEnabled)
                Field category | Must -Be 'AzurePolicyEvaluationDetails'
            } | Must -Be 1
            Count 'metrics[*]' -Where {
                Field enabled | Must -Be (Param MetricsEnabled)
                Field category | Must -Be 'AllMetrics'
            } | Must -Be 1
            Field workspaceId | Must -Be (Param LogAnalyticsWorkspace)
        }
        Deployment -TemplateFile './templates/42308b1f-acbf-4a6c-a37e-f0184d0cad66.json' -Parameters ([ordered]@{
                location              = (Arm '[field(''location'')]')
                resourceName          = (Arm '[field(''name'')]')
                logAnalyticsWorkspace = (Param LogAnalyticsWorkspace)
                profileName           = (Param ProfileName)
                metricsEnabled        = (Param MetricsEnabled)
                logsEnabled           = (Param LogsEnabled)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Key Vault/key-vault-diagnostic-settings-dine/azurepolicy.json in Azure/Community-Policy, under the MIT licence.