diff --git a/cmd/tailscale/cli/whois.go b/cmd/tailscale/cli/whois.go index c6cae118c..fe2f8150b 100644 --- a/cmd/tailscale/cli/whois.go +++ b/cmd/tailscale/cli/whois.go @@ -99,8 +99,10 @@ func printWhoIs(who *apitype.WhoIsResponse, tailnet *ipnstate.TailnetStatus, asJ } } fmt.Fprintln(w, "Tailnet:") - // TODO(will@): plumb through StableTailnetID so we can show that here as well fmt.Fprintf(w, " Name:\t%s\n", cmp.Or(displayName, tailnet.Name)) + if !tailnet.StableID.IsZero() { + fmt.Fprintf(w, " ID:\t%s\n", tailnet.StableID) + } fmt.Fprintf(w, " MagicDNS Suffix:\t%s\n", tailnet.MagicDNSSuffix) } w.Flush() diff --git a/cmd/tailscale/cli/whois_test.go b/cmd/tailscale/cli/whois_test.go new file mode 100644 index 000000000..19e294cb7 --- /dev/null +++ b/cmd/tailscale/cli/whois_test.go @@ -0,0 +1,75 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package cli + +import ( + "encoding/json" + "io" + "reflect" + "strings" + "testing" + + "tailscale.com/client/tailscale/apitype" + "tailscale.com/ipn/ipnstate" + "tailscale.com/tailcfg" + "tailscale.com/tstest" +) + +func TestPrintWhoIsTailnet(t *testing.T) { + who := &apitype.WhoIsResponse{ + Node: &tailcfg.Node{Name: "node.example.ts.net.", StableID: "node-abcd"}, + UserProfile: &tailcfg.UserProfile{ID: 1, LoginName: "alice@example.com"}, + } + for _, tt := range []struct { + name string + tailnet *ipnstate.TailnetStatus + wantTailnet string + }{ + { + name: "with_id", + tailnet: &ipnstate.TailnetStatus{ + Name: "example.com", StableID: "tailnet-abcd", MagicDNSSuffix: "example.ts.net", + }, + wantTailnet: "Name: example.com ID: tailnet-abcd MagicDNS Suffix: example.ts.net", + }, + { + name: "without_id", + tailnet: &ipnstate.TailnetStatus{ + Name: "example.com", MagicDNSSuffix: "example.ts.net", + }, + wantTailnet: "Name: example.com MagicDNS Suffix: example.ts.net", + }, + {name: "peer"}, + } { + t.Run(tt.name, func(t *testing.T) { + var out strings.Builder + tstest.Replace[io.Writer](t, &Stdout, &out) + if err := printWhoIs(who, tt.tailnet, false); err != nil { + t.Fatal(err) + } + // Get everything after the section header + _, tailnetOutput, hasTailnet := strings.Cut(out.String(), "Tailnet:\n") + if want := tt.tailnet != nil; hasTailnet != want { + t.Fatalf("Tailnet section present = %v; want %v", hasTailnet, want) + } + // Ignore tabwriter's spacing when comparing the contents. + gotTailnet := strings.Join(strings.Fields(tailnetOutput), " ") + if gotTailnet != tt.wantTailnet { + t.Errorf("tailnet output = %q; want %q", gotTailnet, tt.wantTailnet) + } + + out.Reset() + if err := printWhoIs(who, tt.tailnet, true); err != nil { + t.Fatal(err) + } + var got whoisAndTailnet + if err := json.Unmarshal([]byte(out.String()), &got); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(got.CurrentTailnet, tt.tailnet) { + t.Errorf("JSON CurrentTailnet = %+v; want %+v", got.CurrentTailnet, tt.tailnet) + } + }) + } +} diff --git a/control/controlclient/map.go b/control/controlclient/map.go index de87edfd4..1d8b76a73 100644 --- a/control/controlclient/map.go +++ b/control/controlclient/map.go @@ -775,8 +775,8 @@ func peerChangeDiff(was tailcfg.NodeView, n *tailcfg.Node, onFalse func(string)) case "computedHostIfDifferent", "ComputedName", "ComputedNameWithHost": // Caller's responsibility to have populated these. continue - case "DataPlaneAuditLogID": - // Not sent for peers. + case "DataPlaneAuditLogID", "StableTailnetID": + // Not sent for peers. case "Capabilities": // Deprecated; see https://github.com/tailscale/tailscale/issues/11508 // And it was never sent by any known control server. diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 04e8de6e9..e43066f04 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -1499,6 +1499,7 @@ func (b *LocalBackend) updateStatusLocked(sb *ipnstate.StatusBuilder) { s.CurrentTailnet.MagicDNSSuffix = nm.MagicDNSSuffix() s.CurrentTailnet.MagicDNSEnabled = nm.DNS.Proxied s.CurrentTailnet.Name = nm.Domain + s.CurrentTailnet.StableID = nm.StableTailnetID() if prefs := b.pm.CurrentPrefs(); prefs.Valid() { if !prefs.RouteAll() && nm.AnyPeersAdvertiseRoutes() { s.Health = append(s.Health, healthmsg.WarnAcceptRoutesOff) diff --git a/ipn/ipnlocal/local_test.go b/ipn/ipnlocal/local_test.go index 944dd68af..c4379cdbc 100644 --- a/ipn/ipnlocal/local_test.go +++ b/ipn/ipnlocal/local_test.go @@ -1936,6 +1936,48 @@ func TestStatusPeerCapabilities(t *testing.T) { } } +func TestStatusStableTailnetID(t *testing.T) { + b := newTestLocalBackend(t) + for _, tt := range []struct { + name string + stableID tailcfg.StableTailnetID + }{ + {name: "populated", stableID: "tailnet-abcd"}, + {name: "missing"}, + } { + t.Run(tt.name, func(t *testing.T) { + b.setNetMapLocked(&netmap.NetworkMap{ + Domain: "example.com", + SelfNode: (&tailcfg.Node{ + MachineAuthorized: true, + Addresses: ipps("100.101.101.101"), + StableTailnetID: tt.stableID, + }).View(), + }) + + // The ID is returned with or without peers. + t.Run("with_peers", func(t *testing.T) { + st := b.Status() + if st.CurrentTailnet == nil { + t.Fatalf("CurrentTailnet is nil") + } + if got := st.CurrentTailnet.StableID; got != tt.stableID { + t.Errorf("CurrentTailnet.StableID = %q; want %q", got, tt.stableID) + } + }) + t.Run("without_peers", func(t *testing.T) { + st := b.StatusWithoutPeers() + if st.CurrentTailnet == nil { + t.Fatalf("CurrentTailnet is nil") + } + if got := st.CurrentTailnet.StableID; got != tt.stableID { + t.Errorf("CurrentTailnet.StableID = %q; want %q", got, tt.stableID) + } + }) + }) + } +} + // TestStatusWithoutPeersSelfUserProfile verifies that the self user's // UserProfile is reported in Status.User even when peers are omitted, so that // callers like `tailscale status --peers=false` can resolve the self node's diff --git a/ipn/ipnstate/ipnstate.go b/ipn/ipnstate/ipnstate.go index 332a4c629..68eb21078 100644 --- a/ipn/ipnstate/ipnstate.go +++ b/ipn/ipnstate/ipnstate.go @@ -173,6 +173,10 @@ type TailnetStatus struct { // Name is the name of the network that's currently in use. Name string + // StableID is the stable, unique identifier of the tailnet, as used to + // identify the tailnet in the Tailscale API. + StableID tailcfg.StableTailnetID + // MagicDNSSuffix is the network's MagicDNS suffix for nodes // in the network such as "userfoo.tailscale.net". // There are no surrounding dots. diff --git a/tailcfg/tailcfg.go b/tailcfg/tailcfg.go index c18db425e..30927b219 100644 --- a/tailcfg/tailcfg.go +++ b/tailcfg/tailcfg.go @@ -196,7 +196,8 @@ type CapabilityVersion int // - 145: 2026-08-04: Client understands [NodeAttrScopeQuad100OnMacOS] // - 146: 2026-09-02: Client understands [NodeAttrConnReject]; can handle C2N /debug/rejects. // - 147: 2026-09-09: Client handles 429/503 responses with retry-after headers to /machine/ endpoints -const CurrentCapabilityVersion CapabilityVersion = 147 +// - 148: 2026-09-15: Client understands [Node.StableTailnetID] +const CurrentCapabilityVersion CapabilityVersion = 148 // ID is an integer ID for a user, node, or login allocated by the // control plane. @@ -261,6 +262,16 @@ func (u StableNodeID) IsZero() bool { return u == "" } +// StableTailnetID is the stable and opaque identifier of the tailnet this node +// is a member of, as used to identify the tailnet in the Tailscale API. These +// IDs are guaranteed to be unique across all tailnets for a single control +// server instance, but may or may not be unique across different servers. +type StableTailnetID string + +func (id StableTailnetID) IsZero() bool { + return id == "" +} + // User is a Tailscale user. // // A user can have multiple logins associated with it (e.g. gmail and github oauth). @@ -543,6 +554,13 @@ type Node struct { // ExitNodeDNSResolvers is the list of DNS servers that should be used when this // node is marked IsWireGuardOnly and being used as an exit node. ExitNodeDNSResolvers []*dnstype.Resolver `json:",omitempty"` + + // StableTailnetID is the identifier of the tailnet this node is a + // member of. + // + // Control only populates this for the self node in a MapResponse + // (MapResponse.Node); it is empty for peers. + StableTailnetID StableTailnetID `json:",omitzero"` } // HasCap reports whether the node has the given capability. @@ -2415,7 +2433,8 @@ func (n *Node) Equal(n2 *Node) bool { eqPtr(n.SelfNodeV4MasqAddrForThisPeer, n2.SelfNodeV4MasqAddrForThisPeer) && eqPtr(n.SelfNodeV6MasqAddrForThisPeer, n2.SelfNodeV6MasqAddrForThisPeer) && n.IsWireGuardOnly == n2.IsWireGuardOnly && - n.IsJailed == n2.IsJailed + n.IsJailed == n2.IsJailed && + n.StableTailnetID == n2.StableTailnetID } func eqPtr[T comparable](a, b *T) bool { diff --git a/tailcfg/tailcfg_clone.go b/tailcfg/tailcfg_clone.go index 966aa1ecc..3efbc0aac 100644 --- a/tailcfg/tailcfg_clone.go +++ b/tailcfg/tailcfg_clone.go @@ -122,6 +122,7 @@ var _NodeCloneNeedsRegeneration = Node(struct { IsWireGuardOnly bool IsJailed bool ExitNodeDNSResolvers []*dnstype.Resolver + StableTailnetID StableTailnetID }{}) // Clone makes a deep copy of Hostinfo. diff --git a/tailcfg/tailcfg_test.go b/tailcfg/tailcfg_test.go index 680f6b933..ddb502b7e 100644 --- a/tailcfg/tailcfg_test.go +++ b/tailcfg/tailcfg_test.go @@ -350,6 +350,7 @@ func TestNodeEqual(t *testing.T) { "ComputedName", "computedHostIfDifferent", "ComputedNameWithHost", "DataPlaneAuditLogID", "Expired", "SelfNodeV4MasqAddrForThisPeer", "SelfNodeV6MasqAddrForThisPeer", "IsWireGuardOnly", "IsJailed", "ExitNodeDNSResolvers", + "StableTailnetID", } if have := fieldsOf(reflect.TypeFor[Node]()); !reflect.DeepEqual(have, nodeHandles) { t.Errorf("Node.Equal check might be out of sync\nfields: %q\nhandled: %q\n", @@ -404,6 +405,21 @@ func TestNodeEqual(t *testing.T) { &Node{StableID: "node-abcd"}, true, }, + { + &Node{StableTailnetID: "tailnet-abcd"}, + &Node{}, + false, + }, + { + &Node{StableTailnetID: "tailnet-abcd"}, + &Node{StableTailnetID: "tailnet-efgh"}, + false, + }, + { + &Node{StableTailnetID: "tailnet-abcd"}, + &Node{StableTailnetID: "tailnet-abcd"}, + true, + }, { &Node{User: 0}, &Node{User: 1}, diff --git a/tailcfg/tailcfg_view.go b/tailcfg/tailcfg_view.go index f4d80cacc..0b8384828 100644 --- a/tailcfg/tailcfg_view.go +++ b/tailcfg/tailcfg_view.go @@ -376,7 +376,14 @@ func (v NodeView) IsJailed() bool { return v.ж.IsJailed } func (v NodeView) ExitNodeDNSResolvers() views.SliceView[*dnstype.Resolver, dnstype.ResolverView] { return views.SliceOfViews[*dnstype.Resolver, dnstype.ResolverView](v.ж.ExitNodeDNSResolvers) } -func (v NodeView) Equal(v2 NodeView) bool { return v.ж.Equal(v2.ж) } + +// StableTailnetID is the identifier of the tailnet this node is a +// member of. +// +// Control only populates this for the self node in a MapResponse +// (MapResponse.Node); it is empty for peers. +func (v NodeView) StableTailnetID() StableTailnetID { return v.ж.StableTailnetID } +func (v NodeView) Equal(v2 NodeView) bool { return v.ж.Equal(v2.ж) } // A compilation failure here means this code must be regenerated, with the command at the top of this file. var _NodeViewNeedsRegeneration = Node(struct { @@ -416,6 +423,7 @@ var _NodeViewNeedsRegeneration = Node(struct { IsWireGuardOnly bool IsJailed bool ExitNodeDNSResolvers []*dnstype.Resolver + StableTailnetID StableTailnetID }{}) // View returns a read-only view of Hostinfo. diff --git a/types/netmap/netmap.go b/types/netmap/netmap.go index 604a5d363..f5124f2c6 100644 --- a/types/netmap/netmap.go +++ b/types/netmap/netmap.go @@ -297,6 +297,16 @@ func (nm *NetworkMap) DomainName() string { return nm.Domain } +// StableTailnetID returns the stable ID of the tailnet the current node is a +// member of, as sent by control on the self node. It returns the empty string +// if nm is nil or nm.SelfNode is invalid. +func (nm *NetworkMap) StableTailnetID() tailcfg.StableTailnetID { + if nm == nil || !nm.SelfNode.Valid() { + return "" + } + return nm.SelfNode.StableTailnetID() +} + // TailnetDisplayName returns the admin-editable name contained in // NodeAttrTailnetDisplayName. If the capability is not present it // returns an empty string. diff --git a/types/netmap/netmap_test.go b/types/netmap/netmap_test.go index e68b243b3..e759c41d6 100644 --- a/types/netmap/netmap_test.go +++ b/types/netmap/netmap_test.go @@ -52,6 +52,34 @@ func eps(s ...string) []netip.AddrPort { return eps } +func TestStableTailnetID(t *testing.T) { + for _, tt := range []struct { + name string + nm *NetworkMap + want tailcfg.StableTailnetID + }{ + {name: "nil_map"}, + {name: "invalid_self_node", nm: &NetworkMap{}}, + { + name: "missing_id", + nm: &NetworkMap{SelfNode: (&tailcfg.Node{}).View()}, + }, + { + name: "populated", + nm: &NetworkMap{ + SelfNode: (&tailcfg.Node{StableTailnetID: "tailnet-abcd"}).View(), + }, + want: "tailnet-abcd", + }, + } { + t.Run(tt.name, func(t *testing.T) { + if got := tt.nm.StableTailnetID(); got != tt.want { + t.Errorf("StableTailnetID() = %q; want %q", got, tt.want) + } + }) + } +} + func TestNetworkMapConcise(t *testing.T) { for _, tt := range []struct { name string