From 33c483b5c528d00bf56debe92b76724c5bfaaf2f Mon Sep 17 00:00:00 2001 From: Rick Rackow Date: Thu, 3 Apr 2025 10:01:31 +0200 Subject: [PATCH] chore(cloud-init): truncate hostnames in determineHostname Too long hostname are not allowed and potentially breaking things. Instead of individually checking the hostname in each metadata provider, we adjust the hostname length when it's used to be set --- coreos-cloudinit.go | 9 +++ coreos-cloudinit_test.go | 76 ++++++++++++++++++++++++ datasource/metadata/ec2/metadata.go | 5 -- datasource/metadata/ec2/metadata_test.go | 20 +------ 4 files changed, 86 insertions(+), 24 deletions(-) diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index d52757f..82e4230 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -23,6 +23,7 @@ import ( "log" "os" "runtime" + "strings" "sync" "time" @@ -285,6 +286,14 @@ func determineHostname(md datasource.Metadata, udata *initialize.UserData) strin hostname = udataHostname } } + 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] + } + } return hostname } diff --git a/coreos-cloudinit_test.go b/coreos-cloudinit_test.go index 77b7b0e..515f2aa 100644 --- a/coreos-cloudinit_test.go +++ b/coreos-cloudinit_test.go @@ -18,6 +18,10 @@ import ( "bytes" "encoding/base64" "errors" + "github.com/flatcar/coreos-cloudinit/datasource" + "github.com/flatcar/coreos-cloudinit/initialize" + "net" + "reflect" "testing" ) @@ -75,3 +79,75 @@ func TestDecompressIfGzip(t *testing.T) { } } + +func TestDetermineHostname(t *testing.T) { + for _, tt := range []struct { + metaData datasource.Metadata + uData *initialize.UserData + expect string + }{ + { + 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", + 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"), + PublicIPv6: net.ParseIP("5.6.7.8"), + PrivateIPv4: net.ParseIP("1.2.3.4"), + PrivateIPv6: net.ParseIP("5.6.7.8"), + Hostname: "this-hostname-is-larger-than-sixty-three-characters-long-and.will.be.truncated.locale", + SSHPublicKeys: map[string]string{"my": "key"}, + NetworkConfig: net.Interface{ + Index: 0, + MTU: 0, + Name: "some-interface", + HardwareAddr: nil, + Flags: 0, + }, + }, + uData: nil, + expect: "this-hostname-is-larger-than-sixty-three-characters-long-and", + }, + { + 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: "this-hostname-is-larger-than-sixty-three-characters-long-and-will-be-truncated.local", + SSHPublicKeys: map[string]string{"my": "key"}, + NetworkConfig: net.Interface{ + Index: 0, + MTU: 0, + Name: "some-interface", + HardwareAddr: nil, + Flags: 0, + }, + }, + uData: nil, + expect: "this-hostname-is-larger-than-sixty-three-characters-long-and-wi", + }, + } { + hostname := determineHostname(tt.metaData, tt.uData) + if !reflect.DeepEqual(tt.expect, hostname) { + t.Fatalf("Bad hostname, want %s, got %s", tt.expect, hostname) + } + + } +} diff --git a/datasource/metadata/ec2/metadata.go b/datasource/metadata/ec2/metadata.go index b65e106..2042bd3 100644 --- a/datasource/metadata/ec2/metadata.go +++ b/datasource/metadata/ec2/metadata.go @@ -78,11 +78,6 @@ func (ms metadataService) FetchMetadata() (datasource.Metadata, error) { } if hostname, err := ms.fetchAttribute(fmt.Sprintf("%s/hostname", ms.MetadataUrl())); err == nil { - hostname := strings.Split(hostname, ".")[0] - if len(hostname) > 63 { - log.Printf("Truncating hostname %s to 63 bytes (%s)", hostname, hostname[:63]) - hostname = hostname[:63] - } metadata.Hostname = hostname } else if _, ok := err.(pkg.ErrNotFound); !ok { return metadata, err diff --git a/datasource/metadata/ec2/metadata_test.go b/datasource/metadata/ec2/metadata_test.go index c81eb6c..ab96885 100644 --- a/datasource/metadata/ec2/metadata_test.go +++ b/datasource/metadata/ec2/metadata_test.go @@ -188,25 +188,7 @@ func TestFetchMetadata(t *testing.T) { "/2009-04-04/meta-data/public-keys/0/openssh-key": "key", }, expect: datasource.Metadata{ - Hostname: "host", - PrivateIPv4: net.ParseIP("1.2.3.4"), - PublicIPv4: net.ParseIP("5.6.7.8"), - SSHPublicKeys: map[string]string{"test1": "key"}, - }, - }, - { - root: "/", - metadataPath: "2009-04-04/meta-data", - resources: map[string]string{ - "/2009-04-04/meta-data/hostname": "this-hostname-is-larger-than-sixty-three-characters-long-and-will-be-truncated.local", - "/2009-04-04/meta-data/local-ipv4": "1.2.3.4", - "/2009-04-04/meta-data/public-ipv4": "5.6.7.8", - "/2009-04-04/meta-data/public-keys": "0=test1\n", - "/2009-04-04/meta-data/public-keys/0": "openssh-key", - "/2009-04-04/meta-data/public-keys/0/openssh-key": "key", - }, - expect: datasource.Metadata{ - Hostname: "this-hostname-is-larger-than-sixty-three-characters-long-and-wi", + Hostname: "host.local", PrivateIPv4: net.ParseIP("1.2.3.4"), PublicIPv4: net.ParseIP("5.6.7.8"), SSHPublicKeys: map[string]string{"test1": "key"},