Introduction
Unfortunately, Azure DevOps does not have a SaaS offering running in Azure Government. The only options are to spin up Azure DevOps server in your Azure Government tenant or connect the Azure DevOps commercial PaaS offering (specifically Azure Pipelines) to Azure Government. Your customer may object to the latter approach; the purpose of this post is to provide you with additional ammunition in making a case that you can securely use commercial Azure DevOps with Azure Government.
Throughout this blog post, the biggest question you should always keep in mind is where is my code running?
Scenario
Take a simple example; a pipeline that calls a PowerShell script to create a key vault and randomly generates a secret to be used later (such as for a password during the creation of a VM)
add-type -AssemblyName System.Web $rgName = "AisDwlBlog-rg" $kvName = "AisDwlBlog-kv" $pw = '"' + [System.Web.Security.Membership]::GeneratePassword(16, 4) + '"' az group create --name $rgName --location eastus --output none az keyvault create --name $kvName --resource-group $rgName --output none az keyvault set-policy --name $kvName --secret-permissions list get --object-id 56155951-2544-4008-9c9a-e53c8e8a1ab2 --output none az keyvault secret set --vault-name $kvName --name "VMPassword" --value $pw
The easiest way we can execute this script is to create a pipeline using a Microsoft-Hosted agent with an Azure PowerShell task that calls our deployment script:
pool: name: 'AisDwl' steps: - task: AzureCLI@2 inputs: azureSubscription: 'DwlAzure' scriptType: 'ps' scriptLocation: 'scriptPath' scriptPath: '.\Deploy.ps1'
When we execute this, note that output from that PowerShell script flows back into the Pipeline:
To circle back to that question that should still be in your mind….where is my code running? In this case, it is running in a Virtual Machine or container provided by Microsoft. If you have a customer that requires all interactions with potentially sensitive data to be executed in a more secure environment (such as IL-4 in Azure Government), you are out of luck as that VM/Container for the hosted build agent is not certified at any DoD Impact Level. Thus, we have to look at other options, where our deployment scripts can run in a more secure environment.
I’ll throw a second wrench into things…did you see the bug in my Deploy.ps1 script above? I forgot to add --output none
to the last command (setting the password in the key vault). When I run the pipeline, this is shown in the output:
Not good! In an ideal world, everyone writing these scripts would be properly handling output, but we need to code defensively to handle unintended situations. Also, think about any error messages that might bubble back to the output of the pipeline.
Option 1
Azure Pipelines provide the capability to run pipelines in self-hosted agents, which could be a VM or container-managed by you/your organization. If you set up this VM in a USGov or DoD region of Azure, your code is running in either an IL-4 or IL-5 compliant environment. However, we can’t simply spin up a build agent and call it a day. As with the Microsoft-hosted build agent, the default behavior of the pipeline still returns output to Azure DevOps. If there is ever an issue like I just demonstrated, or an inadvertent Write-Output or Write-Error, or an unhandled exception containing sensitive information, that will be displayed in the output of the pipeline. We need to prevent that information from flowing back to Azure Pipelines. Fortunately, there is a relatively simple fix for this: instead of having a task to execute your PowerShell scripts directly, create a wrapper/bootstrapper PowerShell script.
The key feature of the bootstrapper is that it executes the actual deployment script as a child process and captures the output from that child process, preventing any output or errors from flowing back into your Pipeline. In this case, I am simply writing output to a file on the build agent, but a more real-world scenario would be to upload that file to a storage account.
try { & "$PSScriptRoot\Deploy.ps1" | Out-File "$PSScriptRoot\log.txt" -append Write-Output "Deployment complete" } catch { Write-Error "there was an error" }
The biggest disadvantage of this approach is the additional administrative burden of setting up and maintaining one (or more) VMs/containers to use as self-hosted build agents.
Option 2
If you would prefer to avoid managing infrastructure, another option is to run your deployment scripts in an Azure Automation Account. Your Pipeline (back to running in a Microsoft-hosted agent) starts an Azure Automation Runbook to kick off the deployment. The disadvantage of this approach is that all of your deployment scripts must either be staged to the Automation Account as modules or converted into “child” runbooks to be executed by the “bootstrapper” runbook. Also, keep in mind that the bootstrapper runbook must take the same preventative action of capturing output from any child scripts or runbooks to prevent potentially sensitive information from flowing back to the Pipeline.
Sample code of calling a runbook:
$resourceGroupName = "automation" $automationAccountName = "dwl-aaa" $runbookName = "Deployment-Bootstrapper" $job = Start-AzAutomationRunbook -AutomationAccountName $automationAccountName -ResourceGroupName $resourceGroupName -Name $runbookName -MaxWaitSeconds 120 -ErrorAction Stop $doLoop = $true While ($doLoop) { Start-Sleep -s 5 $job = Get-AzAutomationJob -ResourceGroupName $resourceGroupName –AutomationAccountName $automationAccountName -Id $job.JobId $status = $job.Status $doLoop = (($status -ne "Completed") -and ($status -ne "Failed") -and ($status -ne "Suspended") -and ($status -ne "Stopped")) } if ($status -eq "Failed") { Write-Error "Job Failed" }
The Deployment script code running as an Azure Automation Runbook (Note that this has been converted to Azure PowerShell as the AzureCLI isn’t supported in an Automation Account Runbook):
$Conn = Get-AutomationConnection -Name AzureRunAsConnection Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint add-type -AssemblyName System.Web $rgName = "AisDwlBlog-rg" $kvName = "AisDwlBlog-kv" $pw = [System.Web.Security.Membership]::GeneratePassword(16, 4) $rg = Get-AzResourceGroup -Name $rgName if ($rg -eq $null) { $rg = New-AzResourceGroup -Name $rgName -Location EastUs } $kv = Get-AzKeyVault -VaultName $kvName -ResourceGroupName $rgName if ($kv -eq $null) { $kv = New-AzKeyVault -Name $kvName -ResourceGroupName $rgName -location EastUs } Set-AzKeyVaultAccessPolicy -VaultName $kvName -PermissionsToSecrets list,get,set -ServicePrincipalName $Conn.ApplicationID $securePw = ConvertTo-SecureString -String $pw -AsPlainText -Force Set-AzKeyVaultSecret -VaultName $kvName -Name "VMPassword" -SecretValue $securePw