-
Notifications
You must be signed in to change notification settings - Fork 51
/
datapath.go
374 lines (303 loc) · 11.2 KB
/
datapath.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
package nfqdatapath
// Go libraries
import (
"context"
"fmt"
"os/exec"
"time"
"go.uber.org/zap"
"github.com/aporeto-inc/netlink-go/conntrack"
"github.com/aporeto-inc/trireme-lib/collector"
"github.com/aporeto-inc/trireme-lib/common"
"github.com/aporeto-inc/trireme-lib/controller/constants"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer/constants"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer/nfqdatapath/nflog"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer/nfqdatapath/tokenaccessor"
"github.com/aporeto-inc/trireme-lib/controller/internal/portset"
"github.com/aporeto-inc/trireme-lib/controller/pkg/connection"
"github.com/aporeto-inc/trireme-lib/controller/pkg/fqconfig"
"github.com/aporeto-inc/trireme-lib/controller/pkg/packet"
"github.com/aporeto-inc/trireme-lib/controller/pkg/packetprocessor"
"github.com/aporeto-inc/trireme-lib/controller/pkg/pucontext"
"github.com/aporeto-inc/trireme-lib/controller/pkg/secrets"
"github.com/aporeto-inc/trireme-lib/policy"
"github.com/aporeto-inc/trireme-lib/utils/cache"
"github.com/aporeto-inc/trireme-lib/utils/portcache"
"github.com/aporeto-inc/trireme-lib/utils/portspec"
)
// DefaultExternalIPTimeout is the default used for the cache for External IPTimeout.
const DefaultExternalIPTimeout = "500ms"
// Datapath is the structure holding all information about a connection filter
type Datapath struct {
// Configuration parameters
filterQueue *fqconfig.FilterQueue
collector collector.EventCollector
tokenAccessor tokenaccessor.TokenAccessor
service packetprocessor.PacketProcessor
secrets secrets.Secrets
nflogger nflog.NFLogger
procMountPoint string
// Internal structures and caches
// Key=ContextId Value=puContext
puFromContextID cache.DataStore
puFromMark cache.DataStore
contextIDFromPort *portcache.PortCache
// For remotes this is a reverse link to the context
puFromIP *pucontext.PUContext
// Hash based on source IP/Port to capture SynAck packets with possible NAT.
// When a new connection is created, we has the source IP/port. A return
// poacket might come with a different source IP NAT is done later.
// If we don't receife a return SynAck in 20 seconds, it expires
sourcePortConnectionCache cache.DataStore
// Hash on full five-tuple and return the connection
// These are auto-expired connections after 60 seconds of inactivity.
appOrigConnectionTracker cache.DataStore
appReplyConnectionTracker cache.DataStore
netOrigConnectionTracker cache.DataStore
netReplyConnectionTracker cache.DataStore
unknownSynConnectionTracker cache.DataStore
// CacheTimeout used for Trireme auto-detecion
ExternalIPCacheTimeout time.Duration
// connctrack handle
conntrackHdl conntrack.Conntrack
// mode captures the mode of the enforcer
mode constants.ModeType
// ack size
ackSize uint32
mutualAuthorization bool
packetLogs bool
portSetInstance portset.PortSet
}
// New will create a new data path structure. It instantiates the data stores
// needed to track sessions. The data path is started with a different call.
// Only required parameters must be provided. Rest a pre-populated with defaults.
func New(
mutualAuth bool,
filterQueue *fqconfig.FilterQueue,
collector collector.EventCollector,
serverID string,
validity time.Duration,
service packetprocessor.PacketProcessor,
secrets secrets.Secrets,
mode constants.ModeType,
procMountPoint string,
ExternalIPCacheTimeout time.Duration,
packetLogs bool,
tokenaccessor tokenaccessor.TokenAccessor,
puFromContextID cache.DataStore,
) *Datapath {
if ExternalIPCacheTimeout <= 0 {
var err error
ExternalIPCacheTimeout, err = time.ParseDuration(enforcerconstants.DefaultExternalIPTimeout)
if err != nil {
ExternalIPCacheTimeout = time.Second
}
}
if mode == constants.RemoteContainer || mode == constants.LocalServer {
// Make conntrack liberal for TCP
sysctlCmd, err := exec.LookPath("sysctl")
if err != nil {
zap.L().Fatal("sysctl command must be installed", zap.Error(err))
}
cmd := exec.Command(sysctlCmd, "-w", "net.netfilter.nf_conntrack_tcp_be_liberal=1")
if err := cmd.Run(); err != nil {
zap.L().Fatal("Failed to set conntrack options", zap.Error(err))
}
}
// This cache is shared with portSetInstance. The portSetInstance
// cleans up the entry corresponding to port when port is no longer
// part of ipset portset.
contextIDFromPort := portcache.NewPortCache("contextIDFromPort")
var portSetInstance portset.PortSet
if mode != constants.RemoteContainer {
portSetInstance = portset.New(contextIDFromPort)
}
d := &Datapath{
puFromMark: cache.NewCache("puFromMark"),
contextIDFromPort: contextIDFromPort,
puFromContextID: puFromContextID,
sourcePortConnectionCache: cache.NewCacheWithExpiration("sourcePortConnectionCache", time.Second*24),
appOrigConnectionTracker: cache.NewCacheWithExpiration("appOrigConnectionTracker", time.Second*24),
appReplyConnectionTracker: cache.NewCacheWithExpiration("appReplyConnectionTracker", time.Second*24),
netOrigConnectionTracker: cache.NewCacheWithExpiration("netOrigConnectionTracker", time.Second*24),
netReplyConnectionTracker: cache.NewCacheWithExpiration("netReplyConnectionTracker", time.Second*24),
unknownSynConnectionTracker: cache.NewCacheWithExpiration("unknownSynConnectionTracker", time.Second*2),
ExternalIPCacheTimeout: ExternalIPCacheTimeout,
filterQueue: filterQueue,
mutualAuthorization: mutualAuth,
service: service,
collector: collector,
tokenAccessor: tokenaccessor,
secrets: secrets,
ackSize: secrets.AckSize(),
mode: mode,
procMountPoint: procMountPoint,
conntrackHdl: conntrack.NewHandle(),
portSetInstance: portSetInstance,
packetLogs: packetLogs,
}
packet.PacketLogLevel = packetLogs
d.nflogger = nflog.NewNFLogger(11, 10, d.puInfoDelegate, collector)
return d
}
// NewWithDefaults create a new data path with most things used by default
func NewWithDefaults(
serverID string,
collector collector.EventCollector,
service packetprocessor.PacketProcessor,
secrets secrets.Secrets,
mode constants.ModeType,
procMountPoint string,
) *Datapath {
if collector == nil {
zap.L().Fatal("Collector must be given to NewDefaultDatapathEnforcer")
}
defaultMutualAuthorization := false
defaultFQConfig := fqconfig.NewFilterQueueWithDefaults()
defaultValidity := time.Hour * 8760
defaultExternalIPCacheTimeout, err := time.ParseDuration(enforcerconstants.DefaultExternalIPTimeout)
if err != nil {
defaultExternalIPCacheTimeout = time.Second
}
defaultPacketLogs := false
tokenaccessor, err := tokenaccessor.New(serverID, defaultValidity, secrets)
if err != nil {
zap.L().Fatal("Cannot create a token engine")
}
puFromContextID := cache.NewCache("puFromContextID")
return New(
defaultMutualAuthorization,
defaultFQConfig,
collector,
serverID,
defaultValidity,
service,
secrets,
mode,
procMountPoint,
defaultExternalIPCacheTimeout,
defaultPacketLogs,
tokenaccessor,
puFromContextID,
)
}
// Enforce implements the Enforce interface method and configures the data path for a new PU
func (d *Datapath) Enforce(contextID string, puInfo *policy.PUInfo) error {
zap.L().Debug("Called Proxy Enforce")
// Always create a new PU context
pu, err := pucontext.NewPU(contextID, puInfo, d.ExternalIPCacheTimeout)
if err != nil {
return fmt.Errorf("error creating new pu: %s", err)
}
// Cache PUs for retrieval based on packet information
if pu.Type() == common.LinuxProcessPU || pu.Type() == common.UIDLoginPU {
mark, ports := pu.GetProcessKeys()
d.puFromMark.AddOrUpdate(mark, pu)
for _, port := range ports {
portSpec, err := portspec.NewPortSpecFromString(port, contextID)
if err != nil {
continue
}
d.contextIDFromPort.AddPortSpec(portSpec)
}
} else {
d.puFromIP = pu
}
// Cache PU from contextID for management and policy updates
d.puFromContextID.AddOrUpdate(contextID, pu)
return nil
}
// Unenforce removes the configuration for the given PU
func (d *Datapath) Unenforce(contextID string) error {
puContext, err := d.puFromContextID.Get(contextID)
if err != nil {
return fmt.Errorf("contextid not found in enforcer: %s", err)
}
// Cleanup the IP based lookup
pu := puContext.(*pucontext.PUContext)
// Cleanup the mark information
if err := d.puFromMark.Remove(pu.Mark()); err != nil {
zap.L().Debug("Unable to remove cache entry during unenforcement",
zap.String("Mark", pu.Mark()),
zap.Error(err),
)
}
// Cleanup the port cache
for _, port := range pu.Ports() {
if err := d.contextIDFromPort.RemoveStringPorts(port); err != nil {
zap.L().Debug("Unable to remove cache entry during unenforcement",
zap.String("Port", port),
zap.Error(err),
)
}
}
// Cleanup the contextID cache
if err := d.puFromContextID.RemoveWithDelay(contextID, 10*time.Second); err != nil {
zap.L().Warn("Unable to remove context from cache",
zap.String("contextID", contextID),
zap.Error(err),
)
}
return nil
}
// GetFilterQueue returns the filter queues used by the data path
func (d *Datapath) GetFilterQueue() *fqconfig.FilterQueue {
return d.filterQueue
}
// GetPortSetInstance returns the portset instance used by data path
func (d *Datapath) GetPortSetInstance() portset.PortSet {
return d.portSetInstance
}
// Run starts the application and network interceptors
func (d *Datapath) Run(ctx context.Context) error {
zap.L().Debug("Start enforcer", zap.Int("mode", int(d.mode)))
if d.service != nil {
d.service.Initialize(d.secrets, d.filterQueue)
}
d.startApplicationInterceptor(ctx)
d.startNetworkInterceptor(ctx)
go d.nflogger.Run(ctx)
return nil
}
// UpdateSecrets updates the secrets used for signing communication between trireme instances
func (d *Datapath) UpdateSecrets(token secrets.Secrets) error {
d.secrets = token
return d.tokenAccessor.SetToken(d.tokenAccessor.GetTokenServerID(), d.tokenAccessor.GetTokenValidity(), token)
}
func (d *Datapath) puInfoDelegate(contextID string) (ID string, tags *policy.TagStore) {
item, err := d.puFromContextID.Get(contextID)
if err != nil {
return
}
ctx := item.(*pucontext.PUContext)
ID = ctx.ManagementID()
tags = ctx.Annotations().Copy()
return
}
func (d *Datapath) reportFlow(p *packet.Packet, connection *connection.TCPConnection, sourceID string, destID string, context *pucontext.PUContext, mode string, report *policy.FlowPolicy, packet *policy.FlowPolicy) {
c := &collector.FlowRecord{
ContextID: context.ID(),
Source: &collector.EndPoint{
ID: sourceID,
IP: p.SourceAddress.String(),
Port: p.SourcePort,
Type: collector.EnpointTypePU,
},
Destination: &collector.EndPoint{
ID: destID,
IP: p.DestinationAddress.String(),
Port: p.DestinationPort,
Type: collector.EnpointTypePU,
},
Tags: context.Annotations(),
Action: report.Action,
DropReason: mode,
PolicyID: report.PolicyID,
L4Protocol: p.IPProto,
}
if report.ObserveAction.Observed() {
c.ObservedAction = packet.Action
c.ObservedPolicyID = packet.PolicyID
}
d.collector.CollectFlowEvent(c)
}