cloudinit: trim trailing whitespaces in #cloud-config header

This commit is contained in:
kayrus 2015-08-28 12:38:47 +02:00
parent 9c4aca6c9a
commit f92dcb7968
2 changed files with 4 additions and 5 deletions

View File

@ -19,6 +19,7 @@ import (
"reflect" "reflect"
"regexp" "regexp"
"strings" "strings"
"unicode"
"github.com/coreos/coreos-cloudinit/Godeps/_workspace/src/github.com/coreos/yaml" "github.com/coreos/coreos-cloudinit/Godeps/_workspace/src/github.com/coreos/yaml"
) )
@ -49,10 +50,8 @@ type CoreOS struct {
func IsCloudConfig(userdata string) bool { func IsCloudConfig(userdata string) bool {
header := strings.SplitN(userdata, "\n", 2)[0] header := strings.SplitN(userdata, "\n", 2)[0]
// Explicitly trim the header so we can handle user-data from // Trim trailing whitespaces
// non-unix operating systems. The rest of the file is parsed header = strings.TrimRightFunc(header, unicode.IsSpace)
// by yaml, which correctly handles CRLF.
header = strings.TrimSuffix(header, "\r")
return (header == "#cloud-config") return (header == "#cloud-config")
} }

View File

@ -47,7 +47,7 @@ func TestParseHeaderCRLF(t *testing.T) {
} }
func TestParseConfigCRLF(t *testing.T) { func TestParseConfigCRLF(t *testing.T) {
contents := "#cloud-config\r\nhostname: foo\r\nssh_authorized_keys:\r\n - foobar\r\n" contents := "#cloud-config \r\nhostname: foo\r\nssh_authorized_keys:\r\n - foobar\r\n"
ud, err := ParseUserData(contents) ud, err := ParseUserData(contents)
if err != nil { if err != nil {
t.Fatalf("Failed parsing config: %v", err) t.Fatalf("Failed parsing config: %v", err)