Logo

dev-resources.site

for different kinds of informations.

Azure DevOps Pipeline deployments to Azure App Services with Access Restrictions

Published at
11/26/2023
Categories
azuredevops
appservices
azureapp
azure
Author
riosengineer
Author
12 person written this
riosengineer
open
Azure DevOps Pipeline deployments to Azure App Services with Access Restrictions

Intro 📖

When you enable Access Restrictions onto your Azure App Services (such as an Azure Function or Web App) you cut off any inbound traffic from the internet. This can pose an extra challenge when needing to deploy code via pipelines as you now have no inbound public access to the SCM (source control manager aka Kudo service).

This guide will go through how to deploy with these restrictions in place via Microsoft hosted Azure DevOps agents.

Azure DevOps Agents 🚀

Firstly comes the challenge of continuous deployment with these restrictions in place. Azure DevOps Agents hosted by Microsoft state:

Meaning if we try to deploy to the app service now we have these restrictions we’ll likely get a 401 unauthorized error, as we’ve disabled public access to the app service (both the frontend and backend-SCM where you’d typically deploy code to).

There’s typically two main options to get around this:

  • Deploy a VM and install an ADO self-hosted agent on the vNet of the app so it’s coming from an ‘internal’ address rather than public (via Private Endpoint vNet integration) or;
  • Create tasks in your pipeline to allow the public IP of the MS agent on the app firewall dynamically during runtime

The blog will focus on the latter as Microsoft have blogged on the first solution here if you’re interested.

_ Note: if you’re happy to use the service tag ‘ AzureCloud ‘ on your sites access rules, this will be sufficient enough to allow public ADO agents access without the need for the tasks below._

_ Note2: There is a weekly JSON file that posts Azure IP ranges, of which you could extrapolate the ADO IP space for your region in an automated solution instead_. This isn’t covered in this blog post however.

The solution 🛠

As part of my deployment tasks I dynamically grab the ADO agent IP during runtime, store this into a variable, add it onto the apps access rules, let deployment run through & then remove the rule after. All via the pipeline deployment so it’s fully automated whilst maintaining our security posture.

Prerequisites:

  • Allow public access: on
  • Unmatched rule action to Deny (super important)

This keeps the app service unavailable from public access due to the Deny rule match. Additionally we will have a ‘deny all’ rule to block all traffic.

(N_ote: the Main site & Advanced tool site can have different rule matches, to avoid this you can enable ‘Use main site rules’_ to consolidate under one site rule set. Below is just an example screenshot for reference):

This will allow the CLI to dynamically add the ADO agent IP to the rule as a higher priority allowing the agent access for deployment.

ADO Tasks 🏗

Now for the tasks themselves.

Grab the agent IP & add it to a pipeline task variable:

Using the website http://ipinfo.io/ip we are able to get the public IP address at the runtime and set it into a pipeline variable called ‘address’ for use later in the pipeline tasks.

          - task: Bash@3
             name: ADOAgentIP
             inputs:
              targetType: 'inline'
              script: |
                ipaddress=$(curl -s http://ipinfo.io/ip)
                echo Azure DevOps Agent IP: $ipaddress
                echo "##vso[task.setvariable variable=address;isOutput=true;]$ipaddress"
Enter fullscreen mode Exit fullscreen mode

YAML

Add the rule to the app service:

Using az webapp CLI we’re able to add the IP to the sites access rule restrictions at a priority level higher than the ‘Deny all’ rule seen from earlier in the post. Using the task name from earlier ‘ADOAgentIP’ and the pipeline variable ‘address’ we can create a dynamic variable to add the IP address $(ADOAgentIP.address) in the CLI.

           - task: AzureCLI@2
             displayName: Add Azure DevOps Agent IP to allow list
             inputs:
               azureSubscription: $(azureServiceConnection)
               scriptType: 'bash'
               scriptLocation: 'inlineScript'
               inlineScript: |
                az account set --subscription $(prodSub)
                az webapp config access-restriction add -g example-rg -n example-func-001 --rule-name ado-agent --action Allow --ip-address "$(ADOAgentIP.address)" --priority 100
Enter fullscreen mode Exit fullscreen mode

YAML

Removing the rule after your deployment steps:

Lastly, using the CLI once more, we can remove our rule using the rule name. In my example ‘ado-agent’.

           - task: AzureCLI@2
             displayName: Remove Azure DevOps Agent IP
             inputs:
               azureSubscription: $(azureServiceConnection)
               scriptType: 'bash'
               scriptLocation: 'inlineScript'
               inlineScript: |
                az webapp config access-restriction remove -g example-rg -n example-func-001 --rule-name ado-agent
Enter fullscreen mode Exit fullscreen mode

YAML

Conclusion 🏁

That’s it! Quite a simple solution which works really nicely as a starting point. The tasks above are mostly an example, and could be improved on in your own workflow – such as making sure you always remove the IP even if the app service deployment fails. However, this is a good starting point to build out from.

I hope others find this useful.

The post Azure DevOps Pipeline deployments to Azure App Services with Access Restrictions appeared first on Rios Engineer.

azuredevops Article's
30 articles in total
Favicon
Creating SBOM with sbom-tool and CycloneDX on Azure DevOps
Favicon
Public IP Address in Azure | Understanding Public IP Address in Azure VM
Favicon
Terraform - Mastering Idempotency Violations - Handling Resource Conflicts and Failures in Azure
Favicon
Azure Devops Converting Classic Pipelines to Yaml Based Pipeline
Favicon
Register Azure DevOps Agents with Service Principal Secret !
Favicon
The Ultimate Guide to Azure DevOps: Key Benefits for Software Development Projects
Favicon
Azure DevOps Zero to Hero Series
Favicon
Azure DevOps Series - Azure Boards
Favicon
cp: cannot stat 'bhfonlineshop': No such file or directory. Azure devops pipeline error
Favicon
How to Maintain Issue Hierarchy Between Jira and Azure DevOps
Favicon
Azure DevOps | Using Terraform
Favicon
Azure DevOps | Installing Postman and Newman using npm
Favicon
Azure DevOps | Running JMeter Test Collection using JMeter Docker Image
Favicon
Azure DevOps | Running a Postman Collection using Newman Docker Image
Favicon
Azure DevOps | Deploy Postman Tests in Azure DevOps Test Plans
Favicon
Exploring Microsoft Azure AI Capabilities Using React, Github Actions, Azure Static Apps and Azure AI
Favicon
Azure DevOps Zero-to-Hero
Favicon
Azure pipelines - Passing variables across stages
Favicon
Terraform - Keep dependencies up to date with Dependabot (Azure DevOps version)
Favicon
Automate Azure VM Password Rotation with PowerShell and Azure DevOps
Favicon
Investigating az-cli performance on the hosted Azure Pipelines and GitHub Runners
Favicon
Azure DevOps Pipeline deployments to Azure App Services with Access Restrictions
Favicon
Step by step for build , test and deploy using azuredevops pipeline
Favicon
Step by Step instruction hosting on aks cluster for begineers
Favicon
Deploying to Azure from Azure DevOps without secrets
Favicon
VSBuild task fails on self-hosted Azure Pipelines Agent
Favicon
Azure devops pipeline for dotnet with cicd
Favicon
Bicep modules with PSRule – Testing, documentation, CI Pipeline & examples
Favicon
Conectando o VS Community ao Azure DevOps
Favicon
Como clonar um projeto no Azure DevOps

Featured ones: