Guest User

Untitled

a guest
Aug 4th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import boto3, argparse, random, string
  4.  
  5. parser = argparse.ArgumentParser(description='Configure a new IAM user in AWS')
  6. parser.add_argument('-p','--password',action='store_true',help='Set a password (optional)')
  7. parser.add_argument('user',action='store',help='Username (typically first.last)')
  8. args = parser.parse_args()
  9.  
  10. iam = boto3.client('iam',region_name='us-east-1')
  11.  
  12. try:
  13. response = iam.create_user(UserName=args.user)
  14. username = response['User']['UserName']
  15. print('{} created successfully'.format(username))
  16. except:
  17. print('Something went wrong creating the user...')
  18.  
  19. try:
  20. response = iam.add_user_to_group(UserName=args.user,GroupName='Developers')
  21. print('Added to the Developers group')
  22. except:
  23. print('Something went wrong adding the user to the Developers group...')
  24.  
  25. try:
  26. response = iam.create_access_key(UserName=args.user)
  27. key = response['AccessKey']['AccessKeyId']
  28. secret = response['AccessKey']['SecretAccessKey']
  29. print('ACCESS_KEY={}'.format(key))
  30. print('SECRET_ACCESS_KEY={}'.format(secret))
  31. except:
  32. print('Something went wrong creating the user access key...')
  33.  
  34. if args.password:
  35. try:
  36. chars = random.choices(string.ascii_uppercase,k=1) # Force an upper case char
  37. chars += random.choices(string.ascii_lowercase,k=1) # Force a lower case char
  38. chars += random.choices(string.punctuation,k=1) # Force a symbol
  39. chars += random.choices(string.digits,k=1) # Force a number
  40. # Add a random assortment of characters to get us to 16
  41. chars += random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits + string.punctuation, k=12)
  42. # Mix it up
  43. random.shuffle(chars)
  44. # Convert array to string
  45. newpass = ''.join(chars)
  46. response = iam.create_login_profile(UserName=args.user,Password=newpass,PasswordResetRequired=True)
  47. print('Password: {}\nUser must change password at next logon.'.format(newpass))
  48. except:
  49. print('Something went wrong creating the password...')
Add Comment
Please, Sign In to add comment