A Principal that represents an MFA-authenticated user
See original GitHub issueWhen trying to define a role that is only assumable when MFA is set, the permission policy of the role can’t be modified but only complemented. This is due to the fact, that iam.Role
has its argument assumed_by
set as required, which automatically sets a policy statement in the permission policy. Later, when setting the MFA requirement via role.assume_role_policy.add_statements(iam.PolicyStatement(...))
it is required to set at least the arguments principals
, actions
, and conditions
. The problem is that this adds another policy statement to the permission policy, which renders the MFA condition useless and allows bypassing it.
Reproduction Steps
from aws_cdk import (
aws_iam as iam,
core,
)
class AssumeRoleStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
user = iam.User(self, 'myuser')
role = iam.Role(self, 'myrole',
assumed_by=iam.ArnPrincipal(user.user_arn))
role.add_managed_policy(iam.ManagedPolicy.from_aws_managed_policy_name('AdministratorAccess'))
role.assume_role_policy.add_statements(
iam.PolicyStatement(principals=[user],
actions=['sts:AssumeRole'],
conditions={'Bool': {'aws:MultiFactorAuthPresent': True}})
)
user.add_to_policy(iam.PolicyStatement(actions=['sts:AssumeRole'], resources=[role.role_arn]))
Resulting permission policy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::012345678910:user/assume-role-myuserZ09A543B-1ULCILBM447SF"
},
"Action": "sts:AssumeRole"
},
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::012345678910:user/assume-role-myuserZ09A543B-1ULCILBM447SF"
},
"Action": "sts:AssumeRole",
"Condition": {
"Bool": {
"aws:MultiFactorAuthPresent": "true"
}
}
}
]
}
Expected permission policy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::012345678910:user/assume-role-myuserZ09A543B-1ULCILBM447SF"
},
"Action": "sts:AssumeRole",
"Condition": {
"Bool": {
"aws:MultiFactorAuthPresent": "true"
}
}
}
]
}
Technical Details
CDK version: 1.20.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:10 (1 by maintainers)
Top Results From Across the Web
CloudTrail userIdentity element - AWS Documentation
The Amazon Resource Name (ARN) of the principal that made the call. ... mfaAuthenticated – The value is true if the root user...
Read more >Policies and permissions in IAM - Amazon Identity and Access ...
Amazon evaluates these policies when an IAM principal (user or role) ... In this case, the Condition evaluates to true when the user...
Read more >AWS IAM: Working, Components, and Features Explained
A principal is an entity that can perform actions on an AWS resource. ... or MFA-authenticated users to make changes to those buckets....
Read more >Solving the AWS Roles Mystery | Authomize.com
Think about how someone can temporarily become a super user in Linux with the sudo command, and you get the general idea. The...
Read more >AWS CloudTrail - Cyderes Documentation
AWS CloudTrail is an AWS service that helps you enable governance, compliance, and operational ... mfaAuthenticated, principal.user.labels.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Not sure if it is better, but I found a reasonably simple way to add the missing condition:
This is a generic way of adding any missing Properties from the intent-based constructor, down to the low level Cfn object
You can solve this yourself by writing an class that implements
IPrincipal
which returns the policy fragment that you need.