As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Commit b1d2c4b5 authored by gaurav.marwal's avatar gaurav.marwal
Browse files

Fix lint issues in client gitlabnet

parent a1846ef0
No related branches found
No related tags found
No related merge requests found
// Package client provides a client for interacting with GitLab API
package client
import (
Loading
Loading
@@ -15,17 +16,19 @@ import (
)
const (
internalApiPath = "/api/v4/internal"
apiSecretHeaderName = "Gitlab-Shell-Api-Request"
internalAPIPath = "/api/v4/internal"
apiSecretHeaderName = "Gitlab-Shell-Api-Request" // #nosec G101
defaultUserAgent = "GitLab-Shell"
jwtTTL = time.Minute
jwtIssuer = "gitlab-shell"
)
// ErrorResponse represents an error response from the API
type ErrorResponse struct {
Message string `json:"message"`
}
// GitlabNetClient is a client for interacting with GitLab API
type GitlabNetClient struct {
httpClient *HttpClient
user string
Loading
Loading
@@ -34,17 +37,19 @@ type GitlabNetClient struct {
userAgent string
}
type ApiError struct {
// APIError represents an API error
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 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
}
// NewGitlabNetClient creates a new GitlabNetClient instance
func NewGitlabNetClient(
user,
password,
Loading
Loading
@@ -52,7 +57,7 @@ func NewGitlabNetClient(
httpClient *HttpClient,
) (*GitlabNetClient, error) {
if httpClient == nil {
return nil, fmt.Errorf("Unsupported protocol")
return nil, fmt.Errorf("unsupported protocol")
}
return &GitlabNetClient{
Loading
Loading
@@ -75,8 +80,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
}
Loading
Loading
@@ -106,39 +111,42 @@ func newRequest(ctx context.Context, method, host, path string, data interface{}
func parseError(resp *http.Response, respErr error) error {
if resp == nil || respErr != nil {
return &ApiError{"Internal API unreachable"}
return &APIError{"Internal API unreachable"}
}
if resp.StatusCode >= 200 && resp.StatusCode <= 399 {
return nil
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
parsedResponse := &ErrorResponse{}
if err := json.NewDecoder(resp.Body).Decode(parsedResponse); err != nil {
return &ApiError{fmt.Sprintf("Internal API error (%v)", resp.StatusCode)}
} else {
return &ApiError{parsedResponse.Message}
return &APIError{fmt.Sprintf("Internal API error (%v)", resp.StatusCode)}
}
return &APIError{parsedResponse.Message}
}
// Get makes a GET request
func (c *GitlabNetClient) Get(ctx context.Context, path string) (*http.Response, error) {
return c.DoRequest(ctx, http.MethodGet, normalizePath(path), nil)
}
// Post makes a POST request
func (c *GitlabNetClient) Post(ctx context.Context, path string, data interface{}) (*http.Response, error) {
return c.DoRequest(ctx, http.MethodPost, normalizePath(path), data)
}
// Do executes a request
func (c *GitlabNetClient) Do(request *http.Request) (*http.Response, error) {
response, err := c.httpClient.RetryableHTTP.HTTPClient.Do(request)
if err := parseError(response, err); err != nil {
response, respErr := c.httpClient.RetryableHTTP.HTTPClient.Do(request)
if err := parseError(response, respErr); err != nil {
return nil, err
}
return response, nil
}
// DoRequest executes a request with the given method, path, and data
func (c *GitlabNetClient) DoRequest(ctx context.Context, method, path string, data interface{}) (*http.Response, error) {
request, err := newRequest(ctx, method, c.httpClient.Host, path, data)
if err != nil {
Loading
Loading
@@ -165,8 +173,8 @@ func (c *GitlabNetClient) DoRequest(ctx context.Context, method, path string, da
request.Header.Add("Content-Type", "application/json")
request.Header.Add("User-Agent", c.userAgent)
response, err := c.httpClient.RetryableHTTP.Do(request)
if err := parseError(response, err); err != nil {
response, respErr := c.httpClient.RetryableHTTP.Do(request)
if err := parseError(response, respErr); err != nil {
return nil, err
}
Loading
Loading
Loading
Loading
@@ -57,21 +57,21 @@ func (c *Client) do(request *http.Request) (*http.Response, error) {
response, err := httpClient.Do(request)
if err != nil {
return nil, &client.ApiError{Msg: repoUnavailableErrMsg}
return nil, &client.APIError{Msg: repoUnavailableErrMsg}
}
if response.StatusCode >= 400 {
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, &client.ApiError{Msg: repoUnavailableErrMsg}
return nil, &client.APIError{Msg: repoUnavailableErrMsg}
}
if len(body) > 0 {
return nil, &client.ApiError{Msg: string(body)}
return nil, &client.APIError{Msg: string(body)}
}
return nil, &client.ApiError{Msg: repoUnavailableErrMsg}
return nil, &client.APIError{Msg: repoUnavailableErrMsg}
}
return response, nil
Loading
Loading
Loading
Loading
@@ -84,7 +84,7 @@ func TestFailedHTTPRequest(t *testing.T) {
require.Nil(t, response)
require.Error(t, err)
var apiErr *httpclient.ApiError
var apiErr *httpclient.APIError
require.ErrorAs(t, err, &apiErr)
require.EqualError(t, err, "You are not allowed to upload code.")
}
Loading
Loading
@@ -111,7 +111,7 @@ func TestFailedErrorReadRequest(t *testing.T) {
require.Nil(t, response)
require.Error(t, err)
var apiErr *httpclient.ApiError
var apiErr *httpclient.APIError
require.ErrorAs(t, err, &apiErr)
require.EqualError(t, err, repoUnavailableErrMsg)
}
Loading
Loading
Loading
Loading
@@ -169,7 +169,7 @@ func (c *connection) sendKeepAliveMsg(ctx context.Context, sconn *ssh.ServerConn
}
func (c *connection) trackError(ctxlog *logrus.Entry, err error) {
var apiError *client.ApiError
var apiError *client.APIError
if errors.As(err, &apiError) {
return
}
Loading
Loading
Loading
Loading
@@ -222,7 +222,7 @@ func TestSessionsMetrics(t *testing.T) {
}{
{"canceled requests", grpcstatus.Error(grpccodes.Canceled, "canceled")},
{"unavailable Gitaly", grpcstatus.Error(grpccodes.Unavailable, "unavailable")},
{"api error", &client.ApiError{"api error"}},
{"api error", &client.APIError{"api error"}},
{"disallowed command", disallowedcommand.Error},
{"not our ref", grpcstatus.Error(grpccodes.Internal, `rpc error: code = Internal desc = cmd wait: exit status 128, stderr: "fatal: git upload-pack: not our ref 9106d18f6a1b8022f6517f479696f3e3ea5e68c1"`)},
} {
Loading
Loading
Loading
Loading
@@ -133,7 +133,7 @@ func TestUserKeyHandling(t *testing.T) {
desc: "API error",
user: "user",
key: rsaPublicKey(t),
expectedErr: &client.ApiError{Msg: "Internal API unreachable"},
expectedErr: &client.APIError{Msg: "Internal API unreachable"},
}, {
desc: "successful request",
user: "user",
Loading
Loading
@@ -210,7 +210,7 @@ func TestUserCertificateHandling(t *testing.T) {
desc: "API error",
cert: userCert(t, ssh.UserCert, time.Now().Add(time.Hour)),
featureFlagValue: "1",
expectedErr: &client.ApiError{Msg: "Internal API unreachable"},
expectedErr: &client.APIError{Msg: "Internal API unreachable"},
}, {
desc: "successful request",
cert: validUserCert,
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment