Skip to content

Commit

Permalink
port dotnet cataloger to new generic cataloger pattern (anchore#1286)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>

Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman authored Oct 24, 2022
1 parent 4ef0a7d commit 05eee23
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 135 deletions.
13 changes: 6 additions & 7 deletions syft/pkg/cataloger/dotnet/cataloger.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package dotnet

import (
"github.com/anchore/syft/syft/pkg/cataloger/common"
"github.com/anchore/syft/syft/pkg/cataloger/generic"
)

// NewDotnetDepsCataloger returns a new Dotnet cataloger object base on deps json files.
func NewDotnetDepsCataloger() *common.GenericCataloger {
globParsers := map[string]common.ParserFn{
"**/*.deps.json": parseDotnetDeps,
}
const catalogerName = "dotnet-deps-cataloger"

return common.NewGenericCataloger(nil, globParsers, "dotnet-deps-cataloger")
// NewDotnetDepsCataloger returns a new Dotnet cataloger object base on deps json files.
func NewDotnetDepsCataloger() *generic.Cataloger {
return generic.NewCataloger(catalogerName).
WithParserByGlobs(parseDotnetDeps, "**/*.deps.json")
}
55 changes: 55 additions & 0 deletions syft/pkg/cataloger/dotnet/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package dotnet

import (
"strings"

"github.com/anchore/packageurl-go"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)

func newDotnetDepsPackage(nameVersion string, lib dotnetDepsLibrary, locations ...source.Location) *pkg.Package {
if lib.Type != "package" {
return nil
}

fields := strings.Split(nameVersion, "/")
name := fields[0]
version := fields[1]

m := pkg.DotnetDepsMetadata{
Name: name,
Version: version,
Path: lib.Path,
Sha512: lib.Sha512,
HashPath: lib.HashPath,
}

p := &pkg.Package{
Name: name,
Version: version,
Locations: source.NewLocationSet(locations...),
PURL: packageURL(m),
Language: pkg.Dotnet,
Type: pkg.DotnetPkg,
MetadataType: pkg.DotnetDepsMetadataType,
Metadata: m,
}

p.SetID()

return p
}

func packageURL(m pkg.DotnetDepsMetadata) string {
var qualifiers packageurl.Qualifiers

return packageurl.NewPackageURL(
packageurl.TypeDotnet,
"",
m.Name,
m.Version,
qualifiers,
"",
).ToString()
}
54 changes: 19 additions & 35 deletions syft/pkg/cataloger/dotnet/parse_dotnet_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package dotnet
import (
"encoding/json"
"fmt"
"io"
"strings"
"sort"

"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/common"
"github.com/anchore/syft/syft/pkg/cataloger/generic"
"github.com/anchore/syft/syft/source"
)

// integrity check
var _ common.ParserFn = parseDotnetDeps
var _ generic.Parser = parseDotnetDeps

type dotnetDeps struct {
Libraries map[string]dotnetDepsLibrary `json:"libraries"`
Expand All @@ -25,8 +24,8 @@ type dotnetDepsLibrary struct {
HashPath string `json:"hashPath"`
}

func parseDotnetDeps(path string, reader io.Reader) ([]*pkg.Package, []artifact.Relationship, error) {
var packages []*pkg.Package
func parseDotnetDeps(_ source.FileResolver, _ *generic.Environment, reader source.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
var pkgs []pkg.Package

dec := json.NewDecoder(reader)

Expand All @@ -35,38 +34,23 @@ func parseDotnetDeps(path string, reader io.Reader) ([]*pkg.Package, []artifact.
return nil, nil, fmt.Errorf("failed to parse deps.json file: %w", err)
}

for nameVersion, lib := range p.Libraries {
dotnetPkg := newDotnetDepsPackage(nameVersion, lib)
var names []string

if dotnetPkg != nil {
packages = append(packages, dotnetPkg)
}
for nameVersion := range p.Libraries {
names = append(names, nameVersion)
}

return packages, nil, nil
}

func newDotnetDepsPackage(nameVersion string, lib dotnetDepsLibrary) *pkg.Package {
if lib.Type != "package" {
return nil
}
// sort the names so that the order of the packages is deterministic
sort.Strings(names)

splitted := strings.Split(nameVersion, "/")
name := splitted[0]
version := splitted[1]
for _, nameVersion := range names {
lib := p.Libraries[nameVersion]
dotnetPkg := newDotnetDepsPackage(nameVersion, lib, reader.Location)

return &pkg.Package{
Name: name,
Version: version,
Language: pkg.Dotnet,
Type: pkg.DotnetPkg,
MetadataType: pkg.DotnetDepsMetadataType,
Metadata: &pkg.DotnetDepsMetadata{
Name: name,
Version: version,
Path: lib.Path,
Sha512: lib.Sha512,
HashPath: lib.HashPath,
},
if dotnetPkg != nil {
pkgs = append(pkgs, *dotnetPkg)
}
}

return pkgs, nil, nil
}
Loading

0 comments on commit 05eee23

Please sign in to comment.