Why 'go get' use https even if ssh is configured?
Asked Answered
P

1

11

I have configured ssh properly and I can do git stuff with our private repo via ssh. But when I tested with go get or go test (for go modules) the cli is not using ssh but instead using https.

.gitconfig

[url "ssh://[email protected]:7999"]
    insteadOf = https://bitbucket.company.com/scm

~/.ssh/config

Host bitbucket.company.com
 User git
 Port 7999
 IdentityFile ~/.ssh/id_rsa

testing if ssh works

$ git ls-remote ssh://[email protected]:7999/project/repo.git
ab86e12ab20775a308b7d0b003ba562263fbfa23        HEAD
ab86e12ab20775a308b7d0b003ba562263fbfa23        refs/heads/master
ab86e12ab20775a308b7d0b003ba562263fbfa23        refs/tags/v0.0.1

testing go get

$ go get -v bitbucket.company.com/project/repo
go get bitbucket.company.com/project/repo: unrecognized import path "bitbucket.company.com/project/repo": https fetch: Get "https://bitbucket.company.com/project/repo?go-get=1": dial tcp 10.68.204.3:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

go version

go version go1.14.1 windows/amd64

go env

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\sailhenz\AppData\Local\go-build
set GOENV=C:\Users\sailhenz\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=none
set GONOSUMDB=*.company.com
set GOOS=windows
set GOPATH=C:\Users\sailhenz\Documents\Development\Go
set GOPRIVATE=*.company.com
set GOPROXY=https://goproxy.io,direct
set GOROOT=C:\Users\sailhenz\Documents\Software\go
set GOSUMDB=off
set GOTMPDIR=
set GOTOOLDIR=C:\Users\sailhenz\Documents\Software\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\sail~1\AppData\Local\Temp\go-build227330694=/tmp/go-build -gno-record-gcc-switches

Any setup/configuration I missed? Thanks in advance.

Plaintiff answered 7/4, 2020 at 2:35 Comment(5)
bitbucket.company.com/project/repo does not match witch https://bitbucket.company.com/scm.Calculator
even matched the pattern but still no luck.Plaintiff
Hey, read this thread: github.com/golang/go/issues/27344Bathyscaphe
I somehow managed to trace the issue and it seems that adding .git in the import path resolves it. Link of the thread here github.com/golang/go/issues/27254 for anyone with the same issues.Plaintiff
@Plaintiff Good catch. I have included that issue in my answer below, with some details about the problem caused by the lack of a .git in said import path.Plastid
P
1

Try:

[url "bitbucket.company.com:"]
    insteadOf = https://bitbucket.company.com/

That would transform any https://bitbucket.company.com/ into bitbucket.company.com: which would then:

  • be interpreted as SSH URL
  • be decoded (through ~/.ssh/config) as ssh://[email protected]:7999/...

The key is to use bitbucket.company.com alone, which is the Host entry in your ~/.ssh/config file, and means it will refer to the right private key file.


subham sarkar reports in the comments about golang/go issue 27344

The solution mentioned above does not seem to work with Go 1.13, and is in "backlog" with Go 1.14.

Check for testing if the same go get would work with Go 1.11.


The OP sailhenz mentions in the comments issue 27254

cmd/go: custom import path private repos require special configuration

adding .git in the import path resolves it

The ticket adds:

The reason the .git solution works is that it helps guide the go tool with information about where the VCS for custom import paths private.repo.net/user/package.git/... resides (namely (https|ssh)://private.repo.net/user/package.git).

Without this (i.e. with an import path like private.repo.net/user/package/a), the go tool first queries https://private.repo.net/user/package/a?go-get=1 to work out (according to https://tip.golang.org/cmd/go/#hdr-Remote_import_paths) where the VCS for that custom import path lives.

A go get -x private.repo.net/user/package is a good way to debug this.

Plastid answered 7/4, 2020 at 4:24 Comment(5)
I still have the same issue. go get somehow trying to connect thru https. ``` dial tcp 10.68.204.3:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ```Plaintiff
@Plaintiff What version of Go are you using (see my edited answer)Plastid
I am using go version go1.14.1 windows/amd64. I also tried the solutions given in the thread and also tried to downgrade to 1.12.9 but still no luck. It's still trying to connect using https so I can't download any private repos. I will try with 1.11 and see if the issue still persists.Plaintiff
I have downgraded to go version go1.11.13 windows/amd64 but go get still using https. ``` Fetching bitbucket.company.com/project/repo?go-get=1 https fetch: Get bitbucket.company.com/project/repo?go-get=1: dial tcp 10.68.204.3:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.) ```. I'm going to put my go env in my original question.Plaintiff
@Plaintiff OK, so another issue is the problem. Can you add your PATH as well?Plastid

© 2022 - 2024 — McMap. All rights reserved.