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
This commit is contained in:
Kristoffer Dalby committed 2026-11-11 15:02:16 +01:00
1 parent 69a9bb3e88
commit c7d097d65e
16 files changed
+94 -47

No files matched your search

+17
View File
@@ -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")
}
+12
View File
@@ -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.
+52
View File
@@ -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)
}
})
}
}
+3 -4
View File
@@ -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()
}
+3 -7
View File
@@ -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
+1 -4
View File
@@ -153,8 +153,5 @@
},
"secrets": {
"cli.api_key": "marker-cli-api-key"
},
"warnings": [
"no config file found, using defaults"
]
}
}
+1 -4
View File
@@ -171,8 +171,5 @@
"NodeStoreBatchSize": 100,
"NodeStoreBatchTimeout": 500000000
}
},
"warnings": [
"no config file found, using defaults"
]
}
}
-1
View File
@@ -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",
-1
View File
@@ -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"
+1 -4
View File
@@ -189,8 +189,5 @@
"NodeStoreBatchSize": 100,
"NodeStoreBatchTimeout": 500000000
}
},
"warnings": [
"no config file found, using defaults"
]
}
}
-3
View File
@@ -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"
}
+1 -4
View File
@@ -172,8 +172,5 @@
"NodeStoreBatchSize": 100,
"NodeStoreBatchTimeout": 500000000
}
},
"warnings": [
"no config file found, using defaults"
]
}
}
+1 -4
View File
@@ -157,8 +157,5 @@
"NodeStoreBatchSize": 100,
"NodeStoreBatchTimeout": 500000000
}
},
"warnings": [
"no config file found, using defaults"
]
}
}
+1 -4
View File
@@ -161,8 +161,5 @@
"secrets": {
"database.postgres.pass": "marker-postgres-pass",
"oidc.client_secret": "marker-oidc-secret"
},
"warnings": [
"no config file found, using defaults"
]
}
}
+1 -4
View File
@@ -221,8 +221,5 @@
"NodeStoreBatchSize": 100,
"NodeStoreBatchTimeout": 500000000
}
},
"warnings": [
"no config file found, using defaults"
]
}
}
-3
View File
@@ -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"
}