Sub topic 10
Using an external ID with third parties
This chapter shows how to protect a role that you let a third-party vendor assume. It assumes you have read the earlier chapters on trust policies, caller policies, and sts:AssumeRole. It also assumes you are comfortable reading JSON policies and running AWS CLI commands.
When a third party assumes your role
So far the trusted account has been one you control. Often it is not. A vendor may offer cost analysis, security scanning, backup, or monitoring. To do the work, the vendor needs read access to your account. You create a role in your account (the trusting account). Its trust policy names the vendor's AWS account (the trusted account). The vendor's software calls sts:AssumeRole and gets temporary credentials.
This works, but it creates a risk that does not exist inside your own organization. The vendor serves many customers from one AWS account. Every customer's role trusts that same account. The trust policy alone cannot tell which customer the vendor is acting for.
The confused deputy problem
A confused deputy is a program with legitimate authority that another party tricks into using that authority for the wrong purpose. Here the vendor is the deputy.
Suppose the vendor asks each customer for a role ARN and then scans that role's account. The attack runs like this:
- Customer A creates
arn:aws:iam::111111111111:role/VendorScan. It trusts the vendor account333333333333. - An attacker is also a customer of the vendor. The attacker learns or guesses Customer A's role ARN.
- The attacker registers that ARN as their own account in the vendor's console.
- The vendor's backend assumes Customer A's role with the vendor's own credentials. The trust policy allows it, because the caller really is account
333333333333. - The vendor returns scan results to the attacker, so the attacker reads data from Customer A's account.
Nothing in the trust policy was violated. The vendor was fooled about whose request it was serving. The external ID closes this gap.
How an external ID works
An external ID is a string that the role's trust policy requires in the AssumeRole call. You enforce it with the sts:ExternalId condition key. If the caller omits the value or sends a different one, STS rejects the request.
The protection depends on who generates the value and how the vendor uses it:
- The vendor generates a unique external ID for each customer. The customer does not choose it.
- The vendor stores the ID with the customer record.
- When the vendor assumes a role, it looks up the external ID for the customer who made the request. It never accepts the ID from the request.
In the attack above, the attacker is a different vendor customer with a different external ID. The vendor passes the attacker's ID, and Customer A's trust policy rejects it. The attacker cannot supply Customer A's ID, because the vendor never shows one customer's ID to another.
The customer also must not choose their own value. If customers could pick IDs, an attacker could register the victim's role ARN together with a copy of the victim's external ID, if they could get hold of it. Vendor-generated values avoid that.
Writing the trust policy
This policy is in Customer A's account. It trusts the vendor account but only when the external ID matches:
{
"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" }
}
}
]
}Three details are worth knowing:
StringEqualscompares the value exactly. Case matters.- An external ID can be 2 to 1224 characters long. It may contain letters, digits, and these symbols:
+ = , . @ : / -and the underscore. - The
:rootprincipal delegates the decision to the vendor's account. Any principal in that account that the vendor has allowed to callsts:AssumeRoleon your role can then assume it. If the vendor gives you a specific role ARN, such asarn:aws:iam::333333333333:role/ScannerService, use that instead. A narrower principal limits who inside the vendor can reach your account.
You still need the permissions policy on the role. It decides what the vendor can do after assuming the role. Grant read-only access to what the service needs and nothing more. The external ID does not limit actions. It only controls who may assume the role.
Assuming the role with the external ID
The vendor passes the value with --external-id:
aws sts assume-role \
--role-arn arn:aws:iam::111111111111:role/VendorScan \
--role-session-name vendor-scan \
--external-id example-external-id-123The vendor's backend usually does this in code. This boto3 sketch shows the important part, which is that the ID comes from the vendor's own records, not from the incoming request:
import boto3
# Stand-in for the vendor's database: customer record -> role ARN and external ID.
CUSTOMERS = {
"cust-001": {
"role_arn": "arn:aws:iam::111111111111:role/VendorScan",
"external_id": "example-external-id-123",
},
}
def session_for_customer(customer_id: str) -> boto3.Session:
record = CUSTOMERS[customer_id] # look up by authenticated customer
sts = boto3.client("sts")
response = sts.assume_role(
RoleArn=record["role_arn"],
RoleSessionName="vendor-scan",
ExternalId=record["external_id"],
DurationSeconds=3600,
)
creds = response["Credentials"]
return boto3.Session(
aws_access_key_id=creds["AccessKeyId"],
aws_secret_access_key=creds["SecretAccessKey"],
aws_session_token=creds["SessionToken"],
)The key point is that customer_id comes from the authenticated session. The role ARN and external ID both come from the stored record for that customer.
Using an external ID in a CLI profile
You can also use an external ID in a profile in ~/.aws/config, for example when testing the role yourself:
[profile vendor-test]
role_arn = arn:aws:iam::111111111111:role/VendorScan
source_profile = default
external_id = example-external-id-123
region = us-east-1Then confirm that the profile works:
aws sts get-caller-identity --profile vendor-testThe AWS console's "Switch Role" feature has no field for an external ID. A role whose trust policy requires one cannot be used through console role switching. This is acceptable for vendor roles, which are meant for programmatic access.
What the external ID is not
Intermediate learners often treat the external ID as a password. It is not one.
- AWS does not treat it as a secret. It appears in the trust policy, which anyone with IAM read access to the role can see. It also appears in CloudTrail records of the
AssumeRolecall. - It does not authenticate the vendor. The trust policy's
Principaldoes that. The external ID only lets the vendor prove which of its customers it is acting for. - It does not replace least privilege. The role's permissions policy still limits damage.
Its strength comes from the vendor's process: unique per customer, generated by the vendor, and never taken from a request.
Common mistakes and failures
| Symptom | Likely cause | Fix |
|---|---|---|
| `AccessDenied` when the vendor assumes the role | The call omitted `ExternalId`, or sent a different value | Compare the value in the trust policy with the one stored for your account at the vendor |
| `AccessDenied` even with the right value | Value differs in case or has trailing whitespace | `StringEquals` is exact. Copy the value without added characters |
| The role works from the CLI but not from the console | Console role switching cannot send an external ID | Use the CLI, an SDK, or a profile with `external_id` |
| Trust policy has the condition but no principal for the vendor | Wrong or mistyped vendor account ID | Confirm the account ID in the vendor's onboarding documentation |
| Anyone in the vendor account can assume the role | Principal uses `:root` | Ask the vendor for a specific role ARN and use it as the principal |
When a call fails, search CloudTrail for the AssumeRole event in the role's account. The errorMessage field shows why STS refused the request. Recent entries show the caller identity and session name, which helps you tell the vendor's failures from your own tests.
Choosing between an external ID and other conditions
The external ID solves a specific problem: a multi-tenant third party acting for many customers. If you only trust principals in your own organization, you do not need one. The aws:PrincipalOrgID condition, covered in the next chapter, is a better fit there. You can also combine conditions in one trust policy. For example, you could require both an external ID and MFA, although a vendor's automated backend cannot supply MFA, so that combination suits human callers only.
Exercise
A vendor's role in account 333333333333 assumes your role using the vendor's own credentials. Your trust policy names account 333333333333 and requires sts:ExternalId to equal abc-123. An attacker who is another customer of the vendor registers your role ARN with the vendor. Why does the attacker's request fail, and what must the vendor do for that to hold?
Show answer
The vendor looks up the external ID for the attacker's customer record and passes that value in AssumeRole. It does not match abc-123, so STS denies the request. For this to hold, the vendor must generate a unique external ID per customer, store it itself, and take it from its own records instead of from the request. The customer must not be able to choose or see another customer's ID.
Exercise
Your trust policy requires sts:ExternalId to equal Example-ID. The vendor sends example-id. What happens, and why?
Show answer
The call fails with AccessDenied. StringEquals compares exactly and is case-sensitive, so the two values do not match.