main.tf 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Roles carried over from the tf11 code have been commented out but may
  2. # need to be re-added.
  3. #
  4. # HOWEVER, it would be better to simply create an additional KMS key
  5. # with the corresponding service. This key is available as a fallback,
  6. # but better to create one per service.
  7. resource "aws_kms_key" "key" {
  8. description = var.description
  9. policy = data.aws_iam_policy_document.kms_policy.json
  10. tags = merge(
  11. var.standard_tags,
  12. { "Name" = var.name },
  13. var.tags
  14. )
  15. }
  16. resource "aws_kms_alias" "alias" {
  17. name = var.alias
  18. target_key_id = aws_kms_key.key.key_id
  19. }
  20. locals {
  21. iam_admins_legacy = [ "arn:${var.aws_partition}:iam::${var.aws_account_id}:root" ]
  22. iam_admins_tf12 = [
  23. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin", # MDRAdmin as a break glass
  24. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer" # Terraformer always gets full access
  25. ]
  26. }
  27. data "aws_iam_policy_document" "kms_policy" {
  28. policy_id = "${var.name}-policy"
  29. statement {
  30. sid = "Enable IAM User Permissions"
  31. effect = "Allow"
  32. principals {
  33. type = "AWS"
  34. identifiers = var.is_legacy ? local.iam_admins_legacy : local.iam_admins_tf12
  35. }
  36. actions = [ "kms:*" ]
  37. resources = [ "*" ]
  38. }
  39. statement {
  40. sid = "Allow access for Key Administrators"
  41. effect = "Allow"
  42. principals {
  43. type = "AWS"
  44. identifiers = concat(var.key_admin_arns, [ "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer" ])
  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"
  66. effect = "Allow"
  67. principals {
  68. type = "AWS"
  69. identifiers = concat(var.key_user_arns, [ "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer" ] )
  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 = concat(var.key_attacher_arns, ["arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer"])
  86. }
  87. actions = [
  88. "kms:CreateGrant",
  89. "kms:ListGrants",
  90. "kms:RevokeGrant"
  91. ]
  92. resources = [ "*" ]
  93. condition {
  94. test = "Bool"
  95. variable = "kms:GrantIsForAWSResource"
  96. values = [ "true" ]
  97. }
  98. }
  99. }