-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathexec.go
More file actions
399 lines (341 loc) · 9.58 KB
/
Copy pathexec.go
File metadata and controls
399 lines (341 loc) · 9.58 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package exec
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"strings"
"text/template"
"github.com/distribution/reference"
"github.com/spf13/cobra"
"github.com/iximiuz/cdebug/pkg/cliutil"
"github.com/iximiuz/cdebug/pkg/kubernetes"
)
const (
defaultToolkitImage = "docker.io/library/busybox:musl"
schemaContainerd = "containerd://"
schemaDocker = "docker://"
schemaKubeCRI = "cri://"
schemaKubeLong = "kubernetes://"
schemaKubeShort = "k8s://"
schemaNerdctl = "nerdctl://"
schemaPodman = "podman://"
schemaOCI = "oci://" // runc, crun, etc.
exampleText = `
# Start a %s shell in the Docker container:
cdebug exec -it mycontainer
cdebug exec -it docker://mycontainer
# Execute a command in the Docker container:
cdebug exec mycontainer cat /etc/os-release
# Use a different debugging toolkit image:
cdebug exec -it --image=alpine mycontainer
# Use a nixery.dev image (https://nixery.dev/):
cdebug exec -it --image=nixery.dev/shell/vim/ps/tshark mycontainer
# Exec into a containerd container:
cdebug exec -it containerd://mycontainer ...
cdebug exec --namespace myns -it containerd://mycontainer ...
# Exec into a nerdctl container:
cdebug exec -it nerdctl://mycontainer ...
# Start a shell in a Kubernetes pod:
cdebug exec -it pod/mypod
cdebug exec -it k8s://mypod
cdebug exec --namespace=myns -it pod/mypod
# Start a shell in a Kubernetes pod's container:
cdebug exec -it pod/mypod/mycontainer`
)
var (
errTargetNotFound = errors.New("target container not found")
errTargetNotRunning = errors.New("target container found but it's not running: executing commands in stopped containers is not supported yet")
)
func errCannotPull(image string, cause error) error {
return fmt.Errorf("cannot pull debugger image %q: %w", image, cause)
}
func errCannotCreate(cause error) error {
return fmt.Errorf("cannot create debugger container: %w", cause)
}
type options struct {
target string
schema string
name string
image string
tty bool
stdin bool
detach bool
cmd []string
user string
privileged bool
autoRemove bool
quiet bool
runtime string
platform string
namespace string
kubeconfig string
kubeconfigContext string
override string
overrideType kubernetes.OverrideType
}
func NewCommand(cli cliutil.CLI) *cobra.Command {
var opts options
cmd := &cobra.Command{
Use: "exec [OPTIONS] [schema://][POD][CONTAINER] [COMMAND] [ARG...]",
Short: "Start a debugger shell in the target container or pod.",
Example: fmt.Sprintf(exampleText[1:], strings.TrimPrefix(defaultToolkitImage, "docker.io/library/")),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if !opts.stdin {
opts.quiet = true
}
cli.SetQuiet(opts.quiet)
if err := cli.InputStream().CheckTty(opts.stdin, opts.tty); err != nil {
return cliutil.WrapStatusError(err)
}
opts.target = args[0]
if len(args) > 1 {
opts.cmd = args[1:]
}
if sep := strings.Index(opts.target, "://"); sep != -1 {
opts.schema = opts.target[:sep+3]
opts.target = opts.target[sep+3:]
} else if strings.HasPrefix(opts.target, "pod/") || strings.HasPrefix(opts.target, "pods/") {
opts.schema = schemaKubeLong
} else {
opts.schema = schemaDocker
}
if !reference.ReferenceRegexp.MatchString(opts.image) {
return cliutil.WrapStatusError(
fmt.Errorf("invalid debugging toolkit image name %q: %v",
opts.image, reference.ErrReferenceInvalidFormat),
)
}
if opts.tty && !opts.stdin {
return cliutil.WrapStatusError(errors.New("the -t/--tty flag requires the -i/--stdin flag"))
}
ctx := context.Background()
switch opts.schema {
case schemaContainerd, schemaNerdctl:
return cliutil.WrapStatusError(wrapExitError(runDebuggerContainerd(ctx, cli, &opts)))
case schemaDocker:
return cliutil.WrapStatusError(wrapExitError(runDebuggerDocker(ctx, cli, &opts)))
case schemaKubeLong, schemaKubeShort:
return cliutil.WrapStatusError(wrapExitError(runDebuggerKubernetes(ctx, cli, &opts)))
case schemaPodman, schemaOCI, schemaKubeCRI:
return cliutil.WrapStatusError(errors.New("coming soon"))
default:
return cliutil.WrapStatusError(fmt.Errorf("unknown schema %q", opts.schema))
}
},
}
flags := cmd.Flags()
flags.SetInterspersed(false) // Instead of relying on --
flags.BoolVarP(
&opts.quiet,
"quiet",
"q",
false,
`Suppress verbose output`,
)
flags.StringVar(
&opts.name,
"name",
"",
`Assign a name to the debugger container`,
)
flags.StringVar(
&opts.image,
"image",
defaultToolkitImage,
`Debugging toolkit image (hint: use "busybox:musl" or "nixery.dev/shell/vim/ps/tool3/tool4/...")`,
)
flags.BoolVarP(
&opts.stdin,
"interactive",
"i",
false,
`Keep the STDIN open (as in "docker exec -i")`,
)
flags.BoolVarP(
&opts.tty,
"tty",
"t",
false,
`Allocate a pseudo-TTY (as in "docker exec -t")`,
)
flags.BoolVarP(
&opts.detach,
"detach",
"d",
false,
`Detached mode: execute the command in the background`,
)
flags.StringVarP(
&opts.user,
"user",
"u",
"",
`Run the debugger container as User (format: <name|uid>[:<group|gid>])`,
)
flags.BoolVar(
&opts.privileged,
"privileged",
false,
`God mode for the debugger container (as in "docker run --privileged")`,
)
flags.BoolVar(
&opts.autoRemove,
"rm",
false,
`Automatically remove the debugger container when it exits (as in "docker run --rm")`,
)
flags.StringVarP(
&opts.namespace,
"namespace",
"n",
"",
`Namespace (the final meaning of this parameter is runtime specific)`,
)
flags.StringVar(
&opts.runtime,
"runtime",
"",
`Runtime address ("/var/run/docker.sock" | "/run/containerd/containerd.sock" | "https://<kube-api-addr>:8433/...)`,
)
flags.StringVar(
&opts.platform,
"platform",
"",
`Platform (e.g., linux/amd64, linux/arm64) of the target container (for some runtimes it's hard to detect it automatically, but the debug sidecar must be of the same platform as the target)`,
)
flags.StringVar(
&opts.kubeconfig,
"kubeconfig",
"",
`Path to the kubeconfig file (default is $HOME/.kube/config)`,
)
flags.StringVar(
&opts.kubeconfigContext,
"kubeconfig-context",
"",
`Name of the kubeconfig context to use`,
)
flags.StringVar(
&opts.override,
"override",
"",
`[Kubernetes only] An inline JSON override for the generated ephemeral container object. Example: '{ "env": [{ "name": "DEBUG", "value": "1" }] }'`,
)
flags.StringVar(
(*string)(&opts.overrideType),
"override-type",
string(kubernetes.DefaultOverrideType),
fmt.Sprintf(`[Kubernetes only] The method used to override the generated ephemeral container object: %s, %s, or %s.`,
kubernetes.OverrideTypeJSON, kubernetes.OverrideTypeMerge, kubernetes.OverrideTypeStrategic,
),
)
return cmd
}
func debuggerName(name string, runID string) string {
if len(name) > 0 {
return name
}
return "cdebug-" + runID
}
var (
simpleEntrypoint = template.Must(template.New("user-entrypoint").Parse(`
set -eu
export CDEBUG_ROOTFS=/
if [ "${HOME:-/}" != "/" ]; then
ln -s /proc/{{ .TARGET_PID }}/root/ ${HOME}target-rootfs
fi
# TODO: Add target container's PATH to the user's PATH
exec {{ .Cmd }}
`))
chrootEntrypoint = template.Must(template.New("chroot-entrypoint").Parse(`
set -eu
CURRENT_PID=$(sh -c 'echo $PPID')
{{ if .IsNix }}
CURRENT_NIX_INODE=$(stat -c '%i' /nix)
TARGET_NIX_INODE=$(stat -c '%i' /proc/{{ .TARGET_PID }}/root/nix 2>/dev/null || echo 0)
if [ ${CURRENT_NIX_INODE} -ne ${TARGET_NIX_INODE} ]; then
rm -rf /proc/{{ .TARGET_PID }}/root/nix
ln -s /proc/${CURRENT_PID}/root/nix /proc/{{ .TARGET_PID }}/root/nix
fi
{{ end }}
export CDEBUG_ROOTFS=/proc/${CURRENT_PID}/root/
cat > /.cdebug-entrypoint.sh <<EOF
#!/bin/sh
export PATH=$PATH:$CDEBUG_ROOTFS/bin:$CDEBUG_ROOTFS/usr/bin:$CDEBUG_ROOTFS/sbin:$CDEBUG_ROOTFS/usr/sbin:$CDEBUG_ROOTFS/usr/local/bin:$CDEBUG_ROOTFS/usr/local/sbin
chroot /proc/{{ .TARGET_PID }}/root {{ .Cmd }}
EOF
exec sh /.cdebug-entrypoint.sh
`))
)
func debuggerEntrypoint(
cli cliutil.CLI,
runID string,
targetPID int,
image string,
cmd []string,
chroot bool,
) string {
if chroot {
return mustRenderTemplate(
cli,
chrootEntrypoint,
map[string]any{
"ID": runID,
"TARGET_PID": targetPID,
"IsNix": strings.Contains(image, "nixery"),
"Cmd": func() string {
if len(cmd) == 0 {
return "sh"
}
return "sh -c '" + strings.Join(shellescape(cmd), " ") + "'"
}(),
},
)
}
return mustRenderTemplate(
cli,
simpleEntrypoint,
map[string]any{
"PID": targetPID,
"Cmd": func() string {
if len(cmd) == 0 {
return "sh"
}
return "sh -c \"" + strings.Join(shellescape(cmd), " ") + "\""
}(),
},
)
}
func mustRenderTemplate(cli cliutil.CLI, t *template.Template, data any) string {
var buf bytes.Buffer
if err := t.Execute(&buf, data); err != nil {
cli.PrintErr("Cannot render template %q: %w", t.Name(), err)
os.Exit(1)
}
return buf.String()
}
// FIXME: Too naive. This will break for args containing escaped symbols.
func shellescape(args []string) (escaped []string) {
for _, a := range args {
if strings.ContainsAny(a, " \t\n\r") {
a = `"` + a + `"`
}
escaped = append(escaped, a)
}
return
}
func isRootUser(user string) bool {
return len(user) == 0 || user == "root" || user == "0" || user == "0:0"
}
func wrapExitError(err error) error {
if err == nil {
return nil
}
if strings.Contains(strings.ToLower(err.Error()), "permission denied") {
return fmt.Errorf("%s\n\nHint: try running `cdebug exec` with the --privileged flag", err)
}
return err
}