main.tf 3.0 KB

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