CloudAcademia
EN

Cloud Computing · Module 07 · Identity and Access Management in the Cloud

Cloud IAM: Who Can Do What

Lesson50 minIntermediateMarkdown

Showing raw Markdown: the renderer could not load.

## Why this matters
A developer pushes a small fix to a public code repository on Friday afternoon. Buried in the commit is a file with a cloud access key, the one she created two years ago "just to test something." Within minutes, automated scanners find it. By Saturday morning the account is running hundreds of servers for a cryptocurrency miner, and the bucket with customer exports has been copied elsewhere. Nothing was "hacked" in the movie sense: the attacker simply asked the cloud to do things, and the cloud checked that the key was valid and that the policy attached to it allowed the actions. It did, because the key belonged to an administrator.

In the cloud, the console and the Application Programming Interface (API) are reachable from anywhere on the Internet. There is no office network to hide behind, so **identity** becomes the real perimeter. **Identity and Access Management (IAM)** is the part of every cloud that decides who is asking and what they may do. Learning it well means you can give each person and each program exactly the access it needs, preferably for a few minutes instead of forever, and you can read someone else's policy and know what it really allows.

## Learning objectives
- Explain the building blocks of cloud IAM: principals (users, groups, roles, service accounts), authentication versus authorization, and how a request is evaluated.
- Read and write JSON access policies with actions, resources, effects and conditions, and predict the result when allow and deny statements overlap.
- Apply the principle of least privilege and compare role-based (RBAC) and attribute-based (ABAC) access control.
- Explain temporary credentials and federation (SAML 2.0, OpenID Connect) and why they are safer than long-lived access keys.
- Assess common IAM mistakes (wildcard policies, leaked keys, unused accounts, no MFA) and how to detect them.

## Key concepts

### Principals: who is asking?
Think of a building with badge readers on every door. Each badge belongs to someone or something, and each door has a rule about which badges open it. In the cloud, the badge holder is a **principal**: any identity that can send a request to the cloud and be recognized. Most clouds offer the same four kinds:

| Principal | What it is | Typical use |
|---|---|---|
| **User** | A named identity for one person, usually with a password and a second factor. It can also have **access keys** for the command line and programs. | An engineer who signs in to the console. |
| **Group** | A set of users. A group is not a principal itself: you cannot sign in as a group. You attach policies to it and every member inherits them. | `analysts`, `network-admins`. |
| **Role** | An identity with permissions but **no password and no permanent keys**. Someone or something *assumes* it and receives temporary credentials. | A person who needs elevated access for an hour; a server; a user from another account. |
| **Service account** (also called a workload identity or machine identity) | The identity of a program rather than a person: an application, a virtual machine (VM), a container, a pipeline. | A web app that reads a bucket. |

<figure class="diagram" style="--dg-w: 1120px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1120 420" width="1120" height="420" role="img" aria-labelledby="iam-who-what-resource-title iam-who-what-resource-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="iam-who-what-resource-title">Cloud IAM: who, what and which resource</title>
<desc id="iam-who-what-resource-desc">A request travels from a principal to a resource through the authorization engine. On the left, the principals: a user, a person with a password and multi-factor authentication; a group, a set of users to which policies are attached once; a role, assumed temporarily with no password or long-lived key; and a service account, the identity of an application or virtual machine. In the middle, the authorization engine authenticates the caller, evaluates the policies and allows or denies the request. Below it, the policies: identity policies attached to principals and resource policies attached to resources, both read by the engine. On the right, the resources, such as the bucket lab-data with ARN arn:aws:s3:::lab-data and a virtual machine, reached only if the request is allowed. A strip at the bottom lists what a request carries: the principal, the action s3:GetObject, the resource arn:aws:s3:::lab-data/reports/q3.txt, and context such as time, source IP address and whether MFA was used.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1119" height="419" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l1"><rect class="dg-tint" x="20" y="20" width="450" height="262" rx="16" fill="#f0f6f9" stroke="#9ac3d9" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(36 33) scale(0.7500)" fill="none" stroke="#0369a1" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></g>
<text x="62" y="47" font-size="13" class="dg-ink" fill="#0369a1" font-weight="700">Who: principals</text></g>
<rect class="dg-card" x="36" y="62" width="196" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(52 78) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></g></g>
<g class="dg-l1"><text x="94" y="99" font-size="15" class="dg-ink" fill="#0369a1" font-weight="700">User</text></g>
<text x="54" y="130" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">a person, with a</text>
<text x="54" y="150" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">password and MFA</text>
<rect class="dg-card" x="244" y="62" width="196" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(260 78) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></g></g>
<g class="dg-l1"><text x="302" y="99" font-size="15" class="dg-ink" fill="#0369a1" font-weight="700">Group</text></g>
<text x="262" y="130" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">a set of users: attach</text>
<text x="262" y="150" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">policies once</text>
<rect class="dg-card" x="36" y="168" width="196" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(52 184) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="7.5" cy="15.5" r="5.5"/><path d="m21 2-9.6 9.6"/><path d="m15.5 7.5 3 3L22 7l-3-3"/></g></g>
<g class="dg-l1"><text x="94" y="205" font-size="15" class="dg-ink" fill="#0369a1" font-weight="700">Role</text></g>
<text x="54" y="236" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">assumed temporarily:</text>
<text x="54" y="256" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">no password, no key</text>
<rect class="dg-card" x="244" y="168" width="196" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(260 184) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M12 8V4H8"/><rect width="16" height="12" x="4" y="8" rx="2"/><path d="M2 14h2"/><path d="M20 14h2"/><path d="M15 13v2"/><path d="M9 13v2"/></g></g>
<g class="dg-l1"><text x="302" y="205" font-size="15" class="dg-ink" fill="#0369a1" font-weight="700">Service account</text></g>
<text x="262" y="236" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">identity of an app or</text>
<text x="262" y="256" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">VM, for programs</text>
<g class="dg-l4"><rect class="dg-tint" x="880" y="20" width="220" height="262" rx="16" fill="#fbf4f0" stroke="#e7b39e" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(896 33) scale(0.7500)" fill="none" stroke="#c2410c" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z"/></g>
<text x="922" y="47" font-size="13" class="dg-ink" fill="#c2410c" font-weight="700">Which resource</text></g>
<rect class="dg-card" x="896" y="62" width="188" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(912 78) scale(1.2500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/></g></g>
<g class="dg-l4"><text x="954" y="99" font-size="15" class="dg-ink" fill="#c2410c" font-weight="700">Bucket lab-data</text></g>
<text x="914" y="130" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">arn:aws:s3:::lab-data</text>
<rect class="dg-card" x="896" y="168" width="188" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(912 184) scale(1.2500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="2" rx="2"/><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6 6h.01"/><path d="M6 18h.01"/></g></g>
<g class="dg-l4"><text x="954" y="205" font-size="15" class="dg-ink" fill="#c2410c" font-weight="700">Virtual machine</text></g>
<text x="914" y="236" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">arn:...:i-0abc</text>
<rect class="dg-card" x="552" y="50" width="240" height="120" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(568 66) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></g></g>
<g class="dg-l2"><text x="610" y="87" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">Authorization engine</text></g>
<text x="570" y="118" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">1. authenticate the caller</text>
<text x="570" y="138" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">2. evaluate policies</text>
<text x="570" y="158" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">3. allow or deny</text>
<rect class="dg-card" x="552" y="200" width="240" height="98" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(568 216) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></g></g>
<g class="dg-l7"><text x="610" y="237" font-size="15" class="dg-ink" fill="#6d28d9" font-weight="700">Policies</text></g>
<text x="570" y="268" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">identity policy: on the principal</text>
<text x="570" y="288" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">resource policy: on the resource</text>
<path class="dg-line" d="M672 198V174" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M666 182l6-8 6 8" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M472 110H544" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M539 104l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-bg-plain" x="477.44" y="78" width="61.12" height="24" rx="12" fill="#ffffff"/><text x="508" y="94.5" font-size="11" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle">request</text>
<g class="dg-l2"><path class="dg-stroke" d="M794 110H872" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M867 104l8 6-8 6" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><rect class="dg-bg-plain" x="794.2" y="78" width="79.6" height="24" rx="12" fill="#ffffff"/><text x="834" y="94.5" font-size="11" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle">if allowed</text></g>
<rect class="dg-panel" x="20" y="308" width="1080" height="92" rx="12" fill="#f0f0ec"/>
<text x="40" y="336" font-size="11" class="dg-muted" fill="#5c5c68" font-weight="700" letter-spacing="1.2">A REQUEST CARRIES</text>
<g class="dg-l1"><rect class="dg-solid" x="40" y="352" width="197" height="26" rx="13" fill="#0369a1"/><text x="138.5" y="369.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">principal: who is asking</text></g>
<g class="dg-l7"><rect class="dg-solid" x="243" y="352" width="168" height="26" rx="13" fill="#6d28d9"/><text x="327" y="369.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">action: s3:GetObject</text></g>
<g class="dg-l4"><rect class="dg-solid" x="417" y="352" width="355" height="26" rx="13" fill="#c2410c"/><text x="594.5" y="369.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">resource: arn:aws:s3:::lab-data/reports/q3.txt</text></g>
<g class="dg-l3"><rect class="dg-solid" x="778" y="352" width="233" height="26" rx="13" fill="#b45309"/><text x="894.5" y="369.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">context: time, source IP, MFA</text></g>
</svg></figure>

*Figure 1. Every request carries a principal, an action, a resource and some context; the authorization engine compares them with the policies and answers allow or deny.*

Every principal has a unique name in the provider's format. On Amazon Web Services (AWS) it is an **Amazon Resource Name (ARN)** such as `arn:aws:iam::123456789012:user/bob`: `arn`, the partition, the service, the region (empty for IAM), the account ID and the resource. Other providers use their own formats (an email-style address for a Google Cloud service account, an object ID for Microsoft Entra), but the idea is the same.

At the top of every account sits one more identity: the **root user** (called the owner or global administrator on other clouds). It is created with the account, it can do everything and it cannot be restricted by normal policies. You protect it and almost never use it.

> [!NOTE]
> This module uses the AWS IAM vocabulary and policy syntax because it is the most widely implemented: many S3-compatible storage services at other providers accept the same bucket-policy JSON, and open-source tools emulate it, which is why the lab can run on your laptop. Other providers use different words for the same ideas: Google Cloud IAM binds roles to members on a resource, Microsoft Azure assigns roles with role-based access control at a scope, and OpenStack Keystone issues tokens for users in projects. Where it matters, the lesson says how they differ.

> [!CHECK]
> A company wants a web application running on a VM to read files from a storage bucket, without storing any password or key on the VM. Which principal is the best fit?
>
> - [ ] A user for the application, with its access key saved in a configuration file
> - [ ] A group, because groups can hold policies for many programs
> - [x] A role (or service account) attached to the VM, which gives it temporary credentials
> - [ ] The root user, because it never needs a policy
>
> A role or service account has no password and hands out short-lived credentials automatically. A group cannot be a caller, and the root user is far too powerful.

### Authentication versus authorization
Two different questions are asked on every request, and mixing them up is the start of many misconfigurations.

- **Authentication (authN)** answers "who are you?" The caller proves its identity with something it knows (a password), has (a hardware key, a phone) or is (a fingerprint), or, for programs, with a cryptographic signature or token.
- **Authorization (authZ)** answers "what are you allowed to do?" The cloud compares the authenticated principal, the requested action and the target resource with the policies.

Authentication always comes first, and passing it grants nothing by itself. You can see the difference in the error you get. A request with an unknown or revoked key fails authentication (in the lab: `InvalidClientTokenId`). A valid key without permission fails authorization (`AccessDenied`). If you see `AccessDenied`, the cloud knows exactly who you are and has decided you may not.

Programs usually authenticate by **signing** each request with a secret key, for example with AWS Signature Version 4: the secret itself never travels over the network, only a signature computed over the request, which also stops anyone from altering it in transit. Tokens work differently: a token is a signed statement, valid for a limited time, that the receiver can check without calling anyone.

> [!CHECK]
> A script gets `AccessDenied` when it lists a bucket. What does this error tell you?
>
> - [ ] The access key is invalid or has been deleted
> - [x] The cloud authenticated the caller, but no policy allows that action on that resource
> - [ ] The request was altered in transit
> - [ ] The caller needs to sign in again with its password
>
> `AccessDenied` is an authorization result: the identity was recognized. An invalid or unknown key would fail earlier, at authentication, with a different error.

### How a request is evaluated
Once the caller is authenticated, the cloud evaluates **policies**: documents that list what is allowed or denied. Two families matter:

- **Identity-based policies** are attached to a principal (user, group or role): "Bob may read the reports folder."
- **Resource-based policies** are attached to a resource, such as a bucket policy: "Carol's role may read this bucket." They name the principal they apply to.

The evaluation rules are simple, and they are the same in every major cloud:

1. **Default deny.** If nothing mentions the request, it is denied. Permissions are never implied.
2. **Explicit allow.** A matching statement with effect `Allow` permits the request.
3. **Explicit deny wins.** A matching statement with effect `Deny` blocks the request, even if ten other statements allow it.

<figure class="diagram" style="--dg-w: 1100px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1100 236" width="1100" height="236" role="img" aria-labelledby="iam-evaluation-flow-title iam-evaluation-flow-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="iam-evaluation-flow-title">How a request is evaluated</title>
<desc id="iam-evaluation-flow-desc">Policy evaluation in four steps, left to right. 1. Authenticate: the caller proves who it is with a signature, token or password; if this fails, the request is rejected. 2. Collect policies: the identity policies of the principal and the resource policy of the target. 3. Any explicit Deny? If a matching statement has Effect Deny, the request is denied and evaluation stops. 4. Any explicit Allow? If a matching statement has Effect Allow, the request is allowed; otherwise it is denied. Three outcomes are shown below: a matching Deny means denied and always wins; a matching Allow with no Deny means allowed; no match at all means denied by default.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1099" height="235" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<rect class="dg-card" x="24" y="24" width="237.5" height="132" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="46" cy="48" r="12" fill="#0369a1"/><text x="46" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(221.5 36) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></g></g>
<text x="38" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Authenticate</text>
<text x="38" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Prove who you are:</text>
<text x="38" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">signature, token, password</text>
<text x="38" y="140" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">fails: request rejected</text>
<path class="dg-line" d="M267.5 90H289.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M281.5 84l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="295.5" y="24" width="237.5" height="132" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="317.5" cy="48" r="12" fill="#0369a1"/><text x="317.5" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(493 36) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M8 6h13"/><path d="M8 12h13"/><path d="M8 18h13"/><path d="M3 6h.01"/><path d="M3 12h.01"/><path d="M3 18h.01"/></g></g>
<text x="309.5" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Collect policies</text>
<text x="309.5" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Identity policies of the</text>
<text x="309.5" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">principal plus the resource</text>
<text x="309.5" y="140" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">policy of the target</text>
<path class="dg-line" d="M539 90H561" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M553 84l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="567" y="24" width="237.5" height="132" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="589" cy="48" r="12" fill="#0369a1"/><text x="589" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(764.5 36) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="m4.9 4.9 14.2 14.2"/></g></g>
<text x="581" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Any explicit Deny?</text>
<text x="581" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">A matching statement with</text>
<text x="581" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Effect: Deny?</text>
<text x="581" y="140" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">yes: DENIED, stop here</text>
<path class="dg-line" d="M810.5 90H832.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M824.5 84l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="838.5" y="24" width="237.5" height="132" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="860.5" cy="48" r="12" fill="#0369a1"/><text x="860.5" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(1036 36) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="852.5" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Any explicit Allow?</text>
<text x="852.5" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">A matching statement with</text>
<text x="852.5" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Effect: Allow?</text>
<text x="852.5" y="140" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">yes: ALLOWED; no: DENIED</text>
<g class="dg-warn"><rect class="dg-tint" x="24" y="178" width="341.333" height="34" rx="10" fill="#f8e8e8" stroke="#e09999" stroke-width="1.2"/><text x="62" y="200" font-size="12.5" class="dg-ink" fill="#b91c1c" font-weight="700">Deny matches: DENIED, always wins</text></g>
<g class="dg-warn"><g class="dg-icon" transform="translate(36 186) scale(0.7500)" fill="none" stroke="#b91c1c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="m4.9 4.9 14.2 14.2"/></g></g>
<g class="dg-ok"><rect class="dg-tint" x="379.333" y="178" width="341.333" height="34" rx="10" fill="#e8f2ec" stroke="#96c6a8" stroke-width="1.2"/><text x="417.333" y="200" font-size="12.5" class="dg-ink" fill="#15803d" font-weight="700">Allow matches, no Deny: ALLOWED</text></g>
<g class="dg-ok"><g class="dg-icon" transform="translate(391.333 186) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<g class="dg-warn"><rect class="dg-tint" x="734.667" y="178" width="341.333" height="34" rx="10" fill="#f8e8e8" stroke="#e09999" stroke-width="1.2"/><text x="772.667" y="200" font-size="12.5" class="dg-ink" fill="#b91c1c" font-weight="700">No match at all: DENIED (default deny)</text></g>
<g class="dg-warn"><g class="dg-icon" transform="translate(746.667 186) scale(0.7500)" fill="none" stroke="#b91c1c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></g></g>
</svg></figure>

*Figure 2. Deny beats allow, and silence is a deny. This order never changes, however many policies apply.*

Within a single account, an allow in *either* the identity policy or the resource policy is enough, as long as nothing denies. Between two accounts, both sides must allow: the resource owner has to let the outside principal in, and the principal's own account has to let it make the call.

Real clouds add **guardrails** on top: AWS permissions boundaries and service control policies, Azure management groups and deny assignments, Google Cloud organization policies and deny policies. A guardrail never grants anything; it only limits the maximum a principal can ever receive, whatever its own policies say. That is how an organization can say "nobody in any project may use these regions" once, for everyone.

> [!CHECK]
> Alice's group policy allows `s3:*` on a bucket. Her user also has a policy with a `Deny` for `s3:DeleteObject` on that bucket. She tries to delete an object. What happens?
>
> - [ ] It succeeds, because the wider allow covers the specific action
> - [ ] It succeeds, because the most recently attached policy wins
> - [x] It is denied, because an explicit deny overrides any allow
> - [ ] It is denied only if she is not in the group
>
> Explicit deny always wins, regardless of how many allows match or where they are attached.

### Reading a JSON policy
Policies in AWS style are JavaScript Object Notation (JSON) documents. The same structure appears in IAM policies, in role trust policies and in bucket policies, and S3-compatible services at many providers accept it too.

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ReadReports",
      "Effect": "Allow",
      "Principal": { "AWS": "arn:aws:iam::123456789012:user/carol" },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::lab-data/reports/*",
      "Condition": { "Bool": { "aws:MultiFactorAuthPresent": "true" } }
    }
  ]
}
```

<figure class="diagram" style="--dg-w: 1120px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1120 398" width="1120" height="398" role="img" aria-labelledby="iam-policy-anatomy-title iam-policy-anatomy-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="iam-policy-anatomy-title">Anatomy of a JSON access policy</title>
<desc id="iam-policy-anatomy-desc">A JSON bucket policy for the bucket lab-data, with each element explained. Version is the policy language version, 2012-10-17. The Statement list holds one statement: Sid ReadReports is an optional label; Effect Allow says whether matching requests are allowed or denied; Principal is the user arn:aws:iam::123456789012:user/carol, who is asking, and appears only in resource and trust policies; Action s3:GetObject is the operation; Resource arn:aws:s3:::lab-data/reports/* are the objects it applies to; Condition tests that multi-factor authentication is present, using Bool aws:MultiFactorAuthPresent true.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1119" height="397" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<rect class="dg-card" x="24" y="24" width="590" height="350" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-icon-neutral" transform="translate(40 34) scale(0.8333)" fill="none" stroke="#17171c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></g>
<text x="68" y="49" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600">bucket policy attached to lab-data</text>
<text x="42" y="80" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">{</text>
<g class="dg-l6"><rect class="dg-tint" x="32" y="89" width="574" height="26" rx="6" fill="#f6e8f7"/></g>
<g class="dg-l6"><rect class="dg-solid" x="640" y="90" width="104" height="26" rx="13" fill="#a21caf"/><text x="692" y="107.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">Version</text></g>
<text x="760" y="108" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">language version: keep 2012-10-17</text>
<text x="42" y="108" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">  &quot;Version&quot;: &quot;2012-10-17&quot;,</text>
<text x="42" y="136" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">  &quot;Statement&quot;: [{</text>
<g class="dg-l5"><rect class="dg-tint" x="32" y="145" width="574" height="26" rx="6" fill="#f8e8ef"/></g>
<g class="dg-l5"><rect class="dg-solid" x="640" y="146" width="104" height="26" rx="13" fill="#be185d"/><text x="692" y="163.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">Sid</text></g>
<text x="760" y="164" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">optional label, unique in the policy</text>
<text x="42" y="164" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">    &quot;Sid&quot;: &quot;ReadReports&quot;,</text>
<g class="dg-l2"><rect class="dg-tint" x="32" y="173" width="574" height="26" rx="6" fill="#e8f2ec"/></g>
<g class="dg-l2"><rect class="dg-solid" x="640" y="174" width="104" height="26" rx="13" fill="#15803d"/><text x="692" y="191.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">Effect</text></g>
<text x="760" y="192" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">Allow or Deny</text>
<text x="42" y="192" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">    &quot;Effect&quot;: &quot;Allow&quot;,</text>
<g class="dg-l1"><rect class="dg-tint" x="32" y="201" width="574" height="26" rx="6" fill="#e6f0f6"/></g>
<g class="dg-l1"><rect class="dg-solid" x="640" y="202" width="104" height="26" rx="13" fill="#0369a1"/><text x="692" y="219.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">Principal</text></g>
<text x="760" y="220" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">who: only in resource and trust policies</text>
<text x="42" y="220" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">    &quot;Principal&quot;: {&quot;AWS&quot;: &quot;arn:aws:iam::123456789012:user/carol&quot;},</text>
<g class="dg-l7"><rect class="dg-tint" x="32" y="229" width="574" height="26" rx="6" fill="#f0eafb"/></g>
<g class="dg-l7"><rect class="dg-solid" x="640" y="230" width="104" height="26" rx="13" fill="#6d28d9"/><text x="692" y="247.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">Action</text></g>
<text x="760" y="248" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">which operations: service:Operation</text>
<text x="42" y="248" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">    &quot;Action&quot;: &quot;s3:GetObject&quot;,</text>
<g class="dg-l4"><rect class="dg-tint" x="32" y="257" width="574" height="26" rx="6" fill="#f9ece7"/></g>
<g class="dg-l4"><rect class="dg-solid" x="640" y="258" width="104" height="26" rx="13" fill="#c2410c"/><text x="692" y="275.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">Resource</text></g>
<text x="760" y="276" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">which objects, written as ARNs</text>
<text x="42" y="276" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">    &quot;Resource&quot;: &quot;arn:aws:s3:::lab-data/reports/*&quot;,</text>
<g class="dg-l3"><rect class="dg-tint" x="32" y="285" width="574" height="26" rx="6" fill="#f8eee6"/></g>
<g class="dg-l3"><rect class="dg-solid" x="640" y="286" width="104" height="26" rx="13" fill="#b45309"/><text x="692" y="303.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">Condition</text></g>
<text x="760" y="304" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">extra tests: MFA, IP, time, tags</text>
<text x="42" y="304" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">    &quot;Condition&quot;: {&quot;Bool&quot;: {&quot;aws:MultiFactorAuthPresent&quot;: &quot;true&quot;}}</text>
<text x="42" y="332" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">  }]</text>
<text x="42" y="360" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">}</text>
</svg></figure>

*Figure 3. A statement answers four questions: effect, who, what and on which resource, and optionally "only if".*

| Element | Meaning |
|---|---|
| `Version` | The policy language version. Always `2012-10-17`: it is not a date you choose, and the older `2008-10-17` lacks features such as policy variables. |
| `Statement` | One statement or a list. Each is evaluated on its own. |
| `Sid` | Optional label, handy in audits. |
| `Effect` | `Allow` or `Deny`. Nothing else. |
| `Principal` | Who the statement is about. Used only in **resource-based** and **trust** policies; in an identity policy the principal is whoever it is attached to, so the element is not allowed there. |
| `Action` | The operations, written `service:Operation`: `s3:GetObject`, `iam:CreateUser`. Wildcards work: `s3:Get*`. |
| `Resource` | What the action applies to, as ARNs. Wildcards work. |
| `Condition` | Extra tests that must be true: the source IP address, the time, whether MFA was used, tags, and more. |

Two details trip up almost everyone:

- **Bucket-level and object-level actions need different ARNs.** `s3:ListBucket` applies to the bucket, `arn:aws:s3:::lab-data`. `s3:GetObject` applies to objects, `arn:aws:s3:::lab-data/reports/*`. A policy that puts `GetObject` on the bucket ARN, or `ListBucket` on `lab-data/*`, silently matches nothing.
- **`Action` and `Resource` are matched together.** A statement applies only if the requested action *and* the requested resource both match (and the condition is true). An allow for `s3:GetObject` on `reports/*` says nothing about `secret/*`.

A **Condition** has an operator, a condition key and a value. `Bool` with `aws:MultiFactorAuthPresent` checks that the caller signed in with a second factor; `IpAddress` with `aws:SourceIp` limits calls to a network range such as `203.0.113.0/24`; `DateLessThan` with `aws:CurrentTime` makes access expire. A request has to satisfy all the conditions of a statement for the statement to apply.

> [!CHECK]
> A statement has `"Effect": "Allow"`, `"Action": "s3:GetObject"` and `"Resource": "arn:aws:s3:::lab-data"`. A user with this policy tries to download `reports/q3.txt` from `lab-data`. What is the result?
>
> - [ ] Allowed: the user can read the whole bucket
> - [x] Denied: `GetObject` acts on objects, so the resource ARN needs `/*` (or a path) after the bucket name
> - [ ] Allowed, but only for objects in the `reports/` folder
> - [ ] The policy is rejected as invalid when it is saved
>
> The ARN `arn:aws:s3:::lab-data` names the bucket itself, not the objects in it. The statement is valid, but it never matches an object request, so the default deny applies.

### Least privilege, RBAC and ABAC
The **principle of least privilege** says every principal gets only the permissions it needs for its task, and only for as long as it needs them. It limits the **blast radius**: if a credential leaks or a person makes a mistake, the damage is bounded by what that identity could do.

In practice you apply it by:
- starting from nothing and adding permissions for a known task, rather than starting from administrator and removing;
- naming specific actions and resources instead of `*`;
- using groups and roles per job function instead of per person;
- adding conditions (MFA, source network, time) for sensitive actions;
- reviewing what is actually used and trimming the rest.

Two models decide how permissions are assigned:

| | **Role-based access control (RBAC)** | **Attribute-based access control (ABAC)** |
|---|---|---|
| Decides by | The role or group the principal belongs to | Attributes of the principal, the resource, the action and the environment |
| Example rule | "Members of `analysts` can read the reports bucket." | "A user can start a VM if the VM's `project` tag equals the user's `project` tag." |
| Strength | Easy to understand and audit; maps to job titles. | Scales without new policies: a new project needs new tags, not new roles. Fine-grained and dynamic. |
| Weakness | Roles multiply ("role explosion") as exceptions appear. | Harder to reason about; depends on trustworthy attributes, so who may change tags becomes a security question. |
| Typical use | Most workloads, a good starting point. | Large, multi-team environments. |

ABAC is defined by the US National Institute of Standards and Technology (NIST) in Special Publication (SP) 800-162, *Guide to Attribute Based Access Control (ABAC) Definition and Considerations*. Most real environments combine both: roles for the broad shape, attributes for the fine print. Here is an ABAC statement in AWS style, which lets a principal start and stop only the VMs tagged with its own project:

```json
{
  "Effect": "Allow",
  "Action": ["ec2:StartInstances", "ec2:StopInstances"],
  "Resource": "arn:aws:ec2:*:*:instance/*",
  "Condition": {
    "StringEquals": { "aws:ResourceTag/project": "${aws:PrincipalTag/project}" }
  }
}
```

`${aws:PrincipalTag/project}` is a policy variable: the cloud substitutes the project tag of the caller at request time. One statement serves every project.

> [!CHECK]
> A company has 200 projects and wants each team to manage only its own project's resources, without writing a new policy for every project. Which approach fits best?
>
> - [ ] One group per project, each with its own handwritten policy
> - [ ] Give every engineer administrator access and rely on trust
> - [x] ABAC: tag principals and resources with `project`, and allow actions when the tags match
> - [ ] Share one access key per team
>
> ABAC compares attributes at request time, so one policy covers all projects. Per-project groups work (RBAC) but multiply the policies to maintain.

### Temporary credentials and assuming a role
Long-lived access keys are the biggest single risk in cloud IAM: they do not expire, they get copied into scripts, laptops and repositories, and nobody notices when they leak. The fix is **temporary credentials**: a set of an access key ID, a secret and a **session token** that stops working after a short time, from minutes to a few hours.

You get them by **assuming a role**, through a **security token service**: an STS-style API (AWS Security Token Service). The role has two kinds of policy:

- A **trust policy** says *who may assume the role* (the principal element, as in the Figure 3 example).
- A **permissions policy** says *what the role may do* once assumed.

<figure class="diagram" style="--dg-w: 960px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 960 746" width="960" height="746" role="img" aria-labelledby="iam-assume-role-title iam-assume-role-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="iam-assume-role-title">Assuming a role to get temporary credentials</title>
<desc id="iam-assume-role-desc">A sequence between Carol, an IAM user, the token service (STS) and the storage service holding the bucket lab-data. 1. Carol calls AssumeRole for the role reader, signed with her own access key. The token service checks the role&#x27;s trust policy and Carol&#x27;s own permission to call AssumeRole. 2. It returns temporary credentials: an access key ID starting with ASIA, a secret and a session token, expiring in 900 seconds. 3. Carol calls GetObject for reports/q3.txt, signed with the temporary key and session token; the storage service evaluates the role&#x27;s policy, not Carol&#x27;s own, and returns the object with 200 OK. She then calls PutObject for reports/c.txt, and gets 403 AccessDenied because the reader role is read-only. When the credentials expire they stop working, so there is nothing to revoke or rotate.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="959" height="745" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<path class="dg-line" d="M130 98V730" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M480 98V730" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M830 98V730" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<rect class="dg-card" x="25" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(39 38) scale(1.1667)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></g></g>
<text x="77" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Carol</text>
<text x="77" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">IAM user</text>
<rect class="dg-card" x="375" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(389 38) scale(1.1667)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="7.5" cy="15.5" r="5.5"/><path d="m21 2-9.6 9.6"/><path d="m15.5 7.5 3 3L22 7l-3-3"/></g></g>
<text x="427" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Token service</text>
<text x="427" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">STS</text>
<rect class="dg-card" x="725" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(739 38) scale(1.1667)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/></g></g>
<text x="777" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Storage</text>
<text x="777" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">bucket lab-data</text>
<text x="305" y="128" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">AssumeRole</text>
<text x="305" y="145" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">role: reader, signed with Carol&#x27;s own key</text>
<g class="dg-l1"><circle class="dg-solid" cx="249.5" cy="124" r="11" fill="#0369a1"/><text x="249.5" y="128.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M134 159H474" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M466 153l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><rect class="dg-tint" x="278.5" y="181" width="403" height="48" rx="8" fill="#f9ece7" stroke="#e4aa92" stroke-width="1.2"/>
<text x="480" y="198" font-size="12.5" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle">Checks the role&#x27;s trust policy (may Carol assume it?)</text>
<text x="480" y="215" font-size="12.5" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle">and Carol&#x27;s own permission to call AssumeRole</text>
</g>
<text x="305" y="263" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">Temporary credentials</text>
<text x="305" y="280" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">ASIA... key + secret + session token</text>
<text x="305" y="297" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">expires in 900 s</text>
<g class="dg-l2"><circle class="dg-solid" cx="208.25" cy="259" r="11" fill="#15803d"/><text x="208.25" y="263.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l2"><path class="dg-stroke" d="M476 311H136" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M144 305l-8 6 8 6" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-bg-plain" x="384" y="336" width="192" height="17" fill="#ffffff"/>
<text x="480" y="349" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">GetObject reports/q3.txt</text>
<rect class="dg-bg-plain" x="305.25" y="353" width="349.5" height="17" fill="#ffffff"/>
<text x="480" y="366" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">signed with the temporary key + session token</text>
<g class="dg-l1"><circle class="dg-solid" cx="372" cy="345" r="11" fill="#0369a1"/><text x="372" y="349.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M134 380H824" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M816 374l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><rect class="dg-tint" x="716" y="402" width="228" height="48" rx="8" fill="#f9ece7" stroke="#e4aa92" stroke-width="1.2"/>
<text x="830" y="419" font-size="12.5" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle">Evaluates the role&#x27;s policy,</text>
<text x="830" y="436" font-size="12.5" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle">not Carol&#x27;s own policy</text>
</g>
<rect class="dg-bg-plain" x="387.75" y="471" width="184.5" height="17" fill="#ffffff"/>
<text x="480" y="484" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">200 OK: object returned</text>
<g class="dg-ok"><path class="dg-stroke" d="M826 498H136" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-ok"><path class="dg-stroke" d="M144 492l-8 6 8 6" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-bg-plain" x="387.75" y="523" width="184.5" height="17" fill="#ffffff"/>
<text x="480" y="536" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">PutObject reports/c.txt</text>
<rect class="dg-bg-plain" x="305.25" y="540" width="349.5" height="17" fill="#ffffff"/>
<text x="480" y="553" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">signed with the temporary key + session token</text>
<g class="dg-l1"><path class="dg-stroke" d="M134 567H824" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M816 561l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-bg-plain" x="414" y="592" width="132" height="17" fill="#ffffff"/>
<text x="480" y="605" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">403 AccessDenied</text>
<rect class="dg-bg-plain" x="369" y="609" width="222" height="17" fill="#ffffff"/>
<text x="480" y="622" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">the reader role is read-only</text>
<g class="dg-warn"><path class="dg-stroke" d="M826 636H136" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M144 630l-8 6 8 6" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-ok"><rect class="dg-tint" x="14" y="658" width="305" height="48" rx="8" fill="#e8f2ec" stroke="#96c6a8" stroke-width="1.2"/>
<text x="166.5" y="675" font-size="12.5" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle">At expiry the credentials stop working:</text>
<text x="166.5" y="692" font-size="12.5" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle">nothing to revoke, nothing to rotate</text>
</g>
</svg></figure>

*Figure 4. While the session lasts, Carol acts as the role, with the role's permissions and not her own. When it expires, access ends by itself.*

The calling identity changes: after `AssumeRole`, the API reports the caller as `arn:aws:sts::123456789012:assumed-role/reader/carol-lab`, not as the user. Audit logs record the session name, so you can still tell who did what. Access keys that begin with `AKIA` are long-lived; keys that begin with `ASIA` are temporary and need the session token as well.

Assuming roles is also how a person gets elevated access only when needed ("break glass" or just-in-time access), and how workloads receive credentials from the platform: an instance profile on a VM, a service account bound to a Kubernetes pod, or a managed identity on Azure. The program asks a local metadata endpoint for fresh credentials; no secret is stored anywhere.

> [!NOTE]
> The role's trust policy names who *may* assume it. In real AWS, a trust policy that names a specific user or role in the same account is enough by itself. If it names the whole account instead (`arn:aws:iam::<account-id>:root`), each principal also needs an identity policy that allows `sts:AssumeRole`. Many organizations still add an identity policy that allows `sts:AssumeRole` on the specific role, so the permission is visible on the principal as well. The lab does this, because the emulator needs it.

> [!CHECK]
> Which statement about temporary credentials from `AssumeRole` is true?
>
> - [ ] They keep the permissions of the user who requested them
> - [ ] They never expire as long as the role exists
> - [x] They carry the role's permissions and stop working after a set time
> - [ ] They are only valid together with the user's password
>
> The session acts as the role, so the role's permissions apply, not the caller's. The expiry is built into the credentials and the session token.

### Federation: SAML 2.0 and OpenID Connect
Creating a separate cloud user for every employee, every contractor and every pipeline does not scale, and the passwords and keys pile up. **Federation** lets the cloud trust an external **identity provider (IdP)**: the company's directory, a social login or a CI system. The user signs in at the IdP, and the IdP vouches for them with a signed token. The cloud never sees the password; it only checks the token and maps the user to a role.

<figure class="diagram" style="--dg-w: 1080px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1080 629" width="1080" height="629" role="img" aria-labelledby="iam-federation-title iam-federation-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="iam-federation-title">Federation: an identity provider vouches for you</title>
<desc id="iam-federation-desc">A sequence with four participants: a user or workload, the identity provider, cloud IAM with its token service, and a cloud resource. 1. The user signs in at the identity provider with a password and MFA, or a CI job presents its own token; the cloud never sees the password. 2. The identity provider returns a signed token: an OpenID Connect ID token in JWT format or a SAML 2.0 assertion. 3. The user presents the token to cloud IAM and exchanges it for a role, with AssumeRoleWithWebIdentity or AssumeRoleWithSAML. Cloud IAM verifies the signature with the identity provider&#x27;s public key, checks issuer, audience and expiry, and matches the role&#x27;s trust policy. 4. It returns temporary credentials valid for minutes to hours. 5. The user sends API requests signed with them to the cloud resource, which allows them according to the role&#x27;s policy.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1079" height="628" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<path class="dg-line" d="M130 98V613" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M400 98V613" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M670 98V613" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M940 98V613" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<rect class="dg-card" x="25" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(39 38) scale(1.1667)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<text x="77" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">User or workload</text>
<text x="77" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">browser or CI job</text>
<rect class="dg-card" x="295" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l5"><g class="dg-icon" transform="translate(309 38) scale(1.1667)" fill="none" stroke="#be185d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M6 22V4a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v18Z"/><path d="M6 12H4a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h2"/><path d="M18 9h2a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2h-2"/><path d="M10 6h4"/><path d="M10 10h4"/><path d="M10 14h4"/><path d="M10 18h4"/></g></g>
<text x="347" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Identity provider</text>
<text x="347" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">IdP: SAML / OIDC</text>
<rect class="dg-card" x="565" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(579 38) scale(1.1667)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></g></g>
<text x="617" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Cloud IAM / STS</text>
<text x="617" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">trusts the IdP</text>
<rect class="dg-card" x="835" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(849 38) scale(1.1667)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/></g></g>
<text x="887" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Cloud resource</text>
<text x="887" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">bucket, VM, API</text>
<text x="265" y="128" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">Sign in</text>
<text x="265" y="145" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">password + MFA, or the CI job&#x27;s own token</text>
<g class="dg-l1"><circle class="dg-solid" cx="220.75" cy="124" r="11" fill="#0369a1"/><text x="220.75" y="128.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M134 159H394" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M386 153l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><rect class="dg-tint" x="156.5" y="181" width="487" height="31" rx="8" fill="#f9ece7" stroke="#e4aa92" stroke-width="1.2"/>
<text x="400" y="198" font-size="12.5" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle">The IdP authenticates the user: the cloud never sees the password</text>
</g>
<text x="265" y="246" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">Signed token</text>
<text x="265" y="263" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">OIDC ID token (JWT) or SAML 2.0 assertion</text>
<g class="dg-l5"><circle class="dg-solid" cx="202" cy="242" r="11" fill="#be185d"/><text x="202" y="246.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l5"><path class="dg-stroke" d="M396 277H136" stroke="#be185d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-l5"><path class="dg-stroke" d="M144 271l-8 6 8 6" stroke="#be185d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-bg-plain" x="285.25" y="302" width="229.5" height="17" fill="#ffffff"/>
<text x="400" y="315" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">Exchange the token for a role</text>
<rect class="dg-bg-plain" x="217.75" y="319" width="364.5" height="17" fill="#ffffff"/>
<text x="400" y="332" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">AssumeRoleWithWebIdentity or AssumeRoleWithSAML</text>
<g class="dg-l1"><circle class="dg-solid" cx="273.25" cy="311" r="11" fill="#0369a1"/><text x="273.25" y="315.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M134 346H664" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M656 340l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><rect class="dg-tint" x="430" y="368" width="480" height="48" rx="8" fill="#f9ece7" stroke="#e4aa92" stroke-width="1.2"/>
<text x="670" y="385" font-size="12.5" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle">Verifies the signature with the IdP&#x27;s public key; checks issuer,</text>
<text x="670" y="402" font-size="12.5" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle">audience and expiry; matches the role&#x27;s trust policy</text>
</g>
<rect class="dg-bg-plain" x="315.25" y="437" width="169.5" height="17" fill="#ffffff"/>
<text x="400" y="450" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">Temporary credentials</text>
<rect class="dg-bg-plain" x="296.5" y="454" width="207" height="17" fill="#ffffff"/>
<text x="400" y="467" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">valid for minutes to hours</text>
<g class="dg-l2"><circle class="dg-solid" cx="303.25" cy="446" r="11" fill="#15803d"/><text x="303.25" y="450.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l2"><path class="dg-stroke" d="M666 481H136" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M144 475l-8 6 8 6" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-bg-plain" x="345.25" y="506" width="379.5" height="17" fill="#ffffff"/>
<text x="535" y="519" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">API request signed with the temporary credentials</text>
<g class="dg-l1"><circle class="dg-solid" cx="333.25" cy="515" r="11" fill="#0369a1"/><text x="333.25" y="519.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">5</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M134 533H934" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M926 527l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-bg-plain" x="424" y="558" width="222" height="17" fill="#ffffff"/>
<text x="535" y="571" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">Allowed by the role&#x27;s policy</text>
<g class="dg-ok"><path class="dg-stroke" d="M936 585H136" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-ok"><path class="dg-stroke" d="M144 579l-8 6 8 6" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
</svg></figure>

*Figure 5. The identity provider authenticates; the cloud authorizes. The result is temporary credentials, with no long-lived key anywhere.*

Two standards dominate:

| | **SAML 2.0** | **OpenID Connect (OIDC)** |
|---|---|---|
| Full name | Security Assertion Markup Language 2.0 (OASIS standard) | OpenID Connect Core 1.0 |
| Token | An **assertion**: a signed Extensible Markup Language (XML) document | An **ID token**: a signed JSON Web Token (JWT, RFC 7519) |
| Built on | XML and browser redirects | OAuth 2.0 (RFC 6749), adding an authentication layer |
| Typical use | Enterprise single sign-on (SSO) for people to the console | Web and mobile apps, and **workload identity federation** for pipelines and programs |
| Example | A staff member signs in to the company portal and lands in the cloud console | A CI job proves "I am the `main` branch of this repository" and gets a role for a deployment |

OAuth 2.0 on its own is about *delegated authorization* (letting an app act on your behalf), not about proving who you are. OIDC adds the missing identity layer on top of it.

Workload identity federation replaces the stored deployment key. A CI platform issues each job a short-lived OIDC token whose claims say where it comes from, for instance `sub: repo:example-org/example-repo:ref:refs/heads/main`. The cloud role's trust policy accepts only that issuer, audience and subject. A different repository, or a different branch, cannot assume the role.

> [!CHECK]
> What does a cloud verify when it receives an OIDC ID token or a SAML assertion from an identity provider?
>
> - [ ] The user's password, which the identity provider forwards
> - [x] The signature (with the IdP's public key), the issuer, the audience and the expiry, then the role's trust policy
> - [ ] Only that the token is a well-formed JSON or XML document
> - [ ] That the user has an access key in the cloud account
>
> The cloud trusts the IdP's signature, not a password. It still checks that the token is meant for it, is still valid, and matches the trust policy of the role.

### Multi-factor authentication
**Multi-factor authentication (MFA)** requires two or more different kinds of proof: something you know, something you have and something you are. A stolen password alone no longer opens the door. The common second factors, from weakest to strongest:

- **SMS codes.** Better than nothing, but vulnerable to SIM swapping and interception.
- **Time-based one-time passwords (TOTP, RFC 6238)** from an authenticator app. Phishable: a fake site can ask for the code and replay it at once.
- **FIDO2 / WebAuthn hardware keys and passkeys.** The key signs a challenge that includes the real site's name, so a look-alike site gets nothing useful. This is **phishing-resistant** MFA.

Enforce MFA for every human user, always for the root user and for sensitive actions through a condition (`aws:MultiFactorAuthPresent`). Programs cannot use MFA, which is one more reason to give them temporary credentials instead of keys.

> [!CHECK]
> Why is a FIDO2 security key considered stronger than a TOTP code from an app?
>
> - [ ] The code is longer, so it is harder to guess
> - [ ] The key works without the Internet, so nobody can intercept it
> - [x] The key signs a challenge tied to the genuine site, so a phishing site cannot reuse it
> - [ ] The cloud stores the key's secret so it can reset it
>
> A TOTP code can be typed into a fake page and replayed. A FIDO2 response is bound to the real site's origin, so a copy is worthless elsewhere.

## How it works
Follow one request from start to finish. Bob, whose user has the least-privilege policy of the lab, runs `aws s3api get-object --bucket lab-data --key reports/q3.txt`:

1. **Sign.** The command line tool builds the HTTP request and signs it with Bob's secret key. The access key ID goes in the request; the secret does not.
2. **Authenticate.** The service looks up the access key ID, recomputes the signature with the matching secret and compares. A mismatch or an unknown key ends the request with an authentication error.
3. **Build the context.** The service notes who (`arn:aws:iam::123456789012:user/bob`), what (`s3:GetObject`), on which resource (`arn:aws:s3:::lab-data/reports/q3.txt`) and the environment (source IP address, time, whether MFA and TLS were used).
4. **Collect policies.** Bob's user policies, those of his groups, the bucket policy of `lab-data`, and any organization guardrails.
5. **Evaluate.** Look for a matching `Deny`: none. Look for a matching `Allow`: the statement for `GetObject` on `lab-data/reports/*` matches. Result: allow.
6. **Act and log.** The object is returned, and the audit log records the call with the principal, action, resource, source IP address and result.

> [!CHECK]
> Which step of a request's life takes place first?
>
> - [ ] The cloud collects the policies that apply
> - [ ] The cloud looks for a matching explicit deny
> - [x] The cloud authenticates the caller from the signature or token
> - [ ] The cloud checks whether the caller's role has expired
>
> Policies are evaluated for a *known* principal, so authentication always comes first. An expired session fails at that same authentication step.

## Worked example
You manage the bucket `lab-data` with two prefixes, `reports/` and `secret/`. Bob has this identity policy (it is the one you build in the lab):

```json
{
  "Version": "2012-10-17",
  "Statement": [
    { "Sid": "ListTheBucket", "Effect": "Allow", "Action": "s3:ListBucket",
      "Resource": "arn:aws:s3:::lab-data" },
    { "Sid": "EverythingInReports", "Effect": "Allow", "Action": "s3:*",
      "Resource": "arn:aws:s3:::lab-data/reports/*" },
    { "Sid": "NeverDelete", "Effect": "Deny", "Action": "s3:DeleteObject",
      "Resource": "*" }
  ]
}
```

The bucket also has a resource policy that refuses any request that is not encrypted in transit (a common baseline):

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "DenyPlainHttp",
      "Effect": "Deny",
      "Principal": "*",
      "Action": "s3:*",
      "Resource": ["arn:aws:s3:::lab-data", "arn:aws:s3:::lab-data/*"],
      "Condition": { "Bool": { "aws:SecureTransport": "false" } }
    }
  ]
}
```

Predict the outcome of each request from Bob, then check the reasoning:

| # | Request | Result | Why |
|---|---|---|---|
| 1 | `GetObject` `reports/q3.txt` over HTTPS | **Allowed** | `EverythingInReports` allows it; no deny matches (`SecureTransport` is true, so `DenyPlainHttp` does not apply). |
| 2 | `GetObject` `reports/q3.txt` over plain HTTP | **Denied** | `DenyPlainHttp` matches: explicit deny wins over the allow. |
| 3 | `PutObject` `reports/q4.txt` over HTTPS | **Allowed** | `s3:*` on `reports/*` covers it. |
| 4 | `DeleteObject` `reports/q3.txt` over HTTPS | **Denied** | `NeverDelete` is an explicit deny; it beats the `s3:*` allow. |
| 5 | `GetObject` `secret/passwords.txt` over HTTPS | **Denied** | No statement allows `secret/*`: default deny. |
| 6 | `ListBucket` on `lab-data` | **Allowed** | `ListTheBucket` allows it, for the whole bucket: Bob sees the *names* of the objects in `secret/` too. |

Row 6 is a classic surprise. Listing is a bucket-level action, so this policy cannot limit it to `reports/` by resource alone. On real clouds you add a `Condition` on the key prefix (for AWS, `s3:prefix`) to narrow it.

## Common pitfalls
- **Putting `*` in `Action` or `Resource` "to make it work"** — it works because it allows everything. Find the exact action from the error or the audit log and name it.
- **Confusing the bucket ARN with the object ARN** — `ListBucket` needs `arn:aws:s3:::name`, `GetObject` needs `arn:aws:s3:::name/*`. If one half of a policy works and the other does not, check this first.
- **Expecting a later or narrower allow to override a deny** — it never does. To restore access you must remove or narrow the deny.
- **Forgetting that a group is not an identity** — you cannot sign in as a group, and you cannot name it as the `Principal` of a resource or trust policy. You attach policies to it and its members inherit them.
- **Testing with the administrator identity** — it can do anything, so you never see the missing permission. Test as the identity that will really run the task.
- **Assuming conditions are always enforced by every tool** — some emulators and some partial S3-compatible services ignore them. Test conditions on the real service before relying on them.

## Security considerations
Most cloud breaches begin with identity, not with a software vulnerability. These are the mistakes that appear again and again, and how you find them.

### Common mistakes
- **Wildcard policies.** `"Action": "*"` on `"Resource": "*"` makes every credential that carries it a master key. Even `s3:*` on `*` lets the holder read, change and delete every bucket in the account.
- **Long-lived and leaked keys.** Access keys committed to repositories, pasted in chat, baked into container images or left in a CI variable for years. Public repositories are scanned for keys, often within minutes of a push. Prefer roles, federation and temporary credentials; where a key is unavoidable, rotate it and limit it with conditions.
- **Unused accounts and credentials.** Former employees, abandoned test users and old keys remain valid until someone deletes them. Every unused identity is an unmonitored way in.
- **Using the root or owner account day to day.** It cannot be restricted. Protect it with a strong password and phishing-resistant MFA, give it no access keys, and keep it for the few tasks that need it.
- **No MFA, or weak MFA.** A stolen password then equals a full compromise. Require MFA for every human, and prefer FIDO2 for administrators.
- **Overly broad trust policies.** A role that trusts an entire account, or `"Principal": "*"`, can be assumed by anyone there, or by anyone at all. Name the exact principal, and for federation the exact issuer, audience and subject.
- **Permissions granted through the ability to change permissions.** A principal allowed `iam:PutUserPolicy`, or `iam:AttachUserPolicy`, can give itself administrator rights. Treat IAM write actions as administrator-level.

### Detection
- **Audit logs.** Every API call is recorded with who, what, when and from where: AWS CloudTrail, Azure Activity Log and Microsoft Entra sign-in logs, Google Cloud Audit Logs. Keep them in a separate, write-protected account, and alert on sensitive events: root use, console sign-in without MFA, new access keys, policy changes, calls from unusual countries.
- **Access reviews.** On a schedule (quarterly is common) the owner of each resource confirms who still needs access, and you remove the rest. Include service accounts and cross-account trusts, which are easily forgotten.
- **Credential hygiene reports.** List users, keys, their age and their last use (the lab uses `get-access-key-last-used`). Delete keys that nobody used for 90 days, rotate those that must stay, and compare permissions granted with permissions used; most clouds can suggest a narrower policy from the activity log (for example IAM Access Analyzer on AWS, the policy analysis tools on Google Cloud).
- **Key rotation.** Create the new key, switch the application over, check that the old key is no longer used, then delete it. At no point do you have to stop the application. Rotation shortens the life of a leak but does not remove the need to avoid long-lived keys.
- **Secret scanning.** Scan repositories and images for keys before they are pushed, and treat any key that ever reached a public place as compromised: delete it, do not just rewrite history.

> [!WARNING]
> If an access key leaks, deactivate or delete it at once, then review the audit log for what it did between the leak and the revocation. Changing the code or deleting the repository does not undo the exposure.

## Summary
- IAM answers two questions: who are you (authentication) and what may you do (authorization). `AccessDenied` means authentication succeeded and authorization did not.
- A request is allowed only if some statement allows it and none denies it: default deny, explicit allow, explicit deny wins.
- A JSON policy statement has an effect, a principal (in resource and trust policies), actions, resources and optional conditions. Mind the difference between bucket and object ARNs.
- Apply least privilege: specific actions and resources, narrow scopes, groups and roles, conditions, and regular reviews. Use RBAC for the broad shape and ABAC for fine, scalable rules.
- Prefer roles and temporary credentials (an STS-style `AssumeRole`) to long-lived keys, and federate people and pipelines with SAML 2.0 or OpenID Connect so the cloud never holds their passwords.
- Enforce MFA, protect the root account, delete unused identities, rotate unavoidable keys, and watch the audit logs.

## Glossary
| Term | Definition |
|---|---|
| Identity and Access Management (IAM) | The cloud service and discipline that decides who is asking and what they may do. |
| Identity | A recognized actor in a system: a person, a program or a role. |
| Principal | Any identity that can make a request: user, group member, role session or service account. |
| User | A named identity for one person, with credentials such as a password and access keys. |
| Group | A set of users to which policies are attached; members inherit them. It cannot make requests itself. |
| Role | An identity with permissions but no permanent credentials, assumed temporarily. |
| Service account | A non-human identity for an application, VM or pipeline. Also called workload identity or machine identity. |
| Root user | The all-powerful identity created with the account (owner or global administrator elsewhere). |
| Amazon Resource Name (ARN) | The unique name of a resource or principal in AWS, such as `arn:aws:iam::123456789012:user/bob`. |
| Access key | A key pair (ID and secret) that signs API requests. Long-lived unless issued as part of temporary credentials. |
| Authentication (authN) | Proving who the caller is. |
| Authorization (authZ) | Deciding what an authenticated caller may do. |
| Policy | A document that lists allowed or denied actions on resources. |
| Identity-based policy | A policy attached to a user, group or role. |
| Resource-based policy | A policy attached to a resource, such as a bucket policy; it names the principals it applies to. |
| Default deny | The rule that anything not explicitly allowed is denied. |
| Explicit deny | A statement with `Effect: Deny`; it overrides every allow. |
| Statement | One rule in a policy: effect, principal, action, resource and optional condition. |
| Condition | An extra test in a statement (MFA, IP range, time, tags) that must be true for it to apply. |
| Guardrail | An organization-level limit (such as permissions boundaries or service control policies) that caps what principals can ever receive. |
| Principle of least privilege | Giving each principal only the permissions it needs, for as long as it needs them. |
| Blast radius | The amount of damage possible if one identity or credential is compromised. |
| Role-based access control (RBAC) | Granting permissions according to the roles or groups a principal belongs to. |
| Attribute-based access control (ABAC) | Granting permissions by comparing attributes of the principal, resource, action and environment (NIST SP 800-162). |
| Temporary credentials | An access key, secret and session token that expire after a short time. |
| Session token | The extra value that must accompany temporary credentials on every request. |
| Security token service (STS) | The API that issues temporary credentials, for example through `AssumeRole`. |
| Assume a role | Exchange your identity for temporary credentials carrying a role's permissions. |
| Trust policy | The policy on a role that says who may assume it. |
| Federation | Trusting an external identity provider to authenticate users, so the cloud holds no passwords for them. |
| Identity provider (IdP) | The system that authenticates users and issues signed tokens or assertions. |
| SAML 2.0 | An XML-based standard for signed assertions, widely used for enterprise single sign-on. |
| OpenID Connect (OIDC) | An authentication layer on top of OAuth 2.0 that issues signed JWT ID tokens. |
| Workload identity federation | Letting a pipeline or program exchange a short-lived token from its platform for cloud credentials, with no stored key. |
| Multi-factor authentication (MFA) | Authentication that requires two or more kinds of proof. |
| Phishing-resistant MFA | MFA, such as FIDO2 keys, whose response is bound to the genuine site and cannot be replayed from a fake one. |
| Audit log | The record of API calls: who, what, when, from where and the result. |
| Access review | A scheduled check that each identity still needs the access it has. |
| Key rotation | Replacing a credential with a new one and retiring the old one. |

## Check your understanding
1. A teammate's policy has `"Action": "s3:*"` and `"Resource": "*"` "because it was the only way the upload script worked." Explain how you would find the real permissions the script needs and write a least-privilege replacement.
2. A user is in two groups. Group A allows `s3:GetObject` on `reports/*`. Group B has an explicit `Deny` for `s3:*` on the whole bucket for anyone without MFA. What can the user do when signed in without MFA, and with MFA? Which element makes the difference?
3. Design the access for a CI pipeline that deploys to your cloud account. Which principal, which credentials, which trust conditions? What do you remove compared with storing an access key in a CI variable?
4. Explain, in your own words, why `AccessDenied` and `InvalidClientTokenId` point to different problems, and what you would check for each.
5. Your company has 40 developers and 5 teams. When would you choose RBAC, when ABAC, and what must you control for ABAC to be safe?
6. You discover that an access key belonging to an employee who left a year ago was last used yesterday. Describe, in order, what you would do in the first hour.

## References
- [NIST SP 800-162](https://csrc.nist.gov/pubs/sp/800/162/upd2/final) — Guide to Attribute Based Access Control (ABAC) Definition and Considerations.
- [NIST SP 800-63-4](https://pages.nist.gov/800-63-4/) — Digital Identity Guidelines (authentication, federation).
- [OpenID Connect Core 1.0](https://openid.net/specs/openid-connect-core-1_0.html).
- [SAML 2.0 Technical Overview](https://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-tech-overview-2.0.html) — OASIS.
- [RFC 6749](https://www.rfc-editor.org/rfc/rfc6749) — The OAuth 2.0 Authorization Framework.
- [RFC 7519](https://www.rfc-editor.org/rfc/rfc7519) — JSON Web Token (JWT).
- [RFC 6238](https://www.rfc-editor.org/rfc/rfc6238) — TOTP: Time-Based One-Time Password Algorithm.
- [AWS IAM User Guide](https://docs.aws.amazon.com/IAM/latest/UserGuide/) — policy evaluation logic, policy reference and temporary credentials.
- [Google Cloud IAM overview](https://cloud.google.com/iam/docs/overview), [Azure RBAC overview](https://learn.microsoft.com/azure/role-based-access-control/overview) and [OpenStack Keystone](https://docs.openstack.org/keystone/latest/) — the same concepts in other providers.