Bylaw

Gallery › Network › Azure Community Policy

Virtual Machine NIC must have NSG

Converted Policy Audit Deny Disabled Mode Indexed Version 1.0.0

This policy prevents NIC attached to a vm to not have an NSG.

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

Azure Policy JSONazurepolicy.json · 52 lines
{
  "name": "03d46a5e-3a84-43ee-83b5-d7f7ee7c8a57",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Virtual Machine NIC must have NSG",
    "description": "This policy prevents NIC attached to a vm to not have an NSG.",
    "metadata": {
      "category": "Network",
      "createdBy": "",
      "updatedBy": null,
      "updatedOn": null,
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "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": [
          {
            "equals": "Microsoft.Network/networkInterfaces",
            "field": "type"
          },
          {
            "exists": "false",
            "field": "Microsoft.Network/networkInterfaces/networkSecurityGroup.id"
          },
          {
            "exists": "true",
            "field": "Microsoft.Network/networkInterfaces/virtualMachine.id"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw03d46a5e-3a84-43ee-83b5-d7f7ee7c8a57.bylaw.ps1 · 31 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '03d46a5e-3a84-43ee-83b5-d7f7ee7c8a57' {

    Properties @{
        DisplayName = 'Virtual Machine NIC must have NSG'
        Description = 'This policy prevents NIC attached to a vm to not have an NSG.'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/networkInterfaces' {

        When {
            Field networkSecurityGroup.id | Must -Not -Exist
        }

        Require 'virtualMachine.id does not exist' {
            Field virtualMachine.id | Must -Not -Exist
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/virtual-machine-nic-must-have-nsg/azurepolicy.json in Azure/Community-Policy, under the MIT licence.