Azure Policy-patroon: logische operators

Een beleidsdefinitie kan verschillende voorwaardelijke instructies bevatten. Mogelijk moeten alle instructies waar zijn of is dit slechts voor een aantal instructies vereist. Ter ondersteuning van deze behoeften heeft de taal logische operators voor not, allOf en anyOf. Deze operators zijn optioneel en kunnen worden genest om complexe scenario's te maken.

Voorbeeld 1: Eén logische operator

Deze beleidsdefinitie evalueert Azure Cosmos DB-accounts om te zien of automatische failovers en meerdere schrijflocaties zijn geconfigureerd. Wanneer dit niet het geval is, wordt de audit geactiveerd en wordt er een logboekvermelding gemaakt wanneer de niet-compatibele resource wordt gemaakt of bijgewerkt.

{
  "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": {}
  }
}

Voorbeeld 1: Uitleg

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

In het blok policyRule.if wordt één allOf gebruikt om ervoor te zorgen dat aan alle drie de voorwaarden wordt voldaan. Alleen wanneer aan al deze voorwaarden wordt voldaan, wordt het effect audit geactiveerd.

Voorbeeld 2: Meerdere logische operators

Deze beleidsdefinitie evalueert resources op een naamgevingspatroon. Als een resource niet voldoet aan het patroon, wordt deze geweigerd met het effect deny.

{
    "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"
            }
        }
    }
}

Voorbeeld 2: Uitleg

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

Het blok policyRule.if bevat ook één allOf, maar elke voorwaarde bevat ook de logische operator not. De voorwaarde binnen de logische operator not evalueert eerst en evalueert vervolgens de niet om te bepalen of de hele component waar of onwaar is. Als beide logische operators not worden geëvalueerd als waar, wordt het beleidseffect geactiveerd.

Voorbeeld 3: Logische operators combineren

Deze beleidsdefinitie evalueert Spring op Azure-accounts om te zien of tracering niet is ingeschakeld of dat tracering niet is geslaagd.

{
    "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"
            }
        }
    }
}

Voorbeeld 3: Uitleg

"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"
    }
}

Dit blok policyRule.if bevat zowel de logische operator allOf als anyOf. De logische operator anyOf levert waar op als ten minste aan één voorwaarde wordt voldaan. Aangezien het type essentieel is voor allOf, moet deze altijd waar opleveren. Als het type en een van de voorwaarden in anyOf waar zijn, wordt het beleidseffect geactiveerd.

Volgende stappen