From 536699ad60498b77379e528fd4961023ae1b186f Mon Sep 17 00:00:00 2001 From: Matt Moore Date: Sat, 2 Jun 2018 12:02:28 -0700 Subject: [PATCH] Recompute the size in the same places that we recompute the digest. Fixes: https://github.com/google/go-containerregistry/issues/192 --- v1/mutate/mutate.go | 18 ++++++++++++++++-- v1/mutate/mutate_test.go | 25 +++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/v1/mutate/mutate.go b/v1/mutate/mutate.go index 457127ea71..0ebeb01e65 100644 --- a/v1/mutate/mutate.go +++ b/v1/mutate/mutate.go @@ -16,6 +16,7 @@ package mutate import ( "archive/tar" + "bytes" "encoding/json" "errors" "fmt" @@ -110,10 +111,17 @@ func Append(base v1.Image, adds ...Addendum) (v1.Image, error) { image.configFile.RootFS.DiffIDs = diffIDs image.configFile.History = history image.manifest.Layers = manifestLayers - image.manifest.Config.Digest, err = image.ConfigName() + + rcfg, err := image.RawConfigFile() + if err != nil { + return nil, err + } + d, sz, err := v1.SHA256(bytes.NewBuffer(rcfg)) if err != nil { return nil, err } + image.manifest.Config.Digest = d + image.manifest.Config.Size = sz return image, nil } @@ -139,10 +147,16 @@ func Config(base v1.Image, cfg v1.Config) (v1.Image, error) { digestMap: make(map[v1.Hash]v1.Layer), } - image.manifest.Config.Digest, err = image.ConfigName() + rcfg, err := image.RawConfigFile() + if err != nil { + return nil, err + } + d, sz, err := v1.SHA256(bytes.NewBuffer(rcfg)) if err != nil { return nil, err } + image.manifest.Config.Digest = d + image.manifest.Config.Size = sz return image, nil } diff --git a/v1/mutate/mutate_test.go b/v1/mutate/mutate_test.go index 3ff7fcc5ae..d152355cdb 100644 --- a/v1/mutate/mutate_test.go +++ b/v1/mutate/mutate_test.go @@ -172,6 +172,10 @@ func TestAppendWithHistory(t *testing.T) { t.Fatalf("correct layer was not appended (-got, +want) %v", diff) } + if configSizesAreEqual(t, source, result) { + t.Fatal("adding a layer MUST change the config file size") + } + cf := getConfigFile(t, result) if diff := cmp.Diff(cf.History[1], addendum.History); diff != "" { @@ -194,6 +198,10 @@ func TestAppendLayers(t *testing.T) { t.Fatal("appending a layer did not mutate the config file") } + if configSizesAreEqual(t, source, result) { + t.Fatal("adding a layer MUST change the config file size") + } + layers := getLayers(t, result) if got, want := len(layers), 2; got != want { @@ -232,6 +240,10 @@ func TestMutateConfig(t *testing.T) { t.Fatal("mutating the config did not mutate the config file") } + if configSizesAreEqual(t, source, result) { + t.Fatal("adding an enviornment variable MUST change the config file size") + } + if !reflect.DeepEqual(cfg.Config.Env, newEnv) { t.Fatalf("incorrect environment set %v!=%v", cfg.Config.Env, newEnv) } @@ -245,7 +257,7 @@ func TestMutateCreatedAt(t *testing.T) { t.Fatalf("failed to mutate a config: %v", err) } - if manifestDigestsAreEqual(t, source, result) { + if configDigestsAreEqual(t, source, result) { t.Fatal("mutating the created time MUST mutate the manifest digest") } @@ -340,7 +352,7 @@ func configFilesAreEqual(t *testing.T, first, second v1.Image) bool { return cmp.Equal(fc, sc) } -func manifestDigestsAreEqual(t *testing.T, first, second v1.Image) bool { +func configDigestsAreEqual(t *testing.T, first, second v1.Image) bool { t.Helper() fm := getManifest(t, first) @@ -349,6 +361,15 @@ func manifestDigestsAreEqual(t *testing.T, first, second v1.Image) bool { return fm.Config.Digest == sm.Config.Digest } +func configSizesAreEqual(t *testing.T, first, second v1.Image) bool { + t.Helper() + + fm := getManifest(t, first) + sm := getManifest(t, second) + + return fm.Config.Size == sm.Config.Size +} + func manifestsAreEqual(t *testing.T, first, second v1.Image) bool { t.Helper()