mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-20 08:41:19 -08:00
ipn/ipnext, feature/guivisibility: move GUI visibility out of ipnlocal, send deltas only for changed peers
Replace the GUIVisibilityProvider/Filter plumbing and the per-session hidden-peer tracking with a generic per-session notify hook, and let the guivisibility extension own all policy interpretation. ipnlocal now only keeps the session's effective policy snapshot (which it already fetched) and runs ipnext.Hooks.MutateNotifyForSessionLocked on the per-session copy of each Notify, after gating, plus on the initial Notify. The hook receives an ipnext.WatchSession exposing that snapshot. The extension populates Notify.NodeGUIVisibility with the hidden peers among the nodes the Notify covers: all peers when it carries Policy (initially and on policy change), or the PeersChanged peers otherwise. Absent means visible, so a Policy-bearing Notify replaces the client's visibility state and a peer-change Notify updates only the changed peers. No NodeGUIVisibilityChanges/Removed fields and no per-session node state are needed: removed peers simply leave the client's peer set, and full netmap updates already resend every peer alongside SelfChange. When the feature isn't linked, the hook slice is empty and the fast path in notifyForSessionLocked is unchanged. When the policy isn't configured, the hook costs a snapshot lookup per session per Notify and sends nothing. Updates tailscale/corp#44104 Signed-off-by: Nick Khyl <[email protected]>
This commit is contained in:
1 parent
2885dabcfe
commit
ea06bb020b
9 files changed
+255
-322
No files matched your search
@@ -1,6 +0,0 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
// Package guivisibility implements policy-based GUI visibility
|
||||
// filtering for tailnet devices.
|
||||
package guivisibility
|
||||
@@ -1,60 +1,125 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
// Package guivisibility interprets the HiddenNetworkDevices policy for IPN
|
||||
// bus watchers and populates [ipn.Notify.NodeGUIVisibility] accordingly.
|
||||
package guivisibility
|
||||
|
||||
import (
|
||||
"tailscale.com/feature"
|
||||
"tailscale.com/ipn"
|
||||
"tailscale.com/ipn/ipnext"
|
||||
"tailscale.com/tailcfg"
|
||||
"tailscale.com/types/logger"
|
||||
"tailscale.com/util/mak"
|
||||
"tailscale.com/util/syspolicy/pkey"
|
||||
"tailscale.com/util/syspolicy/policyclient"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if feature.Register("guivisibility") {
|
||||
ipnext.RegisterExtension("guivisibility", func(logger.Logf, ipnext.SafeBackend) (ipnext.Extension, error) {
|
||||
return extension{}, nil
|
||||
})
|
||||
ipnext.RegisterExtension("guivisibility", newExtension)
|
||||
}
|
||||
}
|
||||
|
||||
type extension struct{}
|
||||
func newExtension(logger.Logf, ipnext.SafeBackend) (ipnext.Extension, error) {
|
||||
return &extension{}, nil
|
||||
}
|
||||
|
||||
func (extension) Name() string { return "guivisibility" }
|
||||
func (extension) Init(ipnext.Host) error { return nil }
|
||||
func (extension) Shutdown() error { return nil }
|
||||
type extension struct {
|
||||
host ipnext.Host
|
||||
}
|
||||
|
||||
func (extension) ForPolicy(snapshot *policyclient.PolicySnapshot) ipnext.GUIVisibilityFilter {
|
||||
f := visibilityFilter{}
|
||||
for _, value := range policyclient.GetStringArrayFromSnapshot(snapshot, pkey.HiddenNetworkDevices) {
|
||||
switch value {
|
||||
case "current-user":
|
||||
f.currentUser = true
|
||||
case "other-users":
|
||||
f.otherUsers = true
|
||||
case "tagged-devices":
|
||||
f.taggedDevices = true
|
||||
func (e *extension) Name() string { return "guivisibility" }
|
||||
|
||||
func (e *extension) Init(h ipnext.Host) error {
|
||||
e.host = h
|
||||
h.Hooks().MutateNotifyForSessionLocked.Add(e.mutateNotifyForSession)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *extension) Shutdown() error { return nil }
|
||||
|
||||
// mutateNotifyForSession populates n.NodeGUIVisibility with the peers that
|
||||
// sess's HiddenNetworkDevices policy hides: all of them if n carries a policy
|
||||
// snapshot, or those among n.PeersChanged otherwise.
|
||||
func (e *extension) mutateNotifyForSession(sess ipnext.WatchSession, n *ipn.Notify) {
|
||||
if n.Policy == nil && len(n.PeersChanged) == 0 {
|
||||
return
|
||||
}
|
||||
hidden := hiddenNetworkDevicesFromPolicy(sess.Policy())
|
||||
if hidden == (hiddenNetworkDevices{}) {
|
||||
return
|
||||
}
|
||||
nb := e.host.NodeBackend()
|
||||
self := nb.Self()
|
||||
if !self.Valid() {
|
||||
return
|
||||
}
|
||||
var visibility map[tailcfg.StableNodeID]ipn.NodeGUIVisibility
|
||||
hide := func(peer tailcfg.NodeView) {
|
||||
if peerHiddenInGUI(peer, self, hidden) {
|
||||
mak.Set(&visibility, peer.StableID(), ipn.NodeGUIVisibility{HideInGUI: true})
|
||||
}
|
||||
}
|
||||
if !f.currentUser && !f.otherUsers && !f.taggedDevices {
|
||||
return nil
|
||||
if n.Policy != nil {
|
||||
for _, peer := range nb.Peers() {
|
||||
hide(peer)
|
||||
}
|
||||
} else {
|
||||
for _, peer := range n.PeersChanged {
|
||||
hide(peer.View())
|
||||
}
|
||||
}
|
||||
return f
|
||||
n.NodeGUIVisibility = visibility
|
||||
}
|
||||
|
||||
type visibilityFilter struct{ currentUser, otherUsers, taggedDevices bool }
|
||||
const (
|
||||
hiddenNetworkDevicesCurrentUser = "current-user"
|
||||
hiddenNetworkDevicesOtherUsers = "other-users"
|
||||
hiddenNetworkDevicesTaggedDevices = "tagged-devices"
|
||||
)
|
||||
|
||||
func (f visibilityFilter) Hide(peer, self tailcfg.NodeView) bool {
|
||||
if !peer.Valid() || !self.Valid() || peer.ID() == self.ID() {
|
||||
// hiddenNetworkDevices is the parsed HiddenNetworkDevices policy for a watch
|
||||
// session. Its zero value means no devices are hidden.
|
||||
type hiddenNetworkDevices struct {
|
||||
currentUser bool
|
||||
otherUsers bool
|
||||
taggedDevices bool
|
||||
}
|
||||
|
||||
func hiddenNetworkDevicesFromPolicy(snapshot *policyclient.PolicySnapshot) hiddenNetworkDevices {
|
||||
values := policyclient.GetStringArrayFromSnapshot(
|
||||
snapshot,
|
||||
pkey.HiddenNetworkDevices,
|
||||
)
|
||||
|
||||
var hidden hiddenNetworkDevices
|
||||
for _, value := range values {
|
||||
switch value {
|
||||
case hiddenNetworkDevicesCurrentUser:
|
||||
hidden.currentUser = true
|
||||
case hiddenNetworkDevicesOtherUsers:
|
||||
hidden.otherUsers = true
|
||||
case hiddenNetworkDevicesTaggedDevices:
|
||||
hidden.taggedDevices = true
|
||||
}
|
||||
}
|
||||
return hidden
|
||||
}
|
||||
|
||||
func peerHiddenInGUI(peer, self tailcfg.NodeView, hidden hiddenNetworkDevices) bool {
|
||||
if peer.ID() == self.ID() {
|
||||
return false
|
||||
}
|
||||
|
||||
if peer.IsTagged() {
|
||||
return f.taggedDevices
|
||||
return hidden.taggedDevices
|
||||
}
|
||||
|
||||
if peer.SharerOrUser() == self.User() {
|
||||
return f.currentUser
|
||||
return hidden.currentUser
|
||||
}
|
||||
return f.otherUsers
|
||||
|
||||
return hidden.otherUsers
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"tailscale.com/tailcfg"
|
||||
)
|
||||
|
||||
func TestVisibilityFilter(t *testing.T) {
|
||||
func TestPeerHiddenInGUI(t *testing.T) {
|
||||
self := (&tailcfg.Node{
|
||||
ID: 1,
|
||||
User: 10,
|
||||
@@ -36,7 +36,7 @@ func TestVisibilityFilter(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
peer tailcfg.NodeView
|
||||
filter visibilityFilter
|
||||
hidden hiddenNetworkDevices
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
@@ -52,73 +52,101 @@ func TestVisibilityFilter(t *testing.T) {
|
||||
peer: tagged,
|
||||
},
|
||||
{
|
||||
name: "current-user/self-stays-visible",
|
||||
peer: self,
|
||||
filter: visibilityFilter{currentUser: true},
|
||||
name: "current-user/self-stays-visible",
|
||||
peer: self,
|
||||
hidden: hiddenNetworkDevices{
|
||||
currentUser: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "current-user/same-user",
|
||||
peer: sameUser,
|
||||
filter: visibilityFilter{currentUser: true},
|
||||
want: true,
|
||||
name: "current-user/same-user",
|
||||
peer: sameUser,
|
||||
hidden: hiddenNetworkDevices{
|
||||
currentUser: true,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "current-user/shared-by-current-user",
|
||||
peer: sharedByCurrentUser,
|
||||
filter: visibilityFilter{currentUser: true},
|
||||
want: true,
|
||||
name: "current-user/shared-by-current-user",
|
||||
peer: sharedByCurrentUser,
|
||||
hidden: hiddenNetworkDevices{
|
||||
currentUser: true,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "current-user/other-user",
|
||||
peer: otherUser,
|
||||
filter: visibilityFilter{currentUser: true},
|
||||
name: "current-user/other-user",
|
||||
peer: otherUser,
|
||||
hidden: hiddenNetworkDevices{
|
||||
currentUser: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "other-users/other-user",
|
||||
peer: otherUser,
|
||||
filter: visibilityFilter{otherUsers: true},
|
||||
want: true,
|
||||
name: "other-users/other-user",
|
||||
peer: otherUser,
|
||||
hidden: hiddenNetworkDevices{
|
||||
otherUsers: true,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "other-users/tagged-is-separate",
|
||||
peer: tagged,
|
||||
filter: visibilityFilter{otherUsers: true},
|
||||
name: "other-users/tagged-is-separate",
|
||||
peer: tagged,
|
||||
hidden: hiddenNetworkDevices{
|
||||
otherUsers: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "tagged-devices/tagged",
|
||||
peer: tagged,
|
||||
filter: visibilityFilter{taggedDevices: true},
|
||||
want: true,
|
||||
name: "tagged-devices/tagged",
|
||||
peer: tagged,
|
||||
hidden: hiddenNetworkDevices{
|
||||
taggedDevices: true,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "tagged-devices/other-user",
|
||||
peer: otherUser,
|
||||
filter: visibilityFilter{taggedDevices: true},
|
||||
name: "tagged-devices/other-user",
|
||||
peer: otherUser,
|
||||
hidden: hiddenNetworkDevices{
|
||||
taggedDevices: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "all/same-user",
|
||||
peer: sameUser,
|
||||
filter: visibilityFilter{true, true, true},
|
||||
want: true,
|
||||
name: "all/same-user",
|
||||
peer: sameUser,
|
||||
hidden: hiddenNetworkDevices{
|
||||
currentUser: true,
|
||||
otherUsers: true,
|
||||
taggedDevices: true,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "all/other-user",
|
||||
peer: otherUser,
|
||||
filter: visibilityFilter{true, true, true},
|
||||
want: true,
|
||||
name: "all/other-user",
|
||||
peer: otherUser,
|
||||
hidden: hiddenNetworkDevices{
|
||||
currentUser: true,
|
||||
otherUsers: true,
|
||||
taggedDevices: true,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "all/tagged",
|
||||
peer: tagged,
|
||||
filter: visibilityFilter{true, true, true},
|
||||
want: true,
|
||||
name: "all/tagged",
|
||||
peer: tagged,
|
||||
hidden: hiddenNetworkDevices{
|
||||
currentUser: true,
|
||||
otherUsers: true,
|
||||
taggedDevices: true,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.filter.Hide(tt.peer, self); got != tt.want {
|
||||
t.Fatalf("Hide() = %v, want %v", got, tt.want)
|
||||
if got := peerHiddenInGUI(tt.peer, self, tt.hidden); got != tt.want {
|
||||
t.Fatalf("peerHiddenInGUI() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
+11
-12
@@ -355,22 +355,21 @@ type Notify struct {
|
||||
// a continuous view of node state without fetching the netmap.
|
||||
InitialStatus *ipnstate.Status `json:",omitzero"`
|
||||
|
||||
// NodeGUIVisibility, when non-nil, is a complete snapshot of GUI
|
||||
// visibility state for the current node and peers, keyed by stable node ID.
|
||||
// It is sent to watchers that requested [NotifySysPolicyChanges]. Clients
|
||||
// should replace their previous visibility state with this map.
|
||||
// NodeGUIVisibility, when non-empty, lists the nodes that GUI clients
|
||||
// should hide, keyed by stable node ID, among the nodes this Notify
|
||||
// covers: all nodes if it carries [Notify.Policy] (initially and on
|
||||
// policy change), or the peers in [Notify.PeersChanged] otherwise.
|
||||
// Covered nodes absent from the map are visible, so a Notify carrying
|
||||
// Policy replaces any previously received visibility state, while one
|
||||
// carrying PeersChanged updates it for the changed peers only.
|
||||
//
|
||||
// It is populated for watchers that requested [NotifySysPolicyChanges];
|
||||
// incremental updates additionally require [NotifyPeerChanges].
|
||||
//
|
||||
// This is presentation metadata only. Nodes remain present in status,
|
||||
// peer notifications, åand the underlying netmap.
|
||||
// peer notifications, and the underlying netmap.
|
||||
NodeGUIVisibility map[tailcfg.StableNodeID]NodeGUIVisibility `json:",omitzero"`
|
||||
|
||||
// NodeGUIVisibilityChanges contains visibility changes for individual peers.
|
||||
// Entries update, rather than replace, NodeGUIVisibility on the client.
|
||||
NodeGUIVisibilityChanges map[tailcfg.StableNodeID]NodeGUIVisibility `json:",omitzero"`
|
||||
|
||||
// NodeGUIVisibilityRemoved lists peers whose visibility entries should be deleted.
|
||||
NodeGUIVisibilityRemoved []tailcfg.StableNodeID `json:",omitzero"`
|
||||
|
||||
// PeerChangedPatch, if non-empty, lists narrow per-field peer patches
|
||||
// since the last Notify (currently Online, LastSeen, DERPHome,
|
||||
// Endpoints). It mirrors [tailcfg.MapResponse.PeersChangedPatch].
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package ipnext
|
||||
|
||||
import (
|
||||
"tailscale.com/tailcfg"
|
||||
"tailscale.com/util/syspolicy/policyclient"
|
||||
)
|
||||
|
||||
// GUIVisibilityProvider is an optional extension that interprets GUI
|
||||
// visibility policy for IPN watchers.
|
||||
type GUIVisibilityProvider interface {
|
||||
ForPolicy(*policyclient.PolicySnapshot) GUIVisibilityFilter
|
||||
}
|
||||
|
||||
// GUIVisibilityFilter classifies peers for one watcher's effective policy.
|
||||
type GUIVisibilityFilter interface {
|
||||
Hide(peer, self tailcfg.NodeView) bool
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
"tailscale.com/types/mapx"
|
||||
"tailscale.com/types/netmap"
|
||||
"tailscale.com/types/views"
|
||||
"tailscale.com/util/syspolicy/policyclient"
|
||||
"tailscale.com/wgengine/filter"
|
||||
)
|
||||
|
||||
@@ -378,6 +379,14 @@ type ProfileStateChangeCallback func(_ ipn.LoginProfileView, _ ipn.PrefsView, sa
|
||||
// back into LocalBackend, which may be locked during shutdown.
|
||||
type NewControlClientCallback func(controlclient.Client, ipn.LoginProfileView) (cleanup func())
|
||||
|
||||
// WatchSession is an IPN bus watch session, as seen by
|
||||
// [Hooks.MutateNotifyForSessionLocked].
|
||||
type WatchSession interface {
|
||||
// Policy returns the effective policy snapshot most recently delivered to
|
||||
// the session, or nil if it did not request [ipn.NotifySysPolicyChanges].
|
||||
Policy() *policyclient.PolicySnapshot
|
||||
}
|
||||
|
||||
// Hooks is a collection of hooks that extensions can add to (non-concurrently)
|
||||
// during program initialization and can be called by LocalBackend and others at
|
||||
// runtime.
|
||||
@@ -439,6 +448,12 @@ type Hooks struct {
|
||||
// before sending it to the IPN bus. It is called with LocalBackend.mu held.
|
||||
MutateNotifyLocked feature.Hooks[func(*ipn.Notify)]
|
||||
|
||||
// MutateNotifyForSessionLocked is called to optionally mutate the provided
|
||||
// Notify before sending it to a particular IPN bus watch session. It runs
|
||||
// after per-session gating, so the Notify reflects what the session will
|
||||
// receive. It is called with LocalBackend.mu held.
|
||||
MutateNotifyForSessionLocked feature.Hooks[func(WatchSession, *ipn.Notify)]
|
||||
|
||||
// SetPeerStatus is called to mutate PeerStatus.
|
||||
// Callers must only use NodeBackend to read data.
|
||||
SetPeerStatus feature.Hooks[func(*ipnstate.PeerStatus, tailcfg.NodeView, NodeBackend)]
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package ipnlocal
|
||||
|
||||
import (
|
||||
"tailscale.com/ipn"
|
||||
"tailscale.com/ipn/ipnext"
|
||||
"tailscale.com/tailcfg"
|
||||
"tailscale.com/util/mak"
|
||||
"tailscale.com/util/syspolicy/policyclient"
|
||||
)
|
||||
|
||||
// guiVisibilityFilterLocked gets the optional GUI-specific extension.
|
||||
// b.mu must be held.
|
||||
func (b *LocalBackend) guiVisibilityFilterLocked(snapshot *policyclient.PolicySnapshot) ipnext.GUIVisibilityFilter {
|
||||
var provider ipnext.GUIVisibilityProvider
|
||||
if !b.FindMatchingExtension(&provider) {
|
||||
return nil
|
||||
}
|
||||
return provider.ForPolicy(snapshot)
|
||||
}
|
||||
|
||||
// nodeGUIVisibilityLocked returns a snapshot of hidden peers. An empty,
|
||||
// non-nil map clears the previous snapshot when the policy is disabled.
|
||||
// b.mu must be held.
|
||||
func (b *LocalBackend) nodeGUIVisibilityLocked(filter ipnext.GUIVisibilityFilter) (map[tailcfg.StableNodeID]ipn.NodeGUIVisibility, map[tailcfg.NodeID]tailcfg.StableNodeID) {
|
||||
visibility := make(map[tailcfg.StableNodeID]ipn.NodeGUIVisibility)
|
||||
if filter == nil {
|
||||
return visibility, nil
|
||||
}
|
||||
nm := b.currentNode().NetMap()
|
||||
if nm == nil || !nm.SelfNode.Valid() {
|
||||
return visibility, nil
|
||||
}
|
||||
var hidden map[tailcfg.NodeID]tailcfg.StableNodeID
|
||||
for _, peer := range nm.Peers {
|
||||
if filter.Hide(peer, nm.SelfNode) {
|
||||
visibility[peer.StableID()] = ipn.NodeGUIVisibility{HideInGUI: true}
|
||||
mak.Set(&hidden, peer.ID(), peer.StableID())
|
||||
}
|
||||
}
|
||||
return visibility, hidden
|
||||
}
|
||||
|
||||
// nodeGUIVisibilityDeltaLocked evaluates only peers in this notification.
|
||||
// It returns nil deltas if effective visibility did not change.
|
||||
// b.mu must be held.
|
||||
func (b *LocalBackend) nodeGUIVisibilityDeltaLocked(sess *watchSession, n *ipn.Notify) (map[tailcfg.StableNodeID]ipn.NodeGUIVisibility, []tailcfg.StableNodeID) {
|
||||
var changes map[tailcfg.StableNodeID]ipn.NodeGUIVisibility
|
||||
var removed []tailcfg.StableNodeID
|
||||
nm := b.currentNode().NetMap()
|
||||
var self tailcfg.NodeView
|
||||
if nm != nil {
|
||||
self = nm.SelfNode
|
||||
}
|
||||
for _, id := range n.PeersRemoved {
|
||||
if stable, ok := sess.hiddenPeers[id]; ok {
|
||||
removed = append(removed, stable)
|
||||
delete(sess.hiddenPeers, id)
|
||||
}
|
||||
}
|
||||
for _, peer := range n.PeersChanged {
|
||||
if peer == nil {
|
||||
continue
|
||||
}
|
||||
if prev, ok := sess.hiddenPeers[peer.ID]; ok && prev != peer.StableID {
|
||||
removed = append(removed, prev)
|
||||
delete(sess.hiddenPeers, peer.ID)
|
||||
}
|
||||
hidden := self.Valid() && sess.guiVisibility.Hide(peer.View(), self)
|
||||
_, wasHidden := sess.hiddenPeers[peer.ID]
|
||||
if hidden == wasHidden {
|
||||
continue
|
||||
}
|
||||
if hidden {
|
||||
mak.Set(&changes, peer.StableID, ipn.NodeGUIVisibility{HideInGUI: true})
|
||||
mak.Set(&sess.hiddenPeers, peer.ID, peer.StableID)
|
||||
} else {
|
||||
removed = append(removed, peer.StableID)
|
||||
delete(sess.hiddenPeers, peer.ID)
|
||||
}
|
||||
}
|
||||
return changes, removed
|
||||
}
|
||||
+35
-36
@@ -212,13 +212,14 @@ type watchSession struct {
|
||||
// Empty string means default/device scope.
|
||||
policyUID string
|
||||
|
||||
// GUI visibility is optional; nil if the extension isn't linked.
|
||||
guiVisibility ipnext.GUIVisibilityFilter
|
||||
// Track hidden peers by NodeID so removals can be delivered even after
|
||||
// they disappear from NodeBackend.
|
||||
hiddenPeers map[tailcfg.NodeID]tailcfg.StableNodeID
|
||||
// policy is the effective policy snapshot most recently delivered to
|
||||
// this session, or nil if it didn't request [ipn.NotifySysPolicyChanges].
|
||||
policy *policyclient.PolicySnapshot
|
||||
}
|
||||
|
||||
// Policy implements [ipnext.WatchSession].
|
||||
func (sess *watchSession) Policy() *policyclient.PolicySnapshot { return sess.policy }
|
||||
|
||||
var (
|
||||
// errShutdown indicates that the [LocalBackend.Shutdown] was called.
|
||||
errShutdown = errors.New("shutting down")
|
||||
@@ -2496,13 +2497,10 @@ func (b *LocalBackend) sysPolicyChangedForSession(sess *watchSession) {
|
||||
return
|
||||
}
|
||||
|
||||
sess.guiVisibility = b.guiVisibilityFilterLocked(snapshot)
|
||||
visibility, hidden := b.nodeGUIVisibilityLocked(sess.guiVisibility)
|
||||
sess.hiddenPeers = hidden
|
||||
sess.policy = snapshot
|
||||
n := ipn.Notify{
|
||||
Policy: snapshot,
|
||||
NodeGUIVisibility: visibility,
|
||||
Version: version.Long(),
|
||||
Policy: snapshot,
|
||||
Version: version.Long(),
|
||||
}
|
||||
for _, f := range b.extHost.Hooks().MutateNotifyLocked {
|
||||
f(&n)
|
||||
@@ -3882,8 +3880,6 @@ func (b *LocalBackend) WatchNotificationsAs(ctx context.Context, actor ipnauth.A
|
||||
b.mu.Lock()
|
||||
|
||||
var policyUID string
|
||||
var guiVisibility ipnext.GUIVisibilityFilter
|
||||
var hiddenPeers map[tailcfg.NodeID]tailcfg.StableNodeID
|
||||
const initialBits = ipn.NotifyInitialState | ipn.NotifyInitialPrefs |
|
||||
ipn.NotifyInitialStatus |
|
||||
ipn.NotifyInitialDriveShares | ipn.NotifyInitialSuggestedExitNode |
|
||||
@@ -3928,9 +3924,6 @@ func (b *LocalBackend) WatchNotificationsAs(ctx context.Context, actor ipnauth.A
|
||||
ini.Policy, err = b.polc.GetPolicySnapshot(policyUID)
|
||||
if err != nil {
|
||||
b.logf("syspolicy: GetPolicySnapshot(%q): %v", policyUID, err)
|
||||
} else if ini.Policy != nil {
|
||||
guiVisibility = b.guiVisibilityFilterLocked(ini.Policy)
|
||||
ini.NodeGUIVisibility, hiddenPeers = b.nodeGUIVisibilityLocked(guiVisibility)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3939,20 +3932,22 @@ func (b *LocalBackend) WatchNotificationsAs(ctx context.Context, actor ipnauth.A
|
||||
defer cancel()
|
||||
|
||||
session := &watchSession{
|
||||
ch: ch,
|
||||
ctx: ctx,
|
||||
owner: actor,
|
||||
sessionID: sessionID,
|
||||
cancel: cancel,
|
||||
mask: mask,
|
||||
policyUID: policyUID,
|
||||
guiVisibility: guiVisibility,
|
||||
hiddenPeers: hiddenPeers,
|
||||
ch: ch,
|
||||
ctx: ctx,
|
||||
owner: actor,
|
||||
sessionID: sessionID,
|
||||
cancel: cancel,
|
||||
mask: mask,
|
||||
policyUID: policyUID,
|
||||
}
|
||||
mak.Set(&b.notifyWatchers, sessionID, session)
|
||||
if mask&ipn.NotifyPeerWireGuardState != 0 {
|
||||
ini.PeerState = maps.Clone(b.peerWGState)
|
||||
}
|
||||
if ini != nil {
|
||||
session.policy = ini.Policy
|
||||
b.mutateNotifyForSessionLocked(session, ini)
|
||||
}
|
||||
b.mu.Unlock()
|
||||
deadlockDone()
|
||||
|
||||
@@ -4250,7 +4245,8 @@ func (b *LocalBackend) closeLaggingWatchSessionLocked(sess *watchSession) {
|
||||
// into full-Node [Notify.PeersChanged] entries for sessions that asked
|
||||
// for peer changes but not patches, and tracking on sess which
|
||||
// [tailcfg.UserProfileView]s have already been delivered so subsequent
|
||||
// sends only carry new/changed profiles.
|
||||
// sends only carry new/changed profiles. Finally, it runs the extensions'
|
||||
// [ipnext.Hooks.MutateNotifyForSessionLocked] hooks, if any, on the result.
|
||||
//
|
||||
// The returned pointer is either n itself (no adjustments needed for
|
||||
// this session) or a fresh *ipn.Notify with the adjusted fields. The
|
||||
@@ -4274,9 +4270,7 @@ func (b *LocalBackend) notifyForSessionLocked(sess *watchSession, n *ipn.Notify)
|
||||
stripPatches := len(n.PeerChangedPatch) > 0 && !wantsPeerPatches
|
||||
stripPeerState := len(n.PeerState) > 0 && sess.mask&ipn.NotifyPeerWireGuardState == 0
|
||||
promotePatches := len(n.PeerChangedPatch) > 0 && wantsPeerChanges && !wantsPeerPatches
|
||||
refreshNodeGUIVisibility := sess.guiVisibility != nil && n.SelfChange != nil
|
||||
updateNodeGUIVisibility := sess.guiVisibility != nil &&
|
||||
(len(n.PeersChanged) != 0 || len(n.PeersRemoved) != 0)
|
||||
mutateHooks := b.extHost.Hooks().MutateNotifyForSessionLocked
|
||||
|
||||
if sess.selfChangeResetsImplicitState(n.SelfChange.View()) {
|
||||
sess.lastSentUserProfile = nil
|
||||
@@ -4306,16 +4300,10 @@ func (b *LocalBackend) notifyForSessionLocked(sess *watchSession, n *ipn.Notify)
|
||||
}
|
||||
replaceUserProfiles := !stripUserProfiles && len(sessUserProfiles) != len(n.UserProfiles)
|
||||
|
||||
if !stripPeersChanged && !stripPeersRemoved && !stripPatches && !stripPeerState && !stripUserProfiles && !replaceUserProfiles && !promotePatches && !refreshNodeGUIVisibility && !updateNodeGUIVisibility {
|
||||
if !stripPeersChanged && !stripPeersRemoved && !stripPatches && !stripPeerState && !stripUserProfiles && !replaceUserProfiles && !promotePatches && len(mutateHooks) == 0 {
|
||||
return n
|
||||
}
|
||||
nCopy := *n
|
||||
if refreshNodeGUIVisibility {
|
||||
nCopy.NodeGUIVisibility, sess.hiddenPeers = b.nodeGUIVisibilityLocked(sess.guiVisibility)
|
||||
} else if updateNodeGUIVisibility {
|
||||
nCopy.NodeGUIVisibilityChanges, nCopy.NodeGUIVisibilityRemoved =
|
||||
b.nodeGUIVisibilityDeltaLocked(sess, n)
|
||||
}
|
||||
if stripPeersChanged {
|
||||
nCopy.PeersChanged = nil
|
||||
}
|
||||
@@ -4346,9 +4334,20 @@ func (b *LocalBackend) notifyForSessionLocked(sess *watchSession, n *ipn.Notify)
|
||||
} else if replaceUserProfiles {
|
||||
nCopy.UserProfiles = sessUserProfiles
|
||||
}
|
||||
b.mutateNotifyForSessionLocked(sess, &nCopy)
|
||||
return &nCopy
|
||||
}
|
||||
|
||||
// mutateNotifyForSessionLocked lets extensions adjust n, which must not be
|
||||
// shared with other sessions, before it is delivered to sess.
|
||||
//
|
||||
// b.mu must be held.
|
||||
func (b *LocalBackend) mutateNotifyForSessionLocked(sess *watchSession, n *ipn.Notify) {
|
||||
for _, f := range b.extHost.Hooks().MutateNotifyForSessionLocked {
|
||||
f(sess, n)
|
||||
}
|
||||
}
|
||||
|
||||
// selfChangeResetsImplicitState reports whether self changes the identity of
|
||||
// the self node last delivered to sess. WatchIPNBus sessions survive profile
|
||||
// switches, but a self-node identity change is a boundary for implicit bus
|
||||
|
||||
+31
-93
@@ -44,7 +44,6 @@ import (
|
||||
"tailscale.com/ipn"
|
||||
"tailscale.com/ipn/conffile"
|
||||
"tailscale.com/ipn/ipnauth"
|
||||
"tailscale.com/ipn/ipnext"
|
||||
"tailscale.com/ipn/ipnlocal/netmapcache"
|
||||
"tailscale.com/ipn/store/mem"
|
||||
"tailscale.com/net/netcheck"
|
||||
@@ -9189,11 +9188,6 @@ func TestHiddenNetworkDevicesNotifyPerUser(t *testing.T) {
|
||||
lb := newTestLocalBackendWithSys(t, sys)
|
||||
lb.extHost.Init()
|
||||
|
||||
var provider ipnext.GUIVisibilityProvider
|
||||
if !lb.FindMatchingExtension(&provider) {
|
||||
t.Fatal("GUI visibility extension not registered or initialized")
|
||||
}
|
||||
|
||||
lb.currentNode().SetNetMap(&netmap.NetworkMap{
|
||||
SelfNode: (&tailcfg.Node{
|
||||
ID: 1,
|
||||
@@ -9216,7 +9210,7 @@ func TestHiddenNetworkDevicesNotifyPerUser(t *testing.T) {
|
||||
return wantedNotification{
|
||||
name: fmt.Sprintf("NodeGUIVisibility-hidden=%v", wantHidden),
|
||||
cond: func(t testing.TB, _ ipnauth.Actor, n *ipn.Notify) bool {
|
||||
if n.NodeGUIVisibility == nil {
|
||||
if n.Policy == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -9287,7 +9281,7 @@ func TestHiddenNetworkDevicesPolicyChangeUpdatesVisibility(t *testing.T) {
|
||||
{
|
||||
name: "initially visible",
|
||||
cond: func(t testing.TB, _ ipnauth.Actor, n *ipn.Notify) bool {
|
||||
if n.NodeGUIVisibility == nil {
|
||||
if n.Policy == nil {
|
||||
return false
|
||||
}
|
||||
return !n.NodeGUIVisibility["peer-10"].HideInGUI
|
||||
@@ -9296,12 +9290,7 @@ func TestHiddenNetworkDevicesPolicyChangeUpdatesVisibility(t *testing.T) {
|
||||
{
|
||||
name: "hidden after policy change",
|
||||
cond: func(t testing.TB, _ ipnauth.Actor, n *ipn.Notify) bool {
|
||||
if n.NodeGUIVisibility == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
got, ok := n.NodeGUIVisibility["peer-10"]
|
||||
if !ok || !got.HideInGUI {
|
||||
if n.Policy == nil || !n.NodeGUIVisibility["peer-10"].HideInGUI {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -9369,99 +9358,48 @@ func TestHiddenNetworkDevicesIncrementalPeerUpdates(t *testing.T) {
|
||||
},
|
||||
})
|
||||
|
||||
// wantVisibility matches a peer-change notification and checks that
|
||||
// its NodeGUIVisibility hides exactly the given peers.
|
||||
wantVisibility := func(name string, hidden ...tailcfg.StableNodeID) wantedNotification {
|
||||
return wantedNotification{
|
||||
name: name,
|
||||
cond: func(t testing.TB, _ ipnauth.Actor, n *ipn.Notify) bool {
|
||||
if len(n.PeersChanged) == 0 && len(n.PeersRemoved) == 0 {
|
||||
return false
|
||||
}
|
||||
want := make(map[tailcfg.StableNodeID]ipn.NodeGUIVisibility)
|
||||
for _, id := range hidden {
|
||||
want[id] = ipn.NodeGUIVisibility{HideInGUI: true}
|
||||
}
|
||||
if got := n.NodeGUIVisibility; !maps.Equal(got, want) {
|
||||
t.Errorf("%s: NodeGUIVisibility = %v; want %v", name, got, want)
|
||||
}
|
||||
return true
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
actor := &ipnauth.TestActor{UID: ipn.WindowsUserID(uid)}
|
||||
nw := newNotificationWatcher(t, lb, actor)
|
||||
|
||||
nw.watch(ipn.NotifySysPolicyChanges, []wantedNotification{
|
||||
nw.watch(ipn.NotifySysPolicyChanges|ipn.NotifyPeerChanges, []wantedNotification{
|
||||
{
|
||||
name: "initial visibility snapshot",
|
||||
cond: func(t testing.TB, _ ipnauth.Actor, n *ipn.Notify) bool {
|
||||
if n.NodeGUIVisibility == nil {
|
||||
if n.Policy == nil {
|
||||
return false
|
||||
}
|
||||
if len(n.NodeGUIVisibility) != 1 ||
|
||||
!n.NodeGUIVisibility["peer-10"].HideInGUI {
|
||||
if len(n.NodeGUIVisibility) != 1 || !n.NodeGUIVisibility["peer-10"].HideInGUI {
|
||||
t.Errorf("unexpected initial visibility: %v", n.NodeGUIVisibility)
|
||||
}
|
||||
return true
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "hidden peer added",
|
||||
cond: func(t testing.TB, _ ipnauth.Actor, n *ipn.Notify) bool {
|
||||
if len(n.NodeGUIVisibilityChanges) == 0 {
|
||||
return false
|
||||
}
|
||||
if n.NodeGUIVisibility != nil {
|
||||
t.Error("peer update sent a full visibility snapshot")
|
||||
}
|
||||
if len(n.NodeGUIVisibilityChanges) != 1 ||
|
||||
!n.NodeGUIVisibilityChanges["peer-20"].HideInGUI {
|
||||
t.Errorf("unexpected visibility changes: %v",
|
||||
n.NodeGUIVisibilityChanges)
|
||||
}
|
||||
if len(n.NodeGUIVisibilityRemoved) != 0 {
|
||||
t.Errorf("unexpected visibility removals: %v",
|
||||
n.NodeGUIVisibilityRemoved)
|
||||
}
|
||||
if len(n.PeersChanged) != 0 {
|
||||
t.Error("policy-only watcher received PeersChanged")
|
||||
}
|
||||
return true
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "hidden peer becomes visible",
|
||||
cond: func(t testing.TB, _ ipnauth.Actor, n *ipn.Notify) bool {
|
||||
if len(n.NodeGUIVisibilityRemoved) == 0 {
|
||||
return false
|
||||
}
|
||||
if n.NodeGUIVisibility != nil {
|
||||
t.Error("peer update sent a full visibility snapshot")
|
||||
}
|
||||
if len(n.NodeGUIVisibilityChanges) != 0 {
|
||||
t.Errorf("unexpected visibility changes: %v",
|
||||
n.NodeGUIVisibilityChanges)
|
||||
}
|
||||
if len(n.NodeGUIVisibilityRemoved) != 1 ||
|
||||
n.NodeGUIVisibilityRemoved[0] != "peer-20" {
|
||||
t.Errorf("unexpected visibility removals: %v",
|
||||
n.NodeGUIVisibilityRemoved)
|
||||
}
|
||||
if len(n.PeersChanged) != 0 {
|
||||
t.Error("policy-only watcher received PeersChanged")
|
||||
}
|
||||
return true
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "hidden peer removed",
|
||||
cond: func(t testing.TB, _ ipnauth.Actor, n *ipn.Notify) bool {
|
||||
if len(n.NodeGUIVisibilityRemoved) == 0 {
|
||||
return false
|
||||
}
|
||||
if len(n.NodeGUIVisibilityRemoved) != 1 ||
|
||||
n.NodeGUIVisibilityRemoved[0] != "peer-10" {
|
||||
t.Errorf("unexpected visibility removals: %v",
|
||||
n.NodeGUIVisibilityRemoved)
|
||||
}
|
||||
if n.NodeGUIVisibility != nil {
|
||||
t.Error("peer removal sent a full visibility snapshot")
|
||||
}
|
||||
if len(n.NodeGUIVisibilityChanges) != 0 {
|
||||
t.Errorf("unexpected visibility changes: %v",
|
||||
n.NodeGUIVisibilityChanges)
|
||||
}
|
||||
if len(n.PeersRemoved) != 0 {
|
||||
t.Error("policy-only watcher received PeersRemoved")
|
||||
}
|
||||
return true
|
||||
},
|
||||
},
|
||||
wantVisibility("hidden peer added", "peer-20"),
|
||||
wantVisibility("hidden peer becomes visible"),
|
||||
wantVisibility("hidden peer removed"),
|
||||
}, func(t testing.TB, _ ipnauth.Actor, n *ipn.Notify) bool {
|
||||
if n.NodeGUIVisibility != nil {
|
||||
t.Errorf("unexpected full visibility snapshot: %v",
|
||||
n.NodeGUIVisibility)
|
||||
t.Errorf("unexpected visibility update: %v", n.NodeGUIVisibility)
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user