我为我的开发环境设置了 Azure Keyvault,使用 RBAC 的建议而不是访问策略进行授权。我已经使用本地代理设置了其他管道,这些代理通过服务主体访问此 keyvault,并且工作得很好,但我的 Azure 代理失败并出现警告:
{"error":{"code":"Forbidden","message":"Client address is not authorized and caller is not a trusted service.\r\nClient address: XXX.XXX.XXX.XXX\r\nCaller: appid=REDACTED;oid=REDACTED;iss=https://sts.windows.net/REDACTED/\r\nVault: MY-KEYVAULT;location=REDACTED","innererror":{"code":"ForbiddenByFirewall"}}}
然后我决定回去阅读一些入门指南比如这个,并发现了以下注释:
Note:
Azure Key Vaults that use Azure role-based access control (Azure RBAC) are not supported.
是否无法设置 KeyVault 以允许使用推荐的 RBAC 设置从 Azure Pipeline 代理进行访问?
{“error”:{“code”:“禁止”,“message”:“客户端地址未经授权,调用者不是受信任的服务。 客户地址:XXX.XXX.XXX.XXX 呼叫者:appid=REDACTED;oid=REDACTED;iss=https://sts.windows.net/REDACTED/ 保管库:MY-KEYVAULT;location=REDACTED","innererror":{"code":"ForbiddenByFirewall"}}}
从错误消息来看,您的 azure keyvault 似乎已启用防火墙。
您可以尝试将Azure Pipeline代理IP添加到Azure keyvault防火墙白名单中。由于Azure Pipelines Agent的IP不固定,您可以使用脚本获取当前IP并添加到Azure KeyVault防火墙。
然后您可以使用 Azure Key Vault 任务下载 Key Vault。
最后,您可以删除 Ip 规则。
这是我的样本:
steps:
- task: AzurePowerShell@5
displayName: 'Azure PowerShell script: InlineScript'
inputs:
azureSubscription: serviceconnection
ScriptType: InlineScript
Inline: |
$IP= Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
$IP
Add-AzKeyVaultNetworkRule -VaultName "keyvaultname" -IpAddressRange "$IP"
preferredAzurePowerShellVersion: 3.1.0
- task: AzureKeyVault@1
displayName: 'Azure Key Vault: kevin0225'
inputs:
azureSubscription: serviceconnection
KeyVaultName: kevin0225
- task: AzurePowerShell@5
displayName: 'Azure PowerShell script: InlineScript'
inputs:
azureSubscription: serviceconnection
ScriptType: InlineScript
Inline: |
$IP= Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
$IP
Remove-AzKeyVaultNetworkRule -VaultName "keyvaultname" -IpAddressRange "$IP"
preferredAzurePowerShellVersion: 3.1.0
这里有一个关于如何将 IP 添加到 azure keyvault 防火墙的文档。