Smarter Transitions to Service Accounts
Smarter’s production pods on Amazon EKS now get their AWS permissions from IAM Roles for Service Accounts (IRSA), and their Kubernetes permissions from a Kubernetes ServiceAccount. Not one of them holds an AWS access key anymore. A week ago, in Running Smarter in Production on Amazon EKS, I wrote that the app’s AWS identity was “the part I’d change first”. It’s changed.
That sentence probably means a lot to some readers and very little to others. So this post explains the pieces from the ground up, namely IAM, Kubernetes ServiceAccounts, RBAC and IRSA, then shows what Smarter’s pods can and can’t do now. Most of all, it compares what an attacker could do with a stolen credential under the old arrangement and under the new one, because that comparison is the whole reason for the change.
What changed, at a glance
| Before | Now | |
|---|---|---|
| AWS credentials in the pods | One long-lived access key, in every pod’s environment | None. Each pod gets temporary credentials for an IAM role |
| Who else used that key | The deploy workflow | Nobody. Each use has its own identity |
| Kubernetes identity of the pods | The IAM user behind the key | The pod’s own ServiceAccount |
| Scope | Broad, shared by everything | One environment, and only what Smarter does |
| Lifetime of a stolen credential | Until someone notices and revokes it | An hour for the credentials, a day at most for the token they come from |
IAM in one minute
AWS Identity and Access Management (IAM) decides who may do what in an AWS account. It has three ideas that matter here.
- A policy is a list of allowed actions on resources, such as “create records in this DNS zone” or “upload files to this bucket”. A policy can also add conditions, like “only if the request carries this tag”.
- An IAM user is a long-lived identity, usually for a person or a program outside AWS. It authenticates with an access key, a pair of strings that works from any computer on the internet until someone deletes it.
- An IAM role is an identity that nobody logs in as. Someone the role trusts asks AWS to assume it, and AWS answers with temporary credentials that expire on their own, an hour later by default.
The old arrangement was an IAM user with an access key. The new one is a set of roles.
Kubernetes ServiceAccounts and RBAC
Kubernetes has its own identities and its own permissions, separate from AWS.
A ServiceAccount is the identity of a program running in the cluster, the way a user account is the identity of a person. Every pod runs as one. Kubernetes mounts a short-lived token for it into the pod, and the pod presents that token whenever it talks to the Kubernetes API.
RBAC (role-based access control) decides what that identity may do. It has four kinds of objects.
- A Role lists the actions allowed on kinds of resources in one namespace, e.g. “create, update and delete ingresses”.
- A RoleBinding grants a Role to a ServiceAccount, a user or a group, in that namespace only.
- A ClusterRole and a ClusterRoleBinding do the same, cluster-wide.
A namespace is a fence inside the cluster. Smarter’s cluster is shared by its production environment, its local development namespace and unrelated workloads, so the fence matters.
Smarter runs kubectl to manage the Kubernetes resources behind its features. Deploying an LLMClient creates an ingress and a TLS certificate. An LLMHost runs a model server, and a vectorstore runs a database. So the app’s ServiceAccount, named smarter, gets a Role in its own namespace with exactly those resources, and nothing else. This is part of it, from the Helm chart’s rbac.yaml.
rules:
- apiGroups: ["networking.k8s.io"]
resources: ["ingresses"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- apiGroups: ["cert-manager.io"]
resources: ["certificates"]
verbs: ["get", "list", "watch", "delete"]
- apiGroups: [""]
resources: ["pods", "pods/log", "events"]
verbs: ["get", "list", "watch"]
A separate, read-only ClusterRole lets it see the cluster’s nodes and persistent volumes, which the cluster status pages and the infrastructure inventory show. It can’t change either one.
How IRSA connects the two
So far the pod has a Kubernetes identity, and AWS knows nothing about it. IRSA is the bridge, and it works without any secret stored anywhere.
- Every EKS cluster runs an OpenID Connect (OIDC) issuer, a public endpoint that vouches for the tokens the cluster signs. The AWS account is told to trust that issuer.
- Each IAM role says exactly which ServiceAccount may assume it. Smarter’s production role trusts only the ServiceAccount
smarterin the production namespace. - The ServiceAccount carries the role’s name in an annotation.
- When a pod starts with that ServiceAccount, an EKS component called the pod identity webhook adds two environment variables, the role’s ARN and the path of a token file, and mounts a token signed by the cluster.
- Boto3, AWS’s Python SDK, finds those variables, sends the token to AWS’s Security Token Service, and gets back temporary credentials for the role. It renews them before they expire, and the kubelet renews the token.
Step 3 is three lines in the chart’s serviceaccount.yaml.
{{- with .Values.serviceAccount.awsRoleArn }}
annotations:
eks.amazonaws.com/role-arn: "{{ . }}"
{{- end }}
Step 2 is a Terraform module in smarter-infrastructure, one per environment.
module "smarter_irsa" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
role_name = "${local.environment_namespace}-smarter-irsa"
provider_url = local.oidc_provider_url
role_policy_arns = [aws_iam_policy.smarter_pods.arn]
oidc_fully_qualified_subjects = ["system:serviceaccount:${local.environment_namespace}:smarter"]
}
Notice what’s missing. There’s no key in a Kubernetes Secret, no key in GitHub, no key in a developer’s .env. The only thing the pod holds is a token that AWS accepts for one role, from one cluster, for one ServiceAccount, for a limited time.
What the pods may do now
The role’s policy lists what Smarter actually does in AWS, for one environment.
- Manage DNS records and hosted zones in Route53, and request TLS certificates from AWS Certificate Manager, for LLMClients and custom domains.
- Read the EKS cluster’s description, add-ons and node groups, for the status pages.
- Create, resize and delete the node groups that run LLMHosts, but only node groups named and tagged for this environment.
- Read and write the environment’s two buckets, one for uploaded media and one for users’ data exports.
Here is how the node group rule is written. A request to create one must carry the environment’s tag, or AWS refuses it.
statement {
sid = "EKSCreateNodegroup"
actions = ["eks:CreateNodegroup"]
resources = [local.eks_cluster_arn]
condition {
test = "StringEquals"
variable = "aws:RequestTag/smarter.sh/environment"
values = [local.smarter_environment]
}
}
The nightly MariaDB backup runs as a second ServiceAccount, smarter-backup, with a second role. That role can upload a file to the backup folder of the private backup bucket, and list it to check its size. It can’t read or delete a backup, and nothing that the app runs can either.
Four identities instead of one
Splitting one key into separate identities is half of the improvement. Each identity gets only what its job needs.
| Identity | Who uses it | What it can do |
|---|---|---|
| The pods’ role | The app, the Celery workers and the scheduler, in production | Smarter’s own AWS calls, for production |
| The backup role | The nightly backup job | Upload backups, nothing else |
| A deploy user | The GitHub Actions workflows that build and deploy production | Push production’s image, and administer production’s namespace |
| A local Docker user | A developer’s laptop | The pods’ permissions, but for the local environment only |
The last two are still IAM users with access keys, because GitHub Actions and a laptop aren’t pods in the cluster. But each one is scoped to a single environment, and neither can do the other’s job.
What a stolen credential costs
Credentials leak in boring ways. A .env file gets committed by mistake, or pasted into a bug report. A key shows up in a log. A laptop is stolen. A dependency of the app turns out to be malicious, or a bug lets someone run code inside a pod. You should plan for it.
With one broadly permissioned key
Suppose the key that the pods and the deploy workflow share is stolen. It works from the attacker’s own computer, and it keeps working until a person notices and deletes it, which is often weeks later. With permissions broad enough to run the whole platform, an attacker could do any of the following.
- Delete or rewrite the DNS zones of the platform and of every customer’s custom domain, which takes them all offline, or point them at a server that collects passwords.
- Download the nightly database backups, a full copy of every user and every resource.
- Read every Kubernetes Secret in the namespace through
kubectl, which hands over the database password, the encryption key for users’ stored secrets, and the LLM providers’ API keys. - Start GPU node groups, which bill by the hour, until the account’s quota runs out. Mining cryptocurrency on someone else’s bill is the classic payoff 😱.
- If the key can manage IAM, as an administrator’s key can, create a new key for themselves, so that deleting the stolen one doesn’t lock them out.
One leak, from the least protected place the key lived, exposes everything that any of its users could do.
With the new identities
Now take the same leaks one at a time.
- A developer’s
.envleaks. It holds the local Docker user’s key. That key reaches the local environment’s buckets, the local namespace and node groups named for the local environment. Production’s namespace refuses it outright, and so do production’s buckets and node groups. - Someone runs code inside a production pod. There’s no access key in the pod to steal, only temporary credentials and the token they’re issued for. Copied out of the pod, the credentials stop working within the hour, and the token within a day. While the attacker stays inside, the role can do Smarter’s work in production, but it can’t read or delete a backup, can’t create an IAM user or key, and can’t touch another namespace. Kubernetes limits it to the resources in its Role.
- The deploy user’s key leaks from GitHub. It can push an image to production’s repository and administer production’s namespace, which is serious, and why it lives only in GitHub’s encrypted secrets. It can’t change DNS, read a bucket, create node groups or reach any other namespace.
- The backup job is compromised. It can upload files to one folder. That’s all.
| Leak | One broad key | Separate scoped identities |
|---|---|---|
Developer’s .env with a broad key | Everything, in every environment | The local environment only |
| Code running in a pod | Everything, until the key is revoked | Smarter’s work in one environment, for a day at most after the attacker loses access |
| Deploy workflow’s secret | Everything | Production’s image and namespace |
| Database backups | Readable by anyone with the key | Readable by no running workload |
| Persistence | New keys, if the key can manage IAM | No identity can create IAM users or keys |
What’s still broad
Some permissions can’t be narrowed today, and you should know which.
- Route53 and Certificate Manager are account-wide. Every custom domain gets its own new hosted zone and certificate, whose IDs aren’t known in advance, so the role can’t be limited to a list of them. A compromised pod could still change DNS records in the account.
- The pod can still read its own namespace’s Secrets. Smarter manages the TLS secrets of LLMClients, so its Role includes them, and the platform’s own settings secret is in that namespace too. A pod already has those values in its environment, though, so this adds little.
- A compromised deploy key can ship a bad image. That’s the price of letting a workflow deploy. Pinned, signed images and protected branches are the defense there, not IAM.
- GPU node groups can still be created in the pod’s own environment. IAM can’t limit instance types for EKS node groups, so EC2’s per-account vCPU quotas and a billing alarm are the guardrail against a large bill.
Running Smarter yourself
If you deploy Smarter with its Helm chart on EKS, set serviceAccount.awsRoleArn to a role that trusts your cluster’s OIDC issuer and the smarter ServiceAccount, and leave AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY out of the chart’s values. An access key in the pods’ environment wins over the role, so the two shouldn’t be mixed. The backup job takes its own role, databaseScheduledBackup.awsRoleArn.
Nothing changes for an installation that still uses keys. Without a role, the chart passes the keys to the pods as before, and kubectl in the pods simply switches to the ServiceAccount and the Role that the chart now creates. The chart creates the read-only ClusterRole too, unless you set serviceAccount.createClusterRole to false and create it yourself, as Smarter’s own Terraform does, so that a deploy needs only namespace permissions.
For local development, use a key with the pods’ permissions, scoped to a local environment, never an administrator’s key. The infrastructure docs describe all three credential setups, for pods, local Docker and the deploy workflow.
Closing thoughts
A broadly permissioned access key is convenient precisely because it works for everything, everywhere, forever, and those are also the three properties an attacker wants most. IRSA takes away all three for the pods. A credential that’s scoped to one job, one environment and a few hours is much less interesting to steal, and when it leaks anyway, the damage stays small enough to name.
