Skip to content
This repository was archived by the owner on Sep 18, 2020. It is now read-only.

Commit fa0178c

Browse files
committed
Merge pull request #400 from sethp/coreos-cloudinit/gzip-magic
gzip autodetection
2 parents 0fd3cd2 + 778a47b commit fa0178c

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

coreos-cloudinit.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
package main
1616

1717
import (
18+
"bytes"
19+
"compress/gzip"
1820
"flag"
1921
"fmt"
22+
"io/ioutil"
2023
"log"
2124
"os"
2225
"runtime"
@@ -185,6 +188,11 @@ func main() {
185188
log.Printf("Failed fetching user-data from datasource: %v. Continuing...\n", err)
186189
failure = true
187190
}
191+
userdataBytes, err = decompressIfGzip(userdataBytes)
192+
if err != nil {
193+
log.Printf("Failed decompressing user-data from datasource: %v. Continuing...\n", err)
194+
failure = true
195+
}
188196

189197
if report, err := validate.Validate(userdataBytes); err == nil {
190198
ret := 0
@@ -399,3 +407,17 @@ func runScript(script config.Script, env *initialize.Environment) error {
399407
}
400408
return err
401409
}
410+
411+
const gzipMagicBytes = "\x1f\x8b"
412+
413+
func decompressIfGzip(userdataBytes []byte) ([]byte, error) {
414+
if !bytes.HasPrefix(userdataBytes, []byte(gzipMagicBytes)) {
415+
return userdataBytes, nil
416+
}
417+
gzr, err := gzip.NewReader(bytes.NewReader(userdataBytes))
418+
if err != nil {
419+
return nil, err
420+
}
421+
defer gzr.Close()
422+
return ioutil.ReadAll(gzr)
423+
}

coreos-cloudinit_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
package main
1616

1717
import (
18+
"bytes"
19+
"encoding/base64"
20+
"errors"
1821
"reflect"
1922
"testing"
2023

@@ -87,3 +90,58 @@ func TestMergeConfigs(t *testing.T) {
8790
}
8891
}
8992
}
93+
94+
func mustDecode(in string) []byte {
95+
out, err := base64.StdEncoding.DecodeString(in)
96+
if err != nil {
97+
panic(err)
98+
}
99+
return out
100+
}
101+
102+
func TestDecompressIfGzip(t *testing.T) {
103+
tests := []struct {
104+
in []byte
105+
106+
out []byte
107+
err error
108+
}{
109+
{
110+
in: nil,
111+
112+
out: nil,
113+
err: nil,
114+
},
115+
{
116+
in: []byte{},
117+
118+
out: []byte{},
119+
err: nil,
120+
},
121+
{
122+
in: mustDecode("H4sIAJWV/VUAA1NOzskvTdFNzs9Ly0wHABt6mQENAAAA"),
123+
124+
out: []byte("#cloud-config"),
125+
err: nil,
126+
},
127+
{
128+
in: []byte("#cloud-config"),
129+
130+
out: []byte("#cloud-config"),
131+
err: nil,
132+
},
133+
{
134+
in: mustDecode("H4sCORRUPT=="),
135+
136+
out: nil,
137+
err: errors.New("any error"),
138+
},
139+
}
140+
for i, tt := range tests {
141+
out, err := decompressIfGzip(tt.in)
142+
if !bytes.Equal(out, tt.out) || (tt.err != nil && err == nil) {
143+
t.Errorf("bad gzip (%d): want (%s, %#v), got (%s, %#v)", i, string(tt.out), tt.err, string(out), err)
144+
}
145+
}
146+
147+
}

test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ SRC="
2121
network
2222
pkg
2323
system
24+
.
2425
"
2526

2627
echo "Checking gofix..."

0 commit comments

Comments
 (0)