Bylaw

Gallery › Authorization › Azure Community Policy

Deploy or audit for a specific role assignment at the subscription scope

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

This policy will validate that a specific role assignment exists or not. It can either audit for the role assignment or deploy it if it does not exist.

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

Azure Policy JSONazurepolicy.json · 117 lines
{
  "name": "7700791c-4631-45b6-9880-3a493d1827a5",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy or audit for a specific role assignment at the subscription scope",
    "description": "This policy will validate that a specific role assignment exists or not. It can either audit for the role assignment or deploy it if it does not exist.",
    "metadata": {
      "version": "1.0.0",
      "category": "Authorization"
    },
    "mode": "All",
    "parameters": {
      "principalId": {
        "type": "String",
        "metadata": {
          "displayName": "Principal Id",
          "description": "The principalId of the user, group, or SPN, that must be granted the role assignment"
        }
      },
      "roleDefinitionId": {
        "type": "String",
        "metadata": {
          "displayName": "Role Definition Id",
          "description": "The role definition Id that must be granted to the principalId. Just the GUID is required."
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Resources/subscriptions"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Authorization/roleAssignments",
          "ExistenceScope": "subscription",
          "DeploymentScope": "subscription",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Authorization/roleAssignments/principalId",
                "equals": "[parameters('principalId')]"
              },
              {
                "field": "Microsoft.Authorization/roleAssignments/roleDefinitionId",
                "equals": "[parameters('roleDefinitionId')]"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635"
          ],
          "deployment": {
            "location": "westus2",
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "principalId": {
                    "type": "string",
                    "metadata": {
                      "description": "principalId of the user that will be given contributor access to the resourceGroup"
                    }
                  },
                  "roleDefinitionId": {
                    "type": "string",
                    "metadata": {
                      "description": "roleDefinition to apply to the resourceGroup - default is contributor"
                    }
                  }
                },
                "variables": {
                  "roleAssignmentName": "[guid(parameters('principalId'), parameters('roleDefinitionId'))]"
                },
                "resources": [
                  {
                    "type": "Microsoft.Authorization/roleAssignments",
                    "apiVersion": "2022-04-01",
                    "name": "[variables('roleAssignmentName')]",
                    "properties": {
                      "principalId": "[parameters('principalId')]",
                      "roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', parameters('roleDefinitionId'))]"
                    }
                  }
                ]
              },
              "parameters": {
                "principalId": {
                  "value": "[parameters('principalId')]"
                },
                "roleDefinitionId": {
                  "value": "[parameters('roleDefinitionId')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw7700791c-4631-45b6-9880-3a493d1827a5.bylaw.ps1 · 46 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '7700791c-4631-45b6-9880-3a493d1827a5' {

    Properties @{
        DisplayName = 'Deploy or audit for a specific role assignment at the subscription scope'
        Description = 'This policy will validate that a specific role assignment exists or not. It can either audit for the role assignment or deploy it if it does not exist.'
        Version     = '1.0.0'
        Category    = 'Authorization'
        Mode        = 'All'
    }

    Parameter PrincipalId @{
        Type        = 'String'
        DisplayName = 'Principal Id'
        Description = 'The principalId of the user, group, or SPN, that must be granted the role assignment'
    }
    Parameter RoleDefinitionId @{
        Type        = 'String'
        DisplayName = 'Role Definition Id'
        Description = 'The role definition Id that must be granted to the principalId. Just the GUID is required.'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Resources/subscriptions'

    DeployIfNotExists 'Microsoft.Authorization/roleAssignments' -Role 'Owner' -ExistenceScope Subscription -Effect (Param Effect) {
        Require 'principalId is the PrincipalId parameter and roleDefinitionId is the RoleDefinitionId parameter' {
            Field principalId | Must -Be (Param PrincipalId)
            Field roleDefinitionId | Must -Be (Param RoleDefinitionId)
        }
        Deployment @{
            TemplateFile = './templates/7700791c-4631-45b6-9880-3a493d1827a5.json'
            Scope        = 'Subscription'
            Location     = 'westus2'
            Parameters   = [ordered]@{
                principalId      = (Param PrincipalId)
                roleDefinitionId = (Param RoleDefinitionId)
            }
        }
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Authorization/deploy-or-audit-for-a-specific-role-assignment-at-the-subscription-scope/azurepolicy.json in Azure/Community-Policy, under the MIT licence.