Home NewsX Manage NSG association on Subnets via Azure Policy

Manage NSG association on Subnets via Azure Policy

by info.odysseyx@gmail.com
0 comment 18 views


This blog article covers how to use a custom Azure Policy to deny the creation of a subnet in a virtual network if the subnet does not have a network security group associated with it.

To create a custom policy, follow these steps:

1) In the Azure portal, access the Azure Policy, Definitions blade.
2) Create a new policy definition.

Photo 1.png

3) Add a definition location (the subscription that will host this policy), a name, and a description.
4) Enable the existing category and select Networking (see below):

Photo 2.png

5) Then add the policy definition below to the Rules field.

memo: You can adjust the parameters below as needed, the example below excludes the following subnets. You can add more subnets as desired.
“Gateway subnet”,
“AzureFirewallSubnet”,
“AzureBastionSubnet”,
“AzureFirewallManagementSubnet”

{
    "mode": "All",
    "parameters": {
        "excludedSubnets": {
            "type": "Array",
            "metadata": {
                "displayName": "Excluded subnets",
                "description": "The list of subnet names to exclude from the policy"
            },
            "defaultValue": [
                "GatewaySubnet",
                "AzureFirewallSubnet",
                "AzureBastionSubnet",
                "AzureFirewallManagementSubnet",
                "YourCustomSubnet"
            ]
        }
    },
    "policyRule": {
        "if": {
            "anyOf": [
                {
                    "allOf": [
                        {
                            "field": "type",
                            "equals": "Microsoft.Network/virtualNetworks"
                        },
                        {
                            "not": {
                                "field": "Microsoft.Network/virtualNetworks/subnets[*].networkSecurityGroup.id",
                                "exists": true
                            }
                        },
                        {
                            "field": "Microsoft.Network/virtualNetworks/subnets[*].name",
                            "notIn": "[parameters('excludedSubnets')]"
                        }
                    ]
                },
                {
                    "allOf": [
                        {
                            "field": "type",
                            "equals": "Microsoft.Network/virtualNetworks/subnets"
                        },
                        {
                            "not": {
                                "field": "Microsoft.Network/virtualNetworks/subnets/networkSecurityGroup.id",
                                "exists": true
                            }
                        },
                        {
                            "field": "name",
                            "notIn": "[parameters('excludedSubnets')]"
                        }
                    ]
                }
            ]
        },
        "then": {
            "effect": "deny"
        }
    }
}

6) Then save the policy.

You can now assign this policy as per your requirements.

1) Azure Policy page and Access Definitions blade -> Select the custom policy you created and click Assign Policy (You can assign it at subscription level or specific resource group as per your business requirement).

Photo 3.png

2) Update the list of excluded subnets when assigning a policy. Go to the Parameters tab, then uncheck the box “Show only parameters that require input or review” and select one of the three dots next to the box “Excluded Subnets”.

Photo 4.png

3) The editor will open. Update the subnet name you want to exclude and click ‘Save’.

Photo 5.png

4) Click Next, then click Next and update the “Non-Compliant Message” to suit your requirements.

Photo 6.png

5) Click Review + Generate and review the output. After confirmation, create the policy assignment. Policy assignment usually takes about 5-15 minutes to take effect.

Update the list of excluded subnets after policy assignment.

1) In the Azure portal, access the Azure Policy, Assignments blade and search for the assignment.
2) Click on the name to open the assignment.

Photo 7.png

3) Select Edit Assignment

Photo 8.png

4) Go to the Parameters tab and uncheck the box “Show only parameters that require input or review” and select from the three dots next to the box “Excluded subnets”.

Photo 9.png

5) The editor will open. Update the subnet name you want to exclude and click ‘Save’.

Photo 10.png

disclaimer

  • The products and options presented in this document are subject to change. This document reflects custom policies for Azure subnets as of September 2024.
  • You can create exceptions for resources if the user has the required permissions, which will render the policy ineffective for that resource.
  • Some subnets managed by Azure services may not require an NSG. Ensure these subnets are added to the list of excluded subnets or use policy exceptions as needed.
  • To avoid unexpected outages and ensure that your requirements are met, we recommend testing your policies in a non-production environment before applying them to your production environment.

References

Tutorial: Creating a custom policy definition – Azure Policy | Microsoft Learn

Create policies programmatically – Azure Policy | Microsoft Learn

Troubleshooting common errors – Azure Policy | Microsoft Learn

Azure Policy overview – Azure Policy | Microsoft Learn





Source link

You may also like

Leave a Comment

Our Company

Welcome to OdysseyX, your one-stop destination for the latest news and opportunities across various domains.

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

Laest News

@2024 – All Right Reserved. Designed and Developed by OdysseyX