Skip to content

Commit

Permalink
Fix most nil pointers, actually make it check for unique across heads…
Browse files Browse the repository at this point in the history
…cale

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
  • Loading branch information
kradalby committed Oct 21, 2022
1 parent 830d59f commit bc1c1f5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion grpcv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ func (api headscaleV1APIServer) DebugCreateMachine(
Hostname: "DebugTestMachine",
}

givenName, err := api.h.GenerateGivenName(namespace.Name, request.GetKey(), request.GetName())
givenName, err := api.h.GenerateGivenName(request.GetKey(), request.GetName())
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions integration/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func TestPingAll(t *testing.T) {

t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps))

err = scenario.Shutdown()
if err != nil {
t.Errorf("failed to tear down scenario: %s", err)
}
// err = scenario.Shutdown()
// if err != nil {
// t.Errorf("failed to tear down scenario: %s", err)
// }
}
31 changes: 23 additions & 8 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,15 @@ func (h *Headscale) ListMachines() ([]Machine, error) {
return machines, nil
}

func (h *Headscale) ListMachinesByGivenName(givenName string) ([]Machine, error) {
machines := []Machine{}
if err := h.db.Preload("AuthKey").Preload("AuthKey.Namespace").Preload("Namespace").Find(&machines).Where("given_name = ?", givenName).Error; err != nil {
return nil, err
}

return machines, nil
}

// GetMachine finds a Machine by name and namespace and returns the Machine struct.
func (h *Headscale) GetMachine(namespace string, name string) (*Machine, error) {
machines, err := h.ListMachinesInNamespace(namespace)
Expand Down Expand Up @@ -1061,21 +1070,27 @@ func (h *Headscale) generateGivenName(suppliedName string, randomSuffix bool) (s
return normalizedHostname, nil
}

func (h *Headscale) GenerateGivenName(namespace string, machineKey string, suppliedName string) (string, error) {
func (h *Headscale) GenerateGivenName(machineKey string, suppliedName string) (string, error) {
givenName, err := h.generateGivenName(suppliedName, false)
if err != nil {
return "", err
}

// Tailscale rules (may differ) https://tailscale.com/kb/1098/machine-names/
machine, _ := h.GetMachineByGivenName(namespace, givenName)
if machine != nil && machine.MachineKey != machineKey && machine.GivenName == givenName {
postfixedName, err := h.generateGivenName(suppliedName, true)
if err != nil {
return "", err
}
machines, err := h.ListMachinesByGivenName(givenName)
if err != nil {
return "", err
}

givenName = postfixedName
for _, machine := range machines {
if machine.MachineKey != machineKey && machine.GivenName == givenName {
postfixedName, err := h.generateGivenName(suppliedName, true)
if err != nil {
return "", err
}

givenName = postfixedName
}
}

return givenName, nil
Expand Down
7 changes: 5 additions & 2 deletions protocol_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ func (h *Headscale) handleRegisterCommon(
Bool("noise", machineKey.IsZero()).
Msg("New machine not yet in the database")

givenName, err := h.GenerateGivenName(machine.Namespace.Name, machine.MachineKey, registerRequest.Hostinfo.Hostname)
givenName, err := h.GenerateGivenName(
machineKey.String(),
registerRequest.Hostinfo.Hostname,
)
if err != nil {
log.Error().
Caller().
Expand Down Expand Up @@ -374,7 +377,7 @@ func (h *Headscale) handleAuthKeyCommon(
} else {
now := time.Now().UTC()

givenName, err := h.GenerateGivenName(machine.Namespace.Name, MachinePublicKeyStripPrefix(machineKey), registerRequest.Hostinfo.Hostname)
givenName, err := h.GenerateGivenName(MachinePublicKeyStripPrefix(machineKey), registerRequest.Hostinfo.Hostname)
if err != nil {
log.Error().
Caller().
Expand Down

0 comments on commit bc1c1f5

Please sign in to comment.