[go: nahoru, domu]

Skip to content

Commit

Permalink
fix issue
Browse files Browse the repository at this point in the history
Signed-off-by: stonezdj <stone.zhang@broadcom.com>
  • Loading branch information
stonezdj committed May 23, 2024
1 parent ecb80ef commit d01af0a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
3 changes: 2 additions & 1 deletion make/migrations/postgresql/0140_2.11.0_schema.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ CREATE TABLE IF NOT EXISTS sbom_report
artifact_id INT NOT NULL,
registration_uuid VARCHAR(64) NOT NULL,
mime_type VARCHAR(256) NOT NULL,
media_type VARCHAR(256) NOT NULL,
report JSON,
UNIQUE(artifact_id, registration_uuid, mime_type)
UNIQUE(artifact_id, registration_uuid, mime_type, media_type)
);
17 changes: 10 additions & 7 deletions src/pkg/scan/sbom/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type Manager interface {
// Returns:
// []*Report : sbom report list
// error : non nil error if any errors occurred
GetBy(ctx context.Context, artifactID int64, registrationUUID string, mimeTypes []string) ([]*model.Report, error)
GetBy(ctx context.Context, artifactID int64, registrationUUID string, mimeType string, mediaType string) ([]*model.Report, error)
// List reports according to the query
//
// Arguments:
Expand Down Expand Up @@ -116,11 +116,11 @@ func NewManager() Manager {
func (bm *basicManager) Create(ctx context.Context, r *model.Report) (string, error) {
// Validate report object
if r == nil {
return "", errors.New("nil scan report object")
return "", errors.New("nil sbom report object")
}

if r.ArtifactID == 0 || len(r.RegistrationUUID) == 0 || len(r.MimeType) == 0 {
return "", errors.New("malformed scan report object")
if r.ArtifactID == 0 || len(r.RegistrationUUID) == 0 || len(r.MimeType) == 0 || len(r.MediaType) == 0 {
return "", errors.New("malformed sbom report object")
}

r.UUID = uuid.New().String()
Expand All @@ -147,7 +147,7 @@ func (bm *basicManager) Delete(ctx context.Context, uuid string) error {

// GetBy ...
func (bm *basicManager) GetBy(ctx context.Context, artifactID int64, registrationUUID string,
mimeTypes []string) ([]*model.Report, error) {
mimeType string, mediaType string) ([]*model.Report, error) {
if artifactID == 0 {
return nil, errors.New("no artifact id to get report data")
}
Expand All @@ -157,8 +157,11 @@ func (bm *basicManager) GetBy(ctx context.Context, artifactID int64, registratio
if len(registrationUUID) > 0 {
kws["registration_uuid"] = registrationUUID
}
if len(mimeTypes) > 0 {
kws["mime_type__in"] = mimeTypes
if len(mimeType) > 0 {
kws["mine_type"] = mimeType
}
if len(mediaType) > 0 {
kws["media_type"] = mediaType
}
// Query all
query := &q.Query{
Expand Down
1 change: 1 addition & 0 deletions src/pkg/scan/sbom/model/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Report struct {
ArtifactID int64 `orm:"column(artifact_id)"`
RegistrationUUID string `orm:"column(registration_uuid)"`
MimeType string `orm:"column(mime_type)"`
MediaType string `orm:"column(media_type)"`
Report string `orm:"column(report);type(json)"`
}

Expand Down
14 changes: 11 additions & 3 deletions src/pkg/scan/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,11 @@ func (h *scanHandler) MakeReportPlaceHolder(ctx context.Context, art *artifact.A
var reports []*scanModel.Report
mgr := h.SBOMMgrFunc()
mimeTypes := r.GetProducesMimeTypes(art.ManifestMediaType, v1.ScanTypeSbom)
sbomReports, err := mgr.GetBy(h.cloneCtx(ctx), art.ID, r.UUID, mimeTypes)
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
}
Expand All @@ -213,6 +217,7 @@ func (h *scanHandler) MakeReportPlaceHolder(ctx context.Context, art *artifact.A
ArtifactID: art.ID,
RegistrationUUID: r.UUID,
MimeType: mt,
MediaType: sbomMediaTypeSpdx,
}

create := func(ctx context.Context) error {
Expand Down Expand Up @@ -294,7 +299,7 @@ func (h *scanHandler) GetReportPlaceHolder(ctx context.Context, artRepo string,
return nil, err
}
mgr := h.SBOMMgrFunc()
rpts, err := mgr.GetBy(ctx, a.ID, scannerUUID, []string{mimeType})
rpts, err := mgr.GetBy(ctx, a.ID, scannerUUID, mimeType, sbomMediaTypeSpdx)
if err != nil {
logger.Errorf("Failed to get report for artifact %s@%s of mimetype %s, error %v", artRepo, artDigest, mimeType, err)
return nil, err
Expand All @@ -310,6 +315,9 @@ func (h *scanHandler) GetReportPlaceHolder(ctx context.Context, artRepo string,
}

func (h *scanHandler) GetSummary(ctx context.Context, art *artifact.Artifact, mimeTypes []string) (map[string]interface{}, error) {
if len(mimeTypes) == 0 {
return nil, errors.New("no mime types to get report summaries")
}
if art == nil {
return nil, errors.New("no way to get report summaries for nil artifact")
}
Expand All @@ -318,7 +326,7 @@ func (h *scanHandler) GetSummary(ctx context.Context, art *artifact.Artifact, mi
if err != nil {
return nil, errors.Wrap(err, "get sbom summary failed")
}
reports, err := h.SBOMMgrFunc().GetBy(ctx, art.ID, r.UUID, mimeTypes)
reports, err := h.SBOMMgrFunc().GetBy(ctx, art.ID, r.UUID, mimeTypes[0], sbomMediaTypeSpdx)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit d01af0a

Please sign in to comment.