Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. import boto3
  2.  
  3. from os.path import expanduser
  4. import ConfigParser
  5.  
  6. print ('\n\n--------------------------HOW TO--------------------------------------')
  7. print ('sts:assumerole* allows a user or system to inject a policy to further restict the user\'s permission during that session. To demonstrate this the script will prompt you for the following: \n\n')
  8. print('Base Profile \t\t This is the profile that will be used to assume the role that the session policy will be applied on. This role / user need the sts:assume permission on the session role.')
  9. print('Session Profile \t The script will store the new session in you AWS credentials file under this name.')
  10. print('AWS Region \t\t Will add the region to the credentials file.')
  11. print('Role to assume \t\t This is the role that the base role will assume and apply the session policy to. This demo works best on a administrative type role.')
  12. print('Session Policy Arn \t The policy that will be overlayed on the session. This will default to the AWS managed policy for S3 read access. Note this policy needs to be in the same account as the role that will be assumed.')
  13. print ('----------------------------------------------------------------------\n\n')
  14.  
  15. AWS_CONFIG_FILE = '/.aws/credentials'
  16.  
  17. AWS_CLI_BASE_PROFILE = raw_input('Base Profile [default]:') or 'default'
  18. SESSION_PROFILE_NAME = raw_input('Store Session\'d Profile As [session_test]:') or 'session_test'
  19. REGION = raw_input('AWS Region [eu-west-1]:') or 'eu-west-1'
  20.  
  21. ROLE_TO_ASSUME_WITH_SESSION = raw_input('Role ARN To Assume:')
  22. SESSION_POLICY_ARN = raw_input('Session Policy Arn [arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess]:') or 'arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess'
  23.  
  24. print ('\n\n--------------------------CONFIG--------------------------------------')
  25. print('Base Profile \t\t {0}'.format(AWS_CLI_BASE_PROFILE))
  26. print('Session Profile \t {0}'.format(SESSION_PROFILE_NAME))
  27. print('AWS Region \t\t {0}'.format(REGION))
  28. print('Role to assume \t\t {0}'.format(ROLE_TO_ASSUME_WITH_SESSION))
  29. print('Session Policy Arn \t {0}'.format(SESSION_POLICY_ARN))
  30. print ('----------------------------------------------------------------------\n\n')
  31.  
  32. base_profile_cli_creds = boto3.session.Session(profile_name=AWS_CLI_BASE_PROFILE, region_name=REGION)
  33. base_client = base_profile_cli_creds.client('sts')
  34.  
  35. session_role_credentials = base_client.assume_role(
  36. RoleArn=ROLE_TO_ASSUME_WITH_SESSION,
  37. RoleSessionName=str("session-policy-demo"),
  38. PolicyArns = [{
  39. 'arn': SESSION_POLICY_ARN
  40. }],
  41. DurationSeconds=900
  42. )
  43.  
  44. session_credentials = session_role_credentials['Credentials']
  45.  
  46. # Write the new token to AWS credentials file
  47. home = expanduser("~")
  48. filename = home + AWS_CONFIG_FILE
  49.  
  50. config = ConfigParser.RawConfigParser()
  51. config.read(filename)
  52.  
  53. if not config.has_section(SESSION_PROFILE_NAME):
  54. config.add_section(SESSION_PROFILE_NAME)
  55.  
  56. config.set(SESSION_PROFILE_NAME, 'output', 'json')
  57. config.set(SESSION_PROFILE_NAME, 'region', REGION)
  58. config.set(SESSION_PROFILE_NAME, 'aws_access_key_id', session_credentials['AccessKeyId'])
  59. config.set(SESSION_PROFILE_NAME, 'aws_secret_access_key', session_credentials['SecretAccessKey'])
  60. config.set(SESSION_PROFILE_NAME, 'aws_session_token', session_credentials['SessionToken'])
  61.  
  62. with open(filename, 'w+') as configfile:
  63. config.write(configfile)
  64.  
  65. print ('\n\n---------------------------RESULT-------------------------------------')
  66. print ('Your new access key pair has been stored in the AWS configuration file {0} under the {1} profile.'.format(filename, SESSION_PROFILE_NAME))
  67. print ('Note that it will expire at {0}.'.format(session_credentials['Expiration']))
  68. print ('After this time, you may safely rerun this script to refresh your access key pair.')
  69. print ('To use this credential, call the AWS CLI with the --profile option (e.g. aws --profile {0} ec2 describe-instances).'.format(SESSION_PROFILE_NAME))
  70. print ('----------------------------------------------------------------------\n\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement