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
|
package praefect
import (
"context"
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
gitalyauth "gitlab.com/gitlab-org/gitaly/v14/auth"
"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config/auth"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/config"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore/glsql"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/mock"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/nodes"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/protoregistry"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/transactions"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper/promtest"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
func TestAuthFailures(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
testCases := []struct {
desc string
opts []grpc.DialOption
code codes.Code
}{
{
desc: "no auth",
opts: nil,
code: codes.Unauthenticated,
},
{
desc: "invalid auth",
opts: []grpc.DialOption{grpc.WithPerRPCCredentials(brokenAuth{})},
code: codes.Unauthenticated,
},
{
desc: "wrong secret new auth",
opts: []grpc.DialOption{grpc.WithPerRPCCredentials(gitalyauth.RPCCredentialsV2("foobar"))},
code: codes.PermissionDenied,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
srv, serverSocketPath, cleanup := runServer(t, "quxbaz", true)
defer srv.Stop()
defer cleanup()
connOpts := append(tc.opts, grpc.WithInsecure())
conn, err := dial(serverSocketPath, connOpts)
require.NoError(t, err, tc.desc)
defer conn.Close()
cli := mock.NewSimpleServiceClient(conn)
_, err = cli.RepoAccessorUnary(ctx, &mock.RepoRequest{})
testhelper.RequireGrpcError(t, err, tc.code)
})
}
}
func TestAuthSuccess(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
token := "foobar"
testCases := []struct {
desc string
opts []grpc.DialOption
required bool
token string
}{
{desc: "no auth, not required"},
{
desc: "v2 correct auth, not required",
opts: []grpc.DialOption{grpc.WithPerRPCCredentials(gitalyauth.RPCCredentialsV2(token))},
token: token,
},
{
desc: "v2 incorrect auth, not required",
opts: []grpc.DialOption{grpc.WithPerRPCCredentials(gitalyauth.RPCCredentialsV2("incorrect"))},
token: token,
},
{
desc: "v2 correct auth, required",
opts: []grpc.DialOption{grpc.WithPerRPCCredentials(gitalyauth.RPCCredentialsV2(token))},
token: token,
required: true,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
srv, serverSocketPath, cleanup := runServer(t, tc.token, tc.required)
defer srv.Stop()
defer cleanup()
connOpts := append(tc.opts, grpc.WithInsecure())
conn, err := dial(serverSocketPath, connOpts)
require.NoError(t, err, tc.desc)
defer conn.Close()
cli := gitalypb.NewServerServiceClient(conn)
_, err = cli.ServerInfo(ctx, &gitalypb.ServerInfoRequest{})
assert.NoError(t, err, tc.desc)
})
}
}
type brokenAuth struct{}
func (brokenAuth) RequireTransportSecurity() bool { return false }
func (brokenAuth) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return map[string]string{"authorization": "Bearer blablabla"}, nil
}
func dial(serverSocketPath string, opts []grpc.DialOption) (*grpc.ClientConn, error) {
return grpc.Dial(serverSocketPath, opts...)
}
func runServer(t *testing.T, token string, required bool) (*grpc.Server, string, func()) {
backendToken := "abcxyz"
backend, cleanup := newMockDownstream(t, backendToken, &mockSvc{})
conf := config.Config{
Auth: auth.Config{Token: token, Transitioning: !required},
VirtualStorages: []*config.VirtualStorage{
&config.VirtualStorage{
Name: "praefect",
Nodes: []*config.Node{
&config.Node{
Storage: "praefect-internal-0",
Address: backend,
Token: backendToken,
},
},
},
},
}
logEntry := testhelper.DiscardTestEntry(t)
queue := datastore.NewPostgresReplicationEventQueue(glsql.GetDB(t))
nodeMgr, err := nodes.NewManager(logEntry, conf, nil, nil, promtest.NewMockHistogramVec(), protoregistry.GitalyProtoPreregistered, nil, nil)
require.NoError(t, err)
txMgr := transactions.NewManager(conf)
registry, err := protoregistry.NewFromPaths("praefect/mock/mock.proto")
require.NoError(t, err)
coordinator := NewCoordinator(queue, nil, NewNodeManagerRouter(nodeMgr, nil), txMgr, conf, registry)
srv := NewGRPCServer(conf, logEntry, registry, coordinator.StreamDirector, nodeMgr, txMgr, queue, nil, nil, nil, nil)
serverSocketPath := testhelper.GetTemporaryGitalySocketFileName(t)
listener, err := net.Listen("unix", serverSocketPath)
require.NoError(t, err)
go srv.Serve(listener)
return srv, "unix://" + serverSocketPath, cleanup
}
|