Share via


Azure 原則模式:欄位屬性

欄位運算子會針對在指定條件下提供的值,評估指定的屬性或別名

原則定義範例

此原則定義可讓您定義符合組織地理位置需求的允許區域。 允許的資源會在 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"
    }
}

欄位運算子會在邏輯運算子allOf 中使用三次。

  • 第一次使用時會透過 listOfAllowedLocations 參數的 notIn 條件來評估 location 屬性。 notIn 會在預期有「陣列」的情況下運作,而該參數就是「陣列」。 如果已建立或已更新資源的 location 不在核准的清單中,則此元素會評估為 true。
  • 第二次使用時也會評估 location 屬性,但會使用 notEquals 條件來查看資源是否為「全域」。 如果已建立或已更新資源的 location 不是「全域」,則此元素會評估為 true。
  • 最後一次使用時會評估 type 屬性,並使用 notEquals 條件來驗證資源類型不是 Microsoft.AzureActiveDirectory/b2cDirectories。 如果不是,此元素會評估為 true。

如果 allOf 邏輯運算子中的三個條件陳述式都評估為 true,則 Azure 原則會封鎖資源建立或更新。

下一步