Skip to content

Commit 84e115b

Browse files
author
Rick Rackow
committed
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
1 parent f3aaab9 commit 84e115b

File tree

4 files changed

+85
-23
lines changed

4 files changed

+85
-23
lines changed

coreos-cloudinit.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"log"
2424
"os"
2525
"runtime"
26+
"strings"
2627
"sync"
2728
"time"
2829

@@ -285,6 +286,14 @@ func determineHostname(md datasource.Metadata, udata *initialize.UserData) strin
285286
hostname = udataHostname
286287
}
287288
}
289+
if len(hostname) > 63 {
290+
log.Printf("Hostname too long. Truncating hostname %s to %s", hostname, strings.Split(hostname, ".")[0])
291+
hostname = strings.Split(hostname, ".")[0]
292+
if len(hostname) > 63 {
293+
log.Printf("Hostname still too long. Truncating hostname %s further to 63 bytes (%s)", hostname, hostname[:63])
294+
hostname = hostname[:63]
295+
}
296+
}
288297
return hostname
289298
}
290299

coreos-cloudinit_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import (
1818
"bytes"
1919
"encoding/base64"
2020
"errors"
21+
"github.com/flatcar/coreos-cloudinit/datasource"
22+
"github.com/flatcar/coreos-cloudinit/initialize"
23+
"net"
24+
"reflect"
2125
"testing"
2226
)
2327

@@ -75,3 +79,75 @@ func TestDecompressIfGzip(t *testing.T) {
7579
}
7680

7781
}
82+
83+
func TestDetermineHostname(t *testing.T) {
84+
for _, tt := range []struct {
85+
metaData datasource.Metadata
86+
uData *initialize.UserData
87+
expect string
88+
}{
89+
{
90+
metaData: datasource.Metadata{
91+
PublicIPv4: net.ParseIP("1.2.3.4"),
92+
PublicIPv6: net.ParseIP("5.6.7.8"),
93+
PrivateIPv4: net.ParseIP("1.2.3.4"),
94+
PrivateIPv6: net.ParseIP("5.6.7.8"),
95+
Hostname: "regular-name",
96+
SSHPublicKeys: map[string]string{"my": "key"},
97+
NetworkConfig: net.Interface{
98+
Index: 0,
99+
MTU: 0,
100+
Name: "some-interface",
101+
HardwareAddr: nil,
102+
Flags: 0,
103+
},
104+
},
105+
uData: nil,
106+
expect: "regular-name",
107+
},
108+
{
109+
metaData: datasource.Metadata{
110+
PublicIPv4: net.ParseIP("1.2.3.4"),
111+
PublicIPv6: net.ParseIP("5.6.7.8"),
112+
PrivateIPv4: net.ParseIP("1.2.3.4"),
113+
PrivateIPv6: net.ParseIP("5.6.7.8"),
114+
Hostname: "this-hostname-is-larger-than-sixty-three-characters-long-and.will.be.truncated.locale",
115+
SSHPublicKeys: map[string]string{"my": "key"},
116+
NetworkConfig: net.Interface{
117+
Index: 0,
118+
MTU: 0,
119+
Name: "some-interface",
120+
HardwareAddr: nil,
121+
Flags: 0,
122+
},
123+
},
124+
uData: nil,
125+
expect: "this-hostname-is-larger-than-sixty-three-characters-long-and",
126+
},
127+
{
128+
metaData: datasource.Metadata{
129+
PublicIPv4: net.ParseIP("1.2.3.4"),
130+
PublicIPv6: net.ParseIP("5.6.7.8"),
131+
PrivateIPv4: net.ParseIP("1.2.3.4"),
132+
PrivateIPv6: net.ParseIP("5.6.7.8"),
133+
Hostname: "this-hostname-is-larger-than-sixty-three-characters-long-and-will-be-truncated.local",
134+
SSHPublicKeys: map[string]string{"my": "key"},
135+
NetworkConfig: net.Interface{
136+
Index: 0,
137+
MTU: 0,
138+
Name: "some-interface",
139+
HardwareAddr: nil,
140+
Flags: 0,
141+
},
142+
},
143+
uData: nil,
144+
expect: "this-hostname-is-larger-than-sixty-three-characters-long-and-wi",
145+
},
146+
} {
147+
hostname := determineHostname(tt.metaData, tt.uData)
148+
if !reflect.DeepEqual(tt.expect, hostname) {
149+
t.Fatalf("Bad hostname, want %s, got %s", tt.expect, hostname)
150+
}
151+
152+
}
153+
}

datasource/metadata/ec2/metadata.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ func (ms metadataService) FetchMetadata() (datasource.Metadata, error) {
7878
}
7979

8080
if hostname, err := ms.fetchAttribute(fmt.Sprintf("%s/hostname", ms.MetadataUrl())); err == nil {
81-
hostname := strings.Split(hostname, ".")[0]
82-
if len(hostname) > 63 {
83-
log.Printf("Truncating hostname %s to 63 bytes (%s)", hostname, hostname[:63])
84-
hostname = hostname[:63]
85-
}
8681
metadata.Hostname = hostname
8782
} else if _, ok := err.(pkg.ErrNotFound); !ok {
8883
return metadata, err

datasource/metadata/ec2/metadata_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -194,24 +194,6 @@ func TestFetchMetadata(t *testing.T) {
194194
SSHPublicKeys: map[string]string{"test1": "key"},
195195
},
196196
},
197-
{
198-
root: "/",
199-
metadataPath: "2009-04-04/meta-data",
200-
resources: map[string]string{
201-
"/2009-04-04/meta-data/hostname": "this-hostname-is-larger-than-sixty-three-characters-long-and-will-be-truncated.local",
202-
"/2009-04-04/meta-data/local-ipv4": "1.2.3.4",
203-
"/2009-04-04/meta-data/public-ipv4": "5.6.7.8",
204-
"/2009-04-04/meta-data/public-keys": "0=test1\n",
205-
"/2009-04-04/meta-data/public-keys/0": "openssh-key",
206-
"/2009-04-04/meta-data/public-keys/0/openssh-key": "key",
207-
},
208-
expect: datasource.Metadata{
209-
Hostname: "this-hostname-is-larger-than-sixty-three-characters-long-and-wi",
210-
PrivateIPv4: net.ParseIP("1.2.3.4"),
211-
PublicIPv4: net.ParseIP("5.6.7.8"),
212-
SSHPublicKeys: map[string]string{"test1": "key"},
213-
},
214-
},
215197
{
216198
clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
217199
expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},

0 commit comments

Comments
 (0)