Skip to content

Commit

Permalink
feat: use variables in M-113
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusfm committed Mar 12, 2024
1 parent 65f1003 commit 9c3a65f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
31 changes: 17 additions & 14 deletions internal/builtins/pss/restricted/M-113_run_as_non_root.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,23 @@ match:
- group: batch
version: v1
resource: jobs
validations:
# Pod or Containers must set `securityContext.runAsNonRoot`
- expression: >
(has(podSpec.securityContext) && has(podSpec.securityContext.runAsNonRoot)) ||
allContainers.all(container,
has(container.securityContext) && has(container.securityContext.runAsNonRoot)
variables:
- name: podRunAsNonRoot
expression: podSpec.?securityContext.?runAsNonRoot.orValue(false)

# containers that explicitly set runAsNonRoot=false
- name: explicitlyBadContainers
expression: >
allContainers.filter(c,
has(c.securityContext) && has(c.securityContext.runAsNonRoot) && c.securityContext.runAsNonRoot == false
)
message: "Container could be running as root user"
# Neither Pod nor Containers should set `securityContext.runAsNonRoot` to false
- expression: >
(!has(podSpec.securityContext) || !has(podSpec.securityContext.runAsNonRoot) || podSpec.securityContext.runAsNonRoot != false)
&&
allContainers.all(container,
!has(container.securityContext) || !has(container.securityContext.runAsNonRoot) || container.securityContext.runAsNonRoot != false
# containers that didn't set runAsNonRoot and aren't caught by a pod-level runAsNonRoot=true
- name: implicitlyBadContainers
expression: >
allContainers.filter(c,
!variables.podRunAsNonRoot && (!has(c.securityContext) || !has(c.securityContext.runAsNonRoot))
)
message: "Container could be running as root user"
validations:
- expression: variables.explicitlyBadContainers.size() == 0 && variables.implicitlyBadContainers.size() == 0
34 changes: 27 additions & 7 deletions internal/builtins/pss/restricted/M-113_run_as_non_root_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

- name: "securityContext not specified"
pass: false
message: "Container could be running as root user"
input: |
apiVersion: apps/v1
kind: Deployment
Expand All @@ -34,9 +33,31 @@
matchLabels:
app: nginx
- name: "Pod explicitly set runAsNonRoot to false and Container to true"
- name: "Pod set runAsNonRoot to false"
pass: false
message: "Container could be running as root user"
input: |
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
template:
metadata:
name: nginx
labels:
app: nginx
spec:
securityContext:
runAsNonRoot: false
containers:
- name: nginx
image: nginx
selector:
matchLabels:
app: nginx
- name: "Pod set runAsNonRoot to false and container to true"
pass: true
input: |
apiVersion: apps/v1
kind: Deployment
Expand All @@ -60,9 +81,8 @@
matchLabels:
app: nginx
- name: "Container explicitly set runAsNonRoot to false and Pod to true"
- name: "container set runAsNonRoot to false and Pod to true"
pass: false
message: "Container could be running as root user"
input: |
apiVersion: apps/v1
kind: Deployment
Expand All @@ -86,7 +106,7 @@
matchLabels:
app: nginx
- name: "Container set runAsNonRoot to true and Pod not specified"
- name: "container set runAsNonRoot to true and Pod not specified"
pass: true
input: |
apiVersion: apps/v1
Expand All @@ -109,7 +129,7 @@
matchLabels:
app: nginx
- name: "Pod set runAsNonRoot to true and Container not specified"
- name: "Pod set runAsNonRoot to true and container not specified"
pass: true
input: |
apiVersion: apps/v1
Expand Down

0 comments on commit 9c3a65f

Please sign in to comment.