Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
Decompress user-data if it is gzipped
Browse files Browse the repository at this point in the history
Fixes #2391

(cherry picked from commit 05c2a40)
  • Loading branch information
stffabi authored and niusmallnan committed Aug 21, 2018
1 parent e4653b5 commit 39e97a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions cmd/cloudinitsave/cloudinitsave.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ func fetchAndSave(ds datasource.Datasource) error {
log.Errorf("Failed fetching user-data from datasource: %v", err)
return err
}
userDataBytes, err = decompressIfGzip(userDataBytes)
if err != nil {
log.Errorf("Failed decompressing user-data from datasource: %v", err)
return err
}
log.Infof("Fetching meta-data from datasource of type %v", ds.Type())
metadata, err = ds.FetchMetadata()
if err != nil {
Expand Down Expand Up @@ -367,3 +372,13 @@ func composeToCloudConfig(bytes []byte) ([]byte, error) {
},
})
}

const gzipMagicBytes = "\x1f\x8b"

func decompressIfGzip(userdataBytes []byte) ([]byte, error) {
if !bytes.HasPrefix(userdataBytes, []byte(gzipMagicBytes)) {
return userdataBytes, nil
}

return config.DecompressGzip(userdataBytes)
}
7 changes: 6 additions & 1 deletion config/cloudinit/config/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ func DecodeBase64Content(content string) ([]byte, error) {
}

func DecodeGzipContent(content string) ([]byte, error) {
gzr, err := gzip.NewReader(bytes.NewReader([]byte(content)))
byteContent := []byte(content)
return DecompressGzip(byteContent)
}

func DecompressGzip(content []byte) ([]byte, error) {
gzr, err := gzip.NewReader(bytes.NewReader(content))

if err != nil {
return nil, fmt.Errorf("Unable to decode gzip: %q", err)
Expand Down

0 comments on commit 39e97a6

Please sign in to comment.