Bylaw

Gallery › Network › Azure Community Policy

Enforce network security groups to have a DENY RDP security rule.

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

Enforce network security groups to have a DENY RDP security rule.

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

Azure Policy JSONazurepolicy.json · 109 lines
{
  "name": "712e27b2-aff0-4b43-9a34-301afc5638f8",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Enforce network security groups to have a DENY RDP security rule.",
    "description": "Enforce network security groups to have a DENY RDP security rule.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "priority": {
        "type": "Integer",
        "metadata": {
          "displayName": "priority",
          "description": "Priority for the deny RDP security rule (Example 100)."
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Network/networkSecurityGroups"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Network/networkSecurityGroups/securityRules",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/4d97b98b-1d4f-4787-a291-c67834d212e7"
          ],
          "existenceCondition": {
            "allof": [
              {
                "field": "Microsoft.Network/networkSecurityGroups/securityRules/access",
                "equals": "Deny"
              },
              {
                "field": "Microsoft.Network/networkSecurityGroups/securityRules/direction",
                "equals": "Inbound"
              },
              {
                "field": "Microsoft.Network/networkSecurityGroups/securityRules/destinationPortRange",
                "equals": "3389"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "nsgName": {
                  "value": "[field('name')]"
                },
                "priority": {
                  "value": "[parameters('priority')]"
                }
              },
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "nsgName": {
                    "type": "string"
                  },
                  "priority": {
                    "type": "int"
                  }
                },
                "resources": [
                  {
                    "name": "[concat(parameters('nsgName'),'/Port_3389_Deny')]",
                    "type": "Microsoft.Network/networkSecurityGroups/securityRules",
                    "apiVersion": "2019-11-01",
                    "properties": {
                      "description": "Deny RDP",
                      "protocol": "*",
                      "sourcePortRange": "*",
                      "destinationPortRange": "3389",
                      "sourceAddressPrefix": "*",
                      "destinationAddressPrefix": "*",
                      "access": "Deny",
                      "priority": "[parameters('priority')]",
                      "direction": "Inbound"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}
Bylaw712e27b2-aff0-4b43-9a34-301afc5638f8.bylaw.ps1 · 37 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '712e27b2-aff0-4b43-9a34-301afc5638f8' {

    Properties @{
        DisplayName = 'Enforce network security groups to have a DENY RDP security rule.'
        Description = 'Enforce network security groups to have a DENY RDP security rule.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter Priority @{
        Type        = 'Integer'
        DisplayName = 'priority'
        Description = 'Priority for the deny RDP security rule (Example 100).'
    }
    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 'access is Deny, direction is Inbound and destinationPortRange is 3389' {
            Field access | Must -Be 'Deny'
            Field direction | Must -Be 'Inbound'
            Field destinationPortRange | Must -Be '3389'
        }
        Deployment -TemplateFile './templates/712e27b2-aff0-4b43-9a34-301afc5638f8.json' -Parameters ([ordered]@{
                nsgName  = (Arm '[field(''name'')]')
                priority = (Param Priority)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Network/enforce-network-security-groups-to-have-a-deny-rdp-security-rule/azurepolicy.json in Azure/Community-Policy, under the MIT licence.