/
crioHandler.go
385 lines (306 loc) · 10.9 KB
/
crioHandler.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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2021 Authors of KubeArmor
package core
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"time"
kl "github.com/kubearmor/KubeArmor/KubeArmor/common"
cfg "github.com/kubearmor/KubeArmor/KubeArmor/config"
kg "github.com/kubearmor/KubeArmor/KubeArmor/log"
tp "github.com/kubearmor/KubeArmor/KubeArmor/types"
spec "github.com/opencontainers/runtime-spec/specs-go"
"google.golang.org/grpc"
pb "k8s.io/cri-api/pkg/apis/runtime/v1"
)
// CrioHandler Structure
type CrioHandler struct {
// connection
conn *grpc.ClientConn
// crio client
client pb.RuntimeServiceClient
// containers is a map with empty value to have lookups in constant time
containers map[string]struct{}
}
// CrioContainerInfo struct corresponds to CRI-O's container info returned
// with container status
type CrioContainerInfo struct {
SandboxID string `json:"sandboxID"`
Pid int `json:"pid"`
RuntimeSpec spec.Spec `json:"runtimeSpec"`
Privileged bool `json:"privileged"`
}
// Crio Handler
var Crio *CrioHandler
// NewCrioHandler Function creates a new Crio handler
func NewCrioHandler() *CrioHandler {
ch := &CrioHandler{}
conn, err := grpc.Dial(cfg.GlobalCfg.CRISocket, grpc.WithInsecure())
if err != nil {
return nil
}
ch.conn = conn
// The runtime service client can be used for all RPCs
ch.client = pb.NewRuntimeServiceClient(ch.conn)
ch.containers = make(map[string]struct{})
return ch
}
// Close the connection
func (ch *CrioHandler) Close() {
if ch.conn != nil {
if err := ch.conn.Close(); err != nil {
kg.Err(err.Error())
}
}
}
// ==================== //
// == Container Info == //
// ==================== //
// GetContainerInfo Function gets info of a particular container
func (ch *CrioHandler) GetContainerInfo(ctx context.Context, containerID string, OwnerInfo map[string]tp.PodOwner) (tp.Container, error) {
// request to get status of specified container
// verbose has to be true to retrieve additional CRI specific info
req := &pb.ContainerStatusRequest{
ContainerId: containerID,
Verbose: true,
}
res, err := ch.client.ContainerStatus(ctx, req)
if err != nil {
return tp.Container{}, err
}
container := tp.Container{}
// == container base == //
resContainerStatus := res.Status
container.ContainerID = resContainerStatus.Id
container.ContainerName = resContainerStatus.Metadata.Name
container.NamespaceName = "Unknown"
container.EndPointName = "Unknown"
// check container labels
containerLables := resContainerStatus.Labels
if val, ok := containerLables["io.kubernetes.pod.namespace"]; ok {
container.NamespaceName = val
}
if val, ok := containerLables["io.kubernetes.pod.name"]; ok {
container.EndPointName = val
}
if len(OwnerInfo) > 0 {
if podOwnerInfo, ok := OwnerInfo[container.EndPointName]; ok {
container.Owner = podOwnerInfo
}
}
// extracting the runtime specific "info"
var containerInfo CrioContainerInfo
err = json.Unmarshal([]byte(res.Info["info"]), &containerInfo)
if err != nil {
return tp.Container{}, err
}
// path to container's root storage
container.AppArmorProfile = containerInfo.RuntimeSpec.Process.ApparmorProfile
container.Privileged = containerInfo.Privileged
pid := strconv.Itoa(containerInfo.Pid)
if data, err := os.Readlink("/proc/" + pid + "/ns/pid"); err == nil {
if _, err := fmt.Sscanf(data, "pid:[%d]\n", &container.PidNS); err != nil {
kg.Warnf("Unable to get PidNS (%s, %s, %s)", containerID, pid, err.Error())
}
} else {
return container, err
}
if data, err := os.Readlink("/proc/" + pid + "/ns/mnt"); err == nil {
if _, err := fmt.Sscanf(data, "mnt:[%d]\n", &container.MntNS); err != nil {
kg.Warnf("Unable to get MntNS (%s, %s, %s)", containerID, pid, err.Error())
}
} else {
return container, err
}
return container, nil
}
// ================= //
// == CRIO Events == //
// ================= //
// GetCrioContainers Function gets IDs of all containers
func (ch *CrioHandler) GetCrioContainers() (map[string]struct{}, error) {
containers := make(map[string]struct{})
var err error
req := pb.ListContainersRequest{}
if containerList, err := ch.client.ListContainers(context.Background(), &req); err == nil {
for _, container := range containerList.Containers {
containers[container.Id] = struct{}{}
}
return containers, nil
}
return nil, err
}
// GetNewCrioContainers Function gets new crio containers
func (ch *CrioHandler) GetNewCrioContainers(containers map[string]struct{}) map[string]struct{} {
newContainers := make(map[string]struct{})
for activeContainerID := range containers {
if _, ok := ch.containers[activeContainerID]; !ok {
newContainers[activeContainerID] = struct{}{}
}
}
return newContainers
}
// GetDeletedCrioContainers Function gets deleted crio containers
func (ch *CrioHandler) GetDeletedCrioContainers(containers map[string]struct{}) map[string]struct{} {
deletedContainers := make(map[string]struct{})
for globalContainerID := range ch.containers {
if _, ok := containers[globalContainerID]; !ok {
deletedContainers[globalContainerID] = struct{}{}
delete(ch.containers, globalContainerID)
}
}
ch.containers = containers
return deletedContainers
}
// UpdateCrioContainer Function
func (dm *KubeArmorDaemon) UpdateCrioContainer(ctx context.Context, containerID, action string) bool {
if Crio == nil {
return false
}
if action == "start" {
// get container info from client
container, err := Crio.GetContainerInfo(ctx, containerID, dm.OwnerInfo)
if err != nil {
return false
}
if container.ContainerID == "" {
return false
}
endpoint := tp.EndPoint{}
dm.ContainersLock.Lock()
if _, ok := dm.Containers[container.ContainerID]; !ok {
dm.Containers[container.ContainerID] = container
dm.ContainersLock.Unlock()
} else if dm.Containers[container.ContainerID].PidNS == 0 && dm.Containers[container.ContainerID].MntNS == 0 {
container.NamespaceName = dm.Containers[container.ContainerID].NamespaceName
container.EndPointName = dm.Containers[container.ContainerID].EndPointName
container.Labels = dm.Containers[container.ContainerID].Labels
container.ContainerName = dm.Containers[container.ContainerID].ContainerName
container.ContainerImage = dm.Containers[container.ContainerID].ContainerImage
container.PolicyEnabled = dm.Containers[container.ContainerID].PolicyEnabled
container.ProcessVisibilityEnabled = dm.Containers[container.ContainerID].ProcessVisibilityEnabled
container.FileVisibilityEnabled = dm.Containers[container.ContainerID].FileVisibilityEnabled
container.NetworkVisibilityEnabled = dm.Containers[container.ContainerID].NetworkVisibilityEnabled
container.CapabilitiesVisibilityEnabled = dm.Containers[container.ContainerID].CapabilitiesVisibilityEnabled
dm.Containers[container.ContainerID] = container
dm.ContainersLock.Unlock()
dm.EndPointsLock.Lock()
for idx, endPoint := range dm.EndPoints {
if endPoint.NamespaceName == container.NamespaceName && endPoint.EndPointName == container.EndPointName && kl.ContainsElement(endPoint.Containers, container.ContainerID) {
// update apparmor profiles
if !kl.ContainsElement(endPoint.AppArmorProfiles, container.AppArmorProfile) {
dm.EndPoints[idx].AppArmorProfiles = append(dm.EndPoints[idx].AppArmorProfiles, container.AppArmorProfile)
}
if container.Privileged && dm.EndPoints[idx].PrivilegedContainers != nil {
dm.EndPoints[idx].PrivilegedContainers[container.ContainerName] = struct{}{}
}
endpoint = dm.EndPoints[idx]
break
}
}
dm.EndPointsLock.Unlock()
} else {
dm.ContainersLock.Unlock()
return false
}
if dm.SystemMonitor != nil && cfg.GlobalCfg.Policy {
// update NsMap
dm.SystemMonitor.AddContainerIDToNsMap(containerID, container.NamespaceName, container.PidNS, container.MntNS)
dm.RuntimeEnforcer.RegisterContainer(containerID, container.PidNS, container.MntNS)
if len(endpoint.SecurityPolicies) > 0 { // struct can be empty or no policies registered for the endpoint yet
dm.Logger.UpdateSecurityPolicies("ADDED", endpoint)
if dm.RuntimeEnforcer != nil && endpoint.PolicyEnabled == tp.KubeArmorPolicyEnabled {
// enforce security policies
dm.RuntimeEnforcer.UpdateSecurityPolicies(endpoint)
}
}
}
if !dm.K8sEnabled {
dm.ContainersLock.Lock()
dm.EndPointsLock.Lock()
dm.MatchandUpdateContainerSecurityPolicies(containerID)
dm.EndPointsLock.Unlock()
dm.ContainersLock.Unlock()
}
dm.Logger.Printf("Detected a container (added/%.12s)", containerID)
} else if action == "destroy" {
dm.ContainersLock.Lock()
container, ok := dm.Containers[containerID]
if !ok {
dm.ContainersLock.Unlock()
return false
}
if !dm.K8sEnabled {
dm.EndPointsLock.Lock()
dm.MatchandRemoveContainerFromEndpoint(containerID)
dm.EndPointsLock.Unlock()
}
delete(dm.Containers, containerID)
dm.ContainersLock.Unlock()
dm.EndPointsLock.Lock()
for idx, endPoint := range dm.EndPoints {
if endPoint.NamespaceName == container.NamespaceName && endPoint.EndPointName == container.EndPointName && kl.ContainsElement(endPoint.Containers, container.ContainerID) {
// update apparmor profiles
for idxA, profile := range endPoint.AppArmorProfiles {
if profile == container.AppArmorProfile {
dm.EndPoints[idx].AppArmorProfiles = append(dm.EndPoints[idx].AppArmorProfiles[:idxA], dm.EndPoints[idx].AppArmorProfiles[idxA+1:]...)
break
}
}
break
}
}
dm.EndPointsLock.Unlock()
if dm.SystemMonitor != nil && cfg.GlobalCfg.Policy {
// update NsMap
dm.SystemMonitor.DeleteContainerIDFromNsMap(containerID, container.NamespaceName, container.PidNS, container.MntNS)
dm.RuntimeEnforcer.UnregisterContainer(containerID)
}
dm.Logger.Printf("Detected a container (removed/%.12s)", containerID)
}
return true
}
// MonitorCrioEvents Function
func (dm *KubeArmorDaemon) MonitorCrioEvents() {
dm.WgDaemon.Add(1)
defer dm.WgDaemon.Done()
Crio = NewCrioHandler()
// check if Crio exists
if Crio == nil {
return
}
dm.Logger.Print("Started to monitor CRI-O events")
for {
select {
case <-StopChan:
return
default:
containers, err := Crio.GetCrioContainers()
if err != nil {
return
}
invalidContainers := []string{}
newContainers := Crio.GetNewCrioContainers(containers)
deletedContainers := Crio.GetDeletedCrioContainers(containers)
if len(newContainers) > 0 {
for containerID := range newContainers {
if !dm.UpdateCrioContainer(context.Background(), containerID, "start") {
invalidContainers = append(invalidContainers, containerID)
}
}
}
for _, invalidContainerID := range invalidContainers {
delete(Crio.containers, invalidContainerID)
}
if len(deletedContainers) > 0 {
for containerID := range deletedContainers {
dm.UpdateCrioContainer(context.Background(), containerID, "destroy")
}
}
}
time.Sleep(time.Millisecond * 50)
}
}