azure-devopsazure-pipelinesazure-devops-rest-apiazure-devops-extensions

how to give the pipelines permanently access to new securefiles uploaded?


We are using azuredevops securefiles to update the secret values in Azure keyvault and created the same. we are using parametrized value for our securfile as we have to use this between different teams, and because of this we are using some naming pattern and each applications should follow the same each time they want to update the secret , also the secure file is not giving option to allow its content for next execution, we have to delete the existing secure file and need to upload secure file with same name by changing the value in it.

because of this, whenever we are trying to execute the pipeline, its asking for granting the permission for pipeline onto the securefile and which is manually need to do. Any dolution here?


Solution

  • Updating the disucussions in the answer that I am afraid there is no built-in functionality to set pipelines permission dynamically to have all the newly uploaded secure files inherit those settings.

    Since the resources permissions are checked at stage level, you may consider adding a stage: Preparation with the PowerShell script below to assign the current pipeline with the permission to use a secure file. Here is the API that the script calls.

    trigger: none
    
    pool:
      vmImage: ubuntu-latest
    
    variables:
      testSecureFile: testSecureFile.json
    
    stages:
    - stage: Preparation
      jobs:
      - job: PreparePipelinePermissionsForSecureFile
        displayName: Grant permission for current pipeline to consume ${{ variables.testSecureFile }}
        steps:
        - pwsh: |
            $headers = @{
                'Authorization' = 'Bearer ' + '$(System.AccessToken)'
                'Content-Type' = 'application/json'
            }
    
            $secureFileURL = "$(System.CollectionUri)/$(System.TeamProject)/_apis/distributedtask/securefiles?api-version=6.0-preview.1"
    
            $secureFiles = Invoke-RestMethod -Method Get -Uri $secureFileURL -Headers $headers
            $testSecureFile = $secureFiles.value | Where-Object { $_.name -eq "$(testSecureFile)" }
    
            if ($testSecureFile) {
                $secureFileId = $testSecureFile.id
                Write-Output "Secure file ID: $secureFileId"
            } else {
                Write-Output "Secure file 'testSecureFile.json' not found."
            }
    
            $URL = "$(System.CollectionUri)/$(System.TeamProject)/_apis/pipelines/pipelinepermissions?api-version=7.2-preview.1"
    
            $body = @(
                @{
                    "resource" = @{
                        "type" = "securefile"
                        "id" = "$secureFileId"
                    }
                    "pipelines" = @(
                        @{
                            "authorized" = $true
                            "id" = $(System.DefinitionId)
                        }
                    )
                }
            )
    
            # $body = @(
            #     @{
            #         "resource" = @{
            #             "type" = "securefile"
            #             "id" = "$secureFileId"
            #         }
            #         "allPipelines" = @{
            #             "authorized" = $true
            #         }
            #     }
            # )
    
            $jsonBody = ConvertTo-Json($body) -Depth 10
            $response = Invoke-RestMethod -Method Patch -Uri $URL -Headers $headers -Body $jsonBody
            $response | ConvertTo-Json -Depth 10
    
    - stage: UseSecureFileInDownstreamStage
      jobs:
      - job: UseSecureFile
        displayName: Download secure file - ${{ variables.testSecureFile }}
        steps:
        - task: DownloadSecureFile@1
          inputs:
            secureFile: '$(testSecureFile)'
    

    Before running a pipeline to consume the secure file that is newly uploaded, you may Edit the Pipeline permissions for this secure file.

    Image

    You may either grant permissions for a selection of pipeline(s) to use this resource or open access to all pipelines.

    Besides, even though the secure files in each upload share the same name, once uploaded to Azure Pipelines Library, they will be considered as different resources with different secureFileIds in the URL. Since they are considered as different resources without any pipeline permissions compared to the previous version, we will have to reassign permissions for the new secure file resources.