codebuild.tf 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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:CreateNetworkInterface",
  149. "ec2:CreateNetworkInterfacePermission",
  150. "ec2:DeleteKeyPair",
  151. "ec2:DeleteNetworkInterface",
  152. "ec2:DeleteSecurityGroup",
  153. "ec2:DeleteSnapshot",
  154. "ec2:DeleteVolume",
  155. "ec2:DeregisterImage",
  156. "ec2:Describe*",
  157. "ec2:DetachVolume",
  158. "ec2:GetPasswordData",
  159. "ec2:ModifyImageAttribute",
  160. "ec2:ModifyInstanceAttribute",
  161. "ec2:ModifySnapshotAttribute",
  162. "ec2:RegisterImage",
  163. "ec2:RunInstances",
  164. "ec2:StopInstances",
  165. "ec2:TerminateInstances"
  166. ]
  167. }
  168. statement {
  169. sid = "BuildEC2WithInstanceRole"
  170. effect = "Allow"
  171. resources = [ "*" ]
  172. actions = [
  173. "iam:PassRole"
  174. ]
  175. }
  176. statement {
  177. sid = "PullFromSecretsManager"
  178. effect = "Allow"
  179. resources = [
  180. "arn:${local.aws_partition}:secretsmanager:${local.aws_region}:${local.aws_account}:secret:msoc-build*",
  181. "arn:${local.aws_partition}:secretsmanager:${local.aws_region}:${local.aws_account}:secret:mdr-aws-codebuild*"
  182. ]
  183. actions = [
  184. "secretsmanager:GetSecretValue"
  185. ]
  186. }
  187. statement {
  188. sid = "KMSAccessNeededForEBS"
  189. effect = "Allow"
  190. resources = [ "*" ]
  191. actions = [
  192. "kms:RevokeGrant",
  193. "kms:ListGrants",
  194. "kms:Decrypt",
  195. "kms:DescribeKey",
  196. "kms:GenerateDataKeyWithoutPlainText",
  197. "kms:ReEncrypt*",
  198. ]
  199. }
  200. statement {
  201. sid = "SSMCodeBuildPause"
  202. effect = "Allow"
  203. resources = [ "*" ]
  204. actions = [
  205. "ssmmessages:CreateControlChannel",
  206. "ssmmessages:CreateDataChannel",
  207. "ssmmessages:OpenControlChannel",
  208. "ssmmessages:OpenDataChannel"
  209. ]
  210. }
  211. statement {
  212. sid = "CreateGrantForEBS"
  213. effect = "Allow"
  214. resources = ["*"]
  215. actions = [
  216. "kms:CreateGrant",
  217. ]
  218. condition {
  219. test = "Bool"
  220. variable = "kms:GrantIsForAWSResource"
  221. values = ["true"]
  222. }
  223. }
  224. }