Last active
December 15, 2017 19:00
-
-
Save japsu/19633913ec821a9d8cb03cc761901505 to your computer and use it in GitHub Desktop.
Authenticate to AWS using MFA and output tokens in a format suitable for `eval $(aws-mfa 666666)`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import configparser | |
import sys | |
import boto3 | |
def main(token_code, serial_number): | |
sts = boto3.client('sts') | |
response = sts.get_session_token( | |
SerialNumber=serial_number, | |
TokenCode=token_code, | |
) | |
access_key_id = response['Credentials']['AccessKeyId'] | |
secret_access_key = response['Credentials']['SecretAccessKey'] | |
session_token = response['Credentials']['SessionToken'] | |
print(f'export AWS_ACCESS_KEY_ID={access_key_id}') | |
print(f'export AWS_SECRET_ACCESS_KEY={secret_access_key}') | |
print(f'export AWS_SESSION_TOKEN={session_token}') | |
if __name__ == '__main__': | |
config = configparser.ConfigParser() | |
config.read(os.path.expanduser('~/.aws/credentials')) | |
serial_number = config.get('default', 'mfa_serial') | |
token_code = sys.argv[1] | |
main(token_code, serial_number) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment