himanshu208

Untitled

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