-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix panic when pulling OCI-packaged helm chart (#228)
* check that diff IDs exist before dereference Previously, this function would panic when parsing OCI-packaged helm charts, which apparently have no diff IDs. Signed-off-by: Will Murphy <will.murphy@anchore.com> * go mod tidy Signed-off-by: Will Murphy <will.murphy@anchore.com> * Only attempt to parse supported layer types Helm charts were causing a panic, but even if parsing the layer metadata succeeded, an error would be returned. Therefore, just return the error pre-emptively on unknown layer media types, since this probably fixes undiscovered bugs similar to the helm chart panic. Signed-off-by: Will Murphy <will.murphy@anchore.com> * refactor Signed-off-by: Will Murphy <will.murphy@anchore.com> * clean up todo and unused file Signed-off-by: Will Murphy <will.murphy@anchore.com> * refactor: address some renames and other feedback Signed-off-by: Will Murphy <will.murphy@anchore.com> --------- Signed-off-by: Will Murphy <will.murphy@anchore.com>
- Loading branch information
1 parent
8eecb8f
commit 3873de5
Showing
6 changed files
with
181 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package image | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
v1Types "github.com/google/go-containerregistry/pkg/v1/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockLayer struct { | ||
mediaType v1Types.MediaType | ||
err error | ||
} | ||
|
||
func (m mockLayer) Digest() (v1.Hash, error) { | ||
return v1.Hash{ | ||
Algorithm: "sha256", | ||
Hex: "aaaaaaaaaa1234", | ||
}, nil | ||
} | ||
|
||
func (m mockLayer) DiffID() (v1.Hash, error) { | ||
return v1.Hash{ | ||
Algorithm: "sha256", | ||
Hex: "aaaaaaaaaa1234", | ||
}, nil | ||
} | ||
|
||
func (m mockLayer) Compressed() (io.ReadCloser, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (m mockLayer) Uncompressed() (io.ReadCloser, error) { | ||
return io.NopCloser(strings.NewReader("")), nil | ||
} | ||
|
||
func (m mockLayer) Size() (int64, error) { | ||
return 0, nil | ||
} | ||
|
||
func (m mockLayer) MediaType() (v1Types.MediaType, error) { | ||
return m.mediaType, m.err | ||
} | ||
|
||
var _ v1.Layer = &mockLayer{} | ||
|
||
func fakeLayer(mediaType v1Types.MediaType, err error) v1.Layer { | ||
return mockLayer{ | ||
mediaType: mediaType, | ||
err: err, | ||
} | ||
} | ||
|
||
func TestRead(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
mediaType v1Types.MediaType | ||
mediaTypeErr error | ||
wantErrContents string | ||
}{ | ||
{ | ||
name: "unsupported media type", | ||
mediaType: "garbage", | ||
mediaTypeErr: nil, | ||
wantErrContents: "unknown layer media type: garbage", | ||
}, | ||
{ | ||
name: "unsupported media type: helm chart", | ||
mediaType: "application/vnd.cncf.helm.chart.content.v1.tar+gzip", | ||
wantErrContents: "application/vnd.cncf.helm.chart.content.v1.tar+gzip", | ||
}, | ||
{ | ||
name: "err on media type returned", | ||
mediaTypeErr: errors.New("no media type for you"), | ||
wantErrContents: "no media type for you", | ||
}, | ||
{ | ||
name: "no error", | ||
mediaType: v1Types.DockerLayer, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
layer := Layer{layer: fakeLayer(tt.mediaType, tt.mediaTypeErr)} | ||
catalog := NewFileCatalog() | ||
err := layer.Read(catalog, 0, t.TempDir()) | ||
if tt.wantErrContents != "" { | ||
require.ErrorContains(t, err, tt.wantErrContents) | ||
return | ||
} | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} |