-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtrireme.go
376 lines (300 loc) · 11 KB
/
trireme.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
package trireme
import (
"fmt"
"go.uber.org/zap"
"github.com/aporeto-inc/trireme-lib/cache"
"github.com/aporeto-inc/trireme-lib/collector"
"github.com/aporeto-inc/trireme-lib/constants"
"github.com/aporeto-inc/trireme-lib/enforcer"
"github.com/aporeto-inc/trireme-lib/enforcer/proxy"
"github.com/aporeto-inc/trireme-lib/monitor"
"github.com/aporeto-inc/trireme-lib/policy"
"github.com/aporeto-inc/trireme-lib/supervisor"
)
// trireme contains references to all the different components involved.
type trireme struct {
serverID string
cache cache.DataStore
supervisors map[constants.PUType]supervisor.Supervisor
enforcers map[constants.PUType]enforcer.PolicyEnforcer
resolver PolicyResolver
collector collector.EventCollector
}
// NewTrireme returns a reference to the trireme object based on the parameter subelements.
func NewTrireme(serverID string, resolver PolicyResolver, supervisors map[constants.PUType]supervisor.Supervisor, enforcers map[constants.PUType]enforcer.PolicyEnforcer, eventCollector collector.EventCollector) Trireme {
t := &trireme{
serverID: serverID,
cache: cache.NewCache("TriremeCache"),
supervisors: supervisors,
enforcers: enforcers,
resolver: resolver,
collector: eventCollector,
}
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)) // really? just a warn?
}
}
// Start all the enforcers
for _, e := range t.enforcers {
if err := e.Start(); err != nil {
return fmt.Errorf("Error while starting the enforcer: %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))
}
}
return nil
}
// HandlePUEvent implements the logic needed between all the Trireme components for
// explicitly adding a new PU.
func (t *trireme) HandlePUEvent(contextID string, event monitor.Event) error {
// Notify The PolicyResolver that an event occurred:
t.resolver.HandlePUEvent(contextID, event)
switch event {
case monitor.EventStart:
return t.doHandleCreate(contextID)
case monitor.EventStop:
return t.doHandleDelete(contextID)
default:
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
}
// SetPURuntime returns the RuntimeInfo based on the contextID.
func (t *trireme) SetPURuntime(contextID string, runtimeInfo *policy.PURuntime) error {
if _, err := t.cache.Get(contextID); err == nil {
return fmt.Errorf("PU Exists Already")
}
t.cache.AddOrUpdate(contextID, runtimeInfo)
return nil
}
// addTransmitterLabel adds the TransmitterLabel as a fixed label in the policy.
// The ManagementID part of the policy is used as the TransmitterLabel.
// If the Policy didn't set the ManagementID, we use the Local contextID as the
// default TransmitterLabel.
func addTransmitterLabel(contextID string, containerInfo *policy.PUInfo) {
if containerInfo.Policy.ManagementID() == "" {
containerInfo.Policy.AddIdentityTag(enforcer.TransmitterLabel, contextID)
} else {
containerInfo.Policy.AddIdentityTag(enforcer.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) doHandleCreate(contextID string) error {
// Retrieve the container runtime information from the cache
cachedElement, err := t.cache.Get(contextID)
if err != nil {
t.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: "N/A",
Tags: nil,
Event: collector.ContainerFailed,
})
return fmt.Errorf("Couldn't get the runtimeInfo from the cache %s", err)
}
runtimeInfo := cachedElement.(*policy.PURuntime)
runtimeInfo.GlobalLock.Lock()
defer runtimeInfo.GlobalLock.Unlock()
policyInfo, err := t.resolver.ResolvePolicy(contextID, runtimeInfo)
if err != nil || policyInfo == nil {
t.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: "N/A",
Tags: nil,
Event: collector.ContainerFailed,
})
return fmt.Errorf("Policy Error for this context: %s. Container killed. %s", contextID, err)
}
ip, _ := policyInfo.DefaultIPAddress()
containerInfo := policy.PUInfoFromPolicyAndRuntime(contextID, policyInfo, runtimeInfo)
addTransmitterLabel(contextID, containerInfo)
if !mustEnforce(contextID, containerInfo) {
t.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: ip,
Tags: policyInfo.Annotations(),
Event: collector.ContainerIgnored,
})
return nil
}
if err := t.enforcers[containerInfo.Runtime.PUType()].Enforce(contextID, containerInfo); err != nil {
t.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: ip,
Tags: policyInfo.Annotations(),
Event: collector.ContainerFailed,
})
return fmt.Errorf("Not able to setup enforcer: %s", err)
}
if err := t.supervisors[containerInfo.Runtime.PUType()].Supervise(contextID, containerInfo); err != nil {
if werr := t.enforcers[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.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: ip,
Tags: policyInfo.Annotations(),
Event: collector.ContainerFailed,
})
return fmt.Errorf("Not able to setup supervisor: %s", err)
}
t.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.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: "N/A",
Tags: nil,
Event: collector.UnknownContainerDelete,
})
return fmt.Errorf("Error getting Runtime out of cache for ContextID %s: %s", contextID, err)
}
runtime := runtimeReader.(*policy.PURuntime)
// Serialize operations
runtime.GlobalLock.Lock()
defer runtime.GlobalLock.Unlock()
ip, _ := runtime.DefaultIPAddress()
errS := t.supervisors[runtime.PUType()].Unsupervise(contextID)
errE := t.enforcers[runtime.PUType()].Unenforce(contextID)
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.collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: contextID,
IPAddress: ip,
Tags: nil,
Event: collector.ContainerDelete,
})
return fmt.Errorf("Delete Error for contextID %s. supervisor %s, enforcer %s", contextID, errS, errE)
}
t.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 because couldn't find runtime for contextID %s", 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[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[containerInfo.Runtime.PUType()].(type) {
case *enforcerproxy.ProxyInfo:
if lerr := t.enforcers[containerInfo.Runtime.PUType()].Unenforce(contextID); lerr != nil {
return err
}
if lerr := t.supervisors[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 PU policy: context=%s error=%s", contextID, err)
}
if err = t.supervisors[containerInfo.Runtime.PUType()].Supervise(contextID, containerInfo); err != nil {
if werr := t.enforcers[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 PU policy: context=%s error=%s", contextID, err)
}
ip, _ := newPolicy.DefaultIPAddress()
t.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[kind]; ok {
return s
}
return nil
}