forked from slackhq/go-audit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
extras_containers.go
200 lines (172 loc) · 4.81 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
// +build amd64
// +build !nocontainers
package main
import (
"context"
"strconv"
"strings"
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 pid_cache=%d docker_cache=%d)\n",
cp.docker != nil,
cacheSize(cp.pidCache),
cacheSize(cp.dockerCache),
)
}
return cp, err
}
return nil, nil
})
}
type ContainerParser struct {
docker *dockerclient.Client
// map[int]string
// (pid -> containerID)
pidCache Cache
// map[string]dockertypes.ContainerJSON
// (containerID -> dockerResponse)
dockerCache 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
}
}
return &ContainerParser{
docker: docker,
pidCache: NewCache(config.GetInt("pid_cache")),
dockerCache: NewCache(config.GetInt("docker_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 getPid(data string) (pid, ppid int) {
start := 0
end := 0
var err error
for {
if start = strings.Index(data, "pid="); start < 0 {
return
}
// Progress the start point beyon the = sign
start += 4
if end = strings.IndexByte(data[start:], spaceChar); end < 0 {
// There was no ending space, maybe the pid is at the end of the line
end = len(data) - start
// If the end of the line is greater than 7 characters away (overflows 22 bit uint) then it can't be a pid
// > On 64-bit systems, pid_max can be set to any value up to 2^22 (PID_MAX_LIMIT, approximately 4 million).
if end > 7 {
return
}
}
id := data[start : start+end]
if start > 4 && data[start-5] == 'p' {
ppid, err = strconv.Atoi(id)
} else {
pid, err = strconv.Atoi(id)
}
if err != nil {
el.Printf("Failed to parse pid: %s: %v\n", id, err)
}
if pid != 0 && ppid != 0 {
return
}
data = data[start+end:]
}
}
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"],
}
}
}
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
}