RoleBinding targets the default ServiceAccount

Avoid unnecessary grants to the default account and use accounts with distinct purposes.

Description

A default ServiceAccount named as a subject shares its grants with every Pod using that identity. A RoleBinding grants access in the binding’s namespace. A ClusterRoleBinding grants access across the cluster.

Potential impact

New Pods without an explicit account can receive these extra permissions. Requests under one identity make it harder to trace activity to a workload.

Remediation

Create a dedicated account and configure the workload to use it. Check the subject name and namespace, bind only the permissions the workload needs, and remove unnecessary grants from the default account. Verify both accounts’ access.

Examples

The Role named admin must exist in default. The ServiceAccount named in each example must exist in kube-system.

Before

hcl
resource "kubernetes_role_binding" "example" {
  metadata {
    name      = "terraform-example"
    namespace = "default"
  }

  role_ref {
    api_group = "rbac.authorization.k8s.io"
    kind      = "Role"
    name      = "admin"
  }

  subject {
    kind      = "ServiceAccount"
    name      = "default"
    namespace = "kube-system"
  }
}

After

hcl
resource "kubernetes_role_binding" "example" {
  metadata {
    name      = "terraform-example"
    namespace = "default"
  }

  role_ref {
    api_group = "rbac.authorization.k8s.io"
    kind      = "Role"
    name      = "admin"
  }

  subject {
    kind      = "ServiceAccount"
    name      = "service-example"
    namespace = "kube-system"
  }
}

Explanation:

  • Before: The default account in kube-system receives the admin Role.
  • After: The binding changes its subject to service-example. Check that the workload needs all the operations in admin.

References