Go get private bitbucket repo giving 403 forbidden

9.2k Views Asked by At

On execution of go get bitbucket.org/../..

I get this error

yash.jain projectname % go get bitbucket.org/../..
go: bitbucket.org/../..
https://api.bitbucket.org/2.0/repositories/../..?fields=scm: 403 Forbidden
go: error loading module requirements

What I tried

  • Setup the ssh key (Working as expected, tested by pushing and pulling the code)

  • Set the go env variables to this:

     GOPRIVATE=""
     GOPROXY="direct"
     GOSUMDB="off"
    
  • .gitconfig looks like this

     [url "[email protected]:"]
         insteadOf = https://bitbucket.org/
     [user]
         email = [email protected]
         name = yashjain
    
  • Added keys to ssh-agent

     ssh-add -l
     ssh-add -k
    
  • .ssh/config file looks like this

     Host bitbucket.org
       HostName bitbucket.org
       User git
       IdentityFile ~/.ssh/id_rsa
       UseKeychain yes
       StrictHostKeyChecking no
    

Still, I get the same error. Please let me know If I'm doing something wrong.

4

There are 4 best solutions below

8
On

Check first what ssh -Tv [email protected] returns, specifically the user mentioned in the Welcome message: it could be an account, which does not have access to the private repository.

As mentioned in "What's the proper way to “go get” a private repository?", try also with a private ssh key not protected by a passphrase, to make sure there is no prompt for said passphrase in the middle of the go get process.

1
On

There are Two ways I normally resolve this. But, for this you need to configure bitbucket with SSH.

1. Edit git.config

edit the global .gitconfig

nano /home/user/.gitconfig

add following lines

[url "[email protected]:"] insteadOf = https://bitbucket.org/

2. Export GOPRIVATE

export GOPRIVATE=bitbucket.org/<your repo>
0
On

These are the steps I tried to make it work.

  1. Setup SSH key and connect to bitbucket. I used sourcetree gui by bitbucket to connect using ssh.
  2. Set the GOPRIVATE=bitbucket.org/<orgname>/*

* is to allow all the repo of the org.

After these 2 steps I am able to import the package.

Note: Sourcetree is only available to Mac and Windows.

Using Commnd Line: For Linux/Mac/Windows:

  1. git config --global url."[email protected]:".insteadOf "https://bitbucket.org/"
  2. Set the GOPRIVATE=bitbucket.org/<orgname>/*

Update You might receive the 404 error even after above configuration.

Error
reading https://api.bitbucket.org/2.0/repositories/xyz/privaterepo?fields=scm: 404 Not Found

Bitbucket recently update their APIs for private repository and due to which certain golang version breaks.

Users who do not have access to a private repository will receive 404 (not found) response codes instead of 403 (forbidden) response codes to mask the existence of a private repository at a given URL path.
Rolling out these changes will break previous versions of Go due to the fact that the go command relies on a 403 response to fetch repositories hosted on Bitbucket Cloud.
To ensure that you will not run into any issues, update GoLang to the latest version.

  1. Ensure you are on a supported version of GoLang: 1.18, 1.17, or 1.16.
  2. If you are using 1.17 or 1.16, ensure you are on the latest patch (1.17.7 or 1.16.14)

This will be in effect from June 1st 2022.

Changes to Bitbucket API Requires Latest Version of Go

0
On

you can do it via Sourcetree as well. follow the steps below:

  1. Download and install Sourcetree
  2. configure your bitbucket ssh key in Sourcetree inside connection
  3. You will be able to see your account in accounts section. Something like below

source tree account added

  1. Next step is to add your GOPRIVATE env variable
  2. for that run this command

go env -w GOPRIVATE="bitbucket.org/<repo>/*"