[Azure] Clone/Export Access Restrictions from One Web App to Another

Access Restrictions for Azure Web Apps

Access Restrictions for Azure Web Apps (or Function Apps) is an application-level firewall that allows you to control what IP addresses, virtual networks, or Azure Services can access your web app. Under the Networking → Inbound traffic configuration, you can decide to: (1) allow all incoming public traffic; (2) deny all incoming public traffic; or (3) allow only selected IP ranges or VNets (screenshots below)

Networking → Inbound traffic configuration
Access Restrictions allowing selected networks

In the screenshot above, the example web app has four entries whitelisted: two IP ranges (8.8.8.8/32 and 4.4.4.4/24), a VNet/Subnet, and connections from Azure Logic Apps (with LogicApps Service Tag).

Clone Access Restriction Rules from One App to Another

While the above example web app has only four rules, the list can easily exceed 100 items in real-world scenarios. If we want to clone or export the rules from one app to another in such a case, we need a smarter way than manual adding. Buck inserts can be done programmatically with Powershell/Azure CLI, or by one-time copy and paste using Azure Resource Explorer.

Using Powershell/Azure CLI

If you need to add the same set of access restriction rules to multiple apps, programmatically doing it with Powershell or Azure CLI is probably the best way. In Powershell, we can use Get-AzWebAppAccessRestrictionConfig to get the rules of an app and run Add-AzWebAppAccessRestrictionRule repeatedly to copy the rules. The code snippet below checks ServiceTag, VNet, and IP restrictions and copies them to another app. Note that we only focus on the app’s main site here. If you have different rules for the SCM site (i.e., the Kudu console or the engine behind App Service for deployment), you need to check $config.ScmSiteAccessRestrictions.

# Connect to the Azure subscription before running the script
# Connect-AzAccount -Subscription "<subscription-name-or-id>"

$sourceResourceGroup = "<source-resource-group-name>"
$sourceWebApp = "<source-web-app-name>"
$targetResourceGroup = "<target-resource-group-name>"
$targetWebApp = "<target-web-app-name"

$config = Get-AzWebAppAccessRestrictionConfig -ResourceGroupName $sourceResourceGroup -Name $sourceWebApp
foreach ($rule in $config.MainSiteAccessRestrictions) {
    # Copy ServiceTag restrictions
    if ($rule.Tag -eq "ServiceTag") {
        Add-AzWebAppAccessRestrictionRule `
            -ResourceGroupName $targetResourceGroup `
            -WebAppName $targetWebApp `
            -Name $rule.RuleName `
            -Priority $rule.Priority `
            -Action $rule.Action `
            -ServiceTag $rule.IPAddress  # the value is a Service Tag such as "LogicApps"
    }
    # Copy VNet restrictions
    elseif ($rule.SubnetId) {
        Add-AzWebAppAccessRestrictionRule `
            -ResourceGroupName $targetResourceGroup `
            -WebAppName $targetWebApp `
            -Name $rule.RuleName `
            -Priority $rule.Priority `
            -Action $rule.Action `
            -SubnetId $rule.SubnetId            
    }
    # Copy IP restrictions by matching a CIDR pattern such as "8.8.8.8/32"
    elseif ($rule.IpAddress -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}") {
        Add-AzWebAppAccessRestrictionRule `
            -ResourceGroupName $targetResourceGroup `
            -WebAppName $targetWebApp `
            -Name $rule.RuleName `
            -Priority $rule.Priority `
            -Action $rule.Action `
            -IpAddress $rule.IpAddress
    }
}

If you prefer Azure CLI, az webapp config access-restriction is the command to show, set, add, and remove access restrictions on a web app. Here are more examples of managing access restrictions programmatically: Azure App Service access restrictions – Azure App Service | Microsoft Learn.

Using Azure Resource Explorer

Azure Resource Explorer is a portal that provides the JSON view of your Azure resources. The website is especially useful when you know exactly the properties of the resource you are looking for, and are tired of getting lost in layers of menus in Azure Portal. Beyond data browsing, Azure Resource Explorer allows you to make actual API calls, i.e., to CRUD the resources.

In our case, we have a newly created app that wants to clone the access restriction rules from an existing app. This can be done quickly with a one-time effort with Azure Resource Explorer, as follows:

  1. Go to https://resources.azure.com/
  2. Search for the name of the existing web app. It should take you to the app entry with the hierarchy: subscriptions > [your-subscription] > resourceGroups > [your-resource-group] > providers > Microsoft.Web > sites > [your-web-app-name]
  3. Expand the entry to config > web. The complete hierarchy is:

subscriptions > [your-subscription] > resourceGroups > [your-resource-group] > providers > Microsoft.Web > sites > [your-web-app-name] > config > web

  1. Copy the following five properties from web (as shown in the screenshot below)
  • “ipSecurityRestrictions”
  • “ipSecurityRestrictionsDefaultAction”
  • “scmIpSecurityRestrictions”
  • “scmIpSecurityRestrictionsDefaultAction”
  • “scmIpSecurityRestrictionsUseMain”
Properties for Access Restrictions in the web config
  1. Search for the name of the new app, go to the same web entry, click Edit, and replace the five properties with the copied value. Click Put to perform the update.
Edit the web config
  1. It’s done! Now you should be able to see the cloned rules on the new app from Azure Portal

Reference: IP restrictions can’t be exported/imported · Issue #12276 · MicrosoftDocs/azure-docs

Jim
Jim

Site reliability engineer working in a global gaming and entertainment company

Articles: 18

Leave a Reply

Your email address will not be published. Required fields are marked *