kms.tf 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. resource "aws_kms_key" "ghe_backup_data" {
  2. description = "EFS for Github Backup Server"
  3. policy = data.aws_iam_policy_document.ghe_backup_data_policy.json
  4. enable_key_rotation = true
  5. }
  6. resource "aws_kms_alias" "ghe_backup_data" {
  7. name = "alias/ghe_backup_data"
  8. target_key_id = aws_kms_key.ghe_backup_data.key_id
  9. }
  10. data "aws_iam_policy_document" "ghe_backup_data_policy" {
  11. policy_id = "ghe_backup_policy"
  12. statement {
  13. sid = "Enable IAM User Permissions"
  14. effect = "Allow"
  15. principals {
  16. type = "AWS"
  17. identifiers = ["arn:${var.aws_partition}:iam::${var.aws_account_id}:root"]
  18. }
  19. actions = ["kms:*"]
  20. resources = ["*"]
  21. }
  22. statement {
  23. sid = "Allow access for Key Administrators"
  24. effect = "Allow"
  25. principals {
  26. type = "AWS"
  27. identifiers = [
  28. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  29. ]
  30. }
  31. actions = [
  32. "kms:Create*",
  33. "kms:Describe*",
  34. "kms:Enable*",
  35. "kms:List*",
  36. "kms:Put*",
  37. "kms:Update*",
  38. "kms:Revoke*",
  39. "kms:Disable*",
  40. "kms:Get*",
  41. "kms:Delete*",
  42. "kms:TagResource",
  43. "kms:UntagResource",
  44. "kms:ScheduleKeyDeletion",
  45. "kms:CancelKeyDeletion",
  46. ]
  47. resources = ["*"]
  48. }
  49. statement {
  50. sid = "Allow use of the key"
  51. effect = "Allow"
  52. principals {
  53. type = "AWS"
  54. identifiers = [
  55. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  56. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/msoc-default-instance-role",
  57. # Portal was in legacy, but doesn't make sense. Removing, but leaving commented for now in case we need to re-add it.
  58. # "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/portal-instance-role",
  59. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling",
  60. ]
  61. }
  62. actions = [
  63. "kms:Encrypt",
  64. "kms:Decrypt",
  65. "kms:ReEncrypt*",
  66. "kms:GenerateDataKey*",
  67. "kms:DescribeKey",
  68. ]
  69. resources = ["*"]
  70. }
  71. statement {
  72. sid = "Allow attachment of persistent resources"
  73. effect = "Allow"
  74. principals {
  75. type = "AWS"
  76. identifiers = [
  77. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  78. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/msoc-default-instance-role",
  79. # Portal was in legacy, but doesn't make sense. Removing, but leaving commented for now in case we need to re-add it.
  80. #"arn:${var.aws_partition}:iam::${var.aws_account_id}:role/portal-instance-role",
  81. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling",
  82. ]
  83. }
  84. actions = [
  85. "kms:CreateGrant",
  86. "kms:ListGrants",
  87. "kms:RevokeGrant",
  88. ]
  89. resources = ["*"]
  90. condition {
  91. test = "Bool"
  92. variable = "kms:GrantIsForAWSResource"
  93. values = ["true"]
  94. }
  95. }
  96. # Basically copied from the default key AWS makes, hopefully improved to
  97. # make it work in multiple AWS regions with a single policy
  98. statement {
  99. sid = "elasticfilesystem"
  100. effect = "Allow"
  101. principals {
  102. type = "AWS"
  103. identifiers = ["*"]
  104. }
  105. actions = [
  106. "kms:Encrypt",
  107. "kms:Decrypt",
  108. "kms:ReEncrypt*",
  109. "kms:GenerateDataKey*",
  110. "kms:CreateGrant",
  111. "kms:DescribeKey",
  112. ]
  113. resources = ["*"]
  114. # https://docs.aws.amazon.com/efs/latest/ug/logging-using-cloudtrail.html#efs-encryption-cloudtrail
  115. condition {
  116. test = "StringEquals"
  117. variable = "kms:CallerAccount"
  118. values = [
  119. "055650462987", # US East (N. Virginia)
  120. "771736226457", # US East (Ohio)
  121. "208867197265", # US West (N. California)
  122. "736298361104", # US West (Oregon)
  123. "167972735943", # US GovCloud (East)
  124. "174619389399", # US GovCloud (West)
  125. ]
  126. }
  127. # https://docs.aws.amazon.com/kms/latest/developerguide/policy-conditions.html#conditions-kms-via-service
  128. condition {
  129. test = "StringEquals"
  130. variable = "kms:ViaService"
  131. values = [
  132. "elasticfilesystem.us-east-1.amazonaws.com",
  133. "elasticfilesystem.us-east-2.amazonaws.com",
  134. "elasticfilesystem.us-west-1.amazonaws.com",
  135. "elasticfilesystem.us-west-2.amazonaws.com",
  136. "elasticfilesystem.us-gov-east-1.amazonaws.com",
  137. "elasticfilesystem.us-gov-west-1.amazonaws.com",
  138. ]
  139. }
  140. }
  141. }