Using Key-Vault RBAC (Role Based Access Control) in Azure Devops pipeline

Hitesh N 40 Reputation points
2025-05-27T19:53:16.3733333+00:00

Question - Using Key-Vault in Azure Devops pipeline

I had Question for Using Key-Vault RBAC ( Role Based Access Control)

in Azure Devops pipeline.

  1. I created Key-Vault in Azure Portal with Azure RBAC ( Role Based Access Control)
  2. Also have added the Access Control (IAM) Policy - Key Vault Secrets User , Key Vault Secrets Officer , Key Vault Administrator for the Service Principal and my User-ID.
  3. In the Resource-Group - Added Key Vault Secrets User, Key Vault Administrator for the Service Principal and my User-ID.
  4. Created the Key and Secret.
  5. In Azure Devops YAML File for the below AzureKeyVault task, after running the Devops PipeLine getting the below Error.
  • task: AzureKeyVault@2 inputs: azureSubscription: 'xxxxx' KeyVaultName: 'xxxx' SecretsFilter: '*' RunAsPreJob: true
  1. Error -

Downloading secrets using: https://azure-fabric-kv-test.vault.azure.net/secrets?maxresults=25&api-version=2016-10-01.

##[error]Get secrets failed. Error: Caller is not authorized to perform action on resource.

If role assignments, deny assignments or role definitions were changed recently, please observe propagation time.

Caller: appid=***;oid=XXX;iss=https://sts.windows.net/0xxxxxx/

Action: 'Microsoft.KeyVault/vaults/secrets/readMetadata/action'

Resource: '/subscriptions/4XXXXX/resourcegroups/XXXX/providers/microsoft.keyvault/vaults/XXXXX'

Assignment: (not found)

DenyAssignmentId: null

DecisionReason: null

Vault: aXXXXXX;location=eastus

. The specified Azure service connection needs to have Get, List secret management permissions on the selected key vault. To set these permissions, download the ProvisionKeyVaultPermissions.ps1 script from build/release logs and execute it, or set them from the Azure portal..

Uploading /home/vsts/work/1/ProvisionKeyVaultPermissions.ps1 as attachment

Finishing: AzureKeyVault

Azure DevOps
0 comments No comments

8 answers

Sort by: Most helpful
  1. Durga Reshma Malthi 11,375 Reputation points Microsoft External Staff Moderator
    2025-06-03T07:52:33.19+00:00

    Hi Hitesh N

    To process all secrets retrieved from Azure Key Vault in a loop - encoding and decoding each one - you can use a foreach loop in PowerShell.

    # Retrieve all secrets from the Key Vault
    $secrets = Get-AzKeyVaultSecret -VaultName "xxxx"
    # Loop through each secret, encode and decode the values
    foreach ($secret in $secrets) {
        $secretName = $secret.Name
        $secretValue = Get-AzKeyVaultSecret -VaultName "xxxx" -Name $secretName -AsPlainText
        Write-Host "Original Value for $secretName: $secretValue"
        # Encoding
        $encodedSecret = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($secretValue))
        Write-Host "Encoded Value for $secretName: $encodedSecret"
        # Decoding
        $decodedValue = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($encodedSecret))
        Write-Host "Decoded Value for $secretName: $decodedValue"
    }
    

    Hope this helps!

    Please Let me know if you have any queries.

    If you found the information helpful, please click "Upvote" on the post to let us know and consider accepting the answer as the token of appreciation. Thank You.

    Was this answer helpful?


  2. Hitesh N 40 Reputation points
    2025-06-02T17:45:36.4333333+00:00

    @Durga Reshma Malthi Thanks Durga for your response, followed the above steps after getting the KeyVaultSecret was getting the values as ***. After encoding and decoding the secret and passing the decoded values worked.

    Had Question, how to retrieve all the secrets from the KeyVault and pass it through the encoding and decoding in loop.

    After the below command , was getting the output as below, how to fetch all the values of secret after the | ( which is ***), currently there are 3 secrets and how to pass it to $encodedSecret and $decodedvalue in foreach loop.

    For-each *** value --> to Encode and Decode it.

    $keyVaultValue = Get-AzKeyVaultSecret -VaultName "xxxx"

    $keyVaultValue | % {Write-Output "$($.name) | $(Get-AzKeyVaultSecret -VaultName $.VaultName -Name $_.Name -AsPlainText)"}

    Write-Host "Value of keyVaultValueSecret ": $keyVaultValueSecret

    Value of keyVaultValueSecret :

    DBUser | ***

    DBPass | ***

    DBPort | ***

    #EncodedSecret

    $encodedSecret = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($keyVaultValue))

    #$encodedSecret = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($secret))

    Write-Host "Value of encodedSecret" $encodedSecret

    #DecodedSecret

    $decodedvalue = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($encodedSecret))

    Write-Host "Value of DecodedSecret" $decodedvalue

    Was this answer helpful?

    0 comments No comments

  3. Hitesh N 40 Reputation points
    2025-05-29T18:15:10.47+00:00

    User's image

    User's image

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.