iam.tf 8.1 KB

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