Skip to content

Commit

Permalink
refactor: use abstract SBOM in lib/snyk
Browse files Browse the repository at this point in the history
  • Loading branch information
mcombuechen authored and garethr committed Jun 20, 2023
1 parent 4f32fba commit f2596d5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
18 changes: 8 additions & 10 deletions internal/commands/snyk/enrich.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package snyk

import (
"bytes"
"os"

cdx "github.com/CycloneDX/cyclonedx-go"
"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/snyk/parlay/internal/utils"
"github.com/snyk/parlay/lib/sbom"
"github.com/snyk/parlay/lib/snyk"
)

Expand All @@ -23,16 +22,15 @@ func NewEnrichCommand(logger zerolog.Logger) *cobra.Command {
logger.Fatal().Err(err).Msg("Problem reading input")
}

bom := new(cdx.BOM)
decoder := cdx.NewBOMDecoder(bytes.NewReader(b), cdx.BOMFileFormatJSON)
if err = decoder.Decode(bom); err != nil {
logger.Fatal().Err(err).Msg("Problem decoding SBOM")
doc, err := sbom.DecodeSBOMDocument(b)
if err != nil {
logger.Fatal().Err(err).Msg("Failed to read SBOM input")
}

bom = snyk.EnrichSBOM(bom)
err = cdx.NewBOMEncoder(os.Stdout, cdx.BOMFileFormatJSON).Encode(bom)
if err != nil {
logger.Fatal().Err(err).Msg("Problem encoding SBOM")
snyk.EnrichSBOM(doc)

if err := doc.Encode(os.Stdout); err != nil {
logger.Fatal().Err(err).Msg("Failed to encode new SBOM")
}
},
}
Expand Down
3 changes: 2 additions & 1 deletion lib/scorecard/enrich_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (

cdx "github.com/CycloneDX/cyclonedx-go"
"github.com/jarcoal/httpmock"
"github.com/snyk/parlay/lib/sbom"
"github.com/stretchr/testify/assert"

"github.com/snyk/parlay/lib/sbom"
)

func TestEnrichSBOM(t *testing.T) {
Expand Down
9 changes: 6 additions & 3 deletions lib/snyk/enrich.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ import (
"github.com/package-url/packageurl-go"
"github.com/remeh/sizedwaitgroup"

"github.com/snyk/parlay/lib/sbom"
"github.com/snyk/parlay/snyk/issues"
)

func EnrichSBOM(bom *cdx.BOM) *cdx.BOM {
func EnrichSBOM(doc *sbom.SBOMDocument) *sbom.SBOMDocument {
bom := doc.BOM

if bom.Components == nil {
return bom
return doc
}

wg := sizedwaitgroup.New(20)
Expand Down Expand Up @@ -157,7 +160,7 @@ func EnrichSBOM(bom *cdx.BOM) *cdx.BOM {
}
}
bom.Vulnerabilities = &vulns
return bom
return doc
}

func levelToCdxSeverity(level *string) (severity cdx.Severity) {
Expand Down

0 comments on commit f2596d5

Please sign in to comment.