Bylaw

Gallery › SQL › Azure Community Policy

PostgreSQL - Diagnostic Settings v1

Converted Policy AuditIfNotExists Disabled Mode All Version 1.0.0

This Azure Policy creates an audit event when specific log (i.e. PostgreSQLLogs) and metric categories (i.e. AllMetrics) in the Diagnostic Settings are not send to a specific Log Analytics Workspace.

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

Azure Policy JSONazurepolicy.json · 90 lines
{
  "name": "f554333e-c68c-4e12-8b87-136f60bcb450",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "PostgreSQL - Diagnostic Settings v1",
    "description": "This Azure Policy creates an audit event when specific log (i.e. PostgreSQLLogs) and metric categories (i.e. AllMetrics) in the Diagnostic Settings are not send to a specific Log Analytics Workspace.",
    "metadata": {
      "version": "1.0.0",
      "category": "SQL"
    },
    "mode": "All",
    "parameters": {
      "logAnalytics": {
        "type": "String",
        "metadata": {
          "displayName": "Log Analytics Workspace ID",
          "description": "The full resource ID for the Azure 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.DBforPostgreSQL/servers"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/diagnosticSettings",
          "existenceCondition": {
            "allOf": [
              {
                "count": {
                  "field": "Microsoft.Insights/diagnosticSettings/logs[*]",
                  "where": {
                    "allOf": [
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
                        "equals": "true"
                      },
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
                        "equals": "PostgreSQLLogs"
                      }
                    ]
                  }
                },
                "equals": 1
              },
              {
                "count": {
                  "field": "Microsoft.Insights/diagnosticSettings/metrics[*]",
                  "where": {
                    "allOf": [
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/metrics[*].enabled",
                        "equals": "true"
                      },
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/metrics[*].category",
                        "equals": "AllMetrics"
                      }
                    ]
                  }
                },
                "equals": 1
              },
              {
                "field": "Microsoft.Insights/diagnosticSettings/workspaceId",
                "equals": "[parameters('logAnalytics')]"
              }
            ]
          }
        }
      }
    }
  }
}
Bylawf554333e-c68c-4e12-8b87-136f60bcb450.bylaw.ps1 · 39 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'f554333e-c68c-4e12-8b87-136f60bcb450' {

    Properties @{
        DisplayName = 'PostgreSQL - Diagnostic Settings v1'
        Description = 'This Azure Policy creates an audit event when specific log (i.e. PostgreSQLLogs) and metric categories (i.e. AllMetrics) in the Diagnostic Settings are not send to a specific Log Analytics Workspace.'
        Version     = '1.0.0'
        Category    = 'SQL'
        Mode        = 'All'
    }

    Parameter LogAnalytics @{
        Type        = 'String'
        DisplayName = 'Log Analytics Workspace ID'
        Description = 'The full resource ID for the Azure Log Analytics Workspace'
    }
    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.DBforPostgreSQL/servers'

    AuditIfNotExists 'Microsoft.Insights/diagnosticSettings' -Effect (Param Effect) {
        Require 'logs, enabled, category and 2 more are as required' {
            Count 'logs[*]' -Where {
                Field enabled | Must -Be 'true'
                Field category | Must -Be 'PostgreSQLLogs'
            } | Must -Be 1
            Count 'metrics[*]' -Where {
                Field enabled | Must -Be 'true'
                Field category | Must -Be 'AllMetrics'
            } | Must -Be 1
            Field workspaceId | Must -Be (Param LogAnalytics)
        }
    }
}

Source: policyDefinitions/Database for PostgreSQL/postgresql-diagnostic-settings-v1/azurepolicy.json in Azure/Community-Policy, under the MIT licence.