Lesson 2 of 4 IAM Policies and Authorization

Writing Resource-Based Policies and Hardening Trust Policies

This lesson explains how resource-based policies control who can access a resource, including principals in other accounts, and how role trust policies control who can assume a role.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe resource-based policies and recognise their risks.
  • Explain how KMS key policies differ from other resource policies.
  • Explain how sts:ExternalId and other condition keys harden trust policies.
  • Prevent the confused deputy problem with aws:SourceArn and aws:SourceAccount.

Resource-based policies

Resource-based policies are attached to resources and can grant access to other principals. The following list describes their use and risks.

  • On S3, KMS, SQS, SNS, Lambda, Secrets Manager, ECR, EventBridge buses, API Gateway and more, they name principals (accounts, roles, users, services) and grant access directly.
  • Service principals such as logs.amazonaws.com and events.amazonaws.com must be allowed when services deliver data to your resources (e.g. SNS topic and KMS key policies).
  • "Principal": "*" without restrictive conditions makes a resource public; S3 Block Public Access, RCPs and Access Analyzer prevent and detect this.
True or false

Resource-based policies

Exam skills 4.2.1

Check how resource-based policies grant access.

Log in to try this activity Interactive activities are free with an account.

KMS key policies are special

KMS key policies differ from other resource-based policies in important ways. The following list describes these differences.

  • Every key must have one; IAM policies grant nothing unless the key policy allows the account.
  • The default key policy delegates to IAM by allowing the account root.
  • Cross-account use needs the key policy to allow the other account and that account's IAM to allow the key.
True or false

KMS key policies

Exam skills 4.2.1 · 5.3.5

Check what makes KMS key policies different.

Log in to try this activity Interactive activities are free with an account.

Hardening trust policies

A role's trust policy decides who can assume it, and condition keys narrow that further. sts:ExternalId is used when a third party, such as a monitoring vendor, assumes a role in your account. The vendor must supply an ID unique to you, which prevents another of its customers from tricking it into using your role, the confused deputy problem. When an AWS service assumes the role on your behalf, aws:SourceArn and aws:SourceAccount provide the same protection by naming the resource and account the request must come from.

aws:PrincipalOrgID restricts the role to principals in your organisation. For web identity federation, conditions on the token's OIDC claims, such as the repository and branch in a GitHub Actions token, ensure that only the intended workflow can assume the role. The following table summarises the conditions.

ConditionUse
sts:ExternalIdThird parties assuming your role (confused deputy protection)
aws:SourceArn, aws:SourceAccountA service assuming the role on your behalf
aws:PrincipalOrgIDLimit to your organisation
OIDC claimsWeb identity federation, e.g. restrict to a GitHub repository

The following diagram compares a bucket policy statement for a service principal with and without conditions that bind it to the expected account.

Binding a service principal's permission to the expected account. Without conditions, AWS Config acting for any account could be tricked into writing to the bucket. aws:SourceAccount, and aws:SourceArn where available, limit it to this account's delivery channel; the same pattern applies to every service principal.
The scenario is illustrative.
Spot the bug

A GitHub OIDC trust policy that trusts too much

Exam skills 4.2.1 · 4.1.2

Find the lines in this trust policy that let the wrong workflows assume the deployment role.

Log in to try this activity Interactive activities are free with an account.
Worked example

A service role safe from the confused deputy

An S3 bucket allows the AWS Config service to deliver configuration snapshots. Without conditions, any account's Config could be tricked into writing to it. Conditions bind the permission to this account's delivery channel:

{
  "Sid": "AWSConfigBucketDelivery",
  "Effect": "Allow",
  "Principal": { "Service": "config.amazonaws.com" },
  "Action": "s3:PutObject",
  "Resource": "arn:aws:s3:::acme-config-111111111111/AWSLogs/111111111111/Config/*",
  "Condition": {
    "StringEquals": {
      "s3:x-amz-acl": "bucket-owner-full-control",
      "aws:SourceAccount": "111111111111"
    }
  }
}

The same pattern applies to every service principal: SNS topics receiving EventBridge events, KMS keys used by CloudTrail, Lambda functions invoked by S3. aws:SourceAccount and, where available, aws:SourceArn ensure the service acts only on behalf of the expected account and resource.

Check yourself

An IAM policy allows KMS use, but the call still fails. What is the likely cause?

Show answer

The key policy does not allow the account or principal.

Exam Tips

Remember for the exam

  • When a third party assumes a role in your account, add an external ID condition to the trust policy.
  • To limit service-to-service access to your own resource, use aws:SourceArn and aws:SourceAccount.
  • If an IAM policy allows KMS use but it still fails, the key policy does not allow the account or principal.
  • To let only your organisation use a bucket, add aws:PrincipalOrgID to the bucket policy.
  • A principal of "*" without conditions makes the resource public.