Skip to content
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

Added policy: deny-default-service-accounts #1117

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions other/deny-default-service-accounts/.chainsaw-test/badpod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: Pod
metadata:
name: badpod01
spec:
serviceAccountName: default
containers:
- name: badpod01
image: dummyimagename
---
apiVersion: v1
kind: Pod
metadata:
name: badpod02
spec:
serviceAccountName: default
containers:
- image: dummyimagename
name: badpod02

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: deny-default-service-accounts
status:
ready: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
creationTimestamp: null
name: deny-default-service-accounts
spec:
steps:
- name: step-01
try:
- create:
resource:
apiVersion: v1
kind: ServiceAccount
metadata:
name: custom-service-account
- apply:
file: ../deny-default-service-accounts.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: deny-default-service-accounts
spec:
validationFailureAction: Enforce
- assert:
file: chainsaw-step-01-assert-1.yaml
- name: step-02
try:
- apply:
file: goodpod.yaml
- apply:
expect:
- check:
($error != null): true
file: badpod.yaml
19 changes: 19 additions & 0 deletions other/deny-default-service-accounts/.chainsaw-test/goodpod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Pod
metadata:
name: goodpod01
spec:
serviceAccountName: custom-service-account
containers:
- image: nginx
name: goodpod01
---
apiVersion: v1
kind: Pod
metadata:
name: goodpod02
spec:
serviceAccountName: custom-service-account
containers:
- image: nginx
name: goodpod02
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Pod
metadata:
name: goodpod01
spec:
serviceAccountName: custom-service-account
containers:
- name: goodpod01
image: dummyimage
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: kyverno-test.yaml
policies:
- ../deny-default-service-accounts.yaml
resources:
- resources.yaml
results:
- kind: Pod
policy: deny-default-service-accounts
resources:
- goodpod01
result: pass
rule: deny-default-service-accounts
- kind: Pod
policy: deny-default-service-accounts
resources:
- goodpod02
result: pass
rule: deny-default-service-accounts
- kind: Pod
policy: deny-default-service-accounts
resources:
- badpod01
result: fail
rule: deny-default-service-accounts
- kind: Pod
policy: deny-default-service-accounts
resources:
- badpod02
result: fail
rule: deny-default-service-accounts
30 changes: 30 additions & 0 deletions other/deny-default-service-accounts/.kyverno-test/policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: deny-default-service-accounts
annotations:
policies.kyverno.io/title: Deny using default service accounts
policies.kyverno.io/category: Other
policies.kyverno.io/subject: Pod
kyverno.io/kyverno-version: 1.11.0
policies.kyverno.io/minversion: 1.10.0
kyverno.io/kubernetes-version: "1.26"
policies.kyverno.io/description: >-
For an enhnaced security posture, it is recommended to use specific service accounts
and not the default service accounts. These service accounts provide an identity for
processes that run in individual Pods and map them to a ServiceAccount object.
This policy flags the Pods that use any default service accounts.
spec:
validationFailureAction: audit
background: false
rules:
- name: deny-default-service-accounts
match:
resources:
kinds:
- Pod
validate:
message: "Default service accounts are not allowed to be used."
pattern:
spec:
serviceAccountName: "!default"
39 changes: 39 additions & 0 deletions other/deny-default-service-accounts/.kyverno-test/resources.yaml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test for disabling of service account altogether.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also recommend testing a few Pod controllers (like Deployment and Job) to ensure this works with autogen as expected.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: v1
kind: Pod
metadata:
name: goodpod01
spec:
serviceAccountName: custom-service-account01
containers:
- name: goodpod01
image: dummyimage
---
apiVersion: v1
kind: Pod
metadata:
name: goodpod02
spec:
serviceAccountName: custom-service-account02
containers:
- name: goodpod02
image: dummyimage
---
apiVersion: v1
kind: Pod
metadata:
name: badpod01
spec:
serviceAccountName: default
containers:
- name: badpod01
image: dummyimage
---
apiVersion: v1
kind: Pod
metadata:
name: badpod02
spec:
serviceAccountName: default
containers:
- name: badpod02
image: dummyimage
Comment on lines +31 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant. Remove.

22 changes: 22 additions & 0 deletions other/deny-default-service-accounts/artifacthub-pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: deny-force-delete
version: 1.0.0
displayName: Deny using Default Service Accounts
createdAt: "2024-08-05T10:30:02.000Z"
description: >-
It is recommended to use specific service accounts and not the default service accounts. These service accounts provide an identity for that run in individual Pods and map them to a ServiceAccount object. This policy flags the Pods that use any default service accounts.
install: |-
```shell
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/other/deny-default-service-accounts//deny-default-service-accounts.yaml
```
keywords:
- kyverno
- Other
readme: |
Resources are not allowed to be deployed with default service accounts.

Refer to the documentation for more details on Kyverno annotations: https://artifacthub.io/docs/topics/annotations/kyverno/
annotations:
kyverno/category: "Other"
kyverno/kubernetesVersion: "1.28"
kyverno/subject: "Pod"
digest: cdb972732797f3434f4c0fa03386167130e612a1f5fafceb6fc28e885df9dc62
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: deny-default-service-accounts
annotations:
policies.kyverno.io/title: Deny using default service accounts
policies.kyverno.io/category: Other
policies.kyverno.io/subject: Pod
kyverno.io/kyverno-version: 1.11.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test on latest.

policies.kyverno.io/minversion: 1.10.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.

kyverno.io/kubernetes-version: "1.26"
policies.kyverno.io/description: >-
For an enhnaced security posture, it is recommended to use specific service accounts
and not the default service accounts. These service accounts provide an identity for
processes that run in individual Pods and map them to a ServiceAccount object.
This policy prevents use of the default service account in Pods.
spec:
validationFailureAction: audit
background: false
rules:
- name: deny-default-service-accounts
match:
resources:
kinds:
- Pod
Comment on lines +22 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any or all, please.

validate:
message: "Default service accounts are not allowed to be used."
pattern:
spec:
(serviceAccountName): "!default"
Loading