diff --git a/.github/actions/headscale-up/action.yml b/.github/actions/headscale-up/action.yml index 585e808c7..83c13366e 100644 --- a/.github/actions/headscale-up/action.yml +++ b/.github/actions/headscale-up/action.yml @@ -51,10 +51,8 @@ runs: server_url: ${URL} listen_addr: 0.0.0.0:8443 metrics_listen_addr: 127.0.0.1:9090 - grpc_listen_addr: 127.0.0.1:50443 unix_socket: /tmp/hs/headscale.sock unix_socket_permission: "0770" - private_key_path: /tmp/hs/private.key noise: private_key_path: /tmp/hs/noise.key prefixes: diff --git a/.golangci.yaml b/.golangci.yaml index daa86c31b..edbb7e33f 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -104,6 +104,12 @@ linters: - common-false-positives - legacy - std-error-handling + 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 + linters: + - goconst paths: - third_party$ - builtin$ diff --git a/cmd/headscale/cli/check.go b/cmd/headscale/cli/check.go index 6cc446717..233a1ad6d 100644 --- a/cmd/headscale/cli/check.go +++ b/cmd/headscale/cli/check.go @@ -11,7 +11,6 @@ import ( policyv2 "github.com/juanfont/headscale/hscontrol/policy/v2" "github.com/juanfont/headscale/hscontrol/types" "github.com/spf13/cobra" - "github.com/spf13/viper" ) var errCheckFailed = errors.New("check failed") @@ -24,7 +23,7 @@ func init() { checkCmd.AddCommand(checkPolicyCmd) for _, c := range []*cobra.Command{checkConfigCmd, checkAllCmd} { - c.Flags().StringP("file", "f", "", "Path to the config file (default: found as serve finds it: -c, HEADSCALE_CONFIG, /etc/headscale, ~/.headscale, .)") + c.Flags().StringP("file", "f", "", "Path to the config file (default: found as serve finds it: -c, HEADSCALE_CONFIG, /etc/headscale, $XDG_CONFIG_HOME/headscale, headscale under each $XDG_CONFIG_DIRS entry, ~/.headscale, .)") checkCmd.AddCommand(c) } @@ -74,9 +73,7 @@ var checkConfigCmd = &cobra.Command{ Long: ` Config validates the configuration: its syntax, keys and values. It does not check the files the configuration names; "headscale check all" does. - Without --file it finds the configuration as "headscale serve" does. Secret - files named by oidc.client_secret_path and the DNS provider *_path settings - are read while the configuration loads.`, + Without --file it finds the configuration as "headscale serve" does.`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { _, src, err := loadCheckedConfig(cmd) @@ -96,7 +93,8 @@ var checkAllCmd = &cobra.Command{ Long: ` All checks the configuration, then each file it names, and lists every file with ok, FAIL (the error below it) or skip. Skip is a key file that startup - creates when it is missing. It exits non-zero when any line is FAIL. + creates when it is missing, or a "file:" secret. It exits non-zero + when any line is FAIL. A derp.paths file that fails to parse is FAIL here, although startup only skips it with a warning.`, @@ -130,6 +128,8 @@ var checkAllCmd = &cobra.Command{ printCheckLine(out, "ok", f.Key, f.Path, width, nil) case f.CreatedAtStartup && errors.Is(err, os.ErrNotExist): printCheckLine(out, "skip", f.Key, f.Path+" (missing; startup creates it)", width, nil) + case errors.Is(err, config.ErrSecretNotRead): + printCheckLine(out, "skip", f.Key, f.Path+" ("+err.Error()+")", width, nil) default: failed++ @@ -180,7 +180,7 @@ func loadCheckedConfig(cmd *cobra.Command) (*types.Config, string, error) { err := loadConfig(path) - src := viper.ConfigFileUsed() + src := types.ConfigFile() if src == "" { src = "(defaults and environment)" } @@ -189,7 +189,7 @@ func loadCheckedConfig(cmd *cobra.Command) (*types.Config, string, error) { return nil, src, err } - cfg, err := types.LoadServerConfig() + cfg, err := types.CheckServerConfig() return cfg, src, err } diff --git a/cmd/headscale/cli/check_test.go b/cmd/headscale/cli/check_test.go index bc139a81d..491947599 100644 --- a/cmd/headscale/cli/check_test.go +++ b/cmd/headscale/cli/check_test.go @@ -273,6 +273,91 @@ func TestCheckAll(t *testing.T) { }) } +// No check reads a file: secret: its file may exist only for the service +// (systemd credentials, sops, agenix). check all lists each one as skipped. +func TestCheckSecretsUnread(t *testing.T) { + const acmeDNS = ` challenge: + provider: + name: acmedns +` + + t.Run("missing secret files", func(t *testing.T) { + dir := t.TempDir() + missing := filepath.Join(dir, "missing") + + for k, v := range map[string]string{ + "HEADSCALE_OIDC_ISSUER": "https://idp.example.com", + "HEADSCALE_OIDC_CLIENT_ID": "headscale", + "HEADSCALE_OIDC_CLIENT_SECRET": "file:" + missing, + "HEADSCALE_DATABASE_TYPE": "postgres", + "HEADSCALE_DATABASE_POSTGRES_PASS": "file:" + missing, + "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", + "HEADSCALE_DNS_CHALLENGE_PROVIDER_API_TOKEN": "file:" + missing, + } { + t.Setenv(k, v) + } + + cfg := writeServerConfig(t, dir, "") + + _, err := runCheck(t, checkConfigCmd, cfg) + require.NoError(t, err) + + out, err := runCheck(t, checkAllCmd, cfg) + require.NoError(t, err) + + lines := strings.Split(out, "\n") + for _, key := range []string{"oidc.client_secret", "database.postgres.pass", "dns.challenge.provider.api_token"} { + require.Contains(t, lines, fmt.Sprintf("skip %-32s %s (secret, not read)", key, missing)) + } + }) + + // A URL from a file cannot be validated unread. + for name, provider := range map[string]string{ + "account": acmeDNS + ` username: user + password: pass + subdomain: reg + server_url: "file:%s" +`, + "registration": acmeDNS + ` config: + laptop.example.com: + username: user + password: pass + subdomain: reg + server_url: "file:%s" +`, + } { + t.Run("acme-dns "+name+" url from file", func(t *testing.T) { + dir := t.TempDir() + cfg := writeServerConfig(t, dir, fmt.Sprintf(provider, filepath.Join(dir, "missing"))) + + for _, c := range []*cobra.Command{checkConfigCmd, checkAllCmd} { + _, err := runCheck(t, c, cfg) + require.NoError(t, err, c.Name()) + } + }) + } + + // Setting names match case-insensitively, as the decoder matches them; + // only the environment's JSON keeps their case. + for _, field := range []string{"server_url", "SERVER_URL", "Server_URL"} { + t.Run("acme-dns registration "+field+" from file in env json", func(t *testing.T) { + dir := t.TempDir() + + t.Setenv("HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME", "acmedns") + t.Setenv("HEADSCALE_DNS_CHALLENGE_PROVIDER", fmt.Sprintf( + `{"config":{"Node.Example.com":{"username":"user","password":"pass","subdomain":"reg",%q:"file:%s"}}}`, + field, filepath.Join(dir, "missing"))) + + cfg := writeServerConfig(t, dir, "") + + for _, c := range []*cobra.Command{checkConfigCmd, checkAllCmd} { + _, err := runCheck(t, c, cfg) + require.NoError(t, err, c.Name()) + } + }) + } +} + func TestCheckFileCommands(t *testing.T) { dir := t.TempDir() diff --git a/cmd/headscale/cli/dump_config.go b/cmd/headscale/cli/dump_config.go index 18b702a13..9ee6038cc 100644 --- a/cmd/headscale/cli/dump_config.go +++ b/cmd/headscale/cli/dump_config.go @@ -2,9 +2,10 @@ package cli import ( "fmt" + "os" + "github.com/juanfont/headscale/hscontrol/types" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func init() { @@ -16,7 +17,12 @@ 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 { - err := viper.WriteConfigAs("/etc/headscale/config.dump.yaml") + out, err := types.DumpConfig() + if err != nil { + return fmt.Errorf("dumping config: %w", err) + } + + err = os.WriteFile("/etc/headscale/config.dump.yaml", out, 0o600) if err != nil { return fmt.Errorf("dumping config: %w", err) } diff --git a/cmd/headscale/cli/root.go b/cmd/headscale/cli/root.go index 20215699c..97f6aa314 100644 --- a/cmd/headscale/cli/root.go +++ b/cmd/headscale/cli/root.go @@ -11,7 +11,6 @@ import ( "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/tcnksm/go-latest" ) @@ -54,8 +53,9 @@ func skipsInitConfig(args []string) bool { } // loadConfig loads path, else the file -c names, else $HEADSCALE_CONFIG, -// else the first config.* in /etc/headscale, ~/.headscale or ".". Without a -// file it loads defaults and environment only. +// 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 == "" { @@ -99,13 +99,11 @@ func initConfig() { zerolog.SetGlobalLevel(zerolog.Disabled) } - logFormat := viper.GetString("log.format") - if logFormat == types.JSONLogFormat { + if types.LogFormat() == types.JSONLogFormat { log.Logger = log.Output(os.Stdout) } - disableUpdateCheck := viper.GetBool("disable_check_updates") - if !disableUpdateCheck && !machineOutput { + if !types.UpdateCheckDisabled() && !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 a7e53fc49..cc1eb600f 100644 --- a/cmd/headscale/cli/root_test.go +++ b/cmd/headscale/cli/root_test.go @@ -466,7 +466,7 @@ func TestLoadConfig(t *testing.T) { t.Cleanup(func() { cfgFile = old }) require.NoError(t, loadConfig(tt.path)) - require.Equal(t, tt.want, viper.ConfigFileUsed()) + require.Equal(t, tt.want, types.ConfigFile()) }) } } diff --git a/cmd/headscale/cli/serve_test.go b/cmd/headscale/cli/serve_test.go index bba34ac5a..291ebd9be 100644 --- a/cmd/headscale/cli/serve_test.go +++ b/cmd/headscale/cli/serve_test.go @@ -39,7 +39,7 @@ func TestClassifyServeError(t *testing.T) { name: "eaddrinuse-named-port", err: &types.ListenerBindError{ Listener: "ACME HTTP-01 challenge", - YAMLKey: "tls_letsencrypt_listen", + YAMLKey: "tls.acme.http_listen", Addr: ":http", Err: &net.OpError{Op: "listen", Net: "tcp", Err: syscall.EADDRINUSE}, }, @@ -77,12 +77,12 @@ func TestClassifyServeError(t *testing.T) { { name: "wrapped-eaddrinuse-still-classified", err: fmt.Errorf("serve: %w", &types.ListenerBindError{ - Listener: "gRPC", - YAMLKey: "grpc_listen_addr", - Addr: "0.0.0.0:50443", + Listener: "metrics", + YAMLKey: "metrics_listen_addr", + Addr: "127.0.0.1:9090", Err: &net.OpError{Op: "listen", Net: "tcp", Err: syscall.EADDRINUSE}, }), - wantHintSubstr: []string{"sudo ss -tlnp 'sport = :50443'"}, + wantHintSubstr: []string{"sudo ss -tlnp 'sport = :9090'"}, }, { name: "eaddrinuse-unparseable-addr-omits-port", diff --git a/cmd/headscale/headscale_test.go b/cmd/headscale/headscale_test.go index ce47cbe6e..5be393027 100644 --- a/cmd/headscale/headscale_test.go +++ b/cmd/headscale/headscale_test.go @@ -7,8 +7,6 @@ import ( "testing" "github.com/juanfont/headscale/hscontrol/types" - "github.com/juanfont/headscale/hscontrol/util" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -32,19 +30,7 @@ func TestConfigFileLoading(t *testing.T) { err = types.LoadConfig(cfgFile, true) require.NoError(t, err) - // Test that config file was interpreted correctly - assert.Equal(t, "http://127.0.0.1:8080", viper.GetString("server_url")) - assert.Equal(t, "127.0.0.1:8080", viper.GetString("listen_addr")) - assert.Equal(t, "127.0.0.1:9090", viper.GetString("metrics_listen_addr")) - assert.Equal(t, "sqlite", viper.GetString("database.type")) - assert.Equal(t, "/var/lib/headscale/db.sqlite", viper.GetString("database.sqlite.path")) - assert.Empty(t, viper.GetString("tls.acme.hostname")) - assert.Equal(t, ":http", viper.GetString("tls.acme.http_listen")) - assert.Equal(t, "HTTP-01", viper.GetString("tls.acme.challenge_type")) - mode, err := util.ParseFileMode(viper.GetString("unix_socket_permission")) - require.NoError(t, err) - assert.Equal(t, fs.FileMode(0o770), mode) - assert.False(t, viper.GetBool("logtail.enabled")) + assertExampleConfig(t) } func TestConfigLoading(t *testing.T) { @@ -64,17 +50,25 @@ func TestConfigLoading(t *testing.T) { err = types.LoadConfig(tmpDir, false) require.NoError(t, err) - // Test that config file was interpreted correctly - assert.Equal(t, "http://127.0.0.1:8080", viper.GetString("server_url")) - assert.Equal(t, "127.0.0.1:8080", viper.GetString("listen_addr")) - assert.Equal(t, "127.0.0.1:9090", viper.GetString("metrics_listen_addr")) - assert.Equal(t, "sqlite", viper.GetString("database.type")) - assert.Equal(t, "/var/lib/headscale/db.sqlite", viper.GetString("database.sqlite.path")) - assert.Empty(t, viper.GetString("tls.acme.hostname")) - assert.Equal(t, ":http", viper.GetString("tls.acme.http_listen")) - assert.Equal(t, "HTTP-01", viper.GetString("tls.acme.challenge_type")) - mode, err := util.ParseFileMode(viper.GetString("unix_socket_permission")) - require.NoError(t, err) - assert.Equal(t, fs.FileMode(0o770), mode) - assert.False(t, viper.GetBool("logtail.enabled")) + assertExampleConfig(t) +} + +// assertExampleConfig checks the loaded config-example.yaml was interpreted +// correctly. +func assertExampleConfig(t *testing.T) { + t.Helper() + + cfg, err := types.LoadServerConfig() + require.NoError(t, err) + + assert.Equal(t, "http://127.0.0.1:8080", cfg.ServerURL) + assert.Equal(t, "127.0.0.1:8080", cfg.Addr) + assert.Equal(t, "127.0.0.1:9090", cfg.MetricsAddr) + assert.Equal(t, types.DatabaseSqlite, cfg.Database.Type) + assert.Equal(t, "/var/lib/headscale/db.sqlite", cfg.Database.Sqlite.Path) + assert.Empty(t, cfg.TLS.ACME.Hostname) + assert.Equal(t, ":http", cfg.TLS.ACME.HTTPListen) + assert.Equal(t, "HTTP-01", cfg.TLS.ACME.ChallengeType) + assert.Equal(t, fs.FileMode(0o770), cfg.UnixSocketPermission) + assert.False(t, cfg.LogTail.Enabled) } diff --git a/config-example.yaml b/config-example.yaml index 1e78d0b17..1e2460152 100644 --- a/config-example.yaml +++ b/config-example.yaml @@ -1,7 +1,10 @@ --- -# headscale will look for a configuration file named `config.yaml` (or `config.json`) in the following order: +# headscale will look for a configuration file named `config.yaml` (or `config.json`, +# `config.yml`, `config.hujson`) in the following order: # # - `/etc/headscale` +# - `$XDG_CONFIG_HOME/headscale` (default `~/.config/headscale`) +# - `headscale` under each `$XDG_CONFIG_DIRS` entry (default `/etc/xdg`) # - `~/.headscale` # - current working directory @@ -144,7 +147,6 @@ node: # Default key expiry for non-tagged nodes, regardless of registration method # (auth key, CLI, web auth). Tagged nodes are exempt and never expire. # - # This is the base default. OIDC can override this via oidc.expiry. # If a client explicitly requests a specific expiry, the client value is used. # # Setting the value to "0" means no default expiry (nodes never expire unless @@ -221,6 +223,7 @@ database: # port: 5432 # name: headscale # user: foo + # # A secret, or "file:" to read it from a file (see oidc.client_secret). # pass: bar # max_open_conns: 10 # max_idle_conns: 10 @@ -379,11 +382,11 @@ dns: # resolvers: [] # acmedns, cloudflare, gigahost, hetzner, powerdns or rfc2136. The other - # keys are settings of that provider; add _path to a key to read it from - # a file. + # keys are settings of that provider; a string setting can be read from + # a file with "file:" (see oidc.client_secret). # provider: # name: cloudflare - # api_token_path: /run/credentials/headscale.service/cloudflare-token + # api_token: "file:${CREDENTIALS_DIRECTORY}/cloudflare-token" # # acmedns needs a registration per certificate name under config, and # each _acme-challenge name CNAMEd to its registration's fulldomain. A @@ -419,12 +422,11 @@ unix_socket_permission: "0770" # client_id: "your-oidc-client-id" # # # Client secret generated by the identity provider -# # Note: client_secret and client_secret_path are mutually exclusive. # client_secret: "your-oidc-client-secret" -# # Alternatively, set `client_secret_path` to read the secret from the file. -# # It resolves environment variables, making integration to systemd's -# # `LoadCredential` straightforward: -# client_secret_path: "${CREDENTIALS_DIRECTORY}/oidc_client_secret" +# # Alternatively, "file:" reads the secret from the file, as for +# # every secret setting. It resolves environment variables, making +# # integration to systemd's `LoadCredential` straightforward: +# # client_secret: "file:${CREDENTIALS_DIRECTORY}/oidc_client_secret" # # # Use the expiry from the token received from OpenID when the user logged # # in. This will typically lead to frequent need to reauthenticate and should diff --git a/hscontrol/api/v1/policy.go b/hscontrol/api/v1/policy.go index 2588b435b..1235d8e22 100644 --- a/hscontrol/api/v1/policy.go +++ b/hscontrol/api/v1/policy.go @@ -73,7 +73,7 @@ func registerPolicy(api huma.API, b Backend) { return out, nil case types.PolicyModeFile: - absPath := util.AbsolutePathFromConfigPath(b.Cfg.Policy.Path) + absPath := util.AbsolutePath(b.Cfg.ConfigDir, b.Cfg.Policy.Path) f, err := os.Open(absPath) if err != nil { diff --git a/hscontrol/api/v2/acl.go b/hscontrol/api/v2/acl.go index 31e28de6f..6e835ce03 100644 --- a/hscontrol/api/v2/acl.go +++ b/hscontrol/api/v2/acl.go @@ -221,7 +221,7 @@ func currentPolicy(b Backend) ([]byte, error) { return []byte(p.Data), nil case types.PolicyModeFile: - path := util.AbsolutePathFromConfigPath(b.Cfg.Policy.Path) + path := util.AbsolutePath(b.Cfg.ConfigDir, b.Cfg.Policy.Path) if path == "" { return []byte(defaultPolicy), nil } diff --git a/hscontrol/config/files.go b/hscontrol/config/files.go index a4f0925dd..73d053625 100644 --- a/hscontrol/config/files.go +++ b/hscontrol/config/files.go @@ -17,7 +17,12 @@ import ( "tailscale.com/types/key" ) -var ErrSameKey = errors.New("noise and DERP private keys must differ") +var ( + ErrSameKey = errors.New("noise and DERP private keys must differ") + + // ErrSecretNotRead is the check of a file: secret: no check reads one. + ErrSecretNotRead = errors.New("secret, not read") +) // File is one file the configuration names. type File struct { @@ -38,7 +43,7 @@ func Files(cfg *types.Config) []File { var files []File if cfg.Policy.Mode == types.PolicyModeFile && cfg.Policy.Path != "" { - p := util.AbsolutePathFromConfigPath(cfg.Policy.Path) + p := util.AbsolutePath(cfg.ConfigDir, cfg.Policy.Path) files = append(files, File{Key: "policy.path", Path: p, Check: func() error { return CheckPolicyFile(p) }}) } @@ -73,6 +78,13 @@ func Files(cfg *types.Config) []File { }) } + for _, kv := range cfg.UnreadSecrets { + files = append(files, File{ + Key: kv.Key, Path: fmt.Sprint(kv.Value), + Check: func() error { return ErrSecretNotRead }, + }) + } + return files } diff --git a/hscontrol/db/db_test.go b/hscontrol/db/db_test.go index 8718a12bf..ed4211ae5 100644 --- a/hscontrol/db/db_test.go +++ b/hscontrol/db/db_test.go @@ -41,8 +41,6 @@ database: type: sqlite3 sqlite: path: headscale.db - gorm: - prepare_stmt: true `), 0o600)) require.NoError(t, types.LoadConfig(configPath, true)) diff --git a/hscontrol/db/policy.go b/hscontrol/db/policy.go index da17c140b..c7ba091cf 100644 --- a/hscontrol/db/policy.go +++ b/hscontrol/db/policy.go @@ -76,7 +76,7 @@ func PolicyBytes(tx *gorm.DB, cfg *types.Config) ([]byte, error) { return nil, nil } - absPath := util.AbsolutePathFromConfigPath(path) + absPath := util.AbsolutePath(cfg.ConfigDir, path) return os.ReadFile(absPath) diff --git a/hscontrol/derp/derp.go b/hscontrol/derp/derp.go index c6bea7bf0..709311574 100644 --- a/hscontrol/derp/derp.go +++ b/hscontrol/derp/derp.go @@ -21,7 +21,6 @@ import ( "github.com/juanfont/headscale/hscontrol/types" "github.com/juanfont/headscale/hscontrol/util" - "github.com/spf13/viper" "tailscale.com/tailcfg" ) @@ -270,12 +269,12 @@ func GetDERPMap(cfg types.DERPConfig) (*tailcfg.DERPMap, error) { } derpMap := mergeDERPMaps(derpMaps) - shuffleDERPMap(derpMap) + shuffleDERPMap(derpMap, cfg.ShuffleSeed) return derpMap, errors.Join(errs...) } -func shuffleDERPMap(dm *tailcfg.DERPMap) { +func shuffleDERPMap(dm *tailcfg.DERPMap, seed string) { if dm == nil || len(dm.Regions) == 0 { return } @@ -293,7 +292,7 @@ func shuffleDERPMap(dm *tailcfg.DERPMap) { // A new generator per call gives the same input the same order. Clients // close their home relay when its node order changes, so a refresh that // finds no change must not reorder anything. - rng := rand.New(rand.NewSource(derpShuffleSeed())) //nolint:gosec // weak random is fine for DERP scrambling + rng := rand.New(rand.NewSource(derpShuffleSeed(seed))) //nolint:gosec // weak random is fine for DERP scrambling for _, id := range ids { region := dm.Regions[id] @@ -313,12 +312,12 @@ var ( derpSeedMu sync.Mutex ) -func derpShuffleSeed() int64 { +func derpShuffleSeed(seed string) int64 { derpSeedMu.Lock() defer derpSeedMu.Unlock() if !derpSeedSet { - seed := cmp.Or(viper.GetString("dns.base_domain"), time.Now().String()) + seed = cmp.Or(seed, time.Now().String()) derpSeed = int64(crc64.Checksum([]byte(seed), crc64Table)) //nolint:gosec // only a seed, overflow is harmless derpSeedSet = true } diff --git a/hscontrol/derp/derp_test.go b/hscontrol/derp/derp_test.go index 7915461a1..82b4a6f2a 100644 --- a/hscontrol/derp/derp_test.go +++ b/hscontrol/derp/derp_test.go @@ -12,7 +12,6 @@ import ( "github.com/google/go-cmp/cmp" "github.com/juanfont/headscale/hscontrol/types" - "github.com/spf13/viper" "github.com/stretchr/testify/require" "tailscale.com/tailcfg" ) @@ -250,14 +249,10 @@ func TestShuffleDERPMapDeterministic(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - viper.Set("dns.base_domain", tt.baseDomain) - - defer viper.Reset() - resetDerpRandomForTesting() testMap := tt.derpMap.View().AsStruct() - shuffleDERPMap(testMap) + shuffleDERPMap(testMap, tt.baseDomain) if diff := cmp.Diff(tt.expected, testMap); diff != "" { t.Errorf("Shuffled DERP map doesn't match expected (-expected +actual):\n%s", diff) @@ -269,10 +264,6 @@ func TestShuffleDERPMapDeterministic(t *testing.T) { // TestGetDERPMapKeepsOrderAcrossRefreshes: clients close their home relay when // its node order changes, so a refresh of the same input must keep the order. func TestGetDERPMapKeepsOrderAcrossRefreshes(t *testing.T) { - viper.Set("dns.base_domain", "stable.example.com") - - defer viper.Reset() - resetDerpRandomForTesting() source := &tailcfg.DERPMap{Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{}} @@ -286,11 +277,11 @@ func TestGetDERPMapKeepsOrderAcrossRefreshes(t *testing.T) { source.Regions[id+1] = region } - first, err := GetDERPMap(types.DERPConfig{DERPMap: source}) + first, err := GetDERPMap(types.DERPConfig{DERPMap: source, ShuffleSeed: "stable.example.com"}) require.NoError(t, err) for range 5 { - next, err := GetDERPMap(types.DERPConfig{DERPMap: source}) + next, err := GetDERPMap(types.DERPConfig{DERPMap: source, ShuffleSeed: "stable.example.com"}) require.NoError(t, err) if diff := cmp.Diff(first, next); diff != "" { @@ -346,13 +337,12 @@ func TestShuffleDERPMapEdgeCases(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - shuffleDERPMap(tt.derpMap) + shuffleDERPMap(tt.derpMap, "") }) } } func TestShuffleDERPMapWithoutBaseDomain(t *testing.T) { - viper.Reset() resetDerpRandomForTesting() derpMap := &tailcfg.DERPMap{ @@ -372,7 +362,7 @@ func TestShuffleDERPMapWithoutBaseDomain(t *testing.T) { } original := derpMap.View().AsStruct() - shuffleDERPMap(derpMap) + shuffleDERPMap(derpMap, "") if len(derpMap.Regions) != 1 || len(derpMap.Regions[1].Nodes) != 4 { t.Error("Shuffle corrupted DERP map structure") diff --git a/hscontrol/dns/provider.go b/hscontrol/dns/provider.go index dd4170c9e..6b49f9c15 100644 --- a/hscontrol/dns/provider.go +++ b/hscontrol/dns/provider.go @@ -49,14 +49,29 @@ type Provider interface { // providers is the compiled-in provider registry. Providers are decoded // from configuration through their json tags, so adding one is one line // plus its module; the set is curated because each provider is a -// dependency to keep current and its quirks become ours. -var providers = map[string]func() Provider{ - "acmedns": func() Provider { return &acmeDNS{served: make(map[string][2]acmeDNSValue)} }, - "cloudflare": func() Provider { return new(cloudflare.Provider) }, - "gigahost": func() Provider { return new(gigahost.Provider) }, - "hetzner": func() Provider { return new(hetzner.Provider) }, - "powerdns": func() Provider { return new(powerdns.Provider) }, - "rfc2136": func() Provider { return new(rfc2136.Provider) }, +// dependency to keep current and its quirks become ours. required lists +// the settings the provider's own code cannot work without, so a missing +// one fails at startup instead of at the first certificate. env names the +// variable the provider's own code reads a required setting from when the +// setting is unset. +var providers = map[string]struct { + build func() Provider + required []string + env map[string]string +}{ + // validate checks the accounts. + "acmedns": {build: func() Provider { return &acmeDNS{served: make(map[string][2]acmeDNSValue)} }}, + // api_token authenticates writes; zone_token only zone lookups. + "cloudflare": {build: func() Provider { return new(cloudflare.Provider) }, required: []string{"api_token"}}, + "gigahost": {build: func() Provider { return new(gigahost.Provider) }, required: []string{"api_key"}}, + "hetzner": { + build: func() Provider { return new(hetzner.Provider) }, + required: []string{"api_token"}, + env: map[string]string{"api_token": "LIBDNS_HETZNER_TOKEN"}, //nolint:gosec // a variable's name, not a credential + }, + "powerdns": {build: func() Provider { return new(powerdns.Provider) }, required: []string{"server_url", "api_token"}}, + // Every request is TSIG-signed. + "rfc2136": {build: func() Provider { return new(rfc2136.Provider) }, required: []string{"server", "key_name", "key_alg", "key"}}, } var ( @@ -68,11 +83,11 @@ var ( errAcmeDNSOneAccount = errors.New("acme-dns with one account answers every name's challenge from one record; " + "node certificates need a registration per name under config") errAcmeDNSShared = errors.New("acme-dns registration mapped to more than one name") -) -// pathSuffix marks a setting whose value is read from a file, keeping -// secrets out of the configuration file. -const pathSuffix = "_path" + // ErrSecretUnread is returned, with the value as given, by a secret + // func that leaves a file: secret unread; checks of it are skipped. + ErrSecretUnread = errors.New("secret left unread") +) // ProviderNames returns the names of the compiled-in providers. func ProviderNames() []string { @@ -84,20 +99,21 @@ func ProviderNames() []string { // settings holds the provider's keys as written in the configuration // file. get looks up a single key and is consulted for every field of // the provider, because environment variables only reach individual -// keys, never a map. Any string field can instead be read from a file -// named by "_path". Unknown keys are an error so a typo does not -// silently leave a credential empty. +// keys, never a map. secret resolves every string setting, nested ones +// included, so any of them can be read from a file. Unknown keys are an +// error so a typo does not silently leave a credential empty. func NewProvider( name string, settings map[string]any, get func(key string) (any, bool), + secret func(key, value string) (string, error), ) (Provider, error) { - newProvider, ok := providers[strings.ToLower(name)] + entry, ok := providers[strings.ToLower(name)] if !ok { return nil, fmt.Errorf("%w: %q", ErrUnknownProvider, name) } - provider := newProvider() + provider := entry.build() input := make(map[string]any, len(settings)) for k, v := range settings { @@ -106,44 +122,37 @@ func NewProvider( delete(input, "name") + // The values left unread, for validate to skip. Kept by value, as a + // setting's name may be spelt in any case. + unread := map[string]bool{} + read := func(key, value string) (string, error) { + s, err := secret(key, value) + if errors.Is(err, ErrSecretUnread) { + unread[value] = true + + return value, nil + } + + return s, err + } + fields := jsonFields(provider) for tag, kind := range fields { if v, ok := get(tag); ok { input[tag] = v } - pathKey := tag + pathSuffix - if _, isField := fields[pathKey]; isField || kind != reflect.String { + value, ok := input[tag] + if _, isMap := value.(map[string]any); !ok || (kind != reflect.String && !isMap) { continue } - if v, ok := get(pathKey); ok { - input[pathKey] = v - } - - path, ok := input[pathKey] - if !ok { - continue - } - - if _, both := input[tag]; both { - return nil, fmt.Errorf("%w: %s and %s are mutually exclusive", ErrProviderConfig, tag, pathKey) - } - - value, err := os.ReadFile(os.ExpandEnv(fmt.Sprint(path))) + value, err := resolveSecrets(tag, value, read) if err != nil { - return nil, fmt.Errorf("%w: reading %s: %w", ErrProviderConfig, pathKey, err) + return nil, fmt.Errorf("%w: %w", ErrProviderConfig, err) } - input[tag] = strings.TrimSpace(string(value)) - delete(input, pathKey) - } - - // Every provider needs a credential or a server: a block with only a - // name is a secret that was not set, and would fail at the first - // certificate instead of at startup. - if len(input) == 0 { - return nil, fmt.Errorf("%w for %s: no settings", ErrProviderConfig, name) + input[tag] = value } decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ @@ -162,8 +171,25 @@ func NewProvider( return nil, fmt.Errorf("%w for %s: %w", ErrProviderConfig, name, err) } - if v, ok := provider.(interface{ validate() error }); ok { - err = v.validate() + // After decoding, so a misspelt setting is reported as that. An unread + // file: value counts as set. + var missing []string + + for _, k := range entry.required { + // os.Getenv("") is "" for a setting without a fallback variable. + if v := input[k]; (v == nil || v == "") && os.Getenv(entry.env[k]) == "" { + missing = append(missing, k) + } + } + + if len(missing) > 0 { + return nil, fmt.Errorf("%w for %s: missing %s", ErrProviderConfig, name, strings.Join(missing, ", ")) + } + + if v, ok := provider.(interface { + validate(unread map[string]bool) error + }); ok { + err = v.validate(unread) if err != nil { return nil, fmt.Errorf("%w for %s: %w", ErrProviderConfig, name, err) } @@ -172,6 +198,83 @@ func NewProvider( return provider, nil } +// resolveSecrets resolves every string in value, down through nested +// settings such as acme-dns's per-name registrations; key names the +// setting in errors. Maps are copied, as they may be the caller's. +func resolveSecrets(key string, value any, secret func(key, value string) (string, error)) (any, error) { + switch value := value.(type) { + case string: + return secret(key, value) + case map[string]any: + out := make(map[string]any, len(value)) + + for k, v := range value { + r, err := resolveSecrets(key+"."+k, v, secret) + if err != nil { + return nil, err + } + + out[k] = r + } + + return out, nil + } + + return value, nil +} + +// IsProviderSetting reports whether a compiled-in provider has a setting +// named name, nested ones such as acme-dns's registrations included. +func IsProviderSetting(name string) bool { + return providerSetting(name, func(reflect.Type) bool { return true }) +} + +// IsProviderMapSetting reports whether a compiled-in provider has a map +// setting named name, whose keys are names, such as acme-dns's +// registrations, rather than settings. +func IsProviderMapSetting(name string) bool { + return providerSetting(name, func(t reflect.Type) bool { return t.Kind() == reflect.Map }) +} + +func providerSetting(name string, match func(reflect.Type) bool) bool { + for _, entry := range providers { + if hasSetting(reflect.TypeOf(entry.build()), name, match) { + return true + } + } + + return false +} + +// hasSetting matches names case-insensitively, as the decoder does. +func hasSetting(t reflect.Type, name string, match func(reflect.Type) bool) bool { + for t.Kind() == reflect.Pointer || t.Kind() == reflect.Map || t.Kind() == reflect.Slice { + t = t.Elem() + } + + if t.Kind() != reflect.Struct { + return false + } + + for _, f := range reflect.VisibleFields(t) { + tag, _, _ := strings.Cut(f.Tag.Get("json"), ",") + if !f.IsExported() || tag == "" || tag == "-" { + continue + } + + ft := f.Type + for ft.Kind() == reflect.Pointer { + ft = ft.Elem() + } + + if (strings.EqualFold(tag, name) && match(ft)) || hasSetting(f.Type, name, match) { + return true + } + } + + return false +} + // jsonFields returns the json key and kind of each configurable field of // the provider struct. func jsonFields(provider Provider) map[string]reflect.Kind { @@ -313,7 +416,8 @@ func (p *acmeDNS) CheckIsolation() error { sub := p.Configs[name].Subdomain if other, shared := names[sub]; shared { - return fmt.Errorf("%w: %s and %s share registration %s", errAcmeDNSShared, other, name, sub) + // The registration is not named: it may have been read from a file. + return fmt.Errorf("%w: %s and %s share a registration", errAcmeDNSShared, other, name) } names[sub] = name @@ -336,8 +440,8 @@ func (p *acmeDNS) registration(zone, name string) string { // validate reports at startup what acmedns would only report at the first // certificate. It builds requests without checking the URL, so a bad -// server_url would panic. -func (p *acmeDNS) validate() error { +// server_url would panic. An unread server_url is checked once it is read. +func (p *acmeDNS) validate(unread map[string]bool) error { accounts := []acmedns.DomainConfig{{ Username: p.Username, Password: p.Password, @@ -351,8 +455,9 @@ func (p *acmeDNS) validate() error { for _, a := range accounts { u, err := url.Parse(a.ServerURL) missing := a.Username == "" || a.Password == "" || a.Subdomain == "" + badURL := !unread[a.ServerURL] && (err != nil || u.Host == "") - if missing || err != nil || u.Host == "" { + if missing || badURL { return errAcmeDNSAccount } } diff --git a/hscontrol/dns/provider_test.go b/hscontrol/dns/provider_test.go index 37dd2843b..180c6eb25 100644 --- a/hscontrol/dns/provider_test.go +++ b/hscontrol/dns/provider_test.go @@ -7,6 +7,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "strings" "sync" "testing" "time" @@ -24,6 +25,69 @@ import ( func noEnv(string) (any, bool) { return nil, false } +// readSecret stands in for the caller's file: resolver. +func readSecret(_, value string) (string, error) { + path, ok := strings.CutPrefix(value, "file:") + if !ok { + return value, nil + } + + b, err := os.ReadFile(path) + + return strings.TrimSpace(string(b)), err +} + +func TestIsProviderSetting(t *testing.T) { + assert.True(t, IsProviderSetting("api_token")) + assert.True(t, IsProviderSetting("config")) + assert.True(t, IsProviderSetting("fulldomain"), "a registration setting") + assert.True(t, IsProviderSetting("API_Token"), "matched as the decoder matches") + assert.True(t, IsProviderMapSetting("config")) + assert.False(t, IsProviderMapSetting("api_token")) + assert.False(t, IsProviderSetting("api_token_path")) +} + +// unreadSecret stands in for a caller that checks without reading files. +func unreadSecret(_, value string) (string, error) { + if strings.HasPrefix(value, "file:") { + return value, ErrSecretUnread + } + + return value, nil +} + +// TestNewProviderUnreadSecrets pins that a check without the files skips +// only what needs their contents. +func TestNewProviderUnreadSecrets(t *testing.T) { + account := func(serverURL string) map[string]any { + return map[string]any{"username": "user", "password": "file:/pass", "subdomain": "reg", "server_url": serverURL} + } + + tests := []struct { + name string + settings map[string]any + wantErr bool + }{ + {name: "server_url from file", settings: account("file:/url")}, + {name: "registration server_url from file", settings: map[string]any{"config": map[string]any{"laptop.example.com": account("file:/url")}}}, + {name: "literal server_url still checked", settings: account("auth.acme-dns.io"), wantErr: true}, + {name: "literal registration server_url still checked", settings: map[string]any{"config": map[string]any{"laptop.example.com": account("auth.acme-dns.io")}}, wantErr: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := NewProvider("acmedns", tt.settings, noEnv, unreadSecret) + if tt.wantErr { + require.ErrorIs(t, err, ErrProviderConfig) + + return + } + + require.NoError(t, err) + }) + } +} + func TestNewProvider(t *testing.T) { secret := filepath.Join(t.TempDir(), "token") require.NoError(t, os.WriteFile(secret, []byte("from-file\n"), 0o600)) @@ -64,21 +128,21 @@ func TestNewProvider(t *testing.T) { }, }, { - name: "secret from path", + name: "secret from file", provider: "cloudflare", - settings: map[string]any{"api_token_path": secret}, + settings: map[string]any{"api_token": "file:" + secret}, want: &cloudflare.Provider{APIToken: "from-file"}, }, { - name: "value and path both set", + name: "path key is unknown", provider: "cloudflare", - settings: map[string]any{"api_token": "tok", "api_token_path": secret}, + settings: map[string]any{"api_token_path": secret}, wantErr: ErrProviderConfig, }, { - name: "unreadable path", + name: "unreadable file", provider: "cloudflare", - settings: map[string]any{"api_token_path": filepath.Join(t.TempDir(), "missing")}, + settings: map[string]any{"api_token": "file:" + filepath.Join(t.TempDir(), "missing")}, wantErr: ErrProviderConfig, }, { @@ -95,13 +159,13 @@ func TestNewProvider(t *testing.T) { { name: "gigahost", provider: "gigahost", - settings: map[string]any{"api_key_path": secret}, + settings: map[string]any{"api_key": "file:" + secret}, want: &gigahost.Provider{APIKey: "from-file"}, }, { name: "hetzner", provider: "hetzner", - settings: map[string]any{"api_token_path": secret}, + settings: map[string]any{"api_token": "file:" + secret}, want: &hetzner.Provider{APIToken: "from-file"}, }, { @@ -114,10 +178,10 @@ func TestNewProvider(t *testing.T) { name: "acmedns", provider: "acmedns", settings: map[string]any{ - "username": "user", - "password_path": secret, - "subdomain": "reg", - "server_url": "https://auth.acme-dns.io", + "username": "user", + "password": "file:" + secret, + "subdomain": "reg", + "server_url": "https://auth.acme-dns.io", }, want: &acmeDNS{ Username: "user", @@ -150,6 +214,42 @@ func TestNewProvider(t *testing.T) { served: map[string][2]acmeDNSValue{}, }, }, + { + name: "acmedns per domain from file", + provider: "acmedns", + settings: map[string]any{"config": map[string]any{ + "node.ts.example.com": map[string]any{ + "username": "user", + "password": "file:" + secret, + "subdomain": "reg", + "server_url": "https://auth.acme-dns.io", + }, + }}, + want: &acmeDNS{ + Configs: map[string]acmedns.DomainConfig{ + "node.ts.example.com": { + Username: "user", + Password: "from-file", + Subdomain: "reg", + ServerURL: "https://auth.acme-dns.io", + }, + }, + served: map[string][2]acmeDNSValue{}, + }, + }, + { + name: "acmedns per domain file missing", + provider: "acmedns", + settings: map[string]any{"config": map[string]any{ + "node.ts.example.com": map[string]any{ + "username": "user", + "password": "file:" + filepath.Join(t.TempDir(), "missing"), + "subdomain": "reg", + "server_url": "https://auth.acme-dns.io", + }, + }}, + wantErr: ErrProviderConfig, + }, { name: "acmedns server_url without scheme", provider: "acmedns", @@ -181,7 +281,7 @@ func TestNewProvider(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := NewProvider(tt.provider, tt.settings, noEnv) + got, err := NewProvider(tt.provider, tt.settings, noEnv, readSecret) if tt.wantErr != nil { require.ErrorIs(t, err, tt.wantErr) @@ -281,7 +381,7 @@ func TestAcmeDNSRefusesEviction(t *testing.T) { srv := newFakeAcmeDNS(t) - p, err := NewProvider("acmedns", srv.registration(), noEnv) + p, err := NewProvider("acmedns", srv.registration(), noEnv, readSecret) require.NoError(t, err) ctx := context.Background() diff --git a/hscontrol/mapper/mapper_test.go b/hscontrol/mapper/mapper_test.go index 90c71e32c..9957be4b1 100644 --- a/hscontrol/mapper/mapper_test.go +++ b/hscontrol/mapper/mapper_test.go @@ -1291,7 +1291,8 @@ func TestCertDomains(t *testing.T) { "subdomain": "reg", "server_url": "https://auth.acme-dns.io", }, - }}, func(string) (any, bool) { return nil, false }) + }}, func(string) (any, bool) { return nil, false }, + func(_, v string) (string, error) { return v, nil }) require.NoError(t, err) tests := []struct { diff --git a/hscontrol/state/state.go b/hscontrol/state/state.go index 55328d8d5..d95e4cb47 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 viper) still work. + // This ensures tests that create Config directly (without LoadConfig) 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 d4e94fead..819305015 100644 --- a/hscontrol/types/config.go +++ b/hscontrol/types/config.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io/fs" - "maps" "net" "net/netip" "net/url" @@ -17,19 +16,17 @@ 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/prometheus/common/model" "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "github.com/spf13/viper" "go4.org/netipx" "tailscale.com/net/tsaddr" "tailscale.com/tailcfg" "tailscale.com/types/dnstype" "tailscale.com/util/dnsname" - "tailscale.com/util/set" ) const ( @@ -42,16 +39,16 @@ const ( ) var ( - errOidcMutuallyExclusive = errors.New("oidc_client_secret and oidc_client_secret_path are mutually exclusive") 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 or oidc.client_secret_path 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") 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 @@ -168,6 +165,13 @@ type Config struct { Policy PolicyConfig Tuning Tuning + + // ConfigDir resolves relative paths such as policy.path. + ConfigDir string `json:"-"` + + // UnreadSecrets are the file: secrets [CheckServerConfig] left unread, + // each as key and path. + UnreadSecrets []KV `json:"-"` } type DNSConfig struct { @@ -318,48 +322,6 @@ type ACMEConfig struct { CacheDir string } -// legacyTLSKeys maps each tls key to the flat key it replaced. The flat -// keys keep working, so existing configs and NixOS modules still run. -var legacyTLSKeys = map[string]string{ - "tls.cert_path": "tls_cert_path", - "tls.key_path": "tls_key_path", - "tls.acme.hostname": "tls_letsencrypt_hostname", - "tls.acme.challenge_type": "tls_letsencrypt_challenge_type", - "tls.acme.http_listen": "tls_letsencrypt_listen", - "tls.acme.cache_dir": "tls_letsencrypt_cache_dir", - "tls.acme.url": "acme_url", - "tls.acme.email": "acme_email", -} - -var tlsDefaults = map[string]string{ - "tls.acme.challenge_type": HTTP01ChallengeType, - "tls.acme.http_listen": ":http", - "tls.acme.cache_dir": "/var/lib/headscale/cache", -} - -// warnLegacyTLSKeys points at the tls keys for flat keys in the config -// file that turn TLS on. NixOS writes the flat keys even with TLS off, and -// the HEADSCALE_TLS_* variables name both the flat and the tls keys. -func warnLegacyTLSKeys(depr *deprecator) { - legacyTLSOn := func(key string) bool { return viper.InConfig(key) && viper.GetString(key) != "" } - if !legacyTLSOn("tls_letsencrypt_hostname") && !legacyTLSOn("tls_cert_path") { - return - } - - for _, key := range slices.Sorted(maps.Keys(legacyTLSKeys)) { - if viper.InConfig(legacyTLSKeys[key]) { - depr.warnNoAlias(key, legacyTLSKeys[key]) - } - } -} - -// tlsKey reads a tls setting, falling back to the flat key it replaced and -// then to its default. The defaults are not viper defaults: those would -// shadow the flat key. -func tlsKey(key string) string { - return cmp.Or(viper.GetString(key), viper.GetString(legacyTLSKeys[key]), tlsDefaults[key]) -} - type PKCEConfig struct { Enabled bool Method string @@ -396,6 +358,8 @@ type DERPConfig struct { UpdateFrequency time.Duration IPv4 string IPv6 string + // ShuffleSeed orders DERP nodes the same on every server of a tailnet. + ShuffleSeed string `json:"-"` } type LogTailConfig struct { @@ -511,23 +475,23 @@ func validatePKCEMethod(method string) error { // error or, worse, resolve to an unintended provider), or a missing client // id/secret. func validateOIDCConfig() error { - err := validatePKCEMethod(viper.GetString("oidc.pkce.method")) + err := validatePKCEMethod(src.String("oidc.pkce.method")) if err != nil { return err } - issuer := viper.GetString("oidc.issuer") + issuer := src.String("oidc.issuer") u, err := url.Parse(issuer) if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" { return fmt.Errorf("%w: got %q", errOIDCIssuerInvalid, issuer) } - if viper.GetString("oidc.client_id") == "" { + if src.String("oidc.client_id") == "" { return errOIDCClientIDRequired } - if viper.GetString("oidc.client_secret") == "" && viper.GetString("oidc.client_secret_path") == "" { + if src.String("oidc.client_secret") == "" { return errOIDCClientSecretRequired } @@ -560,135 +524,72 @@ func (c *Config) ServesRoute(p netip.Prefix) bool { (c.PrefixV6 == nil || !c.PrefixV6.Contains(addr)) } -// LoadConfig prepares and loads the Headscale configuration into Viper. -// This means it sets the default values, reads the configuration file and -// environment variables, and handles deprecated configuration options. -// It has to be called before [LoadServerConfig] and [LoadCLIConfig]. -// The configuration is not validated and the caller should check for errors -// using a validation function. +// 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 validate the +// values. func LoadConfig(path string, isFile bool) error { - if isFile { - viper.SetConfigFile(path) - } else { - viper.SetConfigName("config") + configFile = findConfigFile(path, isFile, os.Environ()) - if path == "" { - viper.AddConfigPath("/etc/headscale/") - viper.AddConfigPath("$HOME/.headscale") - viper.AddConfigPath(".") - } else { - // For testing - viper.AddConfigPath(path) - } - } - - envPrefix := "headscale" - viper.SetEnvPrefix(envPrefix) - viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - viper.AutomaticEnv() - - viper.SetDefault("policy.mode", "file") - - viper.SetDefault("log.level", "info") - viper.SetDefault("log.format", TextLogFormat) - - viper.SetDefault("dns.magic_dns", true) - viper.SetDefault("dns.base_domain", "") - viper.SetDefault("dns.override_local_dns", true) - viper.SetDefault("dns.nameservers.global", []string{}) - viper.SetDefault("dns.nameservers.split", map[string]string{}) - viper.SetDefault("dns.search_domains", []string{}) - viper.SetDefault("dns.certificates.enabled", false) - - viper.SetDefault("dns.challenge.propagation_timeout", "2m") - - viper.SetDefault("derp.server.enabled", false) - viper.SetDefault("derp.server.verify_clients", true) - viper.SetDefault("derp.server.stun.enabled", true) - viper.SetDefault("derp.server.automatically_add_embedded_derp_region", true) - viper.SetDefault("derp.update_frequency", "3h") - - viper.SetDefault("unix_socket", "/var/run/headscale/headscale.sock") - viper.SetDefault("unix_socket_permission", "0o770") - - viper.SetDefault("cli.timeout", "5s") - viper.SetDefault("cli.insecure", false) - - viper.SetDefault("database.postgres.ssl", false) - viper.SetDefault("database.postgres.max_open_conns", 10) - viper.SetDefault("database.postgres.max_idle_conns", 10) - viper.SetDefault("database.postgres.conn_max_idle_time_secs", 3600) - - viper.SetDefault("database.sqlite.write_ahead_log", true) - viper.SetDefault("database.sqlite.wal_autocheckpoint", 1000) // SQLite default - - viper.SetDefault("oidc.scope", []string{oidc.ScopeOpenID, "profile", "email"}) - viper.SetDefault("oidc.only_start_if_oidc_is_available", true) - viper.SetDefault("oidc.use_expiry_from_token", false) - viper.SetDefault("oidc.pkce.enabled", false) - viper.SetDefault("oidc.pkce.method", "S256") - viper.SetDefault("oidc.email_verified_required", true) - - viper.SetDefault("logtail.enabled", false) - viper.SetDefault("taildrop.enabled", true) - viper.SetDefault("auto_update.enabled", false) - - viper.SetDefault("node.expiry", "0") - viper.SetDefault("node.ephemeral.inactivity_timeout", "120s") - viper.SetDefault("preauth_keys.revoked_retention", "168h") - viper.SetDefault("node.routes.ha.probe_interval", "10s") - viper.SetDefault("node.routes.ha.probe_timeout", "5s") - - viper.SetDefault("tuning.notifier_send_timeout", "800ms") - viper.SetDefault("tuning.batch_change_delay", "800ms") - viper.SetDefault("tuning.node_mapsession_buffered_chan_size", 30) - viper.SetDefault("tuning.node_store_batch_size", defaultNodeStoreBatchSize) - viper.SetDefault("tuning.node_store_batch_timeout", "500ms") - - viper.SetDefault("prefixes.allocation", string(IPAllocationStrategySequential)) - - err := viper.ReadInConfig() + s, err := newConfigSource(path, isFile, os.Environ()) if err != nil { - if _, ok := errors.AsType[viper.ConfigFileNotFoundError](err); ok { - log.Warn().Msg("no config file found, using defaults") - return nil - } - - return fmt.Errorf("fatal error reading config file: %w", err) + return err } + for _, w := range s.Warnings() { + log.Warn().Msg(w) + } + + src = s + return nil } -// resolveEphemeralInactivityTimeout resolves the ephemeral inactivity timeout -// from config, supporting both the new key (node.ephemeral.inactivity_timeout) -// and the old key (ephemeral_node_inactivity_timeout) for backwards compatibility. -// -// We cannot use viper.RegisterAlias here because aliases silently ignore -// config values set under the alias name. If a user writes the new key in -// their config file, RegisterAlias redirects reads to the old key (which -// has no config value), returning only the default and discarding the -// user's setting. -func resolveEphemeralInactivityTimeout() time.Duration { - // New key takes precedence if explicitly set in config. - if viper.IsSet("node.ephemeral.inactivity_timeout") && - viper.GetString("node.ephemeral.inactivity_timeout") != "" { - return viper.GetDuration("node.ephemeral.inactivity_timeout") +// configDir is where relative paths in the configuration are resolved: the +// config file's directory, or the working directory without a file. +func configDir() string { + dir := "." + if src.File() != "" { + dir = filepath.Dir(src.File()) } - // Fall back to old key for backwards compatibility. - if viper.IsSet("ephemeral_node_inactivity_timeout") { - return viper.GetDuration("ephemeral_node_inactivity_timeout") + abs, err := filepath.Abs(dir) + if err != nil { + return dir } - // Default - return viper.GetDuration("node.ephemeral.inactivity_timeout") + return abs +} + +// ConfigFile returns the file the last [LoadConfig] loaded or failed to +// load, or "" when it found none. +func ConfigFile() string { return configFile } + +// LogFormat returns the configured log.format. +func LogFormat() string { return src.String("log.format") } + +// UpdateCheckDisabled reports whether disable_check_updates is set. +func UpdateCheckDisabled() bool { return src.Bool("disable_check_updates") } + +// 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()) } // resolveNodeExpiry parses the node.expiry config value. // Returns 0 if set to "0" (no default expiry) or on parse failure. func resolveNodeExpiry() time.Duration { - value := viper.GetString("node.expiry") + value := src.String("node.expiry") if value == "" || value == "0" { return 0 } @@ -742,7 +643,7 @@ var boolConfigKeys = []string{ // Validate raw values before GetBool can discard conversion errors. func validateBoolConfig(v *configValidator, keys ...string) { for _, key := range keys { - value := viper.Get(key) + value := src.Get(key) if value == nil { continue } @@ -759,60 +660,16 @@ func validateBoolConfig(v *configValidator, keys ...string) { } } -// validateServerConfigInto runs every viper-level validation rule +// validateServerConfigInto runs every config-level validation rule // against the provided validator. Caller owns the validator and is // responsible for inspecting v.Err() once all sub-builders have run. func validateServerConfigInto(v *configValidator) { validateBoolConfig(v, boolConfigKeys...) - depr := deprecator{seen: make(set.Set[string])} - - // Register aliases for backward compatibility - // Has to be called _after_ viper.ReadInConfig() - // https://github.com/spf13/viper/issues/560 - - // Alias the old ACL Policy path with the new configuration option. - depr.fatalIfNewKeyIsNotUsed("policy.path", "acl_policy_path") - - // Move dns_config -> dns - depr.fatalIfNewKeyIsNotUsed("dns.magic_dns", "dns_config.magic_dns") - depr.fatalIfNewKeyIsNotUsed("dns.base_domain", "dns_config.base_domain") - depr.fatalIfNewKeyIsNotUsed("dns.override_local_dns", "dns_config.override_local_dns") - depr.fatalIfNewKeyIsNotUsed("dns.nameservers.global", "dns_config.nameservers") - depr.fatalIfNewKeyIsNotUsed("dns.nameservers.split", "dns_config.restricted_nameservers") - depr.fatalIfNewKeyIsNotUsed("dns.search_domains", "dns_config.domains") - depr.fatalIfNewKeyIsNotUsed("dns.extra_records", "dns_config.extra_records") - depr.fatal("dns.use_username_in_magic_dns") - depr.fatal("dns_config.use_username_in_magic_dns") - - // Removed since version v0.26.0 - depr.fatal("oidc.strip_email_domain") - depr.fatal("oidc.map_legacy_users") - - // Removed since v0.29.0: `randomize_client_port` moved to the ACL - // policy as a top-level `randomizeClientPort` field, matching the - // Tailscale-hosted control plane schema. Per-node `nodeAttrs` - // entries granting `https://tailscale.com/cap/randomize-client-port` - // also work. - depr.fatalWithHint("randomize_client_port", - `Set "randomizeClientPort": true at the top level of your policy file `+ - `(see policy.path / policy.mode), or grant the cap per-node via a `+ - `"nodeAttrs" entry. See CHANGELOG.md (BREAKING / Configuration).`) - - // Deprecated: ephemeral_node_inactivity_timeout -> node.ephemeral.inactivity_timeout - depr.warnNoAlias("node.ephemeral.inactivity_timeout", "ephemeral_node_inactivity_timeout") - - // Removed: oidc.expiry -> node.expiry - depr.fatalIfSet("oidc.expiry", "node.expiry") - - // Ignored: database.gorm.prepare_stmt. A warning, not a fatal, so configs - // that shipped with it set still start. - depr.warn("database.gorm.prepare_stmt") - // 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 viper.GetString("oidc.issuer") != "" { + if src.String("oidc.issuer") != "" { err := validateOIDCConfig() if err != nil { v.Add(&ConfigError{ @@ -824,34 +681,32 @@ func validateServerConfigInto(v *configValidator) { } } - if viper.IsSet("dns.extra_records") && viper.IsSet("dns.extra_records_path") { + if src.IsSet("dns.extra_records") && src.IsSet("dns.extra_records_path") { v.Add(&ConfigError{ Reason: "dns.extra_records and dns.extra_records_path are mutually exclusive", Current: []KV{ - {"dns.extra_records_path", viper.GetString("dns.extra_records_path")}, + {"dns.extra_records_path", src.String("dns.extra_records_path")}, {"dns.extra_records", ""}, }, Hint: "keep one (a path is recommended for production); remove the other", }) } - warnLegacyTLSKeys(&depr) - - if tlsKey("tls.acme.hostname") != "" && - (tlsKey("tls.cert_path") != "" || tlsKey("tls.key_path") != "") { + if src.String("tls.acme.hostname") != "" && + (src.String("tls.cert_path") != "" || src.String("tls.key_path") != "") { v.Add(&ConfigError{ Reason: "tls.acme.hostname and tls.cert_path/tls.key_path are mutually exclusive", - Current: []KV{{"tls.acme.hostname", tlsKey("tls.acme.hostname")}}, + Current: []KV{{"tls.acme.hostname", src.String("tls.acme.hostname")}}, ConflictsWith: []KV{ - {"tls.cert_path", tlsKey("tls.cert_path")}, - {"tls.key_path", tlsKey("tls.key_path")}, + {"tls.cert_path", src.String("tls.cert_path")}, + {"tls.key_path", src.String("tls.key_path")}, }, Hint: "choose one TLS strategy and unset the other (Let's Encrypt OR a static keypair)", See: "https://headscale.net/stable/ref/tls/", }) } - if viper.GetString("noise.private_key_path") == "" { + if src.String("noise.private_key_path") == "" { 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)", @@ -859,9 +714,9 @@ func validateServerConfigInto(v *configValidator) { }) } - if tlsKey("tls.acme.hostname") != "" && - tlsKey("tls.acme.challenge_type") == TLSALPN01ChallengeType && - !strings.HasSuffix(viper.GetString("listen_addr"), ":443") { + if src.String("tls.acme.hostname") != "" && + src.String("tls.acme.challenge_type") == TLSALPN01ChallengeType && + !strings.HasSuffix(src.String("listen_addr"), ":443") { // this is only a warning because there could be something sitting in front of headscale that redirects the traffic (e.g. an iptables rule) log.Warn(). Msg("Warning: when using tls.acme.hostname with TLS-ALPN-01 as challenge type, headscale must be reachable on port 443, i.e. listen_addr should probably end in :443") @@ -869,7 +724,7 @@ func validateServerConfigInto(v *configValidator) { validateListenerCollisions(v) - if ct := tlsKey("tls.acme.challenge_type"); ct != HTTP01ChallengeType && + if ct := src.String("tls.acme.challenge_type"); ct != HTTP01ChallengeType && ct != TLSALPN01ChallengeType && ct != DNS01ChallengeType { v.Add(&ConfigError{ Reason: "tls.acme.challenge_type has an unsupported value", @@ -879,7 +734,7 @@ func validateServerConfigInto(v *configValidator) { }) } - serverURL := viper.GetString("server_url") + serverURL := src.String("server_url") if !strings.HasPrefix(serverURL, "http://") && !strings.HasPrefix(serverURL, "https://") { v.Add(&ConfigError{ Reason: "server_url is missing a scheme", @@ -892,7 +747,7 @@ func validateServerConfigInto(v *configValidator) { // to avoid races minInactivityTimeout := 65 * time.Second - ephemeralTimeout := resolveEphemeralInactivityTimeout() + ephemeralTimeout := src.Duration("node.ephemeral.inactivity_timeout") if ephemeralTimeout <= minInactivityTimeout { v.Add(&ConfigError{ Reason: "node.ephemeral.inactivity_timeout is below the minimum", @@ -902,8 +757,8 @@ func validateServerConfigInto(v *configValidator) { }) } - if viper.GetBool("dns.override_local_dns") { - if global := viper.GetStringSlice("dns.nameservers.global"); len(global) == 0 { + if src.Bool("dns.override_local_dns") { + if global := src.StringSlice("dns.nameservers.global"); len(global) == 0 { v.Add(&ConfigError{ Reason: "dns.nameservers.global is required when dns.override_local_dns is true", Current: []KV{ @@ -917,7 +772,7 @@ func validateServerConfigInto(v *configValidator) { } // Validate HA health probing parameters - if haInterval := viper.GetDuration("node.routes.ha.probe_interval"); haInterval > 0 { + if haInterval := src.Duration("node.routes.ha.probe_interval"); haInterval > 0 { if haInterval < 2*time.Second { v.Add(&ConfigError{ Reason: "node.routes.ha.probe_interval is below the minimum", @@ -927,7 +782,7 @@ func validateServerConfigInto(v *configValidator) { }) } - haTimeout := viper.GetDuration("node.routes.ha.probe_timeout") + haTimeout := src.Duration("node.routes.ha.probe_timeout") if haTimeout < 1*time.Second { v.Add(&ConfigError{ Reason: "node.routes.ha.probe_timeout is below the minimum", @@ -950,7 +805,7 @@ func validateServerConfigInto(v *configValidator) { } // Validate tuning parameters - if size := viper.GetInt("tuning.node_store_batch_size"); size <= 0 { + if size := src.Int("tuning.node_store_batch_size"); size <= 0 { v.Add(&ConfigError{ Reason: "tuning.node_store_batch_size must be positive", Current: []KV{{"tuning.node_store_batch_size", size}}, @@ -958,7 +813,7 @@ func validateServerConfigInto(v *configValidator) { }) } - if timeout := viper.GetDuration("tuning.node_store_batch_timeout"); timeout <= 0 { + if timeout := src.Duration("tuning.node_store_batch_timeout"); timeout <= 0 { v.Add(&ConfigError{ Reason: "tuning.node_store_batch_timeout must be positive", Current: []KV{{"tuning.node_store_batch_timeout", timeout.String()}}, @@ -971,14 +826,12 @@ func validateServerConfigInto(v *configValidator) { validatePolicyConfig(v) validateMagicDNSConfig(v) validateCertificatesConfig(v) - - depr.Apply(v) } // validatePolicyConfig records a ConfigError for an unknown policy.mode: // startup would load no policy at all and allow all traffic. func validatePolicyConfig(v *configValidator) { - m := viper.GetString("policy.mode") + m := src.String("policy.mode") if m == PolicyModeFile || m == PolicyModeDB { return } @@ -994,11 +847,11 @@ func validatePolicyConfig(v *configValidator) { // validateDERPConfig records ConfigErrors when the embedded DERP server // is enabled without the addresses or paths it needs. func validateDERPConfig(v *configValidator) { - if !viper.GetBool("derp.server.enabled") { + if !src.Bool("derp.server.enabled") { return } - if viper.GetString("derp.server.stun_listen_addr") == "" { + if src.String("derp.server.stun_listen_addr") == "" { v.Add(&ConfigError{ Reason: "derp.server.stun_listen_addr is required when the embedded DERP server is enabled", Current: []KV{ @@ -1010,7 +863,7 @@ func validateDERPConfig(v *configValidator) { }) } - derpKey, noiseKey := viper.GetString("derp.server.private_key_path"), viper.GetString("noise.private_key_path") + derpKey, noiseKey := src.String("derp.server.private_key_path"), src.String("noise.private_key_path") switch { case derpKey == "": v.Add(&ConfigError{ @@ -1035,8 +888,8 @@ func validateDERPConfig(v *configValidator) { }) } - if !viper.GetBool("derp.server.automatically_add_embedded_derp_region") && - len(viper.GetStringSlice("derp.paths")) == 0 { + if !src.Bool("derp.server.automatically_add_embedded_derp_region") && + len(src.StringSlice("derp.paths")) == 0 { v.Add(&ConfigError{ Reason: "derp.paths is required when derp.server.automatically_add_embedded_derp_region is false", Current: []KV{ @@ -1051,7 +904,7 @@ func validateDERPConfig(v *configValidator) { // validateDatabaseConfig records a ConfigError when database.type is not // one of the supported backends. func validateDatabaseConfig(v *configValidator) { - t := viper.GetString("database.type") + t := src.String("database.type") switch t { case DatabaseSqlite, DatabasePostgres, "sqlite": return @@ -1069,11 +922,11 @@ func validateDatabaseConfig(v *configValidator) { // validateMagicDNSConfig records a ConfigError when MagicDNS is enabled // without a base_domain. func validateMagicDNSConfig(v *configValidator) { - if !viper.GetBool("dns.magic_dns") { + if !src.Bool("dns.magic_dns") { return } - if viper.GetString("dns.base_domain") == "" { + if src.String("dns.base_domain") == "" { v.Add(&ConfigError{ Reason: "dns.base_domain is required when dns.magic_dns is true", Current: []KV{ @@ -1090,7 +943,7 @@ func validateMagicDNSConfig(v *configValidator) { // are enabled without what the Tailscale client needs: a MagicDNS name // it can match exactly, and a provider to publish the challenge. func validateCertificatesConfig(v *configValidator) { - if timeout := viper.GetDuration("dns.challenge.propagation_timeout"); timeout < 0 { + if timeout := src.Duration("dns.challenge.propagation_timeout"); timeout < 0 { v.Add(&ConfigError{ Reason: "dns.challenge.propagation_timeout must not be negative", Current: []KV{{"dns.challenge.propagation_timeout", timeout.String()}}, @@ -1098,11 +951,11 @@ func validateCertificatesConfig(v *configValidator) { }) } - enabled := viper.GetBool("dns.certificates.enabled") - challengeType := tlsKey("tls.acme.challenge_type") - ownDNS01 := tlsKey("tls.acme.hostname") != "" && challengeType == DNS01ChallengeType + enabled := src.Bool("dns.certificates.enabled") + challengeType := src.String("tls.acme.challenge_type") + ownDNS01 := src.String("tls.acme.hostname") != "" && challengeType == DNS01ChallengeType - if (enabled || ownDNS01) && viper.GetString("dns.challenge.provider.name") == "" { + if (enabled || ownDNS01) && src.String("dns.challenge.provider.name") == "" { 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", challengeType}}, @@ -1116,7 +969,7 @@ func validateCertificatesConfig(v *configValidator) { return } - if !viper.GetBool("dns.magic_dns") { + if !src.Bool("dns.magic_dns") { v.Add(&ConfigError{ Reason: "dns.certificates requires dns.magic_dns", Current: []KV{{"dns.certificates.enabled", true}, {"dns.magic_dns", false}}, @@ -1125,7 +978,7 @@ func validateCertificatesConfig(v *configValidator) { }) } - base := strings.TrimSuffix(viper.GetString("dns.base_domain"), ".") + base := strings.TrimSuffix(src.String("dns.base_domain"), ".") 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", @@ -1135,7 +988,7 @@ func validateCertificatesConfig(v *configValidator) { }) } - zone := strings.TrimSuffix(viper.GetString("dns.certificates.zone"), ".") + zone := strings.TrimSuffix(src.String("dns.certificates.zone"), ".") if zone != "" && base != zone && !strings.HasSuffix(base, "."+zone) { v.Add(&ConfigError{ Reason: "dns.base_domain is not inside dns.certificates.zone", @@ -1148,11 +1001,11 @@ func validateCertificatesConfig(v *configValidator) { // dnsChallengeConfig builds the DNS-01 provider. Provider keys are read // one by one, because environment variables never reach into a map. -func dnsChallengeConfig() (DNSChallengeConfig, error) { +func dnsChallengeConfig(secret func(key, value string) (string, error)) (DNSChallengeConfig, error) { cfg := DNSChallengeConfig{ - PropagationTimeout: viper.GetDuration("dns.challenge.propagation_timeout"), - Resolvers: viper.GetStringSlice("dns.challenge.resolvers"), - ProviderName: viper.GetString("dns.challenge.provider.name"), + PropagationTimeout: src.Duration("dns.challenge.propagation_timeout"), + Resolvers: src.StringSlice("dns.challenge.resolvers"), + ProviderName: src.String("dns.challenge.provider.name"), } if len(cfg.Resolvers) == 0 { @@ -1173,11 +1026,20 @@ func dnsChallengeConfig() (DNSChallengeConfig, error) { provider, err := hsdns.NewProvider( cfg.ProviderName, - viper.GetStringMap("dns.challenge.provider"), + src.StringMap("dns.challenge.provider"), func(key string) (any, bool) { key = "dns.challenge.provider." + key - return viper.Get(key), viper.IsSet(key) + // Provider fields are not in configKeys, so the source never + // maps their variables. + if v := os.Getenv(configEnvName(key)); v != "" { + return v, true + } + + return src.Get(key), src.IsSet(key) + }, + func(key, value string) (string, error) { + return secret("dns.challenge.provider."+key, value) }, ) if err != nil { @@ -1186,7 +1048,7 @@ func dnsChallengeConfig() (DNSChallengeConfig, error) { // 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 && viper.GetBool("dns.certificates.enabled") { + if iso, ok := provider.(hsdns.Isolator); ok && src.Bool("dns.certificates.enabled") { err = iso.CheckIsolation() if err != nil { return cfg, fmt.Errorf("%w for %s: %w", hsdns.ErrProviderConfig, cfg.ProviderName, err) @@ -1198,38 +1060,36 @@ func dnsChallengeConfig() (DNSChallengeConfig, error) { return cfg, nil } -func tlsConfig() TLSConfig { +func tlsConfig(dir string) TLSConfig { return TLSConfig{ - CertPath: util.AbsolutePathFromConfigPath(tlsKey("tls.cert_path")), - KeyPath: util.AbsolutePathFromConfigPath(tlsKey("tls.key_path")), + CertPath: util.AbsolutePath(dir, src.String("tls.cert_path")), + KeyPath: util.AbsolutePath(dir, src.String("tls.key_path")), ACME: ACMEConfig{ - Hostname: tlsKey("tls.acme.hostname"), - ChallengeType: tlsKey("tls.acme.challenge_type"), - HTTPListen: tlsKey("tls.acme.http_listen"), - URL: tlsKey("tls.acme.url"), - Email: tlsKey("tls.acme.email"), - CacheDir: util.AbsolutePathFromConfigPath(tlsKey("tls.acme.cache_dir")), + Hostname: src.String("tls.acme.hostname"), + ChallengeType: src.String("tls.acme.challenge_type"), + HTTPListen: src.String("tls.acme.http_listen"), + URL: src.String("tls.acme.url"), + Email: src.String("tls.acme.email"), + CacheDir: util.AbsolutePath(dir, src.String("tls.acme.cache_dir")), }, } } -func derpConfig() DERPConfig { - serverEnabled := viper.GetBool("derp.server.enabled") - serverRegionID := viper.GetInt64("derp.server.region_id") - serverRegionCode := viper.GetString("derp.server.region_code") - serverRegionName := viper.GetString("derp.server.region_name") - serverVerifyClients := viper.GetBool("derp.server.verify_clients") - stunAddr := viper.GetString("derp.server.stun_listen_addr") - privateKeyPath := util.AbsolutePathFromConfigPath( - viper.GetString("derp.server.private_key_path"), - ) - ipv4 := viper.GetString("derp.server.ipv4") - ipv6 := viper.GetString("derp.server.ipv6") - automaticallyAddEmbeddedDerpRegion := viper.GetBool( +func derpConfig(dir string) DERPConfig { + serverEnabled := src.Bool("derp.server.enabled") + serverRegionID := src.Int64("derp.server.region_id") + serverRegionCode := src.String("derp.server.region_code") + serverRegionName := src.String("derp.server.region_name") + serverVerifyClients := src.Bool("derp.server.verify_clients") + stunAddr := src.String("derp.server.stun_listen_addr") + privateKeyPath := util.AbsolutePath(dir, src.String("derp.server.private_key_path")) + ipv4 := src.String("derp.server.ipv4") + ipv6 := src.String("derp.server.ipv6") + automaticallyAddEmbeddedDerpRegion := src.Bool( "derp.server.automatically_add_embedded_derp_region", ) - urlStrs := viper.GetStringSlice("derp.urls") + urlStrs := src.StringSlice("derp.urls") urls := make([]url.URL, 0, len(urlStrs)) for _, urlStr := range urlStrs { @@ -1247,10 +1107,10 @@ func derpConfig() DERPConfig { urls = append(urls, *urlAddr) } - paths := viper.GetStringSlice("derp.paths") + paths := src.StringSlice("derp.paths") - autoUpdate := viper.GetBool("derp.auto_update_enabled") - updateFrequency := viper.GetDuration("derp.update_frequency") + autoUpdate := src.Bool("derp.auto_update_enabled") + updateFrequency := src.Duration("derp.update_frequency") return DERPConfig{ ServerEnabled: serverEnabled, @@ -1267,11 +1127,12 @@ func derpConfig() DERPConfig { IPv4: ipv4, IPv6: ipv6, AutomaticallyAddEmbeddedDerpRegion: automaticallyAddEmbeddedDerpRegion, + ShuffleSeed: src.String("dns.base_domain"), } } func logtailConfig() LogTailConfig { - enabled := viper.GetBool("logtail.enabled") + enabled := src.Bool("logtail.enabled") return LogTailConfig{ Enabled: enabled, @@ -1279,8 +1140,8 @@ func logtailConfig() LogTailConfig { } func policyConfig() PolicyConfig { - policyPath := viper.GetString("policy.path") - policyMode := viper.GetString("policy.mode") + policyPath := src.String("policy.path") + policyMode := src.String("policy.mode") return PolicyConfig{ Path: policyPath, @@ -1289,14 +1150,14 @@ func policyConfig() PolicyConfig { } func logConfig() LogConfig { - logLevelStr := viper.GetString("log.level") + logLevelStr := src.String("log.level") logLevel, err := zerolog.ParseLevel(logLevelStr) if err != nil { logLevel = zerolog.DebugLevel } - logFormatOpt := viper.GetString("log.format") + logFormatOpt := src.String("log.format") var logFormat string @@ -1320,14 +1181,25 @@ func logConfig() LogConfig { } } -func databaseConfig() DatabaseConfig { - debug := viper.GetBool("database.debug") +// sqlitePath resolves database.sqlite.path, keeping SQLite's in-memory +// name, which is not a file. +func sqlitePath(dir string) string { + path := src.String("database.sqlite.path") + if path == ":memory:" { + return path + } - type_ := viper.GetString("database.type") + return util.AbsolutePath(dir, path) +} - skipErrRecordNotFound := viper.GetBool("database.gorm.skip_err_record_not_found") - slowThreshold := time.Duration(viper.GetInt64("database.gorm.slow_threshold")) * time.Millisecond - parameterizedQueries := viper.GetBool("database.gorm.parameterized_queries") +func databaseConfig(v *configValidator, secret func(key, value string) (string, error), dir string) DatabaseConfig { + debug := src.Bool("database.debug") + + type_ := src.String("database.type") + + skipErrRecordNotFound := src.Bool("database.gorm.skip_err_record_not_found") + slowThreshold := time.Duration(src.Int64("database.gorm.slow_threshold")) * time.Millisecond + parameterizedQueries := src.Bool("database.gorm.parameterized_queries") switch type_ { case DatabaseSqlite, DatabasePostgres: @@ -1336,6 +1208,11 @@ func databaseConfig() DatabaseConfig { type_ = "sqlite3" } + pass := src.String("database.postgres.pass") + if type_ == DatabasePostgres { + pass = configSecret(v, secret, "database.postgres.pass", pass) + } + return DatabaseConfig{ Type: type_, Debug: debug, @@ -1346,22 +1223,20 @@ func databaseConfig() DatabaseConfig { ParameterizedQueries: parameterizedQueries, }, Sqlite: SqliteConfig{ - Path: util.AbsolutePathFromConfigPath( - viper.GetString("database.sqlite.path"), - ), - WriteAheadLog: viper.GetBool("database.sqlite.write_ahead_log"), - WALAutoCheckPoint: viper.GetInt("database.sqlite.wal_autocheckpoint"), + Path: sqlitePath(dir), + WriteAheadLog: src.Bool("database.sqlite.write_ahead_log"), + WALAutoCheckPoint: src.Int("database.sqlite.wal_autocheckpoint"), }, Postgres: PostgresConfig{ - Host: viper.GetString("database.postgres.host"), - Port: viper.GetInt("database.postgres.port"), - Name: viper.GetString("database.postgres.name"), - User: viper.GetString("database.postgres.user"), - Pass: viper.GetString("database.postgres.pass"), - Ssl: viper.GetString("database.postgres.ssl"), - MaxOpenConnections: viper.GetInt("database.postgres.max_open_conns"), - MaxIdleConnections: viper.GetInt("database.postgres.max_idle_conns"), - ConnMaxIdleTimeSecs: viper.GetInt( + Host: src.String("database.postgres.host"), + Port: src.Int("database.postgres.port"), + Name: src.String("database.postgres.name"), + User: src.String("database.postgres.user"), + Pass: pass, + Ssl: src.String("database.postgres.ssl"), + MaxOpenConnections: src.Int("database.postgres.max_open_conns"), + MaxIdleConnections: src.Int("database.postgres.max_idle_conns"), + ConnMaxIdleTimeSecs: src.Int( "database.postgres.conn_max_idle_time_secs", ), }, @@ -1373,25 +1248,25 @@ func dns() (DNSConfig, error) { // TODO: Use this instead of manually getting settings when // UnmarshalKey is compatible with Environment Variables. - // err := viper.UnmarshalKey("dns", &dns) + // err := src.UnmarshalKey("dns", &dns) // if err != nil { // return DNSConfig{}, fmt.Errorf("unmarshalling dns config: %w", err) // } - dns.MagicDNS = viper.GetBool("dns.magic_dns") - dns.BaseDomain = viper.GetString("dns.base_domain") - dns.OverrideLocalDNS = viper.GetBool("dns.override_local_dns") - dns.Nameservers.Global = viper.GetStringSlice("dns.nameservers.global") - dns.Nameservers.Split = viper.GetStringMapStringSlice("dns.nameservers.split") - dns.SearchDomains = viper.GetStringSlice("dns.search_domains") - dns.ExtraRecordsPath = viper.GetString("dns.extra_records_path") - dns.Certificates.Enabled = viper.GetBool("dns.certificates.enabled") - dns.Certificates.Zone = cmp.Or(viper.GetString("dns.certificates.zone"), dns.BaseDomain) + dns.MagicDNS = src.Bool("dns.magic_dns") + dns.BaseDomain = src.String("dns.base_domain") + dns.OverrideLocalDNS = src.Bool("dns.override_local_dns") + dns.Nameservers.Global = src.StringSlice("dns.nameservers.global") + dns.Nameservers.Split = src.StringMapStringSlice("dns.nameservers.split") + dns.SearchDomains = src.StringSlice("dns.search_domains") + dns.ExtraRecordsPath = src.String("dns.extra_records_path") + dns.Certificates.Enabled = src.Bool("dns.certificates.enabled") + dns.Certificates.Zone = cmp.Or(src.String("dns.certificates.zone"), dns.BaseDomain) - if viper.IsSet("dns.extra_records") { + if src.IsSet("dns.extra_records") { var extraRecords []tailcfg.DNSRecord - err := viper.UnmarshalKey("dns.extra_records", &extraRecords) + err := src.UnmarshalKey("dns.extra_records", &extraRecords) if err != nil { return DNSConfig{}, fmt.Errorf("unmarshalling dns extra records: %w", err) } @@ -1512,7 +1387,7 @@ func warnBanner(lines []string) { } func parsePrefixConfig(key string, standardRange netip.Prefix, family string) (*netip.Prefix, bool, error) { - s := viper.GetString(key) + s := src.String(key) if s == "" { return nil, false, nil @@ -1534,7 +1409,7 @@ func parsePrefixConfig(key string, standardRange netip.Prefix, family string) (* // trustedProxies rejects 0.0.0.0/0 and ::/0 because they defeat the // peer-trust gate and almost always indicate misconfiguration. func trustedProxies() ([]netip.Prefix, error) { - raw := viper.GetStringSlice("trusted_proxies") + raw := src.StringSlice("trusted_proxies") if len(raw) == 0 { return nil, nil } @@ -1559,8 +1434,17 @@ func trustedProxies() ([]netip.Prefix, error) { // LoadCLIConfig returns the needed configuration for the CLI client // of Headscale to connect to a Headscale server. func LoadCLIConfig() (*Config, error) { + if src == nil { + return nil, errConfigNotLoaded + } + v := &configValidator{} validateBoolConfig(v, "cli.insecure", "disable_check_updates") + // Over the unix socket the CLI needs no key, so it is not read. + cliAPIKey := src.String("cli.api_key") + if src.String("cli.address") != "" { + cliAPIKey = configSecret(v, resolveSecret, "cli.api_key", cliAPIKey) + } err := v.Err() if err != nil { @@ -1571,13 +1455,13 @@ func LoadCLIConfig() (*Config, error) { zerolog.SetGlobalLevel(logConfig.Level) return &Config{ - DisableUpdateCheck: viper.GetBool("disable_check_updates"), - UnixSocket: viper.GetString("unix_socket"), + DisableUpdateCheck: src.Bool("disable_check_updates"), + UnixSocket: src.String("unix_socket"), CLI: CLIConfig{ - Address: viper.GetString("cli.address"), - APIKey: viper.GetString("cli.api_key"), - Timeout: viper.GetDuration("cli.timeout"), - Insecure: viper.GetBool("cli.insecure"), + Address: src.String("cli.address"), + APIKey: cliAPIKey, + Timeout: src.Duration("cli.timeout"), + Insecure: src.Bool("cli.insecure"), }, Log: logConfig, }, nil @@ -1586,6 +1470,37 @@ func LoadCLIConfig() (*Config, error) { // LoadServerConfig returns the full Headscale configuration to // host a Headscale server. This is called as part of `headscale serve`. func LoadServerConfig() (*Config, error) { + return loadServerConfig(resolveSecret) +} + +// CheckServerConfig validates the server configuration as +// LoadServerConfig does, without reading the files file: secrets name; it +// lists them in UnreadSecrets instead. +func CheckServerConfig() (*Config, error) { + var unread []KV + + cfg, err := loadServerConfig(func(key, value string) (string, error) { + s, err := keepSecret(key, value) + if errors.Is(err, hsdns.ErrSecretUnread) { + unread = append(unread, KV{key, strings.TrimPrefix(value, "file:")}) + } + + return s, err + }) + if cfg != nil { + // Provider settings are visited in map order. + slices.SortFunc(unread, func(a, b KV) int { return strings.Compare(a.Key, b.Key) }) + cfg.UnreadSecrets = unread + } + + return cfg, err +} + +func loadServerConfig(secret func(key, value string) (string, error)) (*Config, error) { + if src == nil { + return nil, errConfigNotLoaded + } + v := &configValidator{} validateServerConfigInto(v) @@ -1596,7 +1511,7 @@ func LoadServerConfig() (*Config, error) { if err != nil { v.Add(&ConfigError{ Reason: "prefixes.v4 is not a valid CIDR", - Current: []KV{{"prefixes.v4", viper.GetString("prefixes.v4")}}, + Current: []KV{{"prefixes.v4", src.String("prefixes.v4")}}, Detail: err.Error(), Hint: `use CIDR form, e.g. "100.64.0.0/10" (CGNAT, headscale default)`, Cause: err, @@ -1607,7 +1522,7 @@ func LoadServerConfig() (*Config, error) { if err != nil { v.Add(&ConfigError{ Reason: "prefixes.v6 is not a valid CIDR", - Current: []KV{{"prefixes.v6", viper.GetString("prefixes.v6")}}, + Current: []KV{{"prefixes.v6", src.String("prefixes.v6")}}, Detail: err.Error(), Hint: `use CIDR form, e.g. "fd7a:115c:a1e0::/48" (Tailscale ULA, headscale default)`, Cause: err, @@ -1618,7 +1533,7 @@ func LoadServerConfig() (*Config, error) { if err != nil { v.Add(&ConfigError{ Reason: "trusted_proxies contains an invalid CIDR", - Current: []KV{{"trusted_proxies", viper.GetStringSlice("trusted_proxies")}}, + Current: []KV{{"trusted_proxies", src.StringSlice("trusted_proxies")}}, Detail: err.Error(), Hint: "use specific proxy CIDRs; catch-all ranges are not allowed", Cause: err, @@ -1629,8 +1544,8 @@ func LoadServerConfig() (*Config, error) { v.Add(&ConfigError{ Reason: "no IP prefix configured for the tailnet", Current: []KV{ - {"prefixes.v4", viper.GetString("prefixes.v4")}, - {"prefixes.v6", viper.GetString("prefixes.v6")}, + {"prefixes.v4", src.String("prefixes.v4")}, + {"prefixes.v6", src.String("prefixes.v6")}, }, Hint: `set at least one of prefixes.v4 (default: "100.64.0.0/10") or prefixes.v6 (default: "fd7a:115c:a1e0::/48")`, Cause: ErrNoPrefixConfigured, @@ -1658,7 +1573,7 @@ func LoadServerConfig() (*Config, error) { }) } - allocStr := viper.GetString("prefixes.allocation") + allocStr := src.String("prefixes.allocation") var alloc IPAllocationStrategy @@ -1682,7 +1597,7 @@ func LoadServerConfig() (*Config, error) { // YAML reads an unquoted 0770 as the integer 504, which cannot be told // apart from a literal 504, so only a string is accepted. - socketPermRaw := viper.Get("unix_socket_permission") + socketPermRaw := src.Get("unix_socket_permission") socketPermStr, _ := socketPermRaw.(string) socketPerm, err := util.ParseFileMode(socketPermStr) @@ -1707,10 +1622,11 @@ func LoadServerConfig() (*Config, error) { }) } - derpConfig := derpConfig() + dir := configDir() + derpConfig := derpConfig(dir) logTailConfig := logtailConfig() - dnsChallenge, err := dnsChallengeConfig() + dnsChallenge, err := dnsChallengeConfig(secret) if err != nil { // The provider names only help when the name is what is wrong. var allowed []string @@ -1723,44 +1639,21 @@ func LoadServerConfig() (*Config, error) { Current: []KV{{"dns.challenge.provider.name", dnsChallenge.ProviderName}}, Detail: err.Error(), Allowed: allowed, - Hint: "check the provider name and its settings; any string setting can be read from a file with _path", + Hint: `check the provider name and its settings; any string setting can be read from a file with "file:"`, See: dnsDocsURL, Cause: err, }) } - oidcClientSecret := viper.GetString("oidc.client_secret") - oidcClientSecretPath := viper.GetString("oidc.client_secret_path") - - switch { - case oidcClientSecretPath != "" && oidcClientSecret != "": - v.Add(&ConfigError{ - Reason: "oidc.client_secret and oidc.client_secret_path are mutually exclusive", - Current: []KV{ - {"oidc.client_secret", ""}, - {"oidc.client_secret_path", oidcClientSecretPath}, - }, - Hint: "keep one source for the secret; the path form is recommended so the secret stays out of config files", - See: "https://headscale.net/stable/ref/oidc/", - Cause: errOidcMutuallyExclusive, - }) - - case oidcClientSecretPath != "": - secretBytes, err := os.ReadFile(os.ExpandEnv(oidcClientSecretPath)) - if err != nil { - v.Add(&ConfigError{ - Reason: "oidc.client_secret_path cannot be read", - Current: []KV{{"oidc.client_secret_path", oidcClientSecretPath}}, - Detail: err.Error(), - Hint: "ensure the file exists and is readable by the headscale user", - Cause: err, - }) - } else { - oidcClientSecret = strings.TrimSpace(string(secretBytes)) - } + // OIDC is active only with an issuer; see validateServerConfigInto. + oidcClientSecret := src.String("oidc.client_secret") + if src.String("oidc.issuer") != "" { + oidcClientSecret = configSecret(v, secret, "oidc.client_secret", oidcClientSecret) } - serverURL := strings.TrimRight(viper.GetString("server_url"), "/") + database := databaseConfig(v, secret, dir) + + serverURL := strings.TrimRight(src.String("server_url"), "/") // BaseDomain cannot be the same as the server URL. // This is because Tailscale takes over the domain in BaseDomain, @@ -1783,106 +1676,106 @@ func LoadServerConfig() (*Config, error) { return &Config{ ServerURL: serverURL, - Addr: viper.GetString("listen_addr"), - MetricsAddr: viper.GetString("metrics_listen_addr"), + Addr: src.String("listen_addr"), + MetricsAddr: src.String("metrics_listen_addr"), TrustedProxies: trusted, - DisableUpdateCheck: false, + ConfigDir: dir, + DisableUpdateCheck: src.Bool("disable_check_updates"), PrefixV4: prefix4, PrefixV6: prefix6, IPAllocation: alloc, - NoisePrivateKeyPath: util.AbsolutePathFromConfigPath( - viper.GetString("noise.private_key_path"), - ), - BaseDomain: dnsConfig.BaseDomain, + NoisePrivateKeyPath: util.AbsolutePath(dir, src.String("noise.private_key_path")), + BaseDomain: dnsConfig.BaseDomain, DERP: derpConfig, Node: NodeConfig{ Expiry: resolveNodeExpiry(), Ephemeral: EphemeralConfig{ - InactivityTimeout: resolveEphemeralInactivityTimeout(), + InactivityTimeout: src.Duration("node.ephemeral.inactivity_timeout"), }, Routes: RouteConfig{ HA: HARouteConfig{ - ProbeInterval: viper.GetDuration("node.routes.ha.probe_interval"), - ProbeTimeout: viper.GetDuration("node.routes.ha.probe_timeout"), + ProbeInterval: src.Duration("node.routes.ha.probe_interval"), + ProbeTimeout: src.Duration("node.routes.ha.probe_timeout"), }, }, }, PreAuthKeys: PreAuthKeysConfig{ - RevokedRetention: viper.GetDuration("preauth_keys.revoked_retention"), + RevokedRetention: src.Duration("preauth_keys.revoked_retention"), }, - Database: databaseConfig(), + Database: database, - TLS: tlsConfig(), + TLS: tlsConfig(dir), DNSConfig: dnsConfig, TailcfgDNSConfig: dnsToTailcfgDNS(dnsConfig), DNSChallenge: dnsChallenge, - UnixSocket: viper.GetString("unix_socket"), + UnixSocket: src.String("unix_socket"), UnixSocketPermission: socketPerm, OIDC: OIDCConfig{ - OnlyStartIfOIDCIsAvailable: viper.GetBool( + OnlyStartIfOIDCIsAvailable: src.Bool( "oidc.only_start_if_oidc_is_available", ), - Issuer: viper.GetString("oidc.issuer"), - ClientID: viper.GetString("oidc.client_id"), + Issuer: src.String("oidc.issuer"), + ClientID: src.String("oidc.client_id"), ClientSecret: oidcClientSecret, - Scope: viper.GetStringSlice("oidc.scope"), - ExtraParams: viper.GetStringMapString("oidc.extra_params"), - AllowedDomains: viper.GetStringSlice("oidc.allowed_domains"), - AllowedUsers: viper.GetStringSlice("oidc.allowed_users"), - AllowedGroups: viper.GetStringSlice("oidc.allowed_groups"), - EmailVerifiedRequired: viper.GetBool("oidc.email_verified_required"), - UseExpiryFromToken: viper.GetBool("oidc.use_expiry_from_token"), + Scope: src.StringSlice("oidc.scope"), + ExtraParams: src.StringMapString("oidc.extra_params"), + AllowedDomains: src.StringSlice("oidc.allowed_domains"), + AllowedUsers: src.StringSlice("oidc.allowed_users"), + AllowedGroups: src.StringSlice("oidc.allowed_groups"), + EmailVerifiedRequired: src.Bool("oidc.email_verified_required"), + UseExpiryFromToken: src.Bool("oidc.use_expiry_from_token"), PKCE: PKCEConfig{ - Enabled: viper.GetBool("oidc.pkce.enabled"), - Method: viper.GetString("oidc.pkce.method"), + Enabled: src.Bool("oidc.pkce.enabled"), + Method: src.String("oidc.pkce.method"), }, }, LogTail: logTailConfig, Taildrop: TaildropConfig{ - Enabled: viper.GetBool("taildrop.enabled"), + Enabled: src.Bool("taildrop.enabled"), }, AutoUpdate: AutoUpdateConfig{ - Enabled: viper.GetBool("auto_update.enabled"), + Enabled: src.Bool("auto_update.enabled"), }, Policy: policyConfig(), + // No APIKey: the server never uses the CLI's key, which may be an + // unresolved file: reference here. CLI: CLIConfig{ - Address: viper.GetString("cli.address"), - APIKey: viper.GetString("cli.api_key"), - Timeout: viper.GetDuration("cli.timeout"), - Insecure: viper.GetBool("cli.insecure"), + Address: src.String("cli.address"), + Timeout: src.Duration("cli.timeout"), + Insecure: src.Bool("cli.insecure"), }, Log: logConfig, Tuning: Tuning{ - NotifierSendTimeout: viper.GetDuration("tuning.notifier_send_timeout"), - BatchChangeDelay: viper.GetDuration("tuning.batch_change_delay"), - NodeMapSessionBufferedChanSize: viper.GetInt( + NotifierSendTimeout: src.Duration("tuning.notifier_send_timeout"), + BatchChangeDelay: src.Duration("tuning.batch_change_delay"), + NodeMapSessionBufferedChanSize: src.Int( "tuning.node_mapsession_buffered_chan_size", ), BatcherWorkers: func() int { - if workers := viper.GetInt("tuning.batcher_workers"); workers > 0 { + if workers := src.Int("tuning.batcher_workers"); workers > 0 { return workers } return DefaultBatcherWorkers() }(), - RegisterCacheExpiration: viper.GetDuration("tuning.register_cache_expiration"), - RegisterCacheMaxEntries: viper.GetInt("tuning.register_cache_max_entries"), - NodeStoreBatchSize: viper.GetInt("tuning.node_store_batch_size"), - NodeStoreBatchTimeout: viper.GetDuration("tuning.node_store_batch_timeout"), + RegisterCacheExpiration: src.Duration("tuning.register_cache_expiration"), + RegisterCacheMaxEntries: src.Int("tuning.register_cache_max_entries"), + NodeStoreBatchSize: src.Int("tuning.node_store_batch_size"), + NodeStoreBatchTimeout: src.Duration("tuning.node_store_batch_timeout"), }, }, nil } @@ -1950,157 +1843,6 @@ func isSafeServerURL(serverURL, baseDomain string) error { return nil } -// deprecation describes a configuration key that is no longer -// supported. NewKey is the replacement (when one exists) or empty -// when the key has been removed without a replacement. -type deprecation struct { - OldKey string - NewKey string - Hint string -} - -// deprecator collects deprecated configuration keys observed in the -// current viper config. Warns are non-blocking and surface via -// log.Warn; fatals are pushed onto a configValidator as ConfigError -// entries so they merge with the rest of the validation report -// instead of pre-empting it via log.Fatal. -type deprecator struct { - seen set.Set[string] - warns []deprecation - fatals []deprecation -} - -func (d *deprecator) addWarn(dep deprecation) { - if d.seen.Contains(dep.OldKey) { - return - } - - d.seen.Add(dep.OldKey) - d.warns = append(d.warns, dep) -} - -func (d *deprecator) addFatal(dep deprecation) { - if d.seen.Contains(dep.OldKey) { - return - } - - d.seen.Add(dep.OldKey) - d.fatals = append(d.fatals, dep) -} - -// warnWithAlias registers an alias from newKey to oldKey and records -// a non-blocking warning when oldKey is set. -// -//nolint:unused -func (d *deprecator) warnWithAlias(newKey, oldKey string) { - // NOTE: RegisterAlias is called with NEW KEY -> OLD KEY - viper.RegisterAlias(newKey, oldKey) - - if viper.IsSet(oldKey) { - d.addWarn(deprecation{OldKey: oldKey, NewKey: newKey}) - } -} - -// fatal records a removed key (no replacement) as a config error. -func (d *deprecator) fatal(oldKey string) { - if viper.IsSet(oldKey) { - d.addFatal(deprecation{OldKey: oldKey}) - } -} - -// fatalWithHint behaves like fatal but appends a remediation pointer to -// the message so operators see exactly what to do without leaving the -// terminal. Use it when the removed key has a clean replacement on the -// policy side. -func (d *deprecator) fatalWithHint(oldKey, hint string) { - if viper.IsSet(oldKey) { - d.addFatal(deprecation{OldKey: oldKey, Hint: hint}) - } -} - -// fatalIfNewKeyIsNotUsed records a config error when oldKey is set -// without newKey, or a warning when both are set (oldKey ignored, -// newKey takes precedence). -func (d *deprecator) fatalIfNewKeyIsNotUsed(newKey, oldKey string) { - if !viper.IsSet(oldKey) { - return - } - - if !viper.IsSet(newKey) { - d.addFatal(deprecation{OldKey: oldKey, NewKey: newKey}) - - return - } - - d.addWarn(deprecation{OldKey: oldKey, NewKey: newKey}) -} - -// fatalIfSet records a config error any time oldKey is set, naming -// newKey as the replacement. -func (d *deprecator) fatalIfSet(oldKey, newKey string) { - if viper.IsSet(oldKey) { - d.addFatal(deprecation{OldKey: oldKey, NewKey: newKey}) - } -} - -// warnNoAlias records a non-blocking warning when oldKey is set, -// pointing operators at newKey. -// -//nolint:unused -func (d *deprecator) warnNoAlias(newKey, oldKey string) { - if viper.IsSet(oldKey) { - d.addWarn(deprecation{OldKey: oldKey, NewKey: newKey}) - } -} - -// warn records a non-blocking warning when oldKey is set, with no -// replacement to point at. -func (d *deprecator) warn(oldKey string) { - if viper.IsSet(oldKey) { - d.addWarn(deprecation{OldKey: oldKey}) - } -} - -// Apply emits warns via log.Warn and pushes one *ConfigError per -// fatal onto v. Run after the rest of validation so deprecated-key -// failures surface alongside the other config errors in a single -// pass. -func (d *deprecator) Apply(v *configValidator) { - for _, w := range d.warns { - if w.NewKey != "" { - log.Warn().Msgf( - "configuration key %q is deprecated; use %q instead", - w.OldKey, w.NewKey) - - continue - } - - log.Warn().Msgf("configuration key %q is ignored", w.OldKey) - } - - for _, f := range d.fatals { - ce := &ConfigError{ - Current: []KV{{f.OldKey, viper.Get(f.OldKey)}}, - } - - if f.NewKey != "" { - ce.Reason = fmt.Sprintf( - "configuration key %s has been removed; use %s instead", - f.OldKey, f.NewKey) - ce.Hint = fmt.Sprintf("remove %s and set %s", f.OldKey, f.NewKey) - } else { - ce.Reason = fmt.Sprintf("configuration key %s has been removed", f.OldKey) - if f.Hint != "" { - ce.Hint = f.Hint - } else { - ce.Hint = fmt.Sprintf("remove %s; see the CHANGELOG for context", f.OldKey) - } - } - - v.Add(ce) - } -} - // tailcfgDNSMu guards concurrent access to the mutable ExtraRecords of // [Config.TailcfgDNSConfig] between the extra-records file watcher (writer) // and the per-client map builds that clone it (readers). It is a package-level diff --git a/hscontrol/types/config_keys.go b/hscontrol/types/config_keys.go index 6ee420543..bc080942b 100644 --- a/hscontrol/types/config_keys.go +++ b/hscontrol/types/config_keys.go @@ -50,8 +50,6 @@ func keyPosition(src []byte, path []string) (int, int, bool) { // configKeys lists every key the loader reads, so env mapping and the strict // pass know the schema. Open maps are in openMapKeys. -// -//nolint:goconst // schema table, not shared literals var configKeys = []string{ "server_url", "listen_addr", @@ -129,7 +127,6 @@ var configKeys = []string{ "oidc.issuer", "oidc.client_id", "oidc.client_secret", - "oidc.client_secret_path", "oidc.scope", "oidc.extra_params", "oidc.allowed_domains", @@ -213,12 +210,17 @@ var configDefaults = map[string]any{ "tuning.node_store_batch_size": defaultNodeStoreBatchSize, "tuning.node_store_batch_timeout": "500ms", "prefixes.allocation": string(IPAllocationStrategySequential), + "tls.acme.challenge_type": HTTP01ChallengeType, + "tls.acme.http_listen": ":http", + "tls.acme.cache_dir": "/var/lib/headscale/cache", } // removedKey is a key that once worked and is now rejected. In files Old // matches the key and everything beneath it; for environment variables only -// the exact name, so the old dns_config.* leaves are listed too. -type removedKey struct{ Old, New, Since, Hint string } +// the exact name, so the old dns_config.* leaves are listed too. New holds +// the replacement keys, space separated; Value, when set, is what New takes. +// Hint is free text for what the other fields cannot say. +type removedKey struct{ Old, New, Value, Since, Hint string } var removedKeys = []removedKey{ {Old: "acl_policy_path", New: "policy.path", Since: "0.30"}, @@ -236,7 +238,7 @@ var removedKeys = []removedKey{ {Old: "oidc.map_legacy_users", Since: "0.26"}, {Old: "randomize_client_port", Since: "0.29", Hint: `set "randomizeClientPort": true at the top level of the policy file, or grant the cap per node via a "nodeAttrs" entry`}, {Old: "oidc.expiry", New: "node.expiry", Since: "0.29"}, - {Old: "ephemeral_node_inactivity_timeout", New: "node.ephemeral.inactivity_timeout", Since: "0.29"}, + {Old: "ephemeral_node_inactivity_timeout", New: "node.ephemeral.inactivity_timeout", Since: "0.30"}, {Old: "tls_cert_path", New: "tls.cert_path", Since: "0.30"}, {Old: "tls_key_path", New: "tls.key_path", Since: "0.30"}, {Old: "tls_letsencrypt_hostname", New: "tls.acme.hostname", Since: "0.30"}, @@ -257,9 +259,9 @@ var removedKeys = []removedKey{ {Old: "db_pass", New: "database.postgres.pass", Since: "0.30"}, {Old: "db_ssl", New: "database.postgres.ssl", Since: "0.30"}, {Old: "private_key_path", New: "noise.private_key_path", Since: "0.23"}, - {Old: "ip_prefixes", New: "prefixes.v4", Since: "0.23", Hint: "use prefixes.v4 and prefixes.v6"}, + {Old: "ip_prefixes", New: "prefixes.v4 prefixes.v6", Since: "0.23"}, {Old: "log_level", New: "log.level", Since: "0.17"}, - {Old: "json_logs", New: "log.format", Since: "0.30", Hint: `use log.format: "json"`}, + {Old: "json_logs", New: "log.format", Value: "json", Since: "0.30"}, {Old: "tls_client_auth_mode", Since: "0.30"}, {Old: "node_update_check_interval", Since: "0.30"}, {Old: "changes_check_interval", Since: "0.30"}, @@ -269,4 +271,6 @@ var removedKeys = []removedKey{ {Old: "oidc.domain_map", Since: "0.13"}, {Old: "oidc.namespace", Since: "0.30"}, {Old: "oidc.user", Since: "0.30"}, + {Old: "oidc.client_secret_path", New: "oidc.client_secret", Value: "file:", Since: "0.30"}, + {Old: "database.postgres.password_file", New: "database.postgres.pass", Value: "file:", Since: "0.30"}, } diff --git a/hscontrol/types/config_source.go b/hscontrol/types/config_source.go index f1426f532..8524d3289 100644 --- a/hscontrol/types/config_source.go +++ b/hscontrol/types/config_source.go @@ -12,6 +12,7 @@ import ( "time" "github.com/go-viper/mapstructure/v2" + hsdns "github.com/juanfont/headscale/hscontrol/dns" "github.com/knadh/koanf/parsers/json" "github.com/knadh/koanf/parsers/yaml" "github.com/knadh/koanf/providers/confmap" @@ -363,32 +364,107 @@ func removedConfigKey(path string) (removedKey, bool) { } } - return removedKey{}, false + return removedProviderPath(path) } -func (r removedKey) hint() string { +// removedProviderPath matches a DNS provider's _path, nested ones +// included, the file form "file:" replaced. Provider settings have no +// fixed list, so the form is matched by name, for every provider, selected +// or not. +func removedProviderPath(path string) (removedKey, bool) { + const prefix = "dns.challenge.provider." + + field, ok := strings.CutPrefix(path, prefix) + setting := field[strings.LastIndex(field, ".")+1:] + + if !ok || !strings.HasSuffix(setting, "_path") || hsdns.IsProviderSetting(setting) { + return removedKey{}, false + } + + return removedKey{ + Old: path, + New: prefix + strings.TrimSuffix(field, "_path"), + Value: "file:", + Since: "0.30", + }, true +} + +// walkRemoved calls add for every removed key below path, an open map +// whose contents are otherwise the user's, and deletes it from m, so a +// caller that only warns loads the rest. labels says m's keys are names, +// such as acme-dns's registrations: data, kept as written and not checked. +func walkRemoved(path string, m map[string]any, labels bool, add func(path string, r removedKey)) { + for _, name := range slices.Sorted(maps.Keys(m)) { + key := name + if !labels { + key = strings.ToLower(name) + } + + p := path + "." + key + if r, removed := removedConfigKey(p); removed && !labels { + add(p, r) + delete(m, name) + + continue + } + + if child, ok := m[name].(map[string]any); ok { + walkRemoved(p, child, !labels && hsdns.IsProviderMapSetting(key), add) + } + } +} + +// hintAs says what replaces r, spelling each key with name and a key set +// to Value with assign, so one table serves files and the environment. +func (r removedKey) hintAs(name func(string) string, assign string) string { switch { case r.Hint != "": return r.Hint + case r.Value != "": + return "use " + fmt.Sprintf(assign, name(r.New), r.Value) case r.New != "": - return "use " + r.New + keys := strings.Fields(r.New) + for i, k := range keys { + keys[i] = name(k) + } + + last := len(keys) - 1 + if last == 0 { + return "use " + keys[0] + } + + return "use " + strings.Join(keys[:last], ", ") + " and " + keys[last] } - return fmt.Sprintf("remove %s; see the CHANGELOG for context", r.Old) + return fmt.Sprintf("remove %s; see the CHANGELOG for context", name(r.Old)) +} + +func (r removedKey) hint() string { + return r.hintAs(func(key string) string { return key }, `%s: %q`) +} + +// envHint is hint for a variable. A removed block has no variable of its +// own, so it names the variables replacing its removed keys. +func (r removedKey) envHint() string { + var leaves []string + + for _, l := range removedKeys { + if strings.HasPrefix(l.Old, r.Old+".") && l.New != "" { + leaves = append(leaves, l.New) + } + } + + if len(leaves) > 0 { + r = removedKey{Old: r.Old, New: strings.Join(leaves, " ")} + } + + return r.hintAs(configEnvName, "%s=%s") } func (r removedKey) reason(what, name string) string { return fmt.Sprintf("configuration %s %s was removed in %s", what, name, r.Since) } -func (r removedKey) envHint() string { - if r.New != "" && r.Hint == "" { - return "use " + configEnvName(r.New) - } - - return r.hint() -} - // checkKeys adds an error for every removed or unknown key in the file. func (s *configSource) checkKeys(v *configValidator, m map[string]any) { leaf, branch := configKeySets() @@ -412,15 +488,27 @@ func (s *configSource) checkKeys(v *configValidator, m map[string]any) { Hint: r.hint(), }) case isOpenMapPath(path): - // The contents are the user's. + // The contents are the user's, but for a removed form. A dotted + // key below the open map (provider.config) is walked from the + // map, so its contents get the role they have when nested. + root, below := path, child + if !slices.Contains(openMapKeys, path) { + parts := configPathParts(path) + root = strings.Join(parts[:len(parts)-1], ".") + below = map[string]any{parts[len(parts)-1]: val} + } + + walkRemoved(root, below, false, func(p string, r removedKey) { + v.Add(&ConfigError{Reason: r.reason("key", p) + s.position(p), Hint: r.hint()}) + }) case leaf[path] && !isMap: case (leaf[path] || branch[path]) && isMap: // Under a plain setting every child is unknown. walk(path, child) case branch[path]: + // The value is not shown: a misplaced block may hold secrets. v.Add(&ConfigError{ - Reason: fmt.Sprintf("configuration key %s must be a mapping%s", path, s.position(path)), - Current: []KV{{path, val}}, + 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))}) @@ -472,44 +560,57 @@ func (s *configSource) loadEnv(environ []string) error { } } - v := &configValidator{} - + // Other software shares the prefix (a Kubernetes Service named + // headscale-db sets HEADSCALE_DB_PORT), so variables only ever warn. for _, name := range slices.Sorted(maps.Keys(env)) { - if names[name] || reservedEnv(name) { + if names[name] { continue } + // Before reservedEnv, which spares every provider variable. if r, ok := removedEnv(name); ok { - v.Add(&ConfigError{ - Reason: r.reason("variable", name), - Hint: r.envHint(), - }) + s.warnings = append(s.warnings, r.reason("variable", name)+"; "+r.envHint()) continue } + if reservedEnv(name) { + continue + } + s.warnings = append(s.warnings, "unknown environment variable "+name) } - if v.HasErrors() { - return v.Err() - } - // A variable for an open map replaces the file's map instead of merging // into it. The value stays a raw string, which the cast accessors parse as - // JSON, unless a per-field variable (HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME) - // has to be layered on top: then the JSON object is parsed here. - for key, val := range openLayer { + // JSON, unless the JSON object held a removed form, dropped here, or a + // per-field variable (HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME) has to be + // layered on top: then the parsed object is set. + for _, key := range slices.Sorted(maps.Keys(openLayer)) { + val := openLayer[key] s.k.Delete(key) var parsed map[string]any + dropped := false + + // A failed decode may still have filled part of parsed: never used. + decoded := stdjson.Unmarshal([]byte(val), &parsed) == nil && parsed != nil + if decoded { + // The hint names keys: inside the JSON they are keys, not variables. + name := configEnvName(key) + walkRemoved(key, parsed, false, func(p string, r removedKey) { + s.warnings = append(s.warnings, r.reason("key", p+" in "+name)+"; "+r.hint()) + dropped = true + }) + } + shared := slices.ContainsFunc(slices.Collect(maps.Keys(layer)), func(k string) bool { return strings.HasPrefix(k, key+".") }) var v any = val - if shared && stdjson.Unmarshal([]byte(val), &parsed) == nil && parsed != nil { + if decoded && (dropped || shared) { v = parsed } @@ -543,6 +644,10 @@ func removedEnv(name string) (removedKey, bool) { } } + if field, ok := strings.CutPrefix(name, configEnvName("dns.challenge.provider")+"_"); ok { + return removedProviderPath("dns.challenge.provider." + strings.ToLower(field)) + } + return removedKey{}, false } diff --git a/hscontrol/types/config_source_test.go b/hscontrol/types/config_source_test.go index 2180884a7..5b102b035 100644 --- a/hscontrol/types/config_source_test.go +++ b/hscontrol/types/config_source_test.go @@ -2,6 +2,7 @@ package types import ( "cmp" + "errors" "go/ast" "go/parser" "go/token" @@ -61,9 +62,9 @@ func TestConfigSource(t *testing.T) { errs: []string{"grpc_listen_addr", "bogus"}, }, { - name: "removed env", - env: []string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT=30m"}, - errs: []string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT", "HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT"}, + name: "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", @@ -78,6 +79,12 @@ func TestConfigSource(t *testing.T) { "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"}, @@ -242,14 +249,22 @@ func TestConfigSource(t *testing.T) { }, }, { - name: "removed dns_config env", + name: "removed dns_config env warns", env: []string{"HEADSCALE_DNS_CONFIG_MAGIC_DNS=false", "HEADSCALE_DNS_CONFIG_NAMESERVERS=1.1.1.1"}, - errs: []string{"variable HEADSCALE_DNS_CONFIG_MAGIC_DNS was removed", "HEADSCALE_DNS_CONFIG_NAMESERVERS", "use HEADSCALE_DNS_MAGIC_DNS"}, + 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 env names the new variable", - env: []string{"HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT=30m"}, - errs: []string{"variable HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT was removed in 0.29", "use HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT"}, + name: "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", @@ -391,15 +406,11 @@ var parityRejected = func() map[string][]string { "nixos-nixpkgs": { "tls_letsencrypt_cache_dir", "tls_letsencrypt_challenge_type", "tls_letsencrypt_hostname", "tls_letsencrypt_listen", }, - "env-legacy-ignored": { - "HEADSCALE_DATABASE_GORM_PREPARE_STMT", "HEADSCALE_DB_TYPE", "HEADSCALE_DNS_CONFIG_MAGIC_DNS", - "HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT", "HEADSCALE_PRIVATE_KEY_PATH", - }, - "env-legacy-removed": {"HEADSCALE_ACL_POLICY_PATH", "HEADSCALE_OIDC_EXPIRY", "HEADSCALE_RANDOMIZE_CLIENT_PORT"}, + "file-everything": {"oidc.client_secret_path"}, } }() -var removedKeyInReason = regexp.MustCompile(`configuration (?:key|variable) (\S+) was removed|unknown configuration key (\S+)`) +var removedKeyInReason = regexp.MustCompile(`configuration key (\S+) was removed|unknown configuration key (\S+)`) // TestConfigSourceParity checks the source against viper on every Phase-0 // fixture: same set-ness and same value for each coercion. @@ -412,11 +423,8 @@ func TestConfigSourceParity(t *testing.T) { for _, tt := range rows { t.Run(tt.name, func(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) - dir := stageCompat(t, tt.file, tt.envFile) - require.NoError(t, LoadConfig(dir, false)) + loadViper(t, dir) s, err := newConfigSource(dir, false, os.Environ()) @@ -466,9 +474,7 @@ func TestConfigSourceUnmarshalParity(t *testing.T) { dir := t.TempDir() require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(content), 0o600)) - viper.Reset() - t.Cleanup(viper.Reset) - require.NoError(t, LoadConfig(dir, false)) + loadViper(t, dir) var want []rec @@ -485,9 +491,6 @@ func TestConfigSourceUnmarshalParity(t *testing.T) { } } -// pendingKeys are read by the viper loader but replaced by file: secrets later. -var pendingKeys = map[string]bool{"oidc.client_secret_path": true} - func TestConfigKeysComplete(t *testing.T) { read := map[string]bool{} @@ -501,23 +504,13 @@ func TestConfigKeysComplete(t *testing.T) { return true } - var name string - - switch fn := call.Fun.(type) { - case *ast.SelectorExpr: - if obj, ok := fn.X.(*ast.Ident); !ok || obj.Name != "viper" { - return true - } - - name = fn.Sel.Name - case *ast.Ident: - name = fn.Name - default: + // Every src accessor takes the key first. + fn, ok := call.Fun.(*ast.SelectorExpr) + if !ok { return true } - if !strings.HasPrefix(name, "Get") && name != "IsSet" && name != "InConfig" && - name != "UnmarshalKey" && name != "tlsKey" { + if obj, ok := fn.X.(*ast.Ident); !ok || obj.Name != "src" { return true } @@ -532,23 +525,17 @@ func TestConfigKeysComplete(t *testing.T) { }) } - removed := map[string]bool{} - for _, r := range removedKeys { - removed[r.Old] = true - } - all := slices.Concat(configKeys, openMapKeys) for key := range read { - ok := slices.Contains(all, key) || removed[key] || key == "dns" || - strings.HasPrefix(key, "dns.challenge.provider") + ok := slices.Contains(all, key) || strings.HasPrefix(key, "dns.challenge.provider") assert.Truef(t, ok, "%s is read but missing from the key tables", key) } for _, key := range configKeys { _, hasDefault := configDefaults[key] - assert.Truef(t, read[key] || hasDefault || pendingKeys[key], "%s is listed but never read", key) + assert.Truef(t, read[key] || hasDefault, "%s is listed but never read", key) } for key := range configDefaults { @@ -565,11 +552,28 @@ func TestConfigKeysComplete(t *testing.T) { } } +// loadViper loads the config file in dir as the viper loader did, for the +// parity checks. func loadViper(t *testing.T, dir string) { t.Helper() viper.Reset() t.Cleanup(viper.Reset) - require.NoError(t, LoadConfig(dir, false)) + + viper.SetConfigName("config") + viper.AddConfigPath(dir) + + viper.SetEnvPrefix("headscale") + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viper.AutomaticEnv() + + for k, v := range configDefaults { + viper.SetDefault(k, v) + } + + err := viper.ReadInConfig() + if _, ok := errors.AsType[viper.ConfigFileNotFoundError](err); !ok { + require.NoError(t, err) + } } // TestConfigSourceDottedOverlap reloads the file each time: map iteration @@ -675,6 +679,23 @@ func TestConfigSourceDottedNoOverlap(t *testing.T) { } } +// 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, "config.yaml", 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)) diff --git a/hscontrol/types/config_test.go b/hscontrol/types/config_test.go index 18fa52ec7..1dfa0a3a2 100644 --- a/hscontrol/types/config_test.go +++ b/hscontrol/types/config_test.go @@ -22,10 +22,11 @@ import ( 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/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "tailscale.com/tailcfg" @@ -239,7 +240,7 @@ func TestReadConfig(t *testing.T) { }, { name: "policy-path-is-loaded", - configPath: "testdata/policy-path-is-loaded.yaml", + 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 { @@ -260,8 +261,6 @@ func TestReadConfig(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - viper.Reset() - err := LoadConfig(tt.configPath, true) require.NoError(t, err) @@ -298,11 +297,9 @@ func TestReadConfigFromEnv(t *testing.T) { "HEADSCALE_PREFIXES_V4": "100.64.0.0/10", }, setup: func(t *testing.T) (any, error) { //nolint:thelper // inline test closure - t.Logf("all settings: %#v", viper.AllSettings()) - - assert.Equal(t, "trace", viper.GetString("log.level")) - assert.Equal(t, "100.64.0.0/10", viper.GetString("prefixes.v4")) - assert.False(t, viper.GetBool("database.sqlite.write_ahead_log")) + assert.Equal(t, "trace", src.String("log.level")) + assert.Equal(t, "100.64.0.0/10", src.String("prefixes.v4")) + assert.False(t, src.Bool("database.sqlite.write_ahead_log")) return nil, nil //nolint:nilnil // test setup returns nil to indicate no expected value }, @@ -322,8 +319,6 @@ func TestReadConfigFromEnv(t *testing.T) { // "HEADSCALE_DNS_EXTRA_RECORDS": `[{ name: "prometheus.myvpn.example.com", type: "A", value: "100.64.0.4" }]`, }, setup: func(t *testing.T) (any, error) { //nolint:thelper // inline test closure - t.Logf("all settings: %#v", viper.AllSettings()) - dns, err := dns() if err != nil { return nil, err @@ -356,8 +351,6 @@ func TestReadConfigFromEnv(t *testing.T) { t.Setenv(k, v) } - viper.Reset() - err := LoadConfig("testdata/minimal.yaml", true) require.NoError(t, err) @@ -371,40 +364,6 @@ func TestReadConfigFromEnv(t *testing.T) { } } -func TestPreparedStatementConfigIsIgnoredWithWarning(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) - - var logs bytes.Buffer - - previousLogger := log.Logger - previousLevel := zerolog.GlobalLevel() - - zerolog.SetGlobalLevel(zerolog.WarnLevel) - t.Cleanup(func() { zerolog.SetGlobalLevel(previousLevel) }) - - log.Logger = zerolog.New(&logs) - - t.Cleanup(func() { log.Logger = previousLogger }) - - configPath := filepath.Join(t.TempDir(), "config.yaml") - require.NoError(t, os.WriteFile(configPath, []byte(` -server_url: https://example.com -noise: - private_key_path: noise_private.key -dns: - magic_dns: false - override_local_dns: false -database: - type: sqlite3 - gorm: - prepare_stmt: true -`), 0o600)) - require.NoError(t, LoadConfig(configPath, true)) - require.NoError(t, validateServerConfig()) - assert.Contains(t, logs.String(), "database.gorm.prepare_stmt") -} - func TestTLSConfigValidation(t *testing.T) { tmpDir := t.TempDir() @@ -572,46 +531,34 @@ oidc:` + tt.oidcBlock + "\n") } func TestTLSACMEListenAddrCollision(t *testing.T) { - const ( - tlsKeys = `tls: - acme: - hostname: %q - challenge_type: %q - http_listen: %q -` - legacyKeys = `tls_letsencrypt_hostname: %q -tls_letsencrypt_challenge_type: %q -tls_letsencrypt_listen: %q -` - ) - tests := []struct { name string listenAddr string acmeListen string hostname string challengeType string - keys string wantErr string // empty = expect no error }{ - {"collision-numeric", ":80", ":80", "example.com", "HTTP-01", tlsKeys, "would bind the same TCP socket"}, - {"collision-named-vs-numeric", "0.0.0.0:80", ":http", "example.com", "HTTP-01", tlsKeys, "would bind the same TCP socket"}, - {"collision-https-named", ":443", ":https", "example.com", "HTTP-01", tlsKeys, "would bind the same TCP socket"}, - {"collision-legacy-keys", ":80", ":80", "example.com", "HTTP-01", legacyKeys, "would bind the same TCP socket"}, - {"canonical", "0.0.0.0:443", ":http", "example.com", "HTTP-01", tlsKeys, ""}, - {"no-hostname-skipped", "0.0.0.0:80", ":http", "", "HTTP-01", tlsKeys, ""}, - {"tls-alpn-01-skipped", "0.0.0.0:80", ":http", "example.com", "TLS-ALPN-01", tlsKeys, ""}, - {"different-numeric", ":8080", ":8081", "example.com", "HTTP-01", tlsKeys, ""}, + {"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) { - viper.Reset() - tmpDir := t.TempDir() cfg := fmt.Sprintf(`--- server_url: https://example.com listen_addr: %q -`+tt.keys+`noise: +tls: + acme: + hostname: %q + challenge_type: %q + http_listen: %q +noise: private_key_path: noise_private.key database: type: sqlite3 @@ -636,8 +583,6 @@ dns: } func TestLoadServerConfig_CollectsAcrossSubBuilders(t *testing.T) { - viper.Reset() - tmpDir := t.TempDir() cfg := `--- server_url: https://example.com @@ -647,8 +592,9 @@ prefixes: v6: also-not-a-cidr allocation: bogus oidc: - client_secret: hunter2 - client_secret_path: /nonexistent/secret + issuer: https://idp.example.com + client_id: headscale + client_secret: file:/nonexistent/secret noise: private_key_path: noise_private.key database: @@ -673,8 +619,8 @@ dns: // Sentinels stay reachable through ConfigError.Cause. require.ErrorIs(t, err, ErrInvalidAllocationStrategy, "wrapped ErrInvalidAllocationStrategy not in chain") - require.ErrorIs(t, err, errOidcMutuallyExclusive, - "wrapped errOidcMutuallyExclusive not in chain") + require.ErrorIs(t, err, os.ErrNotExist, + "wrapped secret read error not in chain") // Each sub-builder failure surfaces its YAML key in the rendered output. rendered := err.Error() @@ -698,53 +644,397 @@ func reasons(errs []*ConfigError) []string { return out } -func TestDeprecatedKeysFlowThroughValidator(t *testing.T) { - viper.Reset() +// 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:"`}, + }, + } - tmpDir := t.TempDir() - cfg := `--- -server_url: https://example.com -listen_addr: 0.0.0.0:8080 -oidc: - expiry: 1h -dns_config: - use_username_in_magic_dns: true + 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 secret settings take a value or file:, +// read only by the consumer that uses the secret. +func TestConfigSecrets(t *testing.T) { + dir := t.TempDir() + for _, name := range []string{"oidc", "pg", "api"} { + require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(name+"-from-file\n"), 0o600)) + } + + require.NoError(t, os.WriteFile(filepath.Join(dir, "empty"), []byte(" \n"), 0o600)) + + missing := "file:" + filepath.Join(dir, "missing") + + base := `--- +server_url: https://headscale.example.com noise: private_key_path: noise_private.key +prefixes: + v4: 100.64.0.0/10 database: - type: sqlite3 + type: sqlite dns: magic_dns: false override_local_dns: false +oidc: + issuer: https://idp.example.com + client_id: headscale ` - require.NoError(t, os.WriteFile( - filepath.Join(tmpDir, "config.yaml"), []byte(cfg), 0o600)) - require.NoError(t, LoadConfig(tmpDir, false)) - - err := validateServerConfig() - require.Error(t, err) - - got := ConfigErrors(err) - require.GreaterOrEqual(t, len(got), 2, - "expected fatal deprecated keys to surface as ConfigErrors; got %d", len(got)) - - reasons := make([]string, 0, len(got)) - for _, ce := range got { - reasons = append(reasons, ce.Reason) + // Postgres and a remote CLI are the consumers of the other two secrets. + active := map[string]string{ + "HEADSCALE_DATABASE_TYPE": "postgres", + "HEADSCALE_CLI_ADDRESS": "headscale.example.com:443", } - wantSubstrs := []string{"oidc.expiry", "dns_config.use_username_in_magic_dns"} - for _, want := range wantSubstrs { - var found bool + tests := []struct { + name string + yaml string + env map[string]string + want [3]string // server oidc.client_secret, server database.postgres.pass, CLI cli.api_key + wantErr string // from LoadServerConfig + wantCLIErr string + }{ + { + name: "literal values", + yaml: base + " client_secret: oidc\n", + env: map[string]string{"HEADSCALE_DATABASE_POSTGRES_PASS": "pg", "HEADSCALE_CLI_API_KEY": "api"}, + want: [3]string{"oidc", "pg", "api"}, + }, + { + name: "file values for active consumers", + yaml: base + " client_secret: file:" + filepath.Join(dir, "oidc") + "\n", + //nolint:gosec // paths to test files, not credentials + env: map[string]string{ + "HEADSCALE_DATABASE_TYPE": active["HEADSCALE_DATABASE_TYPE"], + "HEADSCALE_CLI_ADDRESS": active["HEADSCALE_CLI_ADDRESS"], + "SECRET_DIR": dir, + "HEADSCALE_DATABASE_POSTGRES_PASS": "file:$SECRET_DIR/pg", + "HEADSCALE_CLI_API_KEY": "file:${SECRET_DIR}/api", + }, + want: [3]string{"oidc-from-file", "pg-from-file", "api-from-file"}, + }, + { + // sqlite and the unix socket use neither secret. + name: "inactive consumers read nothing", + yaml: base + " client_secret: oidc\n", + env: map[string]string{"HEADSCALE_DATABASE_POSTGRES_PASS": missing, "HEADSCALE_CLI_API_KEY": missing}, + want: [3]string{"oidc", missing, missing}, + }, + { + // OIDC is active only with an issuer. + name: "inactive oidc reads nothing", + yaml: strings.Replace(base, " issuer: https://idp.example.com\n", "", 1) + " client_secret: " + missing + "\n", + want: [3]string{missing, "", ""}, + }, + { + name: "missing oidc file", + yaml: base + " client_secret: " + missing + "\n", + wantErr: "oidc.client_secret cannot be read", + }, + { + name: "empty oidc file", + yaml: base + " client_secret: file:" + filepath.Join(dir, "empty") + "\n", + wantErr: "oidc.client_secret", + }, + { + name: "missing postgres file", + yaml: base + " client_secret: oidc\n", + env: map[string]string{ + "HEADSCALE_DATABASE_TYPE": active["HEADSCALE_DATABASE_TYPE"], + "HEADSCALE_DATABASE_POSTGRES_PASS": missing, + }, + wantErr: "database.postgres.pass cannot be read", + }, + { + name: "missing api key file for a remote CLI", + yaml: base + " client_secret: oidc\n", + env: map[string]string{ + "HEADSCALE_CLI_ADDRESS": active["HEADSCALE_CLI_ADDRESS"], + "HEADSCALE_CLI_API_KEY": missing, + }, + wantCLIErr: "cli.api_key cannot be read", + }, + } - for _, r := range reasons { - if strings.Contains(r, want) { - found = true - break + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for k, v := range tt.env { + t.Setenv(k, v) } - } - assert.True(t, found, "expected ConfigError mentioning %q in reasons %v", want, reasons) + tmpDir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "config.yaml"), []byte(tt.yaml), 0o600)) + require.NoError(t, LoadConfig(tmpDir, false)) + + cfg, err := LoadServerConfig() + if tt.wantErr != "" { + require.ErrorContains(t, err, tt.wantErr) + + return + } + + require.NoError(t, err) + + // The server never uses the CLI's key, so never reads it. + assert.Empty(t, cfg.CLI.APIKey) + + cli, err := LoadCLIConfig() + if tt.wantCLIErr != "" { + require.ErrorContains(t, err, tt.wantCLIErr) + + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, [3]string{cfg.OIDC.ClientSecret, cfg.Database.Postgres.Pass, cli.CLI.APIKey}) + }) } } @@ -783,8 +1073,6 @@ func TestUnixSocketPermission(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - viper.Reset() - if tt.env != "" { t.Setenv("HEADSCALE_UNIX_SOCKET_PERMISSION", tt.env) } @@ -838,15 +1126,14 @@ func TestServerURLTrailingSlash(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) - require.NoError(t, LoadConfig("testdata/minimal.yaml", true)) - - viper.Set("server_url", tt.input) - viper.Set("prefixes.v4", "100.64.0.0/10") - viper.Set("database.type", "sqlite3") - viper.Set("dns.magic_dns", false) - viper.Set("dns.override_local_dns", false) + 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) @@ -1067,12 +1354,13 @@ func TestTrustedProxies(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - viper.Reset() - + values := map[string]any{} if tt.input != nil { - viper.Set("trusted_proxies", tt.input) + values["trusted_proxies"] = tt.input } + useConfig(t, values) + got, err := trustedProxies() if tt.wantErr != "" { @@ -1143,11 +1431,14 @@ dns: yaml string env map[string]string wantErr string + fileErr bool // only reading a file: secret fails, so CheckServerConfig 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", @@ -1182,14 +1473,182 @@ dns: 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_PATH": secret, + "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 CheckServerConfig, 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", + }, + { + // A failed decode can still fill part of the map; none of it is used. + name: "provider env json that does not decode", + yaml: base, + env: map[string]string{ + "HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare", + "HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"api_token":"marker-token","zone_token":1e1000}`, + }, + wantErr: "dns.challenge.provider is invalid", + }, + { + // 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: @@ -1273,7 +1732,7 @@ dns: subdomain: reg server_url: https://auth.acme-dns.io `, - wantErr: "desktop.ts.example.com and laptop.ts.example.com share registration reg", + wantErr: "desktop.ts.example.com and laptop.ts.example.com share a registration", }, { name: "acmedns one account for headscale's own certificate", @@ -1314,7 +1773,7 @@ tls: hostname: headscale.example.com challenge_type: DNS-01 `, - wantErr: "headscale.example.com and laptop.ts.example.com share registration reg", + wantErr: "headscale.example.com and laptop.ts.example.com share a registration", }, { name: "enabled without magic dns", @@ -1389,8 +1848,6 @@ tls: for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - viper.Reset() - for k, v := range tt.env { t.Setenv(k, v) } @@ -1398,6 +1855,19 @@ tls: 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) + } + + _, checkErr := CheckServerConfig() + if tt.wantErr != "" && !tt.fileErr { + require.ErrorContains(t, checkErr, tt.wantErr) + assert.NotContains(t, checkErr.Error(), "marker-") + } else { + require.NoError(t, checkErr) + } cfg, err := LoadServerConfig() if tt.wantErr != "" { @@ -1419,6 +1889,12 @@ tls: return } + if tt.wantProvider != "" { + assert.Equal(t, tt.wantProvider, fmt.Sprintf("%T", cfg.DNSChallenge.Provider)) + + return + } + if iso, ok := cfg.DNSChallenge.Provider.(hsdns.Isolator); ok { if tt.wantIsolates != "" { assert.True(t, iso.Isolates(tt.wantIsolates)) @@ -1434,10 +1910,10 @@ tls: } } -// TestTLSLegacyKeys pins that the flat TLS keys keep working under the tls -// block, with the tls keys winning, and that the NixOS module's defaults -// for the flat keys leave TLS off. -func TestTLSLegacyKeys(t *testing.T) { +// 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: @@ -1452,10 +1928,11 @@ dns: ` tests := []struct { - name string - yaml string - env map[string]string - want TLSConfig + name string + yaml string + env map[string]string + want TLSConfig + wantErr []string }{ { name: "defaults", @@ -1475,49 +1952,33 @@ tls_letsencrypt_cache_dir: /var/lib/headscale/cache acme_url: https://acme.example.com/directory acme_email: admin@example.com `, - want: TLSConfig{ACME: ACMEConfig{ - Hostname: "headscale.example.com", - ChallengeType: TLSALPN01ChallengeType, - HTTPListen: ":8080", - URL: "https://acme.example.com/directory", - Email: "admin@example.com", - CacheDir: "/var/lib/headscale/cache", - }}, + 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: "tls keys win over legacy keys", + 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 `, - want: TLSConfig{ - CertPath: "/new/cert.pem", - KeyPath: "/new/key.pem", - ACME: ACMEConfig{ - ChallengeType: HTTP01ChallengeType, - HTTPListen: ":http", - CacheDir: "/var/lib/headscale/cache", - }, - }, + wantErr: []string{"tls_cert_path was removed", "use tls.cert_path", "tls_key_path was removed"}, }, { - // What the NixOS module writes when TLS is off. + // 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 `, - want: TLSConfig{ACME: ACMEConfig{ - ChallengeType: HTTP01ChallengeType, - HTTPListen: ":http", - CacheDir: "/var/lib/headscale/.cache", - }}, + wantErr: []string{"tls_letsencrypt_hostname was removed", "tls_letsencrypt_cache_dir was removed"}, }, { - // Both keys read HEADSCALE_TLS_CERT_PATH. name: "env", yaml: base, env: map[string]string{ @@ -1538,15 +1999,24 @@ tls_letsencrypt_cache_dir: /var/lib/headscale/.cache for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - viper.Reset() - 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)) + + 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) @@ -1711,8 +2181,6 @@ func TestBoolConfigValidation(t *testing.T) { } t.Run(source+"/"+tt.value, func(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) t.Setenv("HEADSCALE_OIDC_EMAIL_VERIFIED_REQUIRED", "") config := `server_url: https://headscale.example.com @@ -1753,6 +2221,30 @@ dns: } } +// 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, "config.yaml", 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 TestBoolConfigKeysComplete(t *testing.T) { file, err := parser.ParseFile(token.NewFileSet(), "config.go", nil, 0) require.NoError(t, err) @@ -1766,12 +2258,12 @@ func TestBoolConfigKeysComplete(t *testing.T) { } selector, ok := call.Fun.(*ast.SelectorExpr) - if !ok || selector.Sel.Name != "GetBool" { + if !ok || selector.Sel.Name != "Bool" { return true } object, ok := selector.X.(*ast.Ident) - if !ok || object.Name != "viper" { + if !ok || object.Name != "src" { return true } @@ -1793,12 +2285,10 @@ func TestBoolConfigKeysComplete(t *testing.T) { delete(keys, key) } - assert.Empty(t, keys, "GetBool keys missing from boolean validation") + assert.Empty(t, keys, "Bool keys missing from boolean validation") } func TestExampleConfigBooleanSettings(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) require.NoError(t, LoadConfig("../../config-example.yaml", true)) _, err := LoadServerConfig() @@ -1808,20 +2298,18 @@ func TestExampleConfigBooleanSettings(t *testing.T) { func TestLoadCLIConfigRejectsNonBoolean(t *testing.T) { for _, key := range []string{"cli.insecure", "disable_check_updates"} { t.Run(key, func(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) - path := filepath.Join(t.TempDir(), "config.yaml") require.NoError(t, os.WriteFile(path, nil, 0o600)) - require.NoError(t, LoadConfig(path, true)) - env := "HEADSCALE_" + strings.ToUpper(strings.ReplaceAll(key, ".", "_")) + 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) @@ -1933,7 +2421,8 @@ func unsetHeadscaleEnv(t *testing.T) { // (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. +// MinimumConfigYAML and DefaultConfigEnv. -strict is the same source +// as updated for the strict loader. func TestConfigCompat(t *testing.T) { rows := compatRows(t) update := os.Getenv("HEADSCALE_UPDATE_GOLDEN") != "" @@ -1971,9 +2460,6 @@ func TestConfigCompat(t *testing.T) { func loadCompat(t *testing.T, file, envFile string, cli bool) compatRecord { t.Helper() - viper.Reset() - t.Cleanup(viper.Reset) - dir := stageCompat(t, file, envFile) var buf bytes.Buffer @@ -2093,9 +2579,6 @@ func TestDERPServerPrivateKeyPath(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) - dir := t.TempDir() require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(`--- noise: @@ -2139,9 +2622,6 @@ func TestPolicyMode(t *testing.T) { {`""`, true}, } { t.Run(tt.mode, func(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) - dir := t.TempDir() require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(`--- noise: @@ -2169,3 +2649,98 @@ policy: }) } } + +// 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 = CheckServerConfig() + 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() + + s := &configSource{k: koanf.New(".")} + require.NoError(t, s.k.Load(confmap.Provider(configDefaults, "."), 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, filepath.Join(dir, "noise.key"), cfg.NoisePrivateKeyPath) + assert.Equal(t, "./policy.hujson", cfg.Policy.Path) + assert.Equal(t, filepath.Join(dir, "policy.hujson"), util.AbsolutePath(cfg.ConfigDir, 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.Database.Sqlite.Path) + assert.True(t, filepath.IsAbs(cfg.NoisePrivateKeyPath), cfg.NoisePrivateKeyPath) + }) + } +} diff --git a/hscontrol/types/derp_config_test.go b/hscontrol/types/derp_config_test.go index ffff3ed27..fd77e0bc6 100644 --- a/hscontrol/types/derp_config_test.go +++ b/hscontrol/types/derp_config_test.go @@ -2,8 +2,6 @@ package types import ( "testing" - - "github.com/spf13/viper" ) // TestDerpConfigSkipsMalformedURL ensures a malformed derp.urls entry is @@ -11,15 +9,12 @@ import ( // nil *url.URL that url.Parse returns on error, which crashed the server at // startup. func TestDerpConfigSkipsMalformedURL(t *testing.T) { - viper.Reset() - defer viper.Reset() - - viper.Set("derp.urls", []string{ + useConfig(t, map[string]any{"derp.urls": []string{ "https://controlplane.tailscale.com/derpmap/default", "://bad", - }) + }}) - cfg := derpConfig() + cfg := derpConfig("") if len(cfg.URLs) != 1 { t.Fatalf("expected the malformed derp.urls entry to be skipped, got %d urls: %v", diff --git a/hscontrol/types/listener.go b/hscontrol/types/listener.go index dfca1883a..02ef8b506 100644 --- a/hscontrol/types/listener.go +++ b/hscontrol/types/listener.go @@ -5,8 +5,6 @@ import ( "fmt" "net" "strconv" - - "github.com/spf13/viper" ) var errEmptyListenAddr = errors.New("address is empty") @@ -106,21 +104,19 @@ func validateListenerCollisions(v *configValidator) { active bool } - listenAddr := viper.GetString("listen_addr") - grpcAddr := viper.GetString("grpc_listen_addr") - metricsAddr := viper.GetString("metrics_listen_addr") - acmeAddr := tlsKey("tls.acme.http_listen") + listenAddr := src.String("listen_addr") + metricsAddr := src.String("metrics_listen_addr") + acmeAddr := src.String("tls.acme.http_listen") listeners := []spec{ {key: "listen_addr", addr: listenAddr, active: listenAddr != ""}, - {key: "grpc_listen_addr", addr: grpcAddr, active: grpcAddr != ""}, {key: "metrics_listen_addr", addr: metricsAddr, active: metricsAddr != ""}, { key: "tls.acme.http_listen", addr: acmeAddr, active: acmeAddr != "" && - tlsKey("tls.acme.hostname") != "" && - tlsKey("tls.acme.challenge_type") == HTTP01ChallengeType, + src.String("tls.acme.hostname") != "" && + src.String("tls.acme.challenge_type") == HTTP01ChallengeType, }, } diff --git a/hscontrol/types/listener_test.go b/hscontrol/types/listener_test.go index 88b56070f..6b5c6c497 100644 --- a/hscontrol/types/listener_test.go +++ b/hscontrol/types/listener_test.go @@ -7,7 +7,6 @@ import ( "syscall" "testing" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -82,15 +81,12 @@ func TestValidateListenerCollisions_BlamesParseFailure(t *testing.T) { good string // sibling key with a valid addr badAddr string }{ - {"bad-listen_addr", "listen_addr", "grpc_listen_addr", "garbage"}, - {"bad-grpc_listen_addr", "grpc_listen_addr", "listen_addr", "also-garbage"}, + {"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) { - viper.Reset() - viper.Set(tt.bad, tt.badAddr) - viper.Set(tt.good, ":9999") + useConfig(t, map[string]any{tt.bad: tt.badAddr, tt.good: ":9999"}) v := &configValidator{} validateListenerCollisions(v) diff --git a/hscontrol/types/secret.go b/hscontrol/types/secret.go new file mode 100644 index 000000000..6a410f1dd --- /dev/null +++ b/hscontrol/types/secret.go @@ -0,0 +1,68 @@ +package types + +import ( + "errors" + "fmt" + "os" + "strings" + + hsdns "github.com/juanfont/headscale/hscontrol/dns" +) + +var errSecretEmpty = errors.New("file is empty") + +// resolveSecret returns value, or the trimmed contents of the file named +// by "file:" (Tailscale's form), which keeps the secret out of the +// config and the environment. Variables in the path are expanded so a +// systemd credential can be named by ${CREDENTIALS_DIRECTORY}. +func resolveSecret(key, value string) (string, error) { + path, ok := strings.CutPrefix(value, "file:") + if !ok { + return value, nil + } + + path = os.ExpandEnv(path) + + b, err := os.ReadFile(path) + if err != nil { + return "", fmt.Errorf("%s: %w", key, err) + } + + // An empty file is a credential that was not written, not a choice. + secret := strings.TrimSpace(string(b)) + if secret == "" { + return "", fmt.Errorf("%s: %s: %w", key, path, errSecretEmpty) + } + + 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 +} + +// configSecret resolves the secret setting key, recording a failure in v. +func configSecret(v *configValidator, resolve func(key, value string) (string, error), key, value string) string { + secret, err := resolve(key, value) + if errors.Is(err, hsdns.ErrSecretUnread) { + return value + } + + if err != nil { + v.Add(&ConfigError{ + Reason: key + " cannot be read", + Current: []KV{{key, value}}, + Detail: err.Error(), + Hint: "ensure the file exists, is not empty and is readable by the user running headscale", + Cause: err, + }) + } + + return secret +} diff --git a/hscontrol/types/secret_test.go b/hscontrol/types/secret_test.go new file mode 100644 index 000000000..2ada62b4e --- /dev/null +++ b/hscontrol/types/secret_test.go @@ -0,0 +1,46 @@ +package types + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestResolveSecret(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "s"), []byte(" from-file\n"), 0o600)) + require.NoError(t, os.WriteFile(filepath.Join(dir, "empty"), []byte(" \n"), 0o600)) + t.Setenv("SECRET_DIR", dir) + + tests := []struct { + name string + value string + want string + wantErr string + }{ + {name: "literal", value: "hunter2", want: "hunter2"}, + {name: "empty", value: "", want: ""}, + {name: "file is read and trimmed", value: "file:" + filepath.Join(dir, "s"), want: "from-file"}, + {name: "env in path is expanded", value: "file:${SECRET_DIR}/s", want: "from-file"}, + {name: "missing file names the key", value: "file:" + filepath.Join(dir, "missing"), wantErr: "no such file or directory"}, + {name: "empty file", value: "file:" + filepath.Join(dir, "empty"), wantErr: "is empty"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := resolveSecret("oidc.client_secret", tt.value) + if tt.wantErr != "" { + require.ErrorContains(t, err, tt.wantErr) + require.ErrorContains(t, err, "oidc.client_secret: ") + + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } +} diff --git a/hscontrol/types/testdata/compat/bad-values-strict.golden.json b/hscontrol/types/testdata/compat/bad-values-strict.golden.json new file mode 100644 index 000000000..f4343479d --- /dev/null +++ b/hscontrol/types/testdata/compat/bad-values-strict.golden.json @@ -0,0 +1,169 @@ +{ + "config": { + "ServerURL": "https://hs.example.com", + "Addr": "", + "MetricsAddr": "", + "TrustedProxies": null, + "Node": { + "Expiry": 0, + "Ephemeral": { + "InactivityTimeout": 120000000000 + }, + "Routes": { + "HA": { + "ProbeInterval": 10000000000, + "ProbeTimeout": 5000000000 + } + } + }, + "PreAuthKeys": { + "RevokedRetention": 604800000000000 + }, + "PrefixV4": "10.0.0.0/8", + "PrefixV6": null, + "IPAllocation": "sequential", + "NoisePrivateKeyPath": "/var/lib/headscale/noise.key", + "BaseDomain": "", + "Log": { + "Format": "", + "Level": "debug" + }, + "DisableUpdateCheck": false, + "Database": { + "Type": "sqlite3", + "Debug": false, + "Gorm": { + "Debug": false, + "SlowThreshold": 0, + "SkipErrRecordNotFound": false, + "ParameterizedQueries": false + }, + "Sqlite": { + "Path": "/var/lib/headscale/db.sqlite", + "WriteAheadLog": true, + "WALAutoCheckPoint": 1000 + }, + "Postgres": { + "Host": "", + "Port": 0, + "Name": "", + "User": "", + "Ssl": "false", + "MaxOpenConnections": 10, + "MaxIdleConnections": 10, + "ConnMaxIdleTimeSecs": 3600 + } + }, + "DERP": { + "ServerEnabled": false, + "AutomaticallyAddEmbeddedDerpRegion": true, + "ServerRegionID": 0, + "ServerRegionCode": "", + "ServerRegionName": "", + "ServerPrivateKeyPath": "", + "ServerVerifyClients": true, + "STUNAddr": "", + "URLs": [], + "Paths": null, + "DERPMap": null, + "AutoUpdate": false, + "UpdateFrequency": 10800000000000, + "IPv4": "", + "IPv6": "" + }, + "TLS": { + "CertPath": "", + "KeyPath": "", + "ACME": { + "Hostname": "", + "ChallengeType": "HTTP-01", + "HTTPListen": ":http", + "URL": "", + "Email": "", + "CacheDir": "/var/lib/headscale/cache" + } + }, + "DNSConfig": { + "MagicDNS": false, + "BaseDomain": "", + "OverrideLocalDNS": false, + "Nameservers": { + "Global": [], + "Split": {} + }, + "SearchDomains": [], + "ExtraRecords": null, + "ExtraRecordsPath": "", + "Certificates": { + "Enabled": false, + "Zone": "" + } + }, + "TailcfgDNSConfig": {}, + "DNSChallenge": { + "PropagationTimeout": 120000000000, + "Resolvers": [ + "1.1.1.1:53", + "8.8.8.8:53" + ], + "ProviderName": "" + }, + "UnixSocket": "/var/run/headscale/headscale.sock", + "UnixSocketPermission": 504, + "OIDC": { + "OnlyStartIfOIDCIsAvailable": true, + "Issuer": "", + "ClientID": "", + "Scope": [ + "openid", + "profile", + "email" + ], + "ExtraParams": {}, + "AllowedDomains": null, + "AllowedUsers": null, + "AllowedGroups": null, + "EmailVerifiedRequired": true, + "UseExpiryFromToken": false, + "PKCE": { + "Enabled": false, + "Method": "S256" + } + }, + "LogTail": { + "Enabled": false + }, + "Taildrop": { + "Enabled": true + }, + "AutoUpdate": { + "Enabled": false + }, + "CLI": { + "Address": "", + "Timeout": 5000000000, + "Insecure": false + }, + "Policy": { + "Path": "", + "Mode": "file" + }, + "Tuning": { + "NotifierSendTimeout": 0, + "BatchChangeDelay": 800000000, + "NodeMapSessionBufferedChanSize": 30, + "BatcherWorkers": 0, + "RegisterCacheExpiration": 0, + "RegisterCacheMaxEntries": 0, + "NodeStoreBatchSize": 100, + "NodeStoreBatchTimeout": 500000000 + } + }, + "warnings": [ + "\n################################################################\n### __ __ _ _ ###\n### \\ \\ / / (_) | | ###\n### \\ \\ /\\ / /_ _ _ __ _ __ _ _ __ __ _| | ###\n### \\ \\/ \\/ / _` | '__| '_ \\| | '_ \\ / _` | | ###\n### \\ /\\ / (_| | | | | | | | | | | (_| |_| ###\n### \\/ \\/ \\__,_|_| |_| |_|_|_| |_|\\__, (_) ###\n### __/ | ###\n### |___/ ###\n################################################################\n### ###\n### You have overridden the default Headscale IP prefixes ###\n### with a range outside of the standard CGNAT and/or ULA ###\n### ranges. This is NOT a supported configuration. ###\n### ###\n### Using subsets of the default ranges (100.64.0.0/10 for ###\n### IPv4, fd7a:115c:a1e0::/48 for IPv6) is fine. Using ###\n### ranges outside of these will cause undefined behaviour ###\n### as the Tailscale client is NOT designed to operate on ###\n### any other ranges. ###\n### ###\n### Please revert your prefixes to subsets of the standard ###\n### ranges as described in the example configuration. ###\n### ###\n### Any issue raised using a range outside of the ###\n### supported range will be labelled as wontfix ###\n### and closed. ###\n### ###\n################################################################" + ], + "log_errors": [ + "Could not parse log format: xml. Valid choices are 'json' or 'text'", + "Failed to parse url, ignoring..." + ] +} diff --git a/hscontrol/types/testdata/compat/bad-values-strict.yaml b/hscontrol/types/testdata/compat/bad-values-strict.yaml new file mode 100644 index 000000000..6681b84fe --- /dev/null +++ b/hscontrol/types/testdata/compat/bad-values-strict.yaml @@ -0,0 +1,21 @@ +# Input today's loader accepts but mangles or ignores. +server_url: https://hs.example.com +noise: + private_key_path: /var/lib/headscale/noise.key +prefixes: + v4: 10.0.0.0/8 +database: + type: sqlite + sqlite: + path: /var/lib/headscale/db.sqlite +dns: + magic_dns: false + override_local_dns: false +log: + level: verbose + format: xml +tuning: + notifier_send_timeout: abc +derp: + urls: + - "://not-a-url" diff --git a/hscontrol/types/testdata/compat/bad-values.golden.json b/hscontrol/types/testdata/compat/bad-values.golden.json index 895ff7aac..2e68e42d7 100644 --- a/hscontrol/types/testdata/compat/bad-values.golden.json +++ b/hscontrol/types/testdata/compat/bad-values.golden.json @@ -1,170 +1,3 @@ { - "config": { - "ServerURL": "https://hs.example.com", - "Addr": "", - "MetricsAddr": "", - "TrustedProxies": null, - "Node": { - "Expiry": 0, - "Ephemeral": { - "InactivityTimeout": 120000000000 - }, - "Routes": { - "HA": { - "ProbeInterval": 10000000000, - "ProbeTimeout": 5000000000 - } - } - }, - "PreAuthKeys": { - "RevokedRetention": 604800000000000 - }, - "PrefixV4": "10.0.0.0/8", - "PrefixV6": null, - "IPAllocation": "sequential", - "NoisePrivateKeyPath": "/var/lib/headscale/noise.key", - "BaseDomain": "", - "Log": { - "Format": "", - "Level": "debug" - }, - "DisableUpdateCheck": false, - "Database": { - "Type": "sqlite3", - "Debug": false, - "Gorm": { - "Debug": false, - "SlowThreshold": 0, - "SkipErrRecordNotFound": false, - "ParameterizedQueries": false - }, - "Sqlite": { - "Path": "/var/lib/headscale/db.sqlite", - "WriteAheadLog": true, - "WALAutoCheckPoint": 1000 - }, - "Postgres": { - "Host": "", - "Port": 0, - "Name": "", - "User": "", - "Ssl": "false", - "MaxOpenConnections": 10, - "MaxIdleConnections": 10, - "ConnMaxIdleTimeSecs": 3600 - } - }, - "DERP": { - "ServerEnabled": false, - "AutomaticallyAddEmbeddedDerpRegion": true, - "ServerRegionID": 0, - "ServerRegionCode": "", - "ServerRegionName": "", - "ServerPrivateKeyPath": "", - "ServerVerifyClients": true, - "STUNAddr": "", - "URLs": [], - "Paths": null, - "DERPMap": null, - "AutoUpdate": false, - "UpdateFrequency": 10800000000000, - "IPv4": "", - "IPv6": "" - }, - "TLS": { - "CertPath": "", - "KeyPath": "", - "ACME": { - "Hostname": "", - "ChallengeType": "HTTP-01", - "HTTPListen": ":http", - "URL": "", - "Email": "", - "CacheDir": "/var/lib/headscale/cache" - } - }, - "DNSConfig": { - "MagicDNS": false, - "BaseDomain": "", - "OverrideLocalDNS": false, - "Nameservers": { - "Global": [], - "Split": {} - }, - "SearchDomains": [], - "ExtraRecords": null, - "ExtraRecordsPath": "", - "Certificates": { - "Enabled": false, - "Zone": "" - } - }, - "TailcfgDNSConfig": {}, - "DNSChallenge": { - "PropagationTimeout": 120000000000, - "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" - ], - "ProviderName": "" - }, - "UnixSocket": "/var/run/headscale/headscale.sock", - "UnixSocketPermission": 504, - "OIDC": { - "OnlyStartIfOIDCIsAvailable": true, - "Issuer": "", - "ClientID": "", - "Scope": [ - "openid", - "profile", - "email" - ], - "ExtraParams": {}, - "AllowedDomains": null, - "AllowedUsers": null, - "AllowedGroups": null, - "EmailVerifiedRequired": true, - "UseExpiryFromToken": false, - "PKCE": { - "Enabled": false, - "Method": "S256" - } - }, - "LogTail": { - "Enabled": false - }, - "Taildrop": { - "Enabled": true - }, - "AutoUpdate": { - "Enabled": false - }, - "CLI": { - "Address": "", - "Timeout": 5000000000, - "Insecure": false - }, - "Policy": { - "Path": "", - "Mode": "file" - }, - "Tuning": { - "NotifierSendTimeout": 0, - "BatchChangeDelay": 800000000, - "NodeMapSessionBufferedChanSize": 30, - "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, - "NodeStoreBatchSize": 100, - "NodeStoreBatchTimeout": 500000000 - } - }, - "warnings": [ - "configuration key \"ephemeral_node_inactivity_timeout\" is deprecated; use \"node.ephemeral.inactivity_timeout\" instead", - "\n################################################################\n### __ __ _ _ ###\n### \\ \\ / / (_) | | ###\n### \\ \\ /\\ / /_ _ _ __ _ __ _ _ __ __ _| | ###\n### \\ \\/ \\/ / _` | '__| '_ \\| | '_ \\ / _` | | ###\n### \\ /\\ / (_| | | | | | | | | | | (_| |_| ###\n### \\/ \\/ \\__,_|_| |_| |_|_|_| |_|\\__, (_) ###\n### __/ | ###\n### |___/ ###\n################################################################\n### ###\n### You have overridden the default Headscale IP prefixes ###\n### with a range outside of the standard CGNAT and/or ULA ###\n### ranges. This is NOT a supported configuration. ###\n### ###\n### Using subsets of the default ranges (100.64.0.0/10 for ###\n### IPv4, fd7a:115c:a1e0::/48 for IPv6) is fine. Using ###\n### ranges outside of these will cause undefined behaviour ###\n### as the Tailscale client is NOT designed to operate on ###\n### any other ranges. ###\n### ###\n### Please revert your prefixes to subsets of the standard ###\n### ranges as described in the example configuration. ###\n### ###\n### Any issue raised using a range outside of the ###\n### supported range will be labelled as wontfix ###\n### and closed. ###\n### ###\n################################################################" - ], - "log_errors": [ - "Could not parse log format: xml. Valid choices are 'json' or 'text'", - "Failed to parse url, ignoring..." - ] + "error": "Fatal config error: unknown configuration key dns.magic_dsn ($DIR/config.yaml:13:3)\n\nFatal config error: configuration key ephemeral_node_inactivity_timeout was removed in 0.30 ($DIR/config.yaml:23:1)\n hint: use node.ephemeral.inactivity_timeout\n" } diff --git a/hscontrol/types/testdata/compat/env-legacy-ignored.golden.json b/hscontrol/types/testdata/compat/env-legacy-ignored.golden.json index f9d1da521..fce4e981a 100644 --- a/hscontrol/types/testdata/compat/env-legacy-ignored.golden.json +++ b/hscontrol/types/testdata/compat/env-legacy-ignored.golden.json @@ -161,8 +161,10 @@ }, "warnings": [ "no config file found, using defaults", - "configuration key \"dns_config.magic_dns\" is deprecated; use \"dns.magic_dns\" instead", - "configuration key \"ephemeral_node_inactivity_timeout\" is deprecated; use \"node.ephemeral.inactivity_timeout\" instead", - "configuration key \"database.gorm.prepare_stmt\" is ignored" + "configuration variable HEADSCALE_DATABASE_GORM_PREPARE_STMT was removed in 0.30; remove HEADSCALE_DATABASE_GORM_PREPARE_STMT; see the CHANGELOG for context", + "configuration variable HEADSCALE_DB_TYPE was removed in 0.30; use HEADSCALE_DATABASE_TYPE", + "configuration variable HEADSCALE_DNS_CONFIG_MAGIC_DNS was removed in 0.30; use HEADSCALE_DNS_MAGIC_DNS", + "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" ] } diff --git a/hscontrol/types/testdata/compat/env-legacy-removed.golden.json b/hscontrol/types/testdata/compat/env-legacy-removed.golden.json index 4bad80b8d..1b18503d1 100644 --- a/hscontrol/types/testdata/compat/env-legacy-removed.golden.json +++ b/hscontrol/types/testdata/compat/env-legacy-removed.golden.json @@ -1,6 +1,168 @@ { + "config": { + "ServerURL": "https://hs.example.com", + "Addr": "", + "MetricsAddr": "", + "TrustedProxies": null, + "Node": { + "Expiry": 0, + "Ephemeral": { + "InactivityTimeout": 120000000000 + }, + "Routes": { + "HA": { + "ProbeInterval": 10000000000, + "ProbeTimeout": 5000000000 + } + } + }, + "PreAuthKeys": { + "RevokedRetention": 604800000000000 + }, + "PrefixV4": "100.64.0.0/10", + "PrefixV6": null, + "IPAllocation": "sequential", + "NoisePrivateKeyPath": "/var/lib/headscale/noise.key", + "BaseDomain": "", + "Log": { + "Format": "text", + "Level": "info" + }, + "DisableUpdateCheck": false, + "Database": { + "Type": "sqlite3", + "Debug": false, + "Gorm": { + "Debug": false, + "SlowThreshold": 0, + "SkipErrRecordNotFound": false, + "ParameterizedQueries": false + }, + "Sqlite": { + "Path": "/var/lib/headscale/db.sqlite", + "WriteAheadLog": true, + "WALAutoCheckPoint": 1000 + }, + "Postgres": { + "Host": "", + "Port": 0, + "Name": "", + "User": "", + "Ssl": "false", + "MaxOpenConnections": 10, + "MaxIdleConnections": 10, + "ConnMaxIdleTimeSecs": 3600 + } + }, + "DERP": { + "ServerEnabled": false, + "AutomaticallyAddEmbeddedDerpRegion": true, + "ServerRegionID": 0, + "ServerRegionCode": "", + "ServerRegionName": "", + "ServerPrivateKeyPath": "", + "ServerVerifyClients": true, + "STUNAddr": "", + "URLs": [], + "Paths": null, + "DERPMap": null, + "AutoUpdate": false, + "UpdateFrequency": 10800000000000, + "IPv4": "", + "IPv6": "" + }, + "TLS": { + "CertPath": "", + "KeyPath": "", + "ACME": { + "Hostname": "", + "ChallengeType": "HTTP-01", + "HTTPListen": ":http", + "URL": "", + "Email": "", + "CacheDir": "/var/lib/headscale/cache" + } + }, + "DNSConfig": { + "MagicDNS": false, + "BaseDomain": "", + "OverrideLocalDNS": false, + "Nameservers": { + "Global": [], + "Split": {} + }, + "SearchDomains": [], + "ExtraRecords": null, + "ExtraRecordsPath": "", + "Certificates": { + "Enabled": false, + "Zone": "" + } + }, + "TailcfgDNSConfig": {}, + "DNSChallenge": { + "PropagationTimeout": 120000000000, + "Resolvers": [ + "1.1.1.1:53", + "8.8.8.8:53" + ], + "ProviderName": "" + }, + "UnixSocket": "/var/run/headscale/headscale.sock", + "UnixSocketPermission": 504, + "OIDC": { + "OnlyStartIfOIDCIsAvailable": true, + "Issuer": "", + "ClientID": "", + "Scope": [ + "openid", + "profile", + "email" + ], + "ExtraParams": {}, + "AllowedDomains": null, + "AllowedUsers": null, + "AllowedGroups": null, + "EmailVerifiedRequired": true, + "UseExpiryFromToken": false, + "PKCE": { + "Enabled": false, + "Method": "S256" + } + }, + "LogTail": { + "Enabled": false + }, + "Taildrop": { + "Enabled": true + }, + "AutoUpdate": { + "Enabled": false + }, + "CLI": { + "Address": "", + "Timeout": 5000000000, + "Insecure": false + }, + "Policy": { + "Path": "", + "Mode": "file" + }, + "Tuning": { + "NotifierSendTimeout": 800000000, + "BatchChangeDelay": 800000000, + "NodeMapSessionBufferedChanSize": 30, + "BatcherWorkers": 0, + "RegisterCacheExpiration": 0, + "RegisterCacheMaxEntries": 0, + "NodeStoreBatchSize": 100, + "NodeStoreBatchTimeout": 500000000 + } + }, "warnings": [ - "no config file found, using defaults" - ], - "error": "Fatal config error: configuration key acl_policy_path has been removed; use policy.path instead\n current: acl_policy_path: \"/etc/headscale/acl.hujson\"\n hint: remove acl_policy_path and set policy.path\n\nFatal config error: configuration key randomize_client_port has been removed\n current: randomize_client_port: \"true\"\n hint: Set \"randomizeClientPort\": true at the top level of your policy file (see policy.path / policy.mode), or grant the cap per-node via a \"nodeAttrs\" entry. See CHANGELOG.md (BREAKING / Configuration).\n\nFatal config error: configuration key oidc.expiry has been removed; use node.expiry instead\n current: oidc.expiry: \"1d\"\n hint: remove oidc.expiry and set node.expiry\n" + "no config file found, using defaults", + "configuration variable HEADSCALE_ACL_POLICY_PATH was removed in 0.30; use HEADSCALE_POLICY_PATH", + "configuration variable HEADSCALE_OIDC_EXPIRY was removed in 0.29; use HEADSCALE_NODE_EXPIRY", + "configuration variable HEADSCALE_RANDOMIZE_CLIENT_PORT was removed in 0.29; set \"randomizeClientPort\": true at the top level of the policy file, or grant the cap per node via a \"nodeAttrs\" entry" + ] } diff --git a/hscontrol/types/testdata/compat/env-secrets.golden.json b/hscontrol/types/testdata/compat/env-secrets.golden.json index 16ddcce7e..1a0d38e06 100644 --- a/hscontrol/types/testdata/compat/env-secrets.golden.json +++ b/hscontrol/types/testdata/compat/env-secrets.golden.json @@ -160,7 +160,6 @@ } }, "secrets": { - "cli.api_key": "marker-cli-api-key", "database.postgres.pass": "marker-postgres-pass", "oidc.client_secret": "marker-oidc-secret" }, diff --git a/hscontrol/types/testdata/compat/example-v0.23.0.golden.json b/hscontrol/types/testdata/compat/example-v0.23.0.golden.json index e2f101023..c8fda0fb6 100644 --- a/hscontrol/types/testdata/compat/example-v0.23.0.golden.json +++ b/hscontrol/types/testdata/compat/example-v0.23.0.golden.json @@ -1,7 +1,3 @@ { - "warnings": [ - "configuration key \"ephemeral_node_inactivity_timeout\" is deprecated; use \"node.ephemeral.inactivity_timeout\" instead", - "configuration key \"database.gorm.prepare_stmt\" is ignored" - ], - "error": "Fatal config error: configuration key dns.use_username_in_magic_dns has been removed\n current: dns.use_username_in_magic_dns: false\n hint: remove dns.use_username_in_magic_dns; see the CHANGELOG for context\n\nFatal config error: configuration key randomize_client_port has been removed\n current: randomize_client_port: false\n hint: Set \"randomizeClientPort\": true at the top level of your policy file (see policy.path / policy.mode), or grant the cap per-node via a \"nodeAttrs\" entry. See CHANGELOG.md (BREAKING / Configuration).\n" + "error": "Fatal config error: configuration key acme_email was removed in 0.30 ($DIR/config.yaml:200:1)\n hint: use tls.acme.email\n\nFatal config error: configuration key acme_url was removed in 0.30 ($DIR/config.yaml:197:1)\n hint: use tls.acme.url\n\nFatal config error: configuration key database.gorm.prepare_stmt was removed in 0.30 ($DIR/config.yaml:152:5)\n hint: remove database.gorm.prepare_stmt; see the CHANGELOG for context\n\nFatal config error: configuration key dns.use_username_in_magic_dns was removed in 0.24 ($DIR/config.yaml:317:3)\n hint: remove dns.use_username_in_magic_dns; see the CHANGELOG for context\n\nFatal config error: configuration key ephemeral_node_inactivity_timeout was removed in 0.30 ($DIR/config.yaml:138:1)\n hint: use node.ephemeral.inactivity_timeout\n\nFatal config error: configuration key grpc_allow_insecure was removed in 0.30 ($DIR/config.yaml:41:1)\n hint: remove grpc_allow_insecure; see the CHANGELOG for context\n\nFatal config error: configuration key grpc_listen_addr was removed in 0.30 ($DIR/config.yaml:35:1)\n hint: remove grpc_listen_addr; see the CHANGELOG for context\n\nFatal config error: configuration key randomize_client_port was removed in 0.29 ($DIR/config.yaml:387:1)\n hint: set \"randomizeClientPort\": true at the top level of the policy file, or grant the cap per node via a \"nodeAttrs\" entry\n\nFatal config error: configuration key tls_cert_path was removed in 0.30 ($DIR/config.yaml:220:1)\n hint: use tls.cert_path\n\nFatal config error: configuration key tls_key_path was removed in 0.30 ($DIR/config.yaml:221:1)\n hint: use tls.key_path\n\nFatal config error: configuration key tls_letsencrypt_cache_dir was removed in 0.30 ($DIR/config.yaml:208:1)\n hint: use tls.acme.cache_dir\n\nFatal config error: configuration key tls_letsencrypt_challenge_type was removed in 0.30 ($DIR/config.yaml:213:1)\n hint: use tls.acme.challenge_type\n\nFatal config error: configuration key tls_letsencrypt_hostname was removed in 0.30 ($DIR/config.yaml:203:1)\n hint: use tls.acme.hostname\n\nFatal config error: configuration key tls_letsencrypt_listen was removed in 0.30 ($DIR/config.yaml:217:1)\n hint: use tls.acme.http_listen\n" } diff --git a/hscontrol/types/testdata/compat/example-v0.24.0.golden.json b/hscontrol/types/testdata/compat/example-v0.24.0.golden.json index 5d73f533e..78cb67df6 100644 --- a/hscontrol/types/testdata/compat/example-v0.24.0.golden.json +++ b/hscontrol/types/testdata/compat/example-v0.24.0.golden.json @@ -1,7 +1,3 @@ { - "warnings": [ - "configuration key \"ephemeral_node_inactivity_timeout\" is deprecated; use \"node.ephemeral.inactivity_timeout\" instead", - "configuration key \"database.gorm.prepare_stmt\" is ignored" - ], - "error": "Fatal config error: configuration key randomize_client_port has been removed\n current: randomize_client_port: false\n hint: Set \"randomizeClientPort\": true at the top level of your policy file (see policy.path / policy.mode), or grant the cap per-node via a \"nodeAttrs\" entry. See CHANGELOG.md (BREAKING / Configuration).\n" + "error": "Fatal config error: configuration key acme_email was removed in 0.30 ($DIR/config.yaml:205:1)\n hint: use tls.acme.email\n\nFatal config error: configuration key acme_url was removed in 0.30 ($DIR/config.yaml:202:1)\n hint: use tls.acme.url\n\nFatal config error: configuration key database.gorm.prepare_stmt was removed in 0.30 ($DIR/config.yaml:152:5)\n hint: remove database.gorm.prepare_stmt; see the CHANGELOG for context\n\nFatal config error: configuration key ephemeral_node_inactivity_timeout was removed in 0.30 ($DIR/config.yaml:138:1)\n hint: use node.ephemeral.inactivity_timeout\n\nFatal config error: configuration key grpc_allow_insecure was removed in 0.30 ($DIR/config.yaml:41:1)\n hint: remove grpc_allow_insecure; see the CHANGELOG for context\n\nFatal config error: configuration key grpc_listen_addr was removed in 0.30 ($DIR/config.yaml:35:1)\n hint: remove grpc_listen_addr; see the CHANGELOG for context\n\nFatal config error: configuration key randomize_client_port was removed in 0.29 ($DIR/config.yaml:404:1)\n hint: set \"randomizeClientPort\": true at the top level of the policy file, or grant the cap per node via a \"nodeAttrs\" entry\n\nFatal config error: configuration key tls_cert_path was removed in 0.30 ($DIR/config.yaml:225:1)\n hint: use tls.cert_path\n\nFatal config error: configuration key tls_key_path was removed in 0.30 ($DIR/config.yaml:226:1)\n hint: use tls.key_path\n\nFatal config error: configuration key tls_letsencrypt_cache_dir was removed in 0.30 ($DIR/config.yaml:213:1)\n hint: use tls.acme.cache_dir\n\nFatal config error: configuration key tls_letsencrypt_challenge_type was removed in 0.30 ($DIR/config.yaml:218:1)\n hint: use tls.acme.challenge_type\n\nFatal config error: configuration key tls_letsencrypt_hostname was removed in 0.30 ($DIR/config.yaml:208:1)\n hint: use tls.acme.hostname\n\nFatal config error: configuration key tls_letsencrypt_listen was removed in 0.30 ($DIR/config.yaml:222:1)\n hint: use tls.acme.http_listen\n" } diff --git a/hscontrol/types/testdata/compat/example-v0.25.0.golden.json b/hscontrol/types/testdata/compat/example-v0.25.0.golden.json index 5d73f533e..78cb67df6 100644 --- a/hscontrol/types/testdata/compat/example-v0.25.0.golden.json +++ b/hscontrol/types/testdata/compat/example-v0.25.0.golden.json @@ -1,7 +1,3 @@ { - "warnings": [ - "configuration key \"ephemeral_node_inactivity_timeout\" is deprecated; use \"node.ephemeral.inactivity_timeout\" instead", - "configuration key \"database.gorm.prepare_stmt\" is ignored" - ], - "error": "Fatal config error: configuration key randomize_client_port has been removed\n current: randomize_client_port: false\n hint: Set \"randomizeClientPort\": true at the top level of your policy file (see policy.path / policy.mode), or grant the cap per-node via a \"nodeAttrs\" entry. See CHANGELOG.md (BREAKING / Configuration).\n" + "error": "Fatal config error: configuration key acme_email was removed in 0.30 ($DIR/config.yaml:205:1)\n hint: use tls.acme.email\n\nFatal config error: configuration key acme_url was removed in 0.30 ($DIR/config.yaml:202:1)\n hint: use tls.acme.url\n\nFatal config error: configuration key database.gorm.prepare_stmt was removed in 0.30 ($DIR/config.yaml:152:5)\n hint: remove database.gorm.prepare_stmt; see the CHANGELOG for context\n\nFatal config error: configuration key ephemeral_node_inactivity_timeout was removed in 0.30 ($DIR/config.yaml:138:1)\n hint: use node.ephemeral.inactivity_timeout\n\nFatal config error: configuration key grpc_allow_insecure was removed in 0.30 ($DIR/config.yaml:41:1)\n hint: remove grpc_allow_insecure; see the CHANGELOG for context\n\nFatal config error: configuration key grpc_listen_addr was removed in 0.30 ($DIR/config.yaml:35:1)\n hint: remove grpc_listen_addr; see the CHANGELOG for context\n\nFatal config error: configuration key randomize_client_port was removed in 0.29 ($DIR/config.yaml:404:1)\n hint: set \"randomizeClientPort\": true at the top level of the policy file, or grant the cap per node via a \"nodeAttrs\" entry\n\nFatal config error: configuration key tls_cert_path was removed in 0.30 ($DIR/config.yaml:225:1)\n hint: use tls.cert_path\n\nFatal config error: configuration key tls_key_path was removed in 0.30 ($DIR/config.yaml:226:1)\n hint: use tls.key_path\n\nFatal config error: configuration key tls_letsencrypt_cache_dir was removed in 0.30 ($DIR/config.yaml:213:1)\n hint: use tls.acme.cache_dir\n\nFatal config error: configuration key tls_letsencrypt_challenge_type was removed in 0.30 ($DIR/config.yaml:218:1)\n hint: use tls.acme.challenge_type\n\nFatal config error: configuration key tls_letsencrypt_hostname was removed in 0.30 ($DIR/config.yaml:208:1)\n hint: use tls.acme.hostname\n\nFatal config error: configuration key tls_letsencrypt_listen was removed in 0.30 ($DIR/config.yaml:222:1)\n hint: use tls.acme.http_listen\n" } diff --git a/hscontrol/types/testdata/compat/example-v0.26.0.golden.json b/hscontrol/types/testdata/compat/example-v0.26.0.golden.json index 5d73f533e..4cf8bfdf8 100644 --- a/hscontrol/types/testdata/compat/example-v0.26.0.golden.json +++ b/hscontrol/types/testdata/compat/example-v0.26.0.golden.json @@ -1,7 +1,3 @@ { - "warnings": [ - "configuration key \"ephemeral_node_inactivity_timeout\" is deprecated; use \"node.ephemeral.inactivity_timeout\" instead", - "configuration key \"database.gorm.prepare_stmt\" is ignored" - ], - "error": "Fatal config error: configuration key randomize_client_port has been removed\n current: randomize_client_port: false\n hint: Set \"randomizeClientPort\": true at the top level of your policy file (see policy.path / policy.mode), or grant the cap per-node via a \"nodeAttrs\" entry. See CHANGELOG.md (BREAKING / Configuration).\n" + "error": "Fatal config error: configuration key acme_email was removed in 0.30 ($DIR/config.yaml:201:1)\n hint: use tls.acme.email\n\nFatal config error: configuration key acme_url was removed in 0.30 ($DIR/config.yaml:198:1)\n hint: use tls.acme.url\n\nFatal config error: configuration key database.gorm.prepare_stmt was removed in 0.30 ($DIR/config.yaml:148:5)\n hint: remove database.gorm.prepare_stmt; see the CHANGELOG for context\n\nFatal config error: configuration key ephemeral_node_inactivity_timeout was removed in 0.30 ($DIR/config.yaml:134:1)\n hint: use node.ephemeral.inactivity_timeout\n\nFatal config error: configuration key grpc_allow_insecure was removed in 0.30 ($DIR/config.yaml:39:1)\n hint: remove grpc_allow_insecure; see the CHANGELOG for context\n\nFatal config error: configuration key grpc_listen_addr was removed in 0.30 ($DIR/config.yaml:33:1)\n hint: remove grpc_listen_addr; see the CHANGELOG for context\n\nFatal config error: configuration key randomize_client_port was removed in 0.29 ($DIR/config.yaml:391:1)\n hint: set \"randomizeClientPort\": true at the top level of the policy file, or grant the cap per node via a \"nodeAttrs\" entry\n\nFatal config error: configuration key tls_cert_path was removed in 0.30 ($DIR/config.yaml:221:1)\n hint: use tls.cert_path\n\nFatal config error: configuration key tls_key_path was removed in 0.30 ($DIR/config.yaml:222:1)\n hint: use tls.key_path\n\nFatal config error: configuration key tls_letsencrypt_cache_dir was removed in 0.30 ($DIR/config.yaml:209:1)\n hint: use tls.acme.cache_dir\n\nFatal config error: configuration key tls_letsencrypt_challenge_type was removed in 0.30 ($DIR/config.yaml:214:1)\n hint: use tls.acme.challenge_type\n\nFatal config error: configuration key tls_letsencrypt_hostname was removed in 0.30 ($DIR/config.yaml:204:1)\n hint: use tls.acme.hostname\n\nFatal config error: configuration key tls_letsencrypt_listen was removed in 0.30 ($DIR/config.yaml:218:1)\n hint: use tls.acme.http_listen\n" } diff --git a/hscontrol/types/testdata/compat/example-v0.27.0.golden.json b/hscontrol/types/testdata/compat/example-v0.27.0.golden.json index 5d73f533e..baee84d9c 100644 --- a/hscontrol/types/testdata/compat/example-v0.27.0.golden.json +++ b/hscontrol/types/testdata/compat/example-v0.27.0.golden.json @@ -1,7 +1,3 @@ { - "warnings": [ - "configuration key \"ephemeral_node_inactivity_timeout\" is deprecated; use \"node.ephemeral.inactivity_timeout\" instead", - "configuration key \"database.gorm.prepare_stmt\" is ignored" - ], - "error": "Fatal config error: configuration key randomize_client_port has been removed\n current: randomize_client_port: false\n hint: Set \"randomizeClientPort\": true at the top level of your policy file (see policy.path / policy.mode), or grant the cap per-node via a \"nodeAttrs\" entry. See CHANGELOG.md (BREAKING / Configuration).\n" + "error": "Fatal config error: configuration key acme_email was removed in 0.30 ($DIR/config.yaml:206:1)\n hint: use tls.acme.email\n\nFatal config error: configuration key acme_url was removed in 0.30 ($DIR/config.yaml:203:1)\n hint: use tls.acme.url\n\nFatal config error: configuration key database.gorm.prepare_stmt was removed in 0.30 ($DIR/config.yaml:153:5)\n hint: remove database.gorm.prepare_stmt; see the CHANGELOG for context\n\nFatal config error: configuration key ephemeral_node_inactivity_timeout was removed in 0.30 ($DIR/config.yaml:139:1)\n hint: use node.ephemeral.inactivity_timeout\n\nFatal config error: configuration key grpc_allow_insecure was removed in 0.30 ($DIR/config.yaml:39:1)\n hint: remove grpc_allow_insecure; see the CHANGELOG for context\n\nFatal config error: configuration key grpc_listen_addr was removed in 0.30 ($DIR/config.yaml:33:1)\n hint: remove grpc_listen_addr; see the CHANGELOG for context\n\nFatal config error: configuration key randomize_client_port was removed in 0.29 ($DIR/config.yaml:409:1)\n hint: set \"randomizeClientPort\": true at the top level of the policy file, or grant the cap per node via a \"nodeAttrs\" entry\n\nFatal config error: configuration key tls_cert_path was removed in 0.30 ($DIR/config.yaml:226:1)\n hint: use tls.cert_path\n\nFatal config error: configuration key tls_key_path was removed in 0.30 ($DIR/config.yaml:227:1)\n hint: use tls.key_path\n\nFatal config error: configuration key tls_letsencrypt_cache_dir was removed in 0.30 ($DIR/config.yaml:214:1)\n hint: use tls.acme.cache_dir\n\nFatal config error: configuration key tls_letsencrypt_challenge_type was removed in 0.30 ($DIR/config.yaml:219:1)\n hint: use tls.acme.challenge_type\n\nFatal config error: configuration key tls_letsencrypt_hostname was removed in 0.30 ($DIR/config.yaml:209:1)\n hint: use tls.acme.hostname\n\nFatal config error: configuration key tls_letsencrypt_listen was removed in 0.30 ($DIR/config.yaml:223:1)\n hint: use tls.acme.http_listen\n" } diff --git a/hscontrol/types/testdata/compat/example-v0.28.0.golden.json b/hscontrol/types/testdata/compat/example-v0.28.0.golden.json index 5d73f533e..eb990ecdf 100644 --- a/hscontrol/types/testdata/compat/example-v0.28.0.golden.json +++ b/hscontrol/types/testdata/compat/example-v0.28.0.golden.json @@ -1,7 +1,3 @@ { - "warnings": [ - "configuration key \"ephemeral_node_inactivity_timeout\" is deprecated; use \"node.ephemeral.inactivity_timeout\" instead", - "configuration key \"database.gorm.prepare_stmt\" is ignored" - ], - "error": "Fatal config error: configuration key randomize_client_port has been removed\n current: randomize_client_port: false\n hint: Set \"randomizeClientPort\": true at the top level of your policy file (see policy.path / policy.mode), or grant the cap per-node via a \"nodeAttrs\" entry. See CHANGELOG.md (BREAKING / Configuration).\n" + "error": "Fatal config error: configuration key acme_email was removed in 0.30 ($DIR/config.yaml:207:1)\n hint: use tls.acme.email\n\nFatal config error: configuration key acme_url was removed in 0.30 ($DIR/config.yaml:204:1)\n hint: use tls.acme.url\n\nFatal config error: configuration key database.gorm.prepare_stmt was removed in 0.30 ($DIR/config.yaml:154:5)\n hint: remove database.gorm.prepare_stmt; see the CHANGELOG for context\n\nFatal config error: configuration key ephemeral_node_inactivity_timeout was removed in 0.30 ($DIR/config.yaml:140:1)\n hint: use node.ephemeral.inactivity_timeout\n\nFatal config error: configuration key grpc_allow_insecure was removed in 0.30 ($DIR/config.yaml:40:1)\n hint: remove grpc_allow_insecure; see the CHANGELOG for context\n\nFatal config error: configuration key grpc_listen_addr was removed in 0.30 ($DIR/config.yaml:34:1)\n hint: remove grpc_listen_addr; see the CHANGELOG for context\n\nFatal config error: configuration key randomize_client_port was removed in 0.29 ($DIR/config.yaml:416:1)\n hint: set \"randomizeClientPort\": true at the top level of the policy file, or grant the cap per node via a \"nodeAttrs\" entry\n\nFatal config error: configuration key tls_cert_path was removed in 0.30 ($DIR/config.yaml:227:1)\n hint: use tls.cert_path\n\nFatal config error: configuration key tls_key_path was removed in 0.30 ($DIR/config.yaml:228:1)\n hint: use tls.key_path\n\nFatal config error: configuration key tls_letsencrypt_cache_dir was removed in 0.30 ($DIR/config.yaml:215:1)\n hint: use tls.acme.cache_dir\n\nFatal config error: configuration key tls_letsencrypt_challenge_type was removed in 0.30 ($DIR/config.yaml:220:1)\n hint: use tls.acme.challenge_type\n\nFatal config error: configuration key tls_letsencrypt_hostname was removed in 0.30 ($DIR/config.yaml:210:1)\n hint: use tls.acme.hostname\n\nFatal config error: configuration key tls_letsencrypt_listen was removed in 0.30 ($DIR/config.yaml:224:1)\n hint: use tls.acme.http_listen\n" } diff --git a/hscontrol/types/testdata/compat/example-v0.29.0.golden.json b/hscontrol/types/testdata/compat/example-v0.29.0.golden.json index b69daede8..377f31f58 100644 --- a/hscontrol/types/testdata/compat/example-v0.29.0.golden.json +++ b/hscontrol/types/testdata/compat/example-v0.29.0.golden.json @@ -1,203 +1,3 @@ { - "config": { - "ServerURL": "http://127.0.0.1:8080", - "Addr": "127.0.0.1:8080", - "MetricsAddr": "127.0.0.1:9090", - "TrustedProxies": null, - "Node": { - "Expiry": 0, - "Ephemeral": { - "InactivityTimeout": 1800000000000 - }, - "Routes": { - "HA": { - "ProbeInterval": 10000000000, - "ProbeTimeout": 5000000000 - } - } - }, - "PreAuthKeys": { - "RevokedRetention": 604800000000000 - }, - "PrefixV4": "100.64.0.0/10", - "PrefixV6": "fd7a:115c:a1e0::/48", - "IPAllocation": "sequential", - "NoisePrivateKeyPath": "/var/lib/headscale/noise_private.key", - "BaseDomain": "example.com", - "Log": { - "Format": "text", - "Level": "info" - }, - "DisableUpdateCheck": false, - "Database": { - "Type": "sqlite3", - "Debug": false, - "Gorm": { - "Debug": false, - "SlowThreshold": 1000000000, - "SkipErrRecordNotFound": true, - "ParameterizedQueries": true - }, - "Sqlite": { - "Path": "/var/lib/headscale/db.sqlite", - "WriteAheadLog": true, - "WALAutoCheckPoint": 1000 - }, - "Postgres": { - "Host": "", - "Port": 0, - "Name": "", - "User": "", - "Ssl": "false", - "MaxOpenConnections": 10, - "MaxIdleConnections": 10, - "ConnMaxIdleTimeSecs": 3600 - } - }, - "DERP": { - "ServerEnabled": false, - "AutomaticallyAddEmbeddedDerpRegion": true, - "ServerRegionID": 999, - "ServerRegionCode": "headscale", - "ServerRegionName": "Headscale Embedded DERP", - "ServerPrivateKeyPath": "/var/lib/headscale/derp_server_private.key", - "ServerVerifyClients": true, - "STUNAddr": "0.0.0.0:3478", - "URLs": [ - { - "Scheme": "https", - "Opaque": "", - "User": null, - "Host": "controlplane.tailscale.com", - "Path": "/derpmap/default", - "Fragment": "", - "RawQuery": "", - "RawPath": "", - "RawFragment": "", - "ForceQuery": false, - "OmitHost": false - } - ], - "Paths": [], - "DERPMap": null, - "AutoUpdate": true, - "UpdateFrequency": 10800000000000, - "IPv4": "198.51.100.1", - "IPv6": "2001:db8::1" - }, - "TLS": { - "CertPath": "", - "KeyPath": "", - "ACME": { - "Hostname": "", - "ChallengeType": "HTTP-01", - "HTTPListen": ":http", - "URL": "https://acme-v02.api.letsencrypt.org/directory", - "Email": "", - "CacheDir": "/var/lib/headscale/cache" - } - }, - "DNSConfig": { - "MagicDNS": true, - "BaseDomain": "example.com", - "OverrideLocalDNS": true, - "Nameservers": { - "Global": [ - "1.1.1.1", - "1.0.0.1", - "2606:4700:4700::1111", - "2606:4700:4700::1001" - ], - "Split": {} - }, - "SearchDomains": [], - "ExtraRecords": [], - "ExtraRecordsPath": "", - "Certificates": { - "Enabled": false, - "Zone": "example.com" - } - }, - "TailcfgDNSConfig": { - "Resolvers": [ - { - "Addr": "1.1.1.1" - }, - { - "Addr": "1.0.0.1" - }, - { - "Addr": "2606:4700:4700::1111" - }, - { - "Addr": "2606:4700:4700::1001" - } - ], - "Domains": [ - "example.com" - ], - "Proxied": true - }, - "DNSChallenge": { - "PropagationTimeout": 120000000000, - "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" - ], - "ProviderName": "" - }, - "UnixSocket": "/var/run/headscale/headscale.sock", - "UnixSocketPermission": 504, - "OIDC": { - "OnlyStartIfOIDCIsAvailable": true, - "Issuer": "", - "ClientID": "", - "Scope": [ - "openid", - "profile", - "email" - ], - "ExtraParams": {}, - "AllowedDomains": null, - "AllowedUsers": null, - "AllowedGroups": null, - "EmailVerifiedRequired": true, - "UseExpiryFromToken": false, - "PKCE": { - "Enabled": false, - "Method": "S256" - } - }, - "LogTail": { - "Enabled": false - }, - "Taildrop": { - "Enabled": true - }, - "AutoUpdate": { - "Enabled": false - }, - "CLI": { - "Address": "", - "Timeout": 5000000000, - "Insecure": false - }, - "Policy": { - "Path": "", - "Mode": "file" - }, - "Tuning": { - "NotifierSendTimeout": 800000000, - "BatchChangeDelay": 800000000, - "NodeMapSessionBufferedChanSize": 30, - "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, - "NodeStoreBatchSize": 100, - "NodeStoreBatchTimeout": 500000000 - } - }, - "warnings": [ - "configuration key \"database.gorm.prepare_stmt\" is ignored" - ] + "error": "Fatal config error: configuration key acme_email was removed in 0.30 ($DIR/config.yaml:259:1)\n hint: use tls.acme.email\n\nFatal config error: configuration key acme_url was removed in 0.30 ($DIR/config.yaml:256:1)\n hint: use tls.acme.url\n\nFatal config error: configuration key database.gorm.prepare_stmt was removed in 0.30 ($DIR/config.yaml:206:5)\n hint: remove database.gorm.prepare_stmt; see the CHANGELOG for context\n\nFatal config error: configuration key grpc_allow_insecure was removed in 0.30 ($DIR/config.yaml:40:1)\n hint: remove grpc_allow_insecure; see the CHANGELOG for context\n\nFatal config error: configuration key grpc_listen_addr was removed in 0.30 ($DIR/config.yaml:34:1)\n hint: remove grpc_listen_addr; see the CHANGELOG for context\n\nFatal config error: configuration key tls_cert_path was removed in 0.30 ($DIR/config.yaml:279:1)\n hint: use tls.cert_path\n\nFatal config error: configuration key tls_key_path was removed in 0.30 ($DIR/config.yaml:280:1)\n hint: use tls.key_path\n\nFatal config error: configuration key tls_letsencrypt_cache_dir was removed in 0.30 ($DIR/config.yaml:267:1)\n hint: use tls.acme.cache_dir\n\nFatal config error: configuration key tls_letsencrypt_challenge_type was removed in 0.30 ($DIR/config.yaml:272:1)\n hint: use tls.acme.challenge_type\n\nFatal config error: configuration key tls_letsencrypt_hostname was removed in 0.30 ($DIR/config.yaml:262:1)\n hint: use tls.acme.hostname\n\nFatal config error: configuration key tls_letsencrypt_listen was removed in 0.30 ($DIR/config.yaml:276:1)\n hint: use tls.acme.http_listen\n" } diff --git a/hscontrol/types/testdata/compat/example-v0.29.7.golden.json b/hscontrol/types/testdata/compat/example-v0.29.7.golden.json index b69daede8..377f31f58 100644 --- a/hscontrol/types/testdata/compat/example-v0.29.7.golden.json +++ b/hscontrol/types/testdata/compat/example-v0.29.7.golden.json @@ -1,203 +1,3 @@ { - "config": { - "ServerURL": "http://127.0.0.1:8080", - "Addr": "127.0.0.1:8080", - "MetricsAddr": "127.0.0.1:9090", - "TrustedProxies": null, - "Node": { - "Expiry": 0, - "Ephemeral": { - "InactivityTimeout": 1800000000000 - }, - "Routes": { - "HA": { - "ProbeInterval": 10000000000, - "ProbeTimeout": 5000000000 - } - } - }, - "PreAuthKeys": { - "RevokedRetention": 604800000000000 - }, - "PrefixV4": "100.64.0.0/10", - "PrefixV6": "fd7a:115c:a1e0::/48", - "IPAllocation": "sequential", - "NoisePrivateKeyPath": "/var/lib/headscale/noise_private.key", - "BaseDomain": "example.com", - "Log": { - "Format": "text", - "Level": "info" - }, - "DisableUpdateCheck": false, - "Database": { - "Type": "sqlite3", - "Debug": false, - "Gorm": { - "Debug": false, - "SlowThreshold": 1000000000, - "SkipErrRecordNotFound": true, - "ParameterizedQueries": true - }, - "Sqlite": { - "Path": "/var/lib/headscale/db.sqlite", - "WriteAheadLog": true, - "WALAutoCheckPoint": 1000 - }, - "Postgres": { - "Host": "", - "Port": 0, - "Name": "", - "User": "", - "Ssl": "false", - "MaxOpenConnections": 10, - "MaxIdleConnections": 10, - "ConnMaxIdleTimeSecs": 3600 - } - }, - "DERP": { - "ServerEnabled": false, - "AutomaticallyAddEmbeddedDerpRegion": true, - "ServerRegionID": 999, - "ServerRegionCode": "headscale", - "ServerRegionName": "Headscale Embedded DERP", - "ServerPrivateKeyPath": "/var/lib/headscale/derp_server_private.key", - "ServerVerifyClients": true, - "STUNAddr": "0.0.0.0:3478", - "URLs": [ - { - "Scheme": "https", - "Opaque": "", - "User": null, - "Host": "controlplane.tailscale.com", - "Path": "/derpmap/default", - "Fragment": "", - "RawQuery": "", - "RawPath": "", - "RawFragment": "", - "ForceQuery": false, - "OmitHost": false - } - ], - "Paths": [], - "DERPMap": null, - "AutoUpdate": true, - "UpdateFrequency": 10800000000000, - "IPv4": "198.51.100.1", - "IPv6": "2001:db8::1" - }, - "TLS": { - "CertPath": "", - "KeyPath": "", - "ACME": { - "Hostname": "", - "ChallengeType": "HTTP-01", - "HTTPListen": ":http", - "URL": "https://acme-v02.api.letsencrypt.org/directory", - "Email": "", - "CacheDir": "/var/lib/headscale/cache" - } - }, - "DNSConfig": { - "MagicDNS": true, - "BaseDomain": "example.com", - "OverrideLocalDNS": true, - "Nameservers": { - "Global": [ - "1.1.1.1", - "1.0.0.1", - "2606:4700:4700::1111", - "2606:4700:4700::1001" - ], - "Split": {} - }, - "SearchDomains": [], - "ExtraRecords": [], - "ExtraRecordsPath": "", - "Certificates": { - "Enabled": false, - "Zone": "example.com" - } - }, - "TailcfgDNSConfig": { - "Resolvers": [ - { - "Addr": "1.1.1.1" - }, - { - "Addr": "1.0.0.1" - }, - { - "Addr": "2606:4700:4700::1111" - }, - { - "Addr": "2606:4700:4700::1001" - } - ], - "Domains": [ - "example.com" - ], - "Proxied": true - }, - "DNSChallenge": { - "PropagationTimeout": 120000000000, - "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" - ], - "ProviderName": "" - }, - "UnixSocket": "/var/run/headscale/headscale.sock", - "UnixSocketPermission": 504, - "OIDC": { - "OnlyStartIfOIDCIsAvailable": true, - "Issuer": "", - "ClientID": "", - "Scope": [ - "openid", - "profile", - "email" - ], - "ExtraParams": {}, - "AllowedDomains": null, - "AllowedUsers": null, - "AllowedGroups": null, - "EmailVerifiedRequired": true, - "UseExpiryFromToken": false, - "PKCE": { - "Enabled": false, - "Method": "S256" - } - }, - "LogTail": { - "Enabled": false - }, - "Taildrop": { - "Enabled": true - }, - "AutoUpdate": { - "Enabled": false - }, - "CLI": { - "Address": "", - "Timeout": 5000000000, - "Insecure": false - }, - "Policy": { - "Path": "", - "Mode": "file" - }, - "Tuning": { - "NotifierSendTimeout": 800000000, - "BatchChangeDelay": 800000000, - "NodeMapSessionBufferedChanSize": 30, - "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, - "NodeStoreBatchSize": 100, - "NodeStoreBatchTimeout": 500000000 - } - }, - "warnings": [ - "configuration key \"database.gorm.prepare_stmt\" is ignored" - ] + "error": "Fatal config error: configuration key acme_email was removed in 0.30 ($DIR/config.yaml:259:1)\n hint: use tls.acme.email\n\nFatal config error: configuration key acme_url was removed in 0.30 ($DIR/config.yaml:256:1)\n hint: use tls.acme.url\n\nFatal config error: configuration key database.gorm.prepare_stmt was removed in 0.30 ($DIR/config.yaml:206:5)\n hint: remove database.gorm.prepare_stmt; see the CHANGELOG for context\n\nFatal config error: configuration key grpc_allow_insecure was removed in 0.30 ($DIR/config.yaml:40:1)\n hint: remove grpc_allow_insecure; see the CHANGELOG for context\n\nFatal config error: configuration key grpc_listen_addr was removed in 0.30 ($DIR/config.yaml:34:1)\n hint: remove grpc_listen_addr; see the CHANGELOG for context\n\nFatal config error: configuration key tls_cert_path was removed in 0.30 ($DIR/config.yaml:279:1)\n hint: use tls.cert_path\n\nFatal config error: configuration key tls_key_path was removed in 0.30 ($DIR/config.yaml:280:1)\n hint: use tls.key_path\n\nFatal config error: configuration key tls_letsencrypt_cache_dir was removed in 0.30 ($DIR/config.yaml:267:1)\n hint: use tls.acme.cache_dir\n\nFatal config error: configuration key tls_letsencrypt_challenge_type was removed in 0.30 ($DIR/config.yaml:272:1)\n hint: use tls.acme.challenge_type\n\nFatal config error: configuration key tls_letsencrypt_hostname was removed in 0.30 ($DIR/config.yaml:262:1)\n hint: use tls.acme.hostname\n\nFatal config error: configuration key tls_letsencrypt_listen was removed in 0.30 ($DIR/config.yaml:276:1)\n hint: use tls.acme.http_listen\n" } diff --git a/hscontrol/types/testdata/compat/file-everything-strict.env b/hscontrol/types/testdata/compat/file-everything-strict.env new file mode 100644 index 000000000..e664c7012 --- /dev/null +++ b/hscontrol/types/testdata/compat/file-everything-strict.env @@ -0,0 +1 @@ +COMPAT_SECRETS=testdata/compat diff --git a/hscontrol/types/testdata/compat/file-everything-strict.golden.json b/hscontrol/types/testdata/compat/file-everything-strict.golden.json new file mode 100644 index 000000000..1de6e169c --- /dev/null +++ b/hscontrol/types/testdata/compat/file-everything-strict.golden.json @@ -0,0 +1,252 @@ +{ + "config": { + "ServerURL": "https://headscale.example.com", + "Addr": "0.0.0.0:8443", + "MetricsAddr": "127.0.0.1:9091", + "TrustedProxies": [ + "10.0.0.0/8", + "192.168.0.0/16" + ], + "Node": { + "Expiry": 7776000000000000, + "Ephemeral": { + "InactivityTimeout": 2700000000000 + }, + "Routes": { + "HA": { + "ProbeInterval": 20000000000, + "ProbeTimeout": 7000000000 + } + } + }, + "PreAuthKeys": { + "RevokedRetention": 259200000000000 + }, + "PrefixV4": "100.64.0.0/10", + "PrefixV6": "fd7a:115c:a1e0::/48", + "IPAllocation": "random", + "NoisePrivateKeyPath": "$DIR/noise_private.key", + "BaseDomain": "tailnet.example.net", + "Log": { + "Format": "json", + "Level": "debug" + }, + "DisableUpdateCheck": true, + "Database": { + "Type": "postgres", + "Debug": true, + "Gorm": { + "Debug": true, + "SlowThreshold": 2000000000, + "SkipErrRecordNotFound": false, + "ParameterizedQueries": true + }, + "Sqlite": { + "Path": "", + "WriteAheadLog": true, + "WALAutoCheckPoint": 1000 + }, + "Postgres": { + "Host": "db.example.com", + "Port": 5433, + "Name": "headscale", + "User": "headscale", + "Ssl": "true", + "MaxOpenConnections": 20, + "MaxIdleConnections": 5, + "ConnMaxIdleTimeSecs": 1800 + } + }, + "DERP": { + "ServerEnabled": true, + "AutomaticallyAddEmbeddedDerpRegion": false, + "ServerRegionID": 900, + "ServerRegionCode": "hs", + "ServerRegionName": "Headscale Embedded DERP", + "ServerPrivateKeyPath": "$DIR/derp_server_private.key", + "ServerVerifyClients": false, + "STUNAddr": "0.0.0.0:3479", + "URLs": [ + { + "Scheme": "https", + "Opaque": "", + "User": null, + "Host": "controlplane.tailscale.com", + "Path": "/derpmap/default", + "Fragment": "", + "RawQuery": "", + "RawPath": "", + "RawFragment": "", + "ForceQuery": false, + "OmitHost": false + }, + { + "Scheme": "https", + "Opaque": "", + "User": null, + "Host": "derp.example.com", + "Path": "/map.json", + "Fragment": "", + "RawQuery": "", + "RawPath": "", + "RawFragment": "", + "ForceQuery": false, + "OmitHost": false + } + ], + "Paths": [ + "/etc/headscale/derp.yaml" + ], + "DERPMap": null, + "AutoUpdate": false, + "UpdateFrequency": 3600000000000, + "IPv4": "198.51.100.1", + "IPv6": "2001:db8::1" + }, + "TLS": { + "CertPath": "", + "KeyPath": "", + "ACME": { + "Hostname": "headscale.example.com", + "ChallengeType": "TLS-ALPN-01", + "HTTPListen": ":8081", + "URL": "https://acme-staging-v02.api.letsencrypt.org/directory", + "Email": "admin@example.com", + "CacheDir": "$DIR/acme-cache" + } + }, + "DNSConfig": { + "MagicDNS": true, + "BaseDomain": "tailnet.example.net", + "OverrideLocalDNS": true, + "Nameservers": { + "Global": [ + "1.1.1.1", + "https://dns.nextdns.io/abc123" + ], + "Split": { + "corp.example.com": [ + "10.0.0.1" + ] + } + }, + "SearchDomains": [ + "corp.example.com" + ], + "ExtraRecords": [ + { + "Name": "grafana.tailnet.example.net", + "Type": "A", + "Value": "100.64.0.3" + } + ], + "ExtraRecordsPath": "", + "Certificates": { + "Enabled": false, + "Zone": "tailnet.example.net" + } + }, + "TailcfgDNSConfig": { + "Resolvers": [ + { + "Addr": "1.1.1.1" + }, + { + "Addr": "https://dns.nextdns.io/abc123" + } + ], + "Routes": { + "corp.example.com": [ + { + "Addr": "10.0.0.1" + } + ] + }, + "Domains": [ + "tailnet.example.net", + "corp.example.com" + ], + "Proxied": true, + "ExtraRecords": [ + { + "Name": "grafana.tailnet.example.net", + "Type": "A", + "Value": "100.64.0.3" + } + ] + }, + "DNSChallenge": { + "PropagationTimeout": 180000000000, + "Resolvers": [ + "9.9.9.9:53" + ], + "ProviderName": "" + }, + "UnixSocket": "/run/headscale/headscale.sock", + "UnixSocketPermission": 448, + "OIDC": { + "OnlyStartIfOIDCIsAvailable": false, + "Issuer": "https://idp.example.com", + "ClientID": "headscale", + "Scope": [ + "openid", + "profile", + "email", + "groups" + ], + "ExtraParams": { + "login_hint": "file" + }, + "AllowedDomains": [ + "example.com" + ], + "AllowedUsers": [ + "alice@example.com" + ], + "AllowedGroups": [ + "/headscale" + ], + "EmailVerifiedRequired": false, + "UseExpiryFromToken": true, + "PKCE": { + "Enabled": true, + "Method": "plain" + } + }, + "LogTail": { + "Enabled": true + }, + "Taildrop": { + "Enabled": false + }, + "AutoUpdate": { + "Enabled": true + }, + "CLI": { + "Address": "headscale.example.com:443", + "Timeout": 15000000000, + "Insecure": true + }, + "Policy": { + "Path": "", + "Mode": "database" + }, + "Tuning": { + "NotifierSendTimeout": 1000000000, + "BatchChangeDelay": 500000000, + "NodeMapSessionBufferedChanSize": 50, + "BatcherWorkers": 4096, + "RegisterCacheExpiration": 1200000000000, + "RegisterCacheMaxEntries": 2000, + "NodeStoreBatchSize": 50, + "NodeStoreBatchTimeout": 250000000 + } + }, + "secrets": { + "database.postgres.pass": "marker-postgres-pass", + "oidc.client_secret": "marker-oidc-secret-from-file" + }, + "warnings": [ + "Warning: when using tls.acme.hostname with TLS-ALPN-01 as challenge type, headscale must be reachable on port 443, i.e. listen_addr should probably end in :443" + ] +} diff --git a/hscontrol/types/testdata/compat/file-everything-strict.yaml b/hscontrol/types/testdata/compat/file-everything-strict.yaml new file mode 100644 index 000000000..7910e3881 --- /dev/null +++ b/hscontrol/types/testdata/compat/file-everything-strict.yaml @@ -0,0 +1,143 @@ +# Every key the loader reads, set to a valid non-default value. +server_url: https://headscale.example.com +listen_addr: 0.0.0.0:8443 +metrics_listen_addr: 127.0.0.1:9091 +trusted_proxies: [10.0.0.0/8, 192.168.0.0/16] +disable_check_updates: true +unix_socket: /run/headscale/headscale.sock +unix_socket_permission: "0700" + +noise: + private_key_path: ./noise_private.key + +prefixes: + v4: 100.64.0.0/10 + v6: fd7a:115c:a1e0::/48 + allocation: random + +derp: + server: + enabled: true + region_id: 900 + region_code: hs + region_name: Headscale Embedded DERP + verify_clients: false + stun_listen_addr: 0.0.0.0:3479 + private_key_path: ./derp_server_private.key + automatically_add_embedded_derp_region: false + ipv4: 198.51.100.1 + ipv6: 2001:db8::1 + urls: + - https://controlplane.tailscale.com/derpmap/default + - https://derp.example.com/map.json + paths: + - /etc/headscale/derp.yaml + auto_update_enabled: false + update_frequency: 1h + +node: + expiry: 90d + ephemeral: + inactivity_timeout: 45m + routes: + ha: + probe_interval: 20s + probe_timeout: 7s + +preauth_keys: + revoked_retention: 72h + +database: + type: postgres + debug: true + gorm: + parameterized_queries: true + skip_err_record_not_found: false + slow_threshold: 2000 + postgres: + host: db.example.com + port: 5433 + name: headscale + user: headscale + pass: marker-postgres-pass + max_open_conns: 20 + max_idle_conns: 5 + conn_max_idle_time_secs: 1800 + ssl: true + +tls: + acme: + hostname: headscale.example.com + challenge_type: TLS-ALPN-01 + http_listen: :8081 + url: https://acme-staging-v02.api.letsencrypt.org/directory + email: admin@example.com + cache_dir: ./acme-cache + +log: + format: json + level: debug + +policy: + mode: database + +dns: + magic_dns: true + base_domain: tailnet.example.net + override_local_dns: true + nameservers: + global: + - 1.1.1.1 + - https://dns.nextdns.io/abc123 + split: + Corp.Example.COM: + - 10.0.0.1 + search_domains: [corp.example.com] + extra_records: + - name: grafana.tailnet.example.net + type: A + value: 100.64.0.3 + challenge: + propagation_timeout: 3m + resolvers: [9.9.9.9] + +oidc: + only_start_if_oidc_is_available: false + issuer: https://idp.example.com + client_id: headscale + # os.ExpandEnv, then read relative to the test's working directory. + client_secret: file:${COMPAT_SECRETS}/oidc-secret.txt + scope: [openid, profile, email, groups] + extra_params: + Login_Hint: file + allowed_domains: [example.com] + allowed_users: [alice@example.com] + allowed_groups: [/headscale] + email_verified_required: false + use_expiry_from_token: true + pkce: + enabled: true + method: plain + +logtail: + enabled: true +taildrop: + enabled: false +auto_update: + enabled: true + +cli: + address: headscale.example.com:443 + api_key: marker-cli-api-key + timeout: 15s + insecure: true + +tuning: + notifier_send_timeout: 1s + batch_change_delay: 500ms + node_mapsession_buffered_chan_size: 50 + batcher_workers: 4096 + register_cache_expiration: 20m + register_cache_max_entries: 2000 + node_store_batch_size: 50 + node_store_batch_timeout: 250ms diff --git a/hscontrol/types/testdata/compat/file-everything.golden.json b/hscontrol/types/testdata/compat/file-everything.golden.json index c535af103..9ffdba269 100644 --- a/hscontrol/types/testdata/compat/file-everything.golden.json +++ b/hscontrol/types/testdata/compat/file-everything.golden.json @@ -1,253 +1,3 @@ { - "config": { - "ServerURL": "https://headscale.example.com", - "Addr": "0.0.0.0:8443", - "MetricsAddr": "127.0.0.1:9091", - "TrustedProxies": [ - "10.0.0.0/8", - "192.168.0.0/16" - ], - "Node": { - "Expiry": 7776000000000000, - "Ephemeral": { - "InactivityTimeout": 2700000000000 - }, - "Routes": { - "HA": { - "ProbeInterval": 20000000000, - "ProbeTimeout": 7000000000 - } - } - }, - "PreAuthKeys": { - "RevokedRetention": 259200000000000 - }, - "PrefixV4": "100.64.0.0/10", - "PrefixV6": "fd7a:115c:a1e0::/48", - "IPAllocation": "random", - "NoisePrivateKeyPath": "$DIR/noise_private.key", - "BaseDomain": "tailnet.example.net", - "Log": { - "Format": "json", - "Level": "debug" - }, - "DisableUpdateCheck": false, - "Database": { - "Type": "postgres", - "Debug": true, - "Gorm": { - "Debug": true, - "SlowThreshold": 2000000000, - "SkipErrRecordNotFound": false, - "ParameterizedQueries": true - }, - "Sqlite": { - "Path": "", - "WriteAheadLog": true, - "WALAutoCheckPoint": 1000 - }, - "Postgres": { - "Host": "db.example.com", - "Port": 5433, - "Name": "headscale", - "User": "headscale", - "Ssl": "true", - "MaxOpenConnections": 20, - "MaxIdleConnections": 5, - "ConnMaxIdleTimeSecs": 1800 - } - }, - "DERP": { - "ServerEnabled": true, - "AutomaticallyAddEmbeddedDerpRegion": false, - "ServerRegionID": 900, - "ServerRegionCode": "hs", - "ServerRegionName": "Headscale Embedded DERP", - "ServerPrivateKeyPath": "$DIR/derp_server_private.key", - "ServerVerifyClients": false, - "STUNAddr": "0.0.0.0:3479", - "URLs": [ - { - "Scheme": "https", - "Opaque": "", - "User": null, - "Host": "controlplane.tailscale.com", - "Path": "/derpmap/default", - "Fragment": "", - "RawQuery": "", - "RawPath": "", - "RawFragment": "", - "ForceQuery": false, - "OmitHost": false - }, - { - "Scheme": "https", - "Opaque": "", - "User": null, - "Host": "derp.example.com", - "Path": "/map.json", - "Fragment": "", - "RawQuery": "", - "RawPath": "", - "RawFragment": "", - "ForceQuery": false, - "OmitHost": false - } - ], - "Paths": [ - "/etc/headscale/derp.yaml" - ], - "DERPMap": null, - "AutoUpdate": false, - "UpdateFrequency": 3600000000000, - "IPv4": "198.51.100.1", - "IPv6": "2001:db8::1" - }, - "TLS": { - "CertPath": "", - "KeyPath": "", - "ACME": { - "Hostname": "headscale.example.com", - "ChallengeType": "TLS-ALPN-01", - "HTTPListen": ":8081", - "URL": "https://acme-staging-v02.api.letsencrypt.org/directory", - "Email": "admin@example.com", - "CacheDir": "$DIR/acme-cache" - } - }, - "DNSConfig": { - "MagicDNS": true, - "BaseDomain": "tailnet.example.net", - "OverrideLocalDNS": true, - "Nameservers": { - "Global": [ - "1.1.1.1", - "https://dns.nextdns.io/abc123" - ], - "Split": { - "corp.example.com": [ - "10.0.0.1" - ] - } - }, - "SearchDomains": [ - "corp.example.com" - ], - "ExtraRecords": [ - { - "Name": "grafana.tailnet.example.net", - "Type": "A", - "Value": "100.64.0.3" - } - ], - "ExtraRecordsPath": "", - "Certificates": { - "Enabled": false, - "Zone": "tailnet.example.net" - } - }, - "TailcfgDNSConfig": { - "Resolvers": [ - { - "Addr": "1.1.1.1" - }, - { - "Addr": "https://dns.nextdns.io/abc123" - } - ], - "Routes": { - "corp.example.com": [ - { - "Addr": "10.0.0.1" - } - ] - }, - "Domains": [ - "tailnet.example.net", - "corp.example.com" - ], - "Proxied": true, - "ExtraRecords": [ - { - "Name": "grafana.tailnet.example.net", - "Type": "A", - "Value": "100.64.0.3" - } - ] - }, - "DNSChallenge": { - "PropagationTimeout": 180000000000, - "Resolvers": [ - "9.9.9.9:53" - ], - "ProviderName": "" - }, - "UnixSocket": "/run/headscale/headscale.sock", - "UnixSocketPermission": 448, - "OIDC": { - "OnlyStartIfOIDCIsAvailable": false, - "Issuer": "https://idp.example.com", - "ClientID": "headscale", - "Scope": [ - "openid", - "profile", - "email", - "groups" - ], - "ExtraParams": { - "login_hint": "file" - }, - "AllowedDomains": [ - "example.com" - ], - "AllowedUsers": [ - "alice@example.com" - ], - "AllowedGroups": [ - "/headscale" - ], - "EmailVerifiedRequired": false, - "UseExpiryFromToken": true, - "PKCE": { - "Enabled": true, - "Method": "plain" - } - }, - "LogTail": { - "Enabled": true - }, - "Taildrop": { - "Enabled": false - }, - "AutoUpdate": { - "Enabled": true - }, - "CLI": { - "Address": "headscale.example.com:443", - "Timeout": 15000000000, - "Insecure": true - }, - "Policy": { - "Path": "", - "Mode": "database" - }, - "Tuning": { - "NotifierSendTimeout": 1000000000, - "BatchChangeDelay": 500000000, - "NodeMapSessionBufferedChanSize": 50, - "BatcherWorkers": 4096, - "RegisterCacheExpiration": 1200000000000, - "RegisterCacheMaxEntries": 2000, - "NodeStoreBatchSize": 50, - "NodeStoreBatchTimeout": 250000000 - } - }, - "secrets": { - "cli.api_key": "marker-cli-api-key", - "database.postgres.pass": "marker-postgres-pass", - "oidc.client_secret": "marker-oidc-secret-from-file" - }, - "warnings": [ - "Warning: when using tls.acme.hostname with TLS-ALPN-01 as challenge type, headscale must be reachable on port 443, i.e. listen_addr should probably end in :443" - ] + "error": "Fatal config error: configuration key oidc.client_secret_path was removed in 0.30 ($DIR/config.yaml:109:3)\n hint: use oidc.client_secret: \"file:\u003cpath\u003e\"\n" } diff --git a/hscontrol/types/testdata/compat/hsic-default-strict.env b/hscontrol/types/testdata/compat/hsic-default-strict.env new file mode 100644 index 000000000..6675f7eef --- /dev/null +++ b/hscontrol/types/testdata/compat/hsic-default-strict.env @@ -0,0 +1,30 @@ +HEADSCALE_LOG_LEVEL=trace +HEADSCALE_POLICY_PATH= +HEADSCALE_DATABASE_TYPE=sqlite +HEADSCALE_DATABASE_SQLITE_PATH=/tmp/integration_test_db.sqlite3 +HEADSCALE_DATABASE_DEBUG=0 +HEADSCALE_DATABASE_GORM_SLOW_THRESHOLD=1 +HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT=30m +HEADSCALE_PREFIXES_V4=100.64.0.0/10 +HEADSCALE_PREFIXES_V6=fd7a:115c:a1e0::/48 +HEADSCALE_DNS_BASE_DOMAIN=headscale.net +HEADSCALE_DNS_MAGIC_DNS=true +HEADSCALE_DNS_OVERRIDE_LOCAL_DNS=false +HEADSCALE_DNS_NAMESERVERS_GLOBAL=127.0.0.11 1.1.1.1 +HEADSCALE_NOISE_PRIVATE_KEY_PATH=/tmp/noise_private.key +HEADSCALE_METRICS_LISTEN_ADDR=0.0.0.0:9090 +HEADSCALE_DERP_URLS= +HEADSCALE_DERP_AUTO_UPDATE_ENABLED=false +HEADSCALE_DERP_UPDATE_FREQUENCY=1m +HEADSCALE_DERP_SERVER_ENABLED=true +HEADSCALE_DERP_SERVER_REGION_ID=999 +HEADSCALE_DERP_SERVER_REGION_CODE=headscale +HEADSCALE_DERP_SERVER_REGION_NAME=Headscale Embedded DERP +HEADSCALE_DERP_SERVER_STUN_LISTEN_ADDR=0.0.0.0:3478 +HEADSCALE_DERP_SERVER_PRIVATE_KEY_PATH=/tmp/derp.key +DERP_DEBUG_LOGS=true +DERP_PROBER_DEBUG_LOGS=true +HEADSCALE_PREFIXES_ALLOCATION=sequential +# hsic.go sets these per container, outside DefaultConfigEnv. +HEADSCALE_SERVER_URL=http://hs-compat:8080 +HEADSCALE_LISTEN_ADDR=0.0.0.0:8080 diff --git a/hscontrol/types/testdata/compat/hsic-default-strict.golden.json b/hscontrol/types/testdata/compat/hsic-default-strict.golden.json new file mode 100644 index 000000000..ce79bf7e9 --- /dev/null +++ b/hscontrol/types/testdata/compat/hsic-default-strict.golden.json @@ -0,0 +1,178 @@ +{ + "config": { + "ServerURL": "http://hs-compat:8080", + "Addr": "0.0.0.0:8080", + "MetricsAddr": "0.0.0.0:9090", + "TrustedProxies": null, + "Node": { + "Expiry": 0, + "Ephemeral": { + "InactivityTimeout": 1800000000000 + }, + "Routes": { + "HA": { + "ProbeInterval": 10000000000, + "ProbeTimeout": 5000000000 + } + } + }, + "PreAuthKeys": { + "RevokedRetention": 604800000000000 + }, + "PrefixV4": "100.64.0.0/10", + "PrefixV6": "fd7a:115c:a1e0::/48", + "IPAllocation": "sequential", + "NoisePrivateKeyPath": "/tmp/noise_private.key", + "BaseDomain": "headscale.net", + "Log": { + "Format": "text", + "Level": "trace" + }, + "DisableUpdateCheck": false, + "Database": { + "Type": "sqlite3", + "Debug": false, + "Gorm": { + "Debug": false, + "SlowThreshold": 1000000, + "SkipErrRecordNotFound": false, + "ParameterizedQueries": false + }, + "Sqlite": { + "Path": "/tmp/integration_test_db.sqlite3", + "WriteAheadLog": true, + "WALAutoCheckPoint": 1000 + }, + "Postgres": { + "Host": "", + "Port": 0, + "Name": "", + "User": "", + "Ssl": "false", + "MaxOpenConnections": 10, + "MaxIdleConnections": 10, + "ConnMaxIdleTimeSecs": 3600 + } + }, + "DERP": { + "ServerEnabled": true, + "AutomaticallyAddEmbeddedDerpRegion": true, + "ServerRegionID": 999, + "ServerRegionCode": "headscale", + "ServerRegionName": "Headscale Embedded DERP", + "ServerPrivateKeyPath": "/tmp/derp.key", + "ServerVerifyClients": true, + "STUNAddr": "0.0.0.0:3478", + "URLs": [], + "Paths": null, + "DERPMap": null, + "AutoUpdate": false, + "UpdateFrequency": 60000000000, + "IPv4": "", + "IPv6": "" + }, + "TLS": { + "CertPath": "", + "KeyPath": "", + "ACME": { + "Hostname": "", + "ChallengeType": "HTTP-01", + "HTTPListen": ":http", + "URL": "", + "Email": "", + "CacheDir": "/var/lib/headscale/cache" + } + }, + "DNSConfig": { + "MagicDNS": true, + "BaseDomain": "headscale.net", + "OverrideLocalDNS": false, + "Nameservers": { + "Global": [ + "127.0.0.11", + "1.1.1.1" + ], + "Split": {} + }, + "SearchDomains": [], + "ExtraRecords": null, + "ExtraRecordsPath": "", + "Certificates": { + "Enabled": false, + "Zone": "headscale.net" + } + }, + "TailcfgDNSConfig": { + "FallbackResolvers": [ + { + "Addr": "127.0.0.11" + }, + { + "Addr": "1.1.1.1" + } + ], + "Domains": [ + "headscale.net" + ], + "Proxied": true + }, + "DNSChallenge": { + "PropagationTimeout": 120000000000, + "Resolvers": [ + "1.1.1.1:53", + "8.8.8.8:53" + ], + "ProviderName": "" + }, + "UnixSocket": "/var/run/headscale/headscale.sock", + "UnixSocketPermission": 504, + "OIDC": { + "OnlyStartIfOIDCIsAvailable": true, + "Issuer": "", + "ClientID": "", + "Scope": [ + "openid", + "profile", + "email" + ], + "ExtraParams": {}, + "AllowedDomains": null, + "AllowedUsers": null, + "AllowedGroups": null, + "EmailVerifiedRequired": true, + "UseExpiryFromToken": false, + "PKCE": { + "Enabled": false, + "Method": "S256" + } + }, + "LogTail": { + "Enabled": false + }, + "Taildrop": { + "Enabled": true + }, + "AutoUpdate": { + "Enabled": false + }, + "CLI": { + "Address": "", + "Timeout": 5000000000, + "Insecure": false + }, + "Policy": { + "Path": "", + "Mode": "file" + }, + "Tuning": { + "NotifierSendTimeout": 800000000, + "BatchChangeDelay": 800000000, + "NodeMapSessionBufferedChanSize": 30, + "BatcherWorkers": 0, + "RegisterCacheExpiration": 0, + "RegisterCacheMaxEntries": 0, + "NodeStoreBatchSize": 100, + "NodeStoreBatchTimeout": 500000000 + } + } +} diff --git a/hscontrol/types/testdata/compat/hsic-default-strict.yaml b/hscontrol/types/testdata/compat/hsic-default-strict.yaml new file mode 100644 index 000000000..74a9b6b88 --- /dev/null +++ b/hscontrol/types/testdata/compat/hsic-default-strict.yaml @@ -0,0 +1,2 @@ +noise: + private_key_path: /tmp/noise_private.key diff --git a/hscontrol/types/testdata/compat/hsic-default.golden.json b/hscontrol/types/testdata/compat/hsic-default.golden.json index 13da97338..a77eedc4f 100644 --- a/hscontrol/types/testdata/compat/hsic-default.golden.json +++ b/hscontrol/types/testdata/compat/hsic-default.golden.json @@ -1,181 +1,3 @@ { - "config": { - "ServerURL": "http://hs-compat:8080", - "Addr": "0.0.0.0:8080", - "MetricsAddr": "0.0.0.0:9090", - "TrustedProxies": null, - "Node": { - "Expiry": 0, - "Ephemeral": { - "InactivityTimeout": 120000000000 - }, - "Routes": { - "HA": { - "ProbeInterval": 10000000000, - "ProbeTimeout": 5000000000 - } - } - }, - "PreAuthKeys": { - "RevokedRetention": 604800000000000 - }, - "PrefixV4": "100.64.0.0/10", - "PrefixV6": "fd7a:115c:a1e0::/48", - "IPAllocation": "sequential", - "NoisePrivateKeyPath": "/tmp/noise_private.key", - "BaseDomain": "headscale.net", - "Log": { - "Format": "text", - "Level": "trace" - }, - "DisableUpdateCheck": false, - "Database": { - "Type": "sqlite3", - "Debug": false, - "Gorm": { - "Debug": false, - "SlowThreshold": 1000000, - "SkipErrRecordNotFound": false, - "ParameterizedQueries": false - }, - "Sqlite": { - "Path": "/tmp/integration_test_db.sqlite3", - "WriteAheadLog": true, - "WALAutoCheckPoint": 1000 - }, - "Postgres": { - "Host": "", - "Port": 0, - "Name": "", - "User": "", - "Ssl": "false", - "MaxOpenConnections": 10, - "MaxIdleConnections": 10, - "ConnMaxIdleTimeSecs": 3600 - } - }, - "DERP": { - "ServerEnabled": true, - "AutomaticallyAddEmbeddedDerpRegion": true, - "ServerRegionID": 999, - "ServerRegionCode": "headscale", - "ServerRegionName": "Headscale Embedded DERP", - "ServerPrivateKeyPath": "/tmp/derp.key", - "ServerVerifyClients": true, - "STUNAddr": "0.0.0.0:3478", - "URLs": [], - "Paths": null, - "DERPMap": null, - "AutoUpdate": false, - "UpdateFrequency": 60000000000, - "IPv4": "", - "IPv6": "" - }, - "TLS": { - "CertPath": "", - "KeyPath": "", - "ACME": { - "Hostname": "", - "ChallengeType": "HTTP-01", - "HTTPListen": ":http", - "URL": "", - "Email": "", - "CacheDir": "/var/lib/headscale/cache" - } - }, - "DNSConfig": { - "MagicDNS": true, - "BaseDomain": "headscale.net", - "OverrideLocalDNS": false, - "Nameservers": { - "Global": [ - "127.0.0.11", - "1.1.1.1" - ], - "Split": {} - }, - "SearchDomains": [], - "ExtraRecords": null, - "ExtraRecordsPath": "", - "Certificates": { - "Enabled": false, - "Zone": "headscale.net" - } - }, - "TailcfgDNSConfig": { - "FallbackResolvers": [ - { - "Addr": "127.0.0.11" - }, - { - "Addr": "1.1.1.1" - } - ], - "Domains": [ - "headscale.net" - ], - "Proxied": true - }, - "DNSChallenge": { - "PropagationTimeout": 120000000000, - "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" - ], - "ProviderName": "" - }, - "UnixSocket": "/var/run/headscale/headscale.sock", - "UnixSocketPermission": 504, - "OIDC": { - "OnlyStartIfOIDCIsAvailable": true, - "Issuer": "", - "ClientID": "", - "Scope": [ - "openid", - "profile", - "email" - ], - "ExtraParams": {}, - "AllowedDomains": null, - "AllowedUsers": null, - "AllowedGroups": null, - "EmailVerifiedRequired": true, - "UseExpiryFromToken": false, - "PKCE": { - "Enabled": false, - "Method": "S256" - } - }, - "LogTail": { - "Enabled": false - }, - "Taildrop": { - "Enabled": true - }, - "AutoUpdate": { - "Enabled": false - }, - "CLI": { - "Address": "", - "Timeout": 5000000000, - "Insecure": false - }, - "Policy": { - "Path": "", - "Mode": "file" - }, - "Tuning": { - "NotifierSendTimeout": 800000000, - "BatchChangeDelay": 800000000, - "NodeMapSessionBufferedChanSize": 30, - "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, - "NodeStoreBatchSize": 100, - "NodeStoreBatchTimeout": 500000000 - } - }, - "warnings": [ - "configuration key \"ephemeral_node_inactivity_timeout\" is deprecated; use \"node.ephemeral.inactivity_timeout\" instead" - ] + "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-strict.env b/hscontrol/types/testdata/compat/hsic-postgres-strict.env new file mode 100644 index 000000000..4b2577c67 --- /dev/null +++ b/hscontrol/types/testdata/compat/hsic-postgres-strict.env @@ -0,0 +1,34 @@ +HEADSCALE_LOG_LEVEL=trace +HEADSCALE_POLICY_PATH= +HEADSCALE_DATABASE_DEBUG=0 +HEADSCALE_DATABASE_GORM_SLOW_THRESHOLD=1 +HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT=30m +HEADSCALE_PREFIXES_V4=100.64.0.0/10 +HEADSCALE_PREFIXES_V6=fd7a:115c:a1e0::/48 +HEADSCALE_DNS_BASE_DOMAIN=headscale.net +HEADSCALE_DNS_MAGIC_DNS=true +HEADSCALE_DNS_OVERRIDE_LOCAL_DNS=false +HEADSCALE_DNS_NAMESERVERS_GLOBAL=127.0.0.11 1.1.1.1 +HEADSCALE_NOISE_PRIVATE_KEY_PATH=/tmp/noise_private.key +HEADSCALE_METRICS_LISTEN_ADDR=0.0.0.0:9090 +HEADSCALE_DERP_URLS= +HEADSCALE_DERP_AUTO_UPDATE_ENABLED=false +HEADSCALE_DERP_UPDATE_FREQUENCY=1m +HEADSCALE_DERP_SERVER_ENABLED=true +HEADSCALE_DERP_SERVER_REGION_ID=999 +HEADSCALE_DERP_SERVER_REGION_CODE=headscale +HEADSCALE_DERP_SERVER_REGION_NAME=Headscale Embedded DERP +HEADSCALE_DERP_SERVER_STUN_LISTEN_ADDR=0.0.0.0:3478 +HEADSCALE_DERP_SERVER_PRIVATE_KEY_PATH=/tmp/derp.key +DERP_DEBUG_LOGS=true +DERP_PROBER_DEBUG_LOGS=true +HEADSCALE_PREFIXES_ALLOCATION=sequential +# hsic.go sets these per container, outside DefaultConfigEnv. +HEADSCALE_SERVER_URL=http://hs-compat:8080 +HEADSCALE_LISTEN_ADDR=0.0.0.0:8080 +# hsic.WithPostgres overrides. +HEADSCALE_DATABASE_TYPE=postgres +HEADSCALE_DATABASE_POSTGRES_HOST=postgres-compat +HEADSCALE_DATABASE_POSTGRES_USER=headscale +HEADSCALE_DATABASE_POSTGRES_PASS=headscale +HEADSCALE_DATABASE_POSTGRES_NAME=headscale diff --git a/hscontrol/types/testdata/compat/hsic-postgres-strict.golden.json b/hscontrol/types/testdata/compat/hsic-postgres-strict.golden.json new file mode 100644 index 000000000..e9d87224c --- /dev/null +++ b/hscontrol/types/testdata/compat/hsic-postgres-strict.golden.json @@ -0,0 +1,181 @@ +{ + "config": { + "ServerURL": "http://hs-compat:8080", + "Addr": "0.0.0.0:8080", + "MetricsAddr": "0.0.0.0:9090", + "TrustedProxies": null, + "Node": { + "Expiry": 0, + "Ephemeral": { + "InactivityTimeout": 1800000000000 + }, + "Routes": { + "HA": { + "ProbeInterval": 10000000000, + "ProbeTimeout": 5000000000 + } + } + }, + "PreAuthKeys": { + "RevokedRetention": 604800000000000 + }, + "PrefixV4": "100.64.0.0/10", + "PrefixV6": "fd7a:115c:a1e0::/48", + "IPAllocation": "sequential", + "NoisePrivateKeyPath": "/tmp/noise_private.key", + "BaseDomain": "headscale.net", + "Log": { + "Format": "text", + "Level": "trace" + }, + "DisableUpdateCheck": false, + "Database": { + "Type": "postgres", + "Debug": false, + "Gorm": { + "Debug": false, + "SlowThreshold": 1000000, + "SkipErrRecordNotFound": false, + "ParameterizedQueries": false + }, + "Sqlite": { + "Path": "", + "WriteAheadLog": true, + "WALAutoCheckPoint": 1000 + }, + "Postgres": { + "Host": "postgres-compat", + "Port": 0, + "Name": "headscale", + "User": "headscale", + "Ssl": "false", + "MaxOpenConnections": 10, + "MaxIdleConnections": 10, + "ConnMaxIdleTimeSecs": 3600 + } + }, + "DERP": { + "ServerEnabled": true, + "AutomaticallyAddEmbeddedDerpRegion": true, + "ServerRegionID": 999, + "ServerRegionCode": "headscale", + "ServerRegionName": "Headscale Embedded DERP", + "ServerPrivateKeyPath": "/tmp/derp.key", + "ServerVerifyClients": true, + "STUNAddr": "0.0.0.0:3478", + "URLs": [], + "Paths": null, + "DERPMap": null, + "AutoUpdate": false, + "UpdateFrequency": 60000000000, + "IPv4": "", + "IPv6": "" + }, + "TLS": { + "CertPath": "", + "KeyPath": "", + "ACME": { + "Hostname": "", + "ChallengeType": "HTTP-01", + "HTTPListen": ":http", + "URL": "", + "Email": "", + "CacheDir": "/var/lib/headscale/cache" + } + }, + "DNSConfig": { + "MagicDNS": true, + "BaseDomain": "headscale.net", + "OverrideLocalDNS": false, + "Nameservers": { + "Global": [ + "127.0.0.11", + "1.1.1.1" + ], + "Split": {} + }, + "SearchDomains": [], + "ExtraRecords": null, + "ExtraRecordsPath": "", + "Certificates": { + "Enabled": false, + "Zone": "headscale.net" + } + }, + "TailcfgDNSConfig": { + "FallbackResolvers": [ + { + "Addr": "127.0.0.11" + }, + { + "Addr": "1.1.1.1" + } + ], + "Domains": [ + "headscale.net" + ], + "Proxied": true + }, + "DNSChallenge": { + "PropagationTimeout": 120000000000, + "Resolvers": [ + "1.1.1.1:53", + "8.8.8.8:53" + ], + "ProviderName": "" + }, + "UnixSocket": "/var/run/headscale/headscale.sock", + "UnixSocketPermission": 504, + "OIDC": { + "OnlyStartIfOIDCIsAvailable": true, + "Issuer": "", + "ClientID": "", + "Scope": [ + "openid", + "profile", + "email" + ], + "ExtraParams": {}, + "AllowedDomains": null, + "AllowedUsers": null, + "AllowedGroups": null, + "EmailVerifiedRequired": true, + "UseExpiryFromToken": false, + "PKCE": { + "Enabled": false, + "Method": "S256" + } + }, + "LogTail": { + "Enabled": false + }, + "Taildrop": { + "Enabled": true + }, + "AutoUpdate": { + "Enabled": false + }, + "CLI": { + "Address": "", + "Timeout": 5000000000, + "Insecure": false + }, + "Policy": { + "Path": "", + "Mode": "file" + }, + "Tuning": { + "NotifierSendTimeout": 800000000, + "BatchChangeDelay": 800000000, + "NodeMapSessionBufferedChanSize": 30, + "BatcherWorkers": 0, + "RegisterCacheExpiration": 0, + "RegisterCacheMaxEntries": 0, + "NodeStoreBatchSize": 100, + "NodeStoreBatchTimeout": 500000000 + } + }, + "secrets": { + "database.postgres.pass": "headscale" + } +} diff --git a/hscontrol/types/testdata/compat/hsic-postgres-strict.yaml b/hscontrol/types/testdata/compat/hsic-postgres-strict.yaml new file mode 100644 index 000000000..74a9b6b88 --- /dev/null +++ b/hscontrol/types/testdata/compat/hsic-postgres-strict.yaml @@ -0,0 +1,2 @@ +noise: + private_key_path: /tmp/noise_private.key diff --git a/hscontrol/types/testdata/compat/hsic-postgres.golden.json b/hscontrol/types/testdata/compat/hsic-postgres.golden.json index 97687d87e..a77eedc4f 100644 --- a/hscontrol/types/testdata/compat/hsic-postgres.golden.json +++ b/hscontrol/types/testdata/compat/hsic-postgres.golden.json @@ -1,184 +1,3 @@ { - "config": { - "ServerURL": "http://hs-compat:8080", - "Addr": "0.0.0.0:8080", - "MetricsAddr": "0.0.0.0:9090", - "TrustedProxies": null, - "Node": { - "Expiry": 0, - "Ephemeral": { - "InactivityTimeout": 120000000000 - }, - "Routes": { - "HA": { - "ProbeInterval": 10000000000, - "ProbeTimeout": 5000000000 - } - } - }, - "PreAuthKeys": { - "RevokedRetention": 604800000000000 - }, - "PrefixV4": "100.64.0.0/10", - "PrefixV6": "fd7a:115c:a1e0::/48", - "IPAllocation": "sequential", - "NoisePrivateKeyPath": "/tmp/noise_private.key", - "BaseDomain": "headscale.net", - "Log": { - "Format": "text", - "Level": "trace" - }, - "DisableUpdateCheck": false, - "Database": { - "Type": "postgres", - "Debug": false, - "Gorm": { - "Debug": false, - "SlowThreshold": 1000000, - "SkipErrRecordNotFound": false, - "ParameterizedQueries": false - }, - "Sqlite": { - "Path": "", - "WriteAheadLog": true, - "WALAutoCheckPoint": 1000 - }, - "Postgres": { - "Host": "postgres-compat", - "Port": 0, - "Name": "headscale", - "User": "headscale", - "Ssl": "false", - "MaxOpenConnections": 10, - "MaxIdleConnections": 10, - "ConnMaxIdleTimeSecs": 3600 - } - }, - "DERP": { - "ServerEnabled": true, - "AutomaticallyAddEmbeddedDerpRegion": true, - "ServerRegionID": 999, - "ServerRegionCode": "headscale", - "ServerRegionName": "Headscale Embedded DERP", - "ServerPrivateKeyPath": "/tmp/derp.key", - "ServerVerifyClients": true, - "STUNAddr": "0.0.0.0:3478", - "URLs": [], - "Paths": null, - "DERPMap": null, - "AutoUpdate": false, - "UpdateFrequency": 60000000000, - "IPv4": "", - "IPv6": "" - }, - "TLS": { - "CertPath": "", - "KeyPath": "", - "ACME": { - "Hostname": "", - "ChallengeType": "HTTP-01", - "HTTPListen": ":http", - "URL": "", - "Email": "", - "CacheDir": "/var/lib/headscale/cache" - } - }, - "DNSConfig": { - "MagicDNS": true, - "BaseDomain": "headscale.net", - "OverrideLocalDNS": false, - "Nameservers": { - "Global": [ - "127.0.0.11", - "1.1.1.1" - ], - "Split": {} - }, - "SearchDomains": [], - "ExtraRecords": null, - "ExtraRecordsPath": "", - "Certificates": { - "Enabled": false, - "Zone": "headscale.net" - } - }, - "TailcfgDNSConfig": { - "FallbackResolvers": [ - { - "Addr": "127.0.0.11" - }, - { - "Addr": "1.1.1.1" - } - ], - "Domains": [ - "headscale.net" - ], - "Proxied": true - }, - "DNSChallenge": { - "PropagationTimeout": 120000000000, - "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" - ], - "ProviderName": "" - }, - "UnixSocket": "/var/run/headscale/headscale.sock", - "UnixSocketPermission": 504, - "OIDC": { - "OnlyStartIfOIDCIsAvailable": true, - "Issuer": "", - "ClientID": "", - "Scope": [ - "openid", - "profile", - "email" - ], - "ExtraParams": {}, - "AllowedDomains": null, - "AllowedUsers": null, - "AllowedGroups": null, - "EmailVerifiedRequired": true, - "UseExpiryFromToken": false, - "PKCE": { - "Enabled": false, - "Method": "S256" - } - }, - "LogTail": { - "Enabled": false - }, - "Taildrop": { - "Enabled": true - }, - "AutoUpdate": { - "Enabled": false - }, - "CLI": { - "Address": "", - "Timeout": 5000000000, - "Insecure": false - }, - "Policy": { - "Path": "", - "Mode": "file" - }, - "Tuning": { - "NotifierSendTimeout": 800000000, - "BatchChangeDelay": 800000000, - "NodeMapSessionBufferedChanSize": 30, - "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, - "NodeStoreBatchSize": 100, - "NodeStoreBatchTimeout": 500000000 - } - }, - "secrets": { - "database.postgres.pass": "headscale" - }, - "warnings": [ - "configuration key \"ephemeral_node_inactivity_timeout\" is deprecated; use \"node.ephemeral.inactivity_timeout\" instead" - ] + "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/nixos-example-strict.golden.json b/hscontrol/types/testdata/compat/nixos-example-strict.golden.json new file mode 100644 index 000000000..8e682be0b --- /dev/null +++ b/hscontrol/types/testdata/compat/nixos-example-strict.golden.json @@ -0,0 +1,192 @@ +{ + "config": { + "ServerURL": "https://headscale.example.com", + "Addr": "0.0.0.0:8080", + "MetricsAddr": "", + "TrustedProxies": null, + "Node": { + "Expiry": 0, + "Ephemeral": { + "InactivityTimeout": 1800000000000 + }, + "Routes": { + "HA": { + "ProbeInterval": 10000000000, + "ProbeTimeout": 5000000000 + } + } + }, + "PreAuthKeys": { + "RevokedRetention": 604800000000000 + }, + "PrefixV4": "100.64.0.0/10", + "PrefixV6": "fd7a:115c:a1e0::/48", + "IPAllocation": "sequential", + "NoisePrivateKeyPath": "/var/lib/headscale/noise_private.key", + "BaseDomain": "tailnet.example.com", + "Log": { + "Format": "text", + "Level": "info" + }, + "DisableUpdateCheck": true, + "Database": { + "Type": "sqlite3", + "Debug": false, + "Gorm": { + "Debug": false, + "SlowThreshold": 0, + "SkipErrRecordNotFound": false, + "ParameterizedQueries": false + }, + "Sqlite": { + "Path": "/var/lib/headscale/db.sqlite", + "WriteAheadLog": true, + "WALAutoCheckPoint": 1000 + }, + "Postgres": { + "Host": "", + "Port": 0, + "Name": "", + "User": "", + "Ssl": "false", + "MaxOpenConnections": 10, + "MaxIdleConnections": 10, + "ConnMaxIdleTimeSecs": 3600 + } + }, + "DERP": { + "ServerEnabled": false, + "AutomaticallyAddEmbeddedDerpRegion": true, + "ServerRegionID": 0, + "ServerRegionCode": "", + "ServerRegionName": "", + "ServerPrivateKeyPath": "/var/lib/headscale/derp_server_private.key", + "ServerVerifyClients": true, + "STUNAddr": "", + "URLs": [ + { + "Scheme": "https", + "Opaque": "", + "User": null, + "Host": "controlplane.tailscale.com", + "Path": "/derpmap/default", + "Fragment": "", + "RawQuery": "", + "RawPath": "", + "RawFragment": "", + "ForceQuery": false, + "OmitHost": false + } + ], + "Paths": [], + "DERPMap": null, + "AutoUpdate": true, + "UpdateFrequency": 86400000000000, + "IPv4": "", + "IPv6": "" + }, + "TLS": { + "CertPath": "", + "KeyPath": "", + "ACME": { + "Hostname": "", + "ChallengeType": "HTTP-01", + "HTTPListen": ":http", + "URL": "", + "Email": "", + "CacheDir": "/var/lib/headscale/cache" + } + }, + "DNSConfig": { + "MagicDNS": true, + "BaseDomain": "tailnet.example.com", + "OverrideLocalDNS": true, + "Nameservers": { + "Global": [ + "1.1.1.1", + "8.8.8.8" + ], + "Split": {} + }, + "SearchDomains": [], + "ExtraRecords": null, + "ExtraRecordsPath": "", + "Certificates": { + "Enabled": false, + "Zone": "tailnet.example.com" + } + }, + "TailcfgDNSConfig": { + "Resolvers": [ + { + "Addr": "1.1.1.1" + }, + { + "Addr": "8.8.8.8" + } + ], + "Domains": [ + "tailnet.example.com" + ], + "Proxied": true + }, + "DNSChallenge": { + "PropagationTimeout": 120000000000, + "Resolvers": [ + "1.1.1.1:53", + "8.8.8.8:53" + ], + "ProviderName": "" + }, + "UnixSocket": "/run/headscale/headscale.sock", + "UnixSocketPermission": 504, + "OIDC": { + "OnlyStartIfOIDCIsAvailable": true, + "Issuer": "", + "ClientID": "", + "Scope": [ + "openid", + "profile", + "email" + ], + "ExtraParams": {}, + "AllowedDomains": [], + "AllowedUsers": [], + "AllowedGroups": null, + "EmailVerifiedRequired": true, + "UseExpiryFromToken": false, + "PKCE": { + "Enabled": false, + "Method": "S256" + } + }, + "LogTail": { + "Enabled": false + }, + "Taildrop": { + "Enabled": true + }, + "AutoUpdate": { + "Enabled": false + }, + "CLI": { + "Address": "", + "Timeout": 5000000000, + "Insecure": false + }, + "Policy": { + "Path": "/var/lib/headscale/policy.hujson", + "Mode": "file" + }, + "Tuning": { + "NotifierSendTimeout": 800000000, + "BatchChangeDelay": 800000000, + "NodeMapSessionBufferedChanSize": 30, + "BatcherWorkers": 0, + "RegisterCacheExpiration": 0, + "RegisterCacheMaxEntries": 0, + "NodeStoreBatchSize": 100, + "NodeStoreBatchTimeout": 500000000 + } + } +} diff --git a/hscontrol/types/testdata/compat/nixos-example-strict.yaml b/hscontrol/types/testdata/compat/nixos-example-strict.yaml new file mode 100644 index 000000000..6e6fa75bf --- /dev/null +++ b/hscontrol/types/testdata/compat/nixos-example-strict.yaml @@ -0,0 +1,76 @@ +%YAML 1.1 +--- +database: + postgres: + host: null + name: null + port: null + user: null + sqlite: + path: /var/lib/headscale/db.sqlite + write_ahead_log: true + type: sqlite +derp: + auto_update_enabled: true + paths: [] + server: + private_key_path: /var/lib/headscale/derp_server_private.key + update_frequency: 24h + urls: + - https://controlplane.tailscale.com/derpmap/default +disable_check_updates: true +dns: + base_domain: tailnet.example.com + certificates: + enabled: false + zone: '' + challenge: + provider: null + extra_records: null + magic_dns: true + nameservers: + global: + - 1.1.1.1 + - 8.8.8.8 + split: {} + override_local_dns: true + search_domains: [] +listen_addr: 0.0.0.0:8080 +log: + format: text + level: info +node: + ephemeral: + inactivity_timeout: 30m + expiry: '0' +noise: + private_key_path: /var/lib/headscale/noise_private.key +oidc: + allowed_domains: [] + allowed_users: [] + client_id: '' + extra_params: {} + issuer: '' + pkce: + enabled: false + method: S256 + scope: + - openid + - profile + - email +policy: + mode: file + path: /var/lib/headscale/policy.hujson +prefixes: + allocation: sequential + v4: 100.64.0.0/10 + v6: fd7a:115c:a1e0::/48 +server_url: https://headscale.example.com +tls: + acme: + challenge_type: null + hostname: '' + http_listen: null + cert_path: null + key_path: null +unix_socket: /run/headscale/headscale.sock diff --git a/hscontrol/types/testdata/compat/nixos-example.golden.json b/hscontrol/types/testdata/compat/nixos-example.golden.json index 628882097..8e682be0b 100644 --- a/hscontrol/types/testdata/compat/nixos-example.golden.json +++ b/hscontrol/types/testdata/compat/nixos-example.golden.json @@ -28,7 +28,7 @@ "Format": "text", "Level": "info" }, - "DisableUpdateCheck": false, + "DisableUpdateCheck": true, "Database": { "Type": "sqlite3", "Debug": false, diff --git a/hscontrol/types/testdata/compat/nixos-nixpkgs.golden.json b/hscontrol/types/testdata/compat/nixos-nixpkgs.golden.json index 2a76d347f..b69ebac2b 100644 --- a/hscontrol/types/testdata/compat/nixos-nixpkgs.golden.json +++ b/hscontrol/types/testdata/compat/nixos-nixpkgs.golden.json @@ -1,188 +1,3 @@ { - "config": { - "ServerURL": "https://headscale.example.com", - "Addr": "127.0.0.1:8080", - "MetricsAddr": "", - "TrustedProxies": null, - "Node": { - "Expiry": 0, - "Ephemeral": { - "InactivityTimeout": 1800000000000 - }, - "Routes": { - "HA": { - "ProbeInterval": 10000000000, - "ProbeTimeout": 5000000000 - } - } - }, - "PreAuthKeys": { - "RevokedRetention": 604800000000000 - }, - "PrefixV4": "100.64.0.0/10", - "PrefixV6": "fd7a:115c:a1e0::/48", - "IPAllocation": "sequential", - "NoisePrivateKeyPath": "/var/lib/headscale/noise_private.key", - "BaseDomain": "tailnet.example.com", - "Log": { - "Format": "text", - "Level": "info" - }, - "DisableUpdateCheck": false, - "Database": { - "Type": "sqlite3", - "Debug": false, - "Gorm": { - "Debug": false, - "SlowThreshold": 0, - "SkipErrRecordNotFound": false, - "ParameterizedQueries": false - }, - "Sqlite": { - "Path": "/var/lib/headscale/db.sqlite", - "WriteAheadLog": true, - "WALAutoCheckPoint": 1000 - }, - "Postgres": { - "Host": "", - "Port": 0, - "Name": "", - "User": "", - "Ssl": "false", - "MaxOpenConnections": 10, - "MaxIdleConnections": 10, - "ConnMaxIdleTimeSecs": 3600 - } - }, - "DERP": { - "ServerEnabled": false, - "AutomaticallyAddEmbeddedDerpRegion": true, - "ServerRegionID": 0, - "ServerRegionCode": "", - "ServerRegionName": "", - "ServerPrivateKeyPath": "/var/lib/headscale/derp_server_private.key", - "ServerVerifyClients": true, - "STUNAddr": "", - "URLs": [ - { - "Scheme": "https", - "Opaque": "", - "User": null, - "Host": "controlplane.tailscale.com", - "Path": "/derpmap/default", - "Fragment": "", - "RawQuery": "", - "RawPath": "", - "RawFragment": "", - "ForceQuery": false, - "OmitHost": false - } - ], - "Paths": [], - "DERPMap": null, - "AutoUpdate": true, - "UpdateFrequency": 86400000000000, - "IPv4": "", - "IPv6": "" - }, - "TLS": { - "CertPath": "", - "KeyPath": "", - "ACME": { - "Hostname": "", - "ChallengeType": "HTTP-01", - "HTTPListen": ":http", - "URL": "", - "Email": "", - "CacheDir": "/var/lib/headscale/.cache" - } - }, - "DNSConfig": { - "MagicDNS": true, - "BaseDomain": "tailnet.example.com", - "OverrideLocalDNS": true, - "Nameservers": { - "Global": [ - "1.1.1.1" - ], - "Split": {} - }, - "SearchDomains": [], - "ExtraRecords": null, - "ExtraRecordsPath": "", - "Certificates": { - "Enabled": false, - "Zone": "tailnet.example.com" - } - }, - "TailcfgDNSConfig": { - "Resolvers": [ - { - "Addr": "1.1.1.1" - } - ], - "Domains": [ - "tailnet.example.com" - ], - "Proxied": true - }, - "DNSChallenge": { - "PropagationTimeout": 120000000000, - "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" - ], - "ProviderName": "" - }, - "UnixSocket": "/run/headscale/headscale.sock", - "UnixSocketPermission": 504, - "OIDC": { - "OnlyStartIfOIDCIsAvailable": true, - "Issuer": "", - "ClientID": "", - "Scope": [ - "openid", - "profile", - "email" - ], - "ExtraParams": {}, - "AllowedDomains": [], - "AllowedUsers": [], - "AllowedGroups": null, - "EmailVerifiedRequired": true, - "UseExpiryFromToken": false, - "PKCE": { - "Enabled": false, - "Method": "S256" - } - }, - "LogTail": { - "Enabled": false - }, - "Taildrop": { - "Enabled": true - }, - "AutoUpdate": { - "Enabled": false - }, - "CLI": { - "Address": "", - "Timeout": 5000000000, - "Insecure": false - }, - "Policy": { - "Path": "", - "Mode": "file" - }, - "Tuning": { - "NotifierSendTimeout": 800000000, - "BatchChangeDelay": 800000000, - "NodeMapSessionBufferedChanSize": 30, - "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, - "NodeStoreBatchSize": 100, - "NodeStoreBatchTimeout": 500000000 - } - } + "error": "Fatal config error: configuration key tls_letsencrypt_cache_dir was removed in 0.30 ($DIR/config.yaml:56:1)\n hint: use tls.acme.cache_dir\n\nFatal config error: configuration key tls_letsencrypt_challenge_type was removed in 0.30 ($DIR/config.yaml:57:1)\n hint: use tls.acme.challenge_type\n\nFatal config error: configuration key tls_letsencrypt_hostname was removed in 0.30 ($DIR/config.yaml:58:1)\n hint: use tls.acme.hostname\n\nFatal config error: configuration key tls_letsencrypt_listen was removed in 0.30 ($DIR/config.yaml:59:1)\n hint: use tls.acme.http_listen\n" } diff --git a/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json b/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json new file mode 100644 index 000000000..21d707716 --- /dev/null +++ b/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json @@ -0,0 +1,162 @@ +{ + "config": { + "ServerURL": "https://derp.no", + "Addr": "", + "MetricsAddr": "", + "TrustedProxies": null, + "Node": { + "Expiry": 0, + "Ephemeral": { + "InactivityTimeout": 120000000000 + }, + "Routes": { + "HA": { + "ProbeInterval": 10000000000, + "ProbeTimeout": 5000000000 + } + } + }, + "PreAuthKeys": { + "RevokedRetention": 604800000000000 + }, + "PrefixV4": "100.64.0.0/10", + "PrefixV6": "fd7a:115c:a1e0::/48", + "IPAllocation": "sequential", + "NoisePrivateKeyPath": "$DIR/private_key.pem", + "BaseDomain": "", + "Log": { + "Format": "text", + "Level": "info" + }, + "DisableUpdateCheck": false, + "Database": { + "Type": "sqlite3", + "Debug": false, + "Gorm": { + "Debug": false, + "SlowThreshold": 0, + "SkipErrRecordNotFound": false, + "ParameterizedQueries": false + }, + "Sqlite": { + "Path": "", + "WriteAheadLog": true, + "WALAutoCheckPoint": 1000 + }, + "Postgres": { + "Host": "", + "Port": 0, + "Name": "", + "User": "", + "Ssl": "false", + "MaxOpenConnections": 10, + "MaxIdleConnections": 10, + "ConnMaxIdleTimeSecs": 3600 + } + }, + "DERP": { + "ServerEnabled": false, + "AutomaticallyAddEmbeddedDerpRegion": true, + "ServerRegionID": 0, + "ServerRegionCode": "", + "ServerRegionName": "", + "ServerPrivateKeyPath": "", + "ServerVerifyClients": true, + "STUNAddr": "", + "URLs": [], + "Paths": null, + "DERPMap": null, + "AutoUpdate": false, + "UpdateFrequency": 10800000000000, + "IPv4": "", + "IPv6": "" + }, + "TLS": { + "CertPath": "", + "KeyPath": "", + "ACME": { + "Hostname": "", + "ChallengeType": "HTTP-01", + "HTTPListen": ":http", + "URL": "", + "Email": "", + "CacheDir": "/var/lib/headscale/cache" + } + }, + "DNSConfig": { + "MagicDNS": false, + "BaseDomain": "", + "OverrideLocalDNS": false, + "Nameservers": { + "Global": [], + "Split": {} + }, + "SearchDomains": [], + "ExtraRecords": null, + "ExtraRecordsPath": "", + "Certificates": { + "Enabled": false, + "Zone": "" + } + }, + "TailcfgDNSConfig": {}, + "DNSChallenge": { + "PropagationTimeout": 120000000000, + "Resolvers": [ + "1.1.1.1:53", + "8.8.8.8:53" + ], + "ProviderName": "" + }, + "UnixSocket": "/var/run/headscale/headscale.sock", + "UnixSocketPermission": 504, + "OIDC": { + "OnlyStartIfOIDCIsAvailable": true, + "Issuer": "", + "ClientID": "", + "Scope": [ + "openid", + "profile", + "email" + ], + "ExtraParams": {}, + "AllowedDomains": null, + "AllowedUsers": null, + "AllowedGroups": null, + "EmailVerifiedRequired": true, + "UseExpiryFromToken": false, + "PKCE": { + "Enabled": false, + "Method": "S256" + } + }, + "LogTail": { + "Enabled": false + }, + "Taildrop": { + "Enabled": true + }, + "AutoUpdate": { + "Enabled": false + }, + "CLI": { + "Address": "", + "Timeout": 5000000000, + "Insecure": false + }, + "Policy": { + "Path": "/etc/policy.hujson", + "Mode": "file" + }, + "Tuning": { + "NotifierSendTimeout": 800000000, + "BatchChangeDelay": 800000000, + "NodeMapSessionBufferedChanSize": 30, + "BatcherWorkers": 0, + "RegisterCacheExpiration": 0, + "RegisterCacheMaxEntries": 0, + "NodeStoreBatchSize": 100, + "NodeStoreBatchTimeout": 500000000 + } + } +} diff --git a/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded.golden.json b/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded.golden.json index 24cefeb87..77b962243 100644 --- a/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded.golden.json +++ b/hscontrol/types/testdata/compat/testdata-policy-path-is-loaded.golden.json @@ -1,165 +1,3 @@ { - "config": { - "ServerURL": "https://derp.no", - "Addr": "", - "MetricsAddr": "", - "TrustedProxies": null, - "Node": { - "Expiry": 0, - "Ephemeral": { - "InactivityTimeout": 120000000000 - }, - "Routes": { - "HA": { - "ProbeInterval": 10000000000, - "ProbeTimeout": 5000000000 - } - } - }, - "PreAuthKeys": { - "RevokedRetention": 604800000000000 - }, - "PrefixV4": "100.64.0.0/10", - "PrefixV6": "fd7a:115c:a1e0::/48", - "IPAllocation": "sequential", - "NoisePrivateKeyPath": "$DIR/private_key.pem", - "BaseDomain": "", - "Log": { - "Format": "text", - "Level": "info" - }, - "DisableUpdateCheck": false, - "Database": { - "Type": "sqlite3", - "Debug": false, - "Gorm": { - "Debug": false, - "SlowThreshold": 0, - "SkipErrRecordNotFound": false, - "ParameterizedQueries": false - }, - "Sqlite": { - "Path": "", - "WriteAheadLog": true, - "WALAutoCheckPoint": 1000 - }, - "Postgres": { - "Host": "", - "Port": 0, - "Name": "", - "User": "", - "Ssl": "false", - "MaxOpenConnections": 10, - "MaxIdleConnections": 10, - "ConnMaxIdleTimeSecs": 3600 - } - }, - "DERP": { - "ServerEnabled": false, - "AutomaticallyAddEmbeddedDerpRegion": true, - "ServerRegionID": 0, - "ServerRegionCode": "", - "ServerRegionName": "", - "ServerPrivateKeyPath": "", - "ServerVerifyClients": true, - "STUNAddr": "", - "URLs": [], - "Paths": null, - "DERPMap": null, - "AutoUpdate": false, - "UpdateFrequency": 10800000000000, - "IPv4": "", - "IPv6": "" - }, - "TLS": { - "CertPath": "", - "KeyPath": "", - "ACME": { - "Hostname": "", - "ChallengeType": "HTTP-01", - "HTTPListen": ":http", - "URL": "", - "Email": "", - "CacheDir": "/var/lib/headscale/cache" - } - }, - "DNSConfig": { - "MagicDNS": false, - "BaseDomain": "", - "OverrideLocalDNS": false, - "Nameservers": { - "Global": [], - "Split": {} - }, - "SearchDomains": [], - "ExtraRecords": null, - "ExtraRecordsPath": "", - "Certificates": { - "Enabled": false, - "Zone": "" - } - }, - "TailcfgDNSConfig": {}, - "DNSChallenge": { - "PropagationTimeout": 120000000000, - "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" - ], - "ProviderName": "" - }, - "UnixSocket": "/var/run/headscale/headscale.sock", - "UnixSocketPermission": 504, - "OIDC": { - "OnlyStartIfOIDCIsAvailable": true, - "Issuer": "", - "ClientID": "", - "Scope": [ - "openid", - "profile", - "email" - ], - "ExtraParams": {}, - "AllowedDomains": null, - "AllowedUsers": null, - "AllowedGroups": null, - "EmailVerifiedRequired": true, - "UseExpiryFromToken": false, - "PKCE": { - "Enabled": false, - "Method": "S256" - } - }, - "LogTail": { - "Enabled": false - }, - "Taildrop": { - "Enabled": true - }, - "AutoUpdate": { - "Enabled": false - }, - "CLI": { - "Address": "", - "Timeout": 5000000000, - "Insecure": false - }, - "Policy": { - "Path": "/etc/policy.hujson", - "Mode": "file" - }, - "Tuning": { - "NotifierSendTimeout": 800000000, - "BatchChangeDelay": 800000000, - "NodeMapSessionBufferedChanSize": 30, - "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, - "NodeStoreBatchSize": 100, - "NodeStoreBatchTimeout": 500000000 - } - }, - "warnings": [ - "configuration key \"acl_policy_path\" is deprecated; use \"policy.path\" instead" - ] + "error": "Fatal config error: configuration key acl_policy_path was removed in 0.30 ($DIR/config.yaml:13:1)\n hint: use policy.path\n\nFatal config error: unknown configuration key policy.type ($DIR/config.yaml:15:3)\n" } diff --git a/hscontrol/types/testdata/policy-path-is-loaded-strict.yaml b/hscontrol/types/testdata/policy-path-is-loaded-strict.yaml new file mode 100644 index 000000000..c1832d2e2 --- /dev/null +++ b/hscontrol/types/testdata/policy-path-is-loaded-strict.yaml @@ -0,0 +1,18 @@ +noise: + private_key_path: "private_key.pem" + +prefixes: + v6: fd7a:115c:a1e0::/48 + v4: 100.64.0.0/10 + +database: + type: sqlite3 + +server_url: "https://derp.no" + +policy: + path: "/etc/policy.hujson" + +dns: + magic_dns: false + override_local_dns: false diff --git a/hscontrol/util/file.go b/hscontrol/util/file.go index 49cc1a870..8bee77d19 100644 --- a/hscontrol/util/file.go +++ b/hscontrol/util/file.go @@ -12,7 +12,6 @@ import ( "strconv" "strings" - "github.com/spf13/viper" "github.com/tailscale/hujson" "gopkg.in/yaml.v3" ) @@ -99,14 +98,11 @@ func ReadFileByExt[T any](path string) (T, error) { return UnmarshalByExt[T](path, data) } -func AbsolutePathFromConfigPath(path string) string { - // If a relative path is provided, prefix it with the directory where - // the config file was found. - if (path != "") && !strings.HasPrefix(path, string(os.PathSeparator)) { - dir, _ := filepath.Split(viper.ConfigFileUsed()) - if dir != "" { - path = filepath.Join(dir, path) - } +// AbsolutePath resolves a relative path from the configuration against +// configDir, the directory relative paths are read from. +func AbsolutePath(configDir, path string) string { + if path != "" && configDir != "" && !filepath.IsAbs(path) { + return filepath.Join(configDir, path) } return path diff --git a/integration/auth_oidc_test.go b/integration/auth_oidc_test.go index d71ea18c3..f2311e1e6 100644 --- a/integration/auth_oidc_test.go +++ b/integration/auth_oidc_test.go @@ -25,6 +25,12 @@ import ( "tailscale.com/tailcfg" ) +// oidcClientFileRef is HEADSCALE_OIDC_CLIENT_SECRET for tests that write the +// mock provider's client secret to /tmp/hs_client_oidc_secret and set +// CREDENTIALS_DIRECTORY_TEST=/tmp. A named reference, not a literal per map, +// keeps gosec from reading it as a hardcoded credential. +const oidcClientFileRef = "file:${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret" + func TestOIDCAuthenticationPingAll(t *testing.T) { IntegrationSkip(t) @@ -46,10 +52,10 @@ func TestOIDCAuthenticationPingAll(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, } // OIDC tests configure the mock OIDC provider via environment @@ -364,10 +370,10 @@ func TestOIDC024UserCreation(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, } maps.Copy(oidcMap, tt.config) @@ -421,11 +427,11 @@ func TestOIDCAuthenticationWithPKCE(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_PKCE_ENABLED": "1", // Enable PKCE + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_PKCE_ENABLED": "1", // Enable PKCE } err = scenario.CreateHeadscaleEnvWithLoginURL( @@ -477,10 +483,10 @@ func TestOIDCReloginSameNodeNewUser(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, } err = scenario.CreateHeadscaleEnvWithLoginURL( @@ -883,12 +889,11 @@ func TestOIDCFollowUpUrl(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, // smaller cache expiration time to quickly expire AuthURL - "HEADSCALE_TUNING_REGISTER_CACHE_CLEANUP": "10s", "HEADSCALE_TUNING_REGISTER_CACHE_EXPIRATION": "1m30s", } @@ -997,10 +1002,10 @@ func TestOIDCMultipleOpenedLoginUrls(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, } err = scenario.CreateHeadscaleEnvWithLoginURL( @@ -1129,10 +1134,10 @@ func TestOIDCReloginSameNodeSameUser(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, } err = scenario.CreateHeadscaleEnvWithLoginURL( @@ -1353,11 +1358,11 @@ func TestOIDCExpiryAfterRestart(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", - "HEADSCALE_NODE_EXPIRY": "72h", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, + "HEADSCALE_NODE_EXPIRY": "72h", } err = scenario.CreateHeadscaleEnvWithLoginURL( @@ -1498,10 +1503,10 @@ func TestOIDCACLPolicyOnJoin(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, } // Create headscale environment with ACL policy that allows OIDC user @@ -1776,10 +1781,10 @@ func TestOIDCReloginSameUserRoutesPreserved(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, } err = scenario.CreateHeadscaleEnvWithLoginURL( diff --git a/integration/general_test.go b/integration/general_test.go index db1f1c62c..03080f290 100644 --- a/integration/general_test.go +++ b/integration/general_test.go @@ -231,7 +231,7 @@ func TestEphemeral2006DeletedTooQuickly(t *testing.T) { headscale, err := scenario.Headscale( hsic.WithTestName("ephemeral2006"), hsic.WithConfigEnv(map[string]string{ - "HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT": "1m6s", + "HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT": "1m6s", }), ) requireNoErrHeadscaleEnv(t, err) diff --git a/integration/hsic/config.go b/integration/hsic/config.go index 02b5277a8..53c8f4c44 100644 --- a/integration/hsic/config.go +++ b/integration/hsic/config.go @@ -4,7 +4,6 @@ import "github.com/juanfont/headscale/hscontrol/types" func MinimumConfigYAML() string { return ` -private_key_path: /tmp/private.key noise: private_key_path: /tmp/noise_private.key ` @@ -18,17 +17,15 @@ func DefaultConfigEnv() map[string]string { "HEADSCALE_DATABASE_SQLITE_PATH": "/tmp/integration_test_db.sqlite3", "HEADSCALE_DATABASE_DEBUG": "0", "HEADSCALE_DATABASE_GORM_SLOW_THRESHOLD": "1", - "HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT": "30m", + "HEADSCALE_NODE_EPHEMERAL_INACTIVITY_TIMEOUT": "30m", "HEADSCALE_PREFIXES_V4": "100.64.0.0/10", "HEADSCALE_PREFIXES_V6": "fd7a:115c:a1e0::/48", "HEADSCALE_DNS_BASE_DOMAIN": "headscale.net", "HEADSCALE_DNS_MAGIC_DNS": "true", "HEADSCALE_DNS_OVERRIDE_LOCAL_DNS": "false", "HEADSCALE_DNS_NAMESERVERS_GLOBAL": "127.0.0.11 1.1.1.1", - "HEADSCALE_PRIVATE_KEY_PATH": "/tmp/private.key", "HEADSCALE_NOISE_PRIVATE_KEY_PATH": "/tmp/noise_private.key", "HEADSCALE_METRICS_LISTEN_ADDR": "0.0.0.0:9090", - "HEADSCALE_DEBUG_PORT": "40000", // Embedded DERP is the default for test isolation. // Tests should not depend on external DERP infrastructure. diff --git a/integration/ssh_test.go b/integration/ssh_test.go index 94fce0dd4..4ed9777dd 100644 --- a/integration/ssh_test.go +++ b/integration/ssh_test.go @@ -1053,10 +1053,10 @@ func TestSSHOneUserToOneCheckModeOIDC(t *testing.T) { // defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, } err = scenario.CreateHeadscaleEnvWithLoginURL( @@ -1174,7 +1174,6 @@ func TestSSHCheckModeUnapprovedTimeout(t *testing.T) { hsic.WithTestName("sshchecktimeout"), hsic.WithConfigEnv(map[string]string{ "HEADSCALE_TUNING_REGISTER_CACHE_EXPIRATION": "15s", - "HEADSCALE_TUNING_REGISTER_CACHE_CLEANUP": "5s", }), ) require.NoError(t, err) @@ -1666,10 +1665,10 @@ func TestSSHLocalpart(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, } err = scenario.CreateHeadscaleEnvWithLoginURL( diff --git a/integration/tags_test.go b/integration/tags_test.go index 2b9efa89c..00fe525d8 100644 --- a/integration/tags_test.go +++ b/integration/tags_test.go @@ -3642,10 +3642,10 @@ func TestTagsOIDCReauthAddOwnedTag(t *testing.T) { defer scenario.ShutdownAssertNoPanics(t) oidcMap := map[string]string{ - "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), - "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), - "CREDENTIALS_DIRECTORY_TEST": "/tmp", - "HEADSCALE_OIDC_CLIENT_SECRET_PATH": "${CREDENTIALS_DIRECTORY_TEST}/hs_client_oidc_secret", + "HEADSCALE_OIDC_ISSUER": scenario.mockOIDC.Issuer(), + "HEADSCALE_OIDC_CLIENT_ID": scenario.mockOIDC.ClientID(), + "CREDENTIALS_DIRECTORY_TEST": "/tmp", + "HEADSCALE_OIDC_CLIENT_SECRET": oidcClientFileRef, } // The OIDC user owns both tags. Ownership is what authorises the reauth tag diff --git a/nix/example-configuration.nix b/nix/example-configuration.nix index 4f959e65a..cc24d6c6b 100644 --- a/nix/example-configuration.nix +++ b/nix/example-configuration.nix @@ -20,8 +20,9 @@ services.headscale = { enable = true; - # Optional: Use a specific package (defaults to pkgs.headscale) - # package = pkgs.headscale; + # Use this flake's headscale. The default, pkgs.headscale from nixpkgs, + # may predate settings this module writes, such as "file:" secrets. + # package = inputs.headscale.packages.${pkgs.stdenv.hostPlatform.system}.headscale; # Listen on all interfaces (default is 127.0.0.1) address = "0.0.0.0"; @@ -86,7 +87,7 @@ # port = 5432; # name = "headscale"; # user = "headscale"; - # password_file = "/run/secrets/headscale-db-password"; + # pass = "file:\${CREDENTIALS_DIRECTORY}/db-password"; # }; }; @@ -100,7 +101,7 @@ # oidc = { # issuer = "https://accounts.google.com"; # client_id = "your-client-id"; - # client_secret_path = "/run/secrets/oidc-client-secret"; + # client_secret = "file:\${CREDENTIALS_DIRECTORY}/oidc-client-secret"; # scope = [ "openid" "profile" "email" ]; # allowed_domains = [ "example.com" ]; # }; @@ -128,6 +129,13 @@ }; }; + # Secrets: systemd copies each file into $CREDENTIALS_DIRECTORY, readable + # only by the service, and the settings above read it with "file:". + # systemd.services.headscale.serviceConfig.LoadCredential = [ + # "db-password:/run/secrets/headscale-db-password" + # "oidc-client-secret:/run/secrets/oidc-client-secret" + # ]; + # Optional: Open firewall ports networking.firewall = { allowedTCPPorts = [ 8080 ]; diff --git a/nix/module.nix b/nix/module.nix index 3a516a4c7..3633dc3aa 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -290,16 +290,6 @@ in example = "headscale"; description = "Database user."; }; - - password_file = lib.mkOption { - type = lib.types.nullOr lib.types.path; - default = null; - example = "/run/keys/headscale-dbpassword"; - description = '' - A file containing the password corresponding to - {option}`database.user`. - ''; - }; }; }; @@ -466,12 +456,12 @@ in description = '' DNS provider for ACME DNS-01 challenges. Every key besides `name` is a setting of the provider; a text setting can be read - from a file by adding `_path` to its name. + from a file by setting it to `file:`. ''; example = lib.literalExpression '' { name = "cloudflare"; - api_token_path = "''${CREDENTIALS_DIRECTORY}/cloudflare-token"; + api_token = "file:\''${CREDENTIALS_DIRECTORY}/cloudflare-token"; } ''; }; @@ -495,14 +485,6 @@ in ''; }; - client_secret_path = lib.mkOption { - type = lib.types.nullOr lib.types.str; - default = null; - description = '' - Path to OpenID Connect client secret file. Expands environment variables in format ''${VAR}. - ''; - }; - scope = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ @@ -597,8 +579,7 @@ in ''; }; - # Defaults are null so headscale falls back to the flat - # tls_letsencrypt_* keys still set in older configurations. + # Null is written as YAML null, which headscale treats as unset. challenge_type = lib.mkOption { type = lib.types.nullOr ( lib.types.enum [ @@ -688,10 +669,9 @@ in [ "services" "headscale" "openIdConnect" "clientId" ] [ "services" "headscale" "settings" "oidc" "client_id" ] ) - (mkRenamedOptionModule - [ "services" "headscale" "openIdConnect" "clientSecretFile" ] - [ "services" "headscale" "settings" "oidc" "client_secret_path" ] - ) + (mkRemovedOptionModule [ "services" "headscale" "openIdConnect" "clientSecretFile" ] '' + Load the secret with systemd.services.headscale.serviceConfig.LoadCredential and set services.headscale.settings.oidc.client_secret = "file:\''${CREDENTIALS_DIRECTORY}/". + '') (mkRenamedOptionModule [ "services" "headscale" "openIdConnect" "issuer" ] [ "services" "headscale" "settings" "oidc" "issuer" ] @@ -722,7 +702,7 @@ in ) (mkRemovedOptionModule [ "services" "headscale" "openIdConnect" "domainMap" ] '' - Headscale no longer uses domain_map. If you're using an old version of headscale you can still set this option via services.headscale.settings.oidc.domain_map. + Headscale no longer uses domain_map. '') ]; @@ -755,7 +735,7 @@ in (assertRemovedOption [ "settings" "db_password_file" - ] "Use `database.postgres.password_file` instead.") + ] "Use `database.postgres.pass = \"file:\"` instead.") (assertRemovedOption [ "settings" "db_path" ] "Use `database.sqlite.path` instead.") (assertRemovedOption [ "settings" "db_port" ] "Use `database.postgres.port` instead.") (assertRemovedOption [ "settings" "db_type" ] "Use `database.type` instead.") @@ -777,6 +757,17 @@ in "settings" "ephemeral_node_inactivity_timeout" ] "Use `node.ephemeral.inactivity_timeout` instead.") + (assertRemovedOption [ + "settings" + "database" + "postgres" + "password_file" + ] "Use `database.postgres.pass = \"file:\"` instead.") + (assertRemovedOption [ + "settings" + "oidc" + "client_secret_path" + ] "Use `oidc.client_secret = \"file:\"` instead.") ]; services.headscale.settings = lib.mkMerge [ @@ -810,10 +801,6 @@ in wantedBy = [ "multi-user.target" ]; script = '' - ${lib.optionalString (cfg.settings.database.postgres.password_file != null) '' - export HEADSCALE_DATABASE_POSTGRES_PASS="$(head -n1 ${lib.escapeShellArg cfg.settings.database.postgres.password_file})" - ''} - exec ${lib.getExe cfg.package} serve --config ${cfg.configFile} ''; diff --git a/nix/tests/headscale.nix b/nix/tests/headscale.nix index fb9a59674..916615d7a 100644 --- a/nix/tests/headscale.nix +++ b/nix/tests/headscale.nix @@ -37,6 +37,9 @@ in challenge.provider = { name = "rfc2136"; server = "127.0.0.1:53"; + key_name = "headscale."; + key_alg = "hmac-sha256."; + key = "c2VjcmV0"; }; }; };