main.tf 12 KB

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