-
-
Save jvincentnz/9795334014d45a6fa416479096018dd3 to your computer and use it in GitHub Desktop.
Find an AWS IAM user corresponding to an AWS Access Key (boto3)
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
# Find the IAM username belonging to the TARGET_ACCESS_KEY | |
import boto3 | |
from botocore.exceptions import ClientError | |
iam = boto3.client('iam') | |
def find_user(key): | |
try: | |
key_info = iam.get_access_key_last_used(AccessKeyId=key) | |
return key_info['UserName'] | |
except ClientError as e: | |
print "Received error: %s", e | |
if e.response['Error']['Code'] == 'AccessDenied': | |
return "Key does not exist in target account" | |
try: | |
print find_user("AKIAXXXXXXXXXXXXXXXX") | |
except ClientError as e: | |
print "Received error: %s", e | |
if e.response['Error']['Code'] == 'ExpiredToken': | |
print "Please login to the target AWS account" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment