Bylaw

Gallery › SQL › Azure built-in policies

Auditing on SQL server should be enabled

Converted Policy AuditIfNotExists Disabled Mode Indexed Version 2.0.0

Auditing on your SQL Server should be enabled to track database activities across all databases on the server and save them in an audit log.

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

Azure Policy JSONSqlServerAuditing_Audit.json · 68 lines
{
  "properties": {
    "displayName": "Auditing on SQL server should be enabled",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Auditing on your SQL Server should be enabled to track database activities across all databases on the server and save them in an audit log.",
    "metadata": {
      "version": "2.0.0",
      "category": "SQL"
    },
    "version": "2.0.0",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "AuditIfNotExists",
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      },
      "setting": {
        "type": "String",
        "metadata": {
          "displayName": "Desired Auditing setting"
        },
        "defaultValue": "enabled",
        "allowedValues": [
          "enabled",
          "disabled"
        ]
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Sql/servers"
          },
          {
            "field": "kind",
            "notContains": "analytics"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Sql/servers/auditingSettings",
          "name": "default",
          "existenceCondition": {
            "field": "Microsoft.Sql/auditingSettings.state",
            "equals": "[parameters('setting')]"
          }
        }
      }
    },
    "versions": [
      "2.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/a6fb4358-5bf4-4ad7-ba82-2cd2f41ce5e9",
  "name": "a6fb4358-5bf4-4ad7-ba82-2cd2f41ce5e9"
}
Bylawa6fb4358-5bf4-4ad7-ba82-2cd2f41ce5e9.bylaw.ps1 · 38 lines
# Converted from SqlServerAuditing_Audit.json by ConvertTo-Bylaw.
Policy 'a6fb4358-5bf4-4ad7-ba82-2cd2f41ce5e9' {

    Properties @{
        DisplayName   = 'Auditing on SQL server should be enabled'
        Description   = 'Auditing on your SQL Server should be enabled to track database activities across all databases on the server and save them in an audit log.'
        Version       = '2.0.0'
        Category      = 'SQL'
        PolicyVersion = '2.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter Setting @{
        AllowedValues = @('enabled', 'disabled')
        Default       = 'enabled'
        DisplayName   = 'Desired Auditing setting'
    }

    Resource 'Microsoft.Sql/servers' {

        Require 'kind contains analytics' {
            Field kind | Must -ContainText 'analytics'
        }
    }

    AuditIfNotExists 'Microsoft.Sql/servers/auditingSettings' -Name 'default' -Effect (Param Effect) {
        Require 'auditingSettings.state is the Setting parameter' {
            Field 'Microsoft.Sql/auditingSettings.state' | Must -Be (Param Setting)
        }
    }
}

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