From caecddd2499a1612983d92d952c499ec2fec694c Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sun, 8 Nov 2026 12:32:08 +0000 Subject: [PATCH] config: keep every default in Default The DNS-01 challenge resolvers, dns.challenge.resolvers, now default to Quad9 (9.9.9.9:53, 149.112.112.112:53) instead of Cloudflare and Google. Updates #1926 --- hscontrol/config/default.go | 7 +++- hscontrol/config/default_test.go | 16 ++++++++ hscontrol/config/load.go | 17 +++++++- hscontrol/config/load_test.go | 41 ++++++++++++++++--- hscontrol/config/marshal_test.go | 10 +++++ .../testdata/compat/env-empty.golden.json | 8 ++-- .../compat/env-extra-records.golden.json | 8 ++-- .../compat/env-legacy-ignored.golden.json | 8 ++-- .../compat/env-legacy-removed.golden.json | 8 ++-- .../compat/env-lists-maps.golden.json | 8 ++-- .../testdata/compat/env-map-case.golden.json | 8 ++-- .../testdata/compat/env-only.golden.json | 8 ++-- .../compat/env-precedence.golden.json | 8 ++-- .../testdata/compat/env-secrets.golden.json | 8 ++-- .../compat/env-shapes-typed.golden.json | 8 ++-- .../testdata/compat/example-main.golden.json | 8 ++-- .../compat/hsic-default-strict.golden.json | 8 ++-- .../compat/hsic-postgres-strict.golden.json | 8 ++-- .../compat/nixos-example-strict.golden.json | 8 ++-- .../testdata/compat/nixos-example.golden.json | 8 ++-- ...-base-domain-not-in-server-url.golden.json | 8 ++-- .../testdata-dns-override-true.golden.json | 8 ++-- ...a-policy-path-is-loaded-strict.golden.json | 8 ++-- hscontrol/state/debug.go | 2 +- hscontrol/state/debug_test.go | 31 ++++++++++---- hscontrol/state/state.go | 38 ++++++----------- hscontrol/types/config.go | 8 ++-- hscontrol/types/config_test.go | 2 +- 28 files changed, 198 insertions(+), 118 deletions(-) diff --git a/hscontrol/config/default.go b/hscontrol/config/default.go index dbd1fd113..5de72acd0 100644 --- a/hscontrol/config/default.go +++ b/hscontrol/config/default.go @@ -48,7 +48,10 @@ func Default() types.Config { OverrideLocalDNS: true, Nameservers: types.Nameservers{Global: []string{}, Split: map[string][]string{}}, SearchDomains: []string{}, - Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, + Challenge: types.DNSChallengeConfig{ + PropagationTimeout: 2 * time.Minute, + Resolvers: types.DefaultChallengeResolvers(), + }, }, TLS: types.TLSConfig{ACME: types.ACMEConfig{ ChallengeType: types.HTTP01ChallengeType, @@ -68,6 +71,8 @@ func Default() types.Config { NodeMapSessionBufferedChanSize: 30, NodeStoreBatchSize: 100, NodeStoreBatchTimeout: 500 * time.Millisecond, + RegisterCacheExpiration: 15 * time.Minute, + RegisterCacheMaxEntries: 1024, }, } } diff --git a/hscontrol/config/default_test.go b/hscontrol/config/default_test.go index 11acd2f25..5b3d49ae1 100644 --- a/hscontrol/config/default_test.go +++ b/hscontrol/config/default_test.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "testing" + "time" "github.com/juanfont/headscale/hscontrol/types" "github.com/stretchr/testify/assert" @@ -28,6 +29,21 @@ func validConfig() types.Config { return cfg } +// TestDefaultHoldsDefaults: the defaults consumers fall back to for a zero +// value are Default's, so the loaded configuration shows them. +func TestDefaultHoldsDefaults(t *testing.T) { + d := Default() + assert.Equal(t, 15*time.Minute, d.Tuning.RegisterCacheExpiration) + assert.Equal(t, 1024, d.Tuning.RegisterCacheMaxEntries) + assert.Equal(t, 100, d.Tuning.NodeStoreBatchSize) + assert.Equal(t, 500*time.Millisecond, d.Tuning.NodeStoreBatchTimeout) + assert.Zero(t, d.Tuning.BatcherWorkers, "auto: its count depends on the host") + require.Equal(t, []string{"9.9.9.9:53", "149.112.112.112:53"}, d.DNSConfig.Challenge.Resolvers) + + d.DNSConfig.Challenge.Resolvers[0] = "changed" + assert.Equal(t, "9.9.9.9:53", Default().DNSConfig.Challenge.Resolvers[0], "each Default is its own copy") +} + // TestValidateHintsNameDefaults: the defaults Validate's hints quote, which // types cannot read from Default, are Default's. func TestValidateHintsNameDefaults(t *testing.T) { diff --git a/hscontrol/config/load.go b/hscontrol/config/load.go index a75a86535..aa2c1fb3a 100644 --- a/hscontrol/config/load.go +++ b/hscontrol/config/load.go @@ -1404,7 +1404,7 @@ func plainValue(v reflect.Value, redact bool) any { switch x := v.Interface().(type) { case time.Duration: - return x.String() + return formatDuration(x) case types.Secret: if redact { return x.Redacted() @@ -1480,6 +1480,21 @@ func plainValue(v reflect.Value, redact bool) any { } } +// formatDuration is d as Go writes it without trailing zero units: 3h, +// not 3h0m0s. +func formatDuration(d time.Duration) string { + s := d.String() + + if t, ok := strings.CutSuffix(s, "m0s"); ok { + s = t + "m" + if t, ok := strings.CutSuffix(t, "h0"); ok { + s = t + "h" + } + } + + return s +} + func redactProvider(settings map[string]any) map[string]any { out := make(map[string]any, len(settings)) for k, v := range settings { diff --git a/hscontrol/config/load_test.go b/hscontrol/config/load_test.go index 40a7ffd5c..05db8953c 100644 --- a/hscontrol/config/load_test.go +++ b/hscontrol/config/load_test.go @@ -912,7 +912,7 @@ func TestPlainMap(t *testing.T) { assert.Contains(t, prefixes, "v6") node, _ := plain["node"].(map[string]any) - assert.Equal(t, "1h30m0s", node["expiry"]) + assert.Equal(t, "1h30m", node["expiry"]) derp, _ := plain["derp"].(map[string]any) assert.Equal(t, []any{ @@ -969,6 +969,37 @@ func TestPlainMap(t *testing.T) { assert.NotContains(t, plainProvider, "nothing", "a typed nil is left out") } +// TestFormatDuration: a duration prints without trailing zero units and +// parses back to itself. +func TestFormatDuration(t *testing.T) { + tests := []struct { + d time.Duration + want string + }{ + {0, "0s"}, + {3 * time.Hour, "3h"}, + {3*time.Hour + 30*time.Minute, "3h30m"}, + {time.Hour + 30*time.Second, "1h0m30s"}, + {2 * time.Minute, "2m"}, + {90 * time.Second, "1m30s"}, + {800 * time.Millisecond, "800ms"}, + {1500 * time.Millisecond, "1.5s"}, + {168 * time.Hour, "168h"}, + {-5 * time.Second, "-5s"}, + } + + for _, tt := range tests { + t.Run(tt.want, func(t *testing.T) { + got := formatDuration(tt.d) + assert.Equal(t, tt.want, got) + + back, err := time.ParseDuration(got) + require.NoError(t, err) + assert.Equal(t, tt.d, back) + }) + } +} + // TestPlainMapLargeIntegers: JSON parsers read numbers as float64, so an // integer past 2^53 is written as text, which the integer hook takes back. func TestPlainMapLargeIntegers(t *testing.T) { @@ -1096,7 +1127,7 @@ func TestReadConfig(t *testing.T) { {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, }, SearchDomains: []string{"test.com", "bar.com"}, - Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, + Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute, Resolvers: types.DefaultChallengeResolvers()}, }, }, { @@ -1153,7 +1184,7 @@ func TestReadConfig(t *testing.T) { {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, }, SearchDomains: []string{"test.com", "bar.com"}, - Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, + Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute, Resolvers: types.DefaultChallengeResolvers()}, }, }, { @@ -1337,7 +1368,7 @@ func TestReadConfigFromEnv(t *testing.T) { // {Name: "prometheus.myvpn.example.com", Type: "A", Value: "100.64.0.4"}, // }, SearchDomains: []string{"test.com", "bar.com"}, - Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute}, + Challenge: types.DNSChallengeConfig{PropagationTimeout: 2 * time.Minute, Resolvers: types.DefaultChallengeResolvers()}, }, }, } @@ -2212,7 +2243,7 @@ dns: // CertificateZone follows base_domain even when unused, so it is never empty. wantZone: "ts.example.com", wantNoProv: true, - wantResolvers: []string{"1.1.1.1:53", "8.8.8.8:53"}, + wantResolvers: []string{"9.9.9.9:53", "149.112.112.112:53"}, }, { name: "resolvers default to port 53", diff --git a/hscontrol/config/marshal_test.go b/hscontrol/config/marshal_test.go index b90850e98..fd35800f1 100644 --- a/hscontrol/config/marshal_test.go +++ b/hscontrol/config/marshal_test.go @@ -238,6 +238,16 @@ func TestMarshalNullablePrefix(t *testing.T) { } } +// TestMarshalDurations: durations print without trailing zero units. +func TestMarshalDurations(t *testing.T) { + cfg := Default() + + out, err := Marshal(&cfg, ".yaml") + require.NoError(t, err) + assert.Contains(t, string(out), "update_frequency: 3h\n") + assert.Contains(t, string(out), "revoked_retention: 168h\n") +} + // fakeProvider is a Go-only DNS provider. type fakeProvider struct{ Token string } diff --git a/hscontrol/config/testdata/compat/env-empty.golden.json b/hscontrol/config/testdata/compat/env-empty.golden.json index a3205c8da..ee3d0dd9c 100644 --- a/hscontrol/config/testdata/compat/env-empty.golden.json +++ b/hscontrol/config/testdata/compat/env-empty.golden.json @@ -117,8 +117,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -166,8 +166,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/env-extra-records.golden.json b/hscontrol/config/testdata/compat/env-extra-records.golden.json index c9ef4d8d5..4df806727 100644 --- a/hscontrol/config/testdata/compat/env-extra-records.golden.json +++ b/hscontrol/config/testdata/compat/env-extra-records.golden.json @@ -117,8 +117,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -166,8 +166,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/env-legacy-ignored.golden.json b/hscontrol/config/testdata/compat/env-legacy-ignored.golden.json index db13b6316..1f9cc0b55 100644 --- a/hscontrol/config/testdata/compat/env-legacy-ignored.golden.json +++ b/hscontrol/config/testdata/compat/env-legacy-ignored.golden.json @@ -103,8 +103,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -152,8 +152,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/env-legacy-removed.golden.json b/hscontrol/config/testdata/compat/env-legacy-removed.golden.json index cbe032129..e2a7a820e 100644 --- a/hscontrol/config/testdata/compat/env-legacy-removed.golden.json +++ b/hscontrol/config/testdata/compat/env-legacy-removed.golden.json @@ -103,8 +103,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -152,8 +152,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/env-lists-maps.golden.json b/hscontrol/config/testdata/compat/env-lists-maps.golden.json index 2da095dad..edbe2867e 100644 --- a/hscontrol/config/testdata/compat/env-lists-maps.golden.json +++ b/hscontrol/config/testdata/compat/env-lists-maps.golden.json @@ -133,8 +133,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -184,8 +184,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/env-map-case.golden.json b/hscontrol/config/testdata/compat/env-map-case.golden.json index 3d31ef620..28d66f942 100644 --- a/hscontrol/config/testdata/compat/env-map-case.golden.json +++ b/hscontrol/config/testdata/compat/env-map-case.golden.json @@ -115,8 +115,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "cloudflare" }, @@ -167,8 +167,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/env-only.golden.json b/hscontrol/config/testdata/compat/env-only.golden.json index 159b0738e..04f663eac 100644 --- a/hscontrol/config/testdata/compat/env-only.golden.json +++ b/hscontrol/config/testdata/compat/env-only.golden.json @@ -103,8 +103,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -152,8 +152,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/env-precedence.golden.json b/hscontrol/config/testdata/compat/env-precedence.golden.json index cd0c72cf9..cf502fc24 100644 --- a/hscontrol/config/testdata/compat/env-precedence.golden.json +++ b/hscontrol/config/testdata/compat/env-precedence.golden.json @@ -120,8 +120,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -171,8 +171,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/env-secrets.golden.json b/hscontrol/config/testdata/compat/env-secrets.golden.json index 2e9dbcd79..33dea04d2 100644 --- a/hscontrol/config/testdata/compat/env-secrets.golden.json +++ b/hscontrol/config/testdata/compat/env-secrets.golden.json @@ -103,8 +103,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -152,8 +152,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/env-shapes-typed.golden.json b/hscontrol/config/testdata/compat/env-shapes-typed.golden.json index 797043303..c419cb09d 100644 --- a/hscontrol/config/testdata/compat/env-shapes-typed.golden.json +++ b/hscontrol/config/testdata/compat/env-shapes-typed.golden.json @@ -159,8 +159,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -216,8 +216,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/example-main.golden.json b/hscontrol/config/testdata/compat/example-main.golden.json index a3042aed3..d373657a0 100644 --- a/hscontrol/config/testdata/compat/example-main.golden.json +++ b/hscontrol/config/testdata/compat/example-main.golden.json @@ -141,8 +141,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -190,8 +190,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/hsic-default-strict.golden.json b/hscontrol/config/testdata/compat/hsic-default-strict.golden.json index 1b2b0a3be..bbb973305 100644 --- a/hscontrol/config/testdata/compat/hsic-default-strict.golden.json +++ b/hscontrol/config/testdata/compat/hsic-default-strict.golden.json @@ -119,8 +119,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -168,8 +168,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/hsic-postgres-strict.golden.json b/hscontrol/config/testdata/compat/hsic-postgres-strict.golden.json index 87a7bcd53..b15e747f6 100644 --- a/hscontrol/config/testdata/compat/hsic-postgres-strict.golden.json +++ b/hscontrol/config/testdata/compat/hsic-postgres-strict.golden.json @@ -119,8 +119,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -168,8 +168,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/nixos-example-strict.golden.json b/hscontrol/config/testdata/compat/nixos-example-strict.golden.json index 1b2b58578..2bcb469c1 100644 --- a/hscontrol/config/testdata/compat/nixos-example-strict.golden.json +++ b/hscontrol/config/testdata/compat/nixos-example-strict.golden.json @@ -133,8 +133,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -182,8 +182,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/nixos-example.golden.json b/hscontrol/config/testdata/compat/nixos-example.golden.json index 1b2b58578..2bcb469c1 100644 --- a/hscontrol/config/testdata/compat/nixos-example.golden.json +++ b/hscontrol/config/testdata/compat/nixos-example.golden.json @@ -133,8 +133,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -182,8 +182,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/testdata-base-domain-not-in-server-url.golden.json b/hscontrol/config/testdata/compat/testdata-base-domain-not-in-server-url.golden.json index 01763f178..7ee8aa4c3 100644 --- a/hscontrol/config/testdata/compat/testdata-base-domain-not-in-server-url.golden.json +++ b/hscontrol/config/testdata/compat/testdata-base-domain-not-in-server-url.golden.json @@ -108,8 +108,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -157,8 +157,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/testdata-dns-override-true.golden.json b/hscontrol/config/testdata/compat/testdata-dns-override-true.golden.json index ab87572f3..ad3d0cd2a 100644 --- a/hscontrol/config/testdata/compat/testdata-dns-override-true.golden.json +++ b/hscontrol/config/testdata/compat/testdata-dns-override-true.golden.json @@ -119,8 +119,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -168,8 +168,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/config/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json b/hscontrol/config/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json index 029b2d43b..61c313665 100644 --- a/hscontrol/config/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json +++ b/hscontrol/config/testdata/compat/testdata-policy-path-is-loaded-strict.golden.json @@ -103,8 +103,8 @@ "DNSChallenge": { "PropagationTimeout": 120000000000, "Resolvers": [ - "1.1.1.1:53", - "8.8.8.8:53" + "9.9.9.9:53", + "149.112.112.112:53" ], "ProviderName": "" }, @@ -152,8 +152,8 @@ "BatchChangeDelay": 800000000, "NodeMapSessionBufferedChanSize": 30, "BatcherWorkers": 0, - "RegisterCacheExpiration": 0, - "RegisterCacheMaxEntries": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, "NodeStoreBatchSize": 100, "NodeStoreBatchTimeout": 500000000 } diff --git a/hscontrol/state/debug.go b/hscontrol/state/debug.go index c636e32f6..c0ef74348 100644 --- a/hscontrol/state/debug.go +++ b/hscontrol/state/debug.go @@ -180,7 +180,7 @@ func (s *State) DebugSSHPolicies() map[string]*tailcfg.SSHPolicy { func (s *State) DebugRegistrationCache() map[string]any { return map[string]any{ "type": "expirable-lru", - "expiration": registerCacheExpiration.String(), + "expiration": s.authCacheExpiration.String(), "max_entries": s.authCacheMaxEntries, "ssh_reserve": authCacheSSHReserve, "current_len": s.authCache.Len(), diff --git a/hscontrol/state/debug_test.go b/hscontrol/state/debug_test.go index 6fd528a87..2b340837e 100644 --- a/hscontrol/state/debug_test.go +++ b/hscontrol/state/debug_test.go @@ -2,8 +2,10 @@ package state import ( "testing" + "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNodeStoreDebugString(t *testing.T) { @@ -64,15 +66,28 @@ func TestNodeStoreDebugString(t *testing.T) { } } +// TestDebugRegistrationCache: the cache reports the expiration NewState +// chose, the default for zero. func TestDebugRegistrationCache(t *testing.T) { - // Create a minimal NodeStore for testing debug methods - store := NewNodeStore(nil, allowAllPeersFunc, TestBatchSize, TestBatchTimeout) + tests := []struct { + name string + set time.Duration + want time.Duration + }{ + {name: "configured", set: 20 * time.Minute, want: 20 * time.Minute}, + {name: "zero is the default", set: 0, want: 15 * time.Minute}, + } - debugStr := store.DebugString() + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg := persistTestConfig(t.TempDir() + "/headscale.db") + cfg.Tuning.RegisterCacheExpiration = tt.set - // Should contain basic debug information - assert.Contains(t, debugStr, "=== NodeStore Debug Information ===") - assert.Contains(t, debugStr, "Total Nodes: 0") - assert.Contains(t, debugStr, "Users with Nodes: 0") - assert.Contains(t, debugStr, "NodeKey Index: 0 entries") + s, err := NewState(cfg) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, s.Close()) }) + + assert.Equal(t, tt.want.String(), s.DebugRegistrationCache()["expiration"]) + }) + } } diff --git a/hscontrol/state/state.go b/hscontrol/state/state.go index 5d42aa611..20b98a8bb 100644 --- a/hscontrol/state/state.go +++ b/hscontrol/state/state.go @@ -26,6 +26,7 @@ import ( "time" "github.com/hashicorp/golang-lru/v2/expirable" + "github.com/juanfont/headscale/hscontrol/config" hsdb "github.com/juanfont/headscale/hscontrol/db" "github.com/juanfont/headscale/hscontrol/derp" "github.com/juanfont/headscale/hscontrol/policy" @@ -49,26 +50,9 @@ import ( "tailscale.com/util/truncate" ) -const ( - // registerCacheExpiration defines how long node registration entries remain in cache. - registerCacheExpiration = time.Minute * 15 - - // defaultRegisterCacheMaxEntries bounds pending authentication transactions. - // Admission refuses new work at capacity instead of invalidating approvals. - defaultRegisterCacheMaxEntries = 1024 - - // authCacheSSHReserve keeps registered nodes' SSH checks available when - // unauthenticated registrations have filled their allocation. - authCacheSSHReserve = 64 - - // defaultNodeStoreBatchSize is the default number of write operations to batch - // before rebuilding the in-memory node snapshot. - defaultNodeStoreBatchSize = 100 - - // defaultNodeStoreBatchTimeout is the default maximum time to wait before - // processing a partial batch of node operations. - defaultNodeStoreBatchTimeout = 500 * time.Millisecond -) +// authCacheSSHReserve keeps registered nodes' SSH checks available when +// unauthenticated registrations have filled their allocation. +const authCacheSSHReserve = 64 // ErrUnsupportedPolicyMode is returned for invalid policy modes. Valid modes are "file" and "db". var ErrUnsupportedPolicyMode = errors.New("unsupported policy mode") @@ -252,6 +236,7 @@ type State struct { authCache *expirable.LRU[types.AuthID, *types.AuthRequest] authCacheMu sync.Mutex authCacheMaxEntries int + authCacheExpiration time.Duration authCacheClosed bool authCacheRegularEntries atomic.Int64 @@ -357,9 +342,11 @@ func (s *State) lockRegistration(machineKey key.MachinePublic) func() { // NewState creates and initializes a new [State] instance, setting up the database, // IP allocator, DERP map, policy manager, and loading existing users and nodes. func NewState(cfg *types.Config) (*State, error) { - cacheExpiration := cmp.Or(cfg.Tuning.RegisterCacheExpiration, registerCacheExpiration) + defaults := config.Default().Tuning - cacheMaxEntries := defaultRegisterCacheMaxEntries + cacheExpiration := cmp.Or(cfg.Tuning.RegisterCacheExpiration, defaults.RegisterCacheExpiration) + + cacheMaxEntries := defaults.RegisterCacheMaxEntries if cfg.Tuning.RegisterCacheMaxEntries > 0 { cacheMaxEntries = cfg.Tuning.RegisterCacheMaxEntries } @@ -440,11 +427,9 @@ func NewState(cfg *types.Config) (*State, error) { return nil, fmt.Errorf("initializing policy manager: %w", err) } - // Apply defaults for [NodeStore] batch configuration if not set. - // This ensures tests that create Config directly (without config.Load) still work. - batchSize := cmp.Or(cfg.Tuning.NodeStoreBatchSize, defaultNodeStoreBatchSize) + batchSize := cmp.Or(cfg.Tuning.NodeStoreBatchSize, defaults.NodeStoreBatchSize) - batchTimeout := cmp.Or(cfg.Tuning.NodeStoreBatchTimeout, defaultNodeStoreBatchTimeout) + batchTimeout := cmp.Or(cfg.Tuning.NodeStoreBatchTimeout, defaults.NodeStoreBatchTimeout) nodeStore := NewNodeStore(nodes, policyPeersFunc(polMan), batchSize, batchTimeout) polMan.SetRouterSession(nodeStore.RouterSession) @@ -460,6 +445,7 @@ func NewState(cfg *types.Config) (*State, error) { ipAlloc: ipAlloc, polMan: polMan, authCacheMaxEntries: cacheMaxEntries, + authCacheExpiration: cacheExpiration, nodeStore: nodeStore, pings: newPingTracker(), diff --git a/hscontrol/types/config.go b/hscontrol/types/config.go index 57d83c3ff..9a8c89c72 100644 --- a/hscontrol/types/config.go +++ b/hscontrol/types/config.go @@ -38,11 +38,13 @@ var ( ErrInvalidAllocationStrategy = errors.New("invalid prefix allocation strategy") ) -// defaultChallengeResolvers find a zone's nameservers when +// DefaultChallengeResolvers find a zone's nameservers when // dns.challenge.resolvers is empty. The system resolver is not used: on a // host running MagicDNS it answers for base_domain itself instead of // asking the public zone. -var defaultChallengeResolvers = []string{"1.1.1.1:53", "8.8.8.8:53"} +func DefaultChallengeResolvers() []string { + return []string{"9.9.9.9:53", "149.112.112.112:53"} +} // IPAllocationStrategy is how new node IPs are picked. "" decodes without // error to the zero value (unset). @@ -378,7 +380,7 @@ func (c DNSChallengeConfig) NewProvider(secret func(key, value string) (string, // are set. Like dns.nameservers, a resolver may be a bare IP. func (c DNSChallengeConfig) ResolverAddrs() []string { if len(c.Resolvers) == 0 { - return slices.Clone(defaultChallengeResolvers) + return DefaultChallengeResolvers() } addrs := make([]string, len(c.Resolvers)) diff --git a/hscontrol/types/config_test.go b/hscontrol/types/config_test.go index 9cbe14690..423d193bf 100644 --- a/hscontrol/types/config_test.go +++ b/hscontrol/types/config_test.go @@ -475,7 +475,7 @@ func TestDerivedValues(t *testing.T) { {"workers set", Tuning{BatcherWorkers: 7}.BatcherWorkerCount(), 7}, {"zone unset", DNSConfig{BaseDomain: "ts.example.com"}.CertificateZone(), "ts.example.com"}, {"zone set", DNSConfig{BaseDomain: "ts.example.com", Certificates: CertificatesConfig{Zone: "example.com"}}.CertificateZone(), "example.com"}, - {"resolvers unset", resolvers().ResolverAddrs(), []string{"1.1.1.1:53", "8.8.8.8:53"}}, + {"resolvers unset", resolvers().ResolverAddrs(), []string{"9.9.9.9:53", "149.112.112.112:53"}}, {"resolver bare IPv4", resolvers("9.9.9.9").ResolverAddrs(), []string{"9.9.9.9:53"}}, {"resolver bare IPv6", resolvers("2620:fe::fe").ResolverAddrs(), []string{"[2620:fe::fe]:53"}}, {"resolver with port", resolvers("[::1]:5353").ResolverAddrs(), []string{"[::1]:5353"}},