Bylaw

Gallery › Network › Azure Community Policy

Create NSG Rule

Doesn't convert yet Policy Append Audit Deny Disabled Mode All Version 1.0.0

This policy will append a rule to an NSG during creation or update. Only works during creation of the NSG or updates to the NSG as a whole due to the way the Resource Provider works.

ConvertTo-Bylaw can't convert this policy yet:

Azure Policy JSONazurepolicy.json · 105 lines
{
  "name": "b3c42011-a92e-467a-9fe7-cad14c218451",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Create NSG Rule",
    "description": "This policy will append a rule to an NSG during creation or update. Only works during creation of the NSG or updates to the NSG as a whole due to the way the Resource Provider works.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "name": {
        "type": "String"
      },
      "protocol": {
        "type": "String",
        "allowedvalues": [
          "TCP",
          "UDP",
          "ICMP",
          "*"
        ]
      },
      "access": {
        "type": "String",
        "allowedvalues": [
          "Allow",
          "Deny"
        ]
      },
      "priority": {
        "type": "String"
      },
      "direction": {
        "type": "String",
        "allowedvalues": [
          "Inbound",
          "Outbound"
        ]
      },
      "sourcePortRanges": {
        "type": "Array"
      },
      "destinationPortRanges": {
        "type": "Array"
      },
      "sourceAddressPrefixes": {
        "type": "Array"
      },
      "destinationAddressPrefixes": {
        "type": "Array"
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Append, Deny, Audit or Disable the execution of the Policy"
        },
        "allowedValues": [
          "Append",
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Append"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Network/networkSecurityGroups"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": [
          {
            "field": "Microsoft.Network/networkSecurityGroups/securityRules[*]",
            "value": {
              "name": "[parameters('name')]",
              "properties": {
                "protocol": "[parameters('protocol')]",
                "sourcePortRange": "[if(equals(length(parameters('sourcePortRanges')), 1), first(parameters('sourcePortRanges')), '')]",
                "destinationPortRange": "[if(equals(length(parameters('destinationPortRanges')), 1), first(parameters('destinationPortRanges')), '')]",
                "sourceAddressPrefix": "[if(equals(length(parameters('sourceAddressPrefixes')), 1), first(parameters('sourceAddressPrefixes')), '')]",
                "destinationAddressPrefix": "[if(equals(length(parameters('destinationAddressPrefixes')), 1), first(parameters('destinationAddressPrefixes')), '')]",
                "access": "[parameters('access')]",
                "priority": "[parameters('priority')]",
                "direction": "[parameters('direction')]",
                "sourcePortRanges": "[if(greater(length(parameters('sourcePortRanges')), 1), parameters('sourcePortRanges'), take(parameters('sourcePortRanges'),0))]",
                "destinationPortRanges": "[if(greater(length(parameters('destinationPortRanges')), 1), parameters('destinationPortRanges'), take(parameters('destinationPortRanges'),0))]",
                "sourceAddressPrefixes": "[if(greater(length(parameters('sourceAddressPrefixes')), 1), parameters('sourceAddressPrefixes'), take(parameters('sourceAddressPrefixes'),0))]",
                "destinationAddressPrefixes": "[if(greater(length(parameters('destinationAddressPrefixes')), 1), parameters('destinationAddressPrefixes'), take(parameters('destinationAddressPrefixes'),0))]"
              }
            }
          }
        ]
      }
    }
  }
}

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