For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/kubernetes-shared-service-account.md. A documentation index is available at /llms.txt.
This product is not supported for your selected Datadog site. ().

Metadata

Id: kubernetes-shared-service-account

Platform: Kubernetes

Severity: Medium

Category: Secret Management

Learn More

Description

A service account token is shared between workloads. The rule detects distinct workloads in the same namespace that reference the same serviceAccountName in their spec. Copies of one workload in several files, such as per-environment overlays, count as a single workload, and the default service account is left to the dedicated default service account rules. Sharing a service account increases the blast radius and can violate least-privilege principles by causing credentials and permissions to be shared across workloads.

Compliant Code Examples

apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  serviceAccountName : service1
  containers:
  - name: mycontainer
    image: redis
---
apiVersion: v1
kind: Pod
metadata:
  name: pod2
spec:
  serviceAccountName : service2
  containers:
  - name: envars-test-container
    image: nginx
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      serviceAccountName: web
      containers:
      - name: web
        image: nginx:1.27
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      serviceAccountName: web
      containers:
      - name: web
        image: nginx:1.27
---
apiVersion: v1
kind: Pod
metadata:
  name: debug-a
spec:
  serviceAccountName: default
  containers:
  - name: shell
    image: busybox:1.36
---
apiVersion: v1
kind: Pod
metadata:
  name: debug-b
spec:
  serviceAccountName: default
  containers:
  - name: shell
    image: busybox:1.36

Non-Compliant Code Examples

apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  serviceAccountName : service1
  containers:
  - name: mycontainer
    image: redis
---
apiVersion: v1
kind: Pod
metadata:
  name: pod2
spec:
  serviceAccountName : service1
  containers:
  - name: envars-test-container
    image: nginx