jsonazurepolicyazure-policy

Azure policy for naming conventions for subnets to start with "snet", but allow subnet names like "AzureBastionSubnet", "AzureFirewallSubnet", etc


I'm creating an Azure policy that enforces a naming convention for my subnets. The subnets should follow the format: snet---. To do this I used the regular expression:

[if(and(greaterOrEquals(length(split(field('name'), '-')), 0), equals(split(field('name'), '-')[0], 'snet')), if(and(greaterOrEquals(length(split(field('name'), '-')), 1), contains(parameters('unit'), split(field('name'), '-')[1])),  if(and(greaterOrEquals(length(split(field('name'), '-')), 2), contains(parameters('env'), split(field('name'), '-')[2])), 'isValid', 'Environment not accepted')]

This works as expected. However when I try to allow the creation of subnets with names like "AzureBastionSubnet", I get an error that the name should follow the format above mentioned.

Here is the code of the policy:

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "equals": "Microsoft.Network/virtualNetworks/subnets",
          "field": "type"
        },
        {
          "anyOf": [
            {
              "value": "(equals(field('name'),'AzureBastionSubnet')",
              "notEquals": "true"
            },
            {
              "notEquals": "isValid",
              "value": "[if(and(greaterOrEquals(length(split(field('name'), '-')), 0), equals(split(field('name'), '-')[0], 'snet')), if(and(greaterOrEquals(length(split(field('name'), '-')), 1), contains(parameters('unit'), split(field('name'), '-')[1])),  if(and(greaterOrEquals(length(split(field('name'), '-')), 2), contains(parameters('env'), split(field('name'), '-')[2])), 'isValid', 'Environment not accepted')]"
            }
          ]
        }
      ]
    },
    "then": {
      "effect": "deny"
    }
  },
  "parameters": {
    "unit": {
      "type": "Array",
      "metadata": {
        "displayName": "unit",
        "description": null
      }
    },
    "env": {
      "type": "Array",
      "metadata": {
        "displayName": "env",
        "description": null
      }
    }
  }

}

Is there an issue on how I'm expressing that the value "AzureBastionSubnet" is also allowed?

Thank you in advance for the help.


Solution

  • Azure policy for naming conventions for subnets to start with "snet", but allow subnet names like AzureBastionSubnet

    Here is the updated policy to create a subnet with a name starting with snet based on the unit and environment conditions, and also allowing specific names like AzureBastionSubnet. In my case, I have given the unit name as IT and the env as test in the parameters

        {
          "mode": "All",
          "policyRule": {
            "if": {
              "allOf": [
                {
                  "equals": "Microsoft.Network/virtualNetworks/subnets",
                  "field": "type"
                },
                {
                  "not": {
                    "allOf": [
                      {
                        "equals": "AzureBastionSubnet",
                        "field": "name"
                      },
                      {
                        "notEquals": "isValid",
                        "value": "[if(and(greaterOrEquals(length(split(field('name'), '-')), 1), equals(split(field('name'), '-')[0], 'snet'), contains(parameters('unit'), split(field('name'), '-')[1]), contains(parameters('env'), split(field('name'), '-')[2])), 'isValid', 'Environment not accepted')]"
                      }
                    ]
                  }
                }
              ]
            },
            "then": {
              "effect": "deny"
            }
          },
          "parameters": {
            "unit": {
              "type": "Array",
              "metadata": {
                "displayName": "unit",
                "description": null
              }
            },
            "env": {
              "type": "Array",
              "metadata": {
                "displayName": "env",
                "description": null
              }
            }
          }
        }
    

    The policy allows the creation of a subnet only if the specified conditions are met. For example, 'snet-IT-test' is considered valid, while 'snet-IT-demo' is not valid. Similarly, 'AzureBastionSubnet' is valid, but 'AzureBastionSubnet1' is not valid. The policy allows subnet creation only under valid conditions; otherwise, it denies the creation of the subnet.

    Subnet result in Portal

    enter image description here

    enter image description here