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

时间:2020-12-18 18:38:29

标签: azure azure-devops yaml azure-keyvault

我目前有一个 azure 构建管道,需要访问两个不同的 Key Vault。不幸的是,我试图访问的两个秘密的名称都是 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))'

1 个答案:

答案 0 :(得分:1)

<块引用>

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

我们可以添加一个带有 Logging Command 的内联 powershell 任务,以在 first SQLUserNamefoo 任务之后将变量 $(SQLUserName) 设置为值 AzureKeyVault。< /p>

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

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

我们可以设置另一个内联 powershell 任务,在 second SQLUserNamebar 任务之后设置变量 $(SQLUserName) 的值为 AzureKeyVault

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

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

enter image description here

并添加另一个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 文件结果:

enter image description here