wgengine/netlog: include node OS in logged attributes (#17755)
Include the node's OS with network flow log information. Refactor the JSON-length computation to be a bit more precise. Updates tailscale/corp#33352 Fixes tailscale/corp#34030 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"tailscale.com/tailcfg"
|
||||
"tailscale.com/types/bools"
|
||||
"tailscale.com/types/netlogtype"
|
||||
"tailscale.com/util/set"
|
||||
)
|
||||
@@ -134,17 +135,31 @@ func compareConnCnts(x, y netlogtype.ConnectionCounts) int {
|
||||
}
|
||||
|
||||
// jsonLen computes an upper-bound on the size of the JSON representation.
|
||||
func (nu nodeUser) jsonLen() int {
|
||||
func (nu nodeUser) jsonLen() (n int) {
|
||||
if !nu.Valid() {
|
||||
return len(`{"nodeId":""}`)
|
||||
}
|
||||
n := netlogtype.MinNodeJSONSize + jsonQuotedLen(nu.Name())
|
||||
n += len(`{}`)
|
||||
n += len(`"nodeId":`) + jsonQuotedLen(string(nu.StableID())) + len(`,`)
|
||||
if len(nu.Name()) > 0 {
|
||||
n += len(`"name":`) + jsonQuotedLen(nu.Name()) + len(`,`)
|
||||
}
|
||||
if nu.Addresses().Len() > 0 {
|
||||
n += len(`"addresses":[]`)
|
||||
for _, addr := range nu.Addresses().All() {
|
||||
n += bools.IfElse(addr.Addr().Is4(), len(`"255.255.255.255"`), len(`"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"`)) + len(",")
|
||||
}
|
||||
}
|
||||
if nu.Hostinfo().Valid() && len(nu.Hostinfo().OS()) > 0 {
|
||||
n += len(`"os":`) + jsonQuotedLen(nu.Hostinfo().OS()) + len(`,`)
|
||||
}
|
||||
if nu.Tags().Len() > 0 {
|
||||
n += len(`"tags":[]`)
|
||||
for _, tag := range nu.Tags().All() {
|
||||
n += jsonQuotedLen(tag) + len(",")
|
||||
}
|
||||
} else if nu.user.Valid() && nu.user.ID() == nu.User() {
|
||||
n += jsonQuotedLen(nu.user.LoginName())
|
||||
} else if nu.user.Valid() && nu.user.ID() == nu.User() && len(nu.user.LoginName()) > 0 {
|
||||
n += len(`"user":`) + jsonQuotedLen(nu.user.LoginName()) + len(",")
|
||||
}
|
||||
return n
|
||||
}
|
||||
@@ -166,6 +181,9 @@ func (nu nodeUser) toNode() netlogtype.Node {
|
||||
}
|
||||
n.Addresses = []netip.Addr{ipv4, ipv6}
|
||||
n.Addresses = slices.DeleteFunc(n.Addresses, func(a netip.Addr) bool { return !a.IsValid() })
|
||||
if nu.Hostinfo().Valid() {
|
||||
n.OS = nu.Hostinfo().OS()
|
||||
}
|
||||
if nu.Tags().Len() > 0 {
|
||||
n.Tags = nu.Tags().AsSlice()
|
||||
slices.Sort(n.Tags)
|
||||
|
||||
@@ -190,6 +190,7 @@ func TestToNode(t *testing.T) {
|
||||
node: &tailcfg.Node{
|
||||
StableID: "n123456CNTL",
|
||||
Addresses: []netip.Prefix{prefix("100.1.2.3")},
|
||||
Hostinfo: (&tailcfg.Hostinfo{OS: "linux"}).View(),
|
||||
User: 12345,
|
||||
},
|
||||
user: &tailcfg.UserProfile{
|
||||
@@ -199,6 +200,7 @@ func TestToNode(t *testing.T) {
|
||||
want: netlogtype.Node{
|
||||
NodeID: "n123456CNTL",
|
||||
Addresses: []netip.Addr{addr("100.1.2.3")},
|
||||
OS: "linux",
|
||||
User: "user@domain",
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user