iam.tf 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. resource "aws_iam_role" "codebuild_role" {
  2. name = "codebuild_role"
  3. assume_role_policy = <<EOF
  4. {
  5. "Version": "2012-10-17",
  6. "Statement": [
  7. {
  8. "Effect": "Allow",
  9. "Principal": {
  10. "Service": [
  11. "codebuild.amazonaws.com",
  12. "events.amazonaws.com"
  13. ]
  14. },
  15. "Action": "sts:AssumeRole"
  16. }
  17. ]
  18. }
  19. EOF
  20. }
  21. resource "aws_iam_role_policy_attachment" "codebuild_role_policy_attach" {
  22. role = aws_iam_role.codebuild_role.name
  23. policy_arn = aws_iam_policy.codebuild_policy.arn
  24. }
  25. # FIXME: Not sure about this policy
  26. # 1. Lets codebuild (apparently) write to ANY s3 bucket
  27. # 2. Lets codebuild (apparently) write to ANY ECR repo
  28. # 3. It's in JSON instead of a terraform data source so these comments
  29. # have to be at the top instead of inline where they would make sense.
  30. # 4. Latest codebuild policies (from AWS console) have report-group resources and actions
  31. resource "aws_iam_policy" "codebuild_policy" {
  32. name = "codebuild_policy"
  33. description = "Policy for AWS codebuild to build and store artifacts"
  34. policy = <<EOF
  35. {
  36. "Version": "2012-10-17",
  37. "Statement": [
  38. {
  39. "Effect": "Allow",
  40. "Resource": [
  41. "arn:${var.aws_partition}:logs:${var.aws_region}:${var.common_services_account}:log-group:/aws/codebuild/*"
  42. ],
  43. "Action": [
  44. "logs:CreateLogGroup",
  45. "logs:CreateLogStream",
  46. "logs:PutLogEvents"
  47. ]
  48. },
  49. {
  50. "Effect": "Allow",
  51. "Resource": [
  52. "arn:${var.aws_partition}:s3:::codepipeline-${var.aws_region}-*"
  53. ],
  54. "Action": [
  55. "s3:PutObject",
  56. "s3:GetObject",
  57. "s3:GetObjectVersion"
  58. ]
  59. },
  60. {
  61. "Effect": "Allow",
  62. "Resource": [
  63. "arn:${var.aws_partition}:codecommit:${var.aws_region}:${var.common_services_account}:*"
  64. ],
  65. "Action": [
  66. "codecommit:GitPull"
  67. ]
  68. },
  69. {
  70. "Effect": "Allow",
  71. "Resource": [
  72. "arn:${var.aws_partition}:s3:::xdr-codebuild-artifacts/*",
  73. "arn:${var.aws_partition}:s3:::*"
  74. ],
  75. "Action": [
  76. "s3:PutObject",
  77. "s3:GetObject*",
  78. "s3:ListBucket"
  79. ]
  80. },
  81. {
  82. "Effect": "Allow",
  83. "Resource": [
  84. "*"
  85. ],
  86. "Action": [
  87. "ecr:GetAuthorizationToken",
  88. "ecr:BatchCheckLayerAvailability",
  89. "ecr:CompleteLayerUpload",
  90. "ecr:GetAuthorizationToken",
  91. "ecr:InitiateLayerUpload",
  92. "ecr:PutImage",
  93. "ecr:UploadLayerPart"
  94. ]
  95. },
  96. {
  97. "Action": [
  98. "codebuild:StartBuild",
  99. "codebuild:StopBuild",
  100. "codebuild:BatchGet*",
  101. "codebuild:Get*",
  102. "codebuild:List*"
  103. ],
  104. "Effect": "Allow",
  105. "Resource": "*"
  106. }
  107. ]
  108. }
  109. EOF
  110. }
  111. # !!!!! RETAINED FOR FUTURE USE !!!!!
  112. # Defines an IAM user that can only download ECR images, intended for
  113. # use in POP nodes where we need containers, but won't necessarily have
  114. # EC2 instance role credentials. Maybe one day this goes to vault, I
  115. # hope. It would be nice.
  116. # data "aws_iam_policy_document" "ecr_policy_pop" {
  117. # statement {
  118. # sid = "AllowECRReadOnly"
  119. # effect = "Allow"
  120. # actions = [
  121. # "ecr:GetAuthorizationToken",
  122. # "ecr:BatchCheckLayerAvailability",
  123. # "ecr:GetDownloadUrlForLayer",
  124. # "ecr:GetRepositoryPolicy",
  125. # "ecr:DescribeRepositories",
  126. # "ecr:ListImages",
  127. # "ecr:DescribeImages",
  128. # "ecr:BatchGetImage"
  129. # ]
  130. # resources = [
  131. # "*"
  132. # ]
  133. # }
  134. # }
  135. # resource "aws_iam_policy" "ecr_policy_pop" {
  136. # name = "ecr_policy_pop"
  137. # path = "/"
  138. # policy = "${data.aws_iam_policy_document.ecr_policy_pop.json}"
  139. # }
  140. # resource "aws_iam_user" "pop_service_account" {
  141. # name = "svc-mdrpop"
  142. # path = "/service/"
  143. # }
  144. # resource "aws_iam_user_policy_attachment" "pop_service_account_1" {
  145. # user = "${aws_iam_user.pop_service_account.name}"
  146. # policy_arn = "${aws_iam_policy.ecr_policy_pop.arn}"
  147. # }
  148. # resource "aws_iam_access_key" "pop_service_account" {
  149. # user = "${aws_iam_user.pop_service_account.name}"
  150. # pgp_key = "${file("../00-organizations-and-iam/duane_waddle.pgp")}"
  151. # }
  152. # output "pop_service_account_key_id" {
  153. # value = "${aws_iam_access_key.pop_service_account.id}"
  154. # }
  155. # output "pop_service_account_secret" {
  156. # value = "${aws_iam_access_key.pop_service_account.encrypted_secret}"
  157. # }
  158. # !!!!! END OF RETAINED FOR FUTURE USE !!!!!