แก้ไข

แชร์ผ่าน


Azure Policy pattern: the count operator

The count operator evaluates members of a [*] alias.

Sample policy definition

This policy definition audits Network Security Groups configured to allow inbound Remote Desktop Protocol (RDP) traffic.

{
    "properties": {
        "mode": "all",
        "displayName": "Audit Network Security Groups for RDP",
        "description": "This policy audits NSGs with RDP ports enabled",
        "policyRule": {
            "if": {
                "allOf": [{
                        "field": "type",
                        "equals": "Microsoft.Network/networkSecurityGroups"
                    },
                    {
                        "count": {
                            "field": "Microsoft.Network/networkSecurityGroups/securityRules[*]",
                            "where": {
                                "allOf": [{
                                        "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].direction",
                                        "equals": "Inbound"
                                    },
                                    {
                                        "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].access",
                                        "equals": "Allow"
                                    },
                                    {
                                        "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].destinationPortRange",
                                        "equals": "3389"
                                    }
                                ]
                            }
                        },
                        "greater": 0
                    }
                ]
            },
            "then": {
                "effect": "audit"
            }
        }
    }
}

Explanation

The core components of the count operator are field, where, and the condition. Each is highlighted in the following snippet.

  • field tells count which alias to evaluate members of. Here, we're looking at the securityRules[*] alias array of the network security group.
  • where uses the policy language to define which array members meet the criteria. In this example, an allOf logical operator groups three different condition evaluations of alias array properties: direction, access, and destinationPortRange.
  • The count condition in this example is greater. Count evaluates as true when one or more members of the alias array matches the where clause.
{
    "count": {
        "field": "Microsoft.Network/networkSecurityGroups/securityRules[*]",
        "where": {
            "allOf": [{
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].direction",
                    "equals": "Inbound"
                },
                {
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].access",
                    "equals": "Allow"
                },
                {
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].destinationPortRange",
                    "equals": "3389"
                }
            ]
        }
    },
    "greater": 0
}

Next steps