Bylaw

Gallery › Key Vault › Azure built-in policies

Deploy - Configure diagnostic settings for Azure Key Vault to Log Analytics workspace

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 2.0.1

Deploys the diagnostic settings for Azure Key Vault to stream resource logs to a Log Analytics workspace when any Key Vault which is missing this diagnostic settings is created or updated.

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

Azure Policy JSONDataConnectorsAzureKeyVault.json · 204 lines
{
  "properties": {
    "displayName": "Deploy - Configure diagnostic settings for Azure Key Vault to Log Analytics workspace",
    "description": "Deploys the diagnostic settings for Azure Key Vault to stream resource logs to a Log Analytics workspace when any Key Vault which is missing this diagnostic settings is created or updated.",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "metadata": {
      "version": "2.0.1",
      "category": "Key Vault"
    },
    "version": "2.0.1",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "DeployIfNotExists",
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      },
      "diagnosticsSettingNameToUse": {
        "type": "String",
        "metadata": {
          "displayName": "Setting name",
          "description": "Name of the diagnostic settings."
        },
        "defaultValue": "AzureKeyVaultDiagnosticsLogsToWorkspace"
      },
      "logAnalytics": {
        "type": "String",
        "metadata": {
          "displayName": "Log Analytics workspace",
          "description": "Specify the Log Analytics workspace the Key Vault should be connected to.",
          "strongType": "omsWorkspace",
          "assignPermissions": true
        }
      },
      "AuditEventEnabled": {
        "type": "String",
        "metadata": {
          "displayName": "AuditEvent - Enabled",
          "description": "Whether to stream AuditEvent logs to the Log Analytics workspace - True or False"
        },
        "allowedValues": [
          "True",
          "False"
        ],
        "defaultValue": "True"
      },
      "AllMetricsEnabled": {
        "type": "String",
        "metadata": {
          "displayName": "AllMetrics - Enabled",
          "description": "Whether to stream AllMetrics logs to the Log Analytics workspace - True or False"
        },
        "allowedValues": [
          "True",
          "False"
        ],
        "defaultValue": "True"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.KeyVault/vaults"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/diagnosticSettings",
          "evaluationDelay": "AfterProvisioning",
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/749f88d5-cbae-40b8-bcfc-e573ddc772fa",
            "/providers/microsoft.authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "anyof": [
                  {
                    "field": "Microsoft.Insights/diagnosticSettings/metrics.enabled",
                    "equals": "[parameters('AllMetricsEnabled')]"
                  },
                  {
                    "count": {
                      "field": "Microsoft.Insights/diagnosticSettings/logs[*]",
                      "where": {
                        "allOf": [
                          {
                            "field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
                            "equals": "true"
                          },
                          {
                            "field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
                            "equals": "AuditEvent"
                          }
                        ]
                      }
                    },
                    "greaterOrEquals": 1
                  }
                ]
              },
              {
                "field": "Microsoft.Insights/diagnosticSettings/workspaceId",
                "equals": "[parameters('logAnalytics')]"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "diagnosticsSettingNameToUse": {
                    "type": "string"
                  },
                  "resourceName": {
                    "type": "string"
                  },
                  "logAnalytics": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "AuditEventEnabled": {
                    "type": "string"
                  },
                  "AllMetricsEnabled": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.KeyVault/vaults/providers/diagnosticSettings",
                    "apiVersion": "2017-05-01-preview",
                    "name": "[concat(parameters('resourceName'), '/', 'Microsoft.Insights/', parameters('diagnosticsSettingNameToUse'))]",
                    "location": "[parameters('location')]",
                    "dependsOn": [],
                    "properties": {
                      "workspaceId": "[parameters('logAnalytics')]",
                      "metrics": [
                        {
                          "category": "AllMetrics",
                          "enabled": "[parameters('AllMetricsEnabled')]",
                          "retentionPolicy": {
                            "days": 0,
                            "enabled": false
                          },
                          "timeGrain": null
                        }
                      ],
                      "logs": [
                        {
                          "category": "AuditEvent",
                          "enabled": "[parameters('AuditEventEnabled')]"
                        }
                      ]
                    }
                  }
                ],
                "outputs": {}
              },
              "parameters": {
                "diagnosticsSettingNameToUse": {
                  "value": "[parameters('diagnosticsSettingNameToUse')]"
                },
                "logAnalytics": {
                  "value": "[parameters('logAnalytics')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "resourceName": {
                  "value": "[field('name')]"
                },
                "AuditEventEnabled": {
                  "value": "[parameters('AuditEventEnabled')]"
                },
                "AllMetricsEnabled": {
                  "value": "[parameters('AllMetricsEnabled')]"
                }
              }
            }
          }
        }
      }
    },
    "versions": [
      "2.0.1"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/951af2fa-529b-416e-ab6e-066fd85ac459",
  "name": "951af2fa-529b-416e-ab6e-066fd85ac459"
}
Bylaw951af2fa-529b-416e-ab6e-066fd85ac459.bylaw.ps1 · 68 lines
# Converted from DataConnectorsAzureKeyVault.json by ConvertTo-Bylaw.
Policy '951af2fa-529b-416e-ab6e-066fd85ac459' {

    Properties @{
        DisplayName   = 'Deploy - Configure diagnostic settings for Azure Key Vault to Log Analytics workspace'
        Description   = 'Deploys the diagnostic settings for Azure Key Vault to stream resource logs to a Log Analytics workspace when any Key Vault which is missing this diagnostic settings is created or updated.'
        Version       = '2.0.1'
        Category      = 'Key Vault'
        PolicyVersion = '2.0.1'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter DiagnosticsSettingNameToUse @{
        Default     = 'AzureKeyVaultDiagnosticsLogsToWorkspace'
        DisplayName = 'Setting name'
        Description = 'Name of the diagnostic settings.'
    }
    Parameter LogAnalytics @{
        Type        = 'String'
        DisplayName = 'Log Analytics workspace'
        Description = 'Specify the Log Analytics workspace the Key Vault should be connected to.'
        Metadata    = [ordered]@{ strongType = 'omsWorkspace'; assignPermissions = $true }
    }
    Parameter AuditEventEnabled @{
        AllowedValues = @('True', 'False')
        Default       = 'True'
        DisplayName   = 'AuditEvent - Enabled'
        Description   = 'Whether to stream AuditEvent logs to the Log Analytics workspace - True or False'
        JsonName      = 'AuditEventEnabled'
    }
    Parameter AllMetricsEnabled @{
        AllowedValues = @('True', 'False')
        Default       = 'True'
        DisplayName   = 'AllMetrics - Enabled'
        Description   = 'Whether to stream AllMetrics logs to the Log Analytics workspace - True or False'
        JsonName      = 'AllMetricsEnabled'
    }

    Resource 'Microsoft.KeyVault/vaults'

    DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Monitoring Contributor', 'Log Analytics Contributor' -EvaluationDelay AfterProvisioning -Effect (Param Effect) {
        Require 'metrics.enabled, logs, enabled and 2 more are as required' {
            AnyOf {
                Field metrics.enabled | Must -Be (Param AllMetricsEnabled)
                Count 'logs[*]' -Where {
                    Field enabled | Must -Be 'true'
                    Field category | Must -Be 'AuditEvent'
                } | Must -BeGreaterOrEqual 1
            }
            Field workspaceId | Must -Be (Param LogAnalytics)
        }
        Deployment -TemplateFile './templates/951af2fa-529b-416e-ab6e-066fd85ac459.json' -Parameters ([ordered]@{
                diagnosticsSettingNameToUse = (Param DiagnosticsSettingNameToUse)
                logAnalytics                = (Param LogAnalytics)
                location                    = (Arm '[field(''location'')]')
                resourceName                = (Arm '[field(''name'')]')
                AuditEventEnabled           = (Param AuditEventEnabled)
                AllMetricsEnabled           = (Param AllMetricsEnabled)
            })
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/Key Vault/DataConnectorsAzureKeyVault.json in Azure/azure-policy, under the MIT licence.