Using the AWS CLI to Configure a Role Profile With MFA
ACM.226 Automating CLI configuration for an IAM role that requires MFA
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
⚙️ Part of my series on Automating Cybersecurity Metrics. The Code.
🔒 Related Stories: MFA | Passwords | IAM
💻 Free Content on Jobs in Cybersecurity | ✉️ Sign up for the Email List
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In my last post in this series I explained how you can get around some of the weaknesses in AWS Identity Center (formerly SSO).
I showed you in a prior post how to configure the AWS CLI manually with a role that requires MFA by editing the config file.
In this post I’ll show you how to do the same thing with the CLI which lends itself to automation. We might want to auto-configure compute resources with roles that we will use later. Automatic configuration can help prevent errors and make security easier for users, which is always nice!
A note on non-repudiation
Credentials should never be shared to ensure non-repudiation.
However, by enforcing MFA in the trust policy and if only one person has the MFA device that can help with non-repudiation. You might also think about how the user that owns the credentials can securely provide the inputs to this automated process rather than having an administrator access the user credentials.
AWS CLI configure set
We’ll use the AWS CLI configure set command:
If you take a look at the examples, you can set pretty much anything that we configured manually in the config file with the aws configure set command.

We can easily use the aws configure set command to create a cross-account role that requires MFA and an external id with this reusable code:
#this code presumes you ran aws configure and set the keys
#for the default profile. This new profile gets credentials from that
#default proflie
region="[your aws region here]"
role_arn="[your role arn here]"
mfa_serial="[your mfa device arn here]"
externalid="[your external id here]"
aws configure set region $region --profile $profile
aws configure set role_arn $role_arn --profile $profile
aws configure set mfa_serial $mfa_serial --profile $profile
aws configure set externalid $externalid --profile $profile
aws configure set source_profile "default" --profile $profile
aws configure set output "json" --profile $profileYou could also pre-populate the AWS keys for the default profile, if appropriate, but only if you are enforcing MFA, otherwise use an AWS role instead.
#make sure the aws secret key and access key are not visible in the code
#or logs where someone who shouldn't have access to the keys can see them
accesskey=[your access key]
secretkey=[your secret key]
aws configure set aws_access_key_id $accesskey
aws configure set aws_secret_access_key $secretkeyCredential visibility in logs
You should ensure that the aws access key id and aws secret access key are never accessible in logs, source code, or bash history except to the people who are supposed to use them. You can monitor for unauthorized use of the credentials on a network or device other than the one where the credentials should be used.
I explain how credentials show up in logs in my posts on automating git configuration.
Validating the configuration
As always after running your commands you can validate that they created the correct configuration by checking the aws config file.
cat ~/.aws/configand check that your credentials are added to:
cat ~/.aws/credentialsRemember that anyone on the host can read those files which is why I recommend only using hard coded credentials if you are requiring MFA in the trust policy for specific users (not just any MFA). Otherwise use an AWS role.
Follow for updates.
Teri Radichel | © 2nd Sight Lab 2023
About Teri Radichel:
~~~~~~~~~~~~~~~~~~~~
⭐️ Author: Cybersecurity Books
⭐️ Presentations: Presentations by Teri Radichel
⭐️ Recognition: SANS Award, AWS Security Hero, IANS Faculty
⭐️ Certifications: SANS ~ GSE 240
⭐️ Education: BA Business, Master of Software Engineering, Master of Infosec
⭐️ Company: Penetration Tests, Assessments, Phone Consulting ~ 2nd Sight LabNeed Help With Cybersecurity, Cloud, or Application Security?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
🔒 Request a penetration test or security assessment
🔒 Schedule a consulting call
🔒 Cybersecurity Speaker for PresentationFollow for more stories like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
❤️ Sign Up my Medium Email List
❤️ Twitter: @teriradichel
❤️ LinkedIn: https://www.linkedin.com/in/teriradichel
❤️ Mastodon: @teriradichel@infosec.exchange
❤️ Facebook: 2nd Sight Lab
❤️ YouTube: @2ndsightlab
