Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
Add a new command to audit runAsUser fields (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbbc authored Feb 4, 2021
1 parent d9470c4 commit 7468490
Show file tree
Hide file tree
Showing 28 changed files with 543 additions and 32 deletions.
5 changes: 5 additions & 0 deletions auditors/nonroot/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func (f *fixRunAsNonRoot) Apply(resource k8stypes.Resource) []k8stypes.Resource
if f.container.SecurityContext == nil {
f.container.SecurityContext = &k8stypes.SecurityContextV1{}
}

if f.container.SecurityContext.RunAsUser != nil && *f.container.SecurityContext.RunAsUser == 0 {
f.container.SecurityContext.RunAsUser = nil
}

f.container.SecurityContext.RunAsNonRoot = k8s.NewTrue()
return nil
}
52 changes: 39 additions & 13 deletions auditors/nonroot/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ func TestFixRunAsNonRoot(t *testing.T) {
{"run-as-non-root-psc-false-csc-nil-multiple-cont.yml", fixtureDir, k8s.NewTrue()},
{"run-as-non-root-psc-false-csc-true-multiple-cont.yml", fixtureDir, k8s.NewTrue()},
{"run-as-non-root-psc-false-allowed-multi-containers-single-label.yml", fixtureDir, k8s.NewTrue()},
{"run-as-user-0.yml", fixtureDir, k8s.NewTrue()},
{"run-as-user-0-allowed.yml", fixtureDir, nil},
{"run-as-user-psc-0.yml", fixtureDir, k8s.NewTrue()},
{"run-as-user-psc-0-allowed.yml", fixtureDir, nil},
{"run-as-user-psc-1.yml", fixtureDir, nil},
{"run-as-user-psc-1-csc-0.yml", fixtureDir, k8s.NewTrue()},
{"run-as-user-psc-0-csc-0.yml", fixtureDir, k8s.NewTrue()},
{"run-as-user-psc-0-csc-1.yml", fixtureDir, nil},
{"run-as-user-redundant-override-container.yml", fixtureDir, nil},
{"run-as-user-redundant-override-pod.yml", fixtureDir, nil},
{"run-as-user-psc-0-csc-1-multiple-cont.yml", fixtureDir, nil},
{"run-as-user-psc-0-allowed-multi-containers-multi-labels.yml", fixtureDir, nil},
{"run-as-user-psc-0-allowed-multi-containers-single-label.yml", fixtureDir, nil},
{"run-as-user-0-run-as-non-root-true.yml", fixtureDir, k8s.NewTrue()},
{"run-as-user-0-run-as-non-root-false.yml", fixtureDir, k8s.NewTrue()},
{"run-as-user-psc-0-run-as-non-root-psc-true.yml", fixtureDir, k8s.NewTrue()},
{"run-as-user-psc-0-run-as-non-root-psc-false.yml", fixtureDir, k8s.NewTrue()},
{"run-as-user-1-run-as-non-root-true.yml", fixtureDir, k8s.NewTrue()},
{"run-as-user-1-run-as-non-root-false.yml", fixtureDir, k8s.NewFalse()},
{"run-as-user-psc-1-run-as-non-root-psc-true.yml", fixtureDir, nil},
{"run-as-user-psc-1-run-as-non-root-psc-false.yml", fixtureDir, nil},
}

for _, tc := range cases {
Expand All @@ -45,19 +66,24 @@ func TestFixRunAsNonRoot(t *testing.T) {
})
}

file := "run-as-non-root-psc-false-allowed-multi-containers-multi-labels.yml"
t.Run(file, func(t *testing.T) {
resources, _ := test.FixSetup(t, fixtureDir, file, New())
for _, resource := range resources {
containers := k8s.GetContainers(resource)
for _, container := range containers {
switch container.Name {
case "fakeContainerRANR":
assert.True(t, (container.SecurityContext == nil || container.SecurityContext.RunAsNonRoot == nil))
case "fakeContainerRANR2":
assert.True(t, *container.SecurityContext.RunAsNonRoot)
files := []string {
"run-as-non-root-psc-false-allowed-multi-containers-multi-labels.yml",
"run-as-user-psc-0-csc-nil-multiple-cont.yml",
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
resources, _ := test.FixSetup(t, fixtureDir, file, New())
for _, resource := range resources {
containers := k8s.GetContainers(resource)
for _, container := range containers {
switch container.Name {
case "container1":
assert.True(t, (container.SecurityContext == nil || container.SecurityContext.RunAsNonRoot == nil))
case "container2":
assert.True(t, *container.SecurityContext.RunAsNonRoot)
}
}
}
}
})
})
}
}
21 changes: 21 additions & 0 deletions auditors/nonroot/fixtures/run-as-user-0-allowed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
namespace: run-as-user-0-allowed
spec:
selector:
matchLabels:
name: deployment
template:
metadata:
labels:
name: deployment
audit.kubernetes.io/pod.allow-run-as-root: "SuperuserPrivilegesNeeded"
spec:
containers:
- name: container
image: scratch
securityContext:
runAsUser: 0
20 changes: 20 additions & 0 deletions auditors/nonroot/fixtures/run-as-user-0-run-as-non-root-false.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
namespace: run-as-user-0-run-as-non-root-false
spec:
selector:
matchLabels:
name: deployment
template:
metadata:
labels:
name: deployment
spec:
containers:
- name: container
image: scratch
securityContext:
runAsUser: 0
runAsNonRoot: false
20 changes: 20 additions & 0 deletions auditors/nonroot/fixtures/run-as-user-0-run-as-non-root-true.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
namespace: run-as-user-0-run-as-non-root-true
spec:
selector:
matchLabels:
name: deployment
template:
metadata:
labels:
name: deployment
spec:
containers:
- name: container
image: scratch
securityContext:
runAsUser: 0
runAsNonRoot: true
19 changes: 19 additions & 0 deletions auditors/nonroot/fixtures/run-as-user-0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
namespace: run-as-user-0
spec:
selector:
matchLabels:
name: deployment
template:
metadata:
labels:
name: deployment
spec:
containers:
- name: container
image: scratch
securityContext:
runAsUser: 0
20 changes: 20 additions & 0 deletions auditors/nonroot/fixtures/run-as-user-1-run-as-non-root-false.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
namespace: run-as-user-1-run-as-non-root-false
spec:
selector:
matchLabels:
name: deployment
template:
metadata:
labels:
name: deployment
spec:
containers:
- name: container
image: scratch
securityContext:
runAsUser: 1
runAsNonRoot: false
20 changes: 20 additions & 0 deletions auditors/nonroot/fixtures/run-as-user-1-run-as-non-root-true.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
namespace: run-as-user-1-run-as-non-root-true
spec:
selector:
matchLabels:
name: deployment
template:
metadata:
labels:
name: deployment
spec:
containers:
- name: container
image: scratch
securityContext:
runAsUser: 1
runAsNonRoot: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
labels:
name: pod
container.audit.kubernetes.io/container1.allow-run-as-root: "SuperuserPrivilegesNeeded"
container.audit.kubernetes.io/container2.allow-run-as-root: "SuperuserPrivilegesNeeded"
namespace: run-as-user-psc-0-allowed-multi-containers-multi-labels
spec:
securityContext:
runAsUser: 0
containers:
- name: container1
image: scratch
- name: container2
image: scratch
securityContext:
runAsUser: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
labels:
name: pod
container.audit.kubernetes.io/container2.allow-run-as-root: "SuperuserPrivilegesNeeded"
namespace: run-as-user-psc-0-allowed-multi-containers-single-label
spec:
securityContext:
runAsUser: 0
containers:
- name: container1
image: scratch
securityContext:
runAsUser: 1
- name: container2
image: scratch
securityContext:
runAsUser: 0
14 changes: 14 additions & 0 deletions auditors/nonroot/fixtures/run-as-user-psc-0-allowed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
labels:
name: pod
audit.kubernetes.io/pod.allow-run-as-root: "SuperuserPrivilegesNeeded"
namespace: run-as-user-psc-0-allowed
spec:
securityContext:
runAsUser: 0
containers:
- name: container
image: scratch
13 changes: 13 additions & 0 deletions auditors/nonroot/fixtures/run-as-user-psc-0-csc-0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
namespace: run-as-user-psc-0-csc-0
spec:
securityContext:
runAsUser: 0
containers:
- name: container
image: scratch
securityContext:
runAsUser: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
namespace: run-as-user-psc-0-csc-1-multiple-cont
spec:
securityContext:
runAsUser: 0
containers:
- name: container1
image: scratch
securityContext:
runAsUser: 1
- name: container2
image: scratch
securityContext:
runAsUser: 1
13 changes: 13 additions & 0 deletions auditors/nonroot/fixtures/run-as-user-psc-0-csc-1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
namespace: run-as-user-psc-0-csc-1
spec:
securityContext:
runAsUser: 0
containers:
- name: container
image: scratch
securityContext:
runAsUser: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
namespace: run-as-user-psc-0-csc-nil-multiple-cont
spec:
securityContext:
runAsUser: 0
containers:
- name: container1
image: scratch
securityContext:
runAsUser: 1
- name: container2
image: scratch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
labels:
name: pod
namespace: run-as-user-psc-0-run-as-non-root-psc-false
spec:
securityContext:
runAsUser: 0
runAsNonRoot: false
containers:
- name: container
image: scratch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
labels:
name: pod
namespace: run-as-user-psc-0-run-as-non-root-psc-true
spec:
securityContext:
runAsUser: 0
runAsNonRoot: true
containers:
- name: container
image: scratch
13 changes: 13 additions & 0 deletions auditors/nonroot/fixtures/run-as-user-psc-0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
labels:
name: pod
namespace: run-as-user-psc-0
spec:
securityContext:
runAsUser: 0
containers:
- name: container
image: scratch
13 changes: 13 additions & 0 deletions auditors/nonroot/fixtures/run-as-user-psc-1-csc-0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
namespace: run-as-user-psc-1-csc-0
spec:
securityContext:
runAsUser: 1
containers:
- name: container
image: scratch
securityContext:
runAsUser: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
labels:
name: pod
namespace: run-as-user-psc-1-run-as-non-root-psc-false
spec:
securityContext:
runAsUser: 1
runAsNonRoot: false
containers:
- name: container
image: scratch
Loading

0 comments on commit 7468490

Please sign in to comment.