diff --git a/.gitattributes b/.gitattributes index 5279db5d3..ea97a84a1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,4 +2,5 @@ hscontrol/policy/v2/testdata/*_results/** linguist-generated hscontrol/servertest/testdata/steps/** linguist-generated hscontrol/testdata/apiv1_golden/** linguist-generated -hscontrol/types/testdata/compat/*.golden.json linguist-generated +hscontrol/config/testdata/compat/*.golden.json linguist-generated +hscontrol/config/testdata/bad/*.golden linguist-generated diff --git a/.golangci.yaml b/.golangci.yaml index edbb7e33f..c6d38da45 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -107,7 +107,7 @@ linters: rules: # Config key names repeat across the key tables, validation and error # reports until the config reshape gives each key one home. - - path: hscontrol/types/(config|config_keys|listener)\.go + - path: hscontrol/(types/config|config/(keys|validate))\.go linters: - goconst paths: diff --git a/.prettierignore b/.prettierignore index 89980e6e8..14c80546f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -3,5 +3,5 @@ docs/ # Golden fixtures are byte-compared; a formatter must not rewrite them. hscontrol/testdata/ -hscontrol/types/testdata/bad/ -hscontrol/types/testdata/compat/ +hscontrol/config/testdata/bad/ +hscontrol/config/testdata/compat/ diff --git a/AGENTS.md b/AGENTS.md index fc95000c5..306fc358b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -159,11 +159,13 @@ headscale/ Performance-critical. - `policy/` — `policy/v2/` is **the** policy implementation. The top-level `policy.go` is thin wrappers. There is no v1 directory. -- `config/` — home for config code outside the `types.Config` struct: the - checks behind `headscale check` (`Files` lists every file a config names, - with its validator) and `ReadPrivateKey`. The loader, defaults, - removed-keys table and `ConfigError` move here next. It imports `types`; - `types`, `derp`, `dns` and `policy` must never import it. +- `config/` — config code outside the `types.Config` struct: the loader + (`Load`: defaults, file, `HEADSCALE_*` environment; `Default`; `Marshal`; + the removed-keys table), `Validate` and `Warnings`, the checks behind + `headscale check` (`Files` lists every file a config names, with its + validator) and `ReadPrivateKey`. The schema and `ConfigError` stay in + `types`. It imports `types`; `types`, `derp`, `dns` and `policy` must + never import it. - `routes/`, `dns/`, `derp/`, `types/`, `util/`, `templates/`, `capver/` — routing, MagicDNS, relay, core types, helpers, client templates, capability versioning. diff --git a/cmd/headscale/cli/check.go b/cmd/headscale/cli/check.go index 826662f7e..00cc23353 100644 --- a/cmd/headscale/cli/check.go +++ b/cmd/headscale/cli/check.go @@ -1,6 +1,7 @@ package cli import ( + "cmp" "errors" "fmt" "io" @@ -174,24 +175,33 @@ func fileCheckCmd(use, short, valid string, check func(string) error) *cobra.Com } // loadCheckedConfig loads the server config from --file, or finds it as -// serve does, and names where it came from. +// serve does, logs what loading and the config warn about at its log.level +// and in its log.format, and names where it came from. func loadCheckedConfig(cmd *cobra.Command) (*types.Config, string, error) { path, _ := cmd.Flags().GetString("file") - err := loadConfig(path) - - src := types.ConfigFile() - if src == "" { - src = "(defaults and environment)" + l, err := loadConfig(path) + if err == nil { + // As initConfig does; a config that failed keeps the console + // logger and the default level. + useLogConfig(l.Config, cmd.ErrOrStderr()) } + logWarnings(l.Warnings) + + src := cmp.Or(l.File, "(defaults and environment)") + if err != nil { + return nil, src, err + } + + err = config.Validate(l.Config) + logWarnings(config.Warnings(l.Config)) + if err != nil { return nil, src, err } - cfg, err := types.LoadServerConfig() - - return cfg, src, err + return l.Config, src, nil } // printCheckLine prints one result; an error goes indented below it. diff --git a/cmd/headscale/cli/check_test.go b/cmd/headscale/cli/check_test.go index ea33f20df..6b8bc2720 100644 --- a/cmd/headscale/cli/check_test.go +++ b/cmd/headscale/cli/check_test.go @@ -2,14 +2,19 @@ package cli import ( "bytes" + "encoding/json" "fmt" + "io" "os" "path/filepath" "strings" "testing" policyv2 "github.com/juanfont/headscale/hscontrol/policy/v2" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -368,3 +373,100 @@ func TestCheckFileCommands(t *testing.T) { _, err = runCheck(t, checkDERPMapCmd, writeFile(t, dir, "d.yaml", "{}")) require.ErrorContains(t, err, "d.yaml") } + +// TestCheckConfigWarnings: the check commands report what the environment +// warns about, with a valid file and with one that fails, and on a valid +// file the configuration's advisories, in its log.format and at its +// log.level. +func TestCheckConfigWarnings(t *testing.T) { + const ( + unknown = "unknown environment variable HEADSCALE_BOGUS" + removed = "configuration variable HEADSCALE_DB_TYPE was removed" + advisory = "listen_addr should probably end in :443" + acme = "tls:\n acme:\n hostname: example.com\n challenge_type: TLS-ALPN-01\n" + ) + + t.Setenv("HEADSCALE_BOGUS", "1") + t.Setenv("HEADSCALE_DB_TYPE", "sqlite") + + dir := t.TempDir() + text := writeServerConfig(t, t.TempDir(), acme) + jsonLogs := writeServerConfig(t, t.TempDir(), acme+"log:\n format: json\n") + errorLevel := writeServerConfig(t, t.TempDir(), acme+"log:\n level: error\n") + disabled := writeServerConfig(t, t.TempDir(), acme+"log:\n level: disabled\n") + invalid := writeFile(t, dir, "invalid.yaml", "log:\n format: json\nbogus_key: 1\n") + + oldLogger, oldLevel := log.Logger, zerolog.GlobalLevel() + + zerolog.SetGlobalLevel(zerolog.InfoLevel) + t.Cleanup(func() { + log.Logger = oldLogger + + zerolog.SetGlobalLevel(oldLevel) + }) + + for _, c := range []*cobra.Command{checkConfigCmd, checkAllCmd} { + for _, tt := range []struct { + name, file string + wantErr bool + // json: the warnings are JSON on the command's stderr, not on + // the logger the command started with. + json bool + // quiet: log.level hides every warning. + quiet bool + }{ + {name: "text", file: text}, + {name: "json", file: jsonLogs, json: true}, + {name: "invalid", file: invalid, wantErr: true}, + {name: "level error", file: errorLevel, quiet: true}, + {name: "level disabled", file: disabled, quiet: true}, + } { + t.Run(c.Name()+" "+tt.name, func(t *testing.T) { + var logged, stderr bytes.Buffer + + // As initConfig leaves it for the check commands. + log.Logger = zerolog.New(&logged) + + zerolog.SetGlobalLevel(zerolog.InfoLevel) + + cmd := &cobra.Command{} + cmd.Flags().String("file", tt.file, "") + cmd.SetOut(io.Discard) + cmd.SetErr(&stderr) + + err := c.RunE(cmd, nil) + require.Equal(t, tt.wantErr, err != nil, "%v", err) + + out := logged.String() + if tt.quiet { + assert.Empty(t, out) + assert.Empty(t, stderr.String()) + + return + } + + if tt.json { + assert.Empty(t, out) + + out = stderr.String() + for l := range strings.SplitSeq(strings.TrimSpace(out), "\n") { + var rec struct{ Level, Message string } + require.NoError(t, json.Unmarshal([]byte(l), &rec), l) + assert.Equal(t, "warn", rec.Level) + } + } else { + assert.Empty(t, stderr.String()) + } + + assert.Contains(t, out, unknown) + assert.Contains(t, out, removed) + + if tt.wantErr { + assert.NotContains(t, out, advisory) + } else { + assert.Contains(t, out, advisory) + } + }) + } + } +} diff --git a/cmd/headscale/cli/dump_config.go b/cmd/headscale/cli/dump_config.go index 9ee6038cc..0c83ad31b 100644 --- a/cmd/headscale/cli/dump_config.go +++ b/cmd/headscale/cli/dump_config.go @@ -4,7 +4,7 @@ import ( "fmt" "os" - "github.com/juanfont/headscale/hscontrol/types" + "github.com/juanfont/headscale/hscontrol/config" "github.com/spf13/cobra" ) @@ -17,7 +17,7 @@ var dumpConfigCmd = &cobra.Command{ Short: "dump current config to /etc/headscale/config.dump.yaml, integration test only", Hidden: true, RunE: func(cmd *cobra.Command, args []string) error { - out, err := types.DumpConfig() + out, err := config.Marshal(loaded.Config, ".yaml") if err != nil { return fmt.Errorf("dumping config: %w", err) } diff --git a/cmd/headscale/cli/oauth_client.go b/cmd/headscale/cli/oauth_client.go index 7eb58441b..012af5fcf 100644 --- a/cmd/headscale/cli/oauth_client.go +++ b/cmd/headscale/cli/oauth_client.go @@ -10,7 +10,6 @@ import ( "strings" clientv2 "github.com/juanfont/headscale/gen/client/v2" - "github.com/juanfont/headscale/hscontrol/types" "github.com/spf13/cobra" ) @@ -175,10 +174,7 @@ var deleteOAuthClientCmd = &cobra.Command{ // (local trust); a remote address injects the configured API key as a bearer // token. func newV2Client() (context.Context, *clientv2.ClientWithResponses, context.CancelFunc, error) { - cfg, err := types.LoadCLIConfig() - if err != nil { - return nil, nil, nil, fmt.Errorf("loading configuration: %w", err) - } + cfg := loaded.Config ctx, cancel := context.WithTimeout(context.Background(), cfg.CLI.Timeout) diff --git a/cmd/headscale/cli/policy.go b/cmd/headscale/cli/policy.go index 8b36fead9..c5b8ed1bd 100644 --- a/cmd/headscale/cli/policy.go +++ b/cmd/headscale/cli/policy.go @@ -10,7 +10,6 @@ import ( clientv1 "github.com/juanfont/headscale/gen/client/v1" "github.com/juanfont/headscale/hscontrol/db" "github.com/juanfont/headscale/hscontrol/policy" - "github.com/juanfont/headscale/hscontrol/types" "github.com/spf13/cobra" ) @@ -23,7 +22,7 @@ var errAborted = errors.New("command aborted by user") // bypassDatabase opens the database directly, bypassing the running server. // The caller must close the returned handle. func bypassDatabase() (*db.HSDatabase, error) { - cfg, err := types.LoadServerConfig() + cfg, err := serverConfig() if err != nil { return nil, fmt.Errorf("loading config: %w", err) } diff --git a/cmd/headscale/cli/root.go b/cmd/headscale/cli/root.go index 144eb3c05..58d245eae 100644 --- a/cmd/headscale/cli/root.go +++ b/cmd/headscale/cli/root.go @@ -1,12 +1,15 @@ package cli import ( + "cmp" "fmt" + "io" "os" "runtime" "slices" "strings" + "github.com/juanfont/headscale/hscontrol/config" "github.com/juanfont/headscale/hscontrol/types" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -14,7 +17,13 @@ import ( "github.com/tcnksm/go-latest" ) -var cfgFile string = "" +var ( + cfgFile string = "" + + // loaded is what initConfig loaded; nil for the commands that skip it: + // check, version, mockoidc and completion. + loaded *config.Loaded +) func init() { if len(os.Args) > 1 && @@ -55,27 +64,35 @@ func skipsInitConfig(args []string) bool { // loadConfig loads path, else the file -c names, else $HEADSCALE_CONFIG, // else the first config.* in /etc/headscale, $XDG_CONFIG_HOME/headscale, // headscale under each $XDG_CONFIG_DIRS entry, ~/.headscale or ".". Without -// a file it loads defaults and environment only. -func loadConfig(path string) error { - for _, p := range []string{path, cfgFile, os.Getenv("HEADSCALE_CONFIG")} { - if p == "" { - continue - } +// a file it loads defaults and environment only. The result is never nil. +func loadConfig(path string) (*config.Loaded, error) { + path = cmp.Or(path, cfgFile, os.Getenv("HEADSCALE_CONFIG")) - err := types.LoadConfig(p, true) - if err != nil { - return fmt.Errorf("loading config file %s: %w", p, err) - } - - return nil + l, err := config.Load(path, path != "", os.Environ()) + switch { + case err != nil && path != "": + return l, fmt.Errorf("loading config file %s: %w", path, err) + case err != nil: + return l, fmt.Errorf("loading config: %w", err) } - err := types.LoadConfig("", false) - if err != nil { - return fmt.Errorf("loading config: %w", err) - } + return l, nil +} - return nil +// useLogConfig applies cfg's log.level, and logs to w as JSON when cfg sets +// log.format: json. +func useLogConfig(cfg *types.Config, w io.Writer) { + zerolog.SetGlobalLevel(cfg.Log.Level.Zerolog()) + + if cfg.Log.Format == types.JSONLogFormat { + log.Logger = log.Output(w) + } +} + +func logWarnings(warnings []string) { + for _, w := range warnings { + log.Warn().Msg(w) + } } func initConfig() { @@ -86,12 +103,27 @@ func initConfig() { return } - err := loadConfig("") + machineOutput := hasMachineOutputFlag() + + l, err := loadConfig("") if err != nil { + // No log.format applies: main's console logger, on stderr. + logWarnings(l.Warnings) log.Fatal().Caller().Err(err).Msg("error loading config") } - machineOutput := hasMachineOutputFlag() + loaded = l + cfg := l.Config + + // Machine output owns stdout. + logOut := os.Stdout + if machineOutput { + logOut = os.Stderr + } + + useLogConfig(cfg, logOut) + + logWarnings(l.Warnings) // If the user has requested a "node" readable format, // then disable login so the output remains valid. @@ -99,11 +131,7 @@ func initConfig() { zerolog.SetGlobalLevel(zerolog.Disabled) } - if types.LogFormatSetting() == types.JSONLogFormat { - log.Logger = log.Output(os.Stdout) - } - - if !types.UpdateCheckDisabled() && !machineOutput { + if !cfg.DisableUpdateCheck && !machineOutput { versionInfo := types.GetVersionInfo() if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") && !versionInfo.Dirty { diff --git a/cmd/headscale/cli/root_test.go b/cmd/headscale/cli/root_test.go index cb651eaea..8bc9f6ced 100644 --- a/cmd/headscale/cli/root_test.go +++ b/cmd/headscale/cli/root_test.go @@ -1,16 +1,24 @@ package cli import ( + "bytes" + "encoding/json" "fmt" + "maps" "os" + "os/exec" "path/filepath" + "slices" + "strings" "testing" "github.com/juanfont/headscale/hscontrol/db" - "github.com/juanfont/headscale/hscontrol/types" "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) const ( @@ -366,9 +374,9 @@ dns: override_local_dns: false `, filepath.Join(dir, "headscale.db")) require.NoError(t, os.WriteFile(configPath, []byte(config), 0o600)) - require.NoError(t, types.LoadConfig(configPath, true)) + useLoaded(t, configPath) - cfg, err := types.LoadServerConfig() + cfg, err := serverConfig() require.NoError(t, err) database, err := db.NewHeadscaleDatabase(cfg) @@ -414,6 +422,19 @@ dns: } } +// useLoaded loads path as initConfig does, until the test ends. +func useLoaded(t *testing.T, path string) { + t.Helper() + + l, err := loadConfig(path) + require.NoError(t, err) + + old := loaded + loaded = l + + t.Cleanup(func() { loaded = old }) +} + func TestInitConfigSkippedLogLevel(t *testing.T) { oldArgs, oldLevel := os.Args, zerolog.GlobalLevel() @@ -459,8 +480,140 @@ func TestLoadConfig(t *testing.T) { t.Cleanup(func() { cfgFile = old }) - require.NoError(t, loadConfig(tt.path)) - require.Equal(t, tt.want, types.ConfigFile()) + l, err := loadConfig(tt.path) + require.NoError(t, err) + require.Equal(t, tt.want, l.File) + }) + } +} + +// TestCLIOnlyConfig: a file with only what client commands read, as the +// NixOS module writes to /etc/headscale/config.yaml, serves them; check +// config reports what the server lacks. +func TestCLIOnlyConfig(t *testing.T) { + path := writeFile(t, t.TempDir(), "config.yaml", `cli: + address: headscale.example.com:443 + api_key: abc +unix_socket: /run/headscale/headscale.sock +`) + + useLoaded(t, path) + + _, _, cancel, err := newHeadscaleCLIWithConfig() + require.NoError(t, err) + cancel() + + _, err = runCheck(t, checkConfigCmd, path) + require.ErrorContains(t, err, "server_url") +} + +// runHeadscale runs the CLI as main does, in a child process: loading a +// config can exit it. +func runHeadscale(t *testing.T, env []string, args ...string) (string, string, error) { + t.Helper() + + cmd := exec.CommandContext(t.Context(), os.Args[0], "-test.run=^TestHeadscaleProcess$") //nolint:gosec // this test binary + + cmd.Env = append([]string{"HEADSCALE_TEST_ARGS=" + strings.Join(args, "\n")}, env...) + + var stdout, stderr bytes.Buffer + + cmd.Stdout, cmd.Stderr = &stdout, &stderr + err := cmd.Run() + + return stdout.String(), stderr.String(), err +} + +// TestHeadscaleProcess is the child of runHeadscale. +func TestHeadscaleProcess(t *testing.T) { + args := os.Getenv("HEADSCALE_TEST_ARGS") + if args == "" { + t.Skip("run by runHeadscale") + } + + os.Args = append([]string{"headscale"}, strings.Split(args, "\n")...) + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: true}) + + zerolog.SetGlobalLevel(zerolog.TraceLevel) + + Execute() + os.Exit(0) +} + +// TestInitConfigWarnings: what loading warns about follows log.format, never +// reaches stdout under machine output, and is reported with a load error. +func TestInitConfigWarnings(t *testing.T) { + const warning = "unknown environment variable HEADSCALE_BOGUS" + + env := []string{"HEADSCALE_BOGUS=1", "HEADSCALE_DISABLE_CHECK_UPDATES=true"} + dir := t.TempDir() + jsonLogs := writeFile(t, dir, "json.yaml", "log:\n format: json\n") + invalid := writeFile(t, dir, "invalid.yaml", "bogus_key: 1\n") + + // warnLine is the line holding the warning, as JSON when it is one. + warnLine := func(t *testing.T, out string) map[string]any { + t.Helper() + + for l := range strings.SplitSeq(out, "\n") { + if strings.Contains(l, warning) { + var m map[string]any + + _ = json.Unmarshal([]byte(l), &m) + + return m + } + } + + t.Fatalf("no %q in %q", warning, out) + + return nil + } + + t.Run("json log format", func(t *testing.T) { + stdout, stderr, err := runHeadscale(t, env, "-c", jsonLogs, "generate", "private-key") + require.NoError(t, err, stderr) + assert.Equal(t, "warn", warnLine(t, stdout+stderr)["level"]) + }) + + // Every spelling Cobra accepts for --output. + for _, format := range []string{"json", "json-line", "yaml"} { + for _, flag := range [][]string{ + {"-o", format}, {"--output", format}, {"--output=" + format}, {"-o=" + format}, {"-o" + format}, + } { + t.Run("machine output "+strings.Join(flag, " "), func(t *testing.T) { + stdout, stderr, err := runHeadscale(t, env, append([]string{"-c", jsonLogs, "generate", "private-key"}, flag...)...) + require.NoError(t, err, stderr) + + // JSON is YAML too; the result is one object. + var out map[string]string + require.NoError(t, yaml.Unmarshal([]byte(stdout), &out), stdout) + assert.Equal(t, []string{"private_key"}, slices.Collect(maps.Keys(out))) + assert.Equal(t, "warn", warnLine(t, stderr)["level"]) + }) + } + } + + // version has its own --output, which shadows the root's. + for _, flag := range [][]string{{"-o", "json"}, {"--output=json"}} { + t.Run("version "+strings.Join(flag, " "), func(t *testing.T) { + stdout, stderr, err := runHeadscale(t, env, append([]string{"-c", jsonLogs, "version"}, flag...)...) + require.NoError(t, err, stderr) + + var out map[string]any + require.NoError(t, json.Unmarshal([]byte(stdout), &out), stdout) + assert.Contains(t, out, "version") + assert.Equal(t, "warn", warnLine(t, stderr)["level"]) + }) + } + + for _, output := range [][]string{nil, {"-o", "json"}} { + t.Run("load error "+strings.Join(output, " "), func(t *testing.T) { + stdout, stderr, err := runHeadscale(t, env, append([]string{"-c", invalid, "generate", "private-key"}, output...)...) + require.Error(t, err) + assert.Empty(t, stdout) + warnLine(t, stderr) + assert.Contains(t, stderr, "unknown configuration key bogus_key") + assert.NotContains(t, stderr, "panic") }) } } diff --git a/cmd/headscale/cli/utils.go b/cmd/headscale/cli/utils.go index f38768353..8f1cfb5b2 100644 --- a/cmd/headscale/cli/utils.go +++ b/cmd/headscale/cli/utils.go @@ -16,6 +16,7 @@ import ( "github.com/cenkalti/backoff/v5" clientv1 "github.com/juanfont/headscale/gen/client/v1" "github.com/juanfont/headscale/hscontrol" + "github.com/juanfont/headscale/hscontrol/config" "github.com/juanfont/headscale/hscontrol/types" "github.com/juanfont/headscale/hscontrol/util" "github.com/juanfont/headscale/hscontrol/util/zlog/zf" @@ -87,7 +88,7 @@ func mustMarkRequired(cmd *cobra.Command, names ...string) { } func newHeadscaleServerWithConfig() (*hscontrol.Headscale, error) { - cfg, err := types.LoadServerConfig() + cfg, err := serverConfig() if err != nil { return nil, fmt.Errorf( "loading configuration: %w", @@ -103,6 +104,25 @@ func newHeadscaleServerWithConfig() (*hscontrol.Headscale, error) { return app, nil } +// serverConfig is the loaded configuration validated for the server, its +// advisories logged. +func serverConfig() (*types.Config, error) { + cfg := loaded.Config + + err := config.Validate(cfg) + logWarnings(config.Warnings(cfg)) + + if err != nil { + return nil, err + } + + // The server never uses the CLI's key, which may be an unread file: + // reference. + cfg.CLI.APIKey = "" + + return cfg, nil +} + // clientRunE wraps a [cobra.Command.RunE] func, injecting a ready API client // and a context whose timeout/cancel the wrapper owns. func clientRunE( @@ -141,10 +161,7 @@ func withClient( // Otherwise it talks to the remote TCP address over HTTPS and injects the // configured API key as a bearer token. func newHeadscaleCLIWithConfig() (context.Context, *clientv1.ClientWithResponses, context.CancelFunc, error) { - cfg, err := types.LoadCLIConfig() - if err != nil { - return nil, nil, nil, fmt.Errorf("loading configuration: %w", err) - } + cfg := loaded.Config log.Debug(). Dur("timeout", cfg.CLI.Timeout). @@ -408,8 +425,16 @@ func printError(err error, outputFormat string) { fmt.Fprintf(os.Stderr, "%s\n", out) } +// hasMachineOutputFlag reports whether --output, as Cobra parsed it in any +// spelling for the command being run, asks for a machine-readable format. +// The command's own flags hold it: version shadows the root's --output. func hasMachineOutputFlag() bool { - return slices.ContainsFunc(os.Args, func(arg string) bool { - return arg == outputFormatJSON || arg == outputFormatJSONLine || arg == outputFormatYAML - }) + cmd, _, err := rootCmd.Find(os.Args[1:]) + if err != nil { + return false + } + + output, _ := cmd.Flags().GetString("output") + + return slices.Contains([]string{outputFormatJSON, outputFormatJSONLine, outputFormatYAML}, output) } diff --git a/cmd/headscale/cli/utils_test.go b/cmd/headscale/cli/utils_test.go index 87bc46aaa..a366e4359 100644 --- a/cmd/headscale/cli/utils_test.go +++ b/cmd/headscale/cli/utils_test.go @@ -5,7 +5,6 @@ import ( "path/filepath" "testing" - "github.com/juanfont/headscale/hscontrol/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -68,17 +67,15 @@ func TestCLIAPIKey(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cfgDir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(cfgDir, "config.yaml"), + cfgPath := filepath.Join(cfgDir, "config.yaml") + require.NoError(t, os.WriteFile(cfgPath, []byte("unix_socket: "+filepath.Join(cfgDir, "headscale.sock")+"\n"), 0o600)) t.Setenv("HEADSCALE_CLI_ADDRESS", tt.address) t.Setenv("HEADSCALE_CLI_API_KEY", tt.apiKey) - require.NoError(t, types.LoadConfig(cfgDir, false)) + useLoaded(t, cfgPath) if tt.address != "" && tt.wantErr == "" { - cfg, err := types.LoadCLIConfig() - require.NoError(t, err) - - got, err := cliAPIKey(cfg.CLI) + got, err := cliAPIKey(loaded.Config.CLI) require.NoError(t, err) assert.Equal(t, tt.want, got) } @@ -101,3 +98,14 @@ func TestCLIAPIKey(t *testing.T) { }) } } + +// TestServerConfigDropsCLIKey: the server never holds the CLI's key, which +// may be a file only the CLI can read. +func TestServerConfigDropsCLIKey(t *testing.T) { + dir := t.TempDir() + useLoaded(t, writeServerConfig(t, dir, "cli:\n api_key: file:"+filepath.Join(dir, "missing")+"\n")) + + cfg, err := serverConfig() + require.NoError(t, err) + assert.Empty(t, cfg.CLI.APIKey) +} diff --git a/cmd/headscale/headscale_test.go b/cmd/headscale/headscale_test.go index 6f908e5f6..d99439d78 100644 --- a/cmd/headscale/headscale_test.go +++ b/cmd/headscale/headscale_test.go @@ -5,6 +5,7 @@ import ( "path/filepath" "testing" + "github.com/juanfont/headscale/hscontrol/config" "github.com/juanfont/headscale/hscontrol/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -26,10 +27,10 @@ func TestConfigFileLoading(t *testing.T) { require.NoError(t, err) // Load example config, it should load without validation errors - err = types.LoadConfig(cfgFile, true) + l, err := config.Load(cfgFile, true, os.Environ()) require.NoError(t, err) - assertExampleConfig(t) + assertExampleConfig(t, l.Config) } func TestConfigLoading(t *testing.T) { @@ -46,19 +47,18 @@ func TestConfigLoading(t *testing.T) { require.NoError(t, err) // Load example config, it should load without validation errors - err = types.LoadConfig(tmpDir, false) + l, err := config.Load(tmpDir, false, os.Environ()) require.NoError(t, err) - assertExampleConfig(t) + assertExampleConfig(t, l.Config) } // assertExampleConfig checks the loaded config-example.yaml was interpreted // correctly. -func assertExampleConfig(t *testing.T) { +func assertExampleConfig(t *testing.T, cfg *types.Config) { t.Helper() - cfg, err := types.LoadServerConfig() - require.NoError(t, err) + require.NoError(t, config.Validate(cfg)) assert.Equal(t, "http://127.0.0.1:8080", cfg.ServerURL) assert.Equal(t, types.ListenAddr("127.0.0.1:8080"), cfg.Addr) diff --git a/flake.nix b/flake.nix index 53300a9e0..1b5ec661d 100644 --- a/flake.nix +++ b/flake.nix @@ -185,7 +185,7 @@ ]; extraSrc = [ ./hscontrol/testdata - ./hscontrol/types/testdata + ./hscontrol/config/testdata ./hscontrol/db/testdata ./hscontrol/policy/v2/testdata ]; diff --git a/hscontrol/app.go b/hscontrol/app.go index f729cf172..2b86cf748 100644 --- a/hscontrol/app.go +++ b/hscontrol/app.go @@ -29,7 +29,6 @@ import ( "github.com/caddyserver/certmagic" "github.com/cenkalti/backoff/v5" - "github.com/davecgh/go-spew/spew" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "github.com/go-chi/metrics" @@ -577,7 +576,12 @@ func (h *Headscale) Serve() error { } if dumpConfig { - spew.Dump(h.cfg) + out, err := config.Marshal(h.cfg, ".yaml") + if err != nil { + return fmt.Errorf("dumping config: %w", err) + } + + _, _ = os.Stdout.Write(out) } versionInfo := types.GetVersionInfo() diff --git a/hscontrol/app_test.go b/hscontrol/app_test.go index e0f563c15..bc1084207 100644 --- a/hscontrol/app_test.go +++ b/hscontrol/app_test.go @@ -86,6 +86,31 @@ func TestDebugHostGuard(t *testing.T) { } } +// TestDebugConfigRedacts: /debug/config is the configuration as +// config.Marshal writes it, without credentials; the running values keep +// theirs. +func TestDebugConfigRedacts(t *testing.T) { + app := createTestApp(t) + app.cfg.ServerURL = "https://marker-user:marker-pass@localhost:8080" + app.cfg.CLI.Address = "https://marker-cli@cli.example.com" + app.cfg.DNSConfig.Nameservers.Global = []string{"1.1.1.1", "https://marker-dns-user:marker-dns-pass@dns.example.com/q"} + app.cfg.DNSConfig.Nameservers.Split = map[string][]string{"corp.example.com": {"https://marker-split@dns.example.com/q"}} + app.cfg.Database.Postgres.Pass = "marker-pg-pass" + + req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/debug/config", nil) + req.Host = "localhost" + req.RemoteAddr = "127.0.0.1:1234" + rec := httptest.NewRecorder() + app.debugHTTPServer().Handler.ServeHTTP(rec, req) + + require.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, "application/json", rec.Header().Get("Content-Type")) + assert.NotContains(t, rec.Body.String(), "marker") + assert.Contains(t, rec.Body.String(), `"server_url":"https://localhost:8080"`) + assert.Contains(t, rec.Body.String(), `"global":["1.1.1.1","https://dns.example.com/q"]`) + assert.Contains(t, app.cfg.ServerURL, "marker-pass") +} + // TestNewHeadscaleDNSProvider pins that startup builds the provider // dns.challenge.provider names, reading its secrets, unless Go set one, and // refuses one that cannot keep node certificates apart. diff --git a/hscontrol/config/default.go b/hscontrol/config/default.go new file mode 100644 index 000000000..dbd1fd113 --- /dev/null +++ b/hscontrol/config/default.go @@ -0,0 +1,73 @@ +package config + +import ( + "time" + + "github.com/coreos/go-oidc/v3/oidc" + "github.com/juanfont/headscale/hscontrol/types" +) + +// Default is the configuration before a file or variable sets +// anything. database.type has no default. +func Default() types.Config { + return types.Config{ + UnixSocket: "/var/run/headscale/headscale.sock", + UnixSocketPermission: 0o770, + Prefixes: types.PrefixesConfig{Allocation: types.IPAllocationStrategySequential}, + Policy: types.PolicyConfig{Mode: types.PolicyModeFile}, + Log: types.LogConfig{Level: "info", Format: types.TextLogFormat}, + Node: types.NodeConfig{ + Ephemeral: types.EphemeralConfig{InactivityTimeout: 120 * time.Second}, + Routes: types.RouteConfig{HA: types.HARouteConfig{ + ProbeInterval: 10 * time.Second, + ProbeTimeout: 5 * time.Second, + }}, + }, + PreAuthKeys: types.PreAuthKeysConfig{RevokedRetention: 168 * time.Hour}, + DERP: types.DERPConfig{ + Server: types.DERPServerConfig{ + VerifyClients: true, + AutomaticallyAddEmbeddedDerpRegion: true, + }, + UpdateFrequency: 3 * time.Hour, + }, + Database: types.DatabaseConfig{ + Sqlite: types.SqliteConfig{ + WriteAheadLog: true, + WALAutoCheckPoint: 1000, // SQLite default + }, + Postgres: types.PostgresConfig{ + Ssl: "false", + MaxOpenConnections: 10, + MaxIdleConnections: 10, + ConnMaxIdleTimeSecs: 3600, + }, + }, + DNSConfig: types.DNSConfig{ + MagicDNS: true, + OverrideLocalDNS: true, + Nameservers: types.Nameservers{Global: []string{}, Split: map[string][]string{}}, + SearchDomains: []string{}, + Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, + }, + TLS: types.TLSConfig{ACME: types.ACMEConfig{ + ChallengeType: types.HTTP01ChallengeType, + HTTPListen: ":http", + CacheDir: "/var/lib/headscale/cache", + }}, + OIDC: types.OIDCConfig{ + OnlyStartIfOIDCIsAvailable: true, + Scope: []string{oidc.ScopeOpenID, "profile", "email"}, + EmailVerifiedRequired: true, + PKCE: types.PKCEConfig{Method: types.PKCEMethodS256}, + }, + Taildrop: types.TaildropConfig{Enabled: true}, + CLI: types.CLIConfig{Timeout: 5 * time.Second}, + Tuning: types.Tuning{ + BatchChangeDelay: 800 * time.Millisecond, + NodeMapSessionBufferedChanSize: 30, + NodeStoreBatchSize: 100, + NodeStoreBatchTimeout: 500 * time.Millisecond, + }, + } +} diff --git a/hscontrol/config/default_test.go b/hscontrol/config/default_test.go new file mode 100644 index 000000000..83016a4b6 --- /dev/null +++ b/hscontrol/config/default_test.go @@ -0,0 +1,22 @@ +package config + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" +) + +// TestValidateHintsNameDefaults: the defaults Validate's hints quote, which +// types cannot read from Default, are Default's. +func TestValidateHintsNameDefaults(t *testing.T) { + d := Default() + + cfg := Default() + cfg.Tuning.NodeStoreBatchSize = 0 + cfg.Tuning.NodeStoreBatchTimeout = 0 + + err := Validate(&cfg) + require.ErrorContains(t, err, fmt.Sprintf("(default: %d)", d.Tuning.NodeStoreBatchSize)) + require.ErrorContains(t, err, fmt.Sprintf("(default: %s)", d.Tuning.NodeStoreBatchTimeout)) +} diff --git a/hscontrol/config/files.go b/hscontrol/config/files.go index cd81de4be..299167844 100644 --- a/hscontrol/config/files.go +++ b/hscontrol/config/files.go @@ -1,5 +1,6 @@ -// Package config checks headscale's configuration and the files it names. -// It imports types; types, derp, dns and policy must never import it. +// Package config loads and validates headscale's configuration and checks +// the files it names. It imports types; types, derp, dns and policy must +// never import it. package config import ( diff --git a/hscontrol/types/config_keys.go b/hscontrol/config/keys.go similarity index 99% rename from hscontrol/types/config_keys.go rename to hscontrol/config/keys.go index 1d75a0604..a6c862264 100644 --- a/hscontrol/types/config_keys.go +++ b/hscontrol/config/keys.go @@ -1,4 +1,4 @@ -package types +package config import ( "slices" diff --git a/hscontrol/types/config_keys_test.go b/hscontrol/config/keys_test.go similarity index 84% rename from hscontrol/types/config_keys_test.go rename to hscontrol/config/keys_test.go index 1c30a5992..8f79bda3b 100644 --- a/hscontrol/types/config_keys_test.go +++ b/hscontrol/config/keys_test.go @@ -1,4 +1,4 @@ -package types +package config import ( "strings" @@ -74,3 +74,17 @@ func TestDottedPosition(t *testing.T) { }) } } + +// TestRemovedKeysAgainstSchema: a removed key is not a key, and what +// replaces it is. +func TestRemovedKeysAgainstSchema(t *testing.T) { + leaf, branch := configKeySets() + + for _, r := range removedKeys { + assert.Falsef(t, leaf[r.Old] || branch[r.Old], "removed key %s is a key", r.Old) + + for k := range strings.FieldsSeq(r.New) { + assert.Truef(t, leaf[k] || branch[k], "%s replaces %s but is no key", k, r.Old) + } + } +} diff --git a/hscontrol/types/config_source.go b/hscontrol/config/load.go similarity index 87% rename from hscontrol/types/config_source.go rename to hscontrol/config/load.go index a217c8682..9ac3558b6 100644 --- a/hscontrol/types/config_source.go +++ b/hscontrol/config/load.go @@ -1,4 +1,4 @@ -package types +package config import ( "bytes" @@ -20,13 +20,13 @@ import ( "github.com/go-viper/mapstructure/v2" hsdns "github.com/juanfont/headscale/hscontrol/dns" + "github.com/juanfont/headscale/hscontrol/types" "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/tailscale/hujson" ) @@ -54,7 +54,7 @@ var reservedEnvNames = []string{ // configSource is the layered configuration: defaults, then file, then // environment, with lowercase keys. [configSource.decode] turns it into a -// [Config]. +// [types.Config]. type configSource struct { k *koanf.Koanf file string @@ -198,7 +198,7 @@ type schemaKey struct { } func schemaKeys() []schemaKey { - fields := ConfigFields(&Config{}) + fields := types.ConfigFields(&types.Config{}) keys := make([]schemaKey, len(fields)) for i, f := range fields { @@ -306,34 +306,60 @@ func joinConfigPath(prefix, name string) string { return prefix + "." + name } +// Loaded is a configuration as [Load] found it. +type Loaded struct { + // Config is nil when loading failed. + Config *types.Config + // File is the file loaded or failed to load, or "" when none was found. + File string + // Warnings are findings that do not stop loading, kept when it fails. + Warnings []string +} + +// Load loads defaults, the configuration file found via path and isFile, +// and the HEADSCALE_* variables of environ (os.Environ form), rejecting +// removed and unknown file keys, and decodes them. It does not validate: +// see [Validate]. +func Load(path string, isFile bool, environ []string) (*Loaded, error) { + s, err := newConfigSource(path, isFile, environ) + + l := &Loaded{File: s.file, Warnings: s.warnings} + if err != nil { + return l, err + } + + l.Config, err = s.decode() + + return l, err +} + // newConfigSource loads defaults, the config file found via path/isFile and -// environ (os.Environ form), and rejects removed and unknown file keys. +// environ (os.Environ form), and rejects removed and unknown file keys. The +// source is returned with what it found even on error. func newConfigSource(path string, isFile bool, environ []string) (*configSource, error) { s := &configSource{k: koanf.New("."), envVars: map[string]string{}} + s.file = findConfigFile(path, isFile, environ) - d := defaultConfig() + d := Default() err := s.k.Load(confmap.Provider(plainMap(&d, false), ""), nil) if err != nil { - return nil, fmt.Errorf("loading defaults: %w", err) + return s, fmt.Errorf("loading defaults: %w", err) } - s.file = findConfigFile(path, isFile, environ) + var fileErr error + if s.file == "" { - log.Warn().Msg("no config file found, using defaults") + s.warnings = append(s.warnings, "no config file found, using defaults") } else { - err = s.loadFile() - if err != nil { - return nil, err - } + fileErr = s.loadFile() } - err = s.loadEnv(environ) - if err != nil { - return nil, err - } + // Read even when the file fails, so its variables' warnings are reported + // with the file's error. + envErr := s.loadEnv(environ) - return s, nil + return s, cmp.Or(fileErr, envErr) } func (s *configSource) loadFile() error { @@ -372,11 +398,11 @@ func (s *configSource) loadFile() error { m = normaliseConfigMap(m) // Checked before expansion, which would otherwise discard shadowed keys. - v := &configValidator{} - s.checkKeys(v, m) + var errs []error + s.checkKeys(&errs, m) - if v.HasErrors() { - return v.Err() + if len(errs) > 0 { + return errors.Join(errs...) } m = expandDottedKeys(m, "") @@ -516,7 +542,7 @@ func (r removedKey) reason(what, name string) string { } // checkKeys adds an error for every removed or unknown key in the file. -func (s *configSource) checkKeys(v *configValidator, m map[string]any) { +func (s *configSource) checkKeys(errs *[]error, m map[string]any) { leaf, branch := configKeySets() var walk func(prefix string, m map[string]any) @@ -533,7 +559,7 @@ func (s *configSource) checkKeys(v *configValidator, m map[string]any) { switch r, removed := removedConfigKey(path); { case removed: - v.Add(&ConfigError{ + *errs = append(*errs, &types.ConfigError{ Reason: r.reason("key", path) + s.position(path), Hint: r.hint(), }) @@ -549,7 +575,7 @@ func (s *configSource) checkKeys(v *configValidator, m map[string]any) { } walkRemoved(root, below, false, func(p string, r removedKey) { - v.Add(&ConfigError{Reason: r.reason("key", p) + s.position(p), Hint: r.hint()}) + *errs = append(*errs, &types.ConfigError{Reason: r.reason("key", p) + s.position(p), Hint: r.hint()}) }) case leaf[path] && !isMap: case (leaf[path] || branch[path]) && isMap: @@ -557,17 +583,17 @@ func (s *configSource) checkKeys(v *configValidator, m map[string]any) { walk(path, child) case branch[path]: // The value is not shown: a misplaced block may hold secrets. - v.Add(&ConfigError{ + *errs = append(*errs, &types.ConfigError{ Reason: fmt.Sprintf("configuration key %s must be a mapping%s", path, s.position(path)), }) default: - v.Add(&ConfigError{Reason: fmt.Sprintf("unknown configuration key %s%s", path, s.position(path))}) + *errs = append(*errs, &types.ConfigError{Reason: fmt.Sprintf("unknown configuration key %s%s", path, s.position(path))}) } } } walk("", m) - s.checkOverlaps(v, m) + s.checkOverlaps(errs, m) } // position renders " (file:line:col)" when the key can be located, written @@ -700,7 +726,7 @@ func (s *configSource) foldProviderEnv(env map[string]string) error { fold("name") - for _, setting := range hsdns.SettingNames(DNSChallengeConfig{ProviderSettings: settings}.ProviderName()) { + for _, setting := range hsdns.SettingNames(types.DNSChallengeConfig{ProviderSettings: settings}.ProviderName()) { fold(setting) } @@ -739,12 +765,6 @@ func removedEnv(name string) (removedKey, bool) { return removedKey{}, false } -// File returns the config file in use, or "" when only defaults apply. -func (s *configSource) File() string { return s.file } - -// Warnings returns non-fatal findings from loading. -func (s *configSource) Warnings() []string { return s.warnings } - // keyEntry is one key of the file as written, for finding paths that the file // spells in more than one form. type keyEntry struct { @@ -761,7 +781,7 @@ func (e keyEntry) spelling() string { return strings.Join(e.names, " > ") } // checkOverlaps rejects a path given both dotted and nested ("log.level" next // to "log: {level: ...}"): no rule says which of the two should win. -func (s *configSource) checkOverlaps(v *configValidator, m map[string]any) { +func (s *configSource) checkOverlaps(errs *[]error, m map[string]any) { var entries []keyEntry var collect func(prefix string, parent int, names []string, m map[string]any) @@ -823,7 +843,7 @@ func (s *configSource) checkOverlaps(v *configValidator, m map[string]any) { a, b = b, a } - v.Add(&ConfigError{ + *errs = append(*errs, &types.ConfigError{ Reason: fmt.Sprintf("configuration key %s is set both as %q%s and as %q%s", a.path, a.spelling(), s.positionOf(a.names), b.spelling(), s.positionOf(b.names)), Hint: "use one form, either the dotted key or nested mappings", @@ -844,8 +864,8 @@ func (s *configSource) positionOf(names []string) string { // 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 +func (s *configSource) decode() (*types.Config, error) { + var cfg types.Config err := s.k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{ Tag: "koanf", @@ -890,10 +910,11 @@ func (s *configSource) configDir() string { // 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{} + var errs []error + seen := map[string]bool{} - walkErrTree(err, func(e error) { + util.WalkErrTree(err, func(e error) { de, ok := e.(*mapstructure.DecodeError) //nolint:errorlint // every node is visited if !ok { return @@ -902,9 +923,9 @@ func (s *configSource) decodeErrors(err error) error { // An element or entry is reported on its key. key, _, _ := strings.Cut(de.Name(), "[") - var leaf *ConfigError + var leaf *types.ConfigError if !errors.As(de.Unwrap(), &leaf) { - leaf = &ConfigError{Reason: de.Unwrap().Error(), Cause: de.Unwrap()} + leaf = &types.ConfigError{Reason: de.Unwrap().Error(), Cause: de.Unwrap()} // The decoder's own error keeps the value (UnconvertibleTypeError's // Value); its text does not. @@ -917,20 +938,20 @@ func (s *configSource) decodeErrors(err error) error { ce.Reason = key + " " + leaf.Reason + s.origin(key) if !hiddenValue(key) { - ce.Current = []KV{{key, s.k.Get(key)}} + ce.Current = []types.KV{{Key: key, Value: s.k.Get(key)}} } if !seen[ce.Reason] { seen[ce.Reason] = true - v.Add(&ce) + errs = append(errs, &ce) } }) - if !v.HasErrors() { + if len(errs) == 0 { return err } - return v.Err() + return errors.Join(errs...) } // origin renders where key's value came from: its variable, else its @@ -950,26 +971,27 @@ func (s *configSource) origin(key string) string { // 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) { + if key == providerKey || strings.HasPrefix(key, providerKey+".") || slices.Contains(types.URLKeys, key) { return true } - for _, f := range ConfigFields(&Config{}) { + for _, f := range types.ConfigFields(&types.Config{}) { if f.Path != key { continue } - return f.Field.Type == reflect.TypeFor[Secret]() + return f.Field.Type == reflect.TypeFor[types.Secret]() } return false } var ( - durationType = reflect.TypeFor[time.Duration]() - fileModeType = reflect.TypeFor[FileMode]() - prefixType = reflect.TypeFor[netip.Prefix]() - prefixPtrType = reflect.TypeFor[*netip.Prefix]() + textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]() + durationType = reflect.TypeFor[time.Duration]() + fileModeType = reflect.TypeFor[types.FileMode]() + prefixType = reflect.TypeFor[netip.Prefix]() + prefixPtrType = reflect.TypeFor[*netip.Prefix]() ) // scalarHook converts a value as written into what its field's type takes, @@ -985,7 +1007,7 @@ func scalarHook(from, to reflect.Value) (any, error) { 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{ + return nil, &types.ConfigError{ Reason: "must be quoted", Maximum: `"0777"`, Hint: `quote the value, e.g. "0770"; unquoted, YAML reads 0770 as the number 504`, @@ -997,7 +1019,7 @@ func scalarHook(from, to reflect.Value) (any, error) { case t == prefixType && isString: _, err := netip.ParsePrefix(s) if err != nil { - return nil, &ConfigError{ + return nil, &types.ConfigError{ Reason: "is not a valid CIDR", Detail: err.Error(), Hint: `use CIDR form, e.g. "100.64.0.0/10"`, @@ -1005,7 +1027,7 @@ func scalarHook(from, to reflect.Value) (any, error) { } } case t.Kind() == reflect.String: - if text, ok := scalarText(from); ok { + if text, ok := util.ScalarText(from); ok { return text, nil } case t.Kind() == reflect.Bool: @@ -1021,27 +1043,11 @@ func scalarHook(from, to reflect.Value) (any, error) { 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"`} + unit := &types.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 { + if _, ok := util.ScalarText(v); !ok || v.Kind() == reflect.Bool { return v.Interface(), nil } @@ -1069,20 +1075,20 @@ func toDuration(v reflect.Value) (any, error) { return nil, unit } - return nil, &ConfigError{Reason: "is not a duration", Hint: unit.Hint} + return nil, &types.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 { + if text, ok = util.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 nil, &types.ConfigError{Reason: "is not a boolean", Hint: "use true or false", Cause: err} } return b, nil @@ -1099,7 +1105,7 @@ func toInt(v reflect.Value, t reflect.Type) (any, error) { 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"} + return nil, &types.ConfigError{Reason: "is out of range"} } n, whole = i, err == nil @@ -1107,14 +1113,14 @@ func toInt(v reflect.Value, t reflect.Type) (any, error) { n, whole = v.Int(), true case v.CanUint(): if v.Uint() > math.MaxInt64 { - return nil, &ConfigError{Reason: "is out of range"} + return nil, &types.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"} + return nil, &types.ConfigError{Reason: "is out of range"} } n, whole = int64(f), f == math.Trunc(f) @@ -1123,11 +1129,11 @@ func toInt(v reflect.Value, t reflect.Type) (any, error) { } if !whole { - return nil, &ConfigError{Reason: "is not an integer"} + return nil, &types.ConfigError{Reason: "is not an integer"} } if reflect.Zero(t).OverflowInt(n) { - return nil, &ConfigError{Reason: "is out of range"} + return nil, &types.ConfigError{Reason: "is out of range"} } return n, nil @@ -1158,7 +1164,7 @@ func toList(s string, elem reflect.Type) (any, error) { 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 nil, &types.ConfigError{Reason: "is not a JSON list", Hint: hint, Cause: err} } return list, nil @@ -1169,7 +1175,7 @@ func toObject(s string) (any, error) { err := stdjson.Unmarshal([]byte(s), &m) if err != nil || m == nil { - return nil, &ConfigError{ + return nil, &types.ConfigError{ Reason: "is not a JSON object", Hint: `write the map as JSON, e.g. {"example.com": ["1.1.1.1"]}`, Cause: err, @@ -1184,7 +1190,7 @@ func toObject(s string) (any, error) { // 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 { +func plainMap(cfg *types.Config, redact bool) map[string]any { out := map[string]any{} if redact { @@ -1193,7 +1199,7 @@ func plainMap(cfg *Config, redact bool) map[string]any { cfg = &r } - for _, f := range ConfigFields(cfg) { + for _, f := range types.ConfigFields(cfg) { v := plainValue(f.Value, redact) switch settings, isMap := v.(map[string]any); { @@ -1244,13 +1250,13 @@ func plainValue(v reflect.Value, redact bool) any { switch x := v.Interface().(type) { case time.Duration: return x.String() - case Secret: + case types.Secret: if redact { return x.Redacted() } return string(x) - case HTTPURL: + case types.HTTPURL: // As written: its JSON form drops userinfo. if redact { return x.Redacted() @@ -1325,11 +1331,11 @@ func redactProvider(settings map[string]any) map[string]any { 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 - } + // A name that is a map or list stays hidden: it is no name, and its + // ProviderName is "", as an empty name's is. + name, set := settings["name"] + if set && (name == nil || name == "" || (types.DNSChallengeConfig{ProviderSettings: settings}).ProviderName() != "") { + out["name"] = name } return out diff --git a/hscontrol/config/load_test.go b/hscontrol/config/load_test.go new file mode 100644 index 000000000..db5b32ca3 --- /dev/null +++ b/hscontrol/config/load_test.go @@ -0,0 +1,3940 @@ +package config + +import ( + "bytes" + "cmp" + "encoding/json" + "fmt" + "io/fs" + "maps" + "math" + "net/netip" + "net/url" + "os" + "path/filepath" + "reflect" + "runtime" + "slices" + "strconv" + "strings" + "testing" + "time" + + gocmp "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + hsdns "github.com/juanfont/headscale/hscontrol/dns" + "github.com/juanfont/headscale/hscontrol/types" + "github.com/juanfont/headscale/hscontrol/util" + "github.com/knadh/koanf/providers/confmap" + "github.com/knadh/koanf/v2" + "github.com/libdns/cloudflare" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "tailscale.com/tailcfg" + "tailscale.com/types/dnstype" +) + +// TestMain runs the tests from this directory, so the relative testdata/ +// paths resolve when the binary runs elsewhere (e.g. via "go tool stress"). +func TestMain(m *testing.M) { + _, filename, _, ok := runtime.Caller(0) + if !ok { + panic("could not determine test source directory") + } + + err := os.Chdir(filepath.Dir(filename)) + if err != nil { + panic("could not chdir to test source directory: " + err.Error()) + } + + os.Exit(m.Run()) +} + +// 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(), "config.yaml") + require.NoError(t, os.WriteFile(p, []byte(content), 0o600)) + + return p +} + +func TestConfigSource(t *testing.T) { + tests := []struct { + name string + file string // written as config.yaml unless fileName is set + fileName string + emptyFile bool + env []string + errs []string // substrings of the error + warns []string // substrings, one per expected warning + check func(t *testing.T, cfg *types.Config) + }{ + { + name: "unknown file key", + file: "server_url: http://x\ndns:\n magic_dsn: true\n", + errs: []string{"unknown configuration key dns.magic_dsn (", "config.yaml:3:3)"}, + }, + { + name: "removed file key", + file: "grpc_listen_addr: :50443\n", + errs: []string{"grpc_listen_addr", "removed"}, + }, + { + name: "removed file prefix", + file: "dns_config:\n magic_dns: true\n", + errs: []string{"dns_config", "removed", "dns"}, + }, + { + name: "all problems collected", + file: "grpc_listen_addr: x\nbogus: 1\n", + errs: []string{"grpc_listen_addr", "bogus"}, + }, + { + name: "removed env warns", + env: []string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT=30m"}, + warns: []string{"configuration variable HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT was removed in 0.30; use HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT"}, + }, + { + name: "unknown env warns", + env: []string{"HEADSCALE_SERVR_URL=x"}, + warns: []string{"unknown environment variable HEADSCALE_SERVR_URL"}, + }, + { + name: "reserved env is silent", + env: []string{ + "HEADSCALE_PORT=tcp://10.0.0.1:8080", "HEADSCALE_SERVICE_HOST=x", "HEADSCALE_DEBUG_FOO=1", + "HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN=x", "HEADSCALE_CONFIG=/x", "HEADSCALE_TEST_X=1", + "HEADSCALE_INTEGRATION_X=1", "HEADSCALE_UPDATE_GOLDEN=1", + }, + }, + { + // What Kubernetes sets for a Service named headscale-db. + name: "service variable named like a removed key warns", + env: []string{"HEADSCALE_DB_PORT=tcp://10.0.0.5:5432"}, + warns: []string{"configuration variable HEADSCALE_DB_PORT was removed in 0.30; use HEADSCALE_DATABASE_POSTGRES_PORT"}, + }, + { + 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, cfg *types.Config) { + t.Helper() + assert.Equal(t, types.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, cfg *types.Config) { + t.Helper() + assert.Equal(t, "https://x", cfg.ServerURL) + assert.Equal(t, + map[string][]string{"corp.example.com": {"10.0.0.1"}}, + cfg.DNSConfig.Nameservers.Split) + }, + }, + { + name: "null is unset, default stays", + file: "server_url:\nlog:\n level: ~\n", + check: func(t *testing.T, cfg *types.Config) { + t.Helper() + assert.Empty(t, cfg.ServerURL) + assert.Equal(t, types.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, cfg *types.Config) { + t.Helper() + assert.Equal(t, "http://env", cfg.ServerURL) + assert.Equal(t, types.ListenAddr("1.1.1.1:1"), cfg.Addr) + }, + }, + { + name: "env lists and maps", + env: []string{ + "HEADSCALE_DNS_NAMESERVERS_GLOBAL=1.1.1.1 8.8.8.8", + `HEADSCALE_OIDC_EXTRA_PARAMS={"domain_hint":"example.com"}`, + }, + check: func(t *testing.T, cfg *types.Config) { + t.Helper() + 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, cfg *types.Config) { + t.Helper() + assert.Equal(t, "cloudflare", cfg.DNSConfig.Challenge.ProviderName()) + assert.Equal(t, 1, cfg.DNSConfig.Challenge.ProviderSettings["whatever"]) + }, + }, + { + name: "yaml", + fileName: "config.yaml", + file: yamlSample, + check: checkSameValues, + }, + { + name: "yml", + fileName: "config.yml", + file: yamlSample, + check: checkSameValues, + }, + { + name: "json", + fileName: "config.json", + file: `{"server_url":"https://x","node":{"expiry":"1h"},"dns":{"magic_dns":false,"nameservers":{"global":["1.1.1.1","8.8.8.8"]}},"oidc":{"extra_params":{"domain_hint":"example.com"}}}`, + check: checkSameValues, + }, + { + name: "hujson", + fileName: "config.hujson", + file: "{\n // c\n \"server_url\":\"https://x\",\n \"node\":{\"expiry\":\"1h\",},\n \"dns\":{\"magic_dns\":false,\"nameservers\":{\"global\":[\"1.1.1.1\",\"8.8.8.8\",],},},\n \"oidc\":{\"extra_params\":{\"domain_hint\":\"example.com\"}},\n}", + check: checkSameValues, + }, + { + name: "hujson syntax error", + fileName: "config.hujson", + file: "{\n // c\n \"server_url\": x,\n}", + errs: []string{"config.hujson", "hujson: line 3, column 16"}, + }, + { + name: "toml is found and rejected", + fileName: "config.toml", + file: "server_url = \"https://x\"\n", + errs: []string{`unsupported config file format ".toml": use .json, .yaml, .yml or .hujson`, "config.toml)"}, + }, + { + name: ".env is found and rejected", + fileName: "config.env", + file: "SERVER_URL=https://x\n", + errs: []string{`unsupported config file format ".env": use .json, .yaml, .yml or .hujson`, "config.env)"}, + }, + { + name: ".dotenv is found and rejected", + fileName: "config.dotenv", + file: "SERVER_URL=https://x\n", + errs: []string{`unsupported config file format ".dotenv": use .json, .yaml, .yml or .hujson`, "config.dotenv)"}, + }, + { + name: "nested keys that share leaf names with removed keys", + file: "noise:\n private_key_path: /n\nderp:\n server:\n private_key_path: /d\nlog:\n level: debug\ntls:\n cert_path: /c\ndatabase:\n sqlite:\n path: /db\n", + check: func(t *testing.T, cfg *types.Config) { + t.Helper() + assert.Equal(t, "/n", cfg.Noise.PrivateKeyPath) + assert.Equal(t, "/d", cfg.DERP.Server.PrivateKeyPath) + assert.Equal(t, types.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, cfg *types.Config) { + t.Helper() + assert.Equal(t, types.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) + }, + }, + { + name: "dotted file key in a removed prefix", + file: "dns_config.magic_dns: true\n", + errs: []string{"dns_config", "removed"}, + }, + { + name: "mapping under a plain setting", + file: "tuning:\n batcher_workers:\n bogus: 1\nserver_url:\n bogus: x\n", + errs: []string{"unknown configuration key tuning.batcher_workers.bogus", "unknown configuration key server_url.bogus"}, + }, + { + name: "non-string yaml keys are normalised", + file: "dns:\n nameservers:\n split:\n 123: [1.1.1.1]\n Corp.Example.com: [10.0.0.1]\n Empty.Example.com: ~\n", + check: func(t *testing.T, cfg *types.Config) { + t.Helper() + assert.Equal(t, + map[string][]string{"123": {"1.1.1.1"}, "corp.example.com": {"10.0.0.1"}}, + cfg.DNSConfig.Nameservers.Split) + }, + }, + { + name: "removed dns_config env warns", + env: []string{"HEADSCALE_DNS_CONFIG_MAGIC_DNS=false", "HEADSCALE_DNS_CONFIG_NAMESERVERS=1.1.1.1"}, + warns: []string{ + "variable HEADSCALE_DNS_CONFIG_MAGIC_DNS was removed in 0.30; use HEADSCALE_DNS_MAGIC_DNS", + "variable HEADSCALE_DNS_CONFIG_NAMESERVERS was removed in 0.30; use HEADSCALE_DNS_NAMESERVERS_GLOBAL", + }, + }, + { + name: "removed block env names the variables replacing it", + env: []string{`HEADSCALE_DNS_CONFIG={"magic_dns":true}`, "HEADSCALE_RANDOMIZE_CLIENT_PORT=true"}, + warns: []string{ + "variable HEADSCALE_DNS_CONFIG was removed in 0.30; use HEADSCALE_DNS_MAGIC_DNS, HEADSCALE_DNS_BASE_DOMAIN, " + + "HEADSCALE_DNS_OVERRIDE_LOCAL_DNS, HEADSCALE_DNS_NAMESERVERS_GLOBAL, HEADSCALE_DNS_NAMESERVERS_SPLIT, " + + "HEADSCALE_DNS_SEARCH_DOMAINS and HEADSCALE_DNS_EXTRA_RECORDS", + `variable HEADSCALE_RANDOMIZE_CLIENT_PORT was removed in 0.29; set "randomizeClientPort": true at the top level of the policy file`, + }, + }, + { + name: "exact reserved names do not exempt near misses", + env: []string{"HEADSCALE_CONFIG=/x", "HEADSCALE_UPDATE_GOLDEN=1", "HEADSCALE_DNS_CHALLENGE_PROVIDER={}", "HEADSCALE_CONFIG_TYPO=1", "HEADSCALE_UPDATE_GOLDEN_BOGUS=1"}, + warns: []string{"HEADSCALE_CONFIG_TYPO", "HEADSCALE_UPDATE_GOLDEN_BOGUS"}, + }, + { + name: "empty file", + emptyFile: true, + check: func(t *testing.T, cfg *types.Config) { + t.Helper() + assert.Equal(t, types.LogLevel("info"), cfg.Log.Level) + }, + }, + { + name: "unsupported format", + fileName: "config.ini", + file: "a=1\n", + errs: []string{"unsupported config file format"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + path, isFile := dir, false + + warns := append([]string{"no config file found"}, tt.warns...) + + if tt.file != "" || tt.fileName != "" || tt.emptyFile { + warns = tt.warns + + name := cmp.Or(tt.fileName, "config.yaml") + require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(tt.file), 0o600)) + + // .ini is not searched for, so it has to be named. + if filepath.Ext(name) == ".ini" { + path, isFile = filepath.Join(dir, name), true + } + } + + l, err := Load(path, isFile, tt.env) + if len(tt.errs) > 0 { + require.Error(t, err) + + for _, want := range tt.errs { + require.ErrorContains(t, err, want) + } + + return + } + + require.NoError(t, err) + require.Len(t, l.Warnings, len(warns)) + + for i, w := range warns { + assert.Contains(t, l.Warnings[i], w) + } + + if tt.check != nil { + tt.check(t, l.Config) + } + }) + } +} + +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, cfg *types.Config) { + t.Helper() + 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) { + got := configSearchDirs([]string{"HOME=/h", "XDG_CONFIG_HOME=/xh", "XDG_CONFIG_DIRS=/a:/b"}) + assert.Equal(t, []string{"/etc/headscale", "/xh/headscale", "/a/headscale", "/b/headscale", "/h/.headscale", "."}, got) + + got = configSearchDirs([]string{"HOME=/h"}) + assert.Equal(t, []string{"/etc/headscale", "/h/.config/headscale", "/etc/xdg/headscale", "/h/.headscale", "."}, got) + + xdg := t.TempDir() + require.NoError(t, os.MkdirAll(filepath.Join(xdg, "headscale"), 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(xdg, "headscale", "config.yaml"), nil, 0o600)) + assert.Equal(t, filepath.Join(xdg, "headscale", "config.yaml"), findConfigFile("", false, []string{"XDG_CONFIG_HOME=" + xdg})) +} + +func TestConfigSourceSearch(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte("server_url: http://yaml\n"), 0o600)) + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.json"), []byte(`{"server_url":"http://json"}`), 0o600)) + + l, err := Load(dir, false, nil) + require.NoError(t, err) + assert.True(t, strings.HasSuffix(l.File, "config.json")) + assert.Equal(t, "http://json", l.Config.ServerURL) + + l, err = Load(t.TempDir(), false, nil) + require.NoError(t, err) + assert.Empty(t, l.File) + assert.Equal(t, []string{"no config file found, using defaults"}, l.Warnings) +} + +// TestConfigSourceDottedOverlap reloads the file each time: map iteration +// order differs per load, so a single load could hide a nondeterministic result. +func TestConfigSourceDottedOverlap(t *testing.T) { + tests := []struct{ name, file, a, b string }{ + {"scalar", "log:\n level: info\nlog.level: debug\n", `"log.level"`, `"log > level"`}, + {"map shadow", "oidc:\n extra_params:\n a: nested\noidc.extra_params:\n b: dotted\n", `"oidc.extra_params"`, `"oidc > extra_params"`}, + {"split fallback", "dns:\n nameservers:\n split:\n a.example.com: [10.0.0.1]\ndns.nameservers:\n global: [1.1.1.1]\n", `"dns.nameservers"`, `"dns > nameservers"`}, + {"parent map and dotted child", "oidc.extra_params:\n prompt: login\noidc.extra_params.prompt: consent\n", `"oidc.extra_params"`, `"oidc.extra_params.prompt"`}, + {"open map entry twice", "dns:\n nameservers:\n split.corp.example.com: [10.0.0.1]\ndns.nameservers.split.corp.example.com: [10.0.0.2]\n", `"dns > nameservers > split.corp.example.com"`, `"dns.nameservers.split.corp.example.com"`}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(tt.file), 0o600)) + + for range 50 { + _, err := newConfigSource(dir, false, nil) + require.ErrorContains(t, err, "is set both as") + require.ErrorContains(t, err, tt.a) + require.ErrorContains(t, err, tt.b) + require.ErrorContains(t, err, "use one form") + } + }) + } +} + +func TestConfigSourceDottedKeyAlone(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), + []byte("log.level: debug\ndns:\n magic_dns: false\ndns.nameservers.global: [1.1.1.1]\n"), 0o600)) + + for range 50 { + s, err := newConfigSource(dir, false, nil) + require.NoError(t, err) + + cfg, err := s.decode() + require.NoError(t, err) + require.Equal(t, types.LogLevel("debug"), cfg.Log.Level) + require.False(t, cfg.DNSConfig.MagicDNS) + require.Equal(t, []string{"1.1.1.1"}, cfg.DNSConfig.Nameservers.Global) + } +} + +// TestConfigSourceDottedNoOverlap loads files whose dotted keys only share a +// string prefix: below an open map the entry name is one key, dots included. +func TestConfigSourceDottedNoOverlap(t *testing.T) { + tests := []struct { + name, file string + check func(t *testing.T, cfg *types.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, cfg *types.Config) { + t.Helper() + assert.Equal(t, + map[string][]string{"corp.example.com": {"10.0.0.1"}, "corp.example.com.au": {"10.0.0.2"}}, + 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, cfg *types.Config) { + t.Helper() + 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, cfg *types.Config) { + t.Helper() + assert.Equal(t, + map[string][]string{"corp.example.com": {"10.0.0.1"}, "corp.example.com.au": {"10.0.0.2"}}, + 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, cfg *types.Config) { + t.Helper() + assert.Equal(t, types.LogLevel("debug"), cfg.Log.Level) + assert.Equal(t, types.JSONLogFormat, cfg.Log.Format) + assert.True(t, cfg.LogTail.Enabled) + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(tt.file), 0o600)) + + for range 50 { + s, err := newConfigSource(dir, false, nil) + require.NoError(t, err) + + cfg, err := s.decode() + require.NoError(t, err) + tt.check(t, cfg) + } + }) + } +} + +// TestConfigSourceShapeErrorsHideValues: a block in the wrong shape may hold +// credentials, so the error names the key without its value. +func TestConfigSourceShapeErrorsHideValues(t *testing.T) { + for name, file := range map[string]string{ + "list under oidc": "oidc:\n - client_secret: marker-secret\n", + "scalar under postgres": "database:\n postgres: \"pass: marker-secret\"\n", + "scalar under oidc.pkce": "oidc:\n pkce: marker-secret\n", + "list under database.sqlite": "database:\n sqlite: [marker-secret]\n", + } { + t.Run(name, func(t *testing.T) { + _, err := newConfigSource(writeConfig(t, file), true, nil) + require.ErrorContains(t, err, "must be a mapping") + assert.NotContains(t, err.Error(), "marker-secret") + }) + } +} + +func TestConfigSourceStrictBeforeExpansion(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte("log:\n format:\n typo: json\nlog.format: json\n"), 0o600)) + + for range 50 { + _, err := newConfigSource(dir, false, nil) + require.ErrorContains(t, err, "unknown configuration key log.format.typo") + } +} + +func TestConfigSourceProviderEnv(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), + []byte("dns:\n challenge:\n provider:\n name: file\n other: x\n"), 0o600)) + + t.Setenv("HEADSCALE_DNS_CHALLENGE_PROVIDER", `{"api_token":"x"}`) + t.Setenv("HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME", "cloudflare") + + l, err := Load(dir, false, os.Environ()) + require.NoError(t, err) + assert.Empty(t, l.Warnings) + + cfg := l.Config + 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 := cfg.DNSConfig.Challenge.ProviderSettings + assert.Equal(t, "x", got["api_token"]) + assert.NotContains(t, got, "other") +} + +// TestProviderEnvFold pins that HEADSCALE_DNS_CHALLENGE_PROVIDER_ +// reaches the settings of the provider named anywhere, and that a variable +// no provider setting has is ignored without a word. +func TestProviderEnvFold(t *testing.T) { + 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 +` + + tests := []struct { + name string + yaml string + env map[string]string + want map[string]any + }{ + { + name: "named in the file", + yaml: base + " challenge:\n provider:\n name: cloudflare\n", + env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "x"}, + want: map[string]any{"name": "cloudflare", "api_token": "x"}, + }, + { + name: "named by its variable", + yaml: base, + env: map[string]string{ + "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", + "HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "x", + }, + want: map[string]any{"name": "cloudflare", "api_token": "x"}, + }, + { + name: "named in the json variable", + yaml: base, + env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"name":"cloudflare","api_token":"json"}`}, + want: map[string]any{"name": "cloudflare", "api_token": "json"}, + }, + { + name: "setting variable beats the json variable", + yaml: base, + env: map[string]string{ + "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"name":"cloudflare","api_token":"json"}`, + "HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "env", + }, + want: map[string]any{"name": "cloudflare", "api_token": "env"}, + }, + { + name: "name variable beats the json variable", + yaml: base + " challenge:\n provider:\n name: powerdns\n", + env: map[string]string{ + "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"name":"rfc2136","api_token":"json"}`, + "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", + }, + want: map[string]any{"name": "cloudflare", "api_token": "json"}, + }, + { + name: "variable for no setting of the provider", + yaml: base + " challenge:\n provider:\n name: cloudflare\n api_token: tok\n", + env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_BOGUS": "1"}, + want: map[string]any{"name": "cloudflare", "api_token": "tok"}, + }, + { + name: "setting variable without a provider", + yaml: base, + env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "x"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for k, v := range tt.env { + t.Setenv(k, v) + } + + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(tt.yaml), 0o600)) + l, err := Load(dir, false, os.Environ()) + require.NoError(t, err) + assert.Empty(t, l.Warnings) + + cfg := l.Config + require.NoError(t, Validate(cfg)) + + if len(tt.want) == 0 { + assert.Empty(t, cfg.DNSConfig.Challenge.ProviderSettings) + + return + } + + assert.Equal(t, tt.want, cfg.DNSConfig.Challenge.ProviderSettings) + }) + } +} + +// decodeInput writes content as name (none when empty) and decodes it with +// env, as Load does. +func decodeInput(t *testing.T, name, content string, env ...string) (*types.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 *types.Config) + wantErr []string + }{ + {name: "yaml expiry 0", file: "node:\n expiry: 0\n", check: func(t *testing.T, c *types.Config) { t.Helper(); assert.Zero(t, c.Node.Expiry) }}, + {name: "json expiry 0", file: `{"node": {"expiry": 0}}`, check: func(t *testing.T, c *types.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 *types.Config) { t.Helper(); assert.Zero(t, c.Node.Expiry) }}, + {name: "prometheus duration", file: "node:\n expiry: 90d\n", check: func(t *testing.T, c *types.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 *types.Config) { + t.Helper() + assert.Equal(t, types.FileMode(0o770), c.UnixSocketPermission) + }}, + {name: "bool in a string field", file: "database:\n postgres:\n ssl: false\n", check: func(t *testing.T, c *types.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 *types.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 *types.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 *types.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 *types.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 *types.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 *types.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 *types.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 *types.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 *types.Config) { t.Helper(); assert.Equal(t, types.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 *types.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 *types.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) { + _, err := loadServer(writeConfig(t, "log:\n level: verbose\nnode:\n expiry: 90\ntaildrop:\n enabled: maybe\n"), true) + require.Error(t, err) + + got := reasons(types.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") + }) + } +} + +// requireHidden fails when an error in err's tree, its text or its JSON, +// or the JSON of its ConfigErrors holds hidden: the decoder's own errors +// keep the value they rejected. +func requireHidden(t *testing.T, err error, hidden string) { + t.Helper() + + util.WalkErrTree(err, func(e error) { + assert.NotContains(t, e.Error(), hidden) + + b, jerr := json.Marshal(e) + require.NoError(t, jerr) + assert.NotContains(t, string(b), hidden) + }) + + b, jerr := json.Marshal(types.ConfigErrors(err)) + require.NoError(t, jerr) + assert.NotContains(t, string(b), hidden) +} + +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 Default. +func TestDefaultsFitTypes(t *testing.T) { + d := Default() + + for _, f := range types.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(types.Config{}, "ConfigDir"), cmpopts.IgnoreUnexported(types.Config{})); diff != "" { + t.Errorf("defaults layer (-Default +decoded):\n%s", diff) + } +} + +func TestPlainMap(t *testing.T) { + v4 := netip.MustParsePrefix("100.64.0.0/10") + cfg := &types.Config{ + ServerURL: "https://h", + UnixSocketPermission: 0, + TrustedProxies: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/8")}, + Prefixes: types.PrefixesConfig{V4: &v4}, + Node: types.NodeConfig{Expiry: 90 * time.Minute}, + DERP: types.DERPConfig{URLs: []types.HTTPURL{"https://user:pass@derp.example/map"}}, + Database: types.DatabaseConfig{Postgres: types.PostgresConfig{Pass: "marker-pass"}}, + OIDC: types.OIDCConfig{ClientSecret: "file:/run/secret"}, + DNSConfig: types.DNSConfig{ + ExtraRecords: []tailcfg.DNSRecord{{Name: "a.example.com", Type: "A", Value: "100.64.0.1"}}, + Challenge: types.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, types.HTTPURL("https://user:pass@derp.example/map"), cfg.DERP.URLs[0]) + assert.Equal(t, types.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) + _, err := loadServer(staged, false) + 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") + } +} + +func TestReadConfig(t *testing.T) { + tests := []struct { + name string + configPath string + setup func(*testing.T, *types.Config) (any, error) + want any + wantErr string + }{ + { + name: "unmarshal-dns-full-config", + configPath: "testdata/dns_full.yaml", + setup: func(t *testing.T, cfg *types.Config) (any, error) { //nolint:thelper + return cfg.DNSConfig, nil + }, + want: types.DNSConfig{ + MagicDNS: true, + BaseDomain: "example.com", + OverrideLocalDNS: false, + Nameservers: types.Nameservers{ + Global: []string{ + "1.1.1.1", + "1.0.0.1", + "2606:4700:4700::1111", + "2606:4700:4700::1001", + "https://dns.nextdns.io/abc123", + }, + Split: map[string][]string{ + "darp.headscale.net": {"1.1.1.1", "8.8.8.8"}, + "foo.bar.com": {"1.1.1.1"}, + }, + }, + ExtraRecords: []tailcfg.DNSRecord{ + {Name: "grafana.myvpn.example.com", Type: "A", Value: "100.64.0.3"}, + {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, + }, + SearchDomains: []string{"test.com", "bar.com"}, + Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, + }, + }, + { + name: "dns-to-tailcfg.DNSConfig", + configPath: "testdata/dns_full.yaml", + setup: func(t *testing.T, cfg *types.Config) (any, error) { //nolint:thelper + return cfg.TailcfgDNSConfig(), nil + }, + want: &tailcfg.DNSConfig{ + Proxied: true, + Domains: []string{"example.com", "test.com", "bar.com"}, + FallbackResolvers: []*dnstype.Resolver{ + {Addr: "1.1.1.1"}, + {Addr: "1.0.0.1"}, + {Addr: "2606:4700:4700::1111"}, + {Addr: "2606:4700:4700::1001"}, + {Addr: "https://dns.nextdns.io/abc123"}, + }, + Routes: map[string][]*dnstype.Resolver{ + "darp.headscale.net": {{Addr: "1.1.1.1"}, {Addr: "8.8.8.8"}}, + "foo.bar.com": {{Addr: "1.1.1.1"}}, + }, + ExtraRecords: []tailcfg.DNSRecord{ + {Name: "grafana.myvpn.example.com", Type: "A", Value: "100.64.0.3"}, + {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, + }, + }, + }, + { + name: "unmarshal-dns-full-no-magic", + configPath: "testdata/dns_full_no_magic.yaml", + setup: func(t *testing.T, cfg *types.Config) (any, error) { //nolint:thelper + return cfg.DNSConfig, nil + }, + want: types.DNSConfig{ + MagicDNS: false, + BaseDomain: "example.com", + OverrideLocalDNS: false, + Nameservers: types.Nameservers{ + Global: []string{ + "1.1.1.1", + "1.0.0.1", + "2606:4700:4700::1111", + "2606:4700:4700::1001", + "https://dns.nextdns.io/abc123", + }, + Split: map[string][]string{ + "darp.headscale.net": {"1.1.1.1", "8.8.8.8"}, + "foo.bar.com": {"1.1.1.1"}, + }, + }, + ExtraRecords: []tailcfg.DNSRecord{ + {Name: "grafana.myvpn.example.com", Type: "A", Value: "100.64.0.3"}, + {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, + }, + SearchDomains: []string{"test.com", "bar.com"}, + Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, + }, + }, + { + name: "dns-to-tailcfg.DNSConfig", + configPath: "testdata/dns_full_no_magic.yaml", + setup: func(t *testing.T, cfg *types.Config) (any, error) { //nolint:thelper + return cfg.TailcfgDNSConfig(), nil + }, + want: &tailcfg.DNSConfig{ + Proxied: false, + Domains: []string{"test.com", "bar.com"}, + FallbackResolvers: []*dnstype.Resolver{ + {Addr: "1.1.1.1"}, + {Addr: "1.0.0.1"}, + {Addr: "2606:4700:4700::1111"}, + {Addr: "2606:4700:4700::1001"}, + {Addr: "https://dns.nextdns.io/abc123"}, + }, + Routes: map[string][]*dnstype.Resolver{ + "darp.headscale.net": {{Addr: "1.1.1.1"}, {Addr: "8.8.8.8"}}, + "foo.bar.com": {{Addr: "1.1.1.1"}}, + }, + ExtraRecords: []tailcfg.DNSRecord{ + {Name: "grafana.myvpn.example.com", Type: "A", Value: "100.64.0.3"}, + {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, + }, + }, + }, + { + name: "base-domain-in-server-url-err", + configPath: "testdata/base-domain-in-server-url.yaml", + setup: func(t *testing.T, cfg *types.Config) (any, error) { //nolint:thelper + return nil, Validate(cfg) + }, + want: nil, + wantErr: "server_url is a subdomain of dns.base_domain", + }, + { + name: "base-domain-not-in-server-url", + configPath: "testdata/base-domain-not-in-server-url.yaml", + setup: func(t *testing.T, cfg *types.Config) (any, error) { //nolint:thelper + err := Validate(cfg) + if err != nil { + return nil, err + } + + return map[string]string{ + "server_url": cfg.ServerURL, + "base_domain": cfg.DNSConfig.BaseDomain, + }, err + }, + want: map[string]string{ + "server_url": "https://derp.no", + "base_domain": "clients.derp.no", + }, + wantErr: "", + }, + { + name: "dns-override-true-errors", + configPath: "testdata/dns-override-true-error.yaml", + setup: func(t *testing.T, cfg *types.Config) (any, error) { //nolint:thelper + return nil, Validate(cfg) + }, + wantErr: "Fatal config error: dns.nameservers.global is required when dns.override_local_dns is true", + }, + { + name: "dns-override-true", + configPath: "testdata/dns-override-true.yaml", + setup: func(t *testing.T, cfg *types.Config) (any, error) { //nolint:thelper + err := Validate(cfg) + if err != nil { + return nil, err + } + + return cfg.TailcfgDNSConfig(), nil + }, + want: &tailcfg.DNSConfig{ + Proxied: true, + Domains: []string{"derp2.no"}, + Routes: map[string][]*dnstype.Resolver{}, + Resolvers: []*dnstype.Resolver{ + {Addr: "1.1.1.1"}, + {Addr: "1.0.0.1"}, + }, + }, + }, + { + name: "policy-path-is-loaded", + configPath: "testdata/policy-path-is-loaded-strict.yaml", + setup: func(t *testing.T, cfg *types.Config) (any, error) { //nolint:thelper // inline test closure + err := Validate(cfg) + if err != nil { + return nil, err + } + + return map[string]string{ + "policy.mode": string(cfg.Policy.Mode), + "policy.path": cfg.Policy.Path, + }, err + }, + want: map[string]string{ + "policy.mode": "file", + "policy.path": "/etc/policy.hujson", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + l, err := Load(tt.configPath, true, os.Environ()) + require.NoError(t, err) + + conf, err := tt.setup(t, l.Config) + + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + + return + } + + require.NoError(t, err) + + if diff := gocmp.Diff(tt.want, conf); diff != "" { + t.Errorf("ReadConfig() mismatch (-want +got):\n%s", diff) + } + }) + } +} + +func TestReadConfigFromEnv(t *testing.T) { + tests := []struct { + name string + configEnv map[string]string + setup func(*testing.T, *types.Config) (any, error) + want any + }{ + { + name: "test-random-base-settings-with-env", + configEnv: map[string]string{ + "HEADSCALE_LOG_LEVEL": "trace", + "HEADSCALE_DATABASE_SQLITE_WRITE_AHEAD_LOG": "false", + "HEADSCALE_PREFIXES_V4": "100.64.0.0/10", + }, + setup: func(t *testing.T, cfg *types.Config) (any, error) { //nolint:thelper // inline test closure + assert.Equal(t, types.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 + }, + want: nil, + }, + { + name: "unmarshal-dns-full-config", + configEnv: map[string]string{ + "HEADSCALE_DNS_MAGIC_DNS": "true", + "HEADSCALE_DNS_BASE_DOMAIN": "example.com", + "HEADSCALE_DNS_OVERRIDE_LOCAL_DNS": "false", + "HEADSCALE_DNS_NAMESERVERS_GLOBAL": `1.1.1.1 8.8.8.8`, + "HEADSCALE_DNS_SEARCH_DOMAINS": "test.com bar.com", + + // TODO(kradalby): Figure out how to pass these as env vars + // "HEADSCALE_DNS_NAMESERVERS_SPLIT": `{foo.bar.com: ["1.1.1.1"]}`, + // "HEADSCALE_DNS_EXTRA_RECORDS": `[{ name: "prometheus.myvpn.example.com", type: "A", value: "100.64.0.4" }]`, + }, + setup: func(t *testing.T, cfg *types.Config) (any, error) { //nolint:thelper // inline test closure + return cfg.DNSConfig, nil + }, + want: types.DNSConfig{ + MagicDNS: true, + BaseDomain: "example.com", + OverrideLocalDNS: false, + Nameservers: types.Nameservers{ + Global: []string{"1.1.1.1", "8.8.8.8"}, + Split: map[string][]string{ + // "foo.bar.com": {"1.1.1.1"}, + }, + }, + // ExtraRecords: []tailcfg.DNSRecord{ + // {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, + // }, + SearchDomains: []string{"test.com", "bar.com"}, + Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for k, v := range tt.configEnv { + t.Setenv(k, v) + } + + l, err := Load("testdata/minimal.yaml", true, os.Environ()) + require.NoError(t, err) + + conf, err := tt.setup(t, l.Config) + require.NoError(t, err) + + if diff := gocmp.Diff(tt.want, conf, cmpopts.EquateEmpty()); diff != "" { + t.Errorf("ReadConfig() mismatch (-want +got):\n%s", diff) + } + }) + } +} + +func TestTLSConfigValidation(t *testing.T) { + tmpDir := t.TempDir() + + var err error + + configYaml := []byte(`--- +tls: + cert_path: abc.pem + acme: + hostname: example.com + challenge_type: "" +noise: + private_key_path: noise_private.key`) + + // Populate a custom config file + configFilePath := filepath.Join(tmpDir, "config.yaml") + + err = os.WriteFile(configFilePath, configYaml, 0o600) + if err != nil { + t.Fatalf("Couldn't write file %s", configFilePath) + } + + // Check configuration validation errors (1) + _, err = loadServer(tmpDir, false) + require.Error(t, err) + assert.Contains( + t, + err.Error(), + "Fatal config error: tls.acme.hostname and tls.cert_path/tls.key_path are mutually exclusive", + ) + assert.Contains( + t, + err.Error(), + "Fatal config error: tls.acme.challenge_type has an unsupported value", + ) + assert.Contains( + t, + err.Error(), + "Fatal config error: server_url is missing a scheme", + ) + + // Check configuration validation errors (2) + configYaml = []byte(`--- +noise: + private_key_path: noise_private.key +server_url: http://127.0.0.1:8080 +tls: + acme: + hostname: example.com + challenge_type: TLS-ALPN-01 +`) + + err = os.WriteFile(configFilePath, configYaml, 0o600) + if err != nil { + t.Fatalf("Couldn't write file %s", configFilePath) + } + + _, err = Load(tmpDir, false, os.Environ()) + require.NoError(t, err) +} + +func TestPKCEMethodValidation(t *testing.T) { + tmpDir := t.TempDir() + + // OIDC is active (issuer set) with PKCE enabled and an invalid method. + // There is no oidc.enabled key. Loading must reject the invalid method + // rather than silently skipping the check. + configYaml := []byte(`--- +noise: + private_key_path: noise_private.key +server_url: http://127.0.0.1:8080 +dns: + override_local_dns: false +oidc: + issuer: https://idp.example.com + client_id: headscale + pkce: + enabled: true + method: S256-typo +`) + + configFilePath := filepath.Join(tmpDir, "config.yaml") + err := os.WriteFile(configFilePath, configYaml, 0o600) + require.NoError(t, err) + + _, err = loadServer(tmpDir, false) + require.Error(t, err) + assert.Contains(t, err.Error(), "oidc.pkce.method has an unsupported value") +} + +// TestOIDCConfigValidation covers the issuer-URL and required-field checks that +// fail an unworkable OIDC setup fast at config load. +func TestOIDCConfigValidation(t *testing.T) { + tests := []struct { + name string + oidcBlock string + wantErr string + }{ + { + name: "non-http issuer", + oidcBlock: ` + issuer: ftp://idp.example.com + client_id: headscale + client_secret: sekret`, + wantErr: "oidc.issuer is not an http:// or https:// URL", + }, + { + name: "missing client_id", + oidcBlock: ` + issuer: https://idp.example.com + client_secret: sekret`, + wantErr: "client_id is required", + }, + { + name: "missing client_secret", + oidcBlock: ` + issuer: https://idp.example.com + client_id: headscale`, + wantErr: "client_secret", + }, + { + name: "valid", + oidcBlock: ` + issuer: https://idp.example.com + client_id: headscale + client_secret: sekret`, + wantErr: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tmpDir := t.TempDir() + configYaml := []byte(`--- +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 +database: + type: sqlite +oidc:` + tt.oidcBlock + "\n") + + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "config.yaml"), configYaml, 0o600)) + + _, err := loadServer(tmpDir, false) + if tt.wantErr == "" { + require.NoError(t, err) + + return + } + + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + }) + } +} + +func TestTLSACMEListenAddrCollision(t *testing.T) { + tests := []struct { + name string + listenAddr string + acmeListen string + hostname string + challengeType string + wantErr string // empty = expect no error + }{ + {"collision-numeric", ":80", ":80", "example.com", "HTTP-01", "would bind the same TCP socket"}, + {"collision-named-vs-numeric", "0.0.0.0:80", ":http", "example.com", "HTTP-01", "would bind the same TCP socket"}, + {"collision-https-named", ":443", ":https", "example.com", "HTTP-01", "would bind the same TCP socket"}, + {"canonical", "0.0.0.0:443", ":http", "example.com", "HTTP-01", ""}, + {"no-hostname-skipped", "0.0.0.0:80", ":http", "", "HTTP-01", ""}, + {"tls-alpn-01-skipped", "0.0.0.0:80", ":http", "example.com", "TLS-ALPN-01", ""}, + {"different-numeric", ":8080", ":8081", "example.com", "HTTP-01", ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tmpDir := t.TempDir() + cfg := fmt.Sprintf(`--- +server_url: https://example.com +listen_addr: %q +tls: + acme: + hostname: %q + challenge_type: %q + http_listen: %q +noise: + private_key_path: noise_private.key +prefixes: + v4: 100.64.0.0/10 +database: + type: sqlite3 +dns: + magic_dns: false + override_local_dns: false +`, tt.listenAddr, tt.hostname, tt.challengeType, tt.acmeListen) + require.NoError(t, os.WriteFile( + filepath.Join(tmpDir, "config.yaml"), []byte(cfg), 0o600)) + + _, err := loadServer(tmpDir, false) + if tt.wantErr == "" { + require.NoError(t, err) + return + } + + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + }) + } +} + +func TestLoadCollectsAcrossSubBuilders(t *testing.T) { + tmpDir := t.TempDir() + cfg := `--- +server_url: https://example.com +listen_addr: 0.0.0.0:8080 +prefixes: + v4: not-a-cidr + v6: also-not-a-cidr + allocation: bogus +noise: + private_key_path: noise_private.key +database: + type: sqlite3 +dns: + magic_dns: false + override_local_dns: false + base_domain: example.com +` + require.NoError(t, os.WriteFile( + filepath.Join(tmpDir, "config.yaml"), []byte(cfg), 0o600)) + _, err := loadServer(tmpDir, false) + require.Error(t, err) + + got := types.ConfigErrors(err) + assert.GreaterOrEqual(t, len(got), 3, + "expected sub-builder errors collected into one report; got %d: %v", + len(got), reasons(got)) + + // Sentinels stay reachable through ConfigError.Cause. + require.ErrorIs(t, err, types.ErrInvalidAllocationStrategy, + "wrapped ErrInvalidAllocationStrategy not in chain") + + // Each sub-builder failure surfaces its YAML key in the rendered output. + rendered := err.Error() + for _, want := range []string{ + "prefixes.v4", + "prefixes.v6", + "prefixes.allocation", + } { + assert.Contains(t, rendered, want, + "expected rendered error to mention %q", want) + } +} + +func reasons(errs []*types.ConfigError) []string { + out := make([]string, len(errs)) + for i, e := range errs { + out[i] = e.Reason + } + + return out +} + +// TestRemovedKeysAreRejected pins that removed keys stop the load in the file +// and warn in the environment, naming the replacement either way. +func TestRemovedKeysAreRejected(t *testing.T) { + tests := []struct { + name string + file string + env map[string]string + want []string // substrings of the load error + warns []string // substrings of the warnings + serverErr string // from Validate after a clean load + }{ + { + name: "deprecated keys", + file: "oidc:\n expiry: 1h\ndns_config:\n use_username_in_magic_dns: true\n", + want: []string{"oidc.expiry was removed", "use node.expiry", "dns_config was removed"}, + }, + { + name: "ignored gorm key", + file: "database:\n gorm:\n prepare_stmt: true\n", + want: []string{"database.gorm.prepare_stmt was removed"}, + }, + { + name: "ephemeral timeout", + file: "ephemeral_node_inactivity_timeout: 30m\n", + want: []string{"ephemeral_node_inactivity_timeout was removed", "use node.ephemeral.inactivity_timeout"}, + }, + { + name: "ephemeral timeout env", + env: map[string]string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT": "30m"}, + warns: []string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT was removed", "use HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT"}, + }, + { + name: "oidc client secret path", + file: "oidc:\n client_secret_path: /run/secret\n", + want: []string{"oidc.client_secret_path was removed", `oidc.client_secret: "file:"`}, + }, + { + name: "oidc client secret path env", + env: map[string]string{"HEADSCALE_OIDC_CLIENT_SECRET_PATH": "/run/secret"}, + warns: []string{"HEADSCALE_OIDC_CLIENT_SECRET_PATH was removed", "use HEADSCALE_OIDC_CLIENT_SECRET=file:"}, + }, + { + // The warning alone would let OIDC start without its secret; the + // consumer's own check stops it. + name: "removed secret variable leaves the secret unset", + file: "server_url: https://headscale.example.com\nnoise:\n private_key_path: noise_private.key\nprefixes:\n v4: 100.64.0.0/10\ndns:\n magic_dns: false\noidc:\n issuer: https://idp.example.com\n client_id: headscale\n", + env: map[string]string{"HEADSCALE_OIDC_CLIENT_SECRET_PATH": "/run/secret"}, + warns: []string{"HEADSCALE_OIDC_CLIENT_SECRET_PATH was removed in 0.30; use HEADSCALE_OIDC_CLIENT_SECRET=file:"}, + serverErr: "oidc.client_secret is required when oidc.issuer is set", + }, + { + name: "postgres password file env", + env: map[string]string{"HEADSCALE_DATABASE_POSTGRES_PASSWORD_FILE": "/run/secret"}, + warns: []string{"HEADSCALE_DATABASE_POSTGRES_PASSWORD_FILE was removed", "use HEADSCALE_DATABASE_POSTGRES_PASS=file:"}, + }, + { + name: "env hints name variables", + env: map[string]string{ + "HEADSCALE_IP_PREFIXES": "100.64.0.0/10", + "HEADSCALE_JSON_LOGS": "true", + "HEADSCALE_METRICS_ENABLED": "true", + }, + warns: []string{ + "use HEADSCALE_PREFIXES_V4 and HEADSCALE_PREFIXES_V6", + "use HEADSCALE_LOG_FORMAT=json", + "remove HEADSCALE_METRICS_ENABLED", + }, + }, + { + name: "file hints name keys", + file: "ip_prefixes: [100.64.0.0/10]\njson_logs: true\nmetrics_enabled: true\n", + want: []string{"use prefixes.v4 and prefixes.v6", `use log.format: "json"`, "remove metrics_enabled"}, + }, + { + name: "provider path key", + file: "dns:\n challenge:\n provider:\n name: cloudflare\n api_token_path: /run/secret\n", + want: []string{"dns.challenge.provider.api_token_path was removed", `dns.challenge.provider.api_token: "file:"`}, + }, + { + name: "provider path key without provider", + file: "dns:\n challenge:\n provider:\n api_token_path: /run/secret\n", + want: []string{"dns.challenge.provider.api_token_path was removed", `dns.challenge.provider.api_token: "file:"`}, + }, + { + name: "provider path env without provider", + env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN_PATH": "/run/secret"}, + warns: []string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN_PATH was removed", "use HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN=file:"}, + }, + { + name: "provider path env for another provider", + env: map[string]string{ + "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "rfc2136", + "HEADSCALE_DNS_CHALLENGE_PROVIDER_KEY_PATH": "/run/secret", + }, + warns: []string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_KEY_PATH was removed", "use HEADSCALE_DNS_CHALLENGE_PROVIDER_KEY=file:"}, + }, + { + name: "provider path in env json", + env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"api_token_path":"/run/secret"}`}, + warns: []string{"dns.challenge.provider.api_token_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed", `dns.challenge.provider.api_token: "file:"`}, + }, + { + name: "provider path in env json with provider", + env: map[string]string{ + "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"API_Token_Path":"/run/secret"}`, + "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", + }, + warns: []string{"dns.challenge.provider.api_token_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed", `dns.challenge.provider.api_token: "file:"`}, + }, + { + name: "registration path key", + file: "dns:\n challenge:\n provider:\n config:\n laptop.example.com:\n password_path: /run/secret\n", + want: []string{ + "dns.challenge.provider.config.laptop.example.com.password_path was removed", + `dns.challenge.provider.config.laptop.example.com.password: "file:"`, + }, + }, + { + // A registration's name is data, not a setting. + name: "registration named like a path key", + file: "dns:\n challenge:\n provider:\n config:\n node.example_path:\n username: user\n", + }, + { + name: "registration named like a path key in env json", + env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"config":{"Node.Example_path":{"username":"user"}}}`}, + }, + { + name: "path key in a registration named like one", + env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"config":{"Node.Example_path":{"Password_Path":"/run/secret"}}}`}, + warns: []string{ + "dns.challenge.provider.config.Node.Example_path.password_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed", + `dns.challenge.provider.config.Node.Example_path.password: "file:"`, + }, + }, + { + name: "registration path in env json", + env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"config":{"laptop.example.com":{"password_path":"/run/secret"}}}`}, + warns: []string{ + "dns.challenge.provider.config.laptop.example.com.password_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed", + `dns.challenge.provider.config.laptop.example.com.password: "file:"`, + }, + }, + // Every spelling of the registration map gives its keys the role the + // nested one does: registration names, then settings. + { + name: "registration named like a path key, nested", + file: "dns: {challenge: {provider: {config: {node.example_path: {username: user, password: pass, subdomain: reg, server_url: https://auth.example.com}}}}}", + }, + { + name: "path key in a registration named config, nested", + file: "dns: {challenge: {provider: {config: {config: {Password_Path: /run/secret}}}}}", + want: []string{ + "dns.challenge.provider.config.config.password_path was removed", + `dns.challenge.provider.config.config.password: "file:"`, + }, + }, + { + name: "registration named like a path key, dotted provider", + file: "dns.challenge.provider: {config: {node.example_path: {username: user, password: pass, subdomain: reg, server_url: https://auth.example.com}}}", + }, + { + name: "path key in a registration named config, dotted provider", + file: "dns.challenge.provider: {config: {config: {Password_Path: /run/secret}}}", + want: []string{ + "dns.challenge.provider.config.config.password_path was removed", + `dns.challenge.provider.config.config.password: "file:"`, + }, + }, + { + name: "registration named like a path key, dotted registration map", + file: "dns.challenge.provider.config: {node.example_path: {username: user, password: pass, subdomain: reg, server_url: https://auth.example.com}}", + }, + { + name: "path key in a registration named config, dotted registration map", + file: "dns.challenge.provider.config: {config: {Password_Path: /run/secret}}", + want: []string{ + "dns.challenge.provider.config.config.password_path was removed", + `dns.challenge.provider.config.config.password: "file:"`, + }, + }, + { + name: "registration named like a path key, dotted in dns", + file: "dns: {challenge.provider.config: {node.example_path: {username: user, password: pass, subdomain: reg, server_url: https://auth.example.com}}}", + }, + { + name: "path key in a registration named config, dotted in dns", + file: "dns: {challenge.provider.config: {config: {Password_Path: /run/secret}}}", + want: []string{ + "dns.challenge.provider.config.config.password_path was removed", + `dns.challenge.provider.config.config.password: "file:"`, + }, + }, + { + name: "registration named like a path key, dotted in dns.challenge", + file: "dns.challenge: {provider.config: {node.example_path: {username: user, password: pass, subdomain: reg, server_url: https://auth.example.com}}}", + }, + { + name: "path key in a registration named config, dotted in dns.challenge", + file: "dns.challenge: {provider.config: {config: {Password_Path: /run/secret}}}", + want: []string{ + "dns.challenge.provider.config.config.password_path was removed", + `dns.challenge.provider.config.config.password: "file:"`, + }, + }, + { + name: "postgres password file", + file: "database:\n postgres:\n password_file: /run/secret\n", + want: []string{"database.postgres.password_file was removed", `database.postgres.pass: "file:"`}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for k, v := range tt.env { + t.Setenv(k, v) + } + + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(tt.file), 0o600)) + + l, err := Load(dir, false, os.Environ()) + if len(tt.want) == 0 { + require.NoError(t, err) + } else { + require.Error(t, err) + // A removed key's value may be a secret. + require.NotContains(t, err.Error(), "/run/secret") + } + + for _, want := range tt.want { + require.ErrorContains(t, err, want) + } + + if err != nil { + return + } + + warnings := strings.Join(l.Warnings, "\n") + assert.NotContains(t, warnings, "/run/secret") + + if len(tt.warns) == 0 { + assert.NotContains(t, warnings, "was removed") + } + + for _, w := range tt.warns { + assert.Contains(t, warnings, w) + } + + if tt.serverErr != "" { + require.ErrorContains(t, Validate(l.Config), tt.serverErr) + } + }) + } +} + +// TestConfigSecrets pins that Load keeps each secret as written, a +// value or file:, and reads no file: its consumer does +// (TestNewHeadscaleSecrets, TestCLIAPIKey). +func TestConfigSecrets(t *testing.T) { + missing := "file:" + filepath.Join(t.TempDir(), "missing") + + base := `--- +server_url: https://headscale.example.com +noise: + private_key_path: noise_private.key +prefixes: + v4: 100.64.0.0/10 +database: + type: postgres +dns: + magic_dns: false + override_local_dns: false +cli: + address: headscale.example.com:443 +oidc: + issuer: https://idp.example.com + client_id: headscale +` + + for name, value := range map[string]string{"literal": "literal", "missing file": missing} { + t.Run(name, func(t *testing.T) { + t.Setenv("HEADSCALE_DATABASE_POSTGRES_PASS", value) + t.Setenv("HEADSCALE_CLI_API_KEY", value) + + tmpDir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "config.yaml"), []byte(base+" client_secret: "+value+"\n"), 0o600)) + cfg, err := loadServer(tmpDir, false) + require.NoError(t, err) + assert.Equal(t, types.Secret(value), cfg.OIDC.ClientSecret) + assert.Equal(t, types.Secret(value), cfg.Database.Postgres.Pass) + assert.Equal(t, types.Secret(value), cfg.CLI.APIKey) + }) + } +} + +// The unset case is the regression: the default is spelled "0o770", which a +// base-8 parse rejects, so the socket silently became 0700. +// https://github.com/juanfont/headscale/issues/3529 +func TestUnixSocketPermission(t *testing.T) { + tests := []struct { + name string + yaml string + env string + want types.FileMode + wantErr bool + }{ + {name: "unset-uses-default", want: 0o770}, + {name: "null-uses-default", yaml: `unix_socket_permission: null`, want: 0o770}, + {name: "quoted-leading-zero", yaml: `unix_socket_permission: "0770"`, want: 0o770}, + {name: "quoted-plain", yaml: `unix_socket_permission: "770"`, want: 0o770}, + {name: "quoted-0o-prefix", yaml: `unix_socket_permission: "0o770"`, want: 0o770}, + {name: "quoted-0o-prefix-owner-only", yaml: `unix_socket_permission: "0o700"`, want: 0o700}, + {name: "quoted-zero", yaml: `unix_socket_permission: "0000"`, want: 0}, + {name: "env", env: "0660", want: 0o660}, + {name: "env-0o-prefix", env: "0o660", want: 0o660}, + {name: "env-overrides-file", yaml: `unix_socket_permission: "0770"`, env: "0700", want: 0o700}, + {name: "env-invalid-is-error", env: "bogus", wantErr: true}, + {name: "empty-is-error", yaml: `unix_socket_permission: ""`, wantErr: true}, + {name: "symbolic-is-error", yaml: `unix_socket_permission: "rwxrwx---"`, wantErr: true}, + {name: "special-bits-are-error", yaml: `unix_socket_permission: "1770"`, wantErr: true}, + {name: "out-of-range-is-error", yaml: `unix_socket_permission: "17777"`, wantErr: true}, + // YAML reads unquoted 0770 as int 504, indistinguishable from a literal 504. + {name: "unquoted-leading-zero-is-error", yaml: `unix_socket_permission: 0770`, wantErr: true}, + {name: "unquoted-0o-prefix-is-error", yaml: `unix_socket_permission: 0o770`, wantErr: true}, + {name: "unquoted-plain-is-error", yaml: `unix_socket_permission: 770`, wantErr: true}, + {name: "float-is-error", yaml: `unix_socket_permission: 770.0`, wantErr: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.env != "" { + t.Setenv("HEADSCALE_UNIX_SOCKET_PERMISSION", tt.env) + } + + tmpDir := t.TempDir() + cfg := `--- +server_url: https://example.com +listen_addr: 0.0.0.0:8080 +prefixes: + v4: 100.64.0.0/10 +noise: + private_key_path: noise_private.key +database: + type: sqlite3 +dns: + magic_dns: false + override_local_dns: false +` + tt.yaml + "\n" + require.NoError(t, os.WriteFile( + filepath.Join(tmpDir, "config.yaml"), []byte(cfg), 0o600)) + + got, err := loadServer(tmpDir, false) + if tt.wantErr { + require.ErrorIs(t, err, util.ErrInvalidFileMode, + "invalid unix_socket_permission must not silently fall back") + assert.Contains(t, err.Error(), "unix_socket_permission") + + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, got.UnixSocketPermission, + "got %#o, want %#o", got.UnixSocketPermission, tt.want) + }) + } +} + +func TestServerURLTrailingSlash(t *testing.T) { + tests := []struct { + name string + input string + want string + }{ + {name: "no slash", input: "https://h", want: "https://h"}, + {name: "one slash", input: "https://h/", want: "https://h"}, + {name: "two slashes", input: "https://h//", want: "https://h"}, + {name: "port", input: "https://h:8443/", want: "https://h:8443"}, + {name: "path prefix", input: "https://h/hs/", want: "https://h/hs"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg, err := decodeValues(t, map[string]any{ + "server_url": tt.input, + "noise.private_key_path": "private_key.pem", + "prefixes.v4": "100.64.0.0/10", + "database.type": "sqlite3", + "dns.magic_dns": false, + "dns.override_local_dns": false, + }) + require.NoError(t, err) + require.NoError(t, Validate(cfg)) + assert.Equal(t, tt.want, cfg.ServerURL) + }) + } +} + +//nolint:goconst // repeated CIDR strings are test fixtures, not refactor candidates +func TestTrustedProxies(t *testing.T) { + tests := []struct { + name string + input any + want []netip.Prefix + wantErr string + }{ + { + name: "unset", + input: nil, + want: nil, + }, + { + name: "empty", + input: []string{}, + want: nil, + }, + { + name: "single-v4", + input: []string{"10.0.0.0/16"}, + want: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/16")}, + }, + { + name: "single-v6", + input: []string{"fd00::/8"}, + want: []netip.Prefix{netip.MustParsePrefix("fd00::/8")}, + }, + { + name: "mixed-v4-v6", + input: []string{"127.0.0.1/32", "::1/128", "10.0.0.0/16"}, + want: []netip.Prefix{ + netip.MustParsePrefix("127.0.0.1/32"), + netip.MustParsePrefix("::1/128"), + netip.MustParsePrefix("10.0.0.0/16"), + }, + }, + { + name: "non-canonical-masked", + input: []string{"10.0.0.5/16"}, + want: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/16")}, + }, + { + name: "bare-ip-rejected", + input: []string{"10.0.0.1"}, + wantErr: `netip.ParsePrefix("10.0.0.1")`, + }, + { + name: "garbage-names-the-value", + input: []string{"10.0.0.0/16", "not-an-ip"}, + wantErr: `trusted_proxies is not a valid CIDR`, + }, + { + name: "ipv4-zero-rejected", + input: []string{"0.0.0.0/0"}, + wantErr: `trusted_proxies[0] "0.0.0.0/0": 0.0.0.0/0 and ::/0 are not allowed`, + }, + { + name: "ipv6-zero-rejected", + input: []string{"::/0"}, + wantErr: "0.0.0.0/0 and ::/0 are not allowed", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + values := map[string]any{ + "server_url": "https://headscale.example.com", + "noise.private_key_path": "private_key.pem", + "prefixes.v4": "100.64.0.0/10", + "database.type": "sqlite3", + "dns.magic_dns": false, + "dns.override_local_dns": false, + } + if tt.input != nil { + values["trusted_proxies"] = tt.input + } + + var got []netip.Prefix + + cfg, err := decodeValues(t, values) + if err == nil { + got, err = cfg.TrustedProxies, Validate(cfg) + } + + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + + return + } + + require.NoError(t, err) + + if diff := gocmp.Diff(tt.want, got, cmpopts.EquateComparable(netip.Prefix{}), cmpopts.EquateEmpty()); diff != "" { + t.Errorf("trusted_proxies mismatch (-want +got):\n%s", diff) + } + }) + } +} + +// TestNameserverWarnings pins that the config check reports a nameserver +// clients will not get, and that building the DNS config does not report it +// again. +func TestNameserverWarnings(t *testing.T) { + yaml := `--- +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 + base_domain: ts.example.com + override_local_dns: true + nameservers: + global: ["1.1.1.1", "1.1.1.1:53"] + split: + corp.example.com: ["[2606:4700::1111]:53"] +` + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(yaml), 0o600)) + cfg, err := loadServer(dir, false) + require.NoError(t, err) + + warnings := strings.Join(Warnings(cfg), "\n") + assert.Contains(t, warnings, `invalid nameserver "1.1.1.1:53" in dns.nameservers.global, ignoring`) + assert.Contains(t, warnings, `invalid nameserver "[2606:4700::1111]:53" in dns.nameservers.split.corp.example.com, ignoring`) + assert.NotContains(t, warnings, `"1.1.1.1" in`) + + var buf bytes.Buffer + + oldLogger, oldLevel := log.Logger, zerolog.GlobalLevel() + log.Logger = zerolog.New(&buf) + + t.Cleanup(func() { + log.Logger = oldLogger + + zerolog.SetGlobalLevel(oldLevel) + }) + + cfg.StartDNS(nil) + + assert.Equal(t, []*dnstype.Resolver{{Addr: "1.1.1.1"}}, cfg.CloneTailcfgDNSConfig().Resolvers) + assert.Empty(t, buf.String(), "built once, reported once: by Warnings") +} + +func TestDNSChallengeConfig(t *testing.T) { + secret := filepath.Join(t.TempDir(), "token") + require.NoError(t, os.WriteFile(secret, []byte("file-token\n"), 0o600)) + + 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 +` + + tests := []struct { + name string + yaml string + env map[string]string + wantErr string + fileErr bool // only reading a file: secret fails, so the load passes + wantWarns []string + wantZone string + wantToken string + wantNoProv bool + wantResolvers []string + wantIsolates string + wantProvider string // the provider's type, for one other than cloudflare + }{ + { + name: "disabled by default", + yaml: base, + // CertificateZone follows base_domain even when unused, so it is never empty. + wantZone: "ts.example.com", + wantNoProv: true, + wantResolvers: []string{"1.1.1.1:53", "8.8.8.8:53"}, + }, + { + name: "resolvers default to port 53", + yaml: base + ` challenge: + resolvers: ["9.9.9.9", "2620:fe::fe", "127.0.0.1:5353"] +`, + wantZone: "ts.example.com", + wantNoProv: true, + wantResolvers: []string{"9.9.9.9:53", "[2620:fe::fe]:53", "127.0.0.1:5353"}, + }, + { + name: "enabled with inline token", + yaml: base + ` certificates: + enabled: true + challenge: + provider: + name: cloudflare + api_token: yaml-token +`, + wantZone: "ts.example.com", + wantToken: "yaml-token", + }, + { + name: "env only", + yaml: base, + env: map[string]string{ + "HEADSCALE_DNS_CERTIFICATES_ENABLED": "true", + "HEADSCALE_DNS_CERTIFICATES_ZONE": "example.com", + "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", + "HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "file:" + secret, + }, + wantZone: "example.com", + wantToken: "file-token", + }, + { + name: "token from file", + yaml: base + ` challenge: + provider: + name: cloudflare + api_token: "file:` + secret + `" +`, + wantZone: "ts.example.com", + wantToken: "file-token", + }, + { + name: "acmedns registration password from file", + yaml: base + ` certificates: + enabled: true + challenge: + provider: + name: acmedns + config: + laptop.ts.example.com: + username: user + password: "file:` + secret + `" + subdomain: reg-laptop + server_url: https://auth.acme-dns.io +`, + wantZone: "ts.example.com", + wantIsolates: "laptop.ts.example.com", + }, + { + name: "acmedns registration password file missing", + yaml: base + ` certificates: + enabled: true + challenge: + provider: + name: acmedns + config: + laptop.ts.example.com: + username: user + password: "file:/nonexistent/password" + subdomain: reg-laptop + server_url: https://auth.acme-dns.io +`, + wantErr: "dns.challenge.provider.config.laptop.ts.example.com.password: ", + fileErr: true, + }, + { + // Unread by the load, a file: setting counts as present. + name: "token file missing", + yaml: base + ` challenge: + provider: + name: cloudflare + api_token: "file:/nonexistent/token" +`, + wantErr: "dns.challenge.provider.api_token: ", + fileErr: true, + }, + { + name: "provider env json", + yaml: base, + env: map[string]string{ + "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", + "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"api_token":"good","zone_token":"zone"}`, + }, + wantZone: "ts.example.com", + wantToken: "good", + }, + { + // 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 not a JSON object (env HEADSCALE_DNS_CHALLENGE_PROVIDER)", + }, + { + // The removed field warns and is dropped; the rest still loads. + name: "removed field in provider env json", + yaml: base, + env: map[string]string{ + "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", + "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"name":"cloudflare","api_token":"good","api_token_path":"/old"}`, + }, + wantWarns: []string{"dns.challenge.provider.api_token_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed"}, + wantZone: "ts.example.com", + wantToken: "good", + }, + { + name: "removed field in a registration in provider env json", + yaml: base, + env: map[string]string{ + "HEADSCALE_DNS_CERTIFICATES_ENABLED": "true", + "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "acmedns", + "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"config":{"laptop.ts.example.com":{"username":"user","password":"pass",` + + `"subdomain":"reg-laptop","server_url":"https://auth.acme-dns.io","password_path":"/old"}}}`, + }, + wantWarns: []string{"dns.challenge.provider.config.laptop.ts.example.com.password_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed"}, + wantZone: "ts.example.com", + wantIsolates: "laptop.ts.example.com", + }, + { + name: "cloudflare without api_token", + yaml: base + ` challenge: + provider: + name: cloudflare + zone_token: marker-zone-token +`, + wantErr: "cloudflare: missing api_token", + }, + { + // The old variable warns and is ignored, leaving the token unset. + name: "cloudflare token only in a removed variable", + yaml: base + ` challenge: + provider: + name: cloudflare + zone_token: marker-zone-token +`, + env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN_PATH": "/run/token"}, + wantWarns: []string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN_PATH was removed"}, + wantErr: "cloudflare: missing api_token", + }, + { + name: "powerdns without api_token", + yaml: base + ` challenge: + provider: + name: powerdns + server_url: https://pdns.example.com +`, + wantErr: "powerdns: missing api_token", + }, + { + name: "rfc2136 without a key", + yaml: base + ` challenge: + provider: + name: rfc2136 + server: ns1.example.com:53 +`, + wantErr: "rfc2136: missing key_name, key_alg, key", + }, + { + name: "hetzner without api_token", + yaml: base + ` challenge: + provider: + name: hetzner +`, + env: map[string]string{"LIBDNS_HETZNER_TOKEN": ""}, + wantErr: "hetzner: missing api_token", + }, + { + // The provider reads its token from this variable when api_token is unset. + name: "hetzner token from its own variable", + yaml: base + ` challenge: + provider: + name: hetzner + api_token: "" +`, + env: map[string]string{"LIBDNS_HETZNER_TOKEN": "marker-token"}, + wantZone: "ts.example.com", + wantProvider: "*hetzner.Provider", + }, + { + name: "gigahost without api_key", + yaml: base + ` challenge: + provider: + name: gigahost +`, + wantErr: "gigahost: missing api_key", + }, + { + name: "env overrides yaml", + yaml: base + ` challenge: + provider: + name: cloudflare + api_token: yaml-token +`, + env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "env-token"}, + wantZone: "ts.example.com", + wantToken: "env-token", + }, + { + name: "typo in provider key", + yaml: base + ` challenge: + provider: + name: cloudflare + auth_token: tok +`, + wantErr: "dns.challenge.provider is invalid", + }, + { + name: "unknown provider", + yaml: base + ` challenge: + provider: + name: route53 +`, + wantErr: "dns.challenge.provider is invalid", + }, + { + name: "acmedns one account for node certificates", + yaml: base + ` certificates: + enabled: true + challenge: + provider: + name: acmedns + username: user + password: pass + subdomain: reg + server_url: https://auth.acme-dns.io +`, + wantErr: "registration per name", + }, + { + name: "acmedns registration per name", + yaml: base + ` certificates: + enabled: true + challenge: + provider: + name: acmedns + config: + laptop.ts.example.com: + username: user + password: pass + subdomain: reg-laptop + server_url: https://auth.acme-dns.io + desktop.ts.example.com: + username: user + password: pass + subdomain: reg-desktop + server_url: https://auth.acme-dns.io +`, + wantZone: "ts.example.com", + wantIsolates: "laptop.ts.example.com", + }, + { + name: "acmedns registration shared by node names", + yaml: base + ` certificates: + enabled: true + challenge: + provider: + name: acmedns + config: + laptop.ts.example.com: + username: user + password: pass + subdomain: reg + server_url: https://auth.acme-dns.io + desktop.ts.example.com: + username: user + password: pass + subdomain: reg + server_url: https://auth.acme-dns.io +`, + wantErr: "desktop.ts.example.com and laptop.ts.example.com share a registration", + }, + { + name: "acmedns one account for headscale's own certificate", + yaml: base + ` challenge: + provider: + name: acmedns + username: user + password: pass + subdomain: reg + server_url: https://auth.acme-dns.io +tls: + acme: + hostname: headscale.example.com + challenge_type: DNS-01 +`, + wantZone: "ts.example.com", + }, + { + name: "acmedns registration shared with headscale's own name", + yaml: base + ` certificates: + enabled: true + challenge: + provider: + name: acmedns + config: + laptop.ts.example.com: + username: user + password: pass + subdomain: reg + server_url: https://auth.acme-dns.io + headscale.example.com: + username: user + password: pass + subdomain: reg + server_url: https://auth.acme-dns.io +tls: + acme: + hostname: headscale.example.com + challenge_type: DNS-01 +`, + wantErr: "headscale.example.com and laptop.ts.example.com share a registration", + }, + { + name: "enabled without magic dns", + yaml: strings.Replace(base, "magic_dns: true", "magic_dns: false", 1) + ` certificates: + enabled: true + challenge: + provider: + name: cloudflare + api_token: tok +`, + wantErr: "dns.certificates requires dns.magic_dns", + }, + { + name: "enabled without provider", + yaml: base + ` certificates: + enabled: true +`, + wantErr: "dns.certificates and tls.acme DNS-01 require dns.challenge.provider", + }, + { + name: "base_domain outside zone", + yaml: base + ` certificates: + enabled: true + zone: notexample.com + challenge: + provider: + name: cloudflare + api_token: tok +`, + wantErr: "dns.base_domain is not inside dns.certificates.zone", + }, + { + name: "zone suffix without label boundary", + yaml: base + ` certificates: + enabled: true + zone: ample.com + challenge: + provider: + name: cloudflare + api_token: tok +`, + wantErr: "dns.base_domain is not inside dns.certificates.zone", + }, + { + name: "uppercase base_domain", + yaml: strings.Replace(base, "ts.example.com", "TS.example.com", 1) + ` certificates: + enabled: true + challenge: + provider: + name: cloudflare + api_token: tok +`, + wantErr: "dns.base_domain must be a lowercase DNS name", + }, + { + name: "tls DNS-01 without provider", + yaml: base + `tls: + acme: + hostname: headscale.example.com + challenge_type: DNS-01 +`, + wantErr: "dns.certificates and tls.acme DNS-01 require dns.challenge.provider", + }, + { + name: "negative propagation timeout", + yaml: base + ` challenge: + propagation_timeout: -1s +`, + wantErr: "dns.challenge.propagation_timeout must not be negative", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for k, v := range tt.env { + t.Setenv(k, v) + } + + tmpDir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "config.yaml"), []byte(tt.yaml), 0o600)) + l, err := Load(tmpDir, false, os.Environ()) + require.Len(t, l.Warnings, len(tt.wantWarns)) + + for i, w := range tt.wantWarns { + assert.Contains(t, l.Warnings[i], w) + } + + cfg := l.Config + if err == nil { + err = Validate(cfg) + } + + if tt.wantErr != "" && !tt.fileErr { + require.ErrorContains(t, err, tt.wantErr) + assert.NotContains(t, err.Error(), "marker-") + + return + } + + require.NoError(t, err) + + if tt.fileErr { + _, err = cfg.DNSConfig.Challenge.NewProvider(types.ResolveSecret) + require.ErrorContains(t, err, tt.wantErr) + + return + } + + assert.Equal(t, tt.wantZone, cfg.DNSConfig.CertificateZone()) + + if tt.wantResolvers != nil { + assert.Equal(t, tt.wantResolvers, cfg.DNSConfig.Challenge.ResolverAddrs()) + } + + // The loader only validates the provider; startup builds it. + assert.Nil(t, cfg.DNSConfig.Challenge.Provider) + + if tt.wantNoProv { + assert.Empty(t, cfg.DNSConfig.Challenge.ProviderName()) + + return + } + + built, err := cfg.DNSConfig.Challenge.NewProvider(types.ResolveSecret) + require.NoError(t, err) + + if tt.wantProvider != "" { + assert.Equal(t, tt.wantProvider, fmt.Sprintf("%T", built)) + + return + } + + if iso, ok := built.(hsdns.Isolator); ok { + if tt.wantIsolates != "" { + assert.True(t, iso.Isolates(tt.wantIsolates)) + } + + return + } + + provider, ok := built.(*cloudflare.Provider) + require.True(t, ok, "provider is %T", built) + assert.Equal(t, tt.wantToken, provider.APIToken) + }) + } +} + +// TestTLSConfig pins the tls defaults and that the flat keys the tls block +// replaced, including the ones the NixOS module once wrote with TLS off, are +// rejected. +func TestTLSConfig(t *testing.T) { + 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: false + override_local_dns: false +` + + tests := []struct { + name string + yaml string + env map[string]string + want types.TLSConfig + wantErr []string + }{ + { + name: "defaults", + yaml: base, + want: types.TLSConfig{ACME: types.ACMEConfig{ + ChallengeType: types.HTTP01ChallengeType, + HTTPListen: ":http", + CacheDir: "/var/lib/headscale/cache", + }}, + }, + { + name: "legacy keys", + yaml: base + `tls_letsencrypt_hostname: headscale.example.com +tls_letsencrypt_challenge_type: TLS-ALPN-01 +tls_letsencrypt_listen: ":8080" +tls_letsencrypt_cache_dir: /var/lib/headscale/cache +acme_url: https://acme.example.com/directory +acme_email: admin@example.com +`, + wantErr: []string{ + "tls_letsencrypt_hostname was removed", "use tls.acme.hostname", + "tls_letsencrypt_challenge_type was removed", "tls_letsencrypt_listen was removed", + "tls_letsencrypt_cache_dir was removed", "acme_url was removed", "acme_email was removed", + }, + }, + { + name: "legacy keys next to tls keys", + yaml: base + `tls_cert_path: /old/cert.pem +tls_key_path: /old/key.pem +tls: + cert_path: /new/cert.pem + key_path: /new/key.pem +`, + wantErr: []string{"tls_cert_path was removed", "use tls.cert_path", "tls_key_path was removed"}, + }, + { + // What the NixOS module wrote when TLS was off. + name: "nixos defaults", + yaml: base + `tls_letsencrypt_hostname: "" +tls_letsencrypt_challenge_type: HTTP-01 +tls_letsencrypt_listen: ":http" +tls_letsencrypt_cache_dir: /var/lib/headscale/.cache +`, + wantErr: []string{"tls_letsencrypt_hostname was removed", "tls_letsencrypt_cache_dir was removed"}, + }, + { + name: "env", + yaml: base, + env: map[string]string{ + "HEADSCALE_TLS_CERT_PATH": "/env/cert.pem", + "HEADSCALE_TLS_KEY_PATH": "/env/key.pem", + }, + want: types.TLSConfig{ + CertPath: "/env/cert.pem", + KeyPath: "/env/key.pem", + ACME: types.ACMEConfig{ + ChallengeType: types.HTTP01ChallengeType, + HTTPListen: ":http", + CacheDir: "/var/lib/headscale/cache", + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for k, v := range tt.env { + t.Setenv(k, v) + } + + tmpDir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "config.yaml"), []byte(tt.yaml), 0o600)) + + cfg, err := loadServer(tmpDir, false) + + if tt.wantErr != nil { + for _, want := range tt.wantErr { + require.ErrorContains(t, err, want) + } + + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, cfg.TLS) + }) + } +} + +func TestBoolConfigValidation(t *testing.T) { + for _, source := range []string{"file", "env"} { + for _, tt := range []struct { + value string + want bool + invalid bool + }{ + {"true", true, false}, + {"false", false, false}, + {"1", true, false}, + {"0", false, false}, + {"T", true, false}, + {"f", false, false}, + {"TRUE", true, false}, + {"False", false, false}, + {"True", true, false}, + {"FALSE", false, false}, + {"", true, false}, + {"null", true, false}, + {"yes", false, true}, + {"on", false, true}, + {"ture", false, true}, + {"tRuE", false, true}, + {"2", false, true}, + {"[true]", false, true}, + {`""`, false, true}, + } { + if source == "env" && tt.value == "null" { + continue + } + + t.Run(source+"/"+tt.value, func(t *testing.T) { + t.Setenv("HEADSCALE_OIDC_EMAIL_VERIFIED_REQUIRED", "") + + config := `server_url: https://headscale.example.com +noise: + private_key_path: noise_private.key +prefixes: + v4: 100.64.0.0/10 +database: + type: sqlite3 +dns: + magic_dns: false + override_local_dns: false +` + if source == "file" && tt.value != "" { + config += "oidc:\n email_verified_required: " + tt.value + "\n" + } + + if source == "env" { + t.Setenv("HEADSCALE_OIDC_EMAIL_VERIFIED_REQUIRED", tt.value) + } + + path := filepath.Join(t.TempDir(), "config.yaml") + require.NoError(t, os.WriteFile(path, []byte(config), 0o600)) + + loaded, err := loadServer(path, true) + if tt.invalid { + require.Error(t, err) + assert.Contains(t, err.Error(), "oidc.email_verified_required") + + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, loaded.OIDC.EmailVerifiedRequired) + }) + } + } +} + +// TestServerConfigDisableUpdateCheck: the server config carries the setting +// as the CLI config does, for Marshal and embedders. +func TestServerConfigDisableUpdateCheck(t *testing.T) { + for _, want := range []bool{true, false} { + path := writeConfig(t, fmt.Sprintf(`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 +disable_check_updates: %t +`, want)) + cfg, err := loadServer(path, true) + require.NoError(t, err) + assert.Equal(t, want, cfg.DisableUpdateCheck) + } +} + +func TestExampleConfigBooleanSettings(t *testing.T) { + _, err := loadServer("../../config-example.yaml", true) + require.NoError(t, err) +} + +func TestLoadRejectsNonBoolean(t *testing.T) { + for _, key := range []string{"cli.insecure", "disable_check_updates"} { + t.Run(key, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.yaml") + require.NoError(t, os.WriteFile(path, nil, 0o600)) + + env := configEnvName(key) + t.Setenv(env, "true") + + _, err := loadCLI(path) + require.NoError(t, err) + + t.Setenv(env, "ture") + + _, err = loadCLI(path) + require.Error(t, err) + assert.Contains(t, err.Error(), key) + }) + } +} + +// compatRecord is the golden for one config input: what the loader produced. +type compatRecord struct { + Config json.RawMessage `json:"config,omitempty"` + Secrets map[string]string `json:"secrets,omitempty"` // secret strings the JSON form hides + Warnings []string `json:"warnings,omitempty"` + Error string `json:"error,omitempty"` // an error-only golden pins today's rejection +} + +type compatRow struct{ name, file, envFile string } + +var compatDir = filepath.Join("testdata", "compat") + +// compatRows lists every compat/.{yaml,json,env} set and every +// testdata/*.yaml file. +func compatRows(t *testing.T) []compatRow { + t.Helper() + + entries, err := os.ReadDir(compatDir) + require.NoError(t, err) + + var rows []compatRow + + byName := map[string]int{} + + for _, e := range entries { + f, ext := e.Name(), filepath.Ext(e.Name()) + if strings.HasSuffix(f, ".golden.json") || (ext != ".yaml" && ext != ".json" && ext != ".env") { + continue + } + + name := strings.TrimSuffix(f, ext) + + i, ok := byName[name] + if !ok { + i = len(rows) + byName[name] = i + rows = append(rows, compatRow{name: name}) + } + + if ext == ".env" { + rows[i].envFile = filepath.Join(compatDir, f) + } else { + rows[i].file = filepath.Join(compatDir, f) + } + } + + legacy, err := filepath.Glob(filepath.Join("testdata", "*.yaml")) + require.NoError(t, err) + + for _, f := range legacy { + rows = append(rows, compatRow{name: "testdata-" + strings.TrimSuffix(filepath.Base(f), ".yaml"), file: f}) + } + + return rows +} + +// unsetHeadscaleEnv removes ambient HEADSCALE_* for the test, which would +// leak into the goldens. +func unsetHeadscaleEnv(t *testing.T) { + t.Helper() + + for _, kv := range os.Environ() { + if k, _, _ := strings.Cut(kv, "="); strings.HasPrefix(k, "HEADSCALE_") { + t.Setenv(k, "") // registers the restore + require.NoError(t, os.Unsetenv(k)) + } + } +} + +// TestConfigCompat pins what real-world config inputs load to, so a loader +// change shows up as a golden diff, and each such diff needs a BREAKING +// CHANGELOG entry. Every compat/.{yaml,json,env} set is one row; a name +// ending in -cli is loaded as client commands load it, unvalidated. Fixtures +// are frozen inputs: to change one, add a new fixture. Refresh with +// HEADSCALE_UPDATE_GOLDEN=1. +// +// Sources: example-.yaml is `git show :config-example.yaml`; +// example-main.yaml is a frozen copy of the root file; nixos-*.yaml are the +// files `nix build` writes for the flake module with nix/example-configuration.nix +// (services.headscale.configFile, environment.etc."headscale/config.yaml") and +// 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, -pruned without the keys removed +// for having no effect. +func TestConfigCompat(t *testing.T) { + rows := compatRows(t) + update := os.Getenv("HEADSCALE_UPDATE_GOLDEN") != "" + + unsetHeadscaleEnv(t) + + for _, tt := range rows { + t.Run(tt.name, func(t *testing.T) { + rec := loadCompat(t, tt.file, tt.envFile, strings.HasSuffix(tt.name, "-cli")) + + out, err := json.MarshalIndent(rec, "", " ") + require.NoError(t, err) + + out = append(out, '\n') + gpath := filepath.Join(compatDir, tt.name+".golden.json") + + if update { + require.NoError(t, os.WriteFile(gpath, out, 0o600)) + + return + } + + want, err := os.ReadFile(gpath) + require.NoErrorf(t, err, "missing golden %s: run with HEADSCALE_UPDATE_GOLDEN=1", gpath) + + if d := gocmp.Diff(string(want), string(out)); d != "" { + t.Errorf("config compat mismatch (-golden +got):\n%s", d) + } + }) + } +} + +// compatConfig is the JSON shape of Config the goldens were written in, +// frozen so Config can change without a golden diff; compatView renders +// into it. +type compatConfig struct { + ServerURL string + Addr string + MetricsAddr string + TrustedProxies []netip.Prefix + Node compatNodeConfig + PreAuthKeys compatPreAuthKeysConfig + PrefixV4 *netip.Prefix + PrefixV6 *netip.Prefix + IPAllocation string + NoisePrivateKeyPath string + BaseDomain string + Log compatLogConfig + DisableUpdateCheck bool + Database compatDatabaseConfig + DERP compatDERPConfig + TLS compatTLSConfig + DNSConfig compatDNSConfig + TailcfgDNSConfig *tailcfg.DNSConfig + DNSChallenge compatDNSChallengeConfig + UnixSocket string + UnixSocketPermission fs.FileMode + OIDC compatOIDCConfig + LogTail compatLogTailConfig + Taildrop compatTaildropConfig + AutoUpdate compatAutoUpdateConfig + CLI compatCLIConfig + Policy compatPolicyConfig + Tuning compatTuning +} + +type compatNodeConfig struct { + Expiry time.Duration + Ephemeral compatEphemeralConfig + Routes compatRouteConfig +} + +type compatEphemeralConfig struct{ InactivityTimeout time.Duration } + +type compatRouteConfig struct{ HA compatHARouteConfig } + +type compatHARouteConfig struct{ ProbeInterval, ProbeTimeout time.Duration } + +type compatPreAuthKeysConfig struct{ RevokedRetention time.Duration } + +type compatLogConfig struct { + Format string + Level zerolog.Level +} + +type compatDatabaseConfig struct { + Type string + Debug bool + Gorm compatGormConfig + Sqlite compatSqliteConfig + Postgres compatPostgresConfig +} + +type compatGormConfig struct { + Debug bool + SlowThreshold time.Duration + SkipErrRecordNotFound bool + ParameterizedQueries bool +} + +type compatSqliteConfig struct { + Path string + WriteAheadLog bool + WALAutoCheckPoint int +} + +type compatPostgresConfig struct { + Host string + Port int + Name string + User string + Ssl string + MaxOpenConnections int + MaxIdleConnections int + ConnMaxIdleTimeSecs int +} + +type compatDERPConfig struct { + ServerEnabled bool + AutomaticallyAddEmbeddedDerpRegion bool + ServerRegionID tailcfg.DERPRegionID + ServerRegionCode string + ServerRegionName string + ServerPrivateKeyPath string + ServerVerifyClients bool + STUNAddr string + URLs []url.URL + Paths []string + DERPMap *tailcfg.DERPMap + AutoUpdate bool + UpdateFrequency time.Duration + IPv4 string + IPv6 string +} + +type compatTLSConfig struct { + CertPath string + KeyPath string + ACME compatACMEConfig +} + +type compatACMEConfig struct { + Hostname string + ChallengeType string + HTTPListen string + URL string + Email string + CacheDir string +} + +type compatDNSConfig struct { + MagicDNS bool + BaseDomain string + OverrideLocalDNS bool + Nameservers compatNameservers + SearchDomains []string + ExtraRecords []tailcfg.DNSRecord + ExtraRecordsPath string + Certificates compatCertificatesConfig +} + +type compatNameservers struct { + Global []string + Split map[string][]string +} + +type compatCertificatesConfig struct { + Enabled bool + Zone string +} + +type compatDNSChallengeConfig struct { + PropagationTimeout time.Duration + Resolvers []string + ProviderName string +} + +type compatOIDCConfig struct { + OnlyStartIfOIDCIsAvailable bool + Issuer string + ClientID string + Scope []string + ExtraParams map[string]string + AllowedDomains []string + AllowedUsers []string + AllowedGroups []string + EmailVerifiedRequired bool + UseExpiryFromToken bool + PKCE compatPKCEConfig +} + +type compatPKCEConfig struct { + Enabled bool + Method string +} + +type compatLogTailConfig struct{ Enabled bool } + +type compatTaildropConfig struct{ Enabled bool } + +type compatAutoUpdateConfig struct{ Enabled bool } + +type compatCLIConfig struct { + Address string + Timeout time.Duration + Insecure bool +} + +type compatPolicyConfig struct { + Path string + Mode string +} + +type compatTuning struct { + BatchChangeDelay time.Duration + NodeMapSessionBufferedChanSize int + BatcherWorkers int + RegisterCacheExpiration time.Duration + RegisterCacheMaxEntries int + NodeStoreBatchSize int + NodeStoreBatchTimeout time.Duration +} + +// compatView renders cfg in the golden shape, deriving the values the +// loader used to store resolved. +func compatView(cfg *types.Config) compatConfig { + pg := cfg.Database.Postgres + oidc := cfg.OIDC + ch := cfg.DNSConfig.Challenge + acme := cfg.TLS.ACME + derp := cfg.DERP.Server + + dbType := cfg.Database.Type + if cfg.Database.IsSQLite() { + dbType = types.DatabaseSqlite + } + + derpURLs := make([]url.URL, 0, len(cfg.DERP.URLs)) + for _, s := range cfg.DERP.URLs { + u, err := url.Parse(string(s)) + if err == nil { + derpURLs = append(derpURLs, *u) + } + } + + 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: trusted, + Node: compatNodeConfig{ + Expiry: cfg.Node.Expiry, + Ephemeral: compatEphemeralConfig(cfg.Node.Ephemeral), + Routes: compatRouteConfig{HA: compatHARouteConfig(cfg.Node.Routes.HA)}, + }, + PreAuthKeys: compatPreAuthKeysConfig(cfg.PreAuthKeys), + PrefixV4: cfg.Prefixes.V4, + PrefixV6: cfg.Prefixes.V6, + IPAllocation: string(cfg.Prefixes.Allocation), + NoisePrivateKeyPath: cfg.AbsPath(cfg.Noise.PrivateKeyPath), + BaseDomain: cfg.DNSConfig.BaseDomain, + Log: compatLogConfig{ + Format: string(cfg.Log.Format), + Level: cfg.Log.Level.Zerolog(), + }, + DisableUpdateCheck: cfg.DisableUpdateCheck, + Database: compatDatabaseConfig{ + Type: string(dbType), + Debug: cfg.Database.Debug, + Gorm: compatGormConfig{ + Debug: cfg.Database.Debug, + SlowThreshold: time.Duration(cfg.Database.Gorm.SlowThreshold) * time.Millisecond, + SkipErrRecordNotFound: cfg.Database.Gorm.SkipErrRecordNotFound, + ParameterizedQueries: cfg.Database.Gorm.ParameterizedQueries, + }, + Sqlite: compatSqliteConfig{ + Path: cfg.SQLitePath(), + WriteAheadLog: cfg.Database.Sqlite.WriteAheadLog, + WALAutoCheckPoint: cfg.Database.Sqlite.WALAutoCheckPoint, + }, + Postgres: compatPostgresConfig{ + Host: pg.Host, + Port: pg.Port, + Name: pg.Name, + User: pg.User, + Ssl: pg.Ssl, + MaxOpenConnections: pg.MaxOpenConnections, + MaxIdleConnections: pg.MaxIdleConnections, + ConnMaxIdleTimeSecs: pg.ConnMaxIdleTimeSecs, + }, + }, + DERP: compatDERPConfig{ + ServerEnabled: derp.Enabled, + AutomaticallyAddEmbeddedDerpRegion: derp.AutomaticallyAddEmbeddedDerpRegion, + ServerRegionID: derp.RegionID, + ServerRegionCode: derp.RegionCode, + ServerRegionName: derp.RegionName, + ServerPrivateKeyPath: cfg.AbsPath(derp.PrivateKeyPath), + ServerVerifyClients: derp.VerifyClients, + STUNAddr: string(derp.STUNAddr), + URLs: derpURLs, + Paths: cfg.DERP.Paths, + DERPMap: cfg.DERP.DERPMap, + AutoUpdate: cfg.DERP.AutoUpdate, + UpdateFrequency: cfg.DERP.UpdateFrequency, + IPv4: derp.IPv4, + IPv6: derp.IPv6, + }, + TLS: compatTLSConfig{ + CertPath: cfg.AbsPath(cfg.TLS.CertPath), + KeyPath: cfg.AbsPath(cfg.TLS.KeyPath), + ACME: compatACMEConfig{ + Hostname: acme.Hostname, + ChallengeType: string(acme.ChallengeType), + HTTPListen: string(acme.HTTPListen), + URL: string(acme.URL), + Email: acme.Email, + CacheDir: cfg.AbsPath(acme.CacheDir), + }, + }, + DNSConfig: compatDNSConfig{ + MagicDNS: cfg.DNSConfig.MagicDNS, + BaseDomain: cfg.DNSConfig.BaseDomain, + OverrideLocalDNS: cfg.DNSConfig.OverrideLocalDNS, + Nameservers: compatNameservers(cfg.DNSConfig.Nameservers), + SearchDomains: cfg.DNSConfig.SearchDomains, + ExtraRecords: cfg.DNSConfig.ExtraRecords, + ExtraRecordsPath: cfg.DNSConfig.ExtraRecordsPath, + Certificates: compatCertificatesConfig{ + Enabled: cfg.DNSConfig.Certificates.Enabled, + Zone: cfg.DNSConfig.CertificateZone(), + }, + }, + TailcfgDNSConfig: cfg.TailcfgDNSConfig(), + DNSChallenge: compatDNSChallengeConfig{ + PropagationTimeout: ch.PropagationTimeout, + Resolvers: ch.ResolverAddrs(), + ProviderName: ch.ProviderName(), + }, + UnixSocket: cfg.UnixSocket, + UnixSocketPermission: fs.FileMode(cfg.UnixSocketPermission), + OIDC: compatOIDCConfig{ + OnlyStartIfOIDCIsAvailable: oidc.OnlyStartIfOIDCIsAvailable, + Issuer: string(oidc.Issuer), + ClientID: oidc.ClientID, + Scope: oidc.Scope, + ExtraParams: extraParams, + AllowedDomains: oidc.AllowedDomains, + AllowedUsers: oidc.AllowedUsers, + AllowedGroups: oidc.AllowedGroups, + EmailVerifiedRequired: oidc.EmailVerifiedRequired, + UseExpiryFromToken: oidc.UseExpiryFromToken, + PKCE: compatPKCEConfig{Enabled: oidc.PKCE.Enabled, Method: string(oidc.PKCE.Method)}, + }, + LogTail: compatLogTailConfig(cfg.LogTail), + Taildrop: compatTaildropConfig(cfg.Taildrop), + AutoUpdate: compatAutoUpdateConfig(cfg.AutoUpdate), + CLI: compatCLIConfig{ + Address: cfg.CLI.Address, + Timeout: cfg.CLI.Timeout, + Insecure: cfg.CLI.Insecure, + }, + Policy: compatPolicyConfig{Path: cfg.Policy.Path, Mode: string(cfg.Policy.Mode)}, + Tuning: tuning, + } +} + +// loadCompat loads one fixture set the way headscale does, from a temp dir +// so relative paths and the error text do not depend on the checkout. +func loadCompat(t *testing.T, file, envFile string, cli bool) compatRecord { + t.Helper() + + dir := stageCompat(t, file, envFile) + + l, err := Load(dir, false, os.Environ()) + cfg := l.Config + + norm := func(s string) string { return strings.ReplaceAll(s, dir, "$DIR") } + + var rec compatRecord + + warn := func(ws []string) { + for _, w := range ws { + rec.Warnings = append(rec.Warnings, norm(w)) + } + } + + warn(l.Warnings) + + if err == nil && !cli { + // As serve takes it: validated, advisories reported, without the + // CLI's key. + err = Validate(cfg) + warn(Warnings(cfg)) + + cfg.CLI.APIKey = "" + if err != nil { + cfg = nil + } + } + + if err != nil { + rec.Error = norm(err.Error()) + } + + if cfg != nil { + view := compatView(cfg) + if cli { + // Client commands read only these; compatView would derive server + // values from the rest. + view = compatConfig{ + Log: view.Log, + DisableUpdateCheck: view.DisableUpdateCheck, + UnixSocket: view.UnixSocket, + CLI: view.CLI, + } + } + + if view.Tuning.BatcherWorkers == types.DefaultBatcherWorkers() { + view.Tuning.BatcherWorkers = 0 // depends on runtime.NumCPU + } + + raw, err := json.Marshal(view) + require.NoError(t, err) + + rec.Config = json.RawMessage(norm(string(raw))) + // As its consumer reads it; one no consumer reads stays as written. + secret := func(s types.Secret, active bool) string { + if !active { + return string(s) + } + + v, err := s.Resolve() + require.NoError(t, err) + + return v + } + rec.Secrets = map[string]string{ + "database.postgres.pass": secret(cfg.Database.Postgres.Pass, cfg.Database.Type == types.DatabasePostgres), + "oidc.client_secret": secret(cfg.OIDC.ClientSecret, cfg.OIDC.Issuer != ""), + "cli.api_key": secret(cfg.CLI.APIKey, cfg.CLI.Address != ""), + } + maps.DeleteFunc(rec.Secrets, func(_, v string) bool { return v == "" }) + } + + return rec +} + +// stageCompat copies file into a temp dir as config. and sets the +// variables of envFile, returning the dir. +func stageCompat(t *testing.T, file, envFile string) string { + t.Helper() + + dir := t.TempDir() + + if file != "" { + data, err := os.ReadFile(file) + require.NoError(t, err) + require.NoError(t, os.WriteFile(filepath.Join(dir, "config"+filepath.Ext(file)), data, 0o600)) //nolint:gosec // dir is t.TempDir() + } + + if envFile != "" { + raw, err := os.ReadFile(envFile) + require.NoError(t, err) + + // docker --env-file syntax: no quoting, # comments. + for line := range strings.SplitSeq(string(raw), "\n") { + line = strings.TrimSpace(line) + if line == "" || strings.HasPrefix(line, "#") { + continue + } + + k, v, ok := strings.Cut(line, "=") + require.Truef(t, ok, "%s: %q is not KEY=VALUE", envFile, line) + t.Setenv(k, v) + } + } + + return dir +} + +func TestDERPServerPrivateKeyPath(t *testing.T) { + tests := []struct { + name string + derpKey string // derp.server.private_key_path line, if any + wantErr string + }{ + {"missing", "", "derp.server.private_key_path is required"}, + {"distinct", " private_key_path: derp_server_private.key\n", ""}, + {"same as noise", " private_key_path: noise_private.key\n", "must differ from noise.private_key_path"}, + {"same as noise after cleaning", " private_key_path: ./noise_private.key\n", "must differ from noise.private_key_path"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(`--- +noise: + private_key_path: noise_private.key +server_url: http://127.0.0.1:8080 +prefixes: + v4: 100.64.0.0/10 +database: + type: sqlite3 +dns: + magic_dns: false + override_local_dns: false +derp: + server: + enabled: true + stun_listen_addr: "0.0.0.0:3478" +`+tt.derpKey), 0o600)) + + _, err := loadServer(dir, false) + if tt.wantErr == "" { + require.NoError(t, err) + + return + } + + require.ErrorContains(t, err, tt.wantErr) + }) + } +} + +func TestPolicyMode(t *testing.T) { + for _, tt := range []struct { + mode string + wantErr bool + }{ + {"file", false}, + {"database", false}, + {"databse", true}, + {"File", true}, + {`""`, true}, + } { + t.Run(tt.mode, func(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(`--- +noise: + private_key_path: noise_private.key +server_url: http://127.0.0.1:8080 +prefixes: + v4: 100.64.0.0/10 +database: + type: sqlite3 +dns: + magic_dns: false + override_local_dns: false +policy: + mode: `+tt.mode+"\n"), 0o600)) + + _, err := loadServer(dir, false) + if !tt.wantErr { + require.NoError(t, err) + + return + } + + require.ErrorContains(t, err, "policy.mode has an unsupported value") + }) + } +} + +// decodeValues decodes the defaults with values layered on top. +func decodeValues(t *testing.T, values map[string]any) (*types.Config, error) { + t.Helper() + + d := Default() + s := &configSource{k: koanf.New(".")} + require.NoError(t, s.k.Load(confmap.Provider(plainMap(&d, false), ""), nil)) + require.NoError(t, s.k.Load(confmap.Provider(values, "."), nil)) + + return s.decode() +} + +// loadServer loads path with the process environment and validates the +// result, as serve does. +func loadServer(path string, isFile bool) (*types.Config, error) { + l, err := Load(path, isFile, os.Environ()) + if err != nil { + return nil, err + } + + err = Validate(l.Config) + if err != nil { + return nil, err + } + + return l.Config, nil +} + +// loadCLI loads the file path with the process environment, as client +// commands do. +func loadCLI(path string) (*types.Config, error) { + l, err := Load(path, true, os.Environ()) + + return l.Config, err +} + +// TestConfigDirRelativePaths: relative paths resolve against the directory +// the config file was found in, not the working directory. +func TestConfigDirRelativePaths(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(` +server_url: https://headscale.example.com +noise: + private_key_path: ./noise.key +policy: + path: ./policy.hujson +prefixes: + v4: 100.64.0.0/10 +database: + type: sqlite +dns: + magic_dns: false + override_local_dns: false +`), 0o600)) + + t.Chdir(t.TempDir()) + + cfg, err := loadServer(dir, false) + require.NoError(t, err) + + assert.Equal(t, "./noise.key", cfg.Noise.PrivateKeyPath) + assert.Equal(t, filepath.Join(dir, "noise.key"), cfg.AbsPath(cfg.Noise.PrivateKeyPath)) + assert.Equal(t, "./policy.hujson", cfg.Policy.Path) + assert.Equal(t, filepath.Join(dir, "policy.hujson"), cfg.AbsPath(cfg.Policy.Path)) +} + +// TestSQLiteMemoryPath: ":memory:" names SQLite's in-memory database, not a +// file to resolve against the config directory. +func TestSQLiteMemoryPath(t *testing.T) { + for _, withFile := range []bool{false, true} { + t.Run(fmt.Sprintf("file=%t", withFile), func(t *testing.T) { + for k, v := range map[string]string{ + "HEADSCALE_SERVER_URL": "https://headscale.example.com", + "HEADSCALE_NOISE_PRIVATE_KEY_PATH": "noise.key", + "HEADSCALE_PREFIXES_V4": "100.64.0.0/10", + "HEADSCALE_DATABASE_TYPE": "sqlite", + "HEADSCALE_DATABASE_SQLITE_PATH": ":memory:", + "HEADSCALE_DNS_MAGIC_DNS": "false", + "HEADSCALE_DNS_OVERRIDE_LOCAL_DNS": "false", + } { + t.Setenv(k, v) + } + + dir := t.TempDir() + if withFile { + require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), nil, 0o600)) + } + + cfg, err := loadServer(dir, false) + require.NoError(t, err) + assert.Equal(t, ":memory:", cfg.SQLitePath()) + assert.True(t, filepath.IsAbs(cfg.AbsPath(cfg.Noise.PrivateKeyPath)), cfg.AbsPath(cfg.Noise.PrivateKeyPath)) + }) + } +} + +// 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) { + cfg, err := loadCLI(writeConfig(t, tt.file)) + require.NoError(t, err) + + err = Validate(cfg) + if len(tt.want) == 0 { + require.NoError(t, err) + + return + } + + assert.Equal(t, tt.want, reasons(types.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) + } + + cfg, err := loadCLI(writeConfig(t, tt.yaml)) + require.NoError(t, err) + + out, err := json.Marshal(plainMap(cfg, true)) + require.NoError(t, err) + assert.NotContains(t, string(out), "marker-secret") + + err = Validate(cfg) + require.Error(t, err) + assert.Equal(t, []string{"dns.challenge.provider.name must be a provider name"}, reasons(types.ConfigErrors(err))) + assert.NotContains(t, err.Error(), "marker-secret") + }) + } + + // A scalar name is coerced to its text, as before. + assert.Equal(t, "123", types.DNSChallengeConfig{ProviderSettings: map[string]any{"name": 123}}.ProviderName()) +} + +// 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) { + _, err := decodeValues(t, map[string]any{"derp.urls": []string{ + "https://controlplane.tailscale.com/derpmap/default", + "://bad", + }}) + + require.ErrorContains(t, err, "derp.urls is not an http:// or https:// URL") +} + +// TestLoadKeepsWarningsOnError: a file that fails to load still reports +// what the environment said, so the caller can print it with the error. +func TestLoadKeepsWarningsOnError(t *testing.T) { + path := writeConfig(t, "server_url: https://h\nbogus_key: 1\n") + + l, err := Load(path, true, []string{"HEADSCALE_SERVR_URL=x"}) + require.ErrorContains(t, err, "unknown configuration key bogus_key") + require.NotNil(t, l) + assert.Nil(t, l.Config) + assert.Equal(t, path, l.File) + require.Len(t, l.Warnings, 1) + assert.Contains(t, l.Warnings[0], "unknown environment variable HEADSCALE_SERVR_URL") +} + +// TestEnvNames: every key has its own variable, and setting that variable +// alone changes that key alone. +func TestEnvNames(t *testing.T) { + keys := schemaKeys() + + byName := map[string]string{} + + for _, k := range keys { + name := configEnvName(k.Path) + require.NotContainsf(t, byName, name, "%s and %s share %s", byName[name], k.Path, name) + byName[name] = k.Path + } + + // A valid value for each type, unlike any default. + samples := map[reflect.Type]string{ + reflect.TypeFor[time.Duration](): "7s", + reflect.TypeFor[*netip.Prefix](): "100.64.0.0/16", + reflect.TypeFor[[]netip.Prefix](): "10.0.0.0/8 fd00::/8", + reflect.TypeFor[types.ListenAddr](): "127.0.0.1:7", + reflect.TypeFor[types.HTTPURL](): "https://example.com/x", + reflect.TypeFor[[]types.HTTPURL](): "https://example.com/x", + reflect.TypeFor[types.FileMode](): "0700", + reflect.TypeFor[[]string](): "a b", + reflect.TypeFor[[]tailcfg.DNSRecord](): `[{"name":"a.example.com","type":"A","value":"100.64.0.1"}]`, + reflect.TypeFor[map[string][]string](): `{"a.example.com":["1.1.1.1"]}`, + reflect.TypeFor[map[string]string](): `{"k":"v"}`, + reflect.TypeFor[map[string]any](): `{"name":"cloudflare"}`, + } + + path := writeConfig(t, "") + d := Default() + defaults := types.ConfigFields(&d) + + for i, k := range keys { + t.Run(k.Path, func(t *testing.T) { + def := defaults[i].Value + require.Equal(t, k.Path, defaults[i].Path) + + val, ok := samples[def.Type()] + + switch e, enum := reflect.TypeAssert[interface{ Allowed() []string }](def); { + case ok: + case enum: + i := slices.IndexFunc(e.Allowed(), func(v string) bool { return v != def.String() }) + val = e.Allowed()[i] + case def.Kind() == reflect.Bool: + val = strconv.FormatBool(!def.Bool()) + case def.CanInt(): + val = strconv.FormatInt(def.Int()+1, 10) + case def.Kind() == reflect.String: + val = "x" + def.String() + default: + t.Fatalf("no sample for %s", def.Type()) + } + + l, err := Load(path, true, []string{configEnvName(k.Path) + "=" + val}) + require.NoError(t, err) + assert.Empty(t, l.Warnings) + + for j, f := range types.ConfigFields(l.Config) { + same := reflect.DeepEqual(f.Value.Interface(), defaults[j].Value.Interface()) + assert.Equalf(t, f.Path != k.Path, same, "%s=%s: %s", configEnvName(k.Path), val, f.Path) + } + }) + } +} diff --git a/hscontrol/config/marshal.go b/hscontrol/config/marshal.go new file mode 100644 index 000000000..06cef7006 --- /dev/null +++ b/hscontrol/config/marshal.go @@ -0,0 +1,18 @@ +package config + +import ( + "github.com/juanfont/headscale/hscontrol/types" +) + +// Marshal renders cfg in the file format of ext: ".yaml", ".yml", ".json" +// or ".hujson", keyed as a file is, with secrets redacted: a secret shows +// only a file: reference, a URL no userinfo, a DNS provider only its name +// and file: references. +func Marshal(cfg *types.Config, ext string) ([]byte, error) { + parser, err := configParser(ext) + if err != nil { + return nil, err + } + + return parser.Marshal(plainMap(cfg, true)) +} diff --git a/hscontrol/config/marshal_test.go b/hscontrol/config/marshal_test.go new file mode 100644 index 000000000..2869e23b4 --- /dev/null +++ b/hscontrol/config/marshal_test.go @@ -0,0 +1,157 @@ +package config + +import ( + "cmp" + "context" + "maps" + "testing" + + "github.com/juanfont/headscale/hscontrol/types" + "github.com/libdns/libdns" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "tailscale.com/tailcfg" +) + +// fileFormats are the formats a configuration can be written in. +var fileFormats = []string{".yaml", ".json", ".hujson"} + +// fakeProvider is a Go-only DNS provider. +type fakeProvider struct{ Token string } + +func (fakeProvider) AppendRecords(context.Context, string, []libdns.Record) ([]libdns.Record, error) { + return nil, nil +} + +func (fakeProvider) DeleteRecords(context.Context, string, []libdns.Record) ([]libdns.Record, error) { + return nil, nil +} + +// TestMarshalRedacts: what Marshal prints holds no credential, in any +// format: a literal secret, a DNS provider setting of any shape, URL +// userinfo, in URL and plain string keys alike, and nothing set only from +// Go. The running values keep theirs. +func TestMarshalRedacts(t *testing.T) { + tests := []struct { + name string + provider map[string]any + hidden []string + shown []string + }{ + { + name: "string token", + provider: map[string]any{"name": "cloudflare", "api_token": "marker-api-token"}, + hidden: []string{"marker-api-token"}, + shown: []string{"cloudflare"}, + }, + { + name: "numeric token", + provider: map[string]any{"name": "cloudflare", "api_token": 123456}, + hidden: []string{"123456"}, + shown: []string{"cloudflare"}, + }, + { + name: "nested registration", + provider: map[string]any{"name": "acmedns", "config": map[string]any{ + "laptop.example.com": map[string]any{ + "username": "marker-user", + "password": "marker-registration-password", + "subdomain": "marker-subdomain", + "server_url": "https://auth.acme-dns.io", + }, + }}, + hidden: []string{"marker-user", "marker-registration-password", "marker-subdomain"}, + shown: []string{"acmedns"}, + }, + { + name: "file reference", + provider: map[string]any{"name": "cloudflare", "api_token": "file:/run/credentials/cloudflare"}, //nolint:gosec // a file reference, not a credential + shown: []string{"cloudflare", "file:/run/credentials/cloudflare"}, + }, + } + + for _, tt := range tests { + for _, ext := range fileFormats { + t.Run(tt.name+ext, func(t *testing.T) { + cfg := Default() + cfg.ServerURL = "https://marker-server-user:marker-server-pass@headscale.example.com" //nolint:gosec // test markers, not credentials + cfg.CLI.Address = "https://marker-cli-user@cli.example.com" + cfg.DNSConfig.Nameservers.Global = []string{"1.1.1.1", "https://marker-dns-user:marker-dns-pass@dns.example.com/dns-query"} + cfg.DNSConfig.Nameservers.Split = map[string][]string{"corp.example.com": {"https://marker-split-user@dns.example.com/q"}} + cfg.OIDC.ClientSecret = "marker-oidc-secret" + cfg.OIDC.Issuer = "https://marker-issuer-user:marker-issuer-pass@idp.example.com" + cfg.Database.Postgres.Pass = "marker-pg-pass" + cfg.CLI.APIKey = "file:/run/credentials/cli_key" + cfg.TLS.ACME.URL = "https://marker-acme-user@acme.example.com/dir" + cfg.DERP.URLs = []types.HTTPURL{"https://marker-derp-user:marker-derp-pass@derp.example.com/map"} + cfg.DERP.DERPMap = &tailcfg.DERPMap{Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{ + 900: {RegionID: 900, RegionName: "marker-derpmap"}, + }} + cfg.DNSConfig.Challenge.ProviderSettings = maps.Clone(tt.provider) + cfg.DNSConfig.Challenge.Provider = fakeProvider{Token: "marker-go-provider"} //nolint:gosec // a test marker, not a credential + + out, err := Marshal(&cfg, ext) + require.NoError(t, err) + + for _, h := range append([]string{ + "marker-oidc-secret", "marker-issuer-user", "marker-issuer-pass", "marker-pg-pass", + "marker-acme-user", "marker-derp-user", "marker-derp-pass", + "marker-derpmap", "marker-go-provider", + "marker-server-user", "marker-server-pass", "marker-cli-user", + "marker-dns-user", "marker-dns-pass", "marker-split-user", + }, tt.hidden...) { + assert.NotContains(t, string(out), h) + } + + for _, s := range append([]string{ + "file:/run/credentials/cli_key", "idp.example.com", "derp.example.com", + "https://headscale.example.com", "https://cli.example.com", "1.1.1.1", + "https://dns.example.com/dns-query", "https://dns.example.com/q", + }, tt.shown...) { + assert.Contains(t, string(out), s) + } + + assert.Contains(t, cfg.ServerURL, "marker-server-pass") + assert.Contains(t, cfg.CLI.Address, "marker-cli-user") + assert.Contains(t, cfg.DNSConfig.Nameservers.Global[1], "marker-dns-pass") + assert.Contains(t, cfg.DNSConfig.Nameservers.Split["corp.example.com"][0], "marker-split-user") + }) + } + } +} + +// TestMarshalRedactsServerURL: server_url is a string, not an HTTPURL, but +// its userinfo is as secret; a value whose userinfo cannot be told from the +// rest is withheld whole. The raw map, as the round trip writes it, keeps +// the value. +func TestMarshalRedactsServerURL(t *testing.T) { + for _, tt := range []struct { + serverURL string + shown string // "" when withheld + }{ + {serverURL: "https://headscale.example.com", shown: "https://headscale.example.com"}, + {serverURL: "https://marker-user:marker-pass@headscale.example.com", shown: "https://headscale.example.com"}, //nolint:gosec // test markers, not credentials + {serverURL: "https://marker-user@headscale.example.com", shown: "https://headscale.example.com"}, + {serverURL: "https://marker-user:marker-pass@headscale.example.com/%zz"}, //nolint:gosec // test markers, not credentials + {serverURL: "https://alice:marker-pass?@headscale.example.com"}, + {serverURL: "https://alice:marker-pass#@headscale.example.com"}, + {serverURL: "https://alice:marker-pass@headscale.example.com:port"}, + {serverURL: "https:/alice:marker-pass@headscale.example.com"}, + } { + for _, ext := range fileFormats { + t.Run(tt.serverURL+ext, func(t *testing.T) { + cfg := Default() + cfg.ServerURL = tt.serverURL + + out, err := Marshal(&cfg, ext) + require.NoError(t, err) + assert.NotContains(t, string(out), "marker") + + assert.Contains(t, string(out), tt.shown) + assert.Equal(t, cmp.Or(tt.shown, ""), plainMap(&cfg, true)["server_url"]) + assert.Equal(t, tt.serverURL, plainMap(&cfg, false)["server_url"]) + assert.Equal(t, tt.serverURL, cfg.ServerURL) + }) + } + } +} diff --git a/hscontrol/types/testdata/bad/duration-number.golden b/hscontrol/config/testdata/bad/duration-number.golden similarity index 100% rename from hscontrol/types/testdata/bad/duration-number.golden rename to hscontrol/config/testdata/bad/duration-number.golden diff --git a/hscontrol/types/testdata/bad/duration-number.yaml b/hscontrol/config/testdata/bad/duration-number.yaml similarity index 100% rename from hscontrol/types/testdata/bad/duration-number.yaml rename to hscontrol/config/testdata/bad/duration-number.yaml diff --git a/hscontrol/types/testdata/bad/enum.golden b/hscontrol/config/testdata/bad/enum.golden similarity index 100% rename from hscontrol/types/testdata/bad/enum.golden rename to hscontrol/config/testdata/bad/enum.golden diff --git a/hscontrol/types/testdata/bad/enum.yaml b/hscontrol/config/testdata/bad/enum.yaml similarity index 100% rename from hscontrol/types/testdata/bad/enum.yaml rename to hscontrol/config/testdata/bad/enum.yaml diff --git a/hscontrol/types/testdata/bad/env-duration.env b/hscontrol/config/testdata/bad/env-duration.env similarity index 100% rename from hscontrol/types/testdata/bad/env-duration.env rename to hscontrol/config/testdata/bad/env-duration.env diff --git a/hscontrol/types/testdata/bad/env-duration.golden b/hscontrol/config/testdata/bad/env-duration.golden similarity index 100% rename from hscontrol/types/testdata/bad/env-duration.golden rename to hscontrol/config/testdata/bad/env-duration.golden diff --git a/hscontrol/types/testdata/bad/extra-records-field.golden b/hscontrol/config/testdata/bad/extra-records-field.golden similarity index 100% rename from hscontrol/types/testdata/bad/extra-records-field.golden rename to hscontrol/config/testdata/bad/extra-records-field.golden diff --git a/hscontrol/types/testdata/bad/extra-records-field.yaml b/hscontrol/config/testdata/bad/extra-records-field.yaml similarity index 100% rename from hscontrol/types/testdata/bad/extra-records-field.yaml rename to hscontrol/config/testdata/bad/extra-records-field.yaml diff --git a/hscontrol/types/testdata/bad/filemode-number.golden b/hscontrol/config/testdata/bad/filemode-number.golden similarity index 100% rename from hscontrol/types/testdata/bad/filemode-number.golden rename to hscontrol/config/testdata/bad/filemode-number.golden diff --git a/hscontrol/types/testdata/bad/filemode-number.yaml b/hscontrol/config/testdata/bad/filemode-number.yaml similarity index 100% rename from hscontrol/types/testdata/bad/filemode-number.yaml rename to hscontrol/config/testdata/bad/filemode-number.yaml diff --git a/hscontrol/types/testdata/bad/url.golden b/hscontrol/config/testdata/bad/url.golden similarity index 100% rename from hscontrol/types/testdata/bad/url.golden rename to hscontrol/config/testdata/bad/url.golden diff --git a/hscontrol/types/testdata/bad/url.yaml b/hscontrol/config/testdata/bad/url.yaml similarity index 100% rename from hscontrol/types/testdata/bad/url.yaml rename to hscontrol/config/testdata/bad/url.yaml diff --git a/hscontrol/types/testdata/base-domain-in-server-url.yaml b/hscontrol/config/testdata/base-domain-in-server-url.yaml similarity index 100% rename from hscontrol/types/testdata/base-domain-in-server-url.yaml rename to hscontrol/config/testdata/base-domain-in-server-url.yaml diff --git a/hscontrol/types/testdata/base-domain-not-in-server-url.yaml b/hscontrol/config/testdata/base-domain-not-in-server-url.yaml similarity index 100% rename from hscontrol/types/testdata/base-domain-not-in-server-url.yaml rename to hscontrol/config/testdata/base-domain-not-in-server-url.yaml diff --git a/hscontrol/types/testdata/compat/bad-policy-mode.golden.json b/hscontrol/config/testdata/compat/bad-policy-mode.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/bad-policy-mode.golden.json rename to hscontrol/config/testdata/compat/bad-policy-mode.golden.json diff --git a/hscontrol/types/testdata/compat/bad-policy-mode.yaml b/hscontrol/config/testdata/compat/bad-policy-mode.yaml similarity index 100% rename from hscontrol/types/testdata/compat/bad-policy-mode.yaml rename to hscontrol/config/testdata/compat/bad-policy-mode.yaml diff --git a/hscontrol/types/testdata/compat/bad-values-pruned.golden.json b/hscontrol/config/testdata/compat/bad-values-pruned.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/bad-values-pruned.golden.json rename to hscontrol/config/testdata/compat/bad-values-pruned.golden.json diff --git a/hscontrol/types/testdata/compat/bad-values-pruned.yaml b/hscontrol/config/testdata/compat/bad-values-pruned.yaml similarity index 100% rename from hscontrol/types/testdata/compat/bad-values-pruned.yaml rename to hscontrol/config/testdata/compat/bad-values-pruned.yaml diff --git a/hscontrol/types/testdata/compat/bad-values-strict.golden.json b/hscontrol/config/testdata/compat/bad-values-strict.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/bad-values-strict.golden.json rename to hscontrol/config/testdata/compat/bad-values-strict.golden.json diff --git a/hscontrol/types/testdata/compat/bad-values-strict.yaml b/hscontrol/config/testdata/compat/bad-values-strict.yaml similarity index 100% rename from hscontrol/types/testdata/compat/bad-values-strict.yaml rename to hscontrol/config/testdata/compat/bad-values-strict.yaml diff --git a/hscontrol/types/testdata/compat/bad-values.golden.json b/hscontrol/config/testdata/compat/bad-values.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/bad-values.golden.json rename to hscontrol/config/testdata/compat/bad-values.golden.json diff --git a/hscontrol/types/testdata/compat/bad-values.yaml b/hscontrol/config/testdata/compat/bad-values.yaml similarity index 100% rename from hscontrol/types/testdata/compat/bad-values.yaml rename to hscontrol/config/testdata/compat/bad-values.yaml diff --git a/hscontrol/types/testdata/compat/env-cli.env b/hscontrol/config/testdata/compat/env-cli.env similarity index 100% rename from hscontrol/types/testdata/compat/env-cli.env rename to hscontrol/config/testdata/compat/env-cli.env diff --git a/hscontrol/types/testdata/compat/env-cli.golden.json b/hscontrol/config/testdata/compat/env-cli.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/env-cli.golden.json rename to hscontrol/config/testdata/compat/env-cli.golden.json diff --git a/hscontrol/types/testdata/compat/env-empty.env b/hscontrol/config/testdata/compat/env-empty.env similarity index 100% rename from hscontrol/types/testdata/compat/env-empty.env rename to hscontrol/config/testdata/compat/env-empty.env diff --git a/hscontrol/types/testdata/compat/env-empty.golden.json b/hscontrol/config/testdata/compat/env-empty.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/env-empty.golden.json rename to hscontrol/config/testdata/compat/env-empty.golden.json diff --git a/hscontrol/types/testdata/compat/env-empty.yaml b/hscontrol/config/testdata/compat/env-empty.yaml similarity index 100% rename from hscontrol/types/testdata/compat/env-empty.yaml rename to hscontrol/config/testdata/compat/env-empty.yaml diff --git a/hscontrol/types/testdata/compat/env-extra-records.env b/hscontrol/config/testdata/compat/env-extra-records.env similarity index 100% rename from hscontrol/types/testdata/compat/env-extra-records.env rename to hscontrol/config/testdata/compat/env-extra-records.env diff --git a/hscontrol/types/testdata/compat/env-extra-records.golden.json b/hscontrol/config/testdata/compat/env-extra-records.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/env-extra-records.golden.json rename to hscontrol/config/testdata/compat/env-extra-records.golden.json diff --git a/hscontrol/types/testdata/compat/env-legacy-ignored.env b/hscontrol/config/testdata/compat/env-legacy-ignored.env similarity index 100% rename from hscontrol/types/testdata/compat/env-legacy-ignored.env rename to hscontrol/config/testdata/compat/env-legacy-ignored.env diff --git a/hscontrol/types/testdata/compat/env-legacy-ignored.golden.json b/hscontrol/config/testdata/compat/env-legacy-ignored.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/env-legacy-ignored.golden.json rename to hscontrol/config/testdata/compat/env-legacy-ignored.golden.json diff --git a/hscontrol/types/testdata/compat/env-legacy-removed.env b/hscontrol/config/testdata/compat/env-legacy-removed.env similarity index 100% rename from hscontrol/types/testdata/compat/env-legacy-removed.env rename to hscontrol/config/testdata/compat/env-legacy-removed.env diff --git a/hscontrol/types/testdata/compat/env-legacy-removed.golden.json b/hscontrol/config/testdata/compat/env-legacy-removed.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/env-legacy-removed.golden.json rename to hscontrol/config/testdata/compat/env-legacy-removed.golden.json diff --git a/hscontrol/types/testdata/compat/env-lists-maps.env b/hscontrol/config/testdata/compat/env-lists-maps.env similarity index 100% rename from hscontrol/types/testdata/compat/env-lists-maps.env rename to hscontrol/config/testdata/compat/env-lists-maps.env diff --git a/hscontrol/types/testdata/compat/env-lists-maps.golden.json b/hscontrol/config/testdata/compat/env-lists-maps.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/env-lists-maps.golden.json rename to hscontrol/config/testdata/compat/env-lists-maps.golden.json diff --git a/hscontrol/types/testdata/compat/env-only.env b/hscontrol/config/testdata/compat/env-only.env similarity index 100% rename from hscontrol/types/testdata/compat/env-only.env rename to hscontrol/config/testdata/compat/env-only.env diff --git a/hscontrol/types/testdata/compat/env-only.golden.json b/hscontrol/config/testdata/compat/env-only.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/env-only.golden.json rename to hscontrol/config/testdata/compat/env-only.golden.json diff --git a/hscontrol/types/testdata/compat/env-precedence.env b/hscontrol/config/testdata/compat/env-precedence.env similarity index 100% rename from hscontrol/types/testdata/compat/env-precedence.env rename to hscontrol/config/testdata/compat/env-precedence.env diff --git a/hscontrol/types/testdata/compat/env-precedence.golden.json b/hscontrol/config/testdata/compat/env-precedence.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/env-precedence.golden.json rename to hscontrol/config/testdata/compat/env-precedence.golden.json diff --git a/hscontrol/types/testdata/compat/env-precedence.yaml b/hscontrol/config/testdata/compat/env-precedence.yaml similarity index 100% rename from hscontrol/types/testdata/compat/env-precedence.yaml rename to hscontrol/config/testdata/compat/env-precedence.yaml diff --git a/hscontrol/types/testdata/compat/env-secrets.env b/hscontrol/config/testdata/compat/env-secrets.env similarity index 100% rename from hscontrol/types/testdata/compat/env-secrets.env rename to hscontrol/config/testdata/compat/env-secrets.env diff --git a/hscontrol/types/testdata/compat/env-secrets.golden.json b/hscontrol/config/testdata/compat/env-secrets.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/env-secrets.golden.json rename to hscontrol/config/testdata/compat/env-secrets.golden.json diff --git a/hscontrol/types/testdata/compat/env-shapes-typed.env b/hscontrol/config/testdata/compat/env-shapes-typed.env similarity index 100% rename from hscontrol/types/testdata/compat/env-shapes-typed.env rename to hscontrol/config/testdata/compat/env-shapes-typed.env diff --git a/hscontrol/types/testdata/compat/env-shapes-typed.golden.json b/hscontrol/config/testdata/compat/env-shapes-typed.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/env-shapes-typed.golden.json rename to hscontrol/config/testdata/compat/env-shapes-typed.golden.json diff --git a/hscontrol/types/testdata/compat/env-shapes.env b/hscontrol/config/testdata/compat/env-shapes.env similarity index 100% rename from hscontrol/types/testdata/compat/env-shapes.env rename to hscontrol/config/testdata/compat/env-shapes.env diff --git a/hscontrol/types/testdata/compat/env-shapes.golden.json b/hscontrol/config/testdata/compat/env-shapes.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/env-shapes.golden.json rename to hscontrol/config/testdata/compat/env-shapes.golden.json diff --git a/hscontrol/types/testdata/compat/example-main.golden.json b/hscontrol/config/testdata/compat/example-main.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/example-main.golden.json rename to hscontrol/config/testdata/compat/example-main.golden.json diff --git a/hscontrol/types/testdata/compat/example-main.yaml b/hscontrol/config/testdata/compat/example-main.yaml similarity index 100% rename from hscontrol/types/testdata/compat/example-main.yaml rename to hscontrol/config/testdata/compat/example-main.yaml diff --git a/hscontrol/types/testdata/compat/example-v0.23.0.golden.json b/hscontrol/config/testdata/compat/example-v0.23.0.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.23.0.golden.json rename to hscontrol/config/testdata/compat/example-v0.23.0.golden.json diff --git a/hscontrol/types/testdata/compat/example-v0.23.0.yaml b/hscontrol/config/testdata/compat/example-v0.23.0.yaml similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.23.0.yaml rename to hscontrol/config/testdata/compat/example-v0.23.0.yaml diff --git a/hscontrol/types/testdata/compat/example-v0.24.0.golden.json b/hscontrol/config/testdata/compat/example-v0.24.0.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.24.0.golden.json rename to hscontrol/config/testdata/compat/example-v0.24.0.golden.json diff --git a/hscontrol/types/testdata/compat/example-v0.24.0.yaml b/hscontrol/config/testdata/compat/example-v0.24.0.yaml similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.24.0.yaml rename to hscontrol/config/testdata/compat/example-v0.24.0.yaml diff --git a/hscontrol/types/testdata/compat/example-v0.25.0.golden.json b/hscontrol/config/testdata/compat/example-v0.25.0.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.25.0.golden.json rename to hscontrol/config/testdata/compat/example-v0.25.0.golden.json diff --git a/hscontrol/types/testdata/compat/example-v0.25.0.yaml b/hscontrol/config/testdata/compat/example-v0.25.0.yaml similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.25.0.yaml rename to hscontrol/config/testdata/compat/example-v0.25.0.yaml diff --git a/hscontrol/types/testdata/compat/example-v0.26.0.golden.json b/hscontrol/config/testdata/compat/example-v0.26.0.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.26.0.golden.json rename to hscontrol/config/testdata/compat/example-v0.26.0.golden.json diff --git a/hscontrol/types/testdata/compat/example-v0.26.0.yaml b/hscontrol/config/testdata/compat/example-v0.26.0.yaml similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.26.0.yaml rename to hscontrol/config/testdata/compat/example-v0.26.0.yaml diff --git a/hscontrol/types/testdata/compat/example-v0.27.0.golden.json b/hscontrol/config/testdata/compat/example-v0.27.0.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.27.0.golden.json rename to hscontrol/config/testdata/compat/example-v0.27.0.golden.json diff --git a/hscontrol/types/testdata/compat/example-v0.27.0.yaml b/hscontrol/config/testdata/compat/example-v0.27.0.yaml similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.27.0.yaml rename to hscontrol/config/testdata/compat/example-v0.27.0.yaml diff --git a/hscontrol/types/testdata/compat/example-v0.28.0.golden.json b/hscontrol/config/testdata/compat/example-v0.28.0.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.28.0.golden.json rename to hscontrol/config/testdata/compat/example-v0.28.0.golden.json diff --git a/hscontrol/types/testdata/compat/example-v0.28.0.yaml b/hscontrol/config/testdata/compat/example-v0.28.0.yaml similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.28.0.yaml rename to hscontrol/config/testdata/compat/example-v0.28.0.yaml diff --git a/hscontrol/types/testdata/compat/example-v0.29.0.golden.json b/hscontrol/config/testdata/compat/example-v0.29.0.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.29.0.golden.json rename to hscontrol/config/testdata/compat/example-v0.29.0.golden.json diff --git a/hscontrol/types/testdata/compat/example-v0.29.0.yaml b/hscontrol/config/testdata/compat/example-v0.29.0.yaml similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.29.0.yaml rename to hscontrol/config/testdata/compat/example-v0.29.0.yaml diff --git a/hscontrol/types/testdata/compat/example-v0.29.7.golden.json b/hscontrol/config/testdata/compat/example-v0.29.7.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.29.7.golden.json rename to hscontrol/config/testdata/compat/example-v0.29.7.golden.json diff --git a/hscontrol/types/testdata/compat/example-v0.29.7.yaml b/hscontrol/config/testdata/compat/example-v0.29.7.yaml similarity index 100% rename from hscontrol/types/testdata/compat/example-v0.29.7.yaml rename to hscontrol/config/testdata/compat/example-v0.29.7.yaml diff --git a/hscontrol/types/testdata/compat/file-everything-pruned.env b/hscontrol/config/testdata/compat/file-everything-pruned.env similarity index 100% rename from hscontrol/types/testdata/compat/file-everything-pruned.env rename to hscontrol/config/testdata/compat/file-everything-pruned.env diff --git a/hscontrol/types/testdata/compat/file-everything-pruned.golden.json b/hscontrol/config/testdata/compat/file-everything-pruned.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/file-everything-pruned.golden.json rename to hscontrol/config/testdata/compat/file-everything-pruned.golden.json diff --git a/hscontrol/types/testdata/compat/file-everything-pruned.yaml b/hscontrol/config/testdata/compat/file-everything-pruned.yaml similarity index 100% rename from hscontrol/types/testdata/compat/file-everything-pruned.yaml rename to hscontrol/config/testdata/compat/file-everything-pruned.yaml diff --git a/hscontrol/types/testdata/compat/file-everything-strict.env b/hscontrol/config/testdata/compat/file-everything-strict.env similarity index 100% rename from hscontrol/types/testdata/compat/file-everything-strict.env rename to hscontrol/config/testdata/compat/file-everything-strict.env diff --git a/hscontrol/types/testdata/compat/file-everything-strict.golden.json b/hscontrol/config/testdata/compat/file-everything-strict.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/file-everything-strict.golden.json rename to hscontrol/config/testdata/compat/file-everything-strict.golden.json diff --git a/hscontrol/types/testdata/compat/file-everything-strict.yaml b/hscontrol/config/testdata/compat/file-everything-strict.yaml similarity index 100% rename from hscontrol/types/testdata/compat/file-everything-strict.yaml rename to hscontrol/config/testdata/compat/file-everything-strict.yaml diff --git a/hscontrol/types/testdata/compat/file-everything.env b/hscontrol/config/testdata/compat/file-everything.env similarity index 100% rename from hscontrol/types/testdata/compat/file-everything.env rename to hscontrol/config/testdata/compat/file-everything.env diff --git a/hscontrol/types/testdata/compat/file-everything.golden.json b/hscontrol/config/testdata/compat/file-everything.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/file-everything.golden.json rename to hscontrol/config/testdata/compat/file-everything.golden.json diff --git a/hscontrol/types/testdata/compat/file-everything.yaml b/hscontrol/config/testdata/compat/file-everything.yaml similarity index 100% rename from hscontrol/types/testdata/compat/file-everything.yaml rename to hscontrol/config/testdata/compat/file-everything.yaml diff --git a/hscontrol/types/testdata/compat/hsic-default-strict.env b/hscontrol/config/testdata/compat/hsic-default-strict.env similarity index 100% rename from hscontrol/types/testdata/compat/hsic-default-strict.env rename to hscontrol/config/testdata/compat/hsic-default-strict.env diff --git a/hscontrol/types/testdata/compat/hsic-default-strict.golden.json b/hscontrol/config/testdata/compat/hsic-default-strict.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/hsic-default-strict.golden.json rename to hscontrol/config/testdata/compat/hsic-default-strict.golden.json diff --git a/hscontrol/types/testdata/compat/hsic-default-strict.yaml b/hscontrol/config/testdata/compat/hsic-default-strict.yaml similarity index 100% rename from hscontrol/types/testdata/compat/hsic-default-strict.yaml rename to hscontrol/config/testdata/compat/hsic-default-strict.yaml diff --git a/hscontrol/types/testdata/compat/hsic-default.env b/hscontrol/config/testdata/compat/hsic-default.env similarity index 100% rename from hscontrol/types/testdata/compat/hsic-default.env rename to hscontrol/config/testdata/compat/hsic-default.env diff --git a/hscontrol/config/testdata/compat/hsic-default.golden.json b/hscontrol/config/testdata/compat/hsic-default.golden.json new file mode 100644 index 000000000..2e72b6ca5 --- /dev/null +++ b/hscontrol/config/testdata/compat/hsic-default.golden.json @@ -0,0 +1,7 @@ +{ + "warnings": [ + "configuration variable HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT was removed in 0.30; use HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT", + "configuration variable HEADSCALE_PRIVATE_KEY_PATH was removed in 0.23; use HEADSCALE_NOISE_PRIVATE_KEY_PATH" + ], + "error": "Fatal config error: configuration key private_key_path was removed in 0.23 ($DIR/config.yaml:1:1)\n hint: use noise.private_key_path\n" +} diff --git a/hscontrol/types/testdata/compat/hsic-default.yaml b/hscontrol/config/testdata/compat/hsic-default.yaml similarity index 100% rename from hscontrol/types/testdata/compat/hsic-default.yaml rename to hscontrol/config/testdata/compat/hsic-default.yaml diff --git a/hscontrol/types/testdata/compat/hsic-postgres-strict.env b/hscontrol/config/testdata/compat/hsic-postgres-strict.env similarity index 100% rename from hscontrol/types/testdata/compat/hsic-postgres-strict.env rename to hscontrol/config/testdata/compat/hsic-postgres-strict.env diff --git a/hscontrol/types/testdata/compat/hsic-postgres-strict.golden.json b/hscontrol/config/testdata/compat/hsic-postgres-strict.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/hsic-postgres-strict.golden.json rename to hscontrol/config/testdata/compat/hsic-postgres-strict.golden.json diff --git a/hscontrol/types/testdata/compat/hsic-postgres-strict.yaml b/hscontrol/config/testdata/compat/hsic-postgres-strict.yaml similarity index 100% rename from hscontrol/types/testdata/compat/hsic-postgres-strict.yaml rename to hscontrol/config/testdata/compat/hsic-postgres-strict.yaml diff --git a/hscontrol/types/testdata/compat/hsic-postgres.env b/hscontrol/config/testdata/compat/hsic-postgres.env similarity index 100% rename from hscontrol/types/testdata/compat/hsic-postgres.env rename to hscontrol/config/testdata/compat/hsic-postgres.env diff --git a/hscontrol/config/testdata/compat/hsic-postgres.golden.json b/hscontrol/config/testdata/compat/hsic-postgres.golden.json new file mode 100644 index 000000000..2e72b6ca5 --- /dev/null +++ b/hscontrol/config/testdata/compat/hsic-postgres.golden.json @@ -0,0 +1,7 @@ +{ + "warnings": [ + "configuration variable HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT was removed in 0.30; use HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT", + "configuration variable HEADSCALE_PRIVATE_KEY_PATH was removed in 0.23; use HEADSCALE_NOISE_PRIVATE_KEY_PATH" + ], + "error": "Fatal config error: configuration key private_key_path was removed in 0.23 ($DIR/config.yaml:1:1)\n hint: use noise.private_key_path\n" +} diff --git a/hscontrol/types/testdata/compat/hsic-postgres.yaml b/hscontrol/config/testdata/compat/hsic-postgres.yaml similarity index 100% rename from hscontrol/types/testdata/compat/hsic-postgres.yaml rename to hscontrol/config/testdata/compat/hsic-postgres.yaml diff --git a/hscontrol/types/testdata/compat/nixos-cli.golden.json b/hscontrol/config/testdata/compat/nixos-cli.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/nixos-cli.golden.json rename to hscontrol/config/testdata/compat/nixos-cli.golden.json diff --git a/hscontrol/types/testdata/compat/nixos-cli.yaml b/hscontrol/config/testdata/compat/nixos-cli.yaml similarity index 100% rename from hscontrol/types/testdata/compat/nixos-cli.yaml rename to hscontrol/config/testdata/compat/nixos-cli.yaml diff --git a/hscontrol/types/testdata/compat/nixos-example-strict.golden.json b/hscontrol/config/testdata/compat/nixos-example-strict.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/nixos-example-strict.golden.json rename to hscontrol/config/testdata/compat/nixos-example-strict.golden.json diff --git a/hscontrol/types/testdata/compat/nixos-example-strict.yaml b/hscontrol/config/testdata/compat/nixos-example-strict.yaml similarity index 100% rename from hscontrol/types/testdata/compat/nixos-example-strict.yaml rename to hscontrol/config/testdata/compat/nixos-example-strict.yaml diff --git a/hscontrol/types/testdata/compat/nixos-example.golden.json b/hscontrol/config/testdata/compat/nixos-example.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/nixos-example.golden.json rename to hscontrol/config/testdata/compat/nixos-example.golden.json diff --git a/hscontrol/types/testdata/compat/nixos-example.yaml b/hscontrol/config/testdata/compat/nixos-example.yaml similarity index 100% rename from hscontrol/types/testdata/compat/nixos-example.yaml rename to hscontrol/config/testdata/compat/nixos-example.yaml diff --git a/hscontrol/types/testdata/compat/nixos-nixpkgs.golden.json b/hscontrol/config/testdata/compat/nixos-nixpkgs.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/nixos-nixpkgs.golden.json rename to hscontrol/config/testdata/compat/nixos-nixpkgs.golden.json diff --git a/hscontrol/types/testdata/compat/nixos-nixpkgs.yaml b/hscontrol/config/testdata/compat/nixos-nixpkgs.yaml similarity index 100% rename from hscontrol/types/testdata/compat/nixos-nixpkgs.yaml rename to hscontrol/config/testdata/compat/nixos-nixpkgs.yaml diff --git a/hscontrol/types/testdata/compat/oidc-secret.txt b/hscontrol/config/testdata/compat/oidc-secret.txt similarity index 100% rename from hscontrol/types/testdata/compat/oidc-secret.txt rename to hscontrol/config/testdata/compat/oidc-secret.txt diff --git a/hscontrol/types/testdata/compat/testdata-base-domain-in-server-url.golden.json b/hscontrol/config/testdata/compat/testdata-base-domain-in-server-url.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/testdata-base-domain-in-server-url.golden.json rename to hscontrol/config/testdata/compat/testdata-base-domain-in-server-url.golden.json diff --git a/hscontrol/types/testdata/compat/testdata-base-domain-not-in-server-url.golden.json b/hscontrol/config/testdata/compat/testdata-base-domain-not-in-server-url.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/testdata-base-domain-not-in-server-url.golden.json rename to hscontrol/config/testdata/compat/testdata-base-domain-not-in-server-url.golden.json diff --git a/hscontrol/types/testdata/compat/testdata-dns-override-true-error.golden.json b/hscontrol/config/testdata/compat/testdata-dns-override-true-error.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/testdata-dns-override-true-error.golden.json rename to hscontrol/config/testdata/compat/testdata-dns-override-true-error.golden.json diff --git a/hscontrol/types/testdata/compat/testdata-dns-override-true.golden.json b/hscontrol/config/testdata/compat/testdata-dns-override-true.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/testdata-dns-override-true.golden.json rename to hscontrol/config/testdata/compat/testdata-dns-override-true.golden.json diff --git a/hscontrol/types/testdata/compat/testdata-dns_full.golden.json b/hscontrol/config/testdata/compat/testdata-dns_full.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/testdata-dns_full.golden.json rename to hscontrol/config/testdata/compat/testdata-dns_full.golden.json diff --git a/hscontrol/types/testdata/compat/testdata-dns_full_no_magic.golden.json b/hscontrol/config/testdata/compat/testdata-dns_full_no_magic.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/testdata-dns_full_no_magic.golden.json rename to hscontrol/config/testdata/compat/testdata-dns_full_no_magic.golden.json diff --git a/hscontrol/types/testdata/compat/testdata-minimal.golden.json b/hscontrol/config/testdata/compat/testdata-minimal.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/testdata-minimal.golden.json rename to hscontrol/config/testdata/compat/testdata-minimal.golden.json diff --git a/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json b/hscontrol/config/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json rename to hscontrol/config/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json diff --git a/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded.golden.json b/hscontrol/config/testdata/compat/testdata-policy-path-is-loaded.golden.json similarity index 100% rename from hscontrol/types/testdata/compat/testdata-policy-path-is-loaded.golden.json rename to hscontrol/config/testdata/compat/testdata-policy-path-is-loaded.golden.json diff --git a/hscontrol/types/testdata/dns-override-true-error.yaml b/hscontrol/config/testdata/dns-override-true-error.yaml similarity index 100% rename from hscontrol/types/testdata/dns-override-true-error.yaml rename to hscontrol/config/testdata/dns-override-true-error.yaml diff --git a/hscontrol/types/testdata/dns-override-true.yaml b/hscontrol/config/testdata/dns-override-true.yaml similarity index 100% rename from hscontrol/types/testdata/dns-override-true.yaml rename to hscontrol/config/testdata/dns-override-true.yaml diff --git a/hscontrol/types/testdata/dns_full.yaml b/hscontrol/config/testdata/dns_full.yaml similarity index 100% rename from hscontrol/types/testdata/dns_full.yaml rename to hscontrol/config/testdata/dns_full.yaml diff --git a/hscontrol/types/testdata/dns_full_no_magic.yaml b/hscontrol/config/testdata/dns_full_no_magic.yaml similarity index 100% rename from hscontrol/types/testdata/dns_full_no_magic.yaml rename to hscontrol/config/testdata/dns_full_no_magic.yaml diff --git a/hscontrol/types/testdata/minimal.yaml b/hscontrol/config/testdata/minimal.yaml similarity index 100% rename from hscontrol/types/testdata/minimal.yaml rename to hscontrol/config/testdata/minimal.yaml diff --git a/hscontrol/types/testdata/policy-path-is-loaded-strict.yaml b/hscontrol/config/testdata/policy-path-is-loaded-strict.yaml similarity index 100% rename from hscontrol/types/testdata/policy-path-is-loaded-strict.yaml rename to hscontrol/config/testdata/policy-path-is-loaded-strict.yaml diff --git a/hscontrol/types/testdata/policy-path-is-loaded.yaml b/hscontrol/config/testdata/policy-path-is-loaded.yaml similarity index 100% rename from hscontrol/types/testdata/policy-path-is-loaded.yaml rename to hscontrol/config/testdata/policy-path-is-loaded.yaml diff --git a/hscontrol/config/validate.go b/hscontrol/config/validate.go new file mode 100644 index 000000000..01f939c0f --- /dev/null +++ b/hscontrol/config/validate.go @@ -0,0 +1,791 @@ +package config + +import ( + "errors" + "fmt" + "net" + "net/netip" + "net/url" + "path/filepath" + "strings" + "time" + + "github.com/juanfont/headscale/hscontrol/dns" + "github.com/juanfont/headscale/hscontrol/types" + "tailscale.com/net/tsaddr" + "tailscale.com/util/dnsname" +) + +const dnsDocsURL = "https://headscale.net/stable/ref/dns/" + +var ( + errOIDCIssuerInvalid = errors.New("oidc.issuer must be a valid http(s) URL") + errOIDCClientIDRequired = errors.New("oidc.client_id is required when oidc.issuer is set") + errOIDCClientSecretRequired = errors.New("oidc.client_secret is required when oidc.issuer is set") + errServerURLSuffix = errors.New("server_url cannot be part of base_domain in a way that could make the DERP and headscale server unreachable") + errServerURLSame = errors.New("server_url cannot use the same domain as base_domain in a way that could make the DERP and headscale server unreachable") + errInvalidPKCEMethod = errors.New("pkce.method must be either 'plain' or 'S256'") + errTrustedProxyZeroRange = errors.New("0.0.0.0/0 and ::/0 are not allowed") +) + +// Validate reports every rule c breaks, in one error. It reads no file and +// logs nothing; [Warnings] holds the advisories. +func Validate(c *types.Config) error { + v := &configValidator{} + + // 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 c.OIDC.Issuer != "" { + err := validateOIDC(c.OIDC) + if err != nil { + v.Add(&types.ConfigError{ + Reason: "OIDC configuration is invalid", + Detail: err.Error(), + Hint: "check oidc.issuer, oidc.client_id, oidc.client_secret, and oidc.pkce.method", + Cause: err, + }) + } + } + + if len(c.DNSConfig.ExtraRecords) > 0 && c.DNSConfig.ExtraRecordsPath != "" { + v.Add(&types.ConfigError{ + Reason: "dns.extra_records and dns.extra_records_path are mutually exclusive", + Current: []types.KV{ + {Key: "dns.extra_records_path", Value: c.DNSConfig.ExtraRecordsPath}, + {Key: "dns.extra_records", Value: ""}, + }, + Hint: "keep one (a path is recommended for production); remove the other", + }) + } + + acme := c.TLS.ACME + if acme.Hostname != "" && (c.TLS.CertPath != "" || c.TLS.KeyPath != "") { + v.Add(&types.ConfigError{ + Reason: "tls.acme.hostname and tls.cert_path/tls.key_path are mutually exclusive", + Current: []types.KV{{Key: "tls.acme.hostname", Value: acme.Hostname}}, + ConflictsWith: []types.KV{ + {Key: "tls.cert_path", Value: c.TLS.CertPath}, + {Key: "tls.key_path", Value: 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 c.Noise.PrivateKeyPath == "" { + v.Add(&types.ConfigError{ + Reason: "noise.private_key_path is required", + Hint: "set noise.private_key_path: /var/lib/headscale/noise_private.key (or any writable path)", + See: "https://headscale.net/stable/setup/install/official/", + }) + } + + validateListenerCollisions(c, v) + + if ct := acme.ChallengeType; ct != types.HTTP01ChallengeType && + ct != types.TLSALPN01ChallengeType && ct != types.DNS01ChallengeType { + v.Add(&types.ConfigError{ + Reason: "tls.acme.challenge_type has an unsupported value", + Current: []types.KV{{Key: "tls.acme.challenge_type", Value: string(ct)}}, + Allowed: ct.Allowed(), + Hint: "pick one of the allowed values; HTTP-01 is the default", + }) + } + + if !strings.HasPrefix(c.ServerURL, "http://") && !strings.HasPrefix(c.ServerURL, "https://") { + v.Add(&types.ConfigError{ + Reason: "server_url is missing a scheme", + Current: []types.KV{{Key: "server_url", Value: c.ServerURL}}, + Hint: "prefix the URL with https:// (recommended) or http://", + }) + } + + // Minimum inactivity time out is keepalive timeout (60s) plus a few seconds + // to avoid races + minInactivityTimeout := 65 * time.Second + + if ephemeralTimeout := c.Node.Ephemeral.InactivityTimeout; ephemeralTimeout <= minInactivityTimeout { + v.Add(&types.ConfigError{ + Reason: "node.ephemeral.inactivity_timeout is below the minimum", + Current: []types.KV{{Key: "node.ephemeral.inactivity_timeout", Value: ephemeralTimeout.String()}}, + Minimum: minInactivityTimeout.String(), + Hint: "raise the value above the keepalive interval (60s) plus a safety margin", + }) + } + + if c.DNSConfig.OverrideLocalDNS && len(c.DNSConfig.Nameservers.Global) == 0 { + v.Add(&types.ConfigError{ + Reason: "dns.nameservers.global is required when dns.override_local_dns is true", + Current: []types.KV{ + {Key: "dns.override_local_dns", Value: true}, + {Key: "dns.nameservers.global", Value: "[]"}, + }, + Hint: "list at least one upstream nameserver, or set dns.override_local_dns: false", + See: dnsDocsURL, + }) + } + + // Validate HA health probing parameters + if ha := c.Node.Routes.HA; ha.ProbeInterval > 0 { + if ha.ProbeInterval < 2*time.Second { + v.Add(&types.ConfigError{ + Reason: "node.routes.ha.probe_interval is below the minimum", + Current: []types.KV{{Key: "node.routes.ha.probe_interval", Value: ha.ProbeInterval.String()}}, + Minimum: "2s", + Hint: "raise the value to at least the minimum", + }) + } + + if ha.ProbeTimeout < 1*time.Second { + v.Add(&types.ConfigError{ + Reason: "node.routes.ha.probe_timeout is below the minimum", + Current: []types.KV{{Key: "node.routes.ha.probe_timeout", Value: ha.ProbeTimeout.String()}}, + Minimum: "1s", + Hint: "raise the value to at least the minimum", + }) + } + + if ha.ProbeTimeout >= ha.ProbeInterval { + v.Add(&types.ConfigError{ + Reason: "node.routes.ha.probe_timeout must be less than node.routes.ha.probe_interval", + Current: []types.KV{ + {Key: "node.routes.ha.probe_timeout", Value: ha.ProbeTimeout.String()}, + {Key: "node.routes.ha.probe_interval", Value: ha.ProbeInterval.String()}, + }, + Hint: "lower probe_timeout below probe_interval (a probe must finish before the next one starts)", + }) + } + } + + // Validate tuning parameters + if size := c.Tuning.NodeStoreBatchSize; size <= 0 { + v.Add(&types.ConfigError{ + Reason: "tuning.node_store_batch_size must be positive", + Current: []types.KV{{Key: "tuning.node_store_batch_size", Value: size}}, + Hint: "set to a positive integer (default: 100)", + }) + } + + if timeout := c.Tuning.NodeStoreBatchTimeout; timeout <= 0 { + v.Add(&types.ConfigError{ + Reason: "tuning.node_store_batch_timeout must be positive", + Current: []types.KV{{Key: "tuning.node_store_batch_timeout", Value: timeout.String()}}, + Hint: "set to a positive duration (default: 500ms)", + }) + } + + validateDERP(c, v) + validateDatabase(c, v) + validatePolicy(c, v) + validateMagicDNS(c, v) + validateCertificates(c, v) + validatePrefixes(c, v) + validateDNSProvider(c, 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() +} + +// validatePolicy records a ConfigError for an unknown policy.mode: +// startup would load no policy at all and allow all traffic. +func validatePolicy(c *types.Config, v *configValidator) { + m := c.Policy.Mode + if m == types.PolicyModeFile || m == types.PolicyModeDB { + return + } + + v.Add(&types.ConfigError{ + Reason: "policy.mode has an unsupported value", + Current: []types.KV{{Key: "policy.mode", Value: string(m)}}, + Allowed: m.Allowed(), + See: "https://headscale.net/stable/ref/policy/", + }) +} + +// validateDERP records ConfigErrors when the embedded DERP server is +// enabled without the addresses or paths it needs. +func validateDERP(c *types.Config, v *configValidator) { + server := c.DERP.Server + if !server.Enabled { + return + } + + if server.STUNAddr == "" { + v.Add(&types.ConfigError{ + Reason: "derp.server.stun_listen_addr is required when the embedded DERP server is enabled", + Current: []types.KV{ + {Key: "derp.server.enabled", Value: true}, + {Key: "derp.server.stun_listen_addr", Value: ""}, + }, + Hint: `set derp.server.stun_listen_addr (e.g. "0.0.0.0:3478"), or set derp.server.enabled: false`, + See: "https://headscale.net/stable/ref/derp/", + }) + } + + derpKey, noiseKey := server.PrivateKeyPath, c.Noise.PrivateKeyPath + switch { + case derpKey == "": + v.Add(&types.ConfigError{ + Reason: "derp.server.private_key_path is required when the embedded DERP server is enabled", + Current: []types.KV{ + {Key: "derp.server.enabled", Value: true}, + {Key: "derp.server.private_key_path", Value: ""}, + }, + Hint: `set derp.server.private_key_path (e.g. "/var/lib/headscale/derp_server_private.key"), or set derp.server.enabled: false`, + See: "https://headscale.net/stable/ref/derp/", + }) + case filepath.Clean(derpKey) == filepath.Clean(noiseKey): + // Startup would create one key and then refuse it for both uses. + v.Add(&types.ConfigError{ + Reason: "derp.server.private_key_path must differ from noise.private_key_path", + Current: []types.KV{ + {Key: "derp.server.private_key_path", Value: derpKey}, + {Key: "noise.private_key_path", Value: noiseKey}, + }, + Hint: `set derp.server.private_key_path to its own file (e.g. "/var/lib/headscale/derp_server_private.key")`, + See: "https://headscale.net/stable/ref/derp/", + }) + } + + if !server.AutomaticallyAddEmbeddedDerpRegion && len(c.DERP.Paths) == 0 { + v.Add(&types.ConfigError{ + Reason: "derp.paths is required when derp.server.automatically_add_embedded_derp_region is false", + Current: []types.KV{ + {Key: "derp.server.automatically_add_embedded_derp_region", Value: false}, + {Key: "derp.paths", Value: "[]"}, + }, + Hint: "list at least one DERP map file (.yaml, .yml, .json or .hujson) in derp.paths, or set automatically_add_embedded_derp_region: true", + }) + } +} + +// validateDatabase records a ConfigError when database.type is not one of +// the supported backends. +func validateDatabase(c *types.Config, v *configValidator) { + t := c.Database.Type + switch t { + case types.DatabaseSqlite, types.DatabasePostgres, "sqlite": + return + } + + v.Add(&types.ConfigError{ + Reason: "database.type has an unsupported value", + Current: []types.KV{{Key: "database.type", Value: string(t)}}, + Allowed: t.Allowed(), + Hint: "pick one of the allowed values; sqlite is the default for single-host deployments", + See: "https://headscale.net/stable/ref/database/", + }) +} + +// validateMagicDNS records a ConfigError when MagicDNS is enabled without a +// base_domain. +func validateMagicDNS(c *types.Config, v *configValidator) { + if c.DNSConfig.MagicDNS && c.DNSConfig.BaseDomain == "" { + v.Add(&types.ConfigError{ + Reason: "dns.base_domain is required when dns.magic_dns is true", + Current: []types.KV{ + {Key: "dns.magic_dns", Value: true}, + {Key: "dns.base_domain", Value: ""}, + }, + Hint: `set dns.base_domain to a domain you control (e.g. "ts.example.net"), or set dns.magic_dns: false`, + See: dnsDocsURL, + }) + } +} + +// 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 validateCertificates(c *types.Config, v *configValidator) { + ch := c.DNSConfig.Challenge + if timeout := ch.PropagationTimeout; timeout < 0 { + v.Add(&types.ConfigError{ + Reason: "dns.challenge.propagation_timeout must not be negative", + Current: []types.KV{{Key: "dns.challenge.propagation_timeout", Value: timeout.String()}}, + Hint: "use 0 to skip waiting for the record on the zone's nameservers", + }) + } + + enabled := c.DNSConfig.Certificates.Enabled + challengeType := c.TLS.ACME.ChallengeType + ownDNS01 := c.TLS.ACME.Hostname != "" && challengeType == types.DNS01ChallengeType + + if name, ok := ch.ProviderNameText(); (enabled || ownDNS01) && ok && name == "" && ch.Provider == nil { + v.Add(&types.ConfigError{ + Reason: "dns.certificates and tls.acme DNS-01 require dns.challenge.provider", + Current: []types.KV{{Key: "dns.certificates.enabled", Value: enabled}, {Key: "tls.acme.challenge_type", Value: string(challengeType)}}, + Allowed: dns.ProviderNames(), + Hint: "configure the DNS provider that hosts dns.base_domain or tls.acme.hostname under dns.challenge.provider", + See: dnsDocsURL, + }) + } + + if !enabled { + return + } + + if !c.DNSConfig.MagicDNS { + v.Add(&types.ConfigError{ + Reason: "dns.certificates requires dns.magic_dns", + Current: []types.KV{{Key: "dns.certificates.enabled", Value: true}, {Key: "dns.magic_dns", Value: false}}, + Hint: "certificates are issued for MagicDNS names; set dns.magic_dns: true", + See: dnsDocsURL, + }) + } + + base := strings.TrimSuffix(c.DNSConfig.BaseDomain, ".") + if base != strings.ToLower(base) || dnsname.ValidHostname(base) != nil { + v.Add(&types.ConfigError{ + Reason: "dns.base_domain must be a lowercase DNS name for dns.certificates", + Current: []types.KV{{Key: "dns.base_domain", Value: base}}, + Hint: "Tailscale clients match certificate names exactly; use a lowercase public domain you control", + See: dnsDocsURL, + }) + } + + zone := strings.TrimSuffix(c.DNSConfig.Certificates.Zone, ".") + if zone != "" && base != zone && !strings.HasSuffix(base, "."+zone) { + v.Add(&types.ConfigError{ + Reason: "dns.base_domain is not inside dns.certificates.zone", + Current: []types.KV{{Key: "dns.base_domain", Value: base}, {Key: "dns.certificates.zone", Value: zone}}, + Hint: "set zone to the DNS zone that contains base_domain, or leave it empty to use base_domain", + See: dnsDocsURL, + }) + } +} + +// validatePrefixes records ConfigErrors for a catch-all trusted proxy, no +// node range, and no allocation strategy. +func validatePrefixes(c *types.Config, 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(&types.ConfigError{ + Reason: "trusted_proxies contains an invalid CIDR", + Current: []types.KV{{Key: "trusted_proxies", Value: c.TrustedProxies}}, + Detail: err.Error(), + Hint: "use specific proxy CIDRs; catch-all ranges are not allowed", + Cause: err, + }) + + break + } + } + + if c.Prefixes.V4 == nil && c.Prefixes.V6 == nil { + v.Add(&types.ConfigError{ + Reason: "no IP prefix configured for the tailnet", + Current: []types.KV{ + {Key: "prefixes.v4", Value: ""}, + {Key: "prefixes.v6", Value: ""}, + }, + Hint: `set at least one of prefixes.v4 (default: "100.64.0.0/10") or prefixes.v6 (default: "fd7a:115c:a1e0::/48")`, + Cause: types.ErrNoPrefixConfigured, + }) + } + + switch alloc := c.Prefixes.Allocation; alloc { + case types.IPAllocationStrategySequential, types.IPAllocationStrategyRandom: + default: + v.Add(&types.ConfigError{ + Reason: "prefixes.allocation has an unsupported value", + Current: []types.KV{{Key: "prefixes.allocation", Value: string(alloc)}}, + Allowed: alloc.Allowed(), + Hint: "pick one of the allowed values; sequential is the default", + Cause: types.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 validateDNSProvider(c *types.Config, v *configValidator) { + ch := c.DNSConfig.Challenge + if ch.Provider != nil { + return + } + + name, ok := ch.ProviderNameText() + if !ok { + v.Add(&types.ConfigError{ + Reason: "dns.challenge.provider.name must be a provider name", + Allowed: dns.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.(dns.Isolator); ok && err == nil && c.DNSConfig.Certificates.Enabled { + err = iso.CheckIsolation() + if err != nil { + err = fmt.Errorf("%w for %s: %w", dns.ErrProviderConfig, ch.ProviderName(), err) + } + } + + if err == nil { + return + } + + // The provider names only help when the name is what is wrong. + var allowed []string + if errors.Is(err, dns.ErrUnknownProvider) { + allowed = dns.ProviderNames() + } + + v.Add(&types.ConfigError{ + Reason: "dns.challenge.provider is invalid", + Current: []types.KV{{Key: "dns.challenge.provider.name", Value: 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, + }) +} + +// Warnings are advisories about c that do not stop headscale. +func Warnings(c *types.Config) []string { + var warnings []string + + if c.TLS.ACME.Hostname != "" && c.TLS.ACME.ChallengeType == types.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()...) +} + +// outsideRange reports whether p holds addresses outside r. +func outsideRange(p, r netip.Prefix) bool { + return p.Bits() < r.Bits() || !r.Contains(p.Addr()) +} + +func validatePKCEMethod(method types.PKCEMethod) error { + if method != types.PKCEMethodPlain && method != types.PKCEMethodS256 { + return errInvalidPKCEMethod + } + + return nil +} + +// validateOIDC 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 validateOIDC(o types.OIDCConfig) error { + err := validatePKCEMethod(o.PKCE.Method) + if err != nil { + return err + } + + 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, string(o.Issuer)) + } + + if o.ClientID == "" { + return errOIDCClientIDRequired + } + + if o.ClientSecret == "" { + return errOIDCClientSecretRequired + } + + return nil +} + +// listenersOverlap reports whether two parsed TCP listen addresses would +// compete for the same kernel socket. Mirrors kernel rules: +// - different ports → false +// - same port + a wildcard host on either side → true +// - same port + identical specific host → true +// - same port + different specific hosts → false +func listenersOverlap(aHost string, aPort int, bHost string, bPort int) bool { + if aPort != bPort { + return false + } + + if isWildcardHost(aHost) || isWildcardHost(bHost) { + return true + } + + return aHost == bHost +} + +func isWildcardHost(h string) bool { + switch h { + case "", "0.0.0.0", "::", "[::]": + return true + } + + return false +} + +// validateListenerCollisions records a *ConfigError for each pair of +// configured TCP listeners that would bind the same kernel socket. The +// ACME HTTP-01 challenge listener is only considered when a hostname is +// set and the HTTP-01 challenge is selected. +// +// Parsing happens up-front, once per listener: a malformed address +// 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(c *types.Config, v *configValidator) { + type spec struct { + key, addr string + host string + port int + parsed bool + active bool + } + + listenAddr := string(c.Addr) + metricsAddr := string(c.MetricsAddr) + acmeAddr := string(c.TLS.ACME.HTTPListen) + + listeners := []spec{ + {key: "listen_addr", addr: listenAddr, active: listenAddr != ""}, + {key: "metrics_listen_addr", addr: metricsAddr, active: metricsAddr != ""}, + { + key: "tls.acme.http_listen", + addr: acmeAddr, + active: acmeAddr != "" && + c.TLS.ACME.Hostname != "" && + c.TLS.ACME.ChallengeType == types.HTTP01ChallengeType, + }, + } + + for i := range listeners { + l := &listeners[i] + if !l.active { + continue + } + + port, err := types.PortFromAddr(l.addr) + if err != nil { + v.Add(&types.ConfigError{ + Reason: "cannot parse " + l.key, + Current: []types.KV{{Key: l.key, Value: l.addr}}, + Detail: err.Error(), + Hint: `use host:port form, e.g. "0.0.0.0:8080"`, + }) + + continue + } + + host, _, _ := net.SplitHostPort(l.addr) + l.host = host + l.port = port + l.parsed = true + } + + for i := range listeners { + for j := i + 1; j < len(listeners); j++ { + a, b := listeners[i], listeners[j] + if !a.parsed || !b.parsed { + continue + } + + if !listenersOverlap(a.host, a.port, b.host, b.port) { + continue + } + + v.Add(&types.ConfigError{ + Reason: fmt.Sprintf("%s and %s would bind the same TCP socket", a.key, b.key), + Current: []types.KV{{Key: a.key, Value: a.addr}}, + ConflictsWith: []types.KV{{Key: b.key, Value: b.addr}}, + Hint: "give each listener a distinct port, or bind them to different non-wildcard hosts", + See: "https://headscale.net/stable/ref/tls/", + }) + } + } +} + +// 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") + b.WriteString("################################################################\n") + b.WriteString("### __ __ _ _ ###\n") + b.WriteString("### \\ \\ / / (_) | | ###\n") + b.WriteString("### \\ \\ /\\ / /_ _ _ __ _ __ _ _ __ __ _| | ###\n") + b.WriteString("### \\ \\/ \\/ / _` | '__| '_ \\| | '_ \\ / _` | | ###\n") + b.WriteString("### \\ /\\ / (_| | | | | | | | | | | (_| |_| ###\n") + b.WriteString("### \\/ \\/ \\__,_|_| |_| |_|_|_| |_|\\__, (_) ###\n") + b.WriteString("### __/ | ###\n") + b.WriteString("### |___/ ###\n") + b.WriteString("################################################################\n") + b.WriteString("### ###\n") + + for _, line := range lines { + fmt.Fprintf(&b, "### %-54s ###\n", line) + } + + b.WriteString("### ###\n") + b.WriteString("################################################################") + + return b.String() +} + +// addServerURLConfigError pushes a structured ConfigError onto v for +// each isSafeServerURL failure mode so the operator sees the issue +// rendered consistently with the rest of the config-load report. +func addServerURLConfigError(v *configValidator, serverURL, baseDomain string, err error) { + current := []types.KV{ + {Key: "server_url", Value: serverURL}, + {Key: "dns.base_domain", Value: baseDomain}, + } + + switch { + case errors.Is(err, errServerURLSame): + v.Add(&types.ConfigError{ + Reason: "server_url and dns.base_domain refer to the same hostname", + Current: current, + Hint: "give server_url a host distinct from dns.base_domain (Tailscale takes over base_domain via MagicDNS)", + See: dnsDocsURL, + Cause: errServerURLSame, + }) + + case errors.Is(err, errServerURLSuffix): + v.Add(&types.ConfigError{ + Reason: "server_url is a subdomain of dns.base_domain", + Current: current, + Hint: "host headscale on a domain outside dns.base_domain (Tailscale takes over base_domain via MagicDNS)", + See: dnsDocsURL, + Cause: errServerURLSuffix, + }) + + default: + v.Add(&types.ConfigError{ + Reason: "server_url cannot be parsed", + Current: []types.KV{{Key: "server_url", Value: serverURL}}, + Detail: err.Error(), + Hint: "set server_url to an absolute URL, e.g. https://headscale.example.com", + Cause: err, + }) + } +} + +// BaseDomain cannot be a suffix of 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). +func isSafeServerURL(serverURL, baseDomain string) error { + server, err := url.Parse(serverURL) + if err != nil { + return err + } + + if server.Hostname() == baseDomain { + return errServerURLSame + } + + if strings.HasSuffix(server.Hostname(), "."+baseDomain) { + return errServerURLSuffix + } + + return nil +} + +// configValidator collects ConfigError values so an operator sees every +// problem at once instead of fixing them one startup attempt at a time. +// Zero value is ready to use. +type configValidator struct { + errs []error +} + +// Add records a structured rule violation. +func (v *configValidator) Add(e *types.ConfigError) { + v.errs = append(v.errs, e) +} + +// AddErr records any error (e.g. one returned by a sub-builder helper). +// Useful when wrapping fmt.Errorf("..."): %w results that already carry +// their own wrap. +func (v *configValidator) AddErr(err error) { + if err == nil { + return + } + + v.errs = append(v.errs, err) +} + +// HasErrors reports whether any rule has triggered. +func (v *configValidator) HasErrors() bool { return len(v.errs) > 0 } + +// Err returns nil if no rules triggered, otherwise the joined error. +// errors.Join's wrapper exposes Unwrap() []error, so errors.Is and +// errors.As walk every branch — sentinel matching and type extraction +// work uniformly across single and joined results. +func (v *configValidator) Err() error { + if len(v.errs) == 0 { + return nil + } + + return errors.Join(v.errs...) +} + +// keepSecret leaves a file: secret unread, for checking a config without +// reading the files it names; checks that need the secret skip it. +func keepSecret(_, value string) (string, error) { + if strings.HasPrefix(value, "file:") { + return value, dns.ErrSecretUnread + } + + return value, nil +} diff --git a/hscontrol/config/validate_test.go b/hscontrol/config/validate_test.go new file mode 100644 index 000000000..2acf5c0b7 --- /dev/null +++ b/hscontrol/config/validate_test.go @@ -0,0 +1,263 @@ +package config + +import ( + "errors" + "fmt" + "testing" + + "github.com/juanfont/headscale/hscontrol/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var errTestSentinel = errors.New("sentinel") + +// OK +// server_url: headscale.com, base: clients.headscale.com +// server_url: headscale.com, base: headscale.net +// +// NOT OK +// server_url: server.headscale.com, base: headscale.com. +func TestSafeServerURL(t *testing.T) { + tests := []struct { + serverURL, baseDomain, + wantErr string + }{ + { + serverURL: "https://example.com", + baseDomain: "example.org", + }, + { + serverURL: "https://headscale.com", + baseDomain: "headscale.com", + wantErr: errServerURLSame.Error(), + }, + { + serverURL: "https://headscale.com", + baseDomain: "clients.headscale.com", + }, + { + serverURL: "https://headscale.com", + baseDomain: "clients.subdomain.headscale.com", + }, + { + serverURL: "https://headscale.kristoffer.com", + baseDomain: "mybase", + }, + { + serverURL: "https://server.headscale.com", + baseDomain: "headscale.com", + wantErr: errServerURLSuffix.Error(), + }, + { + serverURL: "https://server.subdomain.headscale.com", + baseDomain: "headscale.com", + wantErr: errServerURLSuffix.Error(), + }, + { + serverURL: "http://foo\x00", + wantErr: `parse "http://foo\x00": net/url: invalid control character in URL`, + }, + } + + for _, tt := range tests { + testName := fmt.Sprintf("server=%s domain=%s", tt.serverURL, tt.baseDomain) + t.Run(testName, func(t *testing.T) { + err := isSafeServerURL(tt.serverURL, tt.baseDomain) + if tt.wantErr != "" { + assert.EqualError(t, err, tt.wantErr) + + return + } + + assert.NoError(t, err) + }) + } +} + +func TestSafeServerURLWithPort(t *testing.T) { + tests := []struct { + serverURL, baseDomain, + wantErr string + }{ + { + serverURL: "https://server.headscale.com:443", + baseDomain: "headscale.com", + wantErr: errServerURLSuffix.Error(), + }, + { + serverURL: "https://server.subdomain.headscale.com:8080", + baseDomain: "headscale.com", + wantErr: errServerURLSuffix.Error(), + }, + { + serverURL: "https://headscale.com:443", + baseDomain: "headscale.com", + wantErr: errServerURLSame.Error(), + }, + { + serverURL: "https://example.com:8080", + baseDomain: "example.org", + }, + } + + for _, tt := range tests { + testName := fmt.Sprintf("server=%s domain=%s", tt.serverURL, tt.baseDomain) + t.Run(testName, func(t *testing.T) { + err := isSafeServerURL(tt.serverURL, tt.baseDomain) + if tt.wantErr != "" { + assert.EqualError(t, err, tt.wantErr) + + return + } + + assert.NoError(t, err) + }) + } +} + +// TestNameserverWarningsHideCredentials: a nameserver that does not parse +// is reported without the userinfo it may hold. +func TestNameserverWarningsHideCredentials(t *testing.T) { + cfg := &types.Config{DNSConfig: types.DNSConfig{Nameservers: types.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 := Warnings(cfg) + require.Len(t, warnings, 2) + + for _, w := range warnings { + assert.Contains(t, w, "invalid nameserver") + assert.NotContains(t, w, "marker") + } +} + +func TestListenersOverlap(t *testing.T) { + tests := []struct { + name string + aHost string + aPort int + bHost string + bPort int + wantOverlap bool + }{ + {"different-ports", "", 80, "", 443, false}, + {"same-port-wildcard", "", 80, "", 80, true}, + {"wildcard-vs-loopback-same-port", "0.0.0.0", 80, "127.0.0.1", 80, true}, + {"loopback-vs-wildcard-same-port", "127.0.0.1", 80, "0.0.0.0", 80, true}, + {"ipv6-wildcard-vs-numeric", "[::]", 80, "0.0.0.0", 80, true}, + {"different-specific-hosts-same-port", "192.168.1.1", 80, "192.168.1.2", 80, false}, + {"same-specific-host-same-port", "127.0.0.1", 80, "127.0.0.1", 80, true}, + {"same-specific-host-different-port", "127.0.0.1", 80, "127.0.0.1", 81, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := listenersOverlap(tt.aHost, tt.aPort, tt.bHost, tt.bPort) + assert.Equal(t, tt.wantOverlap, got) + }) + } +} + +// TestValidateListenerCollisions_BlamesParseFailure pins each parse +// error to the listener whose address is malformed, even when paired +// with a well-formed sibling. +func TestValidateListenerCollisions_BlamesParseFailure(t *testing.T) { + tests := []struct { + name string + bad string // YAML key whose addr is malformed + good string // sibling key with a valid addr + badAddr string + }{ + {"bad-listen_addr", "listen_addr", "metrics_listen_addr", "garbage"}, + {"bad-metrics_listen_addr", "metrics_listen_addr", "listen_addr", "no-port"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Decoding rejects these; Go data reaches the rule as is. + cfg := &types.Config{} + for _, f := range types.ConfigFields(cfg) { + switch f.Path { + case tt.bad: + f.Value.SetString(tt.badAddr) + case tt.good: + f.Value.SetString(":9999") + } + } + + v := &configValidator{} + validateListenerCollisions(cfg, v) + + err := v.Err() + require.Error(t, err) + + errs := types.ConfigErrors(err) + require.Len(t, errs, 1, "expected exactly one parse error") + assert.Equal(t, "cannot parse "+tt.bad, errs[0].Reason) + require.Len(t, errs[0].Current, 1) + assert.Equal(t, tt.bad, errs[0].Current[0].Key) + assert.Equal(t, tt.badAddr, errs[0].Current[0].Value) + }) + } +} + +func TestIsWildcardHost(t *testing.T) { + wildcards := []string{"", "0.0.0.0", "::", "[::]"} + for _, h := range wildcards { + t.Run("wildcard-"+h, func(t *testing.T) { + assert.True(t, isWildcardHost(h)) + }) + } + + specifics := []string{"127.0.0.1", "192.168.1.1", "::1", "example.com"} + for _, h := range specifics { + t.Run("specific-"+h, func(t *testing.T) { + assert.False(t, isWildcardHost(h)) + }) + } +} + +func TestConfigValidator_NilWhenEmpty(t *testing.T) { + v := &configValidator{} + assert.False(t, v.HasErrors()) + assert.NoError(t, v.Err()) +} + +func TestConfigValidator_JoinsWithBlankLine(t *testing.T) { + v := &configValidator{} + v.Add(&types.ConfigError{Reason: "first"}) + v.Add(&types.ConfigError{Reason: "second"}) + + want := "Fatal config error: first\n" + + "\n" + + "Fatal config error: second\n" + assert.Equal(t, want, v.Err().Error()) +} + +func TestConfigValidator_JoinedErrorsIs(t *testing.T) { + v := &configValidator{} + v.Add(&types.ConfigError{Reason: "first"}) + v.Add(&types.ConfigError{Reason: "second", Cause: errTestSentinel}) + err := v.Err() + + require.ErrorIs(t, err, types.ErrConfig) + require.ErrorIs(t, err, errTestSentinel) +} + +func TestConfigValidator_JoinedErrorsAs(t *testing.T) { + v := &configValidator{} + v.Add(&types.ConfigError{Reason: "first"}) + v.Add(&types.ConfigError{Reason: "second"}) + err := v.Err() + + var got *types.ConfigError + require.ErrorAs(t, err, &got) + assert.Equal(t, "first", got.Reason) +} + +func TestConfigValidator_AddErrSkipsNil(t *testing.T) { + v := &configValidator{} + v.AddErr(nil) + assert.False(t, v.HasErrors()) + assert.NoError(t, v.Err()) +} diff --git a/hscontrol/db/db_test.go b/hscontrol/db/db_test.go index 0b4016560..f3ab078df 100644 --- a/hscontrol/db/db_test.go +++ b/hscontrol/db/db_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/juanfont/headscale/hscontrol/config" "github.com/juanfont/headscale/hscontrol/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -38,11 +39,10 @@ database: sqlite: path: headscale.db `), 0o600)) - require.NoError(t, types.LoadConfig(configPath, true)) - - cfg, err := types.LoadServerConfig() + l, err := config.Load(configPath, true, os.Environ()) require.NoError(t, err) - hsdb, err := NewHeadscaleDatabase(cfg) + require.NoError(t, config.Validate(l.Config)) + hsdb, err := NewHeadscaleDatabase(l.Config) require.NoError(t, err) t.Cleanup(func() { require.NoError(t, hsdb.Close()) }) diff --git a/hscontrol/debug.go b/hscontrol/debug.go index 5c63329b2..ee6810183 100644 --- a/hscontrol/debug.go +++ b/hscontrol/debug.go @@ -14,6 +14,7 @@ import ( "time" "github.com/arl/statsviz" + "github.com/juanfont/headscale/hscontrol/config" "github.com/juanfont/headscale/hscontrol/templates" "github.com/juanfont/headscale/hscontrol/types" "github.com/juanfont/headscale/hscontrol/types/change" @@ -139,7 +140,15 @@ func (h *Headscale) debugHTTPServer() *http.Server { // Configuration endpoint debug.Handle("config", "Current configuration", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - writeJSON(w, h.state.DebugConfig()) + out, err := config.Marshal(h.cfg, ".json") + if err != nil { + httpError(w, err) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write(out) })) // Policy endpoint diff --git a/hscontrol/state/debug.go b/hscontrol/state/debug.go index 14e4bc88b..c636e32f6 100644 --- a/hscontrol/state/debug.go +++ b/hscontrol/state/debug.go @@ -188,11 +188,6 @@ func (s *State) DebugRegistrationCache() map[string]any { } } -// DebugConfig returns debug information about the current configuration. -func (s *State) DebugConfig() *types.Config { - return s.cfg -} - // DebugPolicy returns the current policy data as a string. func (s *State) DebugPolicy() (string, error) { switch s.cfg.Policy.Mode { diff --git a/hscontrol/state/state.go b/hscontrol/state/state.go index 29d1d0097..5d42aa611 100644 --- a/hscontrol/state/state.go +++ b/hscontrol/state/state.go @@ -441,7 +441,7 @@ func NewState(cfg *types.Config) (*State, error) { } // Apply defaults for [NodeStore] batch configuration if not set. - // This ensures tests that create Config directly (without LoadConfig) still work. + // This ensures tests that create Config directly (without config.Load) still work. batchSize := cmp.Or(cfg.Tuning.NodeStoreBatchSize, defaultNodeStoreBatchSize) batchTimeout := cmp.Or(cfg.Tuning.NodeStoreBatchTimeout, defaultNodeStoreBatchTimeout) diff --git a/hscontrol/types/config.go b/hscontrol/types/config.go index f167f998a..e42b1abe7 100644 --- a/hscontrol/types/config.go +++ b/hscontrol/types/config.go @@ -9,8 +9,6 @@ import ( "net" "net/netip" "net/url" - "os" - "path/filepath" "reflect" "slices" "strconv" @@ -18,12 +16,8 @@ import ( "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/rs/zerolog" - "github.com/rs/zerolog/log" "tailscale.com/net/tsaddr" "tailscale.com/tailcfg" "tailscale.com/types/dnstype" @@ -34,23 +28,14 @@ const ( PKCEMethodPlain PKCEMethod = "plain" PKCEMethodS256 PKCEMethod = "S256" - defaultNodeStoreBatchSize = 100 - - dnsDocsURL = "https://headscale.net/stable/ref/dns/" + providerKey = "dns.challenge.provider" ) var ( - errOIDCIssuerInvalid = errors.New("oidc.issuer must be a valid http(s) URL") - errOIDCClientIDRequired = errors.New("oidc.client_id is required when oidc.issuer is set") - errOIDCClientSecretRequired = errors.New("oidc.client_secret is required when oidc.issuer is set") - errServerURLSuffix = errors.New("server_url cannot be part of base_domain in a way that could make the DERP and headscale server unreachable") - errServerURLSame = errors.New("server_url cannot use the same domain as base_domain in a way that could make the DERP and headscale server unreachable") errInvalidPKCEMethod = errors.New("pkce.method must be either 'plain' or 'S256'") - errTrustedProxyZeroRange = errors.New("0.0.0.0/0 and ::/0 are not allowed") errURLSyntax = errors.New("invalid URL syntax") ErrNoPrefixConfigured = errors.New("no IPv4 or IPv6 prefix configured, minimum one prefix is required") ErrInvalidAllocationStrategy = errors.New("invalid prefix allocation strategy") - errConfigNotLoaded = errors.New("configuration not loaded: call types.LoadConfig first") ) // defaultChallengeResolvers find a zone's nameservers when @@ -222,21 +207,21 @@ type DNSChallengeConfig struct { // ProviderName is the provider ProviderSettings names, or "". func (c DNSChallengeConfig) ProviderName() string { - name, _ := c.providerName() + name, _ := c.ProviderNameText() return name } -// providerName is the name as text, and false when it is a map or list: +// ProviderNameText 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) { +func (c DNSChallengeConfig) ProviderNameText() (string, bool) { switch name := c.ProviderSettings["name"].(type) { case nil: return "", true case string: return name, true default: - return scalarText(reflect.ValueOf(name)) + return util.ScalarText(reflect.ValueOf(name)) } } @@ -544,41 +529,6 @@ func (t Tuning) BatcherWorkerCount() int { return DefaultBatcherWorkers() } -func validatePKCEMethod(method PKCEMethod) error { - if method != PKCEMethodPlain && method != PKCEMethodS256 { - return errInvalidPKCEMethod - } - - return nil -} - -// 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 (o OIDCConfig) validate() error { - err := validatePKCEMethod(o.PKCE.Method) - if err != nil { - return err - } - - 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, string(o.Issuer)) - } - - if o.ClientID == "" { - return errOIDCClientIDRequired - } - - if o.ClientSecret == "" { - return errOIDCClientSecretRequired - } - - return nil -} - // Domain returns the hostname/domain part of the [Config.ServerURL]. // If the [Config.ServerURL] is not a valid URL, it returns dns.base_domain. func (c *Config) Domain() string { @@ -690,615 +640,14 @@ func (c *Config) ServesRoute(p netip.Prefix) bool { (c.Prefixes.V6 == nil || !c.Prefixes.V6.Contains(addr)) } -// src is the configuration [LoadConfig] loaded. -var src *configSource - -// configFile is the file [LoadConfig] last loaded or failed to load. -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 decode the -// values. -func LoadConfig(path string, isFile bool) error { - configFile = findConfigFile(path, isFile, os.Environ()) - - s, err := newConfigSource(path, isFile, os.Environ()) - if err != nil { - return err - } - - for _, w := range s.Warnings() { - log.Warn().Msg(w) - } - - src = s - - return nil -} - -// 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, 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. -// 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) { - if src == nil { - return nil, errConfigNotLoaded - } - - return src.k.Marshal(yaml.Parser()) -} - -// 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, - }, - } -} - -// 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{} - - // 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 c.OIDC.Issuer != "" { - err := c.OIDC.validate() - if err != nil { - v.Add(&ConfigError{ - Reason: "OIDC configuration is invalid", - Detail: err.Error(), - Hint: "check oidc.issuer, oidc.client_id, oidc.client_secret, and oidc.pkce.method", - Cause: err, - }) - } - } - - 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", c.DNSConfig.ExtraRecordsPath}, - {"dns.extra_records", ""}, - }, - Hint: "keep one (a path is recommended for production); remove the other", - }) - } - - 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", acme.Hostname}}, - ConflictsWith: []KV{ - {"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 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)", - See: "https://headscale.net/stable/setup/install/official/", - }) - } - - c.validateListenerCollisions(v) - - if ct := acme.ChallengeType; ct != HTTP01ChallengeType && - ct != TLSALPN01ChallengeType && ct != DNS01ChallengeType { - v.Add(&ConfigError{ - Reason: "tls.acme.challenge_type has an unsupported value", - Current: []KV{{"tls.acme.challenge_type", string(ct)}}, - Allowed: ct.Allowed(), - Hint: "pick one of the allowed values; HTTP-01 is the default", - }) - } - - 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", c.ServerURL}}, - Hint: "prefix the URL with https:// (recommended) or http://", - }) - } - - // Minimum inactivity time out is keepalive timeout (60s) plus a few seconds - // to avoid races - minInactivityTimeout := 65 * time.Second - - 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()}}, - Minimum: minInactivityTimeout.String(), - Hint: "raise the value above the keepalive interval (60s) plus a safety margin", - }) - } - - 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 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", ha.ProbeInterval.String()}}, - Minimum: "2s", - Hint: "raise the value to at least the minimum", - }) - } - - 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", ha.ProbeTimeout.String()}}, - Minimum: "1s", - Hint: "raise the value to at least the minimum", - }) - } - - 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", 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)", - }) - } - } - - // Validate tuning parameters - 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}}, - Hint: fmt.Sprintf("set to a positive integer (default: %d)", defaultNodeStoreBatchSize), - }) - } - - 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()}}, - Hint: "set to a positive duration (default: 500ms)", - }) - } - - 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() -} - -// validatePolicy records a ConfigError for an unknown policy.mode: -// startup would load no policy at all and allow all traffic. -func (c *Config) validatePolicy(v *configValidator) { - m := c.Policy.Mode - if m == PolicyModeFile || m == PolicyModeDB { - return - } - - v.Add(&ConfigError{ - Reason: "policy.mode has an unsupported value", - Current: []KV{{"policy.mode", string(m)}}, - Allowed: m.Allowed(), - See: "https://headscale.net/stable/ref/policy/", - }) -} - -// 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 server.STUNAddr == "" { - v.Add(&ConfigError{ - Reason: "derp.server.stun_listen_addr is required when the embedded DERP server is enabled", - Current: []KV{ - {"derp.server.enabled", true}, - {"derp.server.stun_listen_addr", ""}, - }, - Hint: `set derp.server.stun_listen_addr (e.g. "0.0.0.0:3478"), or set derp.server.enabled: false`, - See: "https://headscale.net/stable/ref/derp/", - }) - } - - derpKey, noiseKey := server.PrivateKeyPath, c.Noise.PrivateKeyPath - switch { - case derpKey == "": - v.Add(&ConfigError{ - Reason: "derp.server.private_key_path is required when the embedded DERP server is enabled", - Current: []KV{ - {"derp.server.enabled", true}, - {"derp.server.private_key_path", ""}, - }, - Hint: `set derp.server.private_key_path (e.g. "/var/lib/headscale/derp_server_private.key"), or set derp.server.enabled: false`, - See: "https://headscale.net/stable/ref/derp/", - }) - case filepath.Clean(derpKey) == filepath.Clean(noiseKey): - // Startup would create one key and then refuse it for both uses. - v.Add(&ConfigError{ - Reason: "derp.server.private_key_path must differ from noise.private_key_path", - Current: []KV{ - {"derp.server.private_key_path", derpKey}, - {"noise.private_key_path", noiseKey}, - }, - Hint: `set derp.server.private_key_path to its own file (e.g. "/var/lib/headscale/derp_server_private.key")`, - See: "https://headscale.net/stable/ref/derp/", - }) - } - - 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{ - {"derp.server.automatically_add_embedded_derp_region", false}, - {"derp.paths", "[]"}, - }, - Hint: "list at least one DERP map file (.yaml, .yml, .json or .hujson) in derp.paths, or set automatically_add_embedded_derp_region: true", - }) - } -} - -// 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 - } - - v.Add(&ConfigError{ - Reason: "database.type has an unsupported value", - Current: []KV{{"database.type", string(t)}}, - Allowed: t.Allowed(), - Hint: "pick one of the allowed values; sqlite is the default for single-host deployments", - See: "https://headscale.net/stable/ref/database/", - }) -} - -// 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{ - {"dns.magic_dns", true}, - {"dns.base_domain", ""}, - }, - Hint: `set dns.base_domain to a domain you control (e.g. "ts.example.net"), or set dns.magic_dns: false`, - See: dnsDocsURL, - }) - } -} - -// 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()}}, - Hint: "use 0 to skip waiting for the record on the zone's nameservers", - }) - } - - enabled := c.DNSConfig.Certificates.Enabled - challengeType := c.TLS.ACME.ChallengeType - ownDNS01 := c.TLS.ACME.Hostname != "" && challengeType == DNS01ChallengeType - - 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)}}, - Allowed: hsdns.ProviderNames(), - Hint: "configure the DNS provider that hosts dns.base_domain or tls.acme.hostname under dns.challenge.provider", - See: dnsDocsURL, - }) - } - - if !enabled { - return - } - - if !c.DNSConfig.MagicDNS { - v.Add(&ConfigError{ - Reason: "dns.certificates requires dns.magic_dns", - Current: []KV{{"dns.certificates.enabled", true}, {"dns.magic_dns", false}}, - Hint: "certificates are issued for MagicDNS names; set dns.magic_dns: true", - See: dnsDocsURL, - }) - } - - 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", - Current: []KV{{"dns.base_domain", base}}, - Hint: "Tailscale clients match certificate names exactly; use a lowercase public domain you control", - See: dnsDocsURL, - }) - } - - 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", - Current: []KV{{"dns.base_domain", base}, {"dns.certificates.zone", zone}}, - Hint: "set zone to the DNS zone that contains base_domain, or leave it empty to use base_domain", - See: dnsDocsURL, - }) - } -} - -// 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 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, - }) - } - - 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 && err == nil && c.DNSConfig.Certificates.Enabled { - err = iso.CheckIsolation() - if err != nil { - err = fmt.Errorf("%w for %s: %w", hsdns.ErrProviderConfig, ch.ProviderName(), err) - } - } - - 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, - }) -} - -// 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()...) -} - // 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 -// plain resolver, a URL a DoH one. Anything else is skipped; the loader -// warns about it, see [DNSConfig.nameserverWarnings]. +// plain resolver, a URL a DoH one. Anything else is skipped; +// [DNSConfig.NameserverWarnings] lists it for config.Warnings. func parseResolvers(nameservers []string) []*dnstype.Resolver { var resolvers []*dnstype.Resolver @@ -1318,9 +667,9 @@ func validNameserver(ns string) bool { return addrErr == nil || urlErr == nil } -// nameserverWarnings lists each nameserver parseResolvers skips, so the -// load that reads the config reports it rather than each build of it. -func (d DNSConfig) nameserverWarnings() []string { +// NameserverWarnings lists each nameserver parseResolvers skips, so +// config.Warnings reports it once rather than on each build. +func (d DNSConfig) NameserverWarnings() []string { var warnings []string check := func(key string, nameservers []string) { @@ -1422,146 +771,6 @@ func (c *Config) TailcfgDNSConfig() *tailcfg.DNSConfig { return cfg } -// 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") - b.WriteString("################################################################\n") - b.WriteString("### __ __ _ _ ###\n") - b.WriteString("### \\ \\ / / (_) | | ###\n") - b.WriteString("### \\ \\ /\\ / /_ _ _ __ _ __ _ _ __ __ _| | ###\n") - b.WriteString("### \\ \\/ \\/ / _` | '__| '_ \\| | '_ \\ / _` | | ###\n") - b.WriteString("### \\ /\\ / (_| | | | | | | | | | | (_| |_| ###\n") - b.WriteString("### \\/ \\/ \\__,_|_| |_| |_|_|_| |_|\\__, (_) ###\n") - b.WriteString("### __/ | ###\n") - b.WriteString("### |___/ ###\n") - b.WriteString("################################################################\n") - b.WriteString("### ###\n") - - for _, line := range lines { - fmt.Fprintf(&b, "### %-54s ###\n", line) - } - - b.WriteString("### ###\n") - b.WriteString("################################################################") - - return b.String() -} - -// 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 - } - - cfg, err := src.decode() - if err != nil { - return nil, err - } - - zerolog.SetGlobalLevel(cfg.Log.Level.Zerolog()) - - return cfg, nil -} - -// 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 - } - - cfg, err := src.decode() - if err != nil { - return nil, err - } - - zerolog.SetGlobalLevel(cfg.Log.Level.Zerolog()) - - err = cfg.Validate() - - for _, w := range cfg.Warnings() { - log.Warn().Msg(w) - } - - if err != nil { - return nil, err - } - - // The server never uses the CLI's key, which may be an unread file: - // reference. - cfg.CLI.APIKey = "" - - return cfg, nil -} - -// addServerURLConfigError pushes a structured ConfigError onto v for -// each isSafeServerURL failure mode so the operator sees the issue -// rendered consistently with the rest of the config-load report. -func addServerURLConfigError(v *configValidator, serverURL, baseDomain string, err error) { - current := []KV{ - {"server_url", serverURL}, - {"dns.base_domain", baseDomain}, - } - - switch { - case errors.Is(err, errServerURLSame): - v.Add(&ConfigError{ - Reason: "server_url and dns.base_domain refer to the same hostname", - Current: current, - Hint: "give server_url a host distinct from dns.base_domain (Tailscale takes over base_domain via MagicDNS)", - See: dnsDocsURL, - Cause: errServerURLSame, - }) - - case errors.Is(err, errServerURLSuffix): - v.Add(&ConfigError{ - Reason: "server_url is a subdomain of dns.base_domain", - Current: current, - Hint: "host headscale on a domain outside dns.base_domain (Tailscale takes over base_domain via MagicDNS)", - See: dnsDocsURL, - Cause: errServerURLSuffix, - }) - - default: - v.Add(&ConfigError{ - Reason: "server_url cannot be parsed", - Current: []KV{{"server_url", serverURL}}, - Detail: err.Error(), - Hint: "set server_url to an absolute URL, e.g. https://headscale.example.com", - Cause: err, - }) - } -} - -// BaseDomain cannot be a suffix of 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). -func isSafeServerURL(serverURL, baseDomain string) error { - server, err := url.Parse(serverURL) - if err != nil { - return err - } - - if server.Hostname() == baseDomain { - return errServerURLSame - } - - if strings.HasSuffix(server.Hostname(), "."+baseDomain) { - return errServerURLSuffix - } - - return nil -} - // StartDNS builds the DNS config clients get, from here on changed only by // [Config.SetExtraRecords], and keeps provider for [Config.DNSProvider]. // Call it before the config is shared. diff --git a/hscontrol/types/config_errors.go b/hscontrol/types/config_errors.go index dda2d8649..86fd4ce38 100644 --- a/hscontrol/types/config_errors.go +++ b/hscontrol/types/config_errors.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" "strings" + + "github.com/juanfont/headscale/hscontrol/util" ) // ErrConfig is a sentinel matching any *ConfigError. Callers can do @@ -127,44 +129,6 @@ func formatKVValue(v any) string { } } -// configValidator collects ConfigError values so an operator sees every -// problem at once instead of fixing them one startup attempt at a time. -// Zero value is ready to use. -type configValidator struct { - errs []error -} - -// Add records a structured rule violation. -func (v *configValidator) Add(e *ConfigError) { - v.errs = append(v.errs, e) -} - -// AddErr records any error (e.g. one returned by a sub-builder helper). -// Useful when wrapping fmt.Errorf("..."): %w results that already carry -// their own wrap. -func (v *configValidator) AddErr(err error) { - if err == nil { - return - } - - v.errs = append(v.errs, err) -} - -// HasErrors reports whether any rule has triggered. -func (v *configValidator) HasErrors() bool { return len(v.errs) > 0 } - -// Err returns nil if no rules triggered, otherwise the joined error. -// errors.Join's wrapper exposes Unwrap() []error, so errors.Is and -// errors.As walk every branch — sentinel matching and type extraction -// work uniformly across single and joined results. -func (v *configValidator) Err() error { - if len(v.errs) == 0 { - return nil - } - - return errors.Join(v.errs...) -} - // ConfigErrors walks an error tree (single Unwrap and multi-Unwrap) and // returns every *ConfigError found. Used by callers that need to // inspect every rule violation, not just the first. @@ -176,7 +140,7 @@ func (v *configValidator) Err() error { func ConfigErrors(err error) []*ConfigError { var out []*ConfigError - walkErrTree(err, func(e error) { + util.WalkErrTree(err, func(e error) { // Direct type assertion is intentional: errors.As walks the // Unwrap chain, which would double-count a *ConfigError once at // the wrapper and again at the wrapped node we recurse into. @@ -187,24 +151,3 @@ func ConfigErrors(err error) []*ConfigError { return out } - -func walkErrTree(err error, fn func(error)) { - if err == nil { - return - } - - fn(err) - - // Type-switch on err itself, not the chain: we want to know whether - // THIS error exposes Unwrap() error or Unwrap() []error so we can - // pick the right traversal. errors.As would jump past the head and - // confuse the walk (e.g. a join inside a single-Unwrap chain). - switch x := err.(type) { //nolint:errorlint // see comment above - case interface{ Unwrap() error }: - walkErrTree(x.Unwrap(), fn) - case interface{ Unwrap() []error }: - for _, b := range x.Unwrap() { - walkErrTree(b, fn) - } - } -} diff --git a/hscontrol/types/config_errors_test.go b/hscontrol/types/config_errors_test.go index f7a11ecc4..69fa672a6 100644 --- a/hscontrol/types/config_errors_test.go +++ b/hscontrol/types/config_errors_test.go @@ -9,10 +9,7 @@ import ( "github.com/stretchr/testify/require" ) -var ( - errTestCustomSentinel = errors.New("custom-sentinel") - errTestSentinel = errors.New("sentinel") -) +var errTestCustomSentinel = errors.New("custom-sentinel") func TestConfigError_Render(t *testing.T) { tests := []struct { @@ -115,58 +112,14 @@ func TestConfigError_As(t *testing.T) { assert.Equal(t, "test", got.Reason) } -func TestConfigValidator_NilWhenEmpty(t *testing.T) { - v := &configValidator{} - assert.False(t, v.HasErrors()) - assert.NoError(t, v.Err()) -} - -func TestConfigValidator_JoinsWithBlankLine(t *testing.T) { - v := &configValidator{} - v.Add(&ConfigError{Reason: "first"}) - v.Add(&ConfigError{Reason: "second"}) - - want := "Fatal config error: first\n" + - "\n" + - "Fatal config error: second\n" - assert.Equal(t, want, v.Err().Error()) -} - -func TestConfigValidator_JoinedErrorsIs(t *testing.T) { - v := &configValidator{} - v.Add(&ConfigError{Reason: "first"}) - v.Add(&ConfigError{Reason: "second", Cause: errTestSentinel}) - err := v.Err() - - require.ErrorIs(t, err, ErrConfig) - require.ErrorIs(t, err, errTestSentinel) -} - -func TestConfigValidator_JoinedErrorsAs(t *testing.T) { - v := &configValidator{} - v.Add(&ConfigError{Reason: "first"}) - v.Add(&ConfigError{Reason: "second"}) - err := v.Err() - - var got *ConfigError - require.ErrorAs(t, err, &got) - assert.Equal(t, "first", got.Reason) -} - -func TestConfigValidator_AddErrSkipsNil(t *testing.T) { - v := &configValidator{} - v.AddErr(nil) - assert.False(t, v.HasErrors()) - assert.NoError(t, v.Err()) -} - func TestConfigErrors_WalkAllBranches(t *testing.T) { - v := &configValidator{} - v.Add(&ConfigError{Reason: "first"}) - v.Add(&ConfigError{Reason: "second"}) - v.AddErr(fmt.Errorf("non-config: %w", &ConfigError{Reason: "third"})) + err := errors.Join( + &ConfigError{Reason: "first"}, + &ConfigError{Reason: "second"}, + fmt.Errorf("non-config: %w", &ConfigError{Reason: "third"}), + ) - got := ConfigErrors(v.Err()) + got := ConfigErrors(err) require.Len(t, got, 3) assert.Equal(t, "first", got[0].Reason) assert.Equal(t, "second", got[1].Reason) diff --git a/hscontrol/types/config_source_test.go b/hscontrol/types/config_source_test.go deleted file mode 100644 index a4b871b8a..000000000 --- a/hscontrol/types/config_source_test.go +++ /dev/null @@ -1,1016 +0,0 @@ -package types - -import ( - "cmp" - "fmt" - "math" - "net/netip" - "os" - "path/filepath" - "reflect" - "slices" - "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" -) - -// 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(), "config.yaml") - require.NoError(t, os.WriteFile(p, []byte(content), 0o600)) - - return p -} - -func TestConfigSource(t *testing.T) { - tests := []struct { - name string - file string // written as config.yaml unless fileName is set - fileName string - emptyFile bool - env []string - errs []string // substrings of the error - warns []string // substrings, one per expected warning - check func(t *testing.T, cfg *Config) - }{ - { - name: "unknown file key", - file: "server_url: http://x\ndns:\n magic_dsn: true\n", - errs: []string{"unknown configuration key dns.magic_dsn (", "config.yaml:3:3)"}, - }, - { - name: "removed file key", - file: "grpc_listen_addr: :50443\n", - errs: []string{"grpc_listen_addr", "removed"}, - }, - { - name: "removed file prefix", - file: "dns_config:\n magic_dns: true\n", - errs: []string{"dns_config", "removed", "dns"}, - }, - { - name: "all problems collected", - file: "grpc_listen_addr: x\nbogus: 1\n", - errs: []string{"grpc_listen_addr", "bogus"}, - }, - { - name: "removed env warns", - env: []string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT=30m"}, - warns: []string{"configuration variable HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT was removed in 0.30; use HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT"}, - }, - { - name: "unknown env warns", - env: []string{"HEADSCALE_SERVR_URL=x"}, - warns: []string{"unknown environment variable HEADSCALE_SERVR_URL"}, - }, - { - name: "reserved env is silent", - env: []string{ - "HEADSCALE_PORT=tcp://10.0.0.1:8080", "HEADSCALE_SERVICE_HOST=x", "HEADSCALE_DEBUG_FOO=1", - "HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN=x", "HEADSCALE_CONFIG=/x", "HEADSCALE_TEST_X=1", - "HEADSCALE_INTEGRATION_X=1", "HEADSCALE_UPDATE_GOLDEN=1", - }, - }, - { - // What Kubernetes sets for a Service named headscale-db. - name: "service variable named like a removed key warns", - env: []string{"HEADSCALE_DB_PORT=tcp://10.0.0.5:5432"}, - warns: []string{"configuration variable HEADSCALE_DB_PORT was removed in 0.30; use HEADSCALE_DATABASE_POSTGRES_PORT"}, - }, - { - 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, cfg *Config) { - t.Helper() - 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, cfg *Config) { - t.Helper() - assert.Equal(t, "https://x", cfg.ServerURL) - assert.Equal(t, - map[string][]string{"corp.example.com": {"10.0.0.1"}}, - cfg.DNSConfig.Nameservers.Split) - }, - }, - { - name: "null is unset, default stays", - file: "server_url:\nlog:\n level: ~\n", - check: func(t *testing.T, cfg *Config) { - t.Helper() - 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, cfg *Config) { - t.Helper() - assert.Equal(t, "http://env", cfg.ServerURL) - assert.Equal(t, ListenAddr("1.1.1.1:1"), cfg.Addr) - }, - }, - { - name: "env lists and maps", - env: []string{ - "HEADSCALE_DNS_NAMESERVERS_GLOBAL=1.1.1.1 8.8.8.8", - `HEADSCALE_OIDC_EXTRA_PARAMS={"domain_hint":"example.com"}`, - }, - check: func(t *testing.T, cfg *Config) { - t.Helper() - 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, cfg *Config) { - t.Helper() - assert.Equal(t, "cloudflare", cfg.DNSConfig.Challenge.ProviderName()) - assert.Equal(t, 1, cfg.DNSConfig.Challenge.ProviderSettings["whatever"]) - }, - }, - { - name: "yaml", - fileName: "config.yaml", - file: yamlSample, - check: checkSameValues, - }, - { - name: "yml", - fileName: "config.yml", - file: yamlSample, - check: checkSameValues, - }, - { - name: "json", - fileName: "config.json", - file: `{"server_url":"https://x","node":{"expiry":"1h"},"dns":{"magic_dns":false,"nameservers":{"global":["1.1.1.1","8.8.8.8"]}},"oidc":{"extra_params":{"domain_hint":"example.com"}}}`, - check: checkSameValues, - }, - { - name: "hujson", - fileName: "config.hujson", - file: "{\n // c\n \"server_url\":\"https://x\",\n \"node\":{\"expiry\":\"1h\",},\n \"dns\":{\"magic_dns\":false,\"nameservers\":{\"global\":[\"1.1.1.1\",\"8.8.8.8\",],},},\n \"oidc\":{\"extra_params\":{\"domain_hint\":\"example.com\"}},\n}", - check: checkSameValues, - }, - { - name: "hujson syntax error", - fileName: "config.hujson", - file: "{\n // c\n \"server_url\": x,\n}", - errs: []string{"config.hujson", "hujson: line 3, column 16"}, - }, - { - name: "toml is found and rejected", - fileName: "config.toml", - file: "server_url = \"https://x\"\n", - errs: []string{`unsupported config file format ".toml": use .json, .yaml, .yml or .hujson`, "config.toml)"}, - }, - { - name: ".env is found and rejected", - fileName: "config.env", - file: "SERVER_URL=https://x\n", - errs: []string{`unsupported config file format ".env": use .json, .yaml, .yml or .hujson`, "config.env)"}, - }, - { - name: ".dotenv is found and rejected", - fileName: "config.dotenv", - file: "SERVER_URL=https://x\n", - errs: []string{`unsupported config file format ".dotenv": use .json, .yaml, .yml or .hujson`, "config.dotenv)"}, - }, - { - name: "nested keys that share leaf names with removed keys", - file: "noise:\n private_key_path: /n\nderp:\n server:\n private_key_path: /d\nlog:\n level: debug\ntls:\n cert_path: /c\ndatabase:\n sqlite:\n path: /db\n", - check: func(t *testing.T, cfg *Config) { - t.Helper() - 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, cfg *Config) { - t.Helper() - 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) - }, - }, - { - name: "dotted file key in a removed prefix", - file: "dns_config.magic_dns: true\n", - errs: []string{"dns_config", "removed"}, - }, - { - name: "mapping under a plain setting", - file: "tuning:\n batcher_workers:\n bogus: 1\nserver_url:\n bogus: x\n", - errs: []string{"unknown configuration key tuning.batcher_workers.bogus", "unknown configuration key server_url.bogus"}, - }, - { - name: "non-string yaml keys are normalised", - file: "dns:\n nameservers:\n split:\n 123: [1.1.1.1]\n Corp.Example.com: [10.0.0.1]\n Empty.Example.com: ~\n", - check: func(t *testing.T, cfg *Config) { - t.Helper() - assert.Equal(t, - map[string][]string{"123": {"1.1.1.1"}, "corp.example.com": {"10.0.0.1"}}, - cfg.DNSConfig.Nameservers.Split) - }, - }, - { - name: "removed dns_config env warns", - env: []string{"HEADSCALE_DNS_CONFIG_MAGIC_DNS=false", "HEADSCALE_DNS_CONFIG_NAMESERVERS=1.1.1.1"}, - warns: []string{ - "variable HEADSCALE_DNS_CONFIG_MAGIC_DNS was removed in 0.30; use HEADSCALE_DNS_MAGIC_DNS", - "variable HEADSCALE_DNS_CONFIG_NAMESERVERS was removed in 0.30; use HEADSCALE_DNS_NAMESERVERS_GLOBAL", - }, - }, - { - name: "removed block env names the variables replacing it", - env: []string{`HEADSCALE_DNS_CONFIG={"magic_dns":true}`, "HEADSCALE_RANDOMIZE_CLIENT_PORT=true"}, - warns: []string{ - "variable HEADSCALE_DNS_CONFIG was removed in 0.30; use HEADSCALE_DNS_MAGIC_DNS, HEADSCALE_DNS_BASE_DOMAIN, " + - "HEADSCALE_DNS_OVERRIDE_LOCAL_DNS, HEADSCALE_DNS_NAMESERVERS_GLOBAL, HEADSCALE_DNS_NAMESERVERS_SPLIT, " + - "HEADSCALE_DNS_SEARCH_DOMAINS and HEADSCALE_DNS_EXTRA_RECORDS", - `variable HEADSCALE_RANDOMIZE_CLIENT_PORT was removed in 0.29; set "randomizeClientPort": true at the top level of the policy file`, - }, - }, - { - name: "exact reserved names do not exempt near misses", - env: []string{"HEADSCALE_CONFIG=/x", "HEADSCALE_UPDATE_GOLDEN=1", "HEADSCALE_DNS_CHALLENGE_PROVIDER=x", "HEADSCALE_CONFIG_TYPO=1", "HEADSCALE_UPDATE_GOLDEN_BOGUS=1"}, - warns: []string{"HEADSCALE_CONFIG_TYPO", "HEADSCALE_UPDATE_GOLDEN_BOGUS"}, - }, - { - name: "empty file", - emptyFile: true, - check: func(t *testing.T, cfg *Config) { - t.Helper() - assert.Equal(t, LogLevel("info"), cfg.Log.Level) - }, - }, - { - name: "unsupported format", - fileName: "config.ini", - file: "a=1\n", - errs: []string{"unsupported config file format"}, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dir := t.TempDir() - path, isFile := dir, false - - if tt.file != "" || tt.fileName != "" || tt.emptyFile { - name := cmp.Or(tt.fileName, "config.yaml") - require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(tt.file), 0o600)) - - // .ini is not searched for, so it has to be named. - if filepath.Ext(name) == ".ini" { - path, isFile = filepath.Join(dir, name), true - } - } - - s, err := newConfigSource(path, isFile, tt.env) - if len(tt.errs) > 0 { - require.Error(t, err) - - for _, want := range tt.errs { - require.ErrorContains(t, err, want) - } - - return - } - - require.NoError(t, err) - require.Len(t, s.Warnings(), len(tt.warns)) - - for i, w := range tt.warns { - assert.Contains(t, s.Warnings()[i], w) - } - - if tt.check != nil { - cfg, err := s.decode() - require.NoError(t, err) - tt.check(t, cfg) - } - }) - } -} - -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, cfg *Config) { - t.Helper() - 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) { - got := configSearchDirs([]string{"HOME=/h", "XDG_CONFIG_HOME=/xh", "XDG_CONFIG_DIRS=/a:/b"}) - assert.Equal(t, []string{"/etc/headscale", "/xh/headscale", "/a/headscale", "/b/headscale", "/h/.headscale", "."}, got) - - got = configSearchDirs([]string{"HOME=/h"}) - assert.Equal(t, []string{"/etc/headscale", "/h/.config/headscale", "/etc/xdg/headscale", "/h/.headscale", "."}, got) - - xdg := t.TempDir() - require.NoError(t, os.MkdirAll(filepath.Join(xdg, "headscale"), 0o700)) - require.NoError(t, os.WriteFile(filepath.Join(xdg, "headscale", "config.yaml"), nil, 0o600)) - assert.Equal(t, filepath.Join(xdg, "headscale", "config.yaml"), findConfigFile("", false, []string{"XDG_CONFIG_HOME=" + xdg})) -} - -func TestConfigSourceSearch(t *testing.T) { - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte("server_url: http://yaml\n"), 0o600)) - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.json"), []byte(`{"server_url":"http://json"}`), 0o600)) - - s, err := newConfigSource(dir, false, nil) - require.NoError(t, err) - assert.True(t, strings.HasSuffix(s.File(), "config.json")) - - 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()) -} - -// TestConfigSourceDottedOverlap reloads the file each time: map iteration -// order differs per load, so a single load could hide a nondeterministic result. -func TestConfigSourceDottedOverlap(t *testing.T) { - tests := []struct{ name, file, a, b string }{ - {"scalar", "log:\n level: info\nlog.level: debug\n", `"log.level"`, `"log > level"`}, - {"map shadow", "oidc:\n extra_params:\n a: nested\noidc.extra_params:\n b: dotted\n", `"oidc.extra_params"`, `"oidc > extra_params"`}, - {"split fallback", "dns:\n nameservers:\n split:\n a.example.com: [10.0.0.1]\ndns.nameservers:\n global: [1.1.1.1]\n", `"dns.nameservers"`, `"dns > nameservers"`}, - {"parent map and dotted child", "oidc.extra_params:\n prompt: login\noidc.extra_params.prompt: consent\n", `"oidc.extra_params"`, `"oidc.extra_params.prompt"`}, - {"open map entry twice", "dns:\n nameservers:\n split.corp.example.com: [10.0.0.1]\ndns.nameservers.split.corp.example.com: [10.0.0.2]\n", `"dns > nameservers > split.corp.example.com"`, `"dns.nameservers.split.corp.example.com"`}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(tt.file), 0o600)) - - for range 50 { - _, err := newConfigSource(dir, false, nil) - require.ErrorContains(t, err, "is set both as") - require.ErrorContains(t, err, tt.a) - require.ErrorContains(t, err, tt.b) - require.ErrorContains(t, err, "use one form") - } - }) - } -} - -func TestConfigSourceDottedKeyAlone(t *testing.T) { - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), - []byte("log.level: debug\ndns:\n magic_dns: false\ndns.nameservers.global: [1.1.1.1]\n"), 0o600)) - - for range 50 { - s, err := newConfigSource(dir, false, nil) - require.NoError(t, err) - - 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) - } -} - -// TestConfigSourceDottedNoOverlap loads files whose dotted keys only share a -// string prefix: below an open map the entry name is one key, dots included. -func TestConfigSourceDottedNoOverlap(t *testing.T) { - tests := []struct { - name, file string - check func(t *testing.T, 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, 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"}}, - 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, cfg *Config) { - t.Helper() - 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, 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"}}, - 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, cfg *Config) { - t.Helper() - assert.Equal(t, LogLevel("debug"), cfg.Log.Level) - assert.Equal(t, JSONLogFormat, cfg.Log.Format) - assert.True(t, cfg.LogTail.Enabled) - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(tt.file), 0o600)) - - for range 50 { - s, err := newConfigSource(dir, false, nil) - require.NoError(t, err) - - cfg, err := s.decode() - require.NoError(t, err) - tt.check(t, cfg) - } - }) - } -} - -// TestConfigSourceShapeErrorsHideValues: a block in the wrong shape may hold -// credentials, so the error names the key without its value. -func TestConfigSourceShapeErrorsHideValues(t *testing.T) { - for name, file := range map[string]string{ - "list under oidc": "oidc:\n - client_secret: marker-secret\n", - "scalar under postgres": "database:\n postgres: \"pass: marker-secret\"\n", - "scalar under oidc.pkce": "oidc:\n pkce: marker-secret\n", - "list under database.sqlite": "database:\n sqlite: [marker-secret]\n", - } { - t.Run(name, func(t *testing.T) { - _, err := newConfigSource(writeConfig(t, file), true, nil) - require.ErrorContains(t, err, "must be a mapping") - assert.NotContains(t, err.Error(), "marker-secret") - }) - } -} - -func TestConfigSourceStrictBeforeExpansion(t *testing.T) { - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte("log:\n format:\n typo: json\nlog.format: json\n"), 0o600)) - - for range 50 { - _, err := newConfigSource(dir, false, nil) - require.ErrorContains(t, err, "unknown configuration key log.format.typo") - } -} - -func TestConfigSourceProviderEnv(t *testing.T) { - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), - []byte("dns:\n challenge:\n provider:\n name: file\n other: x\n"), 0o600)) - - t.Setenv("HEADSCALE_DNS_CHALLENGE_PROVIDER", `{"api_token":"x"}`) - t.Setenv("HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME", "cloudflare") - - s, err := newConfigSource(dir, false, os.Environ()) - require.NoError(t, err) - assert.Empty(t, s.Warnings()) - - 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 := cfg.DNSConfig.Challenge.ProviderSettings - assert.Equal(t, "x", got["api_token"]) - assert.NotContains(t, got, "other") -} - -// TestProviderEnvFold pins that HEADSCALE_DNS_CHALLENGE_PROVIDER_ -// reaches the settings of the provider named anywhere, and that a variable -// no provider setting has is ignored without a word. -func TestProviderEnvFold(t *testing.T) { - 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 -` - - tests := []struct { - name string - yaml string - env map[string]string - want map[string]any - }{ - { - name: "named in the file", - yaml: base + " challenge:\n provider:\n name: cloudflare\n", - env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "x"}, - want: map[string]any{"name": "cloudflare", "api_token": "x"}, - }, - { - name: "named by its variable", - yaml: base, - env: map[string]string{ - "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", - "HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "x", - }, - want: map[string]any{"name": "cloudflare", "api_token": "x"}, - }, - { - name: "named in the json variable", - yaml: base, - env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"name":"cloudflare","api_token":"json"}`}, - want: map[string]any{"name": "cloudflare", "api_token": "json"}, - }, - { - name: "setting variable beats the json variable", - yaml: base, - env: map[string]string{ - "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"name":"cloudflare","api_token":"json"}`, - "HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "env", - }, - want: map[string]any{"name": "cloudflare", "api_token": "env"}, - }, - { - name: "name variable beats the json variable", - yaml: base + " challenge:\n provider:\n name: powerdns\n", - env: map[string]string{ - "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"name":"rfc2136","api_token":"json"}`, - "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", - }, - want: map[string]any{"name": "cloudflare", "api_token": "json"}, - }, - { - name: "variable for no setting of the provider", - yaml: base + " challenge:\n provider:\n name: cloudflare\n api_token: tok\n", - env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_BOGUS": "1"}, - want: map[string]any{"name": "cloudflare", "api_token": "tok"}, - }, - { - name: "setting variable without a provider", - yaml: base, - env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "x"}, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - for k, v := range tt.env { - t.Setenv(k, v) - } - - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(tt.yaml), 0o600)) - require.NoError(t, LoadConfig(dir, false)) - assert.Empty(t, src.Warnings()) - - cfg, err := LoadServerConfig() - require.NoError(t, err) - - if len(tt.want) == 0 { - assert.Empty(t, cfg.DNSConfig.Challenge.ProviderSettings) - - return - } - - assert.Equal(t, tt.want, cfg.DNSConfig.Challenge.ProviderSettings) - }) - } -} - -// 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 fe661bff9..88c985a95 100644 --- a/hscontrol/types/config_test.go +++ b/hscontrol/types/config_test.go @@ -3,1155 +3,26 @@ package types import ( "bytes" "encoding/json" - "fmt" - "io/fs" - "maps" "net/netip" "net/url" - "os" "path/filepath" - "strings" "testing" - "time" "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" - hsdns "github.com/juanfont/headscale/hscontrol/dns" "github.com/juanfont/headscale/hscontrol/types/testcapture" - "github.com/juanfont/headscale/hscontrol/util" - "github.com/knadh/koanf/providers/confmap" - "github.com/knadh/koanf/v2" "github.com/libdns/cloudflare" "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "tailscale.com/tailcfg" "tailscale.com/types/dnstype" ) -func TestReadConfig(t *testing.T) { - tests := []struct { - name string - configPath string - setup func(*testing.T) (any, error) - want any - wantErr string - }{ - { - name: "unmarshal-dns-full-config", - configPath: "testdata/dns_full.yaml", - setup: func(t *testing.T) (any, error) { //nolint:thelper - cfg, err := src.decode() - if err != nil { - return nil, err - } - - return cfg.DNSConfig, nil - }, - want: DNSConfig{ - MagicDNS: true, - BaseDomain: "example.com", - OverrideLocalDNS: false, - Nameservers: Nameservers{ - Global: []string{ - "1.1.1.1", - "1.0.0.1", - "2606:4700:4700::1111", - "2606:4700:4700::1001", - "https://dns.nextdns.io/abc123", - }, - Split: map[string][]string{ - "darp.headscale.net": {"1.1.1.1", "8.8.8.8"}, - "foo.bar.com": {"1.1.1.1"}, - }, - }, - ExtraRecords: []tailcfg.DNSRecord{ - {Name: "grafana.myvpn.example.com", Type: "A", Value: "100.64.0.3"}, - {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 - cfg, err := src.decode() - if err != nil { - return nil, err - } - - return dnsToTailcfgDNS(cfg.DNSConfig), nil - }, - want: &tailcfg.DNSConfig{ - Proxied: true, - Domains: []string{"example.com", "test.com", "bar.com"}, - FallbackResolvers: []*dnstype.Resolver{ - {Addr: "1.1.1.1"}, - {Addr: "1.0.0.1"}, - {Addr: "2606:4700:4700::1111"}, - {Addr: "2606:4700:4700::1001"}, - {Addr: "https://dns.nextdns.io/abc123"}, - }, - Routes: map[string][]*dnstype.Resolver{ - "darp.headscale.net": {{Addr: "1.1.1.1"}, {Addr: "8.8.8.8"}}, - "foo.bar.com": {{Addr: "1.1.1.1"}}, - }, - ExtraRecords: []tailcfg.DNSRecord{ - {Name: "grafana.myvpn.example.com", Type: "A", Value: "100.64.0.3"}, - {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, - }, - }, - }, - { - name: "unmarshal-dns-full-no-magic", - configPath: "testdata/dns_full_no_magic.yaml", - setup: func(t *testing.T) (any, error) { //nolint:thelper - cfg, err := src.decode() - if err != nil { - return nil, err - } - - return cfg.DNSConfig, nil - }, - want: DNSConfig{ - MagicDNS: false, - BaseDomain: "example.com", - OverrideLocalDNS: false, - Nameservers: Nameservers{ - Global: []string{ - "1.1.1.1", - "1.0.0.1", - "2606:4700:4700::1111", - "2606:4700:4700::1001", - "https://dns.nextdns.io/abc123", - }, - Split: map[string][]string{ - "darp.headscale.net": {"1.1.1.1", "8.8.8.8"}, - "foo.bar.com": {"1.1.1.1"}, - }, - }, - ExtraRecords: []tailcfg.DNSRecord{ - {Name: "grafana.myvpn.example.com", Type: "A", Value: "100.64.0.3"}, - {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 - cfg, err := src.decode() - if err != nil { - return nil, err - } - - return dnsToTailcfgDNS(cfg.DNSConfig), nil - }, - want: &tailcfg.DNSConfig{ - Proxied: false, - Domains: []string{"test.com", "bar.com"}, - FallbackResolvers: []*dnstype.Resolver{ - {Addr: "1.1.1.1"}, - {Addr: "1.0.0.1"}, - {Addr: "2606:4700:4700::1111"}, - {Addr: "2606:4700:4700::1001"}, - {Addr: "https://dns.nextdns.io/abc123"}, - }, - Routes: map[string][]*dnstype.Resolver{ - "darp.headscale.net": {{Addr: "1.1.1.1"}, {Addr: "8.8.8.8"}}, - "foo.bar.com": {{Addr: "1.1.1.1"}}, - }, - ExtraRecords: []tailcfg.DNSRecord{ - {Name: "grafana.myvpn.example.com", Type: "A", Value: "100.64.0.3"}, - {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, - }, - }, - }, - { - name: "base-domain-in-server-url-err", - configPath: "testdata/base-domain-in-server-url.yaml", - setup: func(t *testing.T) (any, error) { //nolint:thelper - return LoadServerConfig() - }, - want: nil, - wantErr: "server_url is a subdomain of dns.base_domain", - }, - { - name: "base-domain-not-in-server-url", - configPath: "testdata/base-domain-not-in-server-url.yaml", - setup: func(t *testing.T) (any, error) { //nolint:thelper - cfg, err := LoadServerConfig() - if err != nil { - return nil, err - } - - return map[string]string{ - "server_url": cfg.ServerURL, - "base_domain": cfg.DNSConfig.BaseDomain, - }, err - }, - want: map[string]string{ - "server_url": "https://derp.no", - "base_domain": "clients.derp.no", - }, - wantErr: "", - }, - { - name: "dns-override-true-errors", - configPath: "testdata/dns-override-true-error.yaml", - setup: func(t *testing.T) (any, error) { //nolint:thelper - return LoadServerConfig() - }, - wantErr: "Fatal config error: dns.nameservers.global is required when dns.override_local_dns is true", - }, - { - name: "dns-override-true", - configPath: "testdata/dns-override-true.yaml", - setup: func(t *testing.T) (any, error) { //nolint:thelper - cfg, err := LoadServerConfig() - if err != nil { - return nil, err - } - - return dnsToTailcfgDNS(cfg.DNSConfig), nil - }, - want: &tailcfg.DNSConfig{ - Proxied: true, - Domains: []string{"derp2.no"}, - Routes: map[string][]*dnstype.Resolver{}, - Resolvers: []*dnstype.Resolver{ - {Addr: "1.1.1.1"}, - {Addr: "1.0.0.1"}, - }, - }, - }, - { - name: "policy-path-is-loaded", - configPath: "testdata/policy-path-is-loaded-strict.yaml", - setup: func(t *testing.T) (any, error) { //nolint:thelper // inline test closure - cfg, err := LoadServerConfig() - if err != nil { - return nil, err - } - - return map[string]string{ - "policy.mode": string(cfg.Policy.Mode), - "policy.path": cfg.Policy.Path, - }, err - }, - want: map[string]string{ - "policy.mode": "file", - "policy.path": "/etc/policy.hujson", - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := LoadConfig(tt.configPath, true) - require.NoError(t, err) - - conf, err := tt.setup(t) - - if tt.wantErr != "" { - require.Error(t, err) - assert.Contains(t, err.Error(), tt.wantErr) - - return - } - - require.NoError(t, err) - - if diff := cmp.Diff(tt.want, conf); diff != "" { - t.Errorf("ReadConfig() mismatch (-want +got):\n%s", diff) - } - }) - } -} - -func TestReadConfigFromEnv(t *testing.T) { - tests := []struct { - name string - configEnv map[string]string - setup func(*testing.T) (any, error) - want any - }{ - { - name: "test-random-base-settings-with-env", - configEnv: map[string]string{ - "HEADSCALE_LOG_LEVEL": "trace", - "HEADSCALE_DATABASE_SQLITE_WRITE_AHEAD_LOG": "false", - "HEADSCALE_PREFIXES_V4": "100.64.0.0/10", - }, - setup: func(t *testing.T) (any, error) { //nolint:thelper // inline test closure - 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 - }, - want: nil, - }, - { - name: "unmarshal-dns-full-config", - configEnv: map[string]string{ - "HEADSCALE_DNS_MAGIC_DNS": "true", - "HEADSCALE_DNS_BASE_DOMAIN": "example.com", - "HEADSCALE_DNS_OVERRIDE_LOCAL_DNS": "false", - "HEADSCALE_DNS_NAMESERVERS_GLOBAL": `1.1.1.1 8.8.8.8`, - "HEADSCALE_DNS_SEARCH_DOMAINS": "test.com bar.com", - - // TODO(kradalby): Figure out how to pass these as env vars - // "HEADSCALE_DNS_NAMESERVERS_SPLIT": `{foo.bar.com: ["1.1.1.1"]}`, - // "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 - cfg, err := src.decode() - if err != nil { - return nil, err - } - - return cfg.DNSConfig, nil - }, - want: DNSConfig{ - MagicDNS: true, - BaseDomain: "example.com", - OverrideLocalDNS: false, - Nameservers: Nameservers{ - Global: []string{"1.1.1.1", "8.8.8.8"}, - Split: map[string][]string{ - // "foo.bar.com": {"1.1.1.1"}, - }, - }, - // ExtraRecords: []tailcfg.DNSRecord{ - // {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, - // }, - SearchDomains: []string{"test.com", "bar.com"}, - Challenge: DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - for k, v := range tt.configEnv { - t.Setenv(k, v) - } - - err := LoadConfig("testdata/minimal.yaml", true) - require.NoError(t, err) - - conf, err := tt.setup(t) - require.NoError(t, err) - - if diff := cmp.Diff(tt.want, conf, cmpopts.EquateEmpty()); diff != "" { - t.Errorf("ReadConfig() mismatch (-want +got):\n%s", diff) - } - }) - } -} - -func TestTLSConfigValidation(t *testing.T) { - tmpDir := t.TempDir() - - var err error - - configYaml := []byte(`--- -tls: - cert_path: abc.pem - acme: - hostname: example.com - challenge_type: "" -noise: - private_key_path: noise_private.key`) - - // Populate a custom config file - configFilePath := filepath.Join(tmpDir, "config.yaml") - - err = os.WriteFile(configFilePath, configYaml, 0o600) - if err != nil { - t.Fatalf("Couldn't write file %s", configFilePath) - } - - // Check configuration validation errors (1) - err = LoadConfig(tmpDir, false) - require.NoError(t, err) - - err = validateServerConfig() - require.Error(t, err) - assert.Contains( - t, - err.Error(), - "Fatal config error: tls.acme.hostname and tls.cert_path/tls.key_path are mutually exclusive", - ) - assert.Contains( - t, - err.Error(), - "Fatal config error: tls.acme.challenge_type has an unsupported value", - ) - assert.Contains( - t, - err.Error(), - "Fatal config error: server_url is missing a scheme", - ) - - // Check configuration validation errors (2) - configYaml = []byte(`--- -noise: - private_key_path: noise_private.key -server_url: http://127.0.0.1:8080 -tls: - acme: - hostname: example.com - challenge_type: TLS-ALPN-01 -`) - - err = os.WriteFile(configFilePath, configYaml, 0o600) - if err != nil { - t.Fatalf("Couldn't write file %s", configFilePath) - } - - err = LoadConfig(tmpDir, false) - require.NoError(t, err) -} - -func TestPKCEMethodValidation(t *testing.T) { - tmpDir := t.TempDir() - - // OIDC is active (issuer set) with PKCE enabled and an invalid method. - // There is no oidc.enabled key. validateServerConfig must reject the - // invalid method rather than silently skipping the check. - configYaml := []byte(`--- -noise: - private_key_path: noise_private.key -server_url: http://127.0.0.1:8080 -dns: - override_local_dns: false -oidc: - issuer: https://idp.example.com - client_id: headscale - pkce: - enabled: true - method: S256-typo -`) - - configFilePath := filepath.Join(tmpDir, "config.yaml") - err := os.WriteFile(configFilePath, configYaml, 0o600) - require.NoError(t, err) - - err = LoadConfig(tmpDir, false) - require.NoError(t, err) - - err = validateServerConfig() - 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 -// fail an unworkable OIDC setup fast at config load. -func TestOIDCConfigValidation(t *testing.T) { - tests := []struct { - name string - oidcBlock string - wantErr string - }{ - { - name: "non-http issuer", - oidcBlock: ` - issuer: ftp://idp.example.com - client_id: headscale - client_secret: sekret`, - wantErr: "oidc.issuer is not an http:// or https:// URL", - }, - { - name: "missing client_id", - oidcBlock: ` - issuer: https://idp.example.com - client_secret: sekret`, - wantErr: "client_id is required", - }, - { - name: "missing client_secret", - oidcBlock: ` - issuer: https://idp.example.com - client_id: headscale`, - wantErr: "client_secret", - }, - { - name: "valid", - oidcBlock: ` - issuer: https://idp.example.com - client_id: headscale - client_secret: sekret`, - wantErr: "", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - tmpDir := t.TempDir() - configYaml := []byte(`--- -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 -database: - type: sqlite -oidc:` + tt.oidcBlock + "\n") - - require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "config.yaml"), configYaml, 0o600)) - require.NoError(t, LoadConfig(tmpDir, false)) - - err := validateServerConfig() - if tt.wantErr == "" { - require.NoError(t, err) - - return - } - - require.Error(t, err) - assert.Contains(t, err.Error(), tt.wantErr) - }) - } -} - -func TestTLSACMEListenAddrCollision(t *testing.T) { - tests := []struct { - name string - listenAddr string - acmeListen string - hostname string - challengeType string - wantErr string // empty = expect no error - }{ - {"collision-numeric", ":80", ":80", "example.com", "HTTP-01", "would bind the same TCP socket"}, - {"collision-named-vs-numeric", "0.0.0.0:80", ":http", "example.com", "HTTP-01", "would bind the same TCP socket"}, - {"collision-https-named", ":443", ":https", "example.com", "HTTP-01", "would bind the same TCP socket"}, - {"canonical", "0.0.0.0:443", ":http", "example.com", "HTTP-01", ""}, - {"no-hostname-skipped", "0.0.0.0:80", ":http", "", "HTTP-01", ""}, - {"tls-alpn-01-skipped", "0.0.0.0:80", ":http", "example.com", "TLS-ALPN-01", ""}, - {"different-numeric", ":8080", ":8081", "example.com", "HTTP-01", ""}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - tmpDir := t.TempDir() - cfg := fmt.Sprintf(`--- -server_url: https://example.com -listen_addr: %q -tls: - acme: - hostname: %q - challenge_type: %q - http_listen: %q -noise: - private_key_path: noise_private.key -prefixes: - v4: 100.64.0.0/10 -database: - type: sqlite3 -dns: - magic_dns: false - override_local_dns: false -`, tt.listenAddr, tt.hostname, tt.challengeType, tt.acmeListen) - require.NoError(t, os.WriteFile( - filepath.Join(tmpDir, "config.yaml"), []byte(cfg), 0o600)) - require.NoError(t, LoadConfig(tmpDir, false)) - - err := validateServerConfig() - if tt.wantErr == "" { - require.NoError(t, err) - return - } - - require.Error(t, err) - assert.Contains(t, err.Error(), tt.wantErr) - }) - } -} - -func TestLoadServerConfig_CollectsAcrossSubBuilders(t *testing.T) { - tmpDir := t.TempDir() - cfg := `--- -server_url: https://example.com -listen_addr: 0.0.0.0:8080 -prefixes: - v4: not-a-cidr - v6: also-not-a-cidr - allocation: bogus -noise: - private_key_path: noise_private.key -database: - type: sqlite3 -dns: - magic_dns: false - override_local_dns: false - base_domain: example.com -` - require.NoError(t, os.WriteFile( - filepath.Join(tmpDir, "config.yaml"), []byte(cfg), 0o600)) - require.NoError(t, LoadConfig(tmpDir, false)) - - _, err := LoadServerConfig() - require.Error(t, err) - - got := ConfigErrors(err) - assert.GreaterOrEqual(t, len(got), 3, - "expected sub-builder errors collected into one report; got %d: %v", - len(got), reasons(got)) - - // Sentinels stay reachable through ConfigError.Cause. - require.ErrorIs(t, err, ErrInvalidAllocationStrategy, - "wrapped ErrInvalidAllocationStrategy not in chain") - - // Each sub-builder failure surfaces its YAML key in the rendered output. - rendered := err.Error() - for _, want := range []string{ - "prefixes.v4", - "prefixes.v6", - "prefixes.allocation", - } { - assert.Contains(t, rendered, want, - "expected rendered error to mention %q", want) - } -} - -func reasons(errs []*ConfigError) []string { - out := make([]string, len(errs)) - for i, e := range errs { - out[i] = e.Reason - } - - return out -} - -// TestRemovedKeysAreRejected pins that removed keys stop the load in the file -// and warn in the environment, naming the replacement either way. -func TestRemovedKeysAreRejected(t *testing.T) { - tests := []struct { - name string - file string - env map[string]string - want []string // substrings of the load error - warns []string // substrings of the warnings - serverErr string // from LoadServerConfig after a clean load - }{ - { - name: "deprecated keys", - file: "oidc:\n expiry: 1h\ndns_config:\n use_username_in_magic_dns: true\n", - want: []string{"oidc.expiry was removed", "use node.expiry", "dns_config was removed"}, - }, - { - name: "ignored gorm key", - file: "database:\n gorm:\n prepare_stmt: true\n", - want: []string{"database.gorm.prepare_stmt was removed"}, - }, - { - name: "ephemeral timeout", - file: "ephemeral_node_inactivity_timeout: 30m\n", - want: []string{"ephemeral_node_inactivity_timeout was removed", "use node.ephemeral.inactivity_timeout"}, - }, - { - name: "ephemeral timeout env", - env: map[string]string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT": "30m"}, - warns: []string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT was removed", "use HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT"}, - }, - { - name: "oidc client secret path", - file: "oidc:\n client_secret_path: /run/secret\n", - want: []string{"oidc.client_secret_path was removed", `oidc.client_secret: "file:"`}, - }, - { - name: "oidc client secret path env", - env: map[string]string{"HEADSCALE_OIDC_CLIENT_SECRET_PATH": "/run/secret"}, - warns: []string{"HEADSCALE_OIDC_CLIENT_SECRET_PATH was removed", "use HEADSCALE_OIDC_CLIENT_SECRET=file:"}, - }, - { - // The warning alone would let OIDC start without its secret; the - // consumer's own check stops it. - name: "removed secret variable leaves the secret unset", - file: "server_url: https://headscale.example.com\nnoise:\n private_key_path: noise_private.key\nprefixes:\n v4: 100.64.0.0/10\ndns:\n magic_dns: false\noidc:\n issuer: https://idp.example.com\n client_id: headscale\n", - env: map[string]string{"HEADSCALE_OIDC_CLIENT_SECRET_PATH": "/run/secret"}, - warns: []string{"HEADSCALE_OIDC_CLIENT_SECRET_PATH was removed in 0.30; use HEADSCALE_OIDC_CLIENT_SECRET=file:"}, - serverErr: "oidc.client_secret is required when oidc.issuer is set", - }, - { - name: "postgres password file env", - env: map[string]string{"HEADSCALE_DATABASE_POSTGRES_PASSWORD_FILE": "/run/secret"}, - warns: []string{"HEADSCALE_DATABASE_POSTGRES_PASSWORD_FILE was removed", "use HEADSCALE_DATABASE_POSTGRES_PASS=file:"}, - }, - { - name: "env hints name variables", - env: map[string]string{ - "HEADSCALE_IP_PREFIXES": "100.64.0.0/10", - "HEADSCALE_JSON_LOGS": "true", - "HEADSCALE_METRICS_ENABLED": "true", - }, - warns: []string{ - "use HEADSCALE_PREFIXES_V4 and HEADSCALE_PREFIXES_V6", - "use HEADSCALE_LOG_FORMAT=json", - "remove HEADSCALE_METRICS_ENABLED", - }, - }, - { - name: "file hints name keys", - file: "ip_prefixes: [100.64.0.0/10]\njson_logs: true\nmetrics_enabled: true\n", - want: []string{"use prefixes.v4 and prefixes.v6", `use log.format: "json"`, "remove metrics_enabled"}, - }, - { - name: "provider path key", - file: "dns:\n challenge:\n provider:\n name: cloudflare\n api_token_path: /run/secret\n", - want: []string{"dns.challenge.provider.api_token_path was removed", `dns.challenge.provider.api_token: "file:"`}, - }, - { - name: "provider path key without provider", - file: "dns:\n challenge:\n provider:\n api_token_path: /run/secret\n", - want: []string{"dns.challenge.provider.api_token_path was removed", `dns.challenge.provider.api_token: "file:"`}, - }, - { - name: "provider path env without provider", - env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN_PATH": "/run/secret"}, - warns: []string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN_PATH was removed", "use HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN=file:"}, - }, - { - name: "provider path env for another provider", - env: map[string]string{ - "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "rfc2136", - "HEADSCALE_DNS_CHALLENGE_PROVIDER_KEY_PATH": "/run/secret", - }, - warns: []string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_KEY_PATH was removed", "use HEADSCALE_DNS_CHALLENGE_PROVIDER_KEY=file:"}, - }, - { - name: "provider path in env json", - env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"api_token_path":"/run/secret"}`}, - warns: []string{"dns.challenge.provider.api_token_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed", `dns.challenge.provider.api_token: "file:"`}, - }, - { - name: "provider path in env json with provider", - env: map[string]string{ - "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"API_Token_Path":"/run/secret"}`, - "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", - }, - warns: []string{"dns.challenge.provider.api_token_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed", `dns.challenge.provider.api_token: "file:"`}, - }, - { - name: "registration path key", - file: "dns:\n challenge:\n provider:\n config:\n laptop.example.com:\n password_path: /run/secret\n", - want: []string{ - "dns.challenge.provider.config.laptop.example.com.password_path was removed", - `dns.challenge.provider.config.laptop.example.com.password: "file:"`, - }, - }, - { - // A registration's name is data, not a setting. - name: "registration named like a path key", - file: "dns:\n challenge:\n provider:\n config:\n node.example_path:\n username: user\n", - }, - { - name: "registration named like a path key in env json", - env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"config":{"Node.Example_path":{"username":"user"}}}`}, - }, - { - name: "path key in a registration named like one", - env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"config":{"Node.Example_path":{"Password_Path":"/run/secret"}}}`}, - warns: []string{ - "dns.challenge.provider.config.Node.Example_path.password_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed", - `dns.challenge.provider.config.Node.Example_path.password: "file:"`, - }, - }, - { - name: "registration path in env json", - env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"config":{"laptop.example.com":{"password_path":"/run/secret"}}}`}, - warns: []string{ - "dns.challenge.provider.config.laptop.example.com.password_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed", - `dns.challenge.provider.config.laptop.example.com.password: "file:"`, - }, - }, - // Every spelling of the registration map gives its keys the role the - // nested one does: registration names, then settings. - { - name: "registration named like a path key, nested", - file: "dns: {challenge: {provider: {config: {node.example_path: {username: user, password: pass, subdomain: reg, server_url: https://auth.example.com}}}}}", - }, - { - name: "path key in a registration named config, nested", - file: "dns: {challenge: {provider: {config: {config: {Password_Path: /run/secret}}}}}", - want: []string{ - "dns.challenge.provider.config.config.password_path was removed", - `dns.challenge.provider.config.config.password: "file:"`, - }, - }, - { - name: "registration named like a path key, dotted provider", - file: "dns.challenge.provider: {config: {node.example_path: {username: user, password: pass, subdomain: reg, server_url: https://auth.example.com}}}", - }, - { - name: "path key in a registration named config, dotted provider", - file: "dns.challenge.provider: {config: {config: {Password_Path: /run/secret}}}", - want: []string{ - "dns.challenge.provider.config.config.password_path was removed", - `dns.challenge.provider.config.config.password: "file:"`, - }, - }, - { - name: "registration named like a path key, dotted registration map", - file: "dns.challenge.provider.config: {node.example_path: {username: user, password: pass, subdomain: reg, server_url: https://auth.example.com}}", - }, - { - name: "path key in a registration named config, dotted registration map", - file: "dns.challenge.provider.config: {config: {Password_Path: /run/secret}}", - want: []string{ - "dns.challenge.provider.config.config.password_path was removed", - `dns.challenge.provider.config.config.password: "file:"`, - }, - }, - { - name: "registration named like a path key, dotted in dns", - file: "dns: {challenge.provider.config: {node.example_path: {username: user, password: pass, subdomain: reg, server_url: https://auth.example.com}}}", - }, - { - name: "path key in a registration named config, dotted in dns", - file: "dns: {challenge.provider.config: {config: {Password_Path: /run/secret}}}", - want: []string{ - "dns.challenge.provider.config.config.password_path was removed", - `dns.challenge.provider.config.config.password: "file:"`, - }, - }, - { - name: "registration named like a path key, dotted in dns.challenge", - file: "dns.challenge: {provider.config: {node.example_path: {username: user, password: pass, subdomain: reg, server_url: https://auth.example.com}}}", - }, - { - name: "path key in a registration named config, dotted in dns.challenge", - file: "dns.challenge: {provider.config: {config: {Password_Path: /run/secret}}}", - want: []string{ - "dns.challenge.provider.config.config.password_path was removed", - `dns.challenge.provider.config.config.password: "file:"`, - }, - }, - { - name: "postgres password file", - file: "database:\n postgres:\n password_file: /run/secret\n", - want: []string{"database.postgres.password_file was removed", `database.postgres.pass: "file:"`}, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - for k, v := range tt.env { - t.Setenv(k, v) - } - - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(tt.file), 0o600)) - - err := LoadConfig(dir, false) - if len(tt.want) == 0 { - require.NoError(t, err) - } else { - require.Error(t, err) - // A removed key's value may be a secret. - require.NotContains(t, err.Error(), "/run/secret") - } - - for _, want := range tt.want { - require.ErrorContains(t, err, want) - } - - if err != nil { - return - } - - warnings := strings.Join(src.Warnings(), "\n") - assert.NotContains(t, warnings, "/run/secret") - - if len(tt.warns) == 0 { - assert.NotContains(t, warnings, "was removed") - } - - for _, w := range tt.warns { - assert.Contains(t, warnings, w) - } - - if tt.serverErr != "" { - _, err := LoadServerConfig() - require.ErrorContains(t, err, tt.serverErr) - } - }) - } -} - -// TestConfigSecrets pins that the loaders keep each secret as written, a -// value or file:, and read no file: its consumer does -// (TestNewHeadscaleSecrets, TestCLIAPIKey). -func TestConfigSecrets(t *testing.T) { - missing := "file:" + filepath.Join(t.TempDir(), "missing") - - base := `--- -server_url: https://headscale.example.com -noise: - private_key_path: noise_private.key -prefixes: - v4: 100.64.0.0/10 -database: - type: postgres -dns: - magic_dns: false - override_local_dns: false -cli: - address: headscale.example.com:443 -oidc: - issuer: https://idp.example.com - client_id: headscale -` - - for name, value := range map[string]string{"literal": "literal", "missing file": missing} { - t.Run(name, func(t *testing.T) { - t.Setenv("HEADSCALE_DATABASE_POSTGRES_PASS", value) - t.Setenv("HEADSCALE_CLI_API_KEY", value) - - tmpDir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "config.yaml"), []byte(base+" client_secret: "+value+"\n"), 0o600)) - require.NoError(t, LoadConfig(tmpDir, false)) - - cfg, err := LoadServerConfig() - require.NoError(t, err) - assert.Equal(t, Secret(value), cfg.OIDC.ClientSecret) - assert.Equal(t, Secret(value), cfg.Database.Postgres.Pass) - // The server never uses the CLI's key. - assert.Empty(t, cfg.CLI.APIKey) - - cli, err := LoadCLIConfig() - require.NoError(t, err) - assert.Equal(t, Secret(value), cli.CLI.APIKey) - }) - } -} - -// The unset case is the regression: the default is spelled "0o770", which a -// base-8 parse rejects, so the socket silently became 0700. -// https://github.com/juanfont/headscale/issues/3529 -func TestUnixSocketPermission(t *testing.T) { - tests := []struct { - name string - yaml string - env string - want FileMode - wantErr bool - }{ - {name: "unset-uses-default", want: 0o770}, - {name: "null-uses-default", yaml: `unix_socket_permission: null`, want: 0o770}, - {name: "quoted-leading-zero", yaml: `unix_socket_permission: "0770"`, want: 0o770}, - {name: "quoted-plain", yaml: `unix_socket_permission: "770"`, want: 0o770}, - {name: "quoted-0o-prefix", yaml: `unix_socket_permission: "0o770"`, want: 0o770}, - {name: "quoted-0o-prefix-owner-only", yaml: `unix_socket_permission: "0o700"`, want: 0o700}, - {name: "quoted-zero", yaml: `unix_socket_permission: "0000"`, want: 0}, - {name: "env", env: "0660", want: 0o660}, - {name: "env-0o-prefix", env: "0o660", want: 0o660}, - {name: "env-overrides-file", yaml: `unix_socket_permission: "0770"`, env: "0700", want: 0o700}, - {name: "env-invalid-is-error", env: "bogus", wantErr: true}, - {name: "empty-is-error", yaml: `unix_socket_permission: ""`, wantErr: true}, - {name: "symbolic-is-error", yaml: `unix_socket_permission: "rwxrwx---"`, wantErr: true}, - {name: "special-bits-are-error", yaml: `unix_socket_permission: "1770"`, wantErr: true}, - {name: "out-of-range-is-error", yaml: `unix_socket_permission: "17777"`, wantErr: true}, - // YAML reads unquoted 0770 as int 504, indistinguishable from a literal 504. - {name: "unquoted-leading-zero-is-error", yaml: `unix_socket_permission: 0770`, wantErr: true}, - {name: "unquoted-0o-prefix-is-error", yaml: `unix_socket_permission: 0o770`, wantErr: true}, - {name: "unquoted-plain-is-error", yaml: `unix_socket_permission: 770`, wantErr: true}, - {name: "float-is-error", yaml: `unix_socket_permission: 770.0`, wantErr: true}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if tt.env != "" { - t.Setenv("HEADSCALE_UNIX_SOCKET_PERMISSION", tt.env) - } - - tmpDir := t.TempDir() - cfg := `--- -server_url: https://example.com -listen_addr: 0.0.0.0:8080 -prefixes: - v4: 100.64.0.0/10 -noise: - private_key_path: noise_private.key -database: - type: sqlite3 -dns: - magic_dns: false - override_local_dns: false -` + tt.yaml + "\n" - require.NoError(t, os.WriteFile( - filepath.Join(tmpDir, "config.yaml"), []byte(cfg), 0o600)) - require.NoError(t, LoadConfig(tmpDir, false)) - - got, err := LoadServerConfig() - if tt.wantErr { - require.ErrorIs(t, err, util.ErrInvalidFileMode, - "invalid unix_socket_permission must not silently fall back") - assert.Contains(t, err.Error(), "unix_socket_permission") - - return - } - - require.NoError(t, err) - assert.Equal(t, tt.want, got.UnixSocketPermission, - "got %#o, want %#o", got.UnixSocketPermission, tt.want) - }) - } -} - -func TestServerURLTrailingSlash(t *testing.T) { - tests := []struct { - name string - input string - want string - }{ - {name: "no slash", input: "https://h", want: "https://h"}, - {name: "one slash", input: "https://h/", want: "https://h"}, - {name: "two slashes", input: "https://h//", want: "https://h"}, - {name: "port", input: "https://h:8443/", want: "https://h:8443"}, - {name: "path prefix", input: "https://h/hs/", want: "https://h/hs"}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - useConfig(t, map[string]any{ - "server_url": tt.input, - "noise.private_key_path": "private_key.pem", - "prefixes.v4": "100.64.0.0/10", - "database.type": "sqlite3", - "dns.magic_dns": false, - "dns.override_local_dns": false, - }) - - cfg, err := LoadServerConfig() - require.NoError(t, err) - assert.Equal(t, tt.want, cfg.ServerURL) - }) - } -} - -// OK -// server_url: headscale.com, base: clients.headscale.com -// server_url: headscale.com, base: headscale.net -// -// NOT OK -// server_url: server.headscale.com, base: headscale.com. -func TestSafeServerURL(t *testing.T) { - tests := []struct { - serverURL, baseDomain, - wantErr string - }{ - { - serverURL: "https://example.com", - baseDomain: "example.org", - }, - { - serverURL: "https://headscale.com", - baseDomain: "headscale.com", - wantErr: errServerURLSame.Error(), - }, - { - serverURL: "https://headscale.com", - baseDomain: "clients.headscale.com", - }, - { - serverURL: "https://headscale.com", - baseDomain: "clients.subdomain.headscale.com", - }, - { - serverURL: "https://headscale.kristoffer.com", - baseDomain: "mybase", - }, - { - serverURL: "https://server.headscale.com", - baseDomain: "headscale.com", - wantErr: errServerURLSuffix.Error(), - }, - { - serverURL: "https://server.subdomain.headscale.com", - baseDomain: "headscale.com", - wantErr: errServerURLSuffix.Error(), - }, - { - serverURL: "http://foo\x00", - wantErr: `parse "http://foo\x00": net/url: invalid control character in URL`, - }, - } - - for _, tt := range tests { - testName := fmt.Sprintf("server=%s domain=%s", tt.serverURL, tt.baseDomain) - t.Run(testName, func(t *testing.T) { - err := isSafeServerURL(tt.serverURL, tt.baseDomain) - if tt.wantErr != "" { - assert.EqualError(t, err, tt.wantErr) - - return - } - - assert.NoError(t, err) - }) - } -} - -func TestSafeServerURLWithPort(t *testing.T) { - tests := []struct { - serverURL, baseDomain, - wantErr string - }{ - { - serverURL: "https://server.headscale.com:443", - baseDomain: "headscale.com", - wantErr: errServerURLSuffix.Error(), - }, - { - serverURL: "https://server.subdomain.headscale.com:8080", - baseDomain: "headscale.com", - wantErr: errServerURLSuffix.Error(), - }, - { - serverURL: "https://headscale.com:443", - baseDomain: "headscale.com", - wantErr: errServerURLSame.Error(), - }, - { - serverURL: "https://example.com:8080", - baseDomain: "example.org", - }, - } - - for _, tt := range tests { - testName := fmt.Sprintf("server=%s domain=%s", tt.serverURL, tt.baseDomain) - t.Run(testName, func(t *testing.T) { - err := isSafeServerURL(tt.serverURL, tt.baseDomain) - if tt.wantErr != "" { - assert.EqualError(t, err, tt.wantErr) - - return - } - - assert.NoError(t, err) - }) - } -} - // 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, 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. +// or logging it does 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" @@ -1254,121 +125,6 @@ func TestConfigJSONOmitsSecrets(t *testing.T) { } } -//nolint:goconst // repeated CIDR strings are test fixtures, not refactor candidates -func TestTrustedProxies(t *testing.T) { - tests := []struct { - name string - input any - want []netip.Prefix - wantErr string - }{ - { - name: "unset", - input: nil, - want: nil, - }, - { - name: "empty", - input: []string{}, - want: nil, - }, - { - name: "single-v4", - input: []string{"10.0.0.0/16"}, - want: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/16")}, - }, - { - name: "single-v6", - input: []string{"fd00::/8"}, - want: []netip.Prefix{netip.MustParsePrefix("fd00::/8")}, - }, - { - name: "mixed-v4-v6", - input: []string{"127.0.0.1/32", "::1/128", "10.0.0.0/16"}, - want: []netip.Prefix{ - netip.MustParsePrefix("127.0.0.1/32"), - netip.MustParsePrefix("::1/128"), - netip.MustParsePrefix("10.0.0.0/16"), - }, - }, - { - name: "non-canonical-masked", - input: []string{"10.0.0.5/16"}, - want: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/16")}, - }, - { - name: "bare-ip-rejected", - input: []string{"10.0.0.1"}, - wantErr: `netip.ParsePrefix("10.0.0.1")`, - }, - { - name: "garbage-names-the-value", - input: []string{"10.0.0.0/16", "not-an-ip"}, - wantErr: `trusted_proxies is not a valid CIDR`, - }, - { - name: "ipv4-zero-rejected", - input: []string{"0.0.0.0/0"}, - wantErr: `trusted_proxies[0] "0.0.0.0/0": 0.0.0.0/0 and ::/0 are not allowed`, - }, - { - name: "ipv6-zero-rejected", - input: []string{"::/0"}, - wantErr: "0.0.0.0/0 and ::/0 are not allowed", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - values := map[string]any{"prefixes.v4": "100.64.0.0/10"} - if tt.input != nil { - values["trusted_proxies"] = tt.input - } - - useConfig(t, values) - - 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) - assert.Contains(t, err.Error(), tt.wantErr) - - return - } - - require.NoError(t, err) - - 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) { @@ -1544,681 +300,6 @@ func TestLiveDNS(t *testing.T) { assert.Len(t, cfg.CloneTailcfgDNSConfig().ExtraRecords, 1, "a clone is a copy") } -// TestNameserverWarnings pins that the config check reports a nameserver -// clients will not get, and that building the DNS config does not report it -// again. -func TestNameserverWarnings(t *testing.T) { - yaml := `--- -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 - base_domain: ts.example.com - override_local_dns: true - nameservers: - global: ["1.1.1.1", "1.1.1.1:53"] - split: - corp.example.com: ["[2606:4700::1111]:53"] -` - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(yaml), 0o600)) - require.NoError(t, LoadConfig(dir, false)) - - var buf bytes.Buffer - - oldLogger, oldLevel := log.Logger, zerolog.GlobalLevel() - log.Logger = zerolog.New(&buf) - - t.Cleanup(func() { - log.Logger = oldLogger - - zerolog.SetGlobalLevel(oldLevel) - }) - - cfg, err := LoadServerConfig() - require.NoError(t, err) - - logged := buf.String() - assert.Contains(t, logged, `invalid nameserver \"1.1.1.1:53\" in dns.nameservers.global, ignoring`) - assert.Contains(t, logged, `invalid nameserver \"[2606:4700::1111]:53\" in dns.nameservers.split.corp.example.com, ignoring`) - assert.NotContains(t, logged, `\"1.1.1.1\" in`) - - buf.Reset() - cfg.StartDNS(nil) - - assert.Equal(t, []*dnstype.Resolver{{Addr: "1.1.1.1"}}, cfg.CloneTailcfgDNSConfig().Resolvers) - assert.Empty(t, buf.String(), "built once, reported once: by the load") -} - -func TestDNSChallengeConfig(t *testing.T) { - secret := filepath.Join(t.TempDir(), "token") - require.NoError(t, os.WriteFile(secret, []byte("file-token\n"), 0o600)) - - 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 -` - - tests := []struct { - name string - yaml string - env map[string]string - wantErr string - fileErr bool // only reading a file: secret fails, so the load passes - wantWarns []string - wantZone string - wantToken string - wantNoProv bool - wantResolvers []string - wantIsolates string - wantProvider string // the provider's type, for one other than cloudflare - }{ - { - name: "disabled by default", - yaml: base, - // CertificateZone follows base_domain even when unused, so it is never empty. - wantZone: "ts.example.com", - wantNoProv: true, - wantResolvers: []string{"1.1.1.1:53", "8.8.8.8:53"}, - }, - { - name: "resolvers default to port 53", - yaml: base + ` challenge: - resolvers: ["9.9.9.9", "2620:fe::fe", "127.0.0.1:5353"] -`, - wantZone: "ts.example.com", - wantNoProv: true, - wantResolvers: []string{"9.9.9.9:53", "[2620:fe::fe]:53", "127.0.0.1:5353"}, - }, - { - name: "enabled with inline token", - yaml: base + ` certificates: - enabled: true - challenge: - provider: - name: cloudflare - api_token: yaml-token -`, - wantZone: "ts.example.com", - wantToken: "yaml-token", - }, - { - name: "env only", - yaml: base, - env: map[string]string{ - "HEADSCALE_DNS_CERTIFICATES_ENABLED": "true", - "HEADSCALE_DNS_CERTIFICATES_ZONE": "example.com", - "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", - "HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "file:" + secret, - }, - wantZone: "example.com", - wantToken: "file-token", - }, - { - name: "token from file", - yaml: base + ` challenge: - provider: - name: cloudflare - api_token: "file:` + secret + `" -`, - wantZone: "ts.example.com", - wantToken: "file-token", - }, - { - name: "acmedns registration password from file", - yaml: base + ` certificates: - enabled: true - challenge: - provider: - name: acmedns - config: - laptop.ts.example.com: - username: user - password: "file:` + secret + `" - subdomain: reg-laptop - server_url: https://auth.acme-dns.io -`, - wantZone: "ts.example.com", - wantIsolates: "laptop.ts.example.com", - }, - { - name: "acmedns registration password file missing", - yaml: base + ` certificates: - enabled: true - challenge: - provider: - name: acmedns - config: - laptop.ts.example.com: - username: user - password: "file:/nonexistent/password" - subdomain: reg-laptop - server_url: https://auth.acme-dns.io -`, - wantErr: "dns.challenge.provider.config.laptop.ts.example.com.password: ", - fileErr: true, - }, - { - // Unread by the load, a file: setting counts as present. - name: "token file missing", - yaml: base + ` challenge: - provider: - name: cloudflare - api_token: "file:/nonexistent/token" -`, - wantErr: "dns.challenge.provider.api_token: ", - fileErr: true, - }, - { - name: "provider env json", - yaml: base, - env: map[string]string{ - "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", - "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"api_token":"good","zone_token":"zone"}`, - }, - wantZone: "ts.example.com", - wantToken: "good", - }, - { - // 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 not a JSON object (env HEADSCALE_DNS_CHALLENGE_PROVIDER)", - }, - { - // The removed field warns and is dropped; the rest still loads. - name: "removed field in provider env json", - yaml: base, - env: map[string]string{ - "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", - "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"name":"cloudflare","api_token":"good","api_token_path":"/old"}`, - }, - wantWarns: []string{"dns.challenge.provider.api_token_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed"}, - wantZone: "ts.example.com", - wantToken: "good", - }, - { - name: "removed field in a registration in provider env json", - yaml: base, - env: map[string]string{ - "HEADSCALE_DNS_CERTIFICATES_ENABLED": "true", - "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "acmedns", - "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"config":{"laptop.ts.example.com":{"username":"user","password":"pass",` + - `"subdomain":"reg-laptop","server_url":"https://auth.acme-dns.io","password_path":"/old"}}}`, - }, - wantWarns: []string{"dns.challenge.provider.config.laptop.ts.example.com.password_path in HEADSCALE_DNS_CHALLENGE_PROVIDER was removed"}, - wantZone: "ts.example.com", - wantIsolates: "laptop.ts.example.com", - }, - { - name: "cloudflare without api_token", - yaml: base + ` challenge: - provider: - name: cloudflare - zone_token: marker-zone-token -`, - wantErr: "cloudflare: missing api_token", - }, - { - // The old variable warns and is ignored, leaving the token unset. - name: "cloudflare token only in a removed variable", - yaml: base + ` challenge: - provider: - name: cloudflare - zone_token: marker-zone-token -`, - env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN_PATH": "/run/token"}, - wantWarns: []string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN_PATH was removed"}, - wantErr: "cloudflare: missing api_token", - }, - { - name: "powerdns without api_token", - yaml: base + ` challenge: - provider: - name: powerdns - server_url: https://pdns.example.com -`, - wantErr: "powerdns: missing api_token", - }, - { - name: "rfc2136 without a key", - yaml: base + ` challenge: - provider: - name: rfc2136 - server: ns1.example.com:53 -`, - wantErr: "rfc2136: missing key_name, key_alg, key", - }, - { - name: "hetzner without api_token", - yaml: base + ` challenge: - provider: - name: hetzner -`, - env: map[string]string{"LIBDNS_HETZNER_TOKEN": ""}, - wantErr: "hetzner: missing api_token", - }, - { - // The provider reads its token from this variable when api_token is unset. - name: "hetzner token from its own variable", - yaml: base + ` challenge: - provider: - name: hetzner - api_token: "" -`, - env: map[string]string{"LIBDNS_HETZNER_TOKEN": "marker-token"}, - wantZone: "ts.example.com", - wantProvider: "*hetzner.Provider", - }, - { - name: "gigahost without api_key", - yaml: base + ` challenge: - provider: - name: gigahost -`, - wantErr: "gigahost: missing api_key", - }, - { - name: "env overrides yaml", - yaml: base + ` challenge: - provider: - name: cloudflare - api_token: yaml-token -`, - env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "env-token"}, - wantZone: "ts.example.com", - wantToken: "env-token", - }, - { - name: "typo in provider key", - yaml: base + ` challenge: - provider: - name: cloudflare - auth_token: tok -`, - wantErr: "dns.challenge.provider is invalid", - }, - { - name: "unknown provider", - yaml: base + ` challenge: - provider: - name: route53 -`, - wantErr: "dns.challenge.provider is invalid", - }, - { - name: "acmedns one account for node certificates", - yaml: base + ` certificates: - enabled: true - challenge: - provider: - name: acmedns - username: user - password: pass - subdomain: reg - server_url: https://auth.acme-dns.io -`, - wantErr: "registration per name", - }, - { - name: "acmedns registration per name", - yaml: base + ` certificates: - enabled: true - challenge: - provider: - name: acmedns - config: - laptop.ts.example.com: - username: user - password: pass - subdomain: reg-laptop - server_url: https://auth.acme-dns.io - desktop.ts.example.com: - username: user - password: pass - subdomain: reg-desktop - server_url: https://auth.acme-dns.io -`, - wantZone: "ts.example.com", - wantIsolates: "laptop.ts.example.com", - }, - { - name: "acmedns registration shared by node names", - yaml: base + ` certificates: - enabled: true - challenge: - provider: - name: acmedns - config: - laptop.ts.example.com: - username: user - password: pass - subdomain: reg - server_url: https://auth.acme-dns.io - desktop.ts.example.com: - username: user - password: pass - subdomain: reg - server_url: https://auth.acme-dns.io -`, - wantErr: "desktop.ts.example.com and laptop.ts.example.com share a registration", - }, - { - name: "acmedns one account for headscale's own certificate", - yaml: base + ` challenge: - provider: - name: acmedns - username: user - password: pass - subdomain: reg - server_url: https://auth.acme-dns.io -tls: - acme: - hostname: headscale.example.com - challenge_type: DNS-01 -`, - wantZone: "ts.example.com", - }, - { - name: "acmedns registration shared with headscale's own name", - yaml: base + ` certificates: - enabled: true - challenge: - provider: - name: acmedns - config: - laptop.ts.example.com: - username: user - password: pass - subdomain: reg - server_url: https://auth.acme-dns.io - headscale.example.com: - username: user - password: pass - subdomain: reg - server_url: https://auth.acme-dns.io -tls: - acme: - hostname: headscale.example.com - challenge_type: DNS-01 -`, - wantErr: "headscale.example.com and laptop.ts.example.com share a registration", - }, - { - name: "enabled without magic dns", - yaml: strings.Replace(base, "magic_dns: true", "magic_dns: false", 1) + ` certificates: - enabled: true - challenge: - provider: - name: cloudflare - api_token: tok -`, - wantErr: "dns.certificates requires dns.magic_dns", - }, - { - name: "enabled without provider", - yaml: base + ` certificates: - enabled: true -`, - wantErr: "dns.certificates and tls.acme DNS-01 require dns.challenge.provider", - }, - { - name: "base_domain outside zone", - yaml: base + ` certificates: - enabled: true - zone: notexample.com - challenge: - provider: - name: cloudflare - api_token: tok -`, - wantErr: "dns.base_domain is not inside dns.certificates.zone", - }, - { - name: "zone suffix without label boundary", - yaml: base + ` certificates: - enabled: true - zone: ample.com - challenge: - provider: - name: cloudflare - api_token: tok -`, - wantErr: "dns.base_domain is not inside dns.certificates.zone", - }, - { - name: "uppercase base_domain", - yaml: strings.Replace(base, "ts.example.com", "TS.example.com", 1) + ` certificates: - enabled: true - challenge: - provider: - name: cloudflare - api_token: tok -`, - wantErr: "dns.base_domain must be a lowercase DNS name", - }, - { - name: "tls DNS-01 without provider", - yaml: base + `tls: - acme: - hostname: headscale.example.com - challenge_type: DNS-01 -`, - wantErr: "dns.certificates and tls.acme DNS-01 require dns.challenge.provider", - }, - { - name: "negative propagation timeout", - yaml: base + ` challenge: - propagation_timeout: -1s -`, - wantErr: "dns.challenge.propagation_timeout must not be negative", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - for k, v := range tt.env { - t.Setenv(k, v) - } - - tmpDir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "config.yaml"), []byte(tt.yaml), 0o600)) - require.NoError(t, LoadConfig(tmpDir, false)) - require.Len(t, src.Warnings(), len(tt.wantWarns)) - - for i, w := range tt.wantWarns { - assert.Contains(t, src.Warnings()[i], w) - } - - cfg, err := LoadServerConfig() - if tt.wantErr != "" && !tt.fileErr { - require.ErrorContains(t, err, tt.wantErr) - assert.NotContains(t, err.Error(), "marker-") - - return - } - - require.NoError(t, err) - - if tt.fileErr { - _, err = cfg.DNSConfig.Challenge.NewProvider(ResolveSecret) - require.ErrorContains(t, err, tt.wantErr) - - return - } - - assert.Equal(t, tt.wantZone, cfg.DNSConfig.CertificateZone()) - - if tt.wantResolvers != nil { - assert.Equal(t, tt.wantResolvers, cfg.DNSConfig.Challenge.ResolverAddrs()) - } - - // The loader only validates the provider; startup builds it. - assert.Nil(t, cfg.DNSConfig.Challenge.Provider) - - if tt.wantNoProv { - assert.Empty(t, cfg.DNSConfig.Challenge.ProviderName()) - - return - } - - built, err := cfg.DNSConfig.Challenge.NewProvider(ResolveSecret) - require.NoError(t, err) - - if tt.wantProvider != "" { - assert.Equal(t, tt.wantProvider, fmt.Sprintf("%T", built)) - - return - } - - if iso, ok := built.(hsdns.Isolator); ok { - if tt.wantIsolates != "" { - assert.True(t, iso.Isolates(tt.wantIsolates)) - } - - return - } - - provider, ok := built.(*cloudflare.Provider) - require.True(t, ok, "provider is %T", built) - assert.Equal(t, tt.wantToken, provider.APIToken) - }) - } -} - -// TestTLSConfig pins the tls defaults and that the flat keys the tls block -// replaced, including the ones the NixOS module once wrote with TLS off, are -// rejected. -func TestTLSConfig(t *testing.T) { - 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: false - override_local_dns: false -` - - tests := []struct { - name string - yaml string - env map[string]string - want TLSConfig - wantErr []string - }{ - { - name: "defaults", - yaml: base, - want: TLSConfig{ACME: ACMEConfig{ - ChallengeType: HTTP01ChallengeType, - HTTPListen: ":http", - CacheDir: "/var/lib/headscale/cache", - }}, - }, - { - name: "legacy keys", - yaml: base + `tls_letsencrypt_hostname: headscale.example.com -tls_letsencrypt_challenge_type: TLS-ALPN-01 -tls_letsencrypt_listen: ":8080" -tls_letsencrypt_cache_dir: /var/lib/headscale/cache -acme_url: https://acme.example.com/directory -acme_email: admin@example.com -`, - wantErr: []string{ - "tls_letsencrypt_hostname was removed", "use tls.acme.hostname", - "tls_letsencrypt_challenge_type was removed", "tls_letsencrypt_listen was removed", - "tls_letsencrypt_cache_dir was removed", "acme_url was removed", "acme_email was removed", - }, - }, - { - name: "legacy keys next to tls keys", - yaml: base + `tls_cert_path: /old/cert.pem -tls_key_path: /old/key.pem -tls: - cert_path: /new/cert.pem - key_path: /new/key.pem -`, - wantErr: []string{"tls_cert_path was removed", "use tls.cert_path", "tls_key_path was removed"}, - }, - { - // What the NixOS module wrote when TLS was off. - name: "nixos defaults", - yaml: base + `tls_letsencrypt_hostname: "" -tls_letsencrypt_challenge_type: HTTP-01 -tls_letsencrypt_listen: ":http" -tls_letsencrypt_cache_dir: /var/lib/headscale/.cache -`, - wantErr: []string{"tls_letsencrypt_hostname was removed", "tls_letsencrypt_cache_dir was removed"}, - }, - { - name: "env", - yaml: base, - env: map[string]string{ - "HEADSCALE_TLS_CERT_PATH": "/env/cert.pem", - "HEADSCALE_TLS_KEY_PATH": "/env/key.pem", - }, - want: TLSConfig{ - CertPath: "/env/cert.pem", - KeyPath: "/env/key.pem", - ACME: ACMEConfig{ - ChallengeType: HTTP01ChallengeType, - HTTPListen: ":http", - CacheDir: "/var/lib/headscale/cache", - }, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - for k, v := range tt.env { - t.Setenv(k, v) - } - - tmpDir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "config.yaml"), []byte(tt.yaml), 0o600)) - - err := LoadConfig(tmpDir, false) - - if tt.wantErr != nil { - for _, want := range tt.wantErr { - require.ErrorContains(t, err, want) - } - - return - } - - require.NoError(t, err) - - cfg, err := LoadServerConfig() - require.NoError(t, err) - assert.Equal(t, tt.want, cfg.TLS) - }) - } -} - func TestPostgresConfigDSN(t *testing.T) { tests := []struct { name string @@ -2343,132 +424,6 @@ func TestServesRoute(t *testing.T) { assert.True(t, (&Config{Prefixes: PrefixesConfig{V4: &narrow}}).ServesRoute(netip.MustParsePrefix("100.100.0.1/32"))) } -func TestBoolConfigValidation(t *testing.T) { - for _, source := range []string{"file", "env"} { - for _, tt := range []struct { - value string - want bool - invalid bool - }{ - {"true", true, false}, - {"false", false, false}, - {"1", true, false}, - {"0", false, false}, - {"T", true, false}, - {"f", false, false}, - {"TRUE", true, false}, - {"False", false, false}, - {"True", true, false}, - {"FALSE", false, false}, - {"", true, false}, - {"null", true, false}, - {"yes", false, true}, - {"on", false, true}, - {"ture", false, true}, - {"tRuE", false, true}, - {"2", false, true}, - {"[true]", false, true}, - {`""`, false, true}, - } { - if source == "env" && tt.value == "null" { - continue - } - - t.Run(source+"/"+tt.value, func(t *testing.T) { - t.Setenv("HEADSCALE_OIDC_EMAIL_VERIFIED_REQUIRED", "") - - config := `server_url: https://headscale.example.com -noise: - private_key_path: noise_private.key -prefixes: - v4: 100.64.0.0/10 -database: - type: sqlite3 -dns: - magic_dns: false - override_local_dns: false -` - if source == "file" && tt.value != "" { - config += "oidc:\n email_verified_required: " + tt.value + "\n" - } - - if source == "env" { - t.Setenv("HEADSCALE_OIDC_EMAIL_VERIFIED_REQUIRED", tt.value) - } - - path := filepath.Join(t.TempDir(), "config.yaml") - require.NoError(t, os.WriteFile(path, []byte(config), 0o600)) - require.NoError(t, LoadConfig(path, true)) - - loaded, err := LoadServerConfig() - if tt.invalid { - require.Error(t, err) - assert.Contains(t, err.Error(), "oidc.email_verified_required") - - return - } - - require.NoError(t, err) - assert.Equal(t, tt.want, loaded.OIDC.EmailVerifiedRequired) - }) - } - } -} - -// TestServerConfigDisableUpdateCheck: the server config carries the setting -// as the CLI config does, for DumpConfig and embedders. -func TestServerConfigDisableUpdateCheck(t *testing.T) { - for _, want := range []bool{true, false} { - path := writeConfig(t, fmt.Sprintf(`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 -disable_check_updates: %t -`, want)) - require.NoError(t, LoadConfig(path, true)) - - cfg, err := LoadServerConfig() - require.NoError(t, err) - assert.Equal(t, want, cfg.DisableUpdateCheck) - } -} - -func TestExampleConfigBooleanSettings(t *testing.T) { - require.NoError(t, LoadConfig("../../config-example.yaml", true)) - - _, err := LoadServerConfig() - require.NoError(t, err) -} - -func TestLoadCLIConfigRejectsNonBoolean(t *testing.T) { - for _, key := range []string{"cli.insecure", "disable_check_updates"} { - t.Run(key, func(t *testing.T) { - path := filepath.Join(t.TempDir(), "config.yaml") - require.NoError(t, os.WriteFile(path, nil, 0o600)) - - env := configEnvName(key) - t.Setenv(env, "true") - require.NoError(t, LoadConfig(path, true)) - - _, err := LoadCLIConfig() - require.NoError(t, err) - - t.Setenv(env, "ture") - require.NoError(t, LoadConfig(path, true)) - - _, err = LoadCLIConfig() - require.Error(t, err) - assert.Contains(t, err.Error(), key) - }) - } -} - // TestDNSDomainsCompat: SaaS sends the tailnet domain as a search domain with // MagicDNS on and leaves it out with MagicDNS off. func TestDNSDomainsCompat(t *testing.T) { @@ -2490,785 +445,6 @@ func TestDNSDomainsCompat(t *testing.T) { } } -// compatRecord is the golden for one config input: what the loader produced. -type compatRecord struct { - Config json.RawMessage `json:"config,omitempty"` - Secrets map[string]string `json:"secrets,omitempty"` // secret strings the JSON form hides - Warnings []string `json:"warnings,omitempty"` - LogErrors []string `json:"log_errors,omitempty"` // input the loader logged and dropped - Error string `json:"error,omitempty"` // an error-only golden pins today's rejection -} - -type compatRow struct{ name, file, envFile string } - -var compatDir = filepath.Join("testdata", "compat") - -// compatRows lists every compat/.{yaml,json,env} set and every -// testdata/*.yaml file. -func compatRows(t *testing.T) []compatRow { - t.Helper() - - entries, err := os.ReadDir(compatDir) - require.NoError(t, err) - - var rows []compatRow - - byName := map[string]int{} - - for _, e := range entries { - f, ext := e.Name(), filepath.Ext(e.Name()) - if strings.HasSuffix(f, ".golden.json") || (ext != ".yaml" && ext != ".json" && ext != ".env") { - continue - } - - name := strings.TrimSuffix(f, ext) - - i, ok := byName[name] - if !ok { - i = len(rows) - byName[name] = i - rows = append(rows, compatRow{name: name}) - } - - if ext == ".env" { - rows[i].envFile = filepath.Join(compatDir, f) - } else { - rows[i].file = filepath.Join(compatDir, f) - } - } - - legacy, err := filepath.Glob(filepath.Join("testdata", "*.yaml")) - require.NoError(t, err) - - for _, f := range legacy { - rows = append(rows, compatRow{name: "testdata-" + strings.TrimSuffix(filepath.Base(f), ".yaml"), file: f}) - } - - return rows -} - -// unsetHeadscaleEnv removes ambient HEADSCALE_* for the test, which would -// leak into the goldens. -func unsetHeadscaleEnv(t *testing.T) { - t.Helper() - - for _, kv := range os.Environ() { - if k, _, _ := strings.Cut(kv, "="); strings.HasPrefix(k, "HEADSCALE_") { - t.Setenv(k, "") // registers the restore - require.NoError(t, os.Unsetenv(k)) - } - } -} - -// TestConfigCompat pins what real-world config inputs load to, so a loader -// change shows up as a golden diff, and each such diff needs a BREAKING -// CHANGELOG entry. Every compat/.{yaml,json,env} set is one row; a name -// ending in -cli uses the CLI loader. Fixtures are frozen inputs: to change -// one, add a new fixture. Refresh with HEADSCALE_UPDATE_GOLDEN=1. -// -// Sources: example-.yaml is `git show :config-example.yaml`; -// example-main.yaml is a frozen copy of the root file; nixos-*.yaml are the -// files `nix build` writes for the flake module with nix/example-configuration.nix -// (services.headscale.configFile, environment.etc."headscale/config.yaml") and -// 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, -pruned without the keys removed -// for having no effect. -func TestConfigCompat(t *testing.T) { - rows := compatRows(t) - update := os.Getenv("HEADSCALE_UPDATE_GOLDEN") != "" - - unsetHeadscaleEnv(t) - - for _, tt := range rows { - t.Run(tt.name, func(t *testing.T) { - rec := loadCompat(t, tt.file, tt.envFile, strings.HasSuffix(tt.name, "-cli")) - - out, err := json.MarshalIndent(rec, "", " ") - require.NoError(t, err) - - out = append(out, '\n') - gpath := filepath.Join(compatDir, tt.name+".golden.json") - - if update { - require.NoError(t, os.WriteFile(gpath, out, 0o600)) - - return - } - - want, err := os.ReadFile(gpath) - require.NoErrorf(t, err, "missing golden %s: run with HEADSCALE_UPDATE_GOLDEN=1", gpath) - - if d := cmp.Diff(string(want), string(out)); d != "" { - t.Errorf("config compat mismatch (-golden +got):\n%s", d) - } - }) - } -} - -// compatConfig is the JSON shape of Config the goldens were written in, -// frozen so Config can change without a golden diff; compatView renders -// into it. -type compatConfig struct { - ServerURL string - Addr string - MetricsAddr string - TrustedProxies []netip.Prefix - Node compatNodeConfig - PreAuthKeys compatPreAuthKeysConfig - PrefixV4 *netip.Prefix - PrefixV6 *netip.Prefix - IPAllocation string - NoisePrivateKeyPath string - BaseDomain string - Log compatLogConfig - DisableUpdateCheck bool - Database compatDatabaseConfig - DERP compatDERPConfig - TLS compatTLSConfig - DNSConfig compatDNSConfig - TailcfgDNSConfig *tailcfg.DNSConfig - DNSChallenge compatDNSChallengeConfig - UnixSocket string - UnixSocketPermission fs.FileMode - OIDC compatOIDCConfig - LogTail compatLogTailConfig - Taildrop compatTaildropConfig - AutoUpdate compatAutoUpdateConfig - CLI compatCLIConfig - Policy compatPolicyConfig - Tuning compatTuning -} - -type compatNodeConfig struct { - Expiry time.Duration - Ephemeral compatEphemeralConfig - Routes compatRouteConfig -} - -type compatEphemeralConfig struct{ InactivityTimeout time.Duration } - -type compatRouteConfig struct{ HA compatHARouteConfig } - -type compatHARouteConfig struct{ ProbeInterval, ProbeTimeout time.Duration } - -type compatPreAuthKeysConfig struct{ RevokedRetention time.Duration } - -type compatLogConfig struct { - Format string - Level zerolog.Level -} - -type compatDatabaseConfig struct { - Type string - Debug bool - Gorm compatGormConfig - Sqlite compatSqliteConfig - Postgres compatPostgresConfig -} - -type compatGormConfig struct { - Debug bool - SlowThreshold time.Duration - SkipErrRecordNotFound bool - ParameterizedQueries bool -} - -type compatSqliteConfig struct { - Path string - WriteAheadLog bool - WALAutoCheckPoint int -} - -type compatPostgresConfig struct { - Host string - Port int - Name string - User string - Ssl string - MaxOpenConnections int - MaxIdleConnections int - ConnMaxIdleTimeSecs int -} - -type compatDERPConfig struct { - ServerEnabled bool - AutomaticallyAddEmbeddedDerpRegion bool - ServerRegionID tailcfg.DERPRegionID - ServerRegionCode string - ServerRegionName string - ServerPrivateKeyPath string - ServerVerifyClients bool - STUNAddr string - URLs []url.URL - Paths []string - DERPMap *tailcfg.DERPMap - AutoUpdate bool - UpdateFrequency time.Duration - IPv4 string - IPv6 string -} - -type compatTLSConfig struct { - CertPath string - KeyPath string - ACME compatACMEConfig -} - -type compatACMEConfig struct { - Hostname string - ChallengeType string - HTTPListen string - URL string - Email string - CacheDir string -} - -type compatDNSConfig struct { - MagicDNS bool - BaseDomain string - OverrideLocalDNS bool - Nameservers compatNameservers - SearchDomains []string - ExtraRecords []tailcfg.DNSRecord - ExtraRecordsPath string - Certificates compatCertificatesConfig -} - -type compatNameservers struct { - Global []string - Split map[string][]string -} - -type compatCertificatesConfig struct { - Enabled bool - Zone string -} - -type compatDNSChallengeConfig struct { - PropagationTimeout time.Duration - Resolvers []string - ProviderName string -} - -type compatOIDCConfig struct { - OnlyStartIfOIDCIsAvailable bool - Issuer string - ClientID string - Scope []string - ExtraParams map[string]string - AllowedDomains []string - AllowedUsers []string - AllowedGroups []string - EmailVerifiedRequired bool - UseExpiryFromToken bool - PKCE compatPKCEConfig -} - -type compatPKCEConfig struct { - Enabled bool - Method string -} - -type compatLogTailConfig struct{ Enabled bool } - -type compatTaildropConfig struct{ Enabled bool } - -type compatAutoUpdateConfig struct{ Enabled bool } - -type compatCLIConfig struct { - Address string - Timeout time.Duration - Insecure bool -} - -type compatPolicyConfig struct { - Path string - Mode string -} - -type compatTuning struct { - BatchChangeDelay time.Duration - NodeMapSessionBufferedChanSize int - BatcherWorkers int - RegisterCacheExpiration time.Duration - RegisterCacheMaxEntries int - NodeStoreBatchSize int - NodeStoreBatchTimeout time.Duration -} - -// compatView renders cfg in the golden shape, deriving the values the -// loader used to store resolved. -func compatView(cfg *Config) compatConfig { - pg := cfg.Database.Postgres - oidc := cfg.OIDC - ch := cfg.DNSConfig.Challenge - acme := cfg.TLS.ACME - derp := cfg.DERP.Server - - dbType := cfg.Database.Type - if cfg.Database.IsSQLite() { - dbType = DatabaseSqlite - } - - derpURLs := make([]url.URL, 0, len(cfg.DERP.URLs)) - for _, s := range cfg.DERP.URLs { - u, err := url.Parse(string(s)) - if err == nil { - derpURLs = append(derpURLs, *u) - } - } - - 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: trusted, - Node: compatNodeConfig{ - Expiry: cfg.Node.Expiry, - Ephemeral: compatEphemeralConfig(cfg.Node.Ephemeral), - Routes: compatRouteConfig{HA: compatHARouteConfig(cfg.Node.Routes.HA)}, - }, - PreAuthKeys: compatPreAuthKeysConfig(cfg.PreAuthKeys), - PrefixV4: cfg.Prefixes.V4, - PrefixV6: cfg.Prefixes.V6, - IPAllocation: string(cfg.Prefixes.Allocation), - NoisePrivateKeyPath: cfg.AbsPath(cfg.Noise.PrivateKeyPath), - BaseDomain: cfg.DNSConfig.BaseDomain, - Log: compatLogConfig{ - Format: string(cfg.Log.Format), - Level: cfg.Log.Level.Zerolog(), - }, - DisableUpdateCheck: cfg.DisableUpdateCheck, - Database: compatDatabaseConfig{ - Type: string(dbType), - Debug: cfg.Database.Debug, - Gorm: compatGormConfig{ - Debug: cfg.Database.Debug, - SlowThreshold: time.Duration(cfg.Database.Gorm.SlowThreshold) * time.Millisecond, - SkipErrRecordNotFound: cfg.Database.Gorm.SkipErrRecordNotFound, - ParameterizedQueries: cfg.Database.Gorm.ParameterizedQueries, - }, - Sqlite: compatSqliteConfig{ - Path: cfg.SQLitePath(), - WriteAheadLog: cfg.Database.Sqlite.WriteAheadLog, - WALAutoCheckPoint: cfg.Database.Sqlite.WALAutoCheckPoint, - }, - Postgres: compatPostgresConfig{ - Host: pg.Host, - Port: pg.Port, - Name: pg.Name, - User: pg.User, - Ssl: pg.Ssl, - MaxOpenConnections: pg.MaxOpenConnections, - MaxIdleConnections: pg.MaxIdleConnections, - ConnMaxIdleTimeSecs: pg.ConnMaxIdleTimeSecs, - }, - }, - DERP: compatDERPConfig{ - ServerEnabled: derp.Enabled, - AutomaticallyAddEmbeddedDerpRegion: derp.AutomaticallyAddEmbeddedDerpRegion, - ServerRegionID: derp.RegionID, - ServerRegionCode: derp.RegionCode, - ServerRegionName: derp.RegionName, - ServerPrivateKeyPath: cfg.AbsPath(derp.PrivateKeyPath), - ServerVerifyClients: derp.VerifyClients, - STUNAddr: string(derp.STUNAddr), - URLs: derpURLs, - Paths: cfg.DERP.Paths, - DERPMap: cfg.DERP.DERPMap, - AutoUpdate: cfg.DERP.AutoUpdate, - UpdateFrequency: cfg.DERP.UpdateFrequency, - IPv4: derp.IPv4, - IPv6: derp.IPv6, - }, - TLS: compatTLSConfig{ - CertPath: cfg.AbsPath(cfg.TLS.CertPath), - KeyPath: cfg.AbsPath(cfg.TLS.KeyPath), - ACME: compatACMEConfig{ - Hostname: acme.Hostname, - ChallengeType: string(acme.ChallengeType), - HTTPListen: string(acme.HTTPListen), - URL: string(acme.URL), - Email: acme.Email, - CacheDir: cfg.AbsPath(acme.CacheDir), - }, - }, - DNSConfig: compatDNSConfig{ - MagicDNS: cfg.DNSConfig.MagicDNS, - BaseDomain: cfg.DNSConfig.BaseDomain, - OverrideLocalDNS: cfg.DNSConfig.OverrideLocalDNS, - Nameservers: compatNameservers(cfg.DNSConfig.Nameservers), - SearchDomains: cfg.DNSConfig.SearchDomains, - ExtraRecords: cfg.DNSConfig.ExtraRecords, - ExtraRecordsPath: cfg.DNSConfig.ExtraRecordsPath, - Certificates: compatCertificatesConfig{ - Enabled: cfg.DNSConfig.Certificates.Enabled, - Zone: cfg.DNSConfig.CertificateZone(), - }, - }, - TailcfgDNSConfig: cfg.TailcfgDNSConfig(), - DNSChallenge: compatDNSChallengeConfig{ - PropagationTimeout: ch.PropagationTimeout, - Resolvers: ch.ResolverAddrs(), - ProviderName: ch.ProviderName(), - }, - UnixSocket: cfg.UnixSocket, - UnixSocketPermission: fs.FileMode(cfg.UnixSocketPermission), - OIDC: compatOIDCConfig{ - OnlyStartIfOIDCIsAvailable: oidc.OnlyStartIfOIDCIsAvailable, - Issuer: string(oidc.Issuer), - ClientID: oidc.ClientID, - Scope: oidc.Scope, - ExtraParams: extraParams, - AllowedDomains: oidc.AllowedDomains, - AllowedUsers: oidc.AllowedUsers, - AllowedGroups: oidc.AllowedGroups, - EmailVerifiedRequired: oidc.EmailVerifiedRequired, - UseExpiryFromToken: oidc.UseExpiryFromToken, - PKCE: compatPKCEConfig{Enabled: oidc.PKCE.Enabled, Method: string(oidc.PKCE.Method)}, - }, - LogTail: compatLogTailConfig(cfg.LogTail), - Taildrop: compatTaildropConfig(cfg.Taildrop), - AutoUpdate: compatAutoUpdateConfig(cfg.AutoUpdate), - CLI: compatCLIConfig{ - Address: cfg.CLI.Address, - Timeout: cfg.CLI.Timeout, - Insecure: cfg.CLI.Insecure, - }, - Policy: compatPolicyConfig{Path: cfg.Policy.Path, Mode: string(cfg.Policy.Mode)}, - Tuning: tuning, - } -} - -// loadCompat loads one fixture set the way headscale does, from a temp dir -// so relative paths and the error text do not depend on the checkout. -func loadCompat(t *testing.T, file, envFile string, cli bool) compatRecord { - t.Helper() - - dir := stageCompat(t, file, envFile) - - var buf bytes.Buffer - - oldLogger, oldLevel := log.Logger, zerolog.GlobalLevel() - log.Logger = zerolog.New(&buf) - - // Tests run at ErrorLevel (testlog.go); the loaders then set the level - // from log.level, as in production. - zerolog.SetGlobalLevel(zerolog.TraceLevel) - t.Cleanup(func() { - log.Logger = oldLogger - - zerolog.SetGlobalLevel(oldLevel) - }) - - var cfg *Config - - err := LoadConfig(dir, false) - if err == nil { - if cli { - cfg, err = LoadCLIConfig() - } else { - cfg, err = LoadServerConfig() - } - } - - norm := func(s string) string { return strings.ReplaceAll(s, dir, "$DIR") } - - var rec compatRecord - - for line := range strings.SplitSeq(buf.String(), "\n") { - if line == "" { - continue - } - - var entry struct{ Level, Message string } - - require.NoError(t, json.Unmarshal([]byte(line), &entry)) - - switch entry.Level { - case zerolog.LevelWarnValue: - rec.Warnings = append(rec.Warnings, norm(entry.Message)) - case zerolog.LevelErrorValue: - rec.LogErrors = append(rec.LogErrors, norm(entry.Message)) - } - } - - if err != nil { - rec.Error = norm(err.Error()) - } - - if cfg != nil { - view := compatView(cfg) - if cli { - // The CLI loader sets only what client commands read; compatView - // would derive server values from the zero rest. - view = compatConfig{ - Log: view.Log, - DisableUpdateCheck: view.DisableUpdateCheck, - UnixSocket: view.UnixSocket, - CLI: view.CLI, - } - } - - if view.Tuning.BatcherWorkers == DefaultBatcherWorkers() { - view.Tuning.BatcherWorkers = 0 // depends on runtime.NumCPU - } - - raw, err := json.Marshal(view) - require.NoError(t, err) - - rec.Config = json.RawMessage(norm(string(raw))) - // As its consumer reads it; one no consumer reads stays as written. - secret := func(s Secret, active bool) string { - if !active { - return string(s) - } - - v, err := s.Resolve() - require.NoError(t, err) - - return v - } - rec.Secrets = map[string]string{ - "database.postgres.pass": secret(cfg.Database.Postgres.Pass, cfg.Database.Type == DatabasePostgres), - "oidc.client_secret": secret(cfg.OIDC.ClientSecret, cfg.OIDC.Issuer != ""), - "cli.api_key": secret(cfg.CLI.APIKey, cfg.CLI.Address != ""), - } - maps.DeleteFunc(rec.Secrets, func(_, v string) bool { return v == "" }) - } - - return rec -} - -// stageCompat copies file into a temp dir as config. and sets the -// variables of envFile, returning the dir. -func stageCompat(t *testing.T, file, envFile string) string { - t.Helper() - - dir := t.TempDir() - - if file != "" { - data, err := os.ReadFile(file) - require.NoError(t, err) - require.NoError(t, os.WriteFile(filepath.Join(dir, "config"+filepath.Ext(file)), data, 0o600)) //nolint:gosec // dir is t.TempDir() - } - - if envFile != "" { - raw, err := os.ReadFile(envFile) - require.NoError(t, err) - - // docker --env-file syntax: no quoting, # comments. - for line := range strings.SplitSeq(string(raw), "\n") { - line = strings.TrimSpace(line) - if line == "" || strings.HasPrefix(line, "#") { - continue - } - - k, v, ok := strings.Cut(line, "=") - require.Truef(t, ok, "%s: %q is not KEY=VALUE", envFile, line) - t.Setenv(k, v) - } - } - - return dir -} - -func TestDERPServerPrivateKeyPath(t *testing.T) { - tests := []struct { - name string - derpKey string // derp.server.private_key_path line, if any - wantErr string - }{ - {"missing", "", "derp.server.private_key_path is required"}, - {"distinct", " private_key_path: derp_server_private.key\n", ""}, - {"same as noise", " private_key_path: noise_private.key\n", "must differ from noise.private_key_path"}, - {"same as noise after cleaning", " private_key_path: ./noise_private.key\n", "must differ from noise.private_key_path"}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(`--- -noise: - private_key_path: noise_private.key -server_url: http://127.0.0.1:8080 -prefixes: - v4: 100.64.0.0/10 -database: - type: sqlite3 -dns: - magic_dns: false - override_local_dns: false -derp: - server: - enabled: true - stun_listen_addr: "0.0.0.0:3478" -`+tt.derpKey), 0o600)) - require.NoError(t, LoadConfig(dir, false)) - - err := validateServerConfig() - if tt.wantErr == "" { - require.NoError(t, err) - - return - } - - require.ErrorContains(t, err, tt.wantErr) - }) - } -} - -func TestPolicyMode(t *testing.T) { - for _, tt := range []struct { - mode string - wantErr bool - }{ - {"file", false}, - {"database", false}, - {"databse", true}, - {"File", true}, - {`""`, true}, - } { - t.Run(tt.mode, func(t *testing.T) { - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(`--- -noise: - private_key_path: noise_private.key -server_url: http://127.0.0.1:8080 -prefixes: - v4: 100.64.0.0/10 -database: - type: sqlite3 -dns: - magic_dns: false - override_local_dns: false -policy: - mode: `+tt.mode+"\n"), 0o600)) - require.NoError(t, LoadConfig(dir, false)) - - err := validateServerConfig() - if !tt.wantErr { - require.NoError(t, err) - - return - } - - require.ErrorContains(t, err, "policy.mode has an unsupported value") - }) - } -} - -// TestConfigNotLoaded: an embedder that skips LoadConfig gets an error, not a panic. -func TestConfigNotLoaded(t *testing.T) { - old := src - src = nil - - t.Cleanup(func() { src = old }) - - _, err := LoadServerConfig() - require.ErrorIs(t, err, errConfigNotLoaded) - - _, err = LoadCLIConfig() - require.ErrorIs(t, err, errConfigNotLoaded) - - _, err = DumpConfig() - require.ErrorIs(t, err, errConfigNotLoaded) -} - -// useConfig points src at the defaults plus values until the test ends. -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(plainMap(&d, false), ""), nil)) - require.NoError(t, s.k.Load(confmap.Provider(values, "."), nil)) - - old := src - src = s - - t.Cleanup(func() { src = old }) -} - -// TestConfigDirRelativePaths: relative paths resolve against the directory -// the config file was found in, not the working directory. -func TestConfigDirRelativePaths(t *testing.T) { - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(` -server_url: https://headscale.example.com -noise: - private_key_path: ./noise.key -policy: - path: ./policy.hujson -prefixes: - v4: 100.64.0.0/10 -database: - type: sqlite -dns: - magic_dns: false - override_local_dns: false -`), 0o600)) - - t.Chdir(t.TempDir()) - require.NoError(t, LoadConfig(dir, false)) - - cfg, err := LoadServerConfig() - require.NoError(t, err) - - assert.Equal(t, "./noise.key", cfg.Noise.PrivateKeyPath) - assert.Equal(t, filepath.Join(dir, "noise.key"), cfg.AbsPath(cfg.Noise.PrivateKeyPath)) - assert.Equal(t, "./policy.hujson", cfg.Policy.Path) - assert.Equal(t, filepath.Join(dir, "policy.hujson"), cfg.AbsPath(cfg.Policy.Path)) -} - -// TestSQLiteMemoryPath: ":memory:" names SQLite's in-memory database, not a -// file to resolve against the config directory. -func TestSQLiteMemoryPath(t *testing.T) { - for _, withFile := range []bool{false, true} { - t.Run(fmt.Sprintf("file=%t", withFile), func(t *testing.T) { - for k, v := range map[string]string{ - "HEADSCALE_SERVER_URL": "https://headscale.example.com", - "HEADSCALE_NOISE_PRIVATE_KEY_PATH": "noise.key", - "HEADSCALE_PREFIXES_V4": "100.64.0.0/10", - "HEADSCALE_DATABASE_TYPE": "sqlite", - "HEADSCALE_DATABASE_SQLITE_PATH": ":memory:", - "HEADSCALE_DNS_MAGIC_DNS": "false", - "HEADSCALE_DNS_OVERRIDE_LOCAL_DNS": "false", - } { - t.Setenv(k, v) - } - - dir := t.TempDir() - if withFile { - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), nil, 0o600)) - } - - require.NoError(t, LoadConfig(dir, false)) - - cfg, err := LoadServerConfig() - require.NoError(t, err) - assert.Equal(t, ":memory:", cfg.SQLitePath()) - assert.True(t, filepath.IsAbs(cfg.AbsPath(cfg.Noise.PrivateKeyPath)), cfg.AbsPath(cfg.Noise.PrivateKeyPath)) - }) - } -} - // TestDerivedValues pins the values computed from what the configuration // says, which the loader stores as written. func TestDerivedValues(t *testing.T) { @@ -3312,201 +488,3 @@ 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_test.go b/hscontrol/types/config_values_test.go index afd933e78..3620a1af4 100644 --- a/hscontrol/types/config_values_test.go +++ b/hscontrol/types/config_values_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/juanfont/headscale/hscontrol/util" "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -25,6 +26,8 @@ func TestConfigValues(t *testing.T) { // wantErr is a substring of the ConfigError's Reason. wantErr string wantSee string + // wantIs is a sentinel the error wraps. + wantIs error }{ {name: "filemode/0770", dst: new(FileMode), in: "0770", want: FileMode(0o770)}, {name: "filemode/0o770", dst: new(FileMode), in: "0o770", want: FileMode(0o770)}, @@ -39,7 +42,7 @@ func TestConfigValues(t *testing.T) { {name: "httpurl/no host", dst: new(HTTPURL), in: "https://", wantErr: "is not an http:// or https:// URL"}, {name: "httpurl/port without host", dst: new(HTTPURL), in: "https://:443/x", wantErr: "is not an http:// or https:// URL"}, {name: "httpurl/http port without host", dst: new(HTTPURL), in: "http://:80", wantErr: "is not an http:// or https:// URL"}, - {name: "httpurl/unparsable", dst: new(HTTPURL), in: "https://alice:pw?@h", wantErr: "is not an http:// or https:// URL"}, + {name: "httpurl/unparsable", dst: new(HTTPURL), in: "https://alice:pw?@h", wantErr: "is not an http:// or https:// URL", wantIs: errURLSyntax}, {name: "listenaddr/empty", dst: new(ListenAddr), in: "", want: ListenAddr("")}, {name: "listenaddr/port only", dst: new(ListenAddr), in: ":8080", want: ListenAddr(":8080")}, @@ -82,7 +85,7 @@ func TestConfigValues(t *testing.T) { {name: "pkce/plain", dst: new(PKCEMethod), in: "plain", want: PKCEMethod("plain")}, {name: "pkce/S256", dst: new(PKCEMethod), in: "S256", want: PKCEMethod("S256")}, {name: "pkce/empty", dst: new(PKCEMethod), in: "", want: PKCEMethod("")}, - {name: "pkce/case-sensitive", dst: new(PKCEMethod), in: "s256", wantErr: "has an unsupported value"}, + {name: "pkce/case-sensitive", dst: new(PKCEMethod), in: "s256", wantErr: "has an unsupported value", wantIs: errInvalidPKCEMethod}, {name: "policy/file", dst: new(PolicyMode), in: "file", want: PolicyMode("file")}, {name: "policy/database", dst: new(PolicyMode), in: "database", want: PolicyMode("database")}, @@ -123,6 +126,10 @@ func TestConfigValues(t *testing.T) { require.Empty(t, ce.Current, "the loader adds the key") require.Equal(t, tt.wantSee, ce.See) + if tt.wantIs != nil { + require.ErrorIs(t, err, tt.wantIs) + } + if isEnum { require.Equal(t, enum.Allowed(), ce.Allowed) } @@ -199,7 +206,7 @@ func TestHTTPURLErrorHidesValue(t *testing.T) { func requireHidden(t *testing.T, err error, hidden string) { t.Helper() - walkErrTree(err, func(e error) { + util.WalkErrTree(err, func(e error) { assert.NotContains(t, e.Error(), hidden) b, jerr := json.Marshal(e) diff --git a/hscontrol/types/derp_config_test.go b/hscontrol/types/derp_config_test.go deleted file mode 100644 index 11cdcb4f8..000000000 --- a/hscontrol/types/derp_config_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package types - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -// 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", - }}) - - _, 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 900e94b51..c7aebfad3 100644 --- a/hscontrol/types/listener.go +++ b/hscontrol/types/listener.go @@ -58,110 +58,3 @@ func PortFromAddr(addr string) (int, error) { return int(p), nil } - -// listenersOverlap reports whether two parsed TCP listen addresses would -// compete for the same kernel socket. Mirrors kernel rules: -// - different ports → false -// - same port + a wildcard host on either side → true -// - same port + identical specific host → true -// - same port + different specific hosts → false -func listenersOverlap(aHost string, aPort int, bHost string, bPort int) bool { - if aPort != bPort { - return false - } - - if isWildcardHost(aHost) || isWildcardHost(bHost) { - return true - } - - return aHost == bHost -} - -func isWildcardHost(h string) bool { - switch h { - case "", "0.0.0.0", "::", "[::]": - return true - } - - return false -} - -// validateListenerCollisions records a *ConfigError for each pair of -// configured TCP listeners that would bind the same kernel socket. The -// ACME HTTP-01 challenge listener is only considered when a hostname is -// set and the HTTP-01 challenge is selected. -// -// Parsing happens up-front, once per listener: a malformed address -// 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 (c *Config) validateListenerCollisions(v *configValidator) { - type spec struct { - key, addr string - host string - port int - parsed bool - active bool - } - - listenAddr := string(c.Addr) - metricsAddr := string(c.MetricsAddr) - acmeAddr := string(c.TLS.ACME.HTTPListen) - - listeners := []spec{ - {key: "listen_addr", addr: listenAddr, active: listenAddr != ""}, - {key: "metrics_listen_addr", addr: metricsAddr, active: metricsAddr != ""}, - { - key: "tls.acme.http_listen", - addr: acmeAddr, - active: acmeAddr != "" && - c.TLS.ACME.Hostname != "" && - c.TLS.ACME.ChallengeType == HTTP01ChallengeType, - }, - } - - for i := range listeners { - l := &listeners[i] - if !l.active { - continue - } - - port, err := PortFromAddr(l.addr) - if err != nil { - v.Add(&ConfigError{ - Reason: "cannot parse " + l.key, - Current: []KV{{l.key, l.addr}}, - Detail: err.Error(), - Hint: `use host:port form, e.g. "0.0.0.0:8080"`, - }) - - continue - } - - host, _, _ := net.SplitHostPort(l.addr) - l.host = host - l.port = port - l.parsed = true - } - - for i := range listeners { - for j := i + 1; j < len(listeners); j++ { - a, b := listeners[i], listeners[j] - if !a.parsed || !b.parsed { - continue - } - - if !listenersOverlap(a.host, a.port, b.host, b.port) { - continue - } - - v.Add(&ConfigError{ - Reason: fmt.Sprintf("%s and %s would bind the same TCP socket", a.key, b.key), - Current: []KV{{a.key, a.addr}}, - ConflictsWith: []KV{{b.key, b.addr}}, - Hint: "give each listener a distinct port, or bind them to different non-wildcard hosts", - See: "https://headscale.net/stable/ref/tls/", - }) - } - } -} diff --git a/hscontrol/types/listener_test.go b/hscontrol/types/listener_test.go index 26908fa81..21325d0e3 100644 --- a/hscontrol/types/listener_test.go +++ b/hscontrol/types/listener_test.go @@ -49,74 +49,6 @@ func TestPortFromAddr(t *testing.T) { } } -func TestListenersOverlap(t *testing.T) { - tests := []struct { - name string - aHost string - aPort int - bHost string - bPort int - wantOverlap bool - }{ - {"different-ports", "", 80, "", 443, false}, - {"same-port-wildcard", "", 80, "", 80, true}, - {"wildcard-vs-loopback-same-port", "0.0.0.0", 80, "127.0.0.1", 80, true}, - {"loopback-vs-wildcard-same-port", "127.0.0.1", 80, "0.0.0.0", 80, true}, - {"ipv6-wildcard-vs-numeric", "[::]", 80, "0.0.0.0", 80, true}, - {"different-specific-hosts-same-port", "192.168.1.1", 80, "192.168.1.2", 80, false}, - {"same-specific-host-same-port", "127.0.0.1", 80, "127.0.0.1", 80, true}, - {"same-specific-host-different-port", "127.0.0.1", 80, "127.0.0.1", 81, false}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := listenersOverlap(tt.aHost, tt.aPort, tt.bHost, tt.bPort) - assert.Equal(t, tt.wantOverlap, got) - }) - } -} - -// TestValidateListenerCollisions_BlamesParseFailure pins each parse -// error to the listener whose address is malformed, even when paired -// with a well-formed sibling. -func TestValidateListenerCollisions_BlamesParseFailure(t *testing.T) { - tests := []struct { - name string - bad string // YAML key whose addr is malformed - good string // sibling key with a valid addr - badAddr string - }{ - {"bad-listen_addr", "listen_addr", "metrics_listen_addr", "garbage"}, - {"bad-metrics_listen_addr", "metrics_listen_addr", "listen_addr", "no-port"}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - // 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{} - cfg.validateListenerCollisions(v) - - err := v.Err() - require.Error(t, err) - - errs := ConfigErrors(err) - require.Len(t, errs, 1, "expected exactly one parse error") - assert.Equal(t, "cannot parse "+tt.bad, errs[0].Reason) - require.Len(t, errs[0].Current, 1) - assert.Equal(t, tt.bad, errs[0].Current[0].Key) - assert.Equal(t, tt.badAddr, errs[0].Current[0].Value) - }) - } -} - func TestListenerBindError_IsEADDRINUSE(t *testing.T) { inner := &net.OpError{ Op: "listen", @@ -155,19 +87,3 @@ func TestListenerBindError_Render(t *testing.T) { got, ) } - -func TestIsWildcardHost(t *testing.T) { - wildcards := []string{"", "0.0.0.0", "::", "[::]"} - for _, h := range wildcards { - t.Run("wildcard-"+h, func(t *testing.T) { - assert.True(t, isWildcardHost(h)) - }) - } - - specifics := []string{"127.0.0.1", "192.168.1.1", "::1", "example.com"} - for _, h := range specifics { - t.Run("specific-"+h, func(t *testing.T) { - assert.False(t, isWildcardHost(h)) - }) - } -} diff --git a/hscontrol/types/secret.go b/hscontrol/types/secret.go index c8d8b319c..8d21e2169 100644 --- a/hscontrol/types/secret.go +++ b/hscontrol/types/secret.go @@ -3,9 +3,6 @@ package types import ( "errors" "fmt" - "strings" - - hsdns "github.com/juanfont/headscale/hscontrol/dns" ) var errSecretEmpty = errors.New("file is empty") @@ -20,13 +17,3 @@ func ResolveSecret(key, value string) (string, error) { return secret, nil } - -// keepSecret leaves a file: secret unread, for checking a config without -// reading the files it names; checks that need the secret skip it. -func keepSecret(_, value string) (string, error) { - if strings.HasPrefix(value, "file:") { - return value, hsdns.ErrSecretUnread - } - - return value, nil -} diff --git a/hscontrol/types/testdata/compat/hsic-default.golden.json b/hscontrol/types/testdata/compat/hsic-default.golden.json deleted file mode 100644 index a77eedc4f..000000000 --- a/hscontrol/types/testdata/compat/hsic-default.golden.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "error": "Fatal config error: configuration key private_key_path was removed in 0.23 ($DIR/config.yaml:1:1)\n hint: use noise.private_key_path\n" -} diff --git a/hscontrol/types/testdata/compat/hsic-postgres.golden.json b/hscontrol/types/testdata/compat/hsic-postgres.golden.json deleted file mode 100644 index a77eedc4f..000000000 --- a/hscontrol/types/testdata/compat/hsic-postgres.golden.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "error": "Fatal config error: configuration key private_key_path was removed in 0.23 ($DIR/config.yaml:1:1)\n hint: use noise.private_key_path\n" -} diff --git a/hscontrol/util/util.go b/hscontrol/util/util.go index d0120e051..e0c5268ac 100644 --- a/hscontrol/util/util.go +++ b/hscontrol/util/util.go @@ -6,6 +6,7 @@ import ( "net/netip" "net/url" "os" + "reflect" "regexp" "strconv" "strings" @@ -285,3 +286,39 @@ func GenerateRegistrationKey() (string, error) { return registerKeyPrefix + rands.HexString(registerKeyLength), nil } + +// WalkErrTree calls fn on err and every error below it, wrapped and joined +// ones included. It follows each error's own Unwrap: errors.As would jump +// past the head and miss a join inside a single-Unwrap chain. +func WalkErrTree(err error, fn func(error)) { + if err == nil { + return + } + + fn(err) + + switch x := err.(type) { //nolint:errorlint // walks the tree itself + case interface{ Unwrap() error }: + WalkErrTree(x.Unwrap(), fn) + case interface{ Unwrap() []error }: + for _, e := range x.Unwrap() { + WalkErrTree(e, fn) + } + } +} + +// 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 +}