Azure Policy 패턴: 논리 연산자

정책 정의는 여러 조건문을 포함할 수 있습니다. 각 명령문이 true이어야 하거나 그 중 일부만 true이어야 합니다. 이러한 요구를 지원하기 위해 언어에는 not, allOfanyOf에 대한 논리 연산자가 있습니다. 이러한 항목은 선택 사항이며 복잡한 시나리오를 만들기 위해 중첩될 수 있습니다.

샘플 1: 하나의 논리 연산자

이 정책 정의는 Azure Cosmos DB 계정을 평가하여 자동 장애 조치(failover) 및 여러 쓰기 위치가 구성되어 있는지 여부를 확인합니다. 그렇지 않은 경우 감사는 비규격 리소스를 만들거나 업데이트할 때 트리거하고 로그 항목을 만듭니다.

{
  "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 블록은 단일 allOf를 사용하여 세 가지 조건이 모두 true가 되도록 합니다. 이러한 조건이 모두 true로 평가되는 경우에만 감사 효과가 트리거됩니다.

샘플 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 블록에는 단일 allOf도 포함되지만 각 조건은 not 논리 연산자로 래핑됩니다. not 논리 연산자 내부의 조건을 먼저 평가한 다음, not을 평가하여 전체 절이 true인지 false인지 여부를 확인합니다. not 논리 연산자가 모두 true로 평가되면 정책 효과가 트리거됩니다.

샘플 3: 논리 연산자 결합

이 정책 정의는 Azure의 Spring 계정을 평가하여 추적을 사용하도록 설정하지 않았거나 추적이 성공 상태가 아닌지 여부를 확인합니다.

{
    "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 논리 연산자는 포함된 조건 중 하나가 true인 경우 true를 평가합니다. 형식allOf의 핵심이므로 항상 true를 평가해야 합니다. 형식anyOf의 조건 중 하나가 true인 경우 정책 효과가 트리거됩니다.

다음 단계