Skip to content

Instantly share code, notes, and snippets.

@azurekid
Last active April 28, 2022 15:25
Show Gist options
  • Save azurekid/d33d1792c80b7503928a09c8928eaef9 to your computer and use it in GitHub Desktop.
Save azurekid/d33d1792c80b7503928a09c8928eaef9 to your computer and use it in GitHub Desktop.
Dump DevOps pipeline credentials
pr: none
trigger: none
variables:
serviceConnection: '<NameOfYourServiceConnection>'
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: stage1
displayName: SecureHats Pipeline Credentials
dependsOn: []
jobs:
- job: ServiceConnectionCredentials
displayName: "Retrieve Service Connection password"
steps:
- task: AzureCLI@2
inputs:
azureSubscription: ${{ variables.serviceConnection }}
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
Write-Host "##[section]Copy the code block between the '\\\' to decrypt the values locally is PowerShell:"
Write-Host "##[section]Copy the code block between the '\\\' to decrypt the values locally is PowerShell:"
Write-Host ""
Write-Host '\\\
##[section]$credentials = [ordered]@{
##[section] tenantId = [System.Text.ASCIIEncoding]::ASCII.GetString([System.Convert]::FromBase64String("'$([System.Convert]::ToBase64String([System.Text.ASCIIEncoding]::ASCII.GetBytes([System.Environment]::GetEnvironmentVariable("tenantId"))))'"))
##[section] serviceprincipalId = [System.Text.ASCIIEncoding]::ASCII.GetString([System.Convert]::FromBase64String("'$([System.Convert]::ToBase64String([System.Text.ASCIIEncoding]::ASCII.GetBytes([System.Environment]::GetEnvironmentVariable("servicePrincipalId"))))'"))
##[section] servicePrincipalKey = [System.Text.ASCIIEncoding]::ASCII.GetString([System.Convert]::FromBase64String("'$([System.Convert]::ToBase64String([System.Text.ASCIIEncoding]::ASCII.GetBytes([System.Environment]::GetEnvironmentVariable("servicePrincipalKey"))))'"))
##[section]}
##[section]Write-Output -InputObject $credentials
##[section]
##[section] $psCred = New-Object System.Management.Automation.PSCredential($credentials.serviceprincipalId , (ConvertTo-SecureString $credentials.servicePrincipalKey -AsPlainText -Force))
##[section] Add-AzAccount -Credential $psCred -TenantId $credentials.tenantId -ServicePrincipal
\\\'
addSpnToEnvironment: true
@azurekid
Copy link
Author

Updated the code and included the creation of a credential object in the pipeline output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment