himanshu208

Untitled

Sep 1st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import boto3
  2. import json
  3.  
  4. # Create an S3 client
  5. client = boto3.client('kms', region_name='ap-southeast-2')
  6. #laydown policies for the key
  7. key_policy = {
  8. "Version": "2012-10-17",
  9. "Id": "key-consolepolicy-3",
  10. "Statement": [
  11. {
  12. "Sid": "Enable IAM User Permissions",
  13. "Effect": "Allow",
  14. "Principal": {
  15. "AWS": "arn:aws:iam::032418238795:root"
  16. },
  17. "Action": "kms:*",
  18. "Resource": "*"
  19. },
  20. {
  21. "Sid": "Allow access for Key Administrators",
  22. "Effect": "Allow",
  23. "Principal": {
  24. "AWS": "arn:aws:iam::032418238795:user/CITS5503/21999347@student.uwa.edu.au"
  25. },
  26. "Action": [
  27. "kms:Create*",
  28. "kms:Describe*",
  29. "kms:Enable*",
  30. "kms:List*",
  31. "kms:Put*",
  32. "kms:Update*",
  33. "kms:Revoke*",
  34. "kms:Disable*",
  35. "kms:Get*",
  36. "kms:Delete*",
  37. "kms:TagResource",
  38. "kms:UntagResource",
  39. "kms:ScheduleKeyDeletion",
  40. "kms:CancelKeyDeletion"
  41. ],
  42. "Resource": "*"
  43. },
  44. {
  45. "Sid": "Allow use of the key",
  46. "Effect": "Allow",
  47. "Principal": {
  48. "AWS": "arn:aws:iam::032418238795:user/CITS5503/21999347@student.uwa.edu.au"
  49. },
  50. "Action": [
  51. "kms:Encrypt",
  52. "kms:Decrypt",
  53. "kms:ReEncrypt*",
  54. "kms:GenerateDataKey*",
  55. "kms:DescribeKey"
  56. ],
  57. "Resource": "*"
  58. },
  59. {
  60. "Sid": "Allow attachment of persistent resources",
  61. "Effect": "Allow",
  62. "Principal": {
  63. "AWS": "arn:aws:iam::032418238795:user/CITS5503/21999347@student.uwa.edu.au"
  64. },
  65. "Action": [
  66. "kms:CreateGrant",
  67. "kms:ListGrants",
  68. "kms:RevokeGrant"
  69. ],
  70. "Resource": "*",
  71. "Condition": {
  72. "Bool": {
  73. "kms:GrantIsForAWSResource": "true"
  74. }
  75. }
  76. }
  77. ]
  78. }
  79. #serialise key policies
  80. key_policy = json.dumps(key_policy)
  81. try:
  82. response = client.create_key(
  83. Policy=key_policy,
  84. Description='description',
  85. KeyUsage='ENCRYPT_DECRYPT',
  86. Origin='AWS_KMS'
  87. )
  88. aliasname = "alias/21999347-1231"
  89. cmk_key_arn = response['KeyMetadata']['Arn']
  90. client.create_alias(AliasName=aliasname, TargetKeyId=cmk_key_arn)
  91. res = client.describe_key(KeyId=aliasname)
  92. # assert res['KeyMetadata']['Arn'] == cmk_key_arn
  93. print("your Key id is:")
  94. print (res['KeyMetadata']['KeyId'])
  95. except Exception as error:
  96. print(error)
Add Comment
Please, Sign In to add comment