firewall.tftest.hcl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. mock_provider "digitalocean" {}
  2. variables {
  3. layer = "foundation"
  4. region = "tor1"
  5. purpose = "baseline"
  6. tag = "cah-baseline-tor1"
  7. }
  8. run "denies_all_inbound" {
  9. command = plan
  10. assert {
  11. condition = length(digitalocean_firewall.this.inbound_rule) == 0
  12. error_message = "The baseline firewall must have zero inbound rules."
  13. }
  14. assert {
  15. condition = output.inbound_rule_count == 0
  16. error_message = "inbound_rule_count output must be 0."
  17. }
  18. }
  19. run "applies_by_tag_with_convention_name" {
  20. command = plan
  21. assert {
  22. condition = digitalocean_firewall.this.name == "foundation-tor1-baseline"
  23. error_message = "Name must be <layer>-<region>-<purpose>."
  24. }
  25. assert {
  26. condition = contains(digitalocean_firewall.this.tags, "cah-baseline-tor1")
  27. error_message = "Firewall must attach by the given tag."
  28. }
  29. }
  30. run "default_outbound_is_the_five_expected_rules" {
  31. command = plan
  32. assert {
  33. condition = output.outbound_rule_count == 5
  34. error_message = "Default outbound must be tcp 443/80/53 and udp 53/123 only."
  35. }
  36. }
  37. run "default_outbound_is_exactly_these_rules" {
  38. command = plan
  39. assert {
  40. condition = jsonencode(output.outbound_rules) == jsonencode(sort([
  41. "tcp/443/0.0.0.0/0,::/0",
  42. "tcp/80/0.0.0.0/0,::/0",
  43. "tcp/53/0.0.0.0/0,::/0",
  44. "udp/53/0.0.0.0/0,::/0",
  45. "udp/123/0.0.0.0/0,::/0",
  46. ]))
  47. error_message = "Default outbound must be exactly tcp 443/80/53 and udp 53/123, each to 0.0.0.0/0 and ::/0."
  48. }
  49. assert {
  50. condition = jsonencode(sort([
  51. for r in digitalocean_firewall.this.outbound_rule :
  52. "${r.protocol}/${r.port_range}/${join(",", sort(tolist(r.destination_addresses)))}"
  53. ])) == jsonencode(output.outbound_rules)
  54. error_message = "The outbound_rules output must describe the resource's real rules."
  55. }
  56. }
  57. run "extra_outbound_is_appended" {
  58. command = plan
  59. variables {
  60. extra_outbound_rules = [{ protocol = "tcp", port_range = "30000-31000" }]
  61. }
  62. assert {
  63. condition = output.outbound_rule_count == 6
  64. error_message = "Extra outbound rules must be appended to the defaults."
  65. }
  66. assert {
  67. condition = jsonencode(output.outbound_rules) == jsonencode(sort([
  68. "tcp/443/0.0.0.0/0,::/0",
  69. "tcp/80/0.0.0.0/0,::/0",
  70. "tcp/53/0.0.0.0/0,::/0",
  71. "udp/53/0.0.0.0/0,::/0",
  72. "udp/123/0.0.0.0/0,::/0",
  73. "tcp/30000-31000/0.0.0.0/0,::/0",
  74. ]))
  75. error_message = "Extra rules must be appended with the same any-address destinations."
  76. }
  77. }
  78. run "inbound_from_tag_renders_only_tag_sourced_rules" {
  79. command = plan
  80. variables {
  81. inbound_from_tag_rules = [
  82. { protocol = "tcp", port_range = "22" },
  83. { protocol = "udp", port_range = "9999" },
  84. ]
  85. }
  86. assert {
  87. condition = output.inbound_rule_count == 2
  88. error_message = "Exactly the listed inbound rules must render."
  89. }
  90. assert {
  91. condition = jsonencode(output.inbound_rules) == jsonencode(sort(["tcp/22/cah-baseline-tor1", "udp/9999/cah-baseline-tor1"]))
  92. error_message = "Each inbound rule must be sourced from this firewall's own tag and nothing else."
  93. }
  94. assert {
  95. condition = alltrue([
  96. for r in digitalocean_firewall.this.inbound_rule :
  97. length(coalesce(r.source_addresses, [])) == 0 && length(coalesce(r.source_droplet_ids, [])) == 0 && length(coalesce(r.source_load_balancer_uids, [])) == 0 && toset(r.source_tags) == toset(["cah-baseline-tor1"])
  98. ])
  99. error_message = "Inbound rules must carry source_tags equal to the module tag and no source addresses."
  100. }
  101. assert {
  102. condition = output.outbound_rule_count == 5
  103. error_message = "Inbound rules must not change the outbound set."
  104. }
  105. }
  106. run "an_inbound_rule_with_an_unknown_protocol_is_rejected" {
  107. command = plan
  108. variables {
  109. inbound_from_tag_rules = [{ protocol = "icmp", port_range = "22" }]
  110. }
  111. expect_failures = [var.inbound_from_tag_rules]
  112. }
  113. run "an_inbound_rule_with_an_empty_port_range_is_rejected" {
  114. command = plan
  115. variables {
  116. inbound_from_tag_rules = [{ protocol = "tcp", port_range = "" }]
  117. }
  118. expect_failures = [var.inbound_from_tag_rules]
  119. }
  120. run "default_has_no_tag_scoped_outbound_rules" {
  121. command = plan
  122. assert {
  123. condition = length(output.outbound_tag_rules) == 0
  124. error_message = "No tag-scoped outbound rule may render unless outbound_to_tag_rules lists one."
  125. }
  126. assert {
  127. condition = alltrue([
  128. for r in digitalocean_firewall.this.outbound_rule :
  129. length(coalesce(r.destination_tags, [])) == 0
  130. ])
  131. error_message = "By default no outbound rule may carry destination_tags."
  132. }
  133. }
  134. run "outbound_to_tag_renders_only_tag_destined_rules" {
  135. command = plan
  136. variables {
  137. outbound_to_tag_rules = [
  138. { protocol = "tcp", port_range = "22" },
  139. { protocol = "udp", port_range = "9999" },
  140. ]
  141. }
  142. assert {
  143. condition = output.outbound_rule_count == 7
  144. error_message = "The five defaults plus exactly the two listed tag rules must render."
  145. }
  146. assert {
  147. condition = jsonencode(output.outbound_tag_rules) == jsonencode(sort(["tcp/22/cah-baseline-tor1", "udp/9999/cah-baseline-tor1"]))
  148. error_message = "Each tag-scoped outbound rule must be destined to this firewall's own tag and nothing else."
  149. }
  150. assert {
  151. condition = alltrue([
  152. for r in digitalocean_firewall.this.outbound_rule :
  153. length(coalesce(r.destination_tags, [])) == 0 ? true : (length(coalesce(r.destination_addresses, [])) == 0 && length(coalesce(r.destination_droplet_ids, [])) == 0 && length(coalesce(r.destination_load_balancer_uids, [])) == 0 && toset(r.destination_tags) == toset(["cah-baseline-tor1"]))
  154. ])
  155. error_message = "Tag-scoped outbound rules must carry destination_tags equal to the module tag and no destination addresses."
  156. }
  157. assert {
  158. condition = jsonencode(output.outbound_rules) == jsonencode(sort([
  159. "tcp/443/0.0.0.0/0,::/0",
  160. "tcp/80/0.0.0.0/0,::/0",
  161. "tcp/53/0.0.0.0/0,::/0",
  162. "udp/53/0.0.0.0/0,::/0",
  163. "udp/123/0.0.0.0/0,::/0",
  164. ]))
  165. error_message = "The address-based outbound list must be unchanged by tag-scoped rules."
  166. }
  167. assert {
  168. condition = output.inbound_rule_count == 0
  169. error_message = "Outbound tag rules must not add inbound rules."
  170. }
  171. }
  172. run "an_outbound_tag_rule_with_an_unknown_protocol_is_rejected" {
  173. command = plan
  174. variables {
  175. outbound_to_tag_rules = [{ protocol = "icmp", port_range = "22" }]
  176. }
  177. expect_failures = [var.outbound_to_tag_rules]
  178. }
  179. run "an_outbound_tag_rule_with_an_empty_port_range_is_rejected" {
  180. command = plan
  181. variables {
  182. outbound_to_tag_rules = [{ protocol = "tcp", port_range = "" }]
  183. }
  184. expect_failures = [var.outbound_to_tag_rules]
  185. }
  186. run "contract_roles_are_the_tag_on_digitalocean" {
  187. command = plan
  188. variables {
  189. purpose = "connector"
  190. inbound_from_tag_rules = [{ protocol = "tcp", port_range = "22" }]
  191. outbound_to_tag_rules = [{ protocol = "tcp", port_range = "22" }]
  192. }
  193. assert {
  194. condition = jsonencode(output.rule_targets) == jsonencode(["cah-baseline-tor1"])
  195. error_message = "rule_targets must be the tag the firewall attaches to."
  196. }
  197. assert {
  198. condition = jsonencode(output.source_members) == jsonencode(["cah-baseline-tor1"]) && jsonencode(output.destination_members) == jsonencode(["cah-baseline-tor1"])
  199. error_message = "With member rules, source_members and destination_members must name the tag; address rules must not appear in them."
  200. }
  201. }
  202. run "contract_members_are_empty_without_member_rules" {
  203. command = plan
  204. assert {
  205. condition = length(output.source_members) == 0 && length(output.destination_members) == 0
  206. error_message = "A firewall with only address-based rules has no members."
  207. }
  208. }
  209. run "resource_key_changes_the_name_not_the_tag" {
  210. command = plan
  211. variables {
  212. resource_key = "tor1-b"
  213. }
  214. assert {
  215. condition = digitalocean_firewall.this.name == "foundation-tor1-b-baseline"
  216. error_message = "A resource_key must replace the region in the firewall name."
  217. }
  218. assert {
  219. condition = contains(digitalocean_firewall.this.tags, "cah-baseline-tor1")
  220. error_message = "The tag is the caller's; resource_key must not touch it."
  221. }
  222. }
  223. run "an_empty_resource_key_means_the_region_in_the_firewall_name" {
  224. command = plan
  225. variables {
  226. resource_key = ""
  227. }
  228. assert {
  229. condition = digitalocean_firewall.this.name == "foundation-tor1-baseline"
  230. error_message = "An empty resource_key must leave the name as before."
  231. }
  232. }
  233. run "rejects_a_bad_resource_key" {
  234. command = plan
  235. variables {
  236. resource_key = "x"
  237. }
  238. expect_failures = [var.resource_key]
  239. }
  240. # ---- inbound_from_other_tags / outbound_to_other_tags (stage 2 design 4.2) ----
  241. # Golden: the connector firewall exactly as stacks/10-access calls it. The new
  242. # inputs are left at their defaults, so the rendered descriptors must be the
  243. # ones the live stack has today (plan no-op).
  244. run "golden_stack_style_call_is_unchanged_with_default_other_tags" {
  245. command = plan
  246. variables {
  247. layer = "access"
  248. purpose = "connector"
  249. tag = "cah-connector-tor1"
  250. inbound_from_tag_rules = [{ protocol = "tcp", port_range = "22" }]
  251. outbound_to_tag_rules = [{ protocol = "tcp", port_range = "22" }]
  252. extra_outbound_rules = [
  253. { protocol = "tcp", port_range = "30000-31000" },
  254. { protocol = "udp", port_range = "1-65535" },
  255. ]
  256. }
  257. assert {
  258. condition = jsonencode(output.inbound_rules) == jsonencode(["tcp/22/cah-connector-tor1"])
  259. error_message = "Golden inbound descriptors changed."
  260. }
  261. assert {
  262. condition = jsonencode(output.outbound_tag_rules) == jsonencode(["tcp/22/cah-connector-tor1"])
  263. error_message = "Golden outbound tag descriptors changed."
  264. }
  265. assert {
  266. condition = jsonencode(output.outbound_rules) == jsonencode(sort([
  267. "tcp/443/0.0.0.0/0,::/0",
  268. "tcp/80/0.0.0.0/0,::/0",
  269. "tcp/53/0.0.0.0/0,::/0",
  270. "udp/53/0.0.0.0/0,::/0",
  271. "udp/123/0.0.0.0/0,::/0",
  272. "tcp/30000-31000/0.0.0.0/0,::/0",
  273. "udp/1-65535/0.0.0.0/0,::/0",
  274. ]))
  275. error_message = "Golden address outbound descriptors changed."
  276. }
  277. assert {
  278. condition = output.inbound_rule_count == 1 && output.outbound_rule_count == 8
  279. error_message = "Golden rule counts changed."
  280. }
  281. assert {
  282. condition = jsonencode(output.source_members) == jsonencode(["cah-connector-tor1"]) && jsonencode(output.destination_members) == jsonencode(["cah-connector-tor1"])
  283. error_message = "Golden members changed."
  284. }
  285. }
  286. run "golden_baseline_call_is_unchanged_with_default_other_tags" {
  287. command = plan
  288. assert {
  289. condition = output.inbound_rule_count == 0 && output.outbound_rule_count == 5 && length(output.source_members) == 0 && length(output.destination_members) == 0
  290. error_message = "The baseline firewall must plan exactly as before."
  291. }
  292. }
  293. run "metrics_style_rule_from_another_tag_is_tag_sourced_with_no_address" {
  294. command = plan
  295. variables {
  296. layer = "soc"
  297. purpose = "soc"
  298. tag = "cah-soc-tor1"
  299. inbound_from_other_tags = [{ protocol = "tcp", port_range = "3000", tag = "cah-connector-tor1" }]
  300. }
  301. assert {
  302. condition = jsonencode(output.inbound_rules) == jsonencode(["tcp/3000/cah-connector-tor1"])
  303. error_message = "The rule must be sourced from the other tag."
  304. }
  305. assert {
  306. condition = alltrue([
  307. for r in digitalocean_firewall.this.inbound_rule :
  308. length(coalesce(r.source_addresses, [])) == 0 && length(coalesce(r.source_droplet_ids, [])) == 0 && length(coalesce(r.source_load_balancer_uids, [])) == 0 && length(coalesce(r.source_kubernetes_ids, [])) == 0
  309. ])
  310. error_message = "No inbound rule may have an address (or any non-tag) source."
  311. }
  312. assert {
  313. condition = jsonencode(output.source_members) == jsonencode(["cah-connector-tor1"])
  314. error_message = "source_members must include the other tag."
  315. }
  316. assert {
  317. condition = jsonencode(output.rule_targets) == jsonencode(["cah-soc-tor1"])
  318. error_message = "rule_targets must stay the firewall's own tag."
  319. }
  320. }
  321. run "own_and_other_tag_inbound_rules_coexist" {
  322. command = plan
  323. variables {
  324. inbound_from_tag_rules = [{ protocol = "tcp", port_range = "22" }]
  325. inbound_from_other_tags = [{ protocol = "tcp", port_range = "9100", tag = "cah-soc-tor1" }, { protocol = "tcp", port_range = "9100", tag = "team:b_1" }]
  326. }
  327. assert {
  328. condition = jsonencode(output.inbound_rules) == jsonencode(sort(["tcp/22/cah-baseline-tor1", "tcp/9100/cah-soc-tor1", "tcp/9100/team:b_1"]))
  329. error_message = "Own-tag and other-tag inbound rules must all render."
  330. }
  331. assert {
  332. condition = jsonencode(output.source_members) == jsonencode(sort(["cah-baseline-tor1", "cah-soc-tor1", "team:b_1"]))
  333. error_message = "Members must list every tag."
  334. }
  335. }
  336. run "outbound_to_other_tags_renders_tag_destined_rules_and_members" {
  337. command = plan
  338. variables {
  339. outbound_to_tag_rules = [{ protocol = "tcp", port_range = "22" }]
  340. outbound_to_other_tags = [{ protocol = "tcp", port_range = "3000", tag = "cah-soc-tor1" }]
  341. }
  342. assert {
  343. condition = jsonencode(output.outbound_tag_rules) == jsonencode(sort(["tcp/22/cah-baseline-tor1", "tcp/3000/cah-soc-tor1"]))
  344. error_message = "Other-tag outbound rule must render with its destination tag."
  345. }
  346. assert {
  347. condition = jsonencode(output.destination_members) == jsonencode(sort(["cah-baseline-tor1", "cah-soc-tor1"]))
  348. error_message = "destination_members must include the other tag."
  349. }
  350. assert {
  351. condition = alltrue([
  352. for r in digitalocean_firewall.this.outbound_rule :
  353. length(coalesce(r.destination_tags, [])) == 0 ? true : length(coalesce(r.destination_addresses, [])) == 0
  354. ])
  355. error_message = "Tag outbound rules must carry no address."
  356. }
  357. assert {
  358. condition = output.outbound_rule_count == 7 && length(output.outbound_rules) == 5
  359. error_message = "Address outbound set must be unchanged."
  360. }
  361. }
  362. run "other_tag_bad_protocol_is_rejected" {
  363. command = plan
  364. variables {
  365. inbound_from_other_tags = [{ protocol = "icmp", port_range = "22", tag = "t" }]
  366. }
  367. expect_failures = [var.inbound_from_other_tags]
  368. }
  369. run "other_tag_port_zero_is_rejected" {
  370. command = plan
  371. variables {
  372. inbound_from_other_tags = [{ protocol = "tcp", port_range = "0", tag = "t" }]
  373. }
  374. expect_failures = [var.inbound_from_other_tags]
  375. }
  376. run "other_tag_port_above_range_is_rejected" {
  377. command = plan
  378. variables {
  379. outbound_to_other_tags = [{ protocol = "tcp", port_range = "65536", tag = "t" }]
  380. }
  381. expect_failures = [var.outbound_to_other_tags]
  382. }
  383. run "other_tag_descending_range_is_rejected" {
  384. command = plan
  385. variables {
  386. inbound_from_other_tags = [{ protocol = "tcp", port_range = "200-100", tag = "t" }]
  387. }
  388. expect_failures = [var.inbound_from_other_tags]
  389. }
  390. run "other_tag_non_numeric_port_is_rejected" {
  391. command = plan
  392. variables {
  393. inbound_from_other_tags = [{ protocol = "tcp", port_range = "all", tag = "t" }]
  394. }
  395. expect_failures = [var.inbound_from_other_tags]
  396. }
  397. run "other_tag_empty_tag_is_rejected" {
  398. command = plan
  399. variables {
  400. inbound_from_other_tags = [{ protocol = "tcp", port_range = "22", tag = "" }]
  401. }
  402. expect_failures = [var.inbound_from_other_tags]
  403. }
  404. run "other_tag_with_bad_characters_is_rejected" {
  405. command = plan
  406. variables {
  407. outbound_to_other_tags = [{ protocol = "tcp", port_range = "22", tag = "0.0.0.0/0" }]
  408. }
  409. expect_failures = [var.outbound_to_other_tags]
  410. }
  411. run "other_tag_over_255_characters_is_rejected" {
  412. command = plan
  413. variables {
  414. inbound_from_other_tags = [{ protocol = "tcp", port_range = "22", tag = join("", [for i in range(256) : "a"]) }]
  415. }
  416. expect_failures = [var.inbound_from_other_tags]
  417. }
  418. run "duplicate_other_tag_entries_are_rejected" {
  419. command = plan
  420. variables {
  421. inbound_from_other_tags = [
  422. { protocol = "tcp", port_range = "22", tag = "t" },
  423. { protocol = "tcp", port_range = "22", tag = "t" },
  424. ]
  425. }
  426. expect_failures = [var.inbound_from_other_tags]
  427. }
  428. run "same_port_for_different_tags_is_allowed" {
  429. command = plan
  430. variables {
  431. inbound_from_other_tags = [
  432. { protocol = "tcp", port_range = "22", tag = "a" },
  433. { protocol = "tcp", port_range = "22", tag = "b" },
  434. ]
  435. }
  436. assert {
  437. condition = output.inbound_rule_count == 2
  438. error_message = "Distinct tags on one port are two rules."
  439. }
  440. }
  441. run "inbound_other_tag_equal_to_own_tag_is_rejected" {
  442. command = plan
  443. variables {
  444. inbound_from_other_tags = [{ protocol = "tcp", port_range = "22", tag = "cah-baseline-tor1" }]
  445. }
  446. expect_failures = [digitalocean_firewall.this]
  447. }
  448. run "outbound_other_tag_equal_to_own_tag_is_rejected" {
  449. command = plan
  450. variables {
  451. outbound_to_other_tags = [{ protocol = "tcp", port_range = "22", tag = "cah-baseline-tor1" }]
  452. }
  453. expect_failures = [digitalocean_firewall.this]
  454. }
  455. # ---- extra_outbound_rules destinations (issue #245) ----
  456. run "a_rule_with_ipv4_destinations_emits_only_those_addresses" {
  457. command = plan
  458. variables {
  459. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["203.0.113.0/24", "198.51.100.7"] }]
  460. }
  461. assert {
  462. condition = output.outbound_rule_count == 6
  463. error_message = "The rule must be appended to the five defaults."
  464. }
  465. assert {
  466. condition = jsonencode(output.outbound_rules) == jsonencode(sort([
  467. "tcp/443/0.0.0.0/0,::/0",
  468. "tcp/80/0.0.0.0/0,::/0",
  469. "tcp/53/0.0.0.0/0,::/0",
  470. "udp/53/0.0.0.0/0,::/0",
  471. "udp/123/0.0.0.0/0,::/0",
  472. "tcp/5201/198.51.100.7,203.0.113.0/24",
  473. ]))
  474. error_message = "A rule with destinations must emit exactly those addresses and the defaults must be unchanged."
  475. }
  476. assert {
  477. condition = alltrue([
  478. for r in digitalocean_firewall.this.outbound_rule :
  479. r.port_range != "5201" || (!contains(r.destination_addresses, "0.0.0.0/0") && !contains(r.destination_addresses, "::/0"))
  480. ])
  481. error_message = "A rule with destinations must not carry either any-address entry."
  482. }
  483. }
  484. run "an_ipv4_only_destination_list_opens_no_ipv6" {
  485. command = plan
  486. variables {
  487. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["203.0.113.0/24"] }]
  488. }
  489. assert {
  490. condition = alltrue([
  491. for r in digitalocean_firewall.this.outbound_rule :
  492. r.port_range != "5201" || alltrue([for a in r.destination_addresses : !strcontains(a, ":")])
  493. ])
  494. error_message = "A destination list of IPv4 entries must not add an IPv6 destination."
  495. }
  496. }
  497. run "an_ipv6_destination_list_emits_only_ipv6" {
  498. command = plan
  499. variables {
  500. extra_outbound_rules = [{ protocol = "udp", port_range = "5201", destinations = ["2001:db8::/32", "2001:db8:1::7"] }]
  501. }
  502. assert {
  503. condition = contains(output.outbound_rules, "udp/5201/2001:db8:1::7,2001:db8::/32")
  504. error_message = "An IPv6 list must emit exactly its own entries."
  505. }
  506. assert {
  507. condition = alltrue([
  508. for r in digitalocean_firewall.this.outbound_rule :
  509. r.port_range != "5201" || alltrue([for a in r.destination_addresses : strcontains(a, ":")])
  510. ])
  511. error_message = "A destination list of IPv6 entries must not add an IPv4 destination."
  512. }
  513. }
  514. run "a_mixed_list_emits_both_families_as_given" {
  515. command = plan
  516. variables {
  517. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["203.0.113.7", "2001:db8::7"] }]
  518. }
  519. assert {
  520. condition = contains(output.outbound_rules, "tcp/5201/2001:db8::7,203.0.113.7")
  521. error_message = "A mixed list must emit both entries and nothing more."
  522. }
  523. }
  524. run "a_rule_without_destinations_is_still_any_address" {
  525. command = plan
  526. variables {
  527. extra_outbound_rules = [
  528. { protocol = "tcp", port_range = "30000-31000" },
  529. { protocol = "udp", port_range = "1-65535", destinations = [] },
  530. ]
  531. }
  532. assert {
  533. condition = jsonencode(output.outbound_rules) == jsonencode(sort([
  534. "tcp/443/0.0.0.0/0,::/0",
  535. "tcp/80/0.0.0.0/0,::/0",
  536. "tcp/53/0.0.0.0/0,::/0",
  537. "udp/53/0.0.0.0/0,::/0",
  538. "udp/123/0.0.0.0/0,::/0",
  539. "tcp/30000-31000/0.0.0.0/0,::/0",
  540. "udp/1-65535/0.0.0.0/0,::/0",
  541. ]))
  542. error_message = "A rule with no destinations (unset or empty) must keep 0.0.0.0/0 and ::/0."
  543. }
  544. }
  545. run "destination_rules_mix_with_any_address_rules" {
  546. command = plan
  547. variables {
  548. extra_outbound_rules = [
  549. { protocol = "tcp", port_range = "30000-31000" },
  550. { protocol = "tcp", port_range = "5201", destinations = ["203.0.113.7"] },
  551. ]
  552. }
  553. assert {
  554. condition = contains(output.outbound_rules, "tcp/30000-31000/0.0.0.0/0,::/0") && contains(output.outbound_rules, "tcp/5201/203.0.113.7")
  555. error_message = "A narrowed rule must not change the any-address rule next to it."
  556. }
  557. }
  558. run "an_invalid_destination_is_refused" {
  559. command = plan
  560. variables {
  561. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["203.0.113.0/33"] }]
  562. }
  563. expect_failures = [var.extra_outbound_rules]
  564. }
  565. run "a_hostname_destination_is_refused" {
  566. command = plan
  567. variables {
  568. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["example.com"] }]
  569. }
  570. expect_failures = [var.extra_outbound_rules]
  571. }
  572. run "a_malformed_ipv4_destination_is_refused" {
  573. command = plan
  574. variables {
  575. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["999.1.1.1"] }]
  576. }
  577. expect_failures = [var.extra_outbound_rules]
  578. }
  579. run "a_malformed_ipv6_destination_is_refused" {
  580. command = plan
  581. variables {
  582. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["2001:db8::/129"] }]
  583. }
  584. expect_failures = [var.extra_outbound_rules]
  585. }
  586. run "a_duplicate_destination_is_refused" {
  587. command = plan
  588. variables {
  589. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["203.0.113.7", "203.0.113.7"] }]
  590. }
  591. expect_failures = [var.extra_outbound_rules]
  592. }
  593. run "a_world_open_ipv4_entry_without_opt_in_is_refused" {
  594. command = plan
  595. variables {
  596. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["203.0.113.7", "0.0.0.0/0"] }]
  597. }
  598. expect_failures = [var.extra_outbound_rules]
  599. }
  600. run "a_world_open_ipv6_entry_without_opt_in_is_refused" {
  601. command = plan
  602. variables {
  603. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["::/0"] }]
  604. }
  605. expect_failures = [var.extra_outbound_rules]
  606. }
  607. run "an_expanded_ipv6_world_entry_without_opt_in_is_refused" {
  608. command = plan
  609. variables {
  610. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["0:0:0:0:0:0:0:0/0"] }]
  611. }
  612. expect_failures = [var.extra_outbound_rules]
  613. }
  614. run "an_unspecified_address_without_opt_in_is_refused" {
  615. command = plan
  616. variables {
  617. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["0.0.0.0"] }]
  618. }
  619. expect_failures = [var.extra_outbound_rules]
  620. }
  621. run "a_world_open_entry_with_the_explicit_opt_in_is_allowed" {
  622. command = plan
  623. variables {
  624. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["0.0.0.0/0"], allow_world_destinations = true }]
  625. }
  626. assert {
  627. condition = contains(output.outbound_rules, "tcp/5201/0.0.0.0/0")
  628. error_message = "With allow_world_destinations the listed world entry renders, and only that one (no IPv6 added)."
  629. }
  630. }
  631. run "the_opt_in_flag_without_a_world_entry_is_refused" {
  632. command = plan
  633. variables {
  634. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["203.0.113.7"], allow_world_destinations = true }]
  635. }
  636. expect_failures = [var.extra_outbound_rules]
  637. }
  638. run "the_opt_in_flag_does_not_excuse_another_rule" {
  639. command = plan
  640. variables {
  641. extra_outbound_rules = [
  642. { protocol = "tcp", port_range = "5201", destinations = ["0.0.0.0/0"], allow_world_destinations = true },
  643. { protocol = "tcp", port_range = "5202", destinations = ["0.0.0.0/0"] },
  644. ]
  645. }
  646. expect_failures = [var.extra_outbound_rules]
  647. }
  648. run "an_expanded_unspecified_ipv6_address_without_opt_in_is_refused" {
  649. command = plan
  650. variables {
  651. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["0:0:0:0:0:0:0:0"] }]
  652. }
  653. expect_failures = [var.extra_outbound_rules]
  654. }
  655. run "a_compressed_zero_ipv6_address_without_opt_in_is_refused" {
  656. command = plan
  657. variables {
  658. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["0::0"] }]
  659. }
  660. expect_failures = [var.extra_outbound_rules]
  661. }
  662. run "a_real_ipv6_address_with_zero_groups_is_not_world" {
  663. command = plan
  664. variables {
  665. extra_outbound_rules = [{ protocol = "tcp", port_range = "5201", destinations = ["2001:db8::10", "::1"] }]
  666. }
  667. assert {
  668. condition = contains(output.outbound_rules, "tcp/5201/2001:db8::10,::1")
  669. error_message = "Only an all-zero address is world-open; others must pass."
  670. }
  671. }