Sub topic 10
Using an external ID with third parties
This chapter assumes you can read IAM trust policies, know the difference between a trust policy and an identity policy, and can run aws sts assume-role from the CLI. It covers the external ID, which a role owner uses to protect a role that a third-party service assumes.
The problem an external ID solves
Suppose you hire a vendor, such as a cost-monitoring service, to read data in your AWS account. You create a role in your account (222222222222) and trust the vendor's account (333333333333). The vendor's service assumes the role on your behalf.
The vendor serves many customers from the same account. A second customer could give the vendor your role ARN. The vendor's service would then assume your role and hand your data to the wrong customer. The vendor acts as a confused deputy: it holds legitimate access, and someone else tricks it into using that access for them.
The trust policy alone cannot stop this. It trusts the vendor's whole account, and the vendor's service is the one making the call.
How the external ID works
An external ID is a string that the vendor generates for you and that the vendor's service must pass in every AssumeRole call for your role. Your trust policy checks it with the sts:ExternalId condition key. A different customer cannot guess your value, so a request that uses your role ARN with their own ID fails.
Writing the trust policy
Add the condition to the trust policy of the role in your account.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::333333333333:root" },
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": { "sts:ExternalId": "example-external-id-123" }
}
}
]
}The statement allows principals in account 333333333333 to assume the role, but only when the request includes the external ID example-external-id-123. The vendor still needs an identity policy in its own account that allows sts:AssumeRole on your role ARN.
Passing the external ID in a call
The caller supplies the value with --external-id.
aws sts assume-role \
--role-arn arn:aws:iam::222222222222:role/VendorReadOnly \
--role-session-name vendor-session \
--external-id example-external-id-123In a CLI profile, use the external_id setting.
[profile vendor-test]
role_arn = arn:aws:iam::222222222222:role/VendorReadOnly
source_profile = default
external_id = example-external-id-123Most of the time you will not run these calls yourself. The vendor's service makes them. You only create the role, the trust policy, and the external ID.
Common mistakes
- Treating the ID as a password. The external ID is not a secret credential. It prevents one vendor customer from using another customer's role, and the vendor is expected to generate it, not you. Do not reuse one value across roles or customers.
- Letting the customer choose the value. The vendor should generate a unique, hard-to-guess ID for each customer. Values like
company-nameare guessable. - Omitting the condition. Without it, the role is open to the confused deputy attack even though the account trust looks correct.
- Mismatched values. A typo or trailing space between the trust policy and the call gives
AccessDenied. The condition value is case-sensitive withStringEquals. - Using it for your own accounts. For access between accounts you control, an external ID adds little. Use conditions such as
aws:PrincipalOrgIDinstead (covered in the next chapter).
Checking that it works
Call the role without the ID. The call should fail.
aws sts assume-role \
--role-arn arn:aws:iam::222222222222:role/VendorReadOnly \
--role-session-name no-id-testYou should see an AccessDenied error that mentions the sts:AssumeRole operation. Repeat the call with --external-id and the correct value, and it returns temporary credentials.
Exercise
A vendor's service runs in account 333333333333 and serves many customers. Your trust policy trusts that account but has no condition. Why can another customer of the vendor read your data, and what change to the trust policy prevents it?
Show answer
The vendor's service has permission to assume your role, and it will assume any role ARN it is given. Another customer can supply your role ARN, and the service uses its own trusted access to assume it for them (a confused deputy). Add a Condition with StringEquals on sts:ExternalId, set to a unique value the vendor generated for you, so only requests that pass that value can assume the role.