Bylaw

Gallery › Network › Azure Landing Zones Library

Enforce specific configuration of Network Security Groups (NSG)

Converted Policy Disabled Modify Mode All Version 1.0.0

This policy enforces the configuration of Network Security Groups (NSG).

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

Azure Policy JSONModify-NSG.alz_policy_definition.json · 127 lines
{
  "name": "Modify-NSG",
  "properties": {
    "description": "This policy enforces the configuration of Network Security Groups (NSG).",
    "displayName": "Enforce specific configuration of Network Security Groups (NSG)",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureChinaCloud",
        "AzureUSGovernment"
      ],
      "category": "Network",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "effect": {
        "allowedValues": [
          "Modify",
          "Disabled"
        ],
        "defaultValue": "Modify",
        "metadata": {
          "description": "Enable or disable the execution of the policy",
          "displayName": "Effect"
        },
        "type": "String"
      },
      "nsgRuleAccess": {
        "allowedValues": [
          "Allow",
          "Deny"
        ],
        "defaultValue": "Deny",
        "type": "string"
      },
      "nsgRuleDescription": {
        "defaultValue": "Deny any outbound traffic to the Internet",
        "type": "string"
      },
      "nsgRuleDestinationAddressPrefix": {
        "defaultValue": "Internet",
        "type": "string"
      },
      "nsgRuleDestinationPortRange": {
        "defaultValue": "*",
        "type": "string"
      },
      "nsgRuleDirection": {
        "allowedValues": [
          "Inbound",
          "Outbound"
        ],
        "defaultValue": "Outbound",
        "type": "string"
      },
      "nsgRuleName": {
        "defaultValue": "DenyAnyInternetOutbound",
        "type": "string"
      },
      "nsgRulePriority": {
        "defaultValue": 1000,
        "type": "integer"
      },
      "nsgRuleProtocol": {
        "defaultValue": "*",
        "type": "string"
      },
      "nsgRuleSourceAddressPrefix": {
        "defaultValue": "*",
        "type": "string"
      },
      "nsgRuleSourcePortRange": {
        "defaultValue": "*",
        "type": "string"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "equals": "Microsoft.Network/networkSecurityGroups",
            "field": "type"
          },
          {
            "count": {
              "field": "Microsoft.Network/networkSecurityGroups/securityRules[*]"
            },
            "equals": 0
          }
        ]
      },
      "then": {
        "details": {
          "conflictEffect": "audit",
          "operations": [
            {
              "field": "Microsoft.Network/networkSecurityGroups/securityRules[*]",
              "operation": "add",
              "value": {
                "name": "[parameters('nsgRuleName')]",
                "properties": {
                  "access": "[parameters('nsgRuleAccess')]",
                  "description": "[parameters('nsgRuleDescription')]",
                  "destinationAddressPrefix": "[parameters('nsgRuleDestinationAddressPrefix')]",
                  "destinationPortRange": "[parameters('nsgRuleDestinationPortRange')]",
                  "direction": "[parameters('nsgRuleDirection')]",
                  "priority": "[parameters('nsgRulePriority')]",
                  "protocol": "[parameters('nsgRuleProtocol')]",
                  "sourceAddressPrefix": "[parameters('nsgRuleSourceAddressPrefix')]",
                  "sourcePortRange": "[parameters('nsgRuleSourcePortRange')]"
                }
              }
            }
          ],
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/4d97b98b-1d4f-4787-a291-c67834d212e7"
          ]
        },
        "effect": "[parameters('effect')]"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawModify-NSG.bylaw.ps1 · 72 lines
# Converted from Modify-NSG.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'Modify-NSG' {

    Properties @{
        DisplayName = 'Enforce specific configuration of Network Security Groups (NSG)'
        Description = 'This policy enforces the configuration of Network Security Groups (NSG).'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'All'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureChinaCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

    Parameter Effect @{
        AllowedValues = @('Modify', 'Disabled')
        Default       = 'Modify'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter NsgRuleAccess @{
        Type          = 'string'
        AllowedValues = @('Allow', 'Deny')
        Default       = 'Deny'
    }
    Parameter NsgRuleDescription @{
        Type    = 'string'
        Default = 'Deny any outbound traffic to the Internet'
    }
    Parameter NsgRuleDestinationAddressPrefix @{
        Type    = 'string'
        Default = 'Internet'
    }
    Parameter NsgRuleDestinationPortRange @{
        Type    = 'string'
        Default = '*'
    }
    Parameter NsgRuleDirection @{
        Type          = 'string'
        AllowedValues = @('Inbound', 'Outbound')
        Default       = 'Outbound'
    }
    Parameter NsgRuleName @{
        Type    = 'string'
        Default = 'DenyAnyInternetOutbound'
    }
    Parameter NsgRulePriority @{
        Type    = 'integer'
        Default = 1000
    }
    Parameter NsgRuleProtocol @{
        Type    = 'string'
        Default = '*'
    }
    Parameter NsgRuleSourceAddressPrefix @{
        Type    = 'string'
        Default = '*'
    }
    Parameter NsgRuleSourcePortRange @{
        Type    = 'string'
        Default = '*'
    }

    Resource 'Microsoft.Network/networkSecurityGroups' {

        Require 'securityRules is as required' {
            Count 'securityRules[*]' | Must -Not -Be 0
        }
    }

    Modify -Role 'Network Contributor' -ConflictEffect Audit -Effect (Param Effect) {
        Field 'securityRules[*]' | Add ([ordered]@{ name = '[parameters(''nsgRuleName'')]'; properties = [ordered]@{ access = '[parameters(''nsgRuleAccess'')]'; description = '[parameters(''nsgRuleDescription'')]'; destinationAddressPrefix = '[parameters(''nsgRuleDestinationAddressPrefix'')]'; destinationPortRange = '[parameters(''nsgRuleDestinationPortRange'')]'; direction = '[parameters(''nsgRuleDirection'')]'; priority = '[parameters(''nsgRulePriority'')]'; protocol = '[parameters(''nsgRuleProtocol'')]'; sourceAddressPrefix = '[parameters(''nsgRuleSourceAddressPrefix'')]'; sourcePortRange = '[parameters(''nsgRuleSourcePortRange'')]' } })
    }
}

Source: policy_definitions/Modify-NSG.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.