是否可以创建一个 Git 助手或插件来添加 HTTP 标头或强制进行 Bearer Token 身份验证

问题描述 投票:0回答:2

背景:我们使用 Bitbucket Server(即将升级/切换到 Bitbucket DataCenter)。在我们的身份验证设置中,我们禁用了用户密码(网络身份验证通过不同的方式进行),因此出于 Bitbucket/Git 的目的,我们使用 Bitbucket 个人访问令牌 (PAT)。 (SSH 密钥身份验证在我们的组织中不是一个选项。)对于克隆/推送等,我们在提示输入凭据时一直使用我们的用户名和 PAT。这是可行的,但由于我无法了解我们的身份验证设置的原因,它正在创建与我们的自动化构建以及大型用户交互式本地构建相关的性能问题。

Bitbucket 不使用 HTTP(S) 基本身份验证,还支持 HTTP(S) 承载令牌身份验证,您只需提供 PAT,无需提供用户名。这样做可以解决我们的性能问题。对 REST API 请求执行此操作很简单。在 Git 上这样做比较棘手。 此 Bitbucket 文档 建议这样做:

$ git clone -c http.extraHeader='Authorization: Bearer xxxx' https://bitbucketserver.com/scm/projectname/teamsinspace.git

这有效!但它通过命令历史记录等向其他人公开 PAT。不公开 PAT 的更安全的变体也可以工作:

$ git clone --config-env=http.extraHeader=GIT_AUTHORIZATION_HEADER https://bitbucketserver.com/scm/projectname/teamsinspace.git

其中

GIT_AUTHORIZATION_HEADER
Authorization: Bearer xxxx
。但这仍然有一些缺点,即它不能使用
.gitconfig
并且必须添加到每个命令中,并且用户必须提前配置它并且不能提示输入安全存储在中的PAT Git 凭证助手。

GitLab 提供了一个“神奇的用户名”,

OAuth2
,可以通过基本身份验证向其提供令牌,该身份验证本质上告诉服务器“忽略用户名,仅使用令牌进行身份验证”。 Bitbucket 还提供了类似的“神奇用户名”
x-token-auth
,但它似乎要么仅限于云,要么仅限于存储库访问令牌,或者两者兼而有之,因为我们尝试使用它,但它只会导致身份验证错误。访问这样的功能将允许我们在
.gitconfig
中硬编码神奇的用户名,并且开发人员只会被提示输入“[email protected] 的密码”,这将是 PAT。但是,可惜我们不能使用这个功能。

大约 14 年前,Git 邮件列表上有过一次关于支持 Bearer 令牌身份验证的讨论,甚至还提出了一个补丁,但看起来并没有任何进展。理论上,我可以应用该补丁的现代化版本并重新编译 Git,但这对于我们的开发人员组织来说确实不是一个理想的解决方案。

我考虑创建一个

Git 凭证帮助程序(我之前实际上出于其他原因创建了它)和 Git 主机提供程序,但所有与凭证相关的内容都被硬编码为仅用户名/密码,所以那些不起作用。

所以我正在寻找/希望的是某种方法来创建某种类型的帮助程序、提供程序或插件,它们将能够指定、覆盖或替换 HTTP

Authorization

 标头以使其成为 Bearer基本的,要么使用来自内置 Git 凭证的密码,要么在必要时提示输入新内容,而无需编译我们自己定制的 Git 分支。这有可能吗,还是我应该放弃并使用
--config-env

git authentication bitbucket bearer-token http-authentication
2个回答
3
投票
绝对不为此感到自豪,但不得不在 CI 环境中诉诸一些肮脏的东西,也是因为“原因”......

这些解决方案都不是理想的,但是 最简单的方法是创建一个 shell 别名或在 gitconfig 中添加到从 stdin 读取令牌的 .bashrc/.zshrc 中。

## example that shows header is set correctly but does not show up actual value in command line history ## replace `whoami` command with command/script to retrieve token $ GIT_CURL_VERBOSE=1 git -c http.extraHeader="Hello: $(whoami)" ls-remote https://github.com/deric4/github-action-runner-reference ... 15:43:52.454783 http.c:701 == Info: using HTTP/2 15:43:52.454838 http.c:701 == Info: h2h3 [:method: GET] 15:43:52.454844 http.c:701 == Info: h2h3 [:path: /deric4/github-action-runner-reference/info/refs?service=git-upload-pack] 15:43:52.454849 http.c:701 == Info: h2h3 [:scheme: https] 15:43:52.454853 http.c:701 == Info: h2h3 [:authority: github.com] 15:43:52.454857 http.c:701 == Info: h2h3 [user-agent: git/2.39.2 (Apple Git-143)] 15:43:52.454861 http.c:701 == Info: h2h3 [accept: */*] 15:43:52.454865 http.c:701 == Info: h2h3 [accept-encoding: deflate, gzip] 15:43:52.454869 http.c:701 == Info: h2h3 [hello: deric4] 15:43:52.454873 http.c:701 == Info: h2h3 [pragma: no-cache] 15:43:52.454876 http.c:701 == Info: h2h3 [git-protocol: version=2] ...

# .gitconfig
[alias]
    myalias = "!f() { GIT_CURL_VERBOSE=1 git -c http.extraHeader=\"Hello: $(whoami)\" ls-remote $1 ; }; f"
更激进的方法是在凭证助手的附加/位置中实现自定义的

远程助手 我能够挖掘出一个我并不引以为傲的旧例子,但完成了工作:

这将允许动态获取和设置配置/凭据 基于执行上下文...

git clone accesstoken://some-profile@foo-bar-baz

// Note: that this was written in go 1.12 and hadn't looked at it since then so...
func main() {

    remoteName := os.Args[1]
    repoUrl := os.Args[2]

    u, err := url.Parse(repoUrl)
    if err != nil {
        log.Fatal(err)
    }
    
    // replaced complex steps to just getting the env var
    accessToken := os.Getenv("GIT_ACCESS_TOKEN")

    // get username so it can be reset when password is added
    username := u.User.Username()

    // construct <username>:<password>
    u.User = url.UserPassword(username, accessToken)

    // convert from accesstoken -> https
    u.Scheme = "https"

    cmd := exec.Command("git", "remote-http", remoteName, u.String())
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Stdin = os.Stdin

    err = cmd.Run()
    if err != nil {
        log.Fatal(err)
    }
}

但就你而言

// custom logic to get bearer token cmd := exec.Command("git", "-c", "http.extraHeader='Bearer: <token from somewhere>'", "remote-http", remoteName, u.String())



0
投票
https://github.com/microsoft/azure-pipelines-agent/issues/1601#issuecomment-1683714305

© www.soinside.com 2019 - 2024. All rights reserved.