mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-20 16:51:18 -08:00
Control via nodecap.LogUploadAuth may notify the node that there is an authorization token to use when uploading logs. If specified, then the logtail.Logger will use that token when uploading. Unfortunately, there is a time-of-use and time-of-check discrepancy that complicates logging. When logs are written under the context of being for a particular node, we cannot store the token, as it may be invalid by the time that the logs are uploaded. Instead, we add Logger.SetAuthID to annotate subsequent log entries with an identifier for the token to later use. Only when we upload do we strip out the auth ID, lookup the auth token to use, and then perform the upload. We assume that control will notify the client of new tokens well before they expire. Fetching a refreshed token is not the job of the client. Updates tailscale/corp#39092 Signed-off-by: Joe Tsai <[email protected]>
141 lines
4.5 KiB
Go
141 lines
4.5 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package ipnlocal
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"sync"
|
|
|
|
"tailscale.com/feature/buildfeatures"
|
|
"tailscale.com/tailcfg"
|
|
"tailscale.com/tailcfg/nodecap"
|
|
"tailscale.com/types/logger"
|
|
"tailscale.com/types/netmap"
|
|
"tailscale.com/util/mak"
|
|
)
|
|
|
|
type logAuthID string
|
|
type logAuthToken string
|
|
|
|
// logAuthCache caches log upload auth tokens keyed by auth ID ("nodeid:<nodeID>").
|
|
// Tokens arrive proactively from control via [nodecap.LogUploadAuth]
|
|
// on netmap updates; the client does not refresh them itself.
|
|
//
|
|
// All methods are safe for concurrent use.
|
|
type logAuthCache struct {
|
|
logf logger.Logf
|
|
|
|
mu sync.Mutex
|
|
tokens map[logAuthID]logAuthToken
|
|
}
|
|
|
|
// storeToken stores token for authID.
|
|
func (c *logAuthCache) storeToken(id logAuthID, tok logAuthToken) {
|
|
if id == "" || tok == "" {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
mak.Set(&c.tokens, id, tok)
|
|
}
|
|
|
|
// loadToken returns the cached bearer token for authID, or "" if none.
|
|
// An empty result means the uploader proceeds unauthenticated (matching
|
|
// historical logtail behavior when no auth is available).
|
|
func (c *logAuthCache) loadToken(id logAuthID) logAuthToken {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
return c.tokens[id]
|
|
}
|
|
|
|
// updateLogUploadAuth applies netmap-driven log upload authentication
|
|
// to the main tailscaled [logtail.Logger] and updates the token cache.
|
|
//
|
|
// When nm is nil or lacks a self node, new log entries are stamped with an
|
|
// empty auth ID, but previously cached tokens are retained so buffered logs
|
|
// from the prior node can still upload with Authorization.
|
|
//
|
|
// When CapMap carries [nodecap.LogUploadAuth], the token is cached.
|
|
// Control is expected to push a refreshed token via netmap
|
|
// before the previous one expires.
|
|
//
|
|
// Even when a new auth token arrives as a concise netmap delta
|
|
// with just the self CapMap for [nodecap.LogUploadAuth],
|
|
// the LocalBackend still triggers a full netmap install.
|
|
// Note that peer deltas cannot carry this attribute.
|
|
//
|
|
// [LocalBackend.setNetMapLocked] caches the token via this method, then
|
|
// [stripLogUploadAuth] removes the capability before the map is installed.
|
|
// A token the log service rejects is unstuck by the service, not by falling
|
|
// back to an unauthenticated upload. See [tailscale.com/logtail.Logger.upload].
|
|
func (b *LocalBackend) updateLogUploadAuth(nm *netmap.NetworkMap) {
|
|
if !buildfeatures.HasLogTail {
|
|
return
|
|
}
|
|
|
|
lg := b.logUploader
|
|
if lg == nil || nm == nil || !nm.SelfNode.Valid() {
|
|
if lg != nil {
|
|
lg.SetAuthID("")
|
|
}
|
|
return
|
|
}
|
|
|
|
// Cache any token provided in the netmap CapMap.
|
|
authID := formatAuthID(nm.SelfNode.ID())
|
|
tokens, err := tailcfg.UnmarshalNodeCapViewJSON[string](nm.SelfNode.CapMap(), nodecap.LogUploadAuth)
|
|
if err != nil {
|
|
b.logf("logauth: CapMap %s: %v", nodecap.LogUploadAuth, err)
|
|
return
|
|
}
|
|
for _, tok := range tokens {
|
|
if tok != "" {
|
|
b.logAuth.storeToken(authID, logAuthToken(tok))
|
|
}
|
|
}
|
|
|
|
// Configure the logger to log under the context of the current node.
|
|
lg.SetAuthID(string(authID))
|
|
lg.RegisterAuthToken(string(authID), func() string {
|
|
return string(b.logAuth.loadToken(authID))
|
|
})
|
|
}
|
|
|
|
// formatAuthID returns the canonical auth ID for authenticated log uploads:
|
|
// "nodeid:<nodeID>". A node ID change implies a tailnet change, so the auth ID
|
|
// does not include a separate tailnet component.
|
|
func formatAuthID(nodeID tailcfg.NodeID) logAuthID {
|
|
return logAuthID(fmt.Sprintf("nodeid:%d", nodeID))
|
|
}
|
|
|
|
// stripLogUploadAuth removes [nodecap.LogUploadAuth] from nm.
|
|
//
|
|
// The capability value is a bearer token. Leaving it on the installed netmap
|
|
// publishes it through status JSON, WhoIs, SelfChange notifications, the
|
|
// netmap disk cache, and debug netmap dumps. Callers must cache the token
|
|
// (see [LocalBackend.updateLogUploadAuth]) before calling this.
|
|
//
|
|
// nm is updated in place. The self node and AllCaps set are cloned first so a
|
|
// control-client copy that aliases them keeps the original token.
|
|
func stripLogUploadAuth(nm *netmap.NetworkMap) {
|
|
if nm == nil || !nm.SelfNode.Valid() {
|
|
return
|
|
}
|
|
if !nm.SelfNode.CapMap().Contains(nodecap.LogUploadAuth) && !nm.AllCaps.Contains(nodecap.LogUploadAuth) {
|
|
return
|
|
}
|
|
self := nm.SelfNode.AsStruct()
|
|
delete(self.CapMap, nodecap.LogUploadAuth)
|
|
self.Capabilities = slices.DeleteFunc(self.Capabilities, func(c nodecap.Cap) bool {
|
|
return c == nodecap.LogUploadAuth
|
|
})
|
|
nm.SelfNode = self.View()
|
|
if nm.AllCaps.Contains(nodecap.LogUploadAuth) {
|
|
// AllCaps may alias the control client's cap set.
|
|
nm.AllCaps = nm.AllCaps.Clone()
|
|
nm.AllCaps.Delete(nodecap.LogUploadAuth)
|
|
}
|
|
}
|