types: decode configuration strictly

Fixes #2899
Updates #1926
This commit is contained in:
Kristoffer Dalby authored and GitHub committed 2026-11-11 11:19:27 +00:00
1 parent 3a3a10edf9
commit c6d25d12e1
60 files changed
+2900 -1939

No files matched your search

+1
View File
@@ -3,4 +3,5 @@ docs/
# Golden fixtures are byte-compared; a formatter must not rewrite them.
hscontrol/testdata/
hscontrol/types/testdata/bad/
hscontrol/types/testdata/compat/
+2 -2
View File
@@ -1,6 +1,6 @@
{
"vendor": {
"goModSum": "sha256-jT09QcBZYC/45UW2H9CUiun/gYjVJqFcpTQ8Cf2Jamg=",
"sri": "sha256-FlwgM94RjLO2Ct7ljZDsYrNh23/Obb3RwxjoE1oAndQ="
"goModSum": "sha256-jAlQrliXnbccw4H5cimIeXJgHGvPRU3HkqneDopqbHo=",
"sri": "sha256-CksVgaWynV5w20+MJf9dDTlmPF4CbrkTI2LUPO0QUWs="
}
}
-1
View File
@@ -50,7 +50,6 @@ require (
github.com/rs/zerolog v1.35.1
github.com/samber/lo v1.53.0
github.com/sasha-s/go-deadlock v0.3.9
github.com/spf13/cast v1.10.0
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.12.1
github.com/tailscale/hujson v0.0.0-20260727124030-b80ff77dac4f
-2
View File
@@ -482,8 +482,6 @@ github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBB
github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+420 -716
View File
File diff suppressed because it is too large. Load diff
+9
View File
@@ -1,6 +1,7 @@
package types
import (
"encoding/json"
"errors"
"fmt"
"strings"
@@ -113,6 +114,14 @@ func formatKVValue(v any) string {
return `""`
case string:
return fmt.Sprintf("%q", x)
case []any, map[string]any:
// A value as decoding found it, shown as JSON.
b, err := json.Marshal(x)
if err == nil {
return string(b)
}
return fmt.Sprintf("%v", x)
default:
return fmt.Sprintf("%v", x)
}
+29 -1
View File
@@ -2,6 +2,7 @@ package types
import (
"reflect"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
@@ -9,6 +10,34 @@ import (
"github.com/stretchr/testify/require"
)
// TestURLKeys: every key that may hold a URL is listed, so its userinfo is
// redacted and never shown in an error, and every listed key exists.
func TestURLKeys(t *testing.T) {
paths := map[string]bool{}
for _, f := range ConfigFields(&Config{}) {
paths[f.Path] = true
typ := f.Field.Type
if typ.Kind() == reflect.Slice {
typ = typ.Elem()
}
// shortcut: catches only HTTPURL leaves, lists of them and names ending
// in url, urls or address, not maps or pointers; list any other URL
// key in URLKeys by hand.
name := f.Path[strings.LastIndex(f.Path, ".")+1:]
if typ == reflect.TypeFor[HTTPURL]() || strings.HasSuffix(name, "url") || strings.HasSuffix(name, "urls") ||
strings.HasSuffix(name, "address") {
assert.Contains(t, URLKeys, f.Path)
}
}
for _, k := range URLKeys {
assert.True(t, paths[k], "%s is no key", k)
}
}
func TestConfigFields(t *testing.T) {
// Written out, not derived from the loader's key tables, so it pins the
// schema on its own.
@@ -105,7 +134,6 @@ func TestConfigFields(t *testing.T) {
"cli.api_key",
"cli.timeout",
"cli.insecure",
"tuning.notifier_send_timeout",
"tuning.batch_change_delay",
"tuning.node_mapsession_buffered_chan_size",
"tuning.batcher_workers",
+51 -171
View File
@@ -1,27 +1,34 @@
package types
import (
"slices"
"strings"
"github.com/coreos/go-oidc/v3/oidc"
"go.yaml.in/yaml/v3"
)
// keyPosition returns the 1-based position of the key at path in src.
// JSON and hujson.Standardize output are valid YAML, so one parser serves all.
// Keys match case-insensitively, as the loader lowercases them.
func keyPosition(src []byte, path []string) (int, int, bool) {
// keyNode is the file parsed for the positions of its keys.
type keyNode = yaml.Node
// keyTree parses src for the positions of its keys, or returns nil. JSON
// and hujson.Standardize output are valid YAML, so one parser serves all.
func keyTree(src []byte) *yaml.Node {
var doc yaml.Node
if yaml.Unmarshal(src, &doc) != nil || len(doc.Content) == 0 {
return 0, 0, false
return nil
}
n := doc.Content[0]
return doc.Content[0]
}
// keyPosition returns the 1-based position of the key written as path, one
// name per mapping, below n. Keys match case-insensitively, as the loader
// lowercases them.
func keyPosition(n *yaml.Node, path []string) (int, int, bool) {
var key *yaml.Node
for _, want := range path {
if n.Kind != yaml.MappingNode {
if n == nil || n.Kind != yaml.MappingNode {
return 0, 0, false
}
@@ -48,171 +55,42 @@ func keyPosition(src []byte, path []string) (int, int, bool) {
return key.Line, key.Column, true
}
// configKeys lists every key the loader reads, so env mapping and the strict
// pass know the schema. Open maps are in openMapKeys.
var configKeys = []string{
"server_url",
"listen_addr",
"metrics_listen_addr",
"unix_socket",
"unix_socket_permission",
"trusted_proxies",
"disable_check_updates",
"noise.private_key_path",
"prefixes.v4",
"prefixes.v6",
"prefixes.allocation",
"policy.path",
"policy.mode",
"log.level",
"log.format",
"node.expiry",
"node.ephemeral.inactivity_timeout",
"node.routes.ha.probe_interval",
"node.routes.ha.probe_timeout",
"preauth_keys.revoked_retention",
"derp.server.enabled",
"derp.server.region_id",
"derp.server.region_code",
"derp.server.region_name",
"derp.server.verify_clients",
"derp.server.stun.enabled",
"derp.server.stun_listen_addr",
"derp.server.private_key_path",
"derp.server.ipv4",
"derp.server.ipv6",
"derp.server.automatically_add_embedded_derp_region",
"derp.urls",
"derp.paths",
"derp.auto_update_enabled",
"derp.update_frequency",
"database.type",
"database.debug",
"database.gorm.skip_err_record_not_found",
"database.gorm.slow_threshold",
"database.gorm.parameterized_queries",
"database.sqlite.path",
"database.sqlite.write_ahead_log",
"database.sqlite.wal_autocheckpoint",
"database.postgres.host",
"database.postgres.port",
"database.postgres.name",
"database.postgres.user",
"database.postgres.pass",
"database.postgres.ssl",
"database.postgres.max_open_conns",
"database.postgres.max_idle_conns",
"database.postgres.conn_max_idle_time_secs",
"dns.magic_dns",
"dns.base_domain",
"dns.override_local_dns",
"dns.search_domains",
"dns.extra_records",
"dns.extra_records_path",
"dns.nameservers.global",
"dns.nameservers.split",
"dns.certificates.enabled",
"dns.certificates.zone",
"dns.challenge.propagation_timeout",
"dns.challenge.resolvers",
"dns.challenge.provider.name",
"tls.cert_path",
"tls.key_path",
"tls.acme.hostname",
"tls.acme.challenge_type",
"tls.acme.http_listen",
"tls.acme.cache_dir",
"tls.acme.url",
"tls.acme.email",
"oidc.issuer",
"oidc.client_id",
"oidc.client_secret",
"oidc.scope",
"oidc.extra_params",
"oidc.allowed_domains",
"oidc.allowed_users",
"oidc.allowed_groups",
"oidc.email_verified_required",
"oidc.use_expiry_from_token",
"oidc.only_start_if_oidc_is_available",
"oidc.pkce.enabled",
"oidc.pkce.method",
"logtail.enabled",
"taildrop.enabled",
"auto_update.enabled",
"cli.address",
"cli.api_key",
"cli.timeout",
"cli.insecure",
"tuning.notifier_send_timeout",
"tuning.batch_change_delay",
"tuning.node_mapsession_buffered_chan_size",
"tuning.batcher_workers",
"tuning.register_cache_expiration",
"tuning.register_cache_max_entries",
"tuning.node_store_batch_size",
"tuning.node_store_batch_timeout",
}
// dottedPosition returns the 1-based position of the key at the dotted
// path below n, however the file splits it into nested and dotted keys. It
// follows only the keys n has, so the work is bounded by the file.
func dottedPosition(n *yaml.Node, path []string) (int, int, bool) {
if n == nil || n.Kind != yaml.MappingNode {
return 0, 0, false
}
// openMapKeys hold user-chosen subkeys, so nothing below them is checked.
var openMapKeys = []string{
"dns.nameservers.split",
"oidc.extra_params",
"dns.challenge.provider",
"dns.extra_records",
}
seen := map[string]bool{}
// configDefaults are the values used when no file or environment sets a key.
var configDefaults = map[string]any{
"policy.mode": "file",
"log.level": "info",
"log.format": TextLogFormat,
"dns.magic_dns": true,
"dns.base_domain": "",
"dns.override_local_dns": true,
"dns.nameservers.global": []string{},
"dns.nameservers.split": map[string]string{},
"dns.search_domains": []string{},
"dns.certificates.enabled": false,
"dns.challenge.propagation_timeout": "2m",
"derp.server.enabled": false,
"derp.server.verify_clients": true,
"derp.server.stun.enabled": true,
"derp.server.automatically_add_embedded_derp_region": true,
"derp.update_frequency": "3h",
"unix_socket": "/var/run/headscale/headscale.sock",
"unix_socket_permission": "0o770",
"cli.timeout": "5s",
"cli.insecure": false,
"database.postgres.ssl": false,
"database.postgres.max_open_conns": 10,
"database.postgres.max_idle_conns": 10,
"database.postgres.conn_max_idle_time_secs": 3600,
"database.sqlite.write_ahead_log": true,
"database.sqlite.wal_autocheckpoint": 1000, // SQLite default
"oidc.scope": []string{oidc.ScopeOpenID, "profile", "email"},
"oidc.only_start_if_oidc_is_available": true,
"oidc.use_expiry_from_token": false,
"oidc.pkce.enabled": false,
"oidc.pkce.method": "S256",
"oidc.email_verified_required": true,
"logtail.enabled": false,
"taildrop.enabled": true,
"auto_update.enabled": false,
"node.expiry": "0",
"node.ephemeral.inactivity_timeout": "120s",
"preauth_keys.revoked_retention": "168h",
"node.routes.ha.probe_interval": "10s",
"node.routes.ha.probe_timeout": "5s",
"tuning.notifier_send_timeout": "800ms",
"tuning.batch_change_delay": "800ms",
"tuning.node_mapsession_buffered_chan_size": 30,
"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",
// Last duplicate wins, matching the JSON parser's effective value; an
// earlier one is shadowed, so its children were never loaded.
for i := len(n.Content) - 2; i >= 0; i -= 2 {
key := strings.ToLower(n.Content[i].Value)
if seen[key] {
continue
}
seen[key] = true
names := strings.Split(key, ".")
if len(names) > len(path) || !slices.EqualFunc(names, path[:len(names)], strings.EqualFold) {
continue
}
if len(names) == len(path) {
return n.Content[i].Line, n.Content[i].Column, true
}
line, col, ok := dottedPosition(n.Content[i+1], path[len(names):])
if ok {
return line, col, true
}
}
return 0, 0, false
}
// removedKey is a key that once worked and is now rejected. In files Old
@@ -223,6 +101,8 @@ var configDefaults = map[string]any{
type removedKey struct{ Old, New, Value, Since, Hint string }
var removedKeys = []removedKey{
{Old: "derp.server.stun.enabled", Since: "0.30", Hint: "remove it: headscale never read it; the embedded DERP server always serves STUN on derp.server.stun_listen_addr"},
{Old: "tuning.notifier_send_timeout", Since: "0.30", Hint: "remove it: it had no effect"},
{Old: "acl_policy_path", New: "policy.path", Since: "0.30"},
{Old: "dns_config", New: "dns", Since: "0.30", Hint: "move every dns_config.* key to dns (nameservers to dns.nameservers.global, restricted_nameservers to dns.nameservers.split, domains to dns.search_domains)"},
{Old: "dns_config.magic_dns", New: "dns.magic_dns", Since: "0.30"},
+34 -2
View File
@@ -1,6 +1,7 @@
package types
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -24,7 +25,7 @@ func TestKeyPosition(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
line, col, ok := keyPosition([]byte(tt.src), tt.path)
line, col, ok := keyPosition(keyTree([]byte(tt.src)), tt.path)
assert.Equal(t, tt.ok, ok)
assert.Equal(t, tt.line, line)
assert.Equal(t, tt.col, col)
@@ -37,8 +38,39 @@ func TestKeyPosition(t *testing.T) {
b, err := hujson.Standardize([]byte(src))
require.NoError(t, err)
line, _, ok := keyPosition(b, []string{"b"})
line, _, ok := keyPosition(keyTree(b), []string{"b"})
assert.True(t, ok)
assert.Equal(t, 5, line)
})
}
func TestDottedPosition(t *testing.T) {
tests := []struct {
name string
src string
path string
line, col int
ok bool
}{
{"nested", "dns:\n magic_dsn: true\n", "dns.magic_dsn", 2, 3, true},
{"dotted", "a: 1\ndns.magic_dsn: true\n", "dns.magic_dsn", 2, 1, true},
{"mixed", "dns:\n extra.x: 1\n", "dns.extra.x", 2, 3, true},
// The surviving dns lacks the key; the shadowed one's copy was
// never loaded, so the dotted spelling is the one that set it.
{
"shadowed duplicate parent",
"{\n\"dns.magic_dsn\": 1,\n\"dns\": {\"magic_dsn\": 1},\n\"dns\": {\"x\": 1}\n}",
"dns.magic_dsn", 2, 1, true,
},
{"only in shadowed parent", `{"dns":{"magic_dsn":1},"dns":{"x":1}}`, "dns.magic_dsn", 0, 0, false},
{"missing", "a: 1\n", "a.b", 0, 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
line, col, ok := dottedPosition(keyTree([]byte(tt.src)), strings.Split(tt.path, "."))
assert.Equal(t, tt.ok, ok)
assert.Equal(t, tt.line, line)
assert.Equal(t, tt.col, col)
})
}
}
+618 -94
View File
@@ -2,23 +2,31 @@ package types
import (
"bytes"
"cmp"
"encoding"
stdjson "encoding/json"
"errors"
"fmt"
"maps"
"math"
"net/netip"
"os"
"path/filepath"
"reflect"
"slices"
"strconv"
"strings"
"time"
"github.com/go-viper/mapstructure/v2"
hsdns "github.com/juanfont/headscale/hscontrol/dns"
"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/spf13/cast"
"github.com/tailscale/hujson"
)
@@ -45,13 +53,17 @@ var reservedEnvNames = []string{
}
// configSource is the layered configuration: defaults, then file, then
// environment. Keys are lowercase; accessors coerce values, so a string from
// the environment reads as a bool, number or duration.
// environment, with lowercase keys. [configSource.decode] turns it into a
// [Config].
type configSource struct {
k *koanf.Koanf
file string
fileBytes []byte
warnings []string
// keys is the file parsed for the positions of its keys, or nil.
keys *keyNode
warnings []string
// envVars names the variable that set each key, for errors.
envVars map[string]string
}
func configEnvName(key string) string {
@@ -178,8 +190,38 @@ func normaliseConfigValue(v any) any {
return v
}
// schemaKey is a key of the configuration; an open map's keys are the
// user's.
type schemaKey struct {
Path string
Open bool
}
func schemaKeys() []schemaKey {
fields := ConfigFields(&Config{})
keys := make([]schemaKey, len(fields))
for i, f := range fields {
keys[i] = schemaKey{Path: f.Path, Open: f.Open}
}
return keys
}
func openMapKeys() []string {
var open []string
for _, k := range schemaKeys() {
if k.Open {
open = append(open, k.Path)
}
}
return open
}
func isOpenMapPath(path string) bool {
for _, o := range openMapKeys {
for _, o := range openMapKeys() {
if path == o || strings.HasPrefix(path, o+".") {
return true
}
@@ -194,7 +236,7 @@ func isOpenMapPath(path string) bool {
func configPathParts(path string) []string {
parts := strings.Split(path, ".")
for i := 1; i < len(parts); i++ {
if slices.Contains(openMapKeys, strings.Join(parts[:i], ".")) {
if slices.Contains(openMapKeys(), strings.Join(parts[:i], ".")) {
return append(parts[:i], strings.Join(parts[i:], "."))
}
}
@@ -267,9 +309,11 @@ func joinConfigPath(prefix, name string) string {
// newConfigSource loads defaults, the config file found via path/isFile and
// environ (os.Environ form), and rejects removed and unknown file keys.
func newConfigSource(path string, isFile bool, environ []string) (*configSource, error) {
s := &configSource{k: koanf.New(".")}
s := &configSource{k: koanf.New("."), envVars: map[string]string{}}
err := s.k.Load(confmap.Provider(configDefaults, "."), nil)
d := defaultConfig()
err := s.k.Load(confmap.Provider(plainMap(&d, false), ""), nil)
if err != nil {
return nil, fmt.Errorf("loading defaults: %w", err)
}
@@ -314,6 +358,7 @@ func (s *configSource) loadFile() error {
}
s.fileBytes = raw
s.keys = keyTree(raw)
var m map[string]any
if err == nil {
@@ -349,7 +394,8 @@ func (s *configSource) loadFile() error {
func configKeySets() (map[string]bool, map[string]bool) {
leaf, branch := map[string]bool{}, map[string]bool{}
for _, k := range slices.Concat(configKeys, openMapKeys) {
for _, key := range schemaKeys() {
k := key.Path
leaf[k] = true
parts := strings.Split(k, ".")
@@ -496,7 +542,7 @@ func (s *configSource) checkKeys(v *configValidator, m map[string]any) {
// 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) {
if !slices.Contains(openMapKeys(), path) {
parts := configPathParts(path)
root = strings.Join(parts[:len(parts)-1], ".")
below = map[string]any{parts[len(parts)-1]: val}
@@ -524,9 +570,10 @@ func (s *configSource) checkKeys(v *configValidator, m map[string]any) {
s.checkOverlaps(v, m)
}
// position renders " (file:line:col)" when the key can be located.
// position renders " (file:line:col)" when the key can be located, written
// nested, dotted or mixed, else " (file)".
func (s *configSource) position(path string) string {
line, col, ok := keyPosition(s.fileBytes, strings.Split(path, "."))
line, col, ok := dottedPosition(s.keys, strings.Split(path, "."))
if !ok {
return " (" + s.file + ")"
}
@@ -550,18 +597,21 @@ func (s *configSource) loadEnv(environ []string) error {
openLayer := map[string]string{}
for _, key := range slices.Concat(configKeys, openMapKeys) {
name := configEnvName(key)
for _, key := range schemaKeys() {
name := configEnvName(key.Path)
names[name] = true
val, ok := env[name]
switch {
case !ok:
case slices.Contains(openMapKeys, key):
openLayer[key] = val
continue
case key.Open:
openLayer[key.Path] = val
default:
layer[key] = val
layer[key.Path] = val
}
s.envVars[key.Path] = name
}
// Other software shares the prefix (a Kubernetes Service named
@@ -586,42 +636,26 @@ func (s *configSource) loadEnv(environ []string) error {
}
// 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 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. The provider's is always
// parsed, as its name is looked up inside it; one that does not decode is
// no provider.
// into it. Its JSON object is set without the removed forms it holds;
// a value that is not one stays a string, which decode reports.
for _, key := range slices.Sorted(maps.Keys(openLayer)) {
val := openLayer[key]
s.k.Delete(key)
var parsed map[string]any
dropped := false
var (
v any = val
parsed map[string]any
)
// A failed decode may still have filled part of parsed: never used.
decoded := stdjson.Unmarshal([]byte(val), &parsed) == nil && parsed != nil
if decoded {
if stdjson.Unmarshal([]byte(val), &parsed) == nil && parsed != nil {
// 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
switch {
case decoded && (dropped || shared || key == providerKey):
v = parsed
case key == providerKey:
continue
}
err := s.k.Set(key, v)
@@ -638,21 +672,38 @@ func (s *configSource) loadEnv(environ []string) error {
return s.foldProviderEnv(env)
}
// foldProviderEnv sets each HEADSCALE_DNS_CHALLENGE_PROVIDER_<SETTING> of
// the provider named once the rest is loaded. Settings depend on the
// provider, so configKeys cannot list them; a variable no setting of it has
// is ignored, as the prefix is reserved.
// foldProviderEnv sets HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME, then each
// HEADSCALE_DNS_CHALLENGE_PROVIDER_<SETTING> of the provider named once the
// rest is loaded. Settings depend on the provider, so the schema cannot list
// them; a variable no setting of it has is ignored, as the prefix is
// reserved.
func (s *configSource) foldProviderEnv(env map[string]string) error {
settings := cast.ToStringMap(s.k.Get(providerKey))
folded := false
settings, ok := s.k.Get(providerKey).(map[string]any)
if !ok {
if s.k.Exists(providerKey) {
// Not an object: decode reports it.
return nil
}
for _, setting := range hsdns.SettingNames(cast.ToString(settings["name"])) {
if v := env[configEnvName(providerKey+"."+setting)]; v != "" {
settings = map[string]any{}
}
folded := false
fold := func(setting string) {
key := providerKey + "." + setting
if v := env[configEnvName(key)]; v != "" {
settings[setting] = v
s.envVars[key] = configEnvName(key)
folded = true
}
}
fold("name")
for _, setting := range hsdns.SettingNames(DNSChallengeConfig{ProviderSettings: settings}.ProviderName()) {
fold(setting)
}
if !folded {
return nil
}
@@ -694,51 +745,6 @@ func (s *configSource) File() string { return s.file }
// Warnings returns non-fatal findings from loading.
func (s *configSource) Warnings() []string { return s.warnings }
func (s *configSource) Get(key string) any { return s.k.Get(strings.ToLower(key)) }
func (s *configSource) IsSet(key string) bool { return s.k.Exists(strings.ToLower(key)) }
func (s *configSource) String(key string) string { return cast.ToString(s.Get(key)) }
func (s *configSource) Bool(key string) bool { return cast.ToBool(s.Get(key)) }
func (s *configSource) Int(key string) int { return cast.ToInt(s.Get(key)) }
func (s *configSource) Int64(key string) int64 { return cast.ToInt64(s.Get(key)) }
func (s *configSource) Duration(key string) time.Duration { return cast.ToDuration(s.Get(key)) }
func (s *configSource) StringSlice(key string) []string { return cast.ToStringSlice(s.Get(key)) }
func (s *configSource) StringMap(key string) map[string]any { return cast.ToStringMap(s.Get(key)) }
func (s *configSource) StringMapString(key string) map[string]string {
return cast.ToStringMapString(s.Get(key))
}
func (s *configSource) StringMapStringSlice(key string) map[string][]string {
return cast.ToStringMapStringSlice(s.Get(key))
}
// UnmarshalKey decodes key into out, weakly typed: values from the
// environment are strings.
func (s *configSource) UnmarshalKey(key string, out any) error {
if !s.IsSet(key) {
return nil
}
return s.k.UnmarshalWithConf(strings.ToLower(key), out, koanf.UnmarshalConf{
DecoderConfig: &mapstructure.DecoderConfig{
WeaklyTypedInput: true,
TagName: "mapstructure",
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToWeakSliceHookFunc(","),
),
},
})
}
// keyEntry is one key of the file as written, for finding paths that the file
// spells in more than one form.
type keyEntry struct {
@@ -828,10 +834,528 @@ func (s *configSource) checkOverlaps(v *configValidator, m map[string]any) {
// positionOf renders " (file:line:col)" for a key written as names.
func (s *configSource) positionOf(names []string) string {
line, col, ok := keyPosition(s.fileBytes, names)
line, col, ok := keyPosition(s.keys, names)
if !ok {
return ""
}
return fmt.Sprintf(" (%s:%d:%d)", s.file, line, col)
}
// 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
err := s.k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{
Tag: "koanf",
DecoderConfig: &mapstructure.DecoderConfig{
ErrorUnused: true,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.DecodeHookFuncValue(scalarHook),
mapstructure.TextUnmarshallerHookFunc(),
),
},
})
if err != nil {
return nil, s.decodeErrors(err)
}
cfg.ServerURL = strings.TrimRight(cfg.ServerURL, "/")
for i, p := range cfg.TrustedProxies {
cfg.TrustedProxies[i] = p.Masked()
}
cfg.ConfigDir = s.configDir()
return &cfg, nil
}
// configDir is where relative paths in the configuration are resolved: the
// config file's directory, or the working directory without a file.
func (s *configSource) configDir() string {
dir := "."
if s.file != "" {
dir = filepath.Dir(s.file)
}
abs, err := filepath.Abs(dir)
if err != nil {
return dir
}
return abs
}
// 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{}
seen := map[string]bool{}
walkErrTree(err, func(e error) {
de, ok := e.(*mapstructure.DecodeError) //nolint:errorlint // every node is visited
if !ok {
return
}
// An element or entry is reported on its key.
key, _, _ := strings.Cut(de.Name(), "[")
var leaf *ConfigError
if !errors.As(de.Unwrap(), &leaf) {
leaf = &ConfigError{Reason: de.Unwrap().Error(), Cause: de.Unwrap()}
// The decoder's own error keeps the value (UnconvertibleTypeError's
// Value); its text does not.
if hiddenValue(key) {
leaf.Cause = nil
}
}
ce := *leaf
ce.Reason = key + " " + leaf.Reason + s.origin(key)
if !hiddenValue(key) {
ce.Current = []KV{{key, s.k.Get(key)}}
}
if !seen[ce.Reason] {
seen[ce.Reason] = true
v.Add(&ce)
}
})
if !v.HasErrors() {
return err
}
return v.Err()
}
// origin renders where key's value came from: its variable, else its
// position in the file.
func (s *configSource) origin(key string) string {
if name, ok := s.envVars[key]; ok {
return " (env " + name + ")"
}
if s.file == "" {
return ""
}
return s.position(key)
}
// 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) {
return true
}
for _, f := range ConfigFields(&Config{}) {
if f.Path != key {
continue
}
return f.Field.Type == reflect.TypeFor[Secret]()
}
return false
}
var (
durationType = reflect.TypeFor[time.Duration]()
fileModeType = reflect.TypeFor[FileMode]()
prefixType = reflect.TypeFor[netip.Prefix]()
prefixPtrType = reflect.TypeFor[*netip.Prefix]()
)
// scalarHook converts a value as written into what its field's type takes,
// before mapstructure and TextUnmarshaler see it. A string is the form
// variables take, so it may stand for a number, bool, list or map.
func scalarHook(from, to reflect.Value) (any, error) {
data := from.Interface()
s, isString := data.(string)
switch t := to.Type(); {
case t == durationType:
return toDuration(from)
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{
Reason: "must be quoted",
Maximum: `"0777"`,
Hint: `quote the value, e.g. "0770"; unquoted, YAML reads 0770 as the number 504`,
Cause: util.ErrInvalidFileMode,
}
case t == prefixPtrType && isString && s == "":
// Unset. Typed: the next hook reads its type.
return (*netip.Prefix)(nil), nil
case t == prefixType && isString:
_, err := netip.ParsePrefix(s)
if err != nil {
return nil, &ConfigError{
Reason: "is not a valid CIDR",
Detail: err.Error(),
Hint: `use CIDR form, e.g. "100.64.0.0/10"`,
Cause: err,
}
}
case t.Kind() == reflect.String:
if text, ok := scalarText(from); ok {
return text, nil
}
case t.Kind() == reflect.Bool:
return toBool(from)
case t.Kind() >= reflect.Int && t.Kind() <= reflect.Int64:
return toInt(from, t)
case t.Kind() == reflect.Slice && isString:
return toList(s, t.Elem())
case t.Kind() == reflect.Map && isString:
return toObject(s)
}
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"`}
if v.Kind() != reflect.String {
if _, ok := scalarText(v); !ok || v.Kind() == reflect.Bool {
return v.Interface(), nil
}
if v.IsZero() {
return time.Duration(0), nil
}
return nil, unit
}
s := v.String()
d, err := time.ParseDuration(s)
if err == nil {
return d, nil
}
md, err := model.ParseDuration(s)
if err == nil {
return time.Duration(md), nil
}
_, err = strconv.ParseFloat(s, 64)
if err == nil {
return nil, unit
}
return nil, &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 {
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 b, nil
}
// toInt takes a whole number in t's range, written as one or as text.
func toInt(v reflect.Value, t reflect.Type) (any, error) {
var (
n int64
whole bool
)
switch {
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"}
}
n, whole = i, err == nil
case v.CanInt():
n, whole = v.Int(), true
case v.CanUint():
if v.Uint() > math.MaxInt64 {
return nil, &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"}
}
n, whole = int64(f), f == math.Trunc(f)
default:
return v.Interface(), nil
}
if !whole {
return nil, &ConfigError{Reason: "is not an integer"}
}
if reflect.Zero(t).OverflowInt(n) {
return nil, &ConfigError{Reason: "is out of range"}
}
return n, nil
}
// toList splits a list written as one string: as JSON when it starts with
// "[", as a list of objects must, else on whitespace.
func toList(s string, elem reflect.Type) (any, error) {
object := elem.Kind() == reflect.Struct && !reflect.PointerTo(elem).Implements(textUnmarshalerType)
if !object && !strings.HasPrefix(strings.TrimSpace(s), "[") {
fields := strings.Fields(s)
list := make([]any, len(fields))
for i, f := range fields {
list[i] = f
}
return list, nil
}
var list []any
err := stdjson.Unmarshal([]byte(s), &list)
if err != nil {
hint := `write the list as JSON, e.g. ["a", "b"]`
if object {
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 list, nil
}
func toObject(s string) (any, error) {
var m map[string]any
err := stdjson.Unmarshal([]byte(s), &m)
if err != nil || m == nil {
return nil, &ConfigError{
Reason: "is not a JSON object",
Hint: `write the map as JSON, e.g. {"example.com": ["1.1.1.1"]}`,
Cause: err,
}
}
return m, nil
}
// plainMap is cfg as nested maps of plain values, keyed as in a file: what
// the defaults layer loads and a parser marshals. With redact, a secret
// 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 {
out := map[string]any{}
if redact {
// HTTPURL and Secret values redact themselves, plain strings not.
r := cfg.RedactedURLs()
cfg = &r
}
for _, f := range ConfigFields(cfg) {
v := plainValue(f.Value, redact)
switch settings, isMap := v.(map[string]any); {
case f.Field.Type == prefixPtrType && f.Value.IsNil():
// An explicit unset, which survives a default.
v = ""
case redact && isMap && f.Path == providerKey:
v = redactProvider(settings)
}
if v == nil {
continue
}
parts := strings.Split(f.Path, ".")
m := out
for _, p := range parts[:len(parts)-1] {
child, ok := m[p].(map[string]any)
if !ok {
child = map[string]any{}
m[p] = child
}
m = child
}
m[parts[len(parts)-1]] = v
}
return out
}
// maxExactJSONInt is the largest integer a float64, as JSON parsers read
// numbers, holds exactly.
const maxExactJSONInt = 1 << 53
// plainValue is v as a parser writes it, or nil to leave it out.
func plainValue(v reflect.Value, redact bool) any {
for v.Kind() == reflect.Pointer || v.Kind() == reflect.Interface {
if v.IsNil() {
return nil
}
v = v.Elem()
}
switch x := v.Interface().(type) {
case time.Duration:
return x.String()
case Secret:
if redact {
return x.Redacted()
}
return string(x)
case HTTPURL:
// As written: its JSON form drops userinfo.
if redact {
return x.Redacted()
}
return string(x)
case encoding.TextMarshaler:
text, err := x.MarshalText()
if err != nil {
return nil
}
return string(text)
}
switch v.Kind() { //nolint:exhaustive // the kinds Config holds; default keeps the rest as is
case reflect.String:
return v.String()
case reflect.Bool:
return v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if n := v.Int(); n > maxExactJSONInt || n < -maxExactJSONInt {
// The integer hook takes it back from text.
return strconv.FormatInt(n, 10)
}
return int(v.Int())
case reflect.Slice:
if v.IsNil() {
return nil
}
list := make([]any, v.Len())
for i := range list {
list[i] = plainValue(v.Index(i), redact)
}
return list
case reflect.Map:
if v.IsNil() {
return nil
}
m := make(map[string]any, v.Len())
for iter := v.MapRange(); iter.Next(); {
if e := plainValue(iter.Value(), redact); e != nil {
m[fmt.Sprint(iter.Key().Interface())] = e
}
}
return m
case reflect.Struct:
// Without koanf tags (tailcfg.DNSRecord): its field names, lowercased.
m := map[string]any{}
for f, fv := range v.Fields() {
if f.IsExported() {
m[cmp.Or(f.Tag.Get("koanf"), strings.ToLower(f.Name))] = plainValue(fv, redact)
}
}
return m
default:
return v.Interface()
}
}
func redactProvider(settings map[string]any) map[string]any {
out := make(map[string]any, len(settings))
for k, v := range settings {
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
}
}
return out
}
func redactLeaves(v any) any {
switch x := v.(type) {
case map[string]any:
out := make(map[string]any, len(x))
for k, e := range x {
out[k] = redactLeaves(e)
}
return out
case []any:
out := make([]any, len(x))
for i, e := range x {
out[i] = redactLeaves(e)
}
return out
case string:
if strings.HasPrefix(x, "file:") {
return x
}
}
return "<redacted>"
}
+480 -164
View File
@@ -2,25 +2,29 @@ package types
import (
"cmp"
"go/ast"
"go/parser"
"go/token"
"fmt"
"math"
"net/netip"
"os"
"path/filepath"
"reflect"
"slices"
"strconv"
"strings"
"testing"
"time"
gocmp "github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"tailscale.com/tailcfg"
)
func writeConfig(t *testing.T, name, content string) string {
// writeConfig writes content as config.yaml and returns its path.
func writeConfig(t *testing.T, content string) string {
t.Helper()
p := filepath.Join(t.TempDir(), name)
p := filepath.Join(t.TempDir(), "config.yaml")
require.NoError(t, os.WriteFile(p, []byte(content), 0o600))
return p
@@ -35,7 +39,7 @@ func TestConfigSource(t *testing.T) {
env []string
errs []string // substrings of the error
warns []string // substrings, one per expected warning
check func(t *testing.T, s *configSource)
check func(t *testing.T, cfg *Config)
}{
{
name: "unknown file key",
@@ -84,40 +88,40 @@ func TestConfigSource(t *testing.T) {
{
name: "env shared with a removed key still sets the new key",
env: []string{"HEADSCALE_LOG_LEVEL=debug", "HEADSCALE_TLS_CERT_PATH=/c"},
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t, "debug", s.String("log.level"))
assert.Equal(t, "/c", s.String("tls.cert_path"))
assert.Equal(t, LogLevel("debug"), cfg.Log.Level)
assert.Equal(t, "/c", cfg.TLS.CertPath)
},
},
{
name: "keys are case-insensitive",
file: "Server_URL: https://x\ndns:\n Nameservers:\n Split:\n Corp.Example.com: [10.0.0.1]\n",
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t, "https://x", s.String("server_url"))
assert.Equal(t, "https://x", cfg.ServerURL)
assert.Equal(t,
map[string][]string{"corp.example.com": {"10.0.0.1"}},
s.StringMapStringSlice("dns.nameservers.split"))
cfg.DNSConfig.Nameservers.Split)
},
},
{
name: "null is unset, default stays",
file: "server_url:\nlog:\n level: ~\n",
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.False(t, s.IsSet("server_url"))
assert.Equal(t, "info", s.String("log.level"))
assert.Empty(t, cfg.ServerURL)
assert.Equal(t, LogLevel("info"), cfg.Log.Level)
},
},
{
name: "empty env is unset, env beats file",
file: "server_url: http://file\nlisten_addr: 1.1.1.1:1\n",
env: []string{"HEADSCALE_SERVER_URL=http://env", "HEADSCALE_LISTEN_ADDR="},
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t, "http://env", s.String("server_url"))
assert.Equal(t, "1.1.1.1:1", s.String("listen_addr"))
assert.Equal(t, "http://env", cfg.ServerURL)
assert.Equal(t, ListenAddr("1.1.1.1:1"), cfg.Addr)
},
},
{
@@ -126,19 +130,19 @@ func TestConfigSource(t *testing.T) {
"HEADSCALE_DNS_NAMESERVERS_GLOBAL=1.1.1.1 8.8.8.8",
`HEADSCALE_OIDC_EXTRA_PARAMS={"domain_hint":"example.com"}`,
},
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t, []string{"1.1.1.1", "8.8.8.8"}, s.StringSlice("dns.nameservers.global"))
assert.Equal(t, map[string]string{"domain_hint": "example.com"}, s.StringMapString("oidc.extra_params"))
assert.Equal(t, []string{"1.1.1.1", "8.8.8.8"}, cfg.DNSConfig.Nameservers.Global)
assert.Equal(t, map[string]string{"domain_hint": "example.com"}, cfg.OIDC.ExtraParams)
},
},
{
name: "open maps accept any key",
file: "dns:\n challenge:\n provider:\n name: cloudflare\n whatever: 1\noidc:\n extra_params:\n a: b\n",
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t, "cloudflare", s.String("dns.challenge.provider.name"))
assert.Equal(t, 1, s.Int("dns.challenge.provider.whatever"))
assert.Equal(t, "cloudflare", cfg.DNSConfig.Challenge.ProviderName())
assert.Equal(t, 1, cfg.DNSConfig.Challenge.ProviderSettings["whatever"])
},
},
{
@@ -192,27 +196,23 @@ func TestConfigSource(t *testing.T) {
{
name: "nested keys that share leaf names with removed keys",
file: "noise:\n private_key_path: /n\nderp:\n server:\n private_key_path: /d\nlog:\n level: debug\ntls:\n cert_path: /c\ndatabase:\n sqlite:\n path: /db\n",
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t, "/n", s.String("noise.private_key_path"))
assert.Equal(t, "/d", s.String("derp.server.private_key_path"))
assert.Equal(t, "debug", s.String("log.level"))
assert.Equal(t, "/c", s.String("tls.cert_path"))
assert.Equal(t, "/db", s.String("database.sqlite.path"))
assert.Equal(t, "/n", cfg.Noise.PrivateKeyPath)
assert.Equal(t, "/d", cfg.DERP.Server.PrivateKeyPath)
assert.Equal(t, LogLevel("debug"), cfg.Log.Level)
assert.Equal(t, "/c", cfg.TLS.CertPath)
assert.Equal(t, "/db", cfg.Database.Sqlite.Path)
},
},
{
name: "dotted file keys override nested defaults",
file: "log.level: debug\ndns.magic_dns: false\ndns:\n nameservers.split:\n Corp.Example.com: [10.0.0.1]\n",
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
for range 50 {
assert.Equal(t, "debug", s.String("log.level"))
assert.False(t, s.Bool("dns.magic_dns"))
}
assert.Equal(t, map[string][]string{"corp.example.com": {"10.0.0.1"}}, s.StringMapStringSlice("dns.nameservers.split"))
assert.Equal(t, LogLevel("debug"), cfg.Log.Level)
assert.False(t, cfg.DNSConfig.MagicDNS)
assert.Equal(t, map[string][]string{"corp.example.com": {"10.0.0.1"}}, cfg.DNSConfig.Nameservers.Split)
},
},
{
@@ -228,20 +228,11 @@ func TestConfigSource(t *testing.T) {
{
name: "non-string yaml keys are normalised",
file: "dns:\n nameservers:\n split:\n 123: [1.1.1.1]\n Corp.Example.com: [10.0.0.1]\n Empty.Example.com: ~\n",
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t,
map[string][]string{"123": {"1.1.1.1"}, "corp.example.com": {"10.0.0.1"}},
s.StringMapStringSlice("dns.nameservers.split"))
},
},
{
name: "key arguments are case-insensitive",
file: "server_url: https://x\n",
check: func(t *testing.T, s *configSource) {
t.Helper()
assert.True(t, s.IsSet("Server_URL"))
assert.Equal(t, "https://x", s.String("SERVER_URL"))
cfg.DNSConfig.Nameservers.Split)
},
},
{
@@ -270,9 +261,9 @@ func TestConfigSource(t *testing.T) {
{
name: "empty file",
emptyFile: true,
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t, "info", s.String("log.level"))
assert.Equal(t, LogLevel("info"), cfg.Log.Level)
},
},
{
@@ -317,7 +308,9 @@ func TestConfigSource(t *testing.T) {
}
if tt.check != nil {
tt.check(t, s)
cfg, err := s.decode()
require.NoError(t, err)
tt.check(t, cfg)
}
})
}
@@ -325,13 +318,13 @@ func TestConfigSource(t *testing.T) {
const yamlSample = "server_url: https://x\nnode:\n expiry: 1h\ndns:\n magic_dns: false\n nameservers:\n global: [1.1.1.1, 8.8.8.8]\noidc:\n extra_params:\n domain_hint: example.com\n"
func checkSameValues(t *testing.T, s *configSource) {
func checkSameValues(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t, "https://x", s.String("server_url"))
assert.Equal(t, time.Hour, s.Duration("node.expiry"))
assert.False(t, s.Bool("dns.magic_dns"))
assert.Equal(t, []string{"1.1.1.1", "8.8.8.8"}, s.StringSlice("dns.nameservers.global"))
assert.Equal(t, map[string]string{"domain_hint": "example.com"}, s.StringMapString("oidc.extra_params"))
assert.Equal(t, "https://x", cfg.ServerURL)
assert.Equal(t, time.Hour, cfg.Node.Expiry)
assert.False(t, cfg.DNSConfig.MagicDNS)
assert.Equal(t, []string{"1.1.1.1", "8.8.8.8"}, cfg.DNSConfig.Nameservers.Global)
assert.Equal(t, map[string]string{"domain_hint": "example.com"}, cfg.OIDC.ExtraParams)
}
func TestConfigSearchDirs(t *testing.T) {
@@ -355,99 +348,16 @@ func TestConfigSourceSearch(t *testing.T) {
s, err := newConfigSource(dir, false, nil)
require.NoError(t, err)
assert.True(t, strings.HasSuffix(s.File(), "config.json"))
assert.Equal(t, "http://json", s.String("server_url"))
cfg, err := s.decode()
require.NoError(t, err)
assert.Equal(t, "http://json", cfg.ServerURL)
s, err = newConfigSource(t.TempDir(), false, nil)
require.NoError(t, err)
assert.Empty(t, s.File())
}
func TestConfigSourceUnmarshalKey(t *testing.T) {
type rec struct{ Name, Type, Value string }
tests := []struct {
name, file string
want []rec
}{
// An empty string decodes to an empty, non-nil slice.
{"empty string", "dns:\n extra_records: \"\"\n", []rec{}},
{"list", "dns:\n extra_records:\n - {name: a.example.com, type: A, value: 1.2.3.4}\n", []rec{{"a.example.com", "A", "1.2.3.4"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, err := newConfigSource(writeConfig(t, "config.yaml", tt.file), true, nil)
require.NoError(t, err)
var got []rec
require.NoError(t, s.UnmarshalKey("dns.extra_records", &got))
assert.Equal(t, tt.want, got)
})
}
}
func TestConfigKeysComplete(t *testing.T) {
read := map[string]bool{}
for _, f := range []string{"config.go", "listener.go"} {
file, err := parser.ParseFile(token.NewFileSet(), f, nil, 0)
require.NoError(t, err)
ast.Inspect(file, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok || len(call.Args) == 0 {
return true
}
// Every src accessor takes the key first.
fn, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
if obj, ok := fn.X.(*ast.Ident); !ok || obj.Name != "src" {
return true
}
if lit, ok := call.Args[0].(*ast.BasicLit); ok && lit.Kind == token.STRING {
key, err := strconv.Unquote(lit.Value)
require.NoError(t, err)
read[key] = true
}
return true
})
}
all := slices.Concat(configKeys, openMapKeys)
for key := range read {
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, "%s is listed but never read", key)
}
for key := range configDefaults {
assert.Contains(t, configKeys, key, "default without a key entry")
}
// Two keys sharing one environment variable would shadow each other.
envs := map[string]string{}
for _, key := range configKeys {
name := configEnvName(key)
assert.Emptyf(t, envs[name], "%s and %s share %s", envs[name], key, name)
envs[name] = key
}
}
// TestConfigSourceDottedOverlap reloads the file each time: map iteration
// order differs per load, so a single load could hide a nondeterministic result.
func TestConfigSourceDottedOverlap(t *testing.T) {
@@ -483,9 +393,12 @@ func TestConfigSourceDottedKeyAlone(t *testing.T) {
for range 50 {
s, err := newConfigSource(dir, false, nil)
require.NoError(t, err)
require.Equal(t, "debug", s.String("log.level"))
require.False(t, s.Bool("dns.magic_dns"))
require.Equal(t, []string{"1.1.1.1"}, s.StringSlice("dns.nameservers.global"))
cfg, err := s.decode()
require.NoError(t, err)
require.Equal(t, LogLevel("debug"), cfg.Log.Level)
require.False(t, cfg.DNSConfig.MagicDNS)
require.Equal(t, []string{"1.1.1.1"}, cfg.DNSConfig.Nameservers.Global)
}
}
@@ -494,44 +407,44 @@ func TestConfigSourceDottedKeyAlone(t *testing.T) {
func TestConfigSourceDottedNoOverlap(t *testing.T) {
tests := []struct {
name, file string
check func(t *testing.T, s *configSource)
check func(t *testing.T, cfg *Config)
}{
{
name: "dotted split domains",
file: "dns.nameservers.split.corp.example.com: [10.0.0.1]\ndns.nameservers.split.corp.example.com.au: [10.0.0.2]\n",
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t,
map[string][]string{"corp.example.com": {"10.0.0.1"}, "corp.example.com.au": {"10.0.0.2"}},
s.StringMapStringSlice("dns.nameservers.split"))
cfg.DNSConfig.Nameservers.Split)
},
},
{
name: "dotted extra params",
file: "oidc.extra_params.domain_hint: a\noidc.extra_params.domain_hint.extra: b\n",
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t, map[string]string{"domain_hint": "a", "domain_hint.extra": "b"}, s.StringMapString("oidc.extra_params"))
assert.Equal(t, map[string]string{"domain_hint": "a", "domain_hint.extra": "b"}, cfg.OIDC.ExtraParams)
},
},
{
name: "nested split domains",
file: "dns:\n nameservers:\n split:\n corp.example.com: [10.0.0.1]\n corp.example.com.au: [10.0.0.2]\n",
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t,
map[string][]string{"corp.example.com": {"10.0.0.1"}, "corp.example.com.au": {"10.0.0.2"}},
s.StringMapStringSlice("dns.nameservers.split"))
cfg.DNSConfig.Nameservers.Split)
},
},
{
name: "log and logtail",
file: "log:\n level: debug\nlog.format: json\nlogtail.enabled: true\n",
check: func(t *testing.T, s *configSource) {
check: func(t *testing.T, cfg *Config) {
t.Helper()
assert.Equal(t, "debug", s.String("log.level"))
assert.Equal(t, "json", s.String("log.format"))
assert.True(t, s.Bool("logtail.enabled"))
assert.Equal(t, LogLevel("debug"), cfg.Log.Level)
assert.Equal(t, JSONLogFormat, cfg.Log.Format)
assert.True(t, cfg.LogTail.Enabled)
},
},
}
@@ -544,7 +457,10 @@ func TestConfigSourceDottedNoOverlap(t *testing.T) {
for range 50 {
s, err := newConfigSource(dir, false, nil)
require.NoError(t, err)
tt.check(t, s)
cfg, err := s.decode()
require.NoError(t, err)
tt.check(t, cfg)
}
})
}
@@ -560,7 +476,7 @@ func TestConfigSourceShapeErrorsHideValues(t *testing.T) {
"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)
_, err := newConfigSource(writeConfig(t, file), true, nil)
require.ErrorContains(t, err, "must be a mapping")
assert.NotContains(t, err.Error(), "marker-secret")
})
@@ -589,11 +505,13 @@ func TestConfigSourceProviderEnv(t *testing.T) {
require.NoError(t, err)
assert.Empty(t, s.Warnings())
assert.Equal(t, "cloudflare", s.String("dns.challenge.provider.name"))
cfg, err := s.decode()
require.NoError(t, err)
assert.Equal(t, "cloudflare", cfg.DNSConfig.Challenge.ProviderName())
// The env value replaces the file's map; the per-field
// variable is layered on top of it.
got := s.StringMap("dns.challenge.provider")
got := cfg.DNSConfig.Challenge.ProviderSettings
assert.Equal(t, "x", got["api_token"])
assert.NotContains(t, got, "other")
}
@@ -698,3 +616,401 @@ dns:
})
}
}
// decodeInput writes content as name (none when empty) and decodes it with
// env, as the loaders do.
func decodeInput(t *testing.T, name, content string, env ...string) (*Config, error) {
t.Helper()
dir := t.TempDir()
if content != "" {
require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(content), 0o600))
}
s, err := newConfigSource(dir, false, env)
require.NoError(t, err)
return s.decode()
}
func TestDecodeScalars(t *testing.T) {
tests := []struct {
name string
file string // config.yaml unless it starts with "{"
env []string
check func(t *testing.T, cfg *Config)
wantErr []string
}{
{name: "yaml expiry 0", file: "node:\n expiry: 0\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.Zero(t, c.Node.Expiry) }},
{name: "json expiry 0", file: `{"node": {"expiry": 0}}`, check: func(t *testing.T, c *Config) { t.Helper(); assert.Zero(t, c.Node.Expiry) }},
{name: "env expiry 0", env: []string{"HEADSCALE_NODE_EXPIRY=0"}, check: func(t *testing.T, c *Config) { t.Helper(); assert.Zero(t, c.Node.Expiry) }},
{name: "prometheus duration", file: "node:\n expiry: 90d\n", check: func(t *testing.T, c *Config) {
t.Helper()
assert.Equal(t, 2160*time.Hour, c.Node.Expiry)
}},
{name: "duration without unit", file: "node:\n expiry: 90\n", wantErr: []string{"node.expiry needs a unit (", "config.yaml:2:3)"}},
{name: "env duration without unit", env: []string{"HEADSCALE_TUNING_BATCH_CHANGE_DELAY=800"}, wantErr: []string{"tuning.batch_change_delay needs a unit (env HEADSCALE_TUNING_BATCH_CHANGE_DELAY)"}},
{name: "unquoted file mode", file: "unix_socket_permission: 0770\n", wantErr: []string{"unix_socket_permission must be quoted"}},
{name: "quoted file mode", file: "unix_socket_permission: \"0770\"\n", check: func(t *testing.T, c *Config) {
t.Helper()
assert.Equal(t, FileMode(0o770), c.UnixSocketPermission)
}},
{name: "bool in a string field", file: "database:\n postgres:\n ssl: false\n", check: func(t *testing.T, c *Config) {
t.Helper()
assert.Equal(t, "false", c.Database.Postgres.Ssl)
}},
{name: "milliseconds stay a number", file: "database:\n gorm:\n slow_threshold: 1000\n", check: func(t *testing.T, c *Config) {
t.Helper()
assert.Equal(t, 1000, c.Database.Gorm.SlowThreshold)
}},
{name: "json integer", file: `{"database": {"postgres": {"port": 5432}}}`, check: func(t *testing.T, c *Config) {
t.Helper()
assert.Equal(t, 5432, c.Database.Postgres.Port)
}},
{name: "json fraction", file: `{"database": {"postgres": {"port": 5432.5}}}`, wantErr: []string{"database.postgres.port is not an integer"}},
{name: "json out of range", file: `{"database": {"postgres": {"port": 1e20}}}`, wantErr: []string{"database.postgres.port is out of range"}},
{name: "number 1 for a bool", file: "taildrop:\n enabled: 1\nlogtail:\n enabled: 1\n", check: func(t *testing.T, c *Config) {
t.Helper()
assert.True(t, c.Taildrop.Enabled)
assert.True(t, c.LogTail.Enabled)
}},
{name: "number 0 for a bool", file: "taildrop:\n enabled: 0\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.False(t, c.Taildrop.Enabled) }},
{name: "number 2 for a bool", file: "taildrop:\n enabled: 2\n", wantErr: []string{"taildrop.enabled is not a boolean"}},
{name: "env yes for a bool", env: []string{"HEADSCALE_TAILDROP_ENABLED=yes"}, wantErr: []string{"taildrop.enabled is not a boolean (env HEADSCALE_TAILDROP_ENABLED)"}},
{name: "empty prefix is unset", file: "prefixes:\n v4: \"\"\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.Nil(t, c.Prefixes.V4) }},
{name: "empty enum is unset", file: "log:\n format: \"\"\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.Empty(t, c.Log.Format) }},
{name: "env json list", env: []string{`HEADSCALE_DNS_SEARCH_DOMAINS=["a","b"]`}, check: func(t *testing.T, c *Config) {
t.Helper()
assert.Equal(t, []string{"a", "b"}, c.DNSConfig.SearchDomains)
}},
{name: "env whitespace list", env: []string{"HEADSCALE_DNS_SEARCH_DOMAINS=a b"}, check: func(t *testing.T, c *Config) {
t.Helper()
assert.Equal(t, []string{"a", "b"}, c.DNSConfig.SearchDomains)
}},
{name: "env map not json", env: []string{"HEADSCALE_DNS_NAMESERVERS_SPLIT=x=y"}, wantErr: []string{"dns.nameservers.split is not a JSON object (env HEADSCALE_DNS_NAMESERVERS_SPLIT)"}},
{name: "level is case-insensitive", file: "log:\n level: INFO\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.Equal(t, LogLevel("info"), c.Log.Level) }},
{name: "unknown extra record field", file: "dns:\n extra_records:\n - {name: a.example.com, type: A, value: 100.64.0.1, ttl: 60}\n", wantErr: []string{
"dns.extra_records has invalid keys: ttl",
`current: dns.extra_records: [{"name":"a.example.com","ttl":60,"type":"A","value":"100.64.0.1"}]`,
}},
{name: "empty duration", file: "node:\n expiry: \"\"\n", wantErr: []string{"node.expiry is not a duration"}},
{name: "empty integer", file: "database:\n postgres:\n port: \"\"\n", wantErr: []string{"database.postgres.port is not an integer"}},
{name: "empty bool", file: "taildrop:\n enabled: \"\"\n", wantErr: []string{"taildrop.enabled is not a boolean"}},
{name: "empty string is a value", file: "server_url: \"\"\n", check: func(t *testing.T, c *Config) { t.Helper(); assert.Empty(t, c.ServerURL) }},
{name: "env list not json", env: []string{"HEADSCALE_DNS_SEARCH_DOMAINS=[a"}, wantErr: []string{"dns.search_domains is not a JSON list", `hint: write the list as JSON, e.g. ["a", "b"]`}},
{name: "dotted top-level key", file: "log.level: verbose\n", wantErr: []string{"log.level has an unsupported value (", "config.yaml:1:1)"}},
{name: "nested then dotted key", file: "dns:\n magic_dns: false\n certificates.enabled: maybe\n", wantErr: []string{"dns.certificates.enabled is not a boolean (", "config.yaml:3:3)"}},
{name: "dotted then nested key", file: "dns:\n magic_dns: false\ndns.certificates:\n enabled: maybe\n", wantErr: []string{"dns.certificates.enabled is not a boolean (", "config.yaml:4:3)"}},
{name: "dotted inner list", file: "dns:\n nameservers.global: 1\n", wantErr: []string{"dns.nameservers.global ", "config.yaml:2:3)"}},
{name: "env extra records", env: []string{`HEADSCALE_DNS_EXTRA_RECORDS=[{"name":"a.example.com","type":"A","value":"100.64.0.1"}]`}, check: func(t *testing.T, c *Config) {
t.Helper()
assert.Equal(t, []tailcfg.DNSRecord{{Name: "a.example.com", Type: "A", Value: "100.64.0.1"}}, c.DNSConfig.ExtraRecords)
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
name := "config.yaml"
if strings.HasPrefix(tt.file, "{") {
name = "config.json"
}
cfg, err := decodeInput(t, name, tt.file, tt.env...)
if len(tt.wantErr) > 0 {
for _, want := range tt.wantErr {
require.ErrorContains(t, err, want)
}
return
}
require.NoError(t, err)
tt.check(t, cfg)
})
}
}
// TestDecodeErrors pins that every bad value is reported at once, before
// any rule runs, and that a credential's value never is.
func TestDecodeErrors(t *testing.T) {
t.Run("all bad values in one error", func(t *testing.T) {
require.NoError(t, LoadConfig(writeConfig(t, "log:\n level: verbose\nnode:\n expiry: 90\ntaildrop:\n enabled: maybe\n"), true))
_, err := LoadServerConfig()
require.Error(t, err)
got := reasons(ConfigErrors(err))
require.Len(t, got, 3, "%v", got)
assert.Contains(t, got[0], "log.level")
assert.Contains(t, got[1], "node.expiry")
assert.Contains(t, got[2], "taildrop.enabled")
// No rule ran: server_url and the noise key are missing too.
assert.NotContains(t, err.Error(), "server_url")
})
for name, tt := range map[string]struct {
file string
env []string
key string
}{
"postgres pass": {file: "database:\n postgres:\n pass: [marker-secret]\n", key: "database.postgres.pass"},
"oidc client secret": {file: "oidc:\n client_secret: [marker-secret]\n", key: "oidc.client_secret"},
"cli api key": {file: "cli:\n api_key: [marker-secret]\n", key: "cli.api_key"},
"derp url with a token": {file: "derp:\n urls: [\"https://marker-secret@:443\"]\n", key: "derp.urls"},
"derp url unparsable": {env: []string{"HEADSCALE_DERP_URLS=https://alice:marker-secret?@h"}, key: "derp.urls"},
"oidc issuer": {file: "oidc:\n issuer: https://alice:marker-secret?@idp.example.com\n", key: "oidc.issuer"},
"acme url": {file: "tls:\n acme:\n url: https://alice:marker-secret@acme.example.com:port\n", key: "tls.acme.url"},
"server url list": {file: "server_url: [\"https://alice:marker-secret@h\"]\n", key: "server_url"}, //nolint:gosec // a test marker, not a credential
"cli address list": {file: "cli:\n address: [\"https://alice:marker-secret@h\"]\n", key: "cli.address"}, //nolint:gosec // a test marker, not a credential
"nameserver element": {file: "dns:\n nameservers:\n global: [{url: \"https://alice:marker-secret@h\"}]\n", key: "dns.nameservers.global"}, //nolint:gosec // a test marker, not a credential
"split element": {file: "dns:\n nameservers:\n split:\n a.example.com: [[\"https://alice:marker-secret@h\"]]\n", key: "dns.nameservers.split"}, //nolint:gosec // a test marker, not a credential
"provider json": {env: []string{`HEADSCALE_DNS_CHALLENGE_PROVIDER={"api_token":"marker-secret"`}, key: "dns.challenge.provider is not a JSON object (env HEADSCALE_DNS_CHALLENGE_PROVIDER)"},
"provider scalar": {file: "dns:\n challenge:\n provider: marker-secret\n", key: "dns.challenge.provider"},
} {
t.Run(name, func(t *testing.T) {
_, err := decodeInput(t, "config.yaml", tt.file, tt.env...)
require.ErrorContains(t, err, tt.key)
requireHidden(t, err, "marker-secret")
})
}
}
func TestSchemaKeys(t *testing.T) {
open := map[string]bool{}
paths := map[string]bool{}
for _, k := range schemaKeys() {
paths[k.Path] = true
open[k.Path] = k.Open
}
for _, k := range []string{"dns.nameservers.split", "oidc.extra_params", "dns.challenge.provider"} {
assert.Truef(t, open[k], "%s is open", k)
}
assert.False(t, open["dns.extra_records"])
assert.NotContains(t, paths, "derp.server.stun.enabled")
assert.NotContains(t, paths, "dns.challenge.provider.name")
// Two keys sharing one environment variable would shadow each other.
envs := map[string]string{}
for _, k := range schemaKeys() {
name := configEnvName(k.Path)
assert.Emptyf(t, envs[name], "%s and %s share %s", envs[name], k.Path, name)
envs[name] = k.Path
}
}
// TestDefaultsFitTypes: every default is a value its field's type accepts,
// so the defaults layer decodes back to defaultConfig.
func TestDefaultsFitTypes(t *testing.T) {
d := defaultConfig()
for _, f := range ConfigFields(&d) {
e, ok := reflect.TypeAssert[interface{ Allowed() []string }](f.Value)
if !ok {
continue
}
v := f.Value.String()
assert.Truef(t, v == "" || slices.Contains(e.Allowed(), v), "%s defaults to %q, not in %v", f.Path, v, e.Allowed())
}
cfg, err := decodeInput(t, "", "")
require.NoError(t, err)
if diff := gocmp.Diff(d, *cfg, cmpopts.IgnoreFields(Config{}, "ConfigDir"), cmpopts.IgnoreUnexported(Config{})); diff != "" {
t.Errorf("defaults layer (-defaultConfig +decoded):\n%s", diff)
}
}
func TestPlainMap(t *testing.T) {
v4 := netip.MustParsePrefix("100.64.0.0/10")
cfg := &Config{
ServerURL: "https://h",
UnixSocketPermission: 0,
TrustedProxies: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/8")},
Prefixes: PrefixesConfig{V4: &v4},
Node: NodeConfig{Expiry: 90 * time.Minute},
DERP: DERPConfig{URLs: []HTTPURL{"https://user:pass@derp.example/map"}},
Database: DatabaseConfig{Postgres: PostgresConfig{Pass: "marker-pass"}},
OIDC: OIDCConfig{ClientSecret: "file:/run/secret"},
DNSConfig: DNSConfig{
ExtraRecords: []tailcfg.DNSRecord{{Name: "a.example.com", Type: "A", Value: "100.64.0.1"}},
Challenge: DNSChallengeConfig{ProviderSettings: map[string]any{ //nolint:gosec // test markers, not credentials
"name": "acmedns",
"api_token": 123456,
"token": "file:/run/token",
"config": map[string]any{"a.example.com": map[string]any{"password": "marker-pw", "name": "x"}},
"list": []any{"marker-item"},
}},
},
}
cfg.OIDC.Issuer = "https://client:marker-issuer@idp.example.com/realm"
cfg.TLS.ACME.URL = "https://marker-token@acme.example.com/dir?x=1"
cfg.DERP.URLs = append(cfg.DERP.URLs, "https://marker-only@derp.example/token", "https://h/%zz")
cfg.DNSConfig.Challenge.ProviderSettings["unset"] = (*netip.Prefix)(nil)
cfg.DNSConfig.Challenge.ProviderSettings["nothing"] = (*string)(nil)
plain := plainMap(cfg, false)
assert.Equal(t, "https://h", plain["server_url"])
assert.Equal(t, "0000", plain["unix_socket_permission"])
assert.Equal(t, []any{"10.0.0.0/8"}, plain["trusted_proxies"])
prefixes, _ := plain["prefixes"].(map[string]any)
assert.Equal(t, "100.64.0.0/10", prefixes["v4"])
assert.Empty(t, prefixes["v6"], "a nil prefix prints as an explicit unset")
assert.Contains(t, prefixes, "v6")
node, _ := plain["node"].(map[string]any)
assert.Equal(t, "1h30m0s", node["expiry"])
derp, _ := plain["derp"].(map[string]any)
assert.Equal(t, []any{
"https://user:pass@derp.example/map", "https://marker-only@derp.example/token", "https://h/%zz",
}, derp["urls"], "written as is, not as its JSON")
oidc, _ := plain["oidc"].(map[string]any)
assert.Equal(t, "https://client:marker-issuer@idp.example.com/realm", oidc["issuer"])
tls, _ := plain["tls"].(map[string]any)
acme, _ := tls["acme"].(map[string]any)
assert.Equal(t, "https://marker-token@acme.example.com/dir?x=1", acme["url"])
assert.NotContains(t, derp, "derpmap")
db, _ := plain["database"].(map[string]any)
pg, _ := db["postgres"].(map[string]any)
assert.Equal(t, "marker-pass", pg["pass"])
dns, _ := plain["dns"].(map[string]any)
assert.Equal(t, []any{map[string]any{"name": "a.example.com", "type": "A", "value": "100.64.0.1"}}, dns["extra_records"])
redacted := plainMap(cfg, true)
db, _ = redacted["database"].(map[string]any)
pg, _ = db["postgres"].(map[string]any)
assert.Equal(t, "<redacted>", pg["pass"])
oidc, _ = redacted["oidc"].(map[string]any)
assert.Equal(t, "file:/run/secret", oidc["client_secret"])
assert.Equal(t, "https://idp.example.com/realm", oidc["issuer"])
tls, _ = redacted["tls"].(map[string]any)
acme, _ = tls["acme"].(map[string]any)
assert.Equal(t, "https://acme.example.com/dir?x=1", acme["url"])
derp, _ = redacted["derp"].(map[string]any)
assert.Equal(t, []any{"https://derp.example/map", "https://derp.example/token", "<redacted>"}, derp["urls"])
dns, _ = redacted["dns"].(map[string]any)
challenge, _ := dns["challenge"].(map[string]any)
assert.Equal(t, map[string]any{ //nolint:gosec // test markers, not credentials
"name": "acmedns",
"api_token": "<redacted>",
"token": "file:/run/token",
"config": map[string]any{"a.example.com": map[string]any{"password": "<redacted>", "name": "<redacted>"}},
"list": []any{"<redacted>"},
}, challenge["provider"])
// The caller's config is left alone.
assert.Equal(t, 123456, cfg.DNSConfig.Challenge.ProviderSettings["api_token"])
assert.Equal(t, HTTPURL("https://user:pass@derp.example/map"), cfg.DERP.URLs[0])
assert.Equal(t, HTTPURL("https://client:marker-issuer@idp.example.com/realm"), cfg.OIDC.Issuer)
plainProvider, _ := plain["dns"].(map[string]any)["challenge"].(map[string]any)["provider"].(map[string]any)
assert.NotContains(t, plainProvider, "unset", "a typed nil is left out")
assert.NotContains(t, plainProvider, "nothing", "a typed nil is left out")
}
// TestPlainMapLargeIntegers: JSON parsers read numbers as float64, so an
// integer past 2^53 is written as text, which the integer hook takes back.
func TestPlainMapLargeIntegers(t *testing.T) {
for _, n := range []int{1<<53 + 1, math.MaxInt64} {
for _, ext := range []string{".json", ".hujson"} {
t.Run(fmt.Sprintf("%d%s", n, ext), func(t *testing.T) {
loaded, err := decodeInput(t, "config.yaml", fmt.Sprintf("tuning:\n register_cache_max_entries: %d\n", n))
require.NoError(t, err)
require.Equal(t, n, loaded.Tuning.RegisterCacheMaxEntries)
parser, err := configParser(ext)
require.NoError(t, err)
out, err := parser.Marshal(plainMap(loaded, false))
require.NoError(t, err)
again, err := decodeInput(t, "config"+ext, string(out))
require.NoError(t, err)
assert.Equal(t, n, again.Tuning.RegisterCacheMaxEntries)
})
}
}
}
// TestBadConfigs pins how a bad value is reported: its key, what is wrong
// and where it was set. Every testdata/bad/<name>.{yaml,env} is one row;
// refresh with HEADSCALE_UPDATE_GOLDEN=1.
func TestBadConfigs(t *testing.T) {
dir := filepath.Join("testdata", "bad")
update := os.Getenv("HEADSCALE_UPDATE_GOLDEN") != ""
entries, err := os.ReadDir(dir)
require.NoError(t, err)
unsetHeadscaleEnv(t)
for _, e := range entries {
ext := filepath.Ext(e.Name())
if ext == ".golden" {
continue
}
name := strings.TrimSuffix(e.Name(), ext)
t.Run(name, func(t *testing.T) {
var file, envFile string
if ext == ".env" {
envFile = filepath.Join(dir, e.Name())
} else {
file = filepath.Join(dir, e.Name())
}
staged := stageCompat(t, file, envFile)
require.NoError(t, LoadConfig(staged, false))
_, err := LoadServerConfig()
require.Error(t, err)
got := strings.ReplaceAll(err.Error(), staged, "$DIR")
gpath := filepath.Join(dir, name+".golden")
if update {
require.NoError(t, os.WriteFile(gpath, []byte(got), 0o600))
return
}
want, err := os.ReadFile(gpath)
require.NoErrorf(t, err, "missing golden %s: run with HEADSCALE_UPDATE_GOLDEN=1", gpath)
assert.Equal(t, string(want), got)
})
}
}
// TestPositionBounded: finding where a key is written follows the keys the
// file has, so a long dotted unknown key is located at once.
func TestPositionBounded(t *testing.T) {
key := strings.Repeat("x.", 29) + "x"
path := writeConfig(t, key+": 1\n")
done := make(chan error, 1)
go func() {
_, err := newConfigSource(path, true, nil)
done <- err
}()
select {
case err := <-done:
require.ErrorContains(t, err, "unknown configuration key "+key+" ("+path+":1:1)")
case <-time.After(5 * time.Second):
t.Fatal("locating a dotted key took longer than reading the file")
}
}
+312 -98
View File
@@ -4,16 +4,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"maps"
"net/netip"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
@@ -46,12 +42,12 @@ func TestReadConfig(t *testing.T) {
name: "unmarshal-dns-full-config",
configPath: "testdata/dns_full.yaml",
setup: func(t *testing.T) (any, error) { //nolint:thelper
dns, err := dns()
cfg, err := src.decode()
if err != nil {
return nil, err
}
return dns, nil
return cfg.DNSConfig, nil
},
want: DNSConfig{
MagicDNS: true,
@@ -75,18 +71,19 @@ func TestReadConfig(t *testing.T) {
{Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"},
},
SearchDomains: []string{"test.com", "bar.com"},
Challenge: DNSChallengeConfig{PropagationTimeout: 2 * time.Minute},
},
},
{
name: "dns-to-tailcfg.DNSConfig",
configPath: "testdata/dns_full.yaml",
setup: func(t *testing.T) (any, error) { //nolint:thelper
dns, err := dns()
cfg, err := src.decode()
if err != nil {
return nil, err
}
return dnsToTailcfgDNS(dns), nil
return dnsToTailcfgDNS(cfg.DNSConfig), nil
},
want: &tailcfg.DNSConfig{
Proxied: true,
@@ -112,12 +109,12 @@ func TestReadConfig(t *testing.T) {
name: "unmarshal-dns-full-no-magic",
configPath: "testdata/dns_full_no_magic.yaml",
setup: func(t *testing.T) (any, error) { //nolint:thelper
dns, err := dns()
cfg, err := src.decode()
if err != nil {
return nil, err
}
return dns, nil
return cfg.DNSConfig, nil
},
want: DNSConfig{
MagicDNS: false,
@@ -141,18 +138,19 @@ func TestReadConfig(t *testing.T) {
{Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"},
},
SearchDomains: []string{"test.com", "bar.com"},
Challenge: DNSChallengeConfig{PropagationTimeout: 2 * time.Minute},
},
},
{
name: "dns-to-tailcfg.DNSConfig",
configPath: "testdata/dns_full_no_magic.yaml",
setup: func(t *testing.T) (any, error) { //nolint:thelper
dns, err := dns()
cfg, err := src.decode()
if err != nil {
return nil, err
}
return dnsToTailcfgDNS(dns), nil
return dnsToTailcfgDNS(cfg.DNSConfig), nil
},
want: &tailcfg.DNSConfig{
Proxied: false,
@@ -215,17 +213,12 @@ func TestReadConfig(t *testing.T) {
name: "dns-override-true",
configPath: "testdata/dns-override-true.yaml",
setup: func(t *testing.T) (any, error) { //nolint:thelper
_, err := LoadServerConfig()
cfg, err := LoadServerConfig()
if err != nil {
return nil, err
}
dns, err := dns()
if err != nil {
return nil, err
}
return dnsToTailcfgDNS(dns), nil
return dnsToTailcfgDNS(cfg.DNSConfig), nil
},
want: &tailcfg.DNSConfig{
Proxied: true,
@@ -296,9 +289,11 @@ 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
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"))
cfg, err := src.decode()
require.NoError(t, err)
assert.Equal(t, LogLevel("trace"), cfg.Log.Level)
assert.Equal(t, "100.64.0.0/10", cfg.Prefixes.V4.String())
assert.False(t, cfg.Database.Sqlite.WriteAheadLog)
return nil, nil //nolint:nilnil // test setup returns nil to indicate no expected value
},
@@ -318,12 +313,12 @@ 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
dns, err := dns()
cfg, err := src.decode()
if err != nil {
return nil, err
}
return dns, nil
return cfg.DNSConfig, nil
},
want: DNSConfig{
MagicDNS: true,
@@ -339,6 +334,7 @@ func TestReadConfigFromEnv(t *testing.T) {
// {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"},
// },
SearchDomains: []string{"test.com", "bar.com"},
Challenge: DNSChallengeConfig{PropagationTimeout: 2 * time.Minute},
},
},
}
@@ -372,7 +368,7 @@ tls:
cert_path: abc.pem
acme:
hostname: example.com
challenge_type: bogus
challenge_type: ""
noise:
private_key_path: noise_private.key`)
@@ -454,8 +450,8 @@ oidc:
require.NoError(t, err)
err = validateServerConfig()
require.Error(t, err)
assert.Contains(t, err.Error(), errInvalidPKCEMethod.Error())
require.ErrorIs(t, err, errInvalidPKCEMethod)
assert.Contains(t, err.Error(), "oidc.pkce.method has an unsupported value")
}
// TestOIDCConfigValidation covers the issuer-URL and required-field checks that
@@ -472,7 +468,7 @@ func TestOIDCConfigValidation(t *testing.T) {
issuer: ftp://idp.example.com
client_id: headscale
client_secret: sekret`,
wantErr: "valid http(s) URL",
wantErr: "oidc.issuer is not an http:// or https:// URL",
},
{
name: "missing client_id",
@@ -505,6 +501,8 @@ func TestOIDCConfigValidation(t *testing.T) {
noise:
private_key_path: noise_private.key
server_url: http://127.0.0.1:8080
prefixes:
v4: 100.64.0.0/10
dns:
magic_dns: false
override_local_dns: false
@@ -558,6 +556,8 @@ tls:
http_listen: %q
noise:
private_key_path: noise_private.key
prefixes:
v4: 100.64.0.0/10
database:
type: sqlite3
dns:
@@ -606,7 +606,7 @@ dns:
require.Error(t, err)
got := ConfigErrors(err)
assert.GreaterOrEqual(t, len(got), 4,
assert.GreaterOrEqual(t, len(got), 3,
"expected sub-builder errors collected into one report; got %d: %v",
len(got), reasons(got))
@@ -1148,10 +1148,10 @@ func TestSafeServerURLWithPort(t *testing.T) {
// TestConfigJSONOmitsSecrets verifies that marshalling a [Config] to JSON
// (as /debug/config does via [state.State.DebugConfig]) or logging it does
// not leak the Postgres password, the OIDC client secret, or the headscale
// admin API key. Operators who widen metrics_listen_addr to 0.0.0.0 should
// not be able to read these back via debug endpoints reachable over
// CGNAT/loopback.
// not leak the Postgres password, the OIDC client secret, the headscale
// admin API key, or URL credentials. Operators who widen
// metrics_listen_addr to 0.0.0.0 should not be able to read these back via
// debug endpoints reachable over CGNAT/loopback.
func TestConfigJSONOmitsSecrets(t *testing.T) {
const (
secretPostgresPass = "p0stgres-secret-marker"
@@ -1162,9 +1162,16 @@ func TestConfigJSONOmitsSecrets(t *testing.T) {
secretDERPPass = "derp-url-password-marker" //nolint:gosec // test marker, not a real credential
secretDERPToken = "derp-url-token-marker" //nolint:gosec // test marker, not a real credential
derpMapMarker = "go-only-derp-map-marker"
secretServerUser = "server-url-username-marker" //nolint:gosec // test marker, not a real credential
secretServerPass = "server-url-password-marker" //nolint:gosec // test marker, not a real credential
secretCLIToken = "cli-address-token-marker" //nolint:gosec // test marker, not a real credential
secretDNSUser = "dns-url-username-marker" //nolint:gosec // test marker, not a real credential
secretDNSPass = "dns-url-password-marker" //nolint:gosec // test marker, not a real credential
secretSplitToken = "split-url-token-marker" //nolint:gosec // test marker, not a real credential
)
cfg := &Config{
ServerURL: "https://" + secretServerUser + ":" + secretServerPass + "@headscale.example.com",
Database: DatabaseConfig{
Postgres: PostgresConfig{
Pass: secretPostgresPass,
@@ -1174,12 +1181,19 @@ func TestConfigJSONOmitsSecrets(t *testing.T) {
ClientSecret: secretClientSecret,
},
CLI: CLIConfig{
APIKey: secretAPIKey,
Address: "https://" + secretCLIToken + "@cli.example.com",
APIKey: secretAPIKey,
},
DNSConfig: DNSConfig{
Nameservers: Nameservers{
Global: []string{"1.1.1.1", "https://" + secretDNSUser + ":" + secretDNSPass + "@dns.example.com/dns-query"},
Split: map[string][]string{"corp.example.com": {"https://" + secretSplitToken + "@dns.example.com/q"}},
},
Challenge: DNSChallengeConfig{
ProviderSettings: map[string]any{"name": "cloudflare", "api_token": secretDNSToken},
Provider: &cloudflare.Provider{APIToken: secretDNSToken},
},
},
DNSConfig: DNSConfig{Challenge: DNSChallengeConfig{
ProviderSettings: map[string]any{"name": "cloudflare", "api_token": secretDNSToken},
Provider: &cloudflare.Provider{APIToken: secretDNSToken},
}},
DERP: DERPConfig{
URLs: []HTTPURL{
"https://" + secretDERPUser + ":" + secretDERPPass + "@derp.example/map",
@@ -1194,6 +1208,7 @@ func TestConfigJSONOmitsSecrets(t *testing.T) {
secrets := []string{
secretPostgresPass, secretClientSecret, secretAPIKey, secretDNSToken,
secretDERPUser, secretDERPPass, secretDERPToken,
secretServerUser, secretServerPass, secretCLIToken, secretDNSUser, secretDNSPass, secretSplitToken,
}
out, err := json.Marshal(cfg)
@@ -1204,11 +1219,19 @@ func TestConfigJSONOmitsSecrets(t *testing.T) {
"marshalled Config must not contain secret %q", secret)
}
// The URLs are still shown, without userinfo; the request keeps it.
// The URLs are still shown, without userinfo; the running values keep it.
assert.Contains(t, string(out), `"https://derp.example/map"`)
assert.Contains(t, string(out), `"https://derp.example/token-map"`)
assert.Contains(t, string(out), `"ServerURL":"https://headscale.example.com"`)
assert.Contains(t, string(out), `"Address":"https://cli.example.com"`)
assert.Contains(t, string(out), `"Global":["1.1.1.1","https://dns.example.com/dns-query"]`)
assert.Contains(t, string(out), `"Split":{"corp.example.com":["https://dns.example.com/q"]}`)
assert.Contains(t, string(cfg.DERP.URLs[0]), secretDERPUser+":"+secretDERPPass)
assert.Contains(t, string(cfg.DERP.URLs[1]), secretDERPToken)
assert.Contains(t, cfg.ServerURL, secretServerUser+":"+secretServerPass)
assert.Contains(t, cfg.CLI.Address, secretCLIToken)
assert.Contains(t, cfg.DNSConfig.Nameservers.Global[1], secretDNSUser+":"+secretDNSPass)
assert.Contains(t, cfg.DNSConfig.Nameservers.Split["corp.example.com"][0], secretSplitToken)
// DERPMap is Go-only.
assert.NotContains(t, string(out), derpMapMarker)
@@ -1276,17 +1299,17 @@ func TestTrustedProxies(t *testing.T) {
{
name: "bare-ip-rejected",
input: []string{"10.0.0.1"},
wantErr: `trusted_proxies[0] "10.0.0.1"`,
wantErr: `netip.ParsePrefix("10.0.0.1")`,
},
{
name: "garbage-reports-index",
name: "garbage-names-the-value",
input: []string{"10.0.0.0/16", "not-an-ip"},
wantErr: `trusted_proxies[1] "not-an-ip"`,
wantErr: `trusted_proxies is not a valid CIDR`,
},
{
name: "ipv4-zero-rejected",
input: []string{"0.0.0.0/0"},
wantErr: "0.0.0.0/0 and ::/0 are not allowed",
wantErr: `trusted_proxies[0] "0.0.0.0/0": 0.0.0.0/0 and ::/0 are not allowed`,
},
{
name: "ipv6-zero-rejected",
@@ -1297,14 +1320,21 @@ func TestTrustedProxies(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
values := map[string]any{}
values := map[string]any{"prefixes.v4": "100.64.0.0/10"}
if tt.input != nil {
values["trusted_proxies"] = tt.input
}
useConfig(t, values)
got, err := trustedProxies()
var got []netip.Prefix
cfg, err := src.decode()
if err == nil {
v := &configValidator{}
cfg.validatePrefixes(v)
got, err = cfg.TrustedProxies, v.Err()
}
if tt.wantErr != "" {
require.Error(t, err)
@@ -1315,13 +1345,30 @@ func TestTrustedProxies(t *testing.T) {
require.NoError(t, err)
if diff := cmp.Diff(tt.want, got, cmpopts.EquateComparable(netip.Prefix{})); diff != "" {
t.Errorf("trustedProxies() mismatch (-want +got):\n%s", diff)
if diff := cmp.Diff(tt.want, got, cmpopts.EquateComparable(netip.Prefix{}), cmpopts.EquateEmpty()); diff != "" {
t.Errorf("trusted_proxies mismatch (-want +got):\n%s", diff)
}
})
}
}
// TestNameserverWarningsHideCredentials: a nameserver that does not parse
// is reported without the userinfo it may hold.
func TestNameserverWarningsHideCredentials(t *testing.T) {
cfg := &Config{DNSConfig: DNSConfig{Nameservers: Nameservers{
Global: []string{"https://alice:marker-pass@dns.example.com:port"},
Split: map[string][]string{"corp.example.com": {"https://marker-user@dns.example.com/%zz"}},
}}}
warnings := cfg.Warnings()
require.Len(t, warnings, 2)
for _, w := range warnings {
assert.Contains(t, w, "invalid nameserver")
assert.NotContains(t, w, "marker")
}
}
// DNS names are case-insensitive, but MagicDNS resolution in clients matches
// extra records by exact name, so mixed-case record names never resolve.
func TestExtraRecordsAreLowercased(t *testing.T) {
@@ -1687,14 +1734,14 @@ dns:
wantToken: "good",
},
{
// A failed decode can still fill part of the map; none of it is used.
// Named, never shown: the value holds credentials.
name: "provider env json that does not decode",
yaml: base,
env: map[string]string{
"HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "cloudflare",
"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"api_token":"marker-token","zone_token":1e1000}`,
},
wantErr: "dns.challenge.provider is invalid",
wantErr: "dns.challenge.provider is not a JSON object (env HEADSCALE_DNS_CHALLENGE_PROVIDER)",
},
{
// The removed field warns and is dropped; the rest still loads.
@@ -2372,7 +2419,7 @@ dns:
// 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
path := writeConfig(t, fmt.Sprintf(`server_url: https://headscale.example.com
noise:
private_key_path: noise_private.key
prefixes:
@@ -2392,49 +2439,6 @@ disable_check_updates: %t
}
}
func TestBoolConfigKeysComplete(t *testing.T) {
file, err := parser.ParseFile(token.NewFileSet(), "config.go", nil, 0)
require.NoError(t, err)
keys := make(map[string]bool)
ast.Inspect(file, func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
selector, ok := call.Fun.(*ast.SelectorExpr)
if !ok || selector.Sel.Name != "Bool" {
return true
}
object, ok := selector.X.(*ast.Ident)
if !ok || object.Name != "src" {
return true
}
require.Len(t, call.Args, 1)
literal, ok := call.Args[0].(*ast.BasicLit)
require.True(t, ok, "boolean config keys must be string literals")
key, err := strconv.Unquote(literal.Value)
require.NoError(t, err)
keys[key] = true
return true
})
require.NotEmpty(t, keys)
for _, key := range boolConfigKeys {
assert.Contains(t, keys, key, "unused or repeated boolean config key")
delete(keys, key)
}
assert.Empty(t, keys, "Bool keys missing from boolean validation")
}
func TestExampleConfigBooleanSettings(t *testing.T) {
require.NoError(t, LoadConfig("../../config-example.yaml", true))
@@ -2569,7 +2573,8 @@ func unsetHeadscaleEnv(t *testing.T) {
// for the nixpkgs module with only server_url, dns.base_domain and
// dns.nameservers.global set; hsic-* mirror integration/hsic's
// MinimumConfigYAML and DefaultConfigEnv. <name>-strict is the same source
// as updated for the strict loader.
// as updated for the strict loader, <name>-pruned without the keys removed
// for having no effect.
func TestConfigCompat(t *testing.T) {
rows := compatRows(t)
update := os.Getenv("HEADSCALE_UPDATE_GOLDEN") != ""
@@ -2784,7 +2789,6 @@ type compatPolicyConfig struct {
}
type compatTuning struct {
NotifierSendTimeout time.Duration
BatchChangeDelay time.Duration
NodeMapSessionBufferedChanSize int
BatcherWorkers int
@@ -2819,11 +2823,22 @@ func compatView(cfg *Config) compatConfig {
tuning := compatTuning(cfg.Tuning)
tuning.BatcherWorkers = cfg.Tuning.BatcherWorkerCount()
// Today's loader read an empty list as nil and an unset map as empty.
trusted := cfg.TrustedProxies
if len(trusted) == 0 {
trusted = nil
}
extraParams := oidc.ExtraParams
if extraParams == nil {
extraParams = map[string]string{}
}
return compatConfig{
ServerURL: cfg.ServerURL,
Addr: string(cfg.Addr),
MetricsAddr: string(cfg.MetricsAddr),
TrustedProxies: cfg.TrustedProxies,
TrustedProxies: trusted,
Node: compatNodeConfig{
Expiry: cfg.Node.Expiry,
Ephemeral: compatEphemeralConfig(cfg.Node.Ephemeral),
@@ -2920,7 +2935,7 @@ func compatView(cfg *Config) compatConfig {
Issuer: string(oidc.Issuer),
ClientID: oidc.ClientID,
Scope: oidc.Scope,
ExtraParams: oidc.ExtraParams,
ExtraParams: extraParams,
AllowedDomains: oidc.AllowedDomains,
AllowedUsers: oidc.AllowedUsers,
AllowedGroups: oidc.AllowedGroups,
@@ -3180,8 +3195,9 @@ func TestConfigNotLoaded(t *testing.T) {
func useConfig(t *testing.T, values map[string]any) {
t.Helper()
d := defaultConfig()
s := &configSource{k: koanf.New(".")}
require.NoError(t, s.k.Load(confmap.Provider(configDefaults, "."), nil))
require.NoError(t, s.k.Load(confmap.Provider(plainMap(&d, false), ""), nil))
require.NoError(t, s.k.Load(confmap.Provider(values, "."), nil))
old := src
@@ -3296,3 +3312,201 @@ func TestDerivedValues(t *testing.T) {
resolvers(written...).ResolverAddrs()
assert.Equal(t, []string{"9.9.9.9"}, written)
}
// validateServerConfig decodes the loaded configuration and validates it.
func validateServerConfig() error {
cfg, err := src.decode()
if err != nil {
return err
}
return cfg.Validate()
}
// TestValidate pins the rules on loaded configs and the order they report
// in.
func TestValidate(t *testing.T) {
const valid = `server_url: https://headscale.example.com
noise:
private_key_path: noise_private.key
prefixes:
v4: 100.64.0.0/10
database:
type: sqlite
dns:
magic_dns: false
override_local_dns: false
`
tests := []struct {
name string
file string
want []string // reasons, in order
}{
{name: "valid", file: valid},
{
// What the NixOS module writes: an explicit empty default.
name: "empty extra records next to a path",
file: valid + " extra_records: []\n extra_records_path: /var/lib/headscale/records.json\n",
},
{
name: "extra records next to a path",
file: valid + " extra_records: [{name: a.example.com, type: A, value: 100.64.0.1}]\n extra_records_path: /var/lib/headscale/records.json\n",
want: []string{"dns.extra_records and dns.extra_records_path are mutually exclusive"},
},
{
name: "every rule, in order",
file: `server_url: headscale.example.com
listen_addr: 0.0.0.0:8080
metrics_listen_addr: 0.0.0.0:8080
trusted_proxies: [0.0.0.0/0]
prefixes:
v4: ""
allocation: ""
policy:
mode: ""
node:
ephemeral:
inactivity_timeout: 30s
routes:
ha:
probe_interval: 1s
tuning:
node_store_batch_size: 0
derp:
server:
enabled: true
private_key_path: /var/lib/headscale/derp.key
database:
type: ""
dns:
magic_dns: true
override_local_dns: true
extra_records: [{name: a.example.com, type: A, value: 100.64.0.1}]
extra_records_path: /var/lib/headscale/records.json
challenge:
provider:
name: bogus
tls:
cert_path: /etc/headscale/cert.pem
acme:
hostname: example.com
challenge_type: ""
oidc:
issuer: https://idp.example.com
client_id: headscale
client_secret: secret
pkce:
method: ""
`,
want: []string{
"OIDC configuration is invalid",
"dns.extra_records and dns.extra_records_path are mutually exclusive",
"tls.acme.hostname and tls.cert_path/tls.key_path are mutually exclusive",
"noise.private_key_path is required",
"listen_addr and metrics_listen_addr would bind the same TCP socket",
"tls.acme.challenge_type has an unsupported value",
"server_url is missing a scheme",
"node.ephemeral.inactivity_timeout is below the minimum",
"dns.nameservers.global is required when dns.override_local_dns is true",
"node.routes.ha.probe_interval is below the minimum",
"node.routes.ha.probe_timeout must be less than node.routes.ha.probe_interval",
"tuning.node_store_batch_size must be positive",
"derp.server.stun_listen_addr is required when the embedded DERP server is enabled",
"database.type has an unsupported value",
"policy.mode has an unsupported value",
"dns.base_domain is required when dns.magic_dns is true",
"trusted_proxies contains an invalid CIDR",
"no IP prefix configured for the tailnet",
"prefixes.allocation has an unsupported value",
"dns.challenge.provider is invalid",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.NoError(t, LoadConfig(writeConfig(t, tt.file), true))
cfg, err := src.decode()
require.NoError(t, err)
err = cfg.Validate()
if len(tt.want) == 0 {
require.NoError(t, err)
return
}
assert.Equal(t, tt.want, reasons(ConfigErrors(err)))
})
}
}
// TestProviderNameShape: a provider name that is a map or list is rejected
// by its key alone, as its contents may be credentials, and never shown.
func TestProviderNameShape(t *testing.T) {
const base = `server_url: https://headscale.example.com
noise:
private_key_path: noise_private.key
prefixes:
v4: 100.64.0.0/10
database:
type: sqlite
dns:
magic_dns: true
override_local_dns: false
base_domain: ts.example.com
certificates:
enabled: true
`
for name, tt := range map[string]struct {
yaml string
env map[string]string
}{
"file map": {yaml: base + " challenge:\n provider:\n name: {api_token: marker-secret}\n"},
"file list": {yaml: base + " challenge:\n provider:\n name: [marker-secret]\n"},
"env json": {yaml: base, env: map[string]string{"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"name":{"api_token":"marker-secret"}}`}},
} {
t.Run(name, func(t *testing.T) {
for k, v := range tt.env {
t.Setenv(k, v)
}
require.NoError(t, LoadConfig(writeConfig(t, tt.yaml), true))
cfg, err := src.decode()
require.NoError(t, err)
out, err := json.Marshal(plainMap(cfg, true))
require.NoError(t, err)
assert.NotContains(t, string(out), "marker-secret")
_, err = LoadServerConfig()
require.Error(t, err)
assert.Equal(t, []string{"dns.challenge.provider.name must be a provider name"}, reasons(ConfigErrors(err)))
assert.NotContains(t, err.Error(), "marker-secret")
})
}
// A scalar name is coerced to its text, as before.
assert.Equal(t, "123", DNSChallengeConfig{ProviderSettings: map[string]any{"name": 123}}.ProviderName())
}
// TestUpdateCheckDisabled: a configuration that does not decode never runs
// the update check, whatever it says; the command then reports the error.
func TestUpdateCheckDisabled(t *testing.T) {
for _, tt := range []struct {
file string
want bool
}{
{"disable_check_updates: true\n", true},
{"disable_check_updates: false\n", false},
{"disable_check_updates: true\nlog:\n level: verbose\n", true},
{"disable_check_updates: false\nlog:\n level: verbose\n", true},
} {
require.NoError(t, LoadConfig(writeConfig(t, tt.file), true))
assert.Equalf(t, tt.want, UpdateCheckDisabled(), "%q", tt.file)
}
}
+23 -13
View File
@@ -90,22 +90,32 @@ func (m FileMode) MarshalText() ([]byte, error) {
// text form stays as written.
type HTTPURL string //nolint:recvcheck // UnmarshalText needs a pointer; the rest read the value
// MarshalJSON writes u without userinfo, for debug output and logs: a
// username can be a token too. One that does not parse is withheld, as its
// userinfo cannot be found.
// MarshalJSON writes u [HTTPURL.Redacted], for debug output and logs.
func (u HTTPURL) MarshalJSON() ([]byte, error) {
s := string(u)
if s != "" {
p, err := url.Parse(s)
if err != nil {
s = "<redacted>"
} else {
p.User = nil
s = p.String()
}
return json.Marshal(u.Redacted())
}
// Redacted is u without userinfo: a username can be a token too. One that
// is not an absolute http(s) URL with a host is withheld, as its userinfo
// cannot be told from the rest (https://u:p?@h fails to parse,
// https:/u:p@h parses as a path); one without userinfo is kept as written.
func (u HTTPURL) Redacted() string {
if u == "" {
return ""
}
return json.Marshal(s)
p, err := url.Parse(string(u))
if err != nil || (p.Scheme != "http" && p.Scheme != "https") || p.Hostname() == "" {
return "<redacted>"
}
if p.User == nil {
return string(u)
}
p.User = nil
return p.String()
}
func (u *HTTPURL) UnmarshalText(text []byte) error {
+3
View File
@@ -46,6 +46,9 @@ func TestConfigValues(t *testing.T) {
{name: "listenaddr/named port", dst: new(ListenAddr), in: "0.0.0.0:http", want: ListenAddr("0.0.0.0:http")},
{name: "listenaddr/no colon", dst: new(ListenAddr), in: "8080", wantErr: "host:port"},
{name: "listenaddr/bad port", dst: new(ListenAddr), in: "h:abc", wantErr: "host:port"},
{name: "listenaddr/port above range", dst: new(ListenAddr), in: "h:65536", wantErr: "host:port"},
{name: "listenaddr/negative port", dst: new(ListenAddr), in: "h:-1", wantErr: "host:port"},
{name: "listenaddr/highest port", dst: new(ListenAddr), in: "h:65535", want: ListenAddr("h:65535")},
{name: "loglevel/trace", dst: new(LogLevel), in: "trace", want: LogLevel("trace")},
{name: "loglevel/debug", dst: new(LogLevel), in: "debug", want: LogLevel("debug")},
+8 -11
View File
@@ -2,22 +2,19 @@ package types
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestDerpConfigSkipsMalformedURL ensures a malformed derp.urls entry is
// skipped (as the "ignoring..." log promises) rather than dereferencing the
// nil *url.URL that url.Parse returns on error, which crashed the server at
// startup.
func TestDerpConfigSkipsMalformedURL(t *testing.T) {
// TestDERPURLsMalformed: a malformed derp.urls entry stops the load, naming
// the key, instead of being skipped, as it once was after crashing startup
// on the nil *url.URL url.Parse returns.
func TestDERPURLsMalformed(t *testing.T) {
useConfig(t, map[string]any{"derp.urls": []string{
"https://controlplane.tailscale.com/derpmap/default",
"://bad",
}})
cfg := derpConfig()
if len(cfg.URLs) != 1 {
t.Fatalf("expected the malformed derp.urls entry to be skipped, got %d urls: %v",
len(cfg.URLs), cfg.URLs)
}
_, err := src.decode()
require.ErrorContains(t, err, "derp.urls is not an http:// or https:// URL")
}
+9 -9
View File
@@ -30,7 +30,7 @@ func (e *ListenerBindError) Error() string {
func (e *ListenerBindError) Unwrap() error { return e.Err }
// PortFromAddr resolves the numeric port of a TCP listen address.
// Accepts host:port form with either a numeric port or one of the named
// Accepts host:port form with either a numeric port from 0 to 65535 or one of the named
// services "http" / "https". The named-service table is intentionally
// hardcoded so this stays a pure string->int mapping with no network or
// /etc/services lookups.
@@ -51,12 +51,12 @@ func PortFromAddr(addr string) (int, error) {
return 443, nil
}
p, err := strconv.Atoi(port)
p, err := strconv.ParseUint(port, 10, 16)
if err != nil {
return 0, fmt.Errorf("parse port from %q: %w", addr, err)
}
return p, nil
return int(p), nil
}
// listenersOverlap reports whether two parsed TCP listen addresses would
@@ -95,7 +95,7 @@ func isWildcardHost(h string) bool {
// produces a ConfigError tied to its own YAML key, so an operator can
// see exactly which value to fix instead of guessing from a paired
// comparison.
func validateListenerCollisions(v *configValidator) {
func (c *Config) validateListenerCollisions(v *configValidator) {
type spec struct {
key, addr string
host string
@@ -104,9 +104,9 @@ func validateListenerCollisions(v *configValidator) {
active bool
}
listenAddr := src.String("listen_addr")
metricsAddr := src.String("metrics_listen_addr")
acmeAddr := src.String("tls.acme.http_listen")
listenAddr := string(c.Addr)
metricsAddr := string(c.MetricsAddr)
acmeAddr := string(c.TLS.ACME.HTTPListen)
listeners := []spec{
{key: "listen_addr", addr: listenAddr, active: listenAddr != ""},
@@ -115,8 +115,8 @@ func validateListenerCollisions(v *configValidator) {
key: "tls.acme.http_listen",
addr: acmeAddr,
active: acmeAddr != "" &&
src.String("tls.acme.hostname") != "" &&
ACMEChallenge(src.String("tls.acme.challenge_type")) == HTTP01ChallengeType,
c.TLS.ACME.Hostname != "" &&
c.TLS.ACME.ChallengeType == HTTP01ChallengeType,
},
}
+15 -2
View File
@@ -30,6 +30,10 @@ func TestPortFromAddr(t *testing.T) {
{"empty", "", 0, true},
{"no-port", "0.0.0.0", 0, true},
{"unknown-named", ":bogus", 0, true},
{"highest-port", ":65535", 65535, false},
{"port-above-range", ":65536", 0, true},
{"negative-port", ":-1", 0, true},
{"signed-port", ":+80", 0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -86,10 +90,19 @@ func TestValidateListenerCollisions_BlamesParseFailure(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
useConfig(t, map[string]any{tt.bad: tt.badAddr, tt.good: ":9999"})
// Decoding rejects these; Go data reaches the rule as is.
cfg := &Config{}
for _, f := range ConfigFields(cfg) {
switch f.Path {
case tt.bad:
f.Value.SetString(tt.badAddr)
case tt.good:
f.Value.SetString(":9999")
}
}
v := &configValidator{}
validateListenerCollisions(v)
cfg.validateListenerCollisions(v)
err := v.Err()
require.Error(t, err)
+3
View File
@@ -0,0 +1,3 @@
Fatal config error: node.expiry needs a unit ($DIR/config.yaml:2:3)
current: node.expiry: 90
hint: write the unit, e.g. "30s", "5m" or "90d"
+2
View File
@@ -0,0 +1,2 @@
node:
expiry: 90
+5
View File
@@ -0,0 +1,5 @@
Fatal config error: database.type has an unsupported value ($DIR/config.yaml:2:3)
current: database.type: "mysql"
allowed: "sqlite", "sqlite3", "postgres"
hint: pick one of the allowed values; sqlite is the default for single-host deployments
see: https://headscale.net/stable/ref/database/
+2
View File
@@ -0,0 +1,2 @@
database:
type: mysql
+1
View File
@@ -0,0 +1 @@
HEADSCALE_TUNING_BATCH_CHANGE_DELAY=800
+3
View File
@@ -0,0 +1,3 @@
Fatal config error: tuning.batch_change_delay needs a unit (env HEADSCALE_TUNING_BATCH_CHANGE_DELAY)
current: tuning.batch_change_delay: "800"
hint: write the unit, e.g. "30s", "5m" or "90d"
@@ -0,0 +1,2 @@
Fatal config error: dns.extra_records has invalid keys: ttl ($DIR/config.yaml:2:3)
current: dns.extra_records: [{"name":"a.example.com","ttl":60,"type":"A","value":"100.64.0.1"}]
+6
View File
@@ -0,0 +1,6 @@
dns:
extra_records:
- name: a.example.com
type: A
value: 100.64.0.1
ttl: 60
+4
View File
@@ -0,0 +1,4 @@
Fatal config error: unix_socket_permission must be quoted ($DIR/config.yaml:1:1)
current: unix_socket_permission: 504
maximum: "0777"
hint: quote the value, e.g. "0770"; unquoted, YAML reads 0770 as the number 504
+1
View File
@@ -0,0 +1 @@
unix_socket_permission: 0770
+2
View File
@@ -0,0 +1,2 @@
Fatal config error: derp.urls is not an http:// or https:// URL ($DIR/config.yaml:2:3)
hint: write the scheme and host, e.g. "https://example.com"
+4
View File
@@ -0,0 +1,4 @@
derp:
urls:
- https://derp.example.com/derp.json
- "://not-a-url"
+1 -1
View File
@@ -1,3 +1,3 @@
{
"error": "Fatal config error: policy.mode has an unsupported value\n current: policy.mode: \"databse\"\n allowed: \"file\", \"database\"\n see: https://headscale.net/stable/ref/policy/\n"
"error": "Fatal config error: policy.mode has an unsupported value ($DIR/config.yaml:15:3)\n current: policy.mode: \"databse\"\n allowed: \"file\", \"database\"\n see: https://headscale.net/stable/ref/policy/\n"
}
+3
View File
@@ -0,0 +1,3 @@
{
"error": "Fatal config error: log.level has an unsupported value ($DIR/config.yaml:15:3)\n current: log.level: \"verbose\"\n allowed: \"trace\", \"debug\", \"info\", \"warn\", \"error\", \"fatal\", \"panic\", \"disabled\"\n\nFatal config error: log.format has an unsupported value ($DIR/config.yaml:16:3)\n current: log.format: \"xml\"\n allowed: \"text\", \"json\"\n\nFatal config error: derp.urls is not an http:// or https:// URL ($DIR/config.yaml:20:3)\n hint: write the scheme and host, e.g. \"https://example.com\"\n\nFatal config error: tuning.batch_change_delay is not a duration ($DIR/config.yaml:18:3)\n current: tuning.batch_change_delay: \"abc\"\n hint: write the unit, e.g. \"30s\", \"5m\" or \"90d\"\n"
}
+21
View File
@@ -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:
batch_change_delay: abc
derp:
urls:
- "://not-a-url"
+1 -167
View File
@@ -1,169 +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": [
"\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: configuration key tuning.notifier_send_timeout was removed in 0.30 ($DIR/config.yaml:18:3)\n hint: remove it: it had no effect\n"
}
+1 -1
View File
@@ -1,3 +1,3 @@
{
"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"
"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\nFatal config error: configuration key tuning.notifier_send_timeout was removed in 0.30 ($DIR/config.yaml:19:3)\n hint: remove it: it had no effect\n"
}
-1
View File
@@ -142,7 +142,6 @@
"Mode": ""
},
"Tuning": {
"NotifierSendTimeout": 0,
"BatchChangeDelay": 0,
"NodeMapSessionBufferedChanSize": 0,
"BatcherWorkers": 0,
-1
View File
@@ -163,7 +163,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
+174 -2
View File
@@ -1,6 +1,178 @@
{
"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": [
{
"Name": "a.example.com",
"Type": "A",
"Value": "100.64.0.1"
}
],
"ExtraRecordsPath": "",
"Certificates": {
"Enabled": false,
"Zone": ""
}
},
"TailcfgDNSConfig": {
"ExtraRecords": [
{
"Name": "a.example.com",
"Type": "A",
"Value": "100.64.0.1"
}
]
},
"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": {
"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: dns.extra_records cannot be parsed\n current: dns.extra_records: \"\u003cinline records\u003e\"\n why: unmarshalling dns extra records: decoding failed due to the following error(s):\n\n'[0]' expected a map or struct, got \"string\"\n'[1]' expected a map or struct, got \"string\"\n'[2]' expected a map or struct, got \"string\"\n hint: check the YAML syntax; see config-example.yaml for the expected shape\n"
]
}
-1
View File
@@ -149,7 +149,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
-1
View File
@@ -149,7 +149,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
-1
View File
@@ -181,7 +181,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
-1
View File
@@ -149,7 +149,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
-1
View File
@@ -168,7 +168,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
-1
View File
@@ -149,7 +149,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
+23
View File
@@ -0,0 +1,23 @@
HEADSCALE_SERVER_URL=https://hs.example.com
HEADSCALE_NOISE_PRIVATE_KEY_PATH=/var/lib/headscale/noise.key
HEADSCALE_PREFIXES_V4=100.64.0.0/10
HEADSCALE_DATABASE_TYPE=sqlite
HEADSCALE_DATABASE_SQLITE_PATH=/var/lib/headscale/db.sqlite
HEADSCALE_DNS_MAGIC_DNS=false
HEADSCALE_DNS_OVERRIDE_LOCAL_DNS=false
# Lists: JSON arrays, or split on whitespace; commas are not separators.
HEADSCALE_DNS_SEARCH_DOMAINS=["a.example.com","b.example.com"]
HEADSCALE_DNS_NAMESERVERS_GLOBAL=1.1.1.1,8.8.8.8
HEADSCALE_OIDC_ALLOWED_DOMAINS=["example.com"]
HEADSCALE_OIDC_ALLOWED_GROUPS= a b
HEADSCALE_DERP_URLS=https://a.example.com/derp.json https://b.example.com/derp.json
HEADSCALE_TRUSTED_PROXIES=10.0.0.0/8 192.168.0.0/16
# Maps: JSON, a number value is its text, key case is kept.
HEADSCALE_DNS_NAMESERVERS_SPLIT={"corp.example.com":["10.0.0.1"]}
HEADSCALE_OIDC_EXTRA_PARAMS={"Domain_Hint":"env","n":1}
# Scalars: durations with a unit, octal mode as text, numeric bool.
HEADSCALE_TUNING_BATCH_CHANGE_DELAY=800ms
HEADSCALE_DERP_UPDATE_FREQUENCY=30s
HEADSCALE_NODE_EXPIRY=90d
HEADSCALE_UNIX_SOCKET_PERMISSION=0700
HEADSCALE_TAILDROP_ENABLED=0
+228
View File
@@ -0,0 +1,228 @@
{
"config": {
"ServerURL": "https://hs.example.com",
"Addr": "",
"MetricsAddr": "",
"TrustedProxies": [
"10.0.0.0/8",
"192.168.0.0/16"
],
"Node": {
"Expiry": 7776000000000000,
"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": [
{
"Scheme": "https",
"Opaque": "",
"User": null,
"Host": "a.example.com",
"Path": "/derp.json",
"Fragment": "",
"RawQuery": "",
"RawPath": "",
"RawFragment": "",
"ForceQuery": false,
"OmitHost": false
},
{
"Scheme": "https",
"Opaque": "",
"User": null,
"Host": "b.example.com",
"Path": "/derp.json",
"Fragment": "",
"RawQuery": "",
"RawPath": "",
"RawFragment": "",
"ForceQuery": false,
"OmitHost": false
}
],
"Paths": null,
"DERPMap": null,
"AutoUpdate": false,
"UpdateFrequency": 30000000000,
"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": [
"1.1.1.1,8.8.8.8"
],
"Split": {
"corp.example.com": [
"10.0.0.1"
]
}
},
"SearchDomains": [
"a.example.com",
"b.example.com"
],
"ExtraRecords": null,
"ExtraRecordsPath": "",
"Certificates": {
"Enabled": false,
"Zone": ""
}
},
"TailcfgDNSConfig": {
"Routes": {
"corp.example.com": [
{
"Addr": "10.0.0.1"
}
]
},
"FallbackResolvers": [
{
"Addr": "1.1.1.1,8.8.8.8"
}
],
"Domains": [
"a.example.com",
"b.example.com"
]
},
"DNSChallenge": {
"PropagationTimeout": 120000000000,
"Resolvers": [
"1.1.1.1:53",
"8.8.8.8:53"
],
"ProviderName": ""
},
"UnixSocket": "/var/run/headscale/headscale.sock",
"UnixSocketPermission": 448,
"OIDC": {
"OnlyStartIfOIDCIsAvailable": true,
"Issuer": "",
"ClientID": "",
"Scope": [
"openid",
"profile",
"email"
],
"ExtraParams": {
"Domain_Hint": "env",
"n": "1"
},
"AllowedDomains": [
"example.com"
],
"AllowedUsers": null,
"AllowedGroups": [
"a",
"b"
],
"EmailVerifiedRequired": true,
"UseExpiryFromToken": false,
"PKCE": {
"Enabled": false,
"Method": "S256"
}
},
"LogTail": {
"Enabled": false
},
"Taildrop": {
"Enabled": false
},
"AutoUpdate": {
"Enabled": false
},
"CLI": {
"Address": "",
"Timeout": 5000000000,
"Insecure": false
},
"Policy": {
"Path": "",
"Mode": "file"
},
"Tuning": {
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
"RegisterCacheExpiration": 0,
"RegisterCacheMaxEntries": 0,
"NodeStoreBatchSize": 100,
"NodeStoreBatchTimeout": 500000000
}
},
"warnings": [
"no config file found, using defaults"
]
}
+3 -214
View File
@@ -1,217 +1,6 @@
{
"config": {
"ServerURL": "https://hs.example.com",
"Addr": "",
"MetricsAddr": "",
"TrustedProxies": [
"10.0.0.0/8",
"192.168.0.0/16"
],
"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": [
{
"Scheme": "https",
"Opaque": "",
"User": null,
"Host": "a.example.com",
"Path": "/derp.json",
"Fragment": "",
"RawQuery": "",
"RawPath": "",
"RawFragment": "",
"ForceQuery": false,
"OmitHost": false
},
{
"Scheme": "https",
"Opaque": "",
"User": null,
"Host": "b.example.com",
"Path": "/derp.json",
"Fragment": "",
"RawQuery": "",
"RawPath": "",
"RawFragment": "",
"ForceQuery": false,
"OmitHost": false
}
],
"Paths": null,
"DERPMap": null,
"AutoUpdate": false,
"UpdateFrequency": 30,
"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": [
"1.1.1.1,8.8.8.8"
],
"Split": {}
},
"SearchDomains": [
"[\"a.example.com\",\"b.example.com\"]"
],
"ExtraRecords": null,
"ExtraRecordsPath": "",
"Certificates": {
"Enabled": false,
"Zone": ""
}
},
"TailcfgDNSConfig": {
"FallbackResolvers": [
{
"Addr": "1.1.1.1,8.8.8.8"
}
],
"Domains": [
"[\"a.example.com\",\"b.example.com\"]"
]
},
"DNSChallenge": {
"PropagationTimeout": 120000000000,
"Resolvers": [
"1.1.1.1:53",
"8.8.8.8:53"
],
"ProviderName": ""
},
"UnixSocket": "/var/run/headscale/headscale.sock",
"UnixSocketPermission": 448,
"OIDC": {
"OnlyStartIfOIDCIsAvailable": true,
"Issuer": "",
"ClientID": "",
"Scope": [
"openid",
"profile",
"email"
],
"ExtraParams": {
"Domain_Hint": "env",
"n": ""
},
"AllowedDomains": [
"[\"example.com\"]"
],
"AllowedUsers": null,
"AllowedGroups": [
"a",
"b"
],
"EmailVerifiedRequired": true,
"UseExpiryFromToken": false,
"PKCE": {
"Enabled": false,
"Method": "S256"
}
},
"LogTail": {
"Enabled": false
},
"Taildrop": {
"Enabled": false
},
"AutoUpdate": {
"Enabled": false
},
"CLI": {
"Address": "",
"Timeout": 5000000000,
"Insecure": false
},
"Policy": {
"Path": "",
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
"RegisterCacheExpiration": 0,
"RegisterCacheMaxEntries": 0,
"NodeStoreBatchSize": 100,
"NodeStoreBatchTimeout": 500000000
}
},
"warnings": [
"no config file found, using defaults",
"failed to parse node.expiry, defaulting to no expiry"
]
"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"
}
-1
View File
@@ -187,7 +187,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
@@ -0,0 +1 @@
COMPAT_SECRETS=testdata/compat
+251
View File
@@ -0,0 +1,251 @@
{
"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": {
"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"
]
}
@@ -0,0 +1,142 @@
# 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:
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
+1 -250
View File
@@ -1,252 +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": 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"
]
"error": "Fatal config error: configuration key tuning.notifier_send_timeout was removed in 0.30 ($DIR/config.yaml:136:3)\n hint: remove it: it had no effect\n"
}
+1 -1
View File
@@ -1,3 +1,3 @@
{
"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"
"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\nFatal config error: configuration key tuning.notifier_send_timeout was removed in 0.30 ($DIR/config.yaml:136:3)\n hint: remove it: it had no effect\n"
}
-1
View File
@@ -165,7 +165,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
-1
View File
@@ -165,7 +165,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
-1
View File
@@ -142,7 +142,6 @@
"Mode": ""
},
"Tuning": {
"NotifierSendTimeout": 0,
"BatchChangeDelay": 0,
"NodeMapSessionBufferedChanSize": 0,
"BatcherWorkers": 0,
-1
View File
@@ -179,7 +179,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
-1
View File
@@ -179,7 +179,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
@@ -154,7 +154,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
@@ -165,7 +165,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,
@@ -149,7 +149,6 @@
"Mode": "file"
},
"Tuning": {
"NotifierSendTimeout": 800000000,
"BatchChangeDelay": 800000000,
"NodeMapSessionBufferedChanSize": 30,
"BatcherWorkers": 0,