iam.tf 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. # IAM Roles in All Accounts
  2. #############################
  3. # Default instance profile
  4. #
  5. # Basic profile to allow basic things
  6. resource "aws_iam_instance_profile" "default_instance_profile" {
  7. name = "msoc-default-instance-profile"
  8. role = aws_iam_role.default_instance_role.name
  9. }
  10. data "aws_iam_policy_document" "default_instance_role" {
  11. statement {
  12. sid = "AssumeRoleAnywhere"
  13. effect = "Allow"
  14. actions = ["sts:AssumeRole"]
  15. principals {
  16. type = "Service"
  17. identifiers = [
  18. "ec2.amazonaws.com",
  19. "ssm.amazonaws.com",
  20. ]
  21. }
  22. }
  23. }
  24. resource "aws_iam_role" "default_instance_role" {
  25. name = "msoc-default-instance-role"
  26. assume_role_policy = data.aws_iam_policy_document.default_instance_role.json
  27. }
  28. data "aws_iam_policy_document" "default_instance_policy_doc" {
  29. statement {
  30. effect = "Allow"
  31. actions = [
  32. "ec2:DescribeTags"
  33. ]
  34. resources = [
  35. "*"
  36. ]
  37. }
  38. }
  39. resource "aws_iam_policy" "default_instance_policy" {
  40. name = "default_instance_tag_read"
  41. path = "/launchroles/"
  42. description = "This policy allows a EC2 server to read tags"
  43. policy = data.aws_iam_policy_document.default_instance_policy_doc.json
  44. }
  45. data "aws_iam_policy_document" "default_instance_policy_s3_binaries_doc" {
  46. statement {
  47. sid = "AccessTheBucketItself"
  48. effect = "Allow"
  49. resources = ["arn:${var.aws_partition}:s3:::${var.binaries_bucket}"]
  50. actions = [
  51. "s3:ListBucket",
  52. "s3:GetBucketLocation",
  53. ]
  54. }
  55. statement {
  56. sid = "GetFromTheBucket"
  57. effect = "Allow"
  58. resources = ["arn:${var.aws_partition}:s3:::${var.binaries_bucket}/*"] # tfsec:ignore:aws-iam-no-policy-wildcards This is read-only access
  59. actions = [
  60. "s3:GetObject",
  61. "s3:GetObjectAcl",
  62. ]
  63. }
  64. statement {
  65. sid = "UseTheKey"
  66. effect = "Allow"
  67. resources = [
  68. "arn:${var.aws_partition}:kms:${var.aws_region}:${var.common_services_account}:${local.binaries_key}"
  69. ]
  70. actions = [
  71. "kms:Decrypt",
  72. "kms:DescribeKey"
  73. ]
  74. }
  75. }
  76. resource "aws_iam_policy" "default_instance_policy_s3_binaries" {
  77. name = "default_instance_s3_binaries"
  78. path = "/launchroles/"
  79. description = "This policy allows a EC2 server to read from the s3 binaries bucket"
  80. policy = data.aws_iam_policy_document.default_instance_policy_s3_binaries_doc.json
  81. }
  82. resource "aws_iam_role_policy_attachment" "default_instance_AmazonEC2RoleforSSM" {
  83. role = aws_iam_role.default_instance_role.name
  84. policy_arn = "arn:${var.aws_partition}:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
  85. }
  86. resource "aws_iam_role_policy_attachment" "default_instance_default_policy_attach" {
  87. role = aws_iam_role.default_instance_role.name
  88. policy_arn = aws_iam_policy.default_instance_policy.arn
  89. }
  90. resource "aws_iam_role_policy_attachment" "default_instance_s3_policy_attach" {
  91. role = aws_iam_role.default_instance_role.name
  92. policy_arn = aws_iam_policy.default_instance_policy_s3_binaries.arn
  93. }
  94. resource "aws_iam_role_policy_attachment" "default_instance_cloudwatch_policy_attach" {
  95. role = aws_iam_role.default_instance_role.name
  96. policy_arn = aws_iam_policy.cloudwatch_events.arn
  97. }
  98. ##########################
  99. # cloudwatch events
  100. data "aws_iam_policy_document" "cloudwatch_events" {
  101. # checkov:skip=CKV_AWS_111: see tfsec ignore - we use wildcards
  102. statement {
  103. sid = "1"
  104. actions = [
  105. "events:PutRule"
  106. ]
  107. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  108. resources = ["*"]
  109. }
  110. }
  111. resource "aws_iam_policy" "cloudwatch_events" {
  112. name = "cloudwatch_events"
  113. description = "Creation of cloudwatch events"
  114. policy = data.aws_iam_policy_document.cloudwatch_events.json
  115. }
  116. ##########################
  117. # dlm_lifecycle
  118. #
  119. # This is to setup the needed IAM role and premissions for the AWS feature Data Lifecycle Manager (DLM) lifecycle policy so we can have it do "backups" on our EBS
  120. # Docs can be found here https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/snapshot-lifecycle.html
  121. # Chris Lynch 1/25/2019
  122. resource "aws_iam_role" "dlm_lifecycle_role" {
  123. name = "dlm-lifecycle-role"
  124. assume_role_policy = <<EOF
  125. {
  126. "Version": "2012-10-17",
  127. "Statement": [
  128. {
  129. "Action": "sts:AssumeRole",
  130. "Principal": {
  131. "Service": "dlm.amazonaws.com"
  132. },
  133. "Effect": "Allow",
  134. "Sid": ""
  135. }
  136. ]
  137. }
  138. EOF
  139. }
  140. resource "aws_iam_role_policy" "dlm_lifecycle" {
  141. name = "dlm-lifecycle-policy"
  142. role = aws_iam_role.dlm_lifecycle_role.id
  143. policy = <<EOF
  144. {
  145. "Version": "2012-10-17",
  146. "Statement": [
  147. {
  148. "Effect": "Allow",
  149. "Action": [
  150. "ec2:CreateSnapshot",
  151. "ec2:DeleteSnapshot",
  152. "ec2:DescribeVolumes",
  153. "ec2:DescribeSnapshots",
  154. "ec2:DescribeImages",
  155. "ec2:DescribeInstances",
  156. "ec2:DescribeImageAttribute"
  157. ],
  158. "Resource": "*"
  159. },
  160. {
  161. "Effect": "Allow",
  162. "Action": [
  163. "ec2:CreateTags"
  164. ],
  165. "Resource": [
  166. "arn:${var.aws_partition}:ec2:*::snapshot/*",
  167. "arn:${var.aws_partition}:ec2:*::image/*"
  168. ]
  169. },
  170. {
  171. "Effect": "Allow",
  172. "Action": "ec2:DeleteSnapshot",
  173. "Resource": "arn:${var.aws_partition}:ec2:*::snapshot/*"
  174. },
  175. {
  176. "Effect": "Allow",
  177. "Action": [
  178. "ec2:ResetImageAttribute",
  179. "ec2:DeregisterImage",
  180. "ec2:CreateImage",
  181. "ec2:CopyImage",
  182. "ec2:ModifyImageAttribute"
  183. ],
  184. "Resource": "*"
  185. },
  186. {
  187. "Effect": "Allow",
  188. "Action": [
  189. "kms:ReEncrypt*",
  190. "kms:GenerateDataKey*",
  191. "kms:Encrypt",
  192. "kms:DescribeKey",
  193. "kms:Decrypt",
  194. "kms:Create*"
  195. ],
  196. "Resource": "*"
  197. }
  198. ]
  199. }
  200. EOF
  201. }
  202. ##########################
  203. # moose
  204. #
  205. # See https://docs.splunk.com/Documentation/AddOns/released/AWS/ConfigureAWSpermissions
  206. locals {
  207. trusted_principals_govcloud = [
  208. "arn:${var.aws_partition}:iam::${local.c2_account}:role/instance/moose-hf",
  209. "arn:${var.aws_partition}:iam::${local.c2_account}:user/instance/moose-hf"
  210. ]
  211. trusted_principals_commercial = [
  212. "arn:${var.aws_partition}:iam::${var.legacy_account}:role/splunk-aws-instance-role",
  213. "arn:${var.aws_partition}:iam::${local.c2_account}:user/instance/moose-hf",
  214. ]
  215. trusted_principals = var.aws_partition == "aws" ? local.trusted_principals_commercial : local.trusted_principals_govcloud
  216. }
  217. data "aws_iam_policy_document" "splunk_addon_for_aws_assume_role" {
  218. statement {
  219. sid = ""
  220. effect = "Allow"
  221. actions = ["sts:AssumeRole"]
  222. principals {
  223. type = "AWS"
  224. identifiers = local.trusted_principals
  225. }
  226. }
  227. }
  228. resource "aws_iam_role" "splunk_addon_for_aws" {
  229. name = "splunk-addon-for-aws"
  230. path = "/instance/"
  231. assume_role_policy = data.aws_iam_policy_document.splunk_addon_for_aws_assume_role.json
  232. }
  233. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  234. data "aws_iam_policy_document" "policy" {
  235. statement {
  236. sid = ""
  237. effect = "Allow"
  238. resources = ["*"]
  239. actions = [
  240. "sqs:GetQueueAttributes",
  241. "sqs:ListQueues",
  242. "sqs:ReceiveMessage",
  243. "sqs:GetQueueUrl",
  244. "sqs:SendMessage",
  245. "sqs:DeleteMessage",
  246. "s3:ListBucket",
  247. "s3:GetObject",
  248. "s3:GetBucketLocation",
  249. "s3:ListAllMyBuckets",
  250. "s3:GetBucketTagging",
  251. "s3:GetAccelerateConfiguration",
  252. "s3:GetBucketLogging",
  253. "s3:GetLifecycleConfiguration",
  254. "s3:GetBucketCORS",
  255. "config:DeliverConfigSnapshot",
  256. "config:DescribeConfigRules",
  257. "config:DescribeConfigRuleEvaluationStatus",
  258. "config:GetComplianceDetailsByConfigRule",
  259. "config:GetComplianceSummaryByConfigRule",
  260. "iam:GetUser",
  261. "iam:ListUsers",
  262. "iam:GetAccountPasswordPolicy",
  263. "iam:ListAccessKeys",
  264. "iam:GetAccessKeyLastUsed",
  265. "autoscaling:Describe*",
  266. "cloudwatch:Describe*",
  267. "cloudwatch:Get*",
  268. "cloudwatch:List*",
  269. "sns:Get*",
  270. "sns:List*",
  271. "sns:Publish",
  272. "logs:DescribeLogGroups",
  273. "logs:DescribeLogStreams",
  274. "logs:GetLogEvents",
  275. "ec2:DescribeInstances",
  276. "ec2:DescribeReservedInstances",
  277. "ec2:DescribeSnapshots",
  278. "ec2:DescribeRegions",
  279. "ec2:DescribeKeyPairs",
  280. "ec2:DescribeNetworkAcls",
  281. "ec2:DescribeSecurityGroups",
  282. "ec2:DescribeSubnets",
  283. "ec2:DescribeVolumes",
  284. "ec2:DescribeVpcs",
  285. "ec2:DescribeImages",
  286. "ec2:DescribeAddresses",
  287. "lambda:ListFunctions",
  288. "rds:DescribeDBInstances",
  289. "cloudfront:ListDistributions",
  290. "elasticloadbalancing:DescribeLoadBalancers",
  291. "elasticloadbalancing:DescribeInstanceHealth",
  292. "elasticloadbalancing:DescribeTags",
  293. "elasticloadbalancing:DescribeTargetGroups",
  294. "elasticloadbalancing:DescribeTargetHealth",
  295. "elasticloadbalancing:DescribeListeners",
  296. "inspector:Describe*",
  297. "inspector:List*",
  298. "kinesis:Get*",
  299. "kinesis:DescribeStream",
  300. "kinesis:ListStreams",
  301. "kms:Decrypt",
  302. "sts:AssumeRole",
  303. ]
  304. }
  305. }
  306. resource "aws_iam_role_policy" "splunk_addon_for_aws" {
  307. name = "splunk-addon-for-aws"
  308. role = aws_iam_role.splunk_addon_for_aws.id
  309. policy = data.aws_iam_policy_document.policy.json
  310. }
  311. ## Service Linked Role - For GitHub Runners and Others
  312. resource "aws_iam_service_linked_role" "spot" {
  313. aws_service_name = "spot.amazonaws.com"
  314. }