#aws/cookbook/security
As an IAM Engineer, it is always our responsibility to ensure high privileged permissions are not always in use.
Create a role using an IAM Policy that will allow the role to be assumed later.
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "PRINCIPAL_ARN"
},
"Action": "sts:AssumeRole"
}
]
}
PRINCIPAL_ARN=$(aws sts get-caller-identity --query Arn --output text)
assume-role-policy-template.json
and generate the assume-role-policy.json
.ROLE_ARN=$(aws iam create-role --role-name AWSCookBookSuryenduRole101 \
--assume-role-policy-document file://assume-role-policy.json \
--output text --query Role.Arn )
PowerUserAccess policy is designed to grant full access to AWS services and resources for users without giving them permissions to manage IAM users, groups, and roles.
aws iam attach-role-policy --role-name AWSCookBookSuryenduRole101 \ --policy-arn arn:aws:iam::aws:policy/PowerUserAccess
Power User Access Policy JSON
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "NotAction": [ "iam:*", "organizations:*", "account:*" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "iam:CreateServiceLinkedRole", "iam:DeleteServiceLinkedRole", "iam:ListRoles", "organizations:DescribeOrganization", "account:ListRegions", "account:GetAccountInformation" ], "Resource": "*" } ] }
aws sts assume-role --role-arn $ROLE_ARN \
--role-session-name AWSSuryendu101
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "PRINCIPAL_ARN"
},
"Action": "sts:AssumeRole",
"Condition": {
"Bool": {
"aws:MultiFactorAuthPresent": "***"
}
}
}
]
}
aws iam update-assume-role-policy --role-name AWSCookBookSuryenduRole101 --policy-document file://assume-role-policy.json --profile root
aws sts assume-role --role-arn "arn:aws:iam::471112586770:role/AWSCookBookSuryenduRole101" --role-session-name AWSSuryendu101
This will result in error.
An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam::471112586770:user/admin is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::471112586770:role/AWSCookBookSuryenduRole101
To assume the role with MFA using the AWS CLI, you need the serial number of the MFA device and the current MFA token code.
```aws sts assume-role
–role-arn “arn:aws:iam::471112586770:role/AWSCookBookSuryenduRole101”
–role-session-name “SessionName”
–serial-number “arn:aws:iam::471112586770:mfa/admin”
–token-code “767”
```
We will get similar output as we have seen before.