main.tf 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. data "aws_vpc" "this" {
  2. id = var.vpc_id
  3. }
  4. data "aws_prefix_list" "private_s3" {
  5. filter {
  6. name = "prefix-list-name"
  7. values = ["com.amazonaws.*.s3"]
  8. }
  9. }
  10. data "aws_prefix_list" "private_dynamodb" {
  11. filter {
  12. name = "prefix-list-name"
  13. values = ["com.amazonaws.*.dynamodb"]
  14. }
  15. }
  16. locals {
  17. vpc_name = lookup(data.aws_vpc.this.tags, "Name", data.aws_vpc.this.cidr_block)
  18. }
  19. resource "aws_security_group" "security_group" {
  20. name = "typical-host"
  21. description = "Required typical-host SG for VPC ${local.vpc_name} (${var.vpc_id})"
  22. vpc_id = var.vpc_id
  23. tags = merge(var.tags, { "Name" = "typical-host", "vpc_name" = local.vpc_name })
  24. }
  25. ## Ingress
  26. resource "aws_security_group_rule" "scanner_access" {
  27. security_group_id = aws_security_group.security_group.id
  28. type = "ingress"
  29. description = "Full Access from Security Scanners"
  30. from_port = 0
  31. to_port = 0
  32. protocol = -1
  33. cidr_blocks = var.cidr_map["scanners"]
  34. count = length(var.cidr_map["scanners"]) > 0 ? 1 : 0
  35. }
  36. resource "aws_security_group_rule" "teleport_ssh_access" {
  37. security_group_id = aws_security_group.security_group.id
  38. type = "ingress"
  39. description = "Teleport SSH Access"
  40. from_port = 3022
  41. to_port = 3022
  42. protocol = "tcp"
  43. # Convert to a set to remove duplicates
  44. cidr_blocks = var.cidr_map["vpc-access"]
  45. count = length(var.cidr_map["vpc-access"]) > 0 ? 1 : 0
  46. }
  47. resource "aws_security_group_rule" "ssh_access" {
  48. security_group_id = aws_security_group.security_group.id
  49. type = "ingress"
  50. description = "SSH Access"
  51. from_port = 22
  52. to_port = 22
  53. protocol = "tcp"
  54. # Convert to a set to remove duplicates
  55. cidr_blocks = toset(concat(var.cidr_map["bastions"], var.cidr_map["vpns"]))
  56. count = length(toset(concat(var.cidr_map["bastions"], var.cidr_map["vpns"]))) > 0 ? 1 : 0
  57. }
  58. resource "aws_security_group_rule" "ping_inbound" {
  59. security_group_id = aws_security_group.security_group.id
  60. type = "ingress"
  61. description = "Inbound Pings"
  62. from_port = -1
  63. to_port = -1
  64. protocol = "icmp"
  65. cidr_blocks = ["10.0.0.0/8"]
  66. }
  67. ## Outbound:
  68. resource "aws_security_group_rule" "ping_outbound" {
  69. security_group_id = aws_security_group.security_group.id
  70. type = "egress"
  71. description = "Outbound Pings"
  72. from_port = -1
  73. to_port = -1
  74. protocol = "icmp"
  75. cidr_blocks = ["0.0.0.0/0"]
  76. }
  77. resource "aws_security_group_rule" "github_access_ssh" {
  78. security_group_id = aws_security_group.security_group.id
  79. type = "egress"
  80. description = "Outbound GitHub"
  81. from_port = 22
  82. to_port = 22
  83. protocol = "tcp"
  84. cidr_blocks = var.cidr_map["vpc-public"]
  85. count = length(var.cidr_map["vpc-public"]) > 0 ? 1 : 0
  86. }
  87. resource "aws_security_group_rule" "github_access_http" {
  88. security_group_id = aws_security_group.security_group.id
  89. type = "egress"
  90. description = "Outbound GitHub"
  91. from_port = 80
  92. to_port = 80
  93. protocol = "tcp"
  94. cidr_blocks = var.cidr_map["vpc-public"]
  95. count = length(var.cidr_map["vpc-public"]) > 0 ? 1 : 0
  96. }
  97. resource "aws_security_group_rule" "github_access_https" {
  98. security_group_id = aws_security_group.security_group.id
  99. type = "egress"
  100. description = "Outbound GitHub"
  101. from_port = 443
  102. to_port = 443
  103. protocol = "tcp"
  104. cidr_blocks = var.cidr_map["vpc-public"]
  105. count = length(var.cidr_map["vpc-public"]) > 0 ? 1 : 0
  106. }
  107. resource "aws_security_group_rule" "dns_access_tcp" {
  108. security_group_id = aws_security_group.security_group.id
  109. type = "egress"
  110. description = "Outbound TCP DNS"
  111. from_port = 53
  112. to_port = 53
  113. protocol = "tcp"
  114. cidr_blocks = var.cidr_map["dns"]
  115. count = length(var.cidr_map["dns"]) > 0 ? 1 : 0
  116. }
  117. resource "aws_security_group_rule" "dns_access_udp" {
  118. security_group_id = aws_security_group.security_group.id
  119. type = "egress"
  120. description = "Outbound UDP DNS"
  121. from_port = 53
  122. to_port = 53
  123. protocol = "udp"
  124. cidr_blocks = var.cidr_map["dns"]
  125. count = length(var.cidr_map["dns"]) > 0 ? 1 : 0
  126. }
  127. resource "aws_security_group_rule" "outbound_to_teleport" {
  128. security_group_id = aws_security_group.security_group.id
  129. type = "egress"
  130. description = "Connect to Teleport"
  131. from_port = 3080
  132. to_port = 3080
  133. protocol = "tcp"
  134. cidr_blocks = var.cidr_map["vpc-access"]
  135. count = length(var.cidr_map["vpc-access"]) > 0 ? 1 : 0
  136. }
  137. resource "aws_security_group_rule" "outbound_to_teleport_30xx" {
  138. security_group_id = aws_security_group.security_group.id
  139. type = "egress"
  140. description = "Connect to Teleport"
  141. from_port = 3023
  142. to_port = 3026
  143. protocol = "tcp"
  144. cidr_blocks = var.cidr_map["vpc-access"]
  145. count = length(var.cidr_map["vpc-access"]) > 0 ? 1 : 0
  146. }
  147. resource "aws_security_group_rule" "outbound_to_salt_masters" {
  148. security_group_id = aws_security_group.security_group.id
  149. type = "egress"
  150. description = "Connect to Salt Masters"
  151. from_port = 4505
  152. to_port = 4506
  153. protocol = "tcp"
  154. cidr_blocks = var.cidr_map["salt"]
  155. count = length(var.cidr_map["salt"]) > 0 ? 1 : 0
  156. }
  157. resource "aws_security_group_rule" "outbound_to_web_servers_80" {
  158. security_group_id = aws_security_group.security_group.id
  159. type = "egress"
  160. description = "Connect to Repo Servers"
  161. from_port = 80
  162. to_port = 80
  163. protocol = "tcp"
  164. cidr_blocks = var.cidr_map["web"]
  165. count = length(var.cidr_map["web"]) > 0 ? 1 : 0
  166. }
  167. resource "aws_security_group_rule" "outbound_to_web_servers_443" {
  168. security_group_id = aws_security_group.security_group.id
  169. type = "egress"
  170. description = "Connect to Repo Servers"
  171. from_port = 443
  172. to_port = 443
  173. protocol = "tcp"
  174. cidr_blocks = var.cidr_map["web"]
  175. count = length(var.cidr_map["web"]) > 0 ? 1 : 0
  176. }
  177. # Systems need to be able to access vpc endpoints on 80/443
  178. resource "aws_security_group_rule" "outbound_to_local_vpc_80" {
  179. security_group_id = aws_security_group.security_group.id
  180. type = "egress"
  181. description = "Connect to VPC Endpoints"
  182. from_port = 80
  183. to_port = 80
  184. protocol = "tcp"
  185. source_security_group_id = var.aws_endpoints_sg
  186. }
  187. resource "aws_security_group_rule" "outbound_to_local_vpc_443" {
  188. security_group_id = aws_security_group.security_group.id
  189. type = "egress"
  190. description = "Connect to VPC Endpoints"
  191. from_port = 443
  192. to_port = 443
  193. protocol = "tcp"
  194. source_security_group_id = var.aws_endpoints_sg
  195. }
  196. resource "aws_security_group_rule" "outbound_to_mailrelay_25" {
  197. security_group_id = aws_security_group.security_group.id
  198. type = "egress"
  199. description = "Outbound Email to mailrelay"
  200. from_port = 25
  201. to_port = 25
  202. protocol = "tcp"
  203. cidr_blocks = var.cidr_map["vpc-system-services"]
  204. count = length(var.cidr_map["vpc-system-services"]) > 0 ? 1 : 0
  205. }
  206. resource "aws_security_group_rule" "outbound_to_mailrelay_587" {
  207. security_group_id = aws_security_group.security_group.id
  208. type = "egress"
  209. description = "Outbound Email to mailrelay"
  210. from_port = 587
  211. to_port = 587
  212. protocol = "tcp"
  213. cidr_blocks = var.cidr_map["vpc-system-services"]
  214. count = length(var.cidr_map["vpc-system-services"]) > 0 ? 1 : 0
  215. }
  216. resource "aws_security_group_rule" "outbound_to_ec2_s3_endpoint" {
  217. security_group_id = aws_security_group.security_group.id
  218. type = "egress"
  219. description = "Outbound to S3 endpoint"
  220. from_port = 443
  221. to_port = 443
  222. protocol = "tcp"
  223. prefix_list_ids = [data.aws_prefix_list.private_s3.id]
  224. count = length([data.aws_prefix_list.private_s3.id]) > 0 ? 1 : 0 # todo: handle case of no s3 prefix list
  225. }
  226. resource "aws_security_group_rule" "outbound_to_ec2_dynamodb_endpoint" {
  227. security_group_id = aws_security_group.security_group.id
  228. type = "egress"
  229. description = "Outbound to dynamodb endpoint"
  230. from_port = 443
  231. to_port = 443
  232. protocol = "tcp"
  233. prefix_list_ids = [data.aws_prefix_list.private_dynamodb.id]
  234. count = length([data.aws_prefix_list.private_dynamodb.id]) > 0 ? 1 : 0 # todo: handle case of no s3 prefix list
  235. }
  236. resource "aws_security_group_rule" "outbound_to_sensu" {
  237. security_group_id = aws_security_group.security_group.id
  238. type = "egress"
  239. description = "Monitoring Outbound"
  240. from_port = 8081
  241. to_port = 8081
  242. protocol = "tcp"
  243. cidr_blocks = var.cidr_map["monitoring"]
  244. count = length(var.cidr_map["monitoring"]) > 0 ? 1 : 0
  245. }
  246. resource "aws_security_group_rule" "outbound_to_moose_s2s" {
  247. security_group_id = aws_security_group.security_group.id
  248. type = "egress"
  249. description = "Splunk UF outbound to Moose Indexers"
  250. from_port = 9997
  251. to_port = 9998
  252. protocol = "tcp"
  253. cidr_blocks = var.cidr_map["moose"]
  254. count = length(var.cidr_map["moose"]) > 0 ? 1 : 0
  255. }
  256. resource "aws_security_group_rule" "outbound_to_moose_idxc" {
  257. security_group_id = aws_security_group.security_group.id
  258. type = "egress"
  259. description = "Outbound IDXC Discovery to MOOSE"
  260. from_port = 8089
  261. to_port = 8089
  262. protocol = "tcp"
  263. cidr_blocks = var.cidr_map["moose"]
  264. count = length(var.cidr_map["moose"]) > 0 ? 1 : 0
  265. }
  266. resource "aws_security_group_rule" "outbound_to_moose_hec" {
  267. security_group_id = aws_security_group.security_group.id
  268. type = "egress"
  269. description = "Connect to HEC"
  270. from_port = 8088
  271. to_port = 8088
  272. protocol = "tcp"
  273. cidr_blocks = var.cidr_map["moose"]
  274. count = length(var.cidr_map["moose"]) > 0 ? 1 : 0
  275. }
  276. resource "aws_security_group_rule" "outbound_to_nessus_manager" {
  277. security_group_id = aws_security_group.security_group.id
  278. type = "egress"
  279. description = "Connect to Tenable Nessus Manager"
  280. from_port = 8834
  281. to_port = 8834
  282. protocol = "tcp"
  283. cidr_blocks = var.cidr_map["vpc-private-services"]
  284. count = length(var.cidr_map["vpc-private-services"]) > 0 ? 1 : 0
  285. }