使用 vnet 和防火墙保护时,Azure DevOps Build Pipeline 无法从 Key Vault 获取机密

Pro*_*dip 5 firewall azure-keyvault azure-devops

当使用 vnet 和防火墙保护时,无法从 Key Vault 获取机密。

我想使用 DevOps Build Pipeline 任务中存储在 Key Vault 中的秘密,我想遵循安全最佳实践和深度防御。作为安全最佳实践,我希望可以从选定的虚拟网络、选定的 azure 服务和受信任的 Internet ip 访问密钥保管库。当然,我会使用服务主体和适当的权限(列表/获取)。

不幸的是,Azure DevOps 不是值得信赖的服务之一。因此,我的替代方案是将 DevOps IP 列入白名单。我发现我的 DevOps 位于美国东部 2 区域,我下载了 Azure 数据中心 IP(使用美国东部 2 过滤)。US East 2 大约有 285 个 IP。Key Vault 防火墙对您可以添加的防火墙规则数量有限制,它是 127 个!所以,我倒霉了!

目前,只有在我允许所有网络的情况下,我才能在构建管道中从密钥保管库中获取机密!是的,我仍然需要通过身份验证才能获得秘密,但我在纵深防御上失败了。我真的需要将密钥保管库锁定到受信任的网络,但我不能。为什么?我添加的防火墙规则不能超过 127 个(覆盖该区域),而且 DevOps 不是值得信赖的 Azure 服务之一!

vip*_*pes 7

我想我应该对 Prodip 提供的解决方案进行一些修改。这依赖于这样一个事实:当您请求机密时,az 客户端会很友好地告诉您客户端 IP 地址是什么,即:

az keyvault secret show -n "a-known-client-secret" --vault-name "$keyVaultName"

Attempting to get value for known secret from key vault: '******'
ERROR: Client address is not authorized and caller is not a trusted service.
Client address: 1.1.1.1
Caller: appid=***;oid=****;iss=https://sts.windows.net/***/
Vault: ******;location=******
Run Code Online (Sandbox Code Playgroud)

这是我的 bash 脚本(whitelist-agent-for-key-vault.sh):

#!/usr/bin/env bash

## By default the Azure DevOps IP addresses are NOT whitelisted for key vault access. So even if the service principal has access, you won't get past the firewall.
## The solution is to temporarily add the build agent IP address to the key vault firewall, and remove it when the pipeline is complete. 

if [[ $(uname -s) == "Linux" ]]; then
    azcmd="az"
else
    # If we're in a bash shell on Windows, az commands don't work, but we can call the az.cmd batch file directly from git Bash if we can find it...
    azcmd=$(where az.cmd)
fi

# Are we removing rather than setting?
if [[ $1 == "-r" ]]; then
    if [[ -z "$3" ]]; then
        echo "Build agent IP address is empty, no whitelist entry to remove from key vault: '$2'"
    else
        echo "Removing key vault '$2' network rule for DevOps build agent IP address: '$3'"

        # Remember to specify CIDR /32 for removal
        "$azcmd" keyvault network-rule remove -n $2 --ip-address $3/32
    fi
    exit 0
fi

keyVaultName=$1

########################################################################
##### This is the known secret which we request from the key vault #####
########################################################################

knownSecret="<My known secret>"

echo "Attempting to get value for known secret from key vault: '$keyVaultName'"

# Attempt to show secret - if it doesn't work, we are echoed our IP address on stderror, so capture it
secretOutput=$("$azcmd" keyvault secret show -n "$knownSecret" --vault-name "$keyVaultName" 2>&1)
buildAgentIpAddress=$(echo $secretOutput | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")

set -euo pipefail

if [[ ! -z "$buildAgentIpAddress" ]]; then
    # Temporarily whitelist Azure DevOps IP for key vault access.
    # Note use of /32 for CIDR = 1 IP address. If we omit this Azure adds it anyway and fails to match on the IP when attempting removal.
    echo "Azure DevOps IP address '$buildAgentIpAddress' is blocked. Attempting to whitelist..."
    "$azcmd" keyvault network-rule add -n $keyVaultName --ip-address $buildAgentIpAddress/32

    # Capture the IP address as an ADO variable, so that this can be undone in a later step
    echo "##vso[task.setvariable variable=buildAgentIpAddress]$buildAgentIpAddress"
else
    # We didn't find the IP address - are we already whitelisted?
    secretValue=$(echo $secretOutput | grep -o "value")

    if [[ -z "$secretValue" ]]; then
        echo "Unexpected response from key vault whitelist request, json attribute 'value' not found. Unable to whitelist build agent - response was: '$secretOutput'"
        exit 1
    fi
fi
Run Code Online (Sandbox Code Playgroud)

将IP添加到白名单的方法如下:

  # Add agent IP to key vault white list
  - task: AzureCLI@2
    displayName: Add Azure DevOps build agent IP to key vault white list
    inputs:
      azureSubscription: ${{ parameters.azureSubscription }}
      scriptType: bash
      scriptLocation: scriptPath
      scriptPath: $(Pipeline.Workspace)/server-build-tools/drop/build-scripts/whitelist-agent-for-key-vault.sh
      arguments: '$(keyVaultName)'
Run Code Online (Sandbox Code Playgroud)

这是我从白名单中删除IP的方法

      - task: AzureCLI@2
        displayName: Remove Azure DevOps build agent IP from key vault white list
        condition: always()
        inputs:
          azureSubscription: ${{ parameters.azureSubscription }}
          scriptType: bash
          scriptLocation: scriptPath
          scriptPath: $(Pipeline.Workspace)/server-build-tools/drop/build-scripts/whitelist-agent-for-key-vault.sh
          arguments: '-r "$(keyVaultName)" "$(buildAgentIpAddress)"'
Run Code Online (Sandbox Code Playgroud)

注意事项:

  • 这依赖于 Azure DevOps 服务主体已被授予对密钥保管库机密的读取访问权限
  • 将值替换knownSecret为您已知秘密的名称

奖金:

这可以使用 Azure CLI 运行,并已在适用于 Linux 的 Azure DevOps 和在 Git bash 下运行的 Windows 构建代理上进行了测试。通常,如果您尝试在 Git Bash 中运行“az”命令,您只会得到“找不到命令”。我想要一个适用于两者的解决方案,因为由于 Linux / Windows 构建要求,我需要共享代码。


Jac*_*Jia 2

唯一的解决方案是使用自托管代理

您可以创建一个虚拟机并在其中安装代理客户端。然后您可以在DevOps代理池中添加一个新的代理并使用它。

由于自我代理在您的 Azure VM 中运行,因此它肯定位于虚拟网络中。通过这种方式,您可以将虚拟网络添加到 Key Vault 防火墙白名单中。


事实上,我认为你没有必要这样做。因为,如果没有访问策略,任何人都无法访问您的密钥保管库。所以,理论上来说,它是足够安全的。

但是,如果您必须使用防火墙和网络规则保护密钥保管库,则可以使用自托管代理。