Bylaw

Gallery › API Management › Azure Community Policy

Configure ReadOnly lock for API Management's subnet

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

Deploy ReadOnly resource lock for API Management's configured subnet.

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

Azure Policy JSONazurepolicy.json · 100 lines
{
  "name": "a00ef680-cc0e-4828-b3ea-8586b98be163",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Configure ReadOnly lock for API Management's subnet",
    "description": "Deploy ReadOnly resource lock for API Management's configured subnet.",
    "metadata": {
      "category": "API Management",
      "version": "1.0.0"
    },
    "mode": "All",
    "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.ApiManagement/service"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Authorization/locks",
          "existenceScope": "subscription",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Authorization/locks/level",
                "equals": "ReadOnly"
              },
              {
                "field": "id",
                "contains": "[field('Microsoft.ApiManagement/service/virtualNetworkConfiguration.subnetResourceId')]"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "subnetId": {
                    "type": "string"
                  }
                },
                "functions": [],
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Authorization/locks",
                    "apiVersion": "2020-05-01",
                    "scope": "[parameters('subnetId')]",
                    "name": "ReadOnlyLock",
                    "properties": {
                      "level": "ReadOnly",
                      "notes": "Created by Policy"
                    }
                  }
                ],
                "outputs": {
                  "policy": {
                    "type": "string",
                    "value": "[concat('Resource lock ReadOnly has been set for the subnet ', parameters('subnetId'), ' (Microsoft.ApiManagement/service/virtualNetworkConfiguration.subnetResourceId)')]"
                  }
                }
              },
              "parameters": {
                "subnetId": {
                  "value": "[field('Microsoft.ApiManagement/service/virtualNetworkConfiguration.subnetResourceId')]"
                }
              }
            }
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c",
            "/providers/Microsoft.Authorization/roleDefinitions/18d7d88d-d35e-4fb5-a5c3-7773c20a72d9"
          ]
        }
      }
    }
  }
}
Bylawa00ef680-cc0e-4828-b3ea-8586b98be163.bylaw.ps1 · 30 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'a00ef680-cc0e-4828-b3ea-8586b98be163' {

    Properties @{
        DisplayName = 'Configure ReadOnly lock for API Management''s subnet'
        Description = 'Deploy ReadOnly resource lock for API Management''s configured subnet.'
        Category    = 'API Management'
        Version     = '1.0.0'
        Mode        = 'All'
    }

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

    Resource 'Microsoft.ApiManagement/service'

    DeployIfNotExists 'Microsoft.Authorization/locks' -Role 'Contributor', 'User Access Administrator' -ExistenceScope Subscription -Effect (Param Effect) {
        Require 'level, id and virtualNetworkConfiguration.subnetResourceId are as required' {
            Field level | Must -Be 'ReadOnly'
            Field id | Must -ContainText (Arm '[field(''Microsoft.ApiManagement/service/virtualNetworkConfiguration.subnetResourceId'')]')
        }
        Deployment -TemplateFile './templates/a00ef680-cc0e-4828-b3ea-8586b98be163.json' -Parameters ([ordered]@{
                subnetId = (Arm '[field(''Microsoft.ApiManagement/service/virtualNetworkConfiguration.subnetResourceId'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/API Management/configure-readonly-lock-for-api-management's-subnet/azurepolicy.json in Azure/Community-Policy, under the MIT licence.