284

I'm searching for the way to get $ go get work with private repository, after many google try.

The first try:

$ go get -v gitlab.com/secmask/awserver-go
Fetching https://gitlab.com/secmask/awserver-go?go-get=1
https fetch failed.
Fetching http://gitlab.com/secmask/awserver-go?go-get=1
Parsing meta tags from http://gitlab.com/secmask/awserver-go?go-get=1 (status code 200)
import "gitlab.com/secmask/awserver-go": parse http://gitlab.com/secmask/awserver-go?go-get=1: no go-import meta tags
package gitlab.com/secmask/awserver-go: unrecognized import path "gitlab.com/secmask/awserver-go

Yep, it did not see the meta tags because I could not know how to provide login information.

The second try:

Follow https://gist.github.com/shurcooL/6927554. Add config to .gitconfig.

[url "ssh://git@gitlab.com/"]
    insteadOf = https://gitlab.com/
$ go get -v gitlab.com/secmask/awserver-go --> not work
$ go get -v gitlab.com/secmask/awserver-go.git --> work but I got src/gitlab.com/secmask/awserer-go.git

Yes it work but with .git extension with my project name, I can rename it to original but do it everytime $ go get is not so good, is there an otherway?

Shudipta Sharma
  • 5,178
  • 3
  • 19
  • 33
secmask
  • 7,649
  • 5
  • 35
  • 52

17 Answers17

227

You have one thing to configure. The example is based on GitHub but this shouldn't change the process:

$ git config --global url.git@github.com:.insteadOf https://github.com/
$ cat ~/.gitconfig
[url "git@github.com:"]
    insteadOf = https://github.com/
$ go get github.com/private/repo

For Go modules to work (with Go 1.11 or newer), you'll also need to set the GOPRIVATE variable, to avoid using the public servers to fetch the code:

export GOPRIVATE=github.com/private/repo
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
JulienD
  • 3,172
  • 3
  • 16
  • 16
  • Already did that, actually it's only one config to do, `cat` command is just for verify. – secmask Dec 16 '14 at 09:17
  • 1
    The only drawback with this is that you cant have different config for each host (e.g if you're using multiple providers) without altering the global git configuration for each time. Then it's much better to specify this on «ssh-level» as described her: http://stackoverflow.com/questions/27500861/whats-the-proper-way-to-go-get-a-private-repository/40795707#40795707 – Joachim Feb 23 '17 at 10:21
  • 11
    `go env -w GOPRIVATE=github.com//*`as described here https://stackoverflow.com/questions/58305567/how-to-set-goprivate-environment-variable – huggie Oct 15 '20 at 03:35
  • is quotes around url needed like [another answer](https://stackoverflow.com/a/45936697/1518100) posts? – Lei Yang Jan 22 '21 at 07:27
  • 1
    There can be cache messing things up. Probably from attempting to get some repo without changing the git config as described above. What solved it for me : rm -rf $GOPATH/pkg/mod/cache/vcs – Johan Martinsson Mar 31 '21 at 21:08
  • @LeiYang They are not *strictly* needed, but it's a good idea to put `"quotes"` or `'quotes'` around something that includes special shell characters (like `*` in this case) – Paul Fisher Apr 30 '21 at 16:51
  • 2
    Is anyone able to get this working with gitlab? I keep getting the error `The project you were looking for could not be found or you don't have permission to view it.` when I try. I can run git commands via ssh just fine. This only works for me with the token based solution. – Solidus Aug 25 '21 at 09:45
  • 2
    This appears to be broken in 2021 with go 1.16. Git clone works with public and private repos on github but go get (and go mod tidy) fail on the private repo. Oddly enough, the go commands work correctly with private repos on my own gitea server. – Kevin Shea Nov 25 '21 at 17:43
  • Works like a charm with 1.19! Perhaps a small explanation to help unaware readers: the question tries to replace "https://gitlab.com" with "ssh://git@gitlab.com/", this could work, but using the ssh:// format does not automatically find the .git file that `go get` needs. However, using simply `git@gitlab.com` to replace works, since it automatically returns the .git file that go get expects. – aIKid Oct 06 '22 at 12:40
56

The proper way is to manually put the repository in the right place. Once the repository is there, you can use go get -u to update the package and go install to install it. A package named

github.com/secmask/awserver-go

goes into

$GOPATH/src/github.com/secmask/awserver-go

The commands you type are:

cd $GOPATH/src/github.com/secmask
git clone git@github.com:secmask/awserver-go.git
fuz
  • 88,405
  • 25
  • 200
  • 352
  • 10
    @secmask `go get` is designed as a tool for the common case. The Go team explicitly decided against adding configurability so people adhere to standards instead of rolling out their own cruft. It was never made for the case you have (i.e. private repositories). – fuz Dec 16 '14 at 11:27
  • This is an interesting perspective, but the big limitation of this approach is the handling of recursive, private dependencies. Handling this in a scalable way would essentially require re-implementing `go get`, if SSH access couldn't be handled as described in other answers. – Sean Kelleher Mar 01 '23 at 12:09
56

I had a problem with go get using private repository on gitlab from our company. I lost a few minutes trying to find a solution. And I did find this one:

  1. You need to get a private token at:
    https://gitlab.mycompany.com/profile/account

  2. Configure you git to add extra header with your private token:

     $ git config --global http.extraheader "PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN"
    
  3. Configure your git to convert requests from http to ssh:

     $ git config --global url."git@gitlab.mycompany.com:".insteadOf "https://gitlab.mycompany.com/"
    
  4. Finally you can use your go get normally:

     $ go get gitlab.com/company/private_repo
    
updogliu
  • 6,066
  • 7
  • 37
  • 50
  • 6
    Interesting use of `http.extraheader`. +1 – VonC Aug 29 '17 at 10:45
  • 61
    will `--global` also send your private token to other git server in case you using many repo? any secure risk? – secmask Aug 29 '17 at 15:06
  • Anyone any review on that? I think it will send it to public github repo when we do go get on them – hemu May 30 '20 at 11:45
  • 9
    There is another option where you send token only to your gitlab - but I am not sure this is encoded. `git config --global url."https://${user}:${personal_access_token}@mygitlab.com".insteadOf "https://mygitlab.com"` – S.R Sep 10 '20 at 15:25
  • 1
    I think @S.R solution is best (even though I had to add a trailing slash to the first url to make it work for my case => `[...] url."https://${user}:${personal_access_token}@mygitlab.com/".insteadOf [...]` – Alexander Fortin Apr 16 '21 at 15:10
  • @secmask SSH protocol doesn't "send your private token" to anybody. [Based on this](https://serverfault.com/a/142865/243251) The SSH "server uses your public key to sign a challenge, and your client uses your private key `id_rsa` to decrypt the challenge, re-encrypt it with the server's public `host key` and send it back." There's a privacy risk if you don't want the public server tracking you by the SSH key you've added to your SSH agent. – Carl Walsh Feb 28 '23 at 16:26
45

For people using private GitLabs, here's a snippet that may help: https://gist.github.com/MicahParks/1ba2b19c39d1e5fccc3e892837b10e21

Also pasted below:

Problem

The go command line tool needs to be able to fetch dependencies from your private GitLab, but authenticaiton is required.

This assumes your private GitLab is hosted at privategitlab.company.com.

Environment variables

The following environment variables are recommended:

export GO111MODULE=on
export GOPRIVATE=privategitlab.company.com # this is comma delimited if using multiple private repos

The above lines might fit best in your shell startup, like a ~/.bashrc.

Explanation

GO111MODULE=on tells Golang command line tools you are using modules. I have not tested this with projects not using Golang modules on a private GitLab.

GOPRIVATE=privategitlab.company.com tells Golang command line tools to not use public internet resources for the hostnames listed (like the public module proxy).

Get a personal access token from your private GitLab

To future proof these instructions, please follow this guide from the GitLab docs. I know that the read_api scope is required for Golang command line tools to work, and I may suspect read_repository as well, but have not confirmed this.

Set up the ~/.netrc

In order for the Golang command line tools to authenticate to GitLab, a ~/.netrc file is best to use.

To create the file if it does not exist, run the following commands:

touch ~/.netrc
chmod 600 ~/.netrc

Now edit the contents of the file to match the following:

machine privategitlab.company.com login USERNAME_HERE password TOKEN_HERE

Where USERNAME_HERE is replaced with your GitLab username and TOKEN_HERE is replaced with the access token aquired in the previous section.

Common mistakes

Do not set up a global git configuration with something along the lines of this:

git config --global url."git@privategitlab.company.com:".insteadOf "https://privategitlab.company.com"

I beleive at the time of writing this, the SSH git is not fully supported by Golang command line tools and this may cause conflicts with the ~/.netrc.

Bonus: SSH config file

For regular use of the git tool, not the Golang command line tools, it's convient to have a ~/.ssh/config file set up. In order to do this, run the following commands:

mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/config

Please note the permissions on the files and directory above are essentail for SSH to work in it's default configuration on most Linux systems.

Then, edit the ~/.ssh/config file to match the following:

Host privategitlab.company.com
  Hostname privategitlab.company.com
  User USERNAME_HERE
  IdentityFile ~/.ssh/id_rsa

Please note the spacing in the above file matters and will invalidate the file if it is incorrect.

Where USERNAME_HERE is your GitLab username and ~/.ssh/id_rsa is the path to your SSH private key in your file system. You've already uploaded its public key to GitLab. Here are some instructions.

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
Micah Parks
  • 1,504
  • 1
  • 10
  • 22
  • 2
    There's something that always rubbed me the wrong way about configuring git globally just so that Go dependencies can be downloaded. But doesn't this have that same problem? You're configuring a tool used by your entire system (in this case, if I understand .netrc properly, the way your machine performs every HTTP request) just so that Go dependencies can be downloaded. In fact, this way seems risky, since wouldn't it include that token with every request you send to that host? – Matt Welke Feb 02 '21 at 16:42
  • 1
    Thank you, For gitlab the following worked for me `echo "machine gitlab.example.com login gitlab-ci-token password ${CI_JOB_TOKEN}" > ~/.netrc && chmod 600 ~/.netrc` – spuder Oct 26 '21 at 19:33
  • 1
    Thanks! Little note for windows: %HOME%\\_netrc file there. – Elena Dec 01 '22 at 07:01
23

All of the above did not work for me. Cloning the repo was working correctly but I was still getting an unrecognized import error.

As it stands for Go v1.13, I found in the doc that we should use the GOPRIVATE env variable like so:

$ GOPRIVATE=github.com/ORGANISATION_OR_USER_NAME go get -u github.com/ORGANISATION_OR_USER_NAME/REPO_NAME
jolancornevin
  • 999
  • 1
  • 9
  • 7
18

Generate a github oauth token here and export your github token as an environment variable:

export GITHUB_TOKEN=123

Set git config to use the basic auth url:

git config --global url."https://$GITHUB_TOKEN:x-oauth-basic@github.com/".insteadOf "https://github.com/"

Now you can go get your private repo.

Miguel Mota
  • 20,135
  • 5
  • 45
  • 64
15

If you've already got git using SSH, this answer by Ammar Bandukwala is a simple workaround:


$ go get uses git internally. The following one liners will make git and consequently $ go get clone your package via SSH.

Github:

$ git config --global url."git@github.com:".insteadOf "https://github.com/"

BitBucket:

$ git config --global url."git@bitbucket.org:".insteadOf "https://bitbucket.org/"
Shudipta Sharma
  • 5,178
  • 3
  • 19
  • 33
Robert K. Bell
  • 9,350
  • 2
  • 35
  • 50
12

I came across .netrc and found it relevant to this.

Create a file ~/.netrc with the following content:

machine github.com
    login <github username>
    password <github password or Personal access tokens >

Done!

Additionally, for latest GO versions, you might need to add this to the environment variables GOPRIVATE=github.com/<orgname|username> (I've added it to my .zshrc)

netrc also makes my development environment setup better as my personal github access for HTTPS is been configured now to be used across the machine (just like my SSH configuration).

Generate GitHub personal access tokens: https://github.com/settings/tokens

See this answer for its use with Git on Windows specifically

Ref: netrc man page


If you want to stick with the SSH authentication, then mask the request to use ssh forcefully

git config --global url."git@github.com:<orgname|username>".insteadOf "https://github.com/<orgname|username>"

Replace <orgname|username> with your GitHub org name or username.


Issue is that, go tries to access the dependencies using https protocol and lot of us use ssh instead to authenticate on git. So we either need to setup a https authentication using ~/.netrc or force using ssh on all request to a specific git location using git config

More methods for setting up git access: https://gist.github.com/technoweenie/1072829#gistcomment-2979908

Kanak Singhal
  • 3,074
  • 1
  • 19
  • 17
  • This requires you to store your login credentials in a plain text file on your machine. That's not good. – Joachim Aug 19 '20 at 08:25
  • You are probably right. For me, it looks similar to how AWS credentials are stored in `~/.aws`. And with GitHub's personal access token, I can fine grain the access according to requirement and control rotations. There's SSH way as well – Kanak Singhal Aug 20 '20 at 09:58
9

That looks like the GitLab issue 5769.

In GitLab, since the repositories always end in .git, I must specify .git at the end of the repository name to make it work, for example:

import "example.org/myuser/mygorepo.git"

And:

$ go get example.org/myuser/mygorepo.git

Looks like GitHub solves this by appending ".git".

It is supposed to be resolved in “Added support for Go's repository retrieval. #5958”, provided the right meta tags are in place.
Although there is still an issue for Go itself: “cmd/go: go get cannot discover meta tag in HTML5 documents”.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Actually they (and others) has already supported meta tags, but it just works for public repo (where `go get` can see meta tags without login) – secmask Dec 16 '14 at 10:17
  • @secmask which is why you are using ssh: to provide the credentials that way. – VonC Dec 16 '14 at 10:23
  • oh, that just option, I can use http,https to but `go get` command will look like `go get gitlab.com/secmask/awserver-go.git.git` (will ask http basic authenticate), which look not good too. – secmask Dec 16 '14 at 10:30
9

After trying multiple solutions my problem still persisted. The final solution after setting up the ~/.netrc and SSH config file, was to add the following line to my ~/.bash_profile

export GOPRIVATE="github.com/[organization]"

ofvera
  • 91
  • 1
  • 1
6

I have created a user specific ssh-config, so my user automatically logs in with the correct credentials and key.

First I needed to generate an key-pair

ssh-keygen -t rsa -b 4096 -C "my@email.here"

and saved it to e.g ~/.ssh/id_my_domain. Note that this is also the keypair (private and public) I've connected to my Github account, so mine is stored in~/.ssh/id_github_com.

I have then created (or altered) a file called ~/.ssh/config with an entry:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github_com

On another server, the "ssh-url" is admin@domain.com:username/private-repo.git and the entry for this server would have been:

Host domain.com
    HostName domain.com
    User admin
    IdentityFile ~/.ssh/id_domain_com

Just to clarify that you need ensure that the User, Host and HostName is set correctly.

Now I can just browse into the go path and then go get <package>, e.g go get main where the file main/main.go includes the package (from last example above) domain.com:username/private-repo.git.

Joachim
  • 320
  • 3
  • 12
  • You can also import a package directly with: `go get hostname.com/username/repo.git` (.git extension is crucial). – Joachim Apr 11 '17 at 11:05
  • is there any way to do it without the extension `.git` and why happen it? – Cristian Apr 25 '17 at 16:19
  • 1
    Interesting, the reason cause I don't be able to get the repo, without the `.git` extension it was that on my local git configuration, `git config --global url."git@gitlab.myserver.com:".insteadOf "https://gitlab.myserver.com/"` , but the subdomain `gitlab.myserver.com` doesn't have the ssl certification, so I use `http` instead of `https` , by now – Cristian Apr 25 '17 at 19:13
4

For me, the solutions offered by others still gave the following error during go get

git@gl.nimi24.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

What this solution required

  1. As stated by others:

    git config --global url."git@github.com:".insteadOf "https://github.com/"

  2. Removing the passphrase from my ./ssh/id_rsa key which was used for authenticating the connection to the repository. This can be done by entering an empty password when prompted as a response to:

    ssh-keygen -p

Why this works

This is not a pretty workaround as it is always better to have a passphrase on your private key, but it was causing issues somewhere inside OpenSSH.

go get uses internally git, which uses openssh to open the connection. OpenSSH takes the certs necessary for authentication from .ssh/id_rsa. When executing git commands from the command line an agent can take care of opening the id_rsa file for you so that you do not have to specify the passphrase every time, but when executed in the belly of go get, this did not work somewhy in my case. OpenSSH wants to prompt you then for a password but since it is not possible due to how it was called, it prints to its debug log:

read_passphrase: can't open /dev/tty: No such device or address

And just fails. If you remove the passphrase from the key file, OpenSSH will get to your key without that prompt and it works

This might be caused by Go fetching modules concurrently and opening multiple SSH connections to Github at the same time (as described in this article). This is somewhat supported by the fact that OpenSSH debug log showed the initial connection to the repository succeed, but later tried it again for some reason and this time opted to ask for a passphrase.

However the solution of using SSH connection multiplexing as put forward in the mentioned article did not work for me. For the record, the author suggested adding the collowing conf to the ssh config file for the affected host:

  ControlMaster auto
  ControlPersist 3600
  ControlPath ~/.ssh/%r@%h:%p

But as stated, for me it did not work, maybe I did it wrong

Tarmo
  • 1,136
  • 13
  • 28
  • I think you can get this to work without removing the passphrase on your SSH key by using ssh-agent before running go-get to pre-authenticate your ssh-key. – Keegan Brown Jun 07 '21 at 21:17
  • In my case using an agent beforehand did not work unfortunately – Tarmo Jun 08 '21 at 06:29
4

After setting up GOPRIVATE and git config ...

People may still meeting problems like this when fetching private source:

https fetch: Get "https://private/user/repo?go-get=1": EOF

They can't use private repo without .git extension.

The reason is the go tool has no idea about the VCS protocol of this repo, git or svn or any other, unlike github.com or golang.org them are hardcoded into go's source.

Then the go tool will do a https query before fetching your private repo:

https://private/user/repo?go-get=1

If your private repo has no support for https request, please use replace to tell it directly :

require private/user/repo v1.0.0

...

replace private/user/repo => private.server/user/repo.git v1.0.0

https://golang.org/cmd/go/#hdr-Remote_import_paths

igonejack
  • 2,366
  • 20
  • 29
2

first I tried

[url "ssh://git@github.com/"]
    insteadOf = https://github.com/

but it didn't worked for my local.

I tried

ssh -t git@github.com

and it shows my ssh is fine.

finally, I fix the problem to tell the go get to consider all as private and use ssh instead of HTTPS.

adding export GOPRIVATE=*

Yuseferi
  • 7,931
  • 11
  • 67
  • 103
1

Make sure you remove your previous gitconfigs, I had the same issue.

Previously I executed gitconfig whose token was expired, when you execute the command next time with new token make sure to delete previous one.

0

For standalone/final repos, an as a quick fix, why don't just to name the module within the go.mod as a package using your company's domain ... ?

module go.yourcompany.tld/the_repo

go.yourcompany.tld don't even have to exist as a valid (sub)domain...

Also, in the same go.mod you can use replacement block/lines to use private repos previously cloned the same way (within a respective folder cloned also in $GOPATH/src/go.yourcompany.tld) (why do we have to depend too much in GitHub?)

Edit

  1. Needless to say that a private repo usually shall be a private repo, typically a standard git repo, right? With that, why not just to git clone and then go get within the cloned folder?
-1

It's Hard Code In Go Get. Not The Git Reason. So Modify Go Source.
Reason:
repoRootForImportDynamic Will Request: https://....go-get

// RepoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use.
func RepoRootForImportPath(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) {
    rr, err := repoRootFromVCSPaths(importPath, security, vcsPaths)
    if err == errUnknownSite {
        rr, err = repoRootForImportDynamic(importPath, mod, security)
        if err != nil {
            err = importErrorf(importPath, "unrecognized import path %q: %v", importPath, err)
        }
    }
    if err != nil {
        rr1, err1 := repoRootFromVCSPaths(importPath, security, vcsPathsAfterDynamic)
        if err1 == nil {
            rr = rr1
            err = nil
        }
    }

So add gitlab domain to vcsPaths will ok.
Download go source code:

vi ./src/cmd/go/internal/vcs/vcs.go    

Look for code below:

var vcsPaths = []*vcsPath{
    // GitHub
    {
        pathPrefix: "github.com",
        regexp:     lazyregexp.New(`^(?P<root>github\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
        vcs:        "git",
        repo:       "https://{root}",
        check:      noVCSSuffix,
    },

add Code As Follow,XXXX Is Your Domain:

    // GitLab
    {
        pathPrefix: "gitlab.xxxx.com",
        regexp:     lazyregexp.New(`^(?P<root>gitlab.xxxx\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
        vcs:        "git",
        repo:       "https://{root}",
        check:      noVCSSuffix,
    },

compile and replace go.

rak Toniz
  • 1
  • 1