Bylaw

Gallery › Privileged Identity Management › Azure built-in policies

Azure role assignment management should be restricted to Microsoft Entra Privileged Identity Management

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

Ensures all Azure role assignments are made via Microsoft Entra PIM or explicitly approved apps; others are denied by default. Admins can allow exceptions for specific roles, users, or groups (e.g., break‑glass access). Existing role assignments are unaffected. Ensure your tenant has the required PIM licensing before enabling this policy; otherwise, access may be unintentionally blocked.

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

Azure Policy JSONPimOnlyModeForAzure_Deny.json · 121 lines
{
  "properties": {
    "displayName": "Azure role assignment management should be restricted to Microsoft Entra Privileged Identity Management",
    "policyType": "BuiltIn",
    "mode": "All",
    "description": "Ensures all Azure role assignments are made via Microsoft Entra PIM or explicitly approved apps; others are denied by default. Admins can allow exceptions for specific roles, users, or groups (e.g., break‑glass access). Existing role assignments are unaffected. Ensure your tenant has the required PIM licensing before enabling this policy; otherwise, access may be unintentionally blocked.",
    "metadata": {
      "version": "1.0.0",
      "category": "Privileged Identity Management"
    },
    "version": "1.0.0",
    "parameters": {
      "exemptRoleIds": {
        "type": "Array",
        "metadata": {
          "displayName": "Exempted Role Definition IDs",
          "description": "When it applies: If a role definition ID appears in this list, the policy will not enforce PIM for assignment management to that role — the assignment is permitted even outside PIM. Why to use: Add roles here that are considered low-risk or operationally necessary to assign directly (e.g., Reader), so they can bypass the PIM requirement.Values must be lowercase GUIDs."
        },
        "defaultValue": []
      },
      "exemptRequestorTypes": {
        "type": "Array",
        "metadata": {
          "displayName": "Exempt Requestor Types",
          "description": "When it applies: If the identity making the role assignment request (the requestor) is of a principal type listed here, the assignment management will be allowed outside PIM. Why to use: Permit certain principal types (e.g., ServicePrincipal for automation pipelines, or ForeignGroup for cross-tenant scenarios) to create role assignments directly, since those identities typically cannot interact with PIM."
        },
        "allowedValues": [
          "user",
          "app"
        ],
        "defaultValue": []
      },
      "exemptAssigneeTypes": {
        "type": "Array",
        "metadata": {
          "displayName": "Exempt Assignee Types",
          "description": "When it applies: If the identity receiving the role assignment (the assignee) is of a principal type listed here, the assignment will not be blocked by the PIM-only policy. Why to use: Add types here that you do not require to go through PIM for role assignments. For example, include ServicePrincipal to allow assigning roles to application identities directly, or Group to permit group-based role assignments outside PIM."
        },
        "allowedValues": [
          "device",
          "foreigngroup",
          "group",
          "serviceprincipal",
          "user"
        ],
        "defaultValue": []
      },
      "excludedPrincipals": {
        "type": "Array",
        "metadata": {
          "displayName": "Excluded Requestors",
          "description": "When it applies: If the user or service principle making the role assignment request (the requestor) has one of the object Ids listed here, the assignment management will be allowed outside PIM. Why to use: Include trusted users and service principles that manage role assignments as part of their normal operation. Note that these requesters will only be able to create active assignments outside of PIM. Values must be lowercase GUIDs."
        },
        "defaultValue": []
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny will block non-compliant role assignment requests. Audit will produce audit logs for non-compliant role assignment requests. When disabled no compliance evaluation will be done."
        },
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Deny"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Authorization/roleAssignments"
          },
          {
            "value": "[contains(requestContext().identity, 'appid')]",
            "equals": true
          },
          {
            "value": "[toLower(tryGet(requestContext().identity, 'appid'))]",
            "notEquals": "01fc33a7-78ba-4d2f-a4b7-768e336e890e"
          },
          {
            "value": "[toLower(last(split(field('Microsoft.Authorization/roleAssignments/roleDefinitionId'), '/')))]",
            "notIn": "[parameters('exemptRoleIds')]"
          },
          {
            "value": "[toLower(tryGet(requestContext().identity, 'idtyp'))]",
            "notIn": "[parameters('exemptRequestorTypes')]"
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.Authorization/roleAssignments/principalType",
                "exists": false
              },
              {
                "value": "[toLower(field('Microsoft.Authorization/roleAssignments/principalType'))]",
                "notIn": "[parameters('exemptAssigneeTypes')]"
              }
            ]
          },
          {
            "value": "[toLower(tryGet(requestContext().identity, 'http://schemas.microsoft.com/identity/claims/objectidentifier'))]",
            "notIn": "[parameters('excludedPrincipals')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/25237d14-4a47-4cb6-bc7c-8bbdd4671f31",
  "name": "25237d14-4a47-4cb6-bc7c-8bbdd4671f31"
}
Bylaw25237d14-4a47-4cb6-bc7c-8bbdd4671f31.bylaw.ps1 · 65 lines
# Converted from PimOnlyModeForAzure_Deny.json by ConvertTo-Bylaw.
Policy '25237d14-4a47-4cb6-bc7c-8bbdd4671f31' {

    Properties @{
        DisplayName   = 'Azure role assignment management should be restricted to Microsoft Entra Privileged Identity Management'
        Description   = 'Ensures all Azure role assignments are made via Microsoft Entra PIM or explicitly approved apps; others are denied by default. Admins can allow exceptions for specific roles, users, or groups (e.g., break‑glass access). Existing role assignments are unaffected. Ensure your tenant has the required PIM licensing before enabling this policy; otherwise, access may be unintentionally blocked.'
        Version       = '1.0.0'
        Category      = 'Privileged Identity Management'
        PolicyVersion = '1.0.0'
        Mode          = 'All'
    }

    Parameter ExemptRoleIds @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Exempted Role Definition IDs'
        Description = 'When it applies: If a role definition ID appears in this list, the policy will not enforce PIM for assignment management to that role — the assignment is permitted even outside PIM. Why to use: Add roles here that are considered low-risk or operationally necessary to assign directly (e.g., Reader), so they can bypass the PIM requirement.Values must be lowercase GUIDs.'
    }
    Parameter ExemptRequestorTypes @{
        Type          = 'Array'
        AllowedValues = @('user', 'app')
        Default       = @()
        DisplayName   = 'Exempt Requestor Types'
        Description   = 'When it applies: If the identity making the role assignment request (the requestor) is of a principal type listed here, the assignment management will be allowed outside PIM. Why to use: Permit certain principal types (e.g., ServicePrincipal for automation pipelines, or ForeignGroup for cross-tenant scenarios) to create role assignments directly, since those identities typically cannot interact with PIM.'
    }
    Parameter ExemptAssigneeTypes @{
        Type          = 'Array'
        AllowedValues = @('device', 'foreigngroup', 'group', 'serviceprincipal', 'user')
        Default       = @()
        DisplayName   = 'Exempt Assignee Types'
        Description   = 'When it applies: If the identity receiving the role assignment (the assignee) is of a principal type listed here, the assignment will not be blocked by the PIM-only policy. Why to use: Add types here that you do not require to go through PIM for role assignments. For example, include ServicePrincipal to allow assigning roles to application identities directly, or Group to permit group-based role assignments outside PIM.'
    }
    Parameter ExcludedPrincipals @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Excluded Requestors'
        Description = 'When it applies: If the user or service principle making the role assignment request (the requestor) has one of the object Ids listed here, the assignment management will be allowed outside PIM. Why to use: Include trusted users and service principles that manage role assignments as part of their normal operation. Note that these requesters will only be able to create active assignments outside of PIM. Values must be lowercase GUIDs.'
    }
    Parameter Effect @{
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Deny'
        DisplayName   = 'Effect'
        Description   = 'Deny will block non-compliant role assignment requests. Audit will produce audit logs for non-compliant role assignment requests. When disabled no compliance evaluation will be done.'
    }

    Resource 'Microsoft.Authorization/roleAssignments' {

        When {
            Arm '[contains(requestContext().identity, ''appid'')]' | Must -Be $true
            Arm '[toLower(tryGet(requestContext().identity, ''appid''))]' | Must -Not -Be '01fc33a7-78ba-4d2f-a4b7-768e336e890e'
            Arm '[toLower(last(split(field(''Microsoft.Authorization/roleAssignments/roleDefinitionId''), ''/'')))]' | Must -Not -BeIn (Param ExemptRoleIds)
            Arm '[toLower(tryGet(requestContext().identity, ''idtyp''))]' | Must -Not -BeIn (Param ExemptRequestorTypes)
            AnyOf {
                Field principalType | Must -Not -Exist
                Arm '[toLower(field(''Microsoft.Authorization/roleAssignments/principalType''))]' | Must -Not -BeIn (Param ExemptAssigneeTypes)
            }
        }

        Require 'Requirement 1' {
            Arm '[toLower(tryGet(requestContext().identity, ''http://schemas.microsoft.com/identity/claims/objectidentifier''))]' | Must -BeIn (Param ExcludedPrincipals)
        }
    }

    Effect (Param Effect)
}

Source: built-in-policies/policyDefinitions/Privileged Identity Management/PimOnlyModeForAzure_Deny.json in Azure/azure-policy, under the MIT licence.