Bylaw

Gallery › App Service › Azure built-in policies

Function apps should have resource logs enabled

Uses Condition Policy AuditIfNotExists Disabled Mode Indexed Version 1.0.0

Audit enabling of resource logs on the function app. This enables you to recreate activity trails for investigation purposes if a security incident occurs or your network is compromised.

ConvertTo-Bylaw converted this policy, and proved it compiles back to the same definition. Conditions the language can't express yet are written as raw JSON with Condition.

Azure Policy JSONFunctionApp_ResourceLoggingMonitoring_Audit.json · 122 lines
{
  "properties": {
    "displayName": "Function apps should have resource logs enabled",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Audit enabling of resource logs on the function app. This enables you to recreate activity trails for investigation purposes if a security incident occurs or your network is compromised.",
    "metadata": {
      "version": "1.0.0",
      "category": "App Service"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "AuditIfNotExists",
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      },
      "requiredRetentionDays": {
        "type": "String",
        "defaultValue": "365",
        "metadata": {
          "displayName": "Required retention (days)",
          "description": "The required resource logs retention in days"
        }
      }
    },
    "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.Insights/diagnosticSettings",
          "existenceCondition": {
            "count": {
              "field": "Microsoft.Insights/diagnosticSettings/logs[*]",
              "where": {
                "anyOf": [
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.enabled",
                        "equals": "true"
                      },
                      {
                        "anyOf": [
                          {
                            "field": "Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.days",
                            "equals": "0"
                          },
                          {
                            "value": "[padLeft(current('Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.days'), 3, '0')]",
                            "greaterOrEquals": "[padLeft(parameters('requiredRetentionDays'), 3, '0')]"
                          }
                        ]
                      },
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
                        "equals": "true"
                      }
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
                        "equals": "true"
                      },
                      {
                        "anyOf": [
                          {
                            "field": "Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.enabled",
                            "notEquals": "true"
                          },
                          {
                            "field": "Microsoft.Insights/diagnosticSettings/storageAccountId",
                            "exists": false
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            },
            "greaterOrEquals": 1
          }
        }
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/a209d6bd-0ace-468c-9883-71a82870b46b",
  "name": "a209d6bd-0ace-468c-9883-71a82870b46b"
}
Bylawa209d6bd-0ace-468c-9883-71a82870b46b.bylaw.ps1 · 61 lines
# Converted from FunctionApp_ResourceLoggingMonitoring_Audit.json by ConvertTo-Bylaw.
Policy 'a209d6bd-0ace-468c-9883-71a82870b46b' {

    Properties @{
        DisplayName   = 'Function apps should have resource logs enabled'
        Description   = 'Audit enabling of resource logs on the function app. This enables you to recreate activity trails for investigation purposes if a security incident occurs or your network is compromised.'
        Version       = '1.0.0'
        Category      = 'App Service'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter RequiredRetentionDays @{
        Default     = '365'
        DisplayName = 'Required retention (days)'
        Description = 'The required resource logs retention in days'
    }

    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'
        }
    }

    AuditIfNotExists 'Microsoft.Insights/diagnosticSettings' -Effect (Param Effect) {
        Require 'logs, retentionPolicy.enabled, retentionPolicy.days and 2 more are as required' {
            Count 'logs[*]' -Where {
                AnyOf {
                    AllOf {
                        Field retentionPolicy.enabled | Must -Be 'true'
                        AnyOf {
                            Field retentionPolicy.days | Must -Be '0'
                            Arm '[padLeft(current(''Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.days''), 3, ''0'')]' | Must -BeGreaterOrEqual (Arm '[padLeft(parameters(''requiredRetentionDays''), 3, ''0'')]')
                        }
                        Condition '{ "field": "Microsoft.Insights/diagnosticSettings/logs.enabled", "equals": "true" }'
                    }
                    AllOf {
                        Condition '{ "field": "Microsoft.Insights/diagnosticSettings/logs.enabled", "equals": "true" }'
                        AnyOf {
                            Field retentionPolicy.enabled | Must -Not -Be 'true'
                            Condition '{ "field": "Microsoft.Insights/diagnosticSettings/storageAccountId", "exists": false }'
                        }
                    }
                }
            } | Must -BeGreaterOrEqual 1
        }
    }
}

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