gpt4 book ai didi

git - 从 Kubernetes secret 获取凭据时的 go-git 基本身份验证问题

转载 作者:数据小太阳 更新时间:2023-10-29 03:09:08 25 4
gpt4 key购买 nike

使用 go-git 克隆 github 存储库。尝试使用类似这样的方式使用个人 token 进行身份验证

func (g *Git) pullOptions() *gogit.PullOptions {
branch := fmt.Sprintf("refs/heads/%s", g.BranchName)
// Return options with token auth if enabled
if g.GitToken != "" {
log.Debug("Prepare pull option using gittoken")
return &gogit.PullOptions{
ReferenceName: plumbing.ReferenceName(branch),
Auth: &githttp.BasicAuth{
Username: g.GitUser,
Password: g.GitToken,
},
}
}

使用 spew

还可以看到pull options而且他们似乎是有效的

(*git.PullOptions)(0xc42008de60)({
RemoteName: (string) (len=6) "origin",
ReferenceName: (plumbing.ReferenceName) (len=17) refs/heads/master,
SingleBranch: (bool) false,
Depth: (int) 0,
Auth: (*http.BasicAuth)(0xc4203be300)(http-basic-auth - mygitid
:*******),
RecurseSubmodules: (git.SubmoduleRescursivity) 0,
Progress: (sideband.Progress) <nil>,
Force: (bool) false
})

但不断收到此错误:

time="2019-03-19T05:30:59Z" level=debug msg="Prepare pull option using 
gittoken"
time="2019-03-19T05:30:59Z" level=error msg="Git clone error:
authentication required"

如果我切换到 SSHKey 身份验证,则一切正常。有什么指点吗?

EDIT-1:

从环境变量中提取基本身份验证凭据时,这似乎确实是一个问题。例如,这段代码不起作用:

package main

import (
"fmt"
"os"
"time"

log "github.com/Sirupsen/logrus"
gogit "gopkg.in/src-d/go-git.v4"
gitconfig "gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)

func main() {
var repository *gogit.Repository
var err error

// @TODO: Why not use clone?
if _, err = os.Stat("/tmp/repo"); os.IsNotExist(err) {
repository, err = gogit.PlainInit("/tmp/repo", false)
if err != nil {
log.Errorf("could not init local repository %s: %s", "/tmp", err.Error())
}
} else {
repository, err = gogit.PlainOpen("/tmp/repo")
}

//fmt.Println((repository))
if _, err = repository.Remote("origin"); err == gogit.ErrRemoteNotFound {
_, err = repository.CreateRemote(&gitconfig.RemoteConfig{
Name: "origin",
URLs: []string{"https://xxxxx.git"},
})
if err != nil {
log.Errorf("could not attach to origin %s: %s", "bb", err.Error())
}
}

fmt.Println("Done with mapping")


r, err := gogit.PlainOpen("/tmp/repo")
if err != nil {
log.Fatal(err)
}
//fmt.Println(r)
branch := fmt.Sprintf("refs/heads/%s", "master")

fmt.Println("Setup wotktree")
w, err := r.Worktree()
if err != nil {
log.Fatal(err)
}

fmt.Println("pulling")
fmt.Println(os.Getenv("GIT_USER"))
fmt.Println(os.Getenv("GIT_TOKEN"))
if err := w.Pull(&gogit.PullOptions{
ReferenceName: plumbing.ReferenceName(branch),
Auth: &http.BasicAuth{
// Username: "xxxxxx",
// Password: "xxxxxxxxxx",
Username: os.Getenv("GIT_USER"),
Password: os.Getenv("GIT_TOKEN"),
},
}); err != nil {
log.Fatal(err)
}
fmt.Println("done")
time.Sleep(120 * time.Second)
}

但是,如果我如下硬编码凭据,那么它就可以工作。

    Auth: &http.BasicAuth{
Username: "xxxxxx",
Password: "xxxxxxxxxx",
// Username: os.Getenv("GIT_USER"),
// Password: os.Getenv("GIT_TOKEN"),
},

所以现在的问题是,我们如何安全地将凭据传递给 go-git 以进行基本身份验证?我们是否为 git 雕刻了一个凭证助手,但是 go-git 不依赖 native 客户端的观点可能会被打败。

最佳答案

一个给会遇到麻烦的人的工作示例:

package main

import (
"fmt"
"log"

"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)

func main() {
r, err := git.PlainOpen("<REPOSITORY_PATH>")
if err != nil {
log.Fatal(err)
}

branch := fmt.Sprintf("refs/heads/%s", "master")

w, err := r.Worktree()
if err != nil {
log.Fatal(err)
}

if err := w.Pull(&git.PullOptions{
ReferenceName: plumbing.ReferenceName(branch),
Auth: &http.BasicAuth{
Username: "<GITHUB_USERNAME>",
Password: "<GITHUB_API_KEY>",
},
}); err != nil {
log.Fatal(err)
}
}

关于git - 从 Kubernetes secret 获取凭据时的 go-git 基本身份验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55234675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com