Bylaw

Gallery › Security Center › Azure built-in policies

Configure SQL Virtual Machines to automatically install Microsoft Defender for SQL

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.6.0

Configure Windows SQL Virtual Machines to automatically install the Microsoft Defender for SQL extension. Microsoft Defender for SQL collects events from the agent and uses them to provide security alerts and tailored hardening tasks (recommendations).

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

Azure Policy JSONMDC_DfSQL_DeployMicrosoftDefenderForSQLWindowsAgent_VM.json · 257 lines
{
  "properties": {
    "displayName": "Configure SQL Virtual Machines to automatically install Microsoft Defender for SQL",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Configure Windows SQL Virtual Machines to automatically install the Microsoft Defender for SQL extension. Microsoft Defender for SQL collects events from the agent and uses them to provide security alerts and tailored hardening tasks (recommendations).",
    "version": "1.6.0",
    "metadata": {
      "version": "1.6.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"
      },
      "workspaceRegion": {
        "type": "String",
        "metadata": {
          "displayName": "Workspace region",
          "description": "Region of the Log Analytics workspace destination for the Data Collection Rule.",
          "strongType": "location"
        },
        "defaultValue": ""
      },
      "userWorkspaceId": {
        "type": "String",
        "metadata": {
          "displayName": "Workspace Id",
          "description": "Workspace Id of the Log Analytics workspace destination for the Data Collection Rule."
        },
        "defaultValue": ""
      },
      "bringYourOwnDcr": {
        "type": "Boolean",
        "metadata": {
          "displayName": "Bring your own DCR",
          "description": "Enable or disable the use of a user-defined Data Collection Rule."
        },
        "allowedValues": [
          true,
          false
        ],
        "defaultValue": false
      },
      "dcrResourceId": {
        "type": "String",
        "metadata": {
          "displayName": "DCR Resource Id",
          "description": "Resource Id of the user-defined Data Collection Rule."
        },
        "defaultValue": ""
      }
    },
    "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",
          "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"
                    ]
                  }
                ]
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/749f88d5-cbae-40b8-bcfc-e573ddc772fa",
            "/providers/microsoft.authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "location": {
                  "value": "[field('location')]"
                },
                "vmName": {
                  "value": "[last(split(field('Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId'), '/'))]"
                },
                "workspaceRegion": {
                  "value": "[parameters('workspaceRegion')]"
                },
                "userWorkspaceId": {
                  "value": "[parameters('userWorkspaceId')]"
                },
                "bringYourOwnDcr": {
                  "value": "[parameters('bringYourOwnDcr')]"
                },
                "dcrResourceId": {
                  "value": "[parameters('dcrResourceId')]"
                }
              },
              "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"
                  },
                  "workspaceRegion": {
                    "type": "string"
                  },
                  "userWorkspaceId": {
                    "type": "string"
                  },
                  "bringYourOwnDcr": {
                    "type": "bool"
                  },
                  "dcrResourceId": {
                    "type": "string"
                  }
                },
                "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"
                  },
                  "actualLocation": "[if(empty(parameters('workspaceRegion')), parameters('location'), parameters('workspaceRegion'))]",
                  "locationCode": "[if(contains(variables('locationLongNameToShortMap'), variables('actualLocation')), variables('locationLongNameToShortMap')[variables('actualLocation')], variables('actualLocation'))]",
                  "subscriptionId": "[subscription().subscriptionId]",
                  "defaultRGName": "[concat('DefaultResourceGroup-', variables('locationCode'))]",
                  "dcrName": "[if(empty(parameters('userWorkspaceId')), concat('MicrosoftDefenderForSQL-', variables('actualLocation'), '-dcr'), concat('D4SQL-', replace(parameters('userWorkspaceId'), '-', ''), '-dcr'))]",
                  "dcrId": "[if(parameters('bringYourOwnDcr'), parameters('dcrResourceId'), concat('/subscriptions/', variables('subscriptionId'), '/resourceGroups/', variables('defaultRGName'), '/providers/Microsoft.Insights/dataCollectionRules/', variables('dcrName')))]",
                  "dcraName": "[concat(parameters('vmName'),'/Microsoft.Insights/MicrosoftDefenderForSQL-RulesAssociation')]"
                },
                "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
                    },
                    "dependsOn": [
                      "[extensionResourceId(concat('/subscriptions/', variables('subscriptionId'), '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Compute/virtualMachines/', parameters('vmName')), 'Microsoft.Insights/dataCollectionRuleAssociations','MicrosoftDefenderForSQL-RulesAssociation')]"
                    ]
                  },
                  {
                    "type": "Microsoft.Compute/virtualMachines/providers/dataCollectionRuleAssociations",
                    "name": "[variables('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": "[variables('dcrId')]"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.6.0",
      "1.5.0",
      "1.4.0",
      "1.3.0",
      "1.2.1",
      "1.2.0-PREVIEW"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/ddca0ddc-4e9d-4bbb-92a1-f7c4dd7ef7ce",
  "name": "ddca0ddc-4e9d-4bbb-92a1-f7c4dd7ef7ce"
}
Bylawddca0ddc-4e9d-4bbb-92a1-f7c4dd7ef7ce.bylaw.ps1 · 68 lines
# Converted from MDC_DfSQL_DeployMicrosoftDefenderForSQLWindowsAgent_VM.json by ConvertTo-Bylaw.
Policy 'ddca0ddc-4e9d-4bbb-92a1-f7c4dd7ef7ce' {

    Properties @{
        DisplayName   = 'Configure SQL Virtual Machines to automatically install Microsoft Defender for SQL'
        Description   = 'Configure Windows SQL Virtual Machines to automatically install the Microsoft Defender for SQL extension. Microsoft Defender for SQL collects events from the agent and uses them to provide security alerts and tailored hardening tasks (recommendations).'
        Version       = '1.6.0'
        Category      = 'Security Center'
        PolicyVersion = '1.6.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter WorkspaceRegion @{
        Default     = ''
        DisplayName = 'Workspace region'
        Description = 'Region of the Log Analytics workspace destination for the Data Collection Rule.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter UserWorkspaceId @{
        Default     = ''
        DisplayName = 'Workspace Id'
        Description = 'Workspace Id of the Log Analytics workspace destination for the Data Collection Rule.'
    }
    Parameter BringYourOwnDcr @{
        Type          = 'Boolean'
        AllowedValues = @($true, $false)
        Default       = $false
        DisplayName   = 'Bring your own DCR'
        Description   = 'Enable or disable the use of a user-defined Data Collection Rule.'
    }
    Parameter DcrResourceId @{
        Default     = ''
        DisplayName = 'DCR Resource Id'
        Description = 'Resource Id of the user-defined Data Collection Rule.'
    }

    Resource 'Microsoft.SqlVirtualMachine/SqlVirtualMachines'

    DeployIfNotExists 'Microsoft.Compute/virtualMachines/extensions' -Role 'Monitoring Contributor', 'Log Analytics 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/ddca0ddc-4e9d-4bbb-92a1-f7c4dd7ef7ce.json' -Parameters ([ordered]@{
                location        = (Arm '[field(''location'')]')
                vmName          = (Arm '[last(split(field(''Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId''), ''/''))]')
                workspaceRegion = (Param WorkspaceRegion)
                userWorkspaceId = (Param UserWorkspaceId)
                bringYourOwnDcr = (Param BringYourOwnDcr)
                dcrResourceId   = (Param DcrResourceId)
            })
    }
}

Its deployment templates are written beside it:

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