Azure Policy パターン: パラメーター

パラメーターを使用してポリシー定義を動的に作成することで、必要なポリシー定義の数を減らすことができます。 パラメーターは、ポリシーを割り当てる際に定義します。 パラメーターにはあらかじめ定義された一連のプロパティがあり、そこでパラメーターとその使用方法が説明されます。

サンプル 1:文字列パラメーター

このポリシー定義では、tagNametagValue という 2 つのパラメーターを使用して、ポリシー割り当てによってリソースで検索される内容を設定します。 この形式により、ポリシー定義で任意の数のタグ名とタグ値の組み合わせを使用できるようになりますが、1 つのポリシー定義のみを維持してください。

Note

modeAll を使用して、リソース グループで動作するタグのサンプルについては、「パターン: タグ - サンプル 1」を参照してください。

{
    "properties": {
        "displayName": "Require tag and its value",
        "policyType": "BuiltIn",
        "mode": "Indexed",
        "description": "Enforces a required tag and its value. Does not apply to resource groups.",
        "parameters": {
            "tagName": {
                "type": "String",
                "metadata": {
                    "description": "Name of the tag, such as costCenter"
                }
            },
            "tagValue": {
                "type": "String",
                "metadata": {
                    "description": "Value of the tag, such as headquarter"
                }
            }
        },
        "policyRule": {
            "if": {
                "not": {
                    "field": "[concat('tags[', parameters('tagName'), ']')]",
                    "equals": "[parameters('tagValue')]"
                }
            },
            "then": {
                "effect": "deny"
            }
        }
    }
}

サンプル 1:説明

"tagName": {
    "type": "String",
    "metadata": {
        "description": "Name of the tag, such as costCenter"
    }
},

ポリシー定義のこの部分では、tagName パラメーターは "文字列" として定義され、その使用方法が説明されています。

次に、このパラメーターを policyRule.if ブロックで使用して、ポリシーを動的なものにします。 ここでは評価対象のフィールドを定義するために使用します。これは tagName の値を持つタグです。

"if": {
    "not": {
        "field": "[concat('tags[', parameters('tagName'), ']')]",
        "equals": "[parameters('tagValue')]"
    }
},

サンプル 2:配列パラメーター

このポリシー定義では、1 つのパラメーター listOfBandwidthinMbps を使用して、Express Route 回線リソースで、帯域幅の設定が、承認された値のいずれかに構成されているかどうかを確認します。 一致しない場合、リソースの作成または更新が拒否されます。

{
    "properties": {
        "displayName": "Allowed Express Route bandwidth",
        "description": "This policy enables you to specify a set of express route bandwidths that your organization can deploy.",
        "parameters": {
            "listOfBandwidthinMbps": {
                "type": "Array",
                "metadata": {
                    "description": "The list of SKUs that can be specified for express route.",
                    "displayName": "Allowed Bandwidth"
                }
            }
        },
        "policyRule": {
            "if": {
                "allOf": [{
                        "field": "type",
                        "equals": "Microsoft.Network/expressRouteCircuits"
                    },
                    {
                        "not": {
                            "field": "Microsoft.Network/expressRouteCircuits/serviceProvider.bandwidthInMbps",
                            "in": "[parameters('listOfBandwidthinMbps')]"
                        }
                    }
                ]
            },
            "then": {
                "effect": "Deny"
            }
        }
    }
}

サンプル 2:説明

"listOfBandwidthinMbps": {
    "type": "Array",
    "metadata": {
        "description": "The list of SKUs that can be specified for express route.",
        "displayName": "Allowed Bandwidth"
    }
}

ポリシー定義のこの部分では、listOfBandwidthinMbps パラメーターは "配列" として定義され、その使用方法が説明されています。 "配列" には一致する値が複数あります。

次に、このパラメーターを policyRule.if ブロックで使用します。 "配列" パラメーターなので、"配列" 条件である in または notIn を使用する必要があります。 ここでは、定義済みの値の 1 つとして、serviceProvider.bandwidthInMbps エイリアスに対して使用されています。

"not": {
    "field": "Microsoft.Network/expressRouteCircuits/serviceProvider.bandwidthInMbps",
    "in": "[parameters('listOfBandwidthinMbps')]"
}

サンプル 3: パラメーター化された効果

ポリシー定義を再利用するには、一般的にその効果そのものをパラメーター化します。 この例では、1 つのパラメーターである effect を使用します。 効果をパラメーター化すると、異なる効果を持つ異なるスコープに同じ定義を割り当てることができます。

{
    "properties": {
        "displayName": "All authorization rules except RootManageSharedAccessKey should be removed from Service Bus namespace",
        "policyType": "BuiltIn",
        "mode": "All",
        "description": "Service Bus clients should not use a namespace level access policy that provides access to all queues and topics in a namespace. To align with the least privilege security model, you should create access policies at the entity level for queues and topics to provide access to only the specific entity",
        "metadata": {
            "version": "1.0.1",
            "category": "Service Bus"
        },
        "parameters": {
            "effect": {
                "type": "string",
                "defaultValue": "Audit",
                "allowedValues": [
                    "Audit",
                    "Deny",
                    "Disabled"
                ],
                "metadata": {
                    "displayName": "Effect",
                    "description": "The effect determines what happens when the policy rule is evaluated to match"
                }
            }
        },
        "policyRule": {
            "if": {
                "allOf": [{
                        "field": "type",
                        "equals": "Microsoft.ServiceBus/namespaces/authorizationRules"
                    },
                    {
                        "field": "name",
                        "notEquals": "RootManageSharedAccessKey"
                    }
                ]
            },
            "then": {
                "effect": "[parameters('effect')]"
            }
        }
    }
}

サンプル 3: 説明

"parameters": {
    "effect": {
        "type": "string",
        "defaultValue": "Audit",
        "allowedValues": [
            "Audit",
            "Deny",
            "Disabled"
        ],
        "metadata": {
            "displayName": "Effect",
            "description": "The effect determines what happens when the policy rule is evaluated to match"
        }
    }
},

ポリシー定義のこの部分では、effect パラメーターは "文字列" として定義されています。 このポリシー定義によって、割り当ての既定値が "監査" に設定され、他のオプションが "無効" および "拒否" に制限されます。

次に、このパラメーターを policyRule.then ブロックで "効果" として使用します。

"then": {
    "effect": "[parameters('effect')]"
}

次のステップ