-
Notifications
You must be signed in to change notification settings - Fork 51
/
supervisor.go
424 lines (358 loc) · 11.2 KB
/
supervisor.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
package supervisor
import (
"context"
"errors"
"fmt"
"sync"
"time"
"go.aporeto.io/enforcerd/trireme-lib/collector"
"go.aporeto.io/enforcerd/trireme-lib/common"
"go.aporeto.io/enforcerd/trireme-lib/controller/constants"
"go.aporeto.io/enforcerd/trireme-lib/controller/internal/enforcer"
"go.aporeto.io/enforcerd/trireme-lib/controller/internal/supervisor/iptablesctrl"
supervisornoop "go.aporeto.io/enforcerd/trireme-lib/controller/internal/supervisor/noop"
provider "go.aporeto.io/enforcerd/trireme-lib/controller/pkg/aclprovider"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/counters"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/fqconfig"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/ipsetmanager"
"go.aporeto.io/enforcerd/trireme-lib/controller/runtime"
"go.aporeto.io/enforcerd/trireme-lib/policy"
"go.aporeto.io/enforcerd/trireme-lib/utils/cache"
"go.uber.org/zap"
)
type cacheData struct {
version int
ips policy.ExtendedMap
mark string
tcpPorts string
udpPorts string
username string
containerInfo *policy.PUInfo
}
// Config is the structure holding all information about the supervisor
type Config struct {
// mode is LocalServer or RemoteContainer
mode constants.ModeType
// versionTracker tracks the current version of the ACLs
versionTracker cache.DataStore
// impl is the packet filter implementation
impl Implementor
// collector is the stats collector implementation
collector collector.EventCollector
// filterQueue is the filterqueue parameters
filterQueue fqconfig.FilterQueue
// cfg is the mutable configuration
cfg *runtime.Configuration
sync.Mutex
}
// NewSupervisor will create a new connection supervisor that uses IPTables
// to redirect specific packets to userspace. It instantiates multiple data stores
// to maintain efficient mappings between contextID, policy and IP addresses. This
// simplifies the lookup operations at the expense of memory.
func NewSupervisor(
collector collector.EventCollector,
enforcerInstance enforcer.Enforcer,
mode constants.ModeType,
cfg *runtime.Configuration,
ipv6Enabled bool,
iptablesLockfile string,
) (Supervisor, error) {
// for certain modes we do not want to launch a supervisor at all, so we are going to launch a noop supervisor
// like we do for the supervisor proxy
if mode == constants.RemoteContainerEnvoyAuthorizer {
return supervisornoop.NewNoopSupervisor(), nil
}
if collector == nil || enforcerInstance == nil {
return nil, errors.New("Invalid parameters")
}
filterQueue := enforcerInstance.GetFilterQueue()
if filterQueue == nil {
return nil, errors.New("enforcer filter queues cannot be nil")
}
bpf := enforcerInstance.GetBPFObject()
serviceMeshType := enforcerInstance.GetServiceMeshType()
impl, err := iptablesctrl.NewInstance(filterQueue, mode, ipv6Enabled, bpf, iptablesLockfile, serviceMeshType)
if err != nil {
return nil, fmt.Errorf("unable to initialize supervisor controllers: %s", err)
}
return &Config{
mode: mode,
impl: impl,
versionTracker: cache.NewCache("SupVersionTracker"),
collector: collector,
filterQueue: filterQueue,
cfg: cfg,
}, nil
}
// Run starts the supervisor
func (s *Config) Run(ctx context.Context) error {
s.Lock()
defer s.Unlock()
if err := s.impl.Run(ctx); err != nil {
return fmt.Errorf("unable to start the implementer: %s", err)
}
if err := s.impl.SetTargetNetworks(s.cfg); err != nil {
return err
}
if err := s.impl.CreateCustomRulesChain(); err != nil {
return err
}
return nil
}
// Supervise creates a mapping between an IP address and the corresponding labels.
// it invokes the various handlers that process the parameter policy.
func (s *Config) Supervise(contextID string, pu *policy.PUInfo) error {
if pu == nil || pu.Policy == nil || pu.Runtime == nil {
return errors.New("Invalid PU or policy info")
}
if _, err := s.versionTracker.Get(contextID); err != nil {
// ContextID is not found in Cache, New PU: Do create.
return s.doCreatePU(contextID, pu)
}
// Context already in the cache. Just run update
return s.doUpdatePU(contextID, pu)
}
// Unsupervise removes the mapping from cache and cleans up the iptable rules. ALL
// remove operations will print errors by they don't return error. We want to force
// as much cleanup as possible to avoid stale state
func (s *Config) Unsupervise(contextID string) error {
s.Lock()
defer s.Unlock()
data, err := s.versionTracker.Get(contextID)
if err != nil {
return fmt.Errorf("cannot find policy version: %s", err)
}
cfg := data.(*cacheData)
// TODO (varks): Similar to configureRules and UpdateRules, DeleteRules should take
// only contextID and *policy.PUInfo as function parameters.
if err := s.impl.DeleteRules(cfg.version, contextID, cfg.tcpPorts, cfg.udpPorts, cfg.mark, cfg.username, cfg.containerInfo); err != nil {
zap.L().Warn("Some rules were not deleted during unsupervise", zap.Error(err))
}
if err := s.versionTracker.Remove(contextID); err != nil {
zap.L().Warn("Failed to clean the rule version cache", zap.Error(err))
}
ipsetmanager.V4().RemoveExternalNets(contextID)
ipsetmanager.V6().RemoveExternalNets(contextID)
return nil
}
// CleanUp implements the cleanup interface
func (s *Config) CleanUp() error {
s.Lock()
defer s.Unlock()
return s.impl.CleanUp()
}
// SetTargetNetworks sets the target networks of the supervisor
func (s *Config) SetTargetNetworks(cfg *runtime.Configuration) error {
s.Lock()
defer s.Unlock()
s.cfg = cfg.DeepCopy()
return s.impl.SetTargetNetworks(cfg)
}
// ACLProvider returns the ACL provider used by the supervisor that can be
// shared with other entities.
func (s *Config) ACLProvider() []provider.IptablesProvider {
return s.impl.ACLProvider()
}
func (s *Config) doCreatePU(contextID string, pu *policy.PUInfo) error {
s.Lock()
tcpPorts, udpPorts := common.ConvertServicesToProtocolPortList(pu.Runtime.Options().Services)
c := &cacheData{
version: 0,
ips: pu.Policy.IPAddresses(),
mark: pu.Runtime.Options().CgroupMark,
tcpPorts: tcpPorts,
udpPorts: udpPorts,
username: pu.Runtime.Options().UserID,
containerInfo: pu,
}
// Version the policy so that we can do hitless policy changes
s.versionTracker.AddOrUpdate(contextID, c)
var iprules policy.IPRuleList
iprules = append(iprules, pu.Policy.ApplicationACLs()...)
iprules = append(iprules, pu.Policy.NetworkACLs()...)
if err := ipsetmanager.V4().RegisterExternalNets(contextID, iprules); err != nil {
s.Unlock()
zap.L().Error("Error creating ipsets for external networks", zap.Error(err))
return err
}
if err := ipsetmanager.V6().RegisterExternalNets(contextID, iprules); err != nil {
s.Unlock()
zap.L().Error("Error creating ipsets for external networks", zap.Error(err))
return err
}
// Configure the rules
if err := s.impl.ConfigureRules(c.version, contextID, pu); err != nil {
// Revert what you can since we have an error - it will fail most likely
zap.L().Error("ConfigureRules Failed with error", zap.Error(err))
s.Unlock()
s.Unsupervise(contextID) // nolint
return err
}
s.Unlock()
return nil
}
// UpdatePU creates a mapping between an IP address and the corresponding labels
//and the invokes the various handlers that process all policies.
func (s *Config) doUpdatePU(contextID string, pu *policy.PUInfo) error {
s.Lock()
data, err := s.versionTracker.Get(contextID)
if err != nil {
s.Unlock()
return fmt.Errorf("unable to find pu %s in cache: %s", contextID, err)
}
var iprules policy.IPRuleList
iprules = append(iprules, pu.Policy.ApplicationACLs()...)
iprules = append(iprules, pu.Policy.NetworkACLs()...)
if err := ipsetmanager.V4().RegisterExternalNets(contextID, iprules); err != nil {
s.Unlock()
zap.L().Error("Error creating ipsets for external networks", zap.Error(err))
return err
}
if err := ipsetmanager.V6().RegisterExternalNets(contextID, iprules); err != nil {
s.Unlock()
zap.L().Error("Error creating ipsets for external networks", zap.Error(err))
return err
}
c := data.(*cacheData)
if err := s.impl.UpdateRules(c.version^1, contextID, pu, c.containerInfo); err != nil {
zap.L().Error("Update rules failed with error...Reconfiguring the system", zap.Error(err))
counters.IncrementCounter(counters.ErrIPTablesReset)
s.Unlock()
s.Unsupervise(contextID) //nolint
s.CleanUp() //nolint
s.Run(context.Background()) //nolint
return s.Supervise(contextID, pu)
}
c.version ^= 1
// Updated the policy in the cached processing unit.
c.containerInfo.Policy = pu.Policy
ipsetmanager.V4().DestroyUnusedIPsets()
ipsetmanager.V6().DestroyUnusedIPsets()
s.Unlock()
return nil
}
// EnableIPTablesPacketTracing enables ip tables packet tracing
func (s *Config) EnableIPTablesPacketTracing(ctx context.Context, contextID string, interval time.Duration) error {
data, err := s.versionTracker.Get(contextID)
if err != nil {
return fmt.Errorf("cannot find policy version: %s", err)
}
cfg := data.(*cacheData)
iptablesRules := debugRules(cfg, s.mode)
ipts := s.impl.ACLProvider()
for _, ipt := range ipts {
for _, rule := range iptablesRules {
if err := ipt.Insert(rule[0], rule[1], 1, rule[2:]...); err != nil {
zap.L().Error("Unable to install rule", zap.Error(err))
}
}
// anonymous go func to flush debug iptables after interval
go func(ipt provider.IptablesProvider) {
for {
select {
case <-ctx.Done():
return
case <-time.After(interval):
for _, rule := range iptablesRules {
if err := ipt.Delete(rule[0], rule[1], rule[2:]...); err != nil {
zap.L().Debug("Unable to delete trace rules", zap.Error(err))
}
}
}
}
}(ipt)
}
return nil
}
func debugRules(data *cacheData, mode constants.ModeType) [][]string {
iptables := [][]string{}
if mode == constants.RemoteContainer {
iptables = append(iptables, [][]string{
{
"raw",
"PREROUTING",
"-j", "TRACE",
},
{
"raw",
"OUTPUT",
"-j", "TRACE",
},
}...)
} else {
if data.tcpPorts != "0" {
iptables = append(iptables,
[][]string{
{
"raw",
"PREROUTING",
"-p", "tcp",
"--match", "multiport",
"--destination-ports", data.tcpPorts,
"-j", "TRACE",
},
{
"raw",
"OUTPUT",
"--match", "multiport",
"--source-ports", data.tcpPorts,
"-j", "TRACE",
},
}...,
)
} else {
iptables = append(iptables, [][]string{
{
"raw",
"PREROUTING",
"-p", "tcp",
"-j", "TRACE",
},
{
"raw",
"OUTPUT",
"-m", "cgroup",
"--cgroup", data.mark,
"-j", "TRACE",
},
}...)
}
if data.udpPorts != "0" {
iptables = append(iptables, [][]string{
{
"raw",
"PREROUTING",
"-p", "udp",
"--match", "multiport",
"--destination-ports", data.udpPorts,
"-j", "TRACE",
},
{
"raw",
"OUTPUT",
"--match", "multiport",
"--source-ports", data.tcpPorts,
"-j", "TRACE",
},
}...)
} else {
iptables = append(iptables,
[][]string{
{
"raw",
"PREROUTING",
"-p", "tcp",
"-j", "TRACE",
},
{
"raw",
"OUTPUT",
"-m", "cgroup",
"--cgroup", data.mark,
"-j", "TRACE",
},
}...)
}
}
return iptables
}