tsnet: add Server.Tailnet to require a tailnet

Server.Tailnet is a tailcfg.StableTailnetID. It is sent as a required
tailnet at registration, and Up also fails if the node ends up in a
different tailnet, such as from state saved before Tailnet was set.

Fixes #22515
Updates #22093

Change-Id: I44d4e8fb4e5767fa63dce871c3e093f586a7d300
Signed-off-by: Brad Fitzpatrick <[email protected]>
This commit is contained in:
Brad Fitzpatrick authored and Brad Fitzpatrick committed 2026-11-24 12:04:53 -08:00
1 parent 076d07d626
commit 67e092fe7d
3 files changed
+113

No files matched your search

+38
View File
@@ -352,6 +352,19 @@ type Server struct {
// that the control server will allow the node to adopt that tag.
AdvertiseTags []string
// Tailnet, if non-empty, is the only tailnet this Server may join.
// It is the tailnet's stable ID, as shown in the admin console and
// reported in [ipnstate.TailnetStatus.StableID].
//
// When the node registers, the control server refuses a login to any
// other tailnet. [Server.Up] also returns an error if the node is
// already logged in to a different tailnet, such as from state saved
// before Tailnet was set, or if the control server does not report
// which tailnet the node is in.
//
// This field must be set before calling Start.
Tailnet tailcfg.StableTailnetID
// Tun, if non-nil, specifies a custom tun.Device to use for packet I/O.
//
// This field must be set before calling Start.
@@ -623,6 +636,9 @@ func (s *Server) Up(ctx context.Context) (*ipnstate.Status, error) {
if len(status.TailscaleIPs) == 0 {
return nil, errors.New("tsnet.Up: running, but no ip")
}
if err := s.checkTailnet(status); err != nil {
return nil, fmt.Errorf("tsnet.Up: %w", err)
}
// The first time Up is run, clear the persisted serve config
// and Service advertisements. We do this to prevent messy
@@ -661,6 +677,25 @@ func (s *Server) Up(ctx context.Context) (*ipnstate.Status, error) {
}
}
// checkTailnet returns an error if [Server.Tailnet] is set and st does
// not show the node in that tailnet.
func (s *Server) checkTailnet(st *ipnstate.Status) error {
if s.Tailnet == "" {
return nil
}
var got tailcfg.StableTailnetID
if st.CurrentTailnet != nil {
got = st.CurrentTailnet.StableID
}
if got == "" {
return fmt.Errorf("control server did not report a tailnet ID; want tailnet %q", s.Tailnet)
}
if got != s.Tailnet {
return fmt.Errorf("node is in tailnet %q; want tailnet %q", got, s.Tailnet)
}
return nil
}
// Close stops the server.
//
// It must not be called before or concurrently with Start.
@@ -1030,6 +1065,9 @@ func (s *Server) start() (reterr error) {
prefs.ControlURL = s.getControlURL()
prefs.RunWebClient = s.RunWebClient
prefs.AdvertiseTags = s.AdvertiseTags
if s.Tailnet != "" {
prefs.Tailnet = "required:" + string(s.Tailnet)
}
authKey, err := s.resolveAuthKey()
if err != nil {
return fmt.Errorf("error resolving auth key: %w", err)
+55
View File
@@ -4245,3 +4245,58 @@ func TestHTTPClientDefaultTransport(t *testing.T) {
}
}
}
func TestTailnet(t *testing.T) {
controlURL, control := startControl(t)
control.TailnetID = "T1"
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
newServer := func(hostname, dir string, tailnet tailcfg.StableTailnetID) *Server {
s := &Server{
Dir: dir,
ControlURL: controlURL,
Hostname: hostname,
Tailnet: tailnet,
Logf: tstest.WhileTestRunningLogger(t),
}
t.Cleanup(func() { s.Close() })
return s
}
t.Run("match", func(t *testing.T) {
st, err := newServer("s1", t.TempDir(), "T1").Up(ctx)
if err != nil {
t.Fatalf("Up: %v", err)
}
if got := st.CurrentTailnet.StableID; got != "T1" {
t.Errorf("tailnet = %q; want T1", got)
}
})
t.Run("register_refused", func(t *testing.T) {
_, err := newServer("s2", t.TempDir(), "T2").Up(ctx)
if err == nil || !strings.Contains(err.Error(), "T2") {
t.Fatalf("Up = %v; want a registration error naming T2", err)
}
})
t.Run("saved_state_other_tailnet", func(t *testing.T) {
// Log in with no Tailnet set, then restart from the same state
// asking for a different tailnet. A node that is already logged
// in does not register again, so only the check in Up catches
// this.
dir := t.TempDir()
s := newServer("s3", dir, "")
if _, err := s.Up(ctx); err != nil {
t.Fatalf("first Up: %v", err)
}
s.Close()
_, err := newServer("s3", dir, "T2").Up(ctx)
if err == nil || !strings.Contains(err.Error(), `node is in tailnet "T1"`) {
t.Fatalf("second Up = %v; want a tailnet mismatch error from Up", err)
}
})
}
@@ -69,6 +69,13 @@ type Server struct {
// grants rules.
PeerRelayGrants bool
// TailnetID, if non-empty, is set as [tailcfg.Node.StableTailnetID] on
// newly registered nodes. A RegisterRequest for a new node whose
// Tailnet is "required:" followed by a list that does not contain
// TailnetID is rejected. Requests for nodes that are already
// registered are not checked.
TailnetID tailcfg.StableTailnetID
// SSHPolicy, if non-nil, is sent to every node in MapResponses.
// Each node also gets [tailcfg.CapabilitySSH] added to its capability
// map, permitting "tailscale up --ssh".
@@ -1051,6 +1058,18 @@ func (s *Server) serveRegister(w http.ResponseWriter, r *http.Request, mkey key.
return
}
s.mu.Lock()
_, known := s.nodes[req.NodeKey]
s.mu.Unlock()
if want, ok := strings.CutPrefix(req.Tailnet, "required:"); ok && !known && !slices.Contains(strings.Split(want, ","), string(s.TailnetID)) {
res := must.Get(s.encode(false, tailcfg.RegisterResponse{
Error: fmt.Sprintf("tailnet %q is not one of the required tailnets %q", s.TailnetID, want),
}))
w.WriteHeader(200)
w.Write(res)
return
}
// If this is a followup request, wait until interactive followup URL visit complete.
isFollowup := req.Followup != ""
if isFollowup {
@@ -1138,6 +1157,7 @@ func (s *Server) serveRegister(w http.ResponseWriter, r *http.Request, mkey key.
Cap: req.Version,
CapMap: capMap,
Capabilities: slices.Collect(maps.Keys(capMap)),
StableTailnetID: s.TailnetID,
}
if s.TagOwners != nil && req.Hostinfo != nil {
// Trust the requested tags at signup; ownership checks