From b54570d375f39c26205fb1216e075998ab4f7cd8 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Wed, 4 Nov 2026 21:15:27 +0000 Subject: [PATCH] state: serve the configured DERP map with the policy derpMap applied Applied as SaaS applies it; a derpMap-only policy write reaches clients. Updates #2918 --- hscontrol/derp/derp.go | 30 ++++++ hscontrol/derp/derp_compat_test.go | 118 +++++++++++++++++++++++ hscontrol/servertest/derp_policy_test.go | 62 ++++++++++++ hscontrol/state/state.go | 71 +++++++++++--- 4 files changed, 266 insertions(+), 15 deletions(-) create mode 100644 hscontrol/derp/derp_compat_test.go create mode 100644 hscontrol/servertest/derp_policy_test.go diff --git a/hscontrol/derp/derp.go b/hscontrol/derp/derp.go index dc9b54db7..80f7a19fa 100644 --- a/hscontrol/derp/derp.go +++ b/hscontrol/derp/derp.go @@ -131,6 +131,36 @@ func mergeDERPMaps(derpMaps []*tailcfg.DERPMap) *tailcfg.DERPMap { return &result } +// ApplyPolicy returns base with a policy derpMap applied the way Tailscale +// SaaS applies one: OmitDefaultRegions drops every base region, a null region +// removes one, and the other regions are added. The policy's HomeParams are +// dropped, as SaaS never sends them; base's are kept. +func ApplyPolicy(base, pol *tailcfg.DERPMap) *tailcfg.DERPMap { + if base == nil || pol == nil { + return base + } + + out := &tailcfg.DERPMap{ + HomeParams: base.HomeParams, + Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{}, + } + + if !pol.OmitDefaultRegions { + maps.Copy(out.Regions, base.Regions) + } + + for id, region := range pol.Regions { + if region == nil { + delete(out.Regions, id) + continue + } + + out.Regions[id] = region.Clone() + } + + return out +} + func GetDERPMap(cfg types.DERPConfig) (*tailcfg.DERPMap, error) { var derpMaps []*tailcfg.DERPMap if cfg.DERPMap != nil { diff --git a/hscontrol/derp/derp_compat_test.go b/hscontrol/derp/derp_compat_test.go new file mode 100644 index 000000000..5c6e1b594 --- /dev/null +++ b/hscontrol/derp/derp_compat_test.go @@ -0,0 +1,118 @@ +// This file replays Tailscale SaaS captures of a tailnet policy derpMap +// against [ApplyPolicy] and the derp.paths loader. +// +// Test data source: ../policy/v2/testdata/nodeattrs_results/nodeattrs-derpmap-*.hujson +// Source format: github.com/juanfont/headscale/hscontrol/types/testcapture + +package derp + +import ( + "encoding/json" + "net/http" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/juanfont/headscale/hscontrol/types/testcapture" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tailscale/hujson" + "tailscale.com/tailcfg" +) + +const derpCaptures = "../policy/v2/testdata/nodeattrs_results/" + +// policyDERPMap returns the derpMap of a capture's policy as written. +func policyDERPMap(t *testing.T, c *testcapture.Capture) json.RawMessage { + t.Helper() + + policy, err := hujson.Standardize([]byte(c.Input.FullPolicy)) + require.NoError(t, err) + + var p struct { + DERPMap json.RawMessage `json:"derpMap"` + } + + require.NoError(t, json.Unmarshal(policy, &p)) + + return p.DERPMap +} + +// TestApplyPolicySaaSCompat checks that the map a SaaS client got is SaaS's +// default map with the policy's derpMap applied as [ApplyPolicy] applies it, +// and that no SaaS client ever got HomeParams. +func TestApplyPolicySaaSCompat(t *testing.T) { + // Its policy sets no regions, so its clients got the default map. + plain, err := testcapture.Read(derpCaptures + "nodeattrs-derpmap-homeparams-regionscore.hujson") + require.NoError(t, err) + + var base *tailcfg.DERPMap + for _, node := range plain.Captures { + base = node.Netmap.DERPMap + } + + require.NotEmpty(t, base.Regions) + + files, err := filepath.Glob(derpCaptures + "nodeattrs-derpmap-*.hujson") + require.NoError(t, err) + + for _, file := range files { + c, err := testcapture.Read(file) + require.NoError(t, err) + + t.Run(c.TestID, func(t *testing.T) { + var pol *tailcfg.DERPMap + if c.Input.APIResponseCode == http.StatusOK { + require.NoError(t, json.Unmarshal(policyDERPMap(t, c), &pol)) + } + + for name, node := range c.Captures { + if node.Netmap == nil || node.Netmap.DERPMap == nil { + continue + } + + got := node.Netmap.DERPMap + assert.Nil(t, got.HomeParams, "SaaS sent HomeParams to %s", name) + + // A rejected policy leaves the default map in place. + if diff := cmp.Diff(ApplyPolicy(base, pol).Regions, got.Regions); diff != "" { + t.Errorf("%s: regions mismatch (-headscale +SaaS):\n%s", name, diff) + } + } + }) + } +} + +// TestLoadDERPMapFromPathSaaSDecodeErrors checks a derp.paths file holding a +// derpMap SaaS cannot decode (wrong type, unknown key) fails to load too. +// SaaS's region rules do not apply: a file is the whole map, not an overlay. +func TestLoadDERPMapFromPathSaaSDecodeErrors(t *testing.T) { + files, err := filepath.Glob(derpCaptures + "nodeattrs-derpmap-*.hujson") + require.NoError(t, err) + + var checked int + + for _, file := range files { + c, err := testcapture.Read(file) + require.NoError(t, err) + + body := c.Input.APIResponseBody + if body == nil || !strings.HasPrefix(body.Message, "json:") { + continue + } + + checked++ + + t.Run(c.TestID, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "derp.json") + require.NoError(t, os.WriteFile(path, policyDERPMap(t, c), 0o600)) + + _, err := loadDERPMapFromPath(path) + require.Error(t, err, "SaaS: %s", body.Message) + }) + } + + require.NotZero(t, checked) +} diff --git a/hscontrol/servertest/derp_policy_test.go b/hscontrol/servertest/derp_policy_test.go new file mode 100644 index 000000000..1518dc52c --- /dev/null +++ b/hscontrol/servertest/derp_policy_test.go @@ -0,0 +1,62 @@ +package servertest_test + +import ( + "maps" + "slices" + "testing" + "time" + + "github.com/juanfont/headscale/hscontrol/servertest" + "github.com/juanfont/headscale/hscontrol/types/change" + "github.com/stretchr/testify/require" + "tailscale.com/tailcfg" + "tailscale.com/types/netmap" +) + +// TestPolicyDERPMapReachesClient pins that a policy derpMap changes the map +// clients get, on a policy write that changes nothing else and on a DERP +// map refresh, as on SaaS (nodeattrs-derpmap-* captures). +func TestPolicyDERPMapReachesClient(t *testing.T) { + srv := servertest.NewServer(t) + client := servertest.NewClient(t, srv, "node", servertest.WithUser(srv.CreateUser(t, "derp"))) + + waitRegions := func(desc string, want ...tailcfg.DERPRegionID) { + t.Helper() + + client.WaitForCondition(t, desc, 10*time.Second, func(nm *netmap.NetworkMap) bool { + return nm.DERPMap != nil && + slices.Equal(slices.Sorted(maps.Keys(nm.DERPMap.Regions)), want) + }) + } + + setPolicy := func(pol string) { + t.Helper() + + _, changes, err := srv.State().UpdatePolicy([]byte(pol)) + require.NoError(t, err) + srv.App.Change(changes...) + } + + const region901 = `"901": {"RegionID": 901, "RegionCode": "custom", "Nodes": [ + {"Name": "901a", "RegionID": 901, "HostName": "127.0.0.1", "DERPPort": -1}]}` + + waitRegions("configured map", 900) + + setPolicy(`{"derpMap": {"Regions": {` + region901 + `}}}`) + waitRegions("policy region added", 900, 901) + + setPolicy(`{"derpMap": {"OmitDefaultRegions": true, "Regions": {` + region901 + `}}}`) + waitRegions("configured regions omitted", 901) + + // A refresh of the configured map keeps the policy applied. + srv.State().SetDERPMap(&tailcfg.DERPMap{Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{ + 902: {RegionID: 902, Nodes: []*tailcfg.DERPNode{{Name: "902a", RegionID: 902, DERPPort: -1}}}, + }}) + srv.App.Change(change.DERPMap()) + + setPolicy(`{"derpMap": {"Regions": {` + region901 + `}}}`) + waitRegions("refreshed map with policy region", 901, 902) + + setPolicy(`{}`) + waitRegions("policy derpMap removed", 902) +} diff --git a/hscontrol/state/state.go b/hscontrol/state/state.go index 4484a439e..12cbebe56 100644 --- a/hscontrol/state/state.go +++ b/hscontrol/state/state.go @@ -27,6 +27,7 @@ import ( "github.com/hashicorp/golang-lru/v2/expirable" hsdb "github.com/juanfont/headscale/hscontrol/db" + "github.com/juanfont/headscale/hscontrol/derp" "github.com/juanfont/headscale/hscontrol/policy" "github.com/juanfont/headscale/hscontrol/policy/matcher" "github.com/juanfont/headscale/hscontrol/types" @@ -233,8 +234,11 @@ type State struct { db *hsdb.HSDatabase // ipAlloc manages IP address allocation for nodes ipAlloc *hsdb.IPAllocator - // derpMap contains the current DERP relay configuration - derpMap atomic.Pointer[tailcfg.DERPMap] + // derpMap is configDERPMap with the policy's derpMap applied: the DERP + // map every node gets. derpMu orders its writers. + derpMap atomic.Pointer[tailcfg.DERPMap] + derpMu sync.Mutex + configDERPMap *tailcfg.DERPMap // polMan handles policy evaluation and management polMan policy.PolicyManager @@ -530,9 +534,30 @@ func (s *State) Close() error { return nil } -// SetDERPMap updates the DERP relay configuration. +// SetDERPMap sets the DERP map built from the configuration; nodes get it +// with the policy's derpMap applied. func (s *State) SetDERPMap(dm *tailcfg.DERPMap) { - s.derpMap.Store(dm) + s.derpMu.Lock() + defer s.derpMu.Unlock() + + s.configDERPMap = dm + s.derpMap.Store(derp.ApplyPolicy(dm, s.polMan.DERPMap())) +} + +// applyPolicyDERPMap applies the current policy's derpMap to the configured +// DERP map again and reports whether the map nodes get changed. +func (s *State) applyPolicyDERPMap() bool { + s.derpMu.Lock() + defer s.derpMu.Unlock() + + next := derp.ApplyPolicy(s.configDERPMap, s.polMan.DERPMap()) + if reflect.DeepEqual(next, s.derpMap.Load()) { + return false + } + + s.derpMap.Store(next) + + return true } // DERPMap returns the current DERP relay configuration for peer-to-peer connectivity. @@ -549,22 +574,28 @@ func (s *State) ReloadPolicy() ([]change.Change, error) { s.policyMu.Lock() defer s.policyMu.Unlock() - _, err := s.reloadPolicyLocked() + _, derpChanged, err := s.reloadPolicyLocked() if err != nil { return nil, err } // Nodes whose CapMap shifted get their self refresh from // [State.DrainSelfRefreshes] when these changes are dispatched. - return []change.Change{change.PolicyChange()}, nil + changes := []change.Change{change.PolicyChange()} + if derpChanged { + changes = append(changes, change.DERPMap()) + } + + return changes, nil } // reloadPolicyLocked is [State.ReloadPolicy] for callers holding policyMu. It -// reports whether the policy changed. -func (s *State) reloadPolicyLocked() (bool, error) { +// reports whether the policy changed, and apart from that whether the DERP +// map nodes get did: a derpMap edit changes no filter. +func (s *State) reloadPolicyLocked() (bool, bool, error) { pol, err := hsdb.PolicyBytes(s.db.DB, s.cfg) if err != nil { - return false, fmt.Errorf("loading policy: %w", err) + return false, false, fmt.Errorf("loading policy: %w", err) } // nil means no policy is configured: nothing to install. @@ -572,10 +603,14 @@ func (s *State) reloadPolicyLocked() (bool, error) { if pol != nil { policyChanged, err = s.polMan.SetPolicy(pol) if err != nil { - return false, fmt.Errorf("setting policy: %w", err) + return false, false, fmt.Errorf("setting policy: %w", err) } } + // Checked even when the policy is unchanged: [State.SetPolicy] may have + // installed it already. + derpChanged := s.applyPolicyDERPMap() + // Rebuild peer maps after policy changes because the peersFunc in [NodeStore] // uses the [policy.PolicyManager]'s filters. Without this, nodes won't see // newly allowed peers until a node is added/removed, causing autogroup:self @@ -596,7 +631,7 @@ func (s *State) reloadPolicyLocked() (bool, error) { log.Info().Msg("Policy reload completed with changes") } - return policyChanged, nil + return policyChanged, derpChanged, nil } // CreateUser creates a new user and updates the policy manager. @@ -1908,17 +1943,23 @@ func (s *State) UpdatePolicy(pol []byte) (*types.Policy, []change.Change, error) return nil, nil, fmt.Errorf("storing policy: %w", err) } - changed, err := s.reloadPolicyLocked() + changed, derpChanged, err := s.reloadPolicyLocked() if err != nil { return stored, nil, err } + var changes []change.Change + // As on SaaS, a write that changes nothing sends clients nothing. - if !changed { - return stored, nil, nil + if changed { + changes = append(changes, change.PolicyChange()) } - return stored, []change.Change{change.PolicyChange()}, nil + if derpChanged { + changes = append(changes, change.DERPMap()) + } + + return stored, changes, nil } // GetNodePrimaryRoutes returns the primary routes for a node.