ipn/ipnlocal: add a C2N endpoint for fetching a netmap

For debugging purposes, add a new C2N endpoint returning the current
netmap. Optionally, coordination server can send a new "candidate" map
response, which the client will generate a separate netmap for.
Coordination server can later compare two netmaps, detecting unexpected
changes to the client state.

Updates tailscale/corp#32095

Signed-off-by: Anton Tolchanov <anton@tailscale.com>
This commit is contained in:
Anton Tolchanov
2025-08-13 15:00:35 +01:00
committed by Anton Tolchanov
parent 394718a4ca
commit 4a04161828
9 changed files with 506 additions and 9 deletions
+183
View File
@@ -4,9 +4,11 @@
package ipnlocal
import (
"bytes"
"cmp"
"crypto/x509"
"encoding/json"
"fmt"
"net/http/httptest"
"net/url"
"os"
@@ -18,8 +20,15 @@ import (
"tailscale.com/ipn/store/mem"
"tailscale.com/tailcfg"
"tailscale.com/tstest"
"tailscale.com/types/key"
"tailscale.com/types/logger"
"tailscale.com/types/netmap"
"tailscale.com/types/opt"
"tailscale.com/types/views"
"tailscale.com/util/must"
gcmp "github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestHandleC2NTLSCertStatus(t *testing.T) {
@@ -132,3 +141,177 @@ func TestHandleC2NTLSCertStatus(t *testing.T) {
}
}
// reflectNonzero returns a non-zero value for a given reflect.Value.
func reflectNonzero(t reflect.Type) reflect.Value {
switch t.Kind() {
case reflect.Bool:
return reflect.ValueOf(true)
case reflect.String:
if reflect.TypeFor[opt.Bool]() == t {
return reflect.ValueOf("true").Convert(t)
}
return reflect.ValueOf("foo").Convert(t)
case reflect.Int64:
return reflect.ValueOf(int64(1)).Convert(t)
case reflect.Slice:
return reflect.MakeSlice(t, 1, 1)
case reflect.Ptr:
return reflect.New(t.Elem())
case reflect.Map:
return reflect.MakeMap(t)
case reflect.Struct:
switch t {
case reflect.TypeFor[key.NodePrivate]():
return reflect.ValueOf(key.NewNode())
}
}
panic(fmt.Sprintf("unhandled %v", t))
}
// setFieldsToRedact sets fields in the given netmap to non-zero values
// according to the fieldMap, which maps field names to whether they
// should be reset (true) or not (false).
func setFieldsToRedact(t *testing.T, nm *netmap.NetworkMap, fieldMap map[string]bool) {
t.Helper()
v := reflect.ValueOf(nm).Elem()
for i := range v.NumField() {
name := v.Type().Field(i).Name
f := v.Field(i)
if !f.CanSet() {
continue
}
shouldReset, ok := fieldMap[name]
if !ok {
t.Errorf("fieldMap missing field %q", name)
}
if shouldReset {
f.Set(reflectNonzero(f.Type()))
}
}
}
func TestRedactNetmapPrivateKeys(t *testing.T) {
fieldMap := map[string]bool{
// Private fields (should be redacted):
"PrivateKey": true,
// Public fields (should not be redacted):
"AllCaps": false,
"CollectServices": false,
"DERPMap": false,
"DNS": false,
"DisplayMessages": false,
"Domain": false,
"DomainAuditLogID": false,
"Expiry": false,
"MachineKey": false,
"Name": false,
"NodeKey": false,
"PacketFilter": false,
"PacketFilterRules": false,
"Peers": false,
"SSHPolicy": false,
"SelfNode": false,
"TKAEnabled": false,
"TKAHead": false,
"UserProfiles": false,
}
nm := &netmap.NetworkMap{}
setFieldsToRedact(t, nm, fieldMap)
got, _ := redactNetmapPrivateKeys(nm)
if !reflect.DeepEqual(got, &netmap.NetworkMap{}) {
t.Errorf("redacted netmap is not empty: %+v", got)
}
}
func TestHandleC2NDebugNetmap(t *testing.T) {
nm := &netmap.NetworkMap{
Name: "myhost",
SelfNode: (&tailcfg.Node{
ID: 100,
Name: "myhost",
StableID: "deadbeef",
Key: key.NewNode().Public(),
Hostinfo: (&tailcfg.Hostinfo{Hostname: "myhost"}).View(),
}).View(),
Peers: []tailcfg.NodeView{
(&tailcfg.Node{
ID: 101,
Name: "peer1",
StableID: "deadbeef",
Key: key.NewNode().Public(),
Hostinfo: (&tailcfg.Hostinfo{Hostname: "peer1"}).View(),
}).View(),
},
PrivateKey: key.NewNode(),
}
withoutPrivateKey := *nm
withoutPrivateKey.PrivateKey = key.NodePrivate{}
for _, tt := range []struct {
name string
req *tailcfg.C2NDebugNetmapRequest
want *netmap.NetworkMap
}{
{
name: "simple_get",
want: &withoutPrivateKey,
},
{
name: "post_no_omit",
req: &tailcfg.C2NDebugNetmapRequest{},
want: &withoutPrivateKey,
},
{
name: "post_omit_peers_and_name",
req: &tailcfg.C2NDebugNetmapRequest{OmitFields: []string{"Peers", "Name"}},
want: &netmap.NetworkMap{
SelfNode: nm.SelfNode,
},
},
{
name: "post_omit_nonexistent_field",
req: &tailcfg.C2NDebugNetmapRequest{OmitFields: []string{"ThisFieldDoesNotExist"}},
want: &withoutPrivateKey,
},
} {
t.Run(tt.name, func(t *testing.T) {
b := newTestLocalBackend(t)
b.currentNode().SetNetMap(nm)
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/debug/netmap", nil)
if tt.req != nil {
b, err := json.Marshal(tt.req)
if err != nil {
t.Fatalf("json.Marshal: %v", err)
}
req = httptest.NewRequest("POST", "/debug/netmap", bytes.NewReader(b))
}
handleC2NDebugNetMap(b, rec, req)
res := rec.Result()
wantStatus := 200
if res.StatusCode != wantStatus {
t.Fatalf("status code = %v; want %v. Body: %s", res.Status, wantStatus, rec.Body.Bytes())
}
var resp tailcfg.C2NDebugNetmapResponse
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("bad JSON: %v", err)
}
got := &netmap.NetworkMap{}
if err := json.Unmarshal(resp.Current, got); err != nil {
t.Fatalf("bad JSON: %v", err)
}
if diff := gcmp.Diff(tt.want, got,
gcmp.AllowUnexported(netmap.NetworkMap{}, key.NodePublic{}, views.Slice[tailcfg.FilterRule]{}),
cmpopts.EquateComparable(key.MachinePublic{}),
); diff != "" {
t.Errorf("netmap mismatch (-want +got):\n%s", diff)
}
})
}
}