Bylaw

Gallery › SQL › Azure Community Policy

SQL Server Auditing Settings must log to specified Workspace

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

This compliments an existing built-in Policy: https://github.com/Azure/azure-policy/blob/master/built-in-policies/policyDefinitions/SQL/DataConnectosSqServerLogs_PolicyAssignment.json. This Policy is designed to detect if the Workspace configured for your audit settings has been changed and if so, change them back.

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

Azure Policy JSONazurepolicy.json · 152 lines
{
  "name": "2372a6b7-9b11-4d33-8d7b-38930308b451",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "SQL Server Auditing Settings must log to specified Workspace",
    "description": "This compliments an existing built-in Policy: https://github.com/Azure/azure-policy/blob/master/built-in-policies/policyDefinitions/SQL/DataConnectosSqServerLogs_PolicyAssignment.json. This Policy is designed to detect if the Workspace configured for your audit settings has been changed and if so, change them back.",
    "metadata": {
      "version": "1.0.0",
      "category": "SQL"
    },
    "mode": "Indexed",
    "parameters": {
      "logAnalytics": {
        "type": "String",
        "metadata": {
          "displayName": "Log Analytics workspace",
          "description": "Select the Log Analytics workspace from dropdown list",
          "strongType": "omsWorkspace",
          "assignPermissions": true
        }
      },
      "diagnosticsSettingName": {
        "type": "String",
        "metadata": {
          "displayName": "Setting name",
          "description": "Name of the diagnostic settings."
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Sql/servers/databases"
          },
          {
            "field": "name",
            "equals": "master"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/diagnosticSettings",
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/749f88d5-cbae-40b8-bcfc-e573ddc772fa",
            "/providers/microsoft.authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "count": {
                  "field": "Microsoft.Insights/diagnosticSettings/logs[*]",
                  "where": {
                    "allOf": [
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
                        "equals": "SQLSecurityAuditEvents"
                      },
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
                        "equals": "true"
                      }
                    ]
                  }
                },
                "equals": 1
              },
              {
                "field": "Microsoft.Insights/diagnosticSettings/workspaceId",
                "matchInsensitively": "[parameters('logAnalytics')]"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "logAnalytics": {
                    "type": "String"
                  },
                  "diagnosticsSettingName": {
                    "type": "String"
                  },
                  "resourceName": {
                    "type": "String"
                  },
                  "location": {
                    "type": "String"
                  }
                },
                "resources": [
                  {
                    "type": "Microsoft.Sql/servers/databases/providers/diagnosticSettings",
                    "apiVersion": "2017-05-01-preview",
                    "name": "[concat(parameters('resourceName'), '/Microsoft.Insights/', parameters('diagnosticsSettingName'))]",
                    "location": "[parameters('location')]",
                    "properties": {
                      "workspaceId": "[parameters('logAnalytics')]",
                      "logs": [
                        {
                          "category": "SQLSecurityAuditEvents",
                          "enabled": true,
                          "retentionPolicy": {
                            "enabled": false,
                            "days": 0
                          }
                        }
                      ]
                    }
                  }
                ]
              },
              "parameters": {
                "logAnalytics": {
                  "value": "[parameters('logAnalytics')]"
                },
                "diagnosticsSettingName": {
                  "value": "[parameters('diagnosticsSettingName')]"
                },
                "resourceName": {
                  "value": "[field('fullName')]"
                },
                "location": {
                  "value": "[field('location')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw2372a6b7-9b11-4d33-8d7b-38930308b451.bylaw.ps1 · 52 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '2372a6b7-9b11-4d33-8d7b-38930308b451' {

    Properties @{
        DisplayName = 'SQL Server Auditing Settings must log to specified Workspace'
        Description = 'This compliments an existing built-in Policy: https://github.com/Azure/azure-policy/blob/master/built-in-policies/policyDefinitions/SQL/DataConnectosSqServerLogs_PolicyAssignment.json. This Policy is designed to detect if the Workspace configured for your audit settings has been changed and if so, change them back.'
        Version     = '1.0.0'
        Category    = 'SQL'
        Mode        = 'Indexed'
    }

    Parameter LogAnalytics @{
        Type        = 'String'
        DisplayName = 'Log Analytics workspace'
        Description = 'Select the Log Analytics workspace from dropdown list'
        Metadata    = [ordered]@{ strongType = 'omsWorkspace'; assignPermissions = $true }
    }
    Parameter DiagnosticsSettingName @{
        Type        = 'String'
        DisplayName = 'Setting name'
        Description = 'Name of the diagnostic settings.'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Sql/servers/databases' {

        Require 'name is not master' {
            Field name | Must -Not -Be 'master' -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Monitoring Contributor', 'Log Analytics Contributor' -Effect (Param Effect) {
        Require 'logs, category, enabled and 1 more are as required' {
            Count 'logs[*]' -Where {
                Field category | Must -Be 'SQLSecurityAuditEvents'
                Field enabled | Must -Be 'true'
            } | Must -Be 1
            Field workspaceId | Must -Match (Param LogAnalytics)
        }
        Deployment -TemplateFile './templates/2372a6b7-9b11-4d33-8d7b-38930308b451.json' -Parameters ([ordered]@{
                logAnalytics           = (Param LogAnalytics)
                diagnosticsSettingName = (Param DiagnosticsSettingName)
                resourceName           = (Arm '[field(''fullName'')]')
                location               = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/SQL/sql-server-auditing-settings-must-log-to-specified-workspace/azurepolicy.json in Azure/Community-Policy, under the MIT licence.