Bylaw

Gallery › Network › Azure Community Policy

Deploy NSG rule

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

This policy deploys a rule to an NSG.

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

Azure Policy JSONazurepolicy.json · 295 lines
{
  "name": "a644e5ed-8102-4074-95ad-e08759d38b94",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy NSG rule",
    "description": "This policy deploys a rule to an NSG.",
    "metadata": {
      "category": "Network",
      "createdBy": "",
      "updatedBy": null,
      "updatedOn": null,
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "access": {
        "type": "String",
        "metadata": {
          "description": "The network traffic is allowed or denied. - Allow or Deny",
          "displayName": "access"
        }
      },
      "destinationAddressPrefix": {
        "type": "Array",
        "metadata": {
          "description": "The destination address prefix. CIDR or destination IP range. Asterisk '*' can also be used to match all source IPs. Default tags such as 'VirtualNetwork', 'AzureLoadBalancer' and 'Internet' can also be used.",
          "displayName": "destinationAddressPrefix"
        }
      },
      "destinationPortRange": {
        "type": "Array",
        "metadata": {
          "description": "The destination port or range. Integer or range between 0 and 65535. Asterisk '*' can also be used to match all ports.",
          "displayName": "destinationPortRange"
        }
      },
      "direction": {
        "type": "String",
        "metadata": {
          "description": "The direction of the rule. The direction specifies if rule will be evaluated on incoming or outgoing traffic. - Inbound or Outbound",
          "displayName": "direction"
        }
      },
      "priority": {
        "type": "Integer",
        "metadata": {
          "description": "The priority of the rule. The value can be between 100 and 4096. The priority number must be unique for each rule in the collection. The lower the priority number, the higher the priority of the rule.",
          "displayName": "priority"
        }
      },
      "protocol": {
        "type": "String",
        "metadata": {
          "description": "Network protocol this rule applies to. - Tcp, Udp, Icmp, Esp, *, Ah",
          "displayName": "protocol"
        }
      },
      "ruledescription": {
        "type": "String",
        "metadata": {
          "description": "The description of the rule.",
          "displayName": "description"
        }
      },
      "rulename": {
        "type": "String",
        "metadata": {
          "description": "This is the name of the security rule itself.",
          "displayName": "Rule Name"
        }
      },
      "sourceAddressPrefix": {
        "type": "Array",
        "metadata": {
          "description": "The CIDR or source IP range. Asterisk '*' can also be used to match all source IPs. Default tags such as 'VirtualNetwork', 'AzureLoadBalancer' and 'Internet' can also be used. If this is an ingress rule, specifies where network traffic originates from.",
          "displayName": "sourceAddressPrefix"
        }
      },
      "sourcePortRange": {
        "type": "Array",
        "metadata": {
          "description": "The source port or range. Integer or range between 0 and 65535. Asterisk '*' can also be used to match all ports.",
          "displayName": "sourcePortRange"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "equals": "Microsoft.Network/networkSecurityGroups",
        "field": "type"
      },
      "then": {
        "details": {
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "access": {
                  "value": "[parameters('access')]"
                },
                "description": {
                  "value": "[parameters('ruledescription')]"
                },
                "destinationAddressPrefix": {
                  "value": "[parameters('destinationAddressPrefix')]"
                },
                "destinationPortRange": {
                  "value": "[parameters('destinationPortRange')]"
                },
                "direction": {
                  "value": "[parameters('direction')]"
                },
                "nsgName": {
                  "value": "[field('name')]"
                },
                "priority": {
                  "value": "[parameters('priority')]"
                },
                "protocol": {
                  "value": "[parameters('protocol')]"
                },
                "rulename": {
                  "value": "[parameters('rulename')]"
                },
                "sourceAddressPrefix": {
                  "value": "[parameters('sourceAddressPrefix')]"
                },
                "sourcePortRange": {
                  "value": "[parameters('sourcePortRange')]"
                }
              },
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "access": {
                    "type": "String"
                  },
                  "description": {
                    "type": "String"
                  },
                  "destinationAddressPrefix": {
                    "type": "Array"
                  },
                  "destinationPortRange": {
                    "type": "Array"
                  },
                  "direction": {
                    "type": "String"
                  },
                  "nsgName": {
                    "type": "string"
                  },
                  "priority": {
                    "type": "Integer"
                  },
                  "protocol": {
                    "type": "String"
                  },
                  "rulename": {
                    "type": "String"
                  },
                  "sourceAddressPrefix": {
                    "type": "Array"
                  },
                  "sourcePortRange": {
                    "type": "Array"
                  }
                },
                "resources": [
                  {
                    "apiVersion": "2019-11-01",
                    "properties": {
                      "access": "[parameters('access')]",
                      "description": "[parameters('description')]",
                      "destinationAddressPrefix": "[if(equals(length(parameters('destinationAddressPrefix')),1),parameters('destinationAddressPrefix'),json('null'))]",
                      "destinationAddressPrefixs": "[if(equals(length(parameters('destinationAddressPrefix')),1),json('null'),parameters('destinationAddressPrefix'))]",
                      "destinationPortRange": "[if(equals(length(parameters('destinationPortRange')),1),parameters('destinationPortRange'),json('null'))]",
                      "destinationPortRanges": "[if(equals(length(parameters('destinationPortRange')),1),json('null'),parameters('destinationPortRange'))]",
                      "direction": "[parameters('direction')]",
                      "priority": "[parameters('priority')]",
                      "protocol": "[parameters('protocol')]",
                      "sourceAddressPrefix": "[if(equals(length(parameters('sourceAddressPrefix')),1),parameters('sourceAddressPrefix'),json('null'))]",
                      "sourceAddressPrefixs": "[if(equals(length(parameters('sourceAddressPrefix')),1),json('null'),parameters('sourceAddressPrefix'))]",
                      "sourcePortRange": "[if(equals(length(parameters('sourcePortRange')),1),parameters('sourcePortRange'),json('null'))]",
                      "sourcePortRanges": "[if(equals(length(parameters('sourcePortRange')),1),json('null'),parameters('sourcePortRange'))]"
                    },
                    "rulename": "[concat(parameters('nsgName'),'/',parameters('rulename'))]",
                    "type": "Microsoft.Network/networkSecurityGroups/securityRules"
                  }
                ]
              }
            }
          },
          "existenceCondition": {
            "count": {
              "field": "Microsoft.Network/networkSecurityGroups/securityRules[*]",
              "where": {
                "allOf": [
                  {
                    "equals": "[parameters('rulename')]",
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].name"
                  },
                  {
                    "equals": "[parameters('protocol')]",
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].protocol"
                  },
                  {
                    "anyOf": [
                      {
                        "equals": true,
                        "value": "[equals(field('Microsoft.Network/networkSecurityGroups/securityRules[*].sourcePortRange'), parameters('sourcePortRange'))]"
                      },
                      {
                        "equals": true,
                        "value": "[equals(field('Microsoft.Network/networkSecurityGroups/securityRules[*].sourcePortRanges'), parameters('sourcePortRange'))]"
                      }
                    ]
                  },
                  {
                    "anyOf": [
                      {
                        "equals": true,
                        "value": "[equals(field('Microsoft.Network/networkSecurityGroups/securityRules[*].destinationPortRange'), parameters('destinationPortRange'))]"
                      },
                      {
                        "equals": true,
                        "value": "[equals(field('Microsoft.Network/networkSecurityGroups/securityRules[*].destinationPortRanges'), parameters('destinationPortRange'))]"
                      }
                    ]
                  },
                  {
                    "anyOf": [
                      {
                        "equals": true,
                        "value": "[equals(field('Microsoft.Network/networkSecurityGroups/securityRules[*].sourceAddressPrefix'), parameters('sourceAddressPrefix'))]"
                      },
                      {
                        "equals": true,
                        "value": "[equals(field('Microsoft.Network/networkSecurityGroups/securityRules[*].sourceAddressPrefixes'), parameters('sourceAddressPrefix'))]"
                      }
                    ]
                  },
                  {
                    "anyOf": [
                      {
                        "equals": true,
                        "value": "[equals(field('Microsoft.Network/networkSecurityGroups/securityRules[*].destinationAddressPrefix'), parameters('destinationAddressPrefix'))]"
                      },
                      {
                        "equals": true,
                        "value": "[equals(field('Microsoft.Network/networkSecurityGroups/securityRules[*].destinationAddressPrefixes'), parameters('destinationAddressPrefix'))]"
                      }
                    ]
                  },
                  {
                    "equals": "[parameters('access')]",
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].access"
                  },
                  {
                    "equals": "[parameters('priority')]",
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].priority"
                  },
                  {
                    "equals": "[parameters('direction')]",
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].direction"
                  }
                ]
              }
            },
            "notEquals": 0
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/4d97b98b-1d4f-4787-a291-c67834d212e7"
          ],
          "type": "Microsoft.Network/networkSecurityGroups/securityRules"
        },
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylawa644e5ed-8102-4074-95ad-e08759d38b94.bylaw.ps1 · 111 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'a644e5ed-8102-4074-95ad-e08759d38b94' {

    Properties @{
        DisplayName = 'Deploy NSG rule'
        Description = 'This policy deploys a rule to an NSG.'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Parameter Access @{
        Type        = 'String'
        DisplayName = 'access'
        Description = 'The network traffic is allowed or denied. - Allow or Deny'
    }
    Parameter DestinationAddressPrefix @{
        Type        = 'Array'
        DisplayName = 'destinationAddressPrefix'
        Description = 'The destination address prefix. CIDR or destination IP range. Asterisk ''*'' can also be used to match all source IPs. Default tags such as ''VirtualNetwork'', ''AzureLoadBalancer'' and ''Internet'' can also be used.'
    }
    Parameter DestinationPortRange @{
        Type        = 'Array'
        DisplayName = 'destinationPortRange'
        Description = 'The destination port or range. Integer or range between 0 and 65535. Asterisk ''*'' can also be used to match all ports.'
    }
    Parameter Direction @{
        Type        = 'String'
        DisplayName = 'direction'
        Description = 'The direction of the rule. The direction specifies if rule will be evaluated on incoming or outgoing traffic. - Inbound or Outbound'
    }
    Parameter Priority @{
        Type        = 'Integer'
        DisplayName = 'priority'
        Description = 'The priority of the rule. The value can be between 100 and 4096. The priority number must be unique for each rule in the collection. The lower the priority number, the higher the priority of the rule.'
    }
    Parameter Protocol @{
        Type        = 'String'
        DisplayName = 'protocol'
        Description = 'Network protocol this rule applies to. - Tcp, Udp, Icmp, Esp, *, Ah'
    }
    Parameter Ruledescription @{
        Type        = 'String'
        DisplayName = 'description'
        Description = 'The description of the rule.'
    }
    Parameter Rulename @{
        Type        = 'String'
        DisplayName = 'Rule Name'
        Description = 'This is the name of the security rule itself.'
    }
    Parameter SourceAddressPrefix @{
        Type        = 'Array'
        DisplayName = 'sourceAddressPrefix'
        Description = 'The CIDR or source IP range. Asterisk ''*'' can also be used to match all source IPs. Default tags such as ''VirtualNetwork'', ''AzureLoadBalancer'' and ''Internet'' can also be used. If this is an ingress rule, specifies where network traffic originates from.'
    }
    Parameter SourcePortRange @{
        Type        = 'Array'
        DisplayName = 'sourcePortRange'
        Description = 'The source port or range. Integer or range between 0 and 65535. Asterisk ''*'' can also be used to match all ports.'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/networkSecurityGroups'

    DeployIfNotExists 'Microsoft.Network/networkSecurityGroups/securityRules' -Role 'Network Contributor' -Effect (Param Effect) {
        Require 'securityRules, name, protocol and 11 more are as required' {
            Count 'Microsoft.Network/networkSecurityGroups/securityRules[*]' -Where {
                Field name | Must -Be (Param Rulename)
                Field protocol | Must -Be (Param Protocol)
                AnyOf {
                    Arm '[equals(field(''Microsoft.Network/networkSecurityGroups/securityRules[*].sourcePortRange''), parameters(''sourcePortRange''))]' | Must -Be $true
                    Arm '[equals(field(''Microsoft.Network/networkSecurityGroups/securityRules[*].sourcePortRanges''), parameters(''sourcePortRange''))]' | Must -Be $true
                }
                AnyOf {
                    Arm '[equals(field(''Microsoft.Network/networkSecurityGroups/securityRules[*].destinationPortRange''), parameters(''destinationPortRange''))]' | Must -Be $true
                    Arm '[equals(field(''Microsoft.Network/networkSecurityGroups/securityRules[*].destinationPortRanges''), parameters(''destinationPortRange''))]' | Must -Be $true
                }
                AnyOf {
                    Arm '[equals(field(''Microsoft.Network/networkSecurityGroups/securityRules[*].sourceAddressPrefix''), parameters(''sourceAddressPrefix''))]' | Must -Be $true
                    Arm '[equals(field(''Microsoft.Network/networkSecurityGroups/securityRules[*].sourceAddressPrefixes''), parameters(''sourceAddressPrefix''))]' | Must -Be $true
                }
                AnyOf {
                    Arm '[equals(field(''Microsoft.Network/networkSecurityGroups/securityRules[*].destinationAddressPrefix''), parameters(''destinationAddressPrefix''))]' | Must -Be $true
                    Arm '[equals(field(''Microsoft.Network/networkSecurityGroups/securityRules[*].destinationAddressPrefixes''), parameters(''destinationAddressPrefix''))]' | Must -Be $true
                }
                Field access | Must -Be (Param Access)
                Field priority | Must -Be (Param Priority)
                Field direction | Must -Be (Param Direction)
            } | Must -Not -Be 0
        }
        Deployment -TemplateFile './templates/a644e5ed-8102-4074-95ad-e08759d38b94.json' -Parameters ([ordered]@{
                access                   = (Param Access)
                description              = (Param Ruledescription)
                destinationAddressPrefix = (Param DestinationAddressPrefix)
                destinationPortRange     = (Param DestinationPortRange)
                direction                = (Param Direction)
                nsgName                  = (Arm '[field(''name'')]')
                priority                 = (Param Priority)
                protocol                 = (Param Protocol)
                rulename                 = (Param Rulename)
                sourceAddressPrefix      = (Param SourceAddressPrefix)
                sourcePortRange          = (Param SourcePortRange)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Network/deploy-nsg-rule/azurepolicy.json in Azure/Community-Policy, under the MIT licence.