43

So basically, I have an Openshift Project that on Git push, downloads all libraries with 'Go get' and builds the project on-the-fly and so, I have some code I don't want people to see from my own library, and for it to compile properly, the code needs to be taken from github.com or another repo, so I created a private bitbucket.org repo, now, as a public repo it works fine, but when I try to 'Go Get' from my private repo, it gives me 'Forbidden 403'

How can I avoid this occurency? Thank you for reading and have a nice day!

BeeAdmin
  • 451
  • 1
  • 4
  • 3
  • you are using *pipelines* from BitBucket right? same problem I added an SSH key pair and added `bitbucket.org` as a know host but I still get `403`. I think there is something missing in https://confluence.atlassian.com/x/DBuDMg since we can't add the PubKey to the remote hosts on Bitbucket. – Cedric Jul 03 '18 at 13:13
  • Since go@1.13 you need to follow [this solution](https://stackoverflow.com/questions/61470626/how-do-i-use-go-with-bitbucket-private-repositories/61474660#61474660). – Nazmi ZORLU May 17 '20 at 20:48

4 Answers4

82

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/"

Ammar Bandukwala
  • 1,512
  • 10
  • 13
  • 9
    Still getting 403 error for bitbucket after adding to global config, what other reasons could there be for this and how to solve? – user3226932 Aug 12 '17 at 17:44
  • i'm able to push to and pull from my repo using ssh, not sure how why go get is not seeing this config change, and i'm also getting same error when i put this config change in my local .git/config – user3226932 Aug 12 '17 at 18:03
  • btw, i'm doing the go get while building the docker image for my go app, not sure if it could be related to that too – user3226932 Aug 12 '17 at 18:27
  • 2
    I faced the same issue @user3226932 faced: even after adding global config was getting 403 error. Solved it by doing `git config --system`: `git config --system url."git@github.com:".insteadOf "https://github.com/"`. – Subhash Chandran Aug 31 '17 at 16:22
  • 4
    Still didn't work after the command above, but the error mentioned that it goes to api.bitbucket.org, so I used: `git config --global url."git@bitbucket.org:".insteadOf "https://api.bitbucket.org/"` – kalsky May 14 '18 at 01:33
  • Both tipps in combination helped me, when I tried to use go modules with a public bitbucket repository on windows: `git config --system url."git@bitbucket.org:".insteadOf "https://api.bitbucket.org/"` – Daveman May 04 '20 at 19:43
  • I was using my personal private bit bucket repo. follow helped me --> `export GOPRIVATE=bitbucket.org/companyNameORuserName` --> `git config --global url."git@bitbucket.org:".insteadOf "https://bitbucket.org/"` – suraj.tripathi Aug 28 '20 at 14:26
18

Adition to setting git config described by others, I had to set GOPRIVATE.

Step 1. git config --global url."git@bitbucket.<YOUR-COMPANY>".insteadOf "https://bitbucket.org/<YOUR-COMPANY>"

Step 2. export GOPRIVATE=bitbucket.org/<YOUR-COMPANY>

the GOPRIAVTE is documented here: https://golang.org/cmd/go/#hdr-Module_configuration_for_non_public_modules

naoko
  • 5,064
  • 4
  • 35
  • 28
  • 1
    With private modules hosted on GitHub setting `GOPRIVATE` as advised worked but differently from your example I had to modify git configuration with a specific authentication url e.g. `git config --global url."https://${GITHUB_USER}:${GITHUB_TOKEN}@github.com".insteadOf "https://github.com"`. If the repository is private how do you authenticate? – n1nsa1d00 Jul 28 '20 at 08:22
  • Thank you @MarcoDufal, fixed! – naoko Aug 18 '20 at 22:37
2

Ammar Bandukwala's answer is correct, but if you find yourself behind a firewall with SSH outgoing port (22) closed, you can use SSH over HTTPS port (443) instead:

git config --global url."ssh://git@altssh.bitbucket.org:443/<account_name>".insteadOf "https://bitbucket.org/<account_name>"

source: https://confluence.atlassian.com/bitbucket/troubleshoot-ssh-issues-271943403.html#TroubleshootSSHissues-Ifport22isblocked

vin047
  • 260
  • 3
  • 12
1

For anyone coming here after trying all of the above (and getting a 404 instead of a 403) this is because BB changed the response code from 403 to 404 when you're fetching a private repo. Golang has a fallback to HTTP when the repo cannot be resolved through SSH. This was fixed in golang version 1.16+.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 20 '22 at 13:28