Skip to content

Commit 7780ac9

Browse files
author
Patrick Bajao
committed
Merge branch 'id-ignore-disallowed-cmd-err' into 'main'
Exclude disallowed command from error rate See merge request gitlab-org/gitlab-shell!654
2 parents 158109a + 9cc4380 commit 7780ac9

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

internal/sshd/connection.go

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
grpcstatus "google.golang.org/grpc/status"
1515

1616
"gitlab.com/gitlab-org/gitlab-shell/client"
17+
"gitlab.com/gitlab-org/gitlab-shell/internal/command/shared/disallowedcommand"
1718
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
1819
"gitlab.com/gitlab-org/gitlab-shell/internal/metrics"
1920

@@ -165,6 +166,10 @@ func (c *connection) trackError(ctxlog *logrus.Entry, err error) {
165166
return
166167
}
167168

169+
if errors.Is(err, disallowedcommand.Error) {
170+
return
171+
}
172+
168173
grpcCode := grpcstatus.Code(err)
169174
if grpcCode == grpccodes.Canceled || grpcCode == grpccodes.Unavailable {
170175
return

internal/sshd/connection_test.go

+21-26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
grpcstatus "google.golang.org/grpc/status"
1616

1717
"gitlab.com/gitlab-org/gitlab-shell/client"
18+
"gitlab.com/gitlab-org/gitlab-shell/internal/command/shared/disallowedcommand"
1819
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
1920
"gitlab.com/gitlab-org/gitlab-shell/internal/metrics"
2021
)
@@ -212,30 +213,24 @@ func TestSessionsMetrics(t *testing.T) {
212213
require.InDelta(t, initialSessionsTotal+1, testutil.ToFloat64(metrics.SliSshdSessionsTotal), 0.1)
213214
require.InDelta(t, initialSessionsErrorTotal+1, testutil.ToFloat64(metrics.SliSshdSessionsErrorsTotal), 0.1)
214215

215-
conn, chans = setup(1, newChannel)
216-
conn.handleRequests(context.Background(), nil, chans, func(*ssh.ServerConn, ssh.Channel, <-chan *ssh.Request) error {
217-
close(chans)
218-
return grpcstatus.Error(grpccodes.Canceled, "canceled")
219-
})
220-
221-
require.InDelta(t, initialSessionsTotal+2, testutil.ToFloat64(metrics.SliSshdSessionsTotal), 0.1)
222-
require.InDelta(t, initialSessionsErrorTotal+1, testutil.ToFloat64(metrics.SliSshdSessionsErrorsTotal), 0.1)
223-
224-
conn, chans = setup(1, newChannel)
225-
conn.handleRequests(context.Background(), nil, chans, func(*ssh.ServerConn, ssh.Channel, <-chan *ssh.Request) error {
226-
close(chans)
227-
return &client.ApiError{"api error"}
228-
})
229-
230-
require.InDelta(t, initialSessionsTotal+3, testutil.ToFloat64(metrics.SliSshdSessionsTotal), 0.1)
231-
require.InDelta(t, initialSessionsErrorTotal+1, testutil.ToFloat64(metrics.SliSshdSessionsErrorsTotal), 0.1)
232-
233-
conn, chans = setup(1, newChannel)
234-
conn.handleRequests(context.Background(), nil, chans, func(*ssh.ServerConn, ssh.Channel, <-chan *ssh.Request) error {
235-
close(chans)
236-
return grpcstatus.Error(grpccodes.Unavailable, "unavailable")
237-
})
238-
239-
require.InDelta(t, initialSessionsTotal+4, testutil.ToFloat64(metrics.SliSshdSessionsTotal), 0.1)
240-
require.InDelta(t, initialSessionsErrorTotal+1, testutil.ToFloat64(metrics.SliSshdSessionsErrorsTotal), 0.1)
216+
for i, ignoredError := range []struct {
217+
desc string
218+
err error
219+
}{
220+
{"canceled requests", grpcstatus.Error(grpccodes.Canceled, "canceled")},
221+
{"unavailable Gitaly", grpcstatus.Error(grpccodes.Unavailable, "unavailable")},
222+
{"api error", &client.ApiError{"api error"}},
223+
{"disallowed command", disallowedcommand.Error},
224+
} {
225+
t.Run(ignoredError.desc, func(t *testing.T) {
226+
conn, chans = setup(1, newChannel)
227+
conn.handleRequests(context.Background(), nil, chans, func(*ssh.ServerConn, ssh.Channel, <-chan *ssh.Request) error {
228+
close(chans)
229+
return ignoredError.err
230+
})
231+
232+
require.InDelta(t, initialSessionsTotal+2+float64(i), testutil.ToFloat64(metrics.SliSshdSessionsTotal), 0.1)
233+
require.InDelta(t, initialSessionsErrorTotal+1, testutil.ToFloat64(metrics.SliSshdSessionsErrorsTotal), 0.1)
234+
})
235+
}
241236
}

0 commit comments

Comments
 (0)