-
Notifications
You must be signed in to change notification settings - Fork 29
/
monitor.go
355 lines (299 loc) · 10.5 KB
/
monitor.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
// Copyright 2023 vArmor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package runtime
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/containerd/containerd"
"github.com/containerd/containerd/api/events"
tasks "github.com/containerd/containerd/api/services/tasks/v1"
"github.com/containerd/typeurl/v2"
"github.com/go-logr/logr"
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
"google.golang.org/grpc"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
varmorutils "github.com/bytedance/vArmor/internal/utils"
varmortypes "github.com/bytedance/vArmor/pkg/types"
)
type RuntimeMonitor struct {
containerdClient *containerd.Client
runtimeClient runtimeapi.RuntimeServiceClient
runtimeConn *grpc.ClientConn
running bool
status error
taskCreateCh chan<- varmortypes.ContainerInfo
taskDeleteCh chan<- varmortypes.ContainerInfo
taskDeleteSyncCh chan<- bool
modellerChs map[string]chan<- uint32
log logr.Logger
}
func NewRuntimeMonitor(log logr.Logger) (*RuntimeMonitor, error) {
var err error
monitor := RuntimeMonitor{
modellerChs: make(map[string]chan<- uint32),
log: log,
}
monitor.containerdClient, err = newContainerdClient(varmortypes.RuntimeEndpoint, varmortypes.RuntimeTimeout)
if err != nil {
return nil, err
}
monitor.runtimeClient, monitor.runtimeConn, err = newRuntimeServiceClient(varmortypes.RuntimeEndpoint, varmortypes.RuntimeTimeout)
if err != nil {
return nil, err
}
return &monitor, nil
}
func (monitor *RuntimeMonitor) Close() {
monitor.log.Info("close the connection between the containerd and agent")
monitor.running = false
monitor.containerdClient.Close()
monitor.runtimeConn.Close()
}
func (monitor *RuntimeMonitor) SetTaskNotifyChs(
createCh chan varmortypes.ContainerInfo,
deleteCh chan varmortypes.ContainerInfo,
deleteSynCh chan bool) {
monitor.taskCreateCh = createCh
monitor.taskDeleteCh = deleteCh
monitor.taskDeleteSyncCh = deleteSynCh
}
func (monitor *RuntimeMonitor) AddModellerChs(profileName string, ch chan uint32) {
monitor.modellerChs[profileName] = ch
}
func (monitor *RuntimeMonitor) DeleteModellerChs(profileName string) {
delete(monitor.modellerChs, profileName)
}
func (monitor *RuntimeMonitor) retrieveContainerInfo(containerInfo *varmortypes.ContainerInfo) error {
ctx, cancel := appContext(context.Background(), varmortypes.K8sCriNamespace, varmortypes.RuntimeTimeout)
defer cancel()
container, err := monitor.containerdClient.LoadContainer(ctx, containerInfo.ContainerID)
if err != nil {
return err
}
info, err := container.Info(ctx)
if err != nil {
return err
} else if info.Runtime.Name != "io.containerd.runc.v2" {
return fmt.Errorf("unsupported runtime type: %s", info.Runtime.Name)
}
var spec runtimespec.Spec
if info.Spec != nil {
err = json.Unmarshal(info.Spec.GetValue(), &spec)
if err != nil {
return err
}
}
if containerType, ok := spec.Annotations["io.kubernetes.cri.container-type"]; ok {
if containerType == "sandbox" {
return nil
}
} else {
return fmt.Errorf("spec.Annotations['io.kubernetes.cri.container-type'] isn't exist")
}
if podID, ok := spec.Annotations["io.kubernetes.cri.sandbox-id"]; ok {
containerInfo.PodID = podID
} else {
return fmt.Errorf("spec.Annotations['io.kubernetes.cri.sandbox-id'] isn't exist")
}
if containerName, ok := spec.Annotations["io.kubernetes.cri.container-name"]; ok {
containerInfo.ContainerName = containerName
} else {
return fmt.Errorf("spec.Annotations['io.kubernetes.cri.container-name] isn't exist")
}
return nil
}
func (monitor *RuntimeMonitor) retrievePodInfo(containerInfo *varmortypes.ContainerInfo) error {
ctx, cancel := getContextWithTimeout(context.Background(), varmortypes.RuntimeTimeout)
defer cancel()
request := &runtimeapi.PodSandboxStatusRequest{
PodSandboxId: containerInfo.PodID,
}
response, err := monitor.runtimeClient.PodSandboxStatus(ctx, request)
if err != nil {
return err
}
containerInfo.PodName = response.Status.Metadata.Name
containerInfo.PodNamespace = response.Status.Metadata.Namespace
containerInfo.PodUID = response.Status.Metadata.Uid
containerInfo.PodAnnotations = response.Status.Annotations
return nil
}
// eventHandler monitor the create and delete events of containerd and send them to the enforcer to handle
func (monitor *RuntimeMonitor) eventHandler(stopCh <-chan struct{}) {
logger := monitor.log.WithName("eventHandler()")
logger.Info("start watching the containerd events")
ctx, cancel := appContext(context.Background(), varmortypes.K8sCriNamespace, 0)
defer cancel()
eventsFilter := []string{`topic=="/tasks/create"`, `topic=="/tasks/delete"`}
eventsService := monitor.containerdClient.EventService()
eventsCh, errCh := eventsService.Subscribe(ctx, eventsFilter...)
monitor.running = true
for {
select {
case e := <-eventsCh:
if e.Event == nil {
continue
}
switch e.Topic {
case "/tasks/create":
var createEvent events.TaskCreate
err := typeurl.UnmarshalTo(e.Event, &createEvent)
if err != nil {
logger.Error(err, "typeurl.UnmarshalTo() TaskCreate failed")
continue
}
info := varmortypes.ContainerInfo{
PID: createEvent.Pid,
ContainerID: createEvent.ContainerID,
}
err = monitor.retrieveContainerInfo(&info)
if err != nil {
logger.Error(err, "monitor.retrieveContainerInfo() failed", "container id", createEvent.ContainerID, "pid", createEvent.Pid)
continue
} else if info.PodID == "" {
logger.V(3).Info("sandbox was created, just ignore it")
continue
}
err = monitor.retrievePodInfo(&info)
if err != nil {
logger.Error(err, "monitor.retrievePodInfo() failed", "pod id", info.PodID)
continue
}
logger.V(3).Info("/tasks/create event", "info", info)
key := fmt.Sprintf("container.bpf.security.beta.varmor.org/%s", info.ContainerName)
if _, ok := info.PodAnnotations[key]; ok {
if monitor.taskCreateCh != nil {
monitor.taskCreateCh <- info
}
}
key = fmt.Sprintf("container.apparmor.security.beta.kubernetes.io/%s", info.ContainerName)
if value, ok := info.PodAnnotations[key]; ok {
if strings.HasPrefix(value, "localhost/") {
profileName := value[len("localhost/"):]
if ch, ok := monitor.modellerChs[profileName]; ok {
ch <- info.PID
continue // Seccomp and AppArmor share a common channel.
}
}
}
key = fmt.Sprintf("container.seccomp.security.beta.varmor.org/%s", info.ContainerName)
if value, ok := info.PodAnnotations[key]; ok {
if strings.HasPrefix(value, "localhost/") {
profileName := value[len("localhost/"):]
if ch, ok := monitor.modellerChs[profileName]; ok {
ch <- info.PID
}
}
}
case "/tasks/delete":
var deleteEvent events.TaskDelete
err := typeurl.UnmarshalTo(e.Event, &deleteEvent)
if err != nil {
fmt.Printf("typeurl.UnmarshalTo() ContainerDelete failed: %v\n", err)
continue
}
info := varmortypes.ContainerInfo{
PID: deleteEvent.Pid,
ContainerID: deleteEvent.ContainerID,
}
logger.V(3).Info("/tasks/delete event", "info", info)
if monitor.taskDeleteCh != nil {
monitor.taskDeleteCh <- info
}
}
case err := <-errCh:
varmorutils.SetAgentUnready()
logger.Error(err, "receive an error from the containerd, waiting for it to resume serving")
monitor.running = false
monitor.status = err
serving, err := monitor.containerdClient.IsServing(ctx)
if err != nil {
logger.Error(err, "containerdClient.IsServing() failed")
monitor.status = err
return
} else if serving {
// kindly hold on until containerd is fully initialized
time.Sleep(time.Second * 3)
logger.Info("restart watching the containerd events")
varmorutils.SetAgentReady()
eventsService = monitor.containerdClient.EventService()
eventsCh, errCh = eventsService.Subscribe(ctx, eventsFilter...)
monitor.running = true
monitor.status = nil
if monitor.taskDeleteSyncCh != nil {
logger.V(3).Info("notify the enforcer to handle the containers that exit or are created while the monitor is offline")
monitor.taskDeleteSyncCh <- true
monitor.CollectExistingTargetContainers()
}
} else {
logger.Info("the containerd isn't serving")
return
}
case <-stopCh:
logger.Info("stop watching the containerd events")
return
}
}
}
func (monitor *RuntimeMonitor) Run(stopCh <-chan struct{}) {
monitor.eventHandler(stopCh)
}
func (monitor *RuntimeMonitor) IsMonitoring() (bool, error) {
return monitor.running, monitor.status
}
// CollectExistingTargetContainers collects all existing containers that should be protected
// and sends them to the enforcer
func (monitor *RuntimeMonitor) CollectExistingTargetContainers() error {
logger := monitor.log.WithName("CollectExistingTargetContainers()")
logger.Info("start collecting the existing containers")
ctx, cancel := appContext(context.Background(), varmortypes.K8sCriNamespace, varmortypes.RuntimeTimeout)
defer cancel()
service := monitor.containerdClient.TaskService()
response, err := service.List(ctx, &tasks.ListTasksRequest{})
if err != nil {
return err
}
for _, task := range response.Tasks {
if task.Status.String() != "RUNNING" {
continue
}
info := varmortypes.ContainerInfo{
PID: task.Pid,
ContainerID: task.ID,
}
err = monitor.retrieveContainerInfo(&info)
if err != nil {
logger.Error(err, "monitor.retrieveContainerInfo() failed", "container id", info.ContainerID, "pid", info.PID)
continue
} else if info.PodID == "" {
logger.V(3).Info("sandbox was created, just ignore it", "container id", info.ContainerID, "pid", info.PID)
continue
}
err = monitor.retrievePodInfo(&info)
if err != nil {
logger.Error(err, "monitor.retrievePodInfo() failed", "pod id", info.PodID)
continue
}
key := fmt.Sprintf("container.bpf.security.beta.varmor.org/%s", info.ContainerName)
if _, ok := info.PodAnnotations[key]; ok {
if monitor.taskCreateCh != nil {
monitor.taskCreateCh <- info
}
}
}
return nil
}