Bylaw

Gallery › SQL › Azure Community Policy

Deploy Advanced Data Security on SQL servers - Exempt resources with a matching tag key-value

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

This Policy will deploy Advanced Data Security to all Azure SQL servers unless they have a tag key/value that you define in this Policy. This allows you to exempt certain Storage Accounts from this Policy by leveraging Tags.

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

Azure Policy JSONazurepolicy.json · 134 lines
{
  "name": "4dfaf0b4-1c90-4a17-b190-488f8c9e063e",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy Advanced Data Security on SQL servers - Exempt resources with a matching tag key-value",
    "description": "This Policy will deploy Advanced Data Security to all Azure SQL servers unless they have a tag key/value that you define in this Policy. This allows you to exempt certain Storage Accounts from this Policy by leveraging Tags.",
    "metadata": {
      "version": "1.0.0",
      "category": "SQL"
    },
    "mode": "All",
    "parameters": {
      "tagName": {
        "type": "String"
      },
      "tagValue": {
        "type": "String"
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Sql/servers"
          },
          {
            "field": "[concat('tags[', parameters('tagName'), ']')]",
            "notEquals": "[parameters('tagValue')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Sql/servers/securityAlertPolicies",
          "name": "Default",
          "existenceCondition": {
            "field": "Microsoft.Sql/securityAlertPolicies.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"
                  },
                  "location": {
                    "type": "string"
                  }
                },
                "variables": {
                  "serverResourceGroupName": "[resourceGroup().name]",
                  "subscriptionId": "[subscription().subscriptionId]",
                  "uniqueStorage": "[uniqueString(variables('subscriptionId'), variables('serverResourceGroupName'), parameters('location'))]",
                  "storageName": "[tolower(concat('sqlva', variables('uniqueStorage')))]"
                },
                "resources": [
                  {
                    "type": "Microsoft.Storage/storageAccounts",
                    "name": "[variables('storageName')]",
                    "apiVersion": "2019-04-01",
                    "location": "[parameters('location')]",
                    "sku": {
                      "name": "Standard_LRS"
                    },
                    "kind": "StorageV2",
                    "properties": {}
                  },
                  {
                    "name": "[concat(parameters('serverName'), '/Default')]",
                    "type": "Microsoft.Sql/servers/securityAlertPolicies",
                    "apiVersion": "2017-03-01-preview",
                    "properties": {
                      "state": "Enabled",
                      "emailAccountAdmins": true
                    }
                  },
                  {
                    "name": "[concat(parameters('serverName'), '/Default')]",
                    "type": "Microsoft.Sql/servers/vulnerabilityAssessments",
                    "apiVersion": "2018-06-01-preview",
                    "properties": {
                      "storageContainerPath": "[concat(reference(resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))).primaryEndpoints.blob, 'vulnerability-assessment')]",
                      "storageAccountAccessKey": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageName')), '2018-02-01').keys[0].value]",
                      "recurringScans": {
                        "isEnabled": true,
                        "emailSubscriptionAdmins": true,
                        "emails": []
                      }
                    },
                    "dependsOn": [
                      "[concat('Microsoft.Storage/storageAccounts/', variables('storageName'))]",
                      "[concat('Microsoft.Sql/servers/', parameters('serverName'), '/securityAlertPolicies/Default')]"
                    ]
                  }
                ]
              },
              "parameters": {
                "serverName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw4dfaf0b4-1c90-4a17-b190-488f8c9e063e.bylaw.ps1 · 41 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '4dfaf0b4-1c90-4a17-b190-488f8c9e063e' {

    Properties @{
        DisplayName = 'Deploy Advanced Data Security on SQL servers - Exempt resources with a matching tag key-value'
        Description = 'This Policy will deploy Advanced Data Security to all Azure SQL servers unless they have a tag key/value that you define in this Policy. This allows you to exempt certain Storage Accounts from this Policy by leveraging Tags.'
        Version     = '1.0.0'
        Category    = 'SQL'
        Mode        = 'All'
    }

    Parameter TagName @{
        Type = 'String'
    }
    Parameter TagValue @{
        Type = 'String'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Sql/servers' {

        Require 'tag (Param TagName) is the TagValue parameter' {
            Tag (Param TagName) | Must -Be (Param TagValue)
        }
    }

    DeployIfNotExists 'Microsoft.Sql/servers/securityAlertPolicies' -Role 'SQL Security Manager', 'Storage Account Contributor' -Name 'Default' -Effect (Param Effect) {
        Require 'securityAlertPolicies.state is Enabled' {
            Field 'Microsoft.Sql/securityAlertPolicies.state' | Must -Be 'Enabled'
        }
        Deployment -TemplateFile './templates/4dfaf0b4-1c90-4a17-b190-488f8c9e063e.json' -Parameters ([ordered]@{
                serverName = (Arm '[field(''name'')]')
                location   = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/SQL/deploy-advanced-data-security-on-sql-servers-exempt-resources-with-a-matching-tag-key-value/azurepolicy.json in Azure/Community-Policy, under the MIT licence.