mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-10-28 10:51:44 +01:00 
			
		
		
		
	Merge pull request #495 from appbricks/appbricks/main-bug-fix
Regression bug fix when re-authenticating machine with auth-key
This commit is contained in:
		
						commit
						0abfbdc18a
					
				
							
								
								
									
										77
									
								
								api.go
									
									
									
									
									
								
							
							
						
						
									
										77
									
								
								api.go
									
									
									
									
									
								
							@ -568,8 +568,13 @@ func (h *Headscale) handleAuthKey(
 | 
				
			|||||||
			Str("func", "handleAuthKey").
 | 
								Str("func", "handleAuthKey").
 | 
				
			||||||
			Str("machine", registerRequest.Hostinfo.Hostname).
 | 
								Str("machine", registerRequest.Hostinfo.Hostname).
 | 
				
			||||||
			Msg("Failed authentication via AuthKey")
 | 
								Msg("Failed authentication via AuthKey")
 | 
				
			||||||
		machineRegistrations.WithLabelValues("new", RegisterMethodAuthKey, "error", pak.Namespace.Name).
 | 
					
 | 
				
			||||||
			Inc()
 | 
							if pak != nil {
 | 
				
			||||||
 | 
								machineRegistrations.WithLabelValues("new", RegisterMethodAuthKey, "error", pak.Namespace.Name).
 | 
				
			||||||
 | 
									Inc()
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								machineRegistrations.WithLabelValues("new", RegisterMethodAuthKey, "error").Inc()
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -580,35 +585,51 @@ func (h *Headscale) handleAuthKey(
 | 
				
			|||||||
		Msg("Authentication key was valid, proceeding to acquire IP addresses")
 | 
							Msg("Authentication key was valid, proceeding to acquire IP addresses")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	nodeKey := NodePublicKeyStripPrefix(registerRequest.NodeKey)
 | 
						nodeKey := NodePublicKeyStripPrefix(registerRequest.NodeKey)
 | 
				
			||||||
	now := time.Now().UTC()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	machineToRegister := Machine{
 | 
						// retrieve machine information if it exist
 | 
				
			||||||
		Name:           registerRequest.Hostinfo.Hostname,
 | 
						// The error is not important, because if it does not
 | 
				
			||||||
		NamespaceID:    pak.Namespace.ID,
 | 
						// exist, then this is a new machine and we will move
 | 
				
			||||||
		MachineKey:     machineKeyStr,
 | 
						// on to registration.
 | 
				
			||||||
		RegisterMethod: RegisterMethodAuthKey,
 | 
						machine, _ := h.GetMachineByMachineKey(machineKey)
 | 
				
			||||||
		Expiry:         ®isterRequest.Expiry,
 | 
						if machine != nil {
 | 
				
			||||||
		NodeKey:        nodeKey,
 | 
							log.Trace().
 | 
				
			||||||
		LastSeen:       &now,
 | 
					 | 
				
			||||||
		AuthKeyID:      uint(pak.ID),
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	machine, err := h.RegisterMachine(
 | 
					 | 
				
			||||||
		machineToRegister,
 | 
					 | 
				
			||||||
	)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		log.Error().
 | 
					 | 
				
			||||||
			Caller().
 | 
								Caller().
 | 
				
			||||||
			Err(err).
 | 
								Str("machine", machine.Name).
 | 
				
			||||||
			Msg("could not register machine")
 | 
								Msg("machine already registered, refreshing with new auth key")
 | 
				
			||||||
		machineRegistrations.WithLabelValues("new", RegisterMethodAuthKey, "error", pak.Namespace.Name).
 | 
					 | 
				
			||||||
			Inc()
 | 
					 | 
				
			||||||
		ctx.String(
 | 
					 | 
				
			||||||
			http.StatusInternalServerError,
 | 
					 | 
				
			||||||
			"could not register machine",
 | 
					 | 
				
			||||||
		)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return
 | 
							machine.NodeKey = nodeKey
 | 
				
			||||||
 | 
							machine.AuthKeyID = uint(pak.ID)
 | 
				
			||||||
 | 
							h.RefreshMachine(machine, registerRequest.Expiry)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							now := time.Now().UTC()
 | 
				
			||||||
 | 
							machineToRegister := Machine{
 | 
				
			||||||
 | 
								Name:           registerRequest.Hostinfo.Hostname,
 | 
				
			||||||
 | 
								NamespaceID:    pak.Namespace.ID,
 | 
				
			||||||
 | 
								MachineKey:     machineKeyStr,
 | 
				
			||||||
 | 
								RegisterMethod: RegisterMethodAuthKey,
 | 
				
			||||||
 | 
								Expiry:         ®isterRequest.Expiry,
 | 
				
			||||||
 | 
								NodeKey:        nodeKey,
 | 
				
			||||||
 | 
								LastSeen:       &now,
 | 
				
			||||||
 | 
								AuthKeyID:      uint(pak.ID),
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							machine, err = h.RegisterMachine(
 | 
				
			||||||
 | 
								machineToRegister,
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								log.Error().
 | 
				
			||||||
 | 
									Caller().
 | 
				
			||||||
 | 
									Err(err).
 | 
				
			||||||
 | 
									Msg("could not register machine")
 | 
				
			||||||
 | 
								machineRegistrations.WithLabelValues("new", RegisterMethodAuthKey, "error", pak.Namespace.Name).
 | 
				
			||||||
 | 
									Inc()
 | 
				
			||||||
 | 
								ctx.String(
 | 
				
			||||||
 | 
									http.StatusInternalServerError,
 | 
				
			||||||
 | 
									"could not register machine",
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	h.UsePreAuthKey(pak)
 | 
						h.UsePreAuthKey(pak)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user