Skip to content

Commit

Permalink
fix 20518 (#20521)
Browse files Browse the repository at this point in the history
fixes #20518
Since there is no report for the replicated artifact, update to use the artifact id to remove accessories.

Signed-off-by: wang yan <[email protected]>
Co-authored-by: Shengwen YU <[email protected]>
  • Loading branch information
wy65701436 and Shengwen YU authored May 30, 2024
1 parent 1a36a95 commit d847590
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/pkg/scan/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,15 @@ func retrieveSBOMContent(rawReport string) ([]byte, *v1.Scanner, error) {
}

func (h *scanHandler) MakePlaceHolder(ctx context.Context, art *artifact.Artifact, r *scanner.Registration) (rps []*scanModel.Report, err error) {
var reports []*scanModel.Report
mgr := h.SBOMMgrFunc()
mimeTypes := r.GetProducesMimeTypes(art.ManifestMediaType, v1.ScanTypeSbom)
if len(mimeTypes) == 0 {
return nil, errors.New("no mime types to make report placeholders")
}
sbomReports, err := mgr.GetBy(h.cloneCtx(ctx), art.ID, r.UUID, mimeTypes[0], sbomMediaTypeSpdx)
if err != nil {
return nil, err
}
if err := h.deleteSBOMAccessories(ctx, sbomReports); err != nil {
if err := h.delete(ctx, art, mimeTypes[0], r); err != nil {
return nil, err
}
var reports []*scanModel.Report
for _, mt := range mimeTypes {
report := &sbom.Report{
ArtifactID: art.ID,
Expand Down Expand Up @@ -241,20 +237,24 @@ func (h *scanHandler) MakePlaceHolder(ctx context.Context, art *artifact.Artifac
return reports, nil
}

// deleteSBOMAccessories delete the sbom accessory in reports
func (h *scanHandler) deleteSBOMAccessories(ctx context.Context, reports []*sbom.Report) error {
// delete deletes the sbom report and accessory
func (h *scanHandler) delete(ctx context.Context, art *artifact.Artifact, mimeTypes string, r *scanner.Registration) error {
mgr := h.SBOMMgrFunc()
for _, rpt := range reports {
sbomReports, err := mgr.GetBy(h.cloneCtx(ctx), art.ID, r.UUID, mimeTypes, sbomMediaTypeSpdx)
if err != nil {
return err
}
for _, rpt := range sbomReports {
if rpt.MimeType != v1.MimeTypeSBOMReport {
continue
}
if err := h.deleteSBOMAccessory(ctx, rpt.ArtifactID); err != nil {
return err
}
if err := mgr.Delete(ctx, rpt.UUID); err != nil {
return err
}
}
if err := h.deleteSBOMAccessory(ctx, art.ID); err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit d847590

Please sign in to comment.