Bylaw

Gallery › App Service › Azure Community Policy

Linux function apps should only use a specified Azure Container Registry instance

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Ensure that Linux function apps can only pull custom images from a specified container registry

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

Azure Policy JSONazurepolicy.json · 129 lines
{
  "name": "6e149898-7113-458e-bb08-508c20070a78",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Linux function apps should only use a specified Azure Container Registry instance",
    "description": "Ensure that Linux function apps can only pull custom images from a specified container registry",
    "metadata": {
      "version": "1.0.0",
      "category": "App Service"
    },
    "mode": "Indexed",
    "parameters": {
      "registryName": {
        "type": "String",
        "metadata": {
          "displayName": "Registry name",
          "description": "The name of the Azure Container Registry instance from which the function app is allowed to pull images."
        }
      },
      "defaultRepositoryAndTag": {
        "type": "String",
        "metadata": {
          "displayName": "DefaultRepositoryAndTag",
          "description": "A default image name for the application to use against the registry if this policy will deploy a remediation. A tag may optionally be specified by appending a ':' character and the tag name."
        },
        "defaultValue": "defaultSetByAzurePolicy"
      },
      "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": {
            "anyOf": [
              {
                "field": "Microsoft.Web/sites/config/linuxFxVersion",
                "like": "[concat('DOCKER|',parameters('registryName'), '.azurecr.io/*')]"
              },
              {
                "field": "Microsoft.Web/sites/config/linuxFxVersion",
                "notLike": "DOCKER|*"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/de139f84-1756-47ae-9be6-808fbbe84772"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "siteName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "defaultRepositoryAndTag": {
                  "value": "[parameters('defaultRepositoryAndTag')]"
                },
                "registryName": {
                  "value": "[parameters('registryName')]"
                }
              },
              "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"
                  },
                  "registryName": {
                    "type": "string"
                  },
                  "defaultRepositoryAndTag": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Web/sites/config",
                    "name": "[concat(parameters('siteName'), '/web')]",
                    "apiVersion": "2021-02-01",
                    "location": "[parameters('location')]",
                    "tags": {},
                    "properties": {
                      "linuxFxVersion": "[concat('DOCKER|',parameters('registryName'), '.azurecr.io/', parameters('defaultRepositoryAndTag'))]"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}
Bylaw6e149898-7113-458e-bb08-508c20070a78.bylaw.ps1 · 50 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '6e149898-7113-458e-bb08-508c20070a78' {

    Properties @{
        DisplayName = 'Linux function apps should only use a specified Azure Container Registry instance'
        Description = 'Ensure that Linux function apps can only pull custom images from a specified container registry'
        Version     = '1.0.0'
        Category    = 'App Service'
        Mode        = 'Indexed'
    }

    Parameter RegistryName @{
        Type        = 'String'
        DisplayName = 'Registry name'
        Description = 'The name of the Azure Container Registry instance from which the function app is allowed to pull images.'
    }
    Parameter DefaultRepositoryAndTag @{
        Default     = 'defaultSetByAzurePolicy'
        DisplayName = 'DefaultRepositoryAndTag'
        Description = 'A default image name for the application to use against the registry if this policy will deploy a remediation. A tag may optionally be specified by appending a '':'' character and the tag name.'
    }
    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 'linuxFxVersion is as required' {
            AnyOf {
                Field linuxFxVersion | Must -BeLike (Arm '[concat(''DOCKER|'',parameters(''registryName''), ''.azurecr.io/*'')]')
                Field linuxFxVersion | Must -Not -BeLike 'DOCKER|*' -IfPresent
            }
        }
        Deployment -TemplateFile './templates/6e149898-7113-458e-bb08-508c20070a78.json' -Parameters ([ordered]@{
                siteName                = (Arm '[field(''name'')]')
                location                = (Arm '[field(''location'')]')
                defaultRepositoryAndTag = (Param DefaultRepositoryAndTag)
                registryName            = (Param RegistryName)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/App Service/linux-function-apps-should-only-use-a-specified-azure-container-registry-instance/azurepolicy.json in Azure/Community-Policy, under the MIT licence.