Azure Policy パターン: 論理演算子

ポリシー定義には、条件付きステートメントをいくつか含めることができます。 各ステートメントが true である必要がある場合や、一部のステートメントのみが true である必要がある場合があります。 これらのニーズに対応するために、言語には、notallOfanyOf論理演算子があります。 これらは省略可能であり、入れ子にして複雑なシナリオを作成することもできます。

サンプル 1:1 つの論理演算子

このポリシー定義は、Azure Cosmos DB アカウントを評価して、自動フェールオーバーと複数の書き込み場所が構成されているかどうかを確認します。 そうでない場合は、audit がトリガーされ、非対応のリソースが作成または更新されるときにログ エントリが作成されます。

{
  "properties": {
    "mode": "all",
    "displayName": "Audit Automatic Failover for CosmosDB accounts",
    "description": "This policy audits Automatic Failover for CosmosDB accounts",
    "policyRule": {
      "if": {
        "allOf": [{
            "field": "type",
            "equals": "Microsoft.DocumentDB/databaseAccounts"
          },
          {
            "field": "Microsoft.DocumentDB/databaseAccounts/enableAutomaticFailover",
            "equals": "false"
          },
          {
            "field": "Microsoft.DocumentDB/databaseAccounts/enableMultipleWriteLocations",
            "equals": "false"
          }
        ]
      },
      "then": {
        "effect": "audit"
      }
    },
    "parameters": {},
    "metadata": {}
  }
}

サンプル 1:説明

"policyRule": {
  "if": {
    "allOf": [{
        "field": "type",
        "equals": "Microsoft.DocumentDB/databaseAccounts"
      },
      {
        "field": "Microsoft.DocumentDB/databaseAccounts/enableAutomaticFailover",
        "equals": "false"
      },
      {
        "field": "Microsoft.DocumentDB/databaseAccounts/enableMultipleWriteLocations",
        "equals": "false"
      }
    ]
  },
  "then": {

policyRule.if ブロックで、1 つの allOf を使用して、3 つの条件すべてが true であることを確認します。 これらのすべての条件が true と評価された場合にのみ、audit 効果がトリガーされます。

サンプル 2:複数の論理演算子

このポリシー定義は、リソースの名前付けパターンを評価します。 リソースが一致しない場合は、拒否されます

{
    "properties": {
        "displayName": "Match multiple name patterns.",
        "description": "Allows one of multiple naming patterns for resources.",
        "mode": "Indexed",
        "policyRule": {
            "if": {
                "allOf": [{
                        "not": {
                            "field": "name",
                            "match": "contoso??????"
                        }
                    },
                    {
                        "not": {
                            "field": "name",
                            "match": "contoso-???-##"
                        }
                    }
                ]
            },
            "then": {
                "effect": "deny"
            }
        }
    }
}

サンプル 2:説明

"if": {
    "allOf": [{
            "not": {
                "field": "name",
                "match": "contoso??????"
            }
        },
        {
            "not": {
                "field": "name",
                "match": "contoso-???-##"
            }
        }
    ]
},

この policyRule.if ブロックにも 1 つの allOf が含まれていますが、各条件は not 論理演算子でラップされています。 not 論理演算子内で条件が最初に評価され、次に not が評価されて、句全体が true であるか、false であるかどうかが判定されます。 両方の not 論理演算子が true と評価されると、ポリシー効果がトリガーされます。

サンプル 3: 論理演算子の組み合わせ

このポリシー定義は、Spring on Azure アカウントを評価して、トレースが有効でない、またはトレースが成功状態ではないかどうかを調べるものです。

{
    "properties": {
        "displayName": "Audit Azure Spring Cloud instances where distributed tracing is not enabled",
        "description": "Distributed tracing tools in Azure Spring Cloud allow debugging and monitoring the complex interconnections between microservices in an application. Distributed tracing tools should be enabled and in a healthy state.",
        "mode": "Indexed",
        "policyRule": {
            "if": {
                "allOf": [{
                        "field": "type",
                        "equals": "Microsoft.AppPlatform/Spring"
                    },
                    {
                        "anyOf": [{
                                "field": "Microsoft.AppPlatform/Spring/trace.enabled",
                                "notEquals": "true"
                            },
                            {
                                "field": "Microsoft.AppPlatform/Spring/trace.state",
                                "notEquals": "Succeeded"
                            }
                        ]
                    }
                ]
            },
            "then": {
                "effect": "audit"
            }
        }
    }
}

サンプル 3: 説明

"policyRule": {
    "if": {
        "allOf": [{
                "field": "type",
                "equals": "Microsoft.AppPlatform/Spring"
            },
            {
                "anyOf": [{
                        "field": "Microsoft.AppPlatform/Spring/trace.enabled",
                        "notEquals": "true"
                    },
                    {
                        "field": "Microsoft.AppPlatform/Spring/trace.state",
                        "notEquals": "Succeeded"
                    }
                ]
            }
        ]
    },
    "then": {
        "effect": "audit"
    }
}

この policyRule.if ブロックには、allOfanyOf の両方の論理演算子が含まれています。 anyOf 論理演算子は、指定されている条件がいずれか 1 つでも true であれば、true と評価されます。 typeallOf の中心となるため、常に true と評価される必要があります。 type が true となり、なおかつ anyOf に指定されたいずれかの条件が true である場合に、ポリシーの効果がトリガーされます。

次のステップ