首页 > 解决方案 > 通过没有 SSL 的 HTTP 使用带有私有存储库的 Go 模块

问题描述

有没有办法强制go命令使用 HTTP 而不是 HTTPS?

我正在尝试将一个项目移动到 Go Modules,但该项目包含一个私有git存储库作为依赖项。此git存储库托管在防火墙后面的私有 LAN 中,我通常使用cd $GOPATH/git.ourdomain.net/foo/bar/ && git clone http://my-user:my-password@git.ourdomain.net/foo/bar/reponame.git.

但是现在命令go modgo get并且go test正在尝试将 HTTP 与 SSL (HTTPS) 一起使用。

此设置没有帮助:

git config \
  --global \
  url."http://my-user:my-password@git.ourdomain.net/foo/bar/reponame".insteadOf \
  "http://git.ourdomain.net"

(在此处查看git有关私有存储库设置的一些上下文:https ://medium.com/cloud-native-the-gathering/go-modules-with-private-git-repositories-dfe795068db4 )

我已经修改了文件go.mod以包含对该私有存储库的引用(使用类似的模式git.ourdomain.net/foo/bar/reponame@v0.0.0-<YYYYMMDDHHmmSS>-<GIT_COMMIT_ID>),但是当使用包含该私有存储库go test的文件调用时go.mod,我看到:

go: git.ourdomain.net/foo/bar/reponame@v0.0.0-<YYYYMMDDHHmmSS>-<GIT_COMMIT_ID>: unrecognized import path "git.ourdomain.net/foo/bar/reponame" (https fetch: Get https://git.ourdomain.net/foo/bar/reponame?go-get=1: dial tcp <PUBLIC_IP_ADDRESS>:443: connect: connection refused)

我还尝试恢复go.mod到原始版本(没有该私有存储库)并尝试go get从存储文件的路径恢复go.mod,假设它将go.mod通过 HTTP 以某种方式更新文件,因为-insecure(查看此处https://golang.org /cmd/go/#hdr-Module_aware_go_get),像这样:

GO111MODULE=on go get -insecure -u git.ourdomain.net/foo/bar/reponame

但这给出了错误消息:

fatal: remote error: Repository not found
The requested repository does not exist, or you do not have permission to
access it.

我知道我应该使用 SSL,但我从来没有控制过这个git服务器来配置它应该配置的方式。所以我只是使用git clone提供一个 HTTP URL(在防火墙后面)并且它总是有效的。

标签: githttpgogo-modules

解决方案


go命令通过将该路径解析为版本控制位置来开始获取给定的模块路径。为了解析路径,它发出一个 HTTPS 请求并查找https://tip.golang.org/cmd/go/#hdr-Remote_import_paths<meta name="go-import" […]>中描述的标签。

如果您希望它跳过该步骤,则必须使用版本控制后缀(在同一文档中列出)指示导入路径是原始版本控制路径。

如果你写import "git.ourdomain.net/foo/bar/reponame.git"而不是import "git.ourdomain.net/foo/bar/reponame",你应该会发现最初的 HTTPS 解析被跳过,后续git操作使用insteadOf你全局git配置中的声明。


推荐阅读