-
Notifications
You must be signed in to change notification settings - Fork 51
/
tcp.go
586 lines (500 loc) · 18 KB
/
tcp.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package tcp
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
"sync"
"syscall"
"time"
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/applicationproxy/markedconn"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/applicationproxy/serviceregistry"
enforcerconstants "go.aporeto.io/trireme-lib/controller/internal/enforcer/constants"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/nfqdatapath/tokenaccessor"
"go.aporeto.io/trireme-lib/controller/pkg/connection"
"go.aporeto.io/trireme-lib/controller/pkg/packet"
"go.aporeto.io/trireme-lib/controller/pkg/pucontext"
"go.aporeto.io/trireme-lib/controller/pkg/secrets"
"go.aporeto.io/trireme-lib/policy"
"go.uber.org/zap"
)
const (
proxyMarkInt = 0x40 //Duplicated from supervisor/iptablesctrl refer to it
)
// Proxy maintains state for proxies connections from listen to backend.
type Proxy struct {
tokenaccessor tokenaccessor.TokenAccessor
collector collector.EventCollector
puContext string
registry *serviceregistry.Registry
certificate *tls.Certificate
ca *x509.CertPool
// List of local IP's
localIPs map[string]struct{}
sync.RWMutex
}
// proxyFlowProperties is a struct used to pass flow information up
type proxyFlowProperties struct {
SourceIP string
DestIP string
PolicyID string
ServiceID string
DestType collector.EndPointType
SourceType collector.EndPointType
SourcePort uint16
DestPort uint16
}
// NewTCPProxy creates a new instance of proxy reate a new instance of Proxy
func NewTCPProxy(
tp tokenaccessor.TokenAccessor,
c collector.EventCollector,
puContext string,
registry *serviceregistry.Registry,
certificate *tls.Certificate,
caPool *x509.CertPool,
) *Proxy {
localIPs := markedconn.GetInterfaces()
return &Proxy{
collector: c,
tokenaccessor: tp,
puContext: puContext,
registry: registry,
localIPs: localIPs,
certificate: certificate,
ca: caPool,
}
}
// RunNetworkServer implements enforcer.Enforcer interface
func (p *Proxy) RunNetworkServer(ctx context.Context, listener net.Listener, encrypted bool) error {
// Encryption is done transparently for TCP.
go p.serve(ctx, listener)
return nil
}
// UpdateSecrets updates the secrets of the connections.
func (p *Proxy) UpdateSecrets(cert *tls.Certificate, caPool *x509.CertPool, s secrets.Secrets, certPEM, keyPEM string) {
p.Lock()
defer p.Unlock()
p.certificate = cert
p.ca = caPool
}
func (p *Proxy) serve(ctx context.Context, listener net.Listener) {
for {
select {
case <-ctx.Done():
return
default:
conn, err := listener.Accept()
if err != nil {
return
}
go p.handle(ctx, conn)
}
}
}
// ShutDown shuts down the server.
func (p *Proxy) ShutDown() error {
return nil
}
// handle handles a connection
func (p *Proxy) handle(ctx context.Context, upConn net.Conn) {
defer upConn.Close() // nolint
ip, port := upConn.(*markedconn.ProxiedConnection).GetOriginalDestination()
downConn, err := p.downConnection(ctx, ip, port)
if err != nil {
flowproperties := &proxyFlowProperties{
DestIP: ip.String(),
DestPort: uint16(port),
SourceIP: upConn.RemoteAddr().(*net.TCPAddr).IP.String(),
DestType: collector.EndPointTypeExternalIP,
SourceType: collector.EnpointTypePU,
}
puContext, perr := p.puContextFromContextID(p.puContext)
if perr != nil {
zap.L().Error("Unable to find policy context for tcp connection",
zap.String("Context", p.puContext),
zap.Error(perr))
return
}
p.reportRejectedFlow(flowproperties, puContext.ManagementID(), "default", puContext, collector.UnableToDial, nil, nil)
return
}
defer downConn.Close() // nolint
// Now let us handle the state machine for the down connection
isEncrypted, err := p.CompleteEndPointAuthorization(ip, port, upConn, downConn)
if err != nil {
zap.L().Error("Error on Authorization", zap.Error(err))
return
}
if err := p.proxyData(ctx, isEncrypted, upConn, downConn); err != nil {
zap.L().Debug("Error will proxying data", zap.Error(err))
}
}
func (p *Proxy) startEncryptedClientDataPath(ctx context.Context, isEncrypted bool, downConn net.Conn, serverConn net.Conn) error {
if isEncrypted {
p.RLock()
ca := p.ca
p.RUnlock()
tlsConn := tls.Client(downConn, &tls.Config{
InsecureSkipVerify: true,
ClientCAs: ca,
})
defer tlsConn.Close() // nolint errcheck
downConn = tlsConn
}
// TLS will automatically start negotiation on write. Nothing to do for us.
p.copyData(ctx, serverConn, downConn)
return nil
}
func (p *Proxy) startEncryptedServerDataPath(ctx context.Context, isEncrypted bool, downConn net.Conn, serverConn net.Conn) error {
if isEncrypted {
p.RLock()
if p.certificate == nil {
zap.L().Error("Trying to encrypt without a certificate - value is nil - drop connection")
p.RUnlock()
return fmt.Errorf("Failed to start encryption")
}
certs := []tls.Certificate{*p.certificate}
p.RUnlock()
tlsConn := tls.Server(serverConn.(*markedconn.ProxiedConnection).GetTCPConnection(), &tls.Config{
Certificates: certs,
})
defer tlsConn.Close() // nolint errcheck
serverConn = tlsConn
}
// TLS will automatically start negotiation on write. Nothing to for us.
p.copyData(ctx, serverConn, downConn)
return nil
}
func (p *Proxy) copyData(ctx context.Context, source, dest net.Conn) {
var wg sync.WaitGroup
wg.Add(2)
go func() {
dataprocessor(ctx, source, dest)
wg.Done()
}()
go func() {
dataprocessor(ctx, dest, source)
wg.Done()
}()
wg.Wait()
}
type readwithContext func(p []byte) (n int, err error)
func (r readwithContext) Read(p []byte) (int, error) { return r(p) }
func dataprocessor(ctx context.Context, source, dest net.Conn) { // nolint
defer func() {
switch dest.(type) {
case *tls.Conn:
dest.(*tls.Conn).CloseWrite() // nolint errcheck
case *net.TCPConn:
dest.(*net.TCPConn).CloseWrite() // nolint errcheck
case *markedconn.ProxiedConnection:
dest.(*markedconn.ProxiedConnection).GetTCPConnection().CloseWrite() // nolint errcheck
}
}()
if _, err := io.Copy(dest, readwithContext(
func(p []byte) (int, error) {
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
return source.Read(p)
}
},
),
); err != nil { // nolint
logErr(err)
}
}
func (p *Proxy) proxyData(ctx context.Context, isEncrypted bool, upConn net.Conn, downConn net.Conn) error {
// If the destination is not a local IP, it means that we are processing a client connection.
if p.isLocal(upConn) {
return p.startEncryptedClientDataPath(ctx, isEncrypted, downConn, upConn)
}
return p.startEncryptedServerDataPath(ctx, isEncrypted, downConn, upConn)
}
func (p *Proxy) puContextFromContextID(puID string) (*pucontext.PUContext, error) {
sctx, err := p.registry.RetrieveServiceByID(puID)
if err != nil {
return nil, fmt.Errorf("Context not found %s", puID)
}
return sctx.PUContext, nil
}
// Initiate the downstream connection
func (p *Proxy) downConnection(ctx context.Context, ip net.IP, port int) (net.Conn, error) {
raddr := &net.TCPAddr{
IP: ip,
Port: port,
}
return markedconn.DialMarkedTCPWithContext(ctx, "tcp4", raddr, proxyMarkInt)
}
// CompleteEndPointAuthorization -- Aporeto Handshake on top of a completed connection
// We will define states here equivalent to SYN_SENT AND SYN_RECEIVED
func (p *Proxy) CompleteEndPointAuthorization(downIP net.IP, downPort int, upConn, downConn net.Conn) (bool, error) {
// If the backend is not a local IP it means that we are a client.
if p.isLocal(upConn) {
return p.StartClientAuthStateMachine(downIP, downPort, downConn)
}
isEncrypted, err := p.StartServerAuthStateMachine(downIP, downPort, upConn)
if err != nil {
return false, err
}
return isEncrypted, nil
}
//StartClientAuthStateMachine -- Starts the aporeto handshake for client application
func (p *Proxy) StartClientAuthStateMachine(downIP net.IP, downPort int, downConn net.Conn) (bool, error) {
// We are running on top of TCP nothing should be lost or come out of order makes the state machines easy....
puContext, err := p.puContextFromContextID(p.puContext)
if err != nil {
return false, fmt.Errorf("Cannot find policy context: %s", err)
}
isEncrypted := false
conn := connection.NewProxyConnection()
flowproperties := &proxyFlowProperties{
DestIP: downIP.String(),
DestPort: uint16(downPort),
SourceIP: downConn.LocalAddr().(*net.TCPAddr).IP.String(),
DestType: collector.EndPointTypeExternalIP,
SourceType: collector.EnpointTypePU,
}
defer downConn.SetDeadline(time.Time{}) // nolint errcheck
// First validate that L3 policies do not require a reject.
networkReport, networkPolicy, noNetAccessPolicy := puContext.ApplicationACLPolicyFromAddr(downIP.To4(), uint16(downPort))
if noNetAccessPolicy == nil && networkPolicy.Action.Rejected() {
p.reportRejectedFlow(flowproperties, puContext.ManagementID(), networkPolicy.ServiceID, puContext, collector.PolicyDrop, networkReport, networkPolicy)
return false, fmt.Errorf("Unauthorized by Application ACLs")
}
for {
switch conn.GetState() {
case connection.ClientTokenSend:
if err := downConn.SetWriteDeadline(time.Now().Add(5 * time.Second)); err != nil {
return false, err
}
token, err := p.tokenaccessor.CreateSynPacketToken(puContext, &conn.Auth)
if err != nil {
return isEncrypted, fmt.Errorf("unable to create syn token: %s", err)
}
if n, err := writeMsg(downConn, token); err != nil || n < len(token) {
return isEncrypted, fmt.Errorf("unable to send auth token: %s", err)
}
conn.SetState(connection.ClientPeerTokenReceive)
case connection.ClientPeerTokenReceive:
if err := downConn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
return false, err
}
msg, err := readMsg(downConn)
if err != nil {
return false, fmt.Errorf("Failed to read peer token: %s", err)
}
claims, err := p.tokenaccessor.ParsePacketToken(&conn.Auth, msg)
if err != nil || claims == nil {
p.reportRejectedFlow(flowproperties, puContext.ManagementID(), collector.DefaultEndPoint, puContext, collector.InvalidToken, nil, nil)
return false, fmt.Errorf("peer token reject because of bad claims: error: %s, claims: %v %v", err, claims, string(msg))
}
report, packet := puContext.SearchTxtRules(claims.T, false)
if packet.Action.Rejected() {
p.reportRejectedFlow(flowproperties, puContext.ManagementID(), conn.Auth.RemoteContextID, puContext, collector.PolicyDrop, report, packet)
return isEncrypted, errors.New("dropping because of reject rule on transmitter")
}
if packet.Action.Encrypted() {
isEncrypted = true
}
conn.SetState(connection.ClientSendSignedPair)
case connection.ClientSendSignedPair:
if err := downConn.SetWriteDeadline(time.Now().Add(5 * time.Second)); err != nil {
return false, err
}
token, err := p.tokenaccessor.CreateAckPacketToken(puContext, &conn.Auth)
if err != nil {
return isEncrypted, fmt.Errorf("unable to create ack token: %s", err)
}
if n, err := writeMsg(downConn, token); err != nil || n < len(token) {
return isEncrypted, fmt.Errorf("unable to send ack: %s", err)
}
return isEncrypted, nil
}
}
}
// StartServerAuthStateMachine -- Start the aporeto handshake for a server application
func (p *Proxy) StartServerAuthStateMachine(ip net.IP, backendport int, upConn net.Conn) (bool, error) {
pctx, err := p.registry.RetrieveExposedServiceContext(ip.To4(), backendport, "")
if err != nil {
return false, fmt.Errorf("Service not found")
}
service := pctx.Service
puContext := pctx.PUContext
isEncrypted := false
flowProperties := &proxyFlowProperties{
DestIP: ip.String(),
DestPort: uint16(backendport),
SourceIP: getIP(upConn),
ServiceID: service.ID,
DestType: collector.EnpointTypePU,
SourceType: collector.EnpointTypePU,
}
conn := connection.NewProxyConnection()
conn.SetState(connection.ServerReceivePeerToken)
// First validate that L3 policies do not require a reject.
networkReport, networkPolicy, noNetAccessPolicy := puContext.NetworkACLPolicyFromAddr(upConn.RemoteAddr().(*net.TCPAddr).IP.To4(), uint16(backendport))
if noNetAccessPolicy == nil && networkPolicy.Action.Rejected() {
flowProperties.SourceType = collector.EndPointTypeExternalIP
p.reportRejectedFlow(flowProperties, networkPolicy.ServiceID, puContext.ManagementID(), puContext, collector.PolicyDrop, networkReport, networkPolicy)
return false, fmt.Errorf("Unauthorized by Network ACLs")
}
defer upConn.SetDeadline(time.Time{}) // nolint errcheck
for {
if err := upConn.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {
return false, err
}
switch conn.GetState() {
case connection.ServerReceivePeerToken:
if err := upConn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
return false, err
}
msg, err := readMsg(upConn)
if err != nil {
return false, fmt.Errorf("unable to receive syn token: %s", err)
}
claims, err := p.tokenaccessor.ParsePacketToken(&conn.Auth, msg)
if err != nil || claims == nil {
p.reportRejectedFlow(flowProperties, collector.DefaultEndPoint, puContext.ManagementID(), puContext, collector.InvalidToken, nil, nil)
return isEncrypted, fmt.Errorf("reported rejected flow due to invalid token: %s", err)
}
tags := claims.T.Copy()
tags.AppendKeyValue(enforcerconstants.PortNumberLabelString, strconv.Itoa(backendport))
report, packet := puContext.SearchRcvRules(tags)
if packet.Action.Rejected() {
p.reportRejectedFlow(flowProperties, conn.Auth.RemoteContextID, puContext.ManagementID(), puContext, collector.PolicyDrop, report, packet)
return isEncrypted, fmt.Errorf("connection dropped by policy %s: ", packet.PolicyID)
}
if packet.Action.Encrypted() {
isEncrypted = true
}
conn.ReportFlowPolicy = report
conn.PacketFlowPolicy = packet
conn.SetState(connection.ServerSendToken)
case connection.ServerSendToken:
if err := upConn.SetWriteDeadline(time.Now().Add(5 * time.Second)); err != nil {
return false, err
}
claims, err := p.tokenaccessor.CreateSynAckPacketToken(puContext, &conn.Auth)
if err != nil {
return isEncrypted, fmt.Errorf("unable to create synack token: %s", err)
}
if n, err := writeMsg(upConn, claims); err != nil || n < len(claims) {
zap.L().Error("Failed to write", zap.Error(err))
return false, fmt.Errorf("Failed to write ack: %s", err)
}
conn.SetState(connection.ServerAuthenticatePair)
case connection.ServerAuthenticatePair:
if err := upConn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
return false, err
}
msg, err := readMsg(upConn)
if err != nil {
return false, fmt.Errorf("unable to receive ack token: %s", err)
}
if _, err := p.tokenaccessor.ParseAckToken(&conn.Auth, msg); err != nil {
p.reportRejectedFlow(flowProperties, collector.DefaultEndPoint, puContext.ManagementID(), puContext, collector.InvalidFormat, nil, nil)
return isEncrypted, fmt.Errorf("ack packet dropped because signature validation failed %s", err)
}
p.reportAcceptedFlow(flowProperties, conn.Auth.RemoteContextID, puContext.ManagementID(), puContext, conn.ReportFlowPolicy, conn.PacketFlowPolicy)
return isEncrypted, nil
}
}
}
func (p *Proxy) reportFlow(flowproperties *proxyFlowProperties, sourceID string, destID string, context *pucontext.PUContext, mode string, report *policy.FlowPolicy, actual *policy.FlowPolicy) {
c := &collector.FlowRecord{
ContextID: context.ID(),
Source: &collector.EndPoint{
ID: sourceID,
IP: flowproperties.SourceIP,
Port: flowproperties.SourcePort,
Type: flowproperties.SourceType,
},
Destination: &collector.EndPoint{
ID: destID,
IP: flowproperties.DestIP,
Port: flowproperties.DestPort,
Type: flowproperties.DestType,
},
Tags: context.Annotations(),
Action: actual.Action,
DropReason: mode,
PolicyID: actual.PolicyID,
L4Protocol: packet.IPProtocolTCP,
ServiceType: policy.ServiceTCP,
ServiceID: flowproperties.ServiceID,
}
if report.ObserveAction.Observed() {
c.ObservedAction = report.Action
c.ObservedPolicyID = report.PolicyID
}
p.collector.CollectFlowEvent(c)
}
func (p *Proxy) reportAcceptedFlow(flowproperties *proxyFlowProperties, sourceID string, destID string, context *pucontext.PUContext, report *policy.FlowPolicy, packet *policy.FlowPolicy) {
p.reportFlow(flowproperties, sourceID, destID, context, "N/A", report, packet)
}
func (p *Proxy) reportRejectedFlow(flowproperties *proxyFlowProperties, sourceID string, destID string, context *pucontext.PUContext, mode string, report *policy.FlowPolicy, packet *policy.FlowPolicy) {
if report == nil {
report = &policy.FlowPolicy{
Action: policy.Reject,
PolicyID: "default",
}
}
if packet == nil {
packet = report
}
p.reportFlow(flowproperties, sourceID, destID, context, mode, report, packet)
}
func (p *Proxy) isLocal(conn net.Conn) bool {
addrPair := strings.SplitN(conn.RemoteAddr().String(), ":", 2)
if len(addrPair) != 2 {
return false
}
if _, ok := p.localIPs[addrPair[0]]; ok {
return true
}
return false
}
func readMsg(reader io.Reader) ([]byte, error) {
lread := io.LimitReader(reader, 2)
lbuf := make([]byte, 2)
if _, err := lread.Read(lbuf); err != nil {
return nil, fmt.Errorf("Invalid length: %s", err)
}
dataLength := binary.BigEndian.Uint16(lbuf)
dread := io.LimitReader(reader, int64(dataLength))
data := make([]byte, dataLength)
if _, err := dread.Read(data); err != nil {
return nil, fmt.Errorf("Not enough data to read: %s", err)
}
return data, nil
}
func writeMsg(conn io.Writer, data []byte) (n int, err error) {
lbuf := make([]byte, 2)
binary.BigEndian.PutUint16(lbuf, uint16(len(data)))
data = append(lbuf, data...)
return conn.Write(data)
}
func logErr(err error) bool {
switch err.(type) {
case syscall.Errno:
zap.L().Error("Connection error to destination", zap.Error(err))
default:
zap.L().Error("Connection terminated", zap.Error(err))
}
return false
}
func getIP(conn net.Conn) string {
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
return addr.IP.String()
}
return ""
}