Created
September 25, 2025 13:29
-
-
Save VLoub/6ce0ad87bf16045bf453ba3e736e2abb to your computer and use it in GitHub Desktop.
Azure policy for creation of keyvault and key for SQL that need to be using CMK from its own keyvault. Just ensure that SQL server has private endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
| { | |
| "if": { | |
| "allOf": [ | |
| { | |
| "field": "type", | |
| "equals": "Microsoft.Sql/servers" | |
| }, | |
| { | |
| "value": "[subscription().displayName]", | |
| "contains": "-Secret-" | |
| }, | |
| { | |
| "value": "[resourcegroup().managedBy]", | |
| "notContains": "/providers/Microsoft.Synapse/" | |
| } | |
| ] | |
| }, | |
| "then": { | |
| "effect": "[parameters('effect')]", | |
| "details": { | |
| "type": "Microsoft.Sql/servers/encryptionProtector", | |
| "existenceCondition": { | |
| "allOf": [ | |
| { | |
| "field": "Microsoft.Sql/servers/encryptionProtector/serverKeyType", | |
| "equals": "AzureKeyVault" | |
| } | |
| ] | |
| }, | |
| "roleDefinitionIds": [ | |
| "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c", // Contributor | |
| "/providers/Microsoft.Authorization/roleDefinitions/18d7d88d-d35e-4fb5-a5c3-7773c20a72d9" // User Access Administrator | |
| // "/providers/Microsoft.Authorization/roleDefinitions/b12aa53e-6015-4669-85d0-8515ebb3ae7f" // Private DNS Zone Contributor | |
| ], | |
| "deployment": { | |
| "properties": { | |
| "mode": "incremental", | |
| "expressionEvaluationOptions": { | |
| "scope": "inner" | |
| }, | |
| "template": { | |
| "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", | |
| "contentVersion": "1.0.0.0", | |
| "parameters": { | |
| "sqlServerName": { | |
| "type": "string", | |
| "metadata": { | |
| "description": "Name of SQL Server" | |
| } | |
| }, | |
| "location": { | |
| "type": "string", | |
| "defaultValue": "[resourceGroup().location]" | |
| } | |
| }, | |
| "variables": { | |
| "kv_InstanceID": "[substring(uniqueString(parameters('sqlServerName')), 0, 1)]", | |
| "kv_Name": "[format('{0}-{1}', variables('kv_SolutionName'), variables('kv_InstanceID'))]", // 24 chars only | |
| "privateEndpointNameKv": "[format('{0}-{1}-{2}-{3}', variables('kv_Name'), variables('kv_InstanceID'), 'kv', 'pe')]" | |
| }, | |
| "resources": [ | |
| { | |
| "type": "Microsoft.KeyVault/vaults", | |
| "apiVersion": "2022-07-01", | |
| "name": "[variables('kv_Name')]", | |
| "location": "[parameters('location')]", | |
| "properties": { | |
| "sku": { | |
| "name": "premium", | |
| "family": "A" | |
| }, | |
| "enableRbacAuthorization": true, | |
| "enableSoftDelete": true, | |
| "enablePurgeProtection": true, | |
| "enabledForTemplateDeployment": true, | |
| "publicNetworkAccess": "disabled", | |
| "tenantId": "[tenant().tenantId]", | |
| "networkAcls": { | |
| "bypass": "AzureServices", | |
| "defaultAction": "Deny" | |
| } | |
| } | |
| }, | |
| { | |
| "type": "Microsoft.Authorization/roleAssignments", | |
| "apiVersion": "2022-04-01", | |
| "scope": "[format('Microsoft.KeyVault/vaults/{0}', variables('kv_Name'))]", | |
| "name": "[guid(subscription().id, resourceId('Microsoft.KeyVault/vaults', variables('kv_Name')), resourceId('Microsoft.Sql/servers', parameters('sqlServerName')))]", | |
| "properties": { | |
| "roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'e147488a-f6f5-4113-8e2d-b22465e65bf6')]", // Key Vault Crypto Service Encryption User | |
| "principalId": "[reference(resourceId('Microsoft.Sql/servers', parameters('sqlServerName')), '2021-11-01', 'full').identity.principalId]", | |
| "principalType": "ServicePrincipal" | |
| }, | |
| "dependsOn": [ | |
| "[resourceId('Microsoft.KeyVault/vaults', variables('kv_Name'))]" | |
| ] | |
| }, | |
| { | |
| "type": "Microsoft.KeyVault/vaults/keys", | |
| "apiVersion": "2021-10-01", | |
| "name": "[format('{0}/{1}', variables('kv_Name'), format('{0}-BYOK', parameters('sqlServerName')))]", | |
| "properties": { | |
| "attributes": { | |
| "enabled": true | |
| }, | |
| "keySize": 3072, | |
| "kty": "RSA-HSM" | |
| }, | |
| "dependsOn": [ | |
| "[resourceId('Microsoft.KeyVault/vaults', variables('kv_Name'))]" | |
| ] | |
| }, | |
| { | |
| "type": "Microsoft.Resources/deployments", | |
| "apiVersion": "2022-09-01", | |
| "name": "[format('{0}-kv-pe', deployment().name)]", | |
| "properties": { | |
| "expressionEvaluationOptions": { | |
| "scope": "inner" | |
| }, | |
| "mode": "Incremental", | |
| "parameters": { | |
| "location": { | |
| "value": "[parameters('location')]" | |
| }, | |
| "privateEndpointNameKv": { | |
| "value": "[variables('privateEndpointNameKv')]" | |
| }, | |
| "privateEndpointName": { | |
| "value": "[last(split(reference(resourceId('Microsoft.Sql/servers', parameters('sqlServerName')), '2021-11-01').privateEndpointConnections[0].properties.privateEndpoint.id, '/'))]" | |
| }, | |
| "keyVault_res_id": { | |
| "value": "[resourceId('Microsoft.KeyVault/vaults', variables('kv_Name'))]" | |
| } | |
| }, | |
| "template": { | |
| "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", | |
| "contentVersion": "1.0.0.0", | |
| "parameters": { | |
| "location": { | |
| "type": "string", | |
| "defaultValue": "[resourceGroup().location]" | |
| }, | |
| "privateEndpointNameKv": { | |
| "type": "string" | |
| }, | |
| "keyVault_res_id": { | |
| "type": "string" | |
| }, | |
| "privateEndpointName": { | |
| "type": "string" | |
| } | |
| }, | |
| "resources": [ | |
| { | |
| "type": "Microsoft.Network/privateEndpoints", | |
| "apiVersion": "2021-03-01", | |
| "name": "[parameters('privateEndpointNameKv')]", | |
| "location": "[parameters('location')]", | |
| "properties": { | |
| "subnet": { | |
| "id": "[reference(resourceId('Microsoft.Network/privateEndpoints', parameters('privateEndpointName')), '2021-03-01').subnet.id]" | |
| }, | |
| "privateLinkServiceConnections": [ | |
| { | |
| "name": "[format('{0}-connection', parameters('privateEndpointNameKv'))]", | |
| "properties": { | |
| "privateLinkServiceId": "[parameters('keyVault_res_id')]", | |
| "groupIds": [ | |
| "vault" | |
| ] | |
| } | |
| } | |
| ] | |
| } | |
| } | |
| ] | |
| } | |
| }, | |
| "dependsOn": [ | |
| "[resourceId('Microsoft.KeyVault/vaults', variables('kv_Name'))]" | |
| ] | |
| }, | |
| { | |
| "type": "Microsoft.Resources/deployments", | |
| "apiVersion": "2022-09-01", | |
| "name": "[format('{0}-sql-byok', deployment().name)]", | |
| "properties": { | |
| "expressionEvaluationOptions": { | |
| "scope": "inner" | |
| }, | |
| "mode": "Incremental", | |
| "parameters": { | |
| "sqlServerName": { | |
| "value": "[parameters('sqlServerName')]" | |
| }, | |
| "keyVaultName": { | |
| "value": "[variables('kv_Name')]" | |
| }, | |
| "keyName": { | |
| "value": "[format('{0}-BYOK', parameters('sqlServerName'))]" | |
| }, | |
| "keyVersion": { | |
| "value": "[last(split(reference(resourceId('Microsoft.KeyVault/vaults/keys', variables('kv_Name'), format('{0}-BYOK', parameters('sqlServerName'))), '2021-10-01').keyUriWithVersion, '/'))]" | |
| }, | |
| "keyUri": { | |
| "value": "[reference(resourceId('Microsoft.KeyVault/vaults/keys', variables('kv_Name'), format('{0}-BYOK', parameters('sqlServerName'))), '2021-10-01').keyUriWithVersion]" | |
| } | |
| }, | |
| "template": { | |
| "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", | |
| "contentVersion": "1.0.0.0", | |
| "parameters": { | |
| "sqlServerName": { | |
| "type": "string" | |
| }, | |
| "keyVaultName": { | |
| "type": "string" | |
| }, | |
| "keyName": { | |
| "type": "string" | |
| }, | |
| "keyVersion": { | |
| "type": "string" | |
| }, | |
| "keyUri": { | |
| "type": "string" | |
| } | |
| }, | |
| "resources": [ | |
| { | |
| "type": "Microsoft.Sql/servers/keys", | |
| "apiVersion": "2021-11-01", | |
| "name": "[format('{0}/{1}', parameters('sqlServerName'), format('{0}_{1}_{2}', parameters('keyVaultName'), parameters('keyName'), parameters('keyVersion')))]", | |
| "properties": { | |
| "serverKeyType": "AzureKeyVault", | |
| "uri": "[parameters('keyUri')]" | |
| } | |
| }, | |
| { | |
| "type": "Microsoft.Sql/servers/encryptionProtector", | |
| "apiVersion": "2021-11-01", | |
| "name": "[format('{0}/{1}', parameters('sqlServerName'), 'current')]", | |
| "properties": { | |
| "autoRotationEnabled": true, | |
| "serverKeyType": "AzureKeyVault", | |
| "serverKeyName": "[format('{0}_{1}_{2}', parameters('keyVaultName'), parameters('keyName'), parameters('keyVersion'))]" | |
| }, | |
| "dependsOn": [ | |
| "[resourceId('Microsoft.Sql/servers/keys', parameters('sqlServerName'), format('{0}_{1}_{2}', parameters('keyVaultName'), parameters('keyName'), parameters('keyVersion')))]" | |
| ] | |
| } | |
| ] | |
| } | |
| }, | |
| "dependsOn": [ | |
| "[resourceId('Microsoft.KeyVault/vaults/keys', variables('kv_Name'), format('{0}-BYOK', parameters('sqlServerName')))]", | |
| "[resourceId('Microsoft.KeyVault/vaults', variables('kv_Name'))]", | |
| "[resourceId('Microsoft.Resources/deployments', format('{0}-kv-pe', deployment().name))]", | |
| "[extensionResourceId(resourceId('Microsoft.KeyVault/vaults', variables('kv_Name')), 'Microsoft.Authorization/roleAssignments', guid(subscription().id, resourceId('Microsoft.KeyVault/vaults', variables('kv_Name')), resourceId('Microsoft.Sql/servers', parameters('sqlServerName'))))]" | |
| ] | |
| } | |
| ] | |
| }, | |
| "parameters": { | |
| "sqlServerName": { | |
| "value": "[field('name')]" | |
| }, | |
| "location": { | |
| "value": "[field('location')]" | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment