Bylaw

Gallery › Monitoring › Azure Community Policy

Audit diagnostic setting

Converted Policy AuditIfNotExists Disabled Mode All Version 1.0.0

Audit diagnostic setting for selected resource types

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

Azure Policy JSONazurepolicy.json · 58 lines
{
  "name": "f0b4a01d-f65e-4a49-91e3-bf87831fb207",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Audit diagnostic setting",
    "description": "Audit diagnostic setting for selected resource types",
    "metadata": {
      "version": "1.0.0",
      "category": "Monitoring"
    },
    "mode": "All",
    "parameters": {
      "listOfResourceTypes": {
        "type": "Array",
        "metadata": {
          "displayName": "Resource Types",
          "strongType": "resourceTypes"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "in": "[parameters('listOfResourceTypes')]"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/diagnosticSettings",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
                "equals": "true"
              },
              {
                "field": "Microsoft.Insights/diagnosticSettings/metrics.enabled",
                "equals": "true"
              }
            ]
          }
        }
      }
    }
  }
}
Bylawf0b4a01d-f65e-4a49-91e3-bf87831fb207.bylaw.ps1 · 37 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'f0b4a01d-f65e-4a49-91e3-bf87831fb207' {

    Properties @{
        DisplayName = 'Audit diagnostic setting'
        Description = 'Audit diagnostic setting for selected resource types'
        Version     = '1.0.0'
        Category    = 'Monitoring'
        Mode        = 'All'
    }

    Parameter ListOfResourceTypes @{
        Type        = 'Array'
        DisplayName = 'Resource Types'
        Metadata    = [ordered]@{ strongType = 'resourceTypes' }
    }
    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource * {

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

    AuditIfNotExists 'Microsoft.Insights/diagnosticSettings' -Effect (Param Effect) {
        Require 'logs.enabled is true and metrics.enabled is true' {
            Field logs.enabled | Must -Be 'true'
            Field metrics.enabled | Must -Be 'true'
        }
    }
}

Source: policyDefinitions/Monitoring/audit-diagnostic-setting/azurepolicy.json in Azure/Community-Policy, under the MIT licence.