Skip to content

Commit 35b354b

Browse files
fix(mutate): preserve config blob and layers for non-Docker OCI artifacts (#2286)
* fix(mutate): preserve config blob and layers for non-Docker OCI artifacts compute() unconditionally parses the config as a Docker image config and derives layers from RootFS.DiffIDs. For non-Docker OCI artifacts (Helm charts, WASM modules), the config has no DiffIDs, causing Layers() to return empty (remote.Write skips blob uploads) and RawConfigFile() to corrupt the config digest via re-marshaling. Detect non-image configs by checking the config media type. When it is not DockerConfigJSON or OCIConfigJSON, delegate to computeArtifact which preserves the original config blob and derives layers from the base image and manifest descriptors. Fixes #2251 Signed-off-by: Dayna Blackwell <[email protected]> * test: replace no-op _ = layers with assertion per review * fix: rename unused originalManifest parameter to _ (revive lint) * fix: remove unused originalManifest parameter from computeArtifact per review * refactor: inline artifact logic into compute(), remove computeArtifact Per review feedback: - Inline artifact-specific behavior as conditional blocks within compute() instead of a separate computeArtifact() function - Share manifest layer loop, annotations, mediaType, and subject assignment between both paths - Trim isImageConfig comment - Layers() artifact fallback stays inline (no new abstractions) Net -70 lines. * Combine Layers() artifact check into compute error branch Address review feedback: - Remove comment at line 124 - Merge the !isImageConfig block into the ErrNotComputed condition --------- Signed-off-by: Dayna Blackwell <[email protected]>
1 parent e5983f2 commit 35b354b

2 files changed

Lines changed: 131 additions & 47 deletions

File tree

‎pkg/v1/mutate/image.go‎

Lines changed: 77 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func (i *image) MediaType() (types.MediaType, error) {
5252
return i.base.MediaType()
5353
}
5454

55+
// isImageConfig reports whether the media type is a Docker or OCI image config.
56+
func isImageConfig(mt types.MediaType) bool {
57+
switch mt {
58+
case types.DockerConfigJSON, types.OCIConfigJSON:
59+
return true
60+
default:
61+
return false
62+
}
63+
}
64+
5565
func (i *image) compute() error {
5666
i.Lock()
5767
defer i.Unlock()
@@ -60,6 +70,24 @@ func (i *image) compute() error {
6070
if i.computed {
6171
return nil
6272
}
73+
74+
m, err := i.base.Manifest()
75+
if err != nil {
76+
return err
77+
}
78+
manifest := m.DeepCopy()
79+
80+
diffIDMap := make(map[v1.Hash]v1.Layer)
81+
digestMap := make(map[v1.Hash]v1.Layer)
82+
83+
// Determine effective config media type (user override takes precedence).
84+
cfgMediaType := manifest.Config.MediaType
85+
if i.configMediaType != nil {
86+
cfgMediaType = *i.configMediaType
87+
}
88+
89+
imageConfig := isImageConfig(cfgMediaType)
90+
6391
var configFile *v1.ConfigFile
6492
if i.configFile != nil {
6593
configFile = i.configFile
@@ -70,33 +98,32 @@ func (i *image) compute() error {
7098
}
7199
configFile = cf.DeepCopy()
72100
}
73-
diffIDs := configFile.RootFS.DiffIDs
74-
history := configFile.History
75101

76-
diffIDMap := make(map[v1.Hash]v1.Layer)
77-
digestMap := make(map[v1.Hash]v1.Layer)
102+
// For image configs, update RootFS.DiffIDs and History from added layers.
103+
// For artifacts, skip this: the config has no rootfs or history fields.
104+
if imageConfig {
105+
diffIDs := configFile.RootFS.DiffIDs
106+
history := configFile.History
78107

79-
for _, add := range i.adds {
80-
history = append(history, add.History)
81-
if add.Layer != nil {
82-
diffID, err := add.Layer.DiffID()
83-
if err != nil {
84-
return err
108+
for _, add := range i.adds {
109+
history = append(history, add.History)
110+
if add.Layer != nil {
111+
diffID, err := add.Layer.DiffID()
112+
if err != nil {
113+
return err
114+
}
115+
diffIDs = append(diffIDs, diffID)
116+
diffIDMap[diffID] = add.Layer
85117
}
86-
diffIDs = append(diffIDs, diffID)
87-
diffIDMap[diffID] = add.Layer
88118
}
89-
}
90119

91-
m, err := i.base.Manifest()
92-
if err != nil {
93-
return err
120+
configFile.RootFS.DiffIDs = diffIDs
121+
configFile.History = history
94122
}
95-
manifest := m.DeepCopy()
123+
96124
manifestLayers := manifest.Layers
97125
for _, add := range i.adds {
98126
if add.Layer == nil {
99-
// Empty layers include only history in manifest.
100127
continue
101128
}
102129

@@ -105,57 +132,51 @@ func (i *image) compute() error {
105132
return err
106133
}
107134

108-
// Fields in the addendum override the original descriptor.
109135
if len(add.Annotations) != 0 {
110136
desc.Annotations = add.Annotations
111137
}
112138
if len(add.URLs) != 0 {
113139
desc.URLs = add.URLs
114140
}
115-
116141
if add.MediaType != "" {
117142
desc.MediaType = add.MediaType
118143
}
119144

120145
manifestLayers = append(manifestLayers, *desc)
121146
digestMap[desc.Digest] = add.Layer
122147
}
123-
124-
configFile.RootFS.DiffIDs = diffIDs
125-
configFile.History = history
126-
127148
manifest.Layers = manifestLayers
128149

129-
rcfg, err := json.Marshal(configFile)
130-
if err != nil {
131-
return err
132-
}
133-
d, sz, err := v1.SHA256(bytes.NewBuffer(rcfg))
134-
if err != nil {
135-
return err
136-
}
137-
manifest.Config.Digest = d
138-
manifest.Config.Size = sz
150+
// For image configs, re-marshal the config and update the manifest digest.
151+
// For artifacts, preserve the original config blob as-is to avoid
152+
// corrupting the digest via re-marshaling.
153+
if imageConfig {
154+
rcfg, err := json.Marshal(configFile)
155+
if err != nil {
156+
return err
157+
}
158+
d, sz, err := v1.SHA256(bytes.NewBuffer(rcfg))
159+
if err != nil {
160+
return err
161+
}
162+
manifest.Config.Digest = d
163+
manifest.Config.Size = sz
139164

140-
// If Data was set in the base image, we need to update it in the mutated image.
141-
if m.Config.Data != nil {
142-
manifest.Config.Data = rcfg
165+
if m.Config.Data != nil {
166+
manifest.Config.Data = rcfg
167+
}
143168
}
144169

145-
// If the user wants to mutate the media type of the config
146170
if i.configMediaType != nil {
147171
manifest.Config.MediaType = *i.configMediaType
148172
}
149-
150173
if i.mediaType != nil {
151174
manifest.MediaType = *i.mediaType
152175
}
153-
154176
if i.annotations != nil {
155177
if manifest.Annotations == nil {
156178
manifest.Annotations = map[string]string{}
157179
}
158-
159180
for k, v := range i.annotations {
160181
manifest.Annotations[k] = v
161182
}
@@ -173,16 +194,18 @@ func (i *image) compute() error {
173194
// Layers returns the ordered collection of filesystem layers that comprise this image.
174195
// The order of the list is oldest/base layer first, and most-recent/top layer last.
175196
func (i *image) Layers() ([]v1.Layer, error) {
176-
if err := i.compute(); errors.Is(err, stream.ErrNotComputed) {
177-
// Image contains a streamable layer which has not yet been
178-
// consumed. Just return the layers we have in case the caller
179-
// is going to consume the layers.
197+
if err := i.compute(); errors.Is(err, stream.ErrNotComputed) || (i.manifest != nil && !isImageConfig(i.manifest.Config.MediaType)) {
198+
// Stream not yet consumed, or non-image OCI artifact (RootFS.DiffIDs
199+
// is empty so partial.DiffIDs returns nothing). Fall back to the base
200+
// layers plus any added layers.
180201
layers, err := i.base.Layers()
181202
if err != nil {
182203
return nil, err
183204
}
184205
for _, add := range i.adds {
185-
layers = append(layers, add.Layer)
206+
if add.Layer != nil {
207+
layers = append(layers, add.Layer)
208+
}
186209
}
187210
return layers, nil
188211
} else if err != nil {
@@ -220,11 +243,18 @@ func (i *image) ConfigFile() (*v1.ConfigFile, error) {
220243
return i.configFile.DeepCopy(), nil
221244
}
222245

223-
// RawConfigFile returns the serialized bytes of ConfigFile()
246+
// RawConfigFile returns the serialized bytes of ConfigFile().
247+
// For non-image OCI artifacts, returns the original raw config to preserve
248+
// the config blob digest.
224249
func (i *image) RawConfigFile() ([]byte, error) {
225250
if err := i.compute(); err != nil {
226251
return nil, err
227252
}
253+
// If the manifest config is not a standard image config, return the
254+
// original raw bytes to avoid corrupting the digest via re-marshaling.
255+
if i.manifest != nil && !isImageConfig(i.manifest.Config.MediaType) {
256+
return i.base.RawConfigFile()
257+
}
228258
return json.Marshal(i.configFile)
229259
}
230260

‎pkg/v1/mutate/mutate_test.go‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,60 @@ func TestMutateMediaType(t *testing.T) {
619619
}
620620
}
621621

622+
// TestNonImageArtifactPreservesConfig verifies that mutating an OCI artifact
623+
// with a non-Docker config type (e.g., Helm chart, WASM module) preserves the
624+
// original config blob and correctly returns layers.
625+
// Regression test for https://github.com/google/go-containerregistry/issues/2251
626+
func TestNonImageArtifactPreservesConfig(t *testing.T) {
627+
// Start with an empty image and set a non-image config media type
628+
// to simulate a Helm chart or WASM artifact.
629+
helmConfigType := types.MediaType("application/vnd.cncf.helm.config.v1+json")
630+
img := mutate.ConfigMediaType(empty.Image, helmConfigType)
631+
img = mutate.MediaType(img, types.OCIManifestSchema1)
632+
633+
// Add an annotation to trigger compute().
634+
img = mutate.Annotations(img, map[string]string{
635+
"org.opencontainers.image.title": "test-artifact",
636+
}).(v1.Image)
637+
638+
// Layers() should not panic or return an error.
639+
layers, err := img.Layers()
640+
if err != nil {
641+
t.Fatalf("Layers() failed on artifact: %v", err)
642+
}
643+
if len(layers) != 0 {
644+
t.Errorf("expected 0 layers for empty artifact, got %d", len(layers))
645+
}
646+
647+
// Manifest should be valid and reference the correct config type.
648+
manifest, err := img.Manifest()
649+
if err != nil {
650+
t.Fatalf("Manifest() failed: %v", err)
651+
}
652+
if manifest.Config.MediaType != helmConfigType {
653+
t.Errorf("config media type = %v, want %v", manifest.Config.MediaType, helmConfigType)
654+
}
655+
656+
// The config digest should not have been corrupted by re-marshaling
657+
// through the Docker ConfigFile struct.
658+
rawCfg, err := img.RawConfigFile()
659+
if err != nil {
660+
t.Fatalf("RawConfigFile() failed: %v", err)
661+
}
662+
d, _, err := v1.SHA256(bytes.NewReader(rawCfg))
663+
if err != nil {
664+
t.Fatalf("SHA256 failed: %v", err)
665+
}
666+
if manifest.Config.Digest != d {
667+
t.Errorf("config digest mismatch: manifest says %v, raw config hashes to %v", manifest.Config.Digest, d)
668+
}
669+
670+
// Annotations should be present.
671+
if manifest.Annotations["org.opencontainers.image.title"] != "test-artifact" {
672+
t.Errorf("annotation not found in manifest")
673+
}
674+
}
675+
622676
func TestAppendStreamableLayer(t *testing.T) {
623677
img, err := mutate.AppendLayers(
624678
sourceImage(t),

0 commit comments

Comments
 (0)