-
Notifications
You must be signed in to change notification settings - Fork 267
Add workload security hardening checks #1213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sthadka
wants to merge
3
commits into
main
Choose a base branch
from
security/workload-hardening-checks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+291
−4
Draft
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
pkg/builtinchecks/yamls/automount-service-account-token.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| name: automount-service-account-token | ||
| description: >- | ||
| Pod does not explicitly disable automatic mounting of the service account | ||
| token. If the workload does not call the Kubernetes API, the mounted token | ||
| is unnecessary attack surface. | ||
| remediation: >- | ||
| Set automountServiceAccountToken: false in the pod spec if the workload | ||
| does not need to interact with the Kubernetes API. | ||
| scope: | ||
| objectKinds: | ||
| - DeploymentLike | ||
| template: cel-expression | ||
| params: | ||
| check: > | ||
| (!has(object.spec.template.spec.automountServiceAccountToken) || | ||
| object.spec.template.spec.automountServiceAccountToken == true) | ||
| ? 'service account token is auto-mounted — set automountServiceAccountToken: false if not needed' : '' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| name: init-container-security-gap | ||
| description: >- | ||
| Init container does not set runAsNonRoot or readOnlyRootFilesystem while | ||
| the pod-level securityContext does. Init containers can run as root even | ||
| when main containers are hardened. | ||
| remediation: >- | ||
| Apply the same securityContext restrictions to init containers as to | ||
| regular containers. | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| scope: | ||
| objectKinds: | ||
| - DeploymentLike | ||
| template: cel-expression | ||
| params: | ||
| check: > | ||
| has(object.spec.template.spec.securityContext) && | ||
| has(object.spec.template.spec.securityContext.runAsNonRoot) && | ||
| object.spec.template.spec.securityContext.runAsNonRoot == true && | ||
| has(object.spec.template.spec.initContainers) && | ||
| object.spec.template.spec.initContainers.exists(c, | ||
| !has(c.securityContext) || | ||
| !has(c.securityContext.runAsNonRoot) || | ||
| c.securityContext.runAsNonRoot != true | ||
| ) ? 'init container may run as root despite pod-level runAsNonRoot — apply securityContext to init containers' : '' | ||
23 changes: 23 additions & 0 deletions
23
pkg/builtinchecks/yamls/projected-token-long-expiration.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| name: projected-token-long-expiration | ||
| description: >- | ||
| Projected service account token volume with expirationSeconds > 24h or | ||
| without explicit expiration. Long-lived tokens extracted from compromised | ||
| pods remain valid indefinitely. | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| remediation: >- | ||
| Set expirationSeconds to 3600 (1 hour) or less unless the workload | ||
| specifically requires longer token lifetimes. | ||
| scope: | ||
| objectKinds: | ||
| - DeploymentLike | ||
| template: cel-expression | ||
| params: | ||
| check: > | ||
| has(object.spec.template.spec.volumes) && | ||
| object.spec.template.spec.volumes.exists(v, | ||
| has(v.projected) && | ||
| v.projected.sources.exists(s, | ||
| has(s.serviceAccountToken) && | ||
| has(s.serviceAccountToken.expirationSeconds) && | ||
| s.serviceAccountToken.expirationSeconds > 86400 | ||
| ) | ||
| ) ? 'projected SA token with expirationSeconds > 24h — stolen tokens persist' : '' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: dont-fire | ||
| spec: | ||
| template: | ||
| spec: | ||
| automountServiceAccountToken: false | ||
| containers: | ||
| - name: app | ||
| image: app:v1 | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: fire-default-mount | ||
| spec: | ||
| template: | ||
| spec: | ||
| containers: | ||
| - name: app | ||
| image: app:v1 | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: fire-explicit-true | ||
| spec: | ||
| template: | ||
| spec: | ||
| automountServiceAccountToken: true | ||
| containers: | ||
| - name: app | ||
| image: app:v1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: dont-fire-no-init | ||
| spec: | ||
| template: | ||
| spec: | ||
| securityContext: | ||
| runAsNonRoot: true | ||
| containers: | ||
| - name: app | ||
| image: app:v1 | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: dont-fire-init-hardened | ||
| spec: | ||
| template: | ||
| spec: | ||
| securityContext: | ||
| runAsNonRoot: true | ||
| initContainers: | ||
| - name: init | ||
| image: init:v1 | ||
| securityContext: | ||
| runAsNonRoot: true | ||
| containers: | ||
| - name: app | ||
| image: app:v1 | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: fire-init-no-security-context | ||
| spec: | ||
| template: | ||
| spec: | ||
| securityContext: | ||
| runAsNonRoot: true | ||
| initContainers: | ||
| - name: init | ||
| image: init:v1 | ||
| containers: | ||
| - name: app | ||
| image: app:v1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: dont-fire-short-token | ||
| spec: | ||
| template: | ||
| spec: | ||
| containers: | ||
| - name: app | ||
| image: app:v1 | ||
| volumes: | ||
| - name: token | ||
| projected: | ||
| sources: | ||
| - serviceAccountToken: | ||
| expirationSeconds: 3600 | ||
| path: token | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: fire-long-token | ||
| spec: | ||
| template: | ||
| spec: | ||
| containers: | ||
| - name: app | ||
| image: app:v1 | ||
| volumes: | ||
| - name: token | ||
| projected: | ||
| sources: | ||
| - serviceAccountToken: | ||
| expirationSeconds: 604800 | ||
| path: token |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.