-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathserver.go
514 lines (443 loc) · 14.9 KB
/
server.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
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"
"net"
"strconv"
"strings"
"sync"
"google.golang.org/grpc/metadata"
authv1 "k8s.io/api/authentication/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
"sigs.k8s.io/apiserver-network-proxy/proto/header"
)
// ProxyClientConnection...
type ProxyClientConnection struct {
Mode string
Grpc client.ProxyService_ProxyServer
HTTP net.Conn
connected chan struct{}
connectID int64
agentID string
}
func (c *ProxyClientConnection) send(pkt *client.Packet) error {
if c.Mode == "grpc" {
stream := c.Grpc
return stream.Send(pkt)
} else if c.Mode == "http-connect" {
if pkt.Type == client.PacketType_CLOSE_RSP {
return c.HTTP.Close()
} else if pkt.Type == client.PacketType_DATA {
_, err := c.HTTP.Write(pkt.GetData().Data)
return err
} else if pkt.Type == client.PacketType_DIAL_RSP {
return nil
} else {
return fmt.Errorf("attempt to send via unrecognized connection type %v", pkt.Type)
}
} else {
return fmt.Errorf("attempt to send via unrecognized connection mode %q", c.Mode)
}
}
func NewPendingDialManager() *PendingDialManager {
return &PendingDialManager{
pendingDial: make(map[int64]*ProxyClientConnection),
}
}
type PendingDialManager struct {
mu sync.RWMutex
pendingDial map[int64]*ProxyClientConnection
}
func (pm *PendingDialManager) Add(random int64, clientConn *ProxyClientConnection) {
pm.mu.Lock()
defer pm.mu.Unlock()
pm.pendingDial[random] = clientConn
}
func (pm *PendingDialManager) Get(random int64) (*ProxyClientConnection, bool) {
pm.mu.RLock()
defer pm.mu.RUnlock()
clientConn, ok := pm.pendingDial[random]
return clientConn, ok
}
func (pm *PendingDialManager) Remove(random int64) {
pm.mu.Lock()
defer pm.mu.Unlock()
delete(pm.pendingDial, random)
}
// ProxyServer
type ProxyServer struct {
// BackendManager manages the backends.
BackendManager BackendManager
// fmu protects frontends.
fmu sync.RWMutex
// conn = Frontend[agentID][connID]
frontends map[string]map[int64]*ProxyClientConnection
PendingDial *PendingDialManager
serverID string // unique ID of this server
serverCount int // Number of proxy server instances, should be 1 unless it is a HA server.
// agent authentication
AgentAuthenticationOptions *AgentTokenAuthenticationOptions
}
// AgentTokenAuthenticationOptions contains list of parameters required for agent token based authentication
type AgentTokenAuthenticationOptions struct {
Enabled bool
AgentNamespace string
AgentServiceAccount string
AuthenticationAudience string
KubernetesClient kubernetes.Interface
}
var _ agent.AgentServiceServer = &ProxyServer{}
var _ client.ProxyServiceServer = &ProxyServer{}
func (s *ProxyServer) addFrontend(agentID string, connID int64, p *ProxyClientConnection) {
klog.Infof("register frontend %v for agentID %s, connID %v", p, agentID, connID)
s.fmu.Lock()
defer s.fmu.Unlock()
if _, ok := s.frontends[agentID]; !ok {
s.frontends[agentID] = make(map[int64]*ProxyClientConnection)
}
s.frontends[agentID][connID] = p
}
func (s *ProxyServer) removeFrontend(agentID string, connID int64) {
s.fmu.Lock()
defer s.fmu.Unlock()
conns, ok := s.frontends[agentID]
if !ok {
klog.Warningf("can't find agentID %s in the frontends", agentID)
return
}
if _, ok := conns[connID]; !ok {
klog.Warningf("can't find connID %d in the frontends[%s]", connID, agentID)
return
}
klog.Infof("remove frontend %v for agentID %s, connID %v", conns[connID], agentID, connID)
delete(s.frontends[agentID], connID)
if len(s.frontends[agentID]) == 0 {
delete(s.frontends, agentID)
}
return
}
func (s *ProxyServer) getFrontend(agentID string, connID int64) (*ProxyClientConnection, error) {
s.fmu.RLock()
defer s.fmu.RUnlock()
conns, ok := s.frontends[agentID]
if !ok {
return nil, fmt.Errorf("can't find agentID %s in the frontends", agentID)
}
conn, ok := conns[connID]
if !ok {
return nil, fmt.Errorf("can't find connID %d in the frontends[%s]", connID, agentID)
}
return conn, nil
}
// NewProxyServer creates a new ProxyServer instance
func NewProxyServer(serverID string, serverCount int, agentAuthenticationOptions *AgentTokenAuthenticationOptions) *ProxyServer {
p := &ProxyServer{
frontends: make(map[string](map[int64]*ProxyClientConnection)),
PendingDial: NewPendingDialManager(),
serverID: serverID,
serverCount: serverCount,
BackendManager: NewDefaultBackendManager(),
AgentAuthenticationOptions: agentAuthenticationOptions,
}
return p
}
// Proxy handles incoming streams from gRPC frontend.
func (s *ProxyServer) Proxy(stream client.ProxyService_ProxyServer) error {
klog.Info("proxy request from client")
recvCh := make(chan *client.Packet, 10)
stopCh := make(chan error)
go s.serveRecvFrontend(stream, recvCh)
defer func() {
close(recvCh)
}()
// Start goroutine to receive packets from frontend and push to recvCh
go func() {
for {
in, err := stream.Recv()
if err == io.EOF {
close(stopCh)
return
}
if err != nil {
klog.Warningf(">>> Stream read from frontend error: %v", err)
close(stopCh)
return
}
recvCh <- in
}
}()
return <-stopCh
}
func (s *ProxyServer) serveRecvFrontend(stream client.ProxyService_ProxyServer, recvCh <-chan *client.Packet) {
klog.Info("start serving frontend stream")
var firstConnID int64
// The first packet should be a DIAL_REQ, we will randomly get a
// backend from the BackendManger then.
var backend Backend
var err error
for pkt := range recvCh {
switch pkt.Type {
case client.PacketType_DIAL_REQ:
klog.Info(">>> Received DIAL_REQ")
// TODO: if we track what agent has historically served
// the address, then we can send the Dial_REQ to the
// same agent. That way we save the agent from creating
// a new connection to the address.
backend, err = s.BackendManager.Backend()
if err != nil {
klog.Errorf(">>> failed to get a backend: %v", err)
continue
}
if err := backend.Send(pkt); err != nil {
klog.Warningf(">>> DIAL_REQ to Backend failed: %v", err)
}
s.PendingDial.Add(
pkt.GetDialRequest().Random,
&ProxyClientConnection{
Mode: "grpc",
Grpc: stream,
connected: make(chan struct{}),
})
klog.Info(">>> DIAL_REQ sent to backend") // got this. but backend didn't receive anything.
case client.PacketType_CLOSE_REQ:
connID := pkt.GetCloseRequest().ConnectID
klog.Infof(">>> Received CLOSE_REQ(id=%d)", connID)
if backend == nil {
klog.Errorf("backend has not been initialized for connID %d. Client should send a Dial Request first.", connID)
continue
}
if err := backend.Send(pkt); err != nil {
// TODO: retry with other backends connecting to this agent.
klog.Warningf(">>> CLOSE_REQ to Backend failed: %v", err)
}
klog.Info(">>> CLOSE_REQ sent to backend")
case client.PacketType_DATA:
connID := pkt.GetData().ConnectID
data := pkt.GetData().Data
klog.Infof(">>> Received %d bytes of DATA(id=%d)", len(data), connID)
if firstConnID == 0 {
firstConnID = connID
} else if firstConnID != connID {
klog.Warningf(">>> Data(id=%d) doesn't match first connection id %d", firstConnID, connID)
}
if backend == nil {
klog.Errorf("backend has not been initialized for connID %d. Client should send a Dial Request first.", connID)
continue
}
if err := backend.Send(pkt); err != nil {
// TODO: retry with other backends connecting to this agent.
klog.Warningf(">>> DATA to Backend failed: %v", err)
}
klog.Info(">>> DATA sent to backend")
default:
klog.Infof(">>> Ignore %v packet coming from frontend", pkt.Type)
}
}
klog.Infof(">>> Close streaming (id=%d)", firstConnID)
pkt := &client.Packet{
Type: client.PacketType_CLOSE_REQ,
Payload: &client.Packet_CloseRequest{
CloseRequest: &client.CloseRequest{
ConnectID: firstConnID,
},
},
}
if backend == nil {
klog.Errorf("backend has not been initialized for connID %d. Client should send a Dial Request first.", firstConnID)
return
}
if err := backend.Send(pkt); err != nil {
klog.Warningf(">>> CLOSE_REQ to Backend failed: %v", err)
}
}
func (s *ProxyServer) serveSend(stream client.ProxyService_ProxyServer, sendCh <-chan *client.Packet) {
klog.Info("start serve send ...")
for pkt := range sendCh {
err := stream.Send(pkt)
if err != nil {
klog.Warningf("stream write error: %v", err)
}
}
}
func agentID(stream agent.AgentService_ConnectServer) (string, error) {
md, ok := metadata.FromIncomingContext(stream.Context())
if !ok {
return "", fmt.Errorf("failed to get context")
}
agentIDs := md.Get(header.AgentID)
if len(agentIDs) != 1 {
return "", fmt.Errorf("expected one agent ID in the context, got %v", agentIDs)
}
return agentIDs[0], nil
}
func (s *ProxyServer) validateAuthToken(token string) error {
trReq := &authv1.TokenReview{
Spec: authv1.TokenReviewSpec{
Token: token,
Audiences: []string{s.AgentAuthenticationOptions.AuthenticationAudience},
},
}
r, err := s.AgentAuthenticationOptions.KubernetesClient.AuthenticationV1().TokenReviews().Create(trReq)
if err != nil {
return fmt.Errorf("Failed to authenticate request. err:%v", err)
}
if r.Status.Error != "" {
return fmt.Errorf("lookup failed: %s", r.Status.Error)
}
if !r.Status.Authenticated {
return fmt.Errorf("lookup failed: service account jwt not valid")
}
// The username is of format: system:serviceaccount:(NAMESPACE):(SERVICEACCOUNT)
parts := strings.Split(r.Status.User.Username, ":")
if len(parts) != 4 {
return fmt.Errorf("lookup failed: unexpected username format")
}
// Validate the user that comes back from token review is a service account
if parts[0] != "system" || parts[1] != "serviceaccount" {
return fmt.Errorf("lookup failed: username returned is not a service account")
}
ns := parts[2]
sa := parts[3]
if s.AgentAuthenticationOptions.AgentNamespace != ns {
return fmt.Errorf("lookup failed: incoming request from %q namespace. Expected %q", ns, s.AgentAuthenticationOptions.AgentNamespace)
}
if s.AgentAuthenticationOptions.AgentServiceAccount != sa {
return fmt.Errorf("lookup failed: incoming request from %q service account. Expected %q", sa, s.AgentAuthenticationOptions.AgentServiceAccount)
}
return nil
}
func (s *ProxyServer) authenticateAgentViaToken(ctx context.Context) error {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return fmt.Errorf("Failed to retrieve metadata from context")
}
authContext := md.Get(header.AuthenticationTokenContextKey)
if len(authContext) == 0 {
return fmt.Errorf("Authentication context was not found in metadata")
}
if len(authContext) > 1 {
return fmt.Errorf("too many (%d) tokens are received", len(authContext))
}
if !strings.HasPrefix(authContext[0], header.AuthenticationTokenContextSchemePrefix) {
return fmt.Errorf("received token does not have %q prefix", header.AuthenticationTokenContextSchemePrefix)
}
if err := s.validateAuthToken(strings.TrimPrefix(authContext[0], header.AuthenticationTokenContextSchemePrefix)); err != nil {
return fmt.Errorf("Failed to validate authentication token, err:%v", err)
}
klog.Infof("Client successfully authenticated via token")
return nil
}
// Connect is for agent to connect to ProxyServer as next hop
func (s *ProxyServer) Connect(stream agent.AgentService_ConnectServer) error {
agentID, err := agentID(stream)
if err != nil {
return err
}
klog.Infof("Connect request from agent %s", agentID)
s.BackendManager.AddBackend(agentID, stream)
defer s.BackendManager.RemoveBackend(agentID, stream)
h := metadata.Pairs(header.ServerID, s.serverID, header.ServerCount, strconv.Itoa(s.serverCount))
if err := stream.SendHeader(h); err != nil {
return err
}
recvCh := make(chan *client.Packet, 10)
stopCh := make(chan error)
if s.AgentAuthenticationOptions.Enabled {
if err := s.authenticateAgentViaToken(stream.Context()); err != nil {
klog.Infof("Client authentication failed. err:%v", err)
return err
}
}
go s.serveRecvBackend(stream, agentID, recvCh)
defer func() {
close(recvCh)
}()
go func() {
for {
in, err := stream.Recv()
if err == io.EOF {
close(stopCh)
return
}
if err != nil {
klog.Warningf("stream read error: %v", err)
close(stopCh)
return
}
recvCh <- in
}
}()
return <-stopCh
}
// route the packet back to the correct client
func (s *ProxyServer) serveRecvBackend(stream agent.AgentService_ConnectServer, agentID string, recvCh <-chan *client.Packet) {
for pkt := range recvCh {
switch pkt.Type {
case client.PacketType_DIAL_RSP:
resp := pkt.GetDialResponse()
klog.Infof("<<< Received DIAL_RSP(rand=%d), agentID %s, connID %d)", resp.Random, agentID, resp.ConnectID)
if client, ok := s.PendingDial.Get(resp.Random); !ok {
klog.Warning("<<< DialResp not recognized; dropped")
} else {
err := client.send(pkt)
s.PendingDial.Remove(resp.Random)
if err != nil {
klog.Warningf("<<< DIAL_RSP send to client stream error: %v", err)
} else {
client.connectID = resp.ConnectID
client.agentID = agentID
s.addFrontend(agentID, resp.ConnectID, client)
close(client.connected)
}
}
case client.PacketType_DATA:
resp := pkt.GetData()
klog.Infof("<<< Received %d bytes of DATA from agentID %s, connID %d", len(resp.Data), agentID, resp.ConnectID)
client, err := s.getFrontend(agentID, resp.ConnectID)
if err != nil {
klog.Warning(err)
break
}
if err := client.send(pkt); err != nil {
klog.Warningf("<<< DATA send to client stream error: %v", err)
} else {
klog.V(6).Infof("<<< DATA sent to frontend")
}
case client.PacketType_CLOSE_RSP:
resp := pkt.GetCloseResponse()
klog.Infof("<<< Received CLOSE_RSP(id=%d)", resp.ConnectID)
client, err := s.getFrontend(agentID, resp.ConnectID)
if err != nil {
klog.Warning(err)
break
}
if err := client.send(pkt); err != nil {
// Normal when frontend closes it.
klog.Infof("<<< CLOSE_RSP send to client stream error: %v", err)
} else {
klog.Infof("<<< CLOSE_RSP sent to frontend")
}
s.removeFrontend(agentID, resp.ConnectID)
klog.Infof("<<< Close streaming (agentID=%s, connId=%d)", agentID, resp.ConnectID)
default:
klog.Warningf("<<< Unrecognized packet %+v", pkt)
}
}
klog.Infof("<<< Close backend %v of agent %s", stream, agentID)
}