util/syspolicy/source: notify sessions when a forced read finds a change

A Reader is shared by all effective policies that include its source.
A forced read through one of them updated the Reader's cached snapshot
and marked it up to date, but didn't notify the other sessions, so
their policies kept the old settings.

Since a89b32904, "tailscale syspolicy reload" reloads the caller's user
policy rather than the device policy used by LocalBackend. On Windows,
a registry change made without a Group Policy refresh was therefore
reported as configured by the CLI but never applied by the backend.

Updates tailscale/corp#51820

Signed-off-by: Nick Khyl <nickk@tailscale.com>
This commit is contained in:
Nick Khyl authored and Nick Khyl committed 2026-11-20 11:48:47 -08:00
1 parent b837008974
commit 262640ff4e
2 files changed
+69 -1

No files matched your search

@@ -10,6 +10,7 @@ import (
"strconv"
"sync"
"testing"
"testing/synctest"
"time"
"github.com/google/go-cmp/cmp"
@@ -701,6 +702,60 @@ drain:
}
}
// unobservableStore is a [source.Store] that doesn't report policy changes,
// like the Windows Registry when edited without a Group Policy refresh.
type unobservableStore struct{ source.Store }
func TestForcedReloadUpdatesOtherPolicies(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
settingA := setting.NewDefinition("TestSettingA", setting.DeviceSetting, setting.StringValue)
if err := setting.SetDefinitionsForTest(t, settingA); err != nil {
t.Fatalf("SetDefinitionsForTest failed: %v", err)
}
store := source.NewTestStoreOf[string](t)
if _, err := RegisterStoreForTest(t, "TestSource", setting.DeviceScope, unobservableStore{store}); err != nil {
t.Fatalf("Failed to register policy store: %v", err)
}
// Both the device and the user policies read from the same device store.
devicePolicy, err := policyForTest(t, setting.DeviceScope)
if err != nil {
t.Fatalf("Failed to get device policy: %v", err)
}
userPolicy, err := policyForTest(t, setting.UserScopeOf("test-user"))
if err != nil {
t.Fatalf("Failed to get user policy: %v", err)
}
policyChanged := make(chan policyclient.PolicyChange, 1)
t.Cleanup(devicePolicy.RegisterChangeCallback(func(pc policyclient.PolicyChange) { policyChanged <- pc }))
// Forcibly reloading the user policy must also update the device policy.
store.SetStrings(source.TestSettingOf(settingA.Key(), "A"))
if _, err := userPolicy.Reload(); err != nil {
t.Fatalf("Reload failed: %v", err)
}
if got, want := userPolicy.Get().Get(settingA.Key()), "A"; got != want {
t.Errorf("User policy: setting %q: got %q; want %q", settingA.Key(), got, want)
}
time.Sleep(policyReloadMaxDelay)
synctest.Wait()
select {
case pc := <-policyChanged:
if !pc.HasChanged(settingA.Key()) {
t.Errorf("Policy setting %q has not changed", settingA.Key())
}
default:
t.Fatal("Device policy change callback was not invoked")
}
if got, want := devicePolicy.Get().Get(settingA.Key()), "A"; got != want {
t.Errorf("Device policy: setting %q: got %q; want %q", settingA.Key(), got, want)
}
})
}
func TestClosePolicySource(t *testing.T) {
testSetting := setting.NewDefinition("TestSetting", setting.DeviceSetting, setting.StringValue)
if err := setting.SetDefinitionsForTest(t, testSetting); err != nil {
+14 -1
View File
@@ -130,7 +130,8 @@ func (r *Reader) reload(force bool) (*setting.Snapshot, error) {
// Return the last known policy instead of reading from the store.
return r.lastPolicy, nil
}
if r.upToDate && !force {
wasUpToDate := r.upToDate
if wasUpToDate && !force {
return r.lastPolicy, nil
}
@@ -179,6 +180,12 @@ func (r *Reader) reload(force bool) (*setting.Snapshot, error) {
newPolicy := setting.NewSnapshot(m, setting.SummaryWith(r.origin))
if r.lastPolicy == nil || !newPolicy.EqualItems(r.lastPolicy) {
r.lastPolicy = newPolicy
if wasUpToDate {
// A forced read found a change the store never reported.
// The sessions other than the caller's still hold the old
// policy and won't re-read it unless notified.
r.notifySessionsLocked()
}
}
return r.lastPolicy, nil
}
@@ -241,6 +248,12 @@ func (r *Reader) onPolicyChange() {
r.mu.Lock()
defer r.mu.Unlock()
r.upToDate = false
r.notifySessionsLocked()
}
// notifySessionsLocked notifies the active [ReadingSession]s of a policy change.
// r.mu must be held.
func (r *Reader) notifySessionsLocked() {
for _, s := range r.sessions {
select {
case s.policyChangedCh <- struct{}{}: