|
| 1 | +package runasnonroot |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "golang.stackrox.io/kube-linter/internal/check" |
| 7 | + "golang.stackrox.io/kube-linter/internal/diagnostic" |
| 8 | + "golang.stackrox.io/kube-linter/internal/extract" |
| 9 | + "golang.stackrox.io/kube-linter/internal/lintcontext" |
| 10 | + "golang.stackrox.io/kube-linter/internal/objectkinds" |
| 11 | + "golang.stackrox.io/kube-linter/internal/templates" |
| 12 | + v1 "k8s.io/api/core/v1" |
| 13 | +) |
| 14 | + |
| 15 | +func effectiveRunAsNonRoot(podSC *v1.PodSecurityContext, containerSC *v1.SecurityContext) bool { |
| 16 | + if containerSC != nil && containerSC.RunAsNonRoot != nil { |
| 17 | + return *containerSC.RunAsNonRoot |
| 18 | + } |
| 19 | + if podSC != nil && podSC.RunAsNonRoot != nil { |
| 20 | + return *podSC.RunAsNonRoot |
| 21 | + } |
| 22 | + return false |
| 23 | +} |
| 24 | + |
| 25 | +func effectiveRunAsUser(podSC *v1.PodSecurityContext, containerSC *v1.SecurityContext) *int64 { |
| 26 | + if containerSC != nil && containerSC.RunAsUser != nil { |
| 27 | + return containerSC.RunAsUser |
| 28 | + } |
| 29 | + if podSC != nil { |
| 30 | + return podSC.RunAsUser |
| 31 | + } |
| 32 | + return nil |
| 33 | +} |
| 34 | + |
| 35 | +func init() { |
| 36 | + templates.Register(check.Template{ |
| 37 | + Name: "run-as-non-root", |
| 38 | + Description: "Flag containers set to run as a root user", |
| 39 | + SupportedObjectKinds: check.ObjectKindsDesc{ |
| 40 | + ObjectKinds: []string{objectkinds.DeploymentLike}, |
| 41 | + }, |
| 42 | + Parameters: nil, |
| 43 | + Instantiate: func(_ map[string]string) (check.Func, error) { |
| 44 | + return func(_ *lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic { |
| 45 | + podSpec, found := extract.PodSpec(object.K8sObject) |
| 46 | + if !found { |
| 47 | + return nil |
| 48 | + } |
| 49 | + var results []diagnostic.Diagnostic |
| 50 | + for _, container := range podSpec.Containers { |
| 51 | + runAsUser := effectiveRunAsUser(podSpec.SecurityContext, container.SecurityContext) |
| 52 | + // runAsUser explicitly set to non-root. All good. |
| 53 | + if runAsUser != nil && *runAsUser > 0 { |
| 54 | + continue |
| 55 | + } |
| 56 | + runAsNonRoot := effectiveRunAsNonRoot(podSpec.SecurityContext, container.SecurityContext) |
| 57 | + if runAsNonRoot { |
| 58 | + // runAsNonRoot set, but runAsUser set to 0. This will result in a runtime failure. |
| 59 | + if runAsUser != nil && *runAsUser == 0 { |
| 60 | + results = append(results, diagnostic.Diagnostic{ |
| 61 | + Message: fmt.Sprintf("container %q is set to runAsNonRoot, but runAsUser set to %d", container.Name, *runAsUser), |
| 62 | + }) |
| 63 | + } |
| 64 | + continue |
| 65 | + } |
| 66 | + results = append(results, diagnostic.Diagnostic{Message: fmt.Sprintf("container %q is not set to runAsNonRoot", container.Name)}) |
| 67 | + } |
| 68 | + return results |
| 69 | + }, nil |
| 70 | + }, |
| 71 | + }) |
| 72 | +} |
0 commit comments