From a4aa8a0ecbf545601d4e5f479edaa63deb14452d Mon Sep 17 00:00:00 2001 From: Karthik Nayak Date: Mon, 15 Aug 2022 11:17:16 +0200 Subject: client: Fix linter issues These files were directly copied over from 'gitlab-shell' in 823a6893bb4275f7d21035a34c778317af7d3d16. Let's fix the linter issues pre-existing in these files. --- internal/gitlab/client/gitlabnet.go | 28 +++++++++++++--------------- internal/gitlab/client/httpclient.go | 23 ++++++++++------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/internal/gitlab/client/gitlabnet.go b/internal/gitlab/client/gitlabnet.go index c34f148f3..da4da1c93 100644 --- a/internal/gitlab/client/gitlabnet.go +++ b/internal/gitlab/client/gitlabnet.go @@ -12,12 +12,11 @@ import ( "time" "github.com/golang-jwt/jwt/v4" - "gitlab.com/gitlab-org/labkit/log" ) const ( - internalApiPath = "/api/v4/internal" + internalAPIPath = "/api/v4/internal" secretHeaderName = "Gitlab-Shared-Secret" apiSecretHeaderName = "Gitlab-Shell-Api-Request" defaultUserAgent = "GitLab-Shell" @@ -30,21 +29,22 @@ type ErrorResponse struct { } type GitlabNetClient struct { - httpClient *HttpClient + httpClient *HTTPClient user string password string secret string userAgent string } -type ApiError struct { +type APIError struct { Msg string } -// To use as the key in a Context to set an X-Forwarded-For header in a request +// OriginalRemoteIPContextKey is to be used as the key in a Context +// to set an X-Forwarded-For header in a request type OriginalRemoteIPContextKey struct{} -func (e *ApiError) Error() string { +func (e *APIError) Error() string { return e.Msg } @@ -52,11 +52,10 @@ func NewGitlabNetClient( user, password, secret string, - httpClient *HttpClient, + httpClient *HTTPClient, ) (*GitlabNetClient, error) { - if httpClient == nil { - return nil, fmt.Errorf("Unsupported protocol") + return nil, fmt.Errorf("unsupported protocol") } return &GitlabNetClient{ @@ -79,8 +78,8 @@ func normalizePath(path string) string { path = "/" + path } - if !strings.HasPrefix(path, internalApiPath) { - path = internalApiPath + path + if !strings.HasPrefix(path, internalAPIPath) { + path = internalAPIPath + path } return path } @@ -112,11 +111,10 @@ func parseError(resp *http.Response) error { parsedResponse := &ErrorResponse{} if err := json.NewDecoder(resp.Body).Decode(parsedResponse); err != nil { - return &ApiError{fmt.Sprintf("Internal API error (%v)", resp.StatusCode)} + return &APIError{fmt.Sprintf("Internal API error (%v)", resp.StatusCode)} } else { - return &ApiError{parsedResponse.Message} + return &APIError{parsedResponse.Message} } - } func (c *GitlabNetClient) Get(ctx context.Context, path string) (*http.Response, error) { @@ -173,7 +171,7 @@ func (c *GitlabNetClient) DoRequest(ctx context.Context, method, path string, da if err != nil { logger.WithError(err).Error("Internal API unreachable") - return nil, &ApiError{"Internal API unreachable"} + return nil, &APIError{"Internal API unreachable"} } if response != nil { diff --git a/internal/gitlab/client/httpclient.go b/internal/gitlab/client/httpclient.go index bd00b6b80..dcf83b8c1 100644 --- a/internal/gitlab/client/httpclient.go +++ b/internal/gitlab/client/httpclient.go @@ -18,18 +18,16 @@ import ( ) const ( - socketBaseUrl = "http://unix" + socketBaseURL = "http://unix" unixSocketProtocol = "http+unix://" httpProtocol = "http://" httpsProtocol = "https://" defaultReadTimeoutSeconds = 300 ) -var ( - ErrCafileNotFound = errors.New("cafile not found") -) +var ErrCafileNotFound = errors.New("cafile not found") -type HttpClient struct { +type HTTPClient struct { *http.Client Host string } @@ -70,14 +68,14 @@ func validateCaFile(filename string) error { } // NewHTTPClientWithOpts builds an HTTP client using the provided options -func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath string, readTimeoutSeconds uint64, opts []HTTPClientOpt) (*HttpClient, error) { +func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath string, readTimeoutSeconds uint64, opts []HTTPClientOpt) (*HTTPClient, error) { var transport *http.Transport var host string var err error if strings.HasPrefix(gitlabURL, unixSocketProtocol) { transport, host = buildSocketTransport(gitlabURL, gitlabRelativeURLRoot) } else if strings.HasPrefix(gitlabURL, httpProtocol) { - transport, host = buildHttpTransport(gitlabURL) + transport, host = buildHTTPTransport(gitlabURL) } else if strings.HasPrefix(gitlabURL, httpsProtocol) { err = validateCaFile(caFile) if err != nil { @@ -93,7 +91,7 @@ func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath stri opt(hcc) } - transport, host, err = buildHttpsTransport(*hcc, gitlabURL) + transport, host, err = buildHTTPSTransport(*hcc, gitlabURL) if err != nil { return nil, err } @@ -106,7 +104,7 @@ func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath stri Timeout: readTimeout(readTimeoutSeconds), } - client := &HttpClient{Client: c, Host: host} + client := &HTTPClient{Client: c, Host: host} return client, nil } @@ -121,7 +119,7 @@ func buildSocketTransport(gitlabURL, gitlabRelativeURLRoot string) (*http.Transp }, } - host := socketBaseUrl + host := socketBaseURL gitlabRelativeURLRoot = strings.Trim(gitlabRelativeURLRoot, "/") if gitlabRelativeURLRoot != "" { host = host + "/" + gitlabRelativeURLRoot @@ -130,9 +128,8 @@ func buildSocketTransport(gitlabURL, gitlabRelativeURLRoot string) (*http.Transp return transport, host } -func buildHttpsTransport(hcc httpClientCfg, gitlabURL string) (*http.Transport, string, error) { +func buildHTTPSTransport(hcc httpClientCfg, gitlabURL string) (*http.Transport, string, error) { certPool, err := x509.SystemCertPool() - if err != nil { certPool = x509.NewCertPool() } @@ -178,7 +175,7 @@ func addCertToPool(certPool *x509.CertPool, fileName string) { } } -func buildHttpTransport(gitlabURL string) (*http.Transport, string) { +func buildHTTPTransport(gitlabURL string) (*http.Transport, string) { return &http.Transport{}, gitlabURL } -- cgit v1.2.3