IAM, least privilege and secrets
- IAM
- identity and access management
Mid-level Frequently asked 5 min
On this page
The question
What is the principle of least privilege, and how do you apply it in the cloud?
30-second answer
Every identity, person or workload, gets only the permissions its job needs, on only the resources it touches, for only as long as it needs them. In practice: one role per service, with policies naming specific actions and resource ARNs (Amazon Resource Names) instead of wildcards; no shared users; temporary credentials from roles instead of long-lived keys; separate accounts for production; and regular reviews that remove permissions nobody used. Then a compromised service can only do a small amount of damage.
Understand it
Which call does the policy allow?
PredictThe orders-app role has two statements: an Allow for reading and writing invoice objects, and a Deny on deleting anything. The app makes four calls.
View as text
A policy box with two statements: Allow s3:GetObject and s3:PutObject on arn:aws:s3:::invoices/*, and Deny s3:DeleteObject on everything. Below it, four calls, each showing IAM's decision once revealed. The orders-app role has two statements: an Allow for reading and writing invoice objects, and a Deny on deleting anything. The app makes four calls. The learner predicts which call is allowed.
GetObject on an invoice matches the Allow: action and resource both fit, and no Deny applies. Allowed.
ListBucket acts on the bucket, arn:aws:s3:::invoices, not on its objects, and is not in the Allow. Nothing allows it, so the default implicit deny applies.
GetObject on the payroll bucket: the action fits, the resource does not. No statement allows it, so it is implicitly denied. Least privilege at work.
DeleteObject on an invoice: even if another policy allowed it, the explicit Deny wins. Explicit deny, then allow, then the default deny.
A policy box with two statements: Allow s3:GetObject and s3:PutObject on arn:aws:s3:::invoices/*, and Deny s3:DeleteObject on everything. Below it, four calls, each showing IAM's decision once revealed. The orders-app role has two statements: an Allow for reading and writing invoice objects, and a Deny on deleting anything. The app makes four calls. The learner predicts which call is allowed.
GetObject on an invoice matches the Allow: action and resource both fit, and no Deny applies. Allowed.
ListBucket acts on the bucket, arn:aws:s3:::invoices, not on its objects, and is not in the Allow. Nothing allows it, so the default implicit deny applies.
GetObject on the payroll bucket: the action fits, the resource does not. No statement allows it, so it is implicitly denied. Least privilege at work.
DeleteObject on an invoice: even if another policy allowed it, the explicit Deny wins. Explicit deny, then allow, then the default deny.
Takeaway: IAM denies by default. A request passes only if some statement allows its action on its exact resource, and no statement explicitly denies it. An explicit Deny always wins.
| Concept | AWS (Amazon Web Services) | Azure | GCP (Google Cloud Platform) |
|---|---|---|---|
| Permissions | IAM policies | Azure RBAC (role-based access control) role assignments | IAM role bindings |
| Identity for a workload | IAM role, instance profile, EKS (Elastic Kubernetes Service) Pod Identity | Managed identity | Service account, Workload Identity |
| Secrets store | Secrets Manager, Parameter Store | Key Vault secrets | Secret Manager |
| Encryption keys | KMS (key management service) | Key Vault keys | Cloud KMS |
In code
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadWriteInvoices",
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::invoices/*"
},
{
"Sid": "ReadDbSecret",
"Effect": "Allow",
"Action": "secretsmanager:GetSecretValue",
"Resource": "arn:aws:secretsmanager:eu-west-1:123456789012:secret:prod/orders/db-*"
}
]
}resource "aws_iam_role" "orders_app" {
name = "orders-app"
assume_role_policy = data.aws_iam_policy_document.ecs_tasks_assume.json # who may wear the role
}
resource "aws_iam_role_policy_attachment" "too_much" {
Wrong policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
role = aws_iam_role.orders_app.name
}
Right resource "aws_iam_role_policy" "least_privilege" {
Right policy = file("${path.module}/orders-app-policy.json")
Right role = aws_iam_role.orders_app.id
Right }# The value never appears in the repo, the image or the task definition
spring.config.import=aws-secretsmanager:prod/orders/db
spring.cloud.aws.region.static=eu-west-1
# Keys of the secret's JSON become properties, such as username and password
spring.datasource.url=jdbc:postgresql://orders-db.internal:5432/orders
spring.datasource.username=${username}
spring.datasource.password=${password}public final class DbSecret {
private static final SecretsManagerClient CLIENT = SecretsManagerClient.create(); // default credentials chain: the role
private static volatile String cached;
public static String password() {
if (cached == null) {
String json = CLIENT.getSecretValue(r -> r.secretId("prod/orders/db")).secretString();
cached = json.replaceAll(".*\"password\"\\s*:\\s*\"([^\"]*)\".*", "$1");
}
return cached;
}
}Solve it
An access key was pushed to a public repository. Respond
A developer pushed application-local.properties to a public GitHub repository. It holds an access key for an IAM (identity
and access management) user that the CI (continuous integration) pipeline and a few laptops share, with the
AdministratorAccess policy. The push was 40 minutes ago. Say what you do now, and what you change so it cannot happen again.
- Contain first, investigate second. Deactivate the key immediately; scrapers find public keys within minutes. Deleting the commit does not help, because forks and caches keep it. Then rotate whatever else that file held.
- Find out what the key did. Search the audit log, CloudTrail, by access key ID for the last 40 minutes and beyond: new users, keys, roles, instances in unused regions. Remove anything the attacker created.
- Remove the reason a key existed. The pipeline gets a role through OIDC (OpenID Connect) federation, with short-lived credentials and no stored secret. Developers sign in through single sign-on, not shared users.
- Shrink what any identity can do. Replace
AdministratorAccesswith policies scoped to the actions and resources each job needs, and keep application secrets in a secrets store, never in property files. - Check. The old key is gone and the audit log shows no new activity. Secret scanning blocks a test push with a fake key, and the pipeline deploys while holding no key at all.
aws iam update-access-key --user-name ci-shared --access-key-id AKIAEXAMPLE1234567 --status Inactive
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=AccessKeyId,AttributeValue=AKIAEXAMPLE1234567 \
--start-time 2026-11-13T09:00:00Z --query 'Events[].[EventTime,EventName,AwsRegion]' --output table
aws iam list-users --query 'Users[?CreateDate>=`2026-11-13`].UserName' # anything the attacker created?
aws iam delete-access-key --user-name ci-shared --access-key-id AKIAEXAMPLE1234567How to answer
- Start from nothing and add what the service needs; IAM denies by default.
- Use access logs and the provider's unused-permission reports to tighten policies over time.
- Guardrails such as organization policies and permission boundaries cap even administrators.
- Least privilege applies to networks and databases too, not only to IAM.
Common mistakes
- Giving a service AdministratorAccess or a star on every action to save time.
- Sharing one IAM user's access keys between people, pipelines and apps.
Follow-up questions
What is the difference between an IAM user and a role? Which should an application use?
A user is a long-lived identity with a password or access keys that never expire unless rotated. A role has no credentials of its own: a trusted principal, such as an EC2 (Elastic Compute Cloud) instance, a Kubernetes service account, a pipeline through OIDC (OpenID Connect) or a person through single sign-on, assumes it and receives temporary credentials from STS (Security Token Service) that expire within hours. Applications should always use roles, so there is no key to leak or rotate.
- The trust policy says who may assume a role; the permission policies say what it may do.
- Azure's managed identities and GCP (Google Cloud Platform) service accounts with workload identity play the same part.
- Keep users, if any, for people behind multi-factor sign-in.
See it in the infographic: Which call does the policy allow?
Where should an application's secrets live, such as database passwords and API keys?
In a managed secrets store, such as AWS Secrets Manager, Azure Key Vault or GCP Secret Manager, encrypted with a key from a KMS (key management service). The app reads them at startup or through a cached client, authorized by its own role, so nothing secret sits in the repository, the image or plain environment files. The store gives access policies per secret, audit logs and rotation. Better still, remove secrets entirely: roles for cloud APIs, IAM authentication for databases.
- Never commit secrets; scan every push for them.
- Rotation works only if the app rereads the secret, so cache with a refresh.
- Kubernetes Secrets are base64, not encryption; sync from a store with an operator.
See it in the infographic: Which call does the policy allow?
A role has an Allow and a Deny that both match a request. What happens, and why?
The request is denied. IAM starts from an implicit deny, so nothing is allowed until a statement allows it. It then checks every applicable policy: an explicit Deny anywhere wins over every Allow; otherwise one matching Allow is enough. A statement matches only if the action and the exact resource fit, so s3:ListBucket on a bucket needs the bucket's ARN, while object actions need bucket/*.
- Explicit deny, then allow, then the default deny.
- Organization policies and permission boundaries can only narrow what a policy allows.
- Resource policies, such as a bucket policy, add another place where a Deny can come from.
See it in the infographic: Which call does the policy allow?
How do you find the permissions a service really needs?
Start with the calls the code makes, then run it in a test environment with a narrow policy and read the denied calls in the audit log. Tools such as IAM Access Analyzer can generate a policy from recorded activity. Review again after features change, and remove permissions that the last months of logs never used.
Check yourself
Show answers
- ListBucket acts on the bucket, arn:aws:s3:::invoices, which the policy does not name: Correct. Object actions use bucket/; bucket actions need the bucket's own ARN.
- An explicit Deny must exist somewhere: Incorrect. No Deny is needed; the resource simply does not match.
- Listing a bucket requires administrator access: Incorrect. s3:ListBucket on the bucket's ARN is enough.
Show answers
- From a role attached to its task or Pod, with temporary credentials: Correct. Nothing to store, leak or rotate; the SDK refreshes them itself.
- From access keys in environment variables: Incorrect. Long-lived keys leak through logs, images and dumps.
- From an encrypted properties file in the repository: Incorrect. The decryption key must then live somewhere too.