Skip to content

Commit 34b3820

Browse files
Igor DrozdovArchish27
authored andcommitted
Merge branch '781-sshd-lint' into 'main'
Lint fixes for sshd packages Closes #781 See merge request https://gitlab.com/gitlab-org/gitlab-shell/-/merge_requests/1138 Merged-by: Igor Drozdov <[email protected]> Approved-by: Ash McKenzie <[email protected]> Approved-by: Igor Drozdov <[email protected]> Co-authored-by: Archish <[email protected]>
2 parents e011752 + 21b0d3e commit 34b3820

File tree

5 files changed

+43
-37
lines changed

5 files changed

+43
-37
lines changed

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
}

internal/sshd/gssapi_unsupported.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/config"
99
)
1010

11+
// NewGSSAPIServer initializes and returns a new OSGSSAPIServer.
1112
func NewGSSAPIServer(c *config.GSSAPIConfig) (*OSGSSAPIServer, error) {
1213
s := &OSGSSAPIServer{
1314
ServicePrincipalName: c.ServicePrincipalName,
@@ -16,18 +17,22 @@ func NewGSSAPIServer(c *config.GSSAPIConfig) (*OSGSSAPIServer, error) {
1617
return s, nil
1718
}
1819

20+
// OSGSSAPIServer represents a server that handles GSSAPI requests.
1921
type OSGSSAPIServer struct {
2022
ServicePrincipalName string
2123
}
2224

25+
// AcceptSecContext returns an error indicating that GSSAPI is unsupported.
2326
func (*OSGSSAPIServer) AcceptSecContext([]byte) ([]byte, string, bool, error) {
2427
return []byte{}, "", false, errors.New("gssapi is unsupported")
2528
}
2629

30+
// VerifyMIC returns an error indicating that GSSAPI is unsupported.
2731
func (*OSGSSAPIServer) VerifyMIC([]byte, []byte) error {
2832
return errors.New("gssapi is unsupported")
2933
}
3034

35+
// DeleteSecContext returns an error indicating that GSSAPI is unsupported.
3136
func (*OSGSSAPIServer) DeleteSecContext() error {
3237
return errors.New("gssapi is unsupported")
3338
}

internal/sshd/server_config_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ func TestHostKeyAndCerts(t *testing.T) {
6060
data, err := os.ReadFile(path.Join(testRoot, "certs/valid/server.pub"))
6161
require.NoError(t, err)
6262

63-
publicKey, _, _, _, err := ssh.ParseAuthorizedKey(data)
63+
publicKey, comment, _, _, err := ssh.ParseAuthorizedKey(data)
6464
require.NoError(t, err)
65+
require.NotNil(t, comment)
6566
require.NotNil(t, publicKey)
6667
cert, ok := cfg.hostKeyToCertMap[string(publicKey.Marshal())]
6768
require.True(t, ok)

internal/sshd/sshd_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/pires/go-proxyproto"
15+
"github.com/stretchr/testify/assert"
1516
"github.com/stretchr/testify/require"
1617
"golang.org/x/crypto/ssh"
1718

@@ -409,16 +410,16 @@ func setupServerWithContext(ctx context.Context, t *testing.T, cfg *config.Confi
409410
Handler: func(w http.ResponseWriter, r *http.Request) {
410411
correlationID = r.Header.Get("X-Request-Id")
411412

412-
require.NotEmpty(t, correlationID)
413-
require.Equal(t, xForwardedFor, r.Header.Get("X-Forwarded-For"))
413+
assert.NotEmpty(t, correlationID)
414+
assert.Equal(t, xForwardedFor, r.Header.Get("X-Forwarded-For"))
414415

415416
fmt.Fprint(w, `{"id": 1000, "key": "key"}`)
416417
},
417418
}, {
418419
Path: "/api/v4/internal/discover",
419420
Handler: func(w http.ResponseWriter, r *http.Request) {
420-
require.Equal(t, correlationID, r.Header.Get("X-Request-Id"))
421-
require.Equal(t, xForwardedFor, r.Header.Get("X-Forwarded-For"))
421+
assert.Equal(t, correlationID, r.Header.Get("X-Request-Id"))
422+
assert.Equal(t, xForwardedFor, r.Header.Get("X-Forwarded-For"))
422423

423424
fmt.Fprint(w, `{"id": 1000, "name": "Test User", "username": "test-user"}`)
424425
},

support/lint_last_known_acceptable.txt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ cmd/gitlab-sshd/acceptance_test.go:132:5: go-require: do not use require in http
5353
cmd/gitlab-sshd/acceptance_test.go:135:5: go-require: do not use require in http handlers (testifylint)
5454
cmd/gitlab-sshd/acceptance_test.go:188:4: go-require: do not use require in http handlers (testifylint)
5555
cmd/gitlab-sshd/acceptance_test.go:498:4: go-require: do not use require in http handlers (testifylint)
56-
cmd/gitlab-sshd/main.go:1:1: package-comments: should have a package comment (revive)
57-
cmd/gitlab-sshd/main.go:30:5: var-naming: var gitlabUrl should be gitlabURL (revive)
58-
cmd/gitlab-sshd/main.go:44: Function 'main' is too long (73 > 60) (funlen)
59-
cmd/gitlab-sshd/main.go:70:23: Error return value of `logCloser.Close` is not checked (errcheck)
60-
cmd/gitlab-sshd/main.go:108:18: Error return value of `server.Shutdown` is not checked (errcheck)
6156
internal/command/authorizedkeys/authorized_keys.go:29: internal/command/authorizedkeys/authorized_keys.go:29: Line contains TODO/BUG/FIXME/NOTE/OPTIMIZE/HACK: "TODO: Log this event once we have a cons..." (godox)
6257
internal/command/command.go:1:1: package-comments: should have a package comment (revive)
6358
internal/command/command.go:15:6: exported: exported type Command should have comment or be unexported (revive)
@@ -249,18 +244,8 @@ internal/gitlabnet/client.go:35:1: exported: exported function ParseIP should ha
249244
internal/gitlabnet/healthcheck/client_test.go:19:41: unused-parameter: parameter 'r' seems to be unused, consider removing or renaming it as _ (revive)
250245
internal/gitlabnet/lfstransfer/client.go:137: internal/gitlabnet/lfstransfer/client.go:137: Line contains TODO/BUG/FIXME/NOTE/OPTIMIZE/HACK: "FIXME: This causes tests to fail" (godox)
251246
internal/gitlabnet/personalaccesstoken/client_test.go:30:5: go-require: do not use require in http handlers (testifylint)
252-
internal/sshd/gssapi_unsupported.go:11:1: exported: exported function NewGSSAPIServer should have comment or be unexported (revive)
253-
internal/sshd/gssapi_unsupported.go:19:6: exported: exported type OSGSSAPIServer should have comment or be unexported (revive)
254-
internal/sshd/gssapi_unsupported.go:23:1: exported: exported method OSGSSAPIServer.AcceptSecContext should have comment or be unexported (revive)
255-
internal/sshd/gssapi_unsupported.go:27:1: exported: exported method OSGSSAPIServer.VerifyMIC should have comment or be unexported (revive)
256-
internal/sshd/gssapi_unsupported.go:31:1: exported: exported method OSGSSAPIServer.DeleteSecContext should have comment or be unexported (revive)
257247
internal/sshd/server_config_test.go:5:2: SA1019: "crypto/dsa" has been deprecated since Go 1.16 because it shouldn't be used: DSA is a legacy algorithm, and modern alternatives such as Ed25519 (implemented by package crypto/ed25519) should be used instead. Keys with 1024-bit moduli (L1024N160 parameters) are cryptographically weak, while bigger keys are not widely supported. Note that FIPS 186-5 no longer approves DSA for signature generation. (staticcheck)
258-
internal/sshd/server_config_test.go:63:2: declaration has 3 blank identifiers (dogsled)
259248
internal/sshd/sshd.go:268:6: func `extractDataFromContext` is unused (unused)
260-
internal/sshd/sshd_test.go:412:5: go-require: do not use require in http handlers (testifylint)
261-
internal/sshd/sshd_test.go:413:5: go-require: do not use require in http handlers (testifylint)
262-
internal/sshd/sshd_test.go:420:5: go-require: do not use require in http handlers (testifylint)
263-
internal/sshd/sshd_test.go:421:5: go-require: do not use require in http handlers (testifylint)
264249
internal/testhelper/requesthandlers/requesthandlers.go:25:5: go-require: do not use require in http handlers (testifylint)
265250
internal/testhelper/requesthandlers/requesthandlers.go:63:5: go-require: do not use require in http handlers (testifylint)
266251
internal/testhelper/requesthandlers/requesthandlers.go:90:5: go-require: do not use require in http handlers (testifylint)

0 commit comments

Comments
 (0)