diff --git a/src/controller/artifact/processor/default.go b/src/controller/artifact/processor/default.go index e8fea66f2..c10fa606f 100644 --- a/src/controller/artifact/processor/default.go +++ b/src/controller/artifact/processor/default.go @@ -101,7 +101,12 @@ func (d *defaultProcessor) AbstractMetadata(ctx context.Context, artifact *artif if err := json.Unmarshal(manifest, mani); err != nil { return err } - // get config layer + // if manifest.Config.Mediatype not comply with stanard config json regex (unknown type), + // regex will filter either none-json format config or empty config + if d.GetArtifactType(ctx, artifact) == ArtifactTypeUnknown { + return nil + } + // else get config layer _, blob, err := d.regCli.PullBlob(artifact.RepositoryName, mani.Config.Digest.String()) if err != nil { return err @@ -109,11 +114,8 @@ func (d *defaultProcessor) AbstractMetadata(ctx context.Context, artifact *artif defer blob.Close() // parse metadata from config layer metadata := map[string]interface{}{} - // Some artifact may not have empty config layer. - if mani.Config.Size != 0 { - if err := json.NewDecoder(blob).Decode(&metadata); err != nil { - return err - } + if err = json.NewDecoder(blob).Decode(&metadata); err != nil { + return err } // Populate all metadata into the ExtraAttrs first. artifact.ExtraAttrs = metadata diff --git a/src/controller/artifact/processor/default_test.go b/src/controller/artifact/processor/default_test.go index 9ba55a1fc..bdc04d54e 100644 --- a/src/controller/artifact/processor/default_test.go +++ b/src/controller/artifact/processor/default_test.go @@ -116,6 +116,31 @@ var ( } ] }` + v2ManifestWithUnknownConfig = `{ + "schemaVersion": 2, + "mediaType": "application/vnd.oci.image.manifest.v1+json", + "config": { + "mediaType": "application/vnd.nhl.peanut.butter.bagel", + "digest": "sha256:ee29d2e91da0e5dbf6536f5b369148a83ef59b0ce96e49da65dd6c25eb1fa44f", + "size": 33, + "newUnspecifiedField": null + }, + "layers": [ + { + "mediaType": "application/vnd.oci.empty.v1+json", + "digest": "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a", + "size": 2, + "newUnspecifiedField": "null" + } + ], + "subject": { + "mediaType": "application/vnd.oci.image.manifest.v1+json", + "digest": "sha256:5a01bbc4ce6f52541cbc7e6af4b22bb107991a4bdd433103ff65aeb00756e906", + "size": 714, + "newUnspecifiedField": null + } + }` + unknownConfig = `{NHL Peanut Butter on my NHL bagel}` ) type defaultProcessorTestSuite struct { @@ -146,6 +171,18 @@ func (d *defaultProcessorTestSuite) TestGetArtifactType() { typee = processor.GetArtifactType(nil, art) d.Equal(ArtifactTypeUnknown, typee) + mediaType = "application/vnd.oci.empty.v1+json" + art = &artifact.Artifact{MediaType: mediaType} + processor = &defaultProcessor{} + typee = processor.GetArtifactType(nil, art) + d.Equal(ArtifactTypeUnknown, typee) + + mediaType = "application/vnd.nhl.peanut.butter.bagel" + art = &artifact.Artifact{MediaType: mediaType} + processor = &defaultProcessor{} + typee = processor.GetArtifactType(nil, art) + d.Equal(ArtifactTypeUnknown, typee) + mediaType = "application/vnd.oci.image.config.v1+json" art = &artifact.Artifact{MediaType: mediaType} processor = &defaultProcessor{} @@ -185,6 +222,19 @@ func (d *defaultProcessorTestSuite) TestAbstractMetadata() { d.Require().Nil(err) } +func (d *defaultProcessorTestSuite) TestAbstractMetadataWithUnknownConfig() { + manifest, _, err := distribution.UnmarshalManifest(v1.MediaTypeImageManifest, []byte(v2ManifestWithUnknownConfig)) + d.Require().Nil(err) + manifestMediaType, content, err := manifest.Payload() + d.Require().Nil(err) + + art := &artifact.Artifact{ManifestMediaType: manifestMediaType} + err = d.processor.AbstractMetadata(nil, art, content) + d.Require().Nil(err) + d.Assert().Equal(35, len(unknownConfig)) + d.Assert().Equal(0, len(art.ExtraAttrs)) +} + func TestDefaultProcessorTestSuite(t *testing.T) { suite.Run(t, &defaultProcessorTestSuite{}) }