Bylaw

Gallery › Compute › Azure built-in policies

Configure Windows and Linux virtual machines to use Microsoft Entra ID for sign-in

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Deploys the Microsoft Entra ID login extension (AADLoginForWindows on Windows VMs, AADSSHLoginForLinux on Linux VMs) and enables a system-assigned managed identity on Azure virtual machines, so users can sign in with their Microsoft Entra credentials. Learn more at https://aka.ms/AzureADLoginVMs.

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

Azure Policy JSONEntraLoginForVirtualMachines_DINE.json · 147 lines
{
  "properties": {
    "displayName": "Configure Windows and Linux virtual machines to use Microsoft Entra ID for sign-in",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Deploys the Microsoft Entra ID login extension (AADLoginForWindows on Windows VMs, AADSSHLoginForLinux on Linux VMs) and enables a system-assigned managed identity on Azure virtual machines, so users can sign in with their Microsoft Entra credentials. Learn more at https://aka.ms/AzureADLoginVMs.",
    "metadata": {
      "version": "1.0.0",
      "category": "Compute"
    },
    "version": "1.0.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.Compute/virtualMachines"
          },
          {
            "field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType",
            "in": [
              "Windows",
              "Linux"
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
                "equals": "Microsoft.Azure.ActiveDirectory"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/type",
                "in": [
                  "AADLoginForWindows",
                  "AADSSHLoginForLinux"
                ]
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "vmName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "osType": {
                  "value": "[field('Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "vmName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "osType": {
                    "type": "string",
                    "allowedValues": [
                      "Windows",
                      "Linux"
                    ]
                  }
                },
                "variables": {
                  "extensionMap": {
                    "Windows": {
                      "type": "AADLoginForWindows",
                      "typeHandlerVersion": "2.0"
                    },
                    "Linux": {
                      "type": "AADSSHLoginForLinux",
                      "typeHandlerVersion": "1.0"
                    }
                  },
                  "ext": "[variables('extensionMap')[parameters('osType')]]"
                },
                "resources": [
                  {
                    "type": "Microsoft.Compute/virtualMachines",
                    "name": "[parameters('vmName')]",
                    "apiVersion": "2023-09-01",
                    "location": "[parameters('location')]",
                    "identity": {
                      "type": "SystemAssigned"
                    }
                  },
                  {
                    "type": "Microsoft.Compute/virtualMachines/extensions",
                    "name": "[concat(parameters('vmName'), '/', variables('ext').type)]",
                    "apiVersion": "2023-09-01",
                    "location": "[parameters('location')]",
                    "dependsOn": [
                      "[resourceId('Microsoft.Compute/virtualMachines', parameters('vmName'))]"
                    ],
                    "properties": {
                      "publisher": "Microsoft.Azure.ActiveDirectory",
                      "type": "[variables('ext').type]",
                      "typeHandlerVersion": "[variables('ext').typeHandlerVersion]",
                      "autoUpgradeMinorVersion": true
                    }
                  }
                ]
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/3d9a4145-9197-45fa-afcd-84b887b4e2d3",
  "name": "3d9a4145-9197-45fa-afcd-84b887b4e2d3"
}
Bylaw3d9a4145-9197-45fa-afcd-84b887b4e2d3.bylaw.ps1 · 38 lines
# Converted from EntraLoginForVirtualMachines_DINE.json by ConvertTo-Bylaw.
Policy '3d9a4145-9197-45fa-afcd-84b887b4e2d3' {

    Properties @{
        DisplayName   = 'Configure Windows and Linux virtual machines to use Microsoft Entra ID for sign-in'
        Description   = 'Deploys the Microsoft Entra ID login extension (AADLoginForWindows on Windows VMs, AADSSHLoginForLinux on Linux VMs) and enables a system-assigned managed identity on Azure virtual machines, so users can sign in with their Microsoft Entra credentials. Learn more at https://aka.ms/AzureADLoginVMs.'
        Version       = '1.0.0'
        Category      = 'Compute'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

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

    Resource 'Microsoft.Compute/virtualMachines' {

        Require 'storageProfile.osDisk.osType is not one of 2 values' {
            Field storageProfile.osDisk.osType | Must -Not -BeIn @('Windows', 'Linux') -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Compute/virtualMachines/extensions' -Role 'Virtual Machine Contributor' -Effect (Param Effect) {
        Require 'publisher is Microsoft.Azure.ActiveDirectory and type is one of 2 values' {
            Field publisher | Must -Be 'Microsoft.Azure.ActiveDirectory'
            Field 'Microsoft.Compute/virtualMachines/extensions/type' | Must -BeIn @('AADLoginForWindows', 'AADSSHLoginForLinux')
        }
        Deployment -TemplateFile './templates/3d9a4145-9197-45fa-afcd-84b887b4e2d3.json' -Parameters ([ordered]@{
                vmName   = (Arm '[field(''name'')]')
                location = (Arm '[field(''location'')]')
                osType   = (Arm '[field(''Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType'')]')
            })
    }
}

Its deployment templates are written beside it:

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