| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454 |
- mock_provider "twingate" {}
- variables {
- layer = "access"
- region = "tor1"
- instance = "a"
- remote_network_id = "remote-network-test"
- twingate_network = "acme"
- admin_ssh_public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAItest"
- reboot_time = "03:00"
- }
- run "instance_rejects_uppercase" {
- command = plan
- variables {
- instance = "A"
- }
- expect_failures = [var.instance]
- }
- run "instance_rejects_more_than_one_character" {
- command = plan
- variables {
- instance = "ab"
- }
- expect_failures = [var.instance]
- }
- run "instance_rejects_empty" {
- command = plan
- variables {
- instance = ""
- }
- expect_failures = [var.instance]
- }
- run "cloud_init_installs_connector_for_the_network" {
- command = plan
- assert {
- condition = strcontains(output.user_data, "TWINGATE_URL=https://acme.twingate.com")
- error_message = "cloud-init must point the connector at the Twingate network."
- }
- }
- run "network_slug_accepts_bare_subdomain" {
- command = plan
- variables {
- twingate_network = "acme-2"
- }
- assert {
- condition = strcontains(output.user_data, "TWINGATE_URL=https://acme-2.twingate.com")
- error_message = "A bare subdomain must be accepted."
- }
- }
- run "network_slug_rejects_single_quote" {
- command = plan
- variables {
- twingate_network = "acme'; id #"
- }
- expect_failures = [var.twingate_network]
- }
- run "network_slug_rejects_double_quote" {
- command = plan
- variables {
- twingate_network = "acme\"x"
- }
- expect_failures = [var.twingate_network]
- }
- run "network_slug_rejects_full_url" {
- command = plan
- variables {
- twingate_network = "https://acme.twingate.com"
- }
- expect_failures = [var.twingate_network]
- }
- run "install_fails_loudly_when_download_fails" {
- command = plan
- assert {
- condition = strcontains(output.user_data, "pipefail") && strcontains(output.user_data, "--proto '=https' --tlsv1.2")
- error_message = "The install pipeline must use pipefail and hardened curl."
- }
- }
- run "cloud_init_is_valid_yaml_with_a_key_only_admin_user" {
- command = plan
- assert {
- condition = yamldecode(output.user_data).disable_root == true && yamldecode(output.user_data).ssh_pwauth == false
- error_message = "Root login and SSH passwords must be disabled in cloud-init."
- }
- assert {
- condition = length(yamldecode(output.user_data).users) == 1 && yamldecode(output.user_data).users[0].name == "cah-admin"
- error_message = "Exactly one user, cah-admin, must be declared (no default ubuntu user)."
- }
- assert {
- condition = yamldecode(output.user_data).users[0].ssh_authorized_keys == ["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAItest"]
- error_message = "cah-admin must carry the admin public key."
- }
- assert {
- condition = yamldecode(output.user_data).users[0].lock_passwd == true && yamldecode(output.user_data).users[0].sudo == "ALL=(ALL) NOPASSWD:ALL"
- error_message = "cah-admin must have a locked password and key-based passwordless sudo."
- }
- }
- run "sshd_dropin_has_the_hardening_settings" {
- command = plan
- assert {
- condition = alltrue([
- for line in [
- "PermitRootLogin no", "PasswordAuthentication no", "KbdInteractiveAuthentication no",
- "PubkeyAuthentication yes", "X11Forwarding no", "AllowUsers cah-admin",
- "MaxAuthTries 3", "LoginGraceTime 20",
- ] :
- 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)
- ])
- error_message = "The sshd drop-in must contain every hardening line."
- }
- assert {
- condition = !can(regex("(?i)PermitRootLogin\\s+(yes|prohibit-password|without-password)", output.user_data))
- error_message = "No part of the user_data may allow root login."
- }
- }
- run "unattended_upgrades_reboot_at_the_instance_time" {
- command = plan
- variables {
- reboot_time = "04:00"
- }
- assert {
- 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\";")
- error_message = "The reboot time must come from the reboot_time input."
- }
- assert {
- condition = alltrue([
- for line in ["Unattended-Upgrade::Automatic-Reboot \"true\";", "Unattended-Upgrade::Automatic-Reboot-WithUsers \"true\";", "Unattended-Upgrade::Remove-Unused-Dependencies \"true\";"] :
- 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)
- ])
- error_message = "Automatic reboot (with users) and unused-dependency removal must be on."
- }
- assert {
- 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")
- error_message = "The weekly connector update must run half an hour after the reboot time."
- }
- }
- run "update_time_carries_across_the_hour_and_midnight" {
- command = plan
- variables {
- reboot_time = "23:45"
- }
- assert {
- 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")
- error_message = "23:45 plus 30 minutes must be 00:15."
- }
- }
- run "chrony_and_sysctl_are_configured_without_touching_ip_forward" {
- command = plan
- assert {
- condition = contains(yamldecode(output.user_data).packages, "chrony") && contains(yamldecode(output.user_data).packages, "unattended-upgrades")
- error_message = "chrony and unattended-upgrades must be installed."
- }
- assert {
- condition = alltrue([
- 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"] :
- strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/sysctl.d/99-cah-hardening.conf"]), k)
- ])
- error_message = "The sysctl drop-in must carry the safe hardening settings."
- }
- assert {
- condition = !can(regex("(?m)^\\s*net\\.ipv[46]\\.(conf\\.[a-z]+\\.)?(ip_)?forwarding\\s*=", output.user_data))
- error_message = "The sysctl drop-in must not change forwarding; the connector may need it."
- }
- }
- run "tokens_appear_only_in_the_root_only_config" {
- command = apply
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "FAKE-ACCESS-TOKEN-2"
- refresh_token = "FAKE-REFRESH-TOKEN-2"
- }
- }
- assert {
- condition = length(regexall("FAKE-ACCESS-TOKEN-2", output.user_data)) == 1 && length(regexall("FAKE-REFRESH-TOKEN-2", output.user_data)) == 1
- error_message = "Each token must appear exactly once, in the connector config."
- }
- assert {
- condition = alltrue([
- for f in yamldecode(output.user_data).write_files :
- f.path == "/etc/twingate/connector.conf" && f.permissions == "0600" && f.owner == "root:root"
- if strcontains(f.content, "FAKE-ACCESS-TOKEN-2") || strcontains(f.content, "FAKE-REFRESH-TOKEN-2")
- ]) && length([for f in yamldecode(output.user_data).write_files : f if strcontains(f.content, "FAKE-ACCESS-TOKEN-2")]) == 1
- error_message = "The tokens may only be written to /etc/twingate/connector.conf, mode 0600, owned by root."
- }
- assert {
- condition = !anytrue([for c in yamldecode(output.user_data).runcmd : strcontains(jsonencode(c), "FAKE-ACCESS-TOKEN-2") || strcontains(jsonencode(c), "FAKE-REFRESH-TOKEN-2")])
- error_message = "No runcmd step may carry a token."
- }
- }
- run "reboot_time_rejects_a_bad_hour" {
- command = plan
- variables {
- reboot_time = "24:00"
- }
- expect_failures = [var.reboot_time]
- }
- run "reboot_time_rejects_a_bad_minute" {
- command = plan
- variables {
- reboot_time = "03:60"
- }
- expect_failures = [var.reboot_time]
- }
- run "reboot_time_rejects_a_missing_zero_pad" {
- command = plan
- variables {
- reboot_time = "3:00"
- }
- expect_failures = [var.reboot_time]
- }
- # A normal OpenSSH public key carries a trailing comment. It is accepted, and it
- # is dropped before the key reaches cloud-init, so user_data is the same with or
- # without it.
- run "admin_key_accepts_a_comment_and_renders_without_it" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAItest user@host.example"
- }
- assert {
- condition = yamldecode(output.user_data).users[0].ssh_authorized_keys == ["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAItest"]
- error_message = "The rendered key must be '<algorithm> <key>' with the comment removed."
- }
- assert {
- condition = !strcontains(output.user_data, "user@host.example")
- error_message = "The comment must not appear anywhere in user_data."
- }
- }
- # Golden: the rendered cloud-init for the live connectors, with fixed tokens,
- # measured on origin/main before resource_key and the comment-tolerant key
- # existed. A change here rebuilds the droplets (user_data is force-new), so it
- # must be a deliberate step, never a side effect. Re-taken for #235 (auditd -a
- # rules) and #305 (the first-hour order comment): the values only change with the header comment, since log_shipping is off in these renders.
- run "user_data_is_byte_identical_to_the_live_connector_a" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- 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."
- }
- }
- run "user_data_is_byte_identical_to_the_live_connector_b" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- instance = "b"
- reboot_time = "05:00"
- }
- assert {
- condition = sha256(output.user_data) == "f79a0bbf3ce1b3dbb5ead9b39c94d27ce7aaac71018d1aa4d425b3c3724a85e6"
- 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."
- }
- }
- run "user_data_with_a_comment_matches_the_golden_hash" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- admin_ssh_public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAItest laptop-2026"
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "A key with a comment must render the same user_data as the key without one."
- }
- }
- run "admin_key_accepts_rsa_with_padding_and_ecdsa" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAAtest== me"
- }
- assert {
- condition = yamldecode(output.user_data).users[0].ssh_authorized_keys == ["ssh-rsa AAAAB3NzaC1yc2EAAAAtest=="]
- error_message = "An rsa key with padding must be accepted and rendered without its comment."
- }
- }
- run "admin_key_accepts_ecdsa" {
- command = plan
- variables {
- admin_ssh_public_key = "ecdsa-sha2-nistp256 AAAAE2VjZHNhtest"
- }
- assert {
- condition = yamldecode(output.user_data).users[0].ssh_authorized_keys == ["ecdsa-sha2-nistp256 AAAAE2VjZHNhtest"]
- error_message = "An ecdsa key must be accepted."
- }
- }
- run "admin_key_rejects_a_two_word_comment" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AAAA two words"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_a_newline_after_the_comment" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AAAA user@host\n - evil"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_a_trailing_newline" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AAAA\n"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_a_tab" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519\tAAAA"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_a_trailing_space" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AAAA "
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_unsafe_comment_characters" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AAAA user;id"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_a_quote_in_the_comment" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AAAA us\"er"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_a_hash_in_the_comment" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AAAA #x"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_a_dollar_brace_in_the_comment" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AAAA $${x}"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_a_comment_over_64_characters" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AAAA ${join("", [for i in range(65) : "a"])}"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_an_unknown_algorithm" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-dss AAAA"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_an_option_prefix" {
- command = plan
- variables {
- admin_ssh_public_key = "command=\"id\" ssh-ed25519 AAAA"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_an_empty_key" {
- command = plan
- variables {
- admin_ssh_public_key = ""
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_a_key_with_no_body" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_a_non_base64_body" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AA!A"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_a_leading_space" {
- command = plan
- variables {
- admin_ssh_public_key = " ssh-ed25519 AAAA"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- run "admin_key_rejects_yaml_injection" {
- command = plan
- variables {
- admin_ssh_public_key = "ssh-ed25519 AAAA\n - evil"
- }
- expect_failures = [var.admin_ssh_public_key]
- }
- # First-boot install resilience. The apt lock race with Ubuntu's own unattended
- # upgrades made the one-shot install abort before it wrote the connector config.
- run "apt_waits_for_the_dpkg_lock" {
- command = plan
- assert {
- 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\";")
- error_message = "apt must wait up to 300 seconds for the dpkg lock."
- }
- }
- run "installer_does_not_run_the_vendor_script_or_pipe_into_a_shell" {
- command = plan
- assert {
- condition = !anytrue([for c in yamldecode(output.user_data).runcmd : strcontains(jsonencode(c), "setup.sh")])
- error_message = "Twingate's setup.sh must not be run; piped into bash it can lose the rest of the script to apt."
- }
- assert {
- condition = !anytrue([for c in yamldecode(output.user_data).runcmd : can(regex("\\|\\s*(ba|da)?sh\\b", jsonencode(c)))])
- error_message = "Nothing may be piped into a shell."
- }
- }
- run "repository_key_is_pinned_and_service_is_verified" {
- command = plan
- assert {
- condition = alltrue([
- for c in yamldecode(output.user_data).runcmd :
- 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/")
- if strcontains(jsonencode(c), "apt-get install -yq twingate-connector")
- ])
- error_message = "The Twingate repository key must be checked against the pinned fingerprint and the repository must be signed-by that key."
- }
- assert {
- condition = anytrue([
- for f in yamldecode(output.user_data).write_files :
- f.path == "/usr/local/sbin/cah-apt-key-check" && strcontains(f.content, "gpg --show-keys --with-colons")
- ])
- error_message = "The shared key check must list the key file with gpg and compare its fingerprint."
- }
- assert {
- condition = alltrue([
- for c in yamldecode(output.user_data).runcmd :
- strcontains(jsonencode(c), "systemctl enable --now twingate-connector") && strcontains(jsonencode(c), "systemctl is-active --quiet twingate-connector") && strcontains(jsonencode(c), "DEBIAN_FRONTEND=noninteractive")
- if strcontains(jsonencode(c), "apt-get install -yq twingate-connector")
- ])
- error_message = "An attempt only counts as a success when the service is active, and apt must run non-interactively."
- }
- }
- run "installer_is_retried_a_bounded_number_of_times" {
- command = plan
- assert {
- condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "apt-get install -yq twingate-connector")]) == 1
- error_message = "The connector package must be installed from exactly one runcmd step."
- }
- assert {
- condition = alltrue([
- for c in yamldecode(output.user_data).runcmd :
- strcontains(jsonencode(c), "seq 1 10") && strcontains(jsonencode(c), "sleep 30") && strcontains(jsonencode(c), "exit 1") && strcontains(jsonencode(c), "pipefail")
- if strcontains(jsonencode(c), "apt-get install -yq twingate-connector")
- ])
- error_message = "The install step must retry 10 times, 30 seconds apart, under pipefail, and exit 1 when every attempt fails."
- }
- assert {
- condition = !can(regex("(?m)^\\s*(set -[a-z]*x|bash -x)", output.user_data))
- error_message = "Shell tracing would print the tokens into the cloud-init log."
- }
- }
- run "install_outcome_is_logged_without_tokens" {
- command = apply
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "FAKE-ACCESS-TOKEN-3"
- refresh_token = "FAKE-REFRESH-TOKEN-3"
- }
- }
- assert {
- condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "/var/log/cah-connector-install.log")]) == 1
- error_message = "Exactly one step must write /var/log/cah-connector-install.log."
- }
- assert {
- condition = alltrue([
- for c in yamldecode(output.user_data).runcmd :
- 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")
- if strcontains(jsonencode(c), "/var/log/cah-connector-install.log")
- ])
- error_message = "The log step must record the service state, the last 50 journal lines and the cloud-init output tail, at mode 0640."
- }
- assert {
- condition = !anytrue([
- for c in yamldecode(output.user_data).runcmd :
- 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")
- if strcontains(jsonencode(c), "/var/log/cah-connector-install.log")
- ])
- error_message = "The log step must not read or print a token."
- }
- }
- run "machine_clock_is_eastern" {
- command = plan
- assert {
- condition = yamldecode(output.user_data).timezone == "America/New_York"
- error_message = "The reboot window and the weekly update are Eastern time, so the machine timezone must be America/New_York."
- }
- }
- run "eastern_schedule_for_connector_a_and_b" {
- command = plan
- variables {
- reboot_time = "05:00"
- }
- assert {
- 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")
- error_message = "A 05:00 reboot must put the Sunday connector update at 05:30."
- }
- }
- run "the_module_exposes_only_a_name_and_the_boot_data_and_creates_no_cloud_resource" {
- command = plan
- assert {
- condition = jsonencode(sort(flatten(regexall("output \"([A-Za-z0-9_-]+)\"", file("${path.module}/outputs.tf"))))) == jsonencode(["name", "user_data"])
- error_message = "outputs.tf declares an output this test does not inspect. Add it only after proving it carries no connector secret."
- }
- assert {
- condition = length(regexall("(?m)^\\s*output \"user_data\"[^}]*sensitive\\s*=\\s*true", file("${path.module}/outputs.tf"))) == 1
- error_message = "user_data carries the connector tokens and must stay sensitive."
- }
- assert {
- condition = alltrue([for t in flatten(regexall("(?m)^\\s*resource\\s+\"([a-z0-9_]+)\"", file("${path.module}/main.tf"))) : startswith(t, "twingate_")])
- error_message = "The bootstrap module is cloud-neutral: it must create only twingate_* resources."
- }
- }
- run "connector_joins_the_callers_remote_network_and_is_named_by_instance" {
- command = plan
- variables {
- instance = "b"
- }
- assert {
- condition = twingate_connector.this.remote_network_id == "remote-network-test" && twingate_connector.this.name == "access-tor1-connector-b" && output.name == "access-tor1-connector-b"
- error_message = "The connector must join the caller's remote network and be named <layer>-<region>-connector-<instance>."
- }
- }
- # resource_key: the key in NAMES. Empty means region, so the live names stay.
- run "resource_key_defaults_to_the_region_in_the_name" {
- command = plan
- assert {
- condition = output.name == "access-tor1-connector-a"
- error_message = "With no resource_key the connector is named <layer>-<region>-connector-<instance>."
- }
- }
- run "resource_key_replaces_region_in_the_name_only" {
- command = plan
- variables {
- resource_key = "tor1-b"
- }
- assert {
- condition = output.name == "access-tor1-b-connector-a"
- error_message = "A resource_key must replace the region in the name."
- }
- }
- run "resource_key_rejects_uppercase" {
- command = plan
- variables {
- resource_key = "Tor1"
- }
- expect_failures = [var.resource_key]
- }
- run "resource_key_rejects_a_leading_digit" {
- command = plan
- variables {
- resource_key = "1tor"
- }
- expect_failures = [var.resource_key]
- }
- run "resource_key_rejects_one_character" {
- command = plan
- variables {
- resource_key = "a"
- }
- expect_failures = [var.resource_key]
- }
- run "resource_key_rejects_25_characters" {
- command = plan
- variables {
- resource_key = "abcdefghijklmnopqrstuvwxy"
- }
- expect_failures = [var.resource_key]
- }
- run "resource_key_accepts_24_characters" {
- command = plan
- variables {
- resource_key = "abcdefghijklmnopqrstuvwx"
- }
- assert {
- condition = output.name == "access-abcdefghijklmnopqrstuvwx-connector-a"
- error_message = "A 24 character key is the longest allowed."
- }
- }
- run "resource_key_rejects_an_underscore_and_a_space" {
- command = plan
- variables {
- resource_key = "tor_1"
- }
- expect_failures = [var.resource_key]
- }
- # --- timezone ----------------------------------------------------------------
- # The zone is an input now. The default must render exactly what the literal
- # did (the golden hashes above); a different zone may change only that line.
- run "default_timezone_renders_the_golden_user_data" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- timezone = "America/New_York"
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "Passing the default timezone explicitly must render the golden user_data."
- }
- }
- run "another_timezone_changes_only_the_timezone_line" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- timezone = "Europe/Paris"
- }
- assert {
- condition = yamldecode(output.user_data).timezone == "Europe/Paris"
- error_message = "The machine timezone must be the one given."
- }
- assert {
- condition = sha256(replace(output.user_data, "\ntimezone: Europe/Paris\n", "\ntimezone: America/New_York\n")) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "Changing the timezone must change nothing but the timezone line."
- }
- }
- run "utc_is_accepted" {
- command = plan
- variables {
- timezone = "UTC"
- }
- assert {
- condition = yamldecode(output.user_data).timezone == "UTC"
- error_message = "UTC must be accepted."
- }
- }
- run "three_part_timezone_is_accepted" {
- command = plan
- variables {
- timezone = "America/Argentina/Buenos_Aires"
- }
- assert {
- condition = yamldecode(output.user_data).timezone == "America/Argentina/Buenos_Aires"
- error_message = "A three-part IANA name must be accepted."
- }
- }
- run "timezone_rejects_newline_injection" {
- command = plan
- variables {
- timezone = "America/New_York\nruncmd: [\"id\"]"
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_trailing_newline" {
- command = plan
- variables {
- timezone = "America/New_York\n"
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_yaml_special" {
- command = plan
- variables {
- timezone = "America/New_York: {a: b}"
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_comment_marker" {
- command = plan
- variables {
- timezone = "America/New_York # x"
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_space" {
- command = plan
- variables {
- timezone = "America/New York"
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_no_slash" {
- command = plan
- variables {
- timezone = "Eastern"
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_lowercase_utc" {
- command = plan
- variables {
- timezone = "utc"
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_empty" {
- command = plan
- variables {
- timezone = ""
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_four_parts" {
- command = plan
- variables {
- timezone = "A/b/c/d"
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_template_marker" {
- command = plan
- variables {
- timezone = "America/$${x}"
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_a_made_up_zone" {
- command = plan
- variables {
- timezone = "Foo/Bar"
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_a_plausible_but_missing_zone" {
- command = plan
- variables {
- timezone = "America/Definitely_Not_A_Zone"
- }
- expect_failures = [var.timezone]
- }
- run "timezone_rejects_an_out_of_range_etc_zone" {
- command = plan
- variables {
- timezone = "Etc/GMT+99"
- }
- expect_failures = [var.timezone]
- }
- # ---- connector telemetry (default off) ----
- run "explicit_default_telemetry_renders_the_golden_user_data_a" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- connector_telemetry = {}
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "An empty connector_telemetry must render the golden user_data of connector a."
- }
- }
- run "explicit_default_telemetry_renders_the_golden_user_data_b" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- instance = "b"
- reboot_time = "05:00"
- connector_telemetry = {
- status_updates_enabled = true
- connection_logs = false
- labels = {}
- }
- }
- assert {
- condition = sha256(output.user_data) == "f79a0bbf3ce1b3dbb5ead9b39c94d27ce7aaac71018d1aa4d425b3c3724a85e6"
- error_message = "The spelled-out defaults must render the golden user_data of connector b."
- }
- }
- run "status_updates_is_provider_side_and_never_changes_user_data" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- connector_telemetry = { status_updates_enabled = false }
- }
- assert {
- condition = twingate_connector.this.status_updates_enabled == false
- error_message = "status_updates_enabled must reach the Twingate connector."
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "status_updates_enabled must not touch user_data."
- }
- }
- run "status_updates_is_not_sent_unless_set" {
- command = plan
- assert {
- 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")
- 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."
- }
- }
- run "status_updates_true_is_sent_when_set" {
- command = plan
- variables {
- connector_telemetry = { status_updates_enabled = true }
- }
- assert {
- condition = twingate_connector.this.status_updates_enabled == true
- error_message = "An explicit true must reach the Twingate connector."
- }
- }
- run "an_empty_telemetry_object_plans_and_renders_the_golden_user_data" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- connector_telemetry = {}
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "{} must render the golden user_data."
- }
- }
- run "explicit_null_fields_plan_and_render_the_golden_user_data" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- connector_telemetry = {
- status_updates_enabled = null
- log_level = null
- metrics_port = null
- }
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "Explicit nulls must behave as unset."
- }
- }
- # nullable = false: OpenTofu replaces a null with the default {} instead of
- # letting the attribute accesses crash, so null means "off".
- run "a_null_telemetry_object_means_off_not_a_crash" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- connector_telemetry = null
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "A null connector_telemetry must behave as {} and render the golden user_data."
- }
- }
- run "the_telemetry_variable_is_declared_non_nullable" {
- command = plan
- assert {
- condition = strcontains(file("${path.module}/variables.tf"), "nullable = false")
- error_message = "connector_telemetry must declare nullable = false."
- }
- }
- run "every_telemetry_setting_renders_exactly_its_lines_in_connector_conf" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- connector_telemetry = {
- log_level = 5
- connection_logs = true
- metrics_port = 9999
- labels = { SITE = "tor1", DEPLOYED_BY = "opentofu" }
- }
- }
- assert {
- condition = split("\n", one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/twingate/connector.conf"])) == tolist([
- "TWINGATE_URL=https://acme.twingate.com",
- "TWINGATE_ACCESS_TOKEN=fixed-access-token",
- "TWINGATE_REFRESH_TOKEN=fixed-refresh-token",
- "TWINGATE_LOG_LEVEL=5",
- "TWINGATE_LOG_ANALYTICS=v3",
- "TWINGATE_METRICS_PORT=9999",
- "TWINGATE_LABEL_DEPLOYED_BY=opentofu",
- "TWINGATE_LABEL_SITE=tor1",
- "",
- ])
- error_message = "connector.conf must hold the three original lines and then exactly the enabled telemetry lines, labels sorted."
- }
- assert {
- condition = one([for f in yamldecode(output.user_data).write_files : f.permissions if f.path == "/etc/twingate/connector.conf"]) == "0600"
- error_message = "connector.conf must stay 0600."
- }
- # Nothing else changed: take the five lines out and the golden hash comes back.
- assert {
- 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"
- error_message = "Telemetry may add lines to connector.conf and nothing else to user_data."
- }
- assert {
- condition = length(regexall("fixed-access-token", output.user_data)) == 1 && length(regexall("fixed-refresh-token", output.user_data)) == 1
- error_message = "Each token must still appear exactly once, in connector.conf."
- }
- }
- run "only_the_enabled_settings_render" {
- command = plan
- variables {
- connector_telemetry = { metrics_port = 9999 }
- }
- assert {
- condition = strcontains(output.user_data, " TWINGATE_METRICS_PORT=9999\n") && !strcontains(output.user_data, "TWINGATE_LOG_") && !strcontains(output.user_data, "TWINGATE_LABEL_")
- error_message = "Only metrics_port was set, so only TWINGATE_METRICS_PORT may be added."
- }
- }
- run "log_level_3_is_rendered_when_set_explicitly" {
- command = plan
- variables {
- connector_telemetry = { log_level = 3 }
- }
- assert {
- condition = strcontains(output.user_data, " TWINGATE_LOG_LEVEL=3\n")
- error_message = "An explicit level is written, even Twingate's default."
- }
- }
- run "telemetry_user_data_is_still_valid_yaml_with_the_other_files_intact" {
- command = plan
- variables {
- connector_telemetry = {
- log_level = 7
- labels = { A = "b" }
- }
- }
- assert {
- condition = length(yamldecode(output.user_data).write_files) == 8
- error_message = "The extra lines must stay inside connector.conf and not start another file."
- }
- }
- run "log_level_rejects_a_value_twingate_does_not_document" {
- command = plan
- variables {
- connector_telemetry = { log_level = 6 }
- }
- expect_failures = [var.connector_telemetry]
- }
- run "metrics_port_rejects_a_privileged_port" {
- command = plan
- variables {
- connector_telemetry = { metrics_port = 1023 }
- }
- expect_failures = [var.connector_telemetry]
- }
- run "metrics_port_rejects_65536" {
- command = plan
- variables {
- connector_telemetry = { metrics_port = 65536 }
- }
- expect_failures = [var.connector_telemetry]
- }
- run "metrics_port_rejects_a_fraction" {
- command = plan
- variables {
- connector_telemetry = { metrics_port = 9999.5 }
- }
- expect_failures = [var.connector_telemetry]
- }
- run "metrics_port_rejects_the_node_exporter_port" {
- command = plan
- variables {
- connector_telemetry = { metrics_port = 9100 }
- }
- expect_failures = [var.connector_telemetry]
- }
- run "metrics_port_accepts_the_edges" {
- command = plan
- variables {
- connector_telemetry = { metrics_port = 1024 }
- }
- assert {
- condition = strcontains(output.user_data, "TWINGATE_METRICS_PORT=1024\n")
- error_message = "1024 is allowed."
- }
- }
- run "labels_reject_a_newline_in_a_value" {
- command = plan
- variables {
- connector_telemetry = { labels = { SITE = "a\nTWINGATE_URL=https://evil.example" } }
- }
- expect_failures = [var.connector_telemetry]
- }
- run "labels_reject_a_lowercase_key" {
- command = plan
- variables {
- connector_telemetry = { labels = { site = "tor1" } }
- }
- expect_failures = [var.connector_telemetry]
- }
- run "labels_reject_a_key_that_breaks_the_line" {
- command = plan
- variables {
- connector_telemetry = { labels = { "A=B" = "x" } }
- }
- expect_failures = [var.connector_telemetry]
- }
- run "labels_reject_a_space_or_quote_in_a_value" {
- command = plan
- variables {
- connector_telemetry = { labels = { SITE = "a b" } }
- }
- expect_failures = [var.connector_telemetry]
- }
- run "labels_reject_an_empty_value" {
- command = plan
- variables {
- connector_telemetry = { labels = { SITE = "" } }
- }
- expect_failures = [var.connector_telemetry]
- }
- run "labels_reject_eleven_entries" {
- command = plan
- variables {
- 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" } }
- }
- expect_failures = [var.connector_telemetry]
- }
- # metadata_lock (issue #87): off by default, and off adds nothing at all. The
- # golden-hash runs above already prove the default user_data is byte-identical;
- # this run names the absent pieces so a failure says what leaked.
- run "metadata_lock_default_adds_nothing_to_user_data" {
- command = plan
- assert {
- 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")
- error_message = "With metadata_lock unset no metadata-lock script, unit, rule or nftables package may appear in user_data."
- }
- }
- run "metadata_lock_false_is_the_golden_user_data" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- metadata_lock = false
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "metadata_lock = false must render the same user_data as before the setting existed."
- }
- }
- run "metadata_lock_on_installs_script_unit_and_package" {
- command = plan
- variables {
- metadata_lock = true
- }
- assert {
- condition = contains(yamldecode(output.user_data).packages, "nftables") && contains(yamldecode(output.user_data).packages, "chrony")
- error_message = "nftables must be installed next to the existing packages."
- }
- assert {
- 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"
- error_message = "The lock script must be root-owned and 0750."
- }
- assert {
- condition = alltrue([
- 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"] :
- strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-metadata-lock"]), s)
- ])
- error_message = "The lock script must exempt root and do-agent, use its own idempotent table and reject others."
- }
- assert {
- condition = alltrue([
- 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"] :
- strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/systemd/system/cah-metadata-lock.service"]), s)
- ])
- error_message = "The unit must be a oneshot ordered after cloud-final."
- }
- }
- run "metadata_lock_on_enables_the_unit_without_blocking" {
- command = plan
- variables {
- metadata_lock = true
- }
- assert {
- 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"])
- error_message = "runcmd must daemon-reload and enable the unit with --no-block (cloud-final runs runcmd; blocking would deadlock)."
- }
- }
- run "metadata_lock_on_keeps_the_rest_of_the_cloud_init" {
- command = plan
- variables {
- metadata_lock = true
- }
- assert {
- 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")
- error_message = "The lock must be additive."
- }
- }
- # host_firewall (issue #90): off by default, and off adds nothing at all. The
- # golden-hash runs above already prove the default user_data is byte-identical;
- # this run names the absent pieces so a failure says what leaked.
- run "host_firewall_default_adds_nothing_to_user_data" {
- command = plan
- assert {
- 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")
- error_message = "With host_firewall unset no firewall script, unit, rule or nftables package may appear in user_data."
- }
- }
- run "host_firewall_false_is_the_golden_user_data" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- host_firewall = false
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "host_firewall = false must render the same user_data as before the setting existed."
- }
- }
- run "host_firewall_on_installs_script_unit_and_package" {
- command = plan
- variables {
- host_firewall = true
- }
- assert {
- condition = contains(yamldecode(output.user_data).packages, "nftables") && contains(yamldecode(output.user_data).packages, "chrony")
- error_message = "nftables must be installed next to the existing packages."
- }
- assert {
- condition = length([for p in yamldecode(output.user_data).packages : p if p == "nftables"]) == 1
- error_message = "nftables must appear exactly once."
- }
- assert {
- 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"
- error_message = "The firewall script must be root-owned and 0750."
- }
- assert {
- condition = alltrue([
- 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=\"\""] :
- strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/usr/local/sbin/cah-host-firewall"]), s)
- ])
- error_message = "The script must check, load an idempotent drop-by-default table with the default private ssh sources, and roll back."
- }
- assert {
- 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")
- error_message = "The firewall must never flush the ruleset or touch the metadata-lock table."
- }
- assert {
- condition = alltrue([
- 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"] :
- strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/systemd/system/cah-host-firewall.service"]), s)
- ])
- error_message = "The unit must be a oneshot ordered after cloud-final."
- }
- }
- run "host_firewall_on_enables_the_unit_without_blocking" {
- command = plan
- variables {
- host_firewall = true
- }
- assert {
- 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"])
- error_message = "runcmd must daemon-reload and enable the unit with --no-block (cloud-final runs runcmd; blocking would deadlock)."
- }
- }
- run "host_firewall_on_keeps_the_rest_of_the_cloud_init" {
- command = plan
- variables {
- host_firewall = true
- }
- assert {
- 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")
- error_message = "The firewall must be additive."
- }
- }
- run "host_firewall_metrics_port_is_empty_without_telemetry" {
- command = plan
- variables {
- host_firewall = true
- }
- assert {
- 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=\"\"")
- error_message = "Without connector_telemetry.metrics_port no metrics rule may be configured."
- }
- }
- run "host_firewall_metrics_port_follows_telemetry" {
- command = plan
- variables {
- host_firewall = true
- connector_telemetry = { metrics_port = 9999 }
- }
- assert {
- 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\"")
- error_message = "connector_telemetry.metrics_port must reach the firewall script."
- }
- }
- run "host_firewall_custom_sources_are_rendered" {
- command = plan
- variables {
- host_firewall = true
- host_firewall_ssh_sources = ["10.20.0.0/16", "192.0.2.7/32"]
- }
- assert {
- 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\"")
- error_message = "host_firewall_ssh_sources must be rendered into the script."
- }
- }
- run "host_firewall_sources_reject_a_non_cidr" {
- command = plan
- variables {
- host_firewall_ssh_sources = ["10.0.0.1"]
- }
- expect_failures = [var.host_firewall_ssh_sources]
- }
- run "host_firewall_sources_reject_injection" {
- command = plan
- variables {
- host_firewall_ssh_sources = ["10.0.0.0/8 } accept; ip saddr { 0.0.0.0/0"]
- }
- expect_failures = [var.host_firewall_ssh_sources]
- }
- run "host_firewall_sources_reject_ipv6" {
- command = plan
- variables {
- host_firewall_ssh_sources = ["fd00::/8"]
- }
- expect_failures = [var.host_firewall_ssh_sources]
- }
- run "host_firewall_sources_reject_an_empty_list" {
- command = plan
- variables {
- host_firewall_ssh_sources = []
- }
- expect_failures = [var.host_firewall_ssh_sources]
- }
- run "host_firewall_sources_reject_a_repeat" {
- command = plan
- variables {
- host_firewall_ssh_sources = ["10.0.0.0/8", "10.0.0.0/8"]
- }
- expect_failures = [var.host_firewall_ssh_sources]
- }
- run "host_firewall_and_metadata_lock_render_nftables_once_and_both_tables" {
- command = plan
- variables {
- host_firewall = true
- metadata_lock = true
- }
- assert {
- condition = length([for p in yamldecode(output.user_data).packages : p if p == "nftables"]) == 1
- error_message = "nftables must be listed once when both features are on."
- }
- assert {
- condition = strcontains(output.user_data, "table inet cah_metadata") && strcontains(output.user_data, "table inet cah_host")
- error_message = "Both tables must be present when both features are on."
- }
- assert {
- 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"])
- error_message = "Both units must be enabled."
- }
- }
- run "metadata_lock_alone_still_renders_nftables_once" {
- command = plan
- variables {
- metadata_lock = true
- }
- assert {
- condition = length([for p in yamldecode(output.user_data).packages : p if p == "nftables"]) == 1 && !strcontains(output.user_data, "cah_host")
- error_message = "The metadata lock alone must keep one nftables entry and no firewall."
- }
- }
- # Persistence (issue #90 review): Ubuntu's nftables.service starts with
- # `flush ruleset` and is try-restarted by package upgrades, which would drop our
- # tables silently. Both units must be ordered after it and be part of it so a
- # restart of nftables.service re-runs our script.
- run "host_firewall_unit_follows_nftables_service" {
- command = plan
- variables {
- host_firewall = true
- }
- assert {
- condition = alltrue([
- 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"] :
- strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/systemd/system/cah-host-firewall.service"]), s)
- ])
- error_message = "cah-host-firewall.service must be After= and PartOf= nftables.service."
- }
- }
- run "metadata_lock_unit_follows_nftables_service" {
- command = plan
- variables {
- metadata_lock = true
- }
- assert {
- condition = alltrue([
- 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"] :
- strcontains(one([for f in yamldecode(output.user_data).write_files : f.content if f.path == "/etc/systemd/system/cah-metadata-lock.service"]), s)
- ])
- error_message = "cah-metadata-lock.service must be After= and PartOf= nftables.service."
- }
- }
- run "both_units_follow_nftables_service_when_both_features_are_on" {
- command = plan
- variables {
- host_firewall = true
- metadata_lock = true
- }
- assert {
- condition = alltrue([
- for p in ["/etc/systemd/system/cah-host-firewall.service", "/etc/systemd/system/cah-metadata-lock.service"] :
- 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")
- ])
- error_message = "Both units must follow nftables.service."
- }
- }
- # Tightened host_firewall_ssh_sources validation: nft rejects leading zeros, and
- # a /0 would defeat the narrowing to private space.
- run "host_firewall_sources_reject_a_leading_zero_prefix" {
- command = plan
- variables {
- host_firewall_ssh_sources = ["10.0.0.0/08"]
- }
- expect_failures = [var.host_firewall_ssh_sources]
- }
- run "host_firewall_sources_reject_a_leading_zero_octet" {
- command = plan
- variables {
- host_firewall_ssh_sources = ["010.0.0.0/8"]
- }
- expect_failures = [var.host_firewall_ssh_sources]
- }
- run "host_firewall_sources_reject_slash_zero" {
- command = plan
- variables {
- host_firewall_ssh_sources = ["0.0.0.0/0"]
- }
- expect_failures = [var.host_firewall_ssh_sources]
- }
- run "host_firewall_sources_reject_a_prefix_over_32" {
- command = plan
- variables {
- host_firewall_ssh_sources = ["10.0.0.0/33"]
- }
- expect_failures = [var.host_firewall_ssh_sources]
- }
- run "host_firewall_sources_reject_an_octet_over_255" {
- command = plan
- variables {
- host_firewall_ssh_sources = ["10.0.0.256/32"]
- }
- expect_failures = [var.host_firewall_ssh_sources]
- }
- run "host_firewall_sources_accept_host_bits_overlaps_and_the_extremes" {
- command = plan
- variables {
- host_firewall_ssh_sources = ["10.0.0.1/8", "10.1.0.0/16", "192.0.2.7/32", "128.0.0.0/1"]
- }
- }
- # node_exporter (stage 2 step 5, issue #99): opt-in host metrics. Off by default,
- # and off adds nothing at all, so user_data is byte-identical to the live machine.
- # Enabling it changes user_data and therefore replaces the droplet. Nothing here
- # was applied or tested on a real machine.
- run "node_exporter_default_is_the_golden_user_data_a" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- node_exporter = { enabled = false, port = 9100 }
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "node_exporter disabled must render the golden user_data of connector a."
- }
- }
- run "node_exporter_default_is_the_golden_user_data_b" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- instance = "b"
- reboot_time = "05:00"
- node_exporter = {}
- }
- assert {
- condition = sha256(output.user_data) == "f79a0bbf3ce1b3dbb5ead9b39c94d27ce7aaac71018d1aa4d425b3c3724a85e6"
- error_message = "An empty node_exporter must render the golden user_data of connector b."
- }
- }
- run "node_exporter_port_alone_does_not_enable_it" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- node_exporter = { port = 9200 }
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "Setting only the port must not change user_data."
- }
- }
- run "node_exporter_null_object_means_off_not_a_crash" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- node_exporter = null
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "A null node_exporter must behave as {} and render the golden user_data."
- }
- }
- run "node_exporter_default_adds_nothing_by_name" {
- command = plan
- assert {
- condition = !strcontains(output.user_data, "node-exporter") && !strcontains(output.user_data, "node_exporter") && !strcontains(output.user_data, "web.listen-address")
- error_message = "With node_exporter off no package, script, unit drop-in or listen address may appear in user_data."
- }
- }
- run "node_exporter_on_installs_the_archive_package_once" {
- command = plan
- variables {
- node_exporter = { enabled = true }
- }
- assert {
- condition = length([for p in yamldecode(output.user_data).packages : p if p == "prometheus-node-exporter"]) == 1
- error_message = "prometheus-node-exporter must be installed from the distro archive, exactly once."
- }
- assert {
- condition = contains(yamldecode(output.user_data).packages, "chrony") && contains(yamldecode(output.user_data).packages, "unattended-upgrades")
- error_message = "The existing packages must stay."
- }
- }
- run "node_exporter_on_binds_to_the_private_address_only" {
- command = plan
- variables {
- node_exporter = { enabled = true }
- }
- assert {
- 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\"")
- error_message = "The wrapper must exec the exporter with --web.listen-address set to the private address it found."
- }
- assert {
- 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")
- error_message = "The drop-in must clear the package's ExecStart and run the wrapper, which picks the address at every start."
- }
- assert {
- condition = !strcontains(output.user_data, "EnvironmentFile") && !strcontains(output.user_data, "ExecStartPre") && !strcontains(output.user_data, "cah-node-exporter.env")
- error_message = "No start-order-dependent mechanism (EnvironmentFile, ExecStartPre, a /run file) may be used."
- }
- assert {
- 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")
- error_message = "No wildcard listen address may be rendered."
- }
- assert {
- 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"
- error_message = "The wrapper must be root-owned and 0755 (the exporter unit runs it as its own user)."
- }
- }
- run "node_exporter_port_is_rendered_into_the_bind_script" {
- command = plan
- variables {
- node_exporter = { enabled = true, port = 9200 }
- }
- assert {
- 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\"")
- error_message = "The configured port must reach the bind script."
- }
- }
- run "node_exporter_default_port_is_9100" {
- command = plan
- variables {
- node_exporter = { enabled = true }
- }
- assert {
- 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\"")
- error_message = "The default port must be 9100."
- }
- }
- run "node_exporter_on_keeps_the_rest_of_the_cloud_init" {
- command = plan
- variables {
- node_exporter = { enabled = true }
- }
- assert {
- 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)])
- error_message = "Enabling the node exporter must not drop any existing file."
- }
- }
- run "node_exporter_restart_is_in_runcmd_once" {
- command = plan
- variables {
- node_exporter = { enabled = true }
- }
- assert {
- condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "prometheus-node-exporter")]) == 1
- error_message = "runcmd must restart the exporter once, after the drop-in exists."
- }
- }
- run "node_exporter_port_rejects_a_privileged_port" {
- command = plan
- variables {
- node_exporter = { enabled = true, port = 1023 }
- }
- expect_failures = [var.node_exporter]
- }
- run "node_exporter_port_rejects_65536" {
- command = plan
- variables {
- node_exporter = { enabled = true, port = 65536 }
- }
- expect_failures = [var.node_exporter]
- }
- run "node_exporter_port_rejects_a_fraction" {
- command = plan
- variables {
- node_exporter = { enabled = true, port = 9100.5 }
- }
- expect_failures = [var.node_exporter]
- }
- run "node_exporter_port_is_checked_even_while_disabled" {
- command = plan
- variables {
- node_exporter = { enabled = false, port = 22 }
- }
- expect_failures = [var.node_exporter]
- }
- run "node_exporter_port_must_differ_from_the_connector_metrics_port" {
- command = plan
- variables {
- node_exporter = { enabled = true, port = 9999 }
- connector_telemetry = { metrics_port = 9999 }
- }
- expect_failures = [output.user_data]
- }
- run "node_exporter_and_connector_metrics_on_different_ports_plan" {
- command = plan
- variables {
- node_exporter = { enabled = true, port = 9100 }
- connector_telemetry = { metrics_port = 9999 }
- }
- assert {
- 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")
- error_message = "Both metrics sources must render together."
- }
- }
- run "node_exporter_with_host_firewall_tells_the_firewall_its_port" {
- command = plan
- variables {
- node_exporter = { enabled = true, port = 9200 }
- host_firewall = true
- }
- assert {
- 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\"")
- error_message = "The host firewall must be told the node exporter port."
- }
- }
- # Existing host_firewall users must not see user_data change when the node
- # exporter is off. These hashes were taken from origin/main (before the node
- # exporter existed) with host_firewall = true.
- run "host_firewall_without_node_exporter_renders_nothing_about_it" {
- command = plan
- variables {
- host_firewall = true
- }
- assert {
- condition = !strcontains(output.user_data, "node_exporter") && !strcontains(output.user_data, "node-exporter")
- error_message = "With the node exporter off the host firewall script must not mention it."
- }
- }
- run "host_firewall_true_exporter_off_is_the_main_user_data_a" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- host_firewall = true
- }
- assert {
- condition = sha256(output.user_data) == "b7e6da81f87baac371d4cf9a5d355a694ba9ea6bc426e91011ee3a4149971e08"
- error_message = "host_firewall = true with the node exporter off must render the golden user_data (re-taken for #288)."
- }
- }
- run "host_firewall_true_exporter_off_with_metrics_port_is_the_main_user_data_b" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- instance = "b"
- reboot_time = "05:00"
- host_firewall = true
- connector_telemetry = { metrics_port = 9999 }
- node_exporter = { enabled = false }
- }
- assert {
- condition = sha256(output.user_data) == "fcab80d6cd5a802a4a83738f9e87ab260fcc04d1f2ebb97b888f2c6bcc36b18b"
- error_message = "host_firewall = true with a metrics port and the node exporter off must render the golden user_data (re-taken for #288)."
- }
- }
- # log_shipping (stage 2 step 7): opt-in Wazuh agent and auditd, enrolled at first
- # boot with a secret pulled from Doppler. Off by default, and off adds nothing at
- # all, so user_data is byte-identical to the live machine. Enabling it changes
- # user_data and therefore replaces the droplet. Nothing here was applied or tested
- # on a real machine. Fixture values are obviously fake and built at run time.
- # The four goldens: connector a and b, host_firewall false and true, with
- # node_exporter and log_shipping off. They are the same hashes as the node
- # exporter goldens above and were taken before log_shipping existed.
- run "log_shipping_default_is_the_golden_user_data_a" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- log_shipping = { enabled = false }
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "log_shipping disabled must render the golden user_data of connector a."
- }
- }
- run "log_shipping_default_is_the_golden_user_data_b" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- instance = "b"
- reboot_time = "05:00"
- log_shipping = {}
- }
- assert {
- condition = sha256(output.user_data) == "f79a0bbf3ce1b3dbb5ead9b39c94d27ce7aaac71018d1aa4d425b3c3724a85e6"
- error_message = "An empty log_shipping must render the golden user_data of connector b."
- }
- }
- run "log_shipping_default_with_host_firewall_is_the_golden_user_data_a" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- host_firewall = true
- log_shipping = {}
- }
- assert {
- condition = sha256(output.user_data) == "b7e6da81f87baac371d4cf9a5d355a694ba9ea6bc426e91011ee3a4149971e08"
- error_message = "host_firewall = true with log_shipping off must render the golden user_data of connector a."
- }
- }
- run "log_shipping_default_with_host_firewall_is_the_golden_user_data_b" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- instance = "b"
- reboot_time = "05:00"
- host_firewall = true
- connector_telemetry = { metrics_port = 9999 }
- log_shipping = {}
- }
- assert {
- condition = sha256(output.user_data) == "fcab80d6cd5a802a4a83738f9e87ab260fcc04d1f2ebb97b888f2c6bcc36b18b"
- error_message = "host_firewall = true with log_shipping off must render the golden user_data of connector b."
- }
- }
- # Settings that are only configuration of a feature that is off change nothing,
- # including a token that is supplied while the feature is off.
- run "log_shipping_off_ignores_its_other_settings_and_a_stray_token" {
- command = plan
- override_resource {
- target = twingate_connector_tokens.this
- values = {
- access_token = "fixed-access-token"
- refresh_token = "fixed-refresh-token"
- }
- }
- variables {
- reboot_time = "04:00"
- log_shipping = {
- enabled = false
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- agent_group = "site-tor1"
- agent_version = "4.9.2-1"
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- assert {
- condition = sha256(output.user_data) == "a1c4e2c7c17f532d4c0b807454ad0af137bca0d34ab8865694c806089bec58df"
- error_message = "With log_shipping off no setting and no token may reach user_data."
- }
- }
- run "log_shipping_default_adds_nothing_by_name" {
- command = plan
- assert {
- condition = !strcontains(output.user_data, "wazuh") && !strcontains(output.user_data, "auditd") && !strcontains(output.user_data, "hostboot") && !strcontains(output.user_data, "doppler")
- error_message = "With log_shipping off no package, script, file or URL for it may appear in user_data."
- }
- }
- run "log_shipping_on_installs_the_agent_pieces" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- agent_group = "site-tor1"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- assert {
- condition = length([for p in yamldecode(output.user_data).packages : p if p == "auditd"]) == 1
- error_message = "auditd must be installed from the distro archive, exactly once."
- }
- assert {
- condition = contains(yamldecode(output.user_data).packages, "chrony") && contains(yamldecode(output.user_data).packages, "unattended-upgrades")
- error_message = "The existing packages must stay."
- }
- assert {
- 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)])
- error_message = "Enabling log shipping must add its files and keep every existing one."
- }
- assert {
- 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"
- error_message = "The token file must be root-owned and 0600."
- }
- assert {
- condition = one([for f in yamldecode(output.user_data).write_files : f.permissions if f.path == "/usr/local/sbin/cah-log-shipping-firstboot"]) == "0750"
- error_message = "The first-boot script must be 0750."
- }
- }
- run "log_shipping_on_renders_the_settings_into_the_first_boot_script" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- manager_port = 1600
- enroll_port = 1601
- agent_name = "site-tor1-connector-a"
- agent_group = "site-tor1"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- assert {
- 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)])
- error_message = "The manager, ports, agent name and group must reach the first-boot script."
- }
- }
- run "log_shipping_default_ports_are_1514_and_1515" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- assert {
- 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\"")
- error_message = "The defaults must be the Wazuh agent port 1514 and the enrollment port 1515."
- }
- }
- run "log_shipping_apt_key_is_pinned_to_the_given_fingerprint" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- assert {
- condition = strcontains(output.user_data, join("", ["FPR=", "0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])) && strcontains(output.user_data, "https://packages.wazuh.com/key/GPG-KEY-WAZUH")
- error_message = "The Wazuh key must be checked against the fingerprint input."
- }
- assert {
- condition = strcontains(output.user_data, "AGENT_PACKAGE=wazuh-agent\n")
- error_message = "With no agent_version the package is wazuh-agent, unpinned."
- }
- }
- run "log_shipping_agent_version_pins_the_package" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- agent_version = "4.9.2-1"
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- assert {
- condition = strcontains(output.user_data, "AGENT_PACKAGE=wazuh-agent=4.9.2-1\n")
- error_message = "agent_version must pin the package."
- }
- }
- run "log_shipping_on_runs_the_install_then_the_first_boot_script_once_each" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- assert {
- condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "packages.wazuh.com")]) == 1
- error_message = "runcmd must install the agent once."
- }
- assert {
- condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "cah-log-shipping-firstboot")]) == 1
- error_message = "runcmd must run the first-boot script once."
- }
- assert {
- condition = length([for c in yamldecode(output.user_data).runcmd : c if strcontains(jsonencode(c), "twingate-connector-keyring")]) == 1
- error_message = "The connector install must stay, once."
- }
- }
- # The secret rule (design section 2.8): user_data holds the bootstrap token and no
- # other secret. The fixtures below are supplied as extra attributes of the
- # log_shipping object. An object conversion drops attributes the type does not
- # declare, so these prove no such attribute reaches user_data, and the test fails
- # the day one is declared and rendered.
- run "log_shipping_user_data_holds_the_bootstrap_token_and_no_other_secret" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- enrollment_password = join("-", ["fixture", "wazuh", "enroll", "password", "0001"])
- twingate_service_key = join("-", ["fixture", "twingate", "service", "key", "0001"])
- doppler_secret = join("-", ["fixture", "doppler", "secret", "value", "0001"])
- }
- log_shipping_bootstrap_token = join("-", ["fixture", "bootstrap", "token", "0001"])
- }
- assert {
- condition = length(split("fixture-bootstrap-token-0001", output.user_data)) == 2
- error_message = "The bootstrap token must appear exactly once in user_data."
- }
- assert {
- 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"
- error_message = "The one occurrence must be the root-only token file."
- }
- assert {
- 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")
- error_message = "No enrollment password, Twingate service key or Doppler secret value may reach user_data."
- }
- assert {
- condition = !strcontains(jsonencode(yamldecode(output.user_data).runcmd), "fixture-bootstrap-token-0001")
- error_message = "The token must not be in any command line."
- }
- assert {
- condition = length(regexall("(?i)(WAZUH_REGISTRATION_PASSWORD|TWINGATE_SERVICE_KEY|private_key|--password)", output.user_data)) == 0
- error_message = "No password assignment, deployment variable or key blob may be rendered."
- }
- }
- run "log_shipping_module_declares_no_variable_that_could_carry_a_secret" {
- command = plan
- assert {
- condition = length(regexall("(?m)^variable \"[a-z_]*(password|secret|service_key|private_key|api_key)[a-z_]*\"", file("${path.module}/variables.tf"))) == 0
- error_message = "The bootstrap module must declare no variable for an enrollment password, service key or other secret (only the bootstrap token)."
- }
- assert {
- condition = length(regexall("(?s)variable \"log_shipping_bootstrap_token\" \\{[^}]*sensitive\\s*=\\s*true", file("${path.module}/variables.tf"))) == 1
- error_message = "The bootstrap token variable must be sensitive."
- }
- }
- run "log_shipping_token_is_not_rendered_when_it_is_the_only_thing_set" {
- command = plan
- variables {
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- assert {
- condition = !strcontains(output.user_data, "fixture-bootstrap-token-0001")
- error_message = "A token with the feature off must not reach user_data."
- }
- }
- run "log_shipping_on_requires_the_bootstrap_token" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- }
- expect_failures = [output.user_data]
- }
- run "log_shipping_on_requires_the_manager_address" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- expect_failures = [var.log_shipping]
- }
- run "log_shipping_on_requires_the_agent_name" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- expect_failures = [var.log_shipping]
- }
- run "log_shipping_on_requires_the_pinned_fingerprint_and_has_no_default" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- expect_failures = [var.log_shipping]
- }
- run "log_shipping_fingerprint_must_be_forty_uppercase_hex_digits" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "abcdef0123", "456789abcd", "ef01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- expect_failures = [var.log_shipping]
- }
- run "log_shipping_fingerprint_with_spaces_is_refused" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = "0123 4567 89AB CDEF 0123 4567 89AB CDEF 0123 4567"
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- expect_failures = [var.log_shipping]
- }
- run "log_shipping_agent_name_must_end_in_connector_and_this_instance" {
- command = plan
- variables {
- instance = "b"
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- expect_failures = [output.user_data]
- }
- run "log_shipping_agent_name_rejects_shell_metacharacters" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "x\"; id; echo \"-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- expect_failures = [var.log_shipping]
- }
- run "log_shipping_manager_address_rejects_a_url_a_port_and_a_quote" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "https://siem.example.com:1514"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- expect_failures = [var.log_shipping]
- }
- run "log_shipping_manager_address_accepts_an_ipv4_literal" {
- command = plan
- variables {
- log_shipping = {
- enabled = true
- manager_address = "192.0.2.10"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- }
- run "log_shipping_agent_group_rejects_a_space" {
- command = plan
- variables {
- log_shipping = {
- agent_group = "two words"
- }
- }
- expect_failures = [var.log_shipping]
- }
- run "log_shipping_agent_version_rejects_a_range" {
- command = plan
- variables {
- log_shipping = {
- agent_version = "4.x"
- }
- }
- expect_failures = [var.log_shipping]
- }
- run "log_shipping_ports_are_checked_even_while_disabled" {
- command = plan
- variables {
- log_shipping = {
- enabled = false
- manager_port = 70000
- }
- }
- expect_failures = [var.log_shipping]
- }
- run "log_shipping_enroll_port_rejects_a_fraction" {
- command = plan
- variables {
- log_shipping = {
- enroll_port = 1515.5
- }
- }
- expect_failures = [var.log_shipping]
- }
- run "log_shipping_bootstrap_token_rejects_a_quote_and_a_newline" {
- command = plan
- variables {
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001\"\nruncmd: [x]"
- }
- expect_failures = [var.log_shipping_bootstrap_token]
- }
- run "log_shipping_bootstrap_token_rejects_a_short_value" {
- command = plan
- variables {
- log_shipping_bootstrap_token = "short"
- }
- expect_failures = [var.log_shipping_bootstrap_token]
- }
- run "log_shipping_hostboot_config_is_named_from_the_resource_key" {
- command = plan
- variables {
- resource_key = "tor1-b"
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- assert {
- condition = strcontains(output.user_data, "cah-tor1-b-hostboot")
- error_message = "The hostboot config name must come from resource_key (cah-<resource_key>-hostboot)."
- }
- }
- run "log_shipping_on_is_valid_cloud_config_yaml_with_every_feature_on" {
- command = plan
- variables {
- host_firewall = true
- metadata_lock = true
- node_exporter = { enabled = true }
- connector_telemetry = { metrics_port = 9999 }
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor1-connector-a"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- assert {
- condition = length(yamldecode(output.user_data).write_files) > 8 && length(yamldecode(output.user_data).runcmd) > 8
- error_message = "The cloud-init must still decode with every feature on."
- }
- }
- # The Wazuh agent labels every event with its site (agent.labels.site), which is
- # the field the Grafana Wazuh dashboard filters on. The site is the part of the
- # agent name before -connector-<instance>.
- run "log_shipping_on_labels_the_agent_with_its_site" {
- command = plan
- variables {
- instance = "b"
- log_shipping = {
- enabled = true
- manager_address = "siem.example.com"
- agent_name = "site-tor2-connector-b"
- apt_key_fingerprint = join("", ["0123456789", "ABCDEF0123", "456789ABCD", "EF01234567"])
- }
- log_shipping_bootstrap_token = "fixture-bootstrap-token-0001"
- }
- assert {
- 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>")
- error_message = "The agent must carry the label site=<site part of the agent name>."
- }
- }
- run "log_shipping_bootstrap_token_rejects_a_placeholder" {
- command = plan
- variables {
- log_shipping_bootstrap_token = "changeme-changeme-changeme"
- }
- expect_failures = [var.log_shipping_bootstrap_token]
- }
|