From c7d097d65e87dd21137516f718afeed95b544d15 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sun, 8 Nov 2026 22:55:29 +0000 Subject: [PATCH] config: log a missing config file only for serve The environment alone is a complete configuration and client commands need a file only for an API key, so the loader stops warning; serve logs at info level that it runs on defaults and the environment. Updates #1926 --- cmd/headscale/cli/root_test.go | 17 ++++++ cmd/headscale/cli/serve.go | 12 +++++ cmd/headscale/cli/serve_test.go | 52 +++++++++++++++++++ hscontrol/config/load.go | 7 ++- hscontrol/config/load_test.go | 10 ++-- .../testdata/compat/env-cli.golden.json | 5 +- .../compat/env-extra-records.golden.json | 5 +- .../compat/env-legacy-ignored.golden.json | 1 - .../compat/env-legacy-removed.golden.json | 1 - .../compat/env-lists-maps.golden.json | 5 +- .../compat/env-map-case-collision.golden.json | 3 -- .../testdata/compat/env-map-case.golden.json | 5 +- .../testdata/compat/env-only.golden.json | 5 +- .../testdata/compat/env-secrets.golden.json | 5 +- .../compat/env-shapes-typed.golden.json | 5 +- .../testdata/compat/env-shapes.golden.json | 3 -- 16 files changed, 94 insertions(+), 47 deletions(-) diff --git a/cmd/headscale/cli/root_test.go b/cmd/headscale/cli/root_test.go index cfaeda3fd..213f14d7b 100644 --- a/cmd/headscale/cli/root_test.go +++ b/cmd/headscale/cli/root_test.go @@ -614,3 +614,20 @@ func TestInitConfigWarnings(t *testing.T) { }) } } + +// TestClientCommandWithoutConfigFile: configuration from the environment +// alone is complete, so a client command does not ask for a file. +func TestClientCommandWithoutConfigFile(t *testing.T) { + env := []string{ + "HOME=" + t.TempDir(), + "HEADSCALE_UNIX_SOCKET=" + filepath.Join(t.TempDir(), "absent.sock"), + "HEADSCALE_CLI_TIMEOUT=1s", + "HEADSCALE_DISABLE_CHECK_UPDATES=true", + } + + _, stderr, err := runHeadscale(t, env, "users", "list") + require.Error(t, err, "no server listens on the socket") + assert.Contains(t, stderr, "listing users") + assert.NotContains(t, stderr, "error loading config") + assert.NotContains(t, stderr, "no config file") +} diff --git a/cmd/headscale/cli/serve.go b/cmd/headscale/cli/serve.go index f5b84378d..2698e3dea 100644 --- a/cmd/headscale/cli/serve.go +++ b/cmd/headscale/cli/serve.go @@ -6,7 +6,9 @@ import ( "net/http" "syscall" + "github.com/juanfont/headscale/hscontrol/config" "github.com/juanfont/headscale/hscontrol/types" + "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/tailscale/squibble" ) @@ -19,6 +21,8 @@ var serveCmd = &cobra.Command{ Use: "serve", Short: "Launches the headscale server", RunE: func(cmd *cobra.Command, args []string) error { + logNoConfigFile(loaded) + app, err := newHeadscaleServerWithConfig() if err != nil { if squibbleErr, ok := errors.AsType[squibble.ValidationError](err); ok { @@ -38,6 +42,14 @@ var serveCmd = &cobra.Command{ }, } +// logNoConfigFile says when the server runs without a file. The loader does +// not warn: client commands need no file unless they use an API key. +func logNoConfigFile(l *config.Loaded) { + if l.File == "" { + log.Info().Msg("no configuration file, using defaults and the environment") + } +} + // classifyServeError augments specific error classes with operator // hints. The underlying chain is left intact so errors.Is / errors.As // continue to walk to ListenerBindError, syscall.EADDRINUSE, etc. diff --git a/cmd/headscale/cli/serve_test.go b/cmd/headscale/cli/serve_test.go index 291ebd9be..23fc5a9b4 100644 --- a/cmd/headscale/cli/serve_test.go +++ b/cmd/headscale/cli/serve_test.go @@ -1,13 +1,20 @@ package cli import ( + "bytes" + "encoding/json" "errors" "fmt" "net" + "strings" "syscall" "testing" + "github.com/google/go-cmp/cmp" + "github.com/juanfont/headscale/hscontrol/config" "github.com/juanfont/headscale/hscontrol/types" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -120,3 +127,48 @@ func TestClassifyServeError(t *testing.T) { }) } } + +func TestLogNoConfigFile(t *testing.T) { + oldLogger, oldLevel := log.Logger, zerolog.GlobalLevel() + + zerolog.SetGlobalLevel(zerolog.TraceLevel) + t.Cleanup(func() { + log.Logger = oldLogger + + zerolog.SetGlobalLevel(oldLevel) + }) + + type line struct{ Level, Message string } + + tests := []struct { + name string + file string + want []line + }{ + {name: "no file", want: []line{{Level: "info", Message: "no configuration file, using defaults and the environment"}}}, + {name: "file", file: "/x/config.yaml"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var buf bytes.Buffer + + log.Logger = zerolog.New(&buf) + + logNoConfigFile(&config.Loaded{File: tt.file}) + + var got []line + + for l := range strings.Lines(buf.String()) { + var r line + require.NoError(t, json.Unmarshal([]byte(l), &r), l) + + got = append(got, r) + } + + if diff := cmp.Diff(tt.want, got); diff != "" { + t.Errorf("logged (-want +got):\n%s", diff) + } + }) + } +} diff --git a/hscontrol/config/load.go b/hscontrol/config/load.go index 40aa2d63f..acd35a71f 100644 --- a/hscontrol/config/load.go +++ b/hscontrol/config/load.go @@ -457,11 +457,10 @@ func newConfigSource(path string, isFile bool, environ []string) (*configSource, return s, fmt.Errorf("loading defaults: %w", err) } + // No file is no warning: defaults and the environment are a complete + // configuration. var fileErr error - - if s.file == "" { - s.warnings = append(s.warnings, "no config file found, using defaults") - } else { + if s.file != "" { fileErr = s.loadFile() } diff --git a/hscontrol/config/load_test.go b/hscontrol/config/load_test.go index 71047f4fd..9ad62e878 100644 --- a/hscontrol/config/load_test.go +++ b/hscontrol/config/load_test.go @@ -309,11 +309,7 @@ func TestConfigSource(t *testing.T) { dir := t.TempDir() path, isFile := dir, false - warns := append([]string{"no config file found"}, tt.warns...) - if tt.file != "" || tt.fileName != "" || tt.emptyFile { - warns = tt.warns - name := cmp.Or(tt.fileName, "config.yaml") require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(tt.file), 0o600)) @@ -335,9 +331,9 @@ func TestConfigSource(t *testing.T) { } require.NoError(t, err) - require.Len(t, l.Warnings, len(warns)) + require.Len(t, l.Warnings, len(tt.warns)) - for i, w := range warns { + for i, w := range tt.warns { assert.Contains(t, l.Warnings[i], w) } @@ -385,7 +381,7 @@ func TestConfigSourceSearch(t *testing.T) { l, err = Load(t.TempDir(), false, nil) require.NoError(t, err) assert.Empty(t, l.File) - assert.Equal(t, []string{"no config file found, using defaults"}, l.Warnings) + assert.Empty(t, l.Warnings) } // TestConfigSourceDottedOverlap reloads the file each time: map iteration diff --git a/hscontrol/config/testdata/compat/env-cli.golden.json b/hscontrol/config/testdata/compat/env-cli.golden.json index e381ac32d..ce77eafa2 100644 --- a/hscontrol/config/testdata/compat/env-cli.golden.json +++ b/hscontrol/config/testdata/compat/env-cli.golden.json @@ -153,8 +153,5 @@ }, "secrets": { "cli.api_key": "marker-cli-api-key" - }, - "warnings": [ - "no config file found, using defaults" - ] + } } diff --git a/hscontrol/config/testdata/compat/env-extra-records.golden.json b/hscontrol/config/testdata/compat/env-extra-records.golden.json index 4df806727..d2505de3a 100644 --- a/hscontrol/config/testdata/compat/env-extra-records.golden.json +++ b/hscontrol/config/testdata/compat/env-extra-records.golden.json @@ -171,8 +171,5 @@ "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } - }, - "warnings": [ - "no config file found, using defaults" - ] + } } diff --git a/hscontrol/config/testdata/compat/env-legacy-ignored.golden.json b/hscontrol/config/testdata/compat/env-legacy-ignored.golden.json index 1f9cc0b55..8f05fa5de 100644 --- a/hscontrol/config/testdata/compat/env-legacy-ignored.golden.json +++ b/hscontrol/config/testdata/compat/env-legacy-ignored.golden.json @@ -159,7 +159,6 @@ } }, "warnings": [ - "no config file found, using defaults", "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", diff --git a/hscontrol/config/testdata/compat/env-legacy-removed.golden.json b/hscontrol/config/testdata/compat/env-legacy-removed.golden.json index e2a7a820e..a02cce1b2 100644 --- a/hscontrol/config/testdata/compat/env-legacy-removed.golden.json +++ b/hscontrol/config/testdata/compat/env-legacy-removed.golden.json @@ -159,7 +159,6 @@ } }, "warnings": [ - "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/config/testdata/compat/env-lists-maps.golden.json b/hscontrol/config/testdata/compat/env-lists-maps.golden.json index edbe2867e..bd8a45c41 100644 --- a/hscontrol/config/testdata/compat/env-lists-maps.golden.json +++ b/hscontrol/config/testdata/compat/env-lists-maps.golden.json @@ -189,8 +189,5 @@ "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } - }, - "warnings": [ - "no config file found, using defaults" - ] + } } diff --git a/hscontrol/config/testdata/compat/env-map-case-collision.golden.json b/hscontrol/config/testdata/compat/env-map-case-collision.golden.json index d902f893e..ee67d5b19 100644 --- a/hscontrol/config/testdata/compat/env-map-case-collision.golden.json +++ b/hscontrol/config/testdata/compat/env-map-case-collision.golden.json @@ -1,6 +1,3 @@ { - "warnings": [ - "no config file found, using defaults" - ], "error": "Fatal config error: configuration keys \"Laptop.example.com\" and \"laptop.example.com\" in dns.challenge.provider.config differ only in case (env HEADSCALE_DNS_CHALLENGE_PROVIDER)\n hint: keep one: case is ignored there\n\nFatal config error: configuration keys \"Corp.example.com\" and \"corp.example.com\" in dns.nameservers.split differ only in case (env HEADSCALE_DNS_NAMESERVERS_SPLIT)\n hint: keep one: case is ignored there\n" } diff --git a/hscontrol/config/testdata/compat/env-map-case.golden.json b/hscontrol/config/testdata/compat/env-map-case.golden.json index 28d66f942..addb795b2 100644 --- a/hscontrol/config/testdata/compat/env-map-case.golden.json +++ b/hscontrol/config/testdata/compat/env-map-case.golden.json @@ -172,8 +172,5 @@ "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } - }, - "warnings": [ - "no config file found, using defaults" - ] + } } diff --git a/hscontrol/config/testdata/compat/env-only.golden.json b/hscontrol/config/testdata/compat/env-only.golden.json index 04f663eac..bca656e25 100644 --- a/hscontrol/config/testdata/compat/env-only.golden.json +++ b/hscontrol/config/testdata/compat/env-only.golden.json @@ -157,8 +157,5 @@ "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } - }, - "warnings": [ - "no config file found, using defaults" - ] + } } diff --git a/hscontrol/config/testdata/compat/env-secrets.golden.json b/hscontrol/config/testdata/compat/env-secrets.golden.json index 33dea04d2..004d529ad 100644 --- a/hscontrol/config/testdata/compat/env-secrets.golden.json +++ b/hscontrol/config/testdata/compat/env-secrets.golden.json @@ -161,8 +161,5 @@ "secrets": { "database.postgres.pass": "marker-postgres-pass", "oidc.client_secret": "marker-oidc-secret" - }, - "warnings": [ - "no config file found, using defaults" - ] + } } diff --git a/hscontrol/config/testdata/compat/env-shapes-typed.golden.json b/hscontrol/config/testdata/compat/env-shapes-typed.golden.json index c419cb09d..7f872514a 100644 --- a/hscontrol/config/testdata/compat/env-shapes-typed.golden.json +++ b/hscontrol/config/testdata/compat/env-shapes-typed.golden.json @@ -221,8 +221,5 @@ "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } - }, - "warnings": [ - "no config file found, using defaults" - ] + } } diff --git a/hscontrol/config/testdata/compat/env-shapes.golden.json b/hscontrol/config/testdata/compat/env-shapes.golden.json index 1148b61b7..98187ef8c 100644 --- a/hscontrol/config/testdata/compat/env-shapes.golden.json +++ b/hscontrol/config/testdata/compat/env-shapes.golden.json @@ -1,6 +1,3 @@ { - "warnings": [ - "no config file found, using defaults" - ], "error": "Fatal config error: node.expiry needs a unit (env HEADSCALE_NODE_EXPIRY)\n current: node.expiry: \"90\"\n hint: write the unit, e.g. \"30s\", \"5m\" or \"90d\"\n\nFatal config error: derp.update_frequency needs a unit (env HEADSCALE_DERP_UPDATE_FREQUENCY)\n current: derp.update_frequency: \"30\"\n hint: write the unit, e.g. \"30s\", \"5m\" or \"90d\"\n\nFatal config error: dns.nameservers.split is not a JSON object (env HEADSCALE_DNS_NAMESERVERS_SPLIT)\n hint: write the map as JSON, e.g. {\"example.com\": [\"1.1.1.1\"]}\n\nFatal config error: tuning.batch_change_delay needs a unit (env HEADSCALE_TUNING_BATCH_CHANGE_DELAY)\n current: tuning.batch_change_delay: \"800\"\n hint: write the unit, e.g. \"30s\", \"5m\" or \"90d\"\n" }