Azure Policy 模式:字段属性

field 运算符会对指定属性或别名进行评估,针对为给定条件提供的值。

示例策略定义

此策略定义使你能够定义满足组织的地理位置要求的允许区域。 允许的资源在参数 listOfAllowedLocations(数组)中定义。 与定义匹配的资源会被拒绝

{
    "properties": {
        "displayName": "Allowed locations",
        "policyType": "BuiltIn",
        "description": "This policy enables you to restrict the locations your organization can specify when deploying resources. Use to enforce your geo-compliance requirements. Excludes resource groups, Microsoft.AzureActiveDirectory/b2cDirectories, and resources that use the 'global' region.",
        "mode": "Indexed",
        "parameters": {
            "listOfAllowedLocations": {
                "type": "Array",
                "metadata": {
                    "description": "The list of locations that can be specified when deploying resources.",
                    "strongType": "location",
                    "displayName": "Allowed locations"
                }
            }
        },
        "policyRule": {
            "if": {
                "allOf": [{
                        "field": "location",
                        "notIn": "[parameters('listOfAllowedLocations')]"
                    },
                    {
                        "field": "location",
                        "notEquals": "global"
                    },
                    {
                        "field": "type",
                        "notEquals": "Microsoft.AzureActiveDirectory/b2cDirectories"
                    }
                ]
            },
            "then": {
                "effect": "Deny"
            }
        }
    }
}

说明

    "if": {
        "allOf": [{
                "field": "location",
                "notIn": "[parameters('listOfAllowedLocations')]"
            },
            {
                "field": "location",
                "notEquals": "global"
            },
            {
                "field": "type",
                "notEquals": "Microsoft.AzureActiveDirectory/b2cDirectories"
            }
        ]
    },
    "then": {
        "effect": "Deny"
    }
}

field 运算符在逻辑运算符allOf 中使用三次。

  • 第一次使用时,会通过 listOfAllowedLocations 参数的 notIn 条件评估 location 属性。 notIn 适用是因为它预期的是数组,而参数为数组。 如果创建的或更新的资源的 location 不在批准项列表中,则此元素的评估结果为 true。
  • 第二次使用也评估 location 属性,但使用 notEquals 条件来查看资源是否为全局资源。 如果创建的或更新的资源的 location 不是全局的,则此元素的评估结果为 true。
  • 最后一次使用评估 type 属性,并使用 notEquals 条件来验证资源类型是否为 Microsoft.AzureActiveDirectory/b2cDirectories。 如果为否,则此元素的评估结果为 true。

如果 allOf 逻辑运算符中的所有三个条件语句均为 true,则 Azure Policy 会阻止资源的创建或更新。

后续步骤