Skip to content

Commit c0ab928

Browse files
committed
Add option to pass in uid
1 parent 40ba69b commit c0ab928

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
var image string
2929
var Version string
30+
var containerUser int
3031

3132
func main() {
3233

@@ -50,6 +51,7 @@ func main() {
5051
}
5152

5253
rootCmd.Flags().StringVarP(&image, "image", "i", "alpine", "Image to mount job to")
54+
rootCmd.Flags().IntVarP(&containerUser, "container-user", "u", 0, "User ID to run the container as")
5355
kubeConfigFlags.AddFlags(rootCmd.Flags())
5456

5557
if err := rootCmd.Execute(); err != nil {
@@ -119,6 +121,7 @@ func browseCommand(kubeConfigFlags *genericclioptions.ConfigFlags, pvcName strin
119121
cmd: []string{"/bin/sh", "-c", "--"},
120122
args: commandArgs,
121123
node: node,
124+
user: int64(containerUser),
122125
}
123126

124127
// Build the Job

src/util.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type PodOptions struct {
1313
cmd []string
1414
args []string
1515
node string
16+
user int64
1617
}
1718

1819
var script = `
@@ -56,6 +57,29 @@ func buildPvcbGetJob(options PodOptions) *batchv1.Job {
5657
options.args = []string{script}
5758
}
5859

60+
// Setup SecurityContext
61+
var allowPrivilegeEscalation bool
62+
var runAsNonRoot bool
63+
if options.user == 0 {
64+
runAsNonRoot = false
65+
allowPrivilegeEscalation = true
66+
} else {
67+
runAsNonRoot = true
68+
allowPrivilegeEscalation = false
69+
}
70+
71+
securityContext := corev1.SecurityContext{
72+
RunAsUser: &options.user,
73+
RunAsNonRoot: &runAsNonRoot,
74+
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
75+
Capabilities: &corev1.Capabilities{
76+
Drop: []corev1.Capability{"ALL"},
77+
},
78+
SeccompProfile: &corev1.SeccompProfile{
79+
Type: "RuntimeDefault",
80+
},
81+
}
82+
5983
TTLSecondsAfterFinished := new(int32)
6084
*TTLSecondsAfterFinished = 10
6185

@@ -78,10 +102,11 @@ func buildPvcbGetJob(options PodOptions) *batchv1.Job {
78102
NodeName: options.node,
79103
Containers: []corev1.Container{
80104
{
81-
Name: "browser",
82-
Image: image,
83-
Command: options.cmd,
84-
Args: options.args,
105+
Name: "browser",
106+
Image: image,
107+
Command: options.cmd,
108+
Args: options.args,
109+
SecurityContext: &securityContext,
85110
Env: []corev1.EnvVar{
86111
{
87112
Name: "PS1",

0 commit comments

Comments
 (0)