kms.tf 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. resource "aws_kms_key" "bucketkey" {
  2. description = "S3 KMS for ${var.name}."
  3. deletion_window_in_days = 30
  4. enable_key_rotation = true
  5. policy = data.aws_iam_policy_document.kms_key_policy.json
  6. tags = merge(var.standard_tags, var.tags)
  7. }
  8. resource "aws_kms_alias" "bucketkey" {
  9. name = "alias/${var.name}"
  10. target_key_id = aws_kms_key.bucketkey.key_id
  11. }
  12. data "aws_iam_policy_document" "kms_key_policy" {
  13. policy_id = var.name
  14. statement {
  15. sid = "Enable IAM User Permissions"
  16. effect = "Allow"
  17. principals {
  18. type = "AWS"
  19. identifiers = [
  20. "arn:${var.aws_partition}:iam::${var.aws_account_id}:root",
  21. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin",
  22. ]
  23. }
  24. actions = ["kms:*"]
  25. resources = ["*"]
  26. }
  27. statement {
  28. sid = "Allow access for Engineers"
  29. effect = "Allow"
  30. principals {
  31. type = "AWS"
  32. identifiers = [
  33. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin",
  34. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  35. ]
  36. }
  37. actions = [
  38. "kms:Create*",
  39. "kms:Describe*",
  40. "kms:Enable*",
  41. "kms:List*",
  42. "kms:Put*",
  43. "kms:Update*",
  44. "kms:Revoke*",
  45. "kms:Disable*",
  46. "kms:Get*",
  47. "kms:Delete*",
  48. "kms:TagResource",
  49. "kms:UntagResource",
  50. "kms:ScheduleKeyDeletion",
  51. "kms:CancelKeyDeletion"
  52. ]
  53. resources = ["*"]
  54. }
  55. statement {
  56. sid = "Allow use of the key to encrypt and decrypt"
  57. effect = "Allow"
  58. principals {
  59. type = "AWS"
  60. identifiers = [
  61. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin",
  62. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  63. ]
  64. }
  65. actions = [
  66. "kms:Encrypt",
  67. "kms:Decrypt",
  68. "kms:ReEncrypt*",
  69. "kms:GenerateDataKey*",
  70. "kms:DescribeKey"
  71. ]
  72. resources = ["*"]
  73. }
  74. statement {
  75. sid = "Allow attachment of persistent resources"
  76. effect = "Allow"
  77. principals {
  78. type = "AWS"
  79. identifiers = [
  80. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin",
  81. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  82. ]
  83. }
  84. actions = [
  85. "kms:CreateGrant",
  86. "kms:ListGrants",
  87. "kms:RevokeGrant"
  88. ]
  89. resources = ["*"]
  90. condition {
  91. test = "Bool"
  92. variable = "kms:GrantIsForAWSResource"
  93. values = ["true"]
  94. }
  95. }
  96. # TODO: Do we need to grant read access to other accounts?
  97. }