diff --git a/vendor/github.com/coreos/butane/base/util/url.go b/vendor/github.com/coreos/butane/base/util/url.go index 2cf3bef070..b7bc035924 100644 --- a/vendor/github.com/coreos/butane/base/util/url.go +++ b/vendor/github.com/coreos/butane/base/util/url.go @@ -24,9 +24,24 @@ import ( "github.com/vincent-petithory/dataurl" ) -func MakeDataURL(contents []byte, currentCompression *string, allowCompression bool) (uri string, gzipped bool, err error) { +func MakeDataURL(contents []byte, currentCompression *string, allowCompression bool) (uri string, compression *string, err error) { // try three different encodings, and select the smallest one + if util.NilOrEmpty(currentCompression) { + // The config does not specify compression. We need to + // explicitly set the compression field to avoid a child + // config inheriting a compression setting from the parent, + // which may not have used the same compression algorithm. + compression = util.StrToPtr("") + } else { + // The config specifies compression, meaning that the + // contents were compressed by the user, so we can pick a + // data URL encoding but we can't compress again. Return a + // nil compression value so the caller knows not to record a + // translation from input contents to output compression. + compression = nil + } + // URL-escaped, useful for ASCII text opaque := "," + dataurl.Escape(contents) @@ -53,10 +68,10 @@ func MakeDataURL(contents []byte, currentCompression *string, allowCompression b return } gz := ";base64," + base64.StdEncoding.EncodeToString(buf.Bytes()) - // Account for space needed by "compression": "gzip". - if len(gz)+25 < len(opaque) { + // Account for space needed by the compression value + if len(gz)+len("gzip") < len(opaque) { opaque = gz - gzipped = true + compression = util.StrToPtr("gzip") } } diff --git a/vendor/github.com/coreos/butane/base/v0_3/translate.go b/vendor/github.com/coreos/butane/base/v0_3/translate.go index cf8763423f..2f7fabd4f7 100644 --- a/vendor/github.com/coreos/butane/base/v0_3/translate.go +++ b/vendor/github.com/coreos/butane/base/v0_3/translate.go @@ -150,15 +150,15 @@ func translateResource(from Resource, options common.TranslateOptions) (to types return } - src, gzipped, err := baseutil.MakeDataURL(contents, to.Compression, !options.NoResourceAutoCompression) + src, compression, err := baseutil.MakeDataURL(contents, to.Compression, !options.NoResourceAutoCompression) if err != nil { r.AddOnError(c, err) return } to.Source = &src tm.AddTranslation(c, path.New("json", "source")) - if gzipped { - to.Compression = util.StrToPtr("gzip") + if compression != nil { + to.Compression = compression tm.AddTranslation(c, path.New("json", "compression")) } } @@ -166,15 +166,15 @@ func translateResource(from Resource, options common.TranslateOptions) (to types if from.Inline != nil { c := path.New("yaml", "inline") - src, gzipped, err := baseutil.MakeDataURL([]byte(*from.Inline), to.Compression, !options.NoResourceAutoCompression) + src, compression, err := baseutil.MakeDataURL([]byte(*from.Inline), to.Compression, !options.NoResourceAutoCompression) if err != nil { r.AddOnError(c, err) return } to.Source = &src tm.AddTranslation(c, path.New("json", "source")) - if gzipped { - to.Compression = util.StrToPtr("gzip") + if compression != nil { + to.Compression = compression tm.AddTranslation(c, path.New("json", "compression")) } } @@ -288,15 +288,15 @@ func walkTree(yamlPath path.ContextPath, ts *translate.TranslationSet, r *report r.AddOnError(yamlPath, err) return nil } - url, gzipped, err := baseutil.MakeDataURL(contents, file.Contents.Compression, !options.NoResourceAutoCompression) + url, compression, err := baseutil.MakeDataURL(contents, file.Contents.Compression, !options.NoResourceAutoCompression) if err != nil { r.AddOnError(yamlPath, err) return nil } - file.Contents.Source = util.StrToPtr(url) + file.Contents.Source = &url ts.AddTranslation(yamlPath, path.New("json", "storage", "files", i, "contents", "source")) - if gzipped { - file.Contents.Compression = util.StrToPtr("gzip") + if compression != nil { + file.Contents.Compression = compression ts.AddTranslation(yamlPath, path.New("json", "storage", "files", i, "contents", "compression")) } ts.AddTranslation(yamlPath, path.New("json", "storage", "files", i, "contents"))