kms.tf 2.7 KB

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