Skip to content

Commit

Permalink
Minor fixes for NSA checks (#952)
Browse files Browse the repository at this point in the history
* fix rbac checks

* fix sensitive env var check

* add test case

* fix service account check

* fix comment

---------

Co-authored-by: Andrew Suderman <andy@fairwinds.com>
  • Loading branch information
rbren and sudermanjr authored Jun 9, 2023
1 parent 52a5aa8 commit 8bfed75
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 6 deletions.
5 changes: 2 additions & 3 deletions checks/automountServiceAccountToken.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ target: PodSpec
schema:
'$schema': http://json-schema.org/draft-07/schema
type: object
required: ["serviceAccountName"]
properties:
serviceAccountName:
type: string
Expand All @@ -15,12 +14,11 @@ schema:
const: true
additionalSchemaStrings:
ServiceAccount: |
{{ if not (eq .Polaris.PodSpec.automountServiceAccountToken false) }}
type: object
required:
- metadata
{{ if not (eq .Polaris.PodSpec.automountServiceAccountToken false) }}
- automountServiceAccountToken
{{ end }}
properties:
metadata:
type: object
Expand All @@ -34,3 +32,4 @@ additionalSchemaStrings:
type: boolean
const: false
{{ end }}
{{ end }}
2 changes: 2 additions & 0 deletions checks/clusterrolebindingClusterAdmin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ additionalSchemaStrings:
rbac.authorization.k8s.io/ClusterRole: |
type: object
# Do not alert on default ClusterRoleBindings.
{{ if (ne .roleRef.name "view") }}
{{ if and (ne .metadata.name "cluster-admin") (not (hasPrefix .metadata.name "system:")) (ne .metadata.name "gce:podsecuritypolicy:calico-sa") }}
required: ["metadata", "rules"]
allOf:
Expand Down Expand Up @@ -86,3 +87,4 @@ additionalSchemaStrings:
- "patch"
- "delete"
{{ end }}
{{ end }}
2 changes: 2 additions & 0 deletions checks/clusterrolebindingPodExecAttach.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ additionalSchemaStrings:
rbac.authorization.k8s.io/ClusterRole: |
type: object
# Do not alert on default ClusterRoleBindings.
{{ if (ne .roleRef.name "view") }}
{{ if and (ne .metadata.name "cluster-admin") (not (hasPrefix .metadata.name "system:")) (ne .metadata.name "gce:podsecuritypolicy:calico-sa") }}
required: ["metadata", "rules"]
allOf:
Expand Down Expand Up @@ -80,3 +81,4 @@ additionalSchemaStrings:
- const: 'get'
- const: 'create'
{{ end }}
{{ end }}
4 changes: 3 additions & 1 deletion checks/sensitiveContainerEnvVar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ schemaString: |
type: array
items:
type: object
oneOf:
anyOf:
- not:
required: ["value"]
- required: ["name", "value"]
properties:
name:
Expand Down
8 changes: 6 additions & 2 deletions pkg/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,15 @@ func (check SchemaCheck) TemplateForResource(res interface{}) (*SchemaCheck, err
if err != nil {
return nil, err
}
templated := w.String()
if strings.TrimSpace(templated) == "" {
continue
}

if kind == "" {
newCheck.SchemaString = w.String()
newCheck.SchemaString = templated
} else {
newCheck.AdditionalSchemaStrings[kind] = w.String()
newCheck.AdditionalSchemaStrings[kind] = templated
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This succeeds because automounting is disabled on the pod, and there is no specified service account
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
automountServiceAccountToken: false
containers:
- name: nginx-foo
image: nginx-foo
ports:
- containerPort: 80
14 changes: 14 additions & 0 deletions test/checks/insecureCapabilities/failure.add.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
securityContext:
capabilities:
add:
- ALL
14 changes: 14 additions & 0 deletions test/checks/insecureCapabilities/mutated.add.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
securityContext:
capabilities:
drop:
- ALL
1 change: 1 addition & 0 deletions test/mutation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ checks:
memoryLimitsMissing: warning
readinessProbeMissing: warning
livenessProbeMissing: warning
insecureCapabilities: warning
`

func TestMutations(t *testing.T) {
Expand Down

0 comments on commit 8bfed75

Please sign in to comment.