Skip to content

Commit

Permalink
fix(oci): handling of complex image indexes (aquasecurity#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
afdesk authored and ankk13 committed Oct 27, 2021
1 parent 44131d8 commit b2d6b7e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
12 changes: 11 additions & 1 deletion image/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,21 @@ func tryOCI(fileName string) (v1.Image, error) {
func getOCIImage(m *v1.IndexManifest, index v1.ImageIndex, inputTag string) (v1.Image, error) {
for _, manifest := range m.Manifests {
annotation := manifest.Annotations

tag := annotation[ispec.AnnotationRefName]
if inputTag == "" || // always select the first digest
tag == inputTag {
h := manifest.Digest
if manifest.MediaType.IsIndex() {
childIndex, err := index.ImageIndex(h)
if err != nil {
return nil, xerrors.Errorf("unable to retrieve a child image %q: %w", h.String(), err)
}
childManifest, err := childIndex.IndexManifest()
if err != nil {
return nil, xerrors.Errorf("invalid a child manifest for %q: %w", h.String(), err)
}
return getOCIImage(childManifest, childIndex, "")
}

img, err := index.Image(h)
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions image/oci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package image

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestTryOCI(t *testing.T) {
tests := []struct{
name string
ociImagePath string
wantErr string
}{
{
name: "correct path to index without tag",
ociImagePath: "testdata/multi",
wantErr: "",
},
{
name: "correct path to index with correct tag",
ociImagePath: "testdata/multi:tg11",
wantErr: "",
},
{
name: "correct path to index with incorrect tag",
ociImagePath: "testdata/multi:tg12",
wantErr: "invalid OCI image tag",
},
{
name: "correct path to manifest without tag",
ociImagePath: "testdata/single",
wantErr: "",
},
{
name: "correct path to manifest with correct tag",
ociImagePath: "testdata/single:3.14",
wantErr: "",
},
{
name: "correct path to manifest with incorrect tag",
ociImagePath: "testdata/single:3.11",
wantErr: "invalid OCI image tag",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := tryOCI(test.ociImagePath)
if test.wantErr != "" {
assert.NotNil(t, err)
assert.Contains(t, err.Error(), test.wantErr, err)
} else {
assert.NoError(t, err)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"schemaVersion":2,"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:56ae38f2f5c54b98311b8b2463d4861368c451ac17098f4227d84946b42ab96d","size":348,"platform":{"architecture":"amd64","os":"linux"}},{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:d4797ba2a3f15e3fe4c13398104f2199cc3fe22da004c9d382a60b74990136ad","size":348,"platform":{"architecture":"arm","os":"linux"}}]}
1 change: 1 addition & 0 deletions image/testdata/multi/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"schemaVersion":2,"manifests":[{"mediaType":"application/vnd.oci.image.index.v1+json","digest":"sha256:56f658ee7c94c1a65099c680916c12f6b81ae4c586c662a8146791054fa466ab","size":435,"annotations":{"org.opencontainers.image.ref.name":"tg11"}}]}
1 change: 1 addition & 0 deletions image/testdata/single/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"schemaVersion":2,"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:56ae38f2f5c54b98311b8b2463d4861368c451ac17098f4227d84946b42ab96d","size":348,"annotations":{"org.opencontainers.image.ref.name":"3.14"}}]}

0 comments on commit b2d6b7e

Please sign in to comment.