elb.tf 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # Architecture:
  2. # 1. DNS points to an NLB
  3. # 2. NLB:22 forwards to instance:22
  4. # 3. NLB:443 forward to an ALB, which forwards to the instance
  5. # 4. NLB:80 forwards to the same ALB, which forwards to the instance.
  6. #
  7. # The module "static_nlb_to_alb" takes care of #3, but the rest
  8. # we have to handle here.
  9. #
  10. # tfsec:ignore:aws-elb-alb-not-public Purposefully public
  11. module "elb" {
  12. source = "../../submodules/load_balancer/static_nlb_to_alb"
  13. name = "github"
  14. subject_alternative_names = ["*.github.${var.dns_info["public"]["zone"]}"]
  15. target_ids = aws_instance.ghe[*].id
  16. listener_port = 443
  17. target_port = 443
  18. target_protocol = "HTTPS"
  19. target_security_group = aws_security_group.ghe_server.id
  20. allow_from_any = true
  21. redirect_80 = false # GitHub handles port 80, and needs it for LetsEncrypt
  22. # WAF variables
  23. waf_enabled = true # TODO: Turn this on
  24. fqdns = local.hostnames
  25. # Set WAF to 'count' for now
  26. block_settings = {
  27. "default" = true # Default action. False = count
  28. "custom" = true # XDR Custom Rules. False = count
  29. "admin" = true # Block admin pages.
  30. "AWSManagedRulesCommonRuleSet" = false
  31. "AWSManagedRulesAmazonIpReputationList" = false
  32. "AWSManagedRulesKnownBadInputsRuleSet" = false
  33. "AWSManagedRulesSQLiRuleSet" = false # Irrelevant, module is disabled
  34. "AWSManagedRulesLinuxRuleSet" = false # Irrelevant, module is disabled
  35. "AWSManagedRulesUnixRuleSet" = false # Irrelevant, module is disabled
  36. }
  37. excluded_rules_AWSManagedRulesCommonRuleSet = [
  38. "SizeRestrictions_BODY", # SAML auth
  39. "RestrictedExtensions_URIPATH", # Lots of prohibited extensions, e.g. props.conf
  40. "RestrictedExtensions_QueryArguments", # Again, prohibited extensions don't work here
  41. ]
  42. #excluded_rules_AWSManagedRulesAmazonIpReputationList = []
  43. #excluded_rules_AWSManagedRulesKnownBadInputsRuleSet = []
  44. #excluded_rules_AWSManagedRulesSQLiRuleSet = [] # Module disabled
  45. #excluded_rules_AWSManagedRulesLinuxRuleSet = [] # Module disabled
  46. #excluded_rules_AWSManagedRulesUnixRuleSet = [] # Module disabled
  47. # Excluded Rulesets
  48. # There are too many hostnames, so we have to disable some
  49. excluded_set_AWSManagedRulesCommonRuleSet = false
  50. excluded_set_AWSManagedRulesAmazonIpReputationList = false
  51. excluded_set_AWSManagedRulesKnownBadInputsRuleSet = false
  52. excluded_set_AWSManagedRulesSQLiRuleSet = true
  53. excluded_set_AWSManagedRulesLinuxRuleSet = true
  54. excluded_set_AWSManagedRulesUnixRuleSet = true
  55. #additional_blocked_ips = []
  56. #allowed_ips = []
  57. #admin_ips = []
  58. # Optional Variables
  59. healthcheck_port = 443
  60. healthcheck_protocol = "HTTPS"
  61. healthcheck_path = "/status"
  62. healthcheck_matcher = "200"
  63. stickiness = false
  64. # Inherited Variables
  65. tags = merge(var.standard_tags, var.tags)
  66. dns_info = var.dns_info
  67. public_subnets = var.public_subnets
  68. environment = var.environment
  69. aws_partition = var.aws_partition
  70. aws_region = var.aws_region
  71. aws_account_id = var.aws_account_id
  72. vpc_id = var.vpc_id
  73. providers = {
  74. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  75. aws.c2 = aws.c2
  76. }
  77. }
  78. # Github Needs a Wildcard Record
  79. module "public_dns_record_wildcard" {
  80. source = "../../submodules/dns/public_ALIAS_record"
  81. name = "*.github.${var.dns_info["public"]["zone"]}"
  82. target_dns_name = module.elb.nlb.dns_name
  83. target_zone_id = module.elb.nlb.zone_id
  84. dns_info = var.dns_info
  85. providers = {
  86. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  87. }
  88. }
  89. #################################
  90. # Add port 80 to the ALB and NLB
  91. #
  92. # GHE uses LetsEncrypt, which needs access on port 80.
  93. # ALB side
  94. resource "aws_lb_target_group" "github_alb_80" {
  95. name_prefix = "gita80"
  96. port = 80
  97. protocol = "HTTP"
  98. vpc_id = var.vpc_id
  99. health_check {
  100. protocol = "HTTPS"
  101. port = 443
  102. path = "/status"
  103. matcher = "200"
  104. timeout = "4"
  105. interval = "5"
  106. }
  107. lifecycle {
  108. create_before_destroy = true
  109. }
  110. tags = merge(var.standard_tags, var.tags)
  111. }
  112. resource "aws_lb_target_group_attachment" "github_alb_80" {
  113. for_each = toset(aws_instance.ghe[*].id)
  114. target_group_arn = aws_lb_target_group.github_alb_80.arn
  115. target_id = each.value
  116. port = 80
  117. }
  118. resource "aws_lb_listener" "github_alb_80" {
  119. load_balancer_arn = module.elb.alb_id
  120. port = "80" # tfsec:ignore:aws-elb-http-not-used HTTP only used for letsencrypt and redirect
  121. protocol = "HTTP"
  122. default_action {
  123. type = "forward"
  124. target_group_arn = aws_lb_target_group.github_alb_80.arn
  125. }
  126. lifecycle {
  127. create_before_destroy = true
  128. }
  129. tags = merge(var.standard_tags, var.tags)
  130. }
  131. resource "aws_security_group_rule" "github_alb_80" {
  132. description = "Github - Allow 80 from any"
  133. type = "ingress"
  134. from_port = 80
  135. to_port = 80
  136. protocol = "tcp"
  137. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-ingress-sgr Intentionally Open
  138. security_group_id = module.elb.security_group_id
  139. }
  140. resource "aws_security_group_rule" "github_alb_80_out" {
  141. description = "Github - Allow 80 to the instances"
  142. type = "egress"
  143. from_port = 80
  144. to_port = 80
  145. protocol = "tcp"
  146. source_security_group_id = aws_security_group.ghe_server.id
  147. security_group_id = module.elb.security_group_id
  148. }
  149. # NLB Side
  150. resource "aws_lb_target_group" "github_nlb_80" {
  151. name_prefix = "gitn80"
  152. target_type = "alb"
  153. port = 80
  154. protocol = "TCP"
  155. vpc_id = var.vpc_id
  156. lifecycle {
  157. create_before_destroy = true
  158. }
  159. tags = merge(var.standard_tags, var.tags)
  160. }
  161. resource "aws_lb_target_group_attachment" "github_nlb_80" {
  162. target_group_arn = aws_lb_target_group.github_nlb_80.arn
  163. target_id = module.elb.alb_id
  164. port = 80
  165. }
  166. resource "aws_lb_listener" "github_nlb_80" {
  167. load_balancer_arn = module.elb.nlb_id
  168. port = "80"
  169. protocol = "TCP" # tfsec:ignore:aws-elb-http-not-used HTTP only for letsencrypt and redirects
  170. default_action {
  171. type = "forward"
  172. target_group_arn = aws_lb_target_group.github_nlb_80.arn
  173. }
  174. lifecycle {
  175. create_before_destroy = true
  176. }
  177. tags = merge(var.standard_tags, var.tags)
  178. }
  179. ##########################
  180. # Add port 22 to the NLB
  181. resource "aws_lb_target_group" "github_ssh" {
  182. name_prefix = "gitssh"
  183. port = 22
  184. protocol = "TCP"
  185. vpc_id = var.vpc_id
  186. lifecycle {
  187. create_before_destroy = true
  188. }
  189. tags = merge(var.standard_tags, var.tags)
  190. }
  191. resource "aws_lb_target_group_attachment" "github_ssh" {
  192. for_each = toset(aws_instance.ghe[*].id)
  193. target_group_arn = aws_lb_target_group.github_ssh.arn
  194. target_id = each.value
  195. port = 22
  196. }
  197. resource "aws_lb_listener" "github_ssh" {
  198. load_balancer_arn = module.elb.nlb_id
  199. port = "22"
  200. protocol = "TCP"
  201. default_action {
  202. type = "forward"
  203. target_group_arn = aws_lb_target_group.github_ssh.arn
  204. }
  205. lifecycle {
  206. create_before_destroy = true
  207. }
  208. tags = merge(var.standard_tags, var.tags)
  209. }