Skip to content

Commit

Permalink
update default processor for unknwon type config
Browse files Browse the repository at this point in the history
update OCI-Subject header

Signed-off-by: yminer <[email protected]>
  • Loading branch information
MinerYang committed Sep 19, 2023
1 parent 4051b2b commit 47ea46b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/controller/artifact/processor/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,21 @@ 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
}
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
Expand Down
50 changes: 50 additions & 0 deletions src/controller/artifact/processor/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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{})
}
4 changes: 3 additions & 1 deletion src/server/middleware/subject/subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ func Middleware() func(http.Handler) http.Handler {
return err
}
}
w.Header().Set("OCI-Subject", subjectArt.Digest)
// when subject artifact is pushed after accessory artifact, current subject artifact do not exist.
// so we use reference manifest subject digest instead of subjectArt.Digest
w.Header().Set("OCI-Subject", mf.Subject.Digest.String())
}

return nil
Expand Down

0 comments on commit 47ea46b

Please sign in to comment.