Gallery › Security Center › Azure built-in policies
Create and assign a built-in user-assigned managed identity
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.8.0
Create and assign a built-in user-assigned managed identity at scale to SQL virtual machines.
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONMDC_DfSQL_AddUserAssignedIdentity_VM.json · 343 lines
{
"properties": {
"displayName": "Create and assign a built-in user-assigned managed identity",
"policyType": "BuiltIn",
"mode": "Indexed",
"description": "Create and assign a built-in user-assigned managed identity at scale to SQL virtual machines.",
"version": "1.8.0",
"metadata": {
"version": "1.8.0",
"category": "Security Center"
},
"parameters": {
"bringYourOwnUserAssignedManagedIdentity": {
"type": "Boolean",
"metadata": {
"displayName": "Bring your own User-Assigned Managed Identity",
"description": "Enable this to use your own user-assigned managed identity. The pre-created identity MUST exist otherwise the policy deployment will fail. If enabled, ensure that the user-assigned managed identity resource ID parameter matches the pre-created user-assigned managed identity resource ID. If not enabled, the policy will create a new user-assigned managed identitiy per subscription, in a new resource group named 'Built-In-Identity-RG'."
},
"allowedValues": [
true,
false
],
"defaultValue": false
},
"userAssignedIdentityResourceId": {
"type": "String",
"metadata": {
"displayName": "User-Assigned Managed Identity Resource ID",
"description": "The resource ID of the pre-created user-assigned managed identity. This parameter is only used when bringYourOwnUserAssignedManagedIdentity is set to true"
},
"defaultValue": ""
},
"builtInIdentityResourceGroupLocation": {
"type": "String",
"metadata": {
"displayName": "Built-In-Identity-RG Location",
"description": "The location of the resource group 'Built-In-Identity-RG' created by the policy."
},
"defaultValue": "eastus"
},
"effect": {
"type": "String",
"metadata": {
"displayName": "Policy Effect",
"description": "The effect determines what happens when the policy rule is evaluated to match."
},
"allowedValues": [
"AuditIfNotExists",
"DeployIfNotExists",
"Disabled"
],
"defaultValue": "DeployIfNotExists"
}
},
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.SqlVirtualMachine/SqlVirtualMachines"
}
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.Compute/virtualMachines",
"name": "[last(split(field('Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId'), '/'))]",
"evaluationDelay": "AfterProvisioning",
"deploymentScope": "subscription",
"existenceCondition": {
"anyOf": [
{
"field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType",
"like": "Linux*"
},
{
"allOf": [
{
"field": "identity.type",
"contains": "UserAssigned"
},
{
"field": "identity.userAssignedIdentities",
"containsKey": "[if(parameters('bringYourOwnUserAssignedManagedIdentity'), parameters('userAssignedIdentityResourceId'), concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/Built-In-Identity-RG/providers/Microsoft.ManagedIdentity/userAssignedIdentities/Built-In-Identity-', field('location')))]"
}
]
},
{
"allOf": [
{
"field": "identity.type",
"equals": "UserAssigned"
},
{
"value": "[string(length(field('identity.userAssignedIdentities')))]",
"equals": "1"
}
]
}
]
},
"roleDefinitionIds": [
"/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
],
"deployment": {
"location": "eastus",
"properties": {
"mode": "incremental",
"parameters": {
"location": {
"value": "[field('location')]"
},
"bringYourOwnUserAssignedManagedIdentity": {
"value": "[parameters('bringYourOwnUserAssignedManagedIdentity')]"
},
"userAssignedIdentityResourceId": {
"value": "[parameters('userAssignedIdentityResourceId')]"
},
"builtInIdentityResourceGroupLocation": {
"value": "[parameters('builtInIdentityResourceGroupLocation')]"
},
"vmName": {
"value": "[last(split(field('Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId'), '/'))]"
},
"vmResourceGroup": {
"value": "[resourceGroup().name]"
},
"resourceId": {
"value": "[field('Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId')]"
}
},
"template": {
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.1",
"parameters": {
"location": {
"type": "string"
},
"bringYourOwnUserAssignedManagedIdentity": {
"type": "bool"
},
"userAssignedIdentityResourceId": {
"type": "string"
},
"builtInIdentityResourceGroupLocation": {
"type": "string"
},
"vmName": {
"type": "string"
},
"vmResourceGroup": {
"type": "string"
},
"resourceId": {
"type": "string"
}
},
"variables": {
"userAssignedName": "Built-In-Identity",
"identityResourceGroup": "Built-In-Identity-RG",
"userAssignedNameWithLocation": "[concat(variables('userAssignedName'),'-', parameters('location'))]",
"autoCreatedUaId": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', trim(variables('identityResourceGroup')), '/providers/Microsoft.ManagedIdentity/userAssignedIdentities/', trim(variables('userAssignedName')), '-', parameters('location'))]",
"deployUserAssignedName": "[concat('deployUserAssignedIdentity-', uniqueString(deployment().name))]",
"deployGetResourceProperties": "[concat('deployGetResourceProperties-', uniqueString(deployment().name))]",
"deployAssignUserAssignedName": "[concat('deployAssignUA-', uniqueString(deployment().name))]"
},
"resources": [
{
"condition": "[not(parameters('bringYourOwnUserAssignedManagedIdentity'))]",
"type": "Microsoft.Resources/resourceGroups",
"apiVersion": "2022-09-01",
"name": "[variables('identityResourceGroup')]",
"location": "[parameters('builtInIdentityResourceGroupLocation')]",
"tags": {
"createdBy": "MicrosoftDefenderForSQL"
}
},
{
"condition": "[not(parameters('bringYourOwnUserAssignedManagedIdentity'))]",
"type": "Microsoft.Resources/deployments",
"apiVersion": "2022-09-01",
"name": "[variables('deployUserAssignedName')]",
"resourceGroup": "[variables('identityResourceGroup')]",
"dependsOn": [
"[resourceId('Microsoft.Resources/resourceGroups',variables('identityResourceGroup'))]"
],
"properties": {
"mode": "Incremental",
"expressionEvaluationOptions": {
"scope": "inner"
},
"parameters": {
"userAssignedName": {
"value": "[variables('userAssignedNameWithLocation')]"
},
"location": {
"value": "[parameters('location')]"
}
},
"template": {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"userAssignedName": {
"type": "string"
},
"location": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.ManagedIdentity/userAssignedIdentities",
"name": "[parameters('userAssignedName')]",
"apiVersion": "2023-01-31",
"location": "[parameters('location')]",
"tags": {
"createdBy": "MicrosoftDefenderForSQL"
}
}
]
}
}
},
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2022-09-01",
"name": "[variables('deployGetResourceProperties')]",
"location": "eastus",
"dependsOn": [
"[resourceId('Microsoft.Resources/resourceGroups', variables('identityResourceGroup'))]",
"[variables('deployUserAssignedName')]"
],
"properties": {
"mode": "Incremental",
"template": {
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"resource": {
"type": "object",
"value": "[reference(parameters('resourceId'), '2019-07-01', 'Full')]"
}
}
}
}
},
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2022-09-01",
"name": "[concat(variables('deployAssignUserAssignedName'))]",
"resourceGroup": "[parameters('vmResourceGroup')]",
"dependsOn": [
"[resourceId('Microsoft.Resources/resourceGroups', variables('identityResourceGroup'))]",
"[variables('deployUserAssignedName')]",
"[variables('deployGetResourceProperties')]"
],
"properties": {
"mode": "Incremental",
"expressionEvaluationOptions": {
"scope": "inner"
},
"parameters": {
"userAssignedId": {
"value": "[if(parameters('bringYourOwnUserAssignedManagedIdentity'), parameters('userAssignedIdentityResourceId'), variables('autoCreatedUaId'))]"
},
"vmName": {
"value": "[parameters('vmName')]"
},
"location": {
"value": "[parameters('location')]"
},
"identityType": {
"value": "[if(contains(reference(variables('deployGetResourceProperties')).outputs.resource.value, 'identity'), reference(variables('deployGetResourceProperties')).outputs.resource.value.identity.type, '')]"
},
"userAssignedIdentities": {
"value": "[if(and(contains(reference(variables('deployGetResourceProperties')).outputs.resource.value, 'identity'), contains(reference(variables('deployGetResourceProperties')).outputs.resource.value.identity, 'userAssignedIdentities')), reference(variables('deployGetResourceProperties')).outputs.resource.value.identity.userAssignedIdentities, createObject())]"
}
},
"template": {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"userAssignedId": {
"type": "string"
},
"vmName": {
"type": "string"
},
"location": {
"type": "string"
},
"identityType": {
"type": "string"
},
"userAssignedIdentities": {
"type": "object"
}
},
"variables": {
"identityTypeValue": "[if(contains(parameters('identityType'), 'SystemAssigned'), 'SystemAssigned,UserAssigned', 'UserAssigned')]",
"userAssignedIdentitiesValue": "[union(parameters('userAssignedIdentities'), createObject(parameters('userAssignedId'), createObject()))]",
"resourceWithSingleUserAssignedIdentity": "[and(equals(parameters('identityType'), 'UserAssigned'), equals(string(length(parameters('userAssignedIdentities'))), '1'))]"
},
"resources": [
{
"condition": "[not(variables('resourceWithSingleUserAssignedIdentity'))]",
"apiVersion": "2023-03-01",
"type": "Microsoft.Compute/virtualMachines",
"name": "[parameters('vmName')]",
"location": "[parameters('location')]",
"identity": {
"type": "[variables('identityTypeValue')]",
"userAssignedIdentities": "[variables('userAssignedIdentitiesValue')]"
}
}
]
}
}
}
]
}
}
}
}
}
},
"versions": [
"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/09963c90-6ee7-4215-8d26-1cc660a1682f",
"name": "09963c90-6ee7-4215-8d26-1cc660a1682f"
}Bylaw09963c90-6ee7-4215-8d26-1cc660a1682f.bylaw.ps1 · 68 lines
# Converted from MDC_DfSQL_AddUserAssignedIdentity_VM.json by ConvertTo-Bylaw.
Policy '09963c90-6ee7-4215-8d26-1cc660a1682f' {
Properties @{
DisplayName = 'Create and assign a built-in user-assigned managed identity'
Description = 'Create and assign a built-in user-assigned managed identity at scale to SQL virtual machines.'
Version = '1.8.0'
Category = 'Security Center'
PolicyVersion = '1.8.0'
Mode = 'Indexed'
}
Parameter BringYourOwnUserAssignedManagedIdentity @{
Type = 'Boolean'
AllowedValues = @($true, $false)
Default = $false
DisplayName = 'Bring your own User-Assigned Managed Identity'
Description = 'Enable this to use your own user-assigned managed identity. The pre-created identity MUST exist otherwise the policy deployment will fail. If enabled, ensure that the user-assigned managed identity resource ID parameter matches the pre-created user-assigned managed identity resource ID. If not enabled, the policy will create a new user-assigned managed identitiy per subscription, in a new resource group named ''Built-In-Identity-RG''.'
}
Parameter UserAssignedIdentityResourceId @{
Default = ''
DisplayName = 'User-Assigned Managed Identity Resource ID'
Description = 'The resource ID of the pre-created user-assigned managed identity. This parameter is only used when bringYourOwnUserAssignedManagedIdentity is set to true'
}
Parameter BuiltInIdentityResourceGroupLocation @{
Default = 'eastus'
DisplayName = 'Built-In-Identity-RG Location'
Description = 'The location of the resource group ''Built-In-Identity-RG'' created by the policy.'
}
Parameter Effect @{
AllowedValues = @('AuditIfNotExists', 'DeployIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Policy Effect'
Description = 'The effect determines what happens when the policy rule is evaluated to match.'
}
Resource 'Microsoft.SqlVirtualMachine/SqlVirtualMachines'
DeployIfNotExists 'Microsoft.Compute/virtualMachines' -Role 'Contributor' -Name (Arm '[last(split(field(''Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId''), ''/''))]') -EvaluationDelay AfterProvisioning -Effect (Param Effect) {
Require 'storageProfile.osDisk.osType, identity.type, identity.userAssignedIdentities and 1 more are as required' {
AnyOf {
Field storageProfile.osDisk.osType | Must -BeLike 'Linux*'
AllOf {
Field identity.type | Must -ContainText 'UserAssigned'
Field identity.userAssignedIdentities | Must -ContainKey (Arm '[if(parameters(''bringYourOwnUserAssignedManagedIdentity''), parameters(''userAssignedIdentityResourceId''), concat(''/subscriptions/'', subscription().subscriptionId, ''/resourceGroups/Built-In-Identity-RG/providers/Microsoft.ManagedIdentity/userAssignedIdentities/Built-In-Identity-'', field(''location'')))]')
}
AllOf {
Field identity.type | Must -Be 'UserAssigned'
Arm '[string(length(field(''identity.userAssignedIdentities'')))]' | Must -Be '1'
}
}
}
Deployment @{
TemplateFile = './templates/09963c90-6ee7-4215-8d26-1cc660a1682f.json'
Scope = 'Subscription'
Location = 'eastus'
Parameters = [ordered]@{
location = (Arm '[field(''location'')]')
bringYourOwnUserAssignedManagedIdentity = (Param BringYourOwnUserAssignedManagedIdentity)
userAssignedIdentityResourceId = (Param UserAssignedIdentityResourceId)
builtInIdentityResourceGroupLocation = (Param BuiltInIdentityResourceGroupLocation)
vmName = (Arm '[last(split(field(''Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId''), ''/''))]')
vmResourceGroup = (Arm '[resourceGroup().name]')
resourceId = (Arm '[field(''Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId'')]')
}
}
}
}Its deployment templates are written beside it:
templates/09963c90-6ee7-4215-8d26-1cc660a1682f.json
Source: built-in-policies/policyDefinitions/Security Center/MDC_DfSQL_AddUserAssignedIdentity_VM.json in Azure/azure-policy, under the MIT licence.
Bylaw