Add helper function to create a unique givenname
This commit is contained in:
parent
f4873d9387
commit
177c21b294
3 changed files with 177 additions and 0 deletions
30
machine.go
30
machine.go
|
@ -26,6 +26,7 @@ const (
|
|||
)
|
||||
errCouldNotConvertMachineInterface = Error("failed to convert machine interface")
|
||||
errHostnameTooLong = Error("Hostname too long")
|
||||
MachineGivenNameHashLength = 8
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -813,3 +814,32 @@ func (machine *Machine) RoutesToProto() *v1.Routes {
|
|||
EnabledRoutes: ipPrefixToString(enabledRoutes),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Headscale) GenerateGivenName(suppliedName string) (string, error) {
|
||||
// If a hostname is or will be longer than 63 chars after adding the hash,
|
||||
// it needs to be trimmed.
|
||||
trimmedHostnameLength := labelHostnameLength - MachineGivenNameHashLength - 2
|
||||
|
||||
normalizedHostname, err := NormalizeToFQDNRules(
|
||||
suppliedName,
|
||||
h.cfg.OIDC.StripEmaildomain,
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
postfix, err := GenerateRandomStringDNSSafe(MachineGivenNameHashLength)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Verify that that the new unique name is shorter than the maximum allowed
|
||||
// DNS segment.
|
||||
if len(normalizedHostname) <= trimmedHostnameLength {
|
||||
normalizedHostname = fmt.Sprintf("%s-%s", normalizedHostname, postfix)
|
||||
} else {
|
||||
normalizedHostname = fmt.Sprintf("%s-%s", normalizedHostname[:trimmedHostnameLength], postfix)
|
||||
}
|
||||
|
||||
return normalizedHostname, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue