forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kafka.go
549 lines (471 loc) · 17.3 KB
/
kafka.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// Copyright 2017-2018 Authors of Cilium
//
// 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 proxy
import (
"encoding/json"
"fmt"
"github.com/cilium/cilium/pkg/revert"
"io"
"net"
"strconv"
"github.com/cilium/cilium/pkg/completion"
"github.com/cilium/cilium/pkg/datapath/linux/linux_defaults"
"github.com/cilium/cilium/pkg/flowdebug"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/kafka"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/policy"
"github.com/cilium/cilium/pkg/policy/api"
"github.com/cilium/cilium/pkg/proxy/accesslog"
"github.com/cilium/cilium/pkg/proxy/logger"
"github.com/optiopay/kafka/proto"
"github.com/sirupsen/logrus"
)
const (
fieldID = "id"
)
// The maps holding kafkaListeners (and kafkaRedirects), as well as
// the reference `count` field are protected by `mutex`. `socket` is safe
// to be used from multiple goroutines and the other fields below are
// immutable after initialization.
type kafkaListener struct {
socket *proxySocket
proxyPort uint16
endpointInfoRegistry logger.EndpointInfoRegistry
ingress bool
transparent bool
count int
}
var (
mutex lock.RWMutex // mutex protects accesses to the configuration resources.
kafkaListeners = make(map[uint16]*kafkaListener) // key: proxy port
kafkaRedirects = make(map[uint64]*kafkaRedirect) // key: dst port | dir << 16 | endpoint ID << 32
)
func mapKey(dstPort uint16, ingress bool, eID uint16) uint64 {
var dir uint64
if ingress {
dir = 1
}
return uint64(dstPort) | dir<<16 | uint64(eID)<<32
}
// kafkaRedirect implements the RedirectImplementation interface
// This extends the Redirect with Kafka specific state.
// 'listener' is shared across multiple kafkaRedirects
// 'redirect' is unique for this kafkaRedirect
type kafkaRedirect struct {
listener *kafkaListener
redirect *Redirect
endpointInfoRegistry logger.EndpointInfoRegistry
conf kafkaConfiguration
}
type srcIDLookupFunc func(mapname, remoteAddr, localAddr string, ingress bool) (uint32, error)
type kafkaConfiguration struct {
testMode bool
lookupSrcID srcIDLookupFunc
}
func (l *kafkaListener) Listen() {
for {
pair, err := l.socket.Accept(true)
select {
case <-l.socket.closing:
// Don't report errors while the socket is being closed
return
default:
}
if err != nil {
log.WithField(logfields.Port, l.proxyPort).WithError(err).Error("Unable to accept connection on port")
continue
}
// Locate the redirect for this connection
endpointIPStr, dstPortStr, err := net.SplitHostPort(pair.Rx.conn.LocalAddr().String())
if err != nil {
log.WithField(logfields.Port, l.proxyPort).WithError(err).Error("No destination address")
continue
}
if !l.ingress {
// for egress EP is the source
endpointIPStr, _, err = net.SplitHostPort(pair.Rx.conn.RemoteAddr().String())
if err != nil {
log.WithField(logfields.Port, l.proxyPort).WithError(err).Error("No source address")
continue
}
}
var epinfo accesslog.EndpointInfo
if !l.endpointInfoRegistry.FillEndpointIdentityByIP(net.ParseIP(endpointIPStr), &epinfo) {
log.WithField(logfields.Port, l.proxyPort).Errorf("Can't find endpoint with IP %s", endpointIPStr)
continue
}
portInt, _ := strconv.Atoi(dstPortStr)
key := mapKey(uint16(portInt), l.ingress, uint16(epinfo.ID))
log.WithField(logfields.EndpointID, epinfo.ID).Debugf("Looking up Kafka redirect with port: %d, ingress: %v", uint16(portInt), l.ingress)
mutex.Lock()
redir, ok := kafkaRedirects[key]
mutex.Unlock()
if ok && redir != nil {
go redir.handleRequestConnection(pair)
} else {
log.WithField(logfields.Port, l.proxyPort).Error("No redirect found for accepted connection")
}
}
}
// createKafkaRedirect creates a redirect to the kafka proxy. The redirect structure passed
// in is safe to access for reading and writing.
func createKafkaRedirect(r *Redirect, conf kafkaConfiguration, endpointInfoRegistry logger.EndpointInfoRegistry) (RedirectImplementation, error) {
redir := &kafkaRedirect{
redirect: r,
conf: conf,
endpointInfoRegistry: endpointInfoRegistry,
}
if redir.conf.lookupSrcID == nil {
redir.conf.lookupSrcID = lookupSrcID
}
// must register with the proxy port for unit tests (no IP_TRANSPARENT)
dstPort := r.dstPort
if conf.testMode {
dstPort = r.listener.proxyPort
}
key := mapKey(dstPort, r.listener.ingress, uint16(r.endpointID))
log.WithField(logfields.EndpointID, r.endpointID).Debugf(
"Registering %s with port: %d, ingress: %v",
r.listener.name, dstPort, r.listener.ingress)
mutex.Lock()
if _, ok := kafkaRedirects[key]; ok {
mutex.Unlock()
panic("Kafka redirect already exists for the given dst port and endpoint ID")
}
kafkaRedirects[key] = redir
// Start a listener if not already running
listener := kafkaListeners[r.listener.proxyPort]
if listener == nil {
marker := 0
if !conf.testMode {
marker = linux_defaults.GetMagicProxyMark(r.listener.ingress, 0)
}
// Listen needs to be in the synchronous part of this function to ensure that
// the proxy port is never refusing connections.
socket, err := listenSocket(fmt.Sprintf(":%d", r.listener.proxyPort), marker, !conf.testMode)
if err != nil {
delete(kafkaRedirects, key)
mutex.Unlock()
return nil, err
}
listener = &kafkaListener{
socket: socket,
proxyPort: r.listener.proxyPort,
endpointInfoRegistry: endpointInfoRegistry,
ingress: r.listener.ingress,
transparent: !conf.testMode,
count: 0,
}
go listener.Listen()
kafkaListeners[r.listener.proxyPort] = listener
}
listener.count++
redir.listener = listener
mutex.Unlock()
return redir, nil
}
// canAccess determines if the kafka message req sent by identity is allowed to
// be forwarded according to the rules configured on kafkaRedirect
func (k *kafkaRedirect) canAccess(req *kafka.RequestMessage, srcIdentity identity.NumericIdentity) bool {
scopedLog := log.WithFields(logrus.Fields{
logfields.Request: req.String(),
"NumericIdentity": srcIdentity,
})
k.redirect.mutex.RLock()
rules := k.redirect.rules.GetRelevantRulesForKafka(srcIdentity)
k.redirect.mutex.RUnlock()
if len(rules) == 0 {
flowdebug.Log(scopedLog, "No Kafka rules matching identity, rejecting")
return false
}
if flowdebug.Enabled() {
b, err := json.Marshal(rules)
if err != nil {
flowdebug.Log(scopedLog, "Error marshalling kafka rules to apply")
return false
} else {
flowdebug.Log(scopedLog.WithField("rule", string(b)), "Applying rule")
}
}
return req.MatchesRule(rules)
}
// kafkaLogRecord wraps an accesslog.LogRecord so that we can define methods with a receiver
type kafkaLogRecord struct {
*logger.LogRecord
localEndpoint logger.EndpointUpdater
topics []string
}
func apiKeyToString(apiKey int16) string {
if key, ok := api.KafkaReverseAPIKeyMap[apiKey]; ok {
return key
}
return fmt.Sprintf("%d", apiKey)
}
func (k *kafkaRedirect) newLogRecordFromRequest(req *kafka.RequestMessage) kafkaLogRecord {
return kafkaLogRecord{
LogRecord: logger.NewLogRecord(k.endpointInfoRegistry, k.redirect.localEndpoint,
accesslog.TypeRequest, k.redirect.listener.ingress,
logger.LogTags.Kafka(&accesslog.LogRecordKafka{
APIVersion: req.GetVersion(),
APIKey: apiKeyToString(req.GetAPIKey()),
CorrelationID: int32(req.GetCorrelationID()),
})),
localEndpoint: k.redirect.localEndpoint,
topics: req.GetTopics(),
}
}
func (k *kafkaRedirect) newLogRecordFromResponse(res *kafka.ResponseMessage, req *kafka.RequestMessage) kafkaLogRecord {
lr := kafkaLogRecord{
LogRecord: logger.NewLogRecord(k.endpointInfoRegistry, k.redirect.localEndpoint,
accesslog.TypeResponse, k.redirect.listener.ingress, logger.LogTags.Kafka(&accesslog.LogRecordKafka{})),
localEndpoint: k.redirect.localEndpoint,
}
if res != nil {
lr.Kafka.CorrelationID = int32(res.GetCorrelationID())
}
if req != nil {
lr.Kafka.APIVersion = req.GetVersion()
lr.Kafka.APIKey = apiKeyToString(req.GetAPIKey())
lr.topics = req.GetTopics()
}
return lr
}
// log Kafka log records
func (l *kafkaLogRecord) log(verdict accesslog.FlowVerdict, code int, info string) {
l.ApplyTags(logger.LogTags.Verdict(verdict, info))
l.Kafka.ErrorCode = code
// Log multiple entries for multiple Kafka topics in a single request.
for _, t := range l.topics {
l.Kafka.Topic.Topic = t
l.Log()
}
// Update stats for the endpoint.
// Count only one request.
ingress := l.ObservationPoint == accesslog.Ingress
var port uint16
if ingress {
port = l.DestinationEndpoint.Port
} else {
port = l.SourceEndpoint.Port
}
if port == 0 {
// Something went wrong when identifying the endpoints.
// Ignore in order to avoid polluting the stats.
return
}
request := l.Type == accesslog.TypeRequest
l.localEndpoint.UpdateProxyStatistics("TCP", port, ingress, request, l.Verdict)
}
func (k *kafkaRedirect) handleRequest(pair *connectionPair, req *kafka.RequestMessage, correlationCache *kafka.CorrelationCache,
remoteAddr net.Addr, remoteIdentity uint32, origDstAddr string) {
scopedLog := log.WithField(fieldID, pair.String())
flowdebug.Log(scopedLog.WithField(logfields.Request, req.String()), "Handling Kafka request")
record := k.newLogRecordFromRequest(req)
record.ApplyTags(logger.LogTags.Addressing(logger.AddressingInfo{
SrcIPPort: remoteAddr.String(),
DstIPPort: origDstAddr,
SrcIdentity: remoteIdentity,
}))
if !k.canAccess(req, identity.NumericIdentity(remoteIdentity)) {
flowdebug.Log(scopedLog, "Kafka request is denied by policy")
resp, err := req.CreateResponse(proto.ErrTopicAuthorizationFailed)
if err != nil {
record.log(accesslog.VerdictError,
kafka.ErrInvalidMessage, fmt.Sprintf("Unable to create response: %s", err))
scopedLog.WithError(err).Error("Unable to create Kafka response")
return
}
record.log(accesslog.VerdictDenied,
kafka.ErrTopicAuthorizationFailed, fmt.Sprint("Kafka request is denied by policy"))
pair.Rx.Enqueue(resp.GetRaw())
return
}
if pair.Tx.Closed() {
marker := 0
if !k.conf.testMode {
marker = linux_defaults.GetMagicProxyMark(k.redirect.listener.ingress, int(remoteIdentity))
}
flowdebug.Log(scopedLog.WithFields(logrus.Fields{
"marker": marker,
"destination": origDstAddr,
}), "Dialing original destination")
txConn, err := ciliumDialer(marker, remoteAddr.Network(), origDstAddr)
if err != nil {
scopedLog.WithError(err).WithFields(logrus.Fields{
"origNetwork": remoteAddr.Network(),
"origDest": origDstAddr,
}).Error("Unable to dial original destination")
record.log(accesslog.VerdictError,
kafka.ErrNetwork, fmt.Sprintf("Unable to dial original destination: %s", err))
return
}
pair.Tx.SetConnection(txConn)
// Start go routine to handle responses and pass in a copy of
// the request record as template for all responses
go k.handleResponseConnection(pair, correlationCache, remoteAddr, remoteIdentity, origDstAddr)
}
// The request is allowed so we will forward it:
// 1. Rewrite the correlation ID to a unique ID, it will be restored in
// the response direction
// 2. Store the request in the correlation cache
correlationCache.HandleRequest(req, nil)
flowdebug.Log(scopedLog, "Forwarding Kafka request")
// log valid request
record.log(accesslog.VerdictForwarded, kafka.ErrNone, "")
// Write the entire raw request onto the outgoing connection
pair.Tx.Enqueue(req.GetRaw())
}
type kafkaReqMessageHander func(pair *connectionPair, req *kafka.RequestMessage, correlationCache *kafka.CorrelationCache,
remoteAddr net.Addr, remoteIdentity uint32, origDstAddr string)
type kafkaRespMessageHander func(pair *connectionPair, req *kafka.ResponseMessage)
func (k *kafkaRedirect) handleRequests(done <-chan struct{}, pair *connectionPair, c *proxyConnection,
handler kafkaReqMessageHander) {
defer c.Close()
scopedLog := log.WithField(fieldID, pair.String())
remoteAddr := pair.Rx.conn.RemoteAddr()
if remoteAddr == nil {
scopedLog.Error("Kafka request connection has no remote address")
return
}
localAddr := pair.Rx.conn.LocalAddr()
if localAddr == nil {
scopedLog.Error("Kafka request connection has no local address")
return
}
// retrieve identity of source
k.redirect.localEndpoint.UnconditionalRLock()
mapname := k.redirect.localEndpoint.ConntrackName()
k.redirect.localEndpoint.RUnlock()
srcIdentity, err := k.conf.lookupSrcID(mapname, remoteAddr.String(), localAddr.String(), k.redirect.listener.ingress)
if err != nil {
scopedLog.WithField("source",
remoteAddr.String()).WithError(err).Error("Unable to lookup source security ID")
return
}
// create a correlation cache
correlationCache := kafka.NewCorrelationCache()
defer correlationCache.DeleteCache()
for {
req, err := kafka.ReadRequest(c.conn)
// Ignore any error if the listen socket has been closed, i.e. the
// port redirect has been removed.
select {
case <-done:
scopedLog.Debug("Redirect removed; closing Kafka request connection")
return
default:
}
if err != nil {
if err != io.ErrUnexpectedEOF && err != io.EOF {
scopedLog.WithError(err).Error("Unable to parse Kafka request; closing Kafka request connection")
}
return
}
origDstAddr := localAddr.String()
if k.conf.testMode {
origDstAddr = fmt.Sprintf("127.0.0.1:%d", k.redirect.dstPort)
}
scopedLog.Debugf("Forwarding request to %s", origDstAddr)
handler(pair, req, correlationCache, remoteAddr, srcIdentity, origDstAddr)
}
}
func (k *kafkaRedirect) handleResponses(done <-chan struct{}, pair *connectionPair, c *proxyConnection,
correlationCache *kafka.CorrelationCache, handler kafkaRespMessageHander,
remoteAddr net.Addr, remoteIdentity uint32, origDstAddr string) {
defer c.Close()
scopedLog := log.WithField(fieldID, pair.String())
for {
rsp, err := kafka.ReadResponse(c.conn)
// Ignore any error if the listen socket has been closed, i.e. the
// port redirect has been removed.
select {
case <-done:
scopedLog.Debug("Redirect removed; closing Kafka response connection")
return
default:
}
if err != nil {
record := k.newLogRecordFromResponse(nil, nil)
record.log(accesslog.VerdictError,
kafka.ErrInvalidMessage,
fmt.Sprintf("Unable to parse Kafka response: %s", err))
scopedLog.WithError(err).Error("Unable to parse Kafka response; closing Kafka response connection")
return
}
// 1. Find the request that correlates with this response based
// on the correlation ID
// 2. Restore the original correlation id that was overwritten
// by the proxy so the client is guaranteed to see the
// correlation id as expected
req := correlationCache.CorrelateResponse(rsp)
record := k.newLogRecordFromResponse(rsp, req)
record.ApplyTags(logger.LogTags.Addressing(logger.AddressingInfo{
SrcIPPort: remoteAddr.String(),
DstIPPort: origDstAddr,
SrcIdentity: remoteIdentity,
}))
record.log(accesslog.VerdictForwarded, kafka.ErrNone, "")
handler(pair, rsp)
}
}
func (k *kafkaRedirect) handleRequestConnection(pair *connectionPair) {
flowdebug.Log(log.WithFields(logrus.Fields{
"from": pair.Rx,
"to": pair.Tx,
}), "Proxying request Kafka connection")
k.handleRequests(k.listener.socket.closing, pair, pair.Rx, k.handleRequest)
}
func (k *kafkaRedirect) handleResponseConnection(pair *connectionPair, correlationCache *kafka.CorrelationCache,
remoteAddr net.Addr, remoteIdentity uint32, origDstAddr string) {
flowdebug.Log(log.WithFields(logrus.Fields{
"from": pair.Tx,
"to": pair.Rx,
}), "Proxying response Kafka connection")
k.handleResponses(k.listener.socket.closing, pair, pair.Tx, correlationCache,
func(pair *connectionPair, rsp *kafka.ResponseMessage) {
pair.Rx.Enqueue(rsp.GetRaw())
}, remoteAddr, remoteIdentity, origDstAddr)
}
// UpdateRules is a no-op for kafka redirects, as rules are read directly
// during request processing.
func (k *kafkaRedirect) UpdateRules(wg *completion.WaitGroup, l4 *policy.L4Filter) (revert.RevertFunc, error) {
return func() error { return nil }, nil
}
// Close the redirect.
func (k *kafkaRedirect) Close(wg *completion.WaitGroup) (revert.FinalizeFunc, revert.RevertFunc) {
return func() {
r := k.redirect
log.WithField(logfields.EndpointID, r.endpointID).Debugf("Un-Registering %s port: %d",
r.listener.name, r.dstPort)
key := mapKey(r.dstPort, r.listener.ingress, uint16(r.endpointID))
mutex.Lock()
delete(kafkaRedirects, key)
k.listener.count--
log.Debugf("Close: Listener count: %d", k.listener.count)
if k.listener.count == 0 {
k.listener.socket.Close()
delete(kafkaListeners, r.listener.proxyPort)
}
mutex.Unlock()
}, nil
}
func init() {
if err := proto.ConfigureParser(proto.ParserConfig{
SimplifiedMessageSetParsing: false,
}); err != nil {
log.WithError(err).Fatal("Unable to configure kafka parser")
}
}