config: load configuration outside types

Updates #1926
This commit is contained in:
Kristoffer Dalby committed 2026-11-11 15:02:16 +01:00
1 parent 890adee7fa
commit f1cbdcfacb
155 files changed
+5918 -5381

No files matched your search

+2 -1
View File
@@ -2,4 +2,5 @@
hscontrol/policy/v2/testdata/*_results/** linguist-generated
hscontrol/servertest/testdata/steps/** linguist-generated
hscontrol/testdata/apiv1_golden/** linguist-generated
hscontrol/types/testdata/compat/*.golden.json linguist-generated
hscontrol/config/testdata/compat/*.golden.json linguist-generated
hscontrol/config/testdata/bad/*.golden linguist-generated
+1 -1
View File
@@ -107,7 +107,7 @@ linters:
rules:
# Config key names repeat across the key tables, validation and error
# reports until the config reshape gives each key one home.
- path: hscontrol/types/(config|config_keys|listener)\.go
- path: hscontrol/(types/config|config/(keys|validate))\.go
linters:
- goconst
paths:
+2 -2
View File
@@ -3,5 +3,5 @@ docs/
# Golden fixtures are byte-compared; a formatter must not rewrite them.
hscontrol/testdata/
hscontrol/types/testdata/bad/
hscontrol/types/testdata/compat/
hscontrol/config/testdata/bad/
hscontrol/config/testdata/compat/
+7 -5
View File
@@ -159,11 +159,13 @@ headscale/
Performance-critical.
- `policy/` — `policy/v2/` is **the** policy implementation. The
top-level `policy.go` is thin wrappers. There is no v1 directory.
- `config/` — home for config code outside the `types.Config` struct: the
checks behind `headscale check` (`Files` lists every file a config names,
with its validator) and `ReadPrivateKey`. The loader, defaults,
removed-keys table and `ConfigError` move here next. It imports `types`;
`types`, `derp`, `dns` and `policy` must never import it.
- `config/` — config code outside the `types.Config` struct: the loader
(`Load`: defaults, file, `HEADSCALE_*` environment; `Default`; `Marshal`;
the removed-keys table), `Validate` and `Warnings`, the checks behind
`headscale check` (`Files` lists every file a config names, with its
validator) and `ReadPrivateKey`. The schema and `ConfigError` stay in
`types`. It imports `types`; `types`, `derp`, `dns` and `policy` must
never import it.
- `routes/`, `dns/`, `derp/`, `types/`, `util/`, `templates/`, `capver/`
— routing, MagicDNS, relay, core types, helpers, client templates,
capability versioning.
+19 -9
View File
@@ -1,6 +1,7 @@
package cli
import (
"cmp"
"errors"
"fmt"
"io"
@@ -174,24 +175,33 @@ func fileCheckCmd(use, short, valid string, check func(string) error) *cobra.Com
}
// loadCheckedConfig loads the server config from --file, or finds it as
// serve does, and names where it came from.
// serve does, logs what loading and the config warn about at its log.level
// and in its log.format, and names where it came from.
func loadCheckedConfig(cmd *cobra.Command) (*types.Config, string, error) {
path, _ := cmd.Flags().GetString("file")
err := loadConfig(path)
src := types.ConfigFile()
if src == "" {
src = "(defaults and environment)"
l, err := loadConfig(path)
if err == nil {
// As initConfig does; a config that failed keeps the console
// logger and the default level.
useLogConfig(l.Config, cmd.ErrOrStderr())
}
logWarnings(l.Warnings)
src := cmp.Or(l.File, "(defaults and environment)")
if err != nil {
return nil, src, err
}
err = config.Validate(l.Config)
logWarnings(config.Warnings(l.Config))
if err != nil {
return nil, src, err
}
cfg, err := types.LoadServerConfig()
return cfg, src, err
return l.Config, src, nil
}
// printCheckLine prints one result; an error goes indented below it.
+102
View File
@@ -2,14 +2,19 @@ package cli
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
policyv2 "github.com/juanfont/headscale/hscontrol/policy/v2"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -368,3 +373,100 @@ func TestCheckFileCommands(t *testing.T) {
_, err = runCheck(t, checkDERPMapCmd, writeFile(t, dir, "d.yaml", "{}"))
require.ErrorContains(t, err, "d.yaml")
}
// TestCheckConfigWarnings: the check commands report what the environment
// warns about, with a valid file and with one that fails, and on a valid
// file the configuration's advisories, in its log.format and at its
// log.level.
func TestCheckConfigWarnings(t *testing.T) {
const (
unknown = "unknown environment variable HEADSCALE_BOGUS"
removed = "configuration variable HEADSCALE_DB_TYPE was removed"
advisory = "listen_addr should probably end in :443"
acme = "tls:\n acme:\n hostname: example.com\n challenge_type: TLS-ALPN-01\n"
)
t.Setenv("HEADSCALE_BOGUS", "1")
t.Setenv("HEADSCALE_DB_TYPE", "sqlite")
dir := t.TempDir()
text := writeServerConfig(t, t.TempDir(), acme)
jsonLogs := writeServerConfig(t, t.TempDir(), acme+"log:\n format: json\n")
errorLevel := writeServerConfig(t, t.TempDir(), acme+"log:\n level: error\n")
disabled := writeServerConfig(t, t.TempDir(), acme+"log:\n level: disabled\n")
invalid := writeFile(t, dir, "invalid.yaml", "log:\n format: json\nbogus_key: 1\n")
oldLogger, oldLevel := log.Logger, zerolog.GlobalLevel()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
t.Cleanup(func() {
log.Logger = oldLogger
zerolog.SetGlobalLevel(oldLevel)
})
for _, c := range []*cobra.Command{checkConfigCmd, checkAllCmd} {
for _, tt := range []struct {
name, file string
wantErr bool
// json: the warnings are JSON on the command's stderr, not on
// the logger the command started with.
json bool
// quiet: log.level hides every warning.
quiet bool
}{
{name: "text", file: text},
{name: "json", file: jsonLogs, json: true},
{name: "invalid", file: invalid, wantErr: true},
{name: "level error", file: errorLevel, quiet: true},
{name: "level disabled", file: disabled, quiet: true},
} {
t.Run(c.Name()+" "+tt.name, func(t *testing.T) {
var logged, stderr bytes.Buffer
// As initConfig leaves it for the check commands.
log.Logger = zerolog.New(&logged)
zerolog.SetGlobalLevel(zerolog.InfoLevel)
cmd := &cobra.Command{}
cmd.Flags().String("file", tt.file, "")
cmd.SetOut(io.Discard)
cmd.SetErr(&stderr)
err := c.RunE(cmd, nil)
require.Equal(t, tt.wantErr, err != nil, "%v", err)
out := logged.String()
if tt.quiet {
assert.Empty(t, out)
assert.Empty(t, stderr.String())
return
}
if tt.json {
assert.Empty(t, out)
out = stderr.String()
for l := range strings.SplitSeq(strings.TrimSpace(out), "\n") {
var rec struct{ Level, Message string }
require.NoError(t, json.Unmarshal([]byte(l), &rec), l)
assert.Equal(t, "warn", rec.Level)
}
} else {
assert.Empty(t, stderr.String())
}
assert.Contains(t, out, unknown)
assert.Contains(t, out, removed)
if tt.wantErr {
assert.NotContains(t, out, advisory)
} else {
assert.Contains(t, out, advisory)
}
})
}
}
}
+2 -2
View File
@@ -4,7 +4,7 @@ import (
"fmt"
"os"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/config"
"github.com/spf13/cobra"
)
@@ -17,7 +17,7 @@ var dumpConfigCmd = &cobra.Command{
Short: "dump current config to /etc/headscale/config.dump.yaml, integration test only",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
out, err := types.DumpConfig()
out, err := config.Marshal(loaded.Config, ".yaml")
if err != nil {
return fmt.Errorf("dumping config: %w", err)
}
+1 -5
View File
@@ -10,7 +10,6 @@ import (
"strings"
clientv2 "github.com/juanfont/headscale/gen/client/v2"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/spf13/cobra"
)
@@ -175,10 +174,7 @@ var deleteOAuthClientCmd = &cobra.Command{
// (local trust); a remote address injects the configured API key as a bearer
// token.
func newV2Client() (context.Context, *clientv2.ClientWithResponses, context.CancelFunc, error) {
cfg, err := types.LoadCLIConfig()
if err != nil {
return nil, nil, nil, fmt.Errorf("loading configuration: %w", err)
}
cfg := loaded.Config
ctx, cancel := context.WithTimeout(context.Background(), cfg.CLI.Timeout)
+1 -2
View File
@@ -10,7 +10,6 @@ import (
clientv1 "github.com/juanfont/headscale/gen/client/v1"
"github.com/juanfont/headscale/hscontrol/db"
"github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/spf13/cobra"
)
@@ -23,7 +22,7 @@ var errAborted = errors.New("command aborted by user")
// bypassDatabase opens the database directly, bypassing the running server.
// The caller must close the returned handle.
func bypassDatabase() (*db.HSDatabase, error) {
cfg, err := types.LoadServerConfig()
cfg, err := serverConfig()
if err != nil {
return nil, fmt.Errorf("loading config: %w", err)
}
+53 -25
View File
@@ -1,12 +1,15 @@
package cli
import (
"cmp"
"fmt"
"io"
"os"
"runtime"
"slices"
"strings"
"github.com/juanfont/headscale/hscontrol/config"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@@ -14,7 +17,13 @@ import (
"github.com/tcnksm/go-latest"
)
var cfgFile string = ""
var (
cfgFile string = ""
// loaded is what initConfig loaded; nil for the commands that skip it:
// check, version, mockoidc and completion.
loaded *config.Loaded
)
func init() {
if len(os.Args) > 1 &&
@@ -55,27 +64,35 @@ func skipsInitConfig(args []string) bool {
// loadConfig loads path, else the file -c names, else $HEADSCALE_CONFIG,
// else the first config.* in /etc/headscale, $XDG_CONFIG_HOME/headscale,
// headscale under each $XDG_CONFIG_DIRS entry, ~/.headscale or ".". Without
// a file it loads defaults and environment only.
func loadConfig(path string) error {
for _, p := range []string{path, cfgFile, os.Getenv("HEADSCALE_CONFIG")} {
if p == "" {
continue
}
// a file it loads defaults and environment only. The result is never nil.
func loadConfig(path string) (*config.Loaded, error) {
path = cmp.Or(path, cfgFile, os.Getenv("HEADSCALE_CONFIG"))
err := types.LoadConfig(p, true)
if err != nil {
return fmt.Errorf("loading config file %s: %w", p, err)
}
return nil
l, err := config.Load(path, path != "", os.Environ())
switch {
case err != nil && path != "":
return l, fmt.Errorf("loading config file %s: %w", path, err)
case err != nil:
return l, fmt.Errorf("loading config: %w", err)
}
err := types.LoadConfig("", false)
if err != nil {
return fmt.Errorf("loading config: %w", err)
}
return l, nil
}
return nil
// useLogConfig applies cfg's log.level, and logs to w as JSON when cfg sets
// log.format: json.
func useLogConfig(cfg *types.Config, w io.Writer) {
zerolog.SetGlobalLevel(cfg.Log.Level.Zerolog())
if cfg.Log.Format == types.JSONLogFormat {
log.Logger = log.Output(w)
}
}
func logWarnings(warnings []string) {
for _, w := range warnings {
log.Warn().Msg(w)
}
}
func initConfig() {
@@ -86,12 +103,27 @@ func initConfig() {
return
}
err := loadConfig("")
machineOutput := hasMachineOutputFlag()
l, err := loadConfig("")
if err != nil {
// No log.format applies: main's console logger, on stderr.
logWarnings(l.Warnings)
log.Fatal().Caller().Err(err).Msg("error loading config")
}
machineOutput := hasMachineOutputFlag()
loaded = l
cfg := l.Config
// Machine output owns stdout.
logOut := os.Stdout
if machineOutput {
logOut = os.Stderr
}
useLogConfig(cfg, logOut)
logWarnings(l.Warnings)
// If the user has requested a "node" readable format,
// then disable login so the output remains valid.
@@ -99,11 +131,7 @@ func initConfig() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}
if types.LogFormatSetting() == types.JSONLogFormat {
log.Logger = log.Output(os.Stdout)
}
if !types.UpdateCheckDisabled() && !machineOutput {
if !cfg.DisableUpdateCheck && !machineOutput {
versionInfo := types.GetVersionInfo()
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
!versionInfo.Dirty {
+158 -5
View File
@@ -1,16 +1,24 @@
package cli
import (
"bytes"
"encoding/json"
"fmt"
"maps"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/juanfont/headscale/hscontrol/db"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
const (
@@ -366,9 +374,9 @@ dns:
override_local_dns: false
`, filepath.Join(dir, "headscale.db"))
require.NoError(t, os.WriteFile(configPath, []byte(config), 0o600))
require.NoError(t, types.LoadConfig(configPath, true))
useLoaded(t, configPath)
cfg, err := types.LoadServerConfig()
cfg, err := serverConfig()
require.NoError(t, err)
database, err := db.NewHeadscaleDatabase(cfg)
@@ -414,6 +422,19 @@ dns:
}
}
// useLoaded loads path as initConfig does, until the test ends.
func useLoaded(t *testing.T, path string) {
t.Helper()
l, err := loadConfig(path)
require.NoError(t, err)
old := loaded
loaded = l
t.Cleanup(func() { loaded = old })
}
func TestInitConfigSkippedLogLevel(t *testing.T) {
oldArgs, oldLevel := os.Args, zerolog.GlobalLevel()
@@ -459,8 +480,140 @@ func TestLoadConfig(t *testing.T) {
t.Cleanup(func() { cfgFile = old })
require.NoError(t, loadConfig(tt.path))
require.Equal(t, tt.want, types.ConfigFile())
l, err := loadConfig(tt.path)
require.NoError(t, err)
require.Equal(t, tt.want, l.File)
})
}
}
// TestCLIOnlyConfig: a file with only what client commands read, as the
// NixOS module writes to /etc/headscale/config.yaml, serves them; check
// config reports what the server lacks.
func TestCLIOnlyConfig(t *testing.T) {
path := writeFile(t, t.TempDir(), "config.yaml", `cli:
address: headscale.example.com:443
api_key: abc
unix_socket: /run/headscale/headscale.sock
`)
useLoaded(t, path)
_, _, cancel, err := newHeadscaleCLIWithConfig()
require.NoError(t, err)
cancel()
_, err = runCheck(t, checkConfigCmd, path)
require.ErrorContains(t, err, "server_url")
}
// runHeadscale runs the CLI as main does, in a child process: loading a
// config can exit it.
func runHeadscale(t *testing.T, env []string, args ...string) (string, string, error) {
t.Helper()
cmd := exec.CommandContext(t.Context(), os.Args[0], "-test.run=^TestHeadscaleProcess$") //nolint:gosec // this test binary
cmd.Env = append([]string{"HEADSCALE_TEST_ARGS=" + strings.Join(args, "\n")}, env...)
var stdout, stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
// TestHeadscaleProcess is the child of runHeadscale.
func TestHeadscaleProcess(t *testing.T) {
args := os.Getenv("HEADSCALE_TEST_ARGS")
if args == "" {
t.Skip("run by runHeadscale")
}
os.Args = append([]string{"headscale"}, strings.Split(args, "\n")...)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: true})
zerolog.SetGlobalLevel(zerolog.TraceLevel)
Execute()
os.Exit(0)
}
// TestInitConfigWarnings: what loading warns about follows log.format, never
// reaches stdout under machine output, and is reported with a load error.
func TestInitConfigWarnings(t *testing.T) {
const warning = "unknown environment variable HEADSCALE_BOGUS"
env := []string{"HEADSCALE_BOGUS=1", "HEADSCALE_DISABLE_CHECK_UPDATES=true"}
dir := t.TempDir()
jsonLogs := writeFile(t, dir, "json.yaml", "log:\n format: json\n")
invalid := writeFile(t, dir, "invalid.yaml", "bogus_key: 1\n")
// warnLine is the line holding the warning, as JSON when it is one.
warnLine := func(t *testing.T, out string) map[string]any {
t.Helper()
for l := range strings.SplitSeq(out, "\n") {
if strings.Contains(l, warning) {
var m map[string]any
_ = json.Unmarshal([]byte(l), &m)
return m
}
}
t.Fatalf("no %q in %q", warning, out)
return nil
}
t.Run("json log format", func(t *testing.T) {
stdout, stderr, err := runHeadscale(t, env, "-c", jsonLogs, "generate", "private-key")
require.NoError(t, err, stderr)
assert.Equal(t, "warn", warnLine(t, stdout+stderr)["level"])
})
// Every spelling Cobra accepts for --output.
for _, format := range []string{"json", "json-line", "yaml"} {
for _, flag := range [][]string{
{"-o", format}, {"--output", format}, {"--output=" + format}, {"-o=" + format}, {"-o" + format},
} {
t.Run("machine output "+strings.Join(flag, " "), func(t *testing.T) {
stdout, stderr, err := runHeadscale(t, env, append([]string{"-c", jsonLogs, "generate", "private-key"}, flag...)...)
require.NoError(t, err, stderr)
// JSON is YAML too; the result is one object.
var out map[string]string
require.NoError(t, yaml.Unmarshal([]byte(stdout), &out), stdout)
assert.Equal(t, []string{"private_key"}, slices.Collect(maps.Keys(out)))
assert.Equal(t, "warn", warnLine(t, stderr)["level"])
})
}
}
// version has its own --output, which shadows the root's.
for _, flag := range [][]string{{"-o", "json"}, {"--output=json"}} {
t.Run("version "+strings.Join(flag, " "), func(t *testing.T) {
stdout, stderr, err := runHeadscale(t, env, append([]string{"-c", jsonLogs, "version"}, flag...)...)
require.NoError(t, err, stderr)
var out map[string]any
require.NoError(t, json.Unmarshal([]byte(stdout), &out), stdout)
assert.Contains(t, out, "version")
assert.Equal(t, "warn", warnLine(t, stderr)["level"])
})
}
for _, output := range [][]string{nil, {"-o", "json"}} {
t.Run("load error "+strings.Join(output, " "), func(t *testing.T) {
stdout, stderr, err := runHeadscale(t, env, append([]string{"-c", invalid, "generate", "private-key"}, output...)...)
require.Error(t, err)
assert.Empty(t, stdout)
warnLine(t, stderr)
assert.Contains(t, stderr, "unknown configuration key bogus_key")
assert.NotContains(t, stderr, "panic")
})
}
}
+33 -8
View File
@@ -16,6 +16,7 @@ import (
"github.com/cenkalti/backoff/v5"
clientv1 "github.com/juanfont/headscale/gen/client/v1"
"github.com/juanfont/headscale/hscontrol"
"github.com/juanfont/headscale/hscontrol/config"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/juanfont/headscale/hscontrol/util/zlog/zf"
@@ -87,7 +88,7 @@ func mustMarkRequired(cmd *cobra.Command, names ...string) {
}
func newHeadscaleServerWithConfig() (*hscontrol.Headscale, error) {
cfg, err := types.LoadServerConfig()
cfg, err := serverConfig()
if err != nil {
return nil, fmt.Errorf(
"loading configuration: %w",
@@ -103,6 +104,25 @@ func newHeadscaleServerWithConfig() (*hscontrol.Headscale, error) {
return app, nil
}
// serverConfig is the loaded configuration validated for the server, its
// advisories logged.
func serverConfig() (*types.Config, error) {
cfg := loaded.Config
err := config.Validate(cfg)
logWarnings(config.Warnings(cfg))
if err != nil {
return nil, err
}
// The server never uses the CLI's key, which may be an unread file:
// reference.
cfg.CLI.APIKey = ""
return cfg, nil
}
// clientRunE wraps a [cobra.Command.RunE] func, injecting a ready API client
// and a context whose timeout/cancel the wrapper owns.
func clientRunE(
@@ -141,10 +161,7 @@ func withClient(
// Otherwise it talks to the remote TCP address over HTTPS and injects the
// configured API key as a bearer token.
func newHeadscaleCLIWithConfig() (context.Context, *clientv1.ClientWithResponses, context.CancelFunc, error) {
cfg, err := types.LoadCLIConfig()
if err != nil {
return nil, nil, nil, fmt.Errorf("loading configuration: %w", err)
}
cfg := loaded.Config
log.Debug().
Dur("timeout", cfg.CLI.Timeout).
@@ -408,8 +425,16 @@ func printError(err error, outputFormat string) {
fmt.Fprintf(os.Stderr, "%s\n", out)
}
// hasMachineOutputFlag reports whether --output, as Cobra parsed it in any
// spelling for the command being run, asks for a machine-readable format.
// The command's own flags hold it: version shadows the root's --output.
func hasMachineOutputFlag() bool {
return slices.ContainsFunc(os.Args, func(arg string) bool {
return arg == outputFormatJSON || arg == outputFormatJSONLine || arg == outputFormatYAML
})
cmd, _, err := rootCmd.Find(os.Args[1:])
if err != nil {
return false
}
output, _ := cmd.Flags().GetString("output")
return slices.Contains([]string{outputFormatJSON, outputFormatJSONLine, outputFormatYAML}, output)
}
+15 -7
View File
@@ -5,7 +5,6 @@ import (
"path/filepath"
"testing"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -68,17 +67,15 @@ func TestCLIAPIKey(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfgDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(cfgDir, "config.yaml"),
cfgPath := filepath.Join(cfgDir, "config.yaml")
require.NoError(t, os.WriteFile(cfgPath,
[]byte("unix_socket: "+filepath.Join(cfgDir, "headscale.sock")+"\n"), 0o600))
t.Setenv("HEADSCALE_CLI_ADDRESS", tt.address)
t.Setenv("HEADSCALE_CLI_API_KEY", tt.apiKey)
require.NoError(t, types.LoadConfig(cfgDir, false))
useLoaded(t, cfgPath)
if tt.address != "" && tt.wantErr == "" {
cfg, err := types.LoadCLIConfig()
require.NoError(t, err)
got, err := cliAPIKey(cfg.CLI)
got, err := cliAPIKey(loaded.Config.CLI)
require.NoError(t, err)
assert.Equal(t, tt.want, got)
}
@@ -101,3 +98,14 @@ func TestCLIAPIKey(t *testing.T) {
})
}
}
// TestServerConfigDropsCLIKey: the server never holds the CLI's key, which
// may be a file only the CLI can read.
func TestServerConfigDropsCLIKey(t *testing.T) {
dir := t.TempDir()
useLoaded(t, writeServerConfig(t, dir, "cli:\n api_key: file:"+filepath.Join(dir, "missing")+"\n"))
cfg, err := serverConfig()
require.NoError(t, err)
assert.Empty(t, cfg.CLI.APIKey)
}
+7 -7
View File
@@ -5,6 +5,7 @@ import (
"path/filepath"
"testing"
"github.com/juanfont/headscale/hscontrol/config"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -26,10 +27,10 @@ func TestConfigFileLoading(t *testing.T) {
require.NoError(t, err)
// Load example config, it should load without validation errors
err = types.LoadConfig(cfgFile, true)
l, err := config.Load(cfgFile, true, os.Environ())
require.NoError(t, err)
assertExampleConfig(t)
assertExampleConfig(t, l.Config)
}
func TestConfigLoading(t *testing.T) {
@@ -46,19 +47,18 @@ func TestConfigLoading(t *testing.T) {
require.NoError(t, err)
// Load example config, it should load without validation errors
err = types.LoadConfig(tmpDir, false)
l, err := config.Load(tmpDir, false, os.Environ())
require.NoError(t, err)
assertExampleConfig(t)
assertExampleConfig(t, l.Config)
}
// assertExampleConfig checks the loaded config-example.yaml was interpreted
// correctly.
func assertExampleConfig(t *testing.T) {
func assertExampleConfig(t *testing.T, cfg *types.Config) {
t.Helper()
cfg, err := types.LoadServerConfig()
require.NoError(t, err)
require.NoError(t, config.Validate(cfg))
assert.Equal(t, "http://127.0.0.1:8080", cfg.ServerURL)
assert.Equal(t, types.ListenAddr("127.0.0.1:8080"), cfg.Addr)
+1 -1
View File
@@ -185,7 +185,7 @@
];
extraSrc = [
./hscontrol/testdata
./hscontrol/types/testdata
./hscontrol/config/testdata
./hscontrol/db/testdata
./hscontrol/policy/v2/testdata
];
+6 -2
View File
@@ -29,7 +29,6 @@ import (
"github.com/caddyserver/certmagic"
"github.com/cenkalti/backoff/v5"
"github.com/davecgh/go-spew/spew"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/metrics"
@@ -577,7 +576,12 @@ func (h *Headscale) Serve() error {
}
if dumpConfig {
spew.Dump(h.cfg)
out, err := config.Marshal(h.cfg, ".yaml")
if err != nil {
return fmt.Errorf("dumping config: %w", err)
}
_, _ = os.Stdout.Write(out)
}
versionInfo := types.GetVersionInfo()
+25
View File
@@ -86,6 +86,31 @@ func TestDebugHostGuard(t *testing.T) {
}
}
// TestDebugConfigRedacts: /debug/config is the configuration as
// config.Marshal writes it, without credentials; the running values keep
// theirs.
func TestDebugConfigRedacts(t *testing.T) {
app := createTestApp(t)
app.cfg.ServerURL = "https://marker-user:marker-pass@localhost:8080"
app.cfg.CLI.Address = "https://marker-cli@cli.example.com"
app.cfg.DNSConfig.Nameservers.Global = []string{"1.1.1.1", "https://marker-dns-user:marker-dns-pass@dns.example.com/q"}
app.cfg.DNSConfig.Nameservers.Split = map[string][]string{"corp.example.com": {"https://marker-split@dns.example.com/q"}}
app.cfg.Database.Postgres.Pass = "marker-pg-pass"
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/debug/config", nil)
req.Host = "localhost"
req.RemoteAddr = "127.0.0.1:1234"
rec := httptest.NewRecorder()
app.debugHTTPServer().Handler.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "application/json", rec.Header().Get("Content-Type"))
assert.NotContains(t, rec.Body.String(), "marker")
assert.Contains(t, rec.Body.String(), `"server_url":"https://localhost:8080"`)
assert.Contains(t, rec.Body.String(), `"global":["1.1.1.1","https://dns.example.com/q"]`)
assert.Contains(t, app.cfg.ServerURL, "marker-pass")
}
// TestNewHeadscaleDNSProvider pins that startup builds the provider
// dns.challenge.provider names, reading its secrets, unless Go set one, and
// refuses one that cannot keep node certificates apart.
+73
View File
@@ -0,0 +1,73 @@
package config
import (
"time"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/juanfont/headscale/hscontrol/types"
)
// Default is the configuration before a file or variable sets
// anything. database.type has no default.
func Default() types.Config {
return types.Config{
UnixSocket: "/var/run/headscale/headscale.sock",
UnixSocketPermission: 0o770,
Prefixes: types.PrefixesConfig{Allocation: types.IPAllocationStrategySequential},
Policy: types.PolicyConfig{Mode: types.PolicyModeFile},
Log: types.LogConfig{Level: "info", Format: types.TextLogFormat},
Node: types.NodeConfig{
Ephemeral: types.EphemeralConfig{InactivityTimeout: 120 * time.Second},
Routes: types.RouteConfig{HA: types.HARouteConfig{
ProbeInterval: 10 * time.Second,
ProbeTimeout: 5 * time.Second,
}},
},
PreAuthKeys: types.PreAuthKeysConfig{RevokedRetention: 168 * time.Hour},
DERP: types.DERPConfig{
Server: types.DERPServerConfig{
VerifyClients: true,
AutomaticallyAddEmbeddedDerpRegion: true,
},
UpdateFrequency: 3 * time.Hour,
},
Database: types.DatabaseConfig{
Sqlite: types.SqliteConfig{
WriteAheadLog: true,
WALAutoCheckPoint: 1000, // SQLite default
},
Postgres: types.PostgresConfig{
Ssl: "false",
MaxOpenConnections: 10,
MaxIdleConnections: 10,
ConnMaxIdleTimeSecs: 3600,
},
},
DNSConfig: types.DNSConfig{
MagicDNS: true,
OverrideLocalDNS: true,
Nameservers: types.Nameservers{Global: []string{}, Split: map[string][]string{}},
SearchDomains: []string{},
Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute},
},
TLS: types.TLSConfig{ACME: types.ACMEConfig{
ChallengeType: types.HTTP01ChallengeType,
HTTPListen: ":http",
CacheDir: "/var/lib/headscale/cache",
}},
OIDC: types.OIDCConfig{
OnlyStartIfOIDCIsAvailable: true,
Scope: []string{oidc.ScopeOpenID, "profile", "email"},
EmailVerifiedRequired: true,
PKCE: types.PKCEConfig{Method: types.PKCEMethodS256},
},
Taildrop: types.TaildropConfig{Enabled: true},
CLI: types.CLIConfig{Timeout: 5 * time.Second},
Tuning: types.Tuning{
BatchChangeDelay: 800 * time.Millisecond,
NodeMapSessionBufferedChanSize: 30,
NodeStoreBatchSize: 100,
NodeStoreBatchTimeout: 500 * time.Millisecond,
},
}
}
+22
View File
@@ -0,0 +1,22 @@
package config
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
// TestValidateHintsNameDefaults: the defaults Validate's hints quote, which
// types cannot read from Default, are Default's.
func TestValidateHintsNameDefaults(t *testing.T) {
d := Default()
cfg := Default()
cfg.Tuning.NodeStoreBatchSize = 0
cfg.Tuning.NodeStoreBatchTimeout = 0
err := Validate(&cfg)
require.ErrorContains(t, err, fmt.Sprintf("(default: %d)", d.Tuning.NodeStoreBatchSize))
require.ErrorContains(t, err, fmt.Sprintf("(default: %s)", d.Tuning.NodeStoreBatchTimeout))
}
+3 -2
View File
@@ -1,5 +1,6 @@
// Package config checks headscale's configuration and the files it names.
// It imports types; types, derp, dns and policy must never import it.
// Package config loads and validates headscale's configuration and checks
// the files it names. It imports types; types, derp, dns and policy must
// never import it.
package config
import (
@@ -1,4 +1,4 @@
package types
package config
import (
"slices"
@@ -1,4 +1,4 @@
package types
package config
import (
"strings"
@@ -74,3 +74,17 @@ func TestDottedPosition(t *testing.T) {
})
}
}
// TestRemovedKeysAgainstSchema: a removed key is not a key, and what
// replaces it is.
func TestRemovedKeysAgainstSchema(t *testing.T) {
leaf, branch := configKeySets()
for _, r := range removedKeys {
assert.Falsef(t, leaf[r.Old] || branch[r.Old], "removed key %s is a key", r.Old)
for k := range strings.FieldsSeq(r.New) {
assert.Truef(t, leaf[k] || branch[k], "%s replaces %s but is no key", k, r.Old)
}
}
}
@@ -1,4 +1,4 @@
package types
package config
import (
"bytes"
@@ -20,13 +20,13 @@ import (
"github.com/go-viper/mapstructure/v2"
hsdns "github.com/juanfont/headscale/hscontrol/dns"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/v2"
"github.com/prometheus/common/model"
"github.com/rs/zerolog/log"
"github.com/tailscale/hujson"
)
@@ -54,7 +54,7 @@ var reservedEnvNames = []string{
// configSource is the layered configuration: defaults, then file, then
// environment, with lowercase keys. [configSource.decode] turns it into a
// [Config].
// [types.Config].
type configSource struct {
k *koanf.Koanf
file string
@@ -198,7 +198,7 @@ type schemaKey struct {
}
func schemaKeys() []schemaKey {
fields := ConfigFields(&Config{})
fields := types.ConfigFields(&types.Config{})
keys := make([]schemaKey, len(fields))
for i, f := range fields {
@@ -306,34 +306,60 @@ func joinConfigPath(prefix, name string) string {
return prefix + "." + name
}
// Loaded is a configuration as [Load] found it.
type Loaded struct {
// Config is nil when loading failed.
Config *types.Config
// File is the file loaded or failed to load, or "" when none was found.
File string
// Warnings are findings that do not stop loading, kept when it fails.
Warnings []string
}
// Load loads defaults, the configuration file found via path and isFile,
// and the HEADSCALE_* variables of environ (os.Environ form), rejecting
// removed and unknown file keys, and decodes them. It does not validate:
// see [Validate].
func Load(path string, isFile bool, environ []string) (*Loaded, error) {
s, err := newConfigSource(path, isFile, environ)
l := &Loaded{File: s.file, Warnings: s.warnings}
if err != nil {
return l, err
}
l.Config, err = s.decode()
return l, err
}
// newConfigSource loads defaults, the config file found via path/isFile and
// environ (os.Environ form), and rejects removed and unknown file keys.
// environ (os.Environ form), and rejects removed and unknown file keys. The
// source is returned with what it found even on error.
func newConfigSource(path string, isFile bool, environ []string) (*configSource, error) {
s := &configSource{k: koanf.New("."), envVars: map[string]string{}}
s.file = findConfigFile(path, isFile, environ)
d := defaultConfig()
d := Default()
err := s.k.Load(confmap.Provider(plainMap(&d, false), ""), nil)
if err != nil {
return nil, fmt.Errorf("loading defaults: %w", err)
return s, fmt.Errorf("loading defaults: %w", err)
}
s.file = findConfigFile(path, isFile, environ)
var fileErr error
if s.file == "" {
log.Warn().Msg("no config file found, using defaults")
s.warnings = append(s.warnings, "no config file found, using defaults")
} else {
err = s.loadFile()
if err != nil {
return nil, err
}
fileErr = s.loadFile()
}
err = s.loadEnv(environ)
if err != nil {
return nil, err
}
// Read even when the file fails, so its variables' warnings are reported
// with the file's error.
envErr := s.loadEnv(environ)
return s, nil
return s, cmp.Or(fileErr, envErr)
}
func (s *configSource) loadFile() error {
@@ -372,11 +398,11 @@ func (s *configSource) loadFile() error {
m = normaliseConfigMap(m)
// Checked before expansion, which would otherwise discard shadowed keys.
v := &configValidator{}
s.checkKeys(v, m)
var errs []error
s.checkKeys(&errs, m)
if v.HasErrors() {
return v.Err()
if len(errs) > 0 {
return errors.Join(errs...)
}
m = expandDottedKeys(m, "")
@@ -516,7 +542,7 @@ func (r removedKey) reason(what, name string) string {
}
// checkKeys adds an error for every removed or unknown key in the file.
func (s *configSource) checkKeys(v *configValidator, m map[string]any) {
func (s *configSource) checkKeys(errs *[]error, m map[string]any) {
leaf, branch := configKeySets()
var walk func(prefix string, m map[string]any)
@@ -533,7 +559,7 @@ func (s *configSource) checkKeys(v *configValidator, m map[string]any) {
switch r, removed := removedConfigKey(path); {
case removed:
v.Add(&ConfigError{
*errs = append(*errs, &types.ConfigError{
Reason: r.reason("key", path) + s.position(path),
Hint: r.hint(),
})
@@ -549,7 +575,7 @@ func (s *configSource) checkKeys(v *configValidator, m map[string]any) {
}
walkRemoved(root, below, false, func(p string, r removedKey) {
v.Add(&ConfigError{Reason: r.reason("key", p) + s.position(p), Hint: r.hint()})
*errs = append(*errs, &types.ConfigError{Reason: r.reason("key", p) + s.position(p), Hint: r.hint()})
})
case leaf[path] && !isMap:
case (leaf[path] || branch[path]) && isMap:
@@ -557,17 +583,17 @@ func (s *configSource) checkKeys(v *configValidator, m map[string]any) {
walk(path, child)
case branch[path]:
// The value is not shown: a misplaced block may hold secrets.
v.Add(&ConfigError{
*errs = append(*errs, &types.ConfigError{
Reason: fmt.Sprintf("configuration key %s must be a mapping%s", path, s.position(path)),
})
default:
v.Add(&ConfigError{Reason: fmt.Sprintf("unknown configuration key %s%s", path, s.position(path))})
*errs = append(*errs, &types.ConfigError{Reason: fmt.Sprintf("unknown configuration key %s%s", path, s.position(path))})
}
}
}
walk("", m)
s.checkOverlaps(v, m)
s.checkOverlaps(errs, m)
}
// position renders " (file:line:col)" when the key can be located, written
@@ -700,7 +726,7 @@ func (s *configSource) foldProviderEnv(env map[string]string) error {
fold("name")
for _, setting := range hsdns.SettingNames(DNSChallengeConfig{ProviderSettings: settings}.ProviderName()) {
for _, setting := range hsdns.SettingNames(types.DNSChallengeConfig{ProviderSettings: settings}.ProviderName()) {
fold(setting)
}
@@ -739,12 +765,6 @@ func removedEnv(name string) (removedKey, bool) {
return removedKey{}, false
}
// File returns the config file in use, or "" when only defaults apply.
func (s *configSource) File() string { return s.file }
// Warnings returns non-fatal findings from loading.
func (s *configSource) Warnings() []string { return s.warnings }
// keyEntry is one key of the file as written, for finding paths that the file
// spells in more than one form.
type keyEntry struct {
@@ -761,7 +781,7 @@ func (e keyEntry) spelling() string { return strings.Join(e.names, " > ") }
// checkOverlaps rejects a path given both dotted and nested ("log.level" next
// to "log: {level: ...}"): no rule says which of the two should win.
func (s *configSource) checkOverlaps(v *configValidator, m map[string]any) {
func (s *configSource) checkOverlaps(errs *[]error, m map[string]any) {
var entries []keyEntry
var collect func(prefix string, parent int, names []string, m map[string]any)
@@ -823,7 +843,7 @@ func (s *configSource) checkOverlaps(v *configValidator, m map[string]any) {
a, b = b, a
}
v.Add(&ConfigError{
*errs = append(*errs, &types.ConfigError{
Reason: fmt.Sprintf("configuration key %s is set both as %q%s and as %q%s",
a.path, a.spelling(), s.positionOf(a.names), b.spelling(), s.positionOf(b.names)),
Hint: "use one form, either the dotted key or nested mappings",
@@ -844,8 +864,8 @@ func (s *configSource) positionOf(names []string) string {
// decode decodes the layers into a Config. Every value that does not fit
// its key is reported, in one error.
func (s *configSource) decode() (*Config, error) {
var cfg Config
func (s *configSource) decode() (*types.Config, error) {
var cfg types.Config
err := s.k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{
Tag: "koanf",
@@ -890,10 +910,11 @@ func (s *configSource) configDir() string {
// decodeErrors turns each value mapstructure rejected into a ConfigError
// naming its key and where it was set.
func (s *configSource) decodeErrors(err error) error {
v := &configValidator{}
var errs []error
seen := map[string]bool{}
walkErrTree(err, func(e error) {
util.WalkErrTree(err, func(e error) {
de, ok := e.(*mapstructure.DecodeError) //nolint:errorlint // every node is visited
if !ok {
return
@@ -902,9 +923,9 @@ func (s *configSource) decodeErrors(err error) error {
// An element or entry is reported on its key.
key, _, _ := strings.Cut(de.Name(), "[")
var leaf *ConfigError
var leaf *types.ConfigError
if !errors.As(de.Unwrap(), &leaf) {
leaf = &ConfigError{Reason: de.Unwrap().Error(), Cause: de.Unwrap()}
leaf = &types.ConfigError{Reason: de.Unwrap().Error(), Cause: de.Unwrap()}
// The decoder's own error keeps the value (UnconvertibleTypeError's
// Value); its text does not.
@@ -917,20 +938,20 @@ func (s *configSource) decodeErrors(err error) error {
ce.Reason = key + " " + leaf.Reason + s.origin(key)
if !hiddenValue(key) {
ce.Current = []KV{{key, s.k.Get(key)}}
ce.Current = []types.KV{{Key: key, Value: s.k.Get(key)}}
}
if !seen[ce.Reason] {
seen[ce.Reason] = true
v.Add(&ce)
errs = append(errs, &ce)
}
})
if !v.HasErrors() {
if len(errs) == 0 {
return err
}
return v.Err()
return errors.Join(errs...)
}
// origin renders where key's value came from: its variable, else its
@@ -950,26 +971,27 @@ func (s *configSource) origin(key string) string {
// hiddenValue reports whether key's value may be a credential, which errors
// do not show: a secret, a URL (its userinfo) or a DNS provider setting.
func hiddenValue(key string) bool {
if key == providerKey || strings.HasPrefix(key, providerKey+".") || slices.Contains(URLKeys, key) {
if key == providerKey || strings.HasPrefix(key, providerKey+".") || slices.Contains(types.URLKeys, key) {
return true
}
for _, f := range ConfigFields(&Config{}) {
for _, f := range types.ConfigFields(&types.Config{}) {
if f.Path != key {
continue
}
return f.Field.Type == reflect.TypeFor[Secret]()
return f.Field.Type == reflect.TypeFor[types.Secret]()
}
return false
}
var (
durationType = reflect.TypeFor[time.Duration]()
fileModeType = reflect.TypeFor[FileMode]()
prefixType = reflect.TypeFor[netip.Prefix]()
prefixPtrType = reflect.TypeFor[*netip.Prefix]()
textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]()
durationType = reflect.TypeFor[time.Duration]()
fileModeType = reflect.TypeFor[types.FileMode]()
prefixType = reflect.TypeFor[netip.Prefix]()
prefixPtrType = reflect.TypeFor[*netip.Prefix]()
)
// scalarHook converts a value as written into what its field's type takes,
@@ -985,7 +1007,7 @@ func scalarHook(from, to reflect.Value) (any, error) {
case t == fileModeType && !isString:
// YAML reads an unquoted 0770 as the integer 504, which cannot be
// told apart from a literal 504.
return nil, &ConfigError{
return nil, &types.ConfigError{
Reason: "must be quoted",
Maximum: `"0777"`,
Hint: `quote the value, e.g. "0770"; unquoted, YAML reads 0770 as the number 504`,
@@ -997,7 +1019,7 @@ func scalarHook(from, to reflect.Value) (any, error) {
case t == prefixType && isString:
_, err := netip.ParsePrefix(s)
if err != nil {
return nil, &ConfigError{
return nil, &types.ConfigError{
Reason: "is not a valid CIDR",
Detail: err.Error(),
Hint: `use CIDR form, e.g. "100.64.0.0/10"`,
@@ -1005,7 +1027,7 @@ func scalarHook(from, to reflect.Value) (any, error) {
}
}
case t.Kind() == reflect.String:
if text, ok := scalarText(from); ok {
if text, ok := util.ScalarText(from); ok {
return text, nil
}
case t.Kind() == reflect.Bool:
@@ -1021,27 +1043,11 @@ func scalarHook(from, to reflect.Value) (any, error) {
return data, nil
}
// scalarText is a number or bool as text.
func scalarText(v reflect.Value) (string, bool) {
switch {
case v.Kind() == reflect.Bool:
return strconv.FormatBool(v.Bool()), true
case v.CanInt():
return strconv.FormatInt(v.Int(), 10), true
case v.CanUint():
return strconv.FormatUint(v.Uint(), 10), true
case v.CanFloat():
return strconv.FormatFloat(v.Float(), 'f', -1, 64), true
}
return "", false
}
func toDuration(v reflect.Value) (any, error) {
unit := &ConfigError{Reason: "needs a unit", Hint: `write the unit, e.g. "30s", "5m" or "90d"`}
unit := &types.ConfigError{Reason: "needs a unit", Hint: `write the unit, e.g. "30s", "5m" or "90d"`}
if v.Kind() != reflect.String {
if _, ok := scalarText(v); !ok || v.Kind() == reflect.Bool {
if _, ok := util.ScalarText(v); !ok || v.Kind() == reflect.Bool {
return v.Interface(), nil
}
@@ -1069,20 +1075,20 @@ func toDuration(v reflect.Value) (any, error) {
return nil, unit
}
return nil, &ConfigError{Reason: "is not a duration", Hint: unit.Hint}
return nil, &types.ConfigError{Reason: "is not a duration", Hint: unit.Hint}
}
func toBool(v reflect.Value) (any, error) {
text, ok := reflect.TypeAssert[string](v)
if !ok {
if text, ok = scalarText(v); !ok || v.Kind() == reflect.Bool {
if text, ok = util.ScalarText(v); !ok || v.Kind() == reflect.Bool {
return v.Interface(), nil
}
}
b, err := strconv.ParseBool(text)
if err != nil {
return nil, &ConfigError{Reason: "is not a boolean", Hint: "use true or false", Cause: err}
return nil, &types.ConfigError{Reason: "is not a boolean", Hint: "use true or false", Cause: err}
}
return b, nil
@@ -1099,7 +1105,7 @@ func toInt(v reflect.Value, t reflect.Type) (any, error) {
case v.Kind() == reflect.String:
i, err := strconv.ParseInt(v.String(), 10, 64)
if errors.Is(err, strconv.ErrRange) {
return nil, &ConfigError{Reason: "is out of range"}
return nil, &types.ConfigError{Reason: "is out of range"}
}
n, whole = i, err == nil
@@ -1107,14 +1113,14 @@ func toInt(v reflect.Value, t reflect.Type) (any, error) {
n, whole = v.Int(), true
case v.CanUint():
if v.Uint() > math.MaxInt64 {
return nil, &ConfigError{Reason: "is out of range"}
return nil, &types.ConfigError{Reason: "is out of range"}
}
n, whole = int64(v.Uint()), true //nolint:gosec // bounded above
case v.CanFloat():
f := v.Float()
if f < math.MinInt64 || f >= math.MaxInt64 {
return nil, &ConfigError{Reason: "is out of range"}
return nil, &types.ConfigError{Reason: "is out of range"}
}
n, whole = int64(f), f == math.Trunc(f)
@@ -1123,11 +1129,11 @@ func toInt(v reflect.Value, t reflect.Type) (any, error) {
}
if !whole {
return nil, &ConfigError{Reason: "is not an integer"}
return nil, &types.ConfigError{Reason: "is not an integer"}
}
if reflect.Zero(t).OverflowInt(n) {
return nil, &ConfigError{Reason: "is out of range"}
return nil, &types.ConfigError{Reason: "is out of range"}
}
return n, nil
@@ -1158,7 +1164,7 @@ func toList(s string, elem reflect.Type) (any, error) {
hint = `write the list as JSON, e.g. [{"name": "a.example.com", "type": "A", "value": "100.64.0.1"}]`
}
return nil, &ConfigError{Reason: "is not a JSON list", Hint: hint, Cause: err}
return nil, &types.ConfigError{Reason: "is not a JSON list", Hint: hint, Cause: err}
}
return list, nil
@@ -1169,7 +1175,7 @@ func toObject(s string) (any, error) {
err := stdjson.Unmarshal([]byte(s), &m)
if err != nil || m == nil {
return nil, &ConfigError{
return nil, &types.ConfigError{
Reason: "is not a JSON object",
Hint: `write the map as JSON, e.g. {"example.com": ["1.1.1.1"]}`,
Cause: err,
@@ -1184,7 +1190,7 @@ func toObject(s string) (any, error) {
// shows only a file: reference, a URL no userinfo, and every DNS provider
// setting but a scalar name and file: references is hidden: any of them, a
// number included, may be a credential.
func plainMap(cfg *Config, redact bool) map[string]any {
func plainMap(cfg *types.Config, redact bool) map[string]any {
out := map[string]any{}
if redact {
@@ -1193,7 +1199,7 @@ func plainMap(cfg *Config, redact bool) map[string]any {
cfg = &r
}
for _, f := range ConfigFields(cfg) {
for _, f := range types.ConfigFields(cfg) {
v := plainValue(f.Value, redact)
switch settings, isMap := v.(map[string]any); {
@@ -1244,13 +1250,13 @@ func plainValue(v reflect.Value, redact bool) any {
switch x := v.Interface().(type) {
case time.Duration:
return x.String()
case Secret:
case types.Secret:
if redact {
return x.Redacted()
}
return string(x)
case HTTPURL:
case types.HTTPURL:
// As written: its JSON form drops userinfo.
if redact {
return x.Redacted()
@@ -1325,11 +1331,11 @@ func redactProvider(settings map[string]any) map[string]any {
out[k] = redactLeaves(v)
}
// A name that is a map or list stays hidden: it is no name.
if _, ok := (DNSChallengeConfig{ProviderSettings: settings}).providerName(); ok {
if name, set := settings["name"]; set {
out["name"] = name
}
// A name that is a map or list stays hidden: it is no name, and its
// ProviderName is "", as an empty name's is.
name, set := settings["name"]
if set && (name == nil || name == "" || (types.DNSChallengeConfig{ProviderSettings: settings}).ProviderName() != "") {
out["name"] = name
}
return out
File diff suppressed because it is too large. Load diff
+18
View File
@@ -0,0 +1,18 @@
package config
import (
"github.com/juanfont/headscale/hscontrol/types"
)
// Marshal renders cfg in the file format of ext: ".yaml", ".yml", ".json"
// or ".hujson", keyed as a file is, with secrets redacted: a secret shows
// only a file: reference, a URL no userinfo, a DNS provider only its name
// and file: references.
func Marshal(cfg *types.Config, ext string) ([]byte, error) {
parser, err := configParser(ext)
if err != nil {
return nil, err
}
return parser.Marshal(plainMap(cfg, true))
}
+157
View File
@@ -0,0 +1,157 @@
package config
import (
"cmp"
"context"
"maps"
"testing"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/libdns/libdns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"tailscale.com/tailcfg"
)
// fileFormats are the formats a configuration can be written in.
var fileFormats = []string{".yaml", ".json", ".hujson"}
// fakeProvider is a Go-only DNS provider.
type fakeProvider struct{ Token string }
func (fakeProvider) AppendRecords(context.Context, string, []libdns.Record) ([]libdns.Record, error) {
return nil, nil
}
func (fakeProvider) DeleteRecords(context.Context, string, []libdns.Record) ([]libdns.Record, error) {
return nil, nil
}
// TestMarshalRedacts: what Marshal prints holds no credential, in any
// format: a literal secret, a DNS provider setting of any shape, URL
// userinfo, in URL and plain string keys alike, and nothing set only from
// Go. The running values keep theirs.
func TestMarshalRedacts(t *testing.T) {
tests := []struct {
name string
provider map[string]any
hidden []string
shown []string
}{
{
name: "string token",
provider: map[string]any{"name": "cloudflare", "api_token": "marker-api-token"},
hidden: []string{"marker-api-token"},
shown: []string{"cloudflare"},
},
{
name: "numeric token",
provider: map[string]any{"name": "cloudflare", "api_token": 123456},
hidden: []string{"123456"},
shown: []string{"cloudflare"},
},
{
name: "nested registration",
provider: map[string]any{"name": "acmedns", "config": map[string]any{
"laptop.example.com": map[string]any{
"username": "marker-user",
"password": "marker-registration-password",
"subdomain": "marker-subdomain",
"server_url": "https://auth.acme-dns.io",
},
}},
hidden: []string{"marker-user", "marker-registration-password", "marker-subdomain"},
shown: []string{"acmedns"},
},
{
name: "file reference",
provider: map[string]any{"name": "cloudflare", "api_token": "file:/run/credentials/cloudflare"}, //nolint:gosec // a file reference, not a credential
shown: []string{"cloudflare", "file:/run/credentials/cloudflare"},
},
}
for _, tt := range tests {
for _, ext := range fileFormats {
t.Run(tt.name+ext, func(t *testing.T) {
cfg := Default()
cfg.ServerURL = "https://marker-server-user:marker-server-pass@headscale.example.com" //nolint:gosec // test markers, not credentials
cfg.CLI.Address = "https://marker-cli-user@cli.example.com"
cfg.DNSConfig.Nameservers.Global = []string{"1.1.1.1", "https://marker-dns-user:marker-dns-pass@dns.example.com/dns-query"}
cfg.DNSConfig.Nameservers.Split = map[string][]string{"corp.example.com": {"https://marker-split-user@dns.example.com/q"}}
cfg.OIDC.ClientSecret = "marker-oidc-secret"
cfg.OIDC.Issuer = "https://marker-issuer-user:marker-issuer-pass@idp.example.com"
cfg.Database.Postgres.Pass = "marker-pg-pass"
cfg.CLI.APIKey = "file:/run/credentials/cli_key"
cfg.TLS.ACME.URL = "https://marker-acme-user@acme.example.com/dir"
cfg.DERP.URLs = []types.HTTPURL{"https://marker-derp-user:marker-derp-pass@derp.example.com/map"}
cfg.DERP.DERPMap = &tailcfg.DERPMap{Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{
900: {RegionID: 900, RegionName: "marker-derpmap"},
}}
cfg.DNSConfig.Challenge.ProviderSettings = maps.Clone(tt.provider)
cfg.DNSConfig.Challenge.Provider = fakeProvider{Token: "marker-go-provider"} //nolint:gosec // a test marker, not a credential
out, err := Marshal(&cfg, ext)
require.NoError(t, err)
for _, h := range append([]string{
"marker-oidc-secret", "marker-issuer-user", "marker-issuer-pass", "marker-pg-pass",
"marker-acme-user", "marker-derp-user", "marker-derp-pass",
"marker-derpmap", "marker-go-provider",
"marker-server-user", "marker-server-pass", "marker-cli-user",
"marker-dns-user", "marker-dns-pass", "marker-split-user",
}, tt.hidden...) {
assert.NotContains(t, string(out), h)
}
for _, s := range append([]string{
"file:/run/credentials/cli_key", "idp.example.com", "derp.example.com",
"https://headscale.example.com", "https://cli.example.com", "1.1.1.1",
"https://dns.example.com/dns-query", "https://dns.example.com/q",
}, tt.shown...) {
assert.Contains(t, string(out), s)
}
assert.Contains(t, cfg.ServerURL, "marker-server-pass")
assert.Contains(t, cfg.CLI.Address, "marker-cli-user")
assert.Contains(t, cfg.DNSConfig.Nameservers.Global[1], "marker-dns-pass")
assert.Contains(t, cfg.DNSConfig.Nameservers.Split["corp.example.com"][0], "marker-split-user")
})
}
}
}
// TestMarshalRedactsServerURL: server_url is a string, not an HTTPURL, but
// its userinfo is as secret; a value whose userinfo cannot be told from the
// rest is withheld whole. The raw map, as the round trip writes it, keeps
// the value.
func TestMarshalRedactsServerURL(t *testing.T) {
for _, tt := range []struct {
serverURL string
shown string // "" when withheld
}{
{serverURL: "https://headscale.example.com", shown: "https://headscale.example.com"},
{serverURL: "https://marker-user:marker-pass@headscale.example.com", shown: "https://headscale.example.com"}, //nolint:gosec // test markers, not credentials
{serverURL: "https://marker-user@headscale.example.com", shown: "https://headscale.example.com"},
{serverURL: "https://marker-user:marker-pass@headscale.example.com/%zz"}, //nolint:gosec // test markers, not credentials
{serverURL: "https://alice:marker-pass?@headscale.example.com"},
{serverURL: "https://alice:marker-pass#@headscale.example.com"},
{serverURL: "https://alice:marker-pass@headscale.example.com:port"},
{serverURL: "https:/alice:marker-pass@headscale.example.com"},
} {
for _, ext := range fileFormats {
t.Run(tt.serverURL+ext, func(t *testing.T) {
cfg := Default()
cfg.ServerURL = tt.serverURL
out, err := Marshal(&cfg, ext)
require.NoError(t, err)
assert.NotContains(t, string(out), "marker")
assert.Contains(t, string(out), tt.shown)
assert.Equal(t, cmp.Or(tt.shown, "<redacted>"), plainMap(&cfg, true)["server_url"])
assert.Equal(t, tt.serverURL, plainMap(&cfg, false)["server_url"])
assert.Equal(t, tt.serverURL, cfg.ServerURL)
})
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loaded 100 of 155 files, more files were not shown because too many files have changed in this diff. Show more