Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/enhancement/2025-04-03-sanitize-hostname.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Always truncate hostnames on the first occurrence of `.`
* Ensure hostnames never exceeds 63 characters, regardless of the metadata provider
13 changes: 7 additions & 6 deletions coreos-cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,14 @@ func determineHostname(md datasource.Metadata, udata *initialize.UserData) strin
hostname = udataHostname
}
}
// Always truncate hostnames to everything before the first `.`
hostname = strings.Split(hostname, ".")[0]

// Truncate after 63 characters if the hostname exceeds that
if len(hostname) > 63 {
log.Printf("Hostname too long. Truncating hostname %s to %s", hostname, strings.Split(hostname, ".")[0])
hostname = strings.Split(hostname, ".")[0]
if len(hostname) > 63 {
log.Printf("Hostname still too long. Truncating hostname %s further to 63 bytes (%s)", hostname, hostname[:63])
hostname = hostname[:63]
}
log.Printf("Hostname too long. Truncating hostname %s to 63 bytes (%s)", hostname, hostname[:63])
hostname = hostname[:63]

}
return hostname
}
Expand Down
19 changes: 19 additions & 0 deletions coreos-cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ func TestDetermineHostname(t *testing.T) {
uData: nil,
expect: "regular-name",
},
{
metaData: datasource.Metadata{
PublicIPv4: net.ParseIP("1.2.3.4"),
PublicIPv6: net.ParseIP("5.6.7.8"),
PrivateIPv4: net.ParseIP("1.2.3.4"),
PrivateIPv6: net.ParseIP("5.6.7.8"),
Hostname: "regular-name.domain",
SSHPublicKeys: map[string]string{"my": "key"},
NetworkConfig: net.Interface{
Index: 0,
MTU: 0,
Name: "some-interface",
HardwareAddr: nil,
Flags: 0,
},
},
uData: nil,
expect: "regular-name",
},
{
metaData: datasource.Metadata{
PublicIPv4: net.ParseIP("1.2.3.4"),
Expand Down