Bylaw

Gallery › App Service › Azure Community Policy

Function apps should authenticate to Azure Container Registry using a managed identity

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Function apps should authenticate to Azure Container Registry using a managed identity

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

Azure Policy JSONazurepolicy.json · 94 lines
{
  "name": "0525a312-9ca3-43c8-a5f8-249b34e26208",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Function apps should authenticate to Azure Container Registry using a managed identity",
    "description": "Function apps should authenticate to Azure Container Registry using a managed identity",
    "metadata": {
      "version": "1.0.0",
      "category": "App Service"
    },
    "mode": "Indexed",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Web/sites"
          },
          {
            "field": "kind",
            "contains": "functionapp"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Web/sites/config",
          "name": "web",
          "existenceCondition": {
            "field": "Microsoft.Web/sites/config/acrUseManagedIdentityCreds",
            "equals": true
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/de139f84-1756-47ae-9be6-808fbbe84772"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "siteName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "siteName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Web/sites/config",
                    "name": "[concat(parameters('siteName'), '/web')]",
                    "apiVersion": "2021-02-01",
                    "location": "[parameters('location')]",
                    "tags": {},
                    "properties": {
                      "acrUseManagedIdentityCreds": true
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}
Bylaw0525a312-9ca3-43c8-a5f8-249b34e26208.bylaw.ps1 · 35 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '0525a312-9ca3-43c8-a5f8-249b34e26208' {

    Properties @{
        DisplayName = 'Function apps should authenticate to Azure Container Registry using a managed identity'
        Description = 'Function apps should authenticate to Azure Container Registry using a managed identity'
        Version     = '1.0.0'
        Category    = 'App Service'
        Mode        = 'Indexed'
    }

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

    Resource 'Microsoft.Web/sites' {

        Require 'kind does not contain functionapp' {
            Field kind | Must -Not -ContainText 'functionapp' -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Web/sites/config' -Role 'Website Contributor' -Name 'web' -Effect (Param Effect) {
        Require 'acrUseManagedIdentityCreds is true' {
            Field acrUseManagedIdentityCreds | Must -Be $true
        }
        Deployment -TemplateFile './templates/0525a312-9ca3-43c8-a5f8-249b34e26208.json' -Parameters ([ordered]@{
                siteName = (Arm '[field(''name'')]')
                location = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/App Service/function-apps-should-authenticate-to-azure-container-registry-using-a-managed-identity/azurepolicy.json in Azure/Community-Policy, under the MIT licence.