Bylaw

Gallery › Key Vault › Azure Community Policy

Key Vault - Diagnostic Settings AINE

Converted Policy AuditIfNotExists Disabled Mode All Version 1.0.0

This Azure Policy creates an audit event when all logs and metrics are not send 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 · 139 lines
{
  "name": "c6a36b44-0a98-4857-9890-0b836a3fbd91",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Key Vault - Diagnostic Settings AINE",
    "description": "This Azure Policy creates an audit event when all logs and metrics are not send 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": "AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      }
    },
    "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')]"
              }
            ]
          }
        }
      }
    }
  }
}
Bylawc6a36b44-0a98-4857-9890-0b836a3fbd91.bylaw.ps1 · 60 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'c6a36b44-0a98-4857-9890-0b836a3fbd91' {

    Properties @{
        DisplayName = 'Key Vault - Diagnostic Settings AINE'
        Description = 'This Azure Policy creates an audit event when all logs and metrics are not send 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 = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.KeyVault/vaults'

    AuditIfNotExists 'Microsoft.Insights/diagnosticSettings' -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)
        }
    }
}

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