Review ServiceAccount access to Secrets

Limit Secret-reading permissions bound to a ServiceAccount to what the workload needs.

Description

A Role or ClusterRole bound to a ServiceAccount can disclose Secret values through any of get, list or watch on secrets. With verbs = ["*"], the account can also change or delete Secrets.

Potential impact

A compromised workload using that account can expose passwords, tokens or keys that grant access to other systems.

Remediation

If the app calls the Secrets API, use a dedicated account and limit the namespaces, resources and operations it can access. A Pod receiving Secrets through a volume or environment variable needs no Secret-read grant for that delivery. Verify the account’s effective access, including other bindings.

Examples

Check that the Role is created in default, the RoleBinding’s namespace. The subject is the default ServiceAccount in kube-system.

Before

hcl
resource "kubernetes_role" "role_name" {
  metadata {
    name = "terraform-example"
  }

  rule {
    api_groups = [""]
    resources  = ["secrets"]
    verbs      = ["*"]
  }
}

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

  role_ref {
    api_group = "rbac.authorization.k8s.io"
    kind      = "Role"
    name      = kubernetes_role.role_name.metadata[0].name
  }

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

After

hcl
resource "kubernetes_role" "role_name" {
  metadata {
    name = "terraform-example"
  }

  rule {
    api_groups = [""]
    resources  = ["pods"]
    verbs      = ["get", "list", "watch"]
  }
}

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

  role_ref {
    api_group = "rbac.authorization.k8s.io"
    kind      = "Role"
    name      = kubernetes_role.role_name.metadata[0].name
  }

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

Explanation:

  • Before: This account can perform every operation on Secrets in the default namespace.
  • After: The Role now allows only get, list and watch on pods. Grant Pod access only if the workload needs it.

References