Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import boto3, argparse, random, string
- parser = argparse.ArgumentParser(description='Configure a new IAM user in AWS')
- parser.add_argument('-p','--password',action='store_true',help='Set a password (optional)')
- parser.add_argument('user',action='store',help='Username (typically first.last)')
- args = parser.parse_args()
- iam = boto3.client('iam',region_name='us-east-1')
- try:
- response = iam.create_user(UserName=args.user)
- username = response['User']['UserName']
- print('{} created successfully'.format(username))
- except:
- print('Something went wrong creating the user...')
- try:
- response = iam.add_user_to_group(UserName=args.user,GroupName='Developers')
- print('Added to the Developers group')
- except:
- print('Something went wrong adding the user to the Developers group...')
- try:
- response = iam.create_access_key(UserName=args.user)
- key = response['AccessKey']['AccessKeyId']
- secret = response['AccessKey']['SecretAccessKey']
- print('ACCESS_KEY={}'.format(key))
- print('SECRET_ACCESS_KEY={}'.format(secret))
- except:
- print('Something went wrong creating the user access key...')
- if args.password:
- try:
- chars = random.choices(string.ascii_uppercase,k=1) # Force an upper case char
- chars += random.choices(string.ascii_lowercase,k=1) # Force a lower case char
- chars += random.choices(string.punctuation,k=1) # Force a symbol
- chars += random.choices(string.digits,k=1) # Force a number
- # Add a random assortment of characters to get us to 16
- chars += random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits + string.punctuation, k=12)
- # Mix it up
- random.shuffle(chars)
- # Convert array to string
- newpass = ''.join(chars)
- response = iam.create_login_profile(UserName=args.user,Password=newpass,PasswordResetRequired=True)
- print('Password: {}\nUser must change password at next logon.'.format(newpass))
- except:
- print('Something went wrong creating the password...')
Add Comment
Please, Sign In to add comment