/
server_test.go
366 lines (322 loc) · 11.6 KB
/
server_test.go
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
"context"
"fmt"
"io"
"reflect"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/google/uuid"
"google.golang.org/grpc/metadata"
authv1 "k8s.io/api/authentication/v1"
"k8s.io/apimachinery/pkg/runtime"
k8sfake "k8s.io/client-go/kubernetes/fake"
fakeauthenticationv1 "k8s.io/client-go/kubernetes/typed/authentication/v1/fake"
k8stesting "k8s.io/client-go/testing"
client "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
agentmock "sigs.k8s.io/apiserver-network-proxy/proto/agent/mocks"
"sigs.k8s.io/apiserver-network-proxy/proto/header"
)
func TestAgentTokenAuthenticationErrorsToken(t *testing.T) {
stub := gomock.NewController(t)
defer stub.Finish()
ns := "test_ns"
sa := "test_sa"
testCases := []struct {
desc string
mdKey string
tokens []string
wantNamespace string
wantServiceAccount string
authenticated bool
authError string
tokenReviewError error
wantError bool
}{
{
desc: "no context",
wantError: true,
},
{
desc: "non valid metadata key",
mdKey: "someKey",
tokens: []string{"token1"},
wantError: true,
},
{
desc: "non valid token prefix",
mdKey: header.AuthenticationTokenContextKey,
tokens: []string{"token1"},
wantError: true,
},
{
desc: "multiple valid tokens",
mdKey: header.AuthenticationTokenContextKey,
tokens: []string{header.AuthenticationTokenContextSchemePrefix + "token1", header.AuthenticationTokenContextSchemePrefix + "token2"},
wantError: true,
},
{
desc: "not authenticated",
authenticated: false,
mdKey: header.AuthenticationTokenContextKey,
tokens: []string{header.AuthenticationTokenContextSchemePrefix + "token1"},
wantNamespace: ns,
wantServiceAccount: sa,
wantError: true,
},
{
desc: "tokenReview error",
authenticated: false,
mdKey: header.AuthenticationTokenContextKey,
tokens: []string{header.AuthenticationTokenContextSchemePrefix + "token1"},
tokenReviewError: fmt.Errorf("some error"),
wantNamespace: ns,
wantServiceAccount: sa,
wantError: true,
},
{
desc: "non valid namespace",
authenticated: true,
mdKey: header.AuthenticationTokenContextKey,
tokens: []string{header.AuthenticationTokenContextSchemePrefix + "token1"},
wantNamespace: "_" + ns,
wantServiceAccount: sa,
wantError: true,
},
{
desc: "non valid service account",
authenticated: true,
mdKey: header.AuthenticationTokenContextKey,
tokens: []string{header.AuthenticationTokenContextSchemePrefix + "token1"},
wantNamespace: ns,
wantServiceAccount: "_" + sa,
wantError: true,
},
{
desc: "authorization succeed",
authenticated: true,
mdKey: header.AuthenticationTokenContextKey,
tokens: []string{header.AuthenticationTokenContextSchemePrefix + "token1"},
wantNamespace: ns,
wantServiceAccount: sa,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
kcs := k8sfake.NewSimpleClientset()
kcs.AuthenticationV1().(*fakeauthenticationv1.FakeAuthenticationV1).Fake.PrependReactor("create", "tokenreviews", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
tr := &authv1.TokenReview{
Status: authv1.TokenReviewStatus{
Authenticated: tc.authenticated,
Error: tc.authError,
User: authv1.UserInfo{
Username: fmt.Sprintf("system:serviceaccount:%v:%v", ns, sa),
},
},
}
return true, tr, tc.tokenReviewError
})
var md metadata.MD
for _, token := range tc.tokens {
md = metadata.Join(md, metadata.Pairs(tc.mdKey, token))
}
md = metadata.Join(md, metadata.Pairs(header.AgentID, ""))
ctx := context.Background()
defer ctx.Done()
ctx = metadata.NewIncomingContext(ctx, md)
conn := agentmock.NewMockAgentService_ConnectServer(stub)
conn.EXPECT().Context().AnyTimes().Return(ctx)
// close agent's connection if no error is expected
if !tc.wantError {
conn.EXPECT().SendHeader(gomock.Any()).Return(nil)
conn.EXPECT().Recv().Return(nil, io.EOF)
}
p := NewProxyServer("", []ProxyStrategy{ProxyStrategyDefault}, 1, &AgentTokenAuthenticationOptions{
Enabled: true,
KubernetesClient: kcs,
AgentNamespace: tc.wantNamespace,
AgentServiceAccount: tc.wantServiceAccount,
},
false,
)
err := p.Connect(conn)
if tc.wantError {
if err == nil {
t.Errorf("test case expected for error")
}
} else {
if err != nil {
t.Errorf("did not expected for error but got :%v", err)
}
}
})
}
}
func TestAddRemoveFrontends(t *testing.T) {
agent1ConnID1 := new(ProxyClientConnection)
agent1ConnID2 := new(ProxyClientConnection)
agent2ConnID1 := new(ProxyClientConnection)
agent2ConnID2 := new(ProxyClientConnection)
agent3ConnID1 := new(ProxyClientConnection)
p := NewProxyServer("", []ProxyStrategy{ProxyStrategyDefault}, 1, nil, false)
p.addFrontend("agent1", int64(1), agent1ConnID1)
p.removeFrontend("agent1", int64(1))
expectedFrontends := make(map[string]map[int64]*ProxyClientConnection)
if e, a := expectedFrontends, p.frontends; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
p = NewProxyServer("", []ProxyStrategy{ProxyStrategyDefault}, 1, nil, false)
p.addFrontend("agent1", int64(1), agent1ConnID1)
p.addFrontend("agent1", int64(2), agent1ConnID2)
p.addFrontend("agent2", int64(1), agent2ConnID1)
p.addFrontend("agent2", int64(2), agent2ConnID2)
p.addFrontend("agent3", int64(1), agent3ConnID1)
p.removeFrontend("agent2", int64(1))
p.removeFrontend("agent2", int64(2))
p.removeFrontend("agent1", int64(1))
expectedFrontends = map[string]map[int64]*ProxyClientConnection{
"agent1": {
int64(2): agent1ConnID2,
},
"agent3": {
int64(1): agent3ConnID1,
},
}
if e, a := expectedFrontends, p.frontends; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
}
func prepareFrontendConn(ctrl *gomock.Controller) *agentmock.MockAgentService_ConnectServer {
// prepare the connection to fontend of proxy-server
frontendConn := agentmock.NewMockAgentService_ConnectServer(ctrl)
frontendConnMD := metadata.MD{
":authority": []string{"127.0.0.1:8090"},
"content-type": []string{"application/grpc"},
"user-agent": []string{"grpc-go/1.42.0"},
}
frontendConnCtx := metadata.NewIncomingContext(context.Background(), frontendConnMD)
frontendConn.EXPECT().Context().Return(frontendConnCtx).AnyTimes()
return frontendConn
}
func prepareAgentConnMD(ctrl *gomock.Controller, proxyServer *ProxyServer) *agentmock.MockAgentService_ConnectServer {
// prepare the the connection to agent of proxy-server
agentConn := agentmock.NewMockAgentService_ConnectServer(ctrl)
agentConnMD := metadata.MD{
":authority": []string{"127.0.0.1:8091"},
"agentid": []string{uuid.New().String()},
"agentidentifiers": []string{},
"content-type": []string{"application/grpc"},
"user-agent": []string{"grpc-go/1.42.0"},
}
agentConnCtx := metadata.NewIncomingContext(context.Background(), agentConnMD)
agentConn.EXPECT().Context().Return(agentConnCtx).AnyTimes()
_ = proxyServer.addBackend(uuid.New().String(), agentConn)
return agentConn
}
func baseServerProxyTestWithoutBackend(t *testing.T, validate func(*agentmock.MockAgentService_ConnectServer)) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
frontendConn := prepareFrontendConn(ctrl)
proxyServer := NewProxyServer(uuid.New().String(), []ProxyStrategy{ProxyStrategyDefault}, 1, &AgentTokenAuthenticationOptions{}, true)
validate(frontendConn)
proxyServer.Proxy(frontendConn)
// add a sleep to make sure `serveRecvFrontend` ends after `Proxy` finished.
time.Sleep(1 * time.Second)
}
func baseServerProxyTestWithBackend(t *testing.T, validate func(*agentmock.MockAgentService_ConnectServer, *agentmock.MockAgentService_ConnectServer)) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
frontendConn := prepareFrontendConn(ctrl)
// prepare proxy server
proxyServer := NewProxyServer(uuid.New().String(), []ProxyStrategy{ProxyStrategyDefault}, 1, &AgentTokenAuthenticationOptions{}, true)
agentConn := prepareAgentConnMD(ctrl, proxyServer)
validate(frontendConn, agentConn)
proxyServer.Proxy(frontendConn)
// add a sleep to make sure `serveRecvFrontend` ends after `Proxy` finished.
time.Sleep(1 * time.Second)
}
func TestServerProxyNoBackend(t *testing.T) {
validate := func(frontendConn *agentmock.MockAgentService_ConnectServer) {
// receive DIAL_REQ from frontend and proxy to backend
dialReq := &client.Packet{
Type: client.PacketType_DIAL_REQ,
Payload: &client.Packet_DialRequest{
DialRequest: &client.DialRequest{
Protocol: "tcp",
Address: "127.0.0.1:8080",
Random: 111,
},
},
}
dialResp := &client.Packet{
Type: client.PacketType_DIAL_RSP,
Payload: &client.Packet_DialResponse{
DialResponse: &client.DialResponse{
Random: 111,
Error: (&ErrNotFound{}).Error(),
}},
}
gomock.InOrder(
frontendConn.EXPECT().Recv().Return(dialReq, nil).Times(1),
frontendConn.EXPECT().Recv().Return(nil, io.EOF).Times(1),
// NOTE(mainred): `Send` should come before `Recv` io.EOF, but we cannot add wait between
// two Recvs, thus `Recv`` comes before `Send`
frontendConn.EXPECT().Send(dialResp).Return(nil).Times(1),
)
}
baseServerProxyTestWithoutBackend(t, validate)
}
func TestServerProxyNormalClose(t *testing.T) {
validate := func(frontendConn, agentConn *agentmock.MockAgentService_ConnectServer) {
// receive DIAL_REQ from frontend and proxy to backend
dialReq := &client.Packet{
Type: client.PacketType_DIAL_REQ,
Payload: &client.Packet_DialRequest{
DialRequest: &client.DialRequest{
Protocol: "tcp",
Address: "127.0.0.1:8080",
Random: 111,
},
},
}
// recevie CLOSE_REQ from frontend and proxy to backend
closeReq := &client.Packet{
Type: client.PacketType_CLOSE_REQ,
Payload: &client.Packet_CloseRequest{
CloseRequest: &client.CloseRequest{
ConnectID: 1,
}},
}
// This extra close is unwanted and should be removed; see
// https://github.com/kubernetes-sigs/apiserver-network-proxy/pull/307
extraCloseReq := &client.Packet{
Type: client.PacketType_CLOSE_REQ,
Payload: &client.Packet_CloseRequest{
CloseRequest: &client.CloseRequest{}},
}
gomock.InOrder(
frontendConn.EXPECT().Recv().Return(dialReq, nil).Times(1),
frontendConn.EXPECT().Recv().Return(closeReq, nil).Times(1),
frontendConn.EXPECT().Recv().Return(nil, io.EOF).Times(1),
)
gomock.InOrder(
agentConn.EXPECT().Send(dialReq).Return(nil).Times(1),
agentConn.EXPECT().Send(closeReq).Return(nil).Times(1),
agentConn.EXPECT().Send(extraCloseReq).Return(nil).Times(1),
)
}
baseServerProxyTestWithBackend(t, validate)
}