...
1
2
3
4
19
20 package kuberuntime
21
22 import (
23 "fmt"
24
25 "k8s.io/api/core/v1"
26 "k8s.io/kubernetes/pkg/kubelet/util/format"
27 "k8s.io/kubernetes/pkg/securitycontext"
28 )
29
30
31 func verifyRunAsNonRoot(pod *v1.Pod, container *v1.Container, uid *int64, username string) error {
32 effectiveSc := securitycontext.DetermineEffectiveSecurityContext(pod, container)
33
34 if effectiveSc == nil || effectiveSc.RunAsNonRoot == nil || !*effectiveSc.RunAsNonRoot {
35 return nil
36 }
37
38 if effectiveSc.RunAsUser != nil {
39 if *effectiveSc.RunAsUser == 0 {
40 return fmt.Errorf("container's runAsUser breaks non-root policy (pod: %q, container: %s)", format.Pod(pod), container.Name)
41 }
42 return nil
43 }
44
45 switch {
46 case uid != nil && *uid == 0:
47 return fmt.Errorf("container has runAsNonRoot and image will run as root (pod: %q, container: %s)", format.Pod(pod), container.Name)
48 case uid == nil && len(username) > 0:
49 return fmt.Errorf("container has runAsNonRoot and image has non-numeric user (%s), cannot verify user is non-root (pod: %q, container: %s)", username, format.Pod(pod), container.Name)
50 default:
51 return nil
52 }
53 }
54
View as plain text