main.tf 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. resource "aws_kms_key" "key" {
  2. description = var.description
  3. enable_key_rotation = true
  4. policy = data.aws_iam_policy_document.kms_policy.json
  5. tags = merge(
  6. var.standard_tags,
  7. { "Name" = var.name },
  8. var.tags
  9. )
  10. }
  11. resource "aws_kms_alias" "alias" {
  12. name = var.alias
  13. target_key_id = aws_kms_key.key.key_id
  14. }
  15. data "aws_iam_policy_document" "kms_policy" {
  16. policy_id = "${var.name}-policy"
  17. statement {
  18. sid = "Enable IAM User Permissions"
  19. effect = "Allow"
  20. principals {
  21. type = "AWS"
  22. identifiers = [
  23. # The 'root' account is the entire account, we don't want that
  24. #"arn:${var.aws_partition}:iam::${var.aws_account_id}:root"
  25. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin", # MDRAdmin as a break glass
  26. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer" # Terraformer always gets full access
  27. ]
  28. }
  29. actions = ["kms:*"]
  30. resources = ["*"]
  31. }
  32. statement {
  33. sid = "Allow access for Key Administrators"
  34. effect = "Allow"
  35. principals {
  36. type = "AWS"
  37. identifiers = concat(var.key_admin_arns, ["arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer"])
  38. }
  39. actions = [
  40. "kms:Create*",
  41. "kms:Describe*",
  42. "kms:Enable*",
  43. "kms:List*",
  44. "kms:Put*",
  45. "kms:Update*",
  46. "kms:Revoke*",
  47. "kms:Disable*",
  48. "kms:Get*",
  49. "kms:Delete*",
  50. "kms:TagResource",
  51. "kms:UntagResource",
  52. "kms:ScheduleKeyDeletion",
  53. "kms:CancelKeyDeletion"
  54. ]
  55. resources = ["*"]
  56. }
  57. statement {
  58. sid = "Allow use of the key"
  59. effect = "Allow"
  60. principals {
  61. type = "AWS"
  62. identifiers = concat(var.key_user_arns, ["arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer"])
  63. }
  64. actions = [
  65. "kms:Encrypt",
  66. "kms:Decrypt",
  67. "kms:ReEncrypt*",
  68. "kms:GenerateDataKey*",
  69. "kms:DescribeKey"
  70. ]
  71. resources = ["*"]
  72. }
  73. statement {
  74. sid = "Allow attachment of persistent resources"
  75. effect = "Allow"
  76. principals {
  77. type = "AWS"
  78. identifiers = concat(var.key_attacher_arns, ["arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer"])
  79. }
  80. actions = [
  81. "kms:CreateGrant",
  82. "kms:ListGrants",
  83. "kms:RevokeGrant"
  84. ]
  85. resources = ["*"]
  86. # This condition is great, but means terraformer can't grant to the asg service
  87. # condition {
  88. # test = "Bool"
  89. # variable = "kms:GrantIsForAWSResource"
  90. # values = [ "true" ]
  91. #}
  92. }
  93. #statement {
  94. # sid = "Allow vmimport to decrypt SSE-KMS key"
  95. # effect = "Allow"
  96. # principals {
  97. # type = "AWS"
  98. # identifiers = [ "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/vmimport" ]
  99. # }
  100. # actions = [ "kms:*" ]
  101. # resources = [ "*" ]
  102. #}
  103. statement {
  104. sid = "Allow use of the key by external accounts"
  105. effect = "Allow"
  106. principals {
  107. type = "AWS"
  108. identifiers = var.remote_account_arns
  109. }
  110. actions = [
  111. "kms:ReEncryptFrom",
  112. "kms:DescribeKey"
  113. ]
  114. resources = ["*"]
  115. }
  116. }