Bylaw

Gallery › Network › Azure Community Policy

Azure firewall policy should only allow user defined standard ports and FQDNs within network rules

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

Enforce usage of user defined standard ports, protocols and destination addresses. This is a common requirement in many regulatory and industry compliance standards.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONazurepolicy.json · 114 lines
{
  "name": "d4a058a9-7180-49f7-9895-fedbdd834986",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Azure firewall policy should only allow user defined standard ports and FQDNs within network rules",
    "description": "Enforce usage of user defined standard ports, protocols and destination addresses. This is a common requirement in many regulatory and industry compliance standards.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "priority": {
        "type": "Integer",
        "metadata": {
          "displayName": "priority",
          "description": "Validate Azure firewall network rule priority. (Example: 999)."
        }
      },
      "allowedRules": {
        "type": "Array",
        "metadata": {
          "displayName": "Allowed Rules",
          "description": "Validate and allowed destination ports, portocols and destination addresses. Example: [{'destinationPorts':['443','1688'],'protocols':['TCP'],'enforceDestinationAddresses':false,'destinationAddresses':[]},{'destinationPorts':['123'],'protocols':['UDP'],'enforceDestinationAddresses':true,'destinationAddresses':['13.86.101.172']}]"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Network/firewallPolicies/ruleCollectionGroups"
          },
          {
            "count": {
              "field": "Microsoft.Network/firewallPolicies/ruleCollectionGroups/ruleCollections[*]",
              "where": {
                "allOf": [
                  {
                    "field": "Microsoft.Network/firewallPolicies/ruleCollectionGroups/ruleCollections[*].FirewallPolicyFilterRuleCollection.rules[*].ruleType",
                    "equals": "NetworkRule"
                  },
                  {
                    "anyOf": [
                      {
                        "field": "Microsoft.Network/firewallPolicies/ruleCollectionGroups/ruleCollections[*].priority",
                        "lessOrEquals": "[parameters('priority')]"
                      },
                      {
                        "count": {
                          "field": "Microsoft.Network/firewallPolicies/ruleCollectionGroups/ruleCollections[*].FirewallPolicyFilterRuleCollection.rules[*]",
                          "where": {
                            "count": {
                              "value": "[parameters('allowedRules')]",
                              "name": "allowedRule",
                              "where": {
                                "allOf": [
                                  {
                                    "field": "Microsoft.Network/firewallPolicies/ruleCollectionGroups/ruleCollections[*].FirewallPolicyFilterRuleCollection.rules[*].NetworkRule.destinationPorts[*]",
                                    "in": "[array(current('allowedRule').destinationPorts)]"
                                  },
                                  {
                                    "field": "Microsoft.Network/firewallPolicies/ruleCollectionGroups/ruleCollections[*].FirewallPolicyFilterRuleCollection.rules[*].NetworkRule.ipProtocols[*]",
                                    "in": "[array(current('allowedRule').protocols)]"
                                  },
                                  {
                                    "anyOf": [
                                      {
                                        "value": "[current('allowedRule').enforceDestinationAddresses]",
                                        "equals": false
                                      },
                                      {
                                        "field": "Microsoft.Network/firewallPolicies/ruleCollectionGroups/ruleCollections[*].FirewallPolicyFilterRuleCollection.rules[*].NetworkRule.destinationAddresses[*]",
                                        "in": "[array(current('allowedRule').destinationAddresses)]"
                                      }
                                    ]
                                  }
                                ]
                              }
                            },
                            "equals": 0
                          }
                        },
                        "greater": 0
                      }
                    ]
                  }
                ]
              }
            },
            "greater": 0
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylawd4a058a9-7180-49f7-9895-fedbdd834986.bylaw.ps1 · 70 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'd4a058a9-7180-49f7-9895-fedbdd834986' {

    Properties @{
        DisplayName = 'Azure firewall policy should only allow user defined standard ports and FQDNs within network rules'
        Description = 'Enforce usage of user defined standard ports, protocols and destination addresses. This is a common requirement in many regulatory and industry compliance standards.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter Priority @{
        Type        = 'Integer'
        DisplayName = 'priority'
        Description = 'Validate Azure firewall network rule priority. (Example: 999).'
    }
    Parameter AllowedRules @{
        Type        = 'Array'
        DisplayName = 'Allowed Rules'
        Description = 'Validate and allowed destination ports, portocols and destination addresses. Example: [{''destinationPorts'':[''443'',''1688''],''protocols'':[''TCP''],''enforceDestinationAddresses'':false,''destinationAddresses'':[]},{''destinationPorts'':[''123''],''protocols'':[''UDP''],''enforceDestinationAddresses'':true,''destinationAddresses'':[''13.86.101.172'']}]'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/firewallPolicies/ruleCollectionGroups' {

        Require 'ruleCollections, FirewallPolicyFilterRuleCollection.rules, ruleType and 4 more are as required' {
            Each 'ruleCollections[*]' {
                AnyOf {
                    AnyOf {
                        Field FirewallPolicyFilterRuleCollection.rules | Must -Not -Exist
                        Count 'FirewallPolicyFilterRuleCollection.rules[*]' -Where {
                            Field ruleType | Must -Not -Be 'NetworkRule'
                        } | Must -Not -Be 0
                    }
                    AllOf {
                        Field priority | Must -BeGreaterThan (Param Priority) -Strict
                        Each 'FirewallPolicyFilterRuleCollection.rules[*]' {
                            Count -Value (Param AllowedRules) -Name allowedRule -Where {
                                Field NetworkRule.destinationPorts | Must -Exist
                                Count 'NetworkRule.destinationPorts[*]' -Where {
                                    Element | Must -Not -BeIn (Arm '[array(current(''allowedRule'').destinationPorts)]')
                                } | Must -Be 0
                                Field NetworkRule.ipProtocols | Must -Exist
                                Count 'NetworkRule.ipProtocols[*]' -Where {
                                    Element | Must -Not -BeIn (Arm '[array(current(''allowedRule'').protocols)]')
                                } | Must -Be 0
                                AnyOf {
                                    Arm '[current(''allowedRule'').enforceDestinationAddresses]' | Must -Be $false
                                    AllOf {
                                        Field NetworkRule.destinationAddresses | Must -Exist
                                        Count 'NetworkRule.destinationAddresses[*]' -Where {
                                            Element | Must -Not -BeIn (Arm '[array(current(''allowedRule'').destinationAddresses)]')
                                        } | Must -Be 0
                                    }
                                }
                            } | Must -Not -Be 0
                        }
                    }
                }
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/azure-firewall-policy-should-only-allow-user-defined-standard-ports-and-fqdns-within-network-rules/azurepolicy.json in Azure/Community-Policy, under the MIT licence.