-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Patrik Beno <[email protected]>
- Loading branch information
1 parent
0aea55f
commit 6a04ac1
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package sbom | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/anchore/syft/internal/formats/cyclonedxjson" | ||
"github.com/anchore/syft/syft/artifact" | ||
"github.com/anchore/syft/syft/pkg" | ||
"github.com/anchore/syft/syft/pkg/cataloger/common" | ||
"io" | ||
) | ||
|
||
// NewSBOMCataloger returns a new SBOM cataloger object loaded from saved SBOM JSON. | ||
func NewSBOMCataloger() *common.GenericCataloger { | ||
globParsers := map[string]common.ParserFn{ | ||
"**/deps.json": parseSBOM, | ||
"**/sbom.json": parseSBOM, | ||
} | ||
|
||
return common.NewGenericCataloger(nil, globParsers, "sbom-cataloger") | ||
} | ||
|
||
func parseSBOM(path string, reader io.Reader) ([]*pkg.Package, []artifact.Relationship, error) { | ||
by, err := io.ReadAll(reader) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("unable to read sbom: %w", err) | ||
} | ||
|
||
f := cyclonedxjson.Format() | ||
|
||
s, err := f.Decode(bytes.NewReader(by)) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("unable to decode sbom: %w", err) | ||
} | ||
|
||
var packages []*pkg.Package | ||
for _, p := range s.Artifacts.PackageCatalog.Sorted() { | ||
x := p //copy | ||
packages = append(packages, &x) | ||
} | ||
|
||
return packages, nil, nil | ||
} |