From 73281578177fc6c55eaf641b775157db69c5c342 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sat, 7 Nov 2026 15:46:17 +0000 Subject: [PATCH] types: load configuration with koanf Updates #1926 --- flakehashes.json | 4 +- go.mod | 11 +- go.sum | 18 +- hscontrol/types/config_keys.go | 272 ++++++++++ hscontrol/types/config_keys_test.go | 44 ++ hscontrol/types/config_source.go | 695 +++++++++++++++++++++++++ hscontrol/types/config_source_test.go | 711 ++++++++++++++++++++++++++ hscontrol/types/config_test.go | 165 +++--- 8 files changed, 1846 insertions(+), 74 deletions(-) create mode 100644 hscontrol/types/config_keys.go create mode 100644 hscontrol/types/config_keys_test.go create mode 100644 hscontrol/types/config_source.go create mode 100644 hscontrol/types/config_source_test.go diff --git a/flakehashes.json b/flakehashes.json index 6c277a83c..c78ac8cee 100644 --- a/flakehashes.json +++ b/flakehashes.json @@ -1,6 +1,6 @@ { "vendor": { - "goModSum": "sha256-DgUVaRHBdpCG4fO+IfYPqwkZEHkc1vHp0EeBXH3oMi0=", - "sri": "sha256-e9Jsl+9W2Uf1br4bJc7YMOo/NW08vvUwDMlzPTm6ZyU=" + "goModSum": "sha256-1vBREh4bGi6Pwb3XB2YPm9aJVZmHxN5eCVrcmN44RKI=", + "sri": "sha256-PJNsVZs5twIo7PNAbZL9dfPHT6vAfe1iUZjUTR0ULc0=" } } diff --git a/go.mod b/go.mod index 415452569..57ff02381 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,10 @@ require ( github.com/google/go-cmp v0.7.0 github.com/hashicorp/golang-lru/v2 v2.0.8-0.20261012062354-235099c106fc github.com/jagottsicher/termcolor v1.0.2 + github.com/knadh/koanf/parsers/json v1.0.1 + github.com/knadh/koanf/parsers/yaml v1.1.1 + github.com/knadh/koanf/providers/confmap v1.0.1 + github.com/knadh/koanf/v2 v2.4.1 github.com/kradalby/libdns-gigahost v0.0.0-20261014145029-4c03bc63b974 github.com/letsencrypt/pebble/v2 v2.10.1 github.com/libdns/acmedns v0.5.0 @@ -46,6 +50,7 @@ require ( github.com/rs/zerolog v1.35.1 github.com/samber/lo v1.53.0 github.com/sasha-s/go-deadlock v0.3.9 + github.com/spf13/cast v1.10.0 github.com/spf13/cobra v1.10.2 github.com/spf13/viper v1.21.0 github.com/stretchr/testify v1.12.1 @@ -54,6 +59,7 @@ require ( github.com/tailscale/tailsql v0.0.0-20261014183241-aae04fb51be7 github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e go.uber.org/zap v1.28.0 + go.yaml.in/yaml/v3 v3.0.6 go4.org/netipx v0.0.0-20260823151212-3075585bcbeb golang.org/x/crypto v0.58.0 golang.org/x/exp v0.0.0-20261014143641-f0251eb4aa3e @@ -179,6 +185,7 @@ require ( github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/klauspost/compress v1.20.3 // indirect github.com/klauspost/cpuid/v2 v2.4.1 // indirect + github.com/knadh/koanf/maps v0.1.2 // indirect github.com/kradalby/gigahost-go/client v0.0.0-20261014144355-025b38b667ed // indirect github.com/letsencrypt/challtestsrv v1.4.2 // indirect github.com/lib/pq v1.12.3 // indirect @@ -188,7 +195,9 @@ require ( github.com/mattn/go-runewidth v0.0.31 // indirect github.com/mdlayher/netlink v1.11.2 // indirect github.com/mdlayher/socket v0.7.0 // indirect + github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/go-ps v1.0.0 // indirect + github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/mittwald/go-powerdns v0.6.6 // indirect github.com/moby/docker-image-spec v1.3.1 // indirect github.com/moby/moby/api v1.55.0 // indirect @@ -216,7 +225,6 @@ require ( github.com/sirupsen/logrus v1.10.2 // indirect github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect github.com/spf13/afero v1.15.0 // indirect - github.com/spf13/cast v1.10.0 // indirect github.com/spf13/pflag v1.0.10 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/tailscale/certstore v0.1.1-0.20260409135935-3638fb84b77d // indirect @@ -240,7 +248,6 @@ require ( go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap/exp v0.3.0 // indirect go.yaml.in/yaml/v2 v2.4.4 // indirect - go.yaml.in/yaml/v3 v3.0.5 // indirect go4.org/mem v0.0.0-20240501181205-ae6ca9944745 // indirect golang.org/x/image v0.46.0 // indirect golang.org/x/net v0.61.0 // indirect diff --git a/go.sum b/go.sum index 43c04cd4f..00c0cfc61 100644 --- a/go.sum +++ b/go.sum @@ -315,6 +315,16 @@ github.com/klauspost/compress v1.20.3 h1:zWNoFqq/lSuYQRim3dUXlXeA0IqwuZGicdG5ZTf github.com/klauspost/compress v1.20.3/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= github.com/klauspost/cpuid/v2 v2.4.1 h1:I9KxadAkvo+QjtcEE2f2tyfEvmIla/T0m+14PrZNl0U= github.com/klauspost/cpuid/v2 v2.4.1/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU= +github.com/knadh/koanf/maps v0.1.2 h1:RBfmAW5CnZT+PJ1CVc1QSJKf4Xu9kxfQgYVQSu8hpbo= +github.com/knadh/koanf/maps v0.1.2/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI= +github.com/knadh/koanf/parsers/json v1.0.1 h1:w/HTGw5+t5R4dA1OUtHNwOQCBsdNTcVw8Fhje2u76+c= +github.com/knadh/koanf/parsers/json v1.0.1/go.mod h1:zb5WtibRdpxSoSJfXysqGbVxvbszdlroWDHGdDkkEYU= +github.com/knadh/koanf/parsers/yaml v1.1.1 h1:u70vV5IyaM0HvONh8HoqBC97oTgO33KcpZbTLiKVinU= +github.com/knadh/koanf/parsers/yaml v1.1.1/go.mod h1:HHmcHXUrp9cOPcuC+2wrr44GTUB0EC+PyfN3HZD9tFg= +github.com/knadh/koanf/providers/confmap v1.0.1 h1:L15hbvMqlvhwUuCtL9BkL+rqiMAjk6cZc8O9XoDtE3A= +github.com/knadh/koanf/providers/confmap v1.0.1/go.mod h1:txHYHiI2hAtF0/0sCmcuol4IDcuQbKTybiB1nOcUo1A= +github.com/knadh/koanf/v2 v2.4.1 h1:MK2JDkLy4UGzEUkW1CCCKQftd9+M+NX8hAZ2axbf/jo= +github.com/knadh/koanf/v2 v2.4.1/go.mod h1:gRb40VRAbd4iJMYYD5IxZ6hfuopFcXBpc9bbQpZwo28= github.com/kortschak/wol v0.0.0-20200729010619-da482cc4850a h1:+RR6SqnTkDLWyICxS1xpjCi/3dhyV+TgZwA6Ww3KncQ= github.com/kortschak/wol v0.0.0-20200729010619-da482cc4850a/go.mod h1:YTtCCM3ryyfiu4F7t8HQ1mxvp1UBdWM2r6Xa+nGWvDk= github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= @@ -373,8 +383,12 @@ github.com/mholt/acmez/v3 v3.1.7 h1:XTqpuUcRdRoIBuDoI+M4mCnRnPTPN4rqH3Mb723Rg4U= github.com/mholt/acmez/v3 v3.1.7/go.mod h1:Lwv6P/czh/AOq+c99tAzP68/VP92s+8y+hDs4FCgxvo= github.com/miekg/dns v1.1.73 h1:uhT8nJxmTrPJYClxVxTCX+CVn6qnzSiybRk72Z6DgrE= github.com/miekg/dns v1.1.73/go.mod h1:RW2Obtfd5NZHvOFe3zYG0W8koWOQtAzyHaLo8vASBuQ= +github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= +github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc= github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg= +github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= +github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/mittwald/go-powerdns v0.6.6 h1:yQcuszhl98+jJgELjD5ecfxCQWoshhnArexpwrwQxLY= github.com/mittwald/go-powerdns v0.6.6/go.mod h1:adWJ860laOgm14afg+7V0nCa5NQT37oEYe2HRhoS/CA= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= @@ -587,8 +601,8 @@ go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ go.yaml.in/yaml/v2 v2.4.4 h1:tuyd0P+2Ont/d6e2rl3be67goVK4R6deVxCUX5vyPaQ= go.yaml.in/yaml/v2 v2.4.4/go.mod h1:gMZqIpDtDqOfM0uNfy0SkpRhvUryYH0Z6wdMYcacYXQ= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= -go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= -go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= +go.yaml.in/yaml/v3 v3.0.6 h1:zcDfvjXEqTQmpjniUBLc91xYJMB2c4QOFxEF/LCEckM= +go.yaml.in/yaml/v3 v3.0.6/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= go4.org/mem v0.0.0-20240501181205-ae6ca9944745 h1:Tl++JLUCe4sxGu8cTpDzRLd3tN7US4hOxG5YpKCzkek= go4.org/mem v0.0.0-20240501181205-ae6ca9944745/go.mod h1:reUoABIJ9ikfM5sgtSF3Wushcza7+WeD01VB9Lirh3g= go4.org/netipx v0.0.0-20260823151212-3075585bcbeb h1:XBM4hvfwGAttkkiTIFfeigdfcL1xIfdKXqFdgiHGtDs= diff --git a/hscontrol/types/config_keys.go b/hscontrol/types/config_keys.go new file mode 100644 index 000000000..6ee420543 --- /dev/null +++ b/hscontrol/types/config_keys.go @@ -0,0 +1,272 @@ +package types + +import ( + "strings" + + "github.com/coreos/go-oidc/v3/oidc" + "go.yaml.in/yaml/v3" +) + +// keyPosition returns the 1-based position of the key at path in src. +// JSON and hujson.Standardize output are valid YAML, so one parser serves all. +// Keys match case-insensitively, as viper lowercased them. +func keyPosition(src []byte, path []string) (int, int, bool) { + var doc yaml.Node + if yaml.Unmarshal(src, &doc) != nil || len(doc.Content) == 0 { + return 0, 0, false + } + + n := doc.Content[0] + + var key *yaml.Node + + for _, want := range path { + if n.Kind != yaml.MappingNode { + return 0, 0, false + } + + key = nil + + // Last duplicate wins, matching the JSON parser's effective value. + for i := len(n.Content) - 2; i >= 0; i -= 2 { + if strings.EqualFold(n.Content[i].Value, want) { + key, n = n.Content[i], n.Content[i+1] + + break + } + } + + if key == nil { + return 0, 0, false + } + } + + if key == nil { + return 0, 0, false + } + + return key.Line, key.Column, true +} + +// configKeys lists every key the loader reads, so env mapping and the strict +// pass know the schema. Open maps are in openMapKeys. +// +//nolint:goconst // schema table, not shared literals +var configKeys = []string{ + "server_url", + "listen_addr", + "metrics_listen_addr", + "unix_socket", + "unix_socket_permission", + "trusted_proxies", + "disable_check_updates", + "noise.private_key_path", + "prefixes.v4", + "prefixes.v6", + "prefixes.allocation", + "policy.path", + "policy.mode", + "log.level", + "log.format", + "node.expiry", + "node.ephemeral.inactivity_timeout", + "node.routes.ha.probe_interval", + "node.routes.ha.probe_timeout", + "preauth_keys.revoked_retention", + "derp.server.enabled", + "derp.server.region_id", + "derp.server.region_code", + "derp.server.region_name", + "derp.server.verify_clients", + "derp.server.stun.enabled", + "derp.server.stun_listen_addr", + "derp.server.private_key_path", + "derp.server.ipv4", + "derp.server.ipv6", + "derp.server.automatically_add_embedded_derp_region", + "derp.urls", + "derp.paths", + "derp.auto_update_enabled", + "derp.update_frequency", + "database.type", + "database.debug", + "database.gorm.skip_err_record_not_found", + "database.gorm.slow_threshold", + "database.gorm.parameterized_queries", + "database.sqlite.path", + "database.sqlite.write_ahead_log", + "database.sqlite.wal_autocheckpoint", + "database.postgres.host", + "database.postgres.port", + "database.postgres.name", + "database.postgres.user", + "database.postgres.pass", + "database.postgres.ssl", + "database.postgres.max_open_conns", + "database.postgres.max_idle_conns", + "database.postgres.conn_max_idle_time_secs", + "dns.magic_dns", + "dns.base_domain", + "dns.override_local_dns", + "dns.search_domains", + "dns.extra_records", + "dns.extra_records_path", + "dns.nameservers.global", + "dns.nameservers.split", + "dns.certificates.enabled", + "dns.certificates.zone", + "dns.challenge.propagation_timeout", + "dns.challenge.resolvers", + "dns.challenge.provider.name", + "tls.cert_path", + "tls.key_path", + "tls.acme.hostname", + "tls.acme.challenge_type", + "tls.acme.http_listen", + "tls.acme.cache_dir", + "tls.acme.url", + "tls.acme.email", + "oidc.issuer", + "oidc.client_id", + "oidc.client_secret", + "oidc.client_secret_path", + "oidc.scope", + "oidc.extra_params", + "oidc.allowed_domains", + "oidc.allowed_users", + "oidc.allowed_groups", + "oidc.email_verified_required", + "oidc.use_expiry_from_token", + "oidc.only_start_if_oidc_is_available", + "oidc.pkce.enabled", + "oidc.pkce.method", + "logtail.enabled", + "taildrop.enabled", + "auto_update.enabled", + "cli.address", + "cli.api_key", + "cli.timeout", + "cli.insecure", + "tuning.notifier_send_timeout", + "tuning.batch_change_delay", + "tuning.node_mapsession_buffered_chan_size", + "tuning.batcher_workers", + "tuning.register_cache_expiration", + "tuning.register_cache_max_entries", + "tuning.node_store_batch_size", + "tuning.node_store_batch_timeout", +} + +// openMapKeys hold user-chosen subkeys, so nothing below them is checked. +var openMapKeys = []string{ + "dns.nameservers.split", + "oidc.extra_params", + "dns.challenge.provider", + "dns.extra_records", +} + +// configDefaults are the values used when no file or environment sets a key. +var configDefaults = map[string]any{ + "policy.mode": "file", + "log.level": "info", + "log.format": TextLogFormat, + "dns.magic_dns": true, + "dns.base_domain": "", + "dns.override_local_dns": true, + "dns.nameservers.global": []string{}, + "dns.nameservers.split": map[string]string{}, + "dns.search_domains": []string{}, + "dns.certificates.enabled": false, + "dns.challenge.propagation_timeout": "2m", + "derp.server.enabled": false, + "derp.server.verify_clients": true, + "derp.server.stun.enabled": true, + "derp.server.automatically_add_embedded_derp_region": true, + "derp.update_frequency": "3h", + "unix_socket": "/var/run/headscale/headscale.sock", + "unix_socket_permission": "0o770", + "cli.timeout": "5s", + "cli.insecure": false, + "database.postgres.ssl": false, + "database.postgres.max_open_conns": 10, + "database.postgres.max_idle_conns": 10, + "database.postgres.conn_max_idle_time_secs": 3600, + "database.sqlite.write_ahead_log": true, + "database.sqlite.wal_autocheckpoint": 1000, // SQLite default + "oidc.scope": []string{oidc.ScopeOpenID, "profile", "email"}, + "oidc.only_start_if_oidc_is_available": true, + "oidc.use_expiry_from_token": false, + "oidc.pkce.enabled": false, + "oidc.pkce.method": "S256", + "oidc.email_verified_required": true, + "logtail.enabled": false, + "taildrop.enabled": true, + "auto_update.enabled": false, + "node.expiry": "0", + "node.ephemeral.inactivity_timeout": "120s", + "preauth_keys.revoked_retention": "168h", + "node.routes.ha.probe_interval": "10s", + "node.routes.ha.probe_timeout": "5s", + "tuning.notifier_send_timeout": "800ms", + "tuning.batch_change_delay": "800ms", + "tuning.node_mapsession_buffered_chan_size": 30, + "tuning.node_store_batch_size": defaultNodeStoreBatchSize, + "tuning.node_store_batch_timeout": "500ms", + "prefixes.allocation": string(IPAllocationStrategySequential), +} + +// removedKey is a key that once worked and is now rejected. In files Old +// matches the key and everything beneath it; for environment variables only +// the exact name, so the old dns_config.* leaves are listed too. +type removedKey struct{ Old, New, Since, Hint string } + +var removedKeys = []removedKey{ + {Old: "acl_policy_path", New: "policy.path", Since: "0.30"}, + {Old: "dns_config", New: "dns", Since: "0.30", Hint: "move every dns_config.* key to dns (nameservers to dns.nameservers.global, restricted_nameservers to dns.nameservers.split, domains to dns.search_domains)"}, + {Old: "dns_config.magic_dns", New: "dns.magic_dns", Since: "0.30"}, + {Old: "dns_config.base_domain", New: "dns.base_domain", Since: "0.30"}, + {Old: "dns_config.override_local_dns", New: "dns.override_local_dns", Since: "0.30"}, + {Old: "dns_config.nameservers", New: "dns.nameservers.global", Since: "0.30"}, + {Old: "dns_config.restricted_nameservers", New: "dns.nameservers.split", Since: "0.30"}, + {Old: "dns_config.domains", New: "dns.search_domains", Since: "0.30"}, + {Old: "dns_config.extra_records", New: "dns.extra_records", Since: "0.30"}, + {Old: "dns_config.use_username_in_magic_dns", Since: "0.24"}, + {Old: "dns.use_username_in_magic_dns", Since: "0.24"}, + {Old: "oidc.strip_email_domain", Since: "0.26"}, + {Old: "oidc.map_legacy_users", Since: "0.26"}, + {Old: "randomize_client_port", Since: "0.29", Hint: `set "randomizeClientPort": true at the top level of the policy file, or grant the cap per node via a "nodeAttrs" entry`}, + {Old: "oidc.expiry", New: "node.expiry", Since: "0.29"}, + {Old: "ephemeral_node_inactivity_timeout", New: "node.ephemeral.inactivity_timeout", Since: "0.29"}, + {Old: "tls_cert_path", New: "tls.cert_path", Since: "0.30"}, + {Old: "tls_key_path", New: "tls.key_path", Since: "0.30"}, + {Old: "tls_letsencrypt_hostname", New: "tls.acme.hostname", Since: "0.30"}, + {Old: "tls_letsencrypt_challenge_type", New: "tls.acme.challenge_type", Since: "0.30"}, + {Old: "tls_letsencrypt_listen", New: "tls.acme.http_listen", Since: "0.30"}, + {Old: "tls_letsencrypt_cache_dir", New: "tls.acme.cache_dir", Since: "0.30"}, + {Old: "acme_url", New: "tls.acme.url", Since: "0.30"}, + {Old: "acme_email", New: "tls.acme.email", Since: "0.30"}, + {Old: "database.gorm.prepare_stmt", Since: "0.30"}, + {Old: "grpc_listen_addr", Since: "0.30"}, + {Old: "grpc_allow_insecure", Since: "0.30"}, + {Old: "db_type", New: "database.type", Since: "0.30"}, + {Old: "db_path", New: "database.sqlite.path", Since: "0.30"}, + {Old: "db_host", New: "database.postgres.host", Since: "0.30"}, + {Old: "db_port", New: "database.postgres.port", Since: "0.30"}, + {Old: "db_name", New: "database.postgres.name", Since: "0.30"}, + {Old: "db_user", New: "database.postgres.user", Since: "0.30"}, + {Old: "db_pass", New: "database.postgres.pass", Since: "0.30"}, + {Old: "db_ssl", New: "database.postgres.ssl", Since: "0.30"}, + {Old: "private_key_path", New: "noise.private_key_path", Since: "0.23"}, + {Old: "ip_prefixes", New: "prefixes.v4", Since: "0.23", Hint: "use prefixes.v4 and prefixes.v6"}, + {Old: "log_level", New: "log.level", Since: "0.17"}, + {Old: "json_logs", New: "log.format", Since: "0.30", Hint: `use log.format: "json"`}, + {Old: "tls_client_auth_mode", Since: "0.30"}, + {Old: "node_update_check_interval", Since: "0.30"}, + {Old: "changes_check_interval", Since: "0.30"}, + {Old: "noise_private_key_path", New: "noise.private_key_path", Since: "0.30"}, + {Old: "derp_map_path", New: "derp.paths", Since: "0.30"}, + {Old: "metrics_enabled", Since: "0.30"}, + {Old: "oidc.domain_map", Since: "0.13"}, + {Old: "oidc.namespace", Since: "0.30"}, + {Old: "oidc.user", Since: "0.30"}, +} diff --git a/hscontrol/types/config_keys_test.go b/hscontrol/types/config_keys_test.go new file mode 100644 index 000000000..926330050 --- /dev/null +++ b/hscontrol/types/config_keys_test.go @@ -0,0 +1,44 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tailscale/hujson" +) + +func TestKeyPosition(t *testing.T) { + tests := []struct { + name string + src string + path []string + line, col int + ok bool + }{ + {"yaml nested", "a: 1\nserver:\n Foo: 2\n", []string{"server", "foo"}, 3, 3, true}, + {"json", "{\n \"a\": 1,\n \"b\": {\n \"c\": 2\n }\n}\n", []string{"b", "c"}, 4, 5, true}, + {"duplicate parent", `{"dns":{"magic_dns":true},"dns":{"magic_dsn":true}}`, []string{"dns", "magic_dsn"}, 1, 34, true}, + {"duplicate leaf", "{\n\"a\": 1,\n\"a\": 2\n}", []string{"a"}, 3, 1, true}, + {"missing", "a: 1\n", []string{"a", "b"}, 0, 0, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + line, col, ok := keyPosition([]byte(tt.src), tt.path) + assert.Equal(t, tt.ok, ok) + assert.Equal(t, tt.line, line) + assert.Equal(t, tt.col, col) + }) + } + + t.Run("hujson comments", func(t *testing.T) { + src := "{\n // c\n /* d */\n \"a\": 1,\n \"b\": 2, // x\n}\n" + + b, err := hujson.Standardize([]byte(src)) + require.NoError(t, err) + + line, _, ok := keyPosition(b, []string{"b"}) + assert.True(t, ok) + assert.Equal(t, 5, line) + }) +} diff --git a/hscontrol/types/config_source.go b/hscontrol/types/config_source.go new file mode 100644 index 000000000..f1426f532 --- /dev/null +++ b/hscontrol/types/config_source.go @@ -0,0 +1,695 @@ +package types + +import ( + "bytes" + stdjson "encoding/json" + "fmt" + "maps" + "os" + "path/filepath" + "slices" + "strings" + "time" + + "github.com/go-viper/mapstructure/v2" + "github.com/knadh/koanf/parsers/json" + "github.com/knadh/koanf/parsers/yaml" + "github.com/knadh/koanf/providers/confmap" + "github.com/knadh/koanf/v2" + "github.com/rs/zerolog/log" + "github.com/spf13/cast" + "github.com/tailscale/hujson" +) + +const configEnvPrefix = "HEADSCALE_" + +// configFileNames is the per-directory search order. TOML and .env are not +// read, but still found, so an old file is rejected by name, not skipped. +var configFileNames = []string{ + "config.json", "config.toml", "config.yaml", "config.yml", "config.hujson", "config.dotenv", "config.env", +} + +// reservedEnvPrefixes name variables with the HEADSCALE_ prefix that are not +// configuration, so they never produce an unknown-variable warning. +var reservedEnvPrefixes = []string{ + "HEADSCALE_DEBUG_", "HEADSCALE_INTEGRATION_", "HEADSCALE_TEST_", + "HEADSCALE_DNS_CHALLENGE_PROVIDER_", "HEADSCALE_SERVICE_", "HEADSCALE_PORT", +} + +var reservedEnvNames = []string{ + "HEADSCALE_CONFIG", "HEADSCALE_UPDATE_GOLDEN", +} + +// configSource is the layered configuration: defaults, then file, then +// environment. Keys are lowercase; values coerce like viper's. +type configSource struct { + k *koanf.Koanf + file string + fileBytes []byte + warnings []string +} + +func configEnvName(key string) string { + return configEnvPrefix + strings.ToUpper(strings.ReplaceAll(key, ".", "_")) +} + +// configSearchDirs returns the directories searched when no path is given. +// HOME and the XDG variables come from environ, not the process. +func configSearchDirs(environ []string) []string { + get := func(key string) string { + for _, kv := range environ { + if k, v, _ := strings.Cut(kv, "="); k == key { + return v + } + } + + return "" + } + + dirs := []string{"/etc/headscale"} + home := get("HOME") + + xdgHome := get("XDG_CONFIG_HOME") + if xdgHome == "" && home != "" { + xdgHome = filepath.Join(home, ".config") + } + + if xdgHome != "" { + dirs = append(dirs, filepath.Join(xdgHome, "headscale")) + } + + xdgDirs := get("XDG_CONFIG_DIRS") + if xdgDirs == "" { + xdgDirs = "/etc/xdg" + } + + for d := range strings.SplitSeq(xdgDirs, ":") { + if d != "" { + dirs = append(dirs, filepath.Join(d, "headscale")) + } + } + + if home != "" { + dirs = append(dirs, filepath.Join(home, ".headscale")) + } + + return append(dirs, ".") +} + +// findConfigFile returns the first existing config file, or "" if none. +func findConfigFile(path string, isFile bool, environ []string) string { + if isFile { + return path + } + + dirs := []string{path} + if path == "" { + dirs = configSearchDirs(environ) + } + + for _, d := range dirs { + for _, n := range configFileNames { + p := filepath.Join(d, n) + + st, err := os.Stat(p) + if err == nil && !st.IsDir() { + return p + } + } + } + + return "" +} + +func configParser(file string) (koanf.Parser, error) { + switch strings.ToLower(filepath.Ext(file)) { + case ".json", ".hujson": + // loadFile turns HuJSON into JSON. + return json.Parser(), nil + case ".yaml", ".yml": + return yaml.Parser(), nil + } + + return nil, fmt.Errorf("unsupported config file format %q: use .json, .yaml, .yml or .hujson", filepath.Ext(file)) //nolint:err113 +} + +// normaliseConfigMap lowercases keys at every depth and drops nulls, as +// viper does, so lookups are case-insensitive and null means unset. +func normaliseConfigMap(m map[string]any) map[string]any { + out := make(map[string]any, len(m)) + + for k, v := range m { + if v == nil { + continue + } + + out[strings.ToLower(k)] = normaliseConfigValue(v) + } + + return out +} + +func normaliseConfigValue(v any) any { + switch x := v.(type) { + case map[string]any: + return normaliseConfigMap(x) + case map[any]any: + // YAML maps with a non-string key. + m := make(map[string]any, len(x)) + for k, e := range x { + m[fmt.Sprint(k)] = e + } + + return normaliseConfigMap(m) + case []any: + out := make([]any, len(x)) + for i, e := range x { + out[i] = normaliseConfigValue(e) + } + + return out + } + + return v +} + +func isOpenMapPath(path string) bool { + for _, o := range openMapKeys { + if path == o || strings.HasPrefix(path, o+".") { + return true + } + } + + return false +} + +// configPathParts splits path into names as expansion nests them: below an +// open map the rest is one entry name, so "split.a.com" and "split.a.com.au" +// are siblings, not parent and child. +func configPathParts(path string) []string { + parts := strings.Split(path, ".") + for i := 1; i < len(parts); i++ { + if slices.Contains(openMapKeys, strings.Join(parts[:i], ".")) { + return append(parts[:i], strings.Join(parts[i:], ".")) + } + } + + return parts +} + +// expandDottedKeys nests dotted names ("log.level") outside open maps. The +// strict pass has already rejected files that spell one path two ways. +func expandDottedKeys(m map[string]any, prefix string) map[string]any { + out := map[string]any{} + + var dotted []string + + for _, k := range slices.Sorted(maps.Keys(m)) { + if !isOpenMapPath(prefix) && strings.Contains(k, ".") { + dotted = append(dotted, k) + + continue + } + + out[k] = expandConfigValue(m[k], joinConfigPath(prefix, k)) + } + + for _, k := range dotted { + parts := strings.Split(k, ".") + cur, path := out, prefix + + var final string + + for i := 0; ; i++ { + // Below an open map the rest of the name is one literal key. + if i == len(parts)-1 || isOpenMapPath(path) { + final = strings.Join(parts[i:], ".") + + break + } + + next, ok := cur[parts[i]].(map[string]any) + if !ok { + next = map[string]any{} + cur[parts[i]] = next + } + + cur, path = next, joinConfigPath(path, parts[i]) + } + + cur[final] = expandConfigValue(m[k], joinConfigPath(path, final)) + } + + return out +} + +func expandConfigValue(v any, path string) any { + if child, ok := v.(map[string]any); ok { + return expandDottedKeys(child, path) + } + + return v +} + +func joinConfigPath(prefix, name string) string { + if prefix == "" { + return name + } + + return prefix + "." + name +} + +// newConfigSource loads defaults, the config file found via path/isFile and +// environ (os.Environ form), and rejects removed and unknown file keys. +func newConfigSource(path string, isFile bool, environ []string) (*configSource, error) { + s := &configSource{k: koanf.New(".")} + + err := s.k.Load(confmap.Provider(configDefaults, "."), nil) + if err != nil { + return nil, fmt.Errorf("loading defaults: %w", err) + } + + s.file = findConfigFile(path, isFile, environ) + if s.file == "" { + log.Warn().Msg("no config file found, using defaults") + } else { + err = s.loadFile() + if err != nil { + return nil, err + } + } + + err = s.loadEnv(environ) + if err != nil { + return nil, err + } + + return s, nil +} + +func (s *configSource) loadFile() error { + parser, err := configParser(s.file) + if err != nil { + return fmt.Errorf("%w (%s)", err, s.file) + } + + raw, err := os.ReadFile(s.file) + if err != nil { + return fmt.Errorf("fatal error reading config file: %w", err) + } + + if len(bytes.TrimSpace(raw)) == 0 { + return nil + } + + if strings.EqualFold(filepath.Ext(s.file), ".hujson") { + // Comments and trailing commas become spaces, so line numbers, and + // error positions with them, still point into the file. + raw, err = hujson.Standardize(raw) + } + + s.fileBytes = raw + + var m map[string]any + if err == nil { + m, err = parser.Unmarshal(raw) + } + + if err != nil { + return fmt.Errorf("fatal error reading config file %s: %w", s.file, err) + } + + m = normaliseConfigMap(m) + + // Checked before expansion, which would otherwise discard shadowed keys. + v := &configValidator{} + s.checkKeys(v, m) + + if v.HasErrors() { + return v.Err() + } + + m = expandDottedKeys(m, "") + + // No delimiter: open-map keys such as domain names may contain dots. + err = s.k.Load(confmap.Provider(m, ""), nil) + if err != nil { + return fmt.Errorf("loading config file %s: %w", s.file, err) + } + + return nil +} + +// configKeySets derives the lookup sets the strict pass needs. +func configKeySets() (map[string]bool, map[string]bool) { + leaf, branch := map[string]bool{}, map[string]bool{} + + for _, k := range slices.Concat(configKeys, openMapKeys) { + leaf[k] = true + + parts := strings.Split(k, ".") + for i := 1; i < len(parts); i++ { + branch[strings.Join(parts[:i], ".")] = true + } + } + + return leaf, branch +} + +func removedConfigKey(path string) (removedKey, bool) { + for _, r := range removedKeys { + if r.Old == path || strings.HasPrefix(path, r.Old+".") { + return r, true + } + } + + return removedKey{}, false +} + +func (r removedKey) hint() string { + switch { + case r.Hint != "": + return r.Hint + case r.New != "": + return "use " + r.New + } + + return fmt.Sprintf("remove %s; see the CHANGELOG for context", r.Old) +} + +func (r removedKey) reason(what, name string) string { + return fmt.Sprintf("configuration %s %s was removed in %s", what, name, r.Since) +} + +func (r removedKey) envHint() string { + if r.New != "" && r.Hint == "" { + return "use " + configEnvName(r.New) + } + + return r.hint() +} + +// checkKeys adds an error for every removed or unknown key in the file. +func (s *configSource) checkKeys(v *configValidator, m map[string]any) { + leaf, branch := configKeySets() + + var walk func(prefix string, m map[string]any) + + walk = func(prefix string, m map[string]any) { + for _, name := range slices.Sorted(maps.Keys(m)) { + path := name + if prefix != "" { + path = prefix + "." + name + } + + val := m[name] + child, isMap := val.(map[string]any) + + switch r, removed := removedConfigKey(path); { + case removed: + v.Add(&ConfigError{ + Reason: r.reason("key", path) + s.position(path), + Hint: r.hint(), + }) + case isOpenMapPath(path): + // The contents are the user's. + case leaf[path] && !isMap: + case (leaf[path] || branch[path]) && isMap: + // Under a plain setting every child is unknown. + walk(path, child) + case branch[path]: + v.Add(&ConfigError{ + Reason: fmt.Sprintf("configuration key %s must be a mapping%s", path, s.position(path)), + Current: []KV{{path, val}}, + }) + default: + v.Add(&ConfigError{Reason: fmt.Sprintf("unknown configuration key %s%s", path, s.position(path))}) + } + } + } + + walk("", m) + s.checkOverlaps(v, m) +} + +// position renders " (file:line:col)" when the key can be located. +func (s *configSource) position(path string) string { + line, col, ok := keyPosition(s.fileBytes, strings.Split(path, ".")) + if !ok { + return " (" + s.file + ")" + } + + return fmt.Sprintf(" (%s:%d:%d)", s.file, line, col) +} + +func (s *configSource) loadEnv(environ []string) error { + env := map[string]string{} + + for _, kv := range environ { + name, val, _ := strings.Cut(kv, "=") + // Empty means unset, as with viper. + if val != "" && strings.HasPrefix(name, configEnvPrefix) { + env[name] = val + } + } + + layer := map[string]any{} + names := map[string]bool{} + + openLayer := map[string]string{} + + for _, key := range slices.Concat(configKeys, openMapKeys) { + name := configEnvName(key) + names[name] = true + + val, ok := env[name] + switch { + case !ok: + case slices.Contains(openMapKeys, key): + openLayer[key] = val + default: + layer[key] = val + } + } + + v := &configValidator{} + + for _, name := range slices.Sorted(maps.Keys(env)) { + if names[name] || reservedEnv(name) { + continue + } + + if r, ok := removedEnv(name); ok { + v.Add(&ConfigError{ + Reason: r.reason("variable", name), + Hint: r.envHint(), + }) + + continue + } + + s.warnings = append(s.warnings, "unknown environment variable "+name) + } + + if v.HasErrors() { + return v.Err() + } + + // A variable for an open map replaces the file's map instead of merging + // into it. The value stays a raw string, which the cast accessors parse as + // JSON, unless a per-field variable (HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME) + // has to be layered on top: then the JSON object is parsed here. + for key, val := range openLayer { + s.k.Delete(key) + + var parsed map[string]any + + shared := slices.ContainsFunc(slices.Collect(maps.Keys(layer)), func(k string) bool { + return strings.HasPrefix(k, key+".") + }) + + var v any = val + if shared && stdjson.Unmarshal([]byte(val), &parsed) == nil && parsed != nil { + v = parsed + } + + err := s.k.Set(key, v) + if err != nil { + return err + } + } + + return s.k.Load(confmap.Provider(layer, "."), nil) +} + +func reservedEnv(name string) bool { + if slices.Contains(reservedEnvNames, name) { + return true + } + + for _, p := range reservedEnvPrefixes { + if strings.HasPrefix(name, p) { + return true + } + } + + return false +} + +func removedEnv(name string) (removedKey, bool) { + for _, r := range removedKeys { + if configEnvName(r.Old) == name { + return r, true + } + } + + return removedKey{}, false +} + +// File returns the config file in use, or "" when only defaults apply. +func (s *configSource) File() string { return s.file } + +// Warnings returns non-fatal findings from loading. +func (s *configSource) Warnings() []string { return s.warnings } + +func (s *configSource) Get(key string) any { return s.k.Get(strings.ToLower(key)) } + +func (s *configSource) IsSet(key string) bool { return s.k.Exists(strings.ToLower(key)) } + +func (s *configSource) String(key string) string { return cast.ToString(s.Get(key)) } + +func (s *configSource) Bool(key string) bool { return cast.ToBool(s.Get(key)) } + +func (s *configSource) Int(key string) int { return cast.ToInt(s.Get(key)) } + +func (s *configSource) Int64(key string) int64 { return cast.ToInt64(s.Get(key)) } + +func (s *configSource) Duration(key string) time.Duration { return cast.ToDuration(s.Get(key)) } + +func (s *configSource) StringSlice(key string) []string { return cast.ToStringSlice(s.Get(key)) } + +func (s *configSource) StringMap(key string) map[string]any { return cast.ToStringMap(s.Get(key)) } + +func (s *configSource) StringMapString(key string) map[string]string { + return cast.ToStringMapString(s.Get(key)) +} + +func (s *configSource) StringMapStringSlice(key string) map[string][]string { + return cast.ToStringMapStringSlice(s.Get(key)) +} + +// UnmarshalKey decodes key into out with viper's decoder settings. +func (s *configSource) UnmarshalKey(key string, out any) error { + if !s.IsSet(key) { + return nil + } + + return s.k.UnmarshalWithConf(strings.ToLower(key), out, koanf.UnmarshalConf{ + DecoderConfig: &mapstructure.DecoderConfig{ + WeaklyTypedInput: true, + TagName: "mapstructure", + DecodeHook: mapstructure.ComposeDecodeHookFunc( + mapstructure.StringToTimeDurationHookFunc(), + mapstructure.StringToWeakSliceHookFunc(","), + ), + }, + }) +} + +// keyEntry is one key of the file as written, for finding paths that the file +// spells in more than one form. +type keyEntry struct { + path string + parts []string // path as schema names, see configPathParts + names []string // as written, outermost first + parent int + dotted bool + // container marks a plain mapping that only groups its children. + container bool +} + +func (e keyEntry) spelling() string { return strings.Join(e.names, " > ") } + +// checkOverlaps rejects a path given both dotted and nested ("log.level" next +// to "log: {level: ...}"): viper resolved such pairs by lookup order, which +// nothing here reproduces. +func (s *configSource) checkOverlaps(v *configValidator, m map[string]any) { + var entries []keyEntry + + var collect func(prefix string, parent int, names []string, m map[string]any) + + collect = func(prefix string, parent int, names []string, m map[string]any) { + for _, name := range slices.Sorted(maps.Keys(m)) { + path := joinConfigPath(prefix, name) + child, isMap := m[name].(map[string]any) + own := append(slices.Clone(names), name) + + entries = append(entries, keyEntry{ + path: path, parts: configPathParts(path), names: own, parent: parent, + dotted: strings.Contains(name, "."), + container: isMap && !isOpenMapPath(path) && !strings.Contains(name, "."), + }) + + if isMap && !isOpenMapPath(path) { + collect(path, len(entries)-1, own, child) + } + } + } + + collect("", -1, nil, m) + + ancestor := func(a, b int) bool { + for ; b >= 0; b = entries[b].parent { + if b == a { + return true + } + } + + return false + } + reported := map[[2]int]bool{} + + for j := range entries { + for i := range j { + a, b := entries[i], entries[j] + + if len(a.parts) > len(b.parts) { + a, b = b, a + } + + if !slices.Equal(a.parts, b.parts[:len(a.parts)]) || + ancestor(i, j) || ancestor(j, i) || + (!a.dotted && !b.dotted) || (a.container && a.path != b.path) { + continue + } + + // Its parents already overlap: one report per overlap. + if reported[[2]int{entries[i].parent, j}] || reported[[2]int{i, entries[j].parent}] || + reported[[2]int{entries[i].parent, entries[j].parent}] { + continue + } + + reported[[2]int{i, j}] = true + + if len(b.names) < len(a.names) { + a, b = b, a + } + + v.Add(&ConfigError{ + Reason: fmt.Sprintf("configuration key %s is set both as %q%s and as %q%s", + a.path, a.spelling(), s.positionOf(a.names), b.spelling(), s.positionOf(b.names)), + Hint: "use one form, either the dotted key or nested mappings", + }) + } + } +} + +// positionOf renders " (file:line:col)" for a key written as names. +func (s *configSource) positionOf(names []string) string { + line, col, ok := keyPosition(s.fileBytes, names) + if !ok { + return "" + } + + return fmt.Sprintf(" (%s:%d:%d)", s.file, line, col) +} diff --git a/hscontrol/types/config_source_test.go b/hscontrol/types/config_source_test.go new file mode 100644 index 000000000..2180884a7 --- /dev/null +++ b/hscontrol/types/config_source_test.go @@ -0,0 +1,711 @@ +package types + +import ( + "cmp" + "go/ast" + "go/parser" + "go/token" + "os" + "path/filepath" + "regexp" + "slices" + "strconv" + "strings" + "testing" + "time" + + "github.com/spf13/cast" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func writeConfig(t *testing.T, name, content string) string { + t.Helper() + + p := filepath.Join(t.TempDir(), name) + require.NoError(t, os.WriteFile(p, []byte(content), 0o600)) + + return p +} + +func TestConfigSource(t *testing.T) { + tests := []struct { + name string + file string // written as config.yaml unless fileName is set + fileName string + emptyFile bool + env []string + errs []string // substrings of the error + warns []string // substrings, one per expected warning + check func(t *testing.T, s *configSource) + }{ + { + name: "unknown file key", + file: "server_url: http://x\ndns:\n magic_dsn: true\n", + errs: []string{"unknown configuration key dns.magic_dsn (", "config.yaml:3:3)"}, + }, + { + name: "removed file key", + file: "grpc_listen_addr: :50443\n", + errs: []string{"grpc_listen_addr", "removed"}, + }, + { + name: "removed file prefix", + file: "dns_config:\n magic_dns: true\n", + errs: []string{"dns_config", "removed", "dns"}, + }, + { + name: "all problems collected", + file: "grpc_listen_addr: x\nbogus: 1\n", + errs: []string{"grpc_listen_addr", "bogus"}, + }, + { + name: "removed env", + env: []string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT=30m"}, + errs: []string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT", "HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT"}, + }, + { + name: "unknown env warns", + env: []string{"HEADSCALE_SERVR_URL=x"}, + warns: []string{"unknown environment variable HEADSCALE_SERVR_URL"}, + }, + { + name: "reserved env is silent", + env: []string{ + "HEADSCALE_PORT=tcp://10.0.0.1:8080", "HEADSCALE_SERVICE_HOST=x", "HEADSCALE_DEBUG_FOO=1", + "HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN=x", "HEADSCALE_CONFIG=/x", "HEADSCALE_TEST_X=1", + "HEADSCALE_INTEGRATION_X=1", "HEADSCALE_UPDATE_GOLDEN=1", + }, + }, + { + name: "env shared with a removed key still sets the new key", + env: []string{"HEADSCALE_LOG_LEVEL=debug", "HEADSCALE_TLS_CERT_PATH=/c"}, + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, "debug", s.String("log.level")) + assert.Equal(t, "/c", s.String("tls.cert_path")) + }, + }, + { + name: "keys are case-insensitive", + file: "Server_URL: https://x\ndns:\n Nameservers:\n Split:\n Corp.Example.com: [10.0.0.1]\n", + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, "https://x", s.String("server_url")) + assert.Equal(t, + map[string][]string{"corp.example.com": {"10.0.0.1"}}, + s.StringMapStringSlice("dns.nameservers.split")) + }, + }, + { + name: "null is unset, default stays", + file: "server_url:\nlog:\n level: ~\n", + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.False(t, s.IsSet("server_url")) + assert.Equal(t, "info", s.String("log.level")) + }, + }, + { + name: "empty env is unset, env beats file", + file: "server_url: http://file\nlisten_addr: 1.1.1.1:1\n", + env: []string{"HEADSCALE_SERVER_URL=http://env", "HEADSCALE_LISTEN_ADDR="}, + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, "http://env", s.String("server_url")) + assert.Equal(t, "1.1.1.1:1", s.String("listen_addr")) + }, + }, + { + name: "env lists and maps", + env: []string{ + "HEADSCALE_DNS_NAMESERVERS_GLOBAL=1.1.1.1 8.8.8.8", + `HEADSCALE_OIDC_EXTRA_PARAMS={"domain_hint":"example.com"}`, + }, + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, []string{"1.1.1.1", "8.8.8.8"}, s.StringSlice("dns.nameservers.global")) + assert.Equal(t, map[string]string{"domain_hint": "example.com"}, s.StringMapString("oidc.extra_params")) + }, + }, + { + name: "open maps accept any key", + file: "dns:\n challenge:\n provider:\n name: cloudflare\n whatever: 1\noidc:\n extra_params:\n a: b\n", + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, "cloudflare", s.String("dns.challenge.provider.name")) + assert.Equal(t, 1, s.Int("dns.challenge.provider.whatever")) + }, + }, + { + name: "yaml", + fileName: "config.yaml", + file: yamlSample, + check: checkSameValues, + }, + { + name: "yml", + fileName: "config.yml", + file: yamlSample, + check: checkSameValues, + }, + { + name: "json", + fileName: "config.json", + file: `{"server_url":"https://x","node":{"expiry":"1h"},"dns":{"magic_dns":false,"nameservers":{"global":["1.1.1.1","8.8.8.8"]}},"oidc":{"extra_params":{"domain_hint":"example.com"}}}`, + check: checkSameValues, + }, + { + name: "hujson", + fileName: "config.hujson", + file: "{\n // c\n \"server_url\":\"https://x\",\n \"node\":{\"expiry\":\"1h\",},\n \"dns\":{\"magic_dns\":false,\"nameservers\":{\"global\":[\"1.1.1.1\",\"8.8.8.8\",],},},\n \"oidc\":{\"extra_params\":{\"domain_hint\":\"example.com\"}},\n}", + check: checkSameValues, + }, + { + name: "hujson syntax error", + fileName: "config.hujson", + file: "{\n // c\n \"server_url\": x,\n}", + errs: []string{"config.hujson", "hujson: line 3, column 16"}, + }, + { + name: "toml is found and rejected", + fileName: "config.toml", + file: "server_url = \"https://x\"\n", + errs: []string{`unsupported config file format ".toml": use .json, .yaml, .yml or .hujson`, "config.toml)"}, + }, + { + name: ".env is found and rejected", + fileName: "config.env", + file: "SERVER_URL=https://x\n", + errs: []string{`unsupported config file format ".env": use .json, .yaml, .yml or .hujson`, "config.env)"}, + }, + { + name: ".dotenv is found and rejected", + fileName: "config.dotenv", + file: "SERVER_URL=https://x\n", + errs: []string{`unsupported config file format ".dotenv": use .json, .yaml, .yml or .hujson`, "config.dotenv)"}, + }, + { + name: "nested keys that share leaf names with removed keys", + file: "noise:\n private_key_path: /n\nderp:\n server:\n private_key_path: /d\nlog:\n level: debug\ntls:\n cert_path: /c\ndatabase:\n sqlite:\n path: /db\n", + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, "/n", s.String("noise.private_key_path")) + assert.Equal(t, "/d", s.String("derp.server.private_key_path")) + assert.Equal(t, "debug", s.String("log.level")) + assert.Equal(t, "/c", s.String("tls.cert_path")) + assert.Equal(t, "/db", s.String("database.sqlite.path")) + }, + }, + { + name: "dotted file keys override nested defaults", + file: "log.level: debug\ndns.magic_dns: false\ndns:\n nameservers.split:\n Corp.Example.com: [10.0.0.1]\n", + check: func(t *testing.T, s *configSource) { + t.Helper() + + for range 50 { + assert.Equal(t, "debug", s.String("log.level")) + assert.False(t, s.Bool("dns.magic_dns")) + } + + assert.Equal(t, map[string][]string{"corp.example.com": {"10.0.0.1"}}, s.StringMapStringSlice("dns.nameservers.split")) + }, + }, + { + name: "dotted file key in a removed prefix", + file: "dns_config.magic_dns: true\n", + errs: []string{"dns_config", "removed"}, + }, + { + name: "mapping under a plain setting", + file: "tuning:\n batcher_workers:\n bogus: 1\nserver_url:\n bogus: x\n", + errs: []string{"unknown configuration key tuning.batcher_workers.bogus", "unknown configuration key server_url.bogus"}, + }, + { + name: "non-string yaml keys are normalised", + file: "dns:\n nameservers:\n split:\n 123: [1.1.1.1]\n Corp.Example.com: [10.0.0.1]\n Empty.Example.com: ~\n", + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, + map[string][]string{"123": {"1.1.1.1"}, "corp.example.com": {"10.0.0.1"}}, + s.StringMapStringSlice("dns.nameservers.split")) + }, + }, + { + name: "key arguments are case-insensitive", + file: "server_url: https://x\n", + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.True(t, s.IsSet("Server_URL")) + assert.Equal(t, "https://x", s.String("SERVER_URL")) + }, + }, + { + name: "removed dns_config env", + env: []string{"HEADSCALE_DNS_CONFIG_MAGIC_DNS=false", "HEADSCALE_DNS_CONFIG_NAMESERVERS=1.1.1.1"}, + errs: []string{"variable HEADSCALE_DNS_CONFIG_MAGIC_DNS was removed", "HEADSCALE_DNS_CONFIG_NAMESERVERS", "use HEADSCALE_DNS_MAGIC_DNS"}, + }, + { + name: "removed env names the new variable", + env: []string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT=30m"}, + errs: []string{"variable HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT was removed in 0.29", "use HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT"}, + }, + { + name: "exact reserved names do not exempt near misses", + env: []string{"HEADSCALE_CONFIG=/x", "HEADSCALE_UPDATE_GOLDEN=1", "HEADSCALE_DNS_CHALLENGE_PROVIDER=x", "HEADSCALE_CONFIG_TYPO=1", "HEADSCALE_UPDATE_GOLDEN_BOGUS=1"}, + warns: []string{"HEADSCALE_CONFIG_TYPO", "HEADSCALE_UPDATE_GOLDEN_BOGUS"}, + }, + { + name: "empty file", + emptyFile: true, + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, "info", s.String("log.level")) + }, + }, + { + name: "unsupported format", + fileName: "config.ini", + file: "a=1\n", + errs: []string{"unsupported config file format"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + path, isFile := dir, false + + if tt.file != "" || tt.fileName != "" || tt.emptyFile { + name := cmp.Or(tt.fileName, "config.yaml") + require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(tt.file), 0o600)) + + // .ini is not searched for, so it has to be named. + if filepath.Ext(name) == ".ini" { + path, isFile = filepath.Join(dir, name), true + } + } + + s, err := newConfigSource(path, isFile, tt.env) + if len(tt.errs) > 0 { + require.Error(t, err) + + for _, want := range tt.errs { + require.ErrorContains(t, err, want) + } + + return + } + + require.NoError(t, err) + require.Len(t, s.Warnings(), len(tt.warns)) + + for i, w := range tt.warns { + assert.Contains(t, s.Warnings()[i], w) + } + + if tt.check != nil { + tt.check(t, s) + } + }) + } +} + +const yamlSample = "server_url: https://x\nnode:\n expiry: 1h\ndns:\n magic_dns: false\n nameservers:\n global: [1.1.1.1, 8.8.8.8]\noidc:\n extra_params:\n domain_hint: example.com\n" + +func checkSameValues(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, "https://x", s.String("server_url")) + assert.Equal(t, time.Hour, s.Duration("node.expiry")) + assert.False(t, s.Bool("dns.magic_dns")) + assert.Equal(t, []string{"1.1.1.1", "8.8.8.8"}, s.StringSlice("dns.nameservers.global")) + assert.Equal(t, map[string]string{"domain_hint": "example.com"}, s.StringMapString("oidc.extra_params")) +} + +func TestConfigSearchDirs(t *testing.T) { + got := configSearchDirs([]string{"HOME=/h", "XDG_CONFIG_HOME=/xh", "XDG_CONFIG_DIRS=/a:/b"}) + assert.Equal(t, []string{"/etc/headscale", "/xh/headscale", "/a/headscale", "/b/headscale", "/h/.headscale", "."}, got) + + got = configSearchDirs([]string{"HOME=/h"}) + assert.Equal(t, []string{"/etc/headscale", "/h/.config/headscale", "/etc/xdg/headscale", "/h/.headscale", "."}, got) + + xdg := t.TempDir() + require.NoError(t, os.MkdirAll(filepath.Join(xdg, "headscale"), 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(xdg, "headscale", "config.yaml"), nil, 0o600)) + assert.Equal(t, filepath.Join(xdg, "headscale", "config.yaml"), findConfigFile("", false, []string{"XDG_CONFIG_HOME=" + xdg})) +} + +func TestConfigSourceSearch(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte("server_url: http://yaml\n"), 0o600)) + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.json"), []byte(`{"server_url":"http://json"}`), 0o600)) + + s, err := newConfigSource(dir, false, nil) + require.NoError(t, err) + assert.True(t, strings.HasSuffix(s.File(), "config.json")) + assert.Equal(t, "http://json", s.String("server_url")) + + s, err = newConfigSource(t.TempDir(), false, nil) + require.NoError(t, err) + assert.Empty(t, s.File()) +} + +func TestConfigSourceUnmarshalKey(t *testing.T) { + p := writeConfig(t, "config.yaml", "dns:\n extra_records:\n - name: a.example.com\n type: A\n value: 1.2.3.4\n") + s, err := newConfigSource(p, true, nil) + require.NoError(t, err) + + var recs []struct { + Name, Type, Value string + } + + require.NoError(t, s.UnmarshalKey("dns.extra_records", &recs)) + require.Len(t, recs, 1) + assert.Equal(t, "a.example.com", recs[0].Name) +} + +// parityRejected lists the fixtures the source must reject, with every key it +// must name. All other fixtures must load. +var parityRejected = func() map[string][]string { + core := []string{ + "acme_email", "acme_url", "database.gorm.prepare_stmt", "grpc_allow_insecure", "grpc_listen_addr", + "tls_cert_path", "tls_key_path", "tls_letsencrypt_cache_dir", "tls_letsencrypt_challenge_type", + "tls_letsencrypt_hostname", "tls_letsencrypt_listen", + } + mid := append([]string{"ephemeral_node_inactivity_timeout", "randomize_client_port"}, core...) + + return map[string][]string{ + "example-v0.23.0": append([]string{"dns.use_username_in_magic_dns"}, mid...), + "example-v0.24.0": mid, + "example-v0.25.0": mid, + "example-v0.26.0": mid, + "example-v0.27.0": mid, + "example-v0.28.0": mid, + "example-v0.29.0": core, + "example-v0.29.7": core, + "hsic-default": {"private_key_path"}, + "hsic-postgres": {"private_key_path"}, + "testdata-policy-path-is-loaded": {"acl_policy_path", "policy.type"}, + "bad-values": {"dns.magic_dsn", "ephemeral_node_inactivity_timeout"}, + "nixos-nixpkgs": { + "tls_letsencrypt_cache_dir", "tls_letsencrypt_challenge_type", "tls_letsencrypt_hostname", "tls_letsencrypt_listen", + }, + "env-legacy-ignored": { + "HEADSCALE_DATABASE_GORM_PREPARE_STMT", "HEADSCALE_DB_TYPE", "HEADSCALE_DNS_CONFIG_MAGIC_DNS", + "HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT", "HEADSCALE_PRIVATE_KEY_PATH", + }, + "env-legacy-removed": {"HEADSCALE_ACL_POLICY_PATH", "HEADSCALE_OIDC_EXPIRY", "HEADSCALE_RANDOMIZE_CLIENT_PORT"}, + } +}() + +var removedKeyInReason = regexp.MustCompile(`configuration (?:key|variable) (\S+) was removed|unknown configuration key (\S+)`) + +// TestConfigSourceParity checks the source against viper on every Phase-0 +// fixture: same set-ness and same value for each coercion. +func TestConfigSourceParity(t *testing.T) { + // The shipped example is the only fixture with every current key and no + // removed ones. + rows := append(compatRows(t), compatRow{name: "config-example", file: "../../config-example.yaml"}) + + unsetHeadscaleEnv(t) + + for _, tt := range rows { + t.Run(tt.name, func(t *testing.T) { + viper.Reset() + t.Cleanup(viper.Reset) + + dir := stageCompat(t, tt.file, tt.envFile) + require.NoError(t, LoadConfig(dir, false)) + + s, err := newConfigSource(dir, false, os.Environ()) + + if want, rejected := parityRejected[tt.name]; rejected { + require.Error(t, err) + + var got []string + + for _, ce := range ConfigErrors(err) { + m := removedKeyInReason.FindStringSubmatch(ce.Reason) + require.NotNil(t, m, ce.Reason) + + got = append(got, m[1]+m[2]) + } + + assert.ElementsMatch(t, want, got) + + return + } + + require.NoError(t, err) + + for _, key := range configKeys { + v := viper.Get(key) + + assert.Equalf(t, viper.IsSet(key), s.IsSet(key), "IsSet %s", key) + assert.Equalf(t, cast.ToString(v), s.String(key), "String %s", key) + assert.Equalf(t, cast.ToBool(v), s.Bool(key), "Bool %s", key) + assert.Equalf(t, cast.ToInt(v), s.Int(key), "Int %s", key) + assert.Equalf(t, cast.ToDuration(v), s.Duration(key), "Duration %s", key) + assert.Equalf(t, cast.ToStringSlice(v), s.StringSlice(key), "StringSlice %s", key) + assert.Equalf(t, cast.ToStringMapString(v), s.StringMapString(key), "StringMapString %s", key) + assert.Equalf(t, cast.ToStringMapStringSlice(v), s.StringMapStringSlice(key), "StringMapStringSlice %s", key) + } + }) + } +} + +// TestConfigSourceUnmarshalParity checks UnmarshalKey against viper's decoder. +func TestConfigSourceUnmarshalParity(t *testing.T) { + type rec struct{ Name, Type, Value string } + + for _, content := range []string{ + "dns:\n extra_records: \"\"\n", + "dns:\n extra_records:\n - {name: a.example.com, type: A, value: 1.2.3.4}\n", + } { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(content), 0o600)) + + viper.Reset() + t.Cleanup(viper.Reset) + require.NoError(t, LoadConfig(dir, false)) + + var want []rec + + wantErr := viper.UnmarshalKey("dns.extra_records", &want) + + s, err := newConfigSource(dir, false, nil) + require.NoError(t, err) + + var got []rec + + gotErr := s.UnmarshalKey("dns.extra_records", &got) + require.Equal(t, wantErr == nil, gotErr == nil, "viper: %v, source: %v", wantErr, gotErr) + assert.Equal(t, want, got) + } +} + +// pendingKeys are read by the viper loader but replaced by file: secrets later. +var pendingKeys = map[string]bool{"oidc.client_secret_path": true} + +func TestConfigKeysComplete(t *testing.T) { + read := map[string]bool{} + + for _, f := range []string{"config.go", "listener.go"} { + file, err := parser.ParseFile(token.NewFileSet(), f, nil, 0) + require.NoError(t, err) + + ast.Inspect(file, func(n ast.Node) bool { + call, ok := n.(*ast.CallExpr) + if !ok || len(call.Args) == 0 { + return true + } + + var name string + + switch fn := call.Fun.(type) { + case *ast.SelectorExpr: + if obj, ok := fn.X.(*ast.Ident); !ok || obj.Name != "viper" { + return true + } + + name = fn.Sel.Name + case *ast.Ident: + name = fn.Name + default: + return true + } + + if !strings.HasPrefix(name, "Get") && name != "IsSet" && name != "InConfig" && + name != "UnmarshalKey" && name != "tlsKey" { + return true + } + + if lit, ok := call.Args[0].(*ast.BasicLit); ok && lit.Kind == token.STRING { + key, err := strconv.Unquote(lit.Value) + require.NoError(t, err) + + read[key] = true + } + + return true + }) + } + + removed := map[string]bool{} + for _, r := range removedKeys { + removed[r.Old] = true + } + + all := slices.Concat(configKeys, openMapKeys) + + for key := range read { + ok := slices.Contains(all, key) || removed[key] || key == "dns" || + strings.HasPrefix(key, "dns.challenge.provider") + + assert.Truef(t, ok, "%s is read but missing from the key tables", key) + } + + for _, key := range configKeys { + _, hasDefault := configDefaults[key] + assert.Truef(t, read[key] || hasDefault || pendingKeys[key], "%s is listed but never read", key) + } + + for key := range configDefaults { + assert.Contains(t, configKeys, key, "default without a key entry") + } + + // Two keys sharing one environment variable would shadow each other. + envs := map[string]string{} + + for _, key := range configKeys { + name := configEnvName(key) + assert.Emptyf(t, envs[name], "%s and %s share %s", envs[name], key, name) + envs[name] = key + } +} + +func loadViper(t *testing.T, dir string) { + t.Helper() + viper.Reset() + t.Cleanup(viper.Reset) + require.NoError(t, LoadConfig(dir, false)) +} + +// TestConfigSourceDottedOverlap reloads the file each time: map iteration +// order differs per load, so a single load could hide a nondeterministic result. +func TestConfigSourceDottedOverlap(t *testing.T) { + tests := []struct{ name, file, a, b string }{ + {"scalar", "log:\n level: info\nlog.level: debug\n", `"log.level"`, `"log > level"`}, + {"map shadow", "oidc:\n extra_params:\n a: nested\noidc.extra_params:\n b: dotted\n", `"oidc.extra_params"`, `"oidc > extra_params"`}, + {"split fallback", "dns:\n nameservers:\n split:\n a.example.com: [10.0.0.1]\ndns.nameservers:\n global: [1.1.1.1]\n", `"dns.nameservers"`, `"dns > nameservers"`}, + {"parent map and dotted child", "oidc.extra_params:\n prompt: login\noidc.extra_params.prompt: consent\n", `"oidc.extra_params"`, `"oidc.extra_params.prompt"`}, + {"open map entry twice", "dns:\n nameservers:\n split.corp.example.com: [10.0.0.1]\ndns.nameservers.split.corp.example.com: [10.0.0.2]\n", `"dns > nameservers > split.corp.example.com"`, `"dns.nameservers.split.corp.example.com"`}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(tt.file), 0o600)) + + for range 50 { + _, err := newConfigSource(dir, false, nil) + require.ErrorContains(t, err, "is set both as") + require.ErrorContains(t, err, tt.a) + require.ErrorContains(t, err, tt.b) + require.ErrorContains(t, err, "use one form") + } + }) + } +} + +func TestConfigSourceDottedKeyAlone(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), + []byte("log.level: debug\ndns:\n magic_dns: false\ndns.nameservers.global: [1.1.1.1]\n"), 0o600)) + loadViper(t, dir) + + for range 50 { + s, err := newConfigSource(dir, false, nil) + require.NoError(t, err) + require.Equal(t, viper.GetString("log.level"), s.String("log.level")) + require.Equal(t, viper.GetBool("dns.magic_dns"), s.Bool("dns.magic_dns")) + require.Equal(t, viper.GetStringSlice("dns.nameservers.global"), s.StringSlice("dns.nameservers.global")) + } +} + +// TestConfigSourceDottedNoOverlap loads files whose dotted keys only share a +// string prefix: below an open map the entry name is one key, dots included. +func TestConfigSourceDottedNoOverlap(t *testing.T) { + tests := []struct { + name, file string + check func(t *testing.T, s *configSource) + }{ + { + name: "dotted split domains", + file: "dns.nameservers.split.corp.example.com: [10.0.0.1]\ndns.nameservers.split.corp.example.com.au: [10.0.0.2]\n", + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, + map[string][]string{"corp.example.com": {"10.0.0.1"}, "corp.example.com.au": {"10.0.0.2"}}, + s.StringMapStringSlice("dns.nameservers.split")) + }, + }, + { + name: "dotted extra params", + file: "oidc.extra_params.domain_hint: a\noidc.extra_params.domain_hint.extra: b\n", + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, map[string]string{"domain_hint": "a", "domain_hint.extra": "b"}, s.StringMapString("oidc.extra_params")) + }, + }, + { + name: "nested split domains", + file: "dns:\n nameservers:\n split:\n corp.example.com: [10.0.0.1]\n corp.example.com.au: [10.0.0.2]\n", + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, + map[string][]string{"corp.example.com": {"10.0.0.1"}, "corp.example.com.au": {"10.0.0.2"}}, + s.StringMapStringSlice("dns.nameservers.split")) + }, + }, + { + name: "log and logtail", + file: "log:\n level: debug\nlog.format: json\nlogtail.enabled: true\n", + check: func(t *testing.T, s *configSource) { + t.Helper() + assert.Equal(t, "debug", s.String("log.level")) + assert.Equal(t, "json", s.String("log.format")) + assert.True(t, s.Bool("logtail.enabled")) + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(tt.file), 0o600)) + + for range 50 { + s, err := newConfigSource(dir, false, nil) + require.NoError(t, err) + tt.check(t, s) + } + }) + } +} + +func TestConfigSourceStrictBeforeExpansion(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte("log:\n format:\n typo: json\nlog.format: json\n"), 0o600)) + + for range 50 { + _, err := newConfigSource(dir, false, nil) + require.ErrorContains(t, err, "unknown configuration key log.format.typo") + } +} + +func TestConfigSourceProviderEnvParity(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), + []byte("dns:\n challenge:\n provider:\n name: file\n other: x\n"), 0o600)) + + t.Setenv("HEADSCALE_DNS_CHALLENGE_PROVIDER", `{"api_token":"x"}`) + t.Setenv("HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME", "cloudflare") + loadViper(t, dir) + + s, err := newConfigSource(dir, false, os.Environ()) + require.NoError(t, err) + assert.Empty(t, s.Warnings()) + + assert.Equal(t, viper.GetString("dns.challenge.provider.name"), s.String("dns.challenge.provider.name")) + + // The env value replaces the file's map, as in viper; the per-field + // variable is layered on top of it. + got := s.StringMap("dns.challenge.provider") + for k, v := range viper.GetStringMap("dns.challenge.provider") { + assert.Equal(t, v, got[k]) + } + + assert.NotContains(t, got, "other") +} diff --git a/hscontrol/types/config_test.go b/hscontrol/types/config_test.go index b4b89a93f..18fa52ec7 100644 --- a/hscontrol/types/config_test.go +++ b/hscontrol/types/config_test.go @@ -1860,6 +1860,67 @@ type compatRecord struct { Error string `json:"error,omitempty"` // an error-only golden pins today's rejection } +type compatRow struct{ name, file, envFile string } + +var compatDir = filepath.Join("testdata", "compat") + +// compatRows lists every compat/.{yaml,json,env} set and every +// testdata/*.yaml file. +func compatRows(t *testing.T) []compatRow { + t.Helper() + + entries, err := os.ReadDir(compatDir) + require.NoError(t, err) + + var rows []compatRow + + byName := map[string]int{} + + for _, e := range entries { + f, ext := e.Name(), filepath.Ext(e.Name()) + if strings.HasSuffix(f, ".golden.json") || (ext != ".yaml" && ext != ".json" && ext != ".env") { + continue + } + + name := strings.TrimSuffix(f, ext) + + i, ok := byName[name] + if !ok { + i = len(rows) + byName[name] = i + rows = append(rows, compatRow{name: name}) + } + + if ext == ".env" { + rows[i].envFile = filepath.Join(compatDir, f) + } else { + rows[i].file = filepath.Join(compatDir, f) + } + } + + legacy, err := filepath.Glob(filepath.Join("testdata", "*.yaml")) + require.NoError(t, err) + + for _, f := range legacy { + rows = append(rows, compatRow{name: "testdata-" + strings.TrimSuffix(filepath.Base(f), ".yaml"), file: f}) + } + + return rows +} + +// unsetHeadscaleEnv removes ambient HEADSCALE_* for the test, which would +// leak into the goldens. +func unsetHeadscaleEnv(t *testing.T) { + t.Helper() + + for _, kv := range os.Environ() { + if k, _, _ := strings.Cut(kv, "="); strings.HasPrefix(k, "HEADSCALE_") { + t.Setenv(k, "") // registers the restore + require.NoError(t, os.Unsetenv(k)) + } + } +} + // TestConfigCompat pins what real-world config inputs load to, so a loader // change shows up as a golden diff, and each such diff needs a BREAKING // CHANGELOG entry. Every compat/.{yaml,json,env} set is one row; a name @@ -1874,52 +1935,10 @@ type compatRecord struct { // dns.nameservers.global set; hsic-* mirror integration/hsic's // MinimumConfigYAML and DefaultConfigEnv. func TestConfigCompat(t *testing.T) { - type row struct{ name, file, envFile string } - - compatDir := filepath.Join("testdata", "compat") - - entries, err := os.ReadDir(compatDir) - require.NoError(t, err) - - var rows []*row - - byName := map[string]*row{} - - for _, e := range entries { - f, ext := e.Name(), filepath.Ext(e.Name()) - if strings.HasSuffix(f, ".golden.json") || (ext != ".yaml" && ext != ".json" && ext != ".env") { - continue - } - - name := strings.TrimSuffix(f, ext) - if byName[name] == nil { - byName[name] = &row{name: name} - rows = append(rows, byName[name]) - } - - if ext == ".env" { - byName[name].envFile = filepath.Join(compatDir, f) - } else { - byName[name].file = filepath.Join(compatDir, f) - } - } - - legacy, err := filepath.Glob(filepath.Join("testdata", "*.yaml")) - require.NoError(t, err) - - for _, f := range legacy { - rows = append(rows, &row{name: "testdata-" + strings.TrimSuffix(filepath.Base(f), ".yaml"), file: f}) - } - + rows := compatRows(t) update := os.Getenv("HEADSCALE_UPDATE_GOLDEN") != "" - // Ambient HEADSCALE_* would leak into the goldens. - for _, kv := range os.Environ() { - if k, _, _ := strings.Cut(kv, "="); strings.HasPrefix(k, "HEADSCALE_") { - t.Setenv(k, "") // registers the restore - require.NoError(t, os.Unsetenv(k)) - } - } + unsetHeadscaleEnv(t) for _, tt := range rows { t.Run(tt.name, func(t *testing.T) { @@ -1955,30 +1974,7 @@ func loadCompat(t *testing.T, file, envFile string, cli bool) compatRecord { viper.Reset() t.Cleanup(viper.Reset) - dir := t.TempDir() - - if file != "" { - data, err := os.ReadFile(file) - require.NoError(t, err) - require.NoError(t, os.WriteFile(filepath.Join(dir, "config"+filepath.Ext(file)), data, 0o600)) //nolint:gosec // dir is t.TempDir() - } - - if envFile != "" { - raw, err := os.ReadFile(envFile) - require.NoError(t, err) - - // docker --env-file syntax: no quoting, # comments. - for line := range strings.SplitSeq(string(raw), "\n") { - line = strings.TrimSpace(line) - if line == "" || strings.HasPrefix(line, "#") { - continue - } - - k, v, ok := strings.Cut(line, "=") - require.Truef(t, ok, "%s: %q is not KEY=VALUE", envFile, line) - t.Setenv(k, v) - } - } + dir := stageCompat(t, file, envFile) var buf bytes.Buffer @@ -2050,6 +2046,39 @@ func loadCompat(t *testing.T, file, envFile string, cli bool) compatRecord { return rec } +// stageCompat copies file into a temp dir as config. and sets the +// variables of envFile, returning the dir. +func stageCompat(t *testing.T, file, envFile string) string { + t.Helper() + + dir := t.TempDir() + + if file != "" { + data, err := os.ReadFile(file) + require.NoError(t, err) + require.NoError(t, os.WriteFile(filepath.Join(dir, "config"+filepath.Ext(file)), data, 0o600)) //nolint:gosec // dir is t.TempDir() + } + + if envFile != "" { + raw, err := os.ReadFile(envFile) + require.NoError(t, err) + + // docker --env-file syntax: no quoting, # comments. + for line := range strings.SplitSeq(string(raw), "\n") { + line = strings.TrimSpace(line) + if line == "" || strings.HasPrefix(line, "#") { + continue + } + + k, v, ok := strings.Cut(line, "=") + require.Truef(t, ok, "%s: %q is not KEY=VALUE", envFile, line) + t.Setenv(k, v) + } + } + + return dir +} + func TestDERPServerPrivateKeyPath(t *testing.T) { tests := []struct { name string