-
Notifications
You must be signed in to change notification settings - Fork 163
/
extras_containers.go
230 lines (200 loc) · 5.93 KB
/
extras_containers.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
//go:build !nocontainers
// +build !nocontainers
package main
import (
"context"
"github.com/containerd/containerd"
"github.com/containerd/containerd/containers"
dockertypes "github.com/docker/docker/api/types"
dockerclient "github.com/docker/docker/client"
"github.com/golang/groupcache/lru"
"github.com/spf13/viper"
)
func init() {
RegisterExtraParser(func(config *viper.Viper) (ExtraParser, error) {
if config.GetBool("extras.containers.enabled") {
cp, err := NewContainerParser(config.Sub("extras.containers"))
if err == nil {
l.Printf("ContainerParser enabled (docker=%v containerd=%v pid_cache=%d docker_cache=%d containerd_cache=%d)\n",
cp.docker != nil,
cp.containerd != nil,
cacheSize(cp.pidCache),
cacheSize(cp.dockerCache),
cacheSize(cp.containerdCache),
)
}
return cp, err
}
return nil, nil
})
}
type ContainerParser struct {
docker *dockerclient.Client
containerd *containerd.Client
// map[int]string
// (pid -> containerID)
pidCache Cache
// map[string]dockertypes.ContainerJSON
// (containerID -> dockerResponse)
dockerCache Cache
// map[string]*containers.Container
// (containerID -> containerdResponse)
containerdCache Cache
}
type Cache interface {
Add(lru.Key, interface{})
Get(lru.Key) (interface{}, bool)
}
type NoCache struct{}
func (NoCache) Add(lru.Key, interface{}) {}
func (NoCache) Get(lru.Key) (interface{}, bool) { return nil, false }
// NewCache returns an lru.Cache if size is >0, NoCache otherwise
func NewCache(size int) Cache {
if size > 0 {
return lru.New(size)
}
return NoCache{}
}
func cacheSize(c Cache) int {
switch x := c.(type) {
case *lru.Cache:
return x.MaxEntries
}
return 0
}
func NewContainerParser(config *viper.Viper) (*ContainerParser, error) {
var docker *dockerclient.Client
if config.GetBool("docker") {
version := config.GetString("docker_api_version")
if version == "" {
// > Docker does not recommend running versions prior to 1.12, which
// > means you are encouraged to use an API version of 1.24 or higher.
// https://docs.docker.com/develop/sdk/#api-version-matrix
version = "1.24"
}
var err error
docker, err = dockerclient.NewClientWithOpts(dockerclient.FromEnv, dockerclient.WithVersion(version))
if err != nil {
return nil, err
}
}
var containerdClient *containerd.Client
if config.GetBool("containerd") {
var opts []containerd.ClientOpt
sockAddr := config.GetString("containerd_sock")
if sockAddr == "" {
sockAddr = "/run/containerd/containerd.sock"
}
namespace := config.GetString("containerd_namespace")
if namespace != "" {
opts = append(opts, containerd.WithDefaultNamespace(namespace))
}
var err error
containerdClient, err = containerd.New(sockAddr, opts...)
if err != nil {
return nil, err
}
}
return &ContainerParser{
docker: docker,
containerd: containerdClient,
pidCache: NewCache(config.GetInt("pid_cache")),
dockerCache: NewCache(config.GetInt("docker_cache")),
containerdCache: NewCache(config.GetInt("containerd_cache")),
}, nil
}
// Find `pid=` in a message and adds the container ids to the Extra object
func (c ContainerParser) Parse(am *AuditMessage) {
switch am.Type {
case 1300, 1326:
am.Containers = c.getContainersForPid(getPid(am.Data))
}
}
func (c ContainerParser) getContainersForPid(pid, ppid int) map[string]string {
if pid == 0 {
return nil
}
cid, err := c.getPidContainerID(pid)
if err != nil {
// pid might have exited before we could check it, try the ppid
return c.getContainersForPid(ppid, 0)
}
if cid == "" {
return nil
}
if c.docker != nil {
container, err := c.getDockerContainer(cid)
if err != nil {
el.Printf("failed to query docker for container id: %s: %v\n", cid, err)
} else {
return map[string]string{
"id": cid,
"image": container.Config.Image,
"name": container.Config.Labels["io.kubernetes.container.name"],
"pod_uid": container.Config.Labels["io.kubernetes.pod.uid"],
"pod_name": container.Config.Labels["io.kubernetes.pod.name"],
"pod_namespace": container.Config.Labels["io.kubernetes.pod.namespace"],
}
}
}
if c.containerd != nil {
container, err := c.getContainerdContainer(cid)
if err != nil {
el.Printf("failed to query containerd for container id: %s: %v\n", cid, err)
} else {
if container.Labels != nil {
return map[string]string{
"id": cid,
"image": container.Image,
"name": container.Labels["io.kubernetes.container.name"],
"pod_uid": container.Labels["io.kubernetes.pod.uid"],
"pod_name": container.Labels["io.kubernetes.pod.name"],
"pod_namespace": container.Labels["io.kubernetes.pod.namespace"],
}
} else {
return map[string]string{
"id": cid,
"image": container.Image,
}
}
}
}
return map[string]string{
"id": cid,
}
}
func (c ContainerParser) getPidContainerID(pid int) (string, error) {
if v, found := c.pidCache.Get(pid); found {
return v.(string), nil
}
cid, err := processContainerID(pid)
if err == nil {
c.pidCache.Add(pid, cid)
}
return cid, err
}
func (c ContainerParser) getDockerContainer(containerID string) (dockertypes.ContainerJSON, error) {
if v, found := c.dockerCache.Get(containerID); found {
return v.(dockertypes.ContainerJSON), nil
}
container, err := c.docker.ContainerInspect(context.TODO(), containerID)
if err == nil {
c.dockerCache.Add(containerID, container)
}
return container, err
}
func (c ContainerParser) getContainerdContainer(containerID string) (*containers.Container, error) {
if v, found := c.containerdCache.Get(containerID); found {
return v.(*containers.Container), nil
}
container, err := c.containerd.LoadContainer(context.TODO(), containerID)
if err != nil {
return nil, err
}
info, err := container.Info(context.TODO())
if err != nil {
return nil, err
}
c.containerdCache.Add(containerID, &info)
return &info, nil
}