0
votes

This challenge is regarding Azure and Azure DevOps but I would imagine this happening on similar platforms (AWS, GCP, Github, Gitlab, etc.)

I am currently using Azure DevOps Pipelines but I am facing a problem with interacting with resources behind firewalls (either IP restricted or virtual network restricted). As Azure Pipeline spins up a new VM it requires me to whitelist that given public IP for that newly spun up machine each time I do a run. It is very janky to accommodate for this whitelisting as I am creating Azure Pipelines as submodules for reproducibility purpose extending templates from one project and using it in multiple. Terraform state needs to access configurations on restricted resources, hence throwing access denied messages.

I have looked into the following to solve the challenge and my thoughts about them:

What are your thoughts on solving this challenge?

1
If you are using hosted agent created in inside the network (or paired with) of the VM there should be no need for public ip?magnarwium
I am using Microsoft Hosted Agents on Azure DevOps. This is considered a machine outside Azure just like any other machine (virtual or not). I need to allow for this machine to communicate to the restricted resources in the best possible manner.Sebastian Balle
Maybe you could specify exactly which resources it is you need access to as "regular" terraform would mainly perform provisioning against the Azure Control plane and not necessarilly execute configuration on the resources themselves. There might be workarounds or other technologies that could help.pijemcolu
It might be a container within a storage account which is restricted.Sebastian Balle

1 Answers

1
votes

You can use scripts to get the ip of the cloud agents. And dynamically whitelist the ip address for your azure storage account using Azure PowerShel or Azure Cli. See below example:

1, Add Azure Powershell task before Terraform task in your azure devops pipeline to get the agent's ip address and add whitelist for azure storage account.

- task: AzurePowerShell@5
  displayName: 'Azure PowerShell script: InlineScript copy'
  inputs:
    azureSubscription: 'Microsoft-Azure'
    ScriptType: InlineScript
    Inline: |
     $ip = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip #get agent ip
     #add ip to whitelist
     Add-AzStorageAccountNetworkRule -ResourceGroupName "myresourcegroup" -AccountName "mystorageaccount" -IPAddressOrRange $ip

    azurePowerShellVersion: LatestVersion

2, Add another azure powershell task at the end of your pipeline to remove the whitelist.

- task: AzurePowerShell@5
  displayName: 'Azure PowerShell script: InlineScript copy'
  inputs:
    azureSubscription: 'Microsoft-Azure'
    ScriptType: InlineScript
    Inline: |
     $ip = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
     
     Remove-AzStorageAccountNetworkRule -ResourceGroupName "myresourcegroup" -AccountName "mystorageaccount" -IPAddressOrRange $ip

    azurePowerShellVersion: LatestVersion

Check document here for more information.

The IP ranges for cloud agents changes weekly. You can also check the weekly file and update the whitelist ip address manually. Check here for more information.