From eb3b35b9b0cc55fb8464d9b0662e6b94aafc54cc Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Mon, 17 Aug 2020 22:19:56 -0700 Subject: Fix gitlab-shell not handling relative URLs over UNIX sockets From https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/4498#note_397401883, if you specify a relative path such as: ``` external_url 'http://gitlab.example.com/gitlab' ``` gitlab-shell doesn't have a way to pass the `/gitlab` to the host. For example, let's say we have: ``` gitlab_url: "http+unix://%2Fvar%2Fopt%2Fgitlab%2Fgitlab-workhorse%2Fsocket" ``` If we have `/gitlab` as the relative path, how do we specify what is the UNIX socket path and what is the relative path? If we specify: ``` gitlab_url: "http+unix:///var/opt/gitlab/gitlab-workhorse.socket/gitlab ``` This is ambiguous. Is the socket in `/var/opt/gitlab/gitlab-workhorse.socket/gitlab` or in `/var/opt/gitlab/gitlab-workhorse.socket`? To fix this, this merge request adds an optional `gitlab_relative_url_root` config parameter: ``` gitlab_url: "http+unix://%2Fvar%2Fopt%2Fgitlab%2Fgitlab-workhorse%2Fsocket" gitlab_relative_url_root: /gitlab ``` This is only used with UNIX domain sockets to disambiguate the socket and base URL path. If `gitlab_url` uses `http://` or `https://`, then `gitlab_relative_url_root` is ignored. Relates to https://gitlab.com/gitlab-org/gitlab-shell/-/issues/476 --- client/httpclient.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'client/httpclient.go') diff --git a/client/httpclient.go b/client/httpclient.go index 63386f7..6635f1b 100644 --- a/client/httpclient.go +++ b/client/httpclient.go @@ -27,12 +27,12 @@ type HttpClient struct { Host string } -func NewHTTPClient(gitlabURL, caFile, caPath string, selfSignedCert bool, readTimeoutSeconds uint64) *HttpClient { +func NewHTTPClient(gitlabURL, gitlabRelativeURLRoot, caFile, caPath string, selfSignedCert bool, readTimeoutSeconds uint64) *HttpClient { var transport *http.Transport var host string if strings.HasPrefix(gitlabURL, unixSocketProtocol) { - transport, host = buildSocketTransport(gitlabURL) + transport, host = buildSocketTransport(gitlabURL, gitlabRelativeURLRoot) } else if strings.HasPrefix(gitlabURL, httpProtocol) { transport, host = buildHttpTransport(gitlabURL) } else if strings.HasPrefix(gitlabURL, httpsProtocol) { @@ -41,7 +41,6 @@ func NewHTTPClient(gitlabURL, caFile, caPath string, selfSignedCert bool, readTi return nil } - c := &http.Client{ Transport: correlation.NewInstrumentedRoundTripper(transport), Timeout: readTimeout(readTimeoutSeconds), @@ -52,8 +51,9 @@ func NewHTTPClient(gitlabURL, caFile, caPath string, selfSignedCert bool, readTi return client } -func buildSocketTransport(gitlabURL string) (*http.Transport, string) { +func buildSocketTransport(gitlabURL, gitlabRelativeURLRoot string) (*http.Transport, string) { socketPath := strings.TrimPrefix(gitlabURL, unixSocketProtocol) + transport := &http.Transport{ DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { dialer := net.Dialer{} @@ -61,7 +61,13 @@ func buildSocketTransport(gitlabURL string) (*http.Transport, string) { }, } - return transport, socketBaseUrl + host := socketBaseUrl + gitlabRelativeURLRoot = strings.Trim(gitlabRelativeURLRoot, "/") + if gitlabRelativeURLRoot != "" { + host = host + "/" + gitlabRelativeURLRoot + } + + return transport, host } func buildHttpsTransport(caFile, caPath string, selfSignedCert bool, gitlabURL string) (*http.Transport, string) { -- cgit v1.2.1