Bylaw

Gallery › App Service › Azure built-in policies

Configure Function apps to turn off remote debugging

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.1.0

Remote debugging requires inbound ports to be opened on Function apps. Remote debugging should be turned off.

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

Azure Policy JSONDisableRemoteDebugging_FunctionApp_DINE.json · 100 lines
{
  "properties": {
    "displayName": "Configure Function apps to turn off remote debugging",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Remote debugging requires inbound ports to be opened on Function apps. Remote debugging should be turned off.",
    "metadata": {
      "version": "1.1.0",
      "category": "App Service"
    },
    "version": "1.1.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.Web/sites"
          },
          {
            "field": "kind",
            "contains": "functionapp"
          },
          {
            "field": "kind",
            "notContains": "workflowapp"
          },
          {
            "field": "kind",
            "notContains": "azurecontainerapps"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Web/sites/config",
          "name": "web",
          "existenceCondition": {
            "field": "Microsoft.Web/sites/config/web.remoteDebuggingEnabled",
            "equals": "false"
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/de139f84-1756-47ae-9be6-808fbbe84772"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "siteName": {
                  "value": "[field('name')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "siteName": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Web/sites/config",
                    "apiVersion": "2021-02-01",
                    "name": "[concat(parameters('siteName'), '/web')]",
                    "properties": {
                      "remoteDebuggingEnabled": "false"
                    }
                  }
                ],
                "outputs": {}
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.1.0",
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/25a5046c-c423-4805-9235-e844ae9ef49b",
  "name": "25a5046c-c423-4805-9235-e844ae9ef49b"
}
Bylaw25a5046c-c423-4805-9235-e844ae9ef49b.bylaw.ps1 · 41 lines
# Converted from DisableRemoteDebugging_FunctionApp_DINE.json by ConvertTo-Bylaw.
Policy '25a5046c-c423-4805-9235-e844ae9ef49b' {

    Properties @{
        DisplayName   = 'Configure Function apps to turn off remote debugging'
        Description   = 'Remote debugging requires inbound ports to be opened on Function apps. Remote debugging should be turned off.'
        Version       = '1.1.0'
        Category      = 'App Service'
        PolicyVersion = '1.1.0'
        Mode          = 'Indexed'
    }

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

    Resource 'Microsoft.Web/sites' {

        When {
            Field kind | Must -ContainText 'functionapp'
            Field kind | Must -Not -ContainText 'workflowapp'
        }

        Require 'kind contains azurecontainerapps' {
            Field kind | Must -ContainText 'azurecontainerapps'
        }
    }

    DeployIfNotExists 'Microsoft.Web/sites/config' -Role 'Website Contributor' -Name 'web' -Effect (Param Effect) {
        Require 'web.remoteDebuggingEnabled is false' {
            Field web.remoteDebuggingEnabled | Must -Be 'false'
        }
        Deployment -TemplateFile './templates/25a5046c-c423-4805-9235-e844ae9ef49b.json' -Parameters ([ordered]@{
                siteName = (Arm '[field(''name'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/App Service/DisableRemoteDebugging_FunctionApp_DINE.json in Azure/azure-policy, under the MIT licence.