Bylaw

Gallery › SQL › Azure built-in policies

Deploy SQL DB transparent data encryption

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 2.2.0

Enables transparent data encryption on SQL databases

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

Azure Policy JSONSqlDBEncryption_DINE.json · 97 lines
{
  "properties": {
    "displayName": "Deploy SQL DB transparent data encryption",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Enables transparent data encryption on SQL databases",
    "metadata": {
      "version": "2.2.0",
      "category": "SQL"
    },
    "version": "2.2.0",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "DeployIfNotExists",
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Sql/servers/databases"
          },
          {
            "field": "name",
            "notEquals": "master"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Sql/servers/databases/transparentDataEncryption",
          "name": "current",
          "existenceCondition": {
            "anyOf": [
              {
                "field": "Microsoft.Sql/transparentDataEncryption.status",
                "equals": "enabled"
              },
              {
                "field": "Microsoft.Sql/servers/databases/transparentDataEncryption/state",
                "equals": "enabled"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/9b7fa17d-e63e-47b0-bb0a-15c516ac86ec"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "fullDbName": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "name": "[concat(parameters('fullDbName'), '/current')]",
                    "type": "Microsoft.Sql/servers/databases/transparentDataEncryption",
                    "apiVersion": "2022-05-01-preview",
                    "properties": {
                      "state": "Enabled"
                    }
                  }
                ]
              },
              "parameters": {
                "fullDbName": {
                  "value": "[field('fullName')]"
                }
              }
            }
          }
        }
      }
    },
    "versions": [
      "2.2.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/86a912f6-9a06-4e26-b447-11b16ba8659f",
  "name": "86a912f6-9a06-4e26-b447-11b16ba8659f"
}
Bylaw86a912f6-9a06-4e26-b447-11b16ba8659f.bylaw.ps1 · 39 lines
# Converted from SqlDBEncryption_DINE.json by ConvertTo-Bylaw.
Policy '86a912f6-9a06-4e26-b447-11b16ba8659f' {

    Properties @{
        DisplayName   = 'Deploy SQL DB transparent data encryption'
        Description   = 'Enables transparent data encryption on SQL databases'
        Version       = '2.2.0'
        Category      = 'SQL'
        PolicyVersion = '2.2.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource 'Microsoft.Sql/servers/databases' {

        Require 'name is master' {
            Field name | Must -Be 'master'
        }
    }

    DeployIfNotExists 'Microsoft.Sql/servers/databases/transparentDataEncryption' -Role 'SQL DB Contributor' -Name 'current' -Effect (Param Effect) {
        Require 'transparentDataEncryption.status and state are as required' {
            AnyOf {
                Field 'Microsoft.Sql/transparentDataEncryption.status' | Must -Be 'enabled'
                Field state | Must -Be 'enabled'
            }
        }
        Deployment -TemplateFile './templates/86a912f6-9a06-4e26-b447-11b16ba8659f.json' -Parameters ([ordered]@{
                fullDbName = (Arm '[field(''fullName'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/SQL/SqlDBEncryption_DINE.json in Azure/azure-policy, under the MIT licence.