kms.tf 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Codebuild artifacts by rule must be encrypted by a KMS key
  2. # using the default aws/s3 key doesn't work with cross-account access
  3. resource "aws_kms_key" "s3_codebuild_artifacts" {
  4. description = "Codebuild Artifacts S3 bucket"
  5. enable_key_rotation = true
  6. policy = data.aws_iam_policy_document.codebuild_kms_key_encryption_policy.json
  7. }
  8. resource "aws_kms_alias" "codebuilt-artifacts" {
  9. name = "alias/codebuild-artifacts"
  10. target_key_id = aws_kms_key.s3_codebuild_artifacts.key_id
  11. }
  12. data "aws_iam_policy_document" "codebuild_kms_key_encryption_policy" {
  13. #policy_id = "key-consolepolicy-3"
  14. statement {
  15. sid = "Enable IAM User Permissions"
  16. effect = "Allow"
  17. principals {
  18. type = "AWS"
  19. identifiers = [
  20. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_feedmgmt_readonly",
  21. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  22. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin"
  23. ]
  24. }
  25. actions = ["kms:*"]
  26. resources = ["*"]
  27. }
  28. statement {
  29. sid = "Allow access for Key Administrators"
  30. effect = "Allow"
  31. principals {
  32. type = "AWS"
  33. identifiers = [
  34. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  35. ]
  36. }
  37. actions = [
  38. "kms:Create*",
  39. "kms:Describe*",
  40. "kms:Enable*",
  41. "kms:List*",
  42. "kms:Put*",
  43. "kms:Update*",
  44. "kms:Revoke*",
  45. "kms:Disable*",
  46. "kms:Get*",
  47. "kms:Delete*",
  48. "kms:TagResource",
  49. "kms:UntagResource",
  50. "kms:ScheduleKeyDeletion",
  51. "kms:CancelKeyDeletion"
  52. ]
  53. resources = ["*"]
  54. }
  55. statement {
  56. sid = "Allow use of the key"
  57. effect = "Allow"
  58. principals {
  59. type = "AWS"
  60. identifiers = [
  61. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/msoc-default-instance-role"
  62. ]
  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 access through Amazon S3 for all principals in the account that are authorized to use Amazon S3"
  75. effect = "Allow"
  76. principals {
  77. type = "AWS"
  78. identifiers = ["*"]
  79. }
  80. actions = [
  81. "kms:Encrypt",
  82. "kms:Decrypt",
  83. "kms:ReEncrypt*",
  84. "kms:GenerateDataKey*",
  85. "kms:DescribeKey"
  86. ]
  87. resources = ["*"]
  88. condition {
  89. test = "StringEquals"
  90. variable = "kms.ViaService"
  91. values = ["s3.${var.aws_region}.amazonaws.com"]
  92. }
  93. condition {
  94. test = "StringEquals"
  95. variable = "kms.CallerAccount"
  96. values = [var.aws_account_id]
  97. }
  98. }
  99. statement {
  100. sid = "Allow access from the codebuild role"
  101. effect = "Allow"
  102. principals {
  103. type = "AWS"
  104. # FIXME this needs to be a better role by far
  105. identifiers = [aws_iam_role.codebuild_role.arn]
  106. }
  107. actions = [
  108. "kms:Encrypt",
  109. "kms:Decrypt",
  110. "kms:ReEncrypt*",
  111. "kms:GenerateDataKey*",
  112. "kms:DescribeKey"
  113. ]
  114. resources = ["*"]
  115. }
  116. statement {
  117. sid = "Allow attachment of persistent resources"
  118. effect = "Allow"
  119. principals {
  120. type = "AWS"
  121. identifiers = [
  122. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/msoc-default-instance-role"
  123. ]
  124. }
  125. actions = [
  126. "kms:CreateGrant",
  127. "kms:ListGrants",
  128. "kms:RevokeGrant"
  129. ]
  130. resources = ["*"]
  131. condition {
  132. test = "Bool"
  133. variable = "kms:GrantIsForAWSResource"
  134. values = ["true"]
  135. }
  136. }
  137. }