kms.tf 2.5 KB

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