Last active
May 26, 2017 05:40
-
-
Save nathandines/45c3e8c27c704121b69b1cbfe79ee3d2 to your computer and use it in GitHub Desktop.
Just something quick which I hacked up which logs you into the AWS console using your Access Keys
This file contains 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 python | |
import argparse | |
import json | |
import urllib2 | |
import webbrowser | |
from urllib import urlencode | |
import boto3 | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--profile', type=str, default=None) | |
parser.add_argument('--to-terminal', action="store_true") | |
parser.add_argument('--url', type=str, default="https://console.aws.amazon.com/console/home") | |
args = parser.parse_args() | |
session = boto3.session.Session(profile_name=args.profile) | |
sts_client = session.client('sts') | |
my_identity = sts_client.get_caller_identity() | |
federation_token = sts_client.get_federation_token( | |
Name=my_identity['Arn'].split('/')[-1], | |
Policy=json.dumps( | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": "*", | |
"Resource": "*" | |
} | |
] | |
} | |
), | |
#DurationSeconds=1800 | |
) | |
signin_url = "https://signin.aws.amazon.com/federation" | |
url_params = { | |
"Action": "getSigninToken", | |
"Session": json.dumps( | |
{ | |
"sessionId": federation_token['Credentials']['AccessKeyId'], | |
"sessionKey": federation_token['Credentials']['SecretAccessKey'], | |
"sessionToken": federation_token['Credentials']['SessionToken'] | |
} | |
) | |
} | |
signin_token = json.loads(urllib2.urlopen(signin_url + "?" + urlencode(url_params)).read())['SigninToken'] | |
url_params = { | |
"Action": "login", | |
"Destination": args.url, | |
"SigninToken": signin_token | |
} | |
if not args.to_terminal: | |
webbrowser.open(signin_url + "?" + urlencode(url_params)) | |
else: | |
print(signin_url + "?" + urlencode(url_params)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment