From 86909e5bcb55a2d9133a9bef64cad2dfd45b16ff Mon Sep 17 00:00:00 2001 From: Seth Pellegrino Date: Tue, 10 Nov 2015 08:26:27 -0800 Subject: [PATCH 1/2] test: added coreos-cloudinit_test to test script Include the root project directory in packages to be built/tested. --- test | 1 + 1 file changed, 1 insertion(+) diff --git a/test b/test index 8853dfa..0b6df90 100755 --- a/test +++ b/test @@ -21,6 +21,7 @@ SRC=" network pkg system + . " echo "Checking gofix..." From 778a47b9574ceb989819f1341fd798d47a3572cd Mon Sep 17 00:00:00 2001 From: Seth Pellegrino Date: Sun, 20 Sep 2015 18:11:26 -0700 Subject: [PATCH 2/2] userdata: gzip autodetection look for the gzip magic number at the beginning of a data source, and, if found, decompress the input before further processing. Addresses coreos/bugs#741. --- coreos-cloudinit.go | 22 +++++++++++++++ coreos-cloudinit_test.go | 58 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index 11fa3fe..6939b7f 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -15,8 +15,11 @@ package main import ( + "bytes" + "compress/gzip" "flag" "fmt" + "io/ioutil" "log" "os" "sync" @@ -178,6 +181,11 @@ func main() { log.Printf("Failed fetching user-data from datasource: %v. Continuing...\n", err) failure = true } + userdataBytes, err = decompressIfGzip(userdataBytes) + if err != nil { + log.Printf("Failed decompressing user-data from datasource: %v. Continuing...\n", err) + failure = true + } if report, err := validate.Validate(userdataBytes); err == nil { ret := 0 @@ -392,3 +400,17 @@ func runScript(script config.Script, env *initialize.Environment) error { } return err } + +const gzipMagicBytes = "\x1f\x8b" + +func decompressIfGzip(userdataBytes []byte) ([]byte, error) { + if !bytes.HasPrefix(userdataBytes, []byte(gzipMagicBytes)) { + return userdataBytes, nil + } + gzr, err := gzip.NewReader(bytes.NewReader(userdataBytes)) + if err != nil { + return nil, err + } + defer gzr.Close() + return ioutil.ReadAll(gzr) +} diff --git a/coreos-cloudinit_test.go b/coreos-cloudinit_test.go index cd87d5f..cd5c6da 100644 --- a/coreos-cloudinit_test.go +++ b/coreos-cloudinit_test.go @@ -15,6 +15,9 @@ package main import ( + "bytes" + "encoding/base64" + "errors" "reflect" "testing" @@ -87,3 +90,58 @@ func TestMergeConfigs(t *testing.T) { } } } + +func mustDecode(in string) []byte { + out, err := base64.StdEncoding.DecodeString(in) + if err != nil { + panic(err) + } + return out +} + +func TestDecompressIfGzip(t *testing.T) { + tests := []struct { + in []byte + + out []byte + err error + }{ + { + in: nil, + + out: nil, + err: nil, + }, + { + in: []byte{}, + + out: []byte{}, + err: nil, + }, + { + in: mustDecode("H4sIAJWV/VUAA1NOzskvTdFNzs9Ly0wHABt6mQENAAAA"), + + out: []byte("#cloud-config"), + err: nil, + }, + { + in: []byte("#cloud-config"), + + out: []byte("#cloud-config"), + err: nil, + }, + { + in: mustDecode("H4sCORRUPT=="), + + out: nil, + err: errors.New("any error"), + }, + } + for i, tt := range tests { + out, err := decompressIfGzip(tt.in) + if !bytes.Equal(out, tt.out) || (tt.err != nil && err == nil) { + t.Errorf("bad gzip (%d): want (%s, %#v), got (%s, %#v)", i, string(tt.out), tt.err, string(out), err) + } + } + +}