summaryrefslogtreecommitdiff
path: root/internal/handler/exec_test.go
blob: ecc4435b9df65aa674925ba5c77ae524e326e8f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package handler

import (
	"context"
	"errors"
	"testing"

	"github.com/stretchr/testify/require"
	"google.golang.org/grpc"
	grpccodes "google.golang.org/grpc/codes"
	"google.golang.org/grpc/metadata"
	grpcstatus "google.golang.org/grpc/status"

	pb "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/commandargs"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/config"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/accessverifier"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/sshenv"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
	"google.golang.org/protobuf/types/known/durationpb"
)

func makeHandler(t *testing.T, err error) func(context.Context, *grpc.ClientConn) (int32, error) {
	return func(ctx context.Context, client *grpc.ClientConn) (int32, error) {
		require.NotNil(t, ctx)
		require.NotNil(t, client)

		return 0, err
	}
}

func TestRunGitalyCommand(t *testing.T) {
	cmd := NewGitalyCommand(
		newConfig(),
		string(commandargs.UploadPack),
		&accessverifier.Response{
			Gitaly: accessverifier.Gitaly{Address: "tcp://localhost:9999"},
		},
	)

	err := cmd.RunGitalyCommand(context.Background(), makeHandler(t, nil))
	require.NoError(t, err)

	expectedErr := errors.New("error")
	err = cmd.RunGitalyCommand(context.Background(), makeHandler(t, expectedErr))
	require.Equal(t, err, expectedErr)
}

func TestCachingOfGitalyConnections(t *testing.T) {
	ctx := context.Background()
	cfg := newConfig()
	response := &accessverifier.Response{
		Username: "user",
		Gitaly: accessverifier.Gitaly{
			Address: "tcp://localhost:9999",
			Token:   "token",
		},
	}

	cmd := NewGitalyCommand(cfg, string(commandargs.UploadPack), response)

	conn, err := cmd.getConn(ctx)
	require.NoError(t, err)

	// Reuses connection for different users
	response.Username = "another-user"
	cmd = NewGitalyCommand(cfg, string(commandargs.UploadPack), response)
	newConn, err := cmd.getConn(ctx)
	require.NoError(t, err)
	require.Equal(t, conn, newConn)
}

func TestMissingGitalyAddress(t *testing.T) {
	cmd := GitalyCommand{Config: newConfig()}

	err := cmd.RunGitalyCommand(context.Background(), makeHandler(t, nil))
	require.EqualError(t, err, "no gitaly_address given")
}

func TestUnavailableGitalyErr(t *testing.T) {
	cmd := NewGitalyCommand(
		newConfig(),
		string(commandargs.UploadPack),
		&accessverifier.Response{
			Gitaly: accessverifier.Gitaly{Address: "tcp://localhost:9999"},
		},
	)

	err := cmd.RunGitalyCommand(context.Background(), makeHandler(t, grpcstatus.Error(grpccodes.Unavailable, "error")))
	require.Equal(t, err, grpcstatus.Error(grpccodes.Unavailable, "The git server, Gitaly, is not available at this time. Please contact your administrator."))
}

func TestGitalyLimitErr(t *testing.T) {
	cmd := NewGitalyCommand(
		newConfig(),
		string(commandargs.UploadPack),
		&accessverifier.Response{
			Gitaly: accessverifier.Gitaly{Address: "tcp://localhost:9999"},
		},
	)
	limitErr := errWithDetail(t, &pb.LimitError{
		ErrorMessage: "concurrency queue wait time reached",
		RetryAfter:   durationpb.New(0)})
	err := cmd.RunGitalyCommand(context.Background(), makeHandler(t, limitErr))
	require.Equal(t, err, grpcstatus.Error(grpccodes.Unavailable, "GitLab is currently unable to handle this request due to load."))
}

func TestRunGitalyCommandMetadata(t *testing.T) {
	tests := []struct {
		name string
		gc   *GitalyCommand
		want map[string]string
	}{
		{
			name: "gitaly_feature_flags",
			gc: NewGitalyCommand(
				newConfig(),
				string(commandargs.UploadPack),
				&accessverifier.Response{
					Gitaly: accessverifier.Gitaly{
						Address: "tcp://localhost:9999",
						Features: map[string]string{
							"gitaly-feature-cache_invalidator":        "true",
							"other-ff":                                "true",
							"gitaly-feature-inforef_uploadpack_cache": "false",
						},
					},
				},
			),
			want: map[string]string{
				"gitaly-feature-cache_invalidator":        "true",
				"gitaly-feature-inforef_uploadpack_cache": "false",
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cmd := tt.gc

			err := cmd.RunGitalyCommand(context.Background(), func(ctx context.Context, _ *grpc.ClientConn) (int32, error) {
				md, exists := metadata.FromOutgoingContext(ctx)
				require.True(t, exists)
				require.Equal(t, len(tt.want), md.Len())

				for k, v := range tt.want {
					values := md.Get(k)
					require.Equal(t, 1, len(values))
					require.Equal(t, v, values[0])
				}

				return 0, nil
			})

			require.NoError(t, err)
		})
	}
}

func TestPrepareContext(t *testing.T) {
	tests := []struct {
		name     string
		gc       *GitalyCommand
		env      sshenv.Env
		repo     *pb.Repository
		response *accessverifier.Response
		want     map[string]string
	}{
		{
			name: "client_identity",
			gc: NewGitalyCommand(
				&config.Config{},
				string(commandargs.UploadPack),
				&accessverifier.Response{
					KeyId:    1,
					KeyType:  "key",
					UserId:   "6",
					Username: "jane.doe",
					Gitaly: accessverifier.Gitaly{
						Address: "tcp://localhost:9999",
					},
				},
			),
			env: sshenv.Env{
				GitProtocolVersion: "protocol",
				IsSSHConnection:    true,
				RemoteAddr:         "10.0.0.1",
			},
			repo: &pb.Repository{
				StorageName:                   "default",
				RelativePath:                  "@hashed/5f/9c/5f9c4ab08cac7457e9111a30e4664920607ea2c115a1433d7be98e97e64244ca.git",
				GitObjectDirectory:            "path/to/git_object_directory",
				GitAlternateObjectDirectories: []string{"path/to/git_alternate_object_directory"},
				GlRepository:                  "project-26",
				GlProjectPath:                 "group/private",
			},
			want: map[string]string{
				"key_id":    "1",
				"key_type":  "key",
				"user_id":   "6",
				"username":  "jane.doe",
				"remote_ip": "10.0.0.1",
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ctx := context.Background()

			ctx, cancel := tt.gc.PrepareContext(ctx, tt.repo, tt.env)
			defer cancel()

			md, exists := metadata.FromOutgoingContext(ctx)
			require.True(t, exists)
			require.Equal(t, len(tt.want), md.Len())

			for k, v := range tt.want {
				values := md.Get(k)
				require.Equal(t, 1, len(values))
				require.Equal(t, v, values[0])
			}

		})
	}
}

func newConfig() *config.Config {
	cfg := &config.Config{}
	cfg.GitalyClient.InitSidechannelRegistry(context.Background())
	return cfg
}

// errWithDetail adds the given details to the error if it is a gRPC status whose code is not OK.
func errWithDetail(t *testing.T, detail proto.Message) error {
	st := grpcstatus.New(grpccodes.Unavailable, "too busy")

	proto := st.Proto()
	marshaled, err := anypb.New(detail)
	require.NoError(t, err)

	proto.Details = append(proto.Details, marshaled)

	return grpcstatus.ErrorProto(proto)
}