iam.tf 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. resource "aws_iam_instance_profile" "teleport" {
  2. name = "${var.instance_name}-role"
  3. role = aws_iam_role.auth.name
  4. #depends_on = [aws_iam_role_policy.auth_ssm]
  5. }
  6. // Teleport instance profile and roles
  7. resource "aws_iam_role" "auth" {
  8. name = "${var.instance_name}-role"
  9. path = "/instance/"
  10. assume_role_policy = <<EOF
  11. {
  12. "Version": "2012-10-17",
  13. "Statement": [
  14. {
  15. "Effect": "Allow",
  16. "Principal": {"Service": "ec2.amazonaws.com"},
  17. "Action": "sts:AssumeRole"
  18. }
  19. ]
  20. }
  21. EOF
  22. }
  23. # FTD: No ssm in our deployment
  24. #resource "aws_iam_role_policy" "ssm" {
  25. # name = "${var.instance_name}-teleport-ssm"
  26. # role = aws_iam_role.auth.id
  27. #
  28. # policy = <<EOF
  29. #{
  30. # "Version": "2012-10-17",
  31. # "Statement": [
  32. # {
  33. # "Effect": "Allow",
  34. # "Action": [
  35. # "ssm:DescribeParameters",
  36. # "ssm:GetParameters",
  37. # "ssm:GetParametersByPath",
  38. # "ssm:GetParameter",
  39. # "ssm:PutParameter",
  40. # "ssm:DeleteParameter"
  41. # ],
  42. # "Resource": "arn:${var.aws_partition}:ssm:${var.aws_region}:${var.aws_account_id}:parameter/teleport/${var.instance_name}/*"
  43. # },
  44. # {
  45. # "Effect":"Allow",
  46. # "Action":[
  47. # "kms:Decrypt"
  48. # ],
  49. # "Resource":[
  50. # "arn:${var.aws_partition}:kms:${var.aws_region}:${var.aws_account_id}:key/${data.aws_kms_alias.ssm.target_key_id}"
  51. # ]
  52. # }
  53. # ]
  54. #}
  55. #EOF
  56. #
  57. #}
  58. resource "aws_iam_role_policy_attachment" "teleport_singleinstance_AmazonEC2RoleforSSM" {
  59. role = aws_iam_role.auth.name
  60. policy_arn = "arn:${var.aws_partition}:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
  61. }
  62. resource "aws_iam_role_policy_attachment" "teleport_singleinstance_policy_attach_tag_read" {
  63. role = aws_iam_role.auth.name
  64. policy_arn = "arn:${var.aws_partition}:iam::${var.aws_account_id}:policy/launchroles/default_instance_tag_read"
  65. }
  66. resource "aws_iam_role_policy_attachment" "teleport_singleinstance_policy_attach_cloudwatch" {
  67. role = aws_iam_role.auth.name
  68. policy_arn = "arn:${var.aws_partition}:iam::${var.aws_account_id}:policy/cloudwatch_events"
  69. }
  70. resource "aws_iam_role_policy_attachment" "teleport_singleinstance_policy_attach_binaries" {
  71. role = aws_iam_role.auth.name
  72. policy_arn = "arn:${var.aws_partition}:iam::${var.aws_account_id}:policy/launchroles/default_instance_s3_binaries"
  73. }
  74. // Auth server uses DynamoDB as a backend, and this is to allow read/write from the dynamo tables
  75. data "aws_iam_policy_document" "policy_auth_dynamo" {
  76. statement {
  77. sid = "AllActionsOnTeleportDB"
  78. effect = "Allow"
  79. resources = ["arn:${var.aws_partition}:dynamodb:${var.aws_region}:${var.aws_account_id}:table/${aws_dynamodb_table.teleport.name}"]
  80. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  81. actions = ["dynamodb:*"]
  82. }
  83. statement {
  84. sid = "AllActionsOnTeleportEventsDB"
  85. effect = "Allow"
  86. resources = ["arn:${var.aws_partition}:dynamodb:${var.aws_region}:${var.aws_account_id}:table/${aws_dynamodb_table.teleport_events.name}"]
  87. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  88. actions = ["dynamodb:*"]
  89. }
  90. statement {
  91. sid = "AllActionsOnTeleportEventsIndexDB"
  92. effect = "Allow"
  93. resources = ["arn:${var.aws_partition}:dynamodb:${var.aws_region}:${var.aws_account_id}:table/${aws_dynamodb_table.teleport_events.name}/index/*"]
  94. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  95. actions = ["dynamodb:*"]
  96. }
  97. statement {
  98. sid = "AllActionsOnTeleportStreamsDB"
  99. effect = "Allow"
  100. resources = ["arn:${var.aws_partition}:dynamodb:${var.aws_region}:${var.aws_account_id}:table/${aws_dynamodb_table.teleport.name}/stream/*"]
  101. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  102. actions = ["dynamodb:*"]
  103. }
  104. }
  105. resource "aws_iam_policy" "auth_dynamo" {
  106. name = "${var.instance_name}-auth-dynamo"
  107. policy = data.aws_iam_policy_document.policy_auth_dynamo.json
  108. }
  109. resource "aws_iam_role_policy_attachment" "attach_auth_dynamo" {
  110. role = aws_iam_role.auth.name
  111. policy_arn = aws_iam_policy.auth_dynamo.arn
  112. }
  113. // Allow auth servers to update locks
  114. data "aws_iam_policy_document" "policy_auth_locks" {
  115. statement {
  116. sid = "AllActionsOnLocks"
  117. effect = "Allow"
  118. resources = ["arn:${var.aws_partition}:dynamodb:${var.aws_region}:${var.aws_account_id}:table/${aws_dynamodb_table.locks.name}"]
  119. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  120. actions = ["dynamodb:*"]
  121. }
  122. }
  123. resource "aws_iam_policy" "auth_locks" {
  124. name = "${var.instance_name}-auth-locks"
  125. policy = data.aws_iam_policy_document.policy_auth_locks.json
  126. }
  127. resource "aws_iam_role_policy_attachment" "attach_auth_locks" {
  128. role = aws_iam_role.auth.name
  129. policy_arn = aws_iam_policy.auth_locks.arn
  130. }
  131. // S3 is used for letsencrypt, auth servers request certificates from letsencrypt
  132. // and publish to S3 encrypted bucket. SSM is not used, because certificates and private keys
  133. // are too big for SSM.
  134. data "aws_iam_policy_document" "policy_auth_s3" {
  135. statement {
  136. sid = ""
  137. effect = "Allow"
  138. resources = ["arn:${var.aws_partition}:s3:::${aws_s3_bucket.storage.bucket}"]
  139. actions = [
  140. "s3:ListBucket",
  141. "s3:ListBucketVersions",
  142. ]
  143. }
  144. statement {
  145. sid = ""
  146. effect = "Allow"
  147. resources = ["arn:${var.aws_partition}:s3:::${aws_s3_bucket.storage.bucket}/*"]
  148. actions = [
  149. "s3:PutObject",
  150. "s3:GetObject",
  151. "s3:GetObjectVersion",
  152. ]
  153. }
  154. }
  155. resource "aws_iam_policy" "auth_s3" {
  156. name = "${var.instance_name}-auth-s3"
  157. policy = data.aws_iam_policy_document.policy_auth_s3.json
  158. }
  159. resource "aws_iam_role_policy_attachment" "attach_auth_s3" {
  160. role = aws_iam_role.auth.name
  161. policy_arn = aws_iam_policy.auth_s3.arn
  162. }
  163. // Allow use of the key
  164. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  165. data "aws_iam_policy_document" "policy_kms" {
  166. statement {
  167. sid = "AllowKMSUse"
  168. effect = "Allow"
  169. resources = [aws_kms_key.s3.arn]
  170. actions = [
  171. "kms:Encrypt",
  172. "kms:Decrypt",
  173. "kms:ReEncrypt*",
  174. "kms:GenerateDataKey*",
  175. "kms:DescribeKey"
  176. ]
  177. }
  178. }
  179. resource "aws_iam_policy" "auth_kms" {
  180. name = "${var.instance_name}-kms"
  181. policy = data.aws_iam_policy_document.policy_kms.json
  182. }
  183. resource "aws_iam_role_policy_attachment" "attach_kms" {
  184. role = aws_iam_role.auth.name
  185. policy_arn = aws_iam_policy.auth_kms.arn
  186. }
  187. // FTD: This is for letsencrypt, which we don't (presently) use.
  188. // Auth server uses route53 to get certs for domain, this allows
  189. // read/write operations from the zone.
  190. #resource "aws_iam_role_policy" "auth_route53" {
  191. # name = "${var.instance_name}-auth-route53"
  192. # role = aws_iam_role.auth.id
  193. #
  194. # policy = <<EOF
  195. #{
  196. # "Version": "2012-10-17",
  197. # "Id": "certbot-dns-route53 policy",
  198. # "Statement": [
  199. # {
  200. # "Effect": "Allow",
  201. # "Action": [
  202. # "route53:ListHostedZones",
  203. # "route53:GetChange"
  204. # ],
  205. # "Resource": [
  206. # "*"
  207. # ]
  208. # },
  209. # {
  210. # "Effect" : "Allow",
  211. # "Action" : [
  212. # "route53:ChangeResourceRecordSets"
  213. # ],
  214. # "Resource" : [
  215. # "arn:${var.aws_partition}:route53:::hostedzone/${data.aws_route53_zone.proxy.zone_id}"
  216. # ]
  217. # }
  218. # ]
  219. #}
  220. #EOF
  221. #
  222. #}