diff --git a/api.go b/api.go index 960217dd..575ac8a7 100644 --- a/api.go +++ b/api.go @@ -65,7 +65,7 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { mKey, err := wgkey.ParseHex(mKeyStr) if err != nil { log.Error(). - Str("Handler", "Registration"). + Str("handler", "Registration"). Err(err). Msg("Cannot parse machine key") c.String(http.StatusInternalServerError, "Sad!") @@ -75,7 +75,7 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { err = decode(body, &req, &mKey, h.privateKey) if err != nil { log.Error(). - Str("Handler", "Registration"). + Str("handler", "Registration"). Err(err). Msg("Cannot decode message") c.String(http.StatusInternalServerError, "Very sad!") @@ -84,7 +84,7 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { var m Machine if result := h.db.Preload("Namespace").First(&m, "machine_key = ?", mKey.HexString()); errors.Is(result.Error, gorm.ErrRecordNotFound) { - log.Info().Str("Name", req.Hostinfo.Hostname).Msg("New machine") + log.Info().Str("machine", req.Hostinfo.Hostname).Msg("New machine") m = Machine{ Expiry: &req.Expiry, MachineKey: mKey.HexString(), @@ -93,7 +93,7 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { } if err := h.db.Create(&m).Error; err != nil { log.Error(). - Str("Handler", "Registration"). + Str("handler", "Registration"). Err(err). Msg("Could not create row") return @@ -111,8 +111,8 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { if m.NodeKey == wgkey.Key(req.NodeKey).HexString() { if m.Registered { log.Debug(). - Str("Handler", "Registration"). - Str("Machine", m.Name). + Str("handler", "Registration"). + Str("machine", m.Name). Msg("Client is registered and we have the current NodeKey. All clear to /mSending keepaliveap") resp.AuthURL = "" @@ -121,7 +121,7 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { respBody, err := encode(resp, &mKey, h.privateKey) if err != nil { log.Error(). - Str("Handler", "Registration"). + Str("handler", "Registration"). Err(err). Msg("Cannot encode message") c.String(http.StatusInternalServerError, "") @@ -132,15 +132,15 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { } log.Debug(). - Str("Handler", "Registration"). - Str("Machine", m.Name). + Str("handler", "Registration"). + Str("machine", m.Name). Msg("Not registered and not NodeKey rotation. Sending a authurl to register") resp.AuthURL = fmt.Sprintf("%s/register?key=%s", h.cfg.ServerURL, mKey.HexString()) respBody, err := encode(resp, &mKey, h.privateKey) if err != nil { log.Error(). - Str("Handler", "Registration"). + Str("handler", "Registration"). Err(err). Msg("Cannot encode message") c.String(http.StatusInternalServerError, "") @@ -153,8 +153,8 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { // The NodeKey we have matches OldNodeKey, which means this is a refresh after an key expiration if m.NodeKey == wgkey.Key(req.OldNodeKey).HexString() { log.Debug(). - Str("Handler", "Registration"). - Str("Machine", m.Name). + Str("handler", "Registration"). + Str("machine", m.Name). Msg("We have the OldNodeKey in the database. This is a key refresh") m.NodeKey = wgkey.Key(req.NodeKey).HexString() h.db.Save(&m) @@ -164,7 +164,7 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { respBody, err := encode(resp, &mKey, h.privateKey) if err != nil { log.Error(). - Str("Handler", "Registration"). + Str("handler", "Registration"). Err(err). Msg("Cannot encode message") c.String(http.StatusInternalServerError, "Extremely sad!") @@ -178,8 +178,8 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { // when headscale is stopped in the middle of the auth process. if m.Registered { log.Debug(). - Str("Handler", "Registration"). - Str("Machine", m.Name). + Str("handler", "Registration"). + Str("machine", m.Name). Msg("The node is sending us a new NodeKey, but machine is registered. All clear for /map") resp.AuthURL = "" resp.MachineAuthorized = true @@ -187,7 +187,7 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { respBody, err := encode(resp, &mKey, h.privateKey) if err != nil { log.Error(). - Str("Handler", "Registration"). + Str("handler", "Registration"). Err(err). Msg("Cannot encode message") c.String(http.StatusInternalServerError, "") @@ -198,15 +198,15 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { } log.Debug(). - Str("Handler", "Registration"). - Str("Machine", m.Name). + Str("handler", "Registration"). + Str("machine", m.Name). Msg("The node is sending us a new NodeKey, sending auth url") resp.AuthURL = fmt.Sprintf("%s/register?key=%s", h.cfg.ServerURL, mKey.HexString()) respBody, err := encode(resp, &mKey, h.privateKey) if err != nil { log.Error(). - Str("Handler", "Registration"). + Str("handler", "Registration"). Err(err). Msg("Cannot encode message") c.String(http.StatusInternalServerError, "") @@ -230,7 +230,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) { mKey, err := wgkey.ParseHex(mKeyStr) if err != nil { log.Error(). - Str("Handler", "PollNetMap"). + Str("handler", "PollNetMap"). Err(err). Msg("Cannot parse client key") c.String(http.StatusBadRequest, "") @@ -240,7 +240,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) { err = decode(body, &req, &mKey, h.privateKey) if err != nil { log.Error(). - Str("Handler", "PollNetMap"). + Str("handler", "PollNetMap"). Err(err). Msg("Cannot decode message") c.String(http.StatusBadRequest, "") @@ -250,7 +250,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) { var m Machine if result := h.db.Preload("Namespace").First(&m, "machine_key = ?", mKey.HexString()); errors.Is(result.Error, gorm.ErrRecordNotFound) { log.Warn(). - Str("Handler", "PollNetMap"). + Str("handler", "PollNetMap"). Msgf("Ignoring request, cannot find machine with key %s", mKey.HexString()) c.String(http.StatusUnauthorized, "") return @@ -298,49 +298,49 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) { // Details on the protocol can be found in https://github.com/tailscale/tailscale/blob/main/tailcfg/tailcfg.go#L696 log.Debug(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). - Bool("ReadOnly", req.ReadOnly). - Bool("OmitPeers", req.OmitPeers). - Bool("Stream", req.Stream) + Str("handler", "PollNetMap"). + Str("machine", m.Name). + Bool("readOnly", req.ReadOnly). + Bool("omitPeers", req.OmitPeers). + Bool("stream", req.Stream) if req.ReadOnly { log.Info(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). Msg("Client is starting up. Asking for DERP map") c.Data(200, "application/json; charset=utf-8", *data) return } if req.OmitPeers && !req.Stream { log.Info(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). Msg("Client sent endpoint update and is ok with a response without peer list") c.Data(200, "application/json; charset=utf-8", *data) return } else if req.OmitPeers && req.Stream { log.Warn(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). Msg("Ignoring request, don't know how to handle it") c.String(http.StatusBadRequest, "") return } log.Info(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). Msg("Client is ready to access the tailnet") log.Info(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). Msg("Sending initial map") pollData <- *data log.Info(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). Msg("Notifying peers") peers, _ := h.getPeers(m) h.pollMu.Lock() @@ -348,17 +348,17 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) { pUp, ok := h.clientsPolling[uint64(p.ID)] if ok { log.Info(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). - Str("Peer", m.Name). - Str("Address", p.Addresses[0].String()). + Str("handler", "PollNetMap"). + Str("machine", m.Name). + Str("peer", m.Name). + Str("address", p.Addresses[0].String()). Msgf("Notifying peer %s (%s)", p.Name, p.Addresses[0]) pUp <- []byte{} } else { log.Info(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). - Str("Peer", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). + Str("peer", m.Name). Msgf("Peer %s does not appear to be polling", p.Name) } } @@ -370,15 +370,15 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) { select { case data := <-pollData: log.Trace(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). - Int("Bytes", len(data)). + Str("handler", "PollNetMap"). + Str("machine", m.Name). + Int("bytes", len(data)). Msg("Sending data") _, err := w.Write(data) if err != nil { log.Error(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). Err(err). Msg("Cannot write data") } @@ -389,22 +389,22 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) { case <-update: log.Debug(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). Msg("Received a request for update") data, err := h.getMapResponse(mKey, req, m) if err != nil { log.Error(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). Err(err). Msg("Could not get the map update") } _, err = w.Write(*data) if err != nil { log.Error(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). Err(err). Msg("Could not write the map response") } @@ -412,8 +412,8 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) { case <-c.Request.Context().Done(): log.Info(). - Str("Handler", "PollNetMap"). - Str("Machine", m.Name). + Str("handler", "PollNetMap"). + Str("machine", m.Name). Msg("The client has closed the connection") now := time.Now().UTC() m.LastSeen = &now @@ -440,14 +440,14 @@ func (h *Headscale) keepAlive(cancel chan []byte, pollData chan []byte, mKey wgk data, err := h.getMapKeepAliveResponse(mKey, req, m) if err != nil { log.Error(). - Str("Func", "keepAlive"). + Str("func", "keepAlive"). Err(err). Msg("Error generating the keep alive msg") return } log.Debug(). - Str("Func", "keepAlive"). - Str("Machine", m.Name). + Str("func", "keepAlive"). + Str("machine", m.Name). Msg("Sending keepalive") pollData <- *data h.pollMu.Unlock() @@ -460,7 +460,7 @@ func (h *Headscale) getMapResponse(mKey wgkey.Key, req tailcfg.MapRequest, m Mac node, err := m.toNode() if err != nil { log.Error(). - Str("Func", "getMapResponse"). + Str("func", "getMapResponse"). Err(err). Msg("Cannot convert to node") return nil, err @@ -468,7 +468,7 @@ func (h *Headscale) getMapResponse(mKey wgkey.Key, req tailcfg.MapRequest, m Mac peers, err := h.getPeers(m) if err != nil { log.Error(). - Str("Func", "getMapResponse"). + Str("func", "getMapResponse"). Err(err). Msg("Cannot fetch peers") return nil, err @@ -543,8 +543,8 @@ func (h *Headscale) getMapKeepAliveResponse(mKey wgkey.Key, req tailcfg.MapReque func (h *Headscale) handleAuthKey(c *gin.Context, db *gorm.DB, idKey wgkey.Key, req tailcfg.RegisterRequest, m Machine) { log.Debug(). - Str("Func", "handleAuthKey"). - Str("Machine", req.Hostinfo.Hostname). + Str("func", "handleAuthKey"). + Str("machine", req.Hostinfo.Hostname). Msgf("Processing auth key for %s", req.Hostinfo.Hostname) resp := tailcfg.RegisterResponse{} pak, err := h.checkKeyValidity(req.Auth.AuthKey) @@ -553,8 +553,8 @@ func (h *Headscale) handleAuthKey(c *gin.Context, db *gorm.DB, idKey wgkey.Key, respBody, err := encode(resp, &idKey, h.privateKey) if err != nil { log.Error(). - Str("Func", "handleAuthKey"). - Str("Machine", m.Name). + Str("func", "handleAuthKey"). + Str("machine", m.Name). Err(err). Msg("Cannot encode message") c.String(http.StatusInternalServerError, "") @@ -562,28 +562,28 @@ func (h *Headscale) handleAuthKey(c *gin.Context, db *gorm.DB, idKey wgkey.Key, } c.Data(200, "application/json; charset=utf-8", respBody) log.Error(). - Str("Func", "handleAuthKey"). - Str("Machine", m.Name). + Str("func", "handleAuthKey"). + Str("machine", m.Name). Msg("Failed authentication via AuthKey") return } log.Debug(). - Str("Func", "handleAuthKey"). - Str("Machine", m.Name). + Str("func", "handleAuthKey"). + Str("machine", m.Name). Msg("Authentication key was valid, proceeding to acquire an IP address") ip, err := h.getAvailableIP() if err != nil { log.Error(). - Str("Func", "handleAuthKey"). - Str("Machine", m.Name). + Str("func", "handleAuthKey"). + Str("machine", m.Name). Msg("Failed to find an available IP") return } log.Info(). - Str("Func", "handleAuthKey"). - Str("Machine", m.Name). - Str("IP", ip.String()). + Str("func", "handleAuthKey"). + Str("machine", m.Name). + Str("ip", ip.String()). Msgf("Assining %s to %s", ip, m.Name) m.AuthKeyID = uint(pak.ID) @@ -599,8 +599,8 @@ func (h *Headscale) handleAuthKey(c *gin.Context, db *gorm.DB, idKey wgkey.Key, respBody, err := encode(resp, &idKey, h.privateKey) if err != nil { log.Error(). - Str("Func", "handleAuthKey"). - Str("Machine", m.Name). + Str("func", "handleAuthKey"). + Str("machine", m.Name). Err(err). Msg("Cannot encode message") c.String(http.StatusInternalServerError, "Extremely sad!") @@ -608,8 +608,8 @@ func (h *Headscale) handleAuthKey(c *gin.Context, db *gorm.DB, idKey wgkey.Key, } c.Data(200, "application/json; charset=utf-8", respBody) log.Info(). - Str("Func", "handleAuthKey"). - Str("Machine", m.Name). - Str("IP", ip.String()). + Str("func", "handleAuthKey"). + Str("machine", m.Name). + Str("ip", ip.String()). Msg("Successfully authenticated via AuthKey") } diff --git a/app.go b/app.go index 012ab9ea..45df01c0 100644 --- a/app.go +++ b/app.go @@ -127,15 +127,15 @@ func (h *Headscale) expireEphemeralNodesWorker() { for _, ns := range *namespaces { machines, err := h.ListMachinesInNamespace(ns.Name) if err != nil { - log.Error().Err(err).Str("Namespace", ns.Name).Msg("Error listing machines in namespace") + log.Error().Err(err).Str("namespace", ns.Name).Msg("Error listing machines in namespace") return } for _, m := range *machines { if m.AuthKey != nil && m.LastSeen != nil && m.AuthKey.Ephemeral && time.Now().After(m.LastSeen.Add(h.cfg.EphemeralNodeInactivityTimeout)) { - log.Info().Str("Machine", m.Name).Msg("Ephemeral client removed from database") + log.Info().Str("machine", m.Name).Msg("Ephemeral client removed from database") err = h.db.Unscoped().Delete(m).Error if err != nil { - log.Error().Err(err).Str("Name", m.Name).Msg("🤮 Cannot delete ephemeral machine from the database") + log.Error().Err(err).Str("machine", m.Name).Msg("🤮 Cannot delete ephemeral machine from the database") } } } diff --git a/cmd/headscale/cli/utils.go b/cmd/headscale/cli/utils.go index d104f044..4ada6408 100644 --- a/cmd/headscale/cli/utils.go +++ b/cmd/headscale/cli/utils.go @@ -89,7 +89,7 @@ func getHeadscaleApp() (*headscale.Headscale, error) { derpMap, err := loadDerpMap(derpPath) if err != nil { log.Error(). - Str("Path", derpPath). + Str("path", derpPath). Err(err). Msg("Could not load DERP servers map file") } @@ -140,7 +140,7 @@ func getHeadscaleApp() (*headscale.Headscale, error) { err = h.LoadACLPolicy(aclPath) if err != nil { log.Error(). - Str("Path", aclPath). + Str("path", aclPath). Err(err). Msg("Could not load the ACL policy") } diff --git a/namespaces.go b/namespaces.go index 520cbe55..9bbb6b32 100644 --- a/namespaces.go +++ b/namespaces.go @@ -34,7 +34,7 @@ func (h *Headscale) CreateNamespace(name string) (*Namespace, error) { n.Name = name if err := h.db.Create(&n).Error; err != nil { log.Error(). - Str("Func", "CreateNamespace"). + Str("func", "CreateNamespace"). Err(err). Msg("Could not create row") return nil, err @@ -137,7 +137,7 @@ func (h *Headscale) RequestMapUpdates(namespaceID uint) error { data, err := json.Marshal(names) if err != nil { log.Error(). - Str("Func", "RequestMapUpdates"). + Str("func", "RequestMapUpdates"). Err(err). Msg("Could not marshal namespaces_pending_updates") return err @@ -161,8 +161,8 @@ func (h *Headscale) checkForNamespacesPendingUpdates() { } for _, name := range names { log.Trace(). - Str("Func", "RequestMapUpdates"). - Str("Machine", name). + Str("func", "RequestMapUpdates"). + Str("machine", name). Msg("Sending updates to nodes in namespace") machines, err := h.ListMachinesInNamespace(name) if err != nil { @@ -175,17 +175,17 @@ func (h *Headscale) checkForNamespacesPendingUpdates() { pUp, ok := h.clientsPolling[uint64(p.ID)] if ok { log.Info(). - Str("Func", "checkForNamespacesPendingUpdates"). - Str("Machine", m.Name). - Str("Peer", m.Name). - Str("Address", p.Addresses[0].String()). + Str("func", "checkForNamespacesPendingUpdates"). + Str("machine", m.Name). + Str("peer", m.Name). + Str("address", p.Addresses[0].String()). Msgf("Notifying peer %s (%s)", p.Name, p.Addresses[0]) pUp <- []byte{} } else { log.Info(). - Str("Func", "checkForNamespacesPendingUpdates"). - Str("Machine", m.Name). - Str("Peer", m.Name). + Str("func", "checkForNamespacesPendingUpdates"). + Str("machine", m.Name). + Str("peer", m.Name). Msgf("Peer %s does not appear to be polling", p.Name) } } @@ -200,7 +200,7 @@ func (h *Headscale) checkForNamespacesPendingUpdates() { err = h.setValue("namespaces_pending_updates", "") if err != nil { log.Error(). - Str("Func", "checkForNamespacesPendingUpdates"). + Str("func", "checkForNamespacesPendingUpdates"). Err(err). Msg("Could not save to KV") return