From 535f13097930a41d545067b16db94a3fefd1dd55 Mon Sep 17 00:00:00 2001 From: Rick Rackow Date: Wed, 2 Apr 2025 21:05:58 +0200 Subject: [PATCH] feat(metadata/gcp): truncate too long hostnames on gcp --- datasource/metadata/gce/metadata.go | 11 +++++++++ datasource/metadata/gce/metadata_test.go | 30 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/datasource/metadata/gce/metadata.go b/datasource/metadata/gce/metadata.go index 911da6a..489710f 100644 --- a/datasource/metadata/gce/metadata.go +++ b/datasource/metadata/gce/metadata.go @@ -16,8 +16,10 @@ package gce import ( "fmt" + "log" "net" "net/http" + "strings" "github.com/flatcar/coreos-cloudinit/datasource" "github.com/flatcar/coreos-cloudinit/datasource/metadata" @@ -51,6 +53,15 @@ func (ms metadataService) FetchMetadata() (datasource.Metadata, error) { return datasource.Metadata{}, err } + 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 datasource.Metadata{ PublicIPv4: public, PrivateIPv4: local, diff --git a/datasource/metadata/gce/metadata_test.go b/datasource/metadata/gce/metadata_test.go index fbb9701..4104c64 100644 --- a/datasource/metadata/gce/metadata_test.go +++ b/datasource/metadata/gce/metadata_test.go @@ -71,6 +71,36 @@ func TestFetchMetadata(t *testing.T) { PublicIPv4: net.ParseIP("5.6.7.8"), }, }, + { + // test long host names are truncated on first `.` + root: "/", + metadataPath: "computeMetadata/v1/instance/", + resources: map[string]string{ + "/computeMetadata/v1/instance/hostname": "this-hostname-is-larger-than-sixty-three-characters-long-and.will.be.truncated.local", + "/computeMetadata/v1/instance/network-interfaces/0/ip": "1.2.3.4", + "/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip": "5.6.7.8", + }, + expect: datasource.Metadata{ + Hostname: "this-hostname-is-larger-than-sixty-three-characters-long-and", + PrivateIPv4: net.ParseIP("1.2.3.4"), + PublicIPv4: net.ParseIP("5.6.7.8"), + }, + }, + { + // test long host names are truncated if segment before first `.` is too long + root: "/", + metadataPath: "computeMetadata/v1/instance/", + resources: map[string]string{ + "/computeMetadata/v1/instance/hostname": "this-hostname-is-larger-than-sixty-three-characters-long-and-will-be-truncated.local", + "/computeMetadata/v1/instance/network-interfaces/0/ip": "1.2.3.4", + "/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip": "5.6.7.8", + }, + expect: datasource.Metadata{ + Hostname: "this-hostname-is-larger-than-sixty-three-characters-long-and-wi", + PrivateIPv4: net.ParseIP("1.2.3.4"), + PublicIPv4: net.ParseIP("5.6.7.8"), + }, + }, { clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")}, expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},