Azure DevOps 访问两个具有重复秘密名称的 Key Vault

Azure DevOps accessing two Key Vaults with duplicate secret names

我目前有一个需要访问两个不同 Key Vault 的 Azure 构建管道。不幸的是,我试图访问的两个机密的名称都是 SQLUserName。我试图将这些作为参数传递给 python 脚本。我正在寻找一种在传递参数时可以限定或区分秘密的方法。

理想情况下,我想访问像 $(ServiceConnection1.SQLUserName) 这样的限定变量,但我找不到这方面的任何信息。

我一直在研究重命名变量的方法,因此我可能 运行 第一个 Key Vault 任务然后将 $(SQLUserName) 重命名为 $(SQLUserNamefoo) 然后 运行 第二个 Key Vault任务并将 $(SQLUserName) 重命名为 $(SQLUserNamebar)。我似乎无法在 YML 中重命名变量。

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'
strategy:
  matrix:
    Python37:
      python.version: '3.7'

steps:
- task: AzureKeyVault@1
  inputs:
    azureSubscription: 'ServiceConnection1' 
    KeyVaultName: 'Vault1'
    SecretsFilter: '*'
    RunAsPreJob: true
- task: AzureKeyVault@1
  inputs:
    azureSubscription: 'ServiceConnection2'
    KeyVaultName: 'Vault2'
    SecretsFilter: '*'
    RunAsPreJob: true

- task: UsePythonVersion@0
  inputs:
    versionSpec: '$(python.version)'
  displayName: 'Use Python $(python.version)'

- script: |
    python -m pip install --upgrade pip
    pip install -r requirements.txt
  displayName: 'Install dependencies'

- task: PythonScript@0
  inputs:
      scriptSource: 'filePath'
      scriptPath: 'keyVaultTest.py'
      arguments: '$(SQLUserName))'
      #ideal way to work
      arguments: '$(SQLUserName1) $(SQLUserName2))'

Azure DevOps accessing two Key Vaults with duplicate secret names

我们可以添加一个带 Logging Command 的内联 powershell 任务,以便在 first [=15] 之后将变量 SQLUserNamefoo 设置为值 $(SQLUserName) =] 任务.

Write-Host ("##vso[task.setvariable variable=SQLUserNamefoo]$(SQLUserName)")

然后我们可以在接下来的任务中使用$(SQLUserNamefoo)

并且我们可以设置另一个内联 powershell 任务以在 second AzureKeyVault 任务之后将变量 SQLUserNamebar 设置为 $(SQLUserName) =31=]

Write-Host ("##vso[task.setvariable variable=SQLUserNamebar]$(SQLUserName)")

作为测试,我创建了一个 Key Vault SQLUserName,值为 Leotest。为了验证 SQLUserNamefoo 设置为 $(SQLUserName),我在变量中定义了 SQLUserNamefoo,值为 123:

并添加另一个powershell任务将SQLUserNamefoo的值输出到一个txt文件中进行验证:

cd $(System.DefaultWorkingDirectory)

New-Item $(System.DefaultWorkingDirectory)\temp -type directory

cd temp

New-Item a.txt -type file

write-output $(SQLUserNamefoo)| out-file -filepath $(System.DefaultWorkingDirectory)\temp\a.txt

txt文件的结果: