Bylaw

Gallery › Security Center › Azure built-in policies

Configure SQL Virtual Machines to automatically install Microsoft Defender for SQL and DCR with a Log Analytics workspace

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.9.0

Microsoft Defender for SQL collects events from the agent and uses them to provide security alerts and tailored hardening tasks (recommendations). Create a resource group, a Data Collection Rule and Log Analytics workspace in the same region as the machine.

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

Azure Policy JSONMDC_DfSQL_AMA_DefaultPipeline_VM.json · 430 lines
{
  "properties": {
    "displayName": "Configure SQL Virtual Machines to automatically install Microsoft Defender for SQL and DCR with a Log Analytics workspace",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Microsoft Defender for SQL collects events from the agent and uses them to provide security alerts and tailored hardening tasks (recommendations). Create a resource group, a Data Collection Rule and Log Analytics workspace in the same region as the machine.",
    "version": "1.9.0",
    "metadata": {
      "version": "1.9.0",
      "category": "Security Center"
    },
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      },
      "enableCollectionOfSqlQueriesForSecurityResearch": {
        "type": "Boolean",
        "metadata": {
          "displayName": "Enable collection of SQL queries for security research",
          "description": "Enable or disable the collection of SQL queries for security research."
        },
        "allowedValues": [
          true,
          false
        ],
        "defaultValue": false
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.SqlVirtualMachine/SqlVirtualMachines"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "name": "[concat(last(split(field('Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId'), '/')), '/?')]",
          "evaluationDelay": "AfterProvisioning",
          "deploymentScope": "subscription",
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "existenceCondition": {
            "anyOf": [
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/virtualMachines/extensions/type",
                    "equals": "SqlIaaSAgentLinux"
                  },
                  {
                    "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
                    "equals": "Microsoft.SqlServer.Management"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/virtualMachines/extensions/type",
                    "equals": "AdvancedThreatProtection.Windows"
                  },
                  {
                    "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
                    "equals": "Microsoft.Azure.AzureDefenderForSQL"
                  },
                  {
                    "field": "Microsoft.Compute/virtualMachines/extensions/provisioningState",
                    "in": [
                      "Succeeded",
                      "Provisioning succeeded"
                    ]
                  }
                ]
              }
            ]
          },
          "deployment": {
            "location": "eastus",
            "properties": {
              "mode": "incremental",
              "parameters": {
                "resourceGroup": {
                  "value": "[resourceGroup().name]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "vmName": {
                  "value": "[last(split(field('Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId'), '/'))]"
                },
                "enableCollectionOfSqlQueriesForSecurityResearch": {
                  "value": "[parameters('enableCollectionOfSqlQueriesForSecurityResearch')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "resourceGroup": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "vmName": {
                    "type": "string"
                  },
                  "enableCollectionOfSqlQueriesForSecurityResearch": {
                    "type": "bool"
                  }
                },
                "variables": {
                  "locationLongNameToShortMap": {
                    "australiacentral": "CAU",
                    "australiaeast": "EAU",
                    "australiasoutheast": "SEAU",
                    "brazilsouth": "CQ",
                    "canadacentral": "CCA",
                    "canadaeast": "CCA",
                    "centralindia": "CIN",
                    "centralus": "CUS",
                    "eastasia": "EA",
                    "eastus2euap": "eus2p",
                    "eastus": "EUS",
                    "eastus2": "EUS2",
                    "francecentral": "PAR",
                    "germanywestcentral": "DEWC",
                    "japaneast": "EJP",
                    "jioindiawest": "CIN",
                    "koreacentral": "SE",
                    "koreasouth": "SE",
                    "northcentralus": "NCUS",
                    "northeurope": "NEU",
                    "norwayeast": "NOE",
                    "southafricanorth": "JNB",
                    "southcentralus": "SCUS",
                    "southeastasia": "SEA",
                    "southindia": "CIN",
                    "swedencentral": "SEC",
                    "switzerlandnorth": "CHN",
                    "switzerlandwest": "CHW",
                    "uaenorth": "DXB",
                    "uksouth": "SUK",
                    "ukwest": "WUK",
                    "westcentralus": "WCUS",
                    "westeurope": "WEU",
                    "westindia": "CIN",
                    "westus": "WUS",
                    "westus2": "WUS2"
                  },
                  "locationCode": "[if(contains(variables('locationLongNameToShortMap'), parameters('location')), variables('locationLongNameToShortMap')[parameters('location')], parameters('location'))]",
                  "subscriptionId": "[subscription().subscriptionId]",
                  "defaultRGName": "[concat('DefaultResourceGroup-', variables('locationCode'))]",
                  "defaultRGLocation": "[parameters('location')]",
                  "workspaceName": "[concat('D4SQL-', replace(subscription().subscriptionId, '-', ''), '-', parameters('location'))]",
                  "dcrName": "[concat('MicrosoftDefenderForSQL-', parameters('location'), '-dcr')]",
                  "dcrId": "[concat('/subscriptions/', variables('subscriptionId'), '/resourceGroups/', variables('defaultRGName'), '/providers/Microsoft.Insights/dataCollectionRules/', variables('dcrName'))]",
                  "dcraName": "[concat(parameters('vmName'),'/Microsoft.Insights/MicrosoftDefenderForSQL-RulesAssociation')]",
                  "deployDataCollectionRules": "[concat('deployDataCollectionRules-', uniqueString(deployment().name))]",
                  "deployDataCollectionRulesAssociation": "[concat('deployDataCollectionRulesAssociation-', uniqueString(deployment().name))]",
                  "deployDefenderForSQL": "[concat('deployDefenderForSQL-', uniqueString(deployment().name))]"
                },
                "resources": [
                  {
                    "type": "Microsoft.Resources/resourceGroups",
                    "name": "[variables('defaultRGName')]",
                    "apiVersion": "2022-09-01",
                    "location": "[variables('defaultRGLocation')]",
                    "tags": {
                      "createdBy": "MicrosoftDefenderForSQL"
                    }
                  },
                  {
                    "type": "Microsoft.Resources/deployments",
                    "name": "[variables('deployDataCollectionRules')]",
                    "apiVersion": "2022-09-01",
                    "resourceGroup": "[variables('defaultRGName')]",
                    "dependsOn": [
                      "[variables('defaultRGName')]"
                    ],
                    "properties": {
                      "mode": "Incremental",
                      "expressionEvaluationOptions": {
                        "scope": "inner"
                      },
                      "parameters": {
                        "defaultRGLocation": {
                          "value": "[variables('defaultRGLocation')]"
                        },
                        "workspaceName": {
                          "value": "[variables('workspaceName')]"
                        },
                        "dcrName": {
                          "value": "[variables('dcrName')]"
                        },
                        "enableCollectionOfSqlQueriesForSecurityResearch": {
                          "value": "[parameters('enableCollectionOfSqlQueriesForSecurityResearch')]"
                        }
                      },
                      "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {
                          "defaultRGLocation": {
                            "type": "string"
                          },
                          "workspaceName": {
                            "type": "string"
                          },
                          "dcrName": {
                            "type": "string"
                          },
                          "enableCollectionOfSqlQueriesForSecurityResearch": {
                            "type": "bool"
                          }
                        },
                        "resources": [
                          {
                            "type": "Microsoft.OperationalInsights/workspaces",
                            "name": "[parameters('workspaceName')]",
                            "apiVersion": "2022-10-01",
                            "location": "[parameters('defaultRGLocation')]",
                            "tags": {
                              "createdBy": "MicrosoftDefenderForSQL"
                            },
                            "properties": {
                              "sku": {
                                "name": "pergb2018"
                              },
                              "retentionInDays": 30
                            }
                          },
                          {
                            "type": "Microsoft.Insights/dataCollectionRules",
                            "name": "[parameters('dcrName')]",
                            "apiVersion": "2021-04-01",
                            "location": "[parameters('defaultRGLocation')]",
                            "tags": {
                              "createdBy": "MicrosoftDefenderForSQL"
                            },
                            "dependsOn": [
                              "[parameters('workspaceName')]"
                            ],
                            "properties": {
                              "description": "Data collection rule for Microsoft Defender for SQL. Deleting this rule will break the detection of security vulnerabilities.",
                              "dataSources": {
                                "extensions": [
                                  {
                                    "extensionName": "MicrosoftDefenderForSQL",
                                    "name": "MicrosoftDefenderForSQL",
                                    "streams": [
                                      "Microsoft-DefenderForSqlAlerts",
                                      "Microsoft-DefenderForSqlLogins",
                                      "Microsoft-DefenderForSqlTelemetry",
                                      "Microsoft-DefenderForSqlScanEvents",
                                      "Microsoft-DefenderForSqlScanResults",
                                      "Microsoft-SqlAtpStatus-DefenderForSql"
                                    ],
                                    "extensionSettings": {
                                      "enableCollectionOfSqlQueriesForSecurityResearch": "[parameters('enableCollectionOfSqlQueriesForSecurityResearch')]"
                                    }
                                  }
                                ]
                              },
                              "destinations": {
                                "logAnalytics": [
                                  {
                                    "workspaceResourceId": "[resourceId('Microsoft.OperationalInsights/workspaces/', parameters('workspaceName'))]",
                                    "name": "LogAnalyticsDest"
                                  }
                                ]
                              },
                              "dataFlows": [
                                {
                                  "streams": [
                                    "Microsoft-DefenderForSqlAlerts",
                                    "Microsoft-DefenderForSqlLogins",
                                    "Microsoft-DefenderForSqlTelemetry",
                                    "Microsoft-DefenderForSqlScanEvents",
                                    "Microsoft-DefenderForSqlScanResults",
                                    "Microsoft-SqlAtpStatus-DefenderForSql"
                                  ],
                                  "destinations": [
                                    "LogAnalyticsDest"
                                  ]
                                }
                              ]
                            }
                          }
                        ]
                      }
                    }
                  },
                  {
                    "type": "Microsoft.Resources/deployments",
                    "name": "[variables('deployDataCollectionRulesAssociation')]",
                    "apiVersion": "2022-09-01",
                    "resourceGroup": "[parameters('resourceGroup')]",
                    "dependsOn": [
                      "[variables('deployDataCollectionRules')]"
                    ],
                    "properties": {
                      "mode": "Incremental",
                      "expressionEvaluationOptions": {
                        "scope": "inner"
                      },
                      "parameters": {
                        "dcrId": {
                          "value": "[variables('dcrId')]"
                        },
                        "dcraName": {
                          "value": "[variables('dcraName')]"
                        }
                      },
                      "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {
                          "dcrId": {
                            "type": "string"
                          },
                          "dcraName": {
                            "type": "string"
                          }
                        },
                        "variables": {},
                        "resources": [
                          {
                            "type": "Microsoft.Compute/virtualMachines/providers/dataCollectionRuleAssociations",
                            "name": "[parameters('dcraName')]",
                            "apiVersion": "2021-04-01",
                            "properties": {
                              "description": "Configure association between SQL Virtual Machine and the Microsoft Defender for SQL DCR. Deleting this association will break the detection of security vulnerabilities for this SQL Virtual Machine.",
                              "dataCollectionRuleId": "[parameters('dcrId')]"
                            }
                          }
                        ]
                      }
                    }
                  },
                  {
                    "type": "Microsoft.Resources/deployments",
                    "name": "[variables('deployDefenderForSQL')]",
                    "apiVersion": "2022-09-01",
                    "resourceGroup": "[parameters('resourceGroup')]",
                    "dependsOn": [
                      "[variables('deployDataCollectionRulesAssociation')]"
                    ],
                    "properties": {
                      "mode": "Incremental",
                      "expressionEvaluationOptions": {
                        "scope": "inner"
                      },
                      "parameters": {
                        "location": {
                          "value": "[parameters('location')]"
                        },
                        "vmName": {
                          "value": "[parameters('vmName')]"
                        }
                      },
                      "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {
                          "location": {
                            "type": "string"
                          },
                          "vmName": {
                            "type": "string"
                          }
                        },
                        "variables": {},
                        "resources": [
                          {
                            "type": "Microsoft.Compute/virtualMachines/extensions",
                            "name": "[concat(parameters('vmName'), '/', 'MicrosoftDefenderForSQL')]",
                            "apiVersion": "2023-03-01",
                            "location": "[parameters('location')]",
                            "tags": {
                              "createdBy": "MicrosoftDefenderForSQL"
                            },
                            "properties": {
                              "publisher": "Microsoft.Azure.AzureDefenderForSQL",
                              "type": "AdvancedThreatProtection.Windows",
                              "typeHandlerVersion": "2.0",
                              "autoUpgradeMinorVersion": true,
                              "enableAutomaticUpgrade": true
                            }
                          }
                        ]
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.9.0",
      "1.8.0",
      "1.7.0",
      "1.6.0",
      "1.5.0",
      "1.4.0",
      "1.3.1",
      "1.3.0-PREVIEW"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/c859b78a-a128-4376-a838-e97ce6625d16",
  "name": "c859b78a-a128-4376-a838-e97ce6625d16"
}
Bylawc859b78a-a128-4376-a838-e97ce6625d16.bylaw.ps1 · 55 lines
# Converted from MDC_DfSQL_AMA_DefaultPipeline_VM.json by ConvertTo-Bylaw.
Policy 'c859b78a-a128-4376-a838-e97ce6625d16' {

    Properties @{
        DisplayName   = 'Configure SQL Virtual Machines to automatically install Microsoft Defender for SQL and DCR with a Log Analytics workspace'
        Description   = 'Microsoft Defender for SQL collects events from the agent and uses them to provide security alerts and tailored hardening tasks (recommendations). Create a resource group, a Data Collection Rule and Log Analytics workspace in the same region as the machine.'
        Version       = '1.9.0'
        Category      = 'Security Center'
        PolicyVersion = '1.9.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter EnableCollectionOfSqlQueriesForSecurityResearch @{
        Type          = 'Boolean'
        AllowedValues = @($true, $false)
        Default       = $false
        DisplayName   = 'Enable collection of SQL queries for security research'
        Description   = 'Enable or disable the collection of SQL queries for security research.'
    }

    Resource 'Microsoft.SqlVirtualMachine/SqlVirtualMachines'

    DeployIfNotExists 'Microsoft.Compute/virtualMachines/extensions' -Role 'Contributor' -Name (Arm '[concat(last(split(field(''Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId''), ''/'')), ''/?'')]') -EvaluationDelay AfterProvisioning -Effect (Param Effect) {
        Require 'type, publisher and provisioningState are as required' {
            AnyOf {
                AllOf {
                    Field 'Microsoft.Compute/virtualMachines/extensions/type' | Must -Be 'SqlIaaSAgentLinux'
                    Field publisher | Must -Be 'Microsoft.SqlServer.Management'
                }
                AllOf {
                    Field 'Microsoft.Compute/virtualMachines/extensions/type' | Must -Be 'AdvancedThreatProtection.Windows'
                    Field publisher | Must -Be 'Microsoft.Azure.AzureDefenderForSQL'
                    Field provisioningState | Must -BeIn @('Succeeded', 'Provisioning succeeded')
                }
            }
        }
        Deployment @{
            TemplateFile = './templates/c859b78a-a128-4376-a838-e97ce6625d16.json'
            Scope        = 'Subscription'
            Location     = 'eastus'
            Parameters   = [ordered]@{
                resourceGroup                                   = (Arm '[resourceGroup().name]')
                location                                        = (Arm '[field(''location'')]')
                vmName                                          = (Arm '[last(split(field(''Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId''), ''/''))]')
                enableCollectionOfSqlQueriesForSecurityResearch = (Param EnableCollectionOfSqlQueriesForSecurityResearch)
            }
        }
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/Security Center/MDC_DfSQL_AMA_DefaultPipeline_VM.json in Azure/azure-policy, under the MIT licence.