Deploying to a Secure Azure App Service - The Solutions for Locked-Down Networks

Share on:

Introduction

Deploying to a locked-down Azure App Service is a common challenge for teams working in secure environments. Azure App Services often need to be protected within virtual networks, limiting access only to trusted IPs or networks. While this is great for security, it complicates deployments, especially when using Microsoft-hosted agents in Azure DevOps. These hosted agents run on dynamic IPs, which aren't easily whitelisted, leading to frequent connectivity issues during deployment.

In this guide, we'll explore how to overcome these obstacles by using IP whitelisting, Azure DevOps service connections, and alternatives like self-hosted agents or VPN gateways. By following this approach, you can securely deploy applications without losing efficiency.


The Problem: Deploying to a Locked-Down Network

In a locked-down network, only specific IPs or networks are allowed to communicate with your App Service. The challenge arises when you try to deploy from Azure DevOps, as Microsoft-hosted agents don’t have static IP addresses. These IPs change frequently, making it difficult to whitelist them for secure access. If you don't address this issue, your deployments will fail due to blocked network traffic.


Step-by-Step Guide: Deploying to a Locked-Down App Service

1. Setting Up Access Restrictions

To make your App Service secure, access restrictions must be applied to limit traffic from outside sources. But when deploying from Microsoft-hosted agents, you need to make sure these agents can connect to your App Service.

Steps to Whitelist Azure DevOps Hosted Agents:

  1. Open the Azure Portal and navigate to your Azure App Service.
  2. Go to Networking > Access Restrictions.
  3. Add a new rule:
    • Action: Allow
    • Source: Service Tag (AzureCloud)
    • Priority: 100
    • Name: Allow AzureDevOpsHostedAgents

For tighter security, you can download the list of IPs used by Azure DevOps hosted agents and manually whitelist them.

2. Creating a Service Connection in Azure DevOps

To allow Azure DevOps pipelines to deploy, you’ll need to create a Service Connection that authenticates securely with Azure.

Steps:

  1. In Azure DevOps, go to Project Settings > Service Connections.
  2. Click New Service Connection and select Azure Resource Manager.
  3. Choose Service Principal (automatic).
  4. Follow the prompts to select your subscription and App Service.
  5. Name your service connection (e.g., AzureAppServiceConnection).

This connection allows Azure DevOps pipelines to authenticate securely, making the deployment smoother.

3. Setting Up Your YAML Pipeline

Now that the App Service is set to allow Azure DevOps agents, and the service connection is ready, let’s create a YAML pipeline to deploy your application. Here’s how to include dynamic IP whitelisting and the deployment process.

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

variables:
  azureServiceConnection: 'AzureAppServiceConnection'
  resourceGroup: 'myResourceGroup'
  appServiceName: 'myAppService'

steps:
# Step 1: Add IP restriction dynamically
- task: AzureCLI@2
  displayName: 'Whitelist Azure DevOps IPs'
  inputs:
    azureSubscription: $(azureServiceConnection)
    scriptType: 'ps'
    scriptLocation: 'inlineScript'
    inlineScript: |
      $ips = (Invoke-RestMethod -Uri https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519 | ConvertFrom-Json).Values | Where-Object { $_.name -like "*AzureDevOps*"}
      foreach ($ip in $ips) {
        az webapp config access-restriction add --resource-group $(resourceGroup) --name $(appServiceName) --rule-name "AllowDevOps" --action Allow --ip-address $ip --priority 100
      }

# Step 2: Deploy application
- task: AzureWebApp@1
  inputs:
    azureSubscription: $(azureServiceConnection)
    appType: 'webApp'
    appName: $(appServiceName)
    package: '$(System.DefaultWorkingDirectory)/**/*.zip'

# Step 3: Remove IP restriction dynamically
- task: AzureCLI@2
  displayName: 'Remove Azure DevOps IPs'
  inputs:
    azureSubscription: $(azureServiceConnection)
    scriptType: 'ps'
    scriptLocation: 'inlineScript'
    inlineScript: |
      az webapp config access-restriction remove --resource-group $(resourceGroup) --name $(appServiceName) --rule-name "AllowDevOps"

Key Points:

  • Step 1: Adds Azure DevOps IPs temporarily.
  • Step 2: Deploys the application using the service connection.
  • Step 3: Cleans up by removing the temporary IP restrictions.

Alternatives to Microsoft-Hosted Agents

If managing dynamic IP whitelisting feels too cumbersome, there are two alternatives: using self-hosted agents or deploying via a VPN or Application Gateway.

1. Self-Hosted Agents

A self-hosted agent is a dedicated server running inside your virtual network, which means it can access the App Service directly without the need for IP whitelisting.

Pros:

  • Direct access to the virtual network.
  • No need for managing IP changes or whitelisting.

Cons:

  • You need to maintain and monitor the agent yourself.
  • Involves extra infrastructure and management.

How to Set Up:

  1. Set up a VM in your virtual network.
  2. Install the Azure DevOps agent on the VM.
  3. Register the agent in Azure DevOps.

2. Deploy via VPN Gateway or Application Gateway

Using a VPN Gateway or Application Gateway is another way to securely access the App Service without exposing it to the public.

Pros:

  • Provides secure, scalable access.
  • No need to manage dynamic IPs.

Cons:

  • More complex to set up.
  • Higher cost due to additional infrastructure.

Setup Overview:

  • VPN Gateway: Create a VPN connection between Azure DevOps and your virtual network.
  • Application Gateway: Configure the gateway to expose the App Service endpoints securely.

Pros and Cons Summary

Here’s a quick comparison of the three methods:

Method Pros Cons
Microsoft-Hosted Agents + IP Whitelisting Simple to set up, no extra infrastructure required Requires dynamic IP updates and manual configuration
Self-Hosted Agents Direct access to the App Service, no IP whitelisting Requires extra infrastructure and maintenance
VPN Gateway or Application Gateway Secure, scalable, ideal for large enterprises Complex to configure and more expensive

Conclusion

Deploying to a locked-down Azure App Service can seem tricky, but with the right approach, you can make it work securely and efficiently. For smaller teams or simpler setups, IP whitelisting with Microsoft-hosted agents is a quick and effective solution. For larger organizations or highly secure environments, self-hosted agents or a VPN gateway may provide more scalability and control.

No matter the method you choose, make sure to implement best practices for security and efficiency, ensuring smooth deployments to your locked-down Azure environment.

Let me know in the comments if you’ve faced similar challenges or have any questions!