TExcelTAD
Home › Cloud & Container Security › Workload Identity: Instance Roles, Managed Identities and Pods

Workload Identity: Instance Roles, Managed Identities and Pods

Applications need cloud credentials too: the web app writes to a bucket, the batch job reads a queue, the operator in the cluster creates load balancers. The bad old way is a long-lived key in a config file or an environment variable. The good way is workload identity: the platform proves which workload is running and hands it short-lived credentials for its own identity, with nothing stored. That moves the security question from "where is the key?" to "which workloads can obtain which identity?". The answer is often wider than intended, especially on Kubernetes nodes, where every pod may be able to borrow the node's identity.

How each platform hands out identity

Platform Mechanism How the workload gets credentials Granularity
AWS EC2 instance profile (a role) from the instance metadata service (IMDS) per instance
AWS ECS / Lambda task role / execution role a local credentials endpoint / environment of the function per task or function
AWS EKS IRSA: a projected service-account token, exchanged via AssumeRoleWithWebIdentity; or EKS Pod Identity (an agent on the node) SDKs pick them up automatically per Kubernetes service account
Azure managed identity (system- or user-assigned) on VMs, App Service, Functions; Workload ID federation for AKS pods IMDS endpoint / federated token exchange per resource / service account
Google Cloud service account attached to a VM or service; Workload Identity Federation for GKE metadata server per VM / Kubernetes service account
CI and other clouds OIDC federation (Role Assumption) token exchange per repository / branch

In every case, the trust that decides "this pod is allowed to be app-role" is a configuration you can review. For IRSA it is the role's trust policy with a sub condition of the form system:serviceaccount:<namespace>:<service-account>; for GKE and AKS it is a binding or federated credential naming the namespace and service account. See Managed Identities and Service Accounts and Workload Identity Federation for the setup side.

The node-identity trap

On a Kubernetes node that is an EC2 instance, the node's instance role (with permissions for the kubelet, image pulls and networking) is available from IMDS. If pods can reach IMDS, every pod on the node can use the node's role, regardless of its own service-account mapping. The same holds for GKE nodes' service account when pods can reach the metadata server and for AKS nodes' kubelet identity.

The model below computes, for each pod of the lab cluster, which cloud identities it can obtain under three configurations of the same cluster.

python · try it yourself
from fnmatch import fnmatchcase

pods = [  # (namespace, pod, service account, hostNetwork)
    ("shop", "web-7d9", "web", False),
    ("shop", "worker-2c1", "worker", False),
    ("batch", "report-5f2", "default", False),
    ("kube-system", "cni-agent-x1", "aws-node", True),
    ("playground", "debug-0", "default", False),
]
# IRSA-style role mappings: role -> trust 'sub' pattern(s)
def run(title, irsa_trust, imds_reachable_by_pods, imds_hop_limit):
    print(f"== {title}")
    for ns, pod, sa, host_net in pods:
        sub = f"system:serviceaccount:{ns}:{sa}"
        ids = [role for role, pats in irsa_trust.items() if any(fnmatchcase(sub, p) for p in pats)]
        # a pod reaches IMDS if nothing blocks it; with IMDSv2 and hop limit 1 only host-network pods get a token
        if imds_reachable_by_pods and (host_net or imds_hop_limit >= 2):
            ids.append("node-role (via IMDS)")
        elif host_net:
            ids.append("node-role (via IMDS, host network)")
        print(f"   {ns + '/' + pod:26} sa={sa:9} -> {', '.join(ids) if ids else 'no cloud identity'}")

good_trust = {"shop-web-role": ["system:serviceaccount:shop:web"],
              "shop-worker-role": ["system:serviceaccount:shop:worker"]}
run("A: IRSA pinned, IMDS reachable, hop limit 2", good_trust, True, 2)
run("B: IRSA pinned, IMDSv2 required, hop limit 1", good_trust, True, 1)
loose_trust = {"shop-web-role": ["system:serviceaccount:shop:*"],
               "shop-worker-role": ["system:serviceaccount:*:worker"]}
run("C: wildcard trust, IMDSv2 required, hop limit 1", loose_trust, True, 1)
== A: IRSA pinned, IMDS reachable, hop limit 2
   shop/web-7d9               sa=web       -> shop-web-role, node-role (via IMDS)
   shop/worker-2c1            sa=worker    -> shop-worker-role, node-role (via IMDS)
   batch/report-5f2           sa=default   -> node-role (via IMDS)
   kube-system/cni-agent-x1   sa=aws-node  -> node-role (via IMDS)
   playground/debug-0         sa=default   -> node-role (via IMDS)
== B: IRSA pinned, IMDSv2 required, hop limit 1
   shop/web-7d9               sa=web       -> shop-web-role
   shop/worker-2c1            sa=worker    -> shop-worker-role
   batch/report-5f2           sa=default   -> no cloud identity
   kube-system/cni-agent-x1   sa=aws-node  -> node-role (via IMDS)
   playground/debug-0         sa=default   -> no cloud identity
== C: wildcard trust, IMDSv2 required, hop limit 1
   shop/web-7d9               sa=web       -> shop-web-role
   shop/worker-2c1            sa=worker    -> shop-web-role, shop-worker-role
   batch/report-5f2           sa=default   -> no cloud identity
   kube-system/cni-agent-x1   sa=aws-node  -> node-role (via IMDS)
   playground/debug-0         sa=default   -> no cloud identity
  • A looks fine on paper (each application service account maps to exactly one role) but every pod, including the playground debug pod and the batch job using the default service account, can also take the node role. On EKS the node role can typically pull every image in the account's registries, describe network interfaces and more. Hop limit 2 is not exotic: EC2 gives it by default to instances launched from AMIs registered with ImdsSupport: v2.0, so check what your node AMIs and launch templates actually set.
  • B fixes that with the node setting: IMDSv2 required and a hop limit of 1, so the token response can't travel the extra network hop into a pod's network namespace. Only host-network system pods still reach it, which is expected (they are part of the node).
  • C brings the problem back through trust policies: shop:* gives shop-web-role to every service account in shop, and *:worker gives shop-worker-role to a worker service account in any namespace, so anyone allowed to create a service account named worker in the playground namespace gets it.

How it is detected

  • Inventory the mappings: every IRSA role's trust sub, every Pod Identity association, every GKE/AKS federated binding. Flag wildcards in namespace or service-account names.
  • Check the node side: EC2 launch templates for http_tokens = "required" and http_put_response_hop_limit = 1; GKE node pools for the GKE metadata server; network policies or host firewall rules blocking 169.254.169.254 from pods.
  • In CloudTrail: AssumeRoleWithWebIdentity events whose subject is an unexpected service account; node-role credentials used for calls the node never makes (s3:GetObject on application buckets, iam:*). GuardDuty has findings for instance credentials used from another account or from outside AWS (the IMDS section).
  • Look for keys anyway: GCP service-account keys (iam.serviceAccountKeys.list), Azure service principals with client secrets, AWS access keys in Kubernetes Secrets. Workload identity only helps if the old keys are gone.

How it is fixed

  • One cloud identity per workload, mapped to one exact namespace and service account; no wildcards.
  • Block pod access to node metadata: IMDSv2 with hop limit 1 (or blocking the address with a NetworkPolicy/host rule), GKE's metadata server, AKS's guidance for Workload ID.
  • Give the node role only what nodes need, and never application permissions "for convenience".
  • Disable key creation where the platform allows it (GCP organisation policy iam.disableServiceAccountKeyCreation), and alert on new keys.
  • Control who may create service accounts and pods in each namespace (RBAC Review): a workload identity mapping is only as strong as the right to run a pod under that service account.

Pitfalls

  • Treating the default service account as harmless: if a role trusts system:serviceaccount:batch:default, every pod in batch that doesn't name a service account gets the role.
  • Forgetting that anyone who can create a pod in a namespace can choose any service account in it, and so any cloud identity mapped to those service accounts.
  • Hop limit 1 on the instance but a pod with hostNetwork: true that doesn't need it.

Practice

1. In configuration C, which pods get shop-web-role? Fix the trust so only shop/web does.

shop/web-7d9 and shop/worker-2c1 (both in shop). Use the exact system:serviceaccount:shop:web with StringEquals, plus an aud condition of sts.amazonaws.com.

2. Add a pod ("playground", "x", "worker", False) and run configuration C. What does it get, and what Kubernetes permission would an attacker need?

It gets shop-worker-role. The attacker needs only the right to create a service account named worker and a pod in playground, which developers often have in sandbox namespaces.

3. Why doesn't hop limit 1 affect the CNI agent pod?

It uses hostNetwork: true, so it shares the node's network namespace: there is no extra hop, and the token response reaches it like any process on the node.

Test yourself

In configuration A, why could the playground debug pod obtain cloud credentials?

  1. It had an IRSA mapping
  2. Pods could reach IMDS with hop limit 2, so every pod could use the node role
  3. It ran as root
  4. Playground namespaces are trusted

What does an IRSA trust condition sub = system:serviceaccount:*:worker allow?

  1. Only shop/worker
  2. A service account named worker in any namespace
  3. No one
  4. Only kube-system

Why is the right to create pods in a namespace security-relevant for workload identity?

  1. Pods are expensive
  2. A pod can run under any service account in its namespace and so obtain the cloud identity mapped to it
  3. Pods can change IAM policies
  4. It isn't