我为我的开发环境设置了 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"}}}
然后我决定回去阅读一些入门指南 like this one ,并发现以下注释:
Note:
Azure Key Vaults that use Azure role-based access control (Azure RBAC) are not supported.
是否无法设置 KeyVault 以允许使用推荐的 RBAC 设置从 Azure Pipeline 代理进行访问?
最佳答案
{"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"}}}
从错误消息来看,您的 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
这是关于 add IP to azure keyvault firewall 方法的文档.
关于azure - 是否可以在 Azure Pipeline Agent 上将 Azure KeyVault 与 RBAC 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77220213/