kms.tf 3.5 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" {
  4. description = "Codebuild ${var.name}"
  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-${var.name}"
  10. target_key_id = aws_kms_key.s3_codebuild.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_terraformer",
  21. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin"
  22. ]
  23. }
  24. actions = ["kms:*"]
  25. resources = ["*"]
  26. }
  27. statement {
  28. sid = "Allow access for Key Administrators"
  29. effect = "Allow"
  30. principals {
  31. type = "AWS"
  32. identifiers = [
  33. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  34. ]
  35. }
  36. actions = [
  37. "kms:Create*",
  38. "kms:Describe*",
  39. "kms:Enable*",
  40. "kms:List*",
  41. "kms:Put*",
  42. "kms:Update*",
  43. "kms:Revoke*",
  44. "kms:Disable*",
  45. "kms:Get*",
  46. "kms:Delete*",
  47. "kms:TagResource",
  48. "kms:UntagResource",
  49. "kms:ScheduleKeyDeletion",
  50. "kms:CancelKeyDeletion"
  51. ]
  52. resources = ["*"]
  53. }
  54. statement {
  55. sid = "Allow use of the key"
  56. effect = "Allow"
  57. principals {
  58. type = "AWS"
  59. identifiers = [
  60. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/msoc-default-instance-role"
  61. ]
  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 access through Amazon S3 for all principals in the account that are authorized to use Amazon S3"
  74. effect = "Allow"
  75. principals {
  76. type = "AWS"
  77. identifiers = ["*"]
  78. }
  79. actions = [
  80. "kms:Encrypt",
  81. "kms:Decrypt",
  82. "kms:ReEncrypt*",
  83. "kms:GenerateDataKey*",
  84. "kms:DescribeKey"
  85. ]
  86. resources = ["*"]
  87. condition {
  88. test = "StringEquals"
  89. variable = "kms.ViaService"
  90. values = ["s3.${var.aws_region}.amazonaws.com"]
  91. }
  92. condition {
  93. test = "StringEquals"
  94. variable = "kms.CallerAccount"
  95. values = [var.aws_account_id]
  96. }
  97. }
  98. statement {
  99. sid = "Allow access from the codebuild role"
  100. effect = "Allow"
  101. principals {
  102. type = "AWS"
  103. identifiers = [
  104. aws_iam_role.codebuild_service_role.arn,
  105. ]
  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. }