kms.tf 2.8 KB

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