codebuild.tf 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  74. statement {
  75. sid = "StoreArtifactsInBucket"
  76. effect = "Allow"
  77. resources = [
  78. "arn:${local.aws_partition}:s3:::xdr-codebuild-artifacts/*"
  79. ]
  80. actions = [
  81. "s3:PutObject",
  82. "s3:GetObject*",
  83. "s3:ListBucket"
  84. ]
  85. }
  86. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  87. statement {
  88. sid = "UpdateECRRepos"
  89. effect = "Allow"
  90. resources = [
  91. "*"
  92. ]
  93. actions = [
  94. "ecr:GetAuthorizationToken",
  95. "ecr:BatchCheckLayerAvailability",
  96. "ecr:CompleteLayerUpload",
  97. "ecr:GetAuthorizationToken",
  98. "ecr:InitiateLayerUpload",
  99. "ecr:PutImage",
  100. "ecr:UploadLayerPart"
  101. ]
  102. }
  103. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  104. statement {
  105. sid = "LetEventBridgeTriggerABuild"
  106. effect = "Allow"
  107. resources = [
  108. "*"
  109. ]
  110. actions = [
  111. "codebuild:StartBuild",
  112. "codebuild:StopBuild",
  113. "codebuild:BatchGet*",
  114. "codebuild:Get*",
  115. "codebuild:List*"
  116. ]
  117. }
  118. }
  119. #-----------------------------------------------------------------------
  120. # "EC2" Policy for codebuild - able to build EC2 images / SGs / etc
  121. # FIXME: too powerful
  122. #
  123. # Parts of this are Lifted from
  124. # https://www.packer.io/plugins/builders/amazon#iam-task-or-instance-role and
  125. # converted from JSON to a terraform data source NOT AUDITED - taking Packer
  126. # docs at word that these are "minimal permissions necessary"
  127. #
  128. # The rest is for EBS+KMS support cobbled from AWS docs
  129. #-----------------------------------------------------------------------
  130. resource "aws_iam_policy" "codebuild_build_ec2_amis_policy" {
  131. name = "codebuild_build_ami_policy"
  132. path = "/aws_services/"
  133. description = "Policy for AWS codebuild to build AMIs"
  134. policy = data.aws_iam_policy_document.codebuild_build_ec2_amis.json
  135. }
  136. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  137. data "aws_iam_policy_document" "codebuild_build_ec2_amis" {
  138. statement {
  139. sid = "BuildEC2AMIFromPackerDocs"
  140. effect = "Allow"
  141. resources = [ "*" ]
  142. actions = [
  143. "ec2:AttachVolume",
  144. "ec2:AuthorizeSecurityGroupIngress",
  145. "ec2:CopyImage",
  146. "ec2:CreateImage",
  147. "ec2:CreateKeypair",
  148. "ec2:CreateSecurityGroup",
  149. "ec2:CreateSnapshot",
  150. "ec2:CreateTags",
  151. "ec2:CreateVolume",
  152. "ec2:CreateNetworkInterface",
  153. "ec2:CreateNetworkInterfacePermission",
  154. "ec2:DeleteKeyPair",
  155. "ec2:DeleteNetworkInterface",
  156. "ec2:DeleteSecurityGroup",
  157. "ec2:DeleteSnapshot",
  158. "ec2:DeleteVolume",
  159. "ec2:DeregisterImage",
  160. "ec2:Describe*",
  161. "ec2:DetachVolume",
  162. "ec2:GetPasswordData",
  163. "ec2:ModifyImageAttribute",
  164. "ec2:ModifyInstanceAttribute",
  165. "ec2:ModifySnapshotAttribute",
  166. "ec2:RegisterImage",
  167. "ec2:RunInstances",
  168. "ec2:StopInstances",
  169. "ec2:TerminateInstances"
  170. ]
  171. }
  172. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  173. statement {
  174. sid = "BuildEC2WithInstanceRole"
  175. effect = "Allow"
  176. resources = [ "*" ]
  177. actions = [
  178. "iam:PassRole"
  179. ]
  180. }
  181. statement {
  182. sid = "PullFromSecretsManager"
  183. effect = "Allow"
  184. resources = [
  185. "arn:${local.aws_partition}:secretsmanager:${local.aws_region}:${local.aws_account}:secret:msoc-build*",
  186. "arn:${local.aws_partition}:secretsmanager:${local.aws_region}:${local.aws_account}:secret:mdr-aws-codebuild*"
  187. ]
  188. actions = [
  189. "secretsmanager:GetSecretValue"
  190. ]
  191. }
  192. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  193. statement {
  194. sid = "KMSAccessNeededForEBS"
  195. effect = "Allow"
  196. resources = [ "*" ]
  197. actions = [
  198. "kms:RevokeGrant",
  199. "kms:ListGrants",
  200. "kms:Decrypt",
  201. "kms:DescribeKey",
  202. "kms:GenerateDataKeyWithoutPlainText",
  203. "kms:ReEncrypt*",
  204. ]
  205. }
  206. statement {
  207. sid = "SSMCodeBuildPause"
  208. effect = "Allow"
  209. resources = [ "*" ]
  210. actions = [
  211. "ssmmessages:CreateControlChannel",
  212. "ssmmessages:CreateDataChannel",
  213. "ssmmessages:OpenControlChannel",
  214. "ssmmessages:OpenDataChannel"
  215. ]
  216. }
  217. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  218. statement {
  219. sid = "CreateGrantForEBS"
  220. effect = "Allow"
  221. resources = ["*"]
  222. actions = [
  223. "kms:CreateGrant",
  224. ]
  225. condition {
  226. test = "Bool"
  227. variable = "kms:GrantIsForAWSResource"
  228. values = ["true"]
  229. }
  230. }
  231. }