iam.tf 9.9 KB

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