mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-10-28 10:51:44 +01:00 
			
		
		
		
	Reuse machine structure for parameters, named parameters
This commit is contained in:
		
							parent
							
								
									469551bc5d
								
							
						
					
					
						commit
						402a76070f
					
				
							
								
								
									
										89
									
								
								machine.go
									
									
									
									
									
								
							
							
						
						
									
										89
									
								
								machine.go
									
									
									
									
									
								
							@ -21,7 +21,6 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	errMachineNotFound                  = Error("machine not found")
 | 
						errMachineNotFound                  = Error("machine not found")
 | 
				
			||||||
	errMachineAlreadyRegistered         = Error("machine already registered")
 | 
					 | 
				
			||||||
	errMachineRouteIsNotAvailable       = Error("route is not available on machine")
 | 
						errMachineRouteIsNotAvailable       = Error("route is not available on machine")
 | 
				
			||||||
	errMachineAddressesInvalid          = Error("failed to parse machine addresses")
 | 
						errMachineAddressesInvalid          = Error("failed to parse machine addresses")
 | 
				
			||||||
	errMachineNotFoundRegistrationCache = Error(
 | 
						errMachineNotFoundRegistrationCache = Error(
 | 
				
			||||||
@ -698,19 +697,23 @@ func (h *Headscale) RegisterMachineFromAuthCallback(
 | 
				
			|||||||
) (*Machine, error) {
 | 
					) (*Machine, error) {
 | 
				
			||||||
	if machineInterface, ok := h.registrationCache.Get(machineKeyStr); ok {
 | 
						if machineInterface, ok := h.registrationCache.Get(machineKeyStr); ok {
 | 
				
			||||||
		if registrationMachine, ok := machineInterface.(Machine); ok {
 | 
							if registrationMachine, ok := machineInterface.(Machine); ok {
 | 
				
			||||||
 | 
								namespace, err := h.GetNamespace(namespaceName)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return nil, fmt.Errorf(
 | 
				
			||||||
 | 
										"failed to find namespace in register machine from auth callback, %w",
 | 
				
			||||||
 | 
										err,
 | 
				
			||||||
 | 
									)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								registrationMachine.NamespaceID = namespace.ID
 | 
				
			||||||
 | 
								registrationMachine.RegisterMethod = registrationMethod
 | 
				
			||||||
 | 
								registrationMachine.Expiry = expiry
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			machine, err := h.RegisterMachine(
 | 
								machine, err := h.RegisterMachine(
 | 
				
			||||||
				registrationMachine.Name,
 | 
									registrationMachine,
 | 
				
			||||||
				machineKeyStr,
 | 
					 | 
				
			||||||
				namespaceName,
 | 
					 | 
				
			||||||
				registrationMethod,
 | 
					 | 
				
			||||||
				expiry,
 | 
					 | 
				
			||||||
				nil,
 | 
					 | 
				
			||||||
				®istrationMachine.NodeKey,
 | 
					 | 
				
			||||||
				registrationMachine.LastSeen,
 | 
					 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			return machine, err
 | 
								return machine, err
 | 
				
			||||||
 | 
					 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			return nil, errCouldNotConvertMachineInterface
 | 
								return nil, errCouldNotConvertMachineInterface
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@ -720,49 +723,30 @@ func (h *Headscale) RegisterMachineFromAuthCallback(
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// RegisterMachine is executed from the CLI to register a new Machine using its MachineKey.
 | 
					// RegisterMachine is executed from the CLI to register a new Machine using its MachineKey.
 | 
				
			||||||
func (h *Headscale) RegisterMachine(
 | 
					func (h *Headscale) RegisterMachine(machine Machine,
 | 
				
			||||||
	machineName string,
 | 
					 | 
				
			||||||
	machineKeyStr string,
 | 
					 | 
				
			||||||
	namespaceName string,
 | 
					 | 
				
			||||||
	registrationMethod string,
 | 
					 | 
				
			||||||
	expiry *time.Time,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Optionals
 | 
					 | 
				
			||||||
	authKey *PreAuthKey,
 | 
					 | 
				
			||||||
	nodePublicKey *string,
 | 
					 | 
				
			||||||
	lastSeen *time.Time,
 | 
					 | 
				
			||||||
) (*Machine, error) {
 | 
					) (*Machine, error) {
 | 
				
			||||||
	namespace, err := h.GetNamespace(namespaceName)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var machineKey key.MachinePublic
 | 
					 | 
				
			||||||
	err = machineKey.UnmarshalText([]byte(MachinePublicKeyEnsurePrefix(machineKeyStr)))
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	log.Trace().
 | 
						log.Trace().
 | 
				
			||||||
		Caller().
 | 
							Caller().
 | 
				
			||||||
		Str("machine_key_str", machineKeyStr).
 | 
							Str("machine_key", machine.MachineKey).
 | 
				
			||||||
		Str("machine_key", machineKey.String()).
 | 
					 | 
				
			||||||
		Msg("Registering machine")
 | 
							Msg("Registering machine")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	machine, err := h.GetMachineByMachineKey(machineKey)
 | 
						// If the machine is already in the database, it is seeking
 | 
				
			||||||
	if err != nil {
 | 
						// reauthentication, and by reaching this step, has been authenticated
 | 
				
			||||||
		return nil, err
 | 
						// and need to have an updated expiry.
 | 
				
			||||||
	}
 | 
						var machineKey key.MachinePublic
 | 
				
			||||||
 | 
						_ = machineKey.UnmarshalText(
 | 
				
			||||||
 | 
							[]byte(MachinePublicKeyEnsurePrefix(machine.MachineKey)),
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						machineFromDatabase, _ := h.GetMachineByMachineKey(machineKey)
 | 
				
			||||||
	if machine.isRegistered() {
 | 
						if machine.isRegistered() {
 | 
				
			||||||
		log.Trace().
 | 
							log.Trace().
 | 
				
			||||||
			Caller().
 | 
								Caller().
 | 
				
			||||||
			Str("machine", machine.Name).
 | 
								Str("machine", machine.Name).
 | 
				
			||||||
			Msg("machine already registered, reauthenticating")
 | 
								Msg("machine already registered, reauthenticating")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		h.RefreshMachine(machine, *expiry)
 | 
							h.RefreshMachine(machineFromDatabase, *machine.Expiry)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return machine, nil
 | 
							return machineFromDatabase, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	log.Trace().
 | 
						log.Trace().
 | 
				
			||||||
@ -786,28 +770,9 @@ func (h *Headscale) RegisterMachine(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	machine.IPAddresses = ips
 | 
						machine.IPAddresses = ips
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if expiry != nil {
 | 
					 | 
				
			||||||
		machine.Expiry = expiry
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if authKey != nil {
 | 
					 | 
				
			||||||
		machine.AuthKeyID = uint(authKey.ID)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if nodePublicKey != nil {
 | 
					 | 
				
			||||||
		machine.NodeKey = *nodePublicKey
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if lastSeen != nil {
 | 
					 | 
				
			||||||
		machine.LastSeen = lastSeen
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	machine.Name = machineName
 | 
					 | 
				
			||||||
	machine.NamespaceID = namespace.ID
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// TODO(kradalby): This field is uneccessary metadata,
 | 
						// TODO(kradalby): This field is uneccessary metadata,
 | 
				
			||||||
	// move it to tags instead of having a column.
 | 
						// move it to tags instead of having a column.
 | 
				
			||||||
	machine.RegisterMethod = registrationMethod
 | 
						// machine.RegisterMethod = registrationMethod
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// TODO(kradalby): Registered is a very frustrating value
 | 
						// TODO(kradalby): Registered is a very frustrating value
 | 
				
			||||||
	// to keep up to date, and it makes is have to care if a
 | 
						// to keep up to date, and it makes is have to care if a
 | 
				
			||||||
@ -824,7 +789,7 @@ func (h *Headscale) RegisterMachine(
 | 
				
			|||||||
		Str("ip", strings.Join(ips.ToStringSlice(), ",")).
 | 
							Str("ip", strings.Join(ips.ToStringSlice(), ",")).
 | 
				
			||||||
		Msg("Machine registered with the database")
 | 
							Msg("Machine registered with the database")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return machine, nil
 | 
						return &machine, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (machine *Machine) GetAdvertisedRoutes() ([]netaddr.IPPrefix, error) {
 | 
					func (machine *Machine) GetAdvertisedRoutes() ([]netaddr.IPPrefix, error) {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user