Skip to content

Commit af4bc42

Browse files
committed
improved non-root user handling for the debug command, renamed the --auto-run-as-non-root debug command flag to --fallback-to-target-user
Signed-off-by: Kyle Quest <[email protected]>
1 parent bbcb79f commit af4bc42

File tree

18 files changed

+203
-76
lines changed

18 files changed

+203
-76
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ Debug minimal or regular container images running in Docker, Podman, Kubernetes
587587
- `--gid` - GID to use for the debugging sidecar container.
588588
- `--run-privileged` - Run the debug sidecar as a privileged container (true by default).
589589
- `--security-context-from-target` - Use the security context params from the target container with the debug sidecar container.
590-
- `--auto-run-as-non-root` - Auto-adjust the config to run as non-root (true by default; set it to false to disable this behavior).
590+
- `--fallback-to-target-user` - Fallback to using target container user if it's non-root (true by default; set it to false to disable this behavior).
591591
- `--run-as-target-shell` - Attach an interactive terminal to the debug container and run shell as if it's running in the target container environment (true by default).
592592
- `--list-sessions` - List all debug sessions for the selected target (pod and optionally selected container for k8s or container for other runtimes).
593593
- `--show-session-logs` - Show logs for the selected debug session (using namespace, pod, target container or debug session container name for k8s or debug session container name for other runtimes).
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: example-pod-nonroot
5+
labels:
6+
name: nginx
7+
spec:
8+
securityContext:
9+
runAsNonRoot: true
10+
containers:
11+
- name: example-container
12+
image: cgr.dev/chainguard/nginx:latest
13+
ports:
14+
- containerPort: 8080
15+
hostPort: 8080
16+
17+
# kubectl apply -f manifest_nonroot.yaml
18+
# kubectl exec -it example-pod-nonroot -c example-container -- /bin/sh
19+
# kubectl attach -it example-pod-nonroot -c example-container
20+
# mint debug --runtime=k8s --namespace=default --pod=example-pod-nonroot --target=example-container
21+
# kubectl delete -f manifest_nonroot.yaml
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: example-pod-nonrootc
5+
labels:
6+
name: nginx
7+
spec:
8+
containers:
9+
- name: example-container
10+
image: cgr.dev/chainguard/nginx:latest
11+
securityContext:
12+
runAsNonRoot: true
13+
ports:
14+
- containerPort: 8080
15+
hostPort: 8080
16+
17+
# kubectl apply -f manifest_nonroot.yaml
18+
# kubectl exec -it example-pod-nonroot -c example-container -- /bin/sh
19+
# kubectl attach -it example-pod-nonroot -c example-container
20+
# mint debug --runtime=k8s --namespace=default --pod=example-pod-nonroot --target=example-container
21+
# kubectl delete -f manifest_nonroot.yaml

pkg/app/master/command/build/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func OnCommand(
214214
xc.Out.State("started")
215215

216216
if kubeOpts.HasTargetSet() {
217-
xc.Out.Info("params",
217+
xc.Out.Info("cmd.input.params",
218218
ovars{
219219
"target.type": "kubernetes.workload",
220220
"target": kubeOpts.Target.Workload,
@@ -288,7 +288,7 @@ func OnCommand(
288288
}
289289

290290
if len(composeFiles) > 0 && targetComposeSvc != "" {
291-
xc.Out.Info("params",
291+
xc.Out.Info("cmd.input.params",
292292
ovars{
293293
"target.type": "compose.service",
294294
"target": targetRef,
@@ -299,7 +299,7 @@ func OnCommand(
299299
"image-build-engine": imageBuildEngine,
300300
})
301301
} else if cbOpts.Dockerfile != "" {
302-
xc.Out.Info("params",
302+
xc.Out.Info("cmd.input.params",
303303
ovars{
304304
"target.type": "dockerfile",
305305
"context": targetRef,
@@ -310,7 +310,7 @@ func OnCommand(
310310
"image-build-engine": imageBuildEngine,
311311
})
312312
} else {
313-
xc.Out.Info("params",
313+
xc.Out.Info("cmd.input.params",
314314
ovars{
315315
"target.type": "image",
316316
"target.image": targetRef,

pkg/app/master/command/debug/cli.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ type CommandParams struct {
8383
DoRunPrivileged bool
8484
/// use the security context params from the target container with the debug sidecar container
8585
UseSecurityContextFromTarget bool
86-
/// auto-adjust the config to run as non-root (mostly for kubernetes)
87-
DoAutoRunAsNonRoot bool
86+
/// fallback to using target container user if it's non-root (mostly for kubernetes)
87+
DoFallbackToTargetUser bool
8888
}
8989

9090
func ParseNameValueList(list []string) []NVPair {
@@ -164,7 +164,7 @@ var CLI = &cli.Command{
164164
cflag(FlagGID),
165165
cflag(FlagRunPrivileged),
166166
cflag(FlagSecurityContextFromTarget),
167-
cflag(FlagAutoRunAsNonRoot),
167+
cflag(FlagFallbackToTargetUser),
168168
},
169169
Action: func(ctx *cli.Context) error {
170170
gcvalues := command.GlobalFlagValues(ctx)
@@ -207,7 +207,7 @@ var CLI = &cli.Command{
207207
GID: ctx.Int64(FlagGID),
208208
DoRunPrivileged: ctx.Bool(FlagRunPrivileged),
209209
UseSecurityContextFromTarget: ctx.Bool(FlagSecurityContextFromTarget),
210-
DoAutoRunAsNonRoot: ctx.Bool(FlagAutoRunAsNonRoot),
210+
DoFallbackToTargetUser: ctx.Bool(FlagFallbackToTargetUser),
211211
}
212212

213213
if commandParams.ActionListNamespaces &&

pkg/app/master/command/debug/debug_images.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const (
1111
NicolakaNetshootImage = "nicolaka/netshoot"
1212
KoolkitsNodeImage = "lightruncom/koolkits:node"
1313
KoolkitsPythonImage = "lightruncom/koolkits:python"
14-
KoolkitsGolangImage = "lightruncom/koolkits:golang"
14+
KoolkitsGolangImage = "mintoolkit/koolkits-golang:latest"
1515
KoolkitsJVMImage = "lightruncom/koolkits:jvm"
1616
DigitaloceanDoksImage = "digitalocean/doks-debug:latest"
1717
ZinclabsUbuntuImage = "public.ecr.aws/zinclabs/debug-ubuntu-base:latest"

pkg/app/master/command/debug/flags.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ const (
9797
FlagSecurityContextFromTarget = "security-context-from-target"
9898
FlagSecurityContextFromTargetUsage = "Use the security context params from the target container with the debug sidecar container"
9999

100-
FlagAutoRunAsNonRoot = "auto-run-as-non-root"
101-
FlagAutoRunAsNonRootUsage = "Auto-adjust the config to run as non-root (true by default)"
100+
FlagFallbackToTargetUser = "fallback-to-target-user"
101+
FlagFallbackToTargetUserUsage = "Fallback to using target container user if it's non-root (true by default)"
102102
)
103103

104104
var Flags = map[string]cli.Flag{
@@ -270,11 +270,11 @@ var Flags = map[string]cli.Flag{
270270
Usage: FlagSecurityContextFromTargetUsage,
271271
EnvVars: []string{"DSLIM_DBG_USE_TARGET_SEC_CTX"},
272272
},
273-
FlagAutoRunAsNonRoot: &cli.BoolFlag{
274-
Name: FlagAutoRunAsNonRoot,
273+
FlagFallbackToTargetUser: &cli.BoolFlag{
274+
Name: FlagFallbackToTargetUser,
275275
Value: true, //true by default
276-
Usage: FlagAutoRunAsNonRootUsage,
277-
EnvVars: []string{"DSLIM_DBG_AUTO_RUN_AS_NONROOT"},
276+
Usage: FlagFallbackToTargetUserUsage,
277+
EnvVars: []string{"DSLIM_DBG_FALLBACK_TO_TARGET_USER"},
278278
},
279279
}
280280

pkg/app/master/command/debug/handle_docker_runtime.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ func HandleDockerRuntime(
270270
exe.NetworkMode = mode
271271
exe.PidMode = mode
272272

273+
if commandParams.UID > -1 {
274+
options.User = fmt.Sprintf("%v", commandParams.UID)
275+
}
276+
277+
if commandParams.GID > -1 {
278+
options.Group = fmt.Sprintf("%v", commandParams.GID)
279+
}
280+
273281
xc.FailOn(err)
274282

275283
logger.Tracef("Debugger sidecar spec: %s", jsonutil.ToString(exe))

0 commit comments

Comments
 (0)