mirror of
https://github.com/juanfont/headscale.git
synced 2025-10-15 18:19:49 +00:00
Add helper function to create a unique givenname
This commit is contained in:
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user