codebuild.tf 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #-----------------------------------------------------------------------
  2. # Common AssumeRole policy for these codebuild roles
  3. #-----------------------------------------------------------------------
  4. data "aws_iam_policy_document" "codebuild_role_assume_role_policy" {
  5. statement {
  6. effect = "Allow"
  7. actions = [
  8. "sts:AssumeRole"
  9. ]
  10. principals {
  11. type = "Service"
  12. identifiers = [
  13. "codebuild.amazonaws.com",
  14. "events.amazonaws.com"
  15. ]
  16. }
  17. }
  18. }
  19. #-----------------------------------------------------------------------
  20. # "Basic" Codebuild Role - not capable to make EC2 images / run Packer
  21. #-----------------------------------------------------------------------
  22. resource "aws_iam_role" "codebuild_basic_role" {
  23. name = "codebuild_basic_role"
  24. path = "/aws_services/"
  25. assume_role_policy = data.aws_iam_policy_document.codebuild_role_assume_role_policy.json
  26. }
  27. resource "aws_iam_role_policy_attachment" "codebuild_basic_role_basic_policy_attach" {
  28. role = aws_iam_role.codebuild_basic_role.name
  29. policy_arn = aws_iam_policy.codebuild_basic_policy.arn
  30. }
  31. #-----------------------------------------------------------------------
  32. # "Packer" Codebuild Role
  33. #-----------------------------------------------------------------------
  34. resource "aws_iam_role" "codebuild_packer_role" {
  35. name = "codebuild_packer_role"
  36. path = "/aws_services/"
  37. assume_role_policy = data.aws_iam_policy_document.codebuild_role_assume_role_policy.json
  38. }
  39. # Packer role needs basic role too for things like cloudwatch
  40. resource "aws_iam_role_policy_attachment" "codebuild_packer_role_basic_policy_attach" {
  41. role = aws_iam_role.codebuild_packer_role.name
  42. policy_arn = aws_iam_policy.codebuild_basic_policy.arn
  43. }
  44. resource "aws_iam_role_policy_attachment" "codebuild_packer_role_packer_policy_attach" {
  45. role = aws_iam_role.codebuild_packer_role.name
  46. policy_arn = aws_iam_policy.codebuild_build_ec2_amis_policy.arn
  47. }
  48. #-----------------------------------------------------------------------
  49. # "Basic" Policy for codebuild - can make artifacts and ECR images but not EC2
  50. # FIXME: Not sure about this policy
  51. # 2. Lets codebuild (apparently) write to ANY ECR repo
  52. # 4. Latest codebuild policies (from AWS console) have report-group resources and actions
  53. #-----------------------------------------------------------------------
  54. resource "aws_iam_policy" "codebuild_basic_policy" {
  55. name = "codebuild_basic_policy"
  56. path = "/aws_services/"
  57. description = "Policy for AWS codebuild to build AMIs"
  58. policy = data.aws_iam_policy_document.codebuild_base_policy.json
  59. }
  60. data "aws_iam_policy_document" "codebuild_base_policy" {
  61. statement {
  62. sid = "WriteCodebuildLogsToCloudwatchLogs"
  63. effect = "Allow"
  64. resources = [
  65. "arn:${local.aws_partition}:logs:${local.aws_region}:${local.aws_account}:log-group:/aws/codebuild/*"
  66. ]
  67. actions = [
  68. "logs:CreateLogGroup",
  69. "logs:CreateLogStream",
  70. "logs:PutLogEvents"
  71. ]
  72. }
  73. statement {
  74. sid = "StoreArtifactsInBucket"
  75. effect = "Allow"
  76. resources = [
  77. "arn:${local.aws_partition}:s3:::xdr-codebuild-artifacts/*"
  78. ]
  79. actions = [
  80. "s3:PutObject",
  81. "s3:GetObject*",
  82. "s3:ListBucket"
  83. ]
  84. }
  85. statement {
  86. sid = "UpdateECRRepos"
  87. effect = "Allow"
  88. resources = [
  89. "*"
  90. ]
  91. actions = [
  92. "ecr:GetAuthorizationToken",
  93. "ecr:BatchCheckLayerAvailability",
  94. "ecr:CompleteLayerUpload",
  95. "ecr:GetAuthorizationToken",
  96. "ecr:InitiateLayerUpload",
  97. "ecr:PutImage",
  98. "ecr:UploadLayerPart"
  99. ]
  100. }
  101. statement {
  102. sid = "LetEventBridgeTriggerABuild"
  103. effect = "Allow"
  104. resources = [
  105. "*"
  106. ]
  107. actions = [
  108. "codebuild:StartBuild",
  109. "codebuild:StopBuild",
  110. "codebuild:BatchGet*",
  111. "codebuild:Get*",
  112. "codebuild:List*"
  113. ]
  114. }
  115. }
  116. #-----------------------------------------------------------------------
  117. # "EC2" Policy for codebuild - able to build EC2 images / SGs / etc
  118. # FIXME: too powerful
  119. #
  120. # Parts of this are Lifted from
  121. # https://www.packer.io/plugins/builders/amazon#iam-task-or-instance-role and
  122. # converted from JSON to a terraform data source NOT AUDITED - taking Packer
  123. # docs at word that these are "minimal permissions necessary"
  124. #
  125. # The rest is for EBS+KMS support cobbled from AWS docs
  126. #-----------------------------------------------------------------------
  127. resource "aws_iam_policy" "codebuild_build_ec2_amis_policy" {
  128. name = "codebuild_build_ami_policy"
  129. path = "/aws_services/"
  130. description = "Policy for AWS codebuild to build AMIs"
  131. policy = data.aws_iam_policy_document.codebuild_build_ec2_amis.json
  132. }
  133. data "aws_iam_policy_document" "codebuild_build_ec2_amis" {
  134. statement {
  135. sid = "BuildEC2AMIFromPackerDocs"
  136. effect = "Allow"
  137. resources = ["*"]
  138. actions = [
  139. "ec2:AttachVolume",
  140. "ec2:AuthorizeSecurityGroupIngress",
  141. "ec2:CopyImage",
  142. "ec2:CreateImage",
  143. "ec2:CreateKeypair",
  144. "ec2:CreateSecurityGroup",
  145. "ec2:CreateSnapshot",
  146. "ec2:CreateTags",
  147. "ec2:CreateVolume",
  148. "ec2:DeleteKeyPair",
  149. "ec2:DeleteSecurityGroup",
  150. "ec2:DeleteSnapshot",
  151. "ec2:DeleteVolume",
  152. "ec2:DeregisterImage",
  153. "ec2:DescribeImageAttribute",
  154. "ec2:DescribeImages",
  155. "ec2:DescribeInstances",
  156. "ec2:DescribeInstanceStatus",
  157. "ec2:DescribeRegions",
  158. "ec2:DescribeSecurityGroups",
  159. "ec2:DescribeSnapshots",
  160. "ec2:DescribeSubnets",
  161. "ec2:DescribeTags",
  162. "ec2:DescribeVolumes",
  163. "ec2:DetachVolume",
  164. "ec2:GetPasswordData",
  165. "ec2:ModifyImageAttribute",
  166. "ec2:ModifyInstanceAttribute",
  167. "ec2:ModifySnapshotAttribute",
  168. "ec2:RegisterImage",
  169. "ec2:RunInstances",
  170. "ec2:StopInstances",
  171. "ec2:TerminateInstances"
  172. ]
  173. }
  174. statement {
  175. sid = "KMSAccessNeededForEBS"
  176. effect = "Allow"
  177. resources = ["*"]
  178. actions = [
  179. "kms:RevokeGrant",
  180. "kms:ListGrants",
  181. "kms:Decrypt",
  182. "kms:DescribeKey",
  183. "kms:GenerateDataKeyWithoutPlainText",
  184. "kms:ReEncrypt*",
  185. ]
  186. }
  187. statement {
  188. sid = "CreateGrantForEBS"
  189. effect = "Allow"
  190. resources = ["*"]
  191. actions = [
  192. "kms:CreateGrant",
  193. ]
  194. condition {
  195. test = "Bool"
  196. variable = "kms:GrantIsForAWSResource"
  197. values = ["true"]
  198. }
  199. }
  200. }