main.tf 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. }
  5. # Rather than pass in the aws security group, we just look it up. This will
  6. # probably be useful other places, as well.
  7. data "aws_security_group" "typical-host" {
  8. name = "typical-host"
  9. vpc_id = var.vpc_id
  10. }
  11. # Use the default EBS key
  12. data "aws_kms_key" "ebs-key" {
  13. key_id = "alias/ebs_root_encrypt_decrypt"
  14. }
  15. resource "aws_network_interface" "instance" {
  16. subnet_id = var.subnets[0]
  17. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.openvpn_security_group.id ]
  18. description = var.instance_name
  19. tags = merge(var.standard_tags, var.tags, { Name = var.instance_name })
  20. }
  21. resource "aws_instance" "instance" {
  22. #availability_zone = var.azs[count.index % 2]
  23. tenancy = "default"
  24. ebs_optimized = true
  25. disable_api_termination = var.instance_termination_protection
  26. instance_initiated_shutdown_behavior = "stop"
  27. instance_type = var.instance_type
  28. key_name = "msoc-build"
  29. monitoring = false
  30. iam_instance_profile = "msoc-default-instance-profile"
  31. ami = local.ami_map[local.ami_selection]
  32. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  33. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  34. # that could be removed.
  35. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  36. # These device definitions are optional, but added for clarity.
  37. root_block_device {
  38. volume_type = "gp2"
  39. #volume_size = "60"
  40. delete_on_termination = true
  41. encrypted = true
  42. kms_key_id = data.aws_kms_key.ebs-key.arn
  43. }
  44. ebs_block_device {
  45. # swap
  46. device_name = "/dev/xvdm"
  47. volume_size = 48
  48. delete_on_termination = true
  49. encrypted = true
  50. kms_key_id = data.aws_kms_key.ebs-key.arn
  51. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  52. # This may prompt replacement when the AMI is updated.
  53. # See:
  54. # https://github.com/hashicorp/terraform/issues/19958
  55. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  56. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  57. }
  58. ebs_block_device {
  59. # /home
  60. device_name = "/dev/xvdn"
  61. # volume_size = xx
  62. delete_on_termination = true
  63. encrypted = true
  64. kms_key_id = data.aws_kms_key.ebs-key.arn
  65. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  66. }
  67. ebs_block_device {
  68. # /var
  69. device_name = "/dev/xvdo"
  70. # volume_size = xx
  71. delete_on_termination = true
  72. encrypted = true
  73. kms_key_id = data.aws_kms_key.ebs-key.arn
  74. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  75. }
  76. ebs_block_device {
  77. # /var/tmp
  78. device_name = "/dev/xvdp"
  79. # volume_size = xx
  80. delete_on_termination = true
  81. encrypted = true
  82. kms_key_id = data.aws_kms_key.ebs-key.arn
  83. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  84. }
  85. ebs_block_device {
  86. # /var/log
  87. device_name = "/dev/xvdq"
  88. # volume_size = xx
  89. delete_on_termination = true
  90. encrypted = true
  91. kms_key_id = data.aws_kms_key.ebs-key.arn
  92. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  93. }
  94. ebs_block_device {
  95. # /var/log/audit
  96. device_name = "/dev/xvdr"
  97. # volume_size = xx
  98. delete_on_termination = true
  99. encrypted = true
  100. kms_key_id = data.aws_kms_key.ebs-key.arn
  101. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  102. }
  103. ebs_block_device {
  104. # /tmp
  105. device_name = "/dev/xvds"
  106. # volume_size = xx
  107. delete_on_termination = true
  108. encrypted = true
  109. kms_key_id = data.aws_kms_key.ebs-key.arn
  110. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  111. }
  112. network_interface {
  113. device_index = 0
  114. network_interface_id = aws_network_interface.instance.id
  115. }
  116. user_data = data.template_cloudinit_config.cloud-init.rendered
  117. tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  118. volume_tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  119. }
  120. module "private_dns_record" {
  121. source = "../../submodules/dns/private_A_record"
  122. name = var.instance_name
  123. ip_addresses = [ aws_instance.instance.private_ip ]
  124. dns_info = var.dns_info
  125. reverse_enabled = var.reverse_enabled
  126. providers = {
  127. aws.c2 = aws.c2
  128. }
  129. }
  130. data "template_file" "cloud-init" {
  131. # Should these be in a common directory? I suspect they'd be reusable
  132. template = file("${path.module}/cloud-init/cloud-init.tpl")
  133. vars = {
  134. hostname = var.instance_name
  135. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  136. environment = var.environment
  137. salt_master = var.salt_master
  138. proxy = var.proxy
  139. aws_partition = var.aws_partition
  140. aws_partition_alias = var.aws_partition_alias
  141. aws_region = var.aws_region
  142. }
  143. }
  144. # Render a multi-part cloud-init config making use of the part
  145. # above, and other source files
  146. data "template_cloudinit_config" "cloud-init" {
  147. gzip = true
  148. base64_encode = true
  149. # Main cloud-config configuration file.
  150. part {
  151. filename = "init.cfg"
  152. content_type = "text/cloud-config"
  153. content = data.template_file.cloud-init.rendered
  154. }
  155. # Additional parts as needed
  156. #part {
  157. # content_type = "text/x-shellscript"
  158. # content = "ffbaz"
  159. #}
  160. }
  161. resource "aws_security_group" "openvpn_security_group" {
  162. name = "openvpn_security_group"
  163. description = "Security Group for OpenVPN Instance(s)"
  164. vpc_id = var.vpc_id
  165. tags = merge(var.standard_tags, var.tags)
  166. }
  167. resource "aws_security_group_rule" "openvpn-in" {
  168. type = "ingress"
  169. from_port = 1194
  170. to_port = 1194
  171. protocol = "udp"
  172. # NOTE: For NLBs, the source IP is the public IP, so the security group must allow public access.
  173. cidr_blocks = [ "0.0.0.0/0" ]
  174. security_group_id = aws_security_group.openvpn_security_group.id
  175. }
  176. resource "aws_security_group_rule" "openvpn-https-in" {
  177. type = "ingress"
  178. from_port = 443
  179. to_port = 443
  180. protocol = "tcp"
  181. # NOTE: For NLBs, the source IP is the public IP, so the security group must allow public access.
  182. cidr_blocks = [ "0.0.0.0/0" ]
  183. security_group_id = aws_security_group.openvpn_security_group.id
  184. }
  185. resource "aws_security_group_rule" "openvpn-permissive-out" {
  186. # We allow all outbound for openvpn
  187. type = "egress"
  188. from_port = -1
  189. to_port = -1
  190. protocol = "all"
  191. cidr_blocks = [ "10.0.0.0/8" ]
  192. security_group_id = aws_security_group.openvpn_security_group.id
  193. }
  194. # We have specific egress rules, as well, but the list may be incomplete.
  195. resource "aws_security_group_rule" "openvpn-splunk-out" {
  196. type = "egress"
  197. from_port = 8000
  198. to_port = 8000
  199. protocol = "tcp"
  200. cidr_blocks = [ "10.0.0.0/8" ]
  201. security_group_id = aws_security_group.openvpn_security_group.id
  202. }
  203. resource "aws_security_group_rule" "openvpn-https-out" {
  204. type = "egress"
  205. from_port = 443
  206. to_port = 443
  207. protocol = "tcp"
  208. cidr_blocks = [ "10.0.0.0/8" ]
  209. security_group_id = aws_security_group.openvpn_security_group.id
  210. }
  211. resource "aws_security_group_rule" "openvpn-https-alt-out" {
  212. type = "egress"
  213. from_port = 8443
  214. to_port = 8443
  215. protocol = "tcp"
  216. cidr_blocks = [ "10.0.0.0/8" ]
  217. security_group_id = aws_security_group.openvpn_security_group.id
  218. }
  219. resource "aws_security_group_rule" "openvpn-phantom-out" {
  220. type = "egress"
  221. from_port = 8888
  222. to_port = 8888
  223. protocol = "tcp"
  224. cidr_blocks = [ "10.0.0.0/8" ]
  225. security_group_id = aws_security_group.openvpn_security_group.id
  226. }
  227. resource "aws_security_group_rule" "openvpn-github-ssh-out" {
  228. type = "egress"
  229. from_port = 122
  230. to_port = 122
  231. protocol = "tcp"
  232. cidr_blocks = [ "10.0.0.0/8" ]
  233. security_group_id = aws_security_group.openvpn_security_group.id
  234. }
  235. resource "aws_security_group_rule" "openvpn-ssh-out" {
  236. type = "egress"
  237. from_port = 22
  238. to_port = 22
  239. protocol = "tcp"
  240. cidr_blocks = [ "10.0.0.0/8" ]
  241. security_group_id = aws_security_group.openvpn_security_group.id
  242. }
  243. resource "aws_security_group_rule" "openvpn-nessus-out" {
  244. type = "egress"
  245. from_port = 8834
  246. to_port = 8835
  247. protocol = "tcp"
  248. cidr_blocks = toset(concat(var.cidr_map["vpc-scanners"], var.cidr_map["vpc-private-services"]))
  249. security_group_id = aws_security_group.openvpn_security_group.id
  250. description = "Access to Nessus"
  251. }
  252. resource "aws_security_group_rule" "openvpn-license-server-out" {
  253. # Needed for license server check-in. Seems to be stable IP.
  254. type = "egress"
  255. from_port = 443
  256. to_port = 443
  257. protocol = "tcp"
  258. cidr_blocks = [ "54.183.149.72/32" ]
  259. security_group_id = aws_security_group.openvpn_security_group.id
  260. }
  261. resource "aws_security_group_rule" "openvpn-ldap-out" {
  262. type = "egress"
  263. from_port = 636
  264. to_port = 636
  265. protocol = "tcp"
  266. # Yes this has to be 0.0.0.0/0 because our SSL ldap server is provided by OKTA behind a NLB in AWS with non static IP
  267. cidr_blocks = [ "0.0.0.0/0" ]
  268. security_group_id = aws_security_group.openvpn_security_group.id
  269. }