-
Notifications
You must be signed in to change notification settings - Fork 51
/
processor.go
330 lines (269 loc) · 8.57 KB
/
processor.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
package uidmonitor
import (
"context"
"errors"
"regexp"
"strconv"
"strings"
"sync"
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/monitor/config"
"go.aporeto.io/trireme-lib/monitor/extractors"
"go.aporeto.io/trireme-lib/policy"
"go.aporeto.io/trireme-lib/utils/cache"
"go.aporeto.io/trireme-lib/utils/cgnetcls"
"go.uber.org/zap"
)
var ignoreNames = map[string]*struct{}{
"cgroup.clone_children": nil,
"cgroup.procs": nil,
"net_cls.classid": nil,
"net_prio.ifpriomap": nil,
"net_prio.prioidx": nil,
"notify_on_release": nil,
"tasks": nil,
}
// uidProcessor captures all the monitor processor information for a UIDLoginPU
// It implements the EventProcessor interface of the rpc monitor
type uidProcessor struct {
config *config.ProcessorConfig
metadataExtractor extractors.EventMetadataExtractor
netcls cgnetcls.Cgroupnetcls
regStart *regexp.Regexp
regStop *regexp.Regexp
putoPidMap *cache.Cache
pidToPU *cache.Cache
sync.Mutex
}
const (
triremeBaseCgroup = "/trireme"
)
// puToPidEntry represents an entry to puToPidMap
type puToPidEntry struct {
pidlist map[int32]bool
Info *policy.PURuntime
publishedContextID string
}
// Start handles start events
func (u *uidProcessor) Start(ctx context.Context, eventInfo *common.EventInfo) error {
return u.createAndStart(ctx, eventInfo, false)
}
// Stop handles a stop event and destroy as well. Destroy does nothing for the uid monitor
func (u *uidProcessor) Stop(ctx context.Context, eventInfo *common.EventInfo) error {
puID := eventInfo.PUID
if puID == triremeBaseCgroup {
u.netcls.Deletebasepath(puID)
return nil
}
u.Lock()
defer u.Unlock()
// Take the PID part of the user/pid PUID
var pid string
userID := eventInfo.PUID
parts := strings.SplitN(puID, "/", 2)
if len(parts) == 2 {
userID = parts[0]
pid = parts[1]
}
if len(pid) > 0 {
// Delete the cgroup for that pid
if err := u.netcls.DeleteCgroup(puID); err != nil {
return err
}
if pidlist, err := u.putoPidMap.Get(userID); err == nil {
pidCxt := pidlist.(*puToPidEntry)
iPid, err := strconv.Atoi(pid)
if err != nil {
return err
}
// Clean pid from both caches
delete(pidCxt.pidlist, int32(iPid))
if err = u.pidToPU.Remove(int32(iPid)); err != nil {
zap.L().Warn("Failed to remove entry in the cache", zap.Error(err), zap.String("stopped pid", pid))
}
}
return nil
}
runtime := policy.NewPURuntimeWithDefaults()
runtime.SetPUType(common.UIDLoginPU)
// Since all the PIDs of the user are gone, we can delete the user context.
if err := u.config.Policy.HandlePUEvent(ctx, userID, common.EventStop, runtime); err != nil {
zap.L().Warn("Failed to stop trireme PU ",
zap.String("puID", puID),
zap.Error(err),
)
}
if err := u.config.Policy.HandlePUEvent(ctx, userID, common.EventDestroy, runtime); err != nil {
zap.L().Warn("Failed to Destroy clean trireme ",
zap.String("puID", puID),
zap.Error(err),
)
}
if err := u.putoPidMap.Remove(userID); err != nil {
zap.L().Warn("Failed to remove entry in the cache", zap.Error(err), zap.String("puID", puID))
}
return u.netcls.DeleteCgroup(strings.TrimRight(userID, "/"))
}
// Create handles create events
func (u *uidProcessor) Create(ctx context.Context, eventInfo *common.EventInfo) error {
return nil
}
// Destroy handles a destroy event
func (u *uidProcessor) Destroy(ctx context.Context, eventInfo *common.EventInfo) error {
// Destroy is not used for the UIDMonitor since we will destroy when we get stop
// This is to try and save some time .Stop/Destroy is two RPC calls.
// We don't define pause on uid monitor so stop is always followed by destroy
return nil
}
// Pause handles a pause event
func (u *uidProcessor) Pause(ctx context.Context, eventInfo *common.EventInfo) error {
return u.config.Policy.HandlePUEvent(ctx, eventInfo.PUID, common.EventPause, nil)
}
// Resync resyncs with all the existing services that were there before we start
func (u *uidProcessor) Resync(ctx context.Context, e *common.EventInfo) error {
uids := u.netcls.ListAllCgroups("")
for _, uid := range uids {
if _, ok := ignoreNames[uid]; ok {
continue
}
processesOfUID := u.netcls.ListAllCgroups(uid)
activePids := []int32{}
for _, pid := range processesOfUID {
if _, ok := ignoreNames[pid]; ok {
continue
}
cgroupPath := uid + "/" + pid
pidlist, _ := u.netcls.ListCgroupProcesses(cgroupPath)
if len(pidlist) == 0 {
if err := u.netcls.DeleteCgroup(cgroupPath); err != nil {
zap.L().Warn("Unable to delete cgroup",
zap.String("cgroup", cgroupPath),
zap.Error(err),
)
}
continue
}
iPid, _ := strconv.Atoi(pid)
activePids = append(activePids, int32(iPid))
}
if len(activePids) == 0 {
if err := u.netcls.DeleteCgroup(uid); err != nil {
zap.L().Warn("Unable to delete cgroup",
zap.String("cgroup", uid),
zap.Error(err),
)
}
continue
}
event := &common.EventInfo{
PID: activePids[0],
PUID: uid,
PUType: common.UIDLoginPU,
}
if err := u.createAndStart(ctx, event, true); err != nil {
zap.L().Error("Can not synchronize user", zap.String("user", uid))
}
for i := 1; i < len(activePids); i++ {
event := &common.EventInfo{
PID: activePids[i],
PUID: uid,
PUType: common.UIDLoginPU,
}
if err := u.createAndStart(ctx, event, true); err != nil {
zap.L().Error("Can not synchronize user", zap.String("user", uid))
}
}
}
return nil
}
func (u *uidProcessor) createAndStart(ctx context.Context, eventInfo *common.EventInfo, startOnly bool) error {
u.Lock()
defer u.Unlock()
if eventInfo.Name == "" {
eventInfo.Name = eventInfo.PUID
}
puID := eventInfo.PUID
pids, err := u.putoPidMap.Get(puID)
var runtimeInfo *policy.PURuntime
if err != nil {
runtimeInfo, err = u.metadataExtractor(eventInfo)
if err != nil {
return err
}
publishedContextID := puID
// Setup the run time
if !startOnly {
if perr := u.config.Policy.HandlePUEvent(ctx, publishedContextID, common.EventCreate, runtimeInfo); perr != nil {
zap.L().Error("Failed to create process", zap.Error(perr))
return perr
}
}
if perr := u.config.Policy.HandlePUEvent(ctx, publishedContextID, common.EventStart, runtimeInfo); perr != nil {
zap.L().Error("Failed to start process", zap.Error(perr))
return perr
}
if err = u.processLinuxServiceStart(puID, eventInfo, runtimeInfo); err != nil {
zap.L().Error("processLinuxServiceStart", zap.Error(err))
return err
}
u.config.Collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: puID,
IPAddress: runtimeInfo.IPAddresses(),
Tags: runtimeInfo.Tags(),
Event: collector.ContainerStart,
})
entry := &puToPidEntry{
Info: runtimeInfo,
publishedContextID: publishedContextID,
pidlist: map[int32]bool{},
}
if err := u.putoPidMap.Add(puID, entry); err != nil {
zap.L().Warn("Failed to add puID/PU in the cache",
zap.Error(err),
zap.String("puID", puID),
)
}
pids = entry
}
pids.(*puToPidEntry).pidlist[eventInfo.PID] = true
if err := u.pidToPU.Add(eventInfo.PID, eventInfo.PUID); err != nil {
zap.L().Warn("Failed to add eventInfoPID/eventInfoPUID in the cache",
zap.Error(err),
zap.Int32("eventInfo.PID", eventInfo.PID),
zap.String("eventInfo.PUID", eventInfo.PUID),
)
}
pidPath := puID + "/" + strconv.Itoa(int(eventInfo.PID))
return u.processLinuxServiceStart(pidPath, eventInfo, pids.(*puToPidEntry).Info)
}
func (u *uidProcessor) processLinuxServiceStart(pidName string, event *common.EventInfo, runtimeInfo *policy.PURuntime) error {
if err := u.netcls.Creategroup(pidName); err != nil {
zap.L().Error("Failed to create cgroup for the user", zap.String("user", pidName), zap.Error(err))
return err
}
markval := runtimeInfo.Options().CgroupMark
if markval == "" {
if derr := u.netcls.DeleteCgroup(pidName); derr != nil {
zap.L().Warn("Failed to clean cgroup", zap.Error(derr))
}
return errors.New("mark value not found")
}
mark, err := strconv.ParseUint(markval, 10, 32)
if err != nil {
return err
}
if err = u.netcls.AssignMark(pidName, mark); err != nil {
if derr := u.netcls.DeleteCgroup(pidName); derr != nil {
zap.L().Warn("Failed to clean cgroup", zap.Error(derr))
}
return err
}
if err := u.netcls.AddProcess(pidName, int(event.PID)); err != nil {
if derr := u.netcls.DeleteCgroup(pidName); derr != nil {
zap.L().Warn("Failed to clean cgroup", zap.Error(derr))
}
return err
}
return nil
}