diff --git a/.prettierignore b/.prettierignore index 5560a2fbd..89980e6e8 100644 --- a/.prettierignore +++ b/.prettierignore @@ -3,4 +3,5 @@ docs/ # Golden fixtures are byte-compared; a formatter must not rewrite them. hscontrol/testdata/ +hscontrol/types/testdata/bad/ hscontrol/types/testdata/compat/ diff --git a/flakehashes.json b/flakehashes.json index 95db3a9f2..43dc45aa9 100644 --- a/flakehashes.json +++ b/flakehashes.json @@ -1,6 +1,6 @@ { "vendor": { - "goModSum": "sha256-jT09QcBZYC/45UW2H9CUiun/gYjVJqFcpTQ8Cf2Jamg=", - "sri": "sha256-FlwgM94RjLO2Ct7ljZDsYrNh23/Obb3RwxjoE1oAndQ=" + "goModSum": "sha256-jAlQrliXnbccw4H5cimIeXJgHGvPRU3HkqneDopqbHo=", + "sri": "sha256-CksVgaWynV5w20+MJf9dDTlmPF4CbrkTI2LUPO0QUWs=" } } diff --git a/go.mod b/go.mod index 73a04364f..0f21ce219 100644 --- a/go.mod +++ b/go.mod @@ -50,7 +50,6 @@ 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/stretchr/testify v1.12.1 github.com/tailscale/hujson v0.0.0-20260727124030-b80ff77dac4f diff --git a/go.sum b/go.sum index 863d797c2..66567cc35 100644 --- a/go.sum +++ b/go.sum @@ -482,8 +482,6 @@ github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBB github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= -github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY= -github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo= github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/hscontrol/types/config.go b/hscontrol/types/config.go index 493fac14e..f167f998a 100644 --- a/hscontrol/types/config.go +++ b/hscontrol/types/config.go @@ -2,6 +2,7 @@ package types import ( "cmp" + "encoding/json" "errors" "fmt" "maps" @@ -10,20 +11,19 @@ import ( "net/url" "os" "path/filepath" + "reflect" "slices" "strconv" "strings" "sync" "time" + "github.com/coreos/go-oidc/v3/oidc" hsdns "github.com/juanfont/headscale/hscontrol/dns" "github.com/juanfont/headscale/hscontrol/util" "github.com/knadh/koanf/parsers/yaml" - "github.com/prometheus/common/model" "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "github.com/spf13/cast" - "go4.org/netipx" "tailscale.com/net/tsaddr" "tailscale.com/tailcfg" "tailscale.com/types/dnstype" @@ -126,7 +126,7 @@ type NodeConfig struct { // Config is the Headscale configuration, nested like the configuration // file; koanf tags name the keys. -type Config struct { +type Config struct { //nolint:recvcheck // MarshalJSON reads a value, so a Config value is redacted too ServerURL string `koanf:"server_url"` Addr ListenAddr `koanf:"listen_addr"` MetricsAddr ListenAddr `koanf:"metrics_listen_addr"` @@ -222,7 +222,22 @@ type DNSChallengeConfig struct { // ProviderName is the provider ProviderSettings names, or "". func (c DNSChallengeConfig) ProviderName() string { - return cast.ToString(c.ProviderSettings["name"]) + name, _ := c.providerName() + + return name +} + +// providerName is the name as text, and false when it is a map or list: +// its contents may be credentials, so it is never formatted. +func (c DNSChallengeConfig) providerName() (string, bool) { + switch name := c.ProviderSettings["name"].(type) { + case nil: + return "", true + case string: + return name, true + default: + return scalarText(reflect.ValueOf(name)) + } } // NewProvider builds the provider ProviderSettings names, handing secret @@ -469,10 +484,6 @@ type LogConfig struct { // need adjustment. Changes to these values can significantly impact performance // and resource usage. type Tuning struct { - // NotifierSendTimeout is the maximum time to wait when sending notifications - // to connected clients about network changes. - NotifierSendTimeout time.Duration `koanf:"notifier_send_timeout"` - // BatchChangeDelay controls how long to wait before sending batched updates // to clients when multiple changes occur in rapid succession. BatchChangeDelay time.Duration `koanf:"batch_change_delay"` @@ -541,29 +552,27 @@ func validatePKCEMethod(method PKCEMethod) error { return nil } -// validateOIDCConfig validates the OIDC settings, called when oidc.issuer is -// set. It fails fast on a setup that cannot work: an invalid PKCE method, a +// validate checks the OIDC settings, called when oidc.issuer is set. It +// fails fast on a setup that cannot work: an invalid PKCE method, a // malformed issuer URL (which would otherwise surface as an opaque discovery // error or, worse, resolve to an unintended provider), or a missing client // id/secret. -func validateOIDCConfig() error { - err := validatePKCEMethod(PKCEMethod(src.String("oidc.pkce.method"))) +func (o OIDCConfig) validate() error { + err := validatePKCEMethod(o.PKCE.Method) if err != nil { return err } - issuer := src.String("oidc.issuer") - - u, err := url.Parse(issuer) + u, err := url.Parse(string(o.Issuer)) if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" { - return fmt.Errorf("%w: got %q", errOIDCIssuerInvalid, issuer) + return fmt.Errorf("%w: got %q", errOIDCIssuerInvalid, string(o.Issuer)) } - if src.String("oidc.client_id") == "" { + if o.ClientID == "" { return errOIDCClientIDRequired } - if src.String("oidc.client_secret") == "" { + if o.ClientSecret == "" { return errOIDCClientSecretRequired } @@ -586,6 +595,76 @@ func (c *Config) AbsPath(p string) string { return util.AbsolutePath(c.ConfigDir, p) } +// URLKeys are the keys that may hold a URL, whose userinfo may be a +// credential: [Config.RedactedURLs] redacts it and errors never show their +// values. +var URLKeys = []string{ + "server_url", "cli.address", "dns.nameservers.global", "dns.nameservers.split", + "tls.acme.url", "oidc.issuer", "derp.urls", +} + +// RedactedURLs returns c with the userinfo of the URLs that URLKeys hold +// redacted as [HTTPURL.Redacted] does; a value without an @ has none and is +// kept, a host:port or an IP included. c keeps its values. +func (c Config) RedactedURLs() Config { + for _, f := range ConfigFields(&c) { + if slices.Contains(URLKeys, f.Path) { + f.Value.Set(redactedURLs(f.Value)) + } + } + + return c +} + +// redactedURLs is v, a text or a list or map of texts, with each redacted; +// a list or map is a copy. +func redactedURLs(v reflect.Value) reflect.Value { + switch v.Kind() { //nolint:exhaustive // the kinds URLKeys hold + case reflect.String: + return reflect.ValueOf(redactedURL(v.String())).Convert(v.Type()) + case reflect.Slice: + if v.IsNil() { + return v + } + + out := reflect.MakeSlice(v.Type(), v.Len(), v.Len()) + for i := range v.Len() { + out.Index(i).Set(redactedURLs(v.Index(i))) + } + + return out + case reflect.Map: + if v.IsNil() { + return v + } + + out := reflect.MakeMapWithSize(v.Type(), v.Len()) + for k, e := range v.Seq2() { + out.SetMapIndex(k, redactedURLs(e)) + } + + return out + } + + return v +} + +func redactedURL(s string) string { + if !strings.Contains(s, "@") { + return s + } + + return HTTPURL(s).Redacted() +} + +// MarshalJSON writes c without credentials, for debug output and logs: +// secrets are left out and URLs lose their userinfo. +func (c Config) MarshalJSON() ([]byte, error) { + type plain Config // without this method + + return json.Marshal(plain(c.RedactedURLs())) +} + // SQLitePath is database.sqlite.path resolved, keeping SQLite's in-memory // name, which is not a file. func (c *Config) SQLitePath() string { @@ -619,7 +698,7 @@ var configFile string // LoadConfig loads defaults, the configuration file and HEADSCALE_* // environment variables, rejecting unknown and removed keys. It has to be -// called before [LoadServerConfig] and [LoadCLIConfig], which validate the +// called before [LoadServerConfig] and [LoadCLIConfig], which decode the // values. func LoadConfig(path string, isFile bool) error { configFile = findConfigFile(path, isFile, os.Environ()) @@ -638,31 +717,29 @@ func LoadConfig(path string, isFile bool) error { return nil } -// configDir is where relative paths in the configuration are resolved: the -// config file's directory, or the working directory without a file. -func configDir() string { - dir := "." - if src.File() != "" { - dir = filepath.Dir(src.File()) - } - - abs, err := filepath.Abs(dir) - if err != nil { - return dir - } - - return abs -} - // ConfigFile returns the file the last [LoadConfig] loaded or failed to // load, or "" when it found none. func ConfigFile() string { return configFile } -// LogFormatSetting returns the configured log.format. -func LogFormatSetting() LogFormat { return LogFormat(src.String("log.format")) } +// LogFormatSetting returns the configured log.format, or "" when the +// configuration does not decode. +func LogFormatSetting() LogFormat { + cfg, err := src.decode() + if err != nil { + return "" + } + + return cfg.Log.Format +} // UpdateCheckDisabled reports whether disable_check_updates is set. -func UpdateCheckDisabled() bool { return src.Bool("disable_check_updates") } +// A configuration that does not decode disables it too: the command reports +// the error instead. +func UpdateCheckDisabled() bool { + cfg, err := src.decode() + + return err != nil || cfg.DisableUpdateCheck +} // DumpConfig renders the loaded configuration, defaults included, as YAML. func DumpConfig() ([]byte, error) { @@ -673,91 +750,81 @@ func DumpConfig() ([]byte, error) { return src.k.Marshal(yaml.Parser()) } -// resolveNodeExpiry parses the node.expiry config value. -// Returns 0 if set to "0" (no default expiry) or on parse failure. -func resolveNodeExpiry() time.Duration { - value := src.String("node.expiry") - if value == "" || value == "0" { - return 0 +// defaultConfig is the configuration before a file or variable sets +// anything. database.type has no default. +func defaultConfig() Config { + return Config{ + UnixSocket: "/var/run/headscale/headscale.sock", + UnixSocketPermission: 0o770, + Prefixes: PrefixesConfig{Allocation: IPAllocationStrategySequential}, + Policy: PolicyConfig{Mode: PolicyModeFile}, + Log: LogConfig{Level: "info", Format: TextLogFormat}, + Node: NodeConfig{ + Ephemeral: EphemeralConfig{InactivityTimeout: 120 * time.Second}, + Routes: RouteConfig{HA: HARouteConfig{ + ProbeInterval: 10 * time.Second, + ProbeTimeout: 5 * time.Second, + }}, + }, + PreAuthKeys: PreAuthKeysConfig{RevokedRetention: 168 * time.Hour}, + DERP: DERPConfig{ + Server: DERPServerConfig{ + VerifyClients: true, + AutomaticallyAddEmbeddedDerpRegion: true, + }, + UpdateFrequency: 3 * time.Hour, + }, + Database: DatabaseConfig{ + Sqlite: SqliteConfig{ + WriteAheadLog: true, + WALAutoCheckPoint: 1000, // SQLite default + }, + Postgres: PostgresConfig{ + Ssl: "false", + MaxOpenConnections: 10, + MaxIdleConnections: 10, + ConnMaxIdleTimeSecs: 3600, + }, + }, + DNSConfig: DNSConfig{ + MagicDNS: true, + OverrideLocalDNS: true, + Nameservers: Nameservers{Global: []string{}, Split: map[string][]string{}}, + SearchDomains: []string{}, + Challenge: DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, + }, + TLS: TLSConfig{ACME: ACMEConfig{ + ChallengeType: HTTP01ChallengeType, + HTTPListen: ":http", + CacheDir: "/var/lib/headscale/cache", + }}, + OIDC: OIDCConfig{ + OnlyStartIfOIDCIsAvailable: true, + Scope: []string{oidc.ScopeOpenID, "profile", "email"}, + EmailVerifiedRequired: true, + PKCE: PKCEConfig{Method: PKCEMethodS256}, + }, + Taildrop: TaildropConfig{Enabled: true}, + CLI: CLIConfig{Timeout: 5 * time.Second}, + Tuning: Tuning{ + BatchChangeDelay: 800 * time.Millisecond, + NodeMapSessionBufferedChanSize: 30, + NodeStoreBatchSize: defaultNodeStoreBatchSize, + NodeStoreBatchTimeout: 500 * time.Millisecond, + }, } - - expiry, err := model.ParseDuration(value) - if err != nil { - log.Warn(). - Str("value", value). - Msg("failed to parse node.expiry, defaulting to no expiry") - - return 0 - } - - return time.Duration(expiry) } -// validateServerConfig is the test-only entry point. Production code -// goes through LoadServerConfig, which lifts the validator to span -// every config-time sub-builder so the operator sees every problem -// in one render. -func validateServerConfig() error { +// Validate reports every rule c breaks, in one error. It reads no file and +// logs nothing; [Config.Warnings] holds the advisories. +func (c *Config) Validate() error { v := &configValidator{} - validateServerConfigInto(v) - - return v.Err() -} - -var boolConfigKeys = []string{ - "auto_update.enabled", - "cli.insecure", - "database.debug", - "database.gorm.parameterized_queries", - "database.gorm.skip_err_record_not_found", - "database.sqlite.write_ahead_log", - "derp.auto_update_enabled", - "derp.server.automatically_add_embedded_derp_region", - "derp.server.enabled", - "derp.server.verify_clients", - "disable_check_updates", - "dns.certificates.enabled", - "dns.magic_dns", - "dns.override_local_dns", - "logtail.enabled", - "oidc.email_verified_required", - "oidc.only_start_if_oidc_is_available", - "oidc.pkce.enabled", - "oidc.use_expiry_from_token", - "taildrop.enabled", -} - -// Validate raw values before GetBool can discard conversion errors. -func validateBoolConfig(v *configValidator, keys ...string) { - for _, key := range keys { - value := src.Get(key) - if value == nil { - continue - } - - _, err := strconv.ParseBool(fmt.Sprint(value)) - if err != nil { - v.Add(&ConfigError{ - Reason: key + " is not a boolean", - Current: []KV{{key, value}}, - Hint: "use true or false", - Cause: err, - }) - } - } -} - -// validateServerConfigInto runs every config-level validation rule -// against the provided validator. Caller owns the validator and is -// responsible for inspecting v.Err() once all sub-builders have run. -func validateServerConfigInto(v *configValidator) { - validateBoolConfig(v, boolConfigKeys...) // OIDC is activated by setting oidc.issuer (see app.go), not by a // dedicated oidc.enabled key. Gate validation on the real activation // condition so a misconfiguration fails at startup. - if src.String("oidc.issuer") != "" { - err := validateOIDCConfig() + if c.OIDC.Issuer != "" { + err := c.OIDC.validate() if err != nil { v.Add(&ConfigError{ Reason: "OIDC configuration is invalid", @@ -768,32 +835,32 @@ func validateServerConfigInto(v *configValidator) { } } - if src.IsSet("dns.extra_records") && src.IsSet("dns.extra_records_path") { + if len(c.DNSConfig.ExtraRecords) > 0 && c.DNSConfig.ExtraRecordsPath != "" { v.Add(&ConfigError{ Reason: "dns.extra_records and dns.extra_records_path are mutually exclusive", Current: []KV{ - {"dns.extra_records_path", src.String("dns.extra_records_path")}, + {"dns.extra_records_path", c.DNSConfig.ExtraRecordsPath}, {"dns.extra_records", ""}, }, Hint: "keep one (a path is recommended for production); remove the other", }) } - if src.String("tls.acme.hostname") != "" && - (src.String("tls.cert_path") != "" || src.String("tls.key_path") != "") { + acme := c.TLS.ACME + if acme.Hostname != "" && (c.TLS.CertPath != "" || c.TLS.KeyPath != "") { v.Add(&ConfigError{ Reason: "tls.acme.hostname and tls.cert_path/tls.key_path are mutually exclusive", - Current: []KV{{"tls.acme.hostname", src.String("tls.acme.hostname")}}, + Current: []KV{{"tls.acme.hostname", acme.Hostname}}, ConflictsWith: []KV{ - {"tls.cert_path", src.String("tls.cert_path")}, - {"tls.key_path", src.String("tls.key_path")}, + {"tls.cert_path", c.TLS.CertPath}, + {"tls.key_path", c.TLS.KeyPath}, }, Hint: "choose one TLS strategy and unset the other (Let's Encrypt OR a static keypair)", See: "https://headscale.net/stable/ref/tls/", }) } - if src.String("noise.private_key_path") == "" { + if c.Noise.PrivateKeyPath == "" { v.Add(&ConfigError{ Reason: "noise.private_key_path is required", Hint: "set noise.private_key_path: /var/lib/headscale/noise_private.key (or any writable path)", @@ -801,17 +868,9 @@ func validateServerConfigInto(v *configValidator) { }) } - if src.String("tls.acme.hostname") != "" && - ACMEChallenge(src.String("tls.acme.challenge_type")) == TLSALPN01ChallengeType && - !strings.HasSuffix(src.String("listen_addr"), ":443") { - // this is only a warning because there could be something sitting in front of headscale that redirects the traffic (e.g. an iptables rule) - log.Warn(). - Msg("Warning: when using tls.acme.hostname with TLS-ALPN-01 as challenge type, headscale must be reachable on port 443, i.e. listen_addr should probably end in :443") - } + c.validateListenerCollisions(v) - validateListenerCollisions(v) - - if ct := ACMEChallenge(src.String("tls.acme.challenge_type")); ct != HTTP01ChallengeType && + if ct := acme.ChallengeType; ct != HTTP01ChallengeType && ct != TLSALPN01ChallengeType && ct != DNS01ChallengeType { v.Add(&ConfigError{ Reason: "tls.acme.challenge_type has an unsupported value", @@ -821,11 +880,10 @@ func validateServerConfigInto(v *configValidator) { }) } - serverURL := src.String("server_url") - if !strings.HasPrefix(serverURL, "http://") && !strings.HasPrefix(serverURL, "https://") { + if !strings.HasPrefix(c.ServerURL, "http://") && !strings.HasPrefix(c.ServerURL, "https://") { v.Add(&ConfigError{ Reason: "server_url is missing a scheme", - Current: []KV{{"server_url", serverURL}}, + Current: []KV{{"server_url", c.ServerURL}}, Hint: "prefix the URL with https:// (recommended) or http://", }) } @@ -834,8 +892,7 @@ func validateServerConfigInto(v *configValidator) { // to avoid races minInactivityTimeout := 65 * time.Second - ephemeralTimeout := src.Duration("node.ephemeral.inactivity_timeout") - if ephemeralTimeout <= minInactivityTimeout { + if ephemeralTimeout := c.Node.Ephemeral.InactivityTimeout; ephemeralTimeout <= minInactivityTimeout { v.Add(&ConfigError{ Reason: "node.ephemeral.inactivity_timeout is below the minimum", Current: []KV{{"node.ephemeral.inactivity_timeout", ephemeralTimeout.String()}}, @@ -844,47 +901,44 @@ func validateServerConfigInto(v *configValidator) { }) } - if src.Bool("dns.override_local_dns") { - if global := src.StringSlice("dns.nameservers.global"); len(global) == 0 { - v.Add(&ConfigError{ - Reason: "dns.nameservers.global is required when dns.override_local_dns is true", - Current: []KV{ - {"dns.override_local_dns", true}, - {"dns.nameservers.global", "[]"}, - }, - Hint: "list at least one upstream nameserver, or set dns.override_local_dns: false", - See: dnsDocsURL, - }) - } + if c.DNSConfig.OverrideLocalDNS && len(c.DNSConfig.Nameservers.Global) == 0 { + v.Add(&ConfigError{ + Reason: "dns.nameservers.global is required when dns.override_local_dns is true", + Current: []KV{ + {"dns.override_local_dns", true}, + {"dns.nameservers.global", "[]"}, + }, + Hint: "list at least one upstream nameserver, or set dns.override_local_dns: false", + See: dnsDocsURL, + }) } // Validate HA health probing parameters - if haInterval := src.Duration("node.routes.ha.probe_interval"); haInterval > 0 { - if haInterval < 2*time.Second { + if ha := c.Node.Routes.HA; ha.ProbeInterval > 0 { + if ha.ProbeInterval < 2*time.Second { v.Add(&ConfigError{ Reason: "node.routes.ha.probe_interval is below the minimum", - Current: []KV{{"node.routes.ha.probe_interval", haInterval.String()}}, + Current: []KV{{"node.routes.ha.probe_interval", ha.ProbeInterval.String()}}, Minimum: "2s", Hint: "raise the value to at least the minimum", }) } - haTimeout := src.Duration("node.routes.ha.probe_timeout") - if haTimeout < 1*time.Second { + if ha.ProbeTimeout < 1*time.Second { v.Add(&ConfigError{ Reason: "node.routes.ha.probe_timeout is below the minimum", - Current: []KV{{"node.routes.ha.probe_timeout", haTimeout.String()}}, + Current: []KV{{"node.routes.ha.probe_timeout", ha.ProbeTimeout.String()}}, Minimum: "1s", Hint: "raise the value to at least the minimum", }) } - if haTimeout >= haInterval { + if ha.ProbeTimeout >= ha.ProbeInterval { v.Add(&ConfigError{ Reason: "node.routes.ha.probe_timeout must be less than node.routes.ha.probe_interval", Current: []KV{ - {"node.routes.ha.probe_timeout", haTimeout.String()}, - {"node.routes.ha.probe_interval", haInterval.String()}, + {"node.routes.ha.probe_timeout", ha.ProbeTimeout.String()}, + {"node.routes.ha.probe_interval", ha.ProbeInterval.String()}, }, Hint: "lower probe_timeout below probe_interval (a probe must finish before the next one starts)", }) @@ -892,7 +946,7 @@ func validateServerConfigInto(v *configValidator) { } // Validate tuning parameters - if size := src.Int("tuning.node_store_batch_size"); size <= 0 { + if size := c.Tuning.NodeStoreBatchSize; size <= 0 { v.Add(&ConfigError{ Reason: "tuning.node_store_batch_size must be positive", Current: []KV{{"tuning.node_store_batch_size", size}}, @@ -900,7 +954,7 @@ func validateServerConfigInto(v *configValidator) { }) } - if timeout := src.Duration("tuning.node_store_batch_timeout"); timeout <= 0 { + if timeout := c.Tuning.NodeStoreBatchTimeout; timeout <= 0 { v.Add(&ConfigError{ Reason: "tuning.node_store_batch_timeout must be positive", Current: []KV{{"tuning.node_store_batch_timeout", timeout.String()}}, @@ -908,17 +962,35 @@ func validateServerConfigInto(v *configValidator) { }) } - validateDERPConfig(v) - validateDatabaseConfig(v) - validatePolicyConfig(v) - validateMagicDNSConfig(v) - validateCertificatesConfig(v) + c.validateDERP(v) + c.validateDatabase(v) + c.validatePolicy(v) + c.validateMagicDNS(v) + c.validateCertificates(v) + c.validatePrefixes(v) + c.validateDNSProvider(v) + + // BaseDomain cannot be the same as the server URL. + // This is because Tailscale takes over the domain in BaseDomain, + // causing the headscale server and DERP to be unreachable. + // For Tailscale upstream, the following is true: + // - DERP run on their own domains + // - Control plane runs on login.tailscale.com/controlplane.tailscale.com + // - MagicDNS (BaseDomain) for users is on a *.ts.net domain per tailnet (e.g. tail-scale.ts.net) + if base := c.DNSConfig.BaseDomain; base != "" { + err := isSafeServerURL(c.ServerURL, base) + if err != nil { + addServerURLConfigError(v, c.ServerURL, base, err) + } + } + + return v.Err() } -// validatePolicyConfig records a ConfigError for an unknown policy.mode: +// validatePolicy records a ConfigError for an unknown policy.mode: // startup would load no policy at all and allow all traffic. -func validatePolicyConfig(v *configValidator) { - m := PolicyMode(src.String("policy.mode")) +func (c *Config) validatePolicy(v *configValidator) { + m := c.Policy.Mode if m == PolicyModeFile || m == PolicyModeDB { return } @@ -931,14 +1003,15 @@ func validatePolicyConfig(v *configValidator) { }) } -// validateDERPConfig records ConfigErrors when the embedded DERP server -// is enabled without the addresses or paths it needs. -func validateDERPConfig(v *configValidator) { - if !src.Bool("derp.server.enabled") { +// validateDERP records ConfigErrors when the embedded DERP server is +// enabled without the addresses or paths it needs. +func (c *Config) validateDERP(v *configValidator) { + server := c.DERP.Server + if !server.Enabled { return } - if src.String("derp.server.stun_listen_addr") == "" { + if server.STUNAddr == "" { v.Add(&ConfigError{ Reason: "derp.server.stun_listen_addr is required when the embedded DERP server is enabled", Current: []KV{ @@ -950,7 +1023,7 @@ func validateDERPConfig(v *configValidator) { }) } - derpKey, noiseKey := src.String("derp.server.private_key_path"), src.String("noise.private_key_path") + derpKey, noiseKey := server.PrivateKeyPath, c.Noise.PrivateKeyPath switch { case derpKey == "": v.Add(&ConfigError{ @@ -975,8 +1048,7 @@ func validateDERPConfig(v *configValidator) { }) } - if !src.Bool("derp.server.automatically_add_embedded_derp_region") && - len(src.StringSlice("derp.paths")) == 0 { + if !server.AutomaticallyAddEmbeddedDerpRegion && len(c.DERP.Paths) == 0 { v.Add(&ConfigError{ Reason: "derp.paths is required when derp.server.automatically_add_embedded_derp_region is false", Current: []KV{ @@ -988,10 +1060,10 @@ func validateDERPConfig(v *configValidator) { } } -// validateDatabaseConfig records a ConfigError when database.type is not -// one of the supported backends. -func validateDatabaseConfig(v *configValidator) { - t := DatabaseType(src.String("database.type")) +// validateDatabase records a ConfigError when database.type is not one of +// the supported backends. +func (c *Config) validateDatabase(v *configValidator) { + t := c.Database.Type switch t { case DatabaseSqlite, DatabasePostgres, "sqlite": return @@ -1006,14 +1078,10 @@ func validateDatabaseConfig(v *configValidator) { }) } -// validateMagicDNSConfig records a ConfigError when MagicDNS is enabled -// without a base_domain. -func validateMagicDNSConfig(v *configValidator) { - if !src.Bool("dns.magic_dns") { - return - } - - if src.String("dns.base_domain") == "" { +// validateMagicDNS records a ConfigError when MagicDNS is enabled without a +// base_domain. +func (c *Config) validateMagicDNS(v *configValidator) { + if c.DNSConfig.MagicDNS && c.DNSConfig.BaseDomain == "" { v.Add(&ConfigError{ Reason: "dns.base_domain is required when dns.magic_dns is true", Current: []KV{ @@ -1026,11 +1094,12 @@ func validateMagicDNSConfig(v *configValidator) { } } -// validateCertificatesConfig records ConfigErrors when node certificates -// are enabled without what the Tailscale client needs: a MagicDNS name -// it can match exactly, and a provider to publish the challenge. -func validateCertificatesConfig(v *configValidator) { - if timeout := src.Duration("dns.challenge.propagation_timeout"); timeout < 0 { +// validateCertificates records ConfigErrors when node certificates are +// enabled without what the Tailscale client needs: a MagicDNS name it can +// match exactly, and a provider to publish the challenge. +func (c *Config) validateCertificates(v *configValidator) { + ch := c.DNSConfig.Challenge + if timeout := ch.PropagationTimeout; timeout < 0 { v.Add(&ConfigError{ Reason: "dns.challenge.propagation_timeout must not be negative", Current: []KV{{"dns.challenge.propagation_timeout", timeout.String()}}, @@ -1038,11 +1107,11 @@ func validateCertificatesConfig(v *configValidator) { }) } - enabled := src.Bool("dns.certificates.enabled") - challengeType := ACMEChallenge(src.String("tls.acme.challenge_type")) - ownDNS01 := src.String("tls.acme.hostname") != "" && challengeType == DNS01ChallengeType + enabled := c.DNSConfig.Certificates.Enabled + challengeType := c.TLS.ACME.ChallengeType + ownDNS01 := c.TLS.ACME.Hostname != "" && challengeType == DNS01ChallengeType - if (enabled || ownDNS01) && src.String("dns.challenge.provider.name") == "" { + if name, ok := ch.providerName(); (enabled || ownDNS01) && ok && name == "" && ch.Provider == nil { v.Add(&ConfigError{ Reason: "dns.certificates and tls.acme DNS-01 require dns.challenge.provider", Current: []KV{{"dns.certificates.enabled", enabled}, {"tls.acme.challenge_type", string(challengeType)}}, @@ -1056,7 +1125,7 @@ func validateCertificatesConfig(v *configValidator) { return } - if !src.Bool("dns.magic_dns") { + if !c.DNSConfig.MagicDNS { v.Add(&ConfigError{ Reason: "dns.certificates requires dns.magic_dns", Current: []KV{{"dns.certificates.enabled", true}, {"dns.magic_dns", false}}, @@ -1065,7 +1134,7 @@ func validateCertificatesConfig(v *configValidator) { }) } - base := strings.TrimSuffix(src.String("dns.base_domain"), ".") + base := strings.TrimSuffix(c.DNSConfig.BaseDomain, ".") if base != strings.ToLower(base) || dnsname.ValidHostname(base) != nil { v.Add(&ConfigError{ Reason: "dns.base_domain must be a lowercase DNS name for dns.certificates", @@ -1075,7 +1144,7 @@ func validateCertificatesConfig(v *configValidator) { }) } - zone := strings.TrimSuffix(src.String("dns.certificates.zone"), ".") + zone := strings.TrimSuffix(c.DNSConfig.Certificates.Zone, ".") if zone != "" && base != zone && !strings.HasSuffix(base, "."+zone) { v.Add(&ConfigError{ Reason: "dns.base_domain is not inside dns.certificates.zone", @@ -1086,200 +1155,145 @@ func validateCertificatesConfig(v *configValidator) { } } -// dnsChallengeConfig reads dns.challenge and builds the provider it names, -// its secrets unread, only to report bad settings; startup builds the one -// it uses. -func dnsChallengeConfig() (DNSChallengeConfig, error) { - cfg := DNSChallengeConfig{ - PropagationTimeout: src.Duration("dns.challenge.propagation_timeout"), - Resolvers: src.StringSlice("dns.challenge.resolvers"), - ProviderSettings: src.StringMap("dns.challenge.provider"), +// validatePrefixes records ConfigErrors for a catch-all trusted proxy, no +// node range, and no allocation strategy. +func (c *Config) validatePrefixes(v *configValidator) { + // 0.0.0.0/0 and ::/0 defeat the peer-trust gate and almost always + // indicate misconfiguration. + for i, p := range c.TrustedProxies { + if p.Bits() == 0 { + err := fmt.Errorf("trusted_proxies[%d] %q: %w", i, p.String(), errTrustedProxyZeroRange) + v.Add(&ConfigError{ + Reason: "trusted_proxies contains an invalid CIDR", + Current: []KV{{"trusted_proxies", c.TrustedProxies}}, + Detail: err.Error(), + Hint: "use specific proxy CIDRs; catch-all ranges are not allowed", + Cause: err, + }) + + break + } } - if cfg.ProviderName() == "" { - return cfg, nil + if c.Prefixes.V4 == nil && c.Prefixes.V6 == nil { + v.Add(&ConfigError{ + Reason: "no IP prefix configured for the tailnet", + Current: []KV{ + {"prefixes.v4", ""}, + {"prefixes.v6", ""}, + }, + Hint: `set at least one of prefixes.v4 (default: "100.64.0.0/10") or prefixes.v6 (default: "fd7a:115c:a1e0::/48")`, + Cause: ErrNoPrefixConfigured, + }) } - provider, err := cfg.NewProvider(keepSecret) - if err != nil { - return cfg, err + switch alloc := c.Prefixes.Allocation; alloc { + case IPAllocationStrategySequential, IPAllocationStrategyRandom: + default: + v.Add(&ConfigError{ + Reason: "prefixes.allocation has an unsupported value", + Current: []KV{{"prefixes.allocation", string(alloc)}}, + Allowed: alloc.Allowed(), + Hint: "pick one of the allowed values; sequential is the default", + Cause: ErrInvalidAllocationStrategy, + }) + } +} + +// validateDNSProvider builds the provider dns.challenge names, its secrets +// unread, only to report bad settings; startup builds the one it uses. A +// Go-only provider is used as it is. +func (c *Config) validateDNSProvider(v *configValidator) { + ch := c.DNSConfig.Challenge + if ch.Provider != nil { + return } + name, ok := ch.providerName() + if !ok { + v.Add(&ConfigError{ + Reason: "dns.challenge.provider.name must be a provider name", + Allowed: hsdns.ProviderNames(), + See: dnsDocsURL, + }) + + return + } + + if name == "" { + return + } + + provider, err := ch.NewProvider(keepSecret) // Only node certificates let a client pick what is published; a record // shared by headscale's own certificate alone is harmless. - if iso, ok := provider.(hsdns.Isolator); ok && src.Bool("dns.certificates.enabled") { + if iso, ok := provider.(hsdns.Isolator); ok && err == nil && c.DNSConfig.Certificates.Enabled { err = iso.CheckIsolation() if err != nil { - return cfg, fmt.Errorf("%w for %s: %w", hsdns.ErrProviderConfig, cfg.ProviderName(), err) + err = fmt.Errorf("%w for %s: %w", hsdns.ErrProviderConfig, ch.ProviderName(), err) } } - return cfg, nil + if err == nil { + return + } + + // The provider names only help when the name is what is wrong. + var allowed []string + if errors.Is(err, hsdns.ErrUnknownProvider) { + allowed = hsdns.ProviderNames() + } + + v.Add(&ConfigError{ + Reason: "dns.challenge.provider is invalid", + Current: []KV{{"dns.challenge.provider.name", ch.ProviderName()}}, + Detail: err.Error(), + Allowed: allowed, + Hint: `check the provider name and its settings; any string setting can be read from a file with "file:"`, + See: dnsDocsURL, + Cause: err, + }) } -func tlsConfig() TLSConfig { - return TLSConfig{ - CertPath: src.String("tls.cert_path"), - KeyPath: src.String("tls.key_path"), - ACME: ACMEConfig{ - Hostname: src.String("tls.acme.hostname"), - ChallengeType: ACMEChallenge(src.String("tls.acme.challenge_type")), - HTTPListen: ListenAddr(src.String("tls.acme.http_listen")), - URL: HTTPURL(src.String("tls.acme.url")), - Email: src.String("tls.acme.email"), - CacheDir: src.String("tls.acme.cache_dir"), - }, +// Warnings are advisories about c that do not stop headscale. +func (c *Config) Warnings() []string { + var warnings []string + + if c.TLS.ACME.Hostname != "" && c.TLS.ACME.ChallengeType == TLSALPN01ChallengeType && + !strings.HasSuffix(string(c.Addr), ":443") { + // Only a warning: something in front of headscale may redirect the + // traffic (e.g. an iptables rule). + warnings = append(warnings, "Warning: when using tls.acme.hostname with TLS-ALPN-01 as challenge type, headscale must be reachable on port 443, i.e. listen_addr should probably end in :443") } + + if v4, v6 := c.Prefixes.V4, c.Prefixes.V6; (v4 != nil && outsideRange(*v4, tsaddr.CGNATRange())) || + (v6 != nil && outsideRange(*v6, tsaddr.TailscaleULARange())) { + warnings = append(warnings, bannerText([]string{ + "You have overridden the default Headscale IP prefixes", + "with a range outside of the standard CGNAT and/or ULA", + "ranges. This is NOT a supported configuration.", + "", + "Using subsets of the default ranges (100.64.0.0/10 for", + "IPv4, fd7a:115c:a1e0::/48 for IPv6) is fine. Using", + "ranges outside of these will cause undefined behaviour", + "as the Tailscale client is NOT designed to operate on", + "any other ranges.", + "", + "Please revert your prefixes to subsets of the standard", + "ranges as described in the example configuration.", + "", + "Any issue raised using a range outside of the", + "supported range will be labelled as wontfix", + "and closed.", + })) + } + + return append(warnings, c.DNSConfig.nameserverWarnings()...) } -func derpConfig() DERPConfig { - urlStrs := src.StringSlice("derp.urls") - - urls := make([]HTTPURL, 0, len(urlStrs)) - for _, urlStr := range urlStrs { - _, err := url.Parse(urlStr) - if err != nil { - log.Error(). - Caller(). - Str("url", urlStr). - Err(err). - Msg("Failed to parse url, ignoring...") - - continue - } - - urls = append(urls, HTTPURL(urlStr)) - } - - return DERPConfig{ - Server: DERPServerConfig{ - Enabled: src.Bool("derp.server.enabled"), - RegionID: tailcfg.DERPRegionID(src.Int64("derp.server.region_id")), - RegionCode: src.String("derp.server.region_code"), - RegionName: src.String("derp.server.region_name"), - VerifyClients: src.Bool("derp.server.verify_clients"), - STUNAddr: ListenAddr(src.String("derp.server.stun_listen_addr")), - PrivateKeyPath: src.String("derp.server.private_key_path"), - IPv4: src.String("derp.server.ipv4"), - IPv6: src.String("derp.server.ipv6"), - AutomaticallyAddEmbeddedDerpRegion: src.Bool( - "derp.server.automatically_add_embedded_derp_region", - ), - }, - URLs: urls, - Paths: src.StringSlice("derp.paths"), - AutoUpdate: src.Bool("derp.auto_update_enabled"), - UpdateFrequency: src.Duration("derp.update_frequency"), - } -} - -func logtailConfig() LogTailConfig { - enabled := src.Bool("logtail.enabled") - - return LogTailConfig{ - Enabled: enabled, - } -} - -func policyConfig() PolicyConfig { - policyPath := src.String("policy.path") - policyMode := src.String("policy.mode") - - return PolicyConfig{ - Path: policyPath, - Mode: PolicyMode(policyMode), - } -} - -// logConfig keeps log.level as written; [LogLevel.Zerolog] reads one it -// does not know as debug. -func logConfig() LogConfig { - logFormatOpt := LogFormat(src.String("log.format")) - - var logFormat LogFormat - - switch logFormatOpt { - case JSONLogFormat: - logFormat = JSONLogFormat - case TextLogFormat: - logFormat = TextLogFormat - case "": - logFormat = TextLogFormat - default: - log.Error(). - Caller(). - Str("func", "GetLogConfig"). - Msgf("Could not parse log format: %s. Valid choices are 'json' or 'text'", logFormatOpt) - } - - return LogConfig{ - Level: LogLevel(src.String("log.level")), - Format: logFormat, - } -} - -func databaseConfig() DatabaseConfig { - return DatabaseConfig{ - Type: DatabaseType(src.String("database.type")), - Debug: src.Bool("database.debug"), - Gorm: GormConfig{ - SkipErrRecordNotFound: src.Bool("database.gorm.skip_err_record_not_found"), - SlowThreshold: src.Int("database.gorm.slow_threshold"), - ParameterizedQueries: src.Bool("database.gorm.parameterized_queries"), - }, - Sqlite: SqliteConfig{ - Path: src.String("database.sqlite.path"), - WriteAheadLog: src.Bool("database.sqlite.write_ahead_log"), - WALAutoCheckPoint: src.Int("database.sqlite.wal_autocheckpoint"), - }, - Postgres: PostgresConfig{ - Host: src.String("database.postgres.host"), - Port: src.Int("database.postgres.port"), - Name: src.String("database.postgres.name"), - User: src.String("database.postgres.user"), - Pass: Secret(src.String("database.postgres.pass")), - Ssl: src.String("database.postgres.ssl"), - MaxOpenConnections: src.Int("database.postgres.max_open_conns"), - MaxIdleConnections: src.Int("database.postgres.max_idle_conns"), - ConnMaxIdleTimeSecs: src.Int( - "database.postgres.conn_max_idle_time_secs", - ), - }, - } -} - -func dns() (DNSConfig, error) { - var dns DNSConfig - - // TODO: Use this instead of manually getting settings when - // UnmarshalKey is compatible with Environment Variables. - // err := src.UnmarshalKey("dns", &dns) - // if err != nil { - // return DNSConfig{}, fmt.Errorf("unmarshalling dns config: %w", err) - // } - - dns.MagicDNS = src.Bool("dns.magic_dns") - dns.BaseDomain = src.String("dns.base_domain") - dns.OverrideLocalDNS = src.Bool("dns.override_local_dns") - dns.Nameservers.Global = src.StringSlice("dns.nameservers.global") - dns.Nameservers.Split = src.StringMapStringSlice("dns.nameservers.split") - dns.SearchDomains = src.StringSlice("dns.search_domains") - dns.ExtraRecordsPath = src.String("dns.extra_records_path") - dns.Certificates.Enabled = src.Bool("dns.certificates.enabled") - dns.Certificates.Zone = src.String("dns.certificates.zone") - - if src.IsSet("dns.extra_records") { - var extraRecords []tailcfg.DNSRecord - - err := src.UnmarshalKey("dns.extra_records", &extraRecords) - if err != nil { - return DNSConfig{}, fmt.Errorf("unmarshalling dns extra records: %w", err) - } - - dns.ExtraRecords = extraRecords - } - - return dns, nil +// outsideRange reports whether p holds addresses outside r. +func outsideRange(p, r netip.Prefix) bool { + return p.Bits() < r.Bits() || !r.Contains(p.Addr()) } // parseResolvers converts nameserver strings into DNS resolvers: an IP is a @@ -1312,7 +1326,7 @@ func (d DNSConfig) nameserverWarnings() []string { check := func(key string, nameservers []string) { for _, ns := range nameservers { if !validNameserver(ns) { - warnings = append(warnings, fmt.Sprintf("invalid nameserver %q in %s, ignoring", ns, key)) + warnings = append(warnings, fmt.Sprintf("invalid nameserver %q in %s, ignoring", redactedURL(ns), key)) } } } @@ -1381,16 +1395,12 @@ func (c *Config) TailcfgDNSConfig() *tailcfg.DNSConfig { // Clients route reverse lookups for the Tailscale ranges themselves // (magicDNSRootDomains in tailscale.com/ipn/ipnlocal) and SaaS sends // no reverse routes, so only a prefix outside those ranges needs them. - outside := func(p, r netip.Prefix) bool { - return p.Bits() < r.Bits() || !r.Contains(p.Addr()) - } - var reverse []dnsname.FQDN - if v4 := c.Prefixes.V4; v4 != nil && outside(*v4, tsaddr.CGNATRange()) { + if v4 := c.Prefixes.V4; v4 != nil && outsideRange(*v4, tsaddr.CGNATRange()) { reverse = append(reverse, util.GenerateIPv4DNSRootDomain(*v4)...) } - if v6 := c.Prefixes.V6; v6 != nil && outside(*v6, tsaddr.TailscaleULARange()) { + if v6 := c.Prefixes.V6; v6 != nil && outsideRange(*v6, tsaddr.TailscaleULARange()) { reverse = append(reverse, util.GenerateIPv6DNSRootDomain(*v6)...) } @@ -1412,10 +1422,9 @@ func (c *Config) TailcfgDNSConfig() *tailcfg.DNSConfig { return cfg } -// warnBanner prints a highly visible warning banner to the log output. -// It wraps the provided lines in an ASCII-art box with a "Warning!" header. -// This is intended for critical configuration issues that users must not ignore. -func warnBanner(lines []string) { +// bannerText wraps lines in an ASCII-art box with a "Warning!" header, for +// critical configuration issues that users must not ignore. +func bannerText(lines []string) string { var b strings.Builder b.WriteString("\n") @@ -1438,361 +1447,56 @@ func warnBanner(lines []string) { b.WriteString("### ###\n") b.WriteString("################################################################") - log.Warn().Msg(b.String()) + return b.String() } -func parsePrefixConfig(key string, standardRange netip.Prefix, family string) (*netip.Prefix, bool, error) { - s := src.String(key) - - if s == "" { - return nil, false, nil - } - - prefix, err := netip.ParsePrefix(s) - if err != nil { - return nil, false, fmt.Errorf("parsing %s prefix from config: %w", family, err) - } - - builder := netipx.IPSetBuilder{} - builder.AddPrefix(standardRange) - - ipSet, _ := builder.IPSet() - - return &prefix, !ipSet.ContainsPrefix(prefix), nil -} - -// trustedProxies rejects 0.0.0.0/0 and ::/0 because they defeat the -// peer-trust gate and almost always indicate misconfiguration. -func trustedProxies() ([]netip.Prefix, error) { - raw := src.StringSlice("trusted_proxies") - if len(raw) == 0 { - return nil, nil - } - - out := make([]netip.Prefix, 0, len(raw)) - for i, s := range raw { - p, err := netip.ParsePrefix(s) - if err != nil { - return nil, fmt.Errorf("trusted_proxies[%d] %q: %w", i, s, err) - } - - if p.Bits() == 0 { - return nil, fmt.Errorf("trusted_proxies[%d] %q: %w", i, s, errTrustedProxyZeroRange) - } - - out = append(out, p.Masked()) - } - - return out, nil -} - -// LoadCLIConfig returns the needed configuration for the CLI client -// of Headscale to connect to a Headscale server. +// LoadCLIConfig decodes the configuration [LoadConfig] loaded, for client +// commands, which need no server rule to hold. func LoadCLIConfig() (*Config, error) { if src == nil { return nil, errConfigNotLoaded } - v := &configValidator{} - validateBoolConfig(v, "cli.insecure", "disable_check_updates") - - err := v.Err() + cfg, err := src.decode() if err != nil { return nil, err } - logConfig := logConfig() - zerolog.SetGlobalLevel(logConfig.Level.Zerolog()) + zerolog.SetGlobalLevel(cfg.Log.Level.Zerolog()) - return &Config{ - DisableUpdateCheck: src.Bool("disable_check_updates"), - UnixSocket: src.String("unix_socket"), - CLI: CLIConfig{ - Address: src.String("cli.address"), - APIKey: Secret(src.String("cli.api_key")), - Timeout: src.Duration("cli.timeout"), - Insecure: src.Bool("cli.insecure"), - }, - Log: logConfig, - }, nil + return cfg, nil } -// LoadServerConfig returns the full Headscale configuration to host a -// Headscale server. It reads no file: secret; each consumer reads its own. +// LoadServerConfig decodes the configuration [LoadConfig] loaded and, when +// every value decoded, validates it and logs its warnings. It reads no +// file: secret; each consumer reads its own. func LoadServerConfig() (*Config, error) { if src == nil { return nil, errConfigNotLoaded } - v := &configValidator{} - validateServerConfigInto(v) - - logConfig := logConfig() - zerolog.SetGlobalLevel(logConfig.Level.Zerolog()) - - prefix4, v4NonStandard, err := parsePrefixConfig("prefixes.v4", tsaddr.CGNATRange(), "IPv4") - if err != nil { - v.Add(&ConfigError{ - Reason: "prefixes.v4 is not a valid CIDR", - Current: []KV{{"prefixes.v4", src.String("prefixes.v4")}}, - Detail: err.Error(), - Hint: `use CIDR form, e.g. "100.64.0.0/10" (CGNAT, headscale default)`, - Cause: err, - }) - } - - prefix6, v6NonStandard, err := parsePrefixConfig("prefixes.v6", tsaddr.TailscaleULARange(), "IPv6") - if err != nil { - v.Add(&ConfigError{ - Reason: "prefixes.v6 is not a valid CIDR", - Current: []KV{{"prefixes.v6", src.String("prefixes.v6")}}, - Detail: err.Error(), - Hint: `use CIDR form, e.g. "fd7a:115c:a1e0::/48" (Tailscale ULA, headscale default)`, - Cause: err, - }) - } - - trusted, err := trustedProxies() - if err != nil { - v.Add(&ConfigError{ - Reason: "trusted_proxies contains an invalid CIDR", - Current: []KV{{"trusted_proxies", src.StringSlice("trusted_proxies")}}, - Detail: err.Error(), - Hint: "use specific proxy CIDRs; catch-all ranges are not allowed", - Cause: err, - }) - } - - if prefix4 == nil && prefix6 == nil { - v.Add(&ConfigError{ - Reason: "no IP prefix configured for the tailnet", - Current: []KV{ - {"prefixes.v4", src.String("prefixes.v4")}, - {"prefixes.v6", src.String("prefixes.v6")}, - }, - Hint: `set at least one of prefixes.v4 (default: "100.64.0.0/10") or prefixes.v6 (default: "fd7a:115c:a1e0::/48")`, - Cause: ErrNoPrefixConfigured, - }) - } - - if v4NonStandard || v6NonStandard { - warnBanner([]string{ - "You have overridden the default Headscale IP prefixes", - "with a range outside of the standard CGNAT and/or ULA", - "ranges. This is NOT a supported configuration.", - "", - "Using subsets of the default ranges (100.64.0.0/10 for", - "IPv4, fd7a:115c:a1e0::/48 for IPv6) is fine. Using", - "ranges outside of these will cause undefined behaviour", - "as the Tailscale client is NOT designed to operate on", - "any other ranges.", - "", - "Please revert your prefixes to subsets of the standard", - "ranges as described in the example configuration.", - "", - "Any issue raised using a range outside of the", - "supported range will be labelled as wontfix", - "and closed.", - }) - } - - allocStr := src.String("prefixes.allocation") - - var alloc IPAllocationStrategy - - switch allocStr { - case string(IPAllocationStrategySequential): - alloc = IPAllocationStrategySequential - case string(IPAllocationStrategyRandom): - alloc = IPAllocationStrategyRandom - default: - v.Add(&ConfigError{ - Reason: "prefixes.allocation has an unsupported value", - Current: []KV{{"prefixes.allocation", allocStr}}, - Allowed: []string{ - string(IPAllocationStrategySequential), - string(IPAllocationStrategyRandom), - }, - Hint: "pick one of the allowed values; sequential is the default", - Cause: ErrInvalidAllocationStrategy, - }) - } - - // YAML reads an unquoted 0770 as the integer 504, which cannot be told - // apart from a literal 504, so only a string is accepted. - socketPermRaw := src.Get("unix_socket_permission") - socketPermStr, _ := socketPermRaw.(string) - - socketPerm, err := util.ParseFileMode(socketPermStr) - if err != nil { - v.Add(&ConfigError{ - Reason: "unix_socket_permission is not a quoted octal file mode", - Current: []KV{{"unix_socket_permission", socketPermRaw}}, - Maximum: `"0777"`, - Hint: `quote the value, e.g. "0770"; unquoted, YAML reads 0770 as the number 504`, - Cause: err, - }) - } - - dnsConfig, err := dns() - if err != nil { - v.Add(&ConfigError{ - Reason: "dns.extra_records cannot be parsed", - Current: []KV{{"dns.extra_records", ""}}, - Detail: err.Error(), - Hint: "check the YAML syntax; see config-example.yaml for the expected shape", - Cause: err, - }) - } - - for _, w := range dnsConfig.nameserverWarnings() { - log.Warn().Msg(w) - } - - dir := configDir() - derpConfig := derpConfig() - logTailConfig := logtailConfig() - - dnsChallenge, err := dnsChallengeConfig() - if err != nil { - // The provider names only help when the name is what is wrong. - var allowed []string - if errors.Is(err, hsdns.ErrUnknownProvider) { - allowed = hsdns.ProviderNames() - } - - v.Add(&ConfigError{ - Reason: "dns.challenge.provider is invalid", - Current: []KV{{"dns.challenge.provider.name", dnsChallenge.ProviderName()}}, - Detail: err.Error(), - Allowed: allowed, - Hint: `check the provider name and its settings; any string setting can be read from a file with "file:"`, - See: dnsDocsURL, - Cause: err, - }) - } - - dnsConfig.Challenge = dnsChallenge - database := databaseConfig() - - serverURL := strings.TrimRight(src.String("server_url"), "/") - - // BaseDomain cannot be the same as the server URL. - // This is because Tailscale takes over the domain in BaseDomain, - // causing the headscale server and DERP to be unreachable. - // For Tailscale upstream, the following is true: - // - DERP run on their own domains - // - Control plane runs on login.tailscale.com/controlplane.tailscale.com - // - MagicDNS (BaseDomain) for users is on a *.ts.net domain per tailnet (e.g. tail-scale.ts.net) - if dnsConfig.BaseDomain != "" { - err := isSafeServerURL(serverURL, dnsConfig.BaseDomain) - if err != nil { - addServerURLConfigError(v, serverURL, dnsConfig.BaseDomain, err) - } - } - - err = v.Err() + cfg, err := src.decode() if err != nil { return nil, err } - return &Config{ - ServerURL: serverURL, - Addr: ListenAddr(src.String("listen_addr")), - MetricsAddr: ListenAddr(src.String("metrics_listen_addr")), - TrustedProxies: trusted, - ConfigDir: dir, - DisableUpdateCheck: src.Bool("disable_check_updates"), + zerolog.SetGlobalLevel(cfg.Log.Level.Zerolog()) - Prefixes: PrefixesConfig{ - V4: prefix4, - V6: prefix6, - Allocation: alloc, - }, + err = cfg.Validate() - Noise: NoiseConfig{PrivateKeyPath: src.String("noise.private_key_path")}, + for _, w := range cfg.Warnings() { + log.Warn().Msg(w) + } - DERP: derpConfig, + if err != nil { + return nil, err + } - Node: NodeConfig{ - Expiry: resolveNodeExpiry(), - Ephemeral: EphemeralConfig{ - InactivityTimeout: src.Duration("node.ephemeral.inactivity_timeout"), - }, - Routes: RouteConfig{ - HA: HARouteConfig{ - ProbeInterval: src.Duration("node.routes.ha.probe_interval"), - ProbeTimeout: src.Duration("node.routes.ha.probe_timeout"), - }, - }, - }, + // The server never uses the CLI's key, which may be an unread file: + // reference. + cfg.CLI.APIKey = "" - PreAuthKeys: PreAuthKeysConfig{ - RevokedRetention: src.Duration("preauth_keys.revoked_retention"), - }, - - Database: database, - - TLS: tlsConfig(), - - DNSConfig: dnsConfig, - - UnixSocket: src.String("unix_socket"), - UnixSocketPermission: FileMode(socketPerm), - - OIDC: OIDCConfig{ - OnlyStartIfOIDCIsAvailable: src.Bool( - "oidc.only_start_if_oidc_is_available", - ), - Issuer: HTTPURL(src.String("oidc.issuer")), - ClientID: src.String("oidc.client_id"), - ClientSecret: Secret(src.String("oidc.client_secret")), - Scope: src.StringSlice("oidc.scope"), - ExtraParams: src.StringMapString("oidc.extra_params"), - AllowedDomains: src.StringSlice("oidc.allowed_domains"), - AllowedUsers: src.StringSlice("oidc.allowed_users"), - AllowedGroups: src.StringSlice("oidc.allowed_groups"), - EmailVerifiedRequired: src.Bool("oidc.email_verified_required"), - UseExpiryFromToken: src.Bool("oidc.use_expiry_from_token"), - PKCE: PKCEConfig{ - Enabled: src.Bool("oidc.pkce.enabled"), - Method: PKCEMethod(src.String("oidc.pkce.method")), - }, - }, - - LogTail: logTailConfig, - Taildrop: TaildropConfig{ - Enabled: src.Bool("taildrop.enabled"), - }, - AutoUpdate: AutoUpdateConfig{ - Enabled: src.Bool("auto_update.enabled"), - }, - - Policy: policyConfig(), - - // No APIKey: the server never uses the CLI's key, which may be an - // unresolved file: reference here. - CLI: CLIConfig{ - Address: src.String("cli.address"), - Timeout: src.Duration("cli.timeout"), - Insecure: src.Bool("cli.insecure"), - }, - - Log: logConfig, - - Tuning: Tuning{ - NotifierSendTimeout: src.Duration("tuning.notifier_send_timeout"), - BatchChangeDelay: src.Duration("tuning.batch_change_delay"), - NodeMapSessionBufferedChanSize: src.Int( - "tuning.node_mapsession_buffered_chan_size", - ), - BatcherWorkers: src.Int("tuning.batcher_workers"), - RegisterCacheExpiration: src.Duration("tuning.register_cache_expiration"), - RegisterCacheMaxEntries: src.Int("tuning.register_cache_max_entries"), - NodeStoreBatchSize: src.Int("tuning.node_store_batch_size"), - NodeStoreBatchTimeout: src.Duration("tuning.node_store_batch_timeout"), - }, - }, nil + return cfg, nil } // addServerURLConfigError pushes a structured ConfigError onto v for diff --git a/hscontrol/types/config_errors.go b/hscontrol/types/config_errors.go index c8b82ee0c..dda2d8649 100644 --- a/hscontrol/types/config_errors.go +++ b/hscontrol/types/config_errors.go @@ -1,6 +1,7 @@ package types import ( + "encoding/json" "errors" "fmt" "strings" @@ -113,6 +114,14 @@ func formatKVValue(v any) string { return `""` case string: return fmt.Sprintf("%q", x) + case []any, map[string]any: + // A value as decoding found it, shown as JSON. + b, err := json.Marshal(x) + if err == nil { + return string(b) + } + + return fmt.Sprintf("%v", x) default: return fmt.Sprintf("%v", x) } diff --git a/hscontrol/types/config_fields_test.go b/hscontrol/types/config_fields_test.go index 979ff52c5..3b131935a 100644 --- a/hscontrol/types/config_fields_test.go +++ b/hscontrol/types/config_fields_test.go @@ -2,6 +2,7 @@ package types import ( "reflect" + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -9,6 +10,34 @@ import ( "github.com/stretchr/testify/require" ) +// TestURLKeys: every key that may hold a URL is listed, so its userinfo is +// redacted and never shown in an error, and every listed key exists. +func TestURLKeys(t *testing.T) { + paths := map[string]bool{} + + for _, f := range ConfigFields(&Config{}) { + paths[f.Path] = true + + typ := f.Field.Type + if typ.Kind() == reflect.Slice { + typ = typ.Elem() + } + + // shortcut: catches only HTTPURL leaves, lists of them and names ending + // in url, urls or address, not maps or pointers; list any other URL + // key in URLKeys by hand. + name := f.Path[strings.LastIndex(f.Path, ".")+1:] + if typ == reflect.TypeFor[HTTPURL]() || strings.HasSuffix(name, "url") || strings.HasSuffix(name, "urls") || + strings.HasSuffix(name, "address") { + assert.Contains(t, URLKeys, f.Path) + } + } + + for _, k := range URLKeys { + assert.True(t, paths[k], "%s is no key", k) + } +} + func TestConfigFields(t *testing.T) { // Written out, not derived from the loader's key tables, so it pins the // schema on its own. @@ -105,7 +134,6 @@ func TestConfigFields(t *testing.T) { "cli.api_key", "cli.timeout", "cli.insecure", - "tuning.notifier_send_timeout", "tuning.batch_change_delay", "tuning.node_mapsession_buffered_chan_size", "tuning.batcher_workers", diff --git a/hscontrol/types/config_keys.go b/hscontrol/types/config_keys.go index cddb2d3a8..1d75a0604 100644 --- a/hscontrol/types/config_keys.go +++ b/hscontrol/types/config_keys.go @@ -1,27 +1,34 @@ package types import ( + "slices" "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 the loader lowercases them. -func keyPosition(src []byte, path []string) (int, int, bool) { +// keyNode is the file parsed for the positions of its keys. +type keyNode = yaml.Node + +// keyTree parses src for the positions of its keys, or returns nil. JSON +// and hujson.Standardize output are valid YAML, so one parser serves all. +func keyTree(src []byte) *yaml.Node { var doc yaml.Node if yaml.Unmarshal(src, &doc) != nil || len(doc.Content) == 0 { - return 0, 0, false + return nil } - n := doc.Content[0] + return doc.Content[0] +} +// keyPosition returns the 1-based position of the key written as path, one +// name per mapping, below n. Keys match case-insensitively, as the loader +// lowercases them. +func keyPosition(n *yaml.Node, path []string) (int, int, bool) { var key *yaml.Node for _, want := range path { - if n.Kind != yaml.MappingNode { + if n == nil || n.Kind != yaml.MappingNode { return 0, 0, false } @@ -48,171 +55,42 @@ func keyPosition(src []byte, path []string) (int, int, bool) { 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. -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.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", -} +// dottedPosition returns the 1-based position of the key at the dotted +// path below n, however the file splits it into nested and dotted keys. It +// follows only the keys n has, so the work is bounded by the file. +func dottedPosition(n *yaml.Node, path []string) (int, int, bool) { + if n == nil || n.Kind != yaml.MappingNode { + return 0, 0, false + } -// 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", -} + seen := map[string]bool{} -// 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), - "tls.acme.challenge_type": HTTP01ChallengeType, - "tls.acme.http_listen": ":http", - "tls.acme.cache_dir": "/var/lib/headscale/cache", + // Last duplicate wins, matching the JSON parser's effective value; an + // earlier one is shadowed, so its children were never loaded. + for i := len(n.Content) - 2; i >= 0; i -= 2 { + key := strings.ToLower(n.Content[i].Value) + if seen[key] { + continue + } + + seen[key] = true + + names := strings.Split(key, ".") + if len(names) > len(path) || !slices.EqualFunc(names, path[:len(names)], strings.EqualFold) { + continue + } + + if len(names) == len(path) { + return n.Content[i].Line, n.Content[i].Column, true + } + + line, col, ok := dottedPosition(n.Content[i+1], path[len(names):]) + if ok { + return line, col, true + } + } + + return 0, 0, false } // removedKey is a key that once worked and is now rejected. In files Old @@ -223,6 +101,8 @@ var configDefaults = map[string]any{ type removedKey struct{ Old, New, Value, Since, Hint string } var removedKeys = []removedKey{ + {Old: "derp.server.stun.enabled", Since: "0.30", Hint: "remove it: headscale never read it; the embedded DERP server always serves STUN on derp.server.stun_listen_addr"}, + {Old: "tuning.notifier_send_timeout", Since: "0.30", Hint: "remove it: it had no effect"}, {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"}, diff --git a/hscontrol/types/config_keys_test.go b/hscontrol/types/config_keys_test.go index 926330050..1c30a5992 100644 --- a/hscontrol/types/config_keys_test.go +++ b/hscontrol/types/config_keys_test.go @@ -1,6 +1,7 @@ package types import ( + "strings" "testing" "github.com/stretchr/testify/assert" @@ -24,7 +25,7 @@ func TestKeyPosition(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - line, col, ok := keyPosition([]byte(tt.src), tt.path) + line, col, ok := keyPosition(keyTree([]byte(tt.src)), tt.path) assert.Equal(t, tt.ok, ok) assert.Equal(t, tt.line, line) assert.Equal(t, tt.col, col) @@ -37,8 +38,39 @@ func TestKeyPosition(t *testing.T) { b, err := hujson.Standardize([]byte(src)) require.NoError(t, err) - line, _, ok := keyPosition(b, []string{"b"}) + line, _, ok := keyPosition(keyTree(b), []string{"b"}) assert.True(t, ok) assert.Equal(t, 5, line) }) } + +func TestDottedPosition(t *testing.T) { + tests := []struct { + name string + src string + path string + line, col int + ok bool + }{ + {"nested", "dns:\n magic_dsn: true\n", "dns.magic_dsn", 2, 3, true}, + {"dotted", "a: 1\ndns.magic_dsn: true\n", "dns.magic_dsn", 2, 1, true}, + {"mixed", "dns:\n extra.x: 1\n", "dns.extra.x", 2, 3, true}, + // The surviving dns lacks the key; the shadowed one's copy was + // never loaded, so the dotted spelling is the one that set it. + { + "shadowed duplicate parent", + "{\n\"dns.magic_dsn\": 1,\n\"dns\": {\"magic_dsn\": 1},\n\"dns\": {\"x\": 1}\n}", + "dns.magic_dsn", 2, 1, true, + }, + {"only in shadowed parent", `{"dns":{"magic_dsn":1},"dns":{"x":1}}`, "dns.magic_dsn", 0, 0, false}, + {"missing", "a: 1\n", "a.b", 0, 0, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + line, col, ok := dottedPosition(keyTree([]byte(tt.src)), strings.Split(tt.path, ".")) + assert.Equal(t, tt.ok, ok) + assert.Equal(t, tt.line, line) + assert.Equal(t, tt.col, col) + }) + } +} diff --git a/hscontrol/types/config_source.go b/hscontrol/types/config_source.go index 53b97a16b..a217c8682 100644 --- a/hscontrol/types/config_source.go +++ b/hscontrol/types/config_source.go @@ -2,23 +2,31 @@ package types import ( "bytes" + "cmp" + "encoding" stdjson "encoding/json" + "errors" "fmt" "maps" + "math" + "net/netip" "os" "path/filepath" + "reflect" "slices" + "strconv" "strings" "time" "github.com/go-viper/mapstructure/v2" hsdns "github.com/juanfont/headscale/hscontrol/dns" + "github.com/juanfont/headscale/hscontrol/util" "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/prometheus/common/model" "github.com/rs/zerolog/log" - "github.com/spf13/cast" "github.com/tailscale/hujson" ) @@ -45,13 +53,17 @@ var reservedEnvNames = []string{ } // configSource is the layered configuration: defaults, then file, then -// environment. Keys are lowercase; accessors coerce values, so a string from -// the environment reads as a bool, number or duration. +// environment, with lowercase keys. [configSource.decode] turns it into a +// [Config]. type configSource struct { k *koanf.Koanf file string fileBytes []byte - warnings []string + // keys is the file parsed for the positions of its keys, or nil. + keys *keyNode + warnings []string + // envVars names the variable that set each key, for errors. + envVars map[string]string } func configEnvName(key string) string { @@ -178,8 +190,38 @@ func normaliseConfigValue(v any) any { return v } +// schemaKey is a key of the configuration; an open map's keys are the +// user's. +type schemaKey struct { + Path string + Open bool +} + +func schemaKeys() []schemaKey { + fields := ConfigFields(&Config{}) + + keys := make([]schemaKey, len(fields)) + for i, f := range fields { + keys[i] = schemaKey{Path: f.Path, Open: f.Open} + } + + return keys +} + +func openMapKeys() []string { + var open []string + + for _, k := range schemaKeys() { + if k.Open { + open = append(open, k.Path) + } + } + + return open +} + func isOpenMapPath(path string) bool { - for _, o := range openMapKeys { + for _, o := range openMapKeys() { if path == o || strings.HasPrefix(path, o+".") { return true } @@ -194,7 +236,7 @@ func isOpenMapPath(path string) bool { func configPathParts(path string) []string { parts := strings.Split(path, ".") for i := 1; i < len(parts); i++ { - if slices.Contains(openMapKeys, strings.Join(parts[:i], ".")) { + if slices.Contains(openMapKeys(), strings.Join(parts[:i], ".")) { return append(parts[:i], strings.Join(parts[i:], ".")) } } @@ -267,9 +309,11 @@ func joinConfigPath(prefix, name string) string { // 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(".")} + s := &configSource{k: koanf.New("."), envVars: map[string]string{}} - err := s.k.Load(confmap.Provider(configDefaults, "."), nil) + d := defaultConfig() + + err := s.k.Load(confmap.Provider(plainMap(&d, false), ""), nil) if err != nil { return nil, fmt.Errorf("loading defaults: %w", err) } @@ -314,6 +358,7 @@ func (s *configSource) loadFile() error { } s.fileBytes = raw + s.keys = keyTree(raw) var m map[string]any if err == nil { @@ -349,7 +394,8 @@ func (s *configSource) loadFile() error { func configKeySets() (map[string]bool, map[string]bool) { leaf, branch := map[string]bool{}, map[string]bool{} - for _, k := range slices.Concat(configKeys, openMapKeys) { + for _, key := range schemaKeys() { + k := key.Path leaf[k] = true parts := strings.Split(k, ".") @@ -496,7 +542,7 @@ func (s *configSource) checkKeys(v *configValidator, m map[string]any) { // key below the open map (provider.config) is walked from the // map, so its contents get the role they have when nested. root, below := path, child - if !slices.Contains(openMapKeys, path) { + if !slices.Contains(openMapKeys(), path) { parts := configPathParts(path) root = strings.Join(parts[:len(parts)-1], ".") below = map[string]any{parts[len(parts)-1]: val} @@ -524,9 +570,10 @@ func (s *configSource) checkKeys(v *configValidator, m map[string]any) { s.checkOverlaps(v, m) } -// position renders " (file:line:col)" when the key can be located. +// position renders " (file:line:col)" when the key can be located, written +// nested, dotted or mixed, else " (file)". func (s *configSource) position(path string) string { - line, col, ok := keyPosition(s.fileBytes, strings.Split(path, ".")) + line, col, ok := dottedPosition(s.keys, strings.Split(path, ".")) if !ok { return " (" + s.file + ")" } @@ -550,18 +597,21 @@ func (s *configSource) loadEnv(environ []string) error { openLayer := map[string]string{} - for _, key := range slices.Concat(configKeys, openMapKeys) { - name := configEnvName(key) + for _, key := range schemaKeys() { + name := configEnvName(key.Path) names[name] = true val, ok := env[name] switch { case !ok: - case slices.Contains(openMapKeys, key): - openLayer[key] = val + continue + case key.Open: + openLayer[key.Path] = val default: - layer[key] = val + layer[key.Path] = val } + + s.envVars[key.Path] = name } // Other software shares the prefix (a Kubernetes Service named @@ -586,42 +636,26 @@ func (s *configSource) loadEnv(environ []string) error { } // 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 the JSON object held a removed form, dropped here, or a - // per-field variable (HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME) has to be - // layered on top: then the parsed object is set. The provider's is always - // parsed, as its name is looked up inside it; one that does not decode is - // no provider. + // into it. Its JSON object is set without the removed forms it holds; + // a value that is not one stays a string, which decode reports. for _, key := range slices.Sorted(maps.Keys(openLayer)) { val := openLayer[key] s.k.Delete(key) - var parsed map[string]any - - dropped := false + var ( + v any = val + parsed map[string]any + ) // A failed decode may still have filled part of parsed: never used. - decoded := stdjson.Unmarshal([]byte(val), &parsed) == nil && parsed != nil - if decoded { + if stdjson.Unmarshal([]byte(val), &parsed) == nil && parsed != nil { // The hint names keys: inside the JSON they are keys, not variables. name := configEnvName(key) walkRemoved(key, parsed, false, func(p string, r removedKey) { s.warnings = append(s.warnings, r.reason("key", p+" in "+name)+"; "+r.hint()) - dropped = true }) - } - shared := slices.ContainsFunc(slices.Collect(maps.Keys(layer)), func(k string) bool { - return strings.HasPrefix(k, key+".") - }) - - var v any = val - - switch { - case decoded && (dropped || shared || key == providerKey): v = parsed - case key == providerKey: - continue } err := s.k.Set(key, v) @@ -638,21 +672,38 @@ func (s *configSource) loadEnv(environ []string) error { return s.foldProviderEnv(env) } -// foldProviderEnv sets each HEADSCALE_DNS_CHALLENGE_PROVIDER_ of -// the provider named once the rest is loaded. Settings depend on the -// provider, so configKeys cannot list them; a variable no setting of it has -// is ignored, as the prefix is reserved. +// foldProviderEnv sets HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME, then each +// HEADSCALE_DNS_CHALLENGE_PROVIDER_ of the provider named once the +// rest is loaded. Settings depend on the provider, so the schema cannot list +// them; a variable no setting of it has is ignored, as the prefix is +// reserved. func (s *configSource) foldProviderEnv(env map[string]string) error { - settings := cast.ToStringMap(s.k.Get(providerKey)) - folded := false + settings, ok := s.k.Get(providerKey).(map[string]any) + if !ok { + if s.k.Exists(providerKey) { + // Not an object: decode reports it. + return nil + } - for _, setting := range hsdns.SettingNames(cast.ToString(settings["name"])) { - if v := env[configEnvName(providerKey+"."+setting)]; v != "" { + settings = map[string]any{} + } + + folded := false + fold := func(setting string) { + key := providerKey + "." + setting + if v := env[configEnvName(key)]; v != "" { settings[setting] = v + s.envVars[key] = configEnvName(key) folded = true } } + fold("name") + + for _, setting := range hsdns.SettingNames(DNSChallengeConfig{ProviderSettings: settings}.ProviderName()) { + fold(setting) + } + if !folded { return nil } @@ -694,51 +745,6 @@ 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, weakly typed: values from the -// environment are strings. -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 { @@ -828,10 +834,528 @@ func (s *configSource) checkOverlaps(v *configValidator, m map[string]any) { // 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) + line, col, ok := keyPosition(s.keys, names) if !ok { return "" } return fmt.Sprintf(" (%s:%d:%d)", s.file, line, col) } + +// decode decodes the layers into a Config. Every value that does not fit +// its key is reported, in one error. +func (s *configSource) decode() (*Config, error) { + var cfg Config + + err := s.k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{ + Tag: "koanf", + DecoderConfig: &mapstructure.DecoderConfig{ + ErrorUnused: true, + DecodeHook: mapstructure.ComposeDecodeHookFunc( + mapstructure.DecodeHookFuncValue(scalarHook), + mapstructure.TextUnmarshallerHookFunc(), + ), + }, + }) + if err != nil { + return nil, s.decodeErrors(err) + } + + cfg.ServerURL = strings.TrimRight(cfg.ServerURL, "/") + for i, p := range cfg.TrustedProxies { + cfg.TrustedProxies[i] = p.Masked() + } + + cfg.ConfigDir = s.configDir() + + return &cfg, nil +} + +// configDir is where relative paths in the configuration are resolved: the +// config file's directory, or the working directory without a file. +func (s *configSource) configDir() string { + dir := "." + if s.file != "" { + dir = filepath.Dir(s.file) + } + + abs, err := filepath.Abs(dir) + if err != nil { + return dir + } + + return abs +} + +// decodeErrors turns each value mapstructure rejected into a ConfigError +// naming its key and where it was set. +func (s *configSource) decodeErrors(err error) error { + v := &configValidator{} + seen := map[string]bool{} + + walkErrTree(err, func(e error) { + de, ok := e.(*mapstructure.DecodeError) //nolint:errorlint // every node is visited + if !ok { + return + } + + // An element or entry is reported on its key. + key, _, _ := strings.Cut(de.Name(), "[") + + var leaf *ConfigError + if !errors.As(de.Unwrap(), &leaf) { + leaf = &ConfigError{Reason: de.Unwrap().Error(), Cause: de.Unwrap()} + + // The decoder's own error keeps the value (UnconvertibleTypeError's + // Value); its text does not. + if hiddenValue(key) { + leaf.Cause = nil + } + } + + ce := *leaf + ce.Reason = key + " " + leaf.Reason + s.origin(key) + + if !hiddenValue(key) { + ce.Current = []KV{{key, s.k.Get(key)}} + } + + if !seen[ce.Reason] { + seen[ce.Reason] = true + v.Add(&ce) + } + }) + + if !v.HasErrors() { + return err + } + + return v.Err() +} + +// origin renders where key's value came from: its variable, else its +// position in the file. +func (s *configSource) origin(key string) string { + if name, ok := s.envVars[key]; ok { + return " (env " + name + ")" + } + + if s.file == "" { + return "" + } + + return s.position(key) +} + +// hiddenValue reports whether key's value may be a credential, which errors +// do not show: a secret, a URL (its userinfo) or a DNS provider setting. +func hiddenValue(key string) bool { + if key == providerKey || strings.HasPrefix(key, providerKey+".") || slices.Contains(URLKeys, key) { + return true + } + + for _, f := range ConfigFields(&Config{}) { + if f.Path != key { + continue + } + + return f.Field.Type == reflect.TypeFor[Secret]() + } + + return false +} + +var ( + durationType = reflect.TypeFor[time.Duration]() + fileModeType = reflect.TypeFor[FileMode]() + prefixType = reflect.TypeFor[netip.Prefix]() + prefixPtrType = reflect.TypeFor[*netip.Prefix]() +) + +// scalarHook converts a value as written into what its field's type takes, +// before mapstructure and TextUnmarshaler see it. A string is the form +// variables take, so it may stand for a number, bool, list or map. +func scalarHook(from, to reflect.Value) (any, error) { + data := from.Interface() + s, isString := data.(string) + + switch t := to.Type(); { + case t == durationType: + return toDuration(from) + case t == fileModeType && !isString: + // YAML reads an unquoted 0770 as the integer 504, which cannot be + // told apart from a literal 504. + return nil, &ConfigError{ + Reason: "must be quoted", + Maximum: `"0777"`, + Hint: `quote the value, e.g. "0770"; unquoted, YAML reads 0770 as the number 504`, + Cause: util.ErrInvalidFileMode, + } + case t == prefixPtrType && isString && s == "": + // Unset. Typed: the next hook reads its type. + return (*netip.Prefix)(nil), nil + case t == prefixType && isString: + _, err := netip.ParsePrefix(s) + if err != nil { + return nil, &ConfigError{ + Reason: "is not a valid CIDR", + Detail: err.Error(), + Hint: `use CIDR form, e.g. "100.64.0.0/10"`, + Cause: err, + } + } + case t.Kind() == reflect.String: + if text, ok := scalarText(from); ok { + return text, nil + } + case t.Kind() == reflect.Bool: + return toBool(from) + case t.Kind() >= reflect.Int && t.Kind() <= reflect.Int64: + return toInt(from, t) + case t.Kind() == reflect.Slice && isString: + return toList(s, t.Elem()) + case t.Kind() == reflect.Map && isString: + return toObject(s) + } + + return data, nil +} + +// scalarText is a number or bool as text. +func scalarText(v reflect.Value) (string, bool) { + switch { + case v.Kind() == reflect.Bool: + return strconv.FormatBool(v.Bool()), true + case v.CanInt(): + return strconv.FormatInt(v.Int(), 10), true + case v.CanUint(): + return strconv.FormatUint(v.Uint(), 10), true + case v.CanFloat(): + return strconv.FormatFloat(v.Float(), 'f', -1, 64), true + } + + return "", false +} + +func toDuration(v reflect.Value) (any, error) { + unit := &ConfigError{Reason: "needs a unit", Hint: `write the unit, e.g. "30s", "5m" or "90d"`} + + if v.Kind() != reflect.String { + if _, ok := scalarText(v); !ok || v.Kind() == reflect.Bool { + return v.Interface(), nil + } + + if v.IsZero() { + return time.Duration(0), nil + } + + return nil, unit + } + + s := v.String() + + d, err := time.ParseDuration(s) + if err == nil { + return d, nil + } + + md, err := model.ParseDuration(s) + if err == nil { + return time.Duration(md), nil + } + + _, err = strconv.ParseFloat(s, 64) + if err == nil { + return nil, unit + } + + return nil, &ConfigError{Reason: "is not a duration", Hint: unit.Hint} +} + +func toBool(v reflect.Value) (any, error) { + text, ok := reflect.TypeAssert[string](v) + if !ok { + if text, ok = scalarText(v); !ok || v.Kind() == reflect.Bool { + return v.Interface(), nil + } + } + + b, err := strconv.ParseBool(text) + if err != nil { + return nil, &ConfigError{Reason: "is not a boolean", Hint: "use true or false", Cause: err} + } + + return b, nil +} + +// toInt takes a whole number in t's range, written as one or as text. +func toInt(v reflect.Value, t reflect.Type) (any, error) { + var ( + n int64 + whole bool + ) + + switch { + case v.Kind() == reflect.String: + i, err := strconv.ParseInt(v.String(), 10, 64) + if errors.Is(err, strconv.ErrRange) { + return nil, &ConfigError{Reason: "is out of range"} + } + + n, whole = i, err == nil + case v.CanInt(): + n, whole = v.Int(), true + case v.CanUint(): + if v.Uint() > math.MaxInt64 { + return nil, &ConfigError{Reason: "is out of range"} + } + + n, whole = int64(v.Uint()), true //nolint:gosec // bounded above + case v.CanFloat(): + f := v.Float() + if f < math.MinInt64 || f >= math.MaxInt64 { + return nil, &ConfigError{Reason: "is out of range"} + } + + n, whole = int64(f), f == math.Trunc(f) + default: + return v.Interface(), nil + } + + if !whole { + return nil, &ConfigError{Reason: "is not an integer"} + } + + if reflect.Zero(t).OverflowInt(n) { + return nil, &ConfigError{Reason: "is out of range"} + } + + return n, nil +} + +// toList splits a list written as one string: as JSON when it starts with +// "[", as a list of objects must, else on whitespace. +func toList(s string, elem reflect.Type) (any, error) { + object := elem.Kind() == reflect.Struct && !reflect.PointerTo(elem).Implements(textUnmarshalerType) + + if !object && !strings.HasPrefix(strings.TrimSpace(s), "[") { + fields := strings.Fields(s) + + list := make([]any, len(fields)) + for i, f := range fields { + list[i] = f + } + + return list, nil + } + + var list []any + + err := stdjson.Unmarshal([]byte(s), &list) + if err != nil { + hint := `write the list as JSON, e.g. ["a", "b"]` + if object { + hint = `write the list as JSON, e.g. [{"name": "a.example.com", "type": "A", "value": "100.64.0.1"}]` + } + + return nil, &ConfigError{Reason: "is not a JSON list", Hint: hint, Cause: err} + } + + return list, nil +} + +func toObject(s string) (any, error) { + var m map[string]any + + err := stdjson.Unmarshal([]byte(s), &m) + if err != nil || m == nil { + return nil, &ConfigError{ + Reason: "is not a JSON object", + Hint: `write the map as JSON, e.g. {"example.com": ["1.1.1.1"]}`, + Cause: err, + } + } + + return m, nil +} + +// plainMap is cfg as nested maps of plain values, keyed as in a file: what +// the defaults layer loads and a parser marshals. With redact, a secret +// shows only a file: reference, a URL no userinfo, and every DNS provider +// setting but a scalar name and file: references is hidden: any of them, a +// number included, may be a credential. +func plainMap(cfg *Config, redact bool) map[string]any { + out := map[string]any{} + + if redact { + // HTTPURL and Secret values redact themselves, plain strings not. + r := cfg.RedactedURLs() + cfg = &r + } + + for _, f := range ConfigFields(cfg) { + v := plainValue(f.Value, redact) + + switch settings, isMap := v.(map[string]any); { + case f.Field.Type == prefixPtrType && f.Value.IsNil(): + // An explicit unset, which survives a default. + v = "" + case redact && isMap && f.Path == providerKey: + v = redactProvider(settings) + } + + if v == nil { + continue + } + + parts := strings.Split(f.Path, ".") + m := out + + for _, p := range parts[:len(parts)-1] { + child, ok := m[p].(map[string]any) + if !ok { + child = map[string]any{} + m[p] = child + } + + m = child + } + + m[parts[len(parts)-1]] = v + } + + return out +} + +// maxExactJSONInt is the largest integer a float64, as JSON parsers read +// numbers, holds exactly. +const maxExactJSONInt = 1 << 53 + +// plainValue is v as a parser writes it, or nil to leave it out. +func plainValue(v reflect.Value, redact bool) any { + for v.Kind() == reflect.Pointer || v.Kind() == reflect.Interface { + if v.IsNil() { + return nil + } + + v = v.Elem() + } + + switch x := v.Interface().(type) { + case time.Duration: + return x.String() + case Secret: + if redact { + return x.Redacted() + } + + return string(x) + case HTTPURL: + // As written: its JSON form drops userinfo. + if redact { + return x.Redacted() + } + + return string(x) + case encoding.TextMarshaler: + text, err := x.MarshalText() + if err != nil { + return nil + } + + return string(text) + } + + switch v.Kind() { //nolint:exhaustive // the kinds Config holds; default keeps the rest as is + case reflect.String: + return v.String() + case reflect.Bool: + return v.Bool() + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + if n := v.Int(); n > maxExactJSONInt || n < -maxExactJSONInt { + // The integer hook takes it back from text. + return strconv.FormatInt(n, 10) + } + + return int(v.Int()) + case reflect.Slice: + if v.IsNil() { + return nil + } + + list := make([]any, v.Len()) + for i := range list { + list[i] = plainValue(v.Index(i), redact) + } + + return list + case reflect.Map: + if v.IsNil() { + return nil + } + + m := make(map[string]any, v.Len()) + + for iter := v.MapRange(); iter.Next(); { + if e := plainValue(iter.Value(), redact); e != nil { + m[fmt.Sprint(iter.Key().Interface())] = e + } + } + + return m + case reflect.Struct: + // Without koanf tags (tailcfg.DNSRecord): its field names, lowercased. + m := map[string]any{} + + for f, fv := range v.Fields() { + if f.IsExported() { + m[cmp.Or(f.Tag.Get("koanf"), strings.ToLower(f.Name))] = plainValue(fv, redact) + } + } + + return m + default: + return v.Interface() + } +} + +func redactProvider(settings map[string]any) map[string]any { + out := make(map[string]any, len(settings)) + for k, v := range settings { + out[k] = redactLeaves(v) + } + + // A name that is a map or list stays hidden: it is no name. + if _, ok := (DNSChallengeConfig{ProviderSettings: settings}).providerName(); ok { + if name, set := settings["name"]; set { + out["name"] = name + } + } + + return out +} + +func redactLeaves(v any) any { + switch x := v.(type) { + case map[string]any: + out := make(map[string]any, len(x)) + for k, e := range x { + out[k] = redactLeaves(e) + } + + return out + case []any: + out := make([]any, len(x)) + for i, e := range x { + out[i] = redactLeaves(e) + } + + return out + case string: + if strings.HasPrefix(x, "file:") { + return x + } + } + + return "" +} diff --git a/hscontrol/types/config_source_test.go b/hscontrol/types/config_source_test.go index 249b07b39..a4b871b8a 100644 --- a/hscontrol/types/config_source_test.go +++ b/hscontrol/types/config_source_test.go @@ -2,25 +2,29 @@ package types import ( "cmp" - "go/ast" - "go/parser" - "go/token" + "fmt" + "math" + "net/netip" "os" "path/filepath" + "reflect" "slices" - "strconv" "strings" "testing" "time" + gocmp "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "tailscale.com/tailcfg" ) -func writeConfig(t *testing.T, name, content string) string { +// writeConfig writes content as config.yaml and returns its path. +func writeConfig(t *testing.T, content string) string { t.Helper() - p := filepath.Join(t.TempDir(), name) + p := filepath.Join(t.TempDir(), "config.yaml") require.NoError(t, os.WriteFile(p, []byte(content), 0o600)) return p @@ -35,7 +39,7 @@ func TestConfigSource(t *testing.T) { env []string errs []string // substrings of the error warns []string // substrings, one per expected warning - check func(t *testing.T, s *configSource) + check func(t *testing.T, cfg *Config) }{ { name: "unknown file key", @@ -84,40 +88,40 @@ func TestConfigSource(t *testing.T) { { 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) { + check: func(t *testing.T, cfg *Config) { t.Helper() - assert.Equal(t, "debug", s.String("log.level")) - assert.Equal(t, "/c", s.String("tls.cert_path")) + assert.Equal(t, LogLevel("debug"), cfg.Log.Level) + assert.Equal(t, "/c", cfg.TLS.CertPath) }, }, { 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) { + check: func(t *testing.T, cfg *Config) { t.Helper() - assert.Equal(t, "https://x", s.String("server_url")) + assert.Equal(t, "https://x", cfg.ServerURL) assert.Equal(t, map[string][]string{"corp.example.com": {"10.0.0.1"}}, - s.StringMapStringSlice("dns.nameservers.split")) + cfg.DNSConfig.Nameservers.Split) }, }, { name: "null is unset, default stays", file: "server_url:\nlog:\n level: ~\n", - check: func(t *testing.T, s *configSource) { + check: func(t *testing.T, cfg *Config) { t.Helper() - assert.False(t, s.IsSet("server_url")) - assert.Equal(t, "info", s.String("log.level")) + assert.Empty(t, cfg.ServerURL) + assert.Equal(t, LogLevel("info"), cfg.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) { + check: func(t *testing.T, cfg *Config) { t.Helper() - assert.Equal(t, "http://env", s.String("server_url")) - assert.Equal(t, "1.1.1.1:1", s.String("listen_addr")) + assert.Equal(t, "http://env", cfg.ServerURL) + assert.Equal(t, ListenAddr("1.1.1.1:1"), cfg.Addr) }, }, { @@ -126,19 +130,19 @@ func TestConfigSource(t *testing.T) { "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) { + check: func(t *testing.T, cfg *Config) { 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")) + assert.Equal(t, []string{"1.1.1.1", "8.8.8.8"}, cfg.DNSConfig.Nameservers.Global) + assert.Equal(t, map[string]string{"domain_hint": "example.com"}, cfg.OIDC.ExtraParams) }, }, { 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) { + check: func(t *testing.T, cfg *Config) { t.Helper() - assert.Equal(t, "cloudflare", s.String("dns.challenge.provider.name")) - assert.Equal(t, 1, s.Int("dns.challenge.provider.whatever")) + assert.Equal(t, "cloudflare", cfg.DNSConfig.Challenge.ProviderName()) + assert.Equal(t, 1, cfg.DNSConfig.Challenge.ProviderSettings["whatever"]) }, }, { @@ -192,27 +196,23 @@ func TestConfigSource(t *testing.T) { { 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) { + check: func(t *testing.T, cfg *Config) { 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")) + assert.Equal(t, "/n", cfg.Noise.PrivateKeyPath) + assert.Equal(t, "/d", cfg.DERP.Server.PrivateKeyPath) + assert.Equal(t, LogLevel("debug"), cfg.Log.Level) + assert.Equal(t, "/c", cfg.TLS.CertPath) + assert.Equal(t, "/db", cfg.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) { + check: func(t *testing.T, cfg *Config) { 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")) + assert.Equal(t, LogLevel("debug"), cfg.Log.Level) + assert.False(t, cfg.DNSConfig.MagicDNS) + assert.Equal(t, map[string][]string{"corp.example.com": {"10.0.0.1"}}, cfg.DNSConfig.Nameservers.Split) }, }, { @@ -228,20 +228,11 @@ func TestConfigSource(t *testing.T) { { 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) { + check: func(t *testing.T, cfg *Config) { 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")) + cfg.DNSConfig.Nameservers.Split) }, }, { @@ -270,9 +261,9 @@ func TestConfigSource(t *testing.T) { { name: "empty file", emptyFile: true, - check: func(t *testing.T, s *configSource) { + check: func(t *testing.T, cfg *Config) { t.Helper() - assert.Equal(t, "info", s.String("log.level")) + assert.Equal(t, LogLevel("info"), cfg.Log.Level) }, }, { @@ -317,7 +308,9 @@ func TestConfigSource(t *testing.T) { } if tt.check != nil { - tt.check(t, s) + cfg, err := s.decode() + require.NoError(t, err) + tt.check(t, cfg) } }) } @@ -325,13 +318,13 @@ func TestConfigSource(t *testing.T) { 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) { +func checkSameValues(t *testing.T, cfg *Config) { 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")) + assert.Equal(t, "https://x", cfg.ServerURL) + assert.Equal(t, time.Hour, cfg.Node.Expiry) + assert.False(t, cfg.DNSConfig.MagicDNS) + assert.Equal(t, []string{"1.1.1.1", "8.8.8.8"}, cfg.DNSConfig.Nameservers.Global) + assert.Equal(t, map[string]string{"domain_hint": "example.com"}, cfg.OIDC.ExtraParams) } func TestConfigSearchDirs(t *testing.T) { @@ -355,99 +348,16 @@ func TestConfigSourceSearch(t *testing.T) { 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")) + + cfg, err := s.decode() + require.NoError(t, err) + assert.Equal(t, "http://json", cfg.ServerURL) s, err = newConfigSource(t.TempDir(), false, nil) require.NoError(t, err) assert.Empty(t, s.File()) } -func TestConfigSourceUnmarshalKey(t *testing.T) { - type rec struct{ Name, Type, Value string } - - tests := []struct { - name, file string - want []rec - }{ - // An empty string decodes to an empty, non-nil slice. - {"empty string", "dns:\n extra_records: \"\"\n", []rec{}}, - {"list", "dns:\n extra_records:\n - {name: a.example.com, type: A, value: 1.2.3.4}\n", []rec{{"a.example.com", "A", "1.2.3.4"}}}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, err := newConfigSource(writeConfig(t, "config.yaml", tt.file), true, nil) - require.NoError(t, err) - - var got []rec - - require.NoError(t, s.UnmarshalKey("dns.extra_records", &got)) - assert.Equal(t, tt.want, got) - }) - } -} - -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 - } - - // Every src accessor takes the key first. - fn, ok := call.Fun.(*ast.SelectorExpr) - if !ok { - return true - } - - if obj, ok := fn.X.(*ast.Ident); !ok || obj.Name != "src" { - 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 - }) - } - - all := slices.Concat(configKeys, openMapKeys) - - for key := range read { - ok := slices.Contains(all, key) || 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, "%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 - } -} - // 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) { @@ -483,9 +393,12 @@ func TestConfigSourceDottedKeyAlone(t *testing.T) { for range 50 { s, err := newConfigSource(dir, false, nil) require.NoError(t, err) - require.Equal(t, "debug", s.String("log.level")) - require.False(t, s.Bool("dns.magic_dns")) - require.Equal(t, []string{"1.1.1.1"}, s.StringSlice("dns.nameservers.global")) + + cfg, err := s.decode() + require.NoError(t, err) + require.Equal(t, LogLevel("debug"), cfg.Log.Level) + require.False(t, cfg.DNSConfig.MagicDNS) + require.Equal(t, []string{"1.1.1.1"}, cfg.DNSConfig.Nameservers.Global) } } @@ -494,44 +407,44 @@ func TestConfigSourceDottedKeyAlone(t *testing.T) { func TestConfigSourceDottedNoOverlap(t *testing.T) { tests := []struct { name, file string - check func(t *testing.T, s *configSource) + check func(t *testing.T, cfg *Config) }{ { 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) { + check: func(t *testing.T, cfg *Config) { 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")) + cfg.DNSConfig.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) { + check: func(t *testing.T, cfg *Config) { t.Helper() - assert.Equal(t, map[string]string{"domain_hint": "a", "domain_hint.extra": "b"}, s.StringMapString("oidc.extra_params")) + assert.Equal(t, map[string]string{"domain_hint": "a", "domain_hint.extra": "b"}, cfg.OIDC.ExtraParams) }, }, { 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) { + check: func(t *testing.T, cfg *Config) { 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")) + cfg.DNSConfig.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) { + check: func(t *testing.T, cfg *Config) { 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")) + assert.Equal(t, LogLevel("debug"), cfg.Log.Level) + assert.Equal(t, JSONLogFormat, cfg.Log.Format) + assert.True(t, cfg.LogTail.Enabled) }, }, } @@ -544,7 +457,10 @@ func TestConfigSourceDottedNoOverlap(t *testing.T) { for range 50 { s, err := newConfigSource(dir, false, nil) require.NoError(t, err) - tt.check(t, s) + + cfg, err := s.decode() + require.NoError(t, err) + tt.check(t, cfg) } }) } @@ -560,7 +476,7 @@ func TestConfigSourceShapeErrorsHideValues(t *testing.T) { "list under database.sqlite": "database:\n sqlite: [marker-secret]\n", } { t.Run(name, func(t *testing.T) { - _, err := newConfigSource(writeConfig(t, "config.yaml", file), true, nil) + _, err := newConfigSource(writeConfig(t, file), true, nil) require.ErrorContains(t, err, "must be a mapping") assert.NotContains(t, err.Error(), "marker-secret") }) @@ -589,11 +505,13 @@ func TestConfigSourceProviderEnv(t *testing.T) { require.NoError(t, err) assert.Empty(t, s.Warnings()) - assert.Equal(t, "cloudflare", s.String("dns.challenge.provider.name")) + cfg, err := s.decode() + require.NoError(t, err) + assert.Equal(t, "cloudflare", cfg.DNSConfig.Challenge.ProviderName()) // The env value replaces the file's map; the per-field // variable is layered on top of it. - got := s.StringMap("dns.challenge.provider") + got := cfg.DNSConfig.Challenge.ProviderSettings assert.Equal(t, "x", got["api_token"]) assert.NotContains(t, got, "other") } @@ -698,3 +616,401 @@ dns: }) } } + +// decodeInput writes content as name (none when empty) and decodes it with +// env, as the loaders do. +func decodeInput(t *testing.T, name, content string, env ...string) (*Config, error) { + t.Helper() + + dir := t.TempDir() + if content != "" { + require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(content), 0o600)) + } + + s, err := newConfigSource(dir, false, env) + require.NoError(t, err) + + return s.decode() +} + +func TestDecodeScalars(t *testing.T) { + tests := []struct { + name string + file string // config.yaml unless it starts with "{" + env []string + check func(t *testing.T, cfg *Config) + wantErr []string + }{ + {name: "yaml expiry 0", file: "node:\n expiry: 0\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.Zero(t, c.Node.Expiry) }}, + {name: "json expiry 0", file: `{"node": {"expiry": 0}}`, check: func(t *testing.T, c *Config) { t.Helper(); assert.Zero(t, c.Node.Expiry) }}, + {name: "env expiry 0", env: []string{"HEADSCALE_NODE_EXPIRY=0"}, check: func(t *testing.T, c *Config) { t.Helper(); assert.Zero(t, c.Node.Expiry) }}, + {name: "prometheus duration", file: "node:\n expiry: 90d\n", check: func(t *testing.T, c *Config) { + t.Helper() + assert.Equal(t, 2160*time.Hour, c.Node.Expiry) + }}, + {name: "duration without unit", file: "node:\n expiry: 90\n", wantErr: []string{"node.expiry needs a unit (", "config.yaml:2:3)"}}, + {name: "env duration without unit", env: []string{"HEADSCALE_TUNING_BATCH_CHANGE_DELAY=800"}, wantErr: []string{"tuning.batch_change_delay needs a unit (env HEADSCALE_TUNING_BATCH_CHANGE_DELAY)"}}, + {name: "unquoted file mode", file: "unix_socket_permission: 0770\n", wantErr: []string{"unix_socket_permission must be quoted"}}, + {name: "quoted file mode", file: "unix_socket_permission: \"0770\"\n", check: func(t *testing.T, c *Config) { + t.Helper() + assert.Equal(t, FileMode(0o770), c.UnixSocketPermission) + }}, + {name: "bool in a string field", file: "database:\n postgres:\n ssl: false\n", check: func(t *testing.T, c *Config) { + t.Helper() + assert.Equal(t, "false", c.Database.Postgres.Ssl) + }}, + {name: "milliseconds stay a number", file: "database:\n gorm:\n slow_threshold: 1000\n", check: func(t *testing.T, c *Config) { + t.Helper() + assert.Equal(t, 1000, c.Database.Gorm.SlowThreshold) + }}, + {name: "json integer", file: `{"database": {"postgres": {"port": 5432}}}`, check: func(t *testing.T, c *Config) { + t.Helper() + assert.Equal(t, 5432, c.Database.Postgres.Port) + }}, + {name: "json fraction", file: `{"database": {"postgres": {"port": 5432.5}}}`, wantErr: []string{"database.postgres.port is not an integer"}}, + {name: "json out of range", file: `{"database": {"postgres": {"port": 1e20}}}`, wantErr: []string{"database.postgres.port is out of range"}}, + {name: "number 1 for a bool", file: "taildrop:\n enabled: 1\nlogtail:\n enabled: 1\n", check: func(t *testing.T, c *Config) { + t.Helper() + assert.True(t, c.Taildrop.Enabled) + assert.True(t, c.LogTail.Enabled) + }}, + {name: "number 0 for a bool", file: "taildrop:\n enabled: 0\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.False(t, c.Taildrop.Enabled) }}, + {name: "number 2 for a bool", file: "taildrop:\n enabled: 2\n", wantErr: []string{"taildrop.enabled is not a boolean"}}, + {name: "env yes for a bool", env: []string{"HEADSCALE_TAILDROP_ENABLED=yes"}, wantErr: []string{"taildrop.enabled is not a boolean (env HEADSCALE_TAILDROP_ENABLED)"}}, + {name: "empty prefix is unset", file: "prefixes:\n v4: \"\"\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.Nil(t, c.Prefixes.V4) }}, + {name: "empty enum is unset", file: "log:\n format: \"\"\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.Empty(t, c.Log.Format) }}, + {name: "env json list", env: []string{`HEADSCALE_DNS_SEARCH_DOMAINS=["a","b"]`}, check: func(t *testing.T, c *Config) { + t.Helper() + assert.Equal(t, []string{"a", "b"}, c.DNSConfig.SearchDomains) + }}, + {name: "env whitespace list", env: []string{"HEADSCALE_DNS_SEARCH_DOMAINS=a b"}, check: func(t *testing.T, c *Config) { + t.Helper() + assert.Equal(t, []string{"a", "b"}, c.DNSConfig.SearchDomains) + }}, + {name: "env map not json", env: []string{"HEADSCALE_DNS_NAMESERVERS_SPLIT=x=y"}, wantErr: []string{"dns.nameservers.split is not a JSON object (env HEADSCALE_DNS_NAMESERVERS_SPLIT)"}}, + {name: "level is case-insensitive", file: "log:\n level: INFO\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.Equal(t, LogLevel("info"), c.Log.Level) }}, + {name: "unknown extra record field", file: "dns:\n extra_records:\n - {name: a.example.com, type: A, value: 100.64.0.1, ttl: 60}\n", wantErr: []string{ + "dns.extra_records has invalid keys: ttl", + `current: dns.extra_records: [{"name":"a.example.com","ttl":60,"type":"A","value":"100.64.0.1"}]`, + }}, + {name: "empty duration", file: "node:\n expiry: \"\"\n", wantErr: []string{"node.expiry is not a duration"}}, + {name: "empty integer", file: "database:\n postgres:\n port: \"\"\n", wantErr: []string{"database.postgres.port is not an integer"}}, + {name: "empty bool", file: "taildrop:\n enabled: \"\"\n", wantErr: []string{"taildrop.enabled is not a boolean"}}, + {name: "empty string is a value", file: "server_url: \"\"\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.Empty(t, c.ServerURL) }}, + {name: "env list not json", env: []string{"HEADSCALE_DNS_SEARCH_DOMAINS=[a"}, wantErr: []string{"dns.search_domains is not a JSON list", `hint: write the list as JSON, e.g. ["a", "b"]`}}, + {name: "dotted top-level key", file: "log.level: verbose\n", wantErr: []string{"log.level has an unsupported value (", "config.yaml:1:1)"}}, + {name: "nested then dotted key", file: "dns:\n magic_dns: false\n certificates.enabled: maybe\n", wantErr: []string{"dns.certificates.enabled is not a boolean (", "config.yaml:3:3)"}}, + {name: "dotted then nested key", file: "dns:\n magic_dns: false\ndns.certificates:\n enabled: maybe\n", wantErr: []string{"dns.certificates.enabled is not a boolean (", "config.yaml:4:3)"}}, + {name: "dotted inner list", file: "dns:\n nameservers.global: 1\n", wantErr: []string{"dns.nameservers.global ", "config.yaml:2:3)"}}, + {name: "env extra records", env: []string{`HEADSCALE_DNS_EXTRA_RECORDS=[{"name":"a.example.com","type":"A","value":"100.64.0.1"}]`}, check: func(t *testing.T, c *Config) { + t.Helper() + assert.Equal(t, []tailcfg.DNSRecord{{Name: "a.example.com", Type: "A", Value: "100.64.0.1"}}, c.DNSConfig.ExtraRecords) + }}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + name := "config.yaml" + if strings.HasPrefix(tt.file, "{") { + name = "config.json" + } + + cfg, err := decodeInput(t, name, tt.file, tt.env...) + if len(tt.wantErr) > 0 { + for _, want := range tt.wantErr { + require.ErrorContains(t, err, want) + } + + return + } + + require.NoError(t, err) + tt.check(t, cfg) + }) + } +} + +// TestDecodeErrors pins that every bad value is reported at once, before +// any rule runs, and that a credential's value never is. +func TestDecodeErrors(t *testing.T) { + t.Run("all bad values in one error", func(t *testing.T) { + require.NoError(t, LoadConfig(writeConfig(t, "log:\n level: verbose\nnode:\n expiry: 90\ntaildrop:\n enabled: maybe\n"), true)) + + _, err := LoadServerConfig() + require.Error(t, err) + + got := reasons(ConfigErrors(err)) + require.Len(t, got, 3, "%v", got) + assert.Contains(t, got[0], "log.level") + assert.Contains(t, got[1], "node.expiry") + assert.Contains(t, got[2], "taildrop.enabled") + // No rule ran: server_url and the noise key are missing too. + assert.NotContains(t, err.Error(), "server_url") + }) + + for name, tt := range map[string]struct { + file string + env []string + key string + }{ + "postgres pass": {file: "database:\n postgres:\n pass: [marker-secret]\n", key: "database.postgres.pass"}, + "oidc client secret": {file: "oidc:\n client_secret: [marker-secret]\n", key: "oidc.client_secret"}, + "cli api key": {file: "cli:\n api_key: [marker-secret]\n", key: "cli.api_key"}, + "derp url with a token": {file: "derp:\n urls: [\"https://marker-secret@:443\"]\n", key: "derp.urls"}, + "derp url unparsable": {env: []string{"HEADSCALE_DERP_URLS=https://alice:marker-secret?@h"}, key: "derp.urls"}, + "oidc issuer": {file: "oidc:\n issuer: https://alice:marker-secret?@idp.example.com\n", key: "oidc.issuer"}, + "acme url": {file: "tls:\n acme:\n url: https://alice:marker-secret@acme.example.com:port\n", key: "tls.acme.url"}, + "server url list": {file: "server_url: [\"https://alice:marker-secret@h\"]\n", key: "server_url"}, //nolint:gosec // a test marker, not a credential + "cli address list": {file: "cli:\n address: [\"https://alice:marker-secret@h\"]\n", key: "cli.address"}, //nolint:gosec // a test marker, not a credential + "nameserver element": {file: "dns:\n nameservers:\n global: [{url: \"https://alice:marker-secret@h\"}]\n", key: "dns.nameservers.global"}, //nolint:gosec // a test marker, not a credential + "split element": {file: "dns:\n nameservers:\n split:\n a.example.com: [[\"https://alice:marker-secret@h\"]]\n", key: "dns.nameservers.split"}, //nolint:gosec // a test marker, not a credential + "provider json": {env: []string{`HEADSCALE_DNS_CHALLENGE_PROVIDER={"api_token":"marker-secret"`}, key: "dns.challenge.provider is not a JSON object (env HEADSCALE_DNS_CHALLENGE_PROVIDER)"}, + "provider scalar": {file: "dns:\n challenge:\n provider: marker-secret\n", key: "dns.challenge.provider"}, + } { + t.Run(name, func(t *testing.T) { + _, err := decodeInput(t, "config.yaml", tt.file, tt.env...) + require.ErrorContains(t, err, tt.key) + requireHidden(t, err, "marker-secret") + }) + } +} + +func TestSchemaKeys(t *testing.T) { + open := map[string]bool{} + paths := map[string]bool{} + + for _, k := range schemaKeys() { + paths[k.Path] = true + open[k.Path] = k.Open + } + + for _, k := range []string{"dns.nameservers.split", "oidc.extra_params", "dns.challenge.provider"} { + assert.Truef(t, open[k], "%s is open", k) + } + + assert.False(t, open["dns.extra_records"]) + assert.NotContains(t, paths, "derp.server.stun.enabled") + assert.NotContains(t, paths, "dns.challenge.provider.name") + + // Two keys sharing one environment variable would shadow each other. + envs := map[string]string{} + + for _, k := range schemaKeys() { + name := configEnvName(k.Path) + assert.Emptyf(t, envs[name], "%s and %s share %s", envs[name], k.Path, name) + envs[name] = k.Path + } +} + +// TestDefaultsFitTypes: every default is a value its field's type accepts, +// so the defaults layer decodes back to defaultConfig. +func TestDefaultsFitTypes(t *testing.T) { + d := defaultConfig() + + for _, f := range ConfigFields(&d) { + e, ok := reflect.TypeAssert[interface{ Allowed() []string }](f.Value) + if !ok { + continue + } + + v := f.Value.String() + assert.Truef(t, v == "" || slices.Contains(e.Allowed(), v), "%s defaults to %q, not in %v", f.Path, v, e.Allowed()) + } + + cfg, err := decodeInput(t, "", "") + require.NoError(t, err) + + if diff := gocmp.Diff(d, *cfg, cmpopts.IgnoreFields(Config{}, "ConfigDir"), cmpopts.IgnoreUnexported(Config{})); diff != "" { + t.Errorf("defaults layer (-defaultConfig +decoded):\n%s", diff) + } +} + +func TestPlainMap(t *testing.T) { + v4 := netip.MustParsePrefix("100.64.0.0/10") + cfg := &Config{ + ServerURL: "https://h", + UnixSocketPermission: 0, + TrustedProxies: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/8")}, + Prefixes: PrefixesConfig{V4: &v4}, + Node: NodeConfig{Expiry: 90 * time.Minute}, + DERP: DERPConfig{URLs: []HTTPURL{"https://user:pass@derp.example/map"}}, + Database: DatabaseConfig{Postgres: PostgresConfig{Pass: "marker-pass"}}, + OIDC: OIDCConfig{ClientSecret: "file:/run/secret"}, + DNSConfig: DNSConfig{ + ExtraRecords: []tailcfg.DNSRecord{{Name: "a.example.com", Type: "A", Value: "100.64.0.1"}}, + Challenge: DNSChallengeConfig{ProviderSettings: map[string]any{ //nolint:gosec // test markers, not credentials + "name": "acmedns", + "api_token": 123456, + "token": "file:/run/token", + "config": map[string]any{"a.example.com": map[string]any{"password": "marker-pw", "name": "x"}}, + "list": []any{"marker-item"}, + }}, + }, + } + + cfg.OIDC.Issuer = "https://client:marker-issuer@idp.example.com/realm" + cfg.TLS.ACME.URL = "https://marker-token@acme.example.com/dir?x=1" + cfg.DERP.URLs = append(cfg.DERP.URLs, "https://marker-only@derp.example/token", "https://h/%zz") + cfg.DNSConfig.Challenge.ProviderSettings["unset"] = (*netip.Prefix)(nil) + cfg.DNSConfig.Challenge.ProviderSettings["nothing"] = (*string)(nil) + + plain := plainMap(cfg, false) + assert.Equal(t, "https://h", plain["server_url"]) + assert.Equal(t, "0000", plain["unix_socket_permission"]) + assert.Equal(t, []any{"10.0.0.0/8"}, plain["trusted_proxies"]) + + prefixes, _ := plain["prefixes"].(map[string]any) + assert.Equal(t, "100.64.0.0/10", prefixes["v4"]) + assert.Empty(t, prefixes["v6"], "a nil prefix prints as an explicit unset") + assert.Contains(t, prefixes, "v6") + + node, _ := plain["node"].(map[string]any) + assert.Equal(t, "1h30m0s", node["expiry"]) + + derp, _ := plain["derp"].(map[string]any) + assert.Equal(t, []any{ + "https://user:pass@derp.example/map", "https://marker-only@derp.example/token", "https://h/%zz", + }, derp["urls"], "written as is, not as its JSON") + + oidc, _ := plain["oidc"].(map[string]any) + assert.Equal(t, "https://client:marker-issuer@idp.example.com/realm", oidc["issuer"]) + + tls, _ := plain["tls"].(map[string]any) + acme, _ := tls["acme"].(map[string]any) + assert.Equal(t, "https://marker-token@acme.example.com/dir?x=1", acme["url"]) + assert.NotContains(t, derp, "derpmap") + + db, _ := plain["database"].(map[string]any) + pg, _ := db["postgres"].(map[string]any) + assert.Equal(t, "marker-pass", pg["pass"]) + + dns, _ := plain["dns"].(map[string]any) + assert.Equal(t, []any{map[string]any{"name": "a.example.com", "type": "A", "value": "100.64.0.1"}}, dns["extra_records"]) + + redacted := plainMap(cfg, true) + db, _ = redacted["database"].(map[string]any) + pg, _ = db["postgres"].(map[string]any) + assert.Equal(t, "", pg["pass"]) + + oidc, _ = redacted["oidc"].(map[string]any) + assert.Equal(t, "file:/run/secret", oidc["client_secret"]) + assert.Equal(t, "https://idp.example.com/realm", oidc["issuer"]) + + tls, _ = redacted["tls"].(map[string]any) + acme, _ = tls["acme"].(map[string]any) + assert.Equal(t, "https://acme.example.com/dir?x=1", acme["url"]) + + derp, _ = redacted["derp"].(map[string]any) + assert.Equal(t, []any{"https://derp.example/map", "https://derp.example/token", ""}, derp["urls"]) + + dns, _ = redacted["dns"].(map[string]any) + challenge, _ := dns["challenge"].(map[string]any) + assert.Equal(t, map[string]any{ //nolint:gosec // test markers, not credentials + "name": "acmedns", + "api_token": "", + "token": "file:/run/token", + "config": map[string]any{"a.example.com": map[string]any{"password": "", "name": ""}}, + "list": []any{""}, + }, challenge["provider"]) + // The caller's config is left alone. + assert.Equal(t, 123456, cfg.DNSConfig.Challenge.ProviderSettings["api_token"]) + assert.Equal(t, HTTPURL("https://user:pass@derp.example/map"), cfg.DERP.URLs[0]) + assert.Equal(t, HTTPURL("https://client:marker-issuer@idp.example.com/realm"), cfg.OIDC.Issuer) + + plainProvider, _ := plain["dns"].(map[string]any)["challenge"].(map[string]any)["provider"].(map[string]any) + assert.NotContains(t, plainProvider, "unset", "a typed nil is left out") + assert.NotContains(t, plainProvider, "nothing", "a typed nil is left out") +} + +// TestPlainMapLargeIntegers: JSON parsers read numbers as float64, so an +// integer past 2^53 is written as text, which the integer hook takes back. +func TestPlainMapLargeIntegers(t *testing.T) { + for _, n := range []int{1<<53 + 1, math.MaxInt64} { + for _, ext := range []string{".json", ".hujson"} { + t.Run(fmt.Sprintf("%d%s", n, ext), func(t *testing.T) { + loaded, err := decodeInput(t, "config.yaml", fmt.Sprintf("tuning:\n register_cache_max_entries: %d\n", n)) + require.NoError(t, err) + require.Equal(t, n, loaded.Tuning.RegisterCacheMaxEntries) + + parser, err := configParser(ext) + require.NoError(t, err) + + out, err := parser.Marshal(plainMap(loaded, false)) + require.NoError(t, err) + + again, err := decodeInput(t, "config"+ext, string(out)) + require.NoError(t, err) + assert.Equal(t, n, again.Tuning.RegisterCacheMaxEntries) + }) + } + } +} + +// TestBadConfigs pins how a bad value is reported: its key, what is wrong +// and where it was set. Every testdata/bad/.{yaml,env} is one row; +// refresh with HEADSCALE_UPDATE_GOLDEN=1. +func TestBadConfigs(t *testing.T) { + dir := filepath.Join("testdata", "bad") + update := os.Getenv("HEADSCALE_UPDATE_GOLDEN") != "" + + entries, err := os.ReadDir(dir) + require.NoError(t, err) + unsetHeadscaleEnv(t) + + for _, e := range entries { + ext := filepath.Ext(e.Name()) + if ext == ".golden" { + continue + } + + name := strings.TrimSuffix(e.Name(), ext) + t.Run(name, func(t *testing.T) { + var file, envFile string + if ext == ".env" { + envFile = filepath.Join(dir, e.Name()) + } else { + file = filepath.Join(dir, e.Name()) + } + + staged := stageCompat(t, file, envFile) + require.NoError(t, LoadConfig(staged, false)) + + _, err := LoadServerConfig() + require.Error(t, err) + + got := strings.ReplaceAll(err.Error(), staged, "$DIR") + gpath := filepath.Join(dir, name+".golden") + + if update { + require.NoError(t, os.WriteFile(gpath, []byte(got), 0o600)) + + return + } + + want, err := os.ReadFile(gpath) + require.NoErrorf(t, err, "missing golden %s: run with HEADSCALE_UPDATE_GOLDEN=1", gpath) + assert.Equal(t, string(want), got) + }) + } +} + +// TestPositionBounded: finding where a key is written follows the keys the +// file has, so a long dotted unknown key is located at once. +func TestPositionBounded(t *testing.T) { + key := strings.Repeat("x.", 29) + "x" + path := writeConfig(t, key+": 1\n") + + done := make(chan error, 1) + + go func() { + _, err := newConfigSource(path, true, nil) + done <- err + }() + + select { + case err := <-done: + require.ErrorContains(t, err, "unknown configuration key "+key+" ("+path+":1:1)") + case <-time.After(5 * time.Second): + t.Fatal("locating a dotted key took longer than reading the file") + } +} diff --git a/hscontrol/types/config_test.go b/hscontrol/types/config_test.go index eae59f8e1..fe661bff9 100644 --- a/hscontrol/types/config_test.go +++ b/hscontrol/types/config_test.go @@ -4,16 +4,12 @@ import ( "bytes" "encoding/json" "fmt" - "go/ast" - "go/parser" - "go/token" "io/fs" "maps" "net/netip" "net/url" "os" "path/filepath" - "strconv" "strings" "testing" "time" @@ -46,12 +42,12 @@ func TestReadConfig(t *testing.T) { name: "unmarshal-dns-full-config", configPath: "testdata/dns_full.yaml", setup: func(t *testing.T) (any, error) { //nolint:thelper - dns, err := dns() + cfg, err := src.decode() if err != nil { return nil, err } - return dns, nil + return cfg.DNSConfig, nil }, want: DNSConfig{ MagicDNS: true, @@ -75,18 +71,19 @@ func TestReadConfig(t *testing.T) { {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, }, SearchDomains: []string{"test.com", "bar.com"}, + Challenge: DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, }, }, { name: "dns-to-tailcfg.DNSConfig", configPath: "testdata/dns_full.yaml", setup: func(t *testing.T) (any, error) { //nolint:thelper - dns, err := dns() + cfg, err := src.decode() if err != nil { return nil, err } - return dnsToTailcfgDNS(dns), nil + return dnsToTailcfgDNS(cfg.DNSConfig), nil }, want: &tailcfg.DNSConfig{ Proxied: true, @@ -112,12 +109,12 @@ func TestReadConfig(t *testing.T) { name: "unmarshal-dns-full-no-magic", configPath: "testdata/dns_full_no_magic.yaml", setup: func(t *testing.T) (any, error) { //nolint:thelper - dns, err := dns() + cfg, err := src.decode() if err != nil { return nil, err } - return dns, nil + return cfg.DNSConfig, nil }, want: DNSConfig{ MagicDNS: false, @@ -141,18 +138,19 @@ func TestReadConfig(t *testing.T) { {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, }, SearchDomains: []string{"test.com", "bar.com"}, + Challenge: DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, }, }, { name: "dns-to-tailcfg.DNSConfig", configPath: "testdata/dns_full_no_magic.yaml", setup: func(t *testing.T) (any, error) { //nolint:thelper - dns, err := dns() + cfg, err := src.decode() if err != nil { return nil, err } - return dnsToTailcfgDNS(dns), nil + return dnsToTailcfgDNS(cfg.DNSConfig), nil }, want: &tailcfg.DNSConfig{ Proxied: false, @@ -215,17 +213,12 @@ func TestReadConfig(t *testing.T) { name: "dns-override-true", configPath: "testdata/dns-override-true.yaml", setup: func(t *testing.T) (any, error) { //nolint:thelper - _, err := LoadServerConfig() + cfg, err := LoadServerConfig() if err != nil { return nil, err } - dns, err := dns() - if err != nil { - return nil, err - } - - return dnsToTailcfgDNS(dns), nil + return dnsToTailcfgDNS(cfg.DNSConfig), nil }, want: &tailcfg.DNSConfig{ Proxied: true, @@ -296,9 +289,11 @@ func TestReadConfigFromEnv(t *testing.T) { "HEADSCALE_PREFIXES_V4": "100.64.0.0/10", }, setup: func(t *testing.T) (any, error) { //nolint:thelper // inline test closure - assert.Equal(t, "trace", src.String("log.level")) - assert.Equal(t, "100.64.0.0/10", src.String("prefixes.v4")) - assert.False(t, src.Bool("database.sqlite.write_ahead_log")) + cfg, err := src.decode() + require.NoError(t, err) + assert.Equal(t, LogLevel("trace"), cfg.Log.Level) + assert.Equal(t, "100.64.0.0/10", cfg.Prefixes.V4.String()) + assert.False(t, cfg.Database.Sqlite.WriteAheadLog) return nil, nil //nolint:nilnil // test setup returns nil to indicate no expected value }, @@ -318,12 +313,12 @@ func TestReadConfigFromEnv(t *testing.T) { // "HEADSCALE_DNS_EXTRA_RECORDS": `[{ name: "prometheus.myvpn.example.com", type: "A", value: "100.64.0.4" }]`, }, setup: func(t *testing.T) (any, error) { //nolint:thelper // inline test closure - dns, err := dns() + cfg, err := src.decode() if err != nil { return nil, err } - return dns, nil + return cfg.DNSConfig, nil }, want: DNSConfig{ MagicDNS: true, @@ -339,6 +334,7 @@ func TestReadConfigFromEnv(t *testing.T) { // {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, // }, SearchDomains: []string{"test.com", "bar.com"}, + Challenge: DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, }, }, } @@ -372,7 +368,7 @@ tls: cert_path: abc.pem acme: hostname: example.com - challenge_type: bogus + challenge_type: "" noise: private_key_path: noise_private.key`) @@ -454,8 +450,8 @@ oidc: require.NoError(t, err) err = validateServerConfig() - require.Error(t, err) - assert.Contains(t, err.Error(), errInvalidPKCEMethod.Error()) + require.ErrorIs(t, err, errInvalidPKCEMethod) + assert.Contains(t, err.Error(), "oidc.pkce.method has an unsupported value") } // TestOIDCConfigValidation covers the issuer-URL and required-field checks that @@ -472,7 +468,7 @@ func TestOIDCConfigValidation(t *testing.T) { issuer: ftp://idp.example.com client_id: headscale client_secret: sekret`, - wantErr: "valid http(s) URL", + wantErr: "oidc.issuer is not an http:// or https:// URL", }, { name: "missing client_id", @@ -505,6 +501,8 @@ func TestOIDCConfigValidation(t *testing.T) { noise: private_key_path: noise_private.key server_url: http://127.0.0.1:8080 +prefixes: + v4: 100.64.0.0/10 dns: magic_dns: false override_local_dns: false @@ -558,6 +556,8 @@ tls: http_listen: %q noise: private_key_path: noise_private.key +prefixes: + v4: 100.64.0.0/10 database: type: sqlite3 dns: @@ -606,7 +606,7 @@ dns: require.Error(t, err) got := ConfigErrors(err) - assert.GreaterOrEqual(t, len(got), 4, + assert.GreaterOrEqual(t, len(got), 3, "expected sub-builder errors collected into one report; got %d: %v", len(got), reasons(got)) @@ -1148,10 +1148,10 @@ func TestSafeServerURLWithPort(t *testing.T) { // TestConfigJSONOmitsSecrets verifies that marshalling a [Config] to JSON // (as /debug/config does via [state.State.DebugConfig]) or logging it does -// not leak the Postgres password, the OIDC client secret, or the headscale -// admin API key. Operators who widen metrics_listen_addr to 0.0.0.0 should -// not be able to read these back via debug endpoints reachable over -// CGNAT/loopback. +// not leak the Postgres password, the OIDC client secret, the headscale +// admin API key, or URL credentials. Operators who widen +// metrics_listen_addr to 0.0.0.0 should not be able to read these back via +// debug endpoints reachable over CGNAT/loopback. func TestConfigJSONOmitsSecrets(t *testing.T) { const ( secretPostgresPass = "p0stgres-secret-marker" @@ -1162,9 +1162,16 @@ func TestConfigJSONOmitsSecrets(t *testing.T) { secretDERPPass = "derp-url-password-marker" //nolint:gosec // test marker, not a real credential secretDERPToken = "derp-url-token-marker" //nolint:gosec // test marker, not a real credential derpMapMarker = "go-only-derp-map-marker" + secretServerUser = "server-url-username-marker" //nolint:gosec // test marker, not a real credential + secretServerPass = "server-url-password-marker" //nolint:gosec // test marker, not a real credential + secretCLIToken = "cli-address-token-marker" //nolint:gosec // test marker, not a real credential + secretDNSUser = "dns-url-username-marker" //nolint:gosec // test marker, not a real credential + secretDNSPass = "dns-url-password-marker" //nolint:gosec // test marker, not a real credential + secretSplitToken = "split-url-token-marker" //nolint:gosec // test marker, not a real credential ) cfg := &Config{ + ServerURL: "https://" + secretServerUser + ":" + secretServerPass + "@headscale.example.com", Database: DatabaseConfig{ Postgres: PostgresConfig{ Pass: secretPostgresPass, @@ -1174,12 +1181,19 @@ func TestConfigJSONOmitsSecrets(t *testing.T) { ClientSecret: secretClientSecret, }, CLI: CLIConfig{ - APIKey: secretAPIKey, + Address: "https://" + secretCLIToken + "@cli.example.com", + APIKey: secretAPIKey, + }, + DNSConfig: DNSConfig{ + Nameservers: Nameservers{ + Global: []string{"1.1.1.1", "https://" + secretDNSUser + ":" + secretDNSPass + "@dns.example.com/dns-query"}, + Split: map[string][]string{"corp.example.com": {"https://" + secretSplitToken + "@dns.example.com/q"}}, + }, + Challenge: DNSChallengeConfig{ + ProviderSettings: map[string]any{"name": "cloudflare", "api_token": secretDNSToken}, + Provider: &cloudflare.Provider{APIToken: secretDNSToken}, + }, }, - DNSConfig: DNSConfig{Challenge: DNSChallengeConfig{ - ProviderSettings: map[string]any{"name": "cloudflare", "api_token": secretDNSToken}, - Provider: &cloudflare.Provider{APIToken: secretDNSToken}, - }}, DERP: DERPConfig{ URLs: []HTTPURL{ "https://" + secretDERPUser + ":" + secretDERPPass + "@derp.example/map", @@ -1194,6 +1208,7 @@ func TestConfigJSONOmitsSecrets(t *testing.T) { secrets := []string{ secretPostgresPass, secretClientSecret, secretAPIKey, secretDNSToken, secretDERPUser, secretDERPPass, secretDERPToken, + secretServerUser, secretServerPass, secretCLIToken, secretDNSUser, secretDNSPass, secretSplitToken, } out, err := json.Marshal(cfg) @@ -1204,11 +1219,19 @@ func TestConfigJSONOmitsSecrets(t *testing.T) { "marshalled Config must not contain secret %q", secret) } - // The URLs are still shown, without userinfo; the request keeps it. + // The URLs are still shown, without userinfo; the running values keep it. assert.Contains(t, string(out), `"https://derp.example/map"`) assert.Contains(t, string(out), `"https://derp.example/token-map"`) + assert.Contains(t, string(out), `"ServerURL":"https://headscale.example.com"`) + assert.Contains(t, string(out), `"Address":"https://cli.example.com"`) + assert.Contains(t, string(out), `"Global":["1.1.1.1","https://dns.example.com/dns-query"]`) + assert.Contains(t, string(out), `"Split":{"corp.example.com":["https://dns.example.com/q"]}`) assert.Contains(t, string(cfg.DERP.URLs[0]), secretDERPUser+":"+secretDERPPass) assert.Contains(t, string(cfg.DERP.URLs[1]), secretDERPToken) + assert.Contains(t, cfg.ServerURL, secretServerUser+":"+secretServerPass) + assert.Contains(t, cfg.CLI.Address, secretCLIToken) + assert.Contains(t, cfg.DNSConfig.Nameservers.Global[1], secretDNSUser+":"+secretDNSPass) + assert.Contains(t, cfg.DNSConfig.Nameservers.Split["corp.example.com"][0], secretSplitToken) // DERPMap is Go-only. assert.NotContains(t, string(out), derpMapMarker) @@ -1276,17 +1299,17 @@ func TestTrustedProxies(t *testing.T) { { name: "bare-ip-rejected", input: []string{"10.0.0.1"}, - wantErr: `trusted_proxies[0] "10.0.0.1"`, + wantErr: `netip.ParsePrefix("10.0.0.1")`, }, { - name: "garbage-reports-index", + name: "garbage-names-the-value", input: []string{"10.0.0.0/16", "not-an-ip"}, - wantErr: `trusted_proxies[1] "not-an-ip"`, + wantErr: `trusted_proxies is not a valid CIDR`, }, { name: "ipv4-zero-rejected", input: []string{"0.0.0.0/0"}, - wantErr: "0.0.0.0/0 and ::/0 are not allowed", + wantErr: `trusted_proxies[0] "0.0.0.0/0": 0.0.0.0/0 and ::/0 are not allowed`, }, { name: "ipv6-zero-rejected", @@ -1297,14 +1320,21 @@ func TestTrustedProxies(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - values := map[string]any{} + values := map[string]any{"prefixes.v4": "100.64.0.0/10"} if tt.input != nil { values["trusted_proxies"] = tt.input } useConfig(t, values) - got, err := trustedProxies() + var got []netip.Prefix + + cfg, err := src.decode() + if err == nil { + v := &configValidator{} + cfg.validatePrefixes(v) + got, err = cfg.TrustedProxies, v.Err() + } if tt.wantErr != "" { require.Error(t, err) @@ -1315,13 +1345,30 @@ func TestTrustedProxies(t *testing.T) { require.NoError(t, err) - if diff := cmp.Diff(tt.want, got, cmpopts.EquateComparable(netip.Prefix{})); diff != "" { - t.Errorf("trustedProxies() mismatch (-want +got):\n%s", diff) + if diff := cmp.Diff(tt.want, got, cmpopts.EquateComparable(netip.Prefix{}), cmpopts.EquateEmpty()); diff != "" { + t.Errorf("trusted_proxies mismatch (-want +got):\n%s", diff) } }) } } +// TestNameserverWarningsHideCredentials: a nameserver that does not parse +// is reported without the userinfo it may hold. +func TestNameserverWarningsHideCredentials(t *testing.T) { + cfg := &Config{DNSConfig: DNSConfig{Nameservers: Nameservers{ + Global: []string{"https://alice:marker-pass@dns.example.com:port"}, + Split: map[string][]string{"corp.example.com": {"https://marker-user@dns.example.com/%zz"}}, + }}} + + warnings := cfg.Warnings() + require.Len(t, warnings, 2) + + for _, w := range warnings { + assert.Contains(t, w, "invalid nameserver") + assert.NotContains(t, w, "marker") + } +} + // DNS names are case-insensitive, but MagicDNS resolution in clients matches // extra records by exact name, so mixed-case record names never resolve. func TestExtraRecordsAreLowercased(t *testing.T) { @@ -1687,14 +1734,14 @@ dns: wantToken: "good", }, { - // A failed decode can still fill part of the map; none of it is used. + // Named, never shown: the value holds credentials. name: "provider env json that does not decode", yaml: base, env: map[string]string{ "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"api_token":"marker-token","zone_token":1e1000}`, }, - wantErr: "dns.challenge.provider is invalid", + wantErr: "dns.challenge.provider is not a JSON object (env HEADSCALE_DNS_CHALLENGE_PROVIDER)", }, { // The removed field warns and is dropped; the rest still loads. @@ -2372,7 +2419,7 @@ dns: // as the CLI config does, for DumpConfig and embedders. func TestServerConfigDisableUpdateCheck(t *testing.T) { for _, want := range []bool{true, false} { - path := writeConfig(t, "config.yaml", fmt.Sprintf(`server_url: https://headscale.example.com + path := writeConfig(t, fmt.Sprintf(`server_url: https://headscale.example.com noise: private_key_path: noise_private.key prefixes: @@ -2392,49 +2439,6 @@ disable_check_updates: %t } } -func TestBoolConfigKeysComplete(t *testing.T) { - file, err := parser.ParseFile(token.NewFileSet(), "config.go", nil, 0) - require.NoError(t, err) - - keys := make(map[string]bool) - - ast.Inspect(file, func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - - selector, ok := call.Fun.(*ast.SelectorExpr) - if !ok || selector.Sel.Name != "Bool" { - return true - } - - object, ok := selector.X.(*ast.Ident) - if !ok || object.Name != "src" { - return true - } - - require.Len(t, call.Args, 1) - literal, ok := call.Args[0].(*ast.BasicLit) - require.True(t, ok, "boolean config keys must be string literals") - - key, err := strconv.Unquote(literal.Value) - require.NoError(t, err) - - keys[key] = true - - return true - }) - require.NotEmpty(t, keys) - - for _, key := range boolConfigKeys { - assert.Contains(t, keys, key, "unused or repeated boolean config key") - delete(keys, key) - } - - assert.Empty(t, keys, "Bool keys missing from boolean validation") -} - func TestExampleConfigBooleanSettings(t *testing.T) { require.NoError(t, LoadConfig("../../config-example.yaml", true)) @@ -2569,7 +2573,8 @@ func unsetHeadscaleEnv(t *testing.T) { // for the nixpkgs module with only server_url, dns.base_domain and // dns.nameservers.global set; hsic-* mirror integration/hsic's // MinimumConfigYAML and DefaultConfigEnv. -strict is the same source -// as updated for the strict loader. +// as updated for the strict loader, -pruned without the keys removed +// for having no effect. func TestConfigCompat(t *testing.T) { rows := compatRows(t) update := os.Getenv("HEADSCALE_UPDATE_GOLDEN") != "" @@ -2784,7 +2789,6 @@ type compatPolicyConfig struct { } type compatTuning struct { - NotifierSendTimeout time.Duration BatchChangeDelay time.Duration NodeMapSessionBufferedChanSize int BatcherWorkers int @@ -2819,11 +2823,22 @@ func compatView(cfg *Config) compatConfig { tuning := compatTuning(cfg.Tuning) tuning.BatcherWorkers = cfg.Tuning.BatcherWorkerCount() + // Today's loader read an empty list as nil and an unset map as empty. + trusted := cfg.TrustedProxies + if len(trusted) == 0 { + trusted = nil + } + + extraParams := oidc.ExtraParams + if extraParams == nil { + extraParams = map[string]string{} + } + return compatConfig{ ServerURL: cfg.ServerURL, Addr: string(cfg.Addr), MetricsAddr: string(cfg.MetricsAddr), - TrustedProxies: cfg.TrustedProxies, + TrustedProxies: trusted, Node: compatNodeConfig{ Expiry: cfg.Node.Expiry, Ephemeral: compatEphemeralConfig(cfg.Node.Ephemeral), @@ -2920,7 +2935,7 @@ func compatView(cfg *Config) compatConfig { Issuer: string(oidc.Issuer), ClientID: oidc.ClientID, Scope: oidc.Scope, - ExtraParams: oidc.ExtraParams, + ExtraParams: extraParams, AllowedDomains: oidc.AllowedDomains, AllowedUsers: oidc.AllowedUsers, AllowedGroups: oidc.AllowedGroups, @@ -3180,8 +3195,9 @@ func TestConfigNotLoaded(t *testing.T) { func useConfig(t *testing.T, values map[string]any) { t.Helper() + d := defaultConfig() s := &configSource{k: koanf.New(".")} - require.NoError(t, s.k.Load(confmap.Provider(configDefaults, "."), nil)) + require.NoError(t, s.k.Load(confmap.Provider(plainMap(&d, false), ""), nil)) require.NoError(t, s.k.Load(confmap.Provider(values, "."), nil)) old := src @@ -3296,3 +3312,201 @@ func TestDerivedValues(t *testing.T) { resolvers(written...).ResolverAddrs() assert.Equal(t, []string{"9.9.9.9"}, written) } + +// validateServerConfig decodes the loaded configuration and validates it. +func validateServerConfig() error { + cfg, err := src.decode() + if err != nil { + return err + } + + return cfg.Validate() +} + +// TestValidate pins the rules on loaded configs and the order they report +// in. +func TestValidate(t *testing.T) { + const valid = `server_url: https://headscale.example.com +noise: + private_key_path: noise_private.key +prefixes: + v4: 100.64.0.0/10 +database: + type: sqlite +dns: + magic_dns: false + override_local_dns: false +` + + tests := []struct { + name string + file string + want []string // reasons, in order + }{ + {name: "valid", file: valid}, + { + // What the NixOS module writes: an explicit empty default. + name: "empty extra records next to a path", + file: valid + " extra_records: []\n extra_records_path: /var/lib/headscale/records.json\n", + }, + { + name: "extra records next to a path", + file: valid + " extra_records: [{name: a.example.com, type: A, value: 100.64.0.1}]\n extra_records_path: /var/lib/headscale/records.json\n", + want: []string{"dns.extra_records and dns.extra_records_path are mutually exclusive"}, + }, + { + name: "every rule, in order", + file: `server_url: headscale.example.com +listen_addr: 0.0.0.0:8080 +metrics_listen_addr: 0.0.0.0:8080 +trusted_proxies: [0.0.0.0/0] +prefixes: + v4: "" + allocation: "" +policy: + mode: "" +node: + ephemeral: + inactivity_timeout: 30s + routes: + ha: + probe_interval: 1s +tuning: + node_store_batch_size: 0 +derp: + server: + enabled: true + private_key_path: /var/lib/headscale/derp.key +database: + type: "" +dns: + magic_dns: true + override_local_dns: true + extra_records: [{name: a.example.com, type: A, value: 100.64.0.1}] + extra_records_path: /var/lib/headscale/records.json + challenge: + provider: + name: bogus +tls: + cert_path: /etc/headscale/cert.pem + acme: + hostname: example.com + challenge_type: "" +oidc: + issuer: https://idp.example.com + client_id: headscale + client_secret: secret + pkce: + method: "" +`, + want: []string{ + "OIDC configuration is invalid", + "dns.extra_records and dns.extra_records_path are mutually exclusive", + "tls.acme.hostname and tls.cert_path/tls.key_path are mutually exclusive", + "noise.private_key_path is required", + "listen_addr and metrics_listen_addr would bind the same TCP socket", + "tls.acme.challenge_type has an unsupported value", + "server_url is missing a scheme", + "node.ephemeral.inactivity_timeout is below the minimum", + "dns.nameservers.global is required when dns.override_local_dns is true", + "node.routes.ha.probe_interval is below the minimum", + "node.routes.ha.probe_timeout must be less than node.routes.ha.probe_interval", + "tuning.node_store_batch_size must be positive", + "derp.server.stun_listen_addr is required when the embedded DERP server is enabled", + "database.type has an unsupported value", + "policy.mode has an unsupported value", + "dns.base_domain is required when dns.magic_dns is true", + "trusted_proxies contains an invalid CIDR", + "no IP prefix configured for the tailnet", + "prefixes.allocation has an unsupported value", + "dns.challenge.provider is invalid", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.NoError(t, LoadConfig(writeConfig(t, tt.file), true)) + + cfg, err := src.decode() + require.NoError(t, err) + + err = cfg.Validate() + if len(tt.want) == 0 { + require.NoError(t, err) + + return + } + + assert.Equal(t, tt.want, reasons(ConfigErrors(err))) + }) + } +} + +// TestProviderNameShape: a provider name that is a map or list is rejected +// by its key alone, as its contents may be credentials, and never shown. +func TestProviderNameShape(t *testing.T) { + const base = `server_url: https://headscale.example.com +noise: + private_key_path: noise_private.key +prefixes: + v4: 100.64.0.0/10 +database: + type: sqlite +dns: + magic_dns: true + override_local_dns: false + base_domain: ts.example.com + certificates: + enabled: true +` + + for name, tt := range map[string]struct { + yaml string + env map[string]string + }{ + "file map": {yaml: base + " challenge:\n provider:\n name: {api_token: marker-secret}\n"}, + "file list": {yaml: base + " challenge:\n provider:\n name: [marker-secret]\n"}, + "env json": {yaml: base, env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"name":{"api_token":"marker-secret"}}`}}, + } { + t.Run(name, func(t *testing.T) { + for k, v := range tt.env { + t.Setenv(k, v) + } + + require.NoError(t, LoadConfig(writeConfig(t, tt.yaml), true)) + + cfg, err := src.decode() + require.NoError(t, err) + + out, err := json.Marshal(plainMap(cfg, true)) + require.NoError(t, err) + assert.NotContains(t, string(out), "marker-secret") + + _, err = LoadServerConfig() + require.Error(t, err) + assert.Equal(t, []string{"dns.challenge.provider.name must be a provider name"}, reasons(ConfigErrors(err))) + assert.NotContains(t, err.Error(), "marker-secret") + }) + } + + // A scalar name is coerced to its text, as before. + assert.Equal(t, "123", DNSChallengeConfig{ProviderSettings: map[string]any{"name": 123}}.ProviderName()) +} + +// TestUpdateCheckDisabled: a configuration that does not decode never runs +// the update check, whatever it says; the command then reports the error. +func TestUpdateCheckDisabled(t *testing.T) { + for _, tt := range []struct { + file string + want bool + }{ + {"disable_check_updates: true\n", true}, + {"disable_check_updates: false\n", false}, + {"disable_check_updates: true\nlog:\n level: verbose\n", true}, + {"disable_check_updates: false\nlog:\n level: verbose\n", true}, + } { + require.NoError(t, LoadConfig(writeConfig(t, tt.file), true)) + assert.Equalf(t, tt.want, UpdateCheckDisabled(), "%q", tt.file) + } +} diff --git a/hscontrol/types/config_values.go b/hscontrol/types/config_values.go index 8b010651b..3db3bdb33 100644 --- a/hscontrol/types/config_values.go +++ b/hscontrol/types/config_values.go @@ -90,22 +90,32 @@ func (m FileMode) MarshalText() ([]byte, error) { // text form stays as written. type HTTPURL string //nolint:recvcheck // UnmarshalText needs a pointer; the rest read the value -// MarshalJSON writes u without userinfo, for debug output and logs: a -// username can be a token too. One that does not parse is withheld, as its -// userinfo cannot be found. +// MarshalJSON writes u [HTTPURL.Redacted], for debug output and logs. func (u HTTPURL) MarshalJSON() ([]byte, error) { - s := string(u) - if s != "" { - p, err := url.Parse(s) - if err != nil { - s = "" - } else { - p.User = nil - s = p.String() - } + return json.Marshal(u.Redacted()) +} + +// Redacted is u without userinfo: a username can be a token too. One that +// is not an absolute http(s) URL with a host is withheld, as its userinfo +// cannot be told from the rest (https://u:p?@h fails to parse, +// https:/u:p@h parses as a path); one without userinfo is kept as written. +func (u HTTPURL) Redacted() string { + if u == "" { + return "" } - return json.Marshal(s) + p, err := url.Parse(string(u)) + if err != nil || (p.Scheme != "http" && p.Scheme != "https") || p.Hostname() == "" { + return "" + } + + if p.User == nil { + return string(u) + } + + p.User = nil + + return p.String() } func (u *HTTPURL) UnmarshalText(text []byte) error { diff --git a/hscontrol/types/config_values_test.go b/hscontrol/types/config_values_test.go index bd6cd941f..afd933e78 100644 --- a/hscontrol/types/config_values_test.go +++ b/hscontrol/types/config_values_test.go @@ -46,6 +46,9 @@ func TestConfigValues(t *testing.T) { {name: "listenaddr/named port", dst: new(ListenAddr), in: "0.0.0.0:http", want: ListenAddr("0.0.0.0:http")}, {name: "listenaddr/no colon", dst: new(ListenAddr), in: "8080", wantErr: "host:port"}, {name: "listenaddr/bad port", dst: new(ListenAddr), in: "h:abc", wantErr: "host:port"}, + {name: "listenaddr/port above range", dst: new(ListenAddr), in: "h:65536", wantErr: "host:port"}, + {name: "listenaddr/negative port", dst: new(ListenAddr), in: "h:-1", wantErr: "host:port"}, + {name: "listenaddr/highest port", dst: new(ListenAddr), in: "h:65535", want: ListenAddr("h:65535")}, {name: "loglevel/trace", dst: new(LogLevel), in: "trace", want: LogLevel("trace")}, {name: "loglevel/debug", dst: new(LogLevel), in: "debug", want: LogLevel("debug")}, diff --git a/hscontrol/types/derp_config_test.go b/hscontrol/types/derp_config_test.go index a797d309d..11cdcb4f8 100644 --- a/hscontrol/types/derp_config_test.go +++ b/hscontrol/types/derp_config_test.go @@ -2,22 +2,19 @@ package types import ( "testing" + + "github.com/stretchr/testify/require" ) -// TestDerpConfigSkipsMalformedURL ensures a malformed derp.urls entry is -// skipped (as the "ignoring..." log promises) rather than dereferencing the -// nil *url.URL that url.Parse returns on error, which crashed the server at -// startup. -func TestDerpConfigSkipsMalformedURL(t *testing.T) { +// TestDERPURLsMalformed: a malformed derp.urls entry stops the load, naming +// the key, instead of being skipped, as it once was after crashing startup +// on the nil *url.URL url.Parse returns. +func TestDERPURLsMalformed(t *testing.T) { useConfig(t, map[string]any{"derp.urls": []string{ "https://controlplane.tailscale.com/derpmap/default", "://bad", }}) - cfg := derpConfig() - - if len(cfg.URLs) != 1 { - t.Fatalf("expected the malformed derp.urls entry to be skipped, got %d urls: %v", - len(cfg.URLs), cfg.URLs) - } + _, err := src.decode() + require.ErrorContains(t, err, "derp.urls is not an http:// or https:// URL") } diff --git a/hscontrol/types/listener.go b/hscontrol/types/listener.go index 87bebf0d5..900e94b51 100644 --- a/hscontrol/types/listener.go +++ b/hscontrol/types/listener.go @@ -30,7 +30,7 @@ func (e *ListenerBindError) Error() string { func (e *ListenerBindError) Unwrap() error { return e.Err } // PortFromAddr resolves the numeric port of a TCP listen address. -// Accepts host:port form with either a numeric port or one of the named +// Accepts host:port form with either a numeric port from 0 to 65535 or one of the named // services "http" / "https". The named-service table is intentionally // hardcoded so this stays a pure string->int mapping with no network or // /etc/services lookups. @@ -51,12 +51,12 @@ func PortFromAddr(addr string) (int, error) { return 443, nil } - p, err := strconv.Atoi(port) + p, err := strconv.ParseUint(port, 10, 16) if err != nil { return 0, fmt.Errorf("parse port from %q: %w", addr, err) } - return p, nil + return int(p), nil } // listenersOverlap reports whether two parsed TCP listen addresses would @@ -95,7 +95,7 @@ func isWildcardHost(h string) bool { // produces a ConfigError tied to its own YAML key, so an operator can // see exactly which value to fix instead of guessing from a paired // comparison. -func validateListenerCollisions(v *configValidator) { +func (c *Config) validateListenerCollisions(v *configValidator) { type spec struct { key, addr string host string @@ -104,9 +104,9 @@ func validateListenerCollisions(v *configValidator) { active bool } - listenAddr := src.String("listen_addr") - metricsAddr := src.String("metrics_listen_addr") - acmeAddr := src.String("tls.acme.http_listen") + listenAddr := string(c.Addr) + metricsAddr := string(c.MetricsAddr) + acmeAddr := string(c.TLS.ACME.HTTPListen) listeners := []spec{ {key: "listen_addr", addr: listenAddr, active: listenAddr != ""}, @@ -115,8 +115,8 @@ func validateListenerCollisions(v *configValidator) { key: "tls.acme.http_listen", addr: acmeAddr, active: acmeAddr != "" && - src.String("tls.acme.hostname") != "" && - ACMEChallenge(src.String("tls.acme.challenge_type")) == HTTP01ChallengeType, + c.TLS.ACME.Hostname != "" && + c.TLS.ACME.ChallengeType == HTTP01ChallengeType, }, } diff --git a/hscontrol/types/listener_test.go b/hscontrol/types/listener_test.go index 6b5c6c497..26908fa81 100644 --- a/hscontrol/types/listener_test.go +++ b/hscontrol/types/listener_test.go @@ -30,6 +30,10 @@ func TestPortFromAddr(t *testing.T) { {"empty", "", 0, true}, {"no-port", "0.0.0.0", 0, true}, {"unknown-named", ":bogus", 0, true}, + {"highest-port", ":65535", 65535, false}, + {"port-above-range", ":65536", 0, true}, + {"negative-port", ":-1", 0, true}, + {"signed-port", ":+80", 0, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -86,10 +90,19 @@ func TestValidateListenerCollisions_BlamesParseFailure(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - useConfig(t, map[string]any{tt.bad: tt.badAddr, tt.good: ":9999"}) + // Decoding rejects these; Go data reaches the rule as is. + cfg := &Config{} + for _, f := range ConfigFields(cfg) { + switch f.Path { + case tt.bad: + f.Value.SetString(tt.badAddr) + case tt.good: + f.Value.SetString(":9999") + } + } v := &configValidator{} - validateListenerCollisions(v) + cfg.validateListenerCollisions(v) err := v.Err() require.Error(t, err) diff --git a/hscontrol/types/testdata/bad/duration-number.golden b/hscontrol/types/testdata/bad/duration-number.golden new file mode 100644 index 000000000..76aec416d --- /dev/null +++ b/hscontrol/types/testdata/bad/duration-number.golden @@ -0,0 +1,3 @@ +Fatal config error: node.expiry needs a unit ($DIR/config.yaml:2:3) + current: node.expiry: 90 + hint: write the unit, e.g. "30s", "5m" or "90d" diff --git a/hscontrol/types/testdata/bad/duration-number.yaml b/hscontrol/types/testdata/bad/duration-number.yaml new file mode 100644 index 000000000..bbf821284 --- /dev/null +++ b/hscontrol/types/testdata/bad/duration-number.yaml @@ -0,0 +1,2 @@ +node: + expiry: 90 diff --git a/hscontrol/types/testdata/bad/enum.golden b/hscontrol/types/testdata/bad/enum.golden new file mode 100644 index 000000000..6417a8513 --- /dev/null +++ b/hscontrol/types/testdata/bad/enum.golden @@ -0,0 +1,5 @@ +Fatal config error: database.type has an unsupported value ($DIR/config.yaml:2:3) + current: database.type: "mysql" + allowed: "sqlite", "sqlite3", "postgres" + hint: pick one of the allowed values; sqlite is the default for single-host deployments + see: https://headscale.net/stable/ref/database/ diff --git a/hscontrol/types/testdata/bad/enum.yaml b/hscontrol/types/testdata/bad/enum.yaml new file mode 100644 index 000000000..f234c6471 --- /dev/null +++ b/hscontrol/types/testdata/bad/enum.yaml @@ -0,0 +1,2 @@ +database: + type: mysql diff --git a/hscontrol/types/testdata/bad/env-duration.env b/hscontrol/types/testdata/bad/env-duration.env new file mode 100644 index 000000000..09e14a4e4 --- /dev/null +++ b/hscontrol/types/testdata/bad/env-duration.env @@ -0,0 +1 @@ +HEADSCALE_TUNING_BATCH_CHANGE_DELAY=800 diff --git a/hscontrol/types/testdata/bad/env-duration.golden b/hscontrol/types/testdata/bad/env-duration.golden new file mode 100644 index 000000000..3b7f3865e --- /dev/null +++ b/hscontrol/types/testdata/bad/env-duration.golden @@ -0,0 +1,3 @@ +Fatal config error: tuning.batch_change_delay needs a unit (env HEADSCALE_TUNING_BATCH_CHANGE_DELAY) + current: tuning.batch_change_delay: "800" + hint: write the unit, e.g. "30s", "5m" or "90d" diff --git a/hscontrol/types/testdata/bad/extra-records-field.golden b/hscontrol/types/testdata/bad/extra-records-field.golden new file mode 100644 index 000000000..334789391 --- /dev/null +++ b/hscontrol/types/testdata/bad/extra-records-field.golden @@ -0,0 +1,2 @@ +Fatal config error: dns.extra_records has invalid keys: ttl ($DIR/config.yaml:2:3) + current: dns.extra_records: [{"name":"a.example.com","ttl":60,"type":"A","value":"100.64.0.1"}] diff --git a/hscontrol/types/testdata/bad/extra-records-field.yaml b/hscontrol/types/testdata/bad/extra-records-field.yaml new file mode 100644 index 000000000..57fc6fd3f --- /dev/null +++ b/hscontrol/types/testdata/bad/extra-records-field.yaml @@ -0,0 +1,6 @@ +dns: + extra_records: + - name: a.example.com + type: A + value: 100.64.0.1 + ttl: 60 diff --git a/hscontrol/types/testdata/bad/filemode-number.golden b/hscontrol/types/testdata/bad/filemode-number.golden new file mode 100644 index 000000000..2a1b72768 --- /dev/null +++ b/hscontrol/types/testdata/bad/filemode-number.golden @@ -0,0 +1,4 @@ +Fatal config error: unix_socket_permission must be quoted ($DIR/config.yaml:1:1) + current: unix_socket_permission: 504 + maximum: "0777" + hint: quote the value, e.g. "0770"; unquoted, YAML reads 0770 as the number 504 diff --git a/hscontrol/types/testdata/bad/filemode-number.yaml b/hscontrol/types/testdata/bad/filemode-number.yaml new file mode 100644 index 000000000..83e4211f7 --- /dev/null +++ b/hscontrol/types/testdata/bad/filemode-number.yaml @@ -0,0 +1 @@ +unix_socket_permission: 0770 diff --git a/hscontrol/types/testdata/bad/url.golden b/hscontrol/types/testdata/bad/url.golden new file mode 100644 index 000000000..f73d22395 --- /dev/null +++ b/hscontrol/types/testdata/bad/url.golden @@ -0,0 +1,2 @@ +Fatal config error: derp.urls is not an http:// or https:// URL ($DIR/config.yaml:2:3) + hint: write the scheme and host, e.g. "https://example.com" diff --git a/hscontrol/types/testdata/bad/url.yaml b/hscontrol/types/testdata/bad/url.yaml new file mode 100644 index 000000000..3f989f6b5 --- /dev/null +++ b/hscontrol/types/testdata/bad/url.yaml @@ -0,0 +1,4 @@ +derp: + urls: + - https://derp.example.com/derp.json + - "://not-a-url" diff --git a/hscontrol/types/testdata/compat/bad-policy-mode.golden.json b/hscontrol/types/testdata/compat/bad-policy-mode.golden.json index edde87c38..d23f641c9 100644 --- a/hscontrol/types/testdata/compat/bad-policy-mode.golden.json +++ b/hscontrol/types/testdata/compat/bad-policy-mode.golden.json @@ -1,3 +1,3 @@ { - "error": "Fatal config error: policy.mode has an unsupported value\n current: policy.mode: \"databse\"\n allowed: \"file\", \"database\"\n see: https://headscale.net/stable/ref/policy/\n" + "error": "Fatal config error: policy.mode has an unsupported value ($DIR/config.yaml:15:3)\n current: policy.mode: \"databse\"\n allowed: \"file\", \"database\"\n see: https://headscale.net/stable/ref/policy/\n" } diff --git a/hscontrol/types/testdata/compat/bad-values-pruned.golden.json b/hscontrol/types/testdata/compat/bad-values-pruned.golden.json new file mode 100644 index 000000000..ffc26e5ca --- /dev/null +++ b/hscontrol/types/testdata/compat/bad-values-pruned.golden.json @@ -0,0 +1,3 @@ +{ + "error": "Fatal config error: log.level has an unsupported value ($DIR/config.yaml:15:3)\n current: log.level: \"verbose\"\n allowed: \"trace\", \"debug\", \"info\", \"warn\", \"error\", \"fatal\", \"panic\", \"disabled\"\n\nFatal config error: log.format has an unsupported value ($DIR/config.yaml:16:3)\n current: log.format: \"xml\"\n allowed: \"text\", \"json\"\n\nFatal config error: derp.urls is not an http:// or https:// URL ($DIR/config.yaml:20:3)\n hint: write the scheme and host, e.g. \"https://example.com\"\n\nFatal config error: tuning.batch_change_delay is not a duration ($DIR/config.yaml:18:3)\n current: tuning.batch_change_delay: \"abc\"\n hint: write the unit, e.g. \"30s\", \"5m\" or \"90d\"\n" +} diff --git a/hscontrol/types/testdata/compat/bad-values-pruned.yaml b/hscontrol/types/testdata/compat/bad-values-pruned.yaml new file mode 100644 index 000000000..ac7ea9d20 --- /dev/null +++ b/hscontrol/types/testdata/compat/bad-values-pruned.yaml @@ -0,0 +1,21 @@ +# Input today's loader accepts but mangles or ignores. +server_url: https://hs.example.com +noise: + private_key_path: /var/lib/headscale/noise.key +prefixes: + v4: 10.0.0.0/8 +database: + type: sqlite + sqlite: + path: /var/lib/headscale/db.sqlite +dns: + magic_dns: false + override_local_dns: false +log: + level: verbose + format: xml +tuning: + batch_change_delay: abc +derp: + urls: + - "://not-a-url" diff --git a/hscontrol/types/testdata/compat/bad-values-strict.golden.json b/hscontrol/types/testdata/compat/bad-values-strict.golden.json index f4343479d..02572833e 100644 --- a/hscontrol/types/testdata/compat/bad-values-strict.golden.json +++ b/hscontrol/types/testdata/compat/bad-values-strict.golden.json @@ -1,169 +1,3 @@ { - "config": { - "ServerURL": "https://hs.example.com", - "Addr": "", - "MetricsAddr": "", - "TrustedProxies": null, - "Node": { - "Expiry": 0, - "Ephemeral": { - "InactivityTimeout": 120000000000 - }, - "Routes": { - "HA": { - "ProbeInterval": 10000000000, - "ProbeTimeout": 5000000000 - } - } - }, - "PreAuthKeys": { - "RevokedRetention": 604800000000000 - }, - "PrefixV4": "10.0.0.0/8", - "PrefixV6": null, - "IPAllocation": "sequential", - "NoisePrivateKeyPath": "/var/lib/headscale/noise.key", - "BaseDomain": "", - "Log": { - "Format": "", - "Level": "debug" - }, - "DisableUpdateCheck": false, - "Database": { - "Type": "sqlite3", - "Debug": false, - "Gorm": { - "Debug": false, - "SlowThreshold": 0, - "SkipErrRecordNotFound": false, - "ParameterizedQueries": false - }, - "Sqlite": { - "Path": "/var/lib/headscale/db.sqlite", - "WriteAheadLog": true, - "WALAutoCheckPoint": 1000 - }, - "Postgres": { - "Host": "", - "Port": 0, - "Name": "", - "User": "", - "Ssl": "false", - "MaxOpenConnections": 10, - "MaxIdleConnections": 10, - "ConnMaxIdleTimeSecs": 3600 - } - }, - "DERP": { - "ServerEnabled": false, - "AutomaticallyAddEmbeddedDerpRegion": true, - "ServerRegionID": 0, - "ServerRegionCode": "", - "ServerRegionName": "", - "ServerPrivateKeyPath": "", - "ServerVerifyClients": true, - "STUNAddr": "", - "URLs": [], - "Paths": null, - "DERPMap": null, - "AutoUpdate": false, - "UpdateFrequency": 10800000000000, - "IPv4": "", - "IPv6": "" - }, - "TLS": { - "CertPath": "", - "KeyPath": "", - "ACME": { - "Hostname": "", - "ChallengeType": "HTTP-01", - "HTTPListen": ":http", - "URL": "", - "Email": "", - "CacheDir": "/var/lib/headscale/cache" - } - }, - "DNSConfig": { - "MagicDNS": false, - "BaseDomain": "", - "OverrideLocalDNS": false, - "Nameservers": { - "Global": [], - "Split": {} - }, - "SearchDomains": [], - "ExtraRecords": null, - "ExtraRecordsPath": "", - "Certificates": { - "Enabled": false, - "Zone": "" - } - }, - "TailcfgDNSConfig": {}, - "DNSChallenge": { - "PropagationTimeout": 120000000000, - "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" - ], - "ProviderName": "" - }, - "UnixSocket": "/var/run/headscale/headscale.sock", - "UnixSocketPermission": 504, - "OIDC": { - "OnlyStartIfOIDCIsAvailable": true, - "Issuer": "", - "ClientID": "", - "Scope": [ - "openid", - "profile", - "email" - ], - "ExtraParams": {}, - "AllowedDomains": null, - "AllowedUsers": null, - "AllowedGroups": null, - "EmailVerifiedRequired": true, - "UseExpiryFromToken": false, - "PKCE": { - "Enabled": false, - "Method": "S256" - } - }, - "LogTail": { - "Enabled": false - }, - "Taildrop": { - "Enabled": true - }, - "AutoUpdate": { - "Enabled": false - }, - "CLI": { - "Address": "", - "Timeout": 5000000000, - "Insecure": false - }, - "Policy": { - "Path": "", - "Mode": "file" - }, - "Tuning": { - "NotifierSendTimeout": 0, - "BatchChangeDelay": 800000000, - "NodeMapSessionBufferedChanSize": 30, - "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, - "NodeStoreBatchSize": 100, - "NodeStoreBatchTimeout": 500000000 - } - }, - "warnings": [ - "\n################################################################\n### __ __ _ _ ###\n### \\ \\ / / (_) | | ###\n### \\ \\ /\\ / /_ _ _ __ _ __ _ _ __ __ _| | ###\n### \\ \\/ \\/ / _` | '__| '_ \\| | '_ \\ / _` | | ###\n### \\ /\\ / (_| | | | | | | | | | | (_| |_| ###\n### \\/ \\/ \\__,_|_| |_| |_|_|_| |_|\\__, (_) ###\n### __/ | ###\n### |___/ ###\n################################################################\n### ###\n### You have overridden the default Headscale IP prefixes ###\n### with a range outside of the standard CGNAT and/or ULA ###\n### ranges. This is NOT a supported configuration. ###\n### ###\n### Using subsets of the default ranges (100.64.0.0/10 for ###\n### IPv4, fd7a:115c:a1e0::/48 for IPv6) is fine. Using ###\n### ranges outside of these will cause undefined behaviour ###\n### as the Tailscale client is NOT designed to operate on ###\n### any other ranges. ###\n### ###\n### Please revert your prefixes to subsets of the standard ###\n### ranges as described in the example configuration. ###\n### ###\n### Any issue raised using a range outside of the ###\n### supported range will be labelled as wontfix ###\n### and closed. ###\n### ###\n################################################################" - ], - "log_errors": [ - "Could not parse log format: xml. Valid choices are 'json' or 'text'", - "Failed to parse url, ignoring..." - ] + "error": "Fatal config error: configuration key tuning.notifier_send_timeout was removed in 0.30 ($DIR/config.yaml:18:3)\n hint: remove it: it had no effect\n" } diff --git a/hscontrol/types/testdata/compat/bad-values.golden.json b/hscontrol/types/testdata/compat/bad-values.golden.json index 2e68e42d7..dab0a3f1d 100644 --- a/hscontrol/types/testdata/compat/bad-values.golden.json +++ b/hscontrol/types/testdata/compat/bad-values.golden.json @@ -1,3 +1,3 @@ { - "error": "Fatal config error: unknown configuration key dns.magic_dsn ($DIR/config.yaml:13:3)\n\nFatal config error: configuration key ephemeral_node_inactivity_timeout was removed in 0.30 ($DIR/config.yaml:23:1)\n hint: use node.ephemeral.inactivity_timeout\n" + "error": "Fatal config error: unknown configuration key dns.magic_dsn ($DIR/config.yaml:13:3)\n\nFatal config error: configuration key ephemeral_node_inactivity_timeout was removed in 0.30 ($DIR/config.yaml:23:1)\n hint: use node.ephemeral.inactivity_timeout\n\nFatal config error: configuration key tuning.notifier_send_timeout was removed in 0.30 ($DIR/config.yaml:19:3)\n hint: remove it: it had no effect\n" } diff --git a/hscontrol/types/testdata/compat/env-cli.golden.json b/hscontrol/types/testdata/compat/env-cli.golden.json index aa849c454..e381ac32d 100644 --- a/hscontrol/types/testdata/compat/env-cli.golden.json +++ b/hscontrol/types/testdata/compat/env-cli.golden.json @@ -142,7 +142,6 @@ "Mode": "" }, "Tuning": { - "NotifierSendTimeout": 0, "BatchChangeDelay": 0, "NodeMapSessionBufferedChanSize": 0, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/env-empty.golden.json b/hscontrol/types/testdata/compat/env-empty.golden.json index 4835b1baa..a3205c8da 100644 --- a/hscontrol/types/testdata/compat/env-empty.golden.json +++ b/hscontrol/types/testdata/compat/env-empty.golden.json @@ -163,7 +163,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/env-extra-records.golden.json b/hscontrol/types/testdata/compat/env-extra-records.golden.json index 47580f7f0..c9ef4d8d5 100644 --- a/hscontrol/types/testdata/compat/env-extra-records.golden.json +++ b/hscontrol/types/testdata/compat/env-extra-records.golden.json @@ -1,6 +1,178 @@ { + "config": { + "ServerURL": "https://hs.example.com", + "Addr": "", + "MetricsAddr": "", + "TrustedProxies": null, + "Node": { + "Expiry": 0, + "Ephemeral": { + "InactivityTimeout": 120000000000 + }, + "Routes": { + "HA": { + "ProbeInterval": 10000000000, + "ProbeTimeout": 5000000000 + } + } + }, + "PreAuthKeys": { + "RevokedRetention": 604800000000000 + }, + "PrefixV4": "100.64.0.0/10", + "PrefixV6": null, + "IPAllocation": "sequential", + "NoisePrivateKeyPath": "/var/lib/headscale/noise.key", + "BaseDomain": "", + "Log": { + "Format": "text", + "Level": "info" + }, + "DisableUpdateCheck": false, + "Database": { + "Type": "sqlite3", + "Debug": false, + "Gorm": { + "Debug": false, + "SlowThreshold": 0, + "SkipErrRecordNotFound": false, + "ParameterizedQueries": false + }, + "Sqlite": { + "Path": "/var/lib/headscale/db.sqlite", + "WriteAheadLog": true, + "WALAutoCheckPoint": 1000 + }, + "Postgres": { + "Host": "", + "Port": 0, + "Name": "", + "User": "", + "Ssl": "false", + "MaxOpenConnections": 10, + "MaxIdleConnections": 10, + "ConnMaxIdleTimeSecs": 3600 + } + }, + "DERP": { + "ServerEnabled": false, + "AutomaticallyAddEmbeddedDerpRegion": true, + "ServerRegionID": 0, + "ServerRegionCode": "", + "ServerRegionName": "", + "ServerPrivateKeyPath": "", + "ServerVerifyClients": true, + "STUNAddr": "", + "URLs": [], + "Paths": null, + "DERPMap": null, + "AutoUpdate": false, + "UpdateFrequency": 10800000000000, + "IPv4": "", + "IPv6": "" + }, + "TLS": { + "CertPath": "", + "KeyPath": "", + "ACME": { + "Hostname": "", + "ChallengeType": "HTTP-01", + "HTTPListen": ":http", + "URL": "", + "Email": "", + "CacheDir": "/var/lib/headscale/cache" + } + }, + "DNSConfig": { + "MagicDNS": false, + "BaseDomain": "", + "OverrideLocalDNS": false, + "Nameservers": { + "Global": [], + "Split": {} + }, + "SearchDomains": [], + "ExtraRecords": [ + { + "Name": "a.example.com", + "Type": "A", + "Value": "100.64.0.1" + } + ], + "ExtraRecordsPath": "", + "Certificates": { + "Enabled": false, + "Zone": "" + } + }, + "TailcfgDNSConfig": { + "ExtraRecords": [ + { + "Name": "a.example.com", + "Type": "A", + "Value": "100.64.0.1" + } + ] + }, + "DNSChallenge": { + "PropagationTimeout": 120000000000, + "Resolvers": [ + "1.1.1.1:53", + "8.8.8.8:53" + ], + "ProviderName": "" + }, + "UnixSocket": "/var/run/headscale/headscale.sock", + "UnixSocketPermission": 504, + "OIDC": { + "OnlyStartIfOIDCIsAvailable": true, + "Issuer": "", + "ClientID": "", + "Scope": [ + "openid", + "profile", + "email" + ], + "ExtraParams": {}, + "AllowedDomains": null, + "AllowedUsers": null, + "AllowedGroups": null, + "EmailVerifiedRequired": true, + "UseExpiryFromToken": false, + "PKCE": { + "Enabled": false, + "Method": "S256" + } + }, + "LogTail": { + "Enabled": false + }, + "Taildrop": { + "Enabled": true + }, + "AutoUpdate": { + "Enabled": false + }, + "CLI": { + "Address": "", + "Timeout": 5000000000, + "Insecure": false + }, + "Policy": { + "Path": "", + "Mode": "file" + }, + "Tuning": { + "BatchChangeDelay": 800000000, + "NodeMapSessionBufferedChanSize": 30, + "BatcherWorkers": 0, + "RegisterCacheExpiration": 0, + "RegisterCacheMaxEntries": 0, + "NodeStoreBatchSize": 100, + "NodeStoreBatchTimeout": 500000000 + } + }, "warnings": [ "no config file found, using defaults" - ], - "error": "Fatal config error: dns.extra_records cannot be parsed\n current: dns.extra_records: \"\u003cinline records\u003e\"\n why: unmarshalling dns extra records: decoding failed due to the following error(s):\n\n'[0]' expected a map or struct, got \"string\"\n'[1]' expected a map or struct, got \"string\"\n'[2]' expected a map or struct, got \"string\"\n hint: check the YAML syntax; see config-example.yaml for the expected shape\n" + ] } diff --git a/hscontrol/types/testdata/compat/env-legacy-ignored.golden.json b/hscontrol/types/testdata/compat/env-legacy-ignored.golden.json index fce4e981a..db13b6316 100644 --- a/hscontrol/types/testdata/compat/env-legacy-ignored.golden.json +++ b/hscontrol/types/testdata/compat/env-legacy-ignored.golden.json @@ -149,7 +149,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/env-legacy-removed.golden.json b/hscontrol/types/testdata/compat/env-legacy-removed.golden.json index 1b18503d1..cbe032129 100644 --- a/hscontrol/types/testdata/compat/env-legacy-removed.golden.json +++ b/hscontrol/types/testdata/compat/env-legacy-removed.golden.json @@ -149,7 +149,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/env-lists-maps.golden.json b/hscontrol/types/testdata/compat/env-lists-maps.golden.json index b09dd58e0..2da095dad 100644 --- a/hscontrol/types/testdata/compat/env-lists-maps.golden.json +++ b/hscontrol/types/testdata/compat/env-lists-maps.golden.json @@ -181,7 +181,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/env-only.golden.json b/hscontrol/types/testdata/compat/env-only.golden.json index 0ed6680ea..159b0738e 100644 --- a/hscontrol/types/testdata/compat/env-only.golden.json +++ b/hscontrol/types/testdata/compat/env-only.golden.json @@ -149,7 +149,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/env-precedence.golden.json b/hscontrol/types/testdata/compat/env-precedence.golden.json index f6ee3b970..cd0c72cf9 100644 --- a/hscontrol/types/testdata/compat/env-precedence.golden.json +++ b/hscontrol/types/testdata/compat/env-precedence.golden.json @@ -168,7 +168,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/env-secrets.golden.json b/hscontrol/types/testdata/compat/env-secrets.golden.json index 1a0d38e06..2e9dbcd79 100644 --- a/hscontrol/types/testdata/compat/env-secrets.golden.json +++ b/hscontrol/types/testdata/compat/env-secrets.golden.json @@ -149,7 +149,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/env-shapes-typed.env b/hscontrol/types/testdata/compat/env-shapes-typed.env new file mode 100644 index 000000000..70a5793f5 --- /dev/null +++ b/hscontrol/types/testdata/compat/env-shapes-typed.env @@ -0,0 +1,23 @@ +HEADSCALE_SERVER_URL=https://hs.example.com +HEADSCALE_NOISE_PRIVATE_KEY_PATH=/var/lib/headscale/noise.key +HEADSCALE_PREFIXES_V4=100.64.0.0/10 +HEADSCALE_DATABASE_TYPE=sqlite +HEADSCALE_DATABASE_SQLITE_PATH=/var/lib/headscale/db.sqlite +HEADSCALE_DNS_MAGIC_DNS=false +HEADSCALE_DNS_OVERRIDE_LOCAL_DNS=false +# Lists: JSON arrays, or split on whitespace; commas are not separators. +HEADSCALE_DNS_SEARCH_DOMAINS=["a.example.com","b.example.com"] +HEADSCALE_DNS_NAMESERVERS_GLOBAL=1.1.1.1,8.8.8.8 +HEADSCALE_OIDC_ALLOWED_DOMAINS=["example.com"] +HEADSCALE_OIDC_ALLOWED_GROUPS= a b +HEADSCALE_DERP_URLS=https://a.example.com/derp.json https://b.example.com/derp.json +HEADSCALE_TRUSTED_PROXIES=10.0.0.0/8 192.168.0.0/16 +# Maps: JSON, a number value is its text, key case is kept. +HEADSCALE_DNS_NAMESERVERS_SPLIT={"corp.example.com":["10.0.0.1"]} +HEADSCALE_OIDC_EXTRA_PARAMS={"Domain_Hint":"env","n":1} +# Scalars: durations with a unit, octal mode as text, numeric bool. +HEADSCALE_TUNING_BATCH_CHANGE_DELAY=800ms +HEADSCALE_DERP_UPDATE_FREQUENCY=30s +HEADSCALE_NODE_EXPIRY=90d +HEADSCALE_UNIX_SOCKET_PERMISSION=0700 +HEADSCALE_TAILDROP_ENABLED=0 diff --git a/hscontrol/types/testdata/compat/env-shapes-typed.golden.json b/hscontrol/types/testdata/compat/env-shapes-typed.golden.json new file mode 100644 index 000000000..797043303 --- /dev/null +++ b/hscontrol/types/testdata/compat/env-shapes-typed.golden.json @@ -0,0 +1,228 @@ +{ + "config": { + "ServerURL": "https://hs.example.com", + "Addr": "", + "MetricsAddr": "", + "TrustedProxies": [ + "10.0.0.0/8", + "192.168.0.0/16" + ], + "Node": { + "Expiry": 7776000000000000, + "Ephemeral": { + "InactivityTimeout": 120000000000 + }, + "Routes": { + "HA": { + "ProbeInterval": 10000000000, + "ProbeTimeout": 5000000000 + } + } + }, + "PreAuthKeys": { + "RevokedRetention": 604800000000000 + }, + "PrefixV4": "100.64.0.0/10", + "PrefixV6": null, + "IPAllocation": "sequential", + "NoisePrivateKeyPath": "/var/lib/headscale/noise.key", + "BaseDomain": "", + "Log": { + "Format": "text", + "Level": "info" + }, + "DisableUpdateCheck": false, + "Database": { + "Type": "sqlite3", + "Debug": false, + "Gorm": { + "Debug": false, + "SlowThreshold": 0, + "SkipErrRecordNotFound": false, + "ParameterizedQueries": false + }, + "Sqlite": { + "Path": "/var/lib/headscale/db.sqlite", + "WriteAheadLog": true, + "WALAutoCheckPoint": 1000 + }, + "Postgres": { + "Host": "", + "Port": 0, + "Name": "", + "User": "", + "Ssl": "false", + "MaxOpenConnections": 10, + "MaxIdleConnections": 10, + "ConnMaxIdleTimeSecs": 3600 + } + }, + "DERP": { + "ServerEnabled": false, + "AutomaticallyAddEmbeddedDerpRegion": true, + "ServerRegionID": 0, + "ServerRegionCode": "", + "ServerRegionName": "", + "ServerPrivateKeyPath": "", + "ServerVerifyClients": true, + "STUNAddr": "", + "URLs": [ + { + "Scheme": "https", + "Opaque": "", + "User": null, + "Host": "a.example.com", + "Path": "/derp.json", + "Fragment": "", + "RawQuery": "", + "RawPath": "", + "RawFragment": "", + "ForceQuery": false, + "OmitHost": false + }, + { + "Scheme": "https", + "Opaque": "", + "User": null, + "Host": "b.example.com", + "Path": "/derp.json", + "Fragment": "", + "RawQuery": "", + "RawPath": "", + "RawFragment": "", + "ForceQuery": false, + "OmitHost": false + } + ], + "Paths": null, + "DERPMap": null, + "AutoUpdate": false, + "UpdateFrequency": 30000000000, + "IPv4": "", + "IPv6": "" + }, + "TLS": { + "CertPath": "", + "KeyPath": "", + "ACME": { + "Hostname": "", + "ChallengeType": "HTTP-01", + "HTTPListen": ":http", + "URL": "", + "Email": "", + "CacheDir": "/var/lib/headscale/cache" + } + }, + "DNSConfig": { + "MagicDNS": false, + "BaseDomain": "", + "OverrideLocalDNS": false, + "Nameservers": { + "Global": [ + "1.1.1.1,8.8.8.8" + ], + "Split": { + "corp.example.com": [ + "10.0.0.1" + ] + } + }, + "SearchDomains": [ + "a.example.com", + "b.example.com" + ], + "ExtraRecords": null, + "ExtraRecordsPath": "", + "Certificates": { + "Enabled": false, + "Zone": "" + } + }, + "TailcfgDNSConfig": { + "Routes": { + "corp.example.com": [ + { + "Addr": "10.0.0.1" + } + ] + }, + "FallbackResolvers": [ + { + "Addr": "1.1.1.1,8.8.8.8" + } + ], + "Domains": [ + "a.example.com", + "b.example.com" + ] + }, + "DNSChallenge": { + "PropagationTimeout": 120000000000, + "Resolvers": [ + "1.1.1.1:53", + "8.8.8.8:53" + ], + "ProviderName": "" + }, + "UnixSocket": "/var/run/headscale/headscale.sock", + "UnixSocketPermission": 448, + "OIDC": { + "OnlyStartIfOIDCIsAvailable": true, + "Issuer": "", + "ClientID": "", + "Scope": [ + "openid", + "profile", + "email" + ], + "ExtraParams": { + "Domain_Hint": "env", + "n": "1" + }, + "AllowedDomains": [ + "example.com" + ], + "AllowedUsers": null, + "AllowedGroups": [ + "a", + "b" + ], + "EmailVerifiedRequired": true, + "UseExpiryFromToken": false, + "PKCE": { + "Enabled": false, + "Method": "S256" + } + }, + "LogTail": { + "Enabled": false + }, + "Taildrop": { + "Enabled": false + }, + "AutoUpdate": { + "Enabled": false + }, + "CLI": { + "Address": "", + "Timeout": 5000000000, + "Insecure": false + }, + "Policy": { + "Path": "", + "Mode": "file" + }, + "Tuning": { + "BatchChangeDelay": 800000000, + "NodeMapSessionBufferedChanSize": 30, + "BatcherWorkers": 0, + "RegisterCacheExpiration": 0, + "RegisterCacheMaxEntries": 0, + "NodeStoreBatchSize": 100, + "NodeStoreBatchTimeout": 500000000 + } + }, + "warnings": [ + "no config file found, using defaults" + ] +} diff --git a/hscontrol/types/testdata/compat/env-shapes.golden.json b/hscontrol/types/testdata/compat/env-shapes.golden.json index 3515feed3..1148b61b7 100644 --- a/hscontrol/types/testdata/compat/env-shapes.golden.json +++ b/hscontrol/types/testdata/compat/env-shapes.golden.json @@ -1,217 +1,6 @@ { - "config": { - "ServerURL": "https://hs.example.com", - "Addr": "", - "MetricsAddr": "", - "TrustedProxies": [ - "10.0.0.0/8", - "192.168.0.0/16" - ], - "Node": { - "Expiry": 0, - "Ephemeral": { - "InactivityTimeout": 120000000000 - }, - "Routes": { - "HA": { - "ProbeInterval": 10000000000, - "ProbeTimeout": 5000000000 - } - } - }, - "PreAuthKeys": { - "RevokedRetention": 604800000000000 - }, - "PrefixV4": "100.64.0.0/10", - "PrefixV6": null, - "IPAllocation": "sequential", - "NoisePrivateKeyPath": "/var/lib/headscale/noise.key", - "BaseDomain": "", - "Log": { - "Format": "text", - "Level": "info" - }, - "DisableUpdateCheck": false, - "Database": { - "Type": "sqlite3", - "Debug": false, - "Gorm": { - "Debug": false, - "SlowThreshold": 0, - "SkipErrRecordNotFound": false, - "ParameterizedQueries": false - }, - "Sqlite": { - "Path": "/var/lib/headscale/db.sqlite", - "WriteAheadLog": true, - "WALAutoCheckPoint": 1000 - }, - "Postgres": { - "Host": "", - "Port": 0, - "Name": "", - "User": "", - "Ssl": "false", - "MaxOpenConnections": 10, - "MaxIdleConnections": 10, - "ConnMaxIdleTimeSecs": 3600 - } - }, - "DERP": { - "ServerEnabled": false, - "AutomaticallyAddEmbeddedDerpRegion": true, - "ServerRegionID": 0, - "ServerRegionCode": "", - "ServerRegionName": "", - "ServerPrivateKeyPath": "", - "ServerVerifyClients": true, - "STUNAddr": "", - "URLs": [ - { - "Scheme": "https", - "Opaque": "", - "User": null, - "Host": "a.example.com", - "Path": "/derp.json", - "Fragment": "", - "RawQuery": "", - "RawPath": "", - "RawFragment": "", - "ForceQuery": false, - "OmitHost": false - }, - { - "Scheme": "https", - "Opaque": "", - "User": null, - "Host": "b.example.com", - "Path": "/derp.json", - "Fragment": "", - "RawQuery": "", - "RawPath": "", - "RawFragment": "", - "ForceQuery": false, - "OmitHost": false - } - ], - "Paths": null, - "DERPMap": null, - "AutoUpdate": false, - "UpdateFrequency": 30, - "IPv4": "", - "IPv6": "" - }, - "TLS": { - "CertPath": "", - "KeyPath": "", - "ACME": { - "Hostname": "", - "ChallengeType": "HTTP-01", - "HTTPListen": ":http", - "URL": "", - "Email": "", - "CacheDir": "/var/lib/headscale/cache" - } - }, - "DNSConfig": { - "MagicDNS": false, - "BaseDomain": "", - "OverrideLocalDNS": false, - "Nameservers": { - "Global": [ - "1.1.1.1,8.8.8.8" - ], - "Split": {} - }, - "SearchDomains": [ - "[\"a.example.com\",\"b.example.com\"]" - ], - "ExtraRecords": null, - "ExtraRecordsPath": "", - "Certificates": { - "Enabled": false, - "Zone": "" - } - }, - "TailcfgDNSConfig": { - "FallbackResolvers": [ - { - "Addr": "1.1.1.1,8.8.8.8" - } - ], - "Domains": [ - "[\"a.example.com\",\"b.example.com\"]" - ] - }, - "DNSChallenge": { - "PropagationTimeout": 120000000000, - "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" - ], - "ProviderName": "" - }, - "UnixSocket": "/var/run/headscale/headscale.sock", - "UnixSocketPermission": 448, - "OIDC": { - "OnlyStartIfOIDCIsAvailable": true, - "Issuer": "", - "ClientID": "", - "Scope": [ - "openid", - "profile", - "email" - ], - "ExtraParams": { - "Domain_Hint": "env", - "n": "" - }, - "AllowedDomains": [ - "[\"example.com\"]" - ], - "AllowedUsers": null, - "AllowedGroups": [ - "a", - "b" - ], - "EmailVerifiedRequired": true, - "UseExpiryFromToken": false, - "PKCE": { - "Enabled": false, - "Method": "S256" - } - }, - "LogTail": { - "Enabled": false - }, - "Taildrop": { - "Enabled": false - }, - "AutoUpdate": { - "Enabled": false - }, - "CLI": { - "Address": "", - "Timeout": 5000000000, - "Insecure": false - }, - "Policy": { - "Path": "", - "Mode": "file" - }, - "Tuning": { - "NotifierSendTimeout": 800000000, - "BatchChangeDelay": 800, - "NodeMapSessionBufferedChanSize": 30, - "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, - "NodeStoreBatchSize": 100, - "NodeStoreBatchTimeout": 500000000 - } - }, "warnings": [ - "no config file found, using defaults", - "failed to parse node.expiry, defaulting to no expiry" - ] + "no config file found, using defaults" + ], + "error": "Fatal config error: node.expiry needs a unit (env HEADSCALE_NODE_EXPIRY)\n current: node.expiry: \"90\"\n hint: write the unit, e.g. \"30s\", \"5m\" or \"90d\"\n\nFatal config error: derp.update_frequency needs a unit (env HEADSCALE_DERP_UPDATE_FREQUENCY)\n current: derp.update_frequency: \"30\"\n hint: write the unit, e.g. \"30s\", \"5m\" or \"90d\"\n\nFatal config error: dns.nameservers.split is not a JSON object (env HEADSCALE_DNS_NAMESERVERS_SPLIT)\n hint: write the map as JSON, e.g. {\"example.com\": [\"1.1.1.1\"]}\n\nFatal config error: tuning.batch_change_delay needs a unit (env HEADSCALE_TUNING_BATCH_CHANGE_DELAY)\n current: tuning.batch_change_delay: \"800\"\n hint: write the unit, e.g. \"30s\", \"5m\" or \"90d\"\n" } diff --git a/hscontrol/types/testdata/compat/example-main.golden.json b/hscontrol/types/testdata/compat/example-main.golden.json index 0b3446dc0..a3042aed3 100644 --- a/hscontrol/types/testdata/compat/example-main.golden.json +++ b/hscontrol/types/testdata/compat/example-main.golden.json @@ -187,7 +187,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/file-everything-pruned.env b/hscontrol/types/testdata/compat/file-everything-pruned.env new file mode 100644 index 000000000..e664c7012 --- /dev/null +++ b/hscontrol/types/testdata/compat/file-everything-pruned.env @@ -0,0 +1 @@ +COMPAT_SECRETS=testdata/compat diff --git a/hscontrol/types/testdata/compat/file-everything-pruned.golden.json b/hscontrol/types/testdata/compat/file-everything-pruned.golden.json new file mode 100644 index 000000000..a66ae5b1c --- /dev/null +++ b/hscontrol/types/testdata/compat/file-everything-pruned.golden.json @@ -0,0 +1,251 @@ +{ + "config": { + "ServerURL": "https://headscale.example.com", + "Addr": "0.0.0.0:8443", + "MetricsAddr": "127.0.0.1:9091", + "TrustedProxies": [ + "10.0.0.0/8", + "192.168.0.0/16" + ], + "Node": { + "Expiry": 7776000000000000, + "Ephemeral": { + "InactivityTimeout": 2700000000000 + }, + "Routes": { + "HA": { + "ProbeInterval": 20000000000, + "ProbeTimeout": 7000000000 + } + } + }, + "PreAuthKeys": { + "RevokedRetention": 259200000000000 + }, + "PrefixV4": "100.64.0.0/10", + "PrefixV6": "fd7a:115c:a1e0::/48", + "IPAllocation": "random", + "NoisePrivateKeyPath": "$DIR/noise_private.key", + "BaseDomain": "tailnet.example.net", + "Log": { + "Format": "json", + "Level": "debug" + }, + "DisableUpdateCheck": true, + "Database": { + "Type": "postgres", + "Debug": true, + "Gorm": { + "Debug": true, + "SlowThreshold": 2000000000, + "SkipErrRecordNotFound": false, + "ParameterizedQueries": true + }, + "Sqlite": { + "Path": "", + "WriteAheadLog": true, + "WALAutoCheckPoint": 1000 + }, + "Postgres": { + "Host": "db.example.com", + "Port": 5433, + "Name": "headscale", + "User": "headscale", + "Ssl": "true", + "MaxOpenConnections": 20, + "MaxIdleConnections": 5, + "ConnMaxIdleTimeSecs": 1800 + } + }, + "DERP": { + "ServerEnabled": true, + "AutomaticallyAddEmbeddedDerpRegion": false, + "ServerRegionID": 900, + "ServerRegionCode": "hs", + "ServerRegionName": "Headscale Embedded DERP", + "ServerPrivateKeyPath": "$DIR/derp_server_private.key", + "ServerVerifyClients": false, + "STUNAddr": "0.0.0.0:3479", + "URLs": [ + { + "Scheme": "https", + "Opaque": "", + "User": null, + "Host": "controlplane.tailscale.com", + "Path": "/derpmap/default", + "Fragment": "", + "RawQuery": "", + "RawPath": "", + "RawFragment": "", + "ForceQuery": false, + "OmitHost": false + }, + { + "Scheme": "https", + "Opaque": "", + "User": null, + "Host": "derp.example.com", + "Path": "/map.json", + "Fragment": "", + "RawQuery": "", + "RawPath": "", + "RawFragment": "", + "ForceQuery": false, + "OmitHost": false + } + ], + "Paths": [ + "/etc/headscale/derp.yaml" + ], + "DERPMap": null, + "AutoUpdate": false, + "UpdateFrequency": 3600000000000, + "IPv4": "198.51.100.1", + "IPv6": "2001:db8::1" + }, + "TLS": { + "CertPath": "", + "KeyPath": "", + "ACME": { + "Hostname": "headscale.example.com", + "ChallengeType": "TLS-ALPN-01", + "HTTPListen": ":8081", + "URL": "https://acme-staging-v02.api.letsencrypt.org/directory", + "Email": "admin@example.com", + "CacheDir": "$DIR/acme-cache" + } + }, + "DNSConfig": { + "MagicDNS": true, + "BaseDomain": "tailnet.example.net", + "OverrideLocalDNS": true, + "Nameservers": { + "Global": [ + "1.1.1.1", + "https://dns.nextdns.io/abc123" + ], + "Split": { + "corp.example.com": [ + "10.0.0.1" + ] + } + }, + "SearchDomains": [ + "corp.example.com" + ], + "ExtraRecords": [ + { + "Name": "grafana.tailnet.example.net", + "Type": "A", + "Value": "100.64.0.3" + } + ], + "ExtraRecordsPath": "", + "Certificates": { + "Enabled": false, + "Zone": "tailnet.example.net" + } + }, + "TailcfgDNSConfig": { + "Resolvers": [ + { + "Addr": "1.1.1.1" + }, + { + "Addr": "https://dns.nextdns.io/abc123" + } + ], + "Routes": { + "corp.example.com": [ + { + "Addr": "10.0.0.1" + } + ] + }, + "Domains": [ + "tailnet.example.net", + "corp.example.com" + ], + "Proxied": true, + "ExtraRecords": [ + { + "Name": "grafana.tailnet.example.net", + "Type": "A", + "Value": "100.64.0.3" + } + ] + }, + "DNSChallenge": { + "PropagationTimeout": 180000000000, + "Resolvers": [ + "9.9.9.9:53" + ], + "ProviderName": "" + }, + "UnixSocket": "/run/headscale/headscale.sock", + "UnixSocketPermission": 448, + "OIDC": { + "OnlyStartIfOIDCIsAvailable": false, + "Issuer": "https://idp.example.com", + "ClientID": "headscale", + "Scope": [ + "openid", + "profile", + "email", + "groups" + ], + "ExtraParams": { + "login_hint": "file" + }, + "AllowedDomains": [ + "example.com" + ], + "AllowedUsers": [ + "alice@example.com" + ], + "AllowedGroups": [ + "/headscale" + ], + "EmailVerifiedRequired": false, + "UseExpiryFromToken": true, + "PKCE": { + "Enabled": true, + "Method": "plain" + } + }, + "LogTail": { + "Enabled": true + }, + "Taildrop": { + "Enabled": false + }, + "AutoUpdate": { + "Enabled": true + }, + "CLI": { + "Address": "headscale.example.com:443", + "Timeout": 15000000000, + "Insecure": true + }, + "Policy": { + "Path": "", + "Mode": "database" + }, + "Tuning": { + "BatchChangeDelay": 500000000, + "NodeMapSessionBufferedChanSize": 50, + "BatcherWorkers": 4096, + "RegisterCacheExpiration": 1200000000000, + "RegisterCacheMaxEntries": 2000, + "NodeStoreBatchSize": 50, + "NodeStoreBatchTimeout": 250000000 + } + }, + "secrets": { + "database.postgres.pass": "marker-postgres-pass", + "oidc.client_secret": "marker-oidc-secret-from-file" + }, + "warnings": [ + "Warning: when using tls.acme.hostname with TLS-ALPN-01 as challenge type, headscale must be reachable on port 443, i.e. listen_addr should probably end in :443" + ] +} diff --git a/hscontrol/types/testdata/compat/file-everything-pruned.yaml b/hscontrol/types/testdata/compat/file-everything-pruned.yaml new file mode 100644 index 000000000..4b4477de8 --- /dev/null +++ b/hscontrol/types/testdata/compat/file-everything-pruned.yaml @@ -0,0 +1,142 @@ +# Every key the loader reads, set to a valid non-default value. +server_url: https://headscale.example.com +listen_addr: 0.0.0.0:8443 +metrics_listen_addr: 127.0.0.1:9091 +trusted_proxies: [10.0.0.0/8, 192.168.0.0/16] +disable_check_updates: true +unix_socket: /run/headscale/headscale.sock +unix_socket_permission: "0700" + +noise: + private_key_path: ./noise_private.key + +prefixes: + v4: 100.64.0.0/10 + v6: fd7a:115c:a1e0::/48 + allocation: random + +derp: + server: + enabled: true + region_id: 900 + region_code: hs + region_name: Headscale Embedded DERP + verify_clients: false + stun_listen_addr: 0.0.0.0:3479 + private_key_path: ./derp_server_private.key + automatically_add_embedded_derp_region: false + ipv4: 198.51.100.1 + ipv6: 2001:db8::1 + urls: + - https://controlplane.tailscale.com/derpmap/default + - https://derp.example.com/map.json + paths: + - /etc/headscale/derp.yaml + auto_update_enabled: false + update_frequency: 1h + +node: + expiry: 90d + ephemeral: + inactivity_timeout: 45m + routes: + ha: + probe_interval: 20s + probe_timeout: 7s + +preauth_keys: + revoked_retention: 72h + +database: + type: postgres + debug: true + gorm: + parameterized_queries: true + skip_err_record_not_found: false + slow_threshold: 2000 + postgres: + host: db.example.com + port: 5433 + name: headscale + user: headscale + pass: marker-postgres-pass + max_open_conns: 20 + max_idle_conns: 5 + conn_max_idle_time_secs: 1800 + ssl: true + +tls: + acme: + hostname: headscale.example.com + challenge_type: TLS-ALPN-01 + http_listen: :8081 + url: https://acme-staging-v02.api.letsencrypt.org/directory + email: admin@example.com + cache_dir: ./acme-cache + +log: + format: json + level: debug + +policy: + mode: database + +dns: + magic_dns: true + base_domain: tailnet.example.net + override_local_dns: true + nameservers: + global: + - 1.1.1.1 + - https://dns.nextdns.io/abc123 + split: + Corp.Example.COM: + - 10.0.0.1 + search_domains: [corp.example.com] + extra_records: + - name: grafana.tailnet.example.net + type: A + value: 100.64.0.3 + challenge: + propagation_timeout: 3m + resolvers: [9.9.9.9] + +oidc: + only_start_if_oidc_is_available: false + issuer: https://idp.example.com + client_id: headscale + # os.ExpandEnv, then read relative to the test's working directory. + client_secret: file:${COMPAT_SECRETS}/oidc-secret.txt + scope: [openid, profile, email, groups] + extra_params: + Login_Hint: file + allowed_domains: [example.com] + allowed_users: [alice@example.com] + allowed_groups: [/headscale] + email_verified_required: false + use_expiry_from_token: true + pkce: + enabled: true + method: plain + +logtail: + enabled: true +taildrop: + enabled: false +auto_update: + enabled: true + +cli: + address: headscale.example.com:443 + api_key: marker-cli-api-key + timeout: 15s + insecure: true + +tuning: + batch_change_delay: 500ms + node_mapsession_buffered_chan_size: 50 + batcher_workers: 4096 + register_cache_expiration: 20m + register_cache_max_entries: 2000 + node_store_batch_size: 50 + node_store_batch_timeout: 250ms diff --git a/hscontrol/types/testdata/compat/file-everything-strict.golden.json b/hscontrol/types/testdata/compat/file-everything-strict.golden.json index 1de6e169c..4dad77157 100644 --- a/hscontrol/types/testdata/compat/file-everything-strict.golden.json +++ b/hscontrol/types/testdata/compat/file-everything-strict.golden.json @@ -1,252 +1,3 @@ { - "config": { - "ServerURL": "https://headscale.example.com", - "Addr": "0.0.0.0:8443", - "MetricsAddr": "127.0.0.1:9091", - "TrustedProxies": [ - "10.0.0.0/8", - "192.168.0.0/16" - ], - "Node": { - "Expiry": 7776000000000000, - "Ephemeral": { - "InactivityTimeout": 2700000000000 - }, - "Routes": { - "HA": { - "ProbeInterval": 20000000000, - "ProbeTimeout": 7000000000 - } - } - }, - "PreAuthKeys": { - "RevokedRetention": 259200000000000 - }, - "PrefixV4": "100.64.0.0/10", - "PrefixV6": "fd7a:115c:a1e0::/48", - "IPAllocation": "random", - "NoisePrivateKeyPath": "$DIR/noise_private.key", - "BaseDomain": "tailnet.example.net", - "Log": { - "Format": "json", - "Level": "debug" - }, - "DisableUpdateCheck": true, - "Database": { - "Type": "postgres", - "Debug": true, - "Gorm": { - "Debug": true, - "SlowThreshold": 2000000000, - "SkipErrRecordNotFound": false, - "ParameterizedQueries": true - }, - "Sqlite": { - "Path": "", - "WriteAheadLog": true, - "WALAutoCheckPoint": 1000 - }, - "Postgres": { - "Host": "db.example.com", - "Port": 5433, - "Name": "headscale", - "User": "headscale", - "Ssl": "true", - "MaxOpenConnections": 20, - "MaxIdleConnections": 5, - "ConnMaxIdleTimeSecs": 1800 - } - }, - "DERP": { - "ServerEnabled": true, - "AutomaticallyAddEmbeddedDerpRegion": false, - "ServerRegionID": 900, - "ServerRegionCode": "hs", - "ServerRegionName": "Headscale Embedded DERP", - "ServerPrivateKeyPath": "$DIR/derp_server_private.key", - "ServerVerifyClients": false, - "STUNAddr": "0.0.0.0:3479", - "URLs": [ - { - "Scheme": "https", - "Opaque": "", - "User": null, - "Host": "controlplane.tailscale.com", - "Path": "/derpmap/default", - "Fragment": "", - "RawQuery": "", - "RawPath": "", - "RawFragment": "", - "ForceQuery": false, - "OmitHost": false - }, - { - "Scheme": "https", - "Opaque": "", - "User": null, - "Host": "derp.example.com", - "Path": "/map.json", - "Fragment": "", - "RawQuery": "", - "RawPath": "", - "RawFragment": "", - "ForceQuery": false, - "OmitHost": false - } - ], - "Paths": [ - "/etc/headscale/derp.yaml" - ], - "DERPMap": null, - "AutoUpdate": false, - "UpdateFrequency": 3600000000000, - "IPv4": "198.51.100.1", - "IPv6": "2001:db8::1" - }, - "TLS": { - "CertPath": "", - "KeyPath": "", - "ACME": { - "Hostname": "headscale.example.com", - "ChallengeType": "TLS-ALPN-01", - "HTTPListen": ":8081", - "URL": "https://acme-staging-v02.api.letsencrypt.org/directory", - "Email": "admin@example.com", - "CacheDir": "$DIR/acme-cache" - } - }, - "DNSConfig": { - "MagicDNS": true, - "BaseDomain": "tailnet.example.net", - "OverrideLocalDNS": true, - "Nameservers": { - "Global": [ - "1.1.1.1", - "https://dns.nextdns.io/abc123" - ], - "Split": { - "corp.example.com": [ - "10.0.0.1" - ] - } - }, - "SearchDomains": [ - "corp.example.com" - ], - "ExtraRecords": [ - { - "Name": "grafana.tailnet.example.net", - "Type": "A", - "Value": "100.64.0.3" - } - ], - "ExtraRecordsPath": "", - "Certificates": { - "Enabled": false, - "Zone": "tailnet.example.net" - } - }, - "TailcfgDNSConfig": { - "Resolvers": [ - { - "Addr": "1.1.1.1" - }, - { - "Addr": "https://dns.nextdns.io/abc123" - } - ], - "Routes": { - "corp.example.com": [ - { - "Addr": "10.0.0.1" - } - ] - }, - "Domains": [ - "tailnet.example.net", - "corp.example.com" - ], - "Proxied": true, - "ExtraRecords": [ - { - "Name": "grafana.tailnet.example.net", - "Type": "A", - "Value": "100.64.0.3" - } - ] - }, - "DNSChallenge": { - "PropagationTimeout": 180000000000, - "Resolvers": [ - "9.9.9.9:53" - ], - "ProviderName": "" - }, - "UnixSocket": "/run/headscale/headscale.sock", - "UnixSocketPermission": 448, - "OIDC": { - "OnlyStartIfOIDCIsAvailable": false, - "Issuer": "https://idp.example.com", - "ClientID": "headscale", - "Scope": [ - "openid", - "profile", - "email", - "groups" - ], - "ExtraParams": { - "login_hint": "file" - }, - "AllowedDomains": [ - "example.com" - ], - "AllowedUsers": [ - "alice@example.com" - ], - "AllowedGroups": [ - "/headscale" - ], - "EmailVerifiedRequired": false, - "UseExpiryFromToken": true, - "PKCE": { - "Enabled": true, - "Method": "plain" - } - }, - "LogTail": { - "Enabled": true - }, - "Taildrop": { - "Enabled": false - }, - "AutoUpdate": { - "Enabled": true - }, - "CLI": { - "Address": "headscale.example.com:443", - "Timeout": 15000000000, - "Insecure": true - }, - "Policy": { - "Path": "", - "Mode": "database" - }, - "Tuning": { - "NotifierSendTimeout": 1000000000, - "BatchChangeDelay": 500000000, - "NodeMapSessionBufferedChanSize": 50, - "BatcherWorkers": 4096, - "RegisterCacheExpiration": 1200000000000, - "RegisterCacheMaxEntries": 2000, - "NodeStoreBatchSize": 50, - "NodeStoreBatchTimeout": 250000000 - } - }, - "secrets": { - "database.postgres.pass": "marker-postgres-pass", - "oidc.client_secret": "marker-oidc-secret-from-file" - }, - "warnings": [ - "Warning: when using tls.acme.hostname with TLS-ALPN-01 as challenge type, headscale must be reachable on port 443, i.e. listen_addr should probably end in :443" - ] + "error": "Fatal config error: configuration key tuning.notifier_send_timeout was removed in 0.30 ($DIR/config.yaml:136:3)\n hint: remove it: it had no effect\n" } diff --git a/hscontrol/types/testdata/compat/file-everything.golden.json b/hscontrol/types/testdata/compat/file-everything.golden.json index 9ffdba269..e873b1238 100644 --- a/hscontrol/types/testdata/compat/file-everything.golden.json +++ b/hscontrol/types/testdata/compat/file-everything.golden.json @@ -1,3 +1,3 @@ { - "error": "Fatal config error: configuration key oidc.client_secret_path was removed in 0.30 ($DIR/config.yaml:109:3)\n hint: use oidc.client_secret: \"file:\u003cpath\u003e\"\n" + "error": "Fatal config error: configuration key oidc.client_secret_path was removed in 0.30 ($DIR/config.yaml:109:3)\n hint: use oidc.client_secret: \"file:\u003cpath\u003e\"\n\nFatal config error: configuration key tuning.notifier_send_timeout was removed in 0.30 ($DIR/config.yaml:136:3)\n hint: remove it: it had no effect\n" } diff --git a/hscontrol/types/testdata/compat/hsic-default-strict.golden.json b/hscontrol/types/testdata/compat/hsic-default-strict.golden.json index ce79bf7e9..1b2b0a3be 100644 --- a/hscontrol/types/testdata/compat/hsic-default-strict.golden.json +++ b/hscontrol/types/testdata/compat/hsic-default-strict.golden.json @@ -165,7 +165,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/hsic-postgres-strict.golden.json b/hscontrol/types/testdata/compat/hsic-postgres-strict.golden.json index e9d87224c..87a7bcd53 100644 --- a/hscontrol/types/testdata/compat/hsic-postgres-strict.golden.json +++ b/hscontrol/types/testdata/compat/hsic-postgres-strict.golden.json @@ -165,7 +165,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/nixos-cli.golden.json b/hscontrol/types/testdata/compat/nixos-cli.golden.json index a35023e01..51dac0e9e 100644 --- a/hscontrol/types/testdata/compat/nixos-cli.golden.json +++ b/hscontrol/types/testdata/compat/nixos-cli.golden.json @@ -142,7 +142,6 @@ "Mode": "" }, "Tuning": { - "NotifierSendTimeout": 0, "BatchChangeDelay": 0, "NodeMapSessionBufferedChanSize": 0, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/nixos-example-strict.golden.json b/hscontrol/types/testdata/compat/nixos-example-strict.golden.json index 8e682be0b..1b2b58578 100644 --- a/hscontrol/types/testdata/compat/nixos-example-strict.golden.json +++ b/hscontrol/types/testdata/compat/nixos-example-strict.golden.json @@ -179,7 +179,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/nixos-example.golden.json b/hscontrol/types/testdata/compat/nixos-example.golden.json index 8e682be0b..1b2b58578 100644 --- a/hscontrol/types/testdata/compat/nixos-example.golden.json +++ b/hscontrol/types/testdata/compat/nixos-example.golden.json @@ -179,7 +179,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/testdata-base-domain-not-in-server-url.golden.json b/hscontrol/types/testdata/compat/testdata-base-domain-not-in-server-url.golden.json index 06a531337..01763f178 100644 --- a/hscontrol/types/testdata/compat/testdata-base-domain-not-in-server-url.golden.json +++ b/hscontrol/types/testdata/compat/testdata-base-domain-not-in-server-url.golden.json @@ -154,7 +154,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/testdata-dns-override-true.golden.json b/hscontrol/types/testdata/compat/testdata-dns-override-true.golden.json index 98a8d8c52..ab87572f3 100644 --- a/hscontrol/types/testdata/compat/testdata-dns-override-true.golden.json +++ b/hscontrol/types/testdata/compat/testdata-dns-override-true.golden.json @@ -165,7 +165,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, diff --git a/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json b/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json index 21d707716..029b2d43b 100644 --- a/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json +++ b/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json @@ -149,7 +149,6 @@ "Mode": "file" }, "Tuning": { - "NotifierSendTimeout": 800000000, "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0,