-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
571de36
commit 7bd7f31
Showing
8 changed files
with
150 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
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,64 @@ | ||
package nixstore | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/anchore/syft/internal/log" | ||
"github.com/anchore/syft/syft/artifact" | ||
"github.com/anchore/syft/syft/pkg" | ||
"github.com/anchore/syft/syft/source" | ||
) | ||
|
||
type Cataloger struct{} | ||
|
||
func NewNixStoreCataloger() *Cataloger { | ||
return &Cataloger{} | ||
} | ||
|
||
func (c *Cataloger) Name() string { | ||
return "nix-store-cataloger" | ||
} | ||
|
||
func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []artifact.Relationship, error) { | ||
nixStoreFileMatches, err := resolver.FilesByGlob(pkg.NixStoreGlob) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to find any nix store directory: %w", err) | ||
} | ||
|
||
var allPackages []pkg.Package | ||
pkgMap := make(map[string]interface{}) | ||
for _, storeLocation := range nixStoreFileMatches { | ||
name, version := extractNameAndVersion(storeLocation.VirtualPath) | ||
|
||
if version != "" { | ||
nixLookupPkg := pkgMap[fmt.Sprintf("%s-%s", name, version)] | ||
if nixLookupPkg == nil { | ||
nixPkg := pkg.NixStoreMetadata{ | ||
Source: name, | ||
SourceVersion: version, | ||
} | ||
|
||
p := newNixStorePackage(nixPkg) | ||
p.Name = name | ||
p.Version = version | ||
p.FoundBy = c.Name() | ||
p.Locations = []source.Location{storeLocation} | ||
p.SetID() | ||
|
||
log.Debug(p) | ||
|
||
pkgMap[fmt.Sprintf("%s-%s", name, version)] = p | ||
|
||
} else { | ||
nixLookupPkg := nixLookupPkg.(pkg.Package) | ||
nixLookupPkg.Locations = append(nixLookupPkg.Locations, storeLocation) | ||
pkgMap[fmt.Sprintf("%s-%s", name, version)] = nixLookupPkg | ||
} | ||
} | ||
} | ||
|
||
for _, p := range pkgMap { | ||
allPackages = append(allPackages, p.(pkg.Package)) | ||
} | ||
return allPackages, nil, nil | ||
} |
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,31 @@ | ||
package nixstore | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/anchore/syft/internal" | ||
|
||
"github.com/anchore/syft/syft/pkg" | ||
) | ||
|
||
var ( | ||
errEndOfPackages = fmt.Errorf("no more packages to read") | ||
sourceRegexp = regexp.MustCompile(`/nix/store/[^-]*-(?P<name>[^-]*)-(?P<version>[^-/]*).*/`) | ||
) | ||
|
||
func newNixStorePackage(d pkg.NixStoreMetadata) pkg.Package { | ||
return pkg.Package{ | ||
Name: d.Package, | ||
Version: d.Version, | ||
Type: pkg.NixStorePkg, | ||
MetadataType: pkg.NixStoreMetadataType, | ||
Metadata: d, | ||
} | ||
} | ||
|
||
func extractNameAndVersion(source string) (string, string) { | ||
// special handling for the Source field since it has formatted data | ||
match := internal.MatchNamedCaptureGroups(sourceRegexp, source) | ||
return match["name"], match["version"] | ||
} |
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,37 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/anchore/packageurl-go" | ||
"github.com/anchore/syft/syft/distro" | ||
) | ||
|
||
const NixStoreGlob = "/nix/store/**" | ||
|
||
type NixStoreMetadata struct { | ||
Package string `mapstructure:"Package" json:"package"` | ||
Architecture string `mapstructure:"A" json:"architecture"` | ||
Source string `mapstructure:"Source" json:"source"` | ||
Version string `mapstructure:"Version" json:"version"` | ||
SourceVersion string `mapstructure:"SourceVersion" json:"sourceVersion"` | ||
} | ||
|
||
func (m NixStoreMetadata) PackageURL(d *distro.Distro) string { | ||
if d == nil { | ||
return "" | ||
} | ||
pURL := packageurl.NewPackageURL( | ||
// TODO: replace with `packageurl.TypeDebian` upon merge of https://github.com/package-url/packageurl-go/pull/21 | ||
// TODO: or, since we're now using an Anchore fork of this module, we could do this sooner. | ||
"nix", | ||
d.Type.String(), | ||
m.Package, | ||
m.Version, | ||
packageurl.Qualifiers{ | ||
{ | ||
Key: "arch", | ||
Value: m.Architecture, | ||
}, | ||
}, | ||
"") | ||
return pURL.ToString() | ||
} |
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