du548397507 2019-03-19 06:12
浏览 126

从Kubernetes机密获取凭据时出现go-git基本身份验证问题

Using go-git for a cloning a github repo. Trying to use personal token for authentication using something like this

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,
        },
    }
}

Using spew

can also see the pull options and they seem to be valid

(*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
})

But keep getting this error:

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"

If I switch to SSHKey auth, then this works fine. Any pointers?

EDIT-1:

It definitely seems to be an issue when pulling basic auth credentials from environment variables. For instance, this code does not work:

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)
}

However, If I hardcode credentials as below, then it works.

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

So the question now is really, how do we securely pass credentials to go-git for basic auth? do we carve a credential helper for git but then the point of go-git not relying on native client may be defeated.

  • 写回答

1条回答 默认 最新

  • dqeznd1697 2019-03-19 11:54
    关注

    A working example for people who would get into troubles :

    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)
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。