TExcelTAD
Home › Cloud & Container Security › Role Assumption, Trust Policies and Federation

Role Assumption, Trust Policies and Federation

Long-lived access keys are the most leaked secret in the cloud, so good designs avoid them: people sign in through an identity provider, pipelines exchange a short-lived token for a role, and vendors assume a role in your account instead of receiving a key. All of that rests on one document per role, the trust policy, which says who may become this role. A role's permissions decide what it can do; its trust policy decides who can do it. An over-broad trust policy is therefore a door into the account from outside, and reviewing trust policies is one of the highest-value checks an auditor can make.

How assuming a role works

  1. A caller asks AWS STS for credentials: AssumeRole (an AWS principal), AssumeRoleWithWebIdentity (an OIDC token, e.g. from a CI system or Kubernetes) or AssumeRoleWithSAML (a SAML assertion from a corporate identity provider).
  2. STS evaluates the role's trust policy (a resource policy on the role) against the caller, with the request's condition keys: the caller's account and organisation, MFA, an external ID, or the token's claims (aud, sub, …).
  3. If allowed, STS returns temporary credentials (an access key ID starting ASIA, a secret and a session token) valid for 15 minutes to the role's maximum session duration (1 hour by default, up to 12).

For a caller in another AWS account, its own identity policy must also allow sts:AssumeRole on the role (cross-account needs both sides, as in the evaluator). For federation there is no "other side": the trust policy and its conditions are the only check.

The two classic trust-policy mistakes

The confused deputy. A SaaS vendor assumes roles in many customers' accounts. If your role trusts the vendor's account without more, anyone who can make the vendor act on their behalf (another customer typing your role ARN into the vendor's console) gets into your account through the vendor. The fix is an external ID: a value the vendor generates per customer and sends in sts:ExternalId, required by your trust policy's condition.

Federation without a subject. A CI system's OIDC provider issues tokens to every repository or project that uses it. A trust policy that checks only the audience (aud = sts.amazonaws.com) accepts tokens from every repository in the world on that CI service. The sub claim (for GitHub Actions repo:<owner>/<repo>:ref:refs/heads/<branch>, or :environment:<name>, :pull_request) must be pinned to your repository and the branch or environment allowed to deploy. (Repositories created on GitHub after 15 July 2026, and those that opt in, use an immutable form with numeric IDs, repo:<owner>@<owner-id>/<repo>@<repo-id>:ref:…; the trust policy must match the form the repository actually sends.) IAM now refuses to create or update a role trusting GitHub's shared provider unless the trust policy evaluates sub (or job_workflow_ref), but roles written before that check are not re-validated until someone edits them, so old audience-only roles still exist.

A trust-policy checker

The program evaluates five trust policies of the lab account against a set of would-be callers, then lints the policies for the risky patterns. Token claims are synthetic; the OIDC provider and claim names follow GitHub Actions' documented format.

python · try it yourself
from fnmatch import fnmatchcase

GH = "token.actions.githubusercontent.com"
OIDC_ARN = f"arn:aws:iam::123456789012:oidc-provider/{GH}"

def L(x):
    return x if isinstance(x, list) else [x]

def cond_ok(cond, ctx):
    for op, tests in (cond or {}).items():
        for key, want in tests.items():
            have = ctx.get(key)
            if have is None:
                return False
            test = (lambda v, w: v == w) if op in ("StringEquals", "Bool") else fnmatchcase
            if not any(test(str(have), str(w)) for w in L(want)):
                return False
    return True

def can_assume(trust, caller):
    for st in trust:
        p = st["Principal"]
        if "Federated" in p:
            ok = caller["type"] == "oidc" and p["Federated"] == caller["provider"] and st["Action"] == "sts:AssumeRoleWithWebIdentity"
        else:
            arns = L(p["AWS"])
            ok = caller["type"] == "aws" and st["Action"] == "sts:AssumeRole" and (
                "*" in arns or caller["arn"] in arns or f"arn:aws:iam::{caller['arn'].split(':')[4]}:root" in arns)
        if ok and cond_ok(st.get("Condition"), caller["ctx"]):
            return True
    return False

roles = {
    "ci-deploy": [{"Principal": {"Federated": OIDC_ARN}, "Action": "sts:AssumeRoleWithWebIdentity",
                   "Condition": {"StringEquals": {f"{GH}:aud": "sts.amazonaws.com",
                                                  f"{GH}:sub": "repo:lab-org/shop:ref:refs/heads/main"}}}],
    "legacy-ci": [{"Principal": {"Federated": OIDC_ARN}, "Action": "sts:AssumeRoleWithWebIdentity",
                   "Condition": {"StringEquals": {f"{GH}:aud": "sts.amazonaws.com"}}}],
    "vendor-audit": [{"Principal": {"AWS": "arn:aws:iam::999988887777:root"}, "Action": "sts:AssumeRole",
                      "Condition": {"StringEquals": {"sts:ExternalId": "lab-7f3c-91d2"}}}],
    "break-glass": [{"Principal": {"AWS": "arn:aws:iam::123456789012:root"}, "Action": "sts:AssumeRole",
                     "Condition": {"Bool": {"aws:MultiFactorAuthPresent": "true"}}}],
    "old-partner": [{"Principal": {"AWS": "*"}, "Action": "sts:AssumeRole"}],
}

def gh(sub):
    return {"type": "oidc", "provider": OIDC_ARN, "ctx": {f"{GH}:aud": "sts.amazonaws.com", f"{GH}:sub": sub}}

callers = {
    "lab-org/shop on main": gh("repo:lab-org/shop:ref:refs/heads/main"),
    "lab-org/shop pull request": gh("repo:lab-org/shop:pull_request"),
    "someone-else/fork-of-shop": gh("repo:someone-else/fork-of-shop:ref:refs/heads/main"),
    "vendor with external id": {"type": "aws", "arn": "arn:aws:iam::999988887777:role/scanner", "ctx": {"sts:ExternalId": "lab-7f3c-91d2"}},
    "vendor without external id": {"type": "aws", "arn": "arn:aws:iam::999988887777:role/scanner", "ctx": {}},
    "alice with MFA": {"type": "aws", "arn": "arn:aws:iam::123456789012:user/alice", "ctx": {"aws:MultiFactorAuthPresent": "true"}},
    "alice without MFA": {"type": "aws", "arn": "arn:aws:iam::123456789012:user/alice", "ctx": {}},
    "unknown account 555555555555": {"type": "aws", "arn": "arn:aws:iam::555555555555:user/x", "ctx": {}},
}
names = list(roles)
print(f"{'caller':30}" + "".join(f"{n:>13}" for n in names))
for cname, c in callers.items():
    print(f"{cname:30}" + "".join(f"{('yes' if can_assume(roles[n], c) else '-'):>13}" for n in names))

print()
for name, trust in roles.items():
    for st in trust:
        cond = st.get("Condition", {})
        keys = {k for tests in cond.values() for k in tests}
        p = st["Principal"]
        if p.get("AWS") == "*" and not keys:
            print(f"CRITICAL {name}: any principal in any AWS account can assume it")
        if "Federated" in p and not any(k.endswith(":sub") for k in keys):
            print(f"CRITICAL {name}: OIDC trust without a 'sub' condition accepts every repository")
        if "AWS" in p and any(":root" in a and not a.split(":")[4] == "123456789012" for a in L(p["AWS"])) \
                and "sts:ExternalId" not in keys and "aws:PrincipalOrgID" not in keys:
            print(f"HIGH     {name}: foreign account trusted without external ID or org condition")
caller                            ci-deploy    legacy-ci vendor-audit  break-glass  old-partner
lab-org/shop on main                    yes          yes            -            -            -
lab-org/shop pull request                 -          yes            -            -            -
someone-else/fork-of-shop                 -          yes            -            -            -
vendor with external id                   -            -          yes            -          yes
vendor without external id                -            -            -            -          yes
alice with MFA                            -            -            -          yes          yes
alice without MFA                         -            -            -            -          yes
unknown account 555555555555              -            -            -            -          yes

CRITICAL legacy-ci: OIDC trust without a 'sub' condition accepts every repository
CRITICAL old-partner: any principal in any AWS account can assume it

The matrix is the whole story. ci-deploy accepts exactly one repository and branch. legacy-ci, an old role with only the audience checked (IAM would reject that trust policy today, but never re-checks it), accepts the pull-request workflow of the real repository and a stranger's fork on another account: any GitHub user could deploy to production with it. The vendor gets in only with its external ID. break-glass requires MFA (and, because it trusts the account root, the caller's identity policy must also allow sts:AssumeRole, which this model doesn't check). old-partner, a leftover from a past integration, lets anyone in any AWS account become the role.

How it is detected

  • Lint trust policies for: Principal: "*" or {"AWS": "*"} without restrictive conditions; OIDC or SAML federation without sub (or the provider's equivalent); foreign accounts without sts:ExternalId or aws:PrincipalOrgID; wildcards in sub (repo:lab-org/* lets every repository and every branch in the organisation deploy).
  • External access findings: IAM Access Analyzer reports every role (and bucket, key, queue…) that can be reached from outside your organisation, which is exactly the list to review.
  • CloudTrail: AssumeRole, AssumeRoleWithWebIdentity and AssumeRoleWithSAML events show who assumed what: userIdentity (or the token's subject in requestParameters/additionalEventData), roleArn, roleSessionName, sourceIPAddress. Alert on assumptions from unknown accounts, unknown repositories or unusual IPs, and on UpdateAssumeRolePolicy.

How it is fixed

  • Pin OIDC trust to aud and an exact sub (repository plus protected branch or deployment environment); use StringEquals, not StringLike with *.
  • Require sts:ExternalId for every third party, and aws:PrincipalOrgID (or exact role ARNs) for your own accounts.
  • Require MFA (aws:MultiFactorAuthPresent, or aws:MultiFactorAuthAge) for human break-glass roles, keep their sessions short, and alert on every use.
  • Set sts:SourceIdentity and meaningful session names so CloudTrail ties every role session back to a person or pipeline.
  • Delete unused roles: IAM shows each role's last-used time.

Pitfalls

  • Trusting the account root "for convenience": every principal in that account with sts:AssumeRole on * can now assume the role.
  • Treating the external ID as a secret: it isn't (the vendor shows it in its console); it prevents confused deputies, not stolen vendor credentials.
  • Forgetting role chaining: a role that can assume another role extends the graph; chained sessions are limited to one hour, but one hour is plenty.

Practice

1. Change ci-deploy's sub to StringLike repo:lab-org/*. Which callers can assume it now?

lab-org/shop on main and lab-org/shop pull request (and any other repository in lab-org). Pull-request workflows run code from contributors' branches, so that is a path from an unreviewed change to production credentials. The fork in another account is still refused because its owner isn't lab-org.

2. Write the lint rule for "trusts the account root without MFA" and decide its severity.

if "AWS" in p and any(a.endswith(":root") for a in L(p["AWS"])) and "aws:MultiFactorAuthPresent" not in keys: .... MEDIUM for internal roles (it delegates to identity policies, which may already be tight), HIGH for administrative roles.

3. CloudTrail shows AssumeRoleWithWebIdentity for legacy-ci with subject repo:someone-else/fork-of-shop:ref:refs/heads/main. What do you do?

Treat it as an incident: the role was assumed from outside the organisation. Restrict the trust policy (pin sub), revoke active sessions (an inline deny on aws:TokenIssueTime before now, as the console's "Revoke active sessions" does), then review everything the session did in CloudTrail using its access key ID.

Test yourself

What does an OIDC trust policy that checks only aud = sts.amazonaws.com accept?

  1. Only your repository
  2. Tokens from every repository or project using that OIDC provider
  3. Nothing
  4. Only the main branch

What problem does sts:ExternalId solve?

  1. Stolen vendor credentials
  2. The confused deputy: another customer making the vendor assume your role
  3. Password reuse
  4. Region restrictions

Which credentials does STS return for an assumed role?

  1. A permanent AKIA access key
  2. Temporary credentials: an ASIA key ID, secret and session token
  3. A password
  4. An SSH key

In the matrix, who can assume old-partner?

  1. Nobody
  2. Only the vendor
  3. Every caller of type aws, including an unknown account
  4. Only GitHub workflows