iam.tf 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # include access to the S3 bucket and aws secrets manager in PROD c2
  2. # resource "aws_iam_role" "codebuild_service_role" {
  3. # name = "codebuild_${var.name}_role"
  4. # path = "/aws_services/"
  5. # assume_role_policy = <<EOF
  6. # {
  7. # "Version": "2012-10-17",
  8. # "Statement": [
  9. # {
  10. # "Effect": "Allow",
  11. # "Principal": {
  12. # "Service": [
  13. # "codebuild.amazonaws.com"
  14. # ]
  15. # },
  16. # "Action": "sts:AssumeRole"
  17. # }
  18. # ]
  19. # }
  20. # EOF
  21. # }
  22. # resource "aws_iam_role_policy_attachment" "codebuild_service_policy_attach" {
  23. # role = aws_iam_role.codebuild_service_role.name
  24. # policy_arn = aws_iam_policy.codebuild_service_policy.arn
  25. # }
  26. # # Some things about this policy I'm not perfectly sure about, like
  27. # # should the account number be hardcoded? Also, it reads like we'll have to
  28. # # update it each time we have a new repository added to codecommit - that
  29. # # or we'll need to authorize the codebuild role to be able to pull from any
  30. # # codecommit repo. Which may be fine?
  31. # resource "aws_iam_policy" "codebuild_service_policy" {
  32. # name = "codebuild_${var.name}_policy"
  33. # description = "Policy for AWS codebuild for ${var.name}"
  34. # path = "/aws_services/"
  35. # policy = <<EOF
  36. # {
  37. # "Version":"2012-10-17",
  38. # "Statement":[
  39. # {
  40. # "Effect":"Allow",
  41. # "Action":[
  42. # "ec2:CreateNetworkInterface",
  43. # "ec2:CreateNetworkInterfacePermission",
  44. # "ec2:DeleteNetworkInterface",
  45. # "ec2:Describe*",
  46. # "ec2:TerminateInstances",
  47. # "ec2:RunInstances",
  48. # "ec2:CreateTags"
  49. # ],
  50. # "Resource":"*"
  51. # },
  52. # {
  53. # "Effect":"Allow",
  54. # "Action":"*",
  55. # "Resource":"*"
  56. # },
  57. # {
  58. # "Effect":"Allow",
  59. # "Action":"iam:PassRole",
  60. # "Resource":"arn:${var.aws_partition}:iam::${var.aws_account_id}:role/aws_services/msoc-magic-machine-instance-role"
  61. # },
  62. # {
  63. # "Effect":"Allow",
  64. # "Resource":[
  65. # "arn:${var.aws_partition}:logs:${var.aws_region}:${var.aws_account_id}:log-group:/aws/codebuild/*"
  66. # ],
  67. # "Action":[
  68. # "logs:CreateLogGroup",
  69. # "logs:CreateLogStream",
  70. # "logs:PutLogEvents"
  71. # ]
  72. # },
  73. # {
  74. # "Effect":"Allow",
  75. # "Resource":[
  76. # "arn:${var.aws_partition}:s3:::afsxdr-binaries*"
  77. # ],
  78. # "Action":[
  79. # "s3:PutObject",
  80. # "s3:GetObject",
  81. # "s3:GetObjectVersion"
  82. # ]
  83. # },
  84. # {
  85. # "Sid":"PullFromECR",
  86. # "Effect":"Allow",
  87. # "Resource":[
  88. # "*"
  89. # ],
  90. # "Action":[
  91. # "ecr:GetDownloadUrlForLayer",
  92. # "ecr:BatchGetImage",
  93. # "ecr:BatchCheckLayerAvailability"
  94. # ]
  95. # },
  96. # {
  97. # "Sid":"PullFromSecretsManager",
  98. # "Effect":"Allow",
  99. # "Resource":[
  100. # "arn:${var.aws_partition}:secretsmanager:${var.aws_region}:${var.aws_account_id}:secret:msoc-build*"
  101. # ],
  102. # "Action":[
  103. # "secretsmanager:GetSecretValue"
  104. # ]
  105. # }
  106. # ]
  107. # }
  108. # EOF
  109. # }
  110. # Policy for the Magic Machine iam-instance-policy
  111. resource "aws_iam_instance_profile" "magic_machine" {
  112. name = "msoc-magic-machine-instance-profile-${var.name}"
  113. role = aws_iam_role.magic_machine_instance_role.name
  114. }
  115. resource "aws_iam_role" "magic_machine_instance_role" {
  116. name = "msoc-magic-machine-instance-role-${var.name}"
  117. path = "/aws_services/"
  118. assume_role_policy = <<EOF
  119. {
  120. "Version": "2012-10-17",
  121. "Statement": [
  122. {
  123. "Sid": "AssumeRoleAnywhere",
  124. "Effect": "Allow",
  125. "Principal": {
  126. "Service": [
  127. "ec2.amazonaws.com",
  128. "ssm.amazonaws.com"
  129. ]
  130. },
  131. "Action": "sts:AssumeRole"
  132. }
  133. ]
  134. }
  135. EOF
  136. }
  137. resource "aws_iam_policy" "magic_machine_policy" {
  138. name = "magic_machine_s3_access_${var.name}"
  139. path = "/launchroles/"
  140. description = "This policy allows the magic machine to push the image to S3 for ${var.name}"
  141. policy = data.aws_iam_policy_document.magic_machine_instance_policy_s3_binaries_doc.json
  142. }
  143. data "aws_iam_policy_document" "magic_machine_instance_policy_s3_binaries_doc" {
  144. statement {
  145. sid = "AccessTheBucketItself"
  146. effect = "Allow"
  147. resources = ["arn:${var.aws_partition}:s3:::afsxdr-binaries"]
  148. actions = [
  149. "s3:ListBucket",
  150. "s3:GetBucketLocation",
  151. ]
  152. }
  153. statement {
  154. sid = "GetFromTheBucket"
  155. effect = "Allow"
  156. resources = ["arn:${var.aws_partition}:s3:::afsxdr-binaries/*"]
  157. actions = [
  158. "s3:GetObject",
  159. "s3:GetObjectAcl",
  160. "s3:PutObject",
  161. ]
  162. }
  163. statement {
  164. sid = "UseTheKey"
  165. effect = "Allow"
  166. resources = [
  167. "arn:${var.aws_partition}:kms:${var.aws_region}:${var.common_services_account}:${local.binaries_key}"
  168. ]
  169. actions = [
  170. "kms:Decrypt",
  171. "kms:DescribeKey",
  172. "kms:GenerateDataKey"
  173. ]
  174. }
  175. }
  176. resource "aws_iam_role_policy_attachment" "magic_machine_instance_policy_attach" {
  177. role = aws_iam_role.magic_machine_instance_role.name
  178. policy_arn = var.xdr-s3-binaries-policy
  179. }