Gallery › SQL › Azure Community Policy
Deploy Auditing on SQL servers
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Version 1.0.0
This policy ensures that Auditing is enabled on SQL Servers for enhanced security and compliance. It will automatically create a storage account in the same region as the SQL server to store audit records.
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONazurepolicy.json · 146 lines
{
"properties": {
"displayName": "Deploy Auditing on SQL servers",
"description": "This policy ensures that Auditing is enabled on SQL Servers for enhanced security and compliance. It will automatically create a storage account in the same region as the SQL server to store audit records.",
"metadata": {
"version": "1.0.0",
"category": "SQL"
},
"parameters": {
"effect": {
"type": "String",
"metadata": {
"displayName": "Effect",
"description": "Enable or disable the execution of the policy"
},
"allowedValues": [
"AuditIfNotExists",
"DeployIfNotExists",
"Disabled"
],
"defaultValue": "DeployIfNotExists"
},
"retentionDays": {
"type": "string",
"defaultValue": "180",
"metadata": {
"description": "The value in days of the retention period (0 indicates unlimited retention)",
"displayName": "Retention days (optional, 180 days if unspecified)"
}
},
"storageAccountsResourceGroup": {
"type": "string",
"metadata": {
"displayName": "Resource group name for storage accounts",
"description": "Auditing writes database events to an audit log in your Azure Storage account (a storage account will be created in each region where a SQL Server is created that will be shared by all servers in that region). Important - for proper operation of Auditing do not delete or rename the resource group or the storage accounts.",
"strongType": "existingResourceGroups"
}
}
},
"policyRule": {
"if": {
"field": "type",
"equals": "Microsoft.Sql/servers"
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.Sql/servers/auditingSettings",
"name": "Default",
"existenceCondition": {
"field": "Microsoft.Sql/auditingSettings.state",
"equals": "Enabled"
},
"roleDefinitionIds": [
"/providers/microsoft.authorization/roleDefinitions/056cd41c-7e88-42e1-933e-88ba6a50c9c3",
"/providers/microsoft.authorization/roleDefinitions/17d1049b-9a84-46fb-8f53-869881c3d3ab"
],
"deployment": {
"properties": {
"mode": "incremental",
"template": {
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"serverName": {
"type": "string"
},
"auditRetentionDays": {
"type": "string"
},
"storageAccountsResourceGroup": {
"type": "string"
},
"location": {
"type": "string"
}
},
"variables": {
"retentionDays": "[int(parameters('auditRetentionDays'))]",
"subscriptionId": "[subscription().subscriptionId]",
"uniqueStorage": "[uniqueString(variables('subscriptionId'), parameters('location'), parameters('storageAccountsResourceGroup'))]",
"locationCode": "[substring(parameters('location'), 0, 3)]",
"storageName": "[tolower(concat('sqlaudit', variables('locationCode'), variables('uniqueStorage')))]",
"createStorageAccountDeploymentName": "[concat('sqlServerAuditingStorageAccount-', uniqueString(variables('locationCode'), parameters('serverName')))]"
},
"resources": [
{
"apiVersion": "2017-05-10",
"name": "[variables('createStorageAccountDeploymentName')]",
"type": "Microsoft.Resources/deployments",
"resourceGroup": "[parameters('storageAccountsResourceGroup')]",
"properties": {
"mode": "Incremental",
"parameters": {
"location": {
"value": "[parameters('location')]"
},
"storageName": {
"value": "[variables('storageName')]"
}
},
"templateLink": {
"uri": "https://raw.githubusercontent.com/Azure/azure-policy/master/samples/SQL/deploy-sql-server-auditing/createStorage.template.json",
"contentVersion": "1.0.0.0"
}
}
},
{
"name": "[concat(parameters('serverName'), '/Default')]",
"type": "Microsoft.Sql/servers/auditingSettings",
"apiVersion": "2017-03-01-preview",
"properties": {
"state": "Enabled",
"storageEndpoint": "[reference(variables('createStorageAccountDeploymentName')).outputs.storageAccountEndPoint.value]",
"storageAccountAccessKey": "[reference(variables('createStorageAccountDeploymentName')).outputs.storageAccountKey.value]",
"retentionDays": "[variables('retentionDays')]",
"auditActionsAndGroups": null,
"storageAccountSubscriptionId": "[subscription().subscriptionId]",
"isStorageSecondaryKeyInUse": false
}
}
]
},
"parameters": {
"serverName": {
"value": "[field('name')]"
},
"auditRetentionDays": {
"value": "[parameters('retentionDays')]"
},
"storageAccountsResourceGroup": {
"value": "[parameters('storageAccountsResourceGroup')]"
},
"location": {
"value": "[field('location')]"
}
}
}
}
}
}
}
},
"type": "Microsoft.Authorization/policyDefinitions",
"name": "a33518a2-8f6e-4a16-8a38-0481c006ef55"
}Bylawa33518a2-8f6e-4a16-8a38-0481c006ef55.bylaw.ps1 · 44 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'a33518a2-8f6e-4a16-8a38-0481c006ef55' {
Properties @{
DisplayName = 'Deploy Auditing on SQL servers'
Description = 'This policy ensures that Auditing is enabled on SQL Servers for enhanced security and compliance. It will automatically create a storage account in the same region as the SQL server to store audit records.'
Version = '1.0.0'
Category = 'SQL'
Mode = 'Indexed'
}
Parameter Effect @{
AllowedValues = @('AuditIfNotExists', 'DeployIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'Enable or disable the execution of the policy'
}
Parameter RetentionDays @{
Type = 'string'
Default = '180'
DisplayName = 'Retention days (optional, 180 days if unspecified)'
Description = 'The value in days of the retention period (0 indicates unlimited retention)'
}
Parameter StorageAccountsResourceGroup @{
Type = 'string'
DisplayName = 'Resource group name for storage accounts'
Description = 'Auditing writes database events to an audit log in your Azure Storage account (a storage account will be created in each region where a SQL Server is created that will be shared by all servers in that region). Important - for proper operation of Auditing do not delete or rename the resource group or the storage accounts.'
Metadata = [ordered]@{ strongType = 'existingResourceGroups' }
}
Resource 'Microsoft.Sql/servers'
DeployIfNotExists 'Microsoft.Sql/servers/auditingSettings' -Role 'SQL Security Manager', 'Storage Account Contributor' -Name 'Default' -Effect (Param Effect) {
Require 'auditingSettings.state is Enabled' {
Field 'Microsoft.Sql/auditingSettings.state' | Must -Be 'Enabled'
}
Deployment -TemplateFile './templates/a33518a2-8f6e-4a16-8a38-0481c006ef55.json' -Parameters ([ordered]@{
serverName = (Arm '[field(''name'')]')
auditRetentionDays = (Param RetentionDays)
storageAccountsResourceGroup = (Param StorageAccountsResourceGroup)
location = (Arm '[field(''location'')]')
})
}
}Its deployment templates are written beside it:
templates/a33518a2-8f6e-4a16-8a38-0481c006ef55.json
Source: policyDefinitions/SQL/deploy-sql-server-auditing/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw