Gallery › SQL › Azure built-in policies
Configure Azure SQL database servers diagnostic settings to Log Analytics workspace
Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.0.2
Enables auditing logs for Azure SQL Database server and stream the logs to a Log Analytics workspace when any SQL Server which is missing this auditing is created or updated
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONDataConnectosSqServerLogs.json · 123 lines
{
"properties": {
"mode": "Indexed",
"displayName": "Configure Azure SQL database servers diagnostic settings to Log Analytics workspace",
"description": "Enables auditing logs for Azure SQL Database server and stream the logs to a Log Analytics workspace when any SQL Server which is missing this auditing is created or updated",
"policyType": "BuiltIn",
"metadata": {
"version": "1.0.2",
"category": "SQL"
},
"version": "1.0.2",
"parameters": {
"logAnalyticsWorkspaceId": {
"type": "String",
"metadata": {
"displayName": "Log Analytics workspace",
"description": "Specify the Log Analytics workspace the server should be connected to.",
"strongType": "omsWorkspace",
"assignPermissions": true
}
},
"effect": {
"type": "string",
"defaultValue": "DeployIfNotExists",
"allowedValues": [
"DeployIfNotExists",
"Disabled"
],
"metadata": {
"displayName": "Effect",
"description": "Enable or disable the execution of the policy"
}
}
},
"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/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
],
"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"
},
"logAnalyticsWorkspaceId": {
"type": "string"
}
},
"variables": {
"diagnosticSettingsName": "SQLSecurityAuditEvents_3d229c42-c7e7-4c97-9a99-ec0d0d8b86c1"
},
"resources": [
{
"type": "Microsoft.Sql/servers/databases/providers/diagnosticSettings",
"name": "[concat(parameters('serverName'),'/master/microsoft.insights/',variables('diagnosticSettingsName'))]",
"apiVersion": "2017-05-01-preview",
"properties": {
"name": "[variables('diagnosticSettingsName')]",
"workspaceId": "[parameters('logAnalyticsWorkspaceId')]",
"logs": [
{
"category": "SQLSecurityAuditEvents",
"enabled": true,
"retentionPolicy": {
"days": 0,
"enabled": false
}
}
]
}
},
{
"name": "[concat(parameters('serverName'), '/Default')]",
"type": "Microsoft.Sql/servers/auditingSettings",
"apiVersion": "2017-03-01-preview",
"dependsOn": [
"[concat('Microsoft.Sql/servers/', parameters('serverName'),'/databases/master/providers/microsoft.insights/diagnosticSettings/', variables('diagnosticSettingsName'))]"
],
"properties": {
"state": "Enabled",
"isAzureMonitorTargetEnabled": true
}
}
]
},
"parameters": {
"serverName": {
"value": "[field('name')]"
},
"logAnalyticsWorkspaceId": {
"value": "[parameters('logAnalyticsWorkspaceId')]"
}
}
}
}
}
}
},
"versions": [
"1.0.2"
]
},
"id": "/providers/Microsoft.Authorization/policyDefinitions/7ea8a143-05e3-4553-abfe-f56bef8b0b70",
"name": "7ea8a143-05e3-4553-abfe-f56bef8b0b70"
}Bylaw7ea8a143-05e3-4553-abfe-f56bef8b0b70.bylaw.ps1 · 38 lines
# Converted from DataConnectosSqServerLogs.json by ConvertTo-Bylaw.
Policy '7ea8a143-05e3-4553-abfe-f56bef8b0b70' {
Properties @{
DisplayName = 'Configure Azure SQL database servers diagnostic settings to Log Analytics workspace'
Description = 'Enables auditing logs for Azure SQL Database server and stream the logs to a Log Analytics workspace when any SQL Server which is missing this auditing is created or updated'
Version = '1.0.2'
Category = 'SQL'
PolicyVersion = '1.0.2'
Mode = 'Indexed'
}
Parameter LogAnalyticsWorkspaceId @{
Type = 'String'
DisplayName = 'Log Analytics workspace'
Description = 'Specify the Log Analytics workspace the server should be connected to.'
Metadata = [ordered]@{ strongType = 'omsWorkspace'; assignPermissions = $true }
}
Parameter Effect @{
Type = 'string'
AllowedValues = @('DeployIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'Enable or disable the execution of the policy'
}
Resource 'Microsoft.Sql/servers'
DeployIfNotExists 'Microsoft.Sql/servers/auditingSettings' -Role 'SQL Security Manager', 'Log Analytics Contributor' -Name 'Default' -Effect (Param Effect) {
Require 'auditingSettings.state is Enabled' {
Field 'Microsoft.Sql/auditingSettings.state' | Must -Be 'Enabled'
}
Deployment -TemplateFile './templates/7ea8a143-05e3-4553-abfe-f56bef8b0b70.json' -Parameters ([ordered]@{
serverName = (Arm '[field(''name'')]')
logAnalyticsWorkspaceId = (Param LogAnalyticsWorkspaceId)
})
}
}Its deployment templates are written beside it:
templates/7ea8a143-05e3-4553-abfe-f56bef8b0b70.json
Source: built-in-policies/policyDefinitions/SQL/DataConnectosSqServerLogs.json in Azure/azure-policy, under the MIT licence.
Bylaw