远程身份验证

注册表vcpkg_from_git() 直接使用 Git 命令行工具来提取远程资源。 其中一些资源可能会受到匿名访问的保护,需要身份验证或凭据。

下面的策略都寻求实现相同的基本目标:git clone https://.... 应在不交互的情况下成功。 这使 vcpkg 能够与身份验证方案的具体细节分开,确保将来与任何其他安全改进的向前兼容性。

预种子 git 凭据

可通过 git credential approve 预种子 git 凭据:

Powershell:

"url=https://github.com`npath=Microsoft/vcpkg`nusername=unused`npassword=$MY_PAT`n" | git credential approve

Bash:

echo "url=https://github.com"$'\n'"path=Microsoft/vcpkg"$'\n'"username=unused"$'\n'"password=$MY_PAT"$'\n' | git credential approve

Bearer 身份验证

对于需要持有者身份验证的系统,可以使用 git config

注意

必须使用 --global 进行这些配置更改

git config --global --unset-all http.<uri>.extraheader
git config --global http.<uri>.extraheader "AUTHORIZATION: bearer <System_AccessToken>"

可以使用各种选项填充 <uri>,例如 https://dev.azure.com/MYORG/。 有关详细信息,请参阅 git config 文档。

(原始来源:在生成过程中对 git 存储库进行身份验证的最佳方式)。

Azure DevOps 用户: 可能需要通过 作业授权范围 启用访问权限,并在 yaml 管道中引用存储库:

resources: 
  repositories:
    - repository: <FRIENDLYNAME>
      type: git
      name: <ORG>/<REPO>
      tag: tags/<TAG>

...

jobs:
 - job: Build
   uses:
     repositories: [<FRIENDLYNAME>]

使用 VCPKG_KEEP_ENV_VARSVCPKG_ENV_PASSTHROUGH_UNTRACKED,可以通过环境传入凭据。

export VCPKG_KEEP_ENV_VARS=MY_TOKEN_VAR
export MY_TOKEN_VAR=abc123

然后,可以在专用端口中将其与 vcpkg_from_git()vcpkg_from_github()vcpkg_from_gitlab() 辅助工具配合使用。

# vcpkg-from-git-example/portfile.cmake
set(MY_TOKEN_VAR "")
if(DEFINED ENV{MY_TOKEN_VAR})
    set(MY_TOKEN_VAR "$ENV{MY_TOKEN_VAR}@")
endif()
vcpkg_from_git(
    URLS "https://${MY_TOKEN_VAR}host.com/normal/url/path"
    ...
)
# vcpkg-from-github-example/portfile.cmake
vcpkg_from_github(
    AUTHORIZATION_TOKEN "$ENV{MY_TOKEN_VAR}"
)

对于专用端口,我们建议使用 vcpkg_from_git() 而不是 vcpkg_from_github()/vcpkg_from_gitlab() 和上述预种子方法。

传递 Jenkins gitUsernamePassword 凭据

从 Jenkins 向 GitHub 进行身份验证的最简单且最安全的选项是使用 GitHub 应用,如下所示:

withCredentials([gitUsernamePassword(credentialsId: 'jenkins-github-app')]) {
  withEnv(['VCPKG_KEEP_ENV_VARS=GIT_ASKPASS']) {
    bat 'cmake'
  }
}

这会将 GIT_ASKPASS 设置为指向帮助脚本的路径,该脚本会响应 git 凭据查询,并指示 vcpkg 保留此环境变量。 密码是 GitHub 应用令牌,生存期为 1 小时。