Bylaw

Gallery › Monitoring › Azure built-in policies

Audit diagnostic setting for selected resource types

Converted Policy AuditIfNotExists Mode All Version 2.0.1

Audit diagnostic setting for selected resource types. Be sure to select only resource types which support diagnostics settings.

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

Azure Policy JSONDiagnosticSettingsForTypes_Audit.json · 107 lines
{
  "properties": {
    "displayName": "Audit diagnostic setting for selected resource types",
    "policyType": "BuiltIn",
    "mode": "All",
    "description": "Audit diagnostic setting for selected resource types. Be sure to select only resource types which support diagnostics settings.",
    "metadata": {
      "version": "2.0.1",
      "category": "Monitoring"
    },
    "version": "2.0.1",
    "parameters": {
      "listOfResourceTypes": {
        "type": "Array",
        "metadata": {
          "displayName": "Resource Types",
          "strongType": "resourceTypes"
        }
      },
      "logsEnabled": {
        "type": "Boolean",
        "metadata": {
          "displayName": "Logs Enabled"
        },
        "allowedValues": [
          true,
          false
        ],
        "defaultValue": true
      },
      "metricsEnabled": {
        "type": "Boolean",
        "metadata": {
          "displayName": "Metrics Enabled"
        },
        "allowedValues": [
          true,
          false
        ],
        "defaultValue": true
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "notIn": [
              "microsoft.security/assessmentmetadata",
              "microsoft.network/networksecuritygroups/securityrules",
              "microsoft.storage/storageaccounts/queueservices/queues",
              "microsoft.operationalinsights/workspaces/tables",
              "microsoft.machinelearningservices/workspaces/jobs",
              "microsoft.authorization/rolemanagementpolicies",
              "microsoft.apimanagement/service/apis/operations",
              "microsoft.apimanagement/service/apis/operations/tags",
              "microsoft.compute/disks",
              "microsoft.compute/virtualmachines/extensions",
              "microsoft.authorization/roleassignments",
              "microsoft.storage/storageaccounts/tableservices/tables",
              "microsoft.insights/components/proactivedetectionconfigs",
              "microsoft.network/routetables/routes",
              "microsoft.machinelearningservices/workspaces/environments/versions",
              "microsoft.sql/servers/databases/advisors",
              "microsoft.security/policies",
              "microsoft.resources/subscriptions/resourcegroups",
              "microsoft.authorization/roledefinitions",
              "microsoft.apimanagement/service/apis/operations/policies",
              "microsoft.compute/snapshots",
              "microsoft.datafactory/factories/pipelines",
              "microsoft.datafactory/factories/datasets",
              "microsoft.network/virtualnetworks/subnets",
              "microsoft.security/pricings"
            ]
          },
          {
            "field": "type",
            "in": "[parameters('listOfResourceTypes')]"
          }
        ]
      },
      "then": {
        "effect": "AuditIfNotExists",
        "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')]"
              }
            ]
          }
        }
      }
    },
    "versions": [
      "2.0.1"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/7f89b1eb-583c-429a-8828-af049802c1d9",
  "name": "7f89b1eb-583c-429a-8828-af049802c1d9"
}
Bylaw7f89b1eb-583c-429a-8828-af049802c1d9.bylaw.ps1 · 48 lines
# Converted from DiagnosticSettingsForTypes_Audit.json by ConvertTo-Bylaw.
Policy '7f89b1eb-583c-429a-8828-af049802c1d9' {

    Properties @{
        DisplayName   = 'Audit diagnostic setting for selected resource types'
        Description   = 'Audit diagnostic setting for selected resource types. Be sure to select only resource types which support diagnostics settings.'
        Version       = '2.0.1'
        Category      = 'Monitoring'
        PolicyVersion = '2.0.1'
        Mode          = 'All'
    }

    Parameter ListOfResourceTypes @{
        Type        = 'Array'
        DisplayName = 'Resource Types'
        Metadata    = [ordered]@{ strongType = 'resourceTypes' }
    }
    Parameter LogsEnabled @{
        Type          = 'Boolean'
        AllowedValues = @($true, $false)
        Default       = $true
        DisplayName   = 'Logs Enabled'
    }
    Parameter MetricsEnabled @{
        Type          = 'Boolean'
        AllowedValues = @($true, $false)
        Default       = $true
        DisplayName   = 'Metrics Enabled'
    }

    Resource * {

        When {
            Field type | Must -Not -BeIn @('microsoft.security/assessmentmetadata', 'microsoft.network/networksecuritygroups/securityrules', 'microsoft.storage/storageaccounts/queueservices/queues', 'microsoft.operationalinsights/workspaces/tables', 'microsoft.machinelearningservices/workspaces/jobs', 'microsoft.authorization/rolemanagementpolicies', 'microsoft.apimanagement/service/apis/operations', 'microsoft.apimanagement/service/apis/operations/tags', 'microsoft.compute/disks', 'microsoft.compute/virtualmachines/extensions', 'microsoft.authorization/roleassignments', 'microsoft.storage/storageaccounts/tableservices/tables', 'microsoft.insights/components/proactivedetectionconfigs', 'microsoft.network/routetables/routes', 'microsoft.machinelearningservices/workspaces/environments/versions', 'microsoft.sql/servers/databases/advisors', 'microsoft.security/policies', 'microsoft.resources/subscriptions/resourcegroups', 'microsoft.authorization/roledefinitions', 'microsoft.apimanagement/service/apis/operations/policies', 'microsoft.compute/snapshots', 'microsoft.datafactory/factories/pipelines', 'microsoft.datafactory/factories/datasets', 'microsoft.network/virtualnetworks/subnets', 'microsoft.security/pricings')
        }

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

    AuditIfNotExists 'Microsoft.Insights/diagnosticSettings' {
        Require 'logs.enabled is the LogsEnabled parameter and metrics.enabled is the MetricsEnabled parameter' {
            Field logs.enabled | Must -Be (Param LogsEnabled)
            Field metrics.enabled | Must -Be (Param MetricsEnabled)
        }
    }
}

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