Sphere
Log in Join

Debugging AWS IAM: why sts:AssumeRole works in console but not CLI

I have a love-hate relationship with IAM, mostly hate on days like last Thursday.

We were setting up a deploy role so CI in account 111122223333 could push artifacts into 444455556666. I wrote the trust policy, attached the identity policy, and could assume the role just fine from the AWS console using my own user. From the CI runner, always the same error:

$ aws sts assume-role --role-arn arn:aws:iam::444455556666:role/DeployRole --role-session-name ci-deploy

An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam::111122223333:user/deploy-bot is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::444455556666:role/DeployRole

We were on AWS CLI 2.15.30 on the runner.

## Trust policy vs identity policy

The first thing to get straight is that there are two policies in play and both have to allow the call. The identity policy on deploy-bot grants sts:AssumeRole. The trust policy on DeployRole says who may assume it. A deny in either one wins, and an allow in one does not compensate for a missing allow in the other.

Both looked right. The trust policy had the account principal:

json
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:root" },
"Action": "sts:AssumeRole",
"Condition": { "Bool": { "aws:MultiFactorAuthPresent": "true" } }
}]
}

There it was, and there was the problem. I had copied that condition from an internal doc meant for humans.

## The condition that only passes in a browser

aws:MultiFactorAuthPresent is true for a console session that used an MFA device. An access key used by a CLI process has no MFA context, so the key is false, the condition fails, and you get AccessDenied even though the principal matches. The console worked because my own login had MFA on it. The CLI did not.

That is the whole bug. But here is the part I want to remember next time: IAM does not tell you which policy or which condition blocked you. The error is the same whether the trust policy is missing, the identity policy is missing, a condition failed, or an SCP is in the way.

## How I actually debug these now

I stopped guessing and started using the simulator, with the actual context passed in:

aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::111122223333:user/deploy-bot \
--action-names sts:AssumeRole \
--resource-arns arn:aws:iam::444455556666:role/DeployRole \
--context-entries Key=aws:MultiFactorAuthPresent,Value=false

It came back with "EvalDecision": "implicitDeny", which at least told me it was policy and not credentials or networking. For the trust side there is simulate-custom-policy where you paste the trust document directly.

Then I ran the same call as the role session to check the second half of the problem:

$ aws sts get-caller-identity
{
"UserId": "AROA...:ci-deploy",
"Account": "444455556666",
"Arn": "arn:aws:sts::444455556666:assumed-role/DeployRole/ci-deploy"
}

We removed the MFA condition from the CI role and kept it on the human role, where it belongs. MFA for machines is a different conversation and involves session tags or a (ses:<ticket>) value in the condition, which is a bigger change than a Thursday should allow.

One more thing: CloudTrail logged the failed AssumeRole every time, so we did have the evidence. I just did not think to look there for an hour. Pull the event first, it includes the errorMessage and the userAgent so you can confirm which credential actually made the call.