【问题标题】:go-git: How to authenticate at remote server?go-git:如何在远程服务器上进行身份验证?
【发布时间】:2018-05-01 17:53:51
【问题描述】:

我正在使用 go 项目 go-git 作为 git 客户端,并希望从通过 gitea 托管的私有 git 存储库中获取。

执行此操作的适当函数是 func (r *Remote) Fetch(o *FetchOptions) error,它需要一个 transport.AuthMethod 对象进行身份验证。

我尝试了以下方法:

repo, _ := git.PlainOpen("/path/to/project/folder")
err := repo.Fetch(&git.FetchOptions{
    Auth: http.NewBasicAuth("someUser", "andThePassword"),
})

...简单地返回:

验证方法无效

如果我使用

,也会发生同样的情况
authenticator, _ := ssh.NewSSHAgentAuth("git")

来自包"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"

如果我使用证书:

authenticator, _:= ssh.NewPublicKeysFromFile("gitea name", "/home/name/.ssh/id_rsa", "passphrase")

如何确定支持哪种身份验证方法,是否有可以使用的transport.AuthMethod 的现有实现?

【问题讨论】:

  • 您的用法在我看来是正确的,也许有错误?该项目最近有一个问题#618,这可能表明一个有用的解决方法:&git.CloneOptions{ URL: "https://user:pass@host/repo.git" }
  • @orirawlings 这样的 url 只能在克隆新存储库时应用。当我已经拥有本地存储库时,我使用PlainOpen 打开它,然后使用Fetch,我无法再为其指定 url。 (网址取自.git/config 文件。即使我在那里修改了网址,它也会返回“无效的身份验证方法”
  • 您使用哪种协议? HTTP 还是 SSH?或者可能是 git?
  • @mcuadros 我正在使用 http
  • 你能打印“git remote -v”吗?

标签: git authentication go gitea go-git


【解决方案1】:

这里的问题是,我使用了错误的 http-package 来获取身份验证器。

我使用了 net/http 包中的 BasicAuth() - 但是,要使用的正确 http 包是 gopkg.in/src-d/go-git.v4/plumbing/transport/http

当更改导入并从那里使用(兼容的)BasicAuth-authenticator 时,它可以完美运行。

【讨论】:

    【解决方案2】:

    这对我有用 gopkg.in/src-d/go-git.v4

    func cloneGit() (err error) {
        username := "dude"
        password := "super-secret-Personal-access-token"
        repo := "github.build.company.com/org/repo-name.git"
        url := fmt.Sprintf("https://%s:%s@%s", username, password, repo)
        options := git.CloneOptions{
            URL:      url,
            Progress: os.Stdout,
        }
        r, err := git.PlainClone("./src", false, &options)
    
        if err != nil {
            return err
        }
        fmt.Println(r.Log)
        return err
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      相关资源
      最近更新 更多