Skip to content

Commit 3a35924

Browse files
committed
Merge branch 'main' of gitlab.com:gitlab-community/gitlab-shell into 793-discover-lint
2 parents defe4d4 + c5394a4 commit 3a35924

File tree

14 files changed

+94
-82
lines changed

14 files changed

+94
-82
lines changed

client/transport.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package client provides an HTTP client with enhanced logging, tracing, and correlation handling.
12
package client
23

34
import (
@@ -13,6 +14,7 @@ type transport struct {
1314
next http.RoundTripper
1415
}
1516

17+
// RoundTrip executes a single HTTP transaction, adding logging and tracing capabilities.
1618
func (rt *transport) RoundTrip(request *http.Request) (*http.Response, error) {
1719
ctx := request.Context()
1820

@@ -55,10 +57,12 @@ func (rt *transport) RoundTrip(request *http.Request) (*http.Response, error) {
5557
return response, nil
5658
}
5759

60+
// DefaultTransport returns a clone of the default HTTP transport.
5861
func DefaultTransport() http.RoundTripper {
5962
return http.DefaultTransport.(*http.Transport).Clone()
6063
}
6164

65+
// NewTransport creates a new transport with logging, tracing, and correlation handling.
6266
func NewTransport(next http.RoundTripper) http.RoundTripper {
6367
t := &transport{next: next}
6468
return correlation.NewInstrumentedRoundTripper(tracing.NewRoundTripper(t))

cmd/gitlab-sshd/main.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package main implements the GitLab SSH daemon.
12
package main
23

34
import (
@@ -27,8 +28,8 @@ var (
2728
)
2829

2930
func overrideConfigFromEnvironment(cfg *config.Config) {
30-
if gitlabUrl := os.Getenv("GITLAB_URL"); gitlabUrl != "" {
31-
cfg.GitlabUrl = gitlabUrl
31+
if gitlabURL := os.Getenv("GITLAB_URL"); gitlabURL != "" {
32+
cfg.GitlabUrl = gitlabURL
3233
}
3334
if gitlabTracing := os.Getenv("GITLAB_TRACING"); gitlabTracing != "" {
3435
cfg.GitlabTracing = gitlabTracing
@@ -67,8 +68,11 @@ func main() {
6768
cfg.ApplyGlobalState()
6869

6970
logCloser := logger.ConfigureStandalone(cfg)
70-
defer logCloser.Close()
71-
71+
defer func() {
72+
if err := logCloser.Close(); err != nil {
73+
log.WithError(err).Fatal("Error closing logCloser")
74+
}
75+
}()
7276
ctx, finished := command.Setup("gitlab-sshd", cfg)
7377
defer finished()
7478

@@ -81,15 +85,7 @@ func main() {
8185

8286
// Startup monitoring endpoint.
8387
if cfg.Server.WebListen != "" {
84-
go func() {
85-
err := monitoring.Start(
86-
monitoring.WithListenerAddress(cfg.Server.WebListen),
87-
monitoring.WithBuildInformation(Version, BuildTime),
88-
monitoring.WithServeMux(server.MonitoringServeMux()),
89-
)
90-
91-
log.WithError(err).Fatal("monitoring service raised an error")
92-
}()
88+
startupMonitoringEndpoint(cfg, server)
9389
}
9490

9591
ctx, cancel := context.WithCancel(ctx)
@@ -98,21 +94,39 @@ func main() {
9894
done := make(chan os.Signal, 1)
9995
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
10096

97+
gracefulShutdown(ctx, done, cfg, server, cancel)
98+
99+
if err := server.ListenAndServe(ctx); err != nil {
100+
log.WithError(err).Fatal("GitLab built-in sshd failed to listen for new connections")
101+
}
102+
}
103+
104+
func gracefulShutdown(ctx context.Context, done chan os.Signal, cfg *config.Config, server *sshd.Server, cancel context.CancelFunc) {
101105
go func() {
102106
sig := <-done
103107
signal.Reset(syscall.SIGINT, syscall.SIGTERM)
104108

105109
gracePeriod := time.Duration(cfg.Server.GracePeriod)
106110
log.WithContextFields(ctx, log.Fields{"shutdown_timeout_s": gracePeriod.Seconds(), "signal": sig.String()}).Info("Shutdown initiated")
107111

108-
server.Shutdown()
112+
if err := server.Shutdown(); err != nil {
113+
log.WithError(err).Fatal("Error shutting down the server")
114+
}
109115

110116
<-time.After(gracePeriod)
111117

112118
cancel()
113119
}()
120+
}
114121

115-
if err := server.ListenAndServe(ctx); err != nil {
116-
log.WithError(err).Fatal("GitLab built-in sshd failed to listen for new connections")
117-
}
122+
func startupMonitoringEndpoint(cfg *config.Config, server *sshd.Server) {
123+
go func() {
124+
err := monitoring.Start(
125+
monitoring.WithListenerAddress(cfg.Server.WebListen),
126+
monitoring.WithBuildInformation(Version, BuildTime),
127+
monitoring.WithServeMux(server.MonitoringServeMux()),
128+
)
129+
130+
log.WithError(err).Fatal("monitoring service raised an error")
131+
}()
118132
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22
55
toolchain go1.22.6
66

77
require (
8-
github.com/charmbracelet/git-lfs-transfer v0.1.1-0.20240809134258-2cab0ea18f7e
8+
github.com/charmbracelet/git-lfs-transfer v0.1.1-0.20240909190640-edbf58104250
99
github.com/git-lfs/pktline v0.0.0-20230103162542-ca444d533ef1
1010
github.com/golang-jwt/jwt/v5 v5.2.1
1111
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
@@ -18,11 +18,11 @@ require (
1818
github.com/prometheus/client_golang v1.20.3
1919
github.com/sirupsen/logrus v1.9.3
2020
github.com/stretchr/testify v1.9.0
21-
gitlab.com/gitlab-org/gitaly/v16 v16.11.8
21+
gitlab.com/gitlab-org/gitaly/v16 v16.11.9
2222
gitlab.com/gitlab-org/labkit v1.21.0
2323
golang.org/x/crypto v0.27.0
2424
golang.org/x/sync v0.8.0
25-
google.golang.org/grpc v1.66.0
25+
google.golang.org/grpc v1.66.1
2626
google.golang.org/protobuf v1.34.2
2727
gopkg.in/yaml.v3 v3.0.1
2828
)

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ github.com/census-instrumentation/opencensus-proto v0.4.1 h1:iKLQ0xPNFxR/2hzXZMr
8282
github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw=
8383
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
8484
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
85-
github.com/charmbracelet/git-lfs-transfer v0.1.1-0.20240809134258-2cab0ea18f7e h1:0ykk0ltl/PYMiDz1WfHDTc3WVg/nPY19O9SJq6y/ZW8=
86-
github.com/charmbracelet/git-lfs-transfer v0.1.1-0.20240809134258-2cab0ea18f7e/go.mod h1:84e2N3Hojky9EZivj6QyAQ7bjkZJ+pwFwqzi/1c/4iU=
85+
github.com/charmbracelet/git-lfs-transfer v0.1.1-0.20240909190640-edbf58104250 h1:7q/muqKUnoQgReeoJtS2qY13HS4qmXigl5opxTMcrUg=
86+
github.com/charmbracelet/git-lfs-transfer v0.1.1-0.20240909190640-edbf58104250/go.mod h1:eEYu9YGtNB3EhSYX+vb2BSAfxUuHMhs3mvYM1mj7ZgY=
8787
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
8888
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
8989
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
@@ -383,8 +383,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
383383
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
384384
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
385385
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
386-
gitlab.com/gitlab-org/gitaly/v16 v16.11.8 h1:bL9F90+rXTlQcsSuZJivn+CIwKGXXc787IJi4g3XQEU=
387-
gitlab.com/gitlab-org/gitaly/v16 v16.11.8/go.mod h1:lJizRUtXRd1SBHjNbbbL9OsGN4TiugvfRBd8bIsdWI0=
386+
gitlab.com/gitlab-org/gitaly/v16 v16.11.9 h1:UKuF9m7A6v4vKMWRjWQ4hATWoUd7xZstBZEtBFMdUi4=
387+
gitlab.com/gitlab-org/gitaly/v16 v16.11.9/go.mod h1:lJizRUtXRd1SBHjNbbbL9OsGN4TiugvfRBd8bIsdWI0=
388388
gitlab.com/gitlab-org/labkit v1.21.0 h1:hLmdBDtXjD1yOmZ+uJOac3a5Tlo83QaezwhES4IYik4=
389389
gitlab.com/gitlab-org/labkit v1.21.0/go.mod h1:zeATDAaSBelPcPLbTTq8J3ZJEHyPTLVBM1q3nva+/W4=
390390
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
@@ -788,8 +788,8 @@ google.golang.org/grpc v1.37.1/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQ
788788
google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
789789
google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE=
790790
google.golang.org/grpc v1.39.1/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE=
791-
google.golang.org/grpc v1.66.0 h1:DibZuoBznOxbDQxRINckZcUvnCEvrW9pcWIE2yF9r1c=
792-
google.golang.org/grpc v1.66.0/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y=
791+
google.golang.org/grpc v1.66.1 h1:hO5qAXR19+/Z44hmvIM4dQFMSYX9XcWsByfoxutBpAM=
792+
google.golang.org/grpc v1.66.1/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y=
793793
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
794794
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
795795
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
// Package disallowedcommand provides an error for handling disallowed commands.
12
package disallowedcommand
23

34
import "errors"
45

56
var (
6-
Error = errors.New("Disallowed command")
7+
// Error is returned when a disallowed command is encountered.
8+
Error = errors.New("Disallowed command") //nolint:stylecheck // Used to display the error message to the user.
79
)

internal/command/uploadarchive/gitalycall_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ func TestUploadArchive(t *testing.T) {
2121
for _, network := range []string{"unix", "tcp", "dns"} {
2222
t.Run(fmt.Sprintf("via %s network", network), func(t *testing.T) {
2323
gitalyAddress, testServer := testserver.StartGitalyServer(t, network)
24-
t.Log(fmt.Sprintf("Server address: %s", gitalyAddress))
24+
t.Logf("Server address: %s", gitalyAddress)
2525

2626
requests := requesthandlers.BuildAllowedWithGitalyHandlers(t, gitalyAddress)
2727
url := testserver.StartHTTPServer(t, requests)
2828

2929
output := &bytes.Buffer{}
3030
input := &bytes.Buffer{}
3131

32-
userId := "1"
32+
userID := "1"
3333
repo := "group/repo"
3434

3535
env := sshenv.Env{
@@ -39,7 +39,7 @@ func TestUploadArchive(t *testing.T) {
3939
}
4040

4141
args := &commandargs.Shell{
42-
GitlabKeyId: userId,
42+
GitlabKeyId: userID,
4343
CommandType: commandargs.UploadArchive,
4444
SshArgs: []string{"git-upload-archive", repo},
4545
Env: env,

internal/gitlabnet/lfsauthenticate/client_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"testing"
99

10+
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112

1213
"gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver"
@@ -26,10 +27,10 @@ func setup(t *testing.T) []testserver.TestRequestHandler {
2627
Handler: func(w http.ResponseWriter, r *http.Request) {
2728
b, err := io.ReadAll(r.Body)
2829
defer r.Body.Close()
29-
require.NoError(t, err)
30+
assert.NoError(t, err)
3031

3132
var request *Request
32-
require.NoError(t, json.Unmarshal(b, &request))
33+
assert.NoError(t, json.Unmarshal(b, &request))
3334

3435
switch request.KeyID {
3536
case keyID:
@@ -39,7 +40,7 @@ func setup(t *testing.T) []testserver.TestRequestHandler {
3940
"repository_http_path": "https://gitlab.com/repo/path",
4041
"expires_in": 1800,
4142
}
42-
require.NoError(t, json.NewEncoder(w).Encode(body))
43+
assert.NoError(t, json.NewEncoder(w).Encode(body))
4344
case "forbidden":
4445
w.WriteHeader(http.StatusForbidden)
4546
case "broken":
@@ -88,7 +89,7 @@ func TestFailedRequests(t *testing.T) {
8889
_, err = client.Authenticate(context.Background(), operation, repo, "")
8990
require.Error(t, err)
9091

91-
require.Equal(t, tc.expectedOutput, err.Error())
92+
assert.Equal(t, tc.expectedOutput, err.Error())
9293
})
9394
}
9495
}
@@ -128,7 +129,7 @@ func TestSuccessfulRequests(t *testing.T) {
128129
ExpiresIn: 1800,
129130
}
130131

131-
require.Equal(t, expectedResponse, response)
132+
assert.Equal(t, expectedResponse, response)
132133
})
133134
}
134135
}

internal/gitlabnet/lfstransfer/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package lfstransfer provides functionality for handling LFS (Large File Storage) transfers.
12
package lfstransfer
23

34
import (
@@ -17,6 +18,7 @@ import (
1718
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet"
1819
)
1920

21+
// Client holds configuration, arguments, and authentication details for the client.
2022
type Client struct {
2123
config *config.Config
2224
args *commandargs.Shell
@@ -25,13 +27,15 @@ type Client struct {
2527
header string
2628
}
2729

30+
// BatchAction represents an action for a batch operation with metadata.
2831
type BatchAction struct {
2932
Href string `json:"href"`
3033
Header map[string]string `json:"header,omitempty"`
3134
ExpiresAt time.Time `json:"expires_at,omitempty"`
3235
ExpiresIn int `json:"expires_in,omitempty"`
3336
}
3437

38+
// BatchObject represents an object in a batch operation with its metadata and actions.
3539
type BatchObject struct {
3640
Oid string `json:"oid,omitempty"`
3741
Size int64 `json:"size"`
@@ -50,6 +54,7 @@ type batchRequest struct {
5054
HashAlgorithm string `json:"hash_algo,omitempty"`
5155
}
5256

57+
// BatchResponse contains batch operation results and the hash algorithm used.
5358
type BatchResponse struct {
5459
Objects []*BatchObject `json:"objects"`
5560
HashAlgorithm string `json:"hash_algo,omitempty"`
@@ -79,30 +84,36 @@ type listLocksVerifyRequest struct {
7984
Ref *batchRef `json:"ref,omitempty"`
8085
}
8186

87+
// LockOwner represents the owner of a lock.
8288
type LockOwner struct {
8389
Name string `json:"name"`
8490
}
8591

92+
// Lock represents a lock with its ID, path, timestamp, and owner details.
8693
type Lock struct {
8794
ID string `json:"id"`
8895
Path string `json:"path"`
8996
LockedAt time.Time `json:"locked_at"`
9097
Owner *LockOwner `json:"owner"`
9198
}
9299

100+
// ListLocksResponse contains a list of locks and a cursor for pagination.
93101
type ListLocksResponse struct {
94102
Locks []*Lock `json:"locks,omitempty"`
95103
NextCursor string `json:"next_cursor,omitempty"`
96104
}
97105

106+
// ListLocksVerifyResponse provides lists of locks for "ours" and "theirs" with a cursor for pagination.
98107
type ListLocksVerifyResponse struct {
99108
Ours []*Lock `json:"ours,omitempty"`
100109
Theirs []*Lock `json:"theirs,omitempty"`
101110
NextCursor string `json:"next_cursor,omitempty"`
102111
}
103112

113+
// ClientHeader specifies the content type for Git LFS JSON requests.
104114
var ClientHeader = "application/vnd.git-lfs+json"
105115

116+
// NewClient creates a new Client instance using the provided configuration and credentials.
106117
func NewClient(config *config.Config, args *commandargs.Shell, href string, auth string) (*Client, error) {
107118
return &Client{config: config, args: args, href: href, auth: auth, header: ClientHeader}, nil
108119
}
@@ -121,6 +132,7 @@ func newHTTPClient() *retryablehttp.Client {
121132
return client
122133
}
123134

135+
// Batch performs a batch operation on objects and returns the result.
124136
func (c *Client) Batch(operation string, reqObjects []*BatchObject, ref string, reqHashAlgo string) (*BatchResponse, error) {
125137
// FIXME: This causes tests to fail
126138
// if ref == "" {
@@ -211,6 +223,7 @@ func (c *Client) PutObject(_, href string, headers map[string]string, r io.Reade
211223
return nil
212224
}
213225

226+
// Lock acquires a lock for the specified path with an optional reference name.
214227
func (c *Client) Lock(path, refname string) (*Lock, error) {
215228
var ref *batchRef
216229
if refname != "" {
@@ -268,6 +281,7 @@ func (c *Client) Lock(path, refname string) (*Lock, error) {
268281
}
269282
}
270283

284+
// Unlock releases the lock with the given id, optionally forcing the unlock.
271285
func (c *Client) Unlock(id string, force bool, refname string) (*Lock, error) {
272286
var ref *batchRef
273287
if refname != "" {
@@ -318,6 +332,7 @@ func (c *Client) Unlock(id string, force bool, refname string) (*Lock, error) {
318332
}
319333
}
320334

335+
// ListLocksVerify retrieves locks for the given path and id, with optional pagination.
321336
func (c *Client) ListLocksVerify(path, id, cursor string, limit int, ref string) (*ListLocksVerifyResponse, error) {
322337
url, err := url.Parse(c.href)
323338
if err != nil {

internal/sshd/connection.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,15 @@ func (c *connection) sendKeepAliveMsg(ctx context.Context, sconn *ssh.ServerConn
168168
case <-ticker.C:
169169
ctxlog.Debug("connection: sendKeepAliveMsg: send keepalive message to a client")
170170

171-
_, _, _ = sconn.SendRequest(KeepAliveMsg, true, nil)
171+
status, payload, err := sconn.SendRequest(KeepAliveMsg, true, nil)
172+
if err != nil {
173+
ctxlog.Errorf("Error occurred while sending request :%v", err)
174+
return
175+
}
176+
177+
if status {
178+
ctxlog.Debugf("connection: sendKeepAliveMsg: payload: %v", string(payload))
179+
}
172180
}
173181
}
174182
}

internal/sshd/connection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (f *fakeConn) SendRequest(name string, _ bool, _ []byte) (bool, []byte, err
7878

7979
f.sentRequestName = name
8080

81-
return true, nil, nil
81+
return true, []byte("I am a response"), nil
8282
}
8383

8484
func setup(newChannel *fakeNewChannel) (*connection, chan ssh.NewChannel) {

0 commit comments

Comments
 (0)