Bylaw

Gallery › Storage › Azure Community Policy

Storage Account - 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 · 75 lines
{
  "name": "94876c10-a8cd-4c32-9ce1-ad4140beb1dc",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Storage Account - 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": "Storage"
    },
    "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"
        }
      },
      "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.Storage/storageAccounts"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/diagnosticSettings",
          "name": "[parameters('profileName')]",
          "evaluationDelay": "AfterProvisioning",
          "existenceCondition": {
            "allOf": [
              {
                "anyof": [
                  {
                    "field": "Microsoft.Insights/diagnosticSettings/metrics.enabled",
                    "equals": "True"
                  },
                  {
                    "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
                    "equals": "True"
                  }
                ]
              },
              {
                "field": "Microsoft.Insights/diagnosticSettings/workspaceId",
                "equals": "[parameters('logAnalyticsWorkspace')]"
              }
            ]
          }
        }
      }
    }
  }
}
Bylaw94876c10-a8cd-4c32-9ce1-ad4140beb1dc.bylaw.ps1 · 40 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '94876c10-a8cd-4c32-9ce1-ad4140beb1dc' {

    Properties @{
        DisplayName = 'Storage Account - 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    = 'Storage'
        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 Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Storage/storageAccounts'

    AuditIfNotExists 'Microsoft.Insights/diagnosticSettings' -Name (Param ProfileName) -EvaluationDelay AfterProvisioning -Effect (Param Effect) {
        Require 'metrics.enabled, logs.enabled and workspaceId are as required' {
            AnyOf {
                Field metrics.enabled | Must -Be 'True'
                Field logs.enabled | Must -Be 'True'
            }
            Field workspaceId | Must -Be (Param LogAnalyticsWorkspace)
        }
    }
}

Source: policyDefinitions/Storage/storage-account-diagnostic-settings-aine/azurepolicy.json in Azure/Community-Policy, under the MIT licence.