Bylaw

Gallery › SQL › Azure Landing Zones Library

Deploy SQL database auditing settings

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Deploy auditing settings to SQL Database when it not exist in the deployment

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

Azure Policy JSONDeploy-Sql-AuditingSettings.alz_policy_definition.json · 123 lines
{
  "name": "Deploy-Sql-AuditingSettings",
  "properties": {
    "description": "Deploy auditing settings to SQL Database when it not exist in the deployment",
    "displayName": "Deploy SQL database auditing settings",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureChinaCloud",
        "AzureUSGovernment"
      ],
      "category": "SQL",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "effect": {
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists",
        "metadata": {
          "description": "Enable or disable the execution of the policy",
          "displayName": "Effect"
        },
        "type": "String"
      }
    },
    "policyRule": {
      "if": {
        "equals": "Microsoft.Sql/servers/databases",
        "field": "type"
      },
      "then": {
        "details": {
          "deployment": {
            "properties": {
              "mode": "Incremental",
              "parameters": {
                "location": {
                  "value": "[field('location')]"
                },
                "sqlServerDataBaseName": {
                  "value": "[field('name')]"
                },
                "sqlServerName": {
                  "value": "[first(split(field('fullname'),'/'))]"
                }
              },
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "outputs": {},
                "parameters": {
                  "location": {
                    "type": "String"
                  },
                  "sqlServerDataBaseName": {
                    "type": "String"
                  },
                  "sqlServerName": {
                    "type": "String"
                  }
                },
                "resources": [
                  {
                    "apiVersion": "2017-03-01-preview",
                    "name": "[concat( parameters('sqlServerName'),'/',parameters('sqlServerDataBaseName'),'/default')]",
                    "properties": {
                      "auditActionsAndGroups": [
                        "BATCH_COMPLETED_GROUP",
                        "DATABASE_OBJECT_CHANGE_GROUP",
                        "SCHEMA_OBJECT_CHANGE_GROUP",
                        "BACKUP_RESTORE_GROUP",
                        "APPLICATION_ROLE_CHANGE_PASSWORD_GROUP",
                        "DATABASE_PRINCIPAL_CHANGE_GROUP",
                        "DATABASE_PRINCIPAL_IMPERSONATION_GROUP",
                        "DATABASE_ROLE_MEMBER_CHANGE_GROUP",
                        "USER_CHANGE_PASSWORD_GROUP",
                        "DATABASE_OBJECT_OWNERSHIP_CHANGE_GROUP",
                        "DATABASE_OBJECT_PERMISSION_CHANGE_GROUP",
                        "DATABASE_PERMISSION_CHANGE_GROUP",
                        "SCHEMA_OBJECT_PERMISSION_CHANGE_GROUP",
                        "SUCCESSFUL_DATABASE_AUTHENTICATION_GROUP",
                        "FAILED_DATABASE_AUTHENTICATION_GROUP"
                      ],
                      "isAzureMonitorTargetEnabled": true,
                      "state": "enabled"
                    },
                    "type": "Microsoft.Sql/servers/databases/auditingSettings"
                  }
                ],
                "variables": {}
              }
            }
          },
          "existenceCondition": {
            "allOf": [
              {
                "equals": "enabled",
                "field": "Microsoft.Sql/servers/databases/auditingSettings/state"
              },
              {
                "equals": "true",
                "field": "Microsoft.Sql/servers/databases/auditingSettings/isAzureMonitorTargetEnabled"
              }
            ]
          },
          "name": "default",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/056cd41c-7e88-42e1-933e-88ba6a50c9c3"
          ],
          "type": "Microsoft.Sql/servers/databases/auditingSettings"
        },
        "effect": "[parameters('effect')]"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawDeploy-Sql-AuditingSettings.bylaw.ps1 · 33 lines
# Converted from Deploy-Sql-AuditingSettings.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'Deploy-Sql-AuditingSettings' {

    Properties @{
        DisplayName = 'Deploy SQL database auditing settings'
        Description = 'Deploy auditing settings to SQL Database when it not exist in the deployment'
        Category    = 'SQL'
        Version     = '1.0.0'
        Mode        = 'Indexed'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureChinaCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource 'Microsoft.Sql/servers/databases'

    DeployIfNotExists 'Microsoft.Sql/servers/databases/auditingSettings' -Role 'SQL Security Manager' -Name 'default' -Effect (Param Effect) {
        Require 'state is enabled and isAzureMonitorTargetEnabled is true' {
            Field state | Must -Be 'enabled'
            Field isAzureMonitorTargetEnabled | Must -Be 'true'
        }
        Deployment -TemplateFile './templates/Deploy-Sql-AuditingSettings.json' -Parameters ([ordered]@{
                location              = (Arm '[field(''location'')]')
                sqlServerDataBaseName = (Arm '[field(''name'')]')
                sqlServerName         = (Arm '[first(split(field(''fullname''),''/''))]')
            })
    }
}

Its deployment templates are written beside it:

Source: policy_definitions/Deploy-Sql-AuditingSettings.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.