-
Notifications
You must be signed in to change notification settings - Fork 322
/
main.go
364 lines (310 loc) · 10.5 KB
/
main.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package main
// OCI hook for tetragon
// See: https://github.com/opencontainers/runtime-spec/blob/main/config.md#posix-platform-hooks
//
// This is an implementation of an OCI hook that can be used to notify the tetragon agent about
// events such as container creation. It can be used by any container runtime that implements the
// OCI Runtime Specification.
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"os/signal"
"path"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/alecthomas/kong"
"github.com/cilium/lumberjack/v2"
"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
"github.com/opencontainers/runtime-spec/specs-go"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var (
binDir = getBinaryDir()
defaultLogFname = filepath.Join(binDir, "tetragon-oci-hook.log")
defaultConfFile = filepath.Join(binDir, "tetragon-oci-hook.json")
defaultAgentAddress = "unix:///var/run/cilium/tetragon/tetragon.sock"
defaultAnnotationsNamespaceKeys = "io.kubernetes.pod.namespace,io.kubernetes.cri.sandbox-namespace"
defaultAllowNamspaces = "kube-system"
)
var cliConf struct {
LogFname string `name:"log-fname" default:"${defLogFname}" help:"log output filename."`
LogLevel string `name:"log-level" default:"info" help:"log level"`
AgentAddr string `name:"grpc-address" default:"${defAgentAddress}" help:"Tetragon agent gRPC address"`
GrpcTimeout time.Duration `name:"grpc-timeout" default:"10s" help:"timeout for connecting to the agent"`
DisableGrpc bool `name:"disable-grpc" default:false help:"do not connect to the agent. Instead, write a message to the log"`
JustPrintConfig bool `name:"just-print-config" default:false help:"just print the config and exit"`
AnnNamespaceKeys []string `name:"annotations-namespace-key" default:"${defAnnotationsNamespaceKeys}" help:"Runtime annotation keys for accessing k8s namespace"`
FailCelUser string `name:"fail-cel-expr" help:"CEL expression to decide whether to fail (and stop container from starting) or not"`
FailAllowNamespaces []string `name:"fail-allow-namespaces" default:"${defAllowNamespaces}" help:"The hook will not fail for the specified namespaces, as determined by runtime annotation labels. Flag will be ignored if fail-cel-expr is set."`
HookName string `arg:"" name:"hook"`
}
func readJsonSpec(fname string) (*specs.Spec, error) {
data, err := os.ReadFile(fname)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %w", fname, err)
}
var spec specs.Spec
if err := json.Unmarshal(data, &spec); err != nil {
return nil, fmt.Errorf("unmarshal failed: %w", err)
}
return &spec, nil
}
func getTime() string {
s, _ := time.Now().UTC().MarshalText()
return string(s)
}
func hookRequest(req *tetragon.RuntimeHookRequest) error {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
connCtx, connCancel := context.WithTimeout(ctx, cliConf.GrpcTimeout)
defer connCancel()
conn, err := grpc.DialContext(connCtx, cliConf.AgentAddr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
if err != nil {
return fmt.Errorf("connecting to agent (%s) failed: %s", err, cliConf.AgentAddr)
}
defer conn.Close()
client := tetragon.NewFineGuidanceSensorsClient(conn)
_, err = client.RuntimeHook(ctx, req)
if err != nil {
return err
}
return nil
}
func getCgroupPath(spec *specs.Spec) (string, error) {
var cgroupPath string
if spec.Linux == nil {
return "", fmt.Errorf("Linux is empty in spec: %+v", spec)
}
cgroupPath = spec.Linux.CgroupsPath
if strings.Contains(cgroupPath, "/") {
return cgroupPath, nil
}
// There are some cases where CgroupsPath is specified as "slice:prefix:name"
// From runc --help
// --systemd-cgroup enable systemd cgroup support, expects cgroupsPath to be of form "slice:prefix:name"
// for e.g. "system.slice:runc:434234"
//
// https://github.com/opencontainers/runc/blob/5cf9bb229feed19a767cbfdf9702f6487341e29e/libcontainer/specconv/spec_linux.go#L655-L663
parts := strings.Split(cgroupPath, ":")
if len(parts) == 3 {
var err error
slice, scope, name := parts[0], parts[1], parts[2]
slice, err = systemd.ExpandSlice(slice)
if err != nil {
return "", fmt.Errorf("failed to parse cgroup path: %s (%s does not seem to be a slice)", cgroupPath, slice)
}
// https://github.com/opencontainers/runc/blob/5cf9bb229feed19a767cbfdf9702f6487341e29e/libcontainer/cgroups/systemd/common.go#L95-L101
if !strings.HasSuffix(name, ".slice") {
name = scope + "-" + name + ".scope"
}
return filepath.Join(slice, name), nil
}
return "", fmt.Errorf("Unknown cgroup path: %s", cgroupPath)
}
func containerNameFromAnnotations(annotations map[string]string) string {
// containerd
if val, ok := annotations["io.kubernetes.cri.container-name"]; ok {
return val
}
// crio
if val, ok := annotations["io.kubernetes.container.name"]; ok {
return val
}
return ""
}
// NB: the second argument is only used in case of an error, so disable revive's complains
// revive:disable:error-return
func createContainerHook(log *slog.Logger) (error, map[string]string) {
// rootDir is the current directory
rootDir, err := os.Getwd()
var configName string
if err != nil {
log.Warn("failed to retrieve CWD, using '.'",
"error", err)
rootDir = "."
}
configPaths := []string{
"../config.json", // containerd
"../userdata/config.json", // cri-o
}
configName = ""
for _, path := range configPaths {
p := filepath.Join(rootDir, path)
if _, err := os.Stat(p); err == nil {
configName = p
break
}
}
if configName == "" {
return fmt.Errorf("failed to find spec file. Tried the following dirs: %v", configPaths), nil
}
// We use the config.json file to get the cgroup path. An alternative option is to use
// /proc/self/cgroup, but parsing the spec seems like a better option.
var cgroupPath string
spec, err := readJsonSpec(configName)
if err != nil {
log.Warn("failed to read spec file", "name", configName, "error", err)
} else if cgroupPath, err = getCgroupPath(spec); err != nil {
log.Warn("error getting cgroup path", "error", err)
}
// if have no information whatsover, do not contact the agent
if cgroupPath == "" && rootDir == "" {
return fmt.Errorf("unable to determine either RootDir or cgroupPath, bailing out"), nil
}
containerName := containerNameFromAnnotations(spec.Annotations)
req := &tetragon.RuntimeHookRequest{
Event: &tetragon.RuntimeHookRequest_CreateContainer{
CreateContainer: &tetragon.CreateContainer{
CgroupsPath: cgroupPath,
RootDir: rootDir,
Annotations: spec.Annotations,
ContainerName: containerName,
},
},
}
log = log.With(
"req-cgroups", cgroupPath,
"req-rootdir", rootDir,
"req-containerName", containerName,
)
if log.Enabled(context.TODO(), slog.LevelDebug) {
// NB: only add annotations in debug level since they are too noisy
log = log.With("req-annotations", spec.Annotations)
}
if cliConf.DisableGrpc {
log.Info("gRPC was disabled, so will not be contacting the agent")
return nil, nil
}
err = hookRequest(req)
if err != nil {
log.Warn("hook request to the agent failed", "err", err)
return err, spec.Annotations
}
log.Info("hook request to agent succeeded")
return nil, nil
}
func checkFail(log *slog.Logger, prog *celProg, annotations map[string]string) error {
var err error
log.Debug("running fail check", "annotations", annotations)
fail, err := prog.RunFailCheck(annotations)
if err != nil {
return fmt.Errorf("failed to run failCheck: %w", err)
}
if fail {
return errors.New("failCheck determined that we should fail")
}
log.Info("failCheck determined that we should not fail this container, even if there was an error")
return nil
}
func failTestProg() (*celProg, error) {
var ret *celProg
var err error
if expr := cliConf.FailCelUser; expr != "" {
ret, err = celUserExpr(expr)
} else {
ret, err = celAllowNamespaces(cliConf.FailAllowNamespaces)
}
return ret, err
}
type logHandler struct {
slog.Handler
}
func (lh *logHandler) Handle(ctx context.Context, r slog.Record) error {
err := lh.Handler.Handle(ctx, r)
if err != nil {
fmt.Fprintf(os.Stderr, "error writing to logger: %v\n", err)
}
return err
}
func main() {
ctx := kong.Parse(&cliConf,
kong.Vars{
"defLogFname": defaultLogFname,
"defAgentAddress": defaultAgentAddress,
"defAnnotationsNamespaceKeys": defaultAnnotationsNamespaceKeys,
"defAllowNamespaces": defaultAllowNamspaces,
},
kong.Configuration(kong.JSON, defaultConfFile),
)
if kongCmd := ctx.Command(); kongCmd != "<hook>" {
fmt.Fprintf(os.Stderr, "unexpected parsing result: %s", kongCmd)
os.Exit(1)
}
if cliConf.JustPrintConfig {
fmt.Printf("%+v\n", cliConf)
os.Exit(0)
}
var logLevel slog.Level
var logLevelArgError bool
if err := logLevel.UnmarshalText([]byte(cliConf.LogLevel)); err != nil {
logLevel = slog.LevelInfo
logLevelArgError = true
}
log := slog.New(&logHandler{slog.NewJSONHandler(
&lumberjack.Logger{
Filename: cliConf.LogFname,
MaxSize: 50, // megabytes
MaxBackups: 3,
MaxAge: 7, //days
},
&slog.HandlerOptions{
Level: logLevel,
},
)})
if logLevelArgError {
log.Warn("was not able to parse logLevel, using default",
"arg", cliConf.LogLevel,
"default", logLevel)
}
failTestProg, err := failTestProg()
if err != nil {
log.Warn("error in creating fail test prog, bailing out", "errro", err)
os.Exit(1)
}
switch cliConf.HookName {
case "createContainer":
log = log.With(
"hook", "create-container",
"start-time", getTime(),
)
err, annotations := createContainerHook(log)
if err != nil {
if shouldFail := checkFail(log, failTestProg, annotations); shouldFail != nil {
log.Warn("failing container", "error", shouldFail)
os.Exit(1)
}
}
case "createRuntime":
// do nothing
case "poststop":
// do nothing
default:
log.Warn("hook called with unknown hook",
"hook", cliConf.HookName,
)
}
return
}
func getBinaryDir() string {
p, err := os.Executable()
if err != nil {
// if there is an error, use cwd
return "."
}
p, err = filepath.EvalSymlinks(p)
if err != nil {
// if there is an error, use cwd
return "."
}
return path.Dir(p)
}