Skip to content

Commit

Permalink
fix(config): check for config media type when pushing to repodb
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentiuNiculae committed Jul 28, 2023
1 parent b3ce62a commit cd4d1ce
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 49 deletions.
4 changes: 2 additions & 2 deletions pkg/cli/search_cmd_referrers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestReferrersSearchers(t *testing.T) {
So(ok, ShouldBeFalse)
})

Convey("GetRepoRefference fails", func() {
Convey("GetRepoReference fails", func() {
conf := searchConfig{
params: map[string]*string{
"subject": ref("bad-subject"),
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestReferrersSearchers(t *testing.T) {
So(ok, ShouldBeFalse)
})

Convey("GetRepoRefference fails", func() {
Convey("GetRepoReference fails", func() {
conf := searchConfig{
params: map[string]*string{
"subject": ref("bad-subject"),
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ func (search referrerSearcherGQL) search(config searchConfig) (bool, error) {

username, password := getUsernameAndPassword(*config.user)

repo, ref, refIsTag, err := zcommon.GetRepoRefference(*config.params["subject"])
repo, ref, refIsTag, err := zcommon.GetRepoReference(*config.params["subject"])
if err != nil {
return true, err
}
Expand Down Expand Up @@ -692,7 +692,7 @@ func (search referrerSearcher) search(config searchConfig) (bool, error) {

username, password := getUsernameAndPassword(*config.user)

repo, ref, refIsTag, err := zcommon.GetRepoRefference(*config.params["subject"])
repo, ref, refIsTag, err := zcommon.GetRepoReference(*config.params["subject"])
if err != nil {
return true, err
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"syscall"
"time"
"unicode/utf8"

ispec "github.com/opencontainers/image-spec/specs-go/v1"
)

const (
Expand Down Expand Up @@ -112,3 +114,14 @@ func ContainsString(strSlice []string, str string) bool {

return false
}

func UnmashalImageConfig(blob []byte) ispec.Image {
var configContent ispec.Image

err := json.Unmarshal(blob, &configContent)
if err != nil {
return ispec.Image{}
}

return configContent
}
4 changes: 2 additions & 2 deletions pkg/common/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func GetImageLastUpdated(imageInfo ispec.Image) time.Time {
return *timeStamp
}

// GetRepoRefference returns the components of a repoName:tag or repoName@digest string. If the format is wrong
// GetRepoReference returns the components of a repoName:tag or repoName@digest string. If the format is wrong
// an error is returned.
// The returned values have the following meaning:
//
Expand All @@ -95,7 +95,7 @@ func GetImageLastUpdated(imageInfo ispec.Image) time.Time {
// - bool: value for the statement: "the reference is a tag"
//
// - error: error value.
func GetRepoRefference(repo string) (string, string, bool, error) {
func GetRepoReference(repo string) (string, string, bool, error) {
repoName, digest, found := strings.Cut(repo, "@")

if !found {

Check failure on line 101 in pkg/common/oci.go

View workflow job for this annotation

GitHub Actions / coverage (linux, amd64)

condition "found" was never evaluated
Expand Down
20 changes: 2 additions & 18 deletions pkg/extensions/search/convert/metadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,7 @@ func ImageManifest2ImageSummary(ctx context.Context, repo, tag string, digest go
return &gql_generated.ImageSummary{}, map[string]int64{}, err
}

var configContent ispec.Image

err = json.Unmarshal(manifestMeta.ConfigBlob, &configContent)
if err != nil {
graphql.AddError(ctx, gqlerror.Errorf("can't unmarshal config blob for image: %s:%s, manifest digest: %s, error: %s",
repo, tag, manifestDigest, err.Error()))

return &gql_generated.ImageSummary{}, map[string]int64{}, err
}
configContent := zcommon.UnmashalImageConfig(manifestMeta.ConfigBlob)

var (
repoName = repo
Expand Down Expand Up @@ -485,15 +477,7 @@ func ImageManifest2ManifestSummary(ctx context.Context, repo, tag string, descri
return &gql_generated.ManifestSummary{}, map[string]int64{}, err
}

var configContent ispec.Image

err = json.Unmarshal(manifestMeta.ConfigBlob, &configContent)
if err != nil {
graphql.AddError(ctx, gqlerror.Errorf("can't unmarshal config blob for image: %s:%s, manifest digest: %s, error: %s",
repo, tag, digest, err.Error()))

return &gql_generated.ManifestSummary{}, map[string]int64{}, err
}
configContent := zcommon.UnmashalImageConfig(manifestMeta.ConfigBlob)

var (
manifestDigestStr = digest.String()
Expand Down
7 changes: 1 addition & 6 deletions pkg/extensions/search/cve/cve.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,7 @@ func getConfigAndDigest(metaDB mTypes.MetaDB, manifestDigestStr string) (ispec.I
return ispec.Image{}, "", err
}

var configContent ispec.Image

err = json.Unmarshal(manifestData.ConfigBlob, &configContent)
if err != nil {
return ispec.Image{}, "", err
}
configContent := zcommon.UnmashalImageConfig(manifestData.ConfigBlob)

return configContent, manifestDigest, nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/meta/boltdb/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,17 @@ func (bdw BoltDB) SetReferrer(repo string, referredDigest godigest.Digest, refer
return err
}

refferers := repoMeta.Referrers[referredDigest.String()]
referrers := repoMeta.Referrers[referredDigest.String()]

for i := range refferers {
if refferers[i].Digest == referrer.Digest {
for i := range referrers {
if referrers[i].Digest == referrer.Digest {
return nil
}
}

refferers = append(refferers, referrer)
referrers = append(referrers, referrer)

repoMeta.Referrers[referredDigest.String()] = refferers
repoMeta.Referrers[referredDigest.String()] = referrers

repoMetaBlob, err = json.Marshal(repoMeta)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/meta/dynamodb/dynamo.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,17 @@ func (dwr DynamoDB) SetReferrer(repo string, referredDigest godigest.Digest, ref
}
}

refferers := repoMeta.Referrers[referredDigest.String()]
referrers := repoMeta.Referrers[referredDigest.String()]

for i := range refferers {
if refferers[i].Digest == referrer.Digest {
for i := range referrers {
if referrers[i].Digest == referrer.Digest {
return nil
}
}

refferers = append(refferers, referrer)
referrers = append(referrers, referrer)

repoMeta.Referrers[referredDigest.String()] = refferers
repoMeta.Referrers[referredDigest.String()] = referrers

return dwr.SetRepoMeta(repo, repoMeta)
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/meta/storage_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,11 @@ func NewManifestData(repoName string, manifestBlob []byte, imageStore storageTyp
return mTypes.ManifestData{}, err
}

err = json.Unmarshal(configBlob, &configContent)
if err != nil {
return mTypes.ManifestData{}, err
if manifestContent.Config.MediaType == ispec.MediaTypeImageConfig {
err = json.Unmarshal(configBlob, &configContent)
if err != nil {
return mTypes.ManifestData{}, err
}
}

manifestData.ManifestBlob = manifestBlob
Expand Down Expand Up @@ -381,9 +383,9 @@ func SetImageMetaFromInput(repo, reference, mediaType string, digest godigest.Di
}
}

refferredDigest, referrerInfo, hasSubject, err := GetReferredInfo(descriptorBlob, digest.String(), mediaType)
referredDigest, referrerInfo, hasSubject, err := GetReferredInfo(descriptorBlob, digest.String(), mediaType)
if hasSubject && err == nil {
err := metaDB.SetReferrer(repo, refferredDigest, referrerInfo)
err := metaDB.SetReferrer(repo, referredDigest, referrerInfo)
if err != nil {
log.Error().Err(err).Msg("metadb: error while settingg referrer")

Expand Down
8 changes: 4 additions & 4 deletions pkg/meta/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func OnUpdateManifest(repo, reference, mediaType string, digest godigest.Digest,
}
}
} else {
err := SetImageMetaFromInput(repo, reference, mediaType, digest, body,
err = SetImageMetaFromInput(repo, reference, mediaType, digest, body,
imgStore, metaDB, log)
if err != nil {
metadataSuccessfullySet = false
}
}

if !metadataSuccessfullySet {
log.Info().Str("tag", reference).Str("repository", repo).Msg("uploding image meta was unsuccessful for tag in repo")
log.Info().Str("tag", reference).Str("repository", repo).Msg("uploading image meta was unsuccessful for tag in repo")

if err := imgStore.DeleteImageManifest(repo, reference, false); err != nil {
log.Error().Err(err).Str("reference", reference).Str("repository", repo).
Expand Down Expand Up @@ -122,8 +122,8 @@ func OnDeleteManifest(repo, reference, mediaType string, digest godigest.Digest,
manageRepoMetaSuccessfully = false
}

if refferredDigest, hasSubject := common.GetReferredSubject(manifestBlob); hasSubject {
err := metaDB.DeleteReferrer(repo, refferredDigest, digest)
if referredDigest, hasSubject := common.GetReferredSubject(manifestBlob); hasSubject {
err := metaDB.DeleteReferrer(repo, referredDigest, digest)
if err != nil {
log.Error().Err(err).Msg("metadb: error while deleting referrer")

Expand Down

0 comments on commit cd4d1ce

Please sign in to comment.