go-git basic authentication issues when getting credentials from Kubernetes secrets

3/19/2019

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.

-- f-z-N
app-secret
git
github
go
kubernetes

1 Answer

3/19/2019

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)
    }
}
-- antham
Source: StackOverflow