/
wrappers.go
580 lines (483 loc) · 17.2 KB
/
wrappers.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
package trireme
import (
"fmt"
"github.com/aporeto-inc/trireme-lib/internal/supervisor/proxy"
"github.com/aporeto-inc/trireme-lib/enforcer"
"github.com/aporeto-inc/trireme-lib/enforcer/utils/rpcwrapper"
"go.uber.org/zap"
"github.com/aporeto-inc/trireme-lib/collector"
"github.com/aporeto-inc/trireme-lib/constants"
"github.com/aporeto-inc/trireme-lib/enforcer/constants"
"github.com/aporeto-inc/trireme-lib/enforcer/policyenforcer"
"github.com/aporeto-inc/trireme-lib/enforcer/proxy"
"github.com/aporeto-inc/trireme-lib/enforcer/utils/secrets"
"github.com/aporeto-inc/trireme-lib/internal/monitor"
"github.com/aporeto-inc/trireme-lib/internal/supervisor"
"github.com/aporeto-inc/trireme-lib/policy"
"github.com/aporeto-inc/trireme-lib/rpc/events"
"github.com/aporeto-inc/trireme-lib/utils/allocator"
"github.com/aporeto-inc/trireme-lib/utils/cache"
)
// trireme contains references to all the different components involved.
type trireme struct {
config *config
cache cache.DataStore
supervisors map[constants.ModeType]supervisor.Supervisor
enforcers map[constants.ModeType]policyenforcer.Enforcer
puTypeToEnforcerType map[constants.PUType]constants.ModeType
port allocator.Allocator
rpchdl rpcwrapper.RPCClient
monitors monitor.Monitor
}
func (t *trireme) newEnforcers() error {
zap.L().Debug("LinuxProcessSupport", zap.Bool("Status", t.config.linuxProcess))
if t.config.linuxProcess {
t.enforcers[constants.LocalServer] = enforcer.New(
t.config.mutualAuth,
t.config.fq,
t.config.collector,
t.config.service,
t.config.secret,
t.config.serverID,
t.config.validity,
constants.LocalServer,
t.config.procMountPoint,
t.config.externalIPcacheTimeout,
t.config.packetLogs,
)
}
zap.L().Debug("TriremeMode", zap.Int("Status", int(t.config.mode)))
if t.config.mode == constants.RemoteContainer {
t.enforcers[constants.RemoteContainer] = enforcerproxy.NewProxyEnforcer(
t.config.mutualAuth,
t.config.fq,
t.config.collector,
t.config.service,
t.config.secret,
t.config.serverID,
t.config.validity,
t.rpchdl,
"enforce",
t.config.procMountPoint,
t.config.externalIPcacheTimeout,
t.config.packetLogs,
)
} else {
t.enforcers[constants.LocalContainer] = enforcer.New(
t.config.mutualAuth,
t.config.fq,
t.config.collector,
t.config.service,
t.config.secret,
t.config.serverID,
t.config.validity,
constants.LocalServer,
t.config.procMountPoint,
t.config.externalIPcacheTimeout,
t.config.packetLogs,
)
}
return nil
}
func (t *trireme) newSupervisors() error {
if t.config.linuxProcess {
sup, err := supervisor.NewSupervisor(
t.config.collector,
t.enforcers[constants.LocalServer],
constants.LocalServer,
constants.IPTables,
t.config.targetNetworks,
)
if err != nil {
return fmt.Errorf("Could Not create process supervisor :: received error %v", err)
}
t.supervisors[constants.LocalServer] = sup
}
if t.config.mode == constants.RemoteContainer {
s, err := supervisorproxy.NewProxySupervisor(
t.config.collector,
t.enforcers[constants.RemoteContainer],
t.rpchdl,
)
if err != nil {
zap.L().Error("Unable to create proxy Supervisor:: Returned Error ", zap.Error(err))
return nil
}
t.supervisors[constants.RemoteContainer] = s
} else {
if _, ok := t.supervisors[constants.LocalServer]; ok {
t.supervisors[constants.LocalContainer] = t.supervisors[constants.LocalServer]
} else {
sup, err := supervisor.NewSupervisor(
t.config.collector,
t.enforcers[constants.LocalContainer],
constants.LocalContainer,
constants.IPTables,
t.config.targetNetworks,
)
if err != nil {
return fmt.Errorf("Could Not create process supervisor :: received error %v", err)
}
t.supervisors[constants.LocalContainer] = sup
}
}
return nil
}
// newTrireme returns a reference to the trireme object based on the parameter subelements.
func newTrireme(c *config) Trireme {
var err error
t := &trireme{
config: c,
cache: cache.NewCache("TriremeCache"),
port: allocator.New(5000, 100),
rpchdl: rpcwrapper.NewRPCWrapper(),
enforcers: map[constants.ModeType]policyenforcer.Enforcer{},
supervisors: map[constants.ModeType]supervisor.Supervisor{},
puTypeToEnforcerType: map[constants.PUType]constants.ModeType{},
}
zap.L().Debug("Creating Enforcers")
if err = t.newEnforcers(); err != nil {
zap.L().Error("Unable to create datapath enforcers", zap.Error(err))
return nil
}
zap.L().Debug("Creating Supervisors")
if err = t.newSupervisors(); err != nil {
zap.L().Error("Unable to start datapath supervisor", zap.Error(err))
return nil
}
if c.linuxProcess {
t.puTypeToEnforcerType[constants.LinuxProcessPU] = constants.LocalServer
t.puTypeToEnforcerType[constants.UIDLoginPU] = constants.LocalServer
}
if t.config.mode == constants.RemoteContainer {
t.puTypeToEnforcerType[constants.ContainerPU] = constants.RemoteContainer
t.puTypeToEnforcerType[constants.KubernetesPU] = constants.RemoteContainer
} else {
t.puTypeToEnforcerType[constants.ContainerPU] = constants.LocalContainer
t.puTypeToEnforcerType[constants.KubernetesPU] = constants.LocalContainer
}
zap.L().Debug("Creating Monitors")
if t.monitors, err = monitor.NewMonitors(c.collector, t, c.monitors); err != nil {
zap.L().Error("Unable to start monitors", zap.Error(err))
return nil
}
return t
}
// Start starts the supervisor and the enforcer. It will also start to handling requests
// For new PU Creation and Policy Updates.
func (t *trireme) Start() error {
// Start all the supervisors.
for _, s := range t.supervisors {
if err := s.Start(); err != nil {
zap.L().Error("Error when starting the supervisor", zap.Error(err))
return fmt.Errorf("Error while starting supervisor %v", err)
}
}
// Start all the enforcers.
for _, e := range t.enforcers {
if err := e.Start(); err != nil {
return fmt.Errorf("unable to start the enforcer: %s", err)
}
}
// Start monitors.
if err := t.monitors.Start(); err != nil {
return fmt.Errorf("unable to start monitors: %s", err)
}
return nil
}
// Stop stops the supervisor and enforcer. It also stops handling new request
// for PU Creation/Update and Policy Updates
func (t *trireme) Stop() error {
for _, s := range t.supervisors {
if err := s.Stop(); err != nil {
zap.L().Error("Error when stopping the supervisor", zap.Error(err))
}
}
for _, e := range t.enforcers {
if err := e.Stop(); err != nil {
zap.L().Error("Error when stopping the enforcer", zap.Error(err))
}
}
if err := t.monitors.Stop(); err != nil {
zap.L().Error("Error when stopping the monitor", zap.Error(err))
}
return nil
}
// UpdatePolicy updates a policy for an already activated PU. The PU is identified by the contextID
func (t *trireme) UpdatePolicy(contextID string, newPolicy *policy.PUPolicy) error {
return t.doUpdatePolicy(contextID, newPolicy)
}
// PURuntime returns the RuntimeInfo based on the contextID.
func (t *trireme) PURuntime(contextID string) (policy.RuntimeReader, error) {
container, err := t.cache.Get(contextID)
if err != nil {
return nil, err
}
return container.(*policy.PURuntime), nil
}
// CreatePURuntime implements processor.ProcessingUnitsHandler
func (t *trireme) CreatePURuntime(contextID string, runtimeInfo *policy.PURuntime) error {
if _, err := t.cache.Get(contextID); err == nil {
return fmt.Errorf("pu %s already exists", contextID)
}
t.cache.AddOrUpdate(contextID, runtimeInfo)
return nil
}
// HandlePUEvent implements processor.ProcessingUnitsHandler
func (t *trireme) HandlePUEvent(contextID string, event events.Event) error {
// Notify The PolicyResolver that an event occurred:
t.config.resolver.HandlePUEvent(contextID, event)
switch event {
case events.EventStart:
return t.doHandleCreate(contextID)
case events.EventStop:
return t.doHandleDelete(contextID)
default:
return nil
}
}
// addTransmitterLabel adds the enforcerconstants.TransmitterLabel as a fixed label in the policy.
// The ManagementID part of the policy is used as the enforcerconstants.TransmitterLabel.
// If the Policy didn't set the ManagementID, we use the Local contextID as the
// default enforcerconstants.TransmitterLabel.
func addTransmitterLabel(contextID string, containerInfo *policy.PUInfo) {
if containerInfo.Policy.ManagementID() == "" {
containerInfo.Policy.AddIdentityTag(enforcerconstants.TransmitterLabel, contextID)
} else {
containerInfo.Policy.AddIdentityTag(enforcerconstants.TransmitterLabel, containerInfo.Policy.ManagementID())
}
}
// MustEnforce returns true if the Policy should go Through the Enforcer/Supervisor.
// Return false if:
// - PU is in host namespace.
// - Policy got the AllowAll tag.
func mustEnforce(contextID string, containerInfo *policy.PUInfo) bool {
if containerInfo.Policy.TriremeAction() == policy.AllowAll {
zap.L().Debug("PUPolicy with AllowAll Action. Not policing", zap.String("contextID", contextID))
return false
}
return true
}
func (t *trireme) mergeRuntimeAndPolicy(r *policy.PURuntime, p *policy.PUPolicy) {
if len(t.config.monitors.MergeTags) == 0 {
return
}
tags := r.Tags()
anno := p.Annotations()
if tags == nil || anno == nil {
return
}
for _, mt := range t.config.monitors.MergeTags {
if _, ok := tags.Get(mt); !ok {
if val, ok := anno.Get(mt); ok {
tags.AppendKeyValue(mt, val)
}
}
}
r.SetTags(tags)
}
func (t *trireme) doHandleCreate(contextID string) error {
// Retrieve the container runtime information from the cache
cachedElement, err := t.cache.Get(contextID)
if err != nil {
t.config.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: "N/A",
Tags: nil,
Event: collector.ContainerFailed,
})
return fmt.Errorf("unable get the runtime info from the cache: %s", err)
}
runtimeInfo := cachedElement.(*policy.PURuntime)
runtimeInfo.GlobalLock.Lock()
defer runtimeInfo.GlobalLock.Unlock()
policyInfo, err := t.config.resolver.ResolvePolicy(contextID, runtimeInfo)
if err != nil || policyInfo == nil {
t.config.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: "N/A",
Tags: nil,
Event: collector.ContainerFailed,
})
return fmt.Errorf("policy error for %s. container killed: %s", contextID, err)
}
t.mergeRuntimeAndPolicy(runtimeInfo, policyInfo)
ip, _ := policyInfo.DefaultIPAddress()
containerInfo := policy.PUInfoFromPolicyAndRuntime(contextID, policyInfo, runtimeInfo)
newOptions := containerInfo.Runtime.Options()
newOptions.ProxyPort = t.port.Allocate()
containerInfo.Runtime.SetOptions(newOptions)
addTransmitterLabel(contextID, containerInfo)
if !mustEnforce(contextID, containerInfo) {
t.config.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: ip,
Tags: policyInfo.Annotations(),
Event: collector.ContainerIgnored,
})
return nil
}
if err := t.enforcers[t.puTypeToEnforcerType[containerInfo.Runtime.PUType()]].Enforce(contextID, containerInfo); err != nil {
t.config.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: ip,
Tags: policyInfo.Annotations(),
Event: collector.ContainerFailed,
})
return fmt.Errorf("unable to setup enforcer: %s", err)
}
if err := t.supervisors[t.puTypeToEnforcerType[containerInfo.Runtime.PUType()]].Supervise(contextID, containerInfo); err != nil {
if werr := t.enforcers[t.puTypeToEnforcerType[containerInfo.Runtime.PUType()]].Unenforce(contextID); werr != nil {
zap.L().Warn("Failed to clean up state after failures",
zap.String("contextID", contextID),
zap.Error(werr),
)
}
t.config.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: ip,
Tags: policyInfo.Annotations(),
Event: collector.ContainerFailed,
})
return fmt.Errorf("unable to setup supervisor: %s", err)
}
t.config.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: ip,
Tags: containerInfo.Policy.Annotations(),
Event: collector.ContainerStart,
})
return nil
}
func (t *trireme) doHandleDelete(contextID string) error {
runtimeReader, err := t.PURuntime(contextID)
if err != nil {
t.config.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: "N/A",
Tags: nil,
Event: collector.ContainerDeleteUnknown,
})
return fmt.Errorf("unable to get runtime out of cache for context id %s: %s", contextID, err)
}
runtime := runtimeReader.(*policy.PURuntime)
// Serialize operations
runtime.GlobalLock.Lock()
defer runtime.GlobalLock.Unlock()
ip, _ := runtime.DefaultIPAddress()
errS := t.supervisors[t.puTypeToEnforcerType[runtime.PUType()]].Unsupervise(contextID)
errE := t.enforcers[t.puTypeToEnforcerType[runtime.PUType()]].Unenforce(contextID)
port := runtime.Options().ProxyPort
zap.L().Debug("Releasing Port", zap.String("Port", port))
t.port.Release(port)
if err := t.cache.Remove(contextID); err != nil {
zap.L().Warn("Failed to remove context from cache during cleanup. Entry doesn't exist",
zap.String("contextID", contextID),
zap.Error(err),
)
}
if errS != nil || errE != nil {
t.config.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: ip,
Tags: nil,
Event: collector.ContainerDelete,
})
return fmt.Errorf("unable to delete context id %s, supervisor %s, enforcer %s", contextID, errS, errE)
}
t.config.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: ip,
Tags: nil,
Event: collector.ContainerDelete,
})
return nil
}
func (t *trireme) doUpdatePolicy(contextID string, newPolicy *policy.PUPolicy) error {
runtimeReader, err := t.PURuntime(contextID)
if err != nil {
return fmt.Errorf("policy update failed: runtime for context id %s not found", contextID)
}
runtime := runtimeReader.(*policy.PURuntime)
// Serialize operations
runtime.GlobalLock.Lock()
defer runtime.GlobalLock.Unlock()
_, err = t.PURuntime(contextID)
if err != nil {
zap.L().Error("PU Already Deleted do nothing", zap.String("contextID", contextID))
return err
}
containerInfo := policy.PUInfoFromPolicyAndRuntime(contextID, newPolicy, runtime)
addTransmitterLabel(contextID, containerInfo)
if !mustEnforce(contextID, containerInfo) {
return nil
}
if err = t.enforcers[t.puTypeToEnforcerType[containerInfo.Runtime.PUType()]].Enforce(contextID, containerInfo); err != nil {
//We lost communication with the remote and killed it lets restart it here by feeding a create event in the request channel
zap.L().Warn("Re-initializing enforcers - connection lost")
if containerInfo.Runtime.PUType() == constants.ContainerPU {
//The unsupervise and unenforce functions just make changes to the proxy structures
//and do not depend on the remote instance running and can be called here
switch t.enforcers[t.puTypeToEnforcerType[containerInfo.Runtime.PUType()]].(type) {
case *enforcerproxy.ProxyInfo:
if lerr := t.enforcers[t.puTypeToEnforcerType[containerInfo.Runtime.PUType()]].Unenforce(contextID); lerr != nil {
return err
}
if lerr := t.supervisors[t.puTypeToEnforcerType[containerInfo.Runtime.PUType()]].Unsupervise(contextID); lerr != nil {
return err
}
if lerr := t.doHandleCreate(contextID); lerr != nil {
return err
}
default:
return err
}
return nil
}
return fmt.Errorf("enforcer failed to update policy for pu %s: %s", contextID, err)
}
if err = t.supervisors[t.puTypeToEnforcerType[containerInfo.Runtime.PUType()]].Supervise(contextID, containerInfo); err != nil {
if werr := t.enforcers[t.puTypeToEnforcerType[containerInfo.Runtime.PUType()]].Unenforce(contextID); werr != nil {
zap.L().Warn("Failed to clean up after enforcerments failures",
zap.String("contextID", contextID),
zap.Error(werr),
)
}
return fmt.Errorf("supervisor failed to update policy for pu %s: %s", contextID, err)
}
ip, _ := newPolicy.DefaultIPAddress()
t.config.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: ip,
Tags: containerInfo.Runtime.Tags(),
Event: collector.ContainerUpdate,
})
return nil
}
// Supervisor returns the Trireme supervisor for the given PU Type
func (t *trireme) Supervisor(kind constants.PUType) supervisor.Supervisor {
if s, ok := t.supervisors[t.puTypeToEnforcerType[kind]]; ok {
return s
}
return nil
}
func (t *trireme) UpdateSecrets(secrets secrets.Secrets) error {
for _, enforcer := range t.enforcers {
if err := enforcer.UpdateSecrets(secrets); err != nil {
zap.L().Error("unable to update secrets", zap.Error(err))
}
}
return nil
}
// Supervisors returns a slice of all initialized supervisors.
func Supervisors(t Trireme) []supervisor.Supervisor {
supervisors := []supervisor.Supervisor{}
// LinuxProcessPU, UIDLoginPU and HOstPU share the same supervisor so only one lookup suffices
if s := t.Supervisor(constants.LinuxProcessPU); s != nil {
supervisors = append(supervisors, s)
}
if s := t.Supervisor(constants.ContainerPU); s != nil {
supervisors = append(supervisors, s)
}
return supervisors
}