main.tf 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. data "aws_iam_policy_document" "kms_policy" {
  21. policy_id = "${var.name}-policy"
  22. statement {
  23. sid = "Enable IAM User Permissions"
  24. effect = "Allow"
  25. principals {
  26. type = "AWS"
  27. identifiers = [
  28. # The 'root' account is the entire account, we don't want that
  29. #"arn:${var.aws_partition}:iam::${var.aws_account_id}:root"
  30. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin", # MDRAdmin as a break glass
  31. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer" # Terraformer always gets full access
  32. ]
  33. }
  34. actions = [ "kms:*" ]
  35. resources = [ "*" ]
  36. }
  37. statement {
  38. sid = "Allow access for Key Administrators"
  39. effect = "Allow"
  40. principals {
  41. type = "AWS"
  42. identifiers = concat(var.key_admin_arns, [ "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer" ])
  43. }
  44. actions = [
  45. "kms:Create*",
  46. "kms:Describe*",
  47. "kms:Enable*",
  48. "kms:List*",
  49. "kms:Put*",
  50. "kms:Update*",
  51. "kms:Revoke*",
  52. "kms:Disable*",
  53. "kms:Get*",
  54. "kms:Delete*",
  55. "kms:TagResource",
  56. "kms:UntagResource",
  57. "kms:ScheduleKeyDeletion",
  58. "kms:CancelKeyDeletion"
  59. ]
  60. resources = [ "*" ]
  61. }
  62. statement {
  63. sid = "Allow use of the key"
  64. effect = "Allow"
  65. principals {
  66. type = "AWS"
  67. identifiers = concat(var.key_user_arns, [ "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer" ] )
  68. }
  69. actions = [
  70. "kms:Encrypt",
  71. "kms:Decrypt",
  72. "kms:ReEncrypt*",
  73. "kms:GenerateDataKey*",
  74. "kms:DescribeKey"
  75. ]
  76. resources = [ "*" ]
  77. }
  78. statement {
  79. sid = "Allow attachment of persistent resources"
  80. effect = "Allow"
  81. principals {
  82. type = "AWS"
  83. identifiers = concat(var.key_attacher_arns, ["arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer"])
  84. }
  85. actions = [
  86. "kms:CreateGrant",
  87. "kms:ListGrants",
  88. "kms:RevokeGrant"
  89. ]
  90. resources = [ "*" ]
  91. condition {
  92. test = "Bool"
  93. variable = "kms:GrantIsForAWSResource"
  94. values = [ "true" ]
  95. }
  96. }
  97. }