iam.tf 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. actions = ["dynamodb:*"]
  81. }
  82. statement {
  83. sid = "AllActionsOnTeleportEventsDB"
  84. effect = "Allow"
  85. resources = ["arn:${var.aws_partition}:dynamodb:${var.aws_region}:${var.aws_account_id}:table/${aws_dynamodb_table.teleport_events.name}"]
  86. actions = ["dynamodb:*"]
  87. }
  88. statement {
  89. sid = "AllActionsOnTeleportEventsIndexDB"
  90. effect = "Allow"
  91. resources = ["arn:${var.aws_partition}:dynamodb:${var.aws_region}:${var.aws_account_id}:table/${aws_dynamodb_table.teleport_events.name}/index/*"]
  92. actions = ["dynamodb:*"]
  93. }
  94. statement {
  95. sid = "AllActionsOnTeleportStreamsDB"
  96. effect = "Allow"
  97. resources = ["arn:${var.aws_partition}:dynamodb:${var.aws_region}:${var.aws_account_id}:table/${aws_dynamodb_table.teleport.name}/stream/*"]
  98. actions = ["dynamodb:*"]
  99. }
  100. }
  101. resource "aws_iam_policy" "auth_dynamo" {
  102. name = "${var.instance_name}-auth-dynamo"
  103. policy = data.aws_iam_policy_document.policy_auth_dynamo.json
  104. }
  105. resource "aws_iam_role_policy_attachment" "attach_auth_dynamo" {
  106. role = aws_iam_role.auth.name
  107. policy_arn = aws_iam_policy.auth_dynamo.arn
  108. }
  109. // Allow auth servers to update locks
  110. data "aws_iam_policy_document" "policy_auth_locks" {
  111. statement {
  112. sid = "AllActionsOnLocks"
  113. effect = "Allow"
  114. resources = ["arn:${var.aws_partition}:dynamodb:${var.aws_region}:${var.aws_account_id}:table/${aws_dynamodb_table.locks.name}"]
  115. actions = ["dynamodb:*"]
  116. }
  117. }
  118. resource "aws_iam_policy" "auth_locks" {
  119. name = "${var.instance_name}-auth-locks"
  120. policy = data.aws_iam_policy_document.policy_auth_locks.json
  121. }
  122. resource "aws_iam_role_policy_attachment" "attach_auth_locks" {
  123. role = aws_iam_role.auth.name
  124. policy_arn = aws_iam_policy.auth_locks.arn
  125. }
  126. // S3 is used for letsencrypt, auth servers request certificates from letsencrypt
  127. // and publish to S3 encrypted bucket. SSM is not used, because certificates and private keys
  128. // are too big for SSM.
  129. data "aws_iam_policy_document" "policy_auth_s3" {
  130. statement {
  131. sid = ""
  132. effect = "Allow"
  133. resources = ["arn:${var.aws_partition}:s3:::${aws_s3_bucket.storage.bucket}"]
  134. actions = [
  135. "s3:ListBucket",
  136. "s3:ListBucketVersions",
  137. ]
  138. }
  139. statement {
  140. sid = ""
  141. effect = "Allow"
  142. resources = ["arn:${var.aws_partition}:s3:::${aws_s3_bucket.storage.bucket}/*"]
  143. actions = [
  144. "s3:PutObject",
  145. "s3:GetObject",
  146. "s3:GetObjectVersion",
  147. ]
  148. }
  149. }
  150. resource "aws_iam_policy" "auth_s3" {
  151. name = "${var.instance_name}-auth-s3"
  152. policy = data.aws_iam_policy_document.policy_auth_s3.json
  153. }
  154. resource "aws_iam_role_policy_attachment" "attach_auth_s3" {
  155. role = aws_iam_role.auth.name
  156. policy_arn = aws_iam_policy.auth_s3.arn
  157. }
  158. // Allow use of the key
  159. data "aws_iam_policy_document" "policy_kms" {
  160. statement {
  161. sid = "AllowKMSUse"
  162. effect = "Allow"
  163. resources = [aws_kms_key.s3.arn]
  164. actions = [
  165. "kms:Encrypt",
  166. "kms:Decrypt",
  167. "kms:ReEncrypt*",
  168. "kms:GenerateDataKey*",
  169. "kms:DescribeKey"
  170. ]
  171. }
  172. }
  173. resource "aws_iam_policy" "auth_kms" {
  174. name = "${var.instance_name}-kms"
  175. policy = data.aws_iam_policy_document.policy_kms.json
  176. }
  177. resource "aws_iam_role_policy_attachment" "attach_kms" {
  178. role = aws_iam_role.auth.name
  179. policy_arn = aws_iam_policy.auth_kms.arn
  180. }
  181. // FTD: This is for letsencrypt, which we don't (presently) use.
  182. // Auth server uses route53 to get certs for domain, this allows
  183. // read/write operations from the zone.
  184. #resource "aws_iam_role_policy" "auth_route53" {
  185. # name = "${var.instance_name}-auth-route53"
  186. # role = aws_iam_role.auth.id
  187. #
  188. # policy = <<EOF
  189. #{
  190. # "Version": "2012-10-17",
  191. # "Id": "certbot-dns-route53 policy",
  192. # "Statement": [
  193. # {
  194. # "Effect": "Allow",
  195. # "Action": [
  196. # "route53:ListHostedZones",
  197. # "route53:GetChange"
  198. # ],
  199. # "Resource": [
  200. # "*"
  201. # ]
  202. # },
  203. # {
  204. # "Effect" : "Allow",
  205. # "Action" : [
  206. # "route53:ChangeResourceRecordSets"
  207. # ],
  208. # "Resource" : [
  209. # "arn:${var.aws_partition}:route53:::hostedzone/${data.aws_route53_zone.proxy.zone_id}"
  210. # ]
  211. # }
  212. # ]
  213. #}
  214. #EOF
  215. #
  216. #}