diff --git a/util/syspolicy/rsop/resultant_policy_test.go b/util/syspolicy/rsop/resultant_policy_test.go index 60132eae7..3f83afa72 100644 --- a/util/syspolicy/rsop/resultant_policy_test.go +++ b/util/syspolicy/rsop/resultant_policy_test.go @@ -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 { diff --git a/util/syspolicy/source/policy_reader.go b/util/syspolicy/source/policy_reader.go index ece08e3cb..064fc75d9 100644 --- a/util/syspolicy/source/policy_reader.go +++ b/util/syspolicy/source/policy_reader.go @@ -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{}{}: