bootstrap.tftest.hcl 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454
  1. mock_provider "twingate" {}
  2. variables {
  3. layer = "access"
  4. region = "tor1"
  5. instance = "a"
  6. remote_network_id = "remote-network-test"
  7. twingate_network = "acme"
  8. admin_ssh_public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAItest"
  9. reboot_time = "03:00"
  10. }
  11. run "instance_rejects_uppercase" {
  12. command = plan
  13. variables {
  14. instance = "A"
  15. }
  16. expect_failures = [var.instance]
  17. }
  18. run "instance_rejects_more_than_one_character" {
  19. command = plan
  20. variables {
  21. instance = "ab"
  22. }
  23. expect_failures = [var.instance]
  24. }
  25. run "instance_rejects_empty" {
  26. command = plan
  27. variables {
  28. instance = ""
  29. }
  30. expect_failures = [var.instance]
  31. }
  32. run "cloud_init_installs_connector_for_the_network" {
  33. command = plan
  34. assert {
  35. condition = strcontains(output.user_data, "TWINGATE_URL=https://acme.twingate.com")
  36. error_message = "cloud-init must point the connector at the Twingate network."
  37. }
  38. }
  39. run "network_slug_accepts_bare_subdomain" {
  40. command = plan
  41. variables {
  42. twingate_network = "acme-2"
  43. }
  44. assert {
  45. condition = strcontains(output.user_data, "TWINGATE_URL=https://acme-2.twingate.com")
  46. error_message = "A bare subdomain must be accepted."
  47. }
  48. }
  49. run "network_slug_rejects_single_quote" {
  50. command = plan
  51. variables {
  52. twingate_network = "acme'; id #"
  53. }
  54. expect_failures = [var.twingate_network]
  55. }
  56. run "network_slug_rejects_double_quote" {
  57. command = plan
  58. variables {
  59. twingate_network = "acme\"x"
  60. }
  61. expect_failures = [var.twingate_network]
  62. }
  63. run "network_slug_rejects_full_url" {
  64. command = plan
  65. variables {
  66. twingate_network = "https://acme.twingate.com"
  67. }
  68. expect_failures = [var.twingate_network]
  69. }
  70. run "install_fails_loudly_when_download_fails" {
  71. command = plan
  72. assert {
  73. condition = strcontains(output.user_data, "pipefail") && strcontains(output.user_data, "--proto '=https' --tlsv1.2")
  74. error_message = "The install pipeline must use pipefail and hardened curl."
  75. }
  76. }
  77. run "cloud_init_is_valid_yaml_with_a_key_only_admin_user" {
  78. command = plan
  79. assert {
  80. condition = yamldecode(output.user_data).disable_root == true && yamldecode(output.user_data).ssh_pwauth == false
  81. error_message = "Root login and SSH passwords must be disabled in cloud-init."
  82. }
  83. assert {
  84. condition = length(yamldecode(output.user_data).users) == 1 && yamldecode(output.user_data).users[0].name == "cah-admin"
  85. error_message = "Exactly one user, cah-admin, must be declared (no default ubuntu user)."
  86. }
  87. assert {
  88. condition = yamldecode(output.user_data).users[0].ssh_authorized_keys == ["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAItest"]
  89. error_message = "cah-admin must carry the admin public key."
  90. }
  91. assert {
  92. condition = yamldecode(output.user_data).users[0].lock_passwd == true && yamldecode(output.user_data).users[0].sudo == "ALL=(ALL) NOPASSWD:ALL"
  93. error_message = "cah-admin must have a locked password and key-based passwordless sudo."
  94. }
  95. }
  96. run "sshd_dropin_has_the_hardening_settings" {
  97. command = plan
  98. assert {
  99. condition = alltrue([
  100. for line in [
  101. "PermitRootLogin no", "PasswordAuthentication no", "KbdInteractiveAuthentication no",
  102. "PubkeyAuthentication yes", "X11Forwarding no", "AllowUsers cah-admin",
  103. "MaxAuthTries 3", "LoginGraceTime 20",
  104. ] :
  105. contains(split("\n", one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/ssh/sshd_config.d/99-cah-hardening.conf"])), line)
  106. ])
  107. error_message = "The sshd drop-in must contain every hardening line."
  108. }
  109. assert {
  110. condition = !can(regex("(?i)PermitRootLogin\\s+(yes|prohibit-password|without-password)", output.user_data))
  111. error_message = "No part of the user_data may allow root login."
  112. }
  113. }
  114. run "unattended_upgrades_reboot_at_the_instance_time" {
  115. command = plan
  116. variables {
  117. reboot_time = "04:00"
  118. }
  119. assert {
  120. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/apt/apt.conf.d/52cah-unattended-upgrades"]), "Unattended-Upgrade::Automatic-Reboot-Time \"04:00\";")
  121. error_message = "The reboot time must come from the reboot_time input."
  122. }
  123. assert {
  124. condition = alltrue([
  125. for line in ["Unattended-Upgrade::Automatic-Reboot \"true\";", "Unattended-Upgrade::Automatic-Reboot-WithUsers \"true\";", "Unattended-Upgrade::Remove-Unused-Dependencies \"true\";"] :
  126. strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/apt/apt.conf.d/52cah-unattended-upgrades"]), line)
  127. ])
  128. error_message = "Automatic reboot (with users) and unused-dependency removal must be on."
  129. }
  130. assert {
  131. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/cron.d/twingate-connector-update"]), "30 4 * * 0 root")
  132. error_message = "The weekly connector update must run half an hour after the reboot time."
  133. }
  134. }
  135. run "update_time_carries_across_the_hour_and_midnight" {
  136. command = plan
  137. variables {
  138. reboot_time = "23:45"
  139. }
  140. assert {
  141. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/cron.d/twingate-connector-update"]), "15 0 * * 0 root")
  142. error_message = "23:45 plus 30 minutes must be 00:15."
  143. }
  144. }
  145. run "chrony_and_sysctl_are_configured_without_touching_ip_forward" {
  146. command = plan
  147. assert {
  148. condition = contains(yamldecode(output.user_data).packages, "chrony") && contains(yamldecode(output.user_data).packages, "unattended-upgrades")
  149. error_message = "chrony and unattended-upgrades must be installed."
  150. }
  151. assert {
  152. condition = alltrue([
  153. for k in ["net.ipv4.tcp_syncookies = 1", "net.ipv4.conf.all.log_martians = 1", "net.ipv4.conf.all.accept_redirects = 0", "net.ipv4.conf.all.accept_source_route = 0", "net.ipv4.conf.all.rp_filter = 2"] :
  154. strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/sysctl.d/99-cah-hardening.conf"]), k)
  155. ])
  156. error_message = "The sysctl drop-in must carry the safe hardening settings."
  157. }
  158. assert {
  159. condition = !can(regex("(?m)^\\s*net\\.ipv[46]\\.(conf\\.[a-z]+\\.)?(ip_)?forwarding\\s*=", output.user_data))
  160. error_message = "The sysctl drop-in must not change forwarding; the connector may need it."
  161. }
  162. }
  163. run "tokens_appear_only_in_the_root_only_config" {
  164. command = apply
  165. override_resource {
  166. target = twingate_connector_tokens.this
  167. values = {
  168. access_token = "FAKE-ACCESS-TOKEN-2"
  169. refresh_token = "FAKE-REFRESH-TOKEN-2"
  170. }
  171. }
  172. assert {
  173. condition = length(regexall("FAKE-ACCESS-TOKEN-2", output.user_data)) == 1 && length(regexall("FAKE-REFRESH-TOKEN-2", output.user_data)) == 1
  174. error_message = "Each token must appear exactly once, in the connector config."
  175. }
  176. assert {
  177. condition = alltrue([
  178. for f in yamldecode(output.user_data).write_files :
  179. f.path == "/etc/twingate/connector.conf" && f.permissions == "0600" && f.owner == "root:root"
  180. if strcontains(f.content, "FAKE-ACCESS-TOKEN-2") || strcontains(f.content, "FAKE-REFRESH-TOKEN-2")
  181. ]) && length([for f in yamldecode(output.user_data).write_files : f if strcontains(f.content, "FAKE-ACCESS-TOKEN-2")]) == 1
  182. error_message = "The tokens may only be written to /etc/twingate/connector.conf, mode 0600, owned by root."
  183. }
  184. assert {
  185. condition = !anytrue([for c in yamldecode(output.user_data).runcmd : strcontains(jsonencode(c), "FAKE-ACCESS-TOKEN-2") || strcontains(jsonencode(c), "FAKE-REFRESH-TOKEN-2")])
  186. error_message = "No runcmd step may carry a token."
  187. }
  188. }
  189. run "reboot_time_rejects_a_bad_hour" {
  190. command = plan
  191. variables {
  192. reboot_time = "24:00"
  193. }
  194. expect_failures = [var.reboot_time]
  195. }
  196. run "reboot_time_rejects_a_bad_minute" {
  197. command = plan
  198. variables {
  199. reboot_time = "03:60"
  200. }
  201. expect_failures = [var.reboot_time]
  202. }
  203. run "reboot_time_rejects_a_missing_zero_pad" {
  204. command = plan
  205. variables {
  206. reboot_time = "3:00"
  207. }
  208. expect_failures = [var.reboot_time]
  209. }
  210. # A normal OpenSSH public key carries a trailing comment. It is accepted, and it
  211. # is dropped before the key reaches cloud-init, so user_data is the same with or
  212. # without it.
  213. run "admin_key_accepts_a_comment_and_renders_without_it" {
  214. command = plan
  215. variables {
  216. admin_ssh_public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAItest user@host.example"
  217. }
  218. assert {
  219. condition = yamldecode(output.user_data).users[0].ssh_authorized_keys == ["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAItest"]
  220. error_message = "The rendered key must be '<algorithm> <key>' with the comment removed."
  221. }
  222. assert {
  223. condition = !strcontains(output.user_data, "user@host.example")
  224. error_message = "The comment must not appear anywhere in user_data."
  225. }
  226. }
  227. # Golden: the rendered cloud-init for the live connectors, with fixed tokens,
  228. # measured on origin/main before resource_key and the comment-tolerant key
  229. # existed. A change here rebuilds the droplets (user_data is force-new), so it
  230. # must be a deliberate step, never a side effect. Re-taken for #235 (auditd -a
  231. # rules) and #305 (the first-hour order comment): the values only change with the header comment, since log_shipping is off in these renders.
  232. run "user_data_is_byte_identical_to_the_live_connector_a" {
  233. command = plan
  234. override_resource {
  235. target = twingate_connector_tokens.this
  236. values = {
  237. access_token = "fixed-access-token"
  238. refresh_token = "fixed-refresh-token"
  239. }
  240. }
  241. variables {
  242. reboot_time = "04:00"
  243. }
  244. assert {
  245. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  246. error_message = "user_data for connector a changed. The golden hash was re-taken for the one-key apt key check (#288); a different value means droplets would be replaced."
  247. }
  248. }
  249. run "user_data_is_byte_identical_to_the_live_connector_b" {
  250. command = plan
  251. override_resource {
  252. target = twingate_connector_tokens.this
  253. values = {
  254. access_token = "fixed-access-token"
  255. refresh_token = "fixed-refresh-token"
  256. }
  257. }
  258. variables {
  259. instance = "b"
  260. reboot_time = "05:00"
  261. }
  262. assert {
  263. condition = sha256(output.user_data) == "f79a0bbf3ce1b3dbb5ead9b39c94d27ce7aaac71018d1aa4d425b3c3724a85e6"
  264. error_message = "user_data for connector b changed. The golden hash was re-taken for the one-key apt key check (#288); a different value means droplets would be replaced."
  265. }
  266. }
  267. run "user_data_with_a_comment_matches_the_golden_hash" {
  268. command = plan
  269. override_resource {
  270. target = twingate_connector_tokens.this
  271. values = {
  272. access_token = "fixed-access-token"
  273. refresh_token = "fixed-refresh-token"
  274. }
  275. }
  276. variables {
  277. reboot_time = "04:00"
  278. admin_ssh_public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAItest laptop-2026"
  279. }
  280. assert {
  281. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  282. error_message = "A key with a comment must render the same user_data as the key without one."
  283. }
  284. }
  285. run "admin_key_accepts_rsa_with_padding_and_ecdsa" {
  286. command = plan
  287. variables {
  288. admin_ssh_public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAAtest== me"
  289. }
  290. assert {
  291. condition = yamldecode(output.user_data).users[0].ssh_authorized_keys == ["ssh-rsa AAAAB3NzaC1yc2EAAAAtest=="]
  292. error_message = "An rsa key with padding must be accepted and rendered without its comment."
  293. }
  294. }
  295. run "admin_key_accepts_ecdsa" {
  296. command = plan
  297. variables {
  298. admin_ssh_public_key = "ecdsa-sha2-nistp256 AAAAE2VjZHNhtest"
  299. }
  300. assert {
  301. condition = yamldecode(output.user_data).users[0].ssh_authorized_keys == ["ecdsa-sha2-nistp256 AAAAE2VjZHNhtest"]
  302. error_message = "An ecdsa key must be accepted."
  303. }
  304. }
  305. run "admin_key_rejects_a_two_word_comment" {
  306. command = plan
  307. variables {
  308. admin_ssh_public_key = "ssh-ed25519 AAAA two words"
  309. }
  310. expect_failures = [var.admin_ssh_public_key]
  311. }
  312. run "admin_key_rejects_a_newline_after_the_comment" {
  313. command = plan
  314. variables {
  315. admin_ssh_public_key = "ssh-ed25519 AAAA user@host\n - evil"
  316. }
  317. expect_failures = [var.admin_ssh_public_key]
  318. }
  319. run "admin_key_rejects_a_trailing_newline" {
  320. command = plan
  321. variables {
  322. admin_ssh_public_key = "ssh-ed25519 AAAA\n"
  323. }
  324. expect_failures = [var.admin_ssh_public_key]
  325. }
  326. run "admin_key_rejects_a_tab" {
  327. command = plan
  328. variables {
  329. admin_ssh_public_key = "ssh-ed25519\tAAAA"
  330. }
  331. expect_failures = [var.admin_ssh_public_key]
  332. }
  333. run "admin_key_rejects_a_trailing_space" {
  334. command = plan
  335. variables {
  336. admin_ssh_public_key = "ssh-ed25519 AAAA "
  337. }
  338. expect_failures = [var.admin_ssh_public_key]
  339. }
  340. run "admin_key_rejects_unsafe_comment_characters" {
  341. command = plan
  342. variables {
  343. admin_ssh_public_key = "ssh-ed25519 AAAA user;id"
  344. }
  345. expect_failures = [var.admin_ssh_public_key]
  346. }
  347. run "admin_key_rejects_a_quote_in_the_comment" {
  348. command = plan
  349. variables {
  350. admin_ssh_public_key = "ssh-ed25519 AAAA us\"er"
  351. }
  352. expect_failures = [var.admin_ssh_public_key]
  353. }
  354. run "admin_key_rejects_a_hash_in_the_comment" {
  355. command = plan
  356. variables {
  357. admin_ssh_public_key = "ssh-ed25519 AAAA #x"
  358. }
  359. expect_failures = [var.admin_ssh_public_key]
  360. }
  361. run "admin_key_rejects_a_dollar_brace_in_the_comment" {
  362. command = plan
  363. variables {
  364. admin_ssh_public_key = "ssh-ed25519 AAAA $${x}"
  365. }
  366. expect_failures = [var.admin_ssh_public_key]
  367. }
  368. run "admin_key_rejects_a_comment_over_64_characters" {
  369. command = plan
  370. variables {
  371. admin_ssh_public_key = "ssh-ed25519 AAAA ${join("", [for i in range(65) : "a"])}"
  372. }
  373. expect_failures = [var.admin_ssh_public_key]
  374. }
  375. run "admin_key_rejects_an_unknown_algorithm" {
  376. command = plan
  377. variables {
  378. admin_ssh_public_key = "ssh-dss AAAA"
  379. }
  380. expect_failures = [var.admin_ssh_public_key]
  381. }
  382. run "admin_key_rejects_an_option_prefix" {
  383. command = plan
  384. variables {
  385. admin_ssh_public_key = "command=\"id\" ssh-ed25519 AAAA"
  386. }
  387. expect_failures = [var.admin_ssh_public_key]
  388. }
  389. run "admin_key_rejects_an_empty_key" {
  390. command = plan
  391. variables {
  392. admin_ssh_public_key = ""
  393. }
  394. expect_failures = [var.admin_ssh_public_key]
  395. }
  396. run "admin_key_rejects_a_key_with_no_body" {
  397. command = plan
  398. variables {
  399. admin_ssh_public_key = "ssh-ed25519"
  400. }
  401. expect_failures = [var.admin_ssh_public_key]
  402. }
  403. run "admin_key_rejects_a_non_base64_body" {
  404. command = plan
  405. variables {
  406. admin_ssh_public_key = "ssh-ed25519 AA!A"
  407. }
  408. expect_failures = [var.admin_ssh_public_key]
  409. }
  410. run "admin_key_rejects_a_leading_space" {
  411. command = plan
  412. variables {
  413. admin_ssh_public_key = " ssh-ed25519 AAAA"
  414. }
  415. expect_failures = [var.admin_ssh_public_key]
  416. }
  417. run "admin_key_rejects_yaml_injection" {
  418. command = plan
  419. variables {
  420. admin_ssh_public_key = "ssh-ed25519 AAAA\n - evil"
  421. }
  422. expect_failures = [var.admin_ssh_public_key]
  423. }
  424. # First-boot install resilience. The apt lock race with Ubuntu's own unattended
  425. # upgrades made the one-shot install abort before it wrote the connector config.
  426. run "apt_waits_for_the_dpkg_lock" {
  427. command = plan
  428. assert {
  429. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/apt/apt.conf.d/99cah-lock-wait"]), "DPkg::Lock::Timeout \"300\";")
  430. error_message = "apt must wait up to 300 seconds for the dpkg lock."
  431. }
  432. }
  433. run "installer_does_not_run_the_vendor_script_or_pipe_into_a_shell" {
  434. command = plan
  435. assert {
  436. condition = !anytrue([for c in yamldecode(output.user_data).runcmd : strcontains(jsonencode(c), "setup.sh")])
  437. error_message = "Twingate's setup.sh must not be run; piped into bash it can lose the rest of the script to apt."
  438. }
  439. assert {
  440. condition = !anytrue([for c in yamldecode(output.user_data).runcmd : can(regex("\\|\\s*(ba|da)?sh\\b", jsonencode(c)))])
  441. error_message = "Nothing may be piped into a shell."
  442. }
  443. }
  444. run "repository_key_is_pinned_and_service_is_verified" {
  445. command = plan
  446. assert {
  447. condition = alltrue([
  448. for c in yamldecode(output.user_data).runcmd :
  449. strcontains(jsonencode(c), "FPR=EC952DEE4701A407A82B0C505C363F09A9174A9E") && strcontains(jsonencode(c), "cah-apt-key-check /root/twingate.gpg.key") && strcontains(jsonencode(c), "signed-by=$KEYRING") && strcontains(jsonencode(c), "https://packages.twingate.com/apt/")
  450. if strcontains(jsonencode(c), "apt-get install -yq twingate-connector")
  451. ])
  452. error_message = "The Twingate repository key must be checked against the pinned fingerprint and the repository must be signed-by that key."
  453. }
  454. assert {
  455. condition = anytrue([
  456. for f in yamldecode(output.user_data).write_files :
  457. f.path == "/usr/local/sbin/cah-apt-key-check" && strcontains(f.content, "gpg --show-keys --with-colons")
  458. ])
  459. error_message = "The shared key check must list the key file with gpg and compare its fingerprint."
  460. }
  461. assert {
  462. condition = alltrue([
  463. for c in yamldecode(output.user_data).runcmd :
  464. strcontains(jsonencode(c), "systemctl enable --now twingate-connector") && strcontains(jsonencode(c), "systemctl is-active --quiet twingate-connector") && strcontains(jsonencode(c), "DEBIAN_FRONTEND=noninteractive")
  465. if strcontains(jsonencode(c), "apt-get install -yq twingate-connector")
  466. ])
  467. error_message = "An attempt only counts as a success when the service is active, and apt must run non-interactively."
  468. }
  469. }
  470. run "installer_is_retried_a_bounded_number_of_times" {
  471. command = plan
  472. assert {
  473. condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "apt-get install -yq twingate-connector")]) == 1
  474. error_message = "The connector package must be installed from exactly one runcmd step."
  475. }
  476. assert {
  477. condition = alltrue([
  478. for c in yamldecode(output.user_data).runcmd :
  479. strcontains(jsonencode(c), "seq 1 10") && strcontains(jsonencode(c), "sleep 30") && strcontains(jsonencode(c), "exit 1") && strcontains(jsonencode(c), "pipefail")
  480. if strcontains(jsonencode(c), "apt-get install -yq twingate-connector")
  481. ])
  482. error_message = "The install step must retry 10 times, 30 seconds apart, under pipefail, and exit 1 when every attempt fails."
  483. }
  484. assert {
  485. condition = !can(regex("(?m)^\\s*(set -[a-z]*x|bash -x)", output.user_data))
  486. error_message = "Shell tracing would print the tokens into the cloud-init log."
  487. }
  488. }
  489. run "install_outcome_is_logged_without_tokens" {
  490. command = apply
  491. override_resource {
  492. target = twingate_connector_tokens.this
  493. values = {
  494. access_token = "FAKE-ACCESS-TOKEN-3"
  495. refresh_token = "FAKE-REFRESH-TOKEN-3"
  496. }
  497. }
  498. assert {
  499. condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "/var/log/cah-connector-install.log")]) == 1
  500. error_message = "Exactly one step must write /var/log/cah-connector-install.log."
  501. }
  502. assert {
  503. condition = alltrue([
  504. for c in yamldecode(output.user_data).runcmd :
  505. strcontains(jsonencode(c), "systemctl is-active twingate-connector") && strcontains(jsonencode(c), "journalctl -u twingate-connector -n 50") && strcontains(jsonencode(c), "tail -n 50 /var/log/cloud-init-output.log") && strcontains(jsonencode(c), "chmod 0640")
  506. if strcontains(jsonencode(c), "/var/log/cah-connector-install.log")
  507. ])
  508. error_message = "The log step must record the service state, the last 50 journal lines and the cloud-init output tail, at mode 0640."
  509. }
  510. assert {
  511. condition = !anytrue([
  512. for c in yamldecode(output.user_data).runcmd :
  513. strcontains(jsonencode(c), "FAKE-ACCESS-TOKEN-3") || strcontains(jsonencode(c), "FAKE-REFRESH-TOKEN-3") || strcontains(jsonencode(c), "TWINGATE_ACCESS_TOKEN") || strcontains(jsonencode(c), "TWINGATE_REFRESH_TOKEN")
  514. if strcontains(jsonencode(c), "/var/log/cah-connector-install.log")
  515. ])
  516. error_message = "The log step must not read or print a token."
  517. }
  518. }
  519. run "machine_clock_is_eastern" {
  520. command = plan
  521. assert {
  522. condition = yamldecode(output.user_data).timezone == "America/New_York"
  523. error_message = "The reboot window and the weekly update are Eastern time, so the machine timezone must be America/New_York."
  524. }
  525. }
  526. run "eastern_schedule_for_connector_a_and_b" {
  527. command = plan
  528. variables {
  529. reboot_time = "05:00"
  530. }
  531. assert {
  532. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/cron.d/twingate-connector-update"]), "30 5 * * 0 root")
  533. error_message = "A 05:00 reboot must put the Sunday connector update at 05:30."
  534. }
  535. }
  536. run "the_module_exposes_only_a_name_and_the_boot_data_and_creates_no_cloud_resource" {
  537. command = plan
  538. assert {
  539. condition = jsonencode(sort(flatten(regexall("output \"([A-Za-z0-9_-]+)\"", file("${path.module}/outputs.tf"))))) == jsonencode(["name", "user_data"])
  540. error_message = "outputs.tf declares an output this test does not inspect. Add it only after proving it carries no connector secret."
  541. }
  542. assert {
  543. condition = length(regexall("(?m)^\\s*output \"user_data\"[^}]*sensitive\\s*=\\s*true", file("${path.module}/outputs.tf"))) == 1
  544. error_message = "user_data carries the connector tokens and must stay sensitive."
  545. }
  546. assert {
  547. condition = alltrue([for t in flatten(regexall("(?m)^\\s*resource\\s+\"([a-z0-9_]+)\"", file("${path.module}/main.tf"))) : startswith(t, "twingate_")])
  548. error_message = "The bootstrap module is cloud-neutral: it must create only twingate_* resources."
  549. }
  550. }
  551. run "connector_joins_the_callers_remote_network_and_is_named_by_instance" {
  552. command = plan
  553. variables {
  554. instance = "b"
  555. }
  556. assert {
  557. condition = twingate_connector.this.remote_network_id == "remote-network-test" && twingate_connector.this.name == "access-tor1-connector-b" && output.name == "access-tor1-connector-b"
  558. error_message = "The connector must join the caller's remote network and be named <layer>-<region>-connector-<instance>."
  559. }
  560. }
  561. # resource_key: the key in NAMES. Empty means region, so the live names stay.
  562. run "resource_key_defaults_to_the_region_in_the_name" {
  563. command = plan
  564. assert {
  565. condition = output.name == "access-tor1-connector-a"
  566. error_message = "With no resource_key the connector is named <layer>-<region>-connector-<instance>."
  567. }
  568. }
  569. run "resource_key_replaces_region_in_the_name_only" {
  570. command = plan
  571. variables {
  572. resource_key = "tor1-b"
  573. }
  574. assert {
  575. condition = output.name == "access-tor1-b-connector-a"
  576. error_message = "A resource_key must replace the region in the name."
  577. }
  578. }
  579. run "resource_key_rejects_uppercase" {
  580. command = plan
  581. variables {
  582. resource_key = "Tor1"
  583. }
  584. expect_failures = [var.resource_key]
  585. }
  586. run "resource_key_rejects_a_leading_digit" {
  587. command = plan
  588. variables {
  589. resource_key = "1tor"
  590. }
  591. expect_failures = [var.resource_key]
  592. }
  593. run "resource_key_rejects_one_character" {
  594. command = plan
  595. variables {
  596. resource_key = "a"
  597. }
  598. expect_failures = [var.resource_key]
  599. }
  600. run "resource_key_rejects_25_characters" {
  601. command = plan
  602. variables {
  603. resource_key = "abcdefghijklmnopqrstuvwxy"
  604. }
  605. expect_failures = [var.resource_key]
  606. }
  607. run "resource_key_accepts_24_characters" {
  608. command = plan
  609. variables {
  610. resource_key = "abcdefghijklmnopqrstuvwx"
  611. }
  612. assert {
  613. condition = output.name == "access-abcdefghijklmnopqrstuvwx-connector-a"
  614. error_message = "A 24 character key is the longest allowed."
  615. }
  616. }
  617. run "resource_key_rejects_an_underscore_and_a_space" {
  618. command = plan
  619. variables {
  620. resource_key = "tor_1"
  621. }
  622. expect_failures = [var.resource_key]
  623. }
  624. # --- timezone ----------------------------------------------------------------
  625. # The zone is an input now. The default must render exactly what the literal
  626. # did (the golden hashes above); a different zone may change only that line.
  627. run "default_timezone_renders_the_golden_user_data" {
  628. command = plan
  629. override_resource {
  630. target = twingate_connector_tokens.this
  631. values = {
  632. access_token = "fixed-access-token"
  633. refresh_token = "fixed-refresh-token"
  634. }
  635. }
  636. variables {
  637. reboot_time = "04:00"
  638. timezone = "America/New_York"
  639. }
  640. assert {
  641. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  642. error_message = "Passing the default timezone explicitly must render the golden user_data."
  643. }
  644. }
  645. run "another_timezone_changes_only_the_timezone_line" {
  646. command = plan
  647. override_resource {
  648. target = twingate_connector_tokens.this
  649. values = {
  650. access_token = "fixed-access-token"
  651. refresh_token = "fixed-refresh-token"
  652. }
  653. }
  654. variables {
  655. reboot_time = "04:00"
  656. timezone = "Europe/Paris"
  657. }
  658. assert {
  659. condition = yamldecode(output.user_data).timezone == "Europe/Paris"
  660. error_message = "The machine timezone must be the one given."
  661. }
  662. assert {
  663. condition = sha256(replace(output.user_data, "\ntimezone: Europe/Paris\n", "\ntimezone: America/New_York\n")) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  664. error_message = "Changing the timezone must change nothing but the timezone line."
  665. }
  666. }
  667. run "utc_is_accepted" {
  668. command = plan
  669. variables {
  670. timezone = "UTC"
  671. }
  672. assert {
  673. condition = yamldecode(output.user_data).timezone == "UTC"
  674. error_message = "UTC must be accepted."
  675. }
  676. }
  677. run "three_part_timezone_is_accepted" {
  678. command = plan
  679. variables {
  680. timezone = "America/Argentina/Buenos_Aires"
  681. }
  682. assert {
  683. condition = yamldecode(output.user_data).timezone == "America/Argentina/Buenos_Aires"
  684. error_message = "A three-part IANA name must be accepted."
  685. }
  686. }
  687. run "timezone_rejects_newline_injection" {
  688. command = plan
  689. variables {
  690. timezone = "America/New_York\nruncmd: [\"id\"]"
  691. }
  692. expect_failures = [var.timezone]
  693. }
  694. run "timezone_rejects_trailing_newline" {
  695. command = plan
  696. variables {
  697. timezone = "America/New_York\n"
  698. }
  699. expect_failures = [var.timezone]
  700. }
  701. run "timezone_rejects_yaml_special" {
  702. command = plan
  703. variables {
  704. timezone = "America/New_York: {a: b}"
  705. }
  706. expect_failures = [var.timezone]
  707. }
  708. run "timezone_rejects_comment_marker" {
  709. command = plan
  710. variables {
  711. timezone = "America/New_York # x"
  712. }
  713. expect_failures = [var.timezone]
  714. }
  715. run "timezone_rejects_space" {
  716. command = plan
  717. variables {
  718. timezone = "America/New York"
  719. }
  720. expect_failures = [var.timezone]
  721. }
  722. run "timezone_rejects_no_slash" {
  723. command = plan
  724. variables {
  725. timezone = "Eastern"
  726. }
  727. expect_failures = [var.timezone]
  728. }
  729. run "timezone_rejects_lowercase_utc" {
  730. command = plan
  731. variables {
  732. timezone = "utc"
  733. }
  734. expect_failures = [var.timezone]
  735. }
  736. run "timezone_rejects_empty" {
  737. command = plan
  738. variables {
  739. timezone = ""
  740. }
  741. expect_failures = [var.timezone]
  742. }
  743. run "timezone_rejects_four_parts" {
  744. command = plan
  745. variables {
  746. timezone = "A/b/c/d"
  747. }
  748. expect_failures = [var.timezone]
  749. }
  750. run "timezone_rejects_template_marker" {
  751. command = plan
  752. variables {
  753. timezone = "America/$${x}"
  754. }
  755. expect_failures = [var.timezone]
  756. }
  757. run "timezone_rejects_a_made_up_zone" {
  758. command = plan
  759. variables {
  760. timezone = "Foo/Bar"
  761. }
  762. expect_failures = [var.timezone]
  763. }
  764. run "timezone_rejects_a_plausible_but_missing_zone" {
  765. command = plan
  766. variables {
  767. timezone = "America/Definitely_Not_A_Zone"
  768. }
  769. expect_failures = [var.timezone]
  770. }
  771. run "timezone_rejects_an_out_of_range_etc_zone" {
  772. command = plan
  773. variables {
  774. timezone = "Etc/GMT+99"
  775. }
  776. expect_failures = [var.timezone]
  777. }
  778. # ---- connector telemetry (default off) ----
  779. run "explicit_default_telemetry_renders_the_golden_user_data_a" {
  780. command = plan
  781. override_resource {
  782. target = twingate_connector_tokens.this
  783. values = {
  784. access_token = "fixed-access-token"
  785. refresh_token = "fixed-refresh-token"
  786. }
  787. }
  788. variables {
  789. reboot_time = "04:00"
  790. connector_telemetry = {}
  791. }
  792. assert {
  793. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  794. error_message = "An empty connector_telemetry must render the golden user_data of connector a."
  795. }
  796. }
  797. run "explicit_default_telemetry_renders_the_golden_user_data_b" {
  798. command = plan
  799. override_resource {
  800. target = twingate_connector_tokens.this
  801. values = {
  802. access_token = "fixed-access-token"
  803. refresh_token = "fixed-refresh-token"
  804. }
  805. }
  806. variables {
  807. instance = "b"
  808. reboot_time = "05:00"
  809. connector_telemetry = {
  810. status_updates_enabled = true
  811. connection_logs = false
  812. labels = {}
  813. }
  814. }
  815. assert {
  816. condition = sha256(output.user_data) == "f79a0bbf3ce1b3dbb5ead9b39c94d27ce7aaac71018d1aa4d425b3c3724a85e6"
  817. error_message = "The spelled-out defaults must render the golden user_data of connector b."
  818. }
  819. }
  820. run "status_updates_is_provider_side_and_never_changes_user_data" {
  821. command = plan
  822. override_resource {
  823. target = twingate_connector_tokens.this
  824. values = {
  825. access_token = "fixed-access-token"
  826. refresh_token = "fixed-refresh-token"
  827. }
  828. }
  829. variables {
  830. reboot_time = "04:00"
  831. connector_telemetry = { status_updates_enabled = false }
  832. }
  833. assert {
  834. condition = twingate_connector.this.status_updates_enabled == false
  835. error_message = "status_updates_enabled must reach the Twingate connector."
  836. }
  837. assert {
  838. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  839. error_message = "status_updates_enabled must not touch user_data."
  840. }
  841. }
  842. run "status_updates_is_not_sent_unless_set" {
  843. command = plan
  844. assert {
  845. condition = strcontains(file("${path.module}/variables.tf"), "status_updates_enabled = optional(bool)\n") && strcontains(file("${path.module}/main.tf"), "status_updates_enabled = var.connector_telemetry.status_updates_enabled")
  846. error_message = "status_updates_enabled must be optional(bool) with no default (null when unset) and passed straight to the resource, so the default resource config is what it was before this setting existed."
  847. }
  848. }
  849. run "status_updates_true_is_sent_when_set" {
  850. command = plan
  851. variables {
  852. connector_telemetry = { status_updates_enabled = true }
  853. }
  854. assert {
  855. condition = twingate_connector.this.status_updates_enabled == true
  856. error_message = "An explicit true must reach the Twingate connector."
  857. }
  858. }
  859. run "an_empty_telemetry_object_plans_and_renders_the_golden_user_data" {
  860. command = plan
  861. override_resource {
  862. target = twingate_connector_tokens.this
  863. values = {
  864. access_token = "fixed-access-token"
  865. refresh_token = "fixed-refresh-token"
  866. }
  867. }
  868. variables {
  869. reboot_time = "04:00"
  870. connector_telemetry = {}
  871. }
  872. assert {
  873. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  874. error_message = "{} must render the golden user_data."
  875. }
  876. }
  877. run "explicit_null_fields_plan_and_render_the_golden_user_data" {
  878. command = plan
  879. override_resource {
  880. target = twingate_connector_tokens.this
  881. values = {
  882. access_token = "fixed-access-token"
  883. refresh_token = "fixed-refresh-token"
  884. }
  885. }
  886. variables {
  887. reboot_time = "04:00"
  888. connector_telemetry = {
  889. status_updates_enabled = null
  890. log_level = null
  891. metrics_port = null
  892. }
  893. }
  894. assert {
  895. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  896. error_message = "Explicit nulls must behave as unset."
  897. }
  898. }
  899. # nullable = false: OpenTofu replaces a null with the default {} instead of
  900. # letting the attribute accesses crash, so null means "off".
  901. run "a_null_telemetry_object_means_off_not_a_crash" {
  902. command = plan
  903. override_resource {
  904. target = twingate_connector_tokens.this
  905. values = {
  906. access_token = "fixed-access-token"
  907. refresh_token = "fixed-refresh-token"
  908. }
  909. }
  910. variables {
  911. reboot_time = "04:00"
  912. connector_telemetry = null
  913. }
  914. assert {
  915. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  916. error_message = "A null connector_telemetry must behave as {} and render the golden user_data."
  917. }
  918. }
  919. run "the_telemetry_variable_is_declared_non_nullable" {
  920. command = plan
  921. assert {
  922. condition = strcontains(file("${path.module}/variables.tf"), "nullable = false")
  923. error_message = "connector_telemetry must declare nullable = false."
  924. }
  925. }
  926. run "every_telemetry_setting_renders_exactly_its_lines_in_connector_conf" {
  927. command = plan
  928. override_resource {
  929. target = twingate_connector_tokens.this
  930. values = {
  931. access_token = "fixed-access-token"
  932. refresh_token = "fixed-refresh-token"
  933. }
  934. }
  935. variables {
  936. reboot_time = "04:00"
  937. connector_telemetry = {
  938. log_level = 5
  939. connection_logs = true
  940. metrics_port = 9999
  941. labels = { SITE = "tor1", DEPLOYED_BY = "opentofu" }
  942. }
  943. }
  944. assert {
  945. condition = split("\n", one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/twingate/connector.conf"])) == tolist([
  946. "TWINGATE_URL=https://acme.twingate.com",
  947. "TWINGATE_ACCESS_TOKEN=fixed-access-token",
  948. "TWINGATE_REFRESH_TOKEN=fixed-refresh-token",
  949. "TWINGATE_LOG_LEVEL=5",
  950. "TWINGATE_LOG_ANALYTICS=v3",
  951. "TWINGATE_METRICS_PORT=9999",
  952. "TWINGATE_LABEL_DEPLOYED_BY=opentofu",
  953. "TWINGATE_LABEL_SITE=tor1",
  954. "",
  955. ])
  956. error_message = "connector.conf must hold the three original lines and then exactly the enabled telemetry lines, labels sorted."
  957. }
  958. assert {
  959. condition = one([for f in yamldecode(output.user_data).write_files : f.permissions if f.path == "/etc/twingate/connector.conf"]) == "0600"
  960. error_message = "connector.conf must stay 0600."
  961. }
  962. # Nothing else changed: take the five lines out and the golden hash comes back.
  963. assert {
  964. condition = sha256(replace(output.user_data, " TWINGATE_LOG_LEVEL=5\n TWINGATE_LOG_ANALYTICS=v3\n TWINGATE_METRICS_PORT=9999\n TWINGATE_LABEL_DEPLOYED_BY=opentofu\n TWINGATE_LABEL_SITE=tor1\n", "")) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  965. error_message = "Telemetry may add lines to connector.conf and nothing else to user_data."
  966. }
  967. assert {
  968. condition = length(regexall("fixed-access-token", output.user_data)) == 1 && length(regexall("fixed-refresh-token", output.user_data)) == 1
  969. error_message = "Each token must still appear exactly once, in connector.conf."
  970. }
  971. }
  972. run "only_the_enabled_settings_render" {
  973. command = plan
  974. variables {
  975. connector_telemetry = { metrics_port = 9999 }
  976. }
  977. assert {
  978. condition = strcontains(output.user_data, " TWINGATE_METRICS_PORT=9999\n") && !strcontains(output.user_data, "TWINGATE_LOG_") && !strcontains(output.user_data, "TWINGATE_LABEL_")
  979. error_message = "Only metrics_port was set, so only TWINGATE_METRICS_PORT may be added."
  980. }
  981. }
  982. run "log_level_3_is_rendered_when_set_explicitly" {
  983. command = plan
  984. variables {
  985. connector_telemetry = { log_level = 3 }
  986. }
  987. assert {
  988. condition = strcontains(output.user_data, " TWINGATE_LOG_LEVEL=3\n")
  989. error_message = "An explicit level is written, even Twingate's default."
  990. }
  991. }
  992. run "telemetry_user_data_is_still_valid_yaml_with_the_other_files_intact" {
  993. command = plan
  994. variables {
  995. connector_telemetry = {
  996. log_level = 7
  997. labels = { A = "b" }
  998. }
  999. }
  1000. assert {
  1001. condition = length(yamldecode(output.user_data).write_files) == 8
  1002. error_message = "The extra lines must stay inside connector.conf and not start another file."
  1003. }
  1004. }
  1005. run "log_level_rejects_a_value_twingate_does_not_document" {
  1006. command = plan
  1007. variables {
  1008. connector_telemetry = { log_level = 6 }
  1009. }
  1010. expect_failures = [var.connector_telemetry]
  1011. }
  1012. run "metrics_port_rejects_a_privileged_port" {
  1013. command = plan
  1014. variables {
  1015. connector_telemetry = { metrics_port = 1023 }
  1016. }
  1017. expect_failures = [var.connector_telemetry]
  1018. }
  1019. run "metrics_port_rejects_65536" {
  1020. command = plan
  1021. variables {
  1022. connector_telemetry = { metrics_port = 65536 }
  1023. }
  1024. expect_failures = [var.connector_telemetry]
  1025. }
  1026. run "metrics_port_rejects_a_fraction" {
  1027. command = plan
  1028. variables {
  1029. connector_telemetry = { metrics_port = 9999.5 }
  1030. }
  1031. expect_failures = [var.connector_telemetry]
  1032. }
  1033. run "metrics_port_rejects_the_node_exporter_port" {
  1034. command = plan
  1035. variables {
  1036. connector_telemetry = { metrics_port = 9100 }
  1037. }
  1038. expect_failures = [var.connector_telemetry]
  1039. }
  1040. run "metrics_port_accepts_the_edges" {
  1041. command = plan
  1042. variables {
  1043. connector_telemetry = { metrics_port = 1024 }
  1044. }
  1045. assert {
  1046. condition = strcontains(output.user_data, "TWINGATE_METRICS_PORT=1024\n")
  1047. error_message = "1024 is allowed."
  1048. }
  1049. }
  1050. run "labels_reject_a_newline_in_a_value" {
  1051. command = plan
  1052. variables {
  1053. connector_telemetry = { labels = { SITE = "a\nTWINGATE_URL=https://evil.example" } }
  1054. }
  1055. expect_failures = [var.connector_telemetry]
  1056. }
  1057. run "labels_reject_a_lowercase_key" {
  1058. command = plan
  1059. variables {
  1060. connector_telemetry = { labels = { site = "tor1" } }
  1061. }
  1062. expect_failures = [var.connector_telemetry]
  1063. }
  1064. run "labels_reject_a_key_that_breaks_the_line" {
  1065. command = plan
  1066. variables {
  1067. connector_telemetry = { labels = { "A=B" = "x" } }
  1068. }
  1069. expect_failures = [var.connector_telemetry]
  1070. }
  1071. run "labels_reject_a_space_or_quote_in_a_value" {
  1072. command = plan
  1073. variables {
  1074. connector_telemetry = { labels = { SITE = "a b" } }
  1075. }
  1076. expect_failures = [var.connector_telemetry]
  1077. }
  1078. run "labels_reject_an_empty_value" {
  1079. command = plan
  1080. variables {
  1081. connector_telemetry = { labels = { SITE = "" } }
  1082. }
  1083. expect_failures = [var.connector_telemetry]
  1084. }
  1085. run "labels_reject_eleven_entries" {
  1086. command = plan
  1087. variables {
  1088. connector_telemetry = { labels = { A = "x", B = "x", C = "x", D = "x", E = "x", F = "x", G = "x", H = "x", I = "x", J = "x", K = "x" } }
  1089. }
  1090. expect_failures = [var.connector_telemetry]
  1091. }
  1092. # metadata_lock (issue #87): off by default, and off adds nothing at all. The
  1093. # golden-hash runs above already prove the default user_data is byte-identical;
  1094. # this run names the absent pieces so a failure says what leaked.
  1095. run "metadata_lock_default_adds_nothing_to_user_data" {
  1096. command = plan
  1097. assert {
  1098. condition = !strcontains(output.user_data, "cah-metadata-lock") && !strcontains(output.user_data, "cah_metadata") && !strcontains(output.user_data, "nftables") && !strcontains(output.user_data, "169.254.169.254")
  1099. error_message = "With metadata_lock unset no metadata-lock script, unit, rule or nftables package may appear in user_data."
  1100. }
  1101. }
  1102. run "metadata_lock_false_is_the_golden_user_data" {
  1103. command = plan
  1104. override_resource {
  1105. target = twingate_connector_tokens.this
  1106. values = {
  1107. access_token = "fixed-access-token"
  1108. refresh_token = "fixed-refresh-token"
  1109. }
  1110. }
  1111. variables {
  1112. reboot_time = "04:00"
  1113. metadata_lock = false
  1114. }
  1115. assert {
  1116. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  1117. error_message = "metadata_lock = false must render the same user_data as before the setting existed."
  1118. }
  1119. }
  1120. run "metadata_lock_on_installs_script_unit_and_package" {
  1121. command = plan
  1122. variables {
  1123. metadata_lock = true
  1124. }
  1125. assert {
  1126. condition = contains(yamldecode(output.user_data).packages, "nftables") && contains(yamldecode(output.user_data).packages, "chrony")
  1127. error_message = "nftables must be installed next to the existing packages."
  1128. }
  1129. assert {
  1130. condition = one([for f in yamldecode(output.user_data).write_files : f.permissions if f.path == "/usr/local/sbin/cah-metadata-lock"]) == "0750" && one([for f in yamldecode(output.user_data).write_files : f.owner if f.path == "/usr/local/sbin/cah-metadata-lock"]) == "root:root"
  1131. error_message = "The lock script must be root-owned and 0750."
  1132. }
  1133. assert {
  1134. condition = alltrue([
  1135. for s in ["169.254.169.254", "meta skuid != {", "id -u do-agent", "table inet cah_metadata", "delete table inet cah_metadata", "reject with icmp type admin-prohibited", "set -euo pipefail"] :
  1136. strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-metadata-lock"]), s)
  1137. ])
  1138. error_message = "The lock script must exempt root and do-agent, use its own idempotent table and reject others."
  1139. }
  1140. assert {
  1141. condition = alltrue([
  1142. for s in ["Type=oneshot", "RemainAfterExit=yes", "After=cloud-final.service network-online.target", "ExecStart=/usr/local/sbin/cah-metadata-lock", "WantedBy=multi-user.target"] :
  1143. strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/systemd/system/cah-metadata-lock.service"]), s)
  1144. ])
  1145. error_message = "The unit must be a oneshot ordered after cloud-final."
  1146. }
  1147. }
  1148. run "metadata_lock_on_enables_the_unit_without_blocking" {
  1149. command = plan
  1150. variables {
  1151. metadata_lock = true
  1152. }
  1153. assert {
  1154. condition = contains(yamldecode(output.user_data).runcmd, ["systemctl", "daemon-reload"]) && contains(yamldecode(output.user_data).runcmd, ["systemctl", "enable", "--now", "--no-block", "cah-metadata-lock.service"])
  1155. error_message = "runcmd must daemon-reload and enable the unit with --no-block (cloud-final runs runcmd; blocking would deadlock)."
  1156. }
  1157. }
  1158. run "metadata_lock_on_keeps_the_rest_of_the_cloud_init" {
  1159. command = plan
  1160. variables {
  1161. metadata_lock = true
  1162. }
  1163. assert {
  1164. condition = length(yamldecode(output.user_data).users) == 1 && strcontains(output.user_data, "TWINGATE_URL=https://acme.twingate.com") && strcontains(output.user_data, "99-cah-hardening")
  1165. error_message = "The lock must be additive."
  1166. }
  1167. }
  1168. # host_firewall (issue #90): off by default, and off adds nothing at all. The
  1169. # golden-hash runs above already prove the default user_data is byte-identical;
  1170. # this run names the absent pieces so a failure says what leaked.
  1171. run "host_firewall_default_adds_nothing_to_user_data" {
  1172. command = plan
  1173. assert {
  1174. condition = !strcontains(output.user_data, "cah-host-firewall") && !strcontains(output.user_data, "cah_host") && !strcontains(output.user_data, "nftables") && !strcontains(output.user_data, "policy drop")
  1175. error_message = "With host_firewall unset no firewall script, unit, rule or nftables package may appear in user_data."
  1176. }
  1177. }
  1178. run "host_firewall_false_is_the_golden_user_data" {
  1179. command = plan
  1180. override_resource {
  1181. target = twingate_connector_tokens.this
  1182. values = {
  1183. access_token = "fixed-access-token"
  1184. refresh_token = "fixed-refresh-token"
  1185. }
  1186. }
  1187. variables {
  1188. reboot_time = "04:00"
  1189. host_firewall = false
  1190. }
  1191. assert {
  1192. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  1193. error_message = "host_firewall = false must render the same user_data as before the setting existed."
  1194. }
  1195. }
  1196. run "host_firewall_on_installs_script_unit_and_package" {
  1197. command = plan
  1198. variables {
  1199. host_firewall = true
  1200. }
  1201. assert {
  1202. condition = contains(yamldecode(output.user_data).packages, "nftables") && contains(yamldecode(output.user_data).packages, "chrony")
  1203. error_message = "nftables must be installed next to the existing packages."
  1204. }
  1205. assert {
  1206. condition = length([for p in yamldecode(output.user_data).packages : p if p == "nftables"]) == 1
  1207. error_message = "nftables must appear exactly once."
  1208. }
  1209. assert {
  1210. condition = one([for f in yamldecode(output.user_data).write_files : f.permissions if f.path == "/usr/local/sbin/cah-host-firewall"]) == "0750" && one([for f in yamldecode(output.user_data).write_files : f.owner if f.path == "/usr/local/sbin/cah-host-firewall"]) == "root:root"
  1211. error_message = "The firewall script must be root-owned and 0750."
  1212. }
  1213. assert {
  1214. condition = alltrue([
  1215. for s in ["table inet cah_host", "delete table inet cah_host", "policy drop", "ct state established,related accept", "ct state invalid drop", "udp sport 67 udp dport 68 accept", "tcp dport 22 ip saddr", "nft -c -f -", "nft delete table inet cah_host", "set -euo pipefail", "ssh_sources=\"10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16\"", "metrics_port=\"\""] :
  1216. strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-host-firewall"]), s)
  1217. ])
  1218. error_message = "The script must check, load an idempotent drop-by-default table with the default private ssh sources, and roll back."
  1219. }
  1220. assert {
  1221. condition = !strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-host-firewall"]), "flush ruleset") && !strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-host-firewall"]), "cah_metadata")
  1222. error_message = "The firewall must never flush the ruleset or touch the metadata-lock table."
  1223. }
  1224. assert {
  1225. condition = alltrue([
  1226. for s in ["Type=oneshot", "RemainAfterExit=yes", "After=cloud-final.service network-online.target", "ExecStart=/usr/local/sbin/cah-host-firewall", "WantedBy=multi-user.target"] :
  1227. strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/systemd/system/cah-host-firewall.service"]), s)
  1228. ])
  1229. error_message = "The unit must be a oneshot ordered after cloud-final."
  1230. }
  1231. }
  1232. run "host_firewall_on_enables_the_unit_without_blocking" {
  1233. command = plan
  1234. variables {
  1235. host_firewall = true
  1236. }
  1237. assert {
  1238. condition = contains(yamldecode(output.user_data).runcmd, ["systemctl", "daemon-reload"]) && contains(yamldecode(output.user_data).runcmd, ["systemctl", "enable", "--now", "--no-block", "cah-host-firewall.service"])
  1239. error_message = "runcmd must daemon-reload and enable the unit with --no-block (cloud-final runs runcmd; blocking would deadlock)."
  1240. }
  1241. }
  1242. run "host_firewall_on_keeps_the_rest_of_the_cloud_init" {
  1243. command = plan
  1244. variables {
  1245. host_firewall = true
  1246. }
  1247. assert {
  1248. condition = length(yamldecode(output.user_data).users) == 1 && strcontains(output.user_data, "TWINGATE_URL=https://acme.twingate.com") && strcontains(output.user_data, "99-cah-hardening")
  1249. error_message = "The firewall must be additive."
  1250. }
  1251. }
  1252. run "host_firewall_metrics_port_is_empty_without_telemetry" {
  1253. command = plan
  1254. variables {
  1255. host_firewall = true
  1256. }
  1257. assert {
  1258. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-host-firewall"]), "metrics_port=\"\"")
  1259. error_message = "Without connector_telemetry.metrics_port no metrics rule may be configured."
  1260. }
  1261. }
  1262. run "host_firewall_metrics_port_follows_telemetry" {
  1263. command = plan
  1264. variables {
  1265. host_firewall = true
  1266. connector_telemetry = { metrics_port = 9999 }
  1267. }
  1268. assert {
  1269. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-host-firewall"]), "metrics_port=\"9999\"")
  1270. error_message = "connector_telemetry.metrics_port must reach the firewall script."
  1271. }
  1272. }
  1273. run "host_firewall_custom_sources_are_rendered" {
  1274. command = plan
  1275. variables {
  1276. host_firewall = true
  1277. host_firewall_ssh_sources = ["10.20.0.0/16", "192.0.2.7/32"]
  1278. }
  1279. assert {
  1280. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-host-firewall"]), "ssh_sources=\"10.20.0.0/16, 192.0.2.7/32\"")
  1281. error_message = "host_firewall_ssh_sources must be rendered into the script."
  1282. }
  1283. }
  1284. run "host_firewall_sources_reject_a_non_cidr" {
  1285. command = plan
  1286. variables {
  1287. host_firewall_ssh_sources = ["10.0.0.1"]
  1288. }
  1289. expect_failures = [var.host_firewall_ssh_sources]
  1290. }
  1291. run "host_firewall_sources_reject_injection" {
  1292. command = plan
  1293. variables {
  1294. host_firewall_ssh_sources = ["10.0.0.0/8 } accept; ip saddr { 0.0.0.0/0"]
  1295. }
  1296. expect_failures = [var.host_firewall_ssh_sources]
  1297. }
  1298. run "host_firewall_sources_reject_ipv6" {
  1299. command = plan
  1300. variables {
  1301. host_firewall_ssh_sources = ["fd00::/8"]
  1302. }
  1303. expect_failures = [var.host_firewall_ssh_sources]
  1304. }
  1305. run "host_firewall_sources_reject_an_empty_list" {
  1306. command = plan
  1307. variables {
  1308. host_firewall_ssh_sources = []
  1309. }
  1310. expect_failures = [var.host_firewall_ssh_sources]
  1311. }
  1312. run "host_firewall_sources_reject_a_repeat" {
  1313. command = plan
  1314. variables {
  1315. host_firewall_ssh_sources = ["10.0.0.0/8", "10.0.0.0/8"]
  1316. }
  1317. expect_failures = [var.host_firewall_ssh_sources]
  1318. }
  1319. run "host_firewall_and_metadata_lock_render_nftables_once_and_both_tables" {
  1320. command = plan
  1321. variables {
  1322. host_firewall = true
  1323. metadata_lock = true
  1324. }
  1325. assert {
  1326. condition = length([for p in yamldecode(output.user_data).packages : p if p == "nftables"]) == 1
  1327. error_message = "nftables must be listed once when both features are on."
  1328. }
  1329. assert {
  1330. condition = strcontains(output.user_data, "table inet cah_metadata") && strcontains(output.user_data, "table inet cah_host")
  1331. error_message = "Both tables must be present when both features are on."
  1332. }
  1333. assert {
  1334. condition = contains(yamldecode(output.user_data).runcmd, ["systemctl", "enable", "--now", "--no-block", "cah-host-firewall.service"]) && contains(yamldecode(output.user_data).runcmd, ["systemctl", "enable", "--now", "--no-block", "cah-metadata-lock.service"])
  1335. error_message = "Both units must be enabled."
  1336. }
  1337. }
  1338. run "metadata_lock_alone_still_renders_nftables_once" {
  1339. command = plan
  1340. variables {
  1341. metadata_lock = true
  1342. }
  1343. assert {
  1344. condition = length([for p in yamldecode(output.user_data).packages : p if p == "nftables"]) == 1 && !strcontains(output.user_data, "cah_host")
  1345. error_message = "The metadata lock alone must keep one nftables entry and no firewall."
  1346. }
  1347. }
  1348. # Persistence (issue #90 review): Ubuntu's nftables.service starts with
  1349. # `flush ruleset` and is try-restarted by package upgrades, which would drop our
  1350. # tables silently. Both units must be ordered after it and be part of it so a
  1351. # restart of nftables.service re-runs our script.
  1352. run "host_firewall_unit_follows_nftables_service" {
  1353. command = plan
  1354. variables {
  1355. host_firewall = true
  1356. }
  1357. assert {
  1358. condition = alltrue([
  1359. for s in ["After=nftables.service", "PartOf=nftables.service", "ReloadPropagatedFrom=nftables.service", "ExecReload=/usr/local/sbin/cah-host-firewall", "After=cloud-final.service network-online.target"] :
  1360. strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/systemd/system/cah-host-firewall.service"]), s)
  1361. ])
  1362. error_message = "cah-host-firewall.service must be After= and PartOf= nftables.service."
  1363. }
  1364. }
  1365. run "metadata_lock_unit_follows_nftables_service" {
  1366. command = plan
  1367. variables {
  1368. metadata_lock = true
  1369. }
  1370. assert {
  1371. condition = alltrue([
  1372. for s in ["After=nftables.service", "PartOf=nftables.service", "ReloadPropagatedFrom=nftables.service", "ExecReload=/usr/local/sbin/cah-metadata-lock", "After=cloud-final.service network-online.target"] :
  1373. strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/systemd/system/cah-metadata-lock.service"]), s)
  1374. ])
  1375. error_message = "cah-metadata-lock.service must be After= and PartOf= nftables.service."
  1376. }
  1377. }
  1378. run "both_units_follow_nftables_service_when_both_features_are_on" {
  1379. command = plan
  1380. variables {
  1381. host_firewall = true
  1382. metadata_lock = true
  1383. }
  1384. assert {
  1385. condition = alltrue([
  1386. for p in ["/etc/systemd/system/cah-host-firewall.service", "/etc/systemd/system/cah-metadata-lock.service"] :
  1387. strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == p]), "PartOf=nftables.service") && strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == p]), "After=nftables.service")
  1388. ])
  1389. error_message = "Both units must follow nftables.service."
  1390. }
  1391. }
  1392. # Tightened host_firewall_ssh_sources validation: nft rejects leading zeros, and
  1393. # a /0 would defeat the narrowing to private space.
  1394. run "host_firewall_sources_reject_a_leading_zero_prefix" {
  1395. command = plan
  1396. variables {
  1397. host_firewall_ssh_sources = ["10.0.0.0/08"]
  1398. }
  1399. expect_failures = [var.host_firewall_ssh_sources]
  1400. }
  1401. run "host_firewall_sources_reject_a_leading_zero_octet" {
  1402. command = plan
  1403. variables {
  1404. host_firewall_ssh_sources = ["010.0.0.0/8"]
  1405. }
  1406. expect_failures = [var.host_firewall_ssh_sources]
  1407. }
  1408. run "host_firewall_sources_reject_slash_zero" {
  1409. command = plan
  1410. variables {
  1411. host_firewall_ssh_sources = ["0.0.0.0/0"]
  1412. }
  1413. expect_failures = [var.host_firewall_ssh_sources]
  1414. }
  1415. run "host_firewall_sources_reject_a_prefix_over_32" {
  1416. command = plan
  1417. variables {
  1418. host_firewall_ssh_sources = ["10.0.0.0/33"]
  1419. }
  1420. expect_failures = [var.host_firewall_ssh_sources]
  1421. }
  1422. run "host_firewall_sources_reject_an_octet_over_255" {
  1423. command = plan
  1424. variables {
  1425. host_firewall_ssh_sources = ["10.0.0.256/32"]
  1426. }
  1427. expect_failures = [var.host_firewall_ssh_sources]
  1428. }
  1429. run "host_firewall_sources_accept_host_bits_overlaps_and_the_extremes" {
  1430. command = plan
  1431. variables {
  1432. host_firewall_ssh_sources = ["10.0.0.1/8", "10.1.0.0/16", "192.0.2.7/32", "128.0.0.0/1"]
  1433. }
  1434. }
  1435. # node_exporter (stage 2 step 5, issue #99): opt-in host metrics. Off by default,
  1436. # and off adds nothing at all, so user_data is byte-identical to the live machine.
  1437. # Enabling it changes user_data and therefore replaces the droplet. Nothing here
  1438. # was applied or tested on a real machine.
  1439. run "node_exporter_default_is_the_golden_user_data_a" {
  1440. command = plan
  1441. override_resource {
  1442. target = twingate_connector_tokens.this
  1443. values = {
  1444. access_token = "fixed-access-token"
  1445. refresh_token = "fixed-refresh-token"
  1446. }
  1447. }
  1448. variables {
  1449. reboot_time = "04:00"
  1450. node_exporter = { enabled = false, port = 9100 }
  1451. }
  1452. assert {
  1453. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  1454. error_message = "node_exporter disabled must render the golden user_data of connector a."
  1455. }
  1456. }
  1457. run "node_exporter_default_is_the_golden_user_data_b" {
  1458. command = plan
  1459. override_resource {
  1460. target = twingate_connector_tokens.this
  1461. values = {
  1462. access_token = "fixed-access-token"
  1463. refresh_token = "fixed-refresh-token"
  1464. }
  1465. }
  1466. variables {
  1467. instance = "b"
  1468. reboot_time = "05:00"
  1469. node_exporter = {}
  1470. }
  1471. assert {
  1472. condition = sha256(output.user_data) == "f79a0bbf3ce1b3dbb5ead9b39c94d27ce7aaac71018d1aa4d425b3c3724a85e6"
  1473. error_message = "An empty node_exporter must render the golden user_data of connector b."
  1474. }
  1475. }
  1476. run "node_exporter_port_alone_does_not_enable_it" {
  1477. command = plan
  1478. override_resource {
  1479. target = twingate_connector_tokens.this
  1480. values = {
  1481. access_token = "fixed-access-token"
  1482. refresh_token = "fixed-refresh-token"
  1483. }
  1484. }
  1485. variables {
  1486. reboot_time = "04:00"
  1487. node_exporter = { port = 9200 }
  1488. }
  1489. assert {
  1490. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  1491. error_message = "Setting only the port must not change user_data."
  1492. }
  1493. }
  1494. run "node_exporter_null_object_means_off_not_a_crash" {
  1495. command = plan
  1496. override_resource {
  1497. target = twingate_connector_tokens.this
  1498. values = {
  1499. access_token = "fixed-access-token"
  1500. refresh_token = "fixed-refresh-token"
  1501. }
  1502. }
  1503. variables {
  1504. reboot_time = "04:00"
  1505. node_exporter = null
  1506. }
  1507. assert {
  1508. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  1509. error_message = "A null node_exporter must behave as {} and render the golden user_data."
  1510. }
  1511. }
  1512. run "node_exporter_default_adds_nothing_by_name" {
  1513. command = plan
  1514. assert {
  1515. condition = !strcontains(output.user_data, "node-exporter") && !strcontains(output.user_data, "node_exporter") && !strcontains(output.user_data, "web.listen-address")
  1516. error_message = "With node_exporter off no package, script, unit drop-in or listen address may appear in user_data."
  1517. }
  1518. }
  1519. run "node_exporter_on_installs_the_archive_package_once" {
  1520. command = plan
  1521. variables {
  1522. node_exporter = { enabled = true }
  1523. }
  1524. assert {
  1525. condition = length([for p in yamldecode(output.user_data).packages : p if p == "prometheus-node-exporter"]) == 1
  1526. error_message = "prometheus-node-exporter must be installed from the distro archive, exactly once."
  1527. }
  1528. assert {
  1529. condition = contains(yamldecode(output.user_data).packages, "chrony") && contains(yamldecode(output.user_data).packages, "unattended-upgrades")
  1530. error_message = "The existing packages must stay."
  1531. }
  1532. }
  1533. run "node_exporter_on_binds_to_the_private_address_only" {
  1534. command = plan
  1535. variables {
  1536. node_exporter = { enabled = true }
  1537. }
  1538. assert {
  1539. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-node-exporter-run"]), "exec /usr/bin/prometheus-node-exporter \"--web.listen-address=$addr:$port\"")
  1540. error_message = "The wrapper must exec the exporter with --web.listen-address set to the private address it found."
  1541. }
  1542. assert {
  1543. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/systemd/system/prometheus-node-exporter.service.d/10-cah-private-bind.conf"]), "ExecStart=\nExecStart=/usr/local/sbin/cah-node-exporter-run")
  1544. error_message = "The drop-in must clear the package's ExecStart and run the wrapper, which picks the address at every start."
  1545. }
  1546. assert {
  1547. condition = !strcontains(output.user_data, "EnvironmentFile") && !strcontains(output.user_data, "ExecStartPre") && !strcontains(output.user_data, "cah-node-exporter.env")
  1548. error_message = "No start-order-dependent mechanism (EnvironmentFile, ExecStartPre, a /run file) may be used."
  1549. }
  1550. assert {
  1551. condition = !strcontains(output.user_data, "0.0.0.0:9100") && !strcontains(output.user_data, "--web.listen-address=:") && !strcontains(output.user_data, "--web.listen-address=0.0.0.0")
  1552. error_message = "No wildcard listen address may be rendered."
  1553. }
  1554. assert {
  1555. condition = one([for f in yamldecode(output.user_data).write_files : f.permissions if f.path == "/usr/local/sbin/cah-node-exporter-run"]) == "0755" && one([for f in yamldecode(output.user_data).write_files : f.owner if f.path == "/usr/local/sbin/cah-node-exporter-run"]) == "root:root"
  1556. error_message = "The wrapper must be root-owned and 0755 (the exporter unit runs it as its own user)."
  1557. }
  1558. }
  1559. run "node_exporter_port_is_rendered_into_the_bind_script" {
  1560. command = plan
  1561. variables {
  1562. node_exporter = { enabled = true, port = 9200 }
  1563. }
  1564. assert {
  1565. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-node-exporter-run"]), "port=\"9200\"")
  1566. error_message = "The configured port must reach the bind script."
  1567. }
  1568. }
  1569. run "node_exporter_default_port_is_9100" {
  1570. command = plan
  1571. variables {
  1572. node_exporter = { enabled = true }
  1573. }
  1574. assert {
  1575. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-node-exporter-run"]), "port=\"9100\"")
  1576. error_message = "The default port must be 9100."
  1577. }
  1578. }
  1579. run "node_exporter_on_keeps_the_rest_of_the_cloud_init" {
  1580. command = plan
  1581. variables {
  1582. node_exporter = { enabled = true }
  1583. }
  1584. assert {
  1585. condition = alltrue([for p in ["/etc/twingate/connector.conf", "/etc/ssh/sshd_config.d/99-cah-hardening.conf", "/etc/sysctl.d/99-cah-hardening.conf", "/etc/cron.d/twingate-connector-update"] : contains([for f in yamldecode(output.user_data).write_files : f.path], p)])
  1586. error_message = "Enabling the node exporter must not drop any existing file."
  1587. }
  1588. }
  1589. run "node_exporter_restart_is_in_runcmd_once" {
  1590. command = plan
  1591. variables {
  1592. node_exporter = { enabled = true }
  1593. }
  1594. assert {
  1595. condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "prometheus-node-exporter")]) == 1
  1596. error_message = "runcmd must restart the exporter once, after the drop-in exists."
  1597. }
  1598. }
  1599. run "node_exporter_port_rejects_a_privileged_port" {
  1600. command = plan
  1601. variables {
  1602. node_exporter = { enabled = true, port = 1023 }
  1603. }
  1604. expect_failures = [var.node_exporter]
  1605. }
  1606. run "node_exporter_port_rejects_65536" {
  1607. command = plan
  1608. variables {
  1609. node_exporter = { enabled = true, port = 65536 }
  1610. }
  1611. expect_failures = [var.node_exporter]
  1612. }
  1613. run "node_exporter_port_rejects_a_fraction" {
  1614. command = plan
  1615. variables {
  1616. node_exporter = { enabled = true, port = 9100.5 }
  1617. }
  1618. expect_failures = [var.node_exporter]
  1619. }
  1620. run "node_exporter_port_is_checked_even_while_disabled" {
  1621. command = plan
  1622. variables {
  1623. node_exporter = { enabled = false, port = 22 }
  1624. }
  1625. expect_failures = [var.node_exporter]
  1626. }
  1627. run "node_exporter_port_must_differ_from_the_connector_metrics_port" {
  1628. command = plan
  1629. variables {
  1630. node_exporter = { enabled = true, port = 9999 }
  1631. connector_telemetry = { metrics_port = 9999 }
  1632. }
  1633. expect_failures = [output.user_data]
  1634. }
  1635. run "node_exporter_and_connector_metrics_on_different_ports_plan" {
  1636. command = plan
  1637. variables {
  1638. node_exporter = { enabled = true, port = 9100 }
  1639. connector_telemetry = { metrics_port = 9999 }
  1640. }
  1641. assert {
  1642. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/twingate/connector.conf"]), "TWINGATE_METRICS_PORT=9999")
  1643. error_message = "Both metrics sources must render together."
  1644. }
  1645. }
  1646. run "node_exporter_with_host_firewall_tells_the_firewall_its_port" {
  1647. command = plan
  1648. variables {
  1649. node_exporter = { enabled = true, port = 9200 }
  1650. host_firewall = true
  1651. }
  1652. assert {
  1653. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-host-firewall"]), "node_exporter_port=\"9200\"")
  1654. error_message = "The host firewall must be told the node exporter port."
  1655. }
  1656. }
  1657. # Existing host_firewall users must not see user_data change when the node
  1658. # exporter is off. These hashes were taken from origin/main (before the node
  1659. # exporter existed) with host_firewall = true.
  1660. run "host_firewall_without_node_exporter_renders_nothing_about_it" {
  1661. command = plan
  1662. variables {
  1663. host_firewall = true
  1664. }
  1665. assert {
  1666. condition = !strcontains(output.user_data, "node_exporter") && !strcontains(output.user_data, "node-exporter")
  1667. error_message = "With the node exporter off the host firewall script must not mention it."
  1668. }
  1669. }
  1670. run "host_firewall_true_exporter_off_is_the_main_user_data_a" {
  1671. command = plan
  1672. override_resource {
  1673. target = twingate_connector_tokens.this
  1674. values = {
  1675. access_token = "fixed-access-token"
  1676. refresh_token = "fixed-refresh-token"
  1677. }
  1678. }
  1679. variables {
  1680. reboot_time = "04:00"
  1681. host_firewall = true
  1682. }
  1683. assert {
  1684. condition = sha256(output.user_data) == "b7e6da81f87baac371d4cf9a5d355a694ba9ea6bc426e91011ee3a4149971e08"
  1685. error_message = "host_firewall = true with the node exporter off must render the golden user_data (re-taken for #288)."
  1686. }
  1687. }
  1688. run "host_firewall_true_exporter_off_with_metrics_port_is_the_main_user_data_b" {
  1689. command = plan
  1690. override_resource {
  1691. target = twingate_connector_tokens.this
  1692. values = {
  1693. access_token = "fixed-access-token"
  1694. refresh_token = "fixed-refresh-token"
  1695. }
  1696. }
  1697. variables {
  1698. instance = "b"
  1699. reboot_time = "05:00"
  1700. host_firewall = true
  1701. connector_telemetry = { metrics_port = 9999 }
  1702. node_exporter = { enabled = false }
  1703. }
  1704. assert {
  1705. condition = sha256(output.user_data) == "fcab80d6cd5a802a4a83738f9e87ab260fcc04d1f2ebb97b888f2c6bcc36b18b"
  1706. error_message = "host_firewall = true with a metrics port and the node exporter off must render the golden user_data (re-taken for #288)."
  1707. }
  1708. }
  1709. # log_shipping (stage 2 step 7): opt-in Wazuh agent and auditd, enrolled at first
  1710. # boot with a secret pulled from Doppler. Off by default, and off adds nothing at
  1711. # all, so user_data is byte-identical to the live machine. Enabling it changes
  1712. # user_data and therefore replaces the droplet. Nothing here was applied or tested
  1713. # on a real machine. Fixture values are obviously fake and built at run time.
  1714. # The four goldens: connector a and b, host_firewall false and true, with
  1715. # node_exporter and log_shipping off. They are the same hashes as the node
  1716. # exporter goldens above and were taken before log_shipping existed.
  1717. run "log_shipping_default_is_the_golden_user_data_a" {
  1718. command = plan
  1719. override_resource {
  1720. target = twingate_connector_tokens.this
  1721. values = {
  1722. access_token = "fixed-access-token"
  1723. refresh_token = "fixed-refresh-token"
  1724. }
  1725. }
  1726. variables {
  1727. reboot_time = "04:00"
  1728. log_shipping = { enabled = false }
  1729. }
  1730. assert {
  1731. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  1732. error_message = "log_shipping disabled must render the golden user_data of connector a."
  1733. }
  1734. }
  1735. run "log_shipping_default_is_the_golden_user_data_b" {
  1736. command = plan
  1737. override_resource {
  1738. target = twingate_connector_tokens.this
  1739. values = {
  1740. access_token = "fixed-access-token"
  1741. refresh_token = "fixed-refresh-token"
  1742. }
  1743. }
  1744. variables {
  1745. instance = "b"
  1746. reboot_time = "05:00"
  1747. log_shipping = {}
  1748. }
  1749. assert {
  1750. condition = sha256(output.user_data) == "f79a0bbf3ce1b3dbb5ead9b39c94d27ce7aaac71018d1aa4d425b3c3724a85e6"
  1751. error_message = "An empty log_shipping must render the golden user_data of connector b."
  1752. }
  1753. }
  1754. run "log_shipping_default_with_host_firewall_is_the_golden_user_data_a" {
  1755. command = plan
  1756. override_resource {
  1757. target = twingate_connector_tokens.this
  1758. values = {
  1759. access_token = "fixed-access-token"
  1760. refresh_token = "fixed-refresh-token"
  1761. }
  1762. }
  1763. variables {
  1764. reboot_time = "04:00"
  1765. host_firewall = true
  1766. log_shipping = {}
  1767. }
  1768. assert {
  1769. condition = sha256(output.user_data) == "b7e6da81f87baac371d4cf9a5d355a694ba9ea6bc426e91011ee3a4149971e08"
  1770. error_message = "host_firewall = true with log_shipping off must render the golden user_data of connector a."
  1771. }
  1772. }
  1773. run "log_shipping_default_with_host_firewall_is_the_golden_user_data_b" {
  1774. command = plan
  1775. override_resource {
  1776. target = twingate_connector_tokens.this
  1777. values = {
  1778. access_token = "fixed-access-token"
  1779. refresh_token = "fixed-refresh-token"
  1780. }
  1781. }
  1782. variables {
  1783. instance = "b"
  1784. reboot_time = "05:00"
  1785. host_firewall = true
  1786. connector_telemetry = { metrics_port = 9999 }
  1787. log_shipping = {}
  1788. }
  1789. assert {
  1790. condition = sha256(output.user_data) == "fcab80d6cd5a802a4a83738f9e87ab260fcc04d1f2ebb97b888f2c6bcc36b18b"
  1791. error_message = "host_firewall = true with log_shipping off must render the golden user_data of connector b."
  1792. }
  1793. }
  1794. # Settings that are only configuration of a feature that is off change nothing,
  1795. # including a token that is supplied while the feature is off.
  1796. run "log_shipping_off_ignores_its_other_settings_and_a_stray_token" {
  1797. command = plan
  1798. override_resource {
  1799. target = twingate_connector_tokens.this
  1800. values = {
  1801. access_token = "fixed-access-token"
  1802. refresh_token = "fixed-refresh-token"
  1803. }
  1804. }
  1805. variables {
  1806. reboot_time = "04:00"
  1807. log_shipping = {
  1808. enabled = false
  1809. manager_address = "siem.example.com"
  1810. agent_name = "site-tor1-connector-a"
  1811. agent_group = "site-tor1"
  1812. agent_version = "4.9.2-1"
  1813. }
  1814. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  1815. }
  1816. assert {
  1817. condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
  1818. error_message = "With log_shipping off no setting and no token may reach user_data."
  1819. }
  1820. }
  1821. run "log_shipping_default_adds_nothing_by_name" {
  1822. command = plan
  1823. assert {
  1824. condition = !strcontains(output.user_data, "wazuh") && !strcontains(output.user_data, "auditd") && !strcontains(output.user_data, "hostboot") && !strcontains(output.user_data, "doppler")
  1825. error_message = "With log_shipping off no package, script, file or URL for it may appear in user_data."
  1826. }
  1827. }
  1828. run "log_shipping_on_installs_the_agent_pieces" {
  1829. command = plan
  1830. variables {
  1831. log_shipping = {
  1832. enabled = true
  1833. manager_address = "siem.example.com"
  1834. agent_name = "site-tor1-connector-a"
  1835. agent_group = "site-tor1"
  1836. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  1837. }
  1838. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  1839. }
  1840. assert {
  1841. condition = length([for p in yamldecode(output.user_data).packages : p if p == "auditd"]) == 1
  1842. error_message = "auditd must be installed from the distro archive, exactly once."
  1843. }
  1844. assert {
  1845. condition = contains(yamldecode(output.user_data).packages, "chrony") && contains(yamldecode(output.user_data).packages, "unattended-upgrades")
  1846. error_message = "The existing packages must stay."
  1847. }
  1848. assert {
  1849. condition = alltrue([for p in ["/etc/cah/hostboot.token", "/usr/local/sbin/cah-log-shipping-firstboot", "/etc/cah/wazuh-localfile.xml", "/etc/audit/rules.d/60-cah.rules", "/etc/twingate/connector.conf", "/etc/ssh/sshd_config.d/99-cah-hardening.conf"] : contains([for f in yamldecode(output.user_data).write_files : f.path], p)])
  1850. error_message = "Enabling log shipping must add its files and keep every existing one."
  1851. }
  1852. assert {
  1853. condition = one([for f in yamldecode(output.user_data).write_files : f.permissions if f.path == "/etc/cah/hostboot.token"]) == "0600" && one([for f in yamldecode(output.user_data).write_files : f.owner if f.path == "/etc/cah/hostboot.token"]) == "root:root"
  1854. error_message = "The token file must be root-owned and 0600."
  1855. }
  1856. assert {
  1857. condition = one([for f in yamldecode(output.user_data).write_files : f.permissions if f.path == "/usr/local/sbin/cah-log-shipping-firstboot"]) == "0750"
  1858. error_message = "The first-boot script must be 0750."
  1859. }
  1860. }
  1861. run "log_shipping_on_renders_the_settings_into_the_first_boot_script" {
  1862. command = plan
  1863. variables {
  1864. log_shipping = {
  1865. enabled = true
  1866. manager_address = "siem.example.com"
  1867. manager_port = 1600
  1868. enroll_port = 1601
  1869. agent_name = "site-tor1-connector-a"
  1870. agent_group = "site-tor1"
  1871. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  1872. }
  1873. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  1874. }
  1875. assert {
  1876. condition = alltrue([for l in ["manager=\"siem.example.com\"", "manager_port=\"1600\"", "enroll_port=\"1601\"", "agent_name=\"site-tor1-connector-a\"", "agent_group=\"site-tor1\""] : strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-log-shipping-firstboot"]), l)])
  1877. error_message = "The manager, ports, agent name and group must reach the first-boot script."
  1878. }
  1879. }
  1880. run "log_shipping_default_ports_are_1514_and_1515" {
  1881. command = plan
  1882. variables {
  1883. log_shipping = {
  1884. enabled = true
  1885. manager_address = "siem.example.com"
  1886. agent_name = "site-tor1-connector-a"
  1887. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  1888. }
  1889. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  1890. }
  1891. assert {
  1892. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-log-shipping-firstboot"]), "manager_port=\"1514\"") && strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-log-shipping-firstboot"]), "enroll_port=\"1515\"")
  1893. error_message = "The defaults must be the Wazuh agent port 1514 and the enrollment port 1515."
  1894. }
  1895. }
  1896. run "log_shipping_apt_key_is_pinned_to_the_given_fingerprint" {
  1897. command = plan
  1898. variables {
  1899. log_shipping = {
  1900. enabled = true
  1901. manager_address = "siem.example.com"
  1902. agent_name = "site-tor1-connector-a"
  1903. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  1904. }
  1905. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  1906. }
  1907. assert {
  1908. condition = strcontains(output.user_data, join("", ["FPR=", "0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])) && strcontains(output.user_data, "https://packages.wazuh.com/key/GPG-KEY-WAZUH")
  1909. error_message = "The Wazuh key must be checked against the fingerprint input."
  1910. }
  1911. assert {
  1912. condition = strcontains(output.user_data, "AGENT_PACKAGE=wazuh-agent\n")
  1913. error_message = "With no agent_version the package is wazuh-agent, unpinned."
  1914. }
  1915. }
  1916. run "log_shipping_agent_version_pins_the_package" {
  1917. command = plan
  1918. variables {
  1919. log_shipping = {
  1920. enabled = true
  1921. manager_address = "siem.example.com"
  1922. agent_name = "site-tor1-connector-a"
  1923. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  1924. agent_version = "4.9.2-1"
  1925. }
  1926. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  1927. }
  1928. assert {
  1929. condition = strcontains(output.user_data, "AGENT_PACKAGE=wazuh-agent=4.9.2-1\n")
  1930. error_message = "agent_version must pin the package."
  1931. }
  1932. }
  1933. run "log_shipping_on_runs_the_install_then_the_first_boot_script_once_each" {
  1934. command = plan
  1935. variables {
  1936. log_shipping = {
  1937. enabled = true
  1938. manager_address = "siem.example.com"
  1939. agent_name = "site-tor1-connector-a"
  1940. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  1941. }
  1942. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  1943. }
  1944. assert {
  1945. condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "packages.wazuh.com")]) == 1
  1946. error_message = "runcmd must install the agent once."
  1947. }
  1948. assert {
  1949. condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "cah-log-shipping-firstboot")]) == 1
  1950. error_message = "runcmd must run the first-boot script once."
  1951. }
  1952. assert {
  1953. condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "twingate-connector-keyring")]) == 1
  1954. error_message = "The connector install must stay, once."
  1955. }
  1956. }
  1957. # The secret rule (design section 2.8): user_data holds the bootstrap token and no
  1958. # other secret. The fixtures below are supplied as extra attributes of the
  1959. # log_shipping object. An object conversion drops attributes the type does not
  1960. # declare, so these prove no such attribute reaches user_data, and the test fails
  1961. # the day one is declared and rendered.
  1962. run "log_shipping_user_data_holds_the_bootstrap_token_and_no_other_secret" {
  1963. command = plan
  1964. variables {
  1965. log_shipping = {
  1966. enabled = true
  1967. manager_address = "siem.example.com"
  1968. agent_name = "site-tor1-connector-a"
  1969. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  1970. enrollment_password = join("-", ["fixture", "wazuh", "enroll", "password", "0001"])
  1971. twingate_service_key = join("-", ["fixture", "twingate", "service", "key", "0001"])
  1972. doppler_secret = join("-", ["fixture", "doppler", "secret", "value", "0001"])
  1973. }
  1974. log_shipping_bootstrap_token = join("-", ["fixture", "bootstrap", "token", "0001"])
  1975. }
  1976. assert {
  1977. condition = length(split("fixture-bootstrap-token-0001", output.user_data)) == 2
  1978. error_message = "The bootstrap token must appear exactly once in user_data."
  1979. }
  1980. assert {
  1981. condition = trimspace(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/cah/hostboot.token"])) == "fixture-bootstrap-token-0001"
  1982. error_message = "The one occurrence must be the root-only token file."
  1983. }
  1984. assert {
  1985. condition = !strcontains(output.user_data, "fixture-wazuh-enroll-password-0001") && !strcontains(output.user_data, "fixture-twingate-service-key-0001") && !strcontains(output.user_data, "fixture-doppler-secret-value-0001")
  1986. error_message = "No enrollment password, Twingate service key or Doppler secret value may reach user_data."
  1987. }
  1988. assert {
  1989. condition = !strcontains(jsonencode(yamldecode(output.user_data).runcmd), "fixture-bootstrap-token-0001")
  1990. error_message = "The token must not be in any command line."
  1991. }
  1992. assert {
  1993. condition = length(regexall("(?i)(WAZUH_REGISTRATION_PASSWORD|TWINGATE_SERVICE_KEY|private_key|--password)", output.user_data)) == 0
  1994. error_message = "No password assignment, deployment variable or key blob may be rendered."
  1995. }
  1996. }
  1997. run "log_shipping_module_declares_no_variable_that_could_carry_a_secret" {
  1998. command = plan
  1999. assert {
  2000. condition = length(regexall("(?m)^variable \"[a-z_]*(password|secret|service_key|private_key|api_key)[a-z_]*\"", file("${path.module}/variables.tf"))) == 0
  2001. error_message = "The bootstrap module must declare no variable for an enrollment password, service key or other secret (only the bootstrap token)."
  2002. }
  2003. assert {
  2004. condition = length(regexall("(?s)variable \"log_shipping_bootstrap_token\" \\{[^}]*sensitive\\s*=\\s*true", file("${path.module}/variables.tf"))) == 1
  2005. error_message = "The bootstrap token variable must be sensitive."
  2006. }
  2007. }
  2008. run "log_shipping_token_is_not_rendered_when_it_is_the_only_thing_set" {
  2009. command = plan
  2010. variables {
  2011. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2012. }
  2013. assert {
  2014. condition = !strcontains(output.user_data, "fixture-bootstrap-token-0001")
  2015. error_message = "A token with the feature off must not reach user_data."
  2016. }
  2017. }
  2018. run "log_shipping_on_requires_the_bootstrap_token" {
  2019. command = plan
  2020. variables {
  2021. log_shipping = {
  2022. enabled = true
  2023. manager_address = "siem.example.com"
  2024. agent_name = "site-tor1-connector-a"
  2025. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  2026. }
  2027. }
  2028. expect_failures = [output.user_data]
  2029. }
  2030. run "log_shipping_on_requires_the_manager_address" {
  2031. command = plan
  2032. variables {
  2033. log_shipping = {
  2034. enabled = true
  2035. agent_name = "site-tor1-connector-a"
  2036. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  2037. }
  2038. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2039. }
  2040. expect_failures = [var.log_shipping]
  2041. }
  2042. run "log_shipping_on_requires_the_agent_name" {
  2043. command = plan
  2044. variables {
  2045. log_shipping = {
  2046. enabled = true
  2047. manager_address = "siem.example.com"
  2048. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  2049. }
  2050. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2051. }
  2052. expect_failures = [var.log_shipping]
  2053. }
  2054. run "log_shipping_on_requires_the_pinned_fingerprint_and_has_no_default" {
  2055. command = plan
  2056. variables {
  2057. log_shipping = {
  2058. enabled = true
  2059. manager_address = "siem.example.com"
  2060. agent_name = "site-tor1-connector-a"
  2061. }
  2062. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2063. }
  2064. expect_failures = [var.log_shipping]
  2065. }
  2066. run "log_shipping_fingerprint_must_be_forty_uppercase_hex_digits" {
  2067. command = plan
  2068. variables {
  2069. log_shipping = {
  2070. enabled = true
  2071. manager_address = "siem.example.com"
  2072. agent_name = "site-tor1-connector-a"
  2073. apt_key_fingerprint = join("", ["0123456789", "abcdef0123", "456789abcd", "ef01234567"])
  2074. }
  2075. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2076. }
  2077. expect_failures = [var.log_shipping]
  2078. }
  2079. run "log_shipping_fingerprint_with_spaces_is_refused" {
  2080. command = plan
  2081. variables {
  2082. log_shipping = {
  2083. enabled = true
  2084. manager_address = "siem.example.com"
  2085. agent_name = "site-tor1-connector-a"
  2086. apt_key_fingerprint = "0123 4567 89AB CDEF 0123 4567 89AB CDEF 0123 4567"
  2087. }
  2088. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2089. }
  2090. expect_failures = [var.log_shipping]
  2091. }
  2092. run "log_shipping_agent_name_must_end_in_connector_and_this_instance" {
  2093. command = plan
  2094. variables {
  2095. instance = "b"
  2096. log_shipping = {
  2097. enabled = true
  2098. manager_address = "siem.example.com"
  2099. agent_name = "site-tor1-connector-a"
  2100. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  2101. }
  2102. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2103. }
  2104. expect_failures = [output.user_data]
  2105. }
  2106. run "log_shipping_agent_name_rejects_shell_metacharacters" {
  2107. command = plan
  2108. variables {
  2109. log_shipping = {
  2110. enabled = true
  2111. manager_address = "siem.example.com"
  2112. agent_name = "x\"; id; echo \"-connector-a"
  2113. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  2114. }
  2115. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2116. }
  2117. expect_failures = [var.log_shipping]
  2118. }
  2119. run "log_shipping_manager_address_rejects_a_url_a_port_and_a_quote" {
  2120. command = plan
  2121. variables {
  2122. log_shipping = {
  2123. enabled = true
  2124. manager_address = "https://siem.example.com:1514"
  2125. agent_name = "site-tor1-connector-a"
  2126. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  2127. }
  2128. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2129. }
  2130. expect_failures = [var.log_shipping]
  2131. }
  2132. run "log_shipping_manager_address_accepts_an_ipv4_literal" {
  2133. command = plan
  2134. variables {
  2135. log_shipping = {
  2136. enabled = true
  2137. manager_address = "192.0.2.10"
  2138. agent_name = "site-tor1-connector-a"
  2139. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  2140. }
  2141. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2142. }
  2143. }
  2144. run "log_shipping_agent_group_rejects_a_space" {
  2145. command = plan
  2146. variables {
  2147. log_shipping = {
  2148. agent_group = "two words"
  2149. }
  2150. }
  2151. expect_failures = [var.log_shipping]
  2152. }
  2153. run "log_shipping_agent_version_rejects_a_range" {
  2154. command = plan
  2155. variables {
  2156. log_shipping = {
  2157. agent_version = "4.x"
  2158. }
  2159. }
  2160. expect_failures = [var.log_shipping]
  2161. }
  2162. run "log_shipping_ports_are_checked_even_while_disabled" {
  2163. command = plan
  2164. variables {
  2165. log_shipping = {
  2166. enabled = false
  2167. manager_port = 70000
  2168. }
  2169. }
  2170. expect_failures = [var.log_shipping]
  2171. }
  2172. run "log_shipping_enroll_port_rejects_a_fraction" {
  2173. command = plan
  2174. variables {
  2175. log_shipping = {
  2176. enroll_port = 1515.5
  2177. }
  2178. }
  2179. expect_failures = [var.log_shipping]
  2180. }
  2181. run "log_shipping_bootstrap_token_rejects_a_quote_and_a_newline" {
  2182. command = plan
  2183. variables {
  2184. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001\"\nruncmd: [x]"
  2185. }
  2186. expect_failures = [var.log_shipping_bootstrap_token]
  2187. }
  2188. run "log_shipping_bootstrap_token_rejects_a_short_value" {
  2189. command = plan
  2190. variables {
  2191. log_shipping_bootstrap_token = "short"
  2192. }
  2193. expect_failures = [var.log_shipping_bootstrap_token]
  2194. }
  2195. run "log_shipping_hostboot_config_is_named_from_the_resource_key" {
  2196. command = plan
  2197. variables {
  2198. resource_key = "tor1-b"
  2199. log_shipping = {
  2200. enabled = true
  2201. manager_address = "siem.example.com"
  2202. agent_name = "site-tor1-connector-a"
  2203. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  2204. }
  2205. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2206. }
  2207. assert {
  2208. condition = strcontains(output.user_data, "cah-tor1-b-hostboot")
  2209. error_message = "The hostboot config name must come from resource_key (cah-<resource_key>-hostboot)."
  2210. }
  2211. }
  2212. run "log_shipping_on_is_valid_cloud_config_yaml_with_every_feature_on" {
  2213. command = plan
  2214. variables {
  2215. host_firewall = true
  2216. metadata_lock = true
  2217. node_exporter = { enabled = true }
  2218. connector_telemetry = { metrics_port = 9999 }
  2219. log_shipping = {
  2220. enabled = true
  2221. manager_address = "siem.example.com"
  2222. agent_name = "site-tor1-connector-a"
  2223. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  2224. }
  2225. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2226. }
  2227. assert {
  2228. condition = length(yamldecode(output.user_data).write_files) > 8 && length(yamldecode(output.user_data).runcmd) > 8
  2229. error_message = "The cloud-init must still decode with every feature on."
  2230. }
  2231. }
  2232. # The Wazuh agent labels every event with its site (agent.labels.site), which is
  2233. # the field the Grafana Wazuh dashboard filters on. The site is the part of the
  2234. # agent name before -connector-<instance>.
  2235. run "log_shipping_on_labels_the_agent_with_its_site" {
  2236. command = plan
  2237. variables {
  2238. instance = "b"
  2239. log_shipping = {
  2240. enabled = true
  2241. manager_address = "siem.example.com"
  2242. agent_name = "site-tor2-connector-b"
  2243. apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
  2244. }
  2245. log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
  2246. }
  2247. assert {
  2248. condition = strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/cah/wazuh-localfile.xml"]), "<label key=\"site\">site-tor2</label>")
  2249. error_message = "The agent must carry the label site=<site part of the agent name>."
  2250. }
  2251. }
  2252. run "log_shipping_bootstrap_token_rejects_a_placeholder" {
  2253. command = plan
  2254. variables {
  2255. log_shipping_bootstrap_token = "changeme-changeme-changeme"
  2256. }
  2257. expect_failures = [var.log_shipping_bootstrap_token]
  2258. }