Skip to content

Instantly share code, notes, and snippets.

@atward
Last active August 8, 2022 02:10
Show Gist options
  • Save atward/248383f9623138db26da212e6e310cc4 to your computer and use it in GitHub Desktop.
Save atward/248383f9623138db26da212e6e310cc4 to your computer and use it in GitHub Desktop.
AWS Web console login via boto credentials (IAM access token/profile)
#!/usr/bin/env python
import boto3
import json
import sys
import urllib2
import webbrowser
from urllib import urlencode
AWS_SIGNIN_URL = "https://signin.aws.amazon.com/federation"
AWS_CONSOLE_URL = "https://console.aws.amazon.com/console/home"
def get_federation_token():
sts_client = boto3.client('sts')
my_identity = sts_client.get_caller_identity()
return sts_client.get_federation_token(
Name=my_identity['Arn'].split('/')[-1],
Policy=json.dumps(
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "*",
"Resource": "*"
}
]
}
)
)
def get_url(token, destination=AWS_CONSOLE_URL):
url_params = {
"Action": "getSigninToken",
"Session": json.dumps(
{
"sessionId": token['Credentials']['AccessKeyId'],
"sessionKey": token['Credentials']['SecretAccessKey'],
"sessionToken": token['Credentials']['SessionToken']
}
)
}
result = urllib2.urlopen(AWS_SIGNIN_URL + "?" + urlencode(url_params))
signin_token = json.loads(result.read())['SigninToken']
url_params = {
"Action": "login",
"Destination": destination,
"SigninToken": signin_token
}
return AWS_SIGNIN_URL + "?" + urlencode(url_params)
def main():
federation_token = get_federation_token()
if len(sys.argv) > 1:
url = get_url(federation_token, sys.argv[1])
else:
url = get_url(federation_token)
webbrowser.open(url)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment