Bylaw

Gallery › API Management › Azure built-in policies

API Management policies should inherit parent scope policies using <base />

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

Ensures that every API Management policy includes the <base /> tag at the beginning of each policy section - <inbound>, <outbound>, <backend>, and <on-error> - to inherit policies from parent scopes. Omitting <base /> can lead to bypassing shared rules such as authentication, logging, rate limits, and other critical controls. Learn more: https://aka.ms/apim/policies/how-to

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

Azure Policy JSONBasePolicy_Audit.json · 268 lines
{
  "properties": {
    "displayName": "API Management policies should inherit parent scope policies using <base />",
    "policyType": "BuiltIn",
    "mode": "All",
    "description": "Ensures that every API Management policy includes the <base /> tag at the beginning of each policy section - <inbound>, <outbound>, <backend>, and <on-error> - to inherit policies from parent scopes. Omitting <base /> can lead to bypassing shared rules such as authentication, logging, rate limits, and other critical controls. Learn more: https://aka.ms/apim/policies/how-to",
    "metadata": {
      "version": "1.0.0",
      "category": "API Management"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "string",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "in": [
              "Microsoft.ApiManagement/service/apis/policies",
              "Microsoft.ApiManagement/service/apis/operations/policies",
              "Microsoft.ApiManagement/service/products/policies",
              "Microsoft.ApiManagement/service/workspaces/policies",
              "Microsoft.ApiManagement/service/workspaces/apis/policies",
              "Microsoft.ApiManagement/service/workspaces/apis/operations/policies",
              "Microsoft.ApiManagement/service/workspaces/products/policies"
            ]
          },
          {
            "anyOf": [
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.ApiManagement/service/apis/policies"
                  },
                  {
                    "not": {
                      "allOf": [
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/apis/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<inbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/apis/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<outbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/apis/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<backend><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/apis/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<on-error><base/>"
                        }
                      ]
                    }
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.ApiManagement/service/apis/operations/policies"
                  },
                  {
                    "not": {
                      "allOf": [
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/apis/operations/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<inbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/apis/operations/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<outbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/apis/operations/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<backend><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/apis/operations/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<on-error><base/>"
                        }
                      ]
                    }
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.ApiManagement/service/products/policies"
                  },
                  {
                    "not": {
                      "allOf": [
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/products/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<inbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/products/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<outbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/products/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<backend><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/products/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<on-error><base/>"
                        }
                      ]
                    }
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.ApiManagement/service/workspaces/policies"
                  },
                  {
                    "not": {
                      "allOf": [
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<inbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<outbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<backend><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<on-error><base/>"
                        }
                      ]
                    }
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.ApiManagement/service/workspaces/apis/policies"
                  },
                  {
                    "not": {
                      "allOf": [
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/apis/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<inbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/apis/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<outbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/apis/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<backend><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/apis/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<on-error><base/>"
                        }
                      ]
                    }
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.ApiManagement/service/workspaces/apis/operations/policies"
                  },
                  {
                    "not": {
                      "allOf": [
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/apis/operations/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<inbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/apis/operations/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<outbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/apis/operations/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<backend><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/apis/operations/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<on-error><base/>"
                        }
                      ]
                    }
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.ApiManagement/service/workspaces/products/policies"
                  },
                  {
                    "not": {
                      "allOf": [
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/products/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<inbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/products/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<outbound><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/products/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<backend><base/>"
                        },
                        {
                          "value": "[replace(replace(replace(replace(field('Microsoft.ApiManagement/service/workspaces/products/policies/value'), '\r', ''), '\n', ''), ' ', ''), '\t', '')]",
                          "contains": "<on-error><base/>"
                        }
                      ]
                    }
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/d5448c98-e503-4fdd-bcd2-784960c00d04",
  "name": "d5448c98-e503-4fdd-bcd2-784960c00d04"
}
Bylawd5448c98-e503-4fdd-bcd2-784960c00d04.bylaw.ps1 · 141 lines
# Converted from BasePolicy_Audit.json by ConvertTo-Bylaw.
Policy 'd5448c98-e503-4fdd-bcd2-784960c00d04' {

    Properties @{
        DisplayName   = 'API Management policies should inherit parent scope policies using <base />'
        Description   = 'Ensures that every API Management policy includes the <base /> tag at the beginning of each policy section - <inbound>, <outbound>, <backend>, and <on-error> - to inherit policies from parent scopes. Omitting <base /> can lead to bypassing shared rules such as authentication, logging, rate limits, and other critical controls. Learn more: https://aka.ms/apim/policies/how-to'
        Version       = '1.0.0'
        Category      = 'API Management'
        PolicyVersion = '1.0.0'
        Mode          = 'All'
    }

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

    Resource * {

        When {
            Field type | Must -BeIn @('Microsoft.ApiManagement/service/apis/policies', 'Microsoft.ApiManagement/service/apis/operations/policies', 'Microsoft.ApiManagement/service/products/policies', 'Microsoft.ApiManagement/service/workspaces/policies', 'Microsoft.ApiManagement/service/workspaces/apis/policies', 'Microsoft.ApiManagement/service/workspaces/apis/operations/policies', 'Microsoft.ApiManagement/service/workspaces/products/policies')
        }

        Require 'type and value are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.ApiManagement/service/apis/policies' -IfPresent
                AllOf {
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/apis/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<inbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/apis/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<outbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/apis/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<backend><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/apis/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<on-error><base/>'
                }
            }
        }

        Require 'type and value are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.ApiManagement/service/apis/operations/policies' -IfPresent
                AllOf {
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/apis/operations/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<inbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/apis/operations/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<outbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/apis/operations/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<backend><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/apis/operations/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<on-error><base/>'
                }
            }
        }

        Require 'type and value are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.ApiManagement/service/products/policies' -IfPresent
                AllOf {
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/products/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<inbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/products/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<outbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/products/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<backend><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/products/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<on-error><base/>'
                }
            }
        }

        Require 'type and value are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.ApiManagement/service/workspaces/policies' -IfPresent
                AllOf {
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<inbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<outbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<backend><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<on-error><base/>'
                }
            }
        }

        Require 'type and value are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.ApiManagement/service/workspaces/apis/policies' -IfPresent
                AllOf {
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/apis/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<inbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/apis/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<outbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/apis/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<backend><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/apis/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<on-error><base/>'
                }
            }
        }

        Require 'type and value are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.ApiManagement/service/workspaces/apis/operations/policies' -IfPresent
                AllOf {
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/apis/operations/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<inbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/apis/operations/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<outbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/apis/operations/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<backend><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/apis/operations/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<on-error><base/>'
                }
            }
        }

        Require 'type and value are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.ApiManagement/service/workspaces/products/policies' -IfPresent
                AllOf {
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/products/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<inbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/products/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<outbound><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/products/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<backend><base/>'
                    Arm '[replace(replace(replace(replace(field(''Microsoft.ApiManagement/service/workspaces/products/policies/value''), ''
'', ''''), ''
'', ''''), '' '', ''''), ''	'', '''')]' | Must -ContainText '<on-error><base/>'
                }
            }
        }
    }

    Effect (Param Effect)
}

Source: built-in-policies/policyDefinitions/API Management/BasePolicy_Audit.json in Azure/azure-policy, under the MIT licence.