types: give config values their own types

Updates #1926
This commit is contained in:
Kristoffer Dalby authored and GitHub committed 2026-11-11 11:19:27 +00:00
1 parent 565aaf7e49
commit 3a3a10edf9
58 files changed
+1720 -813

No files matched your search

+1 -1
View File
@@ -189,7 +189,7 @@ func loadCheckedConfig(cmd *cobra.Command) (*types.Config, string, error) {
return nil, src, err
}
cfg, err := types.CheckServerConfig()
cfg, err := types.LoadServerConfig()
return cfg, src, err
}
+3 -4
View File
@@ -201,10 +201,11 @@ func newV2Client() (context.Context, *clientv2.ClientWithResponses, context.Canc
return ctx, client, cancel, nil
}
if cfg.CLI.APIKey == "" {
apiKey, err := cliAPIKey(cfg.CLI)
if err != nil {
cancel()
return nil, nil, nil, errAPIKeyNotSet
return nil, nil, nil, err
}
transport := &http.Transport{}
@@ -213,8 +214,6 @@ func newV2Client() (context.Context, *clientv2.ClientWithResponses, context.Canc
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
apiKey := cfg.CLI.APIKey
client, err := clientv2.NewClientWithResponses(
clientBaseURL(cfg.CLI.Address),
clientv2.WithHTTPClient(&http.Client{Transport: transport}),
+1 -1
View File
@@ -99,7 +99,7 @@ func initConfig() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}
if types.LogFormat() == types.JSONLogFormat {
if types.LogFormatSetting() == types.JSONLogFormat {
log.Logger = log.Output(os.Stdout)
}
+18 -3
View File
@@ -173,11 +173,11 @@ func newHeadscaleCLIWithConfig() (context.Context, *clientv1.ClientWithResponses
}
// Remote connections require an API key for authentication.
apiKey := cfg.CLI.APIKey
if apiKey == "" {
apiKey, err := cliAPIKey(cfg.CLI)
if err != nil {
cancel()
return nil, nil, nil, errAPIKeyNotSet
return nil, nil, nil, err
}
client, err := newRemoteClient(address, apiKey, cfg.CLI.Insecure)
@@ -192,6 +192,21 @@ func newHeadscaleCLIWithConfig() (context.Context, *clientv1.ClientWithResponses
return ctx, client, cancel, nil
}
// cliAPIKey reads the key a remote CLI sends; over the unix socket the CLI
// needs none, so it must not call this.
func cliAPIKey(cfg types.CLIConfig) (string, error) {
key, err := types.ResolveSecret("cli.api_key", string(cfg.APIKey))
if err != nil {
return "", err
}
if key == "" {
return "", errAPIKeyNotSet
}
return key, nil
}
// newSocketClient builds an API client that dials the local unix socket. The
// base-URL host is irrelevant; the custom dialer routes every request to the
// socket.
+69 -1
View File
@@ -1,6 +1,14 @@
package cli
import "testing"
import (
"os"
"path/filepath"
"testing"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClientBaseURL(t *testing.T) {
tests := []struct {
@@ -33,3 +41,63 @@ func TestClientBaseURL(t *testing.T) {
})
}
}
// TestCLIAPIKey pins that only a remote CLI reads cli.api_key; over the unix
// socket the key is never read.
func TestCLIAPIKey(t *testing.T) {
dir := t.TempDir()
keyFile := filepath.Join(dir, "key")
require.NoError(t, os.WriteFile(keyFile, []byte("from-file\n"), 0o600))
missing := "file:" + filepath.Join(dir, "missing")
tests := []struct {
name string
address string
apiKey string
want string // the key a remote CLI sends
wantErr string
}{
{name: "remote literal", address: "headscale.example.com:443", apiKey: "literal", want: "literal"},
{name: "remote reads the file", address: "headscale.example.com:443", apiKey: "file:" + keyFile, want: "from-file"},
{name: "remote file missing", address: "headscale.example.com:443", apiKey: missing, wantErr: "cli.api_key: "},
{name: "remote without a key", address: "headscale.example.com:443", wantErr: errAPIKeyNotSet.Error()},
{name: "socket never reads", apiKey: missing},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfgDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(cfgDir, "config.yaml"),
[]byte("unix_socket: "+filepath.Join(cfgDir, "headscale.sock")+"\n"), 0o600))
t.Setenv("HEADSCALE_CLI_ADDRESS", tt.address)
t.Setenv("HEADSCALE_CLI_API_KEY", tt.apiKey)
require.NoError(t, types.LoadConfig(cfgDir, false))
if tt.address != "" && tt.wantErr == "" {
cfg, err := types.LoadCLIConfig()
require.NoError(t, err)
got, err := cliAPIKey(cfg.CLI)
require.NoError(t, err)
assert.Equal(t, tt.want, got)
}
_, _, cancel, err := newHeadscaleCLIWithConfig()
_, _, cancelV2, errV2 := newV2Client()
for _, err := range []error{err, errV2} {
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
} else {
require.NoError(t, err)
}
}
if err == nil {
cancel()
cancelV2()
}
})
}
}
+5 -6
View File
@@ -1,7 +1,6 @@
package main
import (
"io/fs"
"os"
"path/filepath"
"testing"
@@ -62,13 +61,13 @@ func assertExampleConfig(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "http://127.0.0.1:8080", cfg.ServerURL)
assert.Equal(t, "127.0.0.1:8080", cfg.Addr)
assert.Equal(t, "127.0.0.1:9090", cfg.MetricsAddr)
assert.Equal(t, types.ListenAddr("127.0.0.1:8080"), cfg.Addr)
assert.Equal(t, types.ListenAddr("127.0.0.1:9090"), cfg.MetricsAddr)
assert.True(t, cfg.Database.IsSQLite(), cfg.Database.Type)
assert.Equal(t, "/var/lib/headscale/db.sqlite", cfg.SQLitePath())
assert.Empty(t, cfg.TLS.ACME.Hostname)
assert.Equal(t, ":http", cfg.TLS.ACME.HTTPListen)
assert.Equal(t, "HTTP-01", cfg.TLS.ACME.ChallengeType)
assert.Equal(t, fs.FileMode(0o770), cfg.UnixSocketPermission)
assert.Equal(t, types.ListenAddr(":http"), cfg.TLS.ACME.HTTPListen)
assert.Equal(t, types.HTTP01ChallengeType, cfg.TLS.ACME.ChallengeType)
assert.Equal(t, types.FileMode(0o770), cfg.UnixSocketPermission)
assert.False(t, cfg.LogTail.Enabled)
}
+2 -2
View File
@@ -212,7 +212,7 @@ func registerDevices(api huma.API, b Backend) {
return nil, err
}
return &deviceOutput{Body: deviceFromView(node, in.Fields == "all", b.Cfg.BaseDomain)}, nil
return &deviceOutput{Body: deviceFromView(node, in.Fields == "all", b.Cfg.DNSConfig.BaseDomain)}, nil
})
huma.Register(api, requireScope(huma.Operation{
@@ -253,7 +253,7 @@ func registerDevices(api huma.API, b Backend) {
out.Body.Devices = make([]Device, 0, nodes.Len())
for _, node := range nodes.All() {
out.Body.Devices = append(out.Body.Devices, deviceFromView(node, allFields, b.Cfg.BaseDomain))
out.Body.Devices = append(out.Body.Devices, deviceFromView(node, allFields, b.Cfg.DNSConfig.BaseDomain))
}
// The NodeStore holds no order, and Tailscale lists devices by name.
+2 -2
View File
@@ -202,8 +202,8 @@ func createFilePolicyApp(t *testing.T) *Headscale {
tmpDir := t.TempDir()
cfg := types.Config{
ServerURL: "http://localhost:8080",
NoisePrivateKeyPath: tmpDir + "/noise_private.key",
ServerURL: "http://localhost:8080",
Noise: types.NoiseConfig{PrivateKeyPath: tmpDir + "/noise_private.key"},
Database: types.DatabaseConfig{
Type: "sqlite3",
Sqlite: types.SqliteConfig{
+3 -5
View File
@@ -297,11 +297,9 @@ func newAddressedTestApp(t *testing.T) *Headscale {
prefixV6 := netip.MustParsePrefix("fd7a:115c:a1e0::/48")
cfg := types.Config{
ServerURL: "http://localhost:8080",
NoisePrivateKeyPath: tmpDir + "/noise_private.key",
PrefixV4: &prefixV4,
PrefixV6: &prefixV6,
IPAllocation: types.IPAllocationStrategySequential,
ServerURL: "http://localhost:8080",
Noise: types.NoiseConfig{PrivateKeyPath: tmpDir + "/noise_private.key"},
Prefixes: types.PrefixesConfig{V4: &prefixV4, V6: &prefixV6, Allocation: types.IPAllocationStrategySequential},
Database: types.DatabaseConfig{
Type: "sqlite3",
Sqlite: types.SqliteConfig{Path: tmpDir + "/headscale_test.db"},
+1 -1
View File
@@ -187,7 +187,7 @@ func TestAPIv2Device_EmptyRouteLists(t *testing.T) {
// MagicDNS FQDN, in a read and in the list.
func TestAPIv2Device_NameIsFQDN(t *testing.T) {
e := newDeviceTestEnv(t)
e.app.cfg.BaseDomain = "tailnet.example.com"
e.app.cfg.DNSConfig.BaseDomain = "tailnet.example.com"
assert.Equal(t, "contract-dut.tailnet.example.com", getDevice(t, e.api, e.deviceID).Name)
+49 -36
View File
@@ -11,6 +11,7 @@ import (
"crypto/x509/pkix"
"errors"
"fmt"
"io/fs"
"math/big"
"net"
"net/http"
@@ -128,14 +129,25 @@ func NewHeadscale(cfg *types.Config) (*Headscale, error) {
runtime.SetBlockProfileRate(1)
}
noisePrivateKey, err := readOrCreatePrivateKey(cfg.AbsPath(cfg.NoisePrivateKeyPath))
// Unreadable is a config error, not an unavailable provider: it stops
// startup, before anything is written, whatever
// only_start_if_oidc_is_available says.
var oidcClientSecret string
if cfg.OIDC.Issuer != "" {
oidcClientSecret, err = types.ResolveSecret("oidc.client_secret", string(cfg.OIDC.ClientSecret))
if err != nil {
return nil, err
}
}
noisePrivateKey, err := readOrCreatePrivateKey(cfg.AbsPath(cfg.Noise.PrivateKeyPath))
if err != nil {
return nil, fmt.Errorf("reading or creating Noise protocol private key: %w", err)
}
provider := cfg.DNSChallenge.Provider
if provider == nil && cfg.DNSChallenge.ProviderName() != "" {
provider, err = cfg.DNSChallenge.NewProvider(types.ResolveSecret)
provider := cfg.DNSConfig.Challenge.Provider
if provider == nil && cfg.DNSConfig.Challenge.ProviderName() != "" {
provider, err = cfg.DNSConfig.Challenge.NewProvider(types.ResolveSecret)
if err != nil {
return nil, fmt.Errorf("building dns.challenge.provider: %w", err)
}
@@ -147,7 +159,7 @@ func NewHeadscale(cfg *types.Config) (*Headscale, error) {
err = iso.CheckIsolation()
if err != nil {
// A Go-only provider may have no name.
name := cmp.Or(cfg.DNSChallenge.ProviderName(), fmt.Sprintf("%T", provider))
name := cmp.Or(cfg.DNSConfig.Challenge.ProviderName(), fmt.Sprintf("%T", provider))
return nil, fmt.Errorf("%w for %s: %w", dns.ErrProviderConfig, name, err)
}
@@ -171,8 +183,8 @@ func NewHeadscale(cfg *types.Config) (*Headscale, error) {
app.challenges = dns.NewChallenges(
provider,
cfg.DNSConfig.CertificateZone(),
cfg.DNSChallenge.ResolverAddrs(),
cfg.DNSChallenge.PropagationTimeout,
cfg.DNSConfig.Challenge.ResolverAddrs(),
cfg.DNSConfig.Challenge.PropagationTimeout,
)
}
@@ -240,6 +252,7 @@ func NewHeadscale(cfg *types.Config) (*Headscale, error) {
&app,
cfg.ServerURL,
&cfg.OIDC,
oidcClientSecret,
)
if err != nil {
if cfg.OIDC.OnlyStartIfOIDCIsAvailable {
@@ -254,8 +267,8 @@ func NewHeadscale(cfg *types.Config) (*Headscale, error) {
app.authProvider = authProvider
if cfg.DERP.ServerEnabled {
derpServerKey, err := readOrCreatePrivateKey(cfg.AbsPath(cfg.DERP.ServerPrivateKeyPath))
if cfg.DERP.Server.Enabled {
derpServerKey, err := readOrCreatePrivateKey(cfg.AbsPath(cfg.DERP.Server.PrivateKeyPath))
if err != nil {
return nil, fmt.Errorf("reading or creating DERP server private key: %w", err)
}
@@ -264,7 +277,7 @@ func NewHeadscale(cfg *types.Config) (*Headscale, error) {
return nil, config.ErrSameKey
}
if cfg.DERP.ServerVerifyClients {
if cfg.DERP.Server.VerifyClients {
t := http.DefaultTransport.(*http.Transport) //nolint:forcetypeassert
t.RegisterProtocol(
derpServer.DerpVerifyScheme,
@@ -395,12 +408,12 @@ func (h *Headscale) scheduledTasks(ctx context.Context) {
log.Info().Msg("fetching DERPMap updates")
derpMap, err := backoff.Retry(ctx, func() (*tailcfg.DERPMap, error) { //nolint:contextcheck
derpMap, err := derp.GetDERPMap(h.cfg.DERP, h.cfg.BaseDomain)
derpMap, err := derp.GetDERPMap(h.cfg.DERP, h.cfg.DNSConfig.BaseDomain)
if err != nil {
return nil, err
}
if h.cfg.DERP.ServerEnabled && h.cfg.DERP.AutomaticallyAddEmbeddedDerpRegion {
if h.cfg.DERP.Server.Enabled && h.cfg.DERP.Server.AutomaticallyAddEmbeddedDerpRegion {
region, err := h.DERPServer.GenerateRegion()
if err != nil {
return nil, fmt.Errorf("generating embedded DERP region: %w", err)
@@ -517,7 +530,7 @@ func (h *Headscale) createRouter(apiV1Mux, apiV2Mux http.Handler) *chi.Mux {
r.Post("/verify", h.VerifyHandler)
if h.cfg.DERP.ServerEnabled {
if h.cfg.DERP.Server.Enabled {
r.HandleFunc("/derp", h.DERPServer.DERPHandler)
r.HandleFunc("/derp/probe", derpServer.DERPProbeHandler)
r.HandleFunc("/derp/latency-check", derpServer.DERPProbeHandler)
@@ -578,9 +591,9 @@ func (h *Headscale) Serve() error {
h.mapBatcher.Start()
defer h.mapBatcher.Close()
if h.cfg.DERP.ServerEnabled {
if h.cfg.DERP.Server.Enabled {
// When embedded DERP is enabled we always need a STUN server
if h.cfg.DERP.STUNAddr == "" {
if h.cfg.DERP.Server.STUNAddr == "" {
return errSTUNAddressNotSet
}
@@ -589,12 +602,12 @@ func (h *Headscale) Serve() error {
// Start on the sources that loaded; only a map with no region at all,
// after the embedded region and the policy's derpMap, is fatal.
derpMap, derpErr := derp.GetDERPMap(h.cfg.DERP, h.cfg.BaseDomain)
derpMap, derpErr := derp.GetDERPMap(h.cfg.DERP, h.cfg.DNSConfig.BaseDomain)
if derpErr != nil {
log.Warn().Err(derpErr).Msg("skipping DERP map sources that failed to load")
}
if h.cfg.DERP.ServerEnabled && h.cfg.DERP.AutomaticallyAddEmbeddedDerpRegion {
if h.cfg.DERP.Server.Enabled && h.cfg.DERP.Server.AutomaticallyAddEmbeddedDerpRegion {
region, err := h.DERPServer.GenerateRegion()
if err != nil {
return fmt.Errorf("generating embedded DERP region: %w", err)
@@ -673,7 +686,7 @@ func (h *Headscale) Serve() error {
}
// Change socket permissions
if err := os.Chmod(h.cfg.UnixSocket, h.cfg.UnixSocketPermission); err != nil { //nolint:noinlineerr
if err := os.Chmod(h.cfg.UnixSocket, fs.FileMode(h.cfg.UnixSocketPermission)); err != nil { //nolint:noinlineerr
return fmt.Errorf("changing socket permission: %w", err)
}
@@ -733,7 +746,7 @@ func (h *Headscale) Serve() error {
router := h.createRouter(humaMux, humaV2Mux)
httpServer := &http.Server{
Addr: h.cfg.Addr,
Addr: string(h.cfg.Addr),
Handler: router,
ReadTimeout: types.HTTPTimeout,
@@ -746,16 +759,16 @@ func (h *Headscale) Serve() error {
if tlsConfig != nil {
httpServer.TLSConfig = tlsConfig
httpListener, err = tls.Listen("tcp", h.cfg.Addr, tlsConfig)
httpListener, err = tls.Listen("tcp", string(h.cfg.Addr), tlsConfig)
} else {
httpListener, err = new(net.ListenConfig).Listen(context.Background(), "tcp", h.cfg.Addr)
httpListener, err = new(net.ListenConfig).Listen(context.Background(), "tcp", string(h.cfg.Addr))
}
if err != nil {
return &types.ListenerBindError{
Listener: "main HTTP",
YAMLKey: "listen_addr",
Addr: h.cfg.Addr,
Addr: string(h.cfg.Addr),
Err: err,
}
}
@@ -826,12 +839,12 @@ func (h *Headscale) Serve() error {
var debugHTTPListener net.Listener
if h.cfg.MetricsAddr != "" {
debugHTTPListener, err = (&net.ListenConfig{}).Listen(ctx, "tcp", h.cfg.MetricsAddr)
debugHTTPListener, err = (&net.ListenConfig{}).Listen(ctx, "tcp", string(h.cfg.MetricsAddr))
if err != nil {
return &types.ListenerBindError{
Listener: "metrics",
YAMLKey: "metrics_listen_addr",
Addr: h.cfg.MetricsAddr,
Addr: string(h.cfg.MetricsAddr),
Err: err,
}
}
@@ -852,7 +865,7 @@ func (h *Headscale) Serve() error {
if !h.cfg.Database.IsSQLite() {
//nolint:gocritic // exitAfterDefer: Fatal exits during initialization before servers start
log.Fatal().
Str("type", h.cfg.Database.Type).
Str("type", string(h.cfg.Database.Type)).
Msgf("tailsql only support %q", types.DatabaseSqlite)
}
@@ -1069,7 +1082,7 @@ func (h *Headscale) acmeTLSSettings(ctx context.Context) (*tlsBundle, error) {
logger := zlog.Zap(log.Logger).Named("acme")
issuer := certmagic.ACMEIssuer{
CA: acmeCfg.URL,
CA: string(acmeCfg.URL),
Email: acmeCfg.Email,
Agreed: true,
TrustedRoots: acmeTrustedRoots,
@@ -1080,7 +1093,7 @@ func (h *Headscale) acmeTLSSettings(ctx context.Context) (*tlsBundle, error) {
switch acmeCfg.ChallengeType {
case types.DNS01ChallengeType:
resolvers := h.cfg.DNSChallenge.ResolverAddrs()
resolvers := h.cfg.DNSConfig.Challenge.ResolverAddrs()
issuer.DNS01Solver = dns01Solver{
DNS01Solver: &certmagic.DNS01Solver{
DNSProvider: h.cfg.DNSProvider(),
@@ -1088,15 +1101,15 @@ func (h *Headscale) acmeTLSSettings(ctx context.Context) (*tlsBundle, error) {
Resolvers: resolvers,
Logger: logger,
},
wait: dns.AwaitTXT(resolvers, h.cfg.DNSChallenge.PropagationTimeout),
wait: dns.AwaitTXT(resolvers, h.cfg.DNSConfig.Challenge.PropagationTimeout),
}
case types.TLSALPN01ChallengeType:
// Validated on the main listener, which must be reachable on port 443.
issuer.DisableHTTPChallenge = true
issuer.ListenHost, _, _ = net.SplitHostPort(h.cfg.Addr)
issuer.ListenHost, _, _ = net.SplitHostPort(string(h.cfg.Addr))
issuer.AltTLSALPNPort, err = types.PortFromAddr(h.cfg.Addr)
issuer.AltTLSALPNPort, err = types.PortFromAddr(string(h.cfg.Addr))
if err != nil {
return nil, fmt.Errorf("parsing listen_addr: %w", err)
}
@@ -1104,14 +1117,14 @@ func (h *Headscale) acmeTLSSettings(ctx context.Context) (*tlsBundle, error) {
case types.HTTP01ChallengeType:
// TLS-ALPN-01 stays on, as with autocert: setups that only open
// port 443 have always been validated that way.
issuer.AltTLSALPNPort, err = types.PortFromAddr(h.cfg.Addr)
issuer.AltTLSALPNPort, err = types.PortFromAddr(string(h.cfg.Addr))
if err != nil {
return nil, fmt.Errorf("parsing listen_addr: %w", err)
}
issuer.ListenHost, _, _ = net.SplitHostPort(acmeCfg.HTTPListen)
issuer.ListenHost, _, _ = net.SplitHostPort(string(acmeCfg.HTTPListen))
issuer.AltHTTPPort, err = types.PortFromAddr(acmeCfg.HTTPListen)
issuer.AltHTTPPort, err = types.PortFromAddr(string(acmeCfg.HTTPListen))
if err != nil {
return nil, fmt.Errorf("parsing tls.acme.http_listen: %w", err)
}
@@ -1148,19 +1161,19 @@ func (h *Headscale) acmeTLSSettings(ctx context.Context) (*tlsBundle, error) {
if acmeCfg.ChallengeType == types.HTTP01ChallengeType {
bundle.ACMEServer = &http.Server{
Addr: acmeCfg.HTTPListen,
Addr: string(acmeCfg.HTTPListen),
Handler: acmeIssuer.HTTPChallengeHandler(http.HandlerFunc(h.redirect)),
ReadTimeout: types.HTTPTimeout,
}
bundle.ACMEListener, err = (&net.ListenConfig{}).Listen(ctx, "tcp", acmeCfg.HTTPListen)
bundle.ACMEListener, err = (&net.ListenConfig{}).Listen(ctx, "tcp", string(acmeCfg.HTTPListen))
if err != nil {
cache.Stop()
return nil, &types.ListenerBindError{
Listener: "ACME HTTP-01 challenge",
YAMLKey: "tls.acme.http_listen",
Addr: acmeCfg.HTTPListen,
Addr: string(acmeCfg.HTTPListen),
Err: err,
}
}
+81 -4
View File
@@ -35,7 +35,7 @@ func TestDebugHostGuard(t *testing.T) {
app := createTestApp(t)
app.cfg.ServerURL = "https://Control.Example.COM:443/headscale"
app.cfg.MetricsAddr = "Debug.Internal:9090"
app.cfg.BaseDomain = "tailnet.invalid"
app.cfg.DNSConfig.BaseDomain = "tailnet.invalid"
handler := app.debugHTTPServer().Handler
for _, tt := range []struct {
@@ -146,8 +146,8 @@ func TestNewHeadscaleDNSProvider(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
cfg := types.Config{
ServerURL: "http://localhost:8080",
NoisePrivateKeyPath: tmpDir + "/noise_private.key",
ServerURL: "http://localhost:8080",
Noise: types.NoiseConfig{PrivateKeyPath: tmpDir + "/noise_private.key"},
Database: types.DatabaseConfig{
Type: "sqlite3",
Sqlite: types.SqliteConfig{Path: tmpDir + "/headscale_test.db"},
@@ -157,8 +157,8 @@ func TestNewHeadscaleDNSProvider(t *testing.T) {
MagicDNS: true,
BaseDomain: "ts.example.com",
Certificates: types.CertificatesConfig{Enabled: tt.certificates},
Challenge: tt.challenge,
},
DNSChallenge: tt.challenge,
}
app, err := NewHeadscale(&cfg)
@@ -177,3 +177,80 @@ func TestNewHeadscaleDNSProvider(t *testing.T) {
})
}
}
// TestNewHeadscaleSecrets pins that startup reads a file: secret only where
// it is used, and that one it cannot read stops startup, even when OIDC may
// be unavailable.
func TestNewHeadscaleSecrets(t *testing.T) {
missing := types.Secret("file:" + filepath.Join(t.TempDir(), "missing"))
tests := []struct {
name string
set func(*types.Config)
wantErr string
noFiles bool // fails before startup writes anything
}{
{
name: "oidc client secret file missing",
set: func(c *types.Config) {
// Unreachable, so discovery would fail and fall back, were the
// secret read only there.
c.OIDC = types.OIDCConfig{Issuer: "http://127.0.0.1:1", ClientID: "headscale", ClientSecret: missing}
},
wantErr: "oidc.client_secret: ",
noFiles: true,
},
{
name: "postgres pass file missing",
set: func(c *types.Config) {
c.Database = types.DatabaseConfig{
Type: types.DatabasePostgres,
Postgres: types.PostgresConfig{Host: "127.0.0.1", Port: 1, Name: "headscale", Pass: missing},
}
},
wantErr: "database.postgres.pass: ",
},
{
name: "inactive consumers read nothing",
set: func(c *types.Config) {
c.Database.Postgres.Pass = missing
c.OIDC.ClientSecret = missing
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
cfg := types.Config{
ServerURL: "http://localhost:8080",
Noise: types.NoiseConfig{PrivateKeyPath: tmpDir + "/noise_private.key"},
Database: types.DatabaseConfig{
Type: "sqlite3",
Sqlite: types.SqliteConfig{Path: tmpDir + "/headscale_test.db"},
},
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
}
tt.set(&cfg)
app, err := NewHeadscale(&cfg)
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
require.ErrorContains(t, err, "readable by the user running headscale")
// Read before connecting: a connection error would not name the key.
assert.NotContains(t, err.Error(), "connect")
if tt.noFiles {
entries, err := os.ReadDir(tmpDir)
require.NoError(t, err)
assert.Empty(t, entries, "no noise key or database before the secret is read")
}
return
}
require.NoError(t, err)
t.Cleanup(func() { _ = app.state.Close() })
})
}
}
+13 -13
View File
@@ -200,29 +200,29 @@ func TestDNS01SolverWait(t *testing.T) {
// TestACMETLSSettings obtains headscale's own certificate from Pebble with
// each challenge type, serving it the way [Headscale.Serve] does.
func TestACMETLSSettings(t *testing.T) {
for _, challenge := range []string{
for _, challenge := range []types.ACMEChallenge{
types.DNS01ChallengeType,
types.HTTP01ChallengeType,
types.TLSALPN01ChallengeType,
} {
t.Run(challenge, func(t *testing.T) {
t.Run(string(challenge), func(t *testing.T) {
httpPort, tlsPort := freePort(t), freePort(t)
acme := startTestACME(t, httpPort, tlsPort)
h := &Headscale{cfg: &types.Config{
ServerURL: "https://" + acmeTestHost,
Addr: net.JoinHostPort("127.0.0.1", strconv.Itoa(tlsPort)),
Addr: types.ListenAddr(net.JoinHostPort("127.0.0.1", strconv.Itoa(tlsPort))),
TLS: types.TLSConfig{ACME: types.ACMEConfig{
Hostname: acmeTestHost,
ChallengeType: challenge,
HTTPListen: net.JoinHostPort("127.0.0.1", strconv.Itoa(httpPort)),
URL: acme.directory,
HTTPListen: types.ListenAddr(net.JoinHostPort("127.0.0.1", strconv.Itoa(httpPort))),
URL: types.HTTPURL(acme.directory),
CacheDir: t.TempDir(),
}},
DNSChallenge: types.DNSChallengeConfig{
DNSConfig: types.DNSConfig{Challenge: types.DNSChallengeConfig{
Provider: acme.provider,
Resolvers: []string{acme.resolver},
},
}},
}}
bundle, err := h.getTLSSettings(t.Context())
@@ -230,7 +230,7 @@ func TestACMETLSSettings(t *testing.T) {
t.Cleanup(bundle.Stop)
assert.Nil(t, bundle.Reload, "certmagic renews the certificate itself")
ln, err := tls.Listen("tcp", h.cfg.Addr, bundle.Config)
ln, err := tls.Listen("tcp", string(h.cfg.Addr), bundle.Config)
require.NoError(t, err)
t.Cleanup(func() { _ = ln.Close() })
@@ -262,7 +262,7 @@ func TestACMETLSSettings(t *testing.T) {
d.Config = &tls.Config{ServerName: acmeTestHost, RootCAs: acme.roots, MinVersion: tls.VersionTLS12}
conn, err := d.DialContext(t.Context(), "tcp", h.cfg.Addr)
conn, err := d.DialContext(t.Context(), "tcp", string(h.cfg.Addr))
if assert.NoError(c, err) {
_ = conn.Close()
}
@@ -310,17 +310,17 @@ func TestACMERetryStaysOnConfiguredCA(t *testing.T) {
h := &Headscale{cfg: &types.Config{
ServerURL: "https://" + acmeTestHost,
Addr: net.JoinHostPort("127.0.0.1", strconv.Itoa(freePort(t))),
Addr: types.ListenAddr(net.JoinHostPort("127.0.0.1", strconv.Itoa(freePort(t)))),
TLS: types.TLSConfig{ACME: types.ACMEConfig{
Hostname: acmeTestHost,
ChallengeType: types.DNS01ChallengeType,
URL: acme.directory,
URL: types.HTTPURL(acme.directory),
CacheDir: t.TempDir(),
}},
DNSChallenge: types.DNSChallengeConfig{
DNSConfig: types.DNSConfig{Challenge: types.DNSChallengeConfig{
Provider: published,
Resolvers: []string{acme.resolver},
},
}},
}}
bundle, err := h.getTLSSettings(t.Context())
+2 -2
View File
@@ -20,8 +20,8 @@ func createTestAppWithNodeExpiry(t *testing.T, nodeExpiry time.Duration) *Headsc
tmpDir := t.TempDir()
cfg := types.Config{
ServerURL: "http://localhost:8080",
NoisePrivateKeyPath: tmpDir + "/noise_private.key",
ServerURL: "http://localhost:8080",
Noise: types.NoiseConfig{PrivateKeyPath: tmpDir + "/noise_private.key"},
Node: types.NodeConfig{
Expiry: nodeExpiry,
},
+2 -2
View File
@@ -3365,8 +3365,8 @@ func createTestApp(t *testing.T, options ...func(*types.Config)) *Headscale {
tmpDir := t.TempDir()
cfg := types.Config{
ServerURL: "http://localhost:8080",
NoisePrivateKeyPath: tmpDir + "/noise_private.key",
ServerURL: "http://localhost:8080",
Noise: types.NoiseConfig{PrivateKeyPath: tmpDir + "/noise_private.key"},
Database: types.DatabaseConfig{
Type: "sqlite3",
Sqlite: types.SqliteConfig{
+41 -7
View File
@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"os"
"slices"
"strings"
"github.com/juanfont/headscale/hscontrol/derp"
@@ -26,7 +27,7 @@ var (
// File is one file the configuration names.
type File struct {
Key string // config key, e.g. "policy.path"
Path string // as startup resolves it
Path string // as startup resolves it; a secret's as written
// CreatedAtStartup marks key files: startup creates them when missing,
// so a missing one is not an error.
@@ -62,7 +63,7 @@ func Files(cfg *types.Config) []File {
})
}
noise := cfg.AbsPath(cfg.NoisePrivateKeyPath)
noise := cfg.AbsPath(cfg.Noise.PrivateKeyPath)
if noise != "" {
files = append(files, File{
Key: "noise.private_key_path", Path: noise, CreatedAtStartup: true,
@@ -70,20 +71,53 @@ func Files(cfg *types.Config) []File {
})
}
if d := cfg.AbsPath(cfg.DERP.ServerPrivateKeyPath); cfg.DERP.ServerEnabled && d != "" {
if d := cfg.AbsPath(cfg.DERP.Server.PrivateKeyPath); cfg.DERP.Server.Enabled && d != "" {
files = append(files, File{
Key: "derp.server.private_key_path", Path: d, CreatedAtStartup: true,
Check: func() error { return checkDERPKey(d, noise) },
})
}
for _, kv := range cfg.UnreadSecrets {
files = append(files, File{
Key: kv.Key, Path: fmt.Sprint(kv.Value),
Check: func() error { return ErrSecretNotRead },
return append(files, secretFiles(cfg)...)
}
// secretFiles lists, by key, each file: secret startup reads, unread.
func secretFiles(cfg *types.Config) []File {
var files []File
add := func(key string, s types.Secret) {
if s.IsFile() {
files = append(files, File{
Key: key, Path: strings.TrimPrefix(string(s), "file:"),
Check: func() error { return ErrSecretNotRead },
})
}
}
if cfg.Database.Type == types.DatabasePostgres {
add("database.postgres.pass", cfg.Database.Postgres.Pass)
}
if cfg.OIDC.Issuer != "" {
add("oidc.client_secret", cfg.OIDC.ClientSecret)
}
// Startup builds the provider from its settings only without a Go-only
// one. Bad settings are the loader's to report; this only collects keys.
if cfg.DNSConfig.Challenge.Provider == nil && cfg.DNSConfig.Challenge.ProviderName() != "" {
_, _ = cfg.DNSConfig.Challenge.NewProvider(func(key, value string) (string, error) {
add(key, types.Secret(value))
if types.Secret(value).IsFile() {
return value, dns.ErrSecretUnread
}
return value, nil
})
}
slices.SortFunc(files, func(a, b File) int { return strings.Compare(a.Key, b.Key) })
return files
}
+69 -14
View File
@@ -8,6 +8,7 @@ import (
"github.com/google/go-cmp/cmp"
policyv2 "github.com/juanfont/headscale/hscontrol/policy/v2"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/libdns/cloudflare"
"github.com/stretchr/testify/require"
"tailscale.com/types/key"
)
@@ -33,12 +34,11 @@ const badGroup = `{"acls":[{"action":"accept","src":["group:missing"],"dst":["*:
func TestFiles(t *testing.T) {
dir := t.TempDir()
cfg := &types.Config{
NoisePrivateKeyPath: filepath.Join(dir, "noise.key"),
Policy: types.PolicyConfig{Mode: types.PolicyModeFile, Path: filepath.Join(dir, "policy.hujson")},
Noise: types.NoiseConfig{PrivateKeyPath: filepath.Join(dir, "noise.key")},
Policy: types.PolicyConfig{Mode: types.PolicyModeFile, Path: filepath.Join(dir, "policy.hujson")},
DERP: types.DERPConfig{
Paths: []string{"a.yaml", "b.yaml"},
ServerEnabled: true,
ServerPrivateKeyPath: filepath.Join(dir, "derp.key"),
Paths: []string{"a.yaml", "b.yaml"},
Server: types.DERPServerConfig{Enabled: true, PrivateKeyPath: filepath.Join(dir, "derp.key")},
},
DNSConfig: types.DNSConfig{ExtraRecordsPath: "records.json"},
TLS: types.TLSConfig{CertPath: "c.pem", KeyPath: "k.pem"},
@@ -71,11 +71,68 @@ func TestFiles(t *testing.T) {
// A policy in the database and a disabled DERP server name no file.
cfg.Policy.Mode = types.PolicyModeDB
cfg.DERP.ServerEnabled = false
cfg.DERP.Server.Enabled = false
for _, f := range Files(cfg) {
require.NotContains(t, []string{"policy.path", "derp.server.private_key_path"}, f.Key)
}
// A file: secret is listed, unread, only where startup reads it.
missing := types.Secret("file:" + filepath.Join(dir, "missing"))
postgres := types.DatabaseConfig{Type: types.DatabasePostgres, Postgres: types.PostgresConfig{Pass: missing}}
oidc := types.OIDCConfig{Issuer: "https://idp.example.com", ClientSecret: missing}
provider := types.DNSChallengeConfig{ProviderSettings: map[string]any{
"name": "cloudflare", "zone_token": string(missing), "api_token": string(missing),
}}
for _, tt := range []struct {
name string
cfg types.Config
want []string
}{
{name: "postgres pass", cfg: types.Config{Database: postgres}, want: []string{"database.postgres.pass"}},
{name: "sqlite reads no postgres pass", cfg: types.Config{Database: types.DatabaseConfig{Type: types.DatabaseSqlite, Postgres: postgres.Postgres}}},
{name: "oidc with an issuer", cfg: types.Config{OIDC: oidc}, want: []string{"oidc.client_secret"}},
{name: "oidc without an issuer", cfg: types.Config{OIDC: types.OIDCConfig{ClientSecret: missing}}},
{name: "literal secrets", cfg: types.Config{
Database: types.DatabaseConfig{Type: types.DatabasePostgres, Postgres: types.PostgresConfig{Pass: "literal"}},
OIDC: types.OIDCConfig{Issuer: oidc.Issuer, ClientSecret: "literal"},
}},
{
name: "provider settings",
cfg: types.Config{DNSConfig: types.DNSConfig{Challenge: provider}},
want: []string{"dns.challenge.provider.api_token", "dns.challenge.provider.zone_token"},
},
{
name: "go-only provider reads no settings",
cfg: types.Config{
DNSConfig: types.DNSConfig{Challenge: types.DNSChallengeConfig{
ProviderSettings: provider.ProviderSettings, Provider: &cloudflare.Provider{},
}},
},
},
{
name: "sorted by key",
cfg: types.Config{Database: postgres, OIDC: oidc, DNSConfig: types.DNSConfig{Challenge: provider}},
want: []string{
"database.postgres.pass", "dns.challenge.provider.api_token",
"dns.challenge.provider.zone_token", "oidc.client_secret",
},
},
} {
t.Run(tt.name, func(t *testing.T) {
var keys []string
for _, f := range Files(&tt.cfg) {
require.ErrorIs(t, f.Check(), ErrSecretNotRead, f.Key)
require.Equal(t, filepath.Join(dir, "missing"), f.Path, f.Key)
keys = append(keys, f.Key)
}
require.Equal(t, tt.want, keys)
})
}
}
func TestFilesChecks(t *testing.T) {
@@ -183,7 +240,7 @@ func TestFilesChecks(t *testing.T) {
{
name: "noise key missing",
build: func(_ *testing.T, dir string) *types.Config {
return &types.Config{NoisePrivateKeyPath: filepath.Join(dir, "sub", "noise.key")}
return &types.Config{Noise: types.NoiseConfig{PrivateKeyPath: filepath.Join(dir, "sub", "noise.key")}}
},
key: "noise.private_key_path",
wantErr: os.ErrNotExist,
@@ -193,7 +250,7 @@ func TestFilesChecks(t *testing.T) {
build: func(t *testing.T, dir string) *types.Config {
t.Helper()
return &types.Config{NoisePrivateKeyPath: write(t, filepath.Join(dir, "noise.key"), "garbage")}
return &types.Config{Noise: types.NoiseConfig{PrivateKeyPath: write(t, filepath.Join(dir, "noise.key"), "garbage")}}
},
key: "noise.private_key_path",
wantMsg: "parsing private key",
@@ -206,10 +263,9 @@ func TestFilesChecks(t *testing.T) {
k := keyText(t)
return &types.Config{
NoisePrivateKeyPath: write(t, filepath.Join(dir, "noise.key"), k),
Noise: types.NoiseConfig{PrivateKeyPath: write(t, filepath.Join(dir, "noise.key"), k)},
DERP: types.DERPConfig{
ServerEnabled: true,
ServerPrivateKeyPath: write(t, filepath.Join(dir, "derp.key"), k+"\n"),
Server: types.DERPServerConfig{Enabled: true, PrivateKeyPath: write(t, filepath.Join(dir, "derp.key"), k+"\n")},
},
}
},
@@ -222,10 +278,9 @@ func TestFilesChecks(t *testing.T) {
t.Helper()
return &types.Config{
NoisePrivateKeyPath: write(t, filepath.Join(dir, "noise.key"), keyText(t)),
Noise: types.NoiseConfig{PrivateKeyPath: write(t, filepath.Join(dir, "noise.key"), keyText(t))},
DERP: types.DERPConfig{
ServerEnabled: true,
ServerPrivateKeyPath: write(t, filepath.Join(dir, "derp.key"), keyText(t)),
Server: types.DERPServerConfig{Enabled: true, PrivateKeyPath: write(t, filepath.Join(dir, "derp.key"), keyText(t))},
},
}
},
+14 -7
View File
@@ -502,7 +502,7 @@ func openDB(cfg types.DatabaseConfig, sqlitePath string) (*gorm.DB, error) {
// TODO(kradalby): Integrate this with zerolog
var dbLogger logger.Interface
if cfg.Debug {
dbLogger = util.NewDBLogWrapper(&log.Logger, cfg.Gorm.SlowThreshold, cfg.Gorm.SkipErrRecordNotFound, cfg.Gorm.ParameterizedQueries)
dbLogger = util.NewDBLogWrapper(&log.Logger, time.Duration(cfg.Gorm.SlowThreshold)*time.Millisecond, cfg.Gorm.SkipErrRecordNotFound, cfg.Gorm.ParameterizedQueries)
} else {
dbLogger = logger.Default.LogMode(logger.Silent)
}
@@ -517,7 +517,7 @@ func openDB(cfg types.DatabaseConfig, sqlitePath string) (*gorm.DB, error) {
}
log.Info().
Str("database", types.DatabaseSqlite).
Str("database", string(types.DatabaseSqlite)).
Str("path", sqlitePath).
Msg("Opening database")
@@ -564,15 +564,22 @@ func openDB(cfg types.DatabaseConfig, sqlitePath string) (*gorm.DB, error) {
case cfg.Type == types.DatabasePostgres:
// Log the DSN without the password.
redacted := cfg.Postgres
redacted.Pass = ""
pg := cfg.Postgres
pg.Pass = ""
log.Info().
Str(zf.Database, types.DatabasePostgres).
Str(zf.Path, redacted.DSN()).
Str(zf.Database, string(types.DatabasePostgres)).
Str(zf.Path, pg.DSN()).
Msg("Opening database")
db, err := gorm.Open(postgres.Open(cfg.Postgres.DSN()), &gorm.Config{
pass, err := types.ResolveSecret("database.postgres.pass", string(cfg.Postgres.Pass))
if err != nil {
return nil, err
}
pg.Pass = types.Secret(pass)
db, err := gorm.Open(postgres.Open(pg.DSN()), &gorm.Config{
Logger: dbLogger,
})
if err != nil {
+1 -1
View File
@@ -83,7 +83,7 @@ func newHeadscaleDBFromPostgresURL(t *testing.T, pu *url.URL) *HSDatabase {
Host: pu.Hostname(),
User: pu.User.Username(),
Name: strings.TrimLeft(pu.Path, "/"),
Pass: pass,
Pass: types.Secret(pass),
Port: port,
Ssl: "disable",
},
+2 -2
View File
@@ -88,7 +88,7 @@ func (h *Headscale) debugHostGuard(next http.Handler) http.Handler {
return strings.TrimSuffix(strings.ToLower(strings.Trim(host, "[]")), ".")
}
allowed := []string{"localhost", normalize(h.cfg.MetricsAddr)}
allowed := []string{"localhost", normalize(string(h.cfg.MetricsAddr))}
serverURL, err := url.Parse(h.cfg.ServerURL)
if err == nil {
@@ -274,7 +274,7 @@ func (h *Headscale) debugHTTPServer() *http.Server {
debugMux.Handle("/metrics", promhttp.Handler())
debugHTTPServer := &http.Server{
Addr: h.cfg.MetricsAddr,
Addr: string(h.cfg.MetricsAddr),
Handler: securityHeaders(h.debugHostGuard(debugMux)),
ReadTimeout: types.HTTPTimeout,
WriteTimeout: 0,
+12 -2
View File
@@ -27,6 +27,7 @@ import (
var (
errEmptyDERPMapFile = errors.New("DERP map file has no regions or homeparams")
errDERPMapStatus = errors.New("unexpected HTTP status")
errDERPURLSyntax = errors.New("invalid URL syntax")
ErrRegionIDKey = errors.New("doesn't match its key")
ErrRegionNoNodes = errors.New("must specify at least one node in DERP Region")
@@ -249,8 +250,17 @@ func GetDERPMap(cfg types.DERPConfig, shuffleSeed string) (*tailcfg.DERPMap, err
derpMaps = append(derpMaps, cfg.DERPMap)
}
for _, addr := range cfg.URLs {
derpMap, err := loadDERPMapFromURL(addr)
for i, u := range cfg.URLs {
addr, err := url.Parse(string(u))
if err != nil {
// The parser's error quotes the URL or parts of it, credentials
// included.
errs = append(errs, fmt.Errorf("derp.urls[%d]: %w", i, errDERPURLSyntax))
continue
}
derpMap, err := loadDERPMapFromURL(*addr)
if err != nil {
errs = append(errs, err)
continue
+20 -1
View File
@@ -522,7 +522,7 @@ regions:
require.NoError(t, os.WriteFile(brokenPath, []byte("regions: [\n"), 0o600))
dm, err := GetDERPMap(types.DERPConfig{
URLs: []url.URL{*goodURL, *downURL},
URLs: []types.HTTPURL{types.HTTPURL(goodURL.String()), types.HTTPURL(downURL.String())},
Paths: []string{goodPath, brokenPath, filepath.Join(t.TempDir(), "missing.yaml")},
}, "")
require.ErrorIs(t, err, errDERPMapStatus)
@@ -533,6 +533,25 @@ regions:
require.Contains(t, dm.Regions, tailcfg.DERPRegionID(2))
}
// TestGetDERPMapURLParseError: url.Parse errors quote the URL or parts of
// it, credentials included, so the error names only the setting.
func TestGetDERPMapURLParseError(t *testing.T) {
const marker = "derp-parse-password-marker" //nolint:gosec // test marker, not a real credential
for _, u := range []string{
"https://alice:" + marker + "@derp.example/%zz",
"https://alice:" + marker + "?@derp.example/map",
"https://alice:" + marker + "#@derp.example/map",
"https://alice:" + marker,
} {
t.Run(u, func(t *testing.T) {
_, err := GetDERPMap(types.DERPConfig{URLs: []types.HTTPURL{types.HTTPURL(u)}}, "")
require.EqualError(t, err, "derp.urls[0]: invalid URL syntax")
require.NotContains(t, err.Error(), marker)
})
}
}
// TestLoadDERPMapFromURLRejectsHTTPError: an error page that happens to be
// JSON must not pass as an empty map, or a refresh drops that URL's regions.
func TestLoadDERPMapFromURLRejectsHTTPError(t *testing.T) {
+10 -10
View File
@@ -68,7 +68,7 @@ func NewDERPServer(
log.Trace().Caller().Msg("creating new embedded DERP server")
server := derpserver.New(derpKey, util.TSLogfWrapper()) // nolint // zerolinter complains
if cfg.ServerVerifyClients {
if cfg.Server.VerifyClients {
server.SetVerifyClientURL(DerpVerifyScheme + "://verify")
server.SetVerifyClientURLFailOpen(false)
}
@@ -136,24 +136,24 @@ func (d *DERPServer) GenerateRegion() (tailcfg.DERPRegion, error) {
}
localDERPregion := tailcfg.DERPRegion{
RegionID: d.cfg.ServerRegionID,
RegionCode: d.cfg.ServerRegionCode,
RegionName: d.cfg.ServerRegionName,
RegionID: d.cfg.Server.RegionID,
RegionCode: d.cfg.Server.RegionCode,
RegionName: d.cfg.Server.RegionName,
Nodes: []*tailcfg.DERPNode{
{
Name: d.cfg.ServerRegionID.String(),
RegionID: d.cfg.ServerRegionID,
Name: d.cfg.Server.RegionID.String(),
RegionID: d.cfg.Server.RegionID,
HostName: host,
DERPPort: port,
IPv4: d.cfg.IPv4,
IPv6: d.cfg.IPv6,
IPv4: d.cfg.Server.IPv4,
IPv6: d.cfg.Server.IPv6,
InsecureForTests: insecure,
},
},
}
_, portSTUNStr, err := net.SplitHostPort(d.cfg.STUNAddr)
_, portSTUNStr, err := net.SplitHostPort(string(d.cfg.Server.STUNAddr))
if err != nil {
return tailcfg.DERPRegion{}, err
}
@@ -380,7 +380,7 @@ func DERPBootstrapDNSHandler(
// ServeSTUN starts a STUN server on the configured addr.
func (d *DERPServer) ServeSTUN() {
packetConn, err := new(net.ListenConfig).ListenPacket(context.Background(), "udp", d.cfg.STUNAddr)
packetConn, err := new(net.ListenConfig).ListenPacket(context.Background(), "udp", string(d.cfg.Server.STUNAddr))
if err != nil {
log.Fatal().Msgf("failed to open STUN listener: %v", err)
}
+1 -3
View File
@@ -40,9 +40,7 @@ func TestGenerateRegionInsecureTLS(t *testing.T) {
d := &DERPServer{
serverURL: "http://headscale",
cfg: &types.DERPConfig{
ServerRegionID: 999,
ServerRegionCode: "headscale",
STUNAddr: "[::]:3478",
Server: types.DERPServerConfig{RegionID: 999, RegionCode: "headscale", STUNAddr: "[::]:3478"},
},
}
+3 -5
View File
@@ -73,11 +73,9 @@ func TestVerifyHandler_SuccessSetsJSONContentType(t *testing.T) {
prefixV6 := netip.MustParsePrefix("fd7a:115c:a1e0::/48")
cfg := &types.Config{
ServerURL: "http://localhost:0",
NoisePrivateKeyPath: tmpDir + "/noise_private.key",
PrefixV4: &prefixV4,
PrefixV6: &prefixV6,
IPAllocation: types.IPAllocationStrategySequential,
ServerURL: "http://localhost:0",
Noise: types.NoiseConfig{PrivateKeyPath: tmpDir + "/noise_private.key"},
Prefixes: types.PrefixesConfig{V4: &prefixV4, V6: &prefixV6, Allocation: types.IPAllocationStrategySequential},
Database: types.DatabaseConfig{
Type: "sqlite3",
Sqlite: types.SqliteConfig{
+4 -6
View File
@@ -182,15 +182,13 @@ func setupBatcherWithTestData(
Path: dbPath,
},
},
PrefixV4: &prefixV4,
PrefixV6: &prefixV6,
IPAllocation: types.IPAllocationStrategySequential,
BaseDomain: "headscale.test",
Prefixes: types.PrefixesConfig{V4: &prefixV4, V6: &prefixV6, Allocation: types.IPAllocationStrategySequential},
DNSConfig: types.DNSConfig{BaseDomain: "headscale.test"},
Policy: types.PolicyConfig{
Mode: types.PolicyModeDB,
},
DERP: types.DERPConfig{
ServerEnabled: false,
Server: types.DERPServerConfig{Enabled: false},
DERPMap: &tailcfg.DERPMap{
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{
999: {
@@ -232,7 +230,7 @@ func setupBatcherWithTestData(
tb.Fatalf("Failed to create state: %v", err)
}
derpMap, err := derp.GetDERPMap(cfg.DERP, cfg.BaseDomain)
derpMap, err := derp.GetDERPMap(cfg.DERP, cfg.DNSConfig.BaseDomain)
require.NoError(tb, err)
require.NotNil(tb, derpMap)
+1 -1
View File
@@ -210,7 +210,7 @@ func (b *MapResponseBuilder) WithUserProfiles(peers views.Slice[types.NodeView])
b.resp.UserProfiles = generateUserProfiles(node, peers)
if node.IsTagged() {
fqdn, err := node.GetFQDN(b.mapper.cfg.BaseDomain)
fqdn, err := node.GetFQDN(b.mapper.cfg.DNSConfig.BaseDomain)
if err != nil {
b.addError(err)
return b
+5 -5
View File
@@ -14,7 +14,7 @@ import (
func TestMapResponseBuilder_Basic(t *testing.T) {
cfg := &types.Config{
BaseDomain: "example.com",
DNSConfig: types.DNSConfig{BaseDomain: "example.com"},
LogTail: types.LogTailConfig{
Enabled: true,
},
@@ -60,8 +60,8 @@ func TestMapResponseBuilder_WithCapabilityVersion(t *testing.T) {
func TestMapResponseBuilder_WithDomain(t *testing.T) {
domain := "test.example.com"
cfg := &types.Config{
ServerURL: "https://test.example.com",
BaseDomain: domain,
ServerURL: "https://test.example.com",
DNSConfig: types.DNSConfig{BaseDomain: domain},
}
mockState := &state.State{}
@@ -245,8 +245,8 @@ func TestMapResponseBuilder_ErrorHandling(t *testing.T) {
func TestMapResponseBuilder_ChainedCalls(t *testing.T) {
domain := "chained.example.com"
cfg := &types.Config{
ServerURL: "https://chained.example.com",
BaseDomain: domain,
ServerURL: "https://chained.example.com",
DNSConfig: types.DNSConfig{BaseDomain: domain},
LogTail: types.LogTailConfig{
Enabled: false,
},
+5 -7
View File
@@ -41,13 +41,11 @@ func setupStormBatcher(tb testing.TB, nodeCount, workers int, policy string) (*T
Type: types.DatabaseSqlite,
Sqlite: types.SqliteConfig{Path: tmpDir + "/headscale_test.db"},
},
PrefixV4: &prefixV4,
PrefixV6: &prefixV6,
IPAllocation: types.IPAllocationStrategySequential,
BaseDomain: "headscale.test",
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
Prefixes: types.PrefixesConfig{V4: &prefixV4, V6: &prefixV6, Allocation: types.IPAllocationStrategySequential},
DNSConfig: types.DNSConfig{BaseDomain: "headscale.test"},
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
DERP: types.DERPConfig{
ServerEnabled: false,
Server: types.DERPServerConfig{Enabled: false},
DERPMap: &tailcfg.DERPMap{
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{999: {RegionID: 999}},
},
@@ -79,7 +77,7 @@ func setupStormBatcher(tb testing.TB, nodeCount, workers int, policy string) (*T
st, err := state.NewState(cfg)
require.NoError(tb, err)
derpMap, err := derp.GetDERPMap(cfg.DERP, cfg.BaseDomain)
derpMap, err := derp.GetDERPMap(cfg.DERP, cfg.DNSConfig.BaseDomain)
require.NoError(tb, err)
st.SetDERPMap(derpMap)
+2 -2
View File
@@ -138,8 +138,8 @@ func generateDNSConfig(
// MagicDNS, base_domain can be a public domain whose names clients ask the
// exit node for, so node names stay readable. Reverse lookups stay open, as
// on Tailscale SaaS.
if dnsConfig.Proxied && cfg.BaseDomain != "" {
dnsConfig.ExitNodeFilteredSet = []string{"." + strings.ToLower(cfg.BaseDomain)}
if dnsConfig.Proxied && cfg.DNSConfig.BaseDomain != "" {
dnsConfig.ExitNodeFilteredSet = []string{"." + strings.ToLower(cfg.DNSConfig.BaseDomain)}
}
return dnsConfig
+29 -44
View File
@@ -58,8 +58,7 @@ func TestExitNodeFilteredSet(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got := generateDNSConfig(
&types.Config{
BaseDomain: tt.baseDomain,
DNSConfig: types.DNSConfig{MagicDNS: tt.magicDNS, BaseDomain: tt.baseDomain},
DNSConfig: types.DNSConfig{MagicDNS: tt.magicDNS, BaseDomain: tt.baseDomain},
},
(&types.Node{}).View(),
nil,
@@ -82,7 +81,8 @@ func TestDNSConfigMapResponse(t *testing.T) {
Domains: []string{
"foobar.headscale.net",
},
Proxied: true,
Proxied: true,
ExitNodeFilteredSet: []string{".foobar.headscale.net"},
},
},
{
@@ -370,11 +370,9 @@ func TestBuildFromChangeFiltersPeerPatchesByVisibility(t *testing.T) {
Type: types.DatabaseSqlite,
Sqlite: types.SqliteConfig{Path: tmp + "/h.db"},
},
PrefixV4: &p4,
PrefixV6: &p6,
IPAllocation: types.IPAllocationStrategySequential,
BaseDomain: "headscale.test",
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
Prefixes: types.PrefixesConfig{V4: &p4, V6: &p6, Allocation: types.IPAllocationStrategySequential},
DNSConfig: types.DNSConfig{BaseDomain: "headscale.test"},
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
DERP: types.DERPConfig{
DERPMap: &tailcfg.DERPMap{
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{999: {RegionID: 999}},
@@ -456,11 +454,9 @@ func TestBuildFromChangeFiltersUserProfilesByVisibility(t *testing.T) {
Type: types.DatabaseSqlite,
Sqlite: types.SqliteConfig{Path: tmp + "/h.db"},
},
PrefixV4: &p4,
PrefixV6: &p6,
IPAllocation: types.IPAllocationStrategySequential,
BaseDomain: "headscale.test",
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
Prefixes: types.PrefixesConfig{V4: &p4, V6: &p6, Allocation: types.IPAllocationStrategySequential},
DNSConfig: types.DNSConfig{BaseDomain: "headscale.test"},
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
DERP: types.DERPConfig{
DERPMap: &tailcfg.DERPMap{
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{999: {RegionID: 999}},
@@ -541,11 +537,9 @@ func TestBuildFromChangeVisibilityMatchesFullMap(t *testing.T) {
Type: types.DatabaseSqlite,
Sqlite: types.SqliteConfig{Path: tmp + "/h.db"},
},
PrefixV4: &p4,
PrefixV6: &p6,
IPAllocation: types.IPAllocationStrategySequential,
BaseDomain: "headscale.test",
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
Prefixes: types.PrefixesConfig{V4: &p4, V6: &p6, Allocation: types.IPAllocationStrategySequential},
DNSConfig: types.DNSConfig{BaseDomain: "headscale.test"},
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
DERP: types.DERPConfig{
DERPMap: &tailcfg.DERPMap{
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{999: {RegionID: 999}},
@@ -753,11 +747,9 @@ func TestFullMapResponseSurvivesPeerWithInvalidData(t *testing.T) {
Type: types.DatabaseSqlite,
Sqlite: types.SqliteConfig{Path: tmp + "/h.db"},
},
PrefixV4: &p4,
PrefixV6: &p6,
IPAllocation: types.IPAllocationStrategySequential,
BaseDomain: "headscale.test",
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
Prefixes: types.PrefixesConfig{V4: &p4, V6: &p6, Allocation: types.IPAllocationStrategySequential},
DNSConfig: types.DNSConfig{BaseDomain: "headscale.test"},
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
DERP: types.DERPConfig{
DERPMap: &tailcfg.DERPMap{
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{999: {RegionID: 999}},
@@ -1016,11 +1008,9 @@ func TestBackfillNodeIPsReachesBackfilledNode(t *testing.T) {
Type: types.DatabaseSqlite,
Sqlite: types.SqliteConfig{Path: tmp + "/h.db"},
},
PrefixV4: &p4,
PrefixV6: &p6,
IPAllocation: types.IPAllocationStrategySequential,
BaseDomain: "headscale.test",
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
Prefixes: types.PrefixesConfig{V4: &p4, V6: &p6, Allocation: types.IPAllocationStrategySequential},
DNSConfig: types.DNSConfig{BaseDomain: "headscale.test"},
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
DERP: types.DERPConfig{
DERPMap: &tailcfg.DERPMap{
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{999: {RegionID: 999}},
@@ -1091,11 +1081,9 @@ func TestFailedExpiryOfPrimaryAnnouncesBackup(t *testing.T) {
Type: types.DatabaseSqlite,
Sqlite: types.SqliteConfig{Path: tmp + "/h.db"},
},
PrefixV4: &p4,
PrefixV6: &p6,
IPAllocation: types.IPAllocationStrategySequential,
BaseDomain: "headscale.test",
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
Prefixes: types.PrefixesConfig{V4: &p4, V6: &p6, Allocation: types.IPAllocationStrategySequential},
DNSConfig: types.DNSConfig{BaseDomain: "headscale.test"},
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
DERP: types.DERPConfig{
DERPMap: &tailcfg.DERPMap{
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{999: {RegionID: 999}},
@@ -1183,11 +1171,8 @@ func TestGenerateMapResponseDebugOnlyOnFullMaps(t *testing.T) {
Type: types.DatabaseSqlite,
Sqlite: types.SqliteConfig{Path: tmp + "/h.db"},
},
PrefixV4: &p4,
PrefixV6: &p6,
IPAllocation: types.IPAllocationStrategySequential,
BaseDomain: "headscale.test",
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
Prefixes: types.PrefixesConfig{V4: &p4, V6: &p6, Allocation: types.IPAllocationStrategySequential},
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
DERP: types.DERPConfig{
DERPMap: &tailcfg.DERPMap{
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{999: {RegionID: 999}},
@@ -1195,7 +1180,7 @@ func TestGenerateMapResponseDebugOnlyOnFullMaps(t *testing.T) {
},
// Without these the DERP map and DNS config renders carry
// nothing and no frame is generated at all.
DNSConfig: types.DNSConfig{MagicDNS: true},
DNSConfig: types.DNSConfig{MagicDNS: true, BaseDomain: "headscale.test"},
Tuning: types.Tuning{
NodeStoreBatchSize: state.TestBatchSize,
NodeStoreBatchTimeout: state.TestBatchTimeout,
@@ -1358,12 +1343,13 @@ func TestCertDomains(t *testing.T) {
dnsCfg := tt.dns
dnsCfg.MagicDNS = true
dnsCfg.BaseDomain = baseDomain
dnsCfg.ExtraRecords = []tailcfg.DNSRecord{
{Name: "Grafana.ts.example.com", Type: "A", Value: "100.64.0.3"},
{Name: "grafana.svc.ts.example.com.", Type: "A", Value: "100.64.0.4"},
}
cfg := &types.Config{BaseDomain: baseDomain, DNSConfig: dnsCfg}
cfg := &types.Config{DNSConfig: dnsCfg}
cfg.StartDNS(tt.provider)
got := generateDNSConfig(cfg, tt.node.View(), nil)
@@ -1862,10 +1848,9 @@ func capMapsMapperFixture(tb testing.TB, n int, attrs string) (*mapper, []types.
Type: types.DatabaseSqlite,
Sqlite: types.SqliteConfig{Path: tb.TempDir() + "/caps.db"},
},
PrefixV4: &p4, PrefixV6: &p6,
IPAllocation: types.IPAllocationStrategySequential,
BaseDomain: "headscale.test",
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
Prefixes: types.PrefixesConfig{V4: &p4, V6: &p6, Allocation: types.IPAllocationStrategySequential},
DNSConfig: types.DNSConfig{BaseDomain: "headscale.test"},
Policy: types.PolicyConfig{Mode: types.PolicyModeDB},
Tuning: types.Tuning{
NodeStoreBatchSize: state.TestBatchSize,
NodeStoreBatchTimeout: state.TestBatchTimeout,
+2 -2
View File
@@ -199,8 +199,8 @@ func TestTailNode(t *testing.T) {
t.Parallel()
cfg := &types.Config{
BaseDomain: tt.baseDomain,
Taildrop: types.TaildropConfig{Enabled: true},
DNSConfig: types.DNSConfig{BaseDomain: tt.baseDomain},
Taildrop: types.TaildropConfig{Enabled: true},
}
// Stub primary-route lookup: tt.node owns its SubnetRoutes,
+2 -2
View File
@@ -76,7 +76,7 @@ func TestNoiseRouter_DoesNotExposeAdministrativeEndpoints(t *testing.T) {
serverKey := key.NewMachine()
app := &Headscale{
cfg: &types.Config{MetricsAddr: metricsAddr},
cfg: &types.Config{MetricsAddr: types.ListenAddr(metricsAddr)},
noisePrivateKey: &serverKey,
}
server := httptest.NewServer(http.HandlerFunc(app.NoiseUpgradeHandler))
@@ -1665,7 +1665,7 @@ func setDNSTestApp(t *testing.T) (*Headscale, *dnstest.Provider) {
t.Helper()
app := createTestApp(t)
app.cfg.BaseDomain = setDNSZone
app.cfg.DNSConfig.BaseDomain = setDNSZone
app.cfg.DNSConfig.Certificates = types.CertificatesConfig{Enabled: true, Zone: setDNSZone}
provider := &dnstest.Provider{}
+3 -2
View File
@@ -87,17 +87,18 @@ func NewAuthProviderOIDC(
h *Headscale,
serverURL string,
cfg *types.OIDCConfig,
clientSecret string, // cfg.ClientSecret, resolved
) (*AuthProviderOIDC, error) {
// Use the caller's context (bounded, see app.go) so a slow or unreachable
// issuer fails discovery within the timeout instead of hanging startup.
oidcProvider, err := oidc.NewProvider(ctx, cfg.Issuer)
oidcProvider, err := oidc.NewProvider(ctx, string(cfg.Issuer))
if err != nil {
return nil, fmt.Errorf("creating OIDC provider from issuer config: %w", err)
}
oauth2Config := &oauth2.Config{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
ClientSecret: clientSecret,
Endpoint: oidcProvider.Endpoint(),
RedirectURL: oidcCallbackURL(serverURL),
Scopes: cfg.Scope,
+6 -6
View File
@@ -896,8 +896,8 @@ func TestOIDCPendingCallbackCookieBinding(t *testing.T) {
}
func TestOIDCPendingCallbackPKCEFlow(t *testing.T) {
for _, method := range []string{"", types.PKCEMethodPlain, types.PKCEMethodS256} {
t.Run("method="+method, func(t *testing.T) {
for _, method := range []types.PKCEMethod{"", types.PKCEMethodPlain, types.PKCEMethodS256} {
t.Run("method="+string(method), func(t *testing.T) {
b := newOIDCBrowser(t)
a, ok := b.app.authProvider.(*AuthProviderOIDC)
require.True(t, ok)
@@ -1103,11 +1103,11 @@ func newOIDCBrowserWithPrefix(t *testing.T, prefix string) *oidcBrowser {
app,
publicURL,
&types.OIDCConfig{
Issuer: idp.Issuer(),
ClientID: idp.ClientID,
ClientSecret: idp.ClientSecret,
Scope: []string{"openid", "profile", "email"},
Issuer: types.HTTPURL(idp.Issuer()),
ClientID: idp.ClientID,
Scope: []string{"openid", "profile", "email"},
},
idp.ClientSecret,
)
require.NoError(t, err)
t.Cleanup(provider.authCache.Close)
+9 -8
View File
@@ -167,17 +167,19 @@ func NewServer(tb testing.TB, opts ...ServerOption) *TestServer {
cfg := types.Config{
// Placeholder; updated below once the in-memory server starts.
ServerURL: "http://localhost:0",
NoisePrivateKeyPath: tmpDir + "/noise_private.key",
ServerURL: "http://localhost:0",
Noise: types.NoiseConfig{PrivateKeyPath: tmpDir + "/noise_private.key"},
Node: types.NodeConfig{
Expiry: sc.nodeExpiry,
Ephemeral: types.EphemeralConfig{
InactivityTimeout: sc.ephemeralTimeout,
},
},
PrefixV4: &prefixV4,
PrefixV6: &prefixV6,
IPAllocation: types.IPAllocationStrategySequential,
Prefixes: types.PrefixesConfig{
V4: &prefixV4,
V6: &prefixV6,
Allocation: types.IPAllocationStrategySequential,
},
Database: types.DatabaseConfig{
Type: "sqlite3",
Sqlite: types.SqliteConfig{
@@ -198,14 +200,13 @@ func NewServer(tb testing.TB, opts ...ServerOption) *TestServer {
}
if sc.magicDNSDomain != "" {
cfg.BaseDomain = sc.magicDNSDomain
cfg.DNSConfig.MagicDNS = true
cfg.DNSConfig.BaseDomain = sc.magicDNSDomain
}
if sc.certProvider != nil {
cfg.DNSConfig.Certificates = types.CertificatesConfig{Enabled: true, Zone: cfg.BaseDomain}
cfg.DNSChallenge.Provider = sc.certProvider
cfg.DNSConfig.Certificates = types.CertificatesConfig{Enabled: true, Zone: cfg.DNSConfig.BaseDomain}
cfg.DNSConfig.Challenge.Provider = sc.certProvider
}
if len(sc.dnsResolvers) > 0 {
+1 -1
View File
@@ -377,7 +377,7 @@ func TestBackfillNodeIPsKeepsLiveSessions(t *testing.T) {
// Dropping the IPv6 prefix gives the backfill work to do.
cfg := persistTestConfig(dbPath)
cfg.PrefixV6 = nil
cfg.Prefixes.V6 = nil
s, err := NewState(cfg)
require.NoError(t, err)
+1 -1
View File
@@ -30,7 +30,7 @@ var nodeHealthChecks = []nodeHealthCheck{givenNameMapsToValidFQDN, expiryIsEncod
var givenNameMapsToValidFQDN = nodeHealthCheck{
name: "given-name-maps-to-valid-fqdn",
check: func(nv types.NodeView, cfg *types.Config) (string, string, bool) {
err := types.ValidateGivenName(nv.GivenName(), cfg.BaseDomain)
err := types.ValidateGivenName(nv.GivenName(), cfg.DNSConfig.BaseDomain)
if err != nil {
return err.Error(), fmt.Sprintf("headscale nodes rename %d <name>", nv.ID()), false
}
+1 -1
View File
@@ -10,7 +10,7 @@ import (
)
func TestGivenNameMapsToValidFQDNCheck(t *testing.T) {
cfg := &types.Config{BaseDomain: "example.com"}
cfg := &types.Config{DNSConfig: types.DNSConfig{BaseDomain: "example.com"}}
_, _, ok := givenNameMapsToValidFQDN.check((&types.Node{ID: 1, GivenName: "valid"}).View(), cfg)
require.True(t, ok, "a valid given name must pass the check")
+3 -3
View File
@@ -42,8 +42,8 @@ func TestNewNodeStoreBumpsDuplicateNames(t *testing.T) {
func TestPutNodeBumpsPastReservedLabel(t *testing.T) {
cfg := persistTestConfig(t.TempDir() + "/headscale.db")
cfg.DNSConfig.ExtraRecords = []tailcfg.DNSRecord{
{Name: "laptop." + cfg.BaseDomain},
{Name: "laptop-1." + cfg.BaseDomain},
{Name: "laptop." + cfg.DNSConfig.BaseDomain},
{Name: "laptop-1." + cfg.DNSConfig.BaseDomain},
}
s, err := NewState(cfg)
require.NoError(t, err)
@@ -56,7 +56,7 @@ func TestPutNodeBumpsPastReservedLabel(t *testing.T) {
func TestSetGivenNameRejectsReservedLabel(t *testing.T) {
cfg := persistTestConfig(t.TempDir() + "/headscale.db")
cfg.DNSConfig.ExtraRecords = []tailcfg.DNSRecord{
{Name: "laptop." + cfg.BaseDomain},
{Name: "laptop." + cfg.DNSConfig.BaseDomain},
}
s, err := NewState(cfg)
require.NoError(t, err)
+8 -10
View File
@@ -86,10 +86,8 @@ func persistTestConfig(dbPath string) *types.Config {
Path: dbPath,
},
},
PrefixV4: &prefixV4,
PrefixV6: &prefixV6,
IPAllocation: types.IPAllocationStrategySequential,
BaseDomain: "headscale.test",
Prefixes: types.PrefixesConfig{V4: &prefixV4, V6: &prefixV6, Allocation: types.IPAllocationStrategySequential},
DNSConfig: types.DNSConfig{BaseDomain: "headscale.test"},
Policy: types.PolicyConfig{
Mode: types.PolicyModeDB,
},
@@ -104,7 +102,7 @@ func TestLoadedDuplicateNamesAreResolvedInMemory(t *testing.T) {
dbPath := t.TempDir() + "/headscale.db"
cfg := persistTestConfig(dbPath)
cfg.DNSConfig.ExtraRecords = []tailcfg.DNSRecord{
{Name: "grafana." + cfg.BaseDomain, Type: "A", Value: "100.64.0.100"},
{Name: "grafana." + cfg.DNSConfig.BaseDomain, Type: "A", Value: "100.64.0.100"},
}
database, err := db.NewHeadscaleDatabase(cfg)
require.NoError(t, err)
@@ -737,9 +735,9 @@ func (failingSetNodesPolicyManager) SetNodes(views.Slice[types.NodeView]) (bool,
func TestDeleteNodeTwiceReleasesAddressOnce(t *testing.T) {
prefix := netip.MustParsePrefix("100.64.0.0/30")
cfg := persistTestConfig(t.TempDir() + "/headscale.db")
cfg.PrefixV4 = &prefix
cfg.PrefixV6 = nil
cfg.IPAllocation = types.IPAllocationStrategyRandom
cfg.Prefixes.V4 = &prefix
cfg.Prefixes.V6 = nil
cfg.Prefixes.Allocation = types.IPAllocationStrategyRandom
s, err := NewState(cfg)
require.NoError(t, err)
@@ -1555,7 +1553,7 @@ func TestPostgresUpdatePolicyConcurrentWrites(t *testing.T) {
Port: port,
Name: strings.TrimLeft(u.Path, "/"),
User: u.User.Username(),
Pass: pass,
Pass: types.Secret(pass),
Ssl: "disable",
},
}
@@ -1744,7 +1742,7 @@ func TestBackfillNodeIPsSurvivesConcurrentPersist(t *testing.T) {
// Dropping the IPv6 prefix gives the backfill work to do.
cfg := persistTestConfig(dbPath)
cfg.PrefixV6 = nil
cfg.Prefixes.V6 = nil
s, err := NewState(cfg)
require.NoError(t, err)
+5 -5
View File
@@ -15,7 +15,7 @@ import (
func TestNodeNameCannotShadowExtraRecord(t *testing.T) {
cfg := persistTestConfig(t.TempDir() + "/headscale.db")
cfg.DNSConfig.ExtraRecords = []tailcfg.DNSRecord{
{Name: "grafana." + cfg.BaseDomain, Type: "A", Value: "100.64.0.100"},
{Name: "grafana." + cfg.DNSConfig.BaseDomain, Type: "A", Value: "100.64.0.100"},
}
cfg.StartDNS(nil) // as NewHeadscale does, so SetExtraRecords takes
s, err := NewState(cfg)
@@ -44,9 +44,9 @@ func TestNodeNameCannotShadowExtraRecord(t *testing.T) {
existing := register("existing")
cfg.SetExtraRecords([]tailcfg.DNSRecord{
{Name: "SVC." + cfg.BaseDomain + ".", Type: "A", Value: "100.64.0.101"},
{Name: "existing." + cfg.BaseDomain, Type: "A", Value: "100.64.0.102"},
{Name: "child.parent." + cfg.BaseDomain, Type: "A", Value: "100.64.0.103"},
{Name: "SVC." + cfg.DNSConfig.BaseDomain + ".", Type: "A", Value: "100.64.0.101"},
{Name: "existing." + cfg.DNSConfig.BaseDomain, Type: "A", Value: "100.64.0.102"},
{Name: "child.parent." + cfg.DNSConfig.BaseDomain, Type: "A", Value: "100.64.0.103"},
})
fresh, _ := s.GetNodeByID(existing.ID())
@@ -95,7 +95,7 @@ func TestRenameNodeRejectsNameExceedingFQDNLimit(t *testing.T) {
dbPath := t.TempDir() + "/headscale.db"
cfg := persistTestConfig(dbPath)
// A long base domain so a 63-char label overflows the 255-char FQDN bound.
cfg.BaseDomain = strings.Repeat("b", 200) + ".example.com"
cfg.DNSConfig.BaseDomain = strings.Repeat("b", 200) + ".example.com"
database, err := db.NewHeadscaleDatabase(cfg)
require.NoError(t, err)
+4 -4
View File
@@ -369,7 +369,7 @@ func NewState(cfg *types.Config) (*State, error) {
return nil, fmt.Errorf("initializing database: %w", err)
}
ipAlloc, err := hsdb.NewIPAllocator(db, cfg.PrefixV4, cfg.PrefixV6, cfg.IPAllocation)
ipAlloc, err := hsdb.NewIPAllocator(db, cfg.Prefixes.V4, cfg.Prefixes.V6, cfg.Prefixes.Allocation)
if err != nil {
return nil, fmt.Errorf("initializing IP allocator: %w", err)
}
@@ -449,7 +449,7 @@ func NewState(cfg *types.Config) (*State, error) {
nodeStore := NewNodeStore(nodes, policyPeersFunc(polMan), batchSize, batchTimeout)
polMan.SetRouterSession(nodeStore.RouterSession)
nodeStore.reservedLabel = func(label string) bool {
return cfg.BaseDomain != "" && cfg.HasExtraRecord(label+"."+cfg.BaseDomain)
return cfg.DNSConfig.BaseDomain != "" && cfg.HasExtraRecord(label+"."+cfg.DNSConfig.BaseDomain)
}
nodeStore.Start()
@@ -1555,7 +1555,7 @@ func (s *State) RenameNode(nodeID types.NodeID, newName string) (types.NodeView,
// Validate the label AND that the resulting FQDN fits MaxHostnameLength:
// a valid 63-char label can still overflow under a long base_domain, and
// an unmappable name would break this node and its peers (issue #3346).
err := types.ValidateGivenName(newName, s.cfg.BaseDomain)
err := types.ValidateGivenName(newName, s.cfg.DNSConfig.BaseDomain)
if err != nil {
return types.NodeView{}, change.Change{}, fmt.Errorf("%w: %w", ErrGivenNameInvalid, err)
}
@@ -1607,7 +1607,7 @@ func (s *State) WarnNodesHoldingExtraRecords() []types.NodeID {
ids = append(ids, n.ID())
log.Warn().
Uint64(zf.NodeID, n.ID().Uint64()).
Str("record", n.GivenName()+"."+s.cfg.BaseDomain).
Str("record", n.GivenName()+"."+s.cfg.DNSConfig.BaseDomain).
Msg("node keeps a name that is also an extra DNS record")
}
+3 -3
View File
@@ -16,9 +16,9 @@ import (
)
const (
SelfUpdateIdentifier = "self-update"
DatabasePostgres = "postgres"
DatabaseSqlite = "sqlite3"
SelfUpdateIdentifier = "self-update"
DatabasePostgres DatabaseType = "postgres"
DatabaseSqlite DatabaseType = "sqlite3"
)
// Common errors.
+248 -316
View File
File diff suppressed because it is too large. Load diff
+52
View File
@@ -0,0 +1,52 @@
package types
import (
"encoding"
"reflect"
)
// ConfigField is one key of the configuration: a leaf value or an open map.
type ConfigField struct {
// Path is the dotted key, e.g. "dns.nameservers.global".
Path string
Field reflect.StructField
// Value is the field in the walked Config; it is settable.
Value reflect.Value
// Open marks a map, whose keys the user chooses.
Open bool
}
var textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]()
// ConfigFields lists every leaf and open map of cfg reachable through koanf
// tags, in declaration order. A struct is a leaf when *T unmarshals text;
// otherwise it is descended into. Fields tagged koanf:"-" are skipped.
func ConfigFields(cfg *Config) []ConfigField {
return appendConfigFields(nil, "", reflect.ValueOf(cfg).Elem())
}
func appendConfigFields(fields []ConfigField, prefix string, v reflect.Value) []ConfigField {
for f, fv := range v.Fields() {
name := f.Tag.Get("koanf")
if !f.IsExported() || name == "" || name == "-" {
continue
}
path := prefix + name
if f.Type.Kind() == reflect.Struct && !reflect.PointerTo(f.Type).Implements(textUnmarshalerType) {
fields = appendConfigFields(fields, path+".", fv)
continue
}
fields = append(fields, ConfigField{
Path: path,
Field: f,
Value: fv,
Open: f.Type.Kind() == reflect.Map,
})
}
return fields
}
+173
View File
@@ -0,0 +1,173 @@
package types
import (
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfigFields(t *testing.T) {
// Written out, not derived from the loader's key tables, so it pins the
// schema on its own.
wantPaths := []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_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.slow_threshold",
"database.gorm.skip_err_record_not_found",
"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.nameservers.global",
"dns.nameservers.split",
"dns.search_domains",
"dns.extra_records",
"dns.extra_records_path",
"dns.certificates.enabled",
"dns.certificates.zone",
"dns.challenge.propagation_timeout",
"dns.challenge.resolvers",
"dns.challenge.provider",
"tls.cert_path",
"tls.key_path",
"tls.acme.hostname",
"tls.acme.challenge_type",
"tls.acme.http_listen",
"tls.acme.url",
"tls.acme.email",
"tls.acme.cache_dir",
"oidc.only_start_if_oidc_is_available",
"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.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",
}
// dns.extra_records is a list, a leaf like any other.
wantOpen := []string{"dns.nameservers.split", "dns.challenge.provider", "oidc.extra_params"}
var cfg Config
fields := ConfigFields(&cfg)
gotPaths := make([]string, 0, len(fields))
var gotOpen []string
for _, f := range fields {
gotPaths = append(gotPaths, f.Path)
if f.Open {
gotOpen = append(gotOpen, f.Path)
}
}
if diff := cmp.Diff(wantPaths, gotPaths); diff != "" {
t.Errorf("ConfigFields paths (-want +got):\n%s", diff)
}
assert.Equal(t, wantOpen, gotOpen)
t.Run("values are the config's fields", func(t *testing.T) {
require.Equal(t, "server_url", fields[0].Path)
require.True(t, fields[0].Value.CanSet())
fields[0].Value.SetString("https://h")
assert.Equal(t, "https://h", cfg.ServerURL)
assert.Equal(t, "ServerURL", fields[0].Field.Name)
})
t.Run("every exported field has a koanf tag", func(t *testing.T) {
var check func(reflect.Type, string)
check = func(typ reflect.Type, at string) {
for f := range typ.Fields() {
tag := f.Tag.Get("koanf")
if !f.IsExported() || tag == "-" {
continue
}
if tag == "" {
t.Errorf("%s.%s has no koanf tag", at, f.Name)
continue
}
if f.Type.Kind() == reflect.Struct {
check(f.Type, at+"."+f.Name)
}
}
}
check(reflect.TypeFor[Config](), "Config")
})
}
+3 -3
View File
@@ -685,16 +685,16 @@ dns:
require.NoError(t, LoadConfig(dir, false))
assert.Empty(t, src.Warnings())
cfg, err := CheckServerConfig()
cfg, err := LoadServerConfig()
require.NoError(t, err)
if len(tt.want) == 0 {
assert.Empty(t, cfg.DNSChallenge.ProviderSettings)
assert.Empty(t, cfg.DNSConfig.Challenge.ProviderSettings)
return
}
assert.Equal(t, tt.want, cfg.DNSChallenge.ProviderSettings)
assert.Equal(t, tt.want, cfg.DNSConfig.Challenge.ProviderSettings)
})
}
}
+162 -191
View File
@@ -194,7 +194,7 @@ func TestReadConfig(t *testing.T) {
return map[string]string{
"server_url": cfg.ServerURL,
"base_domain": cfg.BaseDomain,
"base_domain": cfg.DNSConfig.BaseDomain,
}, err
},
want: map[string]string{
@@ -589,10 +589,6 @@ prefixes:
v4: not-a-cidr
v6: also-not-a-cidr
allocation: bogus
oidc:
issuer: https://idp.example.com
client_id: headscale
client_secret: file:/nonexistent/secret
noise:
private_key_path: noise_private.key
database:
@@ -617,8 +613,6 @@ dns:
// Sentinels stay reachable through ConfigError.Cause.
require.ErrorIs(t, err, ErrInvalidAllocationStrategy,
"wrapped ErrInvalidAllocationStrategy not in chain")
require.ErrorIs(t, err, os.ErrNotExist,
"wrapped secret read error not in chain")
// Each sub-builder failure surfaces its YAML key in the rendered output.
rendered := err.Error()
@@ -626,7 +620,6 @@ dns:
"prefixes.v4",
"prefixes.v6",
"prefixes.allocation",
"oidc.client_secret",
} {
assert.Contains(t, rendered, want,
"expected rendered error to mention %q", want)
@@ -898,17 +891,11 @@ func TestRemovedKeysAreRejected(t *testing.T) {
}
}
// TestConfigSecrets pins that the secret settings take a value or file:<path>,
// read only by the consumer that uses the secret.
// TestConfigSecrets pins that the loaders keep each secret as written, a
// value or file:<path>, and read no file: its consumer does
// (TestNewHeadscaleSecrets, TestCLIAPIKey).
func TestConfigSecrets(t *testing.T) {
dir := t.TempDir()
for _, name := range []string{"oidc", "pg", "api"} {
require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(name+"-from-file\n"), 0o600))
}
require.NoError(t, os.WriteFile(filepath.Join(dir, "empty"), []byte(" \n"), 0o600))
missing := "file:" + filepath.Join(dir, "missing")
missing := "file:" + filepath.Join(t.TempDir(), "missing")
base := `---
server_url: https://headscale.example.com
@@ -917,121 +904,36 @@ noise:
prefixes:
v4: 100.64.0.0/10
database:
type: sqlite
type: postgres
dns:
magic_dns: false
override_local_dns: false
cli:
address: headscale.example.com:443
oidc:
issuer: https://idp.example.com
client_id: headscale
`
// Postgres and a remote CLI are the consumers of the other two secrets.
active := map[string]string{
"HEADSCALE_DATABASE_TYPE": "postgres",
"HEADSCALE_CLI_ADDRESS": "headscale.example.com:443",
}
tests := []struct {
name string
yaml string
env map[string]string
want [3]string // server oidc.client_secret, server database.postgres.pass, CLI cli.api_key
wantErr string // from LoadServerConfig
wantCLIErr string
}{
{
name: "literal values",
yaml: base + " client_secret: oidc\n",
env: map[string]string{"HEADSCALE_DATABASE_POSTGRES_PASS": "pg", "HEADSCALE_CLI_API_KEY": "api"},
want: [3]string{"oidc", "pg", "api"},
},
{
name: "file values for active consumers",
yaml: base + " client_secret: file:" + filepath.Join(dir, "oidc") + "\n",
//nolint:gosec // paths to test files, not credentials
env: map[string]string{
"HEADSCALE_DATABASE_TYPE": active["HEADSCALE_DATABASE_TYPE"],
"HEADSCALE_CLI_ADDRESS": active["HEADSCALE_CLI_ADDRESS"],
"SECRET_DIR": dir,
"HEADSCALE_DATABASE_POSTGRES_PASS": "file:$SECRET_DIR/pg",
"HEADSCALE_CLI_API_KEY": "file:${SECRET_DIR}/api",
},
want: [3]string{"oidc-from-file", "pg-from-file", "api-from-file"},
},
{
// sqlite and the unix socket use neither secret.
name: "inactive consumers read nothing",
yaml: base + " client_secret: oidc\n",
env: map[string]string{"HEADSCALE_DATABASE_POSTGRES_PASS": missing, "HEADSCALE_CLI_API_KEY": missing},
want: [3]string{"oidc", missing, missing},
},
{
// OIDC is active only with an issuer.
name: "inactive oidc reads nothing",
yaml: strings.Replace(base, " issuer: https://idp.example.com\n", "", 1) + " client_secret: " + missing + "\n",
want: [3]string{missing, "", ""},
},
{
name: "missing oidc file",
yaml: base + " client_secret: " + missing + "\n",
wantErr: "oidc.client_secret cannot be read",
},
{
name: "empty oidc file",
yaml: base + " client_secret: file:" + filepath.Join(dir, "empty") + "\n",
wantErr: "oidc.client_secret",
},
{
name: "missing postgres file",
yaml: base + " client_secret: oidc\n",
env: map[string]string{
"HEADSCALE_DATABASE_TYPE": active["HEADSCALE_DATABASE_TYPE"],
"HEADSCALE_DATABASE_POSTGRES_PASS": missing,
},
wantErr: "database.postgres.pass cannot be read",
},
{
name: "missing api key file for a remote CLI",
yaml: base + " client_secret: oidc\n",
env: map[string]string{
"HEADSCALE_CLI_ADDRESS": active["HEADSCALE_CLI_ADDRESS"],
"HEADSCALE_CLI_API_KEY": missing,
},
wantCLIErr: "cli.api_key cannot be read",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for k, v := range tt.env {
t.Setenv(k, v)
}
for name, value := range map[string]string{"literal": "literal", "missing file": missing} {
t.Run(name, func(t *testing.T) {
t.Setenv("HEADSCALE_DATABASE_POSTGRES_PASS", value)
t.Setenv("HEADSCALE_CLI_API_KEY", value)
tmpDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "config.yaml"), []byte(tt.yaml), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "config.yaml"), []byte(base+" client_secret: "+value+"\n"), 0o600))
require.NoError(t, LoadConfig(tmpDir, false))
cfg, err := LoadServerConfig()
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
return
}
require.NoError(t, err)
// The server never uses the CLI's key, so never reads it.
assert.Equal(t, Secret(value), cfg.OIDC.ClientSecret)
assert.Equal(t, Secret(value), cfg.Database.Postgres.Pass)
// The server never uses the CLI's key.
assert.Empty(t, cfg.CLI.APIKey)
cli, err := LoadCLIConfig()
if tt.wantCLIErr != "" {
require.ErrorContains(t, err, tt.wantCLIErr)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, [3]string{cfg.OIDC.ClientSecret, cfg.Database.Postgres.Pass, cli.CLI.APIKey})
assert.Equal(t, Secret(value), cli.CLI.APIKey)
})
}
}
@@ -1044,7 +946,7 @@ func TestUnixSocketPermission(t *testing.T) {
name string
yaml string
env string
want fs.FileMode
want FileMode
wantErr bool
}{
{name: "unset-uses-default", want: 0o770},
@@ -1245,9 +1147,9 @@ func TestSafeServerURLWithPort(t *testing.T) {
}
// TestConfigJSONOmitsSecrets verifies that marshalling a [Config] to JSON
// (as /debug/config does via [state.State.DebugConfig]) 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
// (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.
func TestConfigJSONOmitsSecrets(t *testing.T) {
@@ -1256,6 +1158,10 @@ func TestConfigJSONOmitsSecrets(t *testing.T) {
secretClientSecret = "oidc-client-secret-marker" //nolint:gosec // test marker, not a real credential
secretAPIKey = "headscale-cli-api-key-marker" //nolint:gosec // test marker, not a real credential
secretDNSToken = "dns-provider-token-marker" //nolint:gosec // test marker, not a real credential
secretDERPUser = "derp-url-username-marker" //nolint:gosec // test marker, not a real credential
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"
)
cfg := &Config{
@@ -1270,20 +1176,59 @@ func TestConfigJSONOmitsSecrets(t *testing.T) {
CLI: CLIConfig{
APIKey: secretAPIKey,
},
DNSChallenge: DNSChallengeConfig{
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",
"https://" + secretDERPToken + "@derp.example/token-map",
},
DERPMap: &tailcfg.DERPMap{Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{
900: {RegionCode: derpMapMarker},
}},
},
}
secrets := []string{
secretPostgresPass, secretClientSecret, secretAPIKey, secretDNSToken,
secretDERPUser, secretDERPPass, secretDERPToken,
}
out, err := json.Marshal(cfg)
require.NoError(t, err)
body := string(out)
for _, secret := range []string{secretPostgresPass, secretClientSecret, secretAPIKey, secretDNSToken} {
assert.NotContains(t, body, secret,
for _, secret := range secrets {
assert.NotContains(t, string(out), secret,
"marshalled Config must not contain secret %q", secret)
}
// The URLs are still shown, without userinfo; the request keeps it.
assert.Contains(t, string(out), `"https://derp.example/map"`)
assert.Contains(t, string(out), `"https://derp.example/token-map"`)
assert.Contains(t, string(cfg.DERP.URLs[0]), secretDERPUser+":"+secretDERPPass)
assert.Contains(t, string(cfg.DERP.URLs[1]), secretDERPToken)
// DERPMap is Go-only.
assert.NotContains(t, string(out), derpMapMarker)
// Secret redacts only through fmt's String; zerolog marshals it. Test
// binaries log at Error (testlog.go), which would drop this record.
oldLevel := zerolog.GlobalLevel()
t.Cleanup(func() { zerolog.SetGlobalLevel(oldLevel) })
zerolog.SetGlobalLevel(zerolog.InfoLevel)
var logged bytes.Buffer
logger := zerolog.New(&logged)
logger.Info().Interface("config", cfg).Send()
require.Contains(t, logged.String(), `"config":{`)
for _, secret := range secrets {
assert.NotContains(t, logged.String(), secret,
"logged Config must not contain secret %q", secret)
}
}
//nolint:goconst // repeated CIDR strings are test fixtures, not refactor candidates
@@ -1457,7 +1402,7 @@ func TestTailcfgDNSConfig(t *testing.T) {
{
name: "magic dns reverse routes outside cgnat",
cfg: Config{
PrefixV4: &ten,
Prefixes: PrefixesConfig{V4: &ten},
DNSConfig: DNSConfig{MagicDNS: true, BaseDomain: "example.com"},
},
want: &tailcfg.DNSConfig{
@@ -1469,7 +1414,7 @@ func TestTailcfgDNSConfig(t *testing.T) {
{
name: "magic dns reverse routes outside ula",
cfg: Config{
PrefixV6: &fd00,
Prefixes: PrefixesConfig{V6: &fd00},
DNSConfig: DNSConfig{MagicDNS: true, BaseDomain: "example.com"},
},
want: &tailcfg.DNSConfig{
@@ -1481,8 +1426,7 @@ func TestTailcfgDNSConfig(t *testing.T) {
{
name: "magic dns inside cgnat and ula",
cfg: Config{
PrefixV4: &cgnat,
PrefixV6: &ula,
Prefixes: PrefixesConfig{V4: &cgnat, V6: &ula},
DNSConfig: DNSConfig{MagicDNS: true, BaseDomain: "example.com"},
},
want: &tailcfg.DNSConfig{
@@ -1493,7 +1437,7 @@ func TestTailcfgDNSConfig(t *testing.T) {
},
{
name: "no reverse routes without magic dns",
cfg: Config{PrefixV4: &ten},
cfg: Config{Prefixes: PrefixesConfig{V4: &ten}},
want: &tailcfg.DNSConfig{Routes: map[string][]*dnstype.Resolver{}},
},
{
@@ -1525,8 +1469,8 @@ func TestLiveDNS(t *testing.T) {
MagicDNS: true,
BaseDomain: "example.com",
ExtraRecords: []tailcfg.DNSRecord{{Name: "Old.example.com", Type: "A", Value: "100.64.0.1"}},
Challenge: DNSChallengeConfig{Provider: goOnly},
},
DNSChallenge: DNSChallengeConfig{Provider: goOnly},
}
assert.Equal(t, cfg.TailcfgDNSConfig(), cfg.CloneTailcfgDNSConfig())
@@ -1589,7 +1533,7 @@ dns:
zerolog.SetGlobalLevel(oldLevel)
})
cfg, err := CheckServerConfig()
cfg, err := LoadServerConfig()
require.NoError(t, err)
logged := buf.String()
@@ -1627,7 +1571,7 @@ dns:
yaml string
env map[string]string
wantErr string
fileErr bool // only reading a file: secret fails, so CheckServerConfig passes
fileErr bool // only reading a file: secret fails, so the load passes
wantWarns []string
wantZone string
wantToken string
@@ -1722,7 +1666,7 @@ dns:
fileErr: true,
},
{
// Unread by CheckServerConfig, a file: setting counts as present.
// Unread by the load, a file: setting counts as present.
name: "token file missing",
yaml: base + ` challenge:
provider:
@@ -2057,38 +2001,39 @@ tls:
assert.Contains(t, src.Warnings()[i], w)
}
_, checkErr := CheckServerConfig()
if tt.wantErr != "" && !tt.fileErr {
require.ErrorContains(t, checkErr, tt.wantErr)
assert.NotContains(t, checkErr.Error(), "marker-")
} else {
require.NoError(t, checkErr)
}
cfg, err := LoadServerConfig()
if tt.wantErr != "" {
if tt.wantErr != "" && !tt.fileErr {
require.ErrorContains(t, err, tt.wantErr)
assert.NotContains(t, err.Error(), "marker-")
return
}
require.NoError(t, err)
assert.Equal(t, tt.wantZone, cfg.DNSConfig.CertificateZone())
if tt.wantResolvers != nil {
assert.Equal(t, tt.wantResolvers, cfg.DNSChallenge.ResolverAddrs())
}
// The loader only validates the provider; startup builds it.
assert.Nil(t, cfg.DNSChallenge.Provider)
if tt.wantNoProv {
assert.Empty(t, cfg.DNSChallenge.ProviderName())
if tt.fileErr {
_, err = cfg.DNSConfig.Challenge.NewProvider(ResolveSecret)
require.ErrorContains(t, err, tt.wantErr)
return
}
built, err := cfg.DNSChallenge.NewProvider(ResolveSecret)
assert.Equal(t, tt.wantZone, cfg.DNSConfig.CertificateZone())
if tt.wantResolvers != nil {
assert.Equal(t, tt.wantResolvers, cfg.DNSConfig.Challenge.ResolverAddrs())
}
// The loader only validates the provider; startup builds it.
assert.Nil(t, cfg.DNSConfig.Challenge.Provider)
if tt.wantNoProv {
assert.Empty(t, cfg.DNSConfig.Challenge.ProviderName())
return
}
built, err := cfg.DNSConfig.Challenge.NewProvider(ResolveSecret)
require.NoError(t, err)
if tt.wantProvider != "" {
@@ -2324,7 +2269,7 @@ func TestPostgresConfigDSN(t *testing.T) {
func TestServesRoute(t *testing.T) {
v4 := netip.MustParsePrefix("100.64.0.0/10")
v6 := netip.MustParsePrefix("fd7a:115c:a1e0::/48")
cfg := &Config{PrefixV4: &v4, PrefixV6: &v6}
cfg := &Config{Prefixes: PrefixesConfig{V4: &v4, V6: &v6}}
for route, want := range map[string]bool{
"100.64.0.11/32": false,
@@ -2348,7 +2293,7 @@ func TestServesRoute(t *testing.T) {
// Only the configured ranges hold nodes.
narrow := netip.MustParsePrefix("100.64.0.0/24")
assert.True(t, (&Config{PrefixV4: &narrow}).ServesRoute(netip.MustParsePrefix("100.100.0.1/32")))
assert.True(t, (&Config{Prefixes: PrefixesConfig{V4: &narrow}}).ServesRoute(netip.MustParsePrefix("100.100.0.1/32")))
}
func TestBoolConfigValidation(t *testing.T) {
@@ -2854,21 +2799,30 @@ type compatTuning struct {
func compatView(cfg *Config) compatConfig {
pg := cfg.Database.Postgres
oidc := cfg.OIDC
ch := cfg.DNSChallenge
ch := cfg.DNSConfig.Challenge
acme := cfg.TLS.ACME
derp := cfg.DERP.Server
dbType := cfg.Database.Type
if cfg.Database.IsSQLite() {
dbType = DatabaseSqlite
}
derpURLs := make([]url.URL, 0, len(cfg.DERP.URLs))
for _, s := range cfg.DERP.URLs {
u, err := url.Parse(string(s))
if err == nil {
derpURLs = append(derpURLs, *u)
}
}
tuning := compatTuning(cfg.Tuning)
tuning.BatcherWorkers = cfg.Tuning.BatcherWorkerCount()
return compatConfig{
ServerURL: cfg.ServerURL,
Addr: cfg.Addr,
MetricsAddr: cfg.MetricsAddr,
Addr: string(cfg.Addr),
MetricsAddr: string(cfg.MetricsAddr),
TrustedProxies: cfg.TrustedProxies,
Node: compatNodeConfig{
Expiry: cfg.Node.Expiry,
@@ -2876,17 +2830,25 @@ func compatView(cfg *Config) compatConfig {
Routes: compatRouteConfig{HA: compatHARouteConfig(cfg.Node.Routes.HA)},
},
PreAuthKeys: compatPreAuthKeysConfig(cfg.PreAuthKeys),
PrefixV4: cfg.PrefixV4,
PrefixV6: cfg.PrefixV6,
IPAllocation: string(cfg.IPAllocation),
NoisePrivateKeyPath: cfg.AbsPath(cfg.NoisePrivateKeyPath),
BaseDomain: cfg.BaseDomain,
Log: compatLogConfig(cfg.Log),
DisableUpdateCheck: cfg.DisableUpdateCheck,
PrefixV4: cfg.Prefixes.V4,
PrefixV6: cfg.Prefixes.V6,
IPAllocation: string(cfg.Prefixes.Allocation),
NoisePrivateKeyPath: cfg.AbsPath(cfg.Noise.PrivateKeyPath),
BaseDomain: cfg.DNSConfig.BaseDomain,
Log: compatLogConfig{
Format: string(cfg.Log.Format),
Level: cfg.Log.Level.Zerolog(),
},
DisableUpdateCheck: cfg.DisableUpdateCheck,
Database: compatDatabaseConfig{
Type: dbType,
Type: string(dbType),
Debug: cfg.Database.Debug,
Gorm: compatGormConfig(cfg.Database.Gorm),
Gorm: compatGormConfig{
Debug: cfg.Database.Debug,
SlowThreshold: time.Duration(cfg.Database.Gorm.SlowThreshold) * time.Millisecond,
SkipErrRecordNotFound: cfg.Database.Gorm.SkipErrRecordNotFound,
ParameterizedQueries: cfg.Database.Gorm.ParameterizedQueries,
},
Sqlite: compatSqliteConfig{
Path: cfg.SQLitePath(),
WriteAheadLog: cfg.Database.Sqlite.WriteAheadLog,
@@ -2904,30 +2866,30 @@ func compatView(cfg *Config) compatConfig {
},
},
DERP: compatDERPConfig{
ServerEnabled: cfg.DERP.ServerEnabled,
AutomaticallyAddEmbeddedDerpRegion: cfg.DERP.AutomaticallyAddEmbeddedDerpRegion,
ServerRegionID: cfg.DERP.ServerRegionID,
ServerRegionCode: cfg.DERP.ServerRegionCode,
ServerRegionName: cfg.DERP.ServerRegionName,
ServerPrivateKeyPath: cfg.AbsPath(cfg.DERP.ServerPrivateKeyPath),
ServerVerifyClients: cfg.DERP.ServerVerifyClients,
STUNAddr: cfg.DERP.STUNAddr,
URLs: cfg.DERP.URLs,
ServerEnabled: derp.Enabled,
AutomaticallyAddEmbeddedDerpRegion: derp.AutomaticallyAddEmbeddedDerpRegion,
ServerRegionID: derp.RegionID,
ServerRegionCode: derp.RegionCode,
ServerRegionName: derp.RegionName,
ServerPrivateKeyPath: cfg.AbsPath(derp.PrivateKeyPath),
ServerVerifyClients: derp.VerifyClients,
STUNAddr: string(derp.STUNAddr),
URLs: derpURLs,
Paths: cfg.DERP.Paths,
DERPMap: cfg.DERP.DERPMap,
AutoUpdate: cfg.DERP.AutoUpdate,
UpdateFrequency: cfg.DERP.UpdateFrequency,
IPv4: cfg.DERP.IPv4,
IPv6: cfg.DERP.IPv6,
IPv4: derp.IPv4,
IPv6: derp.IPv6,
},
TLS: compatTLSConfig{
CertPath: cfg.AbsPath(cfg.TLS.CertPath),
KeyPath: cfg.AbsPath(cfg.TLS.KeyPath),
ACME: compatACMEConfig{
Hostname: acme.Hostname,
ChallengeType: acme.ChallengeType,
HTTPListen: acme.HTTPListen,
URL: acme.URL,
ChallengeType: string(acme.ChallengeType),
HTTPListen: string(acme.HTTPListen),
URL: string(acme.URL),
Email: acme.Email,
CacheDir: cfg.AbsPath(acme.CacheDir),
},
@@ -2952,10 +2914,10 @@ func compatView(cfg *Config) compatConfig {
ProviderName: ch.ProviderName(),
},
UnixSocket: cfg.UnixSocket,
UnixSocketPermission: cfg.UnixSocketPermission,
UnixSocketPermission: fs.FileMode(cfg.UnixSocketPermission),
OIDC: compatOIDCConfig{
OnlyStartIfOIDCIsAvailable: oidc.OnlyStartIfOIDCIsAvailable,
Issuer: oidc.Issuer,
Issuer: string(oidc.Issuer),
ClientID: oidc.ClientID,
Scope: oidc.Scope,
ExtraParams: oidc.ExtraParams,
@@ -2964,7 +2926,7 @@ func compatView(cfg *Config) compatConfig {
AllowedGroups: oidc.AllowedGroups,
EmailVerifiedRequired: oidc.EmailVerifiedRequired,
UseExpiryFromToken: oidc.UseExpiryFromToken,
PKCE: compatPKCEConfig(oidc.PKCE),
PKCE: compatPKCEConfig{Enabled: oidc.PKCE.Enabled, Method: string(oidc.PKCE.Method)},
},
LogTail: compatLogTailConfig(cfg.LogTail),
Taildrop: compatTaildropConfig(cfg.Taildrop),
@@ -3057,10 +3019,21 @@ func loadCompat(t *testing.T, file, envFile string, cli bool) compatRecord {
require.NoError(t, err)
rec.Config = json.RawMessage(norm(string(raw)))
// As its consumer reads it; one no consumer reads stays as written.
secret := func(s Secret, active bool) string {
if !active {
return string(s)
}
v, err := s.Resolve()
require.NoError(t, err)
return v
}
rec.Secrets = map[string]string{
"database.postgres.pass": cfg.Database.Postgres.Pass,
"oidc.client_secret": cfg.OIDC.ClientSecret,
"cli.api_key": cfg.CLI.APIKey,
"database.postgres.pass": secret(cfg.Database.Postgres.Pass, cfg.Database.Type == DatabasePostgres),
"oidc.client_secret": secret(cfg.OIDC.ClientSecret, cfg.OIDC.Issuer != ""),
"cli.api_key": secret(cfg.CLI.APIKey, cfg.CLI.Address != ""),
}
maps.DeleteFunc(rec.Secrets, func(_, v string) bool { return v == "" })
}
@@ -3195,8 +3168,6 @@ func TestConfigNotLoaded(t *testing.T) {
_, err := LoadServerConfig()
require.ErrorIs(t, err, errConfigNotLoaded)
_, err = CheckServerConfig()
require.ErrorIs(t, err, errConfigNotLoaded)
_, err = LoadCLIConfig()
require.ErrorIs(t, err, errConfigNotLoaded)
@@ -3244,8 +3215,8 @@ dns:
cfg, err := LoadServerConfig()
require.NoError(t, err)
assert.Equal(t, "./noise.key", cfg.NoisePrivateKeyPath)
assert.Equal(t, filepath.Join(dir, "noise.key"), cfg.AbsPath(cfg.NoisePrivateKeyPath))
assert.Equal(t, "./noise.key", cfg.Noise.PrivateKeyPath)
assert.Equal(t, filepath.Join(dir, "noise.key"), cfg.AbsPath(cfg.Noise.PrivateKeyPath))
assert.Equal(t, "./policy.hujson", cfg.Policy.Path)
assert.Equal(t, filepath.Join(dir, "policy.hujson"), cfg.AbsPath(cfg.Policy.Path))
}
@@ -3277,7 +3248,7 @@ func TestSQLiteMemoryPath(t *testing.T) {
cfg, err := LoadServerConfig()
require.NoError(t, err)
assert.Equal(t, ":memory:", cfg.SQLitePath())
assert.True(t, filepath.IsAbs(cfg.AbsPath(cfg.NoisePrivateKeyPath)), cfg.AbsPath(cfg.NoisePrivateKeyPath))
assert.True(t, filepath.IsAbs(cfg.AbsPath(cfg.Noise.PrivateKeyPath)), cfg.AbsPath(cfg.Noise.PrivateKeyPath))
})
}
}
+267
View File
@@ -0,0 +1,267 @@
package types
import (
"encoding/json"
"fmt"
"io/fs"
"net/url"
"os"
"slices"
"strings"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/rs/zerolog"
)
// Parse errors of the value types below are *ConfigError without the key;
// the loader adds the key and where the value came from.
// Secret is a credential as written: a literal, or "file:<path>" naming
// the file that holds it (Tailscale's form), which keeps the secret out of
// the config and the environment. Only fmt's String path redacts; JSON,
// %#v and zerolog's Interface print the raw value.
type Secret string
// Resolve returns the literal, or the trimmed contents of the file. The
// path is expanded so a systemd credential can be named by
// ${CREDENTIALS_DIRECTORY}. Errors name the path; callers add the key.
func (s Secret) Resolve() (string, error) {
path, ok := strings.CutPrefix(string(s), "file:")
if !ok {
return string(s), nil
}
path = os.ExpandEnv(path)
b, err := os.ReadFile(path)
if err != nil {
return "", err
}
// An empty file is a credential that was not written, not a choice.
secret := strings.TrimSpace(string(b))
if secret == "" {
return "", fmt.Errorf("%s: %w", path, errSecretEmpty)
}
return secret, nil
}
// IsFile reports whether s names a file.
func (s Secret) IsFile() bool { return strings.HasPrefix(string(s), "file:") }
// Redacted returns s safe to show: a file reference as written, a literal
// as <redacted>.
func (s Secret) Redacted() string {
if s == "" || s.IsFile() {
return string(s)
}
return "<redacted>"
}
func (s Secret) String() string { return s.Redacted() }
// FileMode is a permission written as quoted octal text ("0770", "0o770").
type FileMode fs.FileMode
func (m *FileMode) UnmarshalText(text []byte) error {
mode, err := util.ParseFileMode(string(text))
if err != nil {
return &ConfigError{
Reason: "is not a quoted octal file mode",
Maximum: `"0777"`,
Hint: `quote the value, e.g. "0770"; unquoted, YAML reads 0770 as the number 504`,
Cause: err,
}
}
*m = FileMode(mode)
return nil
}
func (m FileMode) MarshalText() ([]byte, error) {
return fmt.Appendf(nil, "%04o", uint32(m)), nil
}
// HTTPURL is "" or an absolute http:// or https:// URL with a hostname, kept
// as written. JSON shows it without userinfo; it has no MarshalText, so the
// 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.
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(s)
}
func (u *HTTPURL) UnmarshalText(text []byte) error {
s := string(text)
if s != "" {
p, err := url.Parse(s)
if err != nil || (p.Scheme != "http" && p.Scheme != "https") || p.Hostname() == "" {
e := &ConfigError{
Reason: "is not an http:// or https:// URL",
Hint: `write the scheme and host, e.g. "https://example.com"`,
}
if err != nil {
// Not the parser's error: it quotes the URL, userinfo included.
e.Cause = errURLSyntax
}
return e
}
}
*u = HTTPURL(s)
return nil
}
// ListenAddr is "" or a TCP host:port address; the port may be "http" or
// "https".
type ListenAddr string
func (a *ListenAddr) UnmarshalText(text []byte) error {
s := string(text)
if s != "" {
_, err := PortFromAddr(s)
if err != nil {
return &ConfigError{
Reason: "is not a host:port address",
Detail: err.Error(),
Hint: `use host:port form, e.g. "0.0.0.0:8080"`,
Cause: err,
}
}
}
*a = ListenAddr(s)
return nil
}
// setEnum stores s in dst when it is "" (unset) or one of allowed;
// otherwise it returns e completed with the allowed values.
func setEnum[T ~string](dst *T, s string, allowed []string, e ConfigError) error {
if s != "" && !slices.Contains(allowed, s) {
e.Reason = "has an unsupported value"
e.Allowed = allowed
return &e
}
*dst = T(s)
return nil
}
// LogLevel is a zerolog level name, case-insensitive and stored lowercase.
// "" decodes without error to the zero value (unset).
type LogLevel string //nolint:recvcheck // UnmarshalText needs a pointer; the rest read the value
func (LogLevel) Allowed() []string {
return []string{"trace", "debug", "info", "warn", "error", "fatal", "panic", "disabled"}
}
func (l *LogLevel) UnmarshalText(text []byte) error {
return setEnum(l, strings.ToLower(string(text)), l.Allowed(), ConfigError{})
}
// Zerolog returns the level: info when unset (zerolog's NoLevel would
// hide every line); one zerolog does not know is debug.
func (l LogLevel) Zerolog() zerolog.Level {
if l == "" {
return zerolog.InfoLevel
}
lvl, err := zerolog.ParseLevel(string(l))
if err != nil {
return zerolog.DebugLevel
}
return lvl
}
// LogFormat is the log output format. "" decodes without error to the zero
// value (unset).
type LogFormat string //nolint:recvcheck // UnmarshalText needs a pointer; the rest read the value
func (LogFormat) Allowed() []string { return []string{string(TextLogFormat), string(JSONLogFormat)} }
func (f *LogFormat) UnmarshalText(text []byte) error {
return setEnum(f, string(text), f.Allowed(), ConfigError{})
}
// DatabaseType is the database backend. "" decodes without error to the
// zero value (unset).
type DatabaseType string //nolint:recvcheck // UnmarshalText needs a pointer; the rest read the value
func (DatabaseType) Allowed() []string {
return []string{"sqlite", string(DatabaseSqlite), string(DatabasePostgres)}
}
func (d *DatabaseType) UnmarshalText(text []byte) error {
return setEnum(d, string(text), d.Allowed(), ConfigError{
Hint: "pick one of the allowed values; sqlite is the default for single-host deployments",
See: "https://headscale.net/stable/ref/database/",
})
}
// ACMEChallenge is the ACME challenge type. "" decodes without error to the
// zero value (unset).
type ACMEChallenge string //nolint:recvcheck // UnmarshalText needs a pointer; the rest read the value
func (ACMEChallenge) Allowed() []string {
return []string{string(HTTP01ChallengeType), string(TLSALPN01ChallengeType), string(DNS01ChallengeType)}
}
func (c *ACMEChallenge) UnmarshalText(text []byte) error {
return setEnum(c, string(text), c.Allowed(), ConfigError{
Hint: "pick one of the allowed values; HTTP-01 is the default",
})
}
// PKCEMethod is the OIDC PKCE challenge method. "" decodes without error to
// the zero value (unset).
type PKCEMethod string //nolint:recvcheck // UnmarshalText needs a pointer; the rest read the value
func (PKCEMethod) Allowed() []string {
return []string{string(PKCEMethodPlain), string(PKCEMethodS256)}
}
func (p *PKCEMethod) UnmarshalText(text []byte) error {
return setEnum(p, string(text), p.Allowed(), ConfigError{Cause: errInvalidPKCEMethod})
}
func (PolicyMode) Allowed() []string { return []string{string(PolicyModeFile), string(PolicyModeDB)} }
func (m *PolicyMode) UnmarshalText(text []byte) error {
return setEnum(m, string(text), m.Allowed(), ConfigError{
See: "https://headscale.net/stable/ref/policy/",
})
}
func (IPAllocationStrategy) Allowed() []string {
return []string{string(IPAllocationStrategySequential), string(IPAllocationStrategyRandom)}
}
func (s *IPAllocationStrategy) UnmarshalText(text []byte) error {
return setEnum(s, string(text), s.Allowed(), ConfigError{
Hint: "pick one of the allowed values; sequential is the default",
Cause: ErrInvalidAllocationStrategy,
})
}
+276
View File
@@ -0,0 +1,276 @@
package types
import (
"encoding"
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfigValues(t *testing.T) {
tests := []struct {
name string
// dst is a fresh pointer to decode into.
dst encoding.TextUnmarshaler
in string
want any
// wantErr is a substring of the ConfigError's Reason.
wantErr string
wantSee string
}{
{name: "filemode/0770", dst: new(FileMode), in: "0770", want: FileMode(0o770)},
{name: "filemode/0o770", dst: new(FileMode), in: "0o770", want: FileMode(0o770)},
{name: "filemode/above perm bits", dst: new(FileMode), in: "17777", wantErr: "octal file mode"},
{name: "httpurl/empty", dst: new(HTTPURL), in: "", want: HTTPURL("")},
{name: "httpurl/https", dst: new(HTTPURL), in: "https://h/x", want: HTTPURL("https://h/x")},
{name: "httpurl/ftp", dst: new(HTTPURL), in: "ftp://h", wantErr: "is not an http:// or https:// URL"},
{name: "httpurl/no scheme", dst: new(HTTPURL), in: "h.example.com", wantErr: "is not an http:// or https:// URL"},
{name: "httpurl/host and port", dst: new(HTTPURL), in: "https://h:8443/x", want: HTTPURL("https://h:8443/x")},
{name: "httpurl/ipv6", dst: new(HTTPURL), in: "http://[::1]:8080", want: HTTPURL("http://[::1]:8080")},
{name: "httpurl/no host", dst: new(HTTPURL), in: "https://", wantErr: "is not an http:// or https:// URL"},
{name: "httpurl/port without host", dst: new(HTTPURL), in: "https://:443/x", wantErr: "is not an http:// or https:// URL"},
{name: "httpurl/http port without host", dst: new(HTTPURL), in: "http://:80", wantErr: "is not an http:// or https:// URL"},
{name: "httpurl/unparsable", dst: new(HTTPURL), in: "https://alice:pw?@h", wantErr: "is not an http:// or https:// URL"},
{name: "listenaddr/empty", dst: new(ListenAddr), in: "", want: ListenAddr("")},
{name: "listenaddr/port only", dst: new(ListenAddr), in: ":8080", want: ListenAddr(":8080")},
{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: "loglevel/trace", dst: new(LogLevel), in: "trace", want: LogLevel("trace")},
{name: "loglevel/debug", dst: new(LogLevel), in: "debug", want: LogLevel("debug")},
{name: "loglevel/info", dst: new(LogLevel), in: "info", want: LogLevel("info")},
{name: "loglevel/warn", dst: new(LogLevel), in: "warn", want: LogLevel("warn")},
{name: "loglevel/error", dst: new(LogLevel), in: "error", want: LogLevel("error")},
{name: "loglevel/fatal", dst: new(LogLevel), in: "fatal", want: LogLevel("fatal")},
{name: "loglevel/panic", dst: new(LogLevel), in: "panic", want: LogLevel("panic")},
{name: "loglevel/disabled", dst: new(LogLevel), in: "disabled", want: LogLevel("disabled")},
{name: "loglevel/upper case stored lower", dst: new(LogLevel), in: "INFO", want: LogLevel("info")},
{name: "loglevel/empty", dst: new(LogLevel), in: "", want: LogLevel("")},
{name: "loglevel/unknown", dst: new(LogLevel), in: "verbose", wantErr: "has an unsupported value"},
{name: "logformat/text", dst: new(LogFormat), in: "text", want: LogFormat("text")},
{name: "logformat/json", dst: new(LogFormat), in: "json", want: LogFormat("json")},
{name: "logformat/empty", dst: new(LogFormat), in: "", want: LogFormat("")},
{name: "logformat/unknown", dst: new(LogFormat), in: "yaml", wantErr: "has an unsupported value"},
{name: "database/sqlite", dst: new(DatabaseType), in: "sqlite", want: DatabaseType("sqlite")},
{name: "database/sqlite3", dst: new(DatabaseType), in: "sqlite3", want: DatabaseType("sqlite3")},
{name: "database/postgres", dst: new(DatabaseType), in: "postgres", want: DatabaseType("postgres")},
{name: "database/empty", dst: new(DatabaseType), in: "", want: DatabaseType("")},
{name: "database/unknown", dst: new(DatabaseType), in: "mysql", wantErr: "has an unsupported value", wantSee: "https://headscale.net/stable/ref/database/"},
{name: "acme/HTTP-01", dst: new(ACMEChallenge), in: "HTTP-01", want: ACMEChallenge("HTTP-01")},
{name: "acme/TLS-ALPN-01", dst: new(ACMEChallenge), in: "TLS-ALPN-01", want: ACMEChallenge("TLS-ALPN-01")},
{name: "acme/DNS-01", dst: new(ACMEChallenge), in: "DNS-01", want: ACMEChallenge("DNS-01")},
{name: "acme/empty", dst: new(ACMEChallenge), in: "", want: ACMEChallenge("")},
{name: "acme/case-sensitive", dst: new(ACMEChallenge), in: "http-01", wantErr: "has an unsupported value"},
{name: "pkce/plain", dst: new(PKCEMethod), in: "plain", want: PKCEMethod("plain")},
{name: "pkce/S256", dst: new(PKCEMethod), in: "S256", want: PKCEMethod("S256")},
{name: "pkce/empty", dst: new(PKCEMethod), in: "", want: PKCEMethod("")},
{name: "pkce/case-sensitive", dst: new(PKCEMethod), in: "s256", wantErr: "has an unsupported value"},
{name: "policy/file", dst: new(PolicyMode), in: "file", want: PolicyMode("file")},
{name: "policy/database", dst: new(PolicyMode), in: "database", want: PolicyMode("database")},
{name: "policy/empty", dst: new(PolicyMode), in: "", want: PolicyMode("")},
{name: "policy/unknown", dst: new(PolicyMode), in: "db", wantErr: "has an unsupported value", wantSee: "https://headscale.net/stable/ref/policy/"},
{name: "allocation/sequential", dst: new(IPAllocationStrategy), in: "sequential", want: IPAllocationStrategySequential},
{name: "allocation/random", dst: new(IPAllocationStrategy), in: "random", want: IPAllocationStrategyRandom},
{name: "allocation/empty", dst: new(IPAllocationStrategy), in: "", want: IPAllocationStrategy("")},
{name: "allocation/unknown", dst: new(IPAllocationStrategy), in: "linear", wantErr: "has an unsupported value"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.dst.UnmarshalText([]byte(tt.in))
enum, isEnum := tt.dst.(interface{ Allowed() []string })
if isEnum {
require.NotContains(t, enum.Allowed(), "", "Allowed lists only named values")
}
if tt.wantErr == "" {
require.NoError(t, err)
got := reflect.ValueOf(tt.dst).Elem().Interface()
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("decoded value mismatch (-want +got):\n%s", diff)
}
return
}
require.ErrorIs(t, err, ErrConfig)
var ce *ConfigError
require.ErrorAs(t, err, &ce)
require.Contains(t, ce.Reason, tt.wantErr)
require.Empty(t, ce.Current, "the loader adds the key")
require.Equal(t, tt.wantSee, ce.See)
if isEnum {
require.Equal(t, enum.Allowed(), ce.Allowed)
}
})
}
}
func TestFileModeMarshalText(t *testing.T) {
tests := []struct {
mode FileMode
want string
}{
{0, "0000"},
{0o600, "0600"},
{0o770, "0770"},
{0o777, "0777"},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
b, err := tt.mode.MarshalText()
require.NoError(t, err)
require.Equal(t, tt.want, string(b))
var got FileMode
require.NoError(t, got.UnmarshalText(b))
require.Equal(t, tt.mode, got, "round trip")
})
}
}
func TestHTTPURLMarshalJSON(t *testing.T) {
tests := []struct {
url HTTPURL
want string
}{
{"", `""`},
{"https://derp.example/map", `"https://derp.example/map"`},
{"https://alice:pw-marker@derp.example/map", `"https://derp.example/map"`},
{"https://token-marker@derp.example/map?x=1", `"https://derp.example/map?x=1"`},
{"https://alice:pw-marker@derp.example/%zz", `"\u003credacted\u003e"`},
}
for _, tt := range tests {
t.Run(string(tt.url), func(t *testing.T) {
b, err := json.Marshal(tt.url)
require.NoError(t, err)
assert.Equal(t, tt.want, string(b))
})
}
}
// TestHTTPURLErrorHidesValue: a URL may hold credentials, so no error in
// the tree of its parse error, nor its JSON, holds the value.
func TestHTTPURLErrorHidesValue(t *testing.T) {
for _, in := range []string{
"https://alice:marker-pass?@h.example.com",
"https://marker-user:marker-pass@h.example.com/%zz",
"https://marker-user:marker-pass@h.example.com:port",
"ftp://marker-user:marker-pass@h.example.com",
} {
t.Run(in, func(t *testing.T) {
var u HTTPURL
err := u.UnmarshalText([]byte(in))
require.Error(t, err)
requireHidden(t, err, "marker")
})
}
}
// requireHidden fails when an error in err's tree, its text or its JSON,
// or the JSON of its ConfigErrors holds hidden.
func requireHidden(t *testing.T, err error, hidden string) {
t.Helper()
walkErrTree(err, func(e error) {
assert.NotContains(t, e.Error(), hidden)
b, jerr := json.Marshal(e)
require.NoError(t, jerr)
assert.NotContains(t, string(b), hidden)
})
b, jerr := json.Marshal(ConfigErrors(err))
require.NoError(t, jerr)
assert.NotContains(t, string(b), hidden)
}
func TestLogLevelZerolog(t *testing.T) {
tests := []struct {
level LogLevel
want zerolog.Level
}{
{"trace", zerolog.TraceLevel},
{"warn", zerolog.WarnLevel},
{"disabled", zerolog.Disabled},
{"", zerolog.InfoLevel},
{"verbose", zerolog.DebugLevel},
}
for _, tt := range tests {
t.Run(string(tt.level), func(t *testing.T) {
require.Equal(t, tt.want, tt.level.Zerolog())
})
}
}
func TestSecret(t *testing.T) {
dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "s"), []byte(" from-file\n"), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(dir, "empty"), []byte(" \n"), 0o600))
t.Setenv("D", dir)
missing := filepath.Join(dir, "missing")
empty := filepath.Join(dir, "empty")
tests := []struct {
name string
in Secret
want string
wantErr string
wantFile bool
wantRedacted string
}{
{name: "empty", in: "", want: "", wantRedacted: ""},
{name: "literal", in: "literal-value", want: "literal-value", wantRedacted: "<redacted>"},
{name: "file trimmed", in: Secret("file:" + filepath.Join(dir, "s")), want: "from-file", wantFile: true, wantRedacted: "file:" + filepath.Join(dir, "s")},
{name: "file path expanded", in: "file:$D/s", want: "from-file", wantFile: true, wantRedacted: "file:$D/s"},
{name: "missing file names the path", in: Secret("file:" + missing), wantErr: missing, wantFile: true, wantRedacted: "file:" + missing},
{name: "empty file names the path", in: Secret("file:" + empty), wantErr: empty + ": file is empty", wantFile: true, wantRedacted: "file:" + empty},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.wantFile, tt.in.IsFile())
require.Equal(t, tt.wantRedacted, tt.in.Redacted())
require.Equal(t, tt.wantRedacted, fmt.Sprint(tt.in), "String redacts")
got, err := tt.in.Resolve()
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
return
}
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
_, err := Secret("file:" + missing).Resolve()
require.ErrorIs(t, err, os.ErrNotExist)
}
+7 -7
View File
@@ -3,14 +3,14 @@ package types
import "time"
const (
HTTPTimeout = 30 * time.Second
HTTPShutdownTimeout = 3 * time.Second
TLSALPN01ChallengeType = "TLS-ALPN-01"
HTTP01ChallengeType = "HTTP-01"
DNS01ChallengeType = "DNS-01"
HTTPTimeout = 30 * time.Second
HTTPShutdownTimeout = 3 * time.Second
TLSALPN01ChallengeType ACMEChallenge = "TLS-ALPN-01"
HTTP01ChallengeType ACMEChallenge = "HTTP-01"
DNS01ChallengeType ACMEChallenge = "DNS-01"
JSONLogFormat = "json"
TextLogFormat = "text"
JSONLogFormat LogFormat = "json"
TextLogFormat LogFormat = "text"
MaxHostnameLength = 255
)
+1 -1
View File
@@ -116,7 +116,7 @@ func validateListenerCollisions(v *configValidator) {
addr: acmeAddr,
active: acmeAddr != "" &&
src.String("tls.acme.hostname") != "" &&
src.String("tls.acme.challenge_type") == HTTP01ChallengeType,
ACMEChallenge(src.String("tls.acme.challenge_type")) == HTTP01ChallengeType,
},
}
+2 -2
View File
@@ -920,7 +920,7 @@ func (nv NodeView) CertDomain(cfg *Config) (string, bool) {
return "", false
}
fqdn, err := nv.GetFQDN(cfg.BaseDomain)
fqdn, err := nv.GetFQDN(cfg.DNSConfig.BaseDomain)
if err != nil {
return "", false
}
@@ -1380,7 +1380,7 @@ func (nv NodeView) TailNode(
return nil, ErrInvalidNodeView
}
hostname, err := nv.GetFQDN(cfg.BaseDomain)
hostname, err := nv.GetFQDN(cfg.DNSConfig.BaseDomain)
if err != nil {
return nil, err
}
+4 -40
View File
@@ -3,7 +3,6 @@ package types
import (
"errors"
"fmt"
"os"
"strings"
hsdns "github.com/juanfont/headscale/hscontrol/dns"
@@ -11,27 +10,12 @@ import (
var errSecretEmpty = errors.New("file is empty")
// ResolveSecret returns value, or the trimmed contents of the file named
// by "file:<path>" (Tailscale's form), which keeps the secret out of the
// config and the environment. Variables in the path are expanded so a
// systemd credential can be named by ${CREDENTIALS_DIRECTORY}.
// ResolveSecret resolves the secret setting key as [Secret.Resolve] does;
// errors name the key and say what to fix.
func ResolveSecret(key, value string) (string, error) {
path, ok := strings.CutPrefix(value, "file:")
if !ok {
return value, nil
}
path = os.ExpandEnv(path)
b, err := os.ReadFile(path)
secret, err := Secret(value).Resolve()
if err != nil {
return "", fmt.Errorf("%s: %w", key, err)
}
// An empty file is a credential that was not written, not a choice.
secret := strings.TrimSpace(string(b))
if secret == "" {
return "", fmt.Errorf("%s: %s: %w", key, path, errSecretEmpty)
return "", fmt.Errorf("%s: %w; ensure the file exists, is not empty and is readable by the user running headscale", key, err)
}
return secret, nil
@@ -46,23 +30,3 @@ func keepSecret(_, value string) (string, error) {
return value, nil
}
// configSecret resolves the secret setting key, recording a failure in v.
func configSecret(v *configValidator, resolve func(key, value string) (string, error), key, value string) string {
secret, err := resolve(key, value)
if errors.Is(err, hsdns.ErrSecretUnread) {
return value
}
if err != nil {
v.Add(&ConfigError{
Reason: key + " cannot be read",
Current: []KV{{key, value}},
Detail: err.Error(),
Hint: "ensure the file exists, is not empty and is readable by the user running headscale",
Cause: err,
})
}
return secret
}
+1
View File
@@ -35,6 +35,7 @@ func TestResolveSecret(t *testing.T) {
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
require.ErrorContains(t, err, "oidc.client_secret: ")
require.ErrorContains(t, err, "readable by the user running headscale")
return
}
+1 -1
View File
@@ -74,7 +74,7 @@ func TestSSHTestsRejectFailingPolicy(t *testing.T) {
[]tsic.Option{},
hsic.WithTestName("cli-policyset-sshtests"),
hsic.WithConfigEnv(map[string]string{
"HEADSCALE_POLICY_MODE": types.PolicyModeDB,
"HEADSCALE_POLICY_MODE": string(types.PolicyModeDB),
}),
)
require.NoError(t, err)
+4 -4
View File
@@ -1565,7 +1565,7 @@ func (o *scenarioOIDC) Issuer() string {
panic("OIDC has not been created")
}
return o.cfg.Issuer
return string(o.cfg.Issuer)
}
func (o *scenarioOIDC) ClientSecret() string {
@@ -1573,7 +1573,7 @@ func (o *scenarioOIDC) ClientSecret() string {
panic("OIDC has not been created")
}
return o.cfg.ClientSecret
return string(o.cfg.ClientSecret)
}
func (o *scenarioOIDC) ClientID() string {
@@ -1708,10 +1708,10 @@ func (s *Scenario) runMockOIDC(accessTTL time.Duration, users []mockoidc.MockUse
}
s.mockOIDC.cfg = &types.OIDCConfig{
Issuer: fmt.Sprintf(
Issuer: types.HTTPURL(fmt.Sprintf(
"http://%s/oidc",
hostEndpoint,
),
)),
ClientID: "superclient",
ClientSecret: "supersecret",
OnlyStartIfOIDCIsAvailable: true,