Bylaw

Gallery › Monitoring › Azure built-in policies

Resource logs should be enabled for Audit on supported resources

Converted Policy AuditIfNotExists Disabled Mode Indexed Version 1.0.0

Resource logs should be enabled to track activities and events that take place on your resources and give you visibility and insights into any changes that occur. The existence of a diagnostic setting for category group Audit on the selected resource types ensures that these logs are enabled and captured. Applicable resource types are those that support the "Audit" category group.

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

Azure Policy JSONDiagnosticSettingsExistsAudit_Audit.json · 163 lines
{
  "properties": {
    "displayName": "Resource logs should be enabled for Audit on supported resources",
    "description": "Resource logs should be enabled to track activities and events that take place on your resources and give you visibility and insights into any changes that occur. The existence of a diagnostic setting for category group Audit on the selected resource types ensures that these logs are enabled and captured. Applicable resource types are those that support the \"Audit\" category group.",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "metadata": {
      "version": "1.0.0",
      "category": "Monitoring"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      },
      "requiredLogLevel": {
        "type": "String",
        "metadata": {
          "displayName": "Required Log Level",
          "description": "Granularity of logging required to be enabled on a resource to meet compliance. Audit will check that the resource has a diagnostic setting enabled for categoryGroup audit or allLogs. Any will only check that a diagnostic setting exists for the resource."
        },
        "allowedValues": [
          "Audit",
          "Any"
        ],
        "defaultValue": "Audit"
      },
      "resourceTypeList": {
        "type": "array",
        "metadata": {
          "displayName": "Resource Types",
          "description": "List of Resource Types to be evaluated for diagnostic setting existence."
        },
        "allowedValues": [
          "microsoft.agfoodplatform/farmbeats",
          "microsoft.apimanagement/service",
          "microsoft.appconfiguration/configurationstores",
          "microsoft.attestation/attestationproviders",
          "microsoft.automation/automationaccounts",
          "microsoft.avs/privateclouds",
          "microsoft.cache/redis",
          "microsoft.cdn/profiles",
          "microsoft.cognitiveservices/accounts",
          "microsoft.containerregistry/registries",
          "microsoft.devices/iothubs",
          "microsoft.eventgrid/topics",
          "microsoft.eventgrid/domains",
          "microsoft.eventgrid/partnernamespaces",
          "microsoft.eventhub/namespaces",
          "microsoft.keyvault/vaults",
          "microsoft.keyvault/managedhsms",
          "microsoft.machinelearningservices/workspaces",
          "microsoft.media/mediaservices",
          "microsoft.media/videoanalyzers",
          "microsoft.netapp/netappaccounts/capacitypools/volumes",
          "microsoft.network/publicipaddresses",
          "microsoft.network/virtualnetworkgateways",
          "microsoft.network/p2svpngateways",
          "microsoft.network/frontdoors",
          "microsoft.network/bastionhosts",
          "microsoft.operationalinsights/workspaces",
          "microsoft.purview/accounts",
          "microsoft.servicebus/namespaces",
          "microsoft.signalrservice/signalr",
          "microsoft.signalrservice/webpubsub",
          "microsoft.sql/servers/databases",
          "microsoft.sql/managedinstances"
        ],
        "defaultValue": [
          "microsoft.agfoodplatform/farmbeats",
          "microsoft.apimanagement/service",
          "microsoft.appconfiguration/configurationstores",
          "microsoft.attestation/attestationproviders",
          "microsoft.automation/automationaccounts",
          "microsoft.avs/privateclouds",
          "microsoft.cache/redis",
          "microsoft.cdn/profiles",
          "microsoft.cognitiveservices/accounts",
          "microsoft.containerregistry/registries",
          "microsoft.devices/iothubs",
          "microsoft.eventgrid/topics",
          "microsoft.eventgrid/domains",
          "microsoft.eventgrid/partnernamespaces",
          "microsoft.eventhub/namespaces",
          "microsoft.keyvault/vaults",
          "microsoft.keyvault/managedhsms",
          "microsoft.machinelearningservices/workspaces",
          "microsoft.media/mediaservices",
          "microsoft.media/videoanalyzers",
          "microsoft.netapp/netappaccounts/capacitypools/volumes",
          "microsoft.network/publicipaddresses",
          "microsoft.network/virtualnetworkgateways",
          "microsoft.network/p2svpngateways",
          "microsoft.network/frontdoors",
          "microsoft.network/bastionhosts",
          "microsoft.operationalinsights/workspaces",
          "microsoft.purview/accounts",
          "microsoft.servicebus/namespaces",
          "microsoft.signalrservice/signalr",
          "microsoft.signalrservice/webpubsub",
          "microsoft.sql/servers/databases",
          "microsoft.sql/managedinstances"
        ]
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "in": "[parameters('resourceTypeList')]"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/diagnosticSettings",
          "existenceCondition": {
            "count": {
              "field": "Microsoft.Insights/diagnosticSettings/logs[*]",
              "where": {
                "allOf": [
                  {
                    "field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
                    "equals": "true"
                  },
                  {
                    "anyOf": [
                      {
                        "value": "[parameters('requiredLogLevel')]",
                        "equals": "any"
                      },
                      {
                        "field": "microsoft.insights/diagnosticSettings/logs[*].categoryGroup",
                        "equals": "audit"
                      },
                      {
                        "field": "microsoft.insights/diagnosticSettings/logs[*].categoryGroup",
                        "equals": "allLogs"
                      }
                    ]
                  }
                ]
              }
            },
            "greater": 0
          }
        }
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/187242f4-89c6-4c43-9a4e-188c0efacc5f",
  "name": "187242f4-89c6-4c43-9a4e-188c0efacc5f"
}
Bylaw187242f4-89c6-4c43-9a4e-188c0efacc5f.bylaw.ps1 · 52 lines
# Converted from DiagnosticSettingsExistsAudit_Audit.json by ConvertTo-Bylaw.
Policy '187242f4-89c6-4c43-9a4e-188c0efacc5f' {

    Properties @{
        DisplayName   = 'Resource logs should be enabled for Audit on supported resources'
        Description   = 'Resource logs should be enabled to track activities and events that take place on your resources and give you visibility and insights into any changes that occur. The existence of a diagnostic setting for category group Audit on the selected resource types ensures that these logs are enabled and captured. Applicable resource types are those that support the "Audit" category group.'
        Version       = '1.0.0'
        Category      = 'Monitoring'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter RequiredLogLevel @{
        AllowedValues = @('Audit', 'Any')
        Default       = 'Audit'
        DisplayName   = 'Required Log Level'
        Description   = 'Granularity of logging required to be enabled on a resource to meet compliance. Audit will check that the resource has a diagnostic setting enabled for categoryGroup audit or allLogs. Any will only check that a diagnostic setting exists for the resource.'
    }
    Parameter ResourceTypeList @{
        Type          = 'array'
        AllowedValues = @('microsoft.agfoodplatform/farmbeats', 'microsoft.apimanagement/service', 'microsoft.appconfiguration/configurationstores', 'microsoft.attestation/attestationproviders', 'microsoft.automation/automationaccounts', 'microsoft.avs/privateclouds', 'microsoft.cache/redis', 'microsoft.cdn/profiles', 'microsoft.cognitiveservices/accounts', 'microsoft.containerregistry/registries', 'microsoft.devices/iothubs', 'microsoft.eventgrid/topics', 'microsoft.eventgrid/domains', 'microsoft.eventgrid/partnernamespaces', 'microsoft.eventhub/namespaces', 'microsoft.keyvault/vaults', 'microsoft.keyvault/managedhsms', 'microsoft.machinelearningservices/workspaces', 'microsoft.media/mediaservices', 'microsoft.media/videoanalyzers', 'microsoft.netapp/netappaccounts/capacitypools/volumes', 'microsoft.network/publicipaddresses', 'microsoft.network/virtualnetworkgateways', 'microsoft.network/p2svpngateways', 'microsoft.network/frontdoors', 'microsoft.network/bastionhosts', 'microsoft.operationalinsights/workspaces', 'microsoft.purview/accounts', 'microsoft.servicebus/namespaces', 'microsoft.signalrservice/signalr', 'microsoft.signalrservice/webpubsub', 'microsoft.sql/servers/databases', 'microsoft.sql/managedinstances')
        Default       = @('microsoft.agfoodplatform/farmbeats', 'microsoft.apimanagement/service', 'microsoft.appconfiguration/configurationstores', 'microsoft.attestation/attestationproviders', 'microsoft.automation/automationaccounts', 'microsoft.avs/privateclouds', 'microsoft.cache/redis', 'microsoft.cdn/profiles', 'microsoft.cognitiveservices/accounts', 'microsoft.containerregistry/registries', 'microsoft.devices/iothubs', 'microsoft.eventgrid/topics', 'microsoft.eventgrid/domains', 'microsoft.eventgrid/partnernamespaces', 'microsoft.eventhub/namespaces', 'microsoft.keyvault/vaults', 'microsoft.keyvault/managedhsms', 'microsoft.machinelearningservices/workspaces', 'microsoft.media/mediaservices', 'microsoft.media/videoanalyzers', 'microsoft.netapp/netappaccounts/capacitypools/volumes', 'microsoft.network/publicipaddresses', 'microsoft.network/virtualnetworkgateways', 'microsoft.network/p2svpngateways', 'microsoft.network/frontdoors', 'microsoft.network/bastionhosts', 'microsoft.operationalinsights/workspaces', 'microsoft.purview/accounts', 'microsoft.servicebus/namespaces', 'microsoft.signalrservice/signalr', 'microsoft.signalrservice/webpubsub', 'microsoft.sql/servers/databases', 'microsoft.sql/managedinstances')
        DisplayName   = 'Resource Types'
        Description   = 'List of Resource Types to be evaluated for diagnostic setting existence.'
    }

    Resource * {

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

    AuditIfNotExists 'Microsoft.Insights/diagnosticSettings' -Effect (Param Effect) {
        Require 'logs, enabled and categoryGroup are as required' {
            Count 'logs[*]' -Where {
                Field enabled | Must -Be 'true'
                AnyOf {
                    Param RequiredLogLevel | Must -Be 'any'
                    Field categoryGroup | Must -Be 'audit'
                    Field categoryGroup | Must -Be 'allLogs'
                }
            } | Must -BeGreaterThan 0
        }
    }
}

Source: built-in-policies/policyDefinitions/Monitoring/DiagnosticSettingsExistsAudit_Audit.json in Azure/azure-policy, under the MIT licence.