Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to new syft source API #1376

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ require (
github.com/wagoodman/go-progress v0.0.0-20230301185719-21920a456ad5
github.com/wagoodman/jotframe v0.0.0-20211129225309-56b0d0a4aebb
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/term v0.9.0
golang.org/x/term v0.10.0
gopkg.in/yaml.v2 v2.4.0
gorm.io/gorm v1.23.10
)

require (
github.com/anchore/go-logger v0.0.0-20220728155337-03b66a5207d8
github.com/anchore/sqlite v1.4.6-0.20220607210448-bcc6ee5c4963
github.com/anchore/syft v0.84.1
github.com/anchore/syft v0.84.2-0.20230705174713-cfbb9f703bd7
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b
github.com/mitchellh/mapstructure v1.5.0
)
Expand Down Expand Up @@ -170,11 +170,11 @@ require (
go.uber.org/goleak v1.2.0 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/exp v0.0.0-20230202163644-54bba9f4231b // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/time v0.2.0 // indirect
golang.org/x/tools v0.8.0 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ github.com/anchore/sqlite v1.4.6-0.20220607210448-bcc6ee5c4963 h1:vrf2PYH77vqVJo
github.com/anchore/sqlite v1.4.6-0.20220607210448-bcc6ee5c4963/go.mod h1:AVRyXOUP0hTz9Cb8OlD1XnwA8t4lBPfTuwPHmEUuiLc=
github.com/anchore/stereoscope v0.0.0-20230627195312-cd49355d934e h1:zhk3ZLtomMJ750nNCE+c24PonMzoO/SeL/4uTr1L9kM=
github.com/anchore/stereoscope v0.0.0-20230627195312-cd49355d934e/go.mod h1:0LsgHgXO4QFnk2hsYwtqd3fR18PIZXlFLIl2qb9tu3g=
github.com/anchore/syft v0.84.1 h1:O6V1gCSHTVbyfQq6M1qB86ui64qobZRC3h7lvKpVNWw=
github.com/anchore/syft v0.84.1/go.mod h1:dozEWcwhRawdB3ArPM2BGfZWLslZ+bDNwW+wWUwKySY=
github.com/anchore/syft v0.84.2-0.20230705174713-cfbb9f703bd7 h1:E8pdc689HTwXaHLRcmMTGi6TBukDa6oD8dQ0bJTSUm0=
github.com/anchore/syft v0.84.2-0.20230705174713-cfbb9f703bd7/go.mod h1:4ruIUJNJY2IsuUPrvUdYu8kG4ScFjGoiy/PPmgBEuTw=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
Expand Down Expand Up @@ -942,8 +942,8 @@ golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -1149,8 +1149,8 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand All @@ -1159,8 +1159,8 @@ golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28=
golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo=
golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
2 changes: 1 addition & 1 deletion grype/distro/distro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func Test_NewDistroFromRelease_Coverage(t *testing.T) {

for _, test := range tests {
t.Run(test.fixture, func(t *testing.T) {
s, err := source.NewFromDirectory(test.fixture)
s, err := source.NewFromDirectory(source.DirectoryConfig{Path: test.fixture})
require.NoError(t, err)

resolver, err := s.FileResolver(source.SquashedScope)
Expand Down
16 changes: 8 additions & 8 deletions grype/match/ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
grypeDb "github.com/anchore/grype/grype/db/v5"
"github.com/anchore/grype/grype/pkg"
"github.com/anchore/grype/grype/vulnerability"
"github.com/anchore/syft/syft/file"
syftPkg "github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)

var (
Expand All @@ -28,7 +28,7 @@ var (
Name: "dive",
Version: "0.5.2",
Type: "deb",
Locations: source.NewLocationSet(source.NewLocation("/path/that/has/dive")),
Locations: file.NewLocationSet(file.NewLocation("/path/that/has/dive")),
},
},
{
Expand All @@ -45,7 +45,7 @@ var (
Version: "100.0.50",
Language: syftPkg.Ruby,
Type: syftPkg.GemPkg,
Locations: source.NewLocationSet(source.NewVirtualLocation("/real/path/with/reach",
Locations: file.NewLocationSet(file.NewVirtualLocation("/real/path/with/reach",
"/virtual/path/that/has/reach")),
},
},
Expand All @@ -63,7 +63,7 @@ var (
Version: "100.0.51",
Language: syftPkg.Ruby,
Type: syftPkg.GemPkg,
Locations: source.NewLocationSet(source.NewVirtualLocation("/real/path/with/beach",
Locations: file.NewLocationSet(file.NewVirtualLocation("/real/path/with/beach",
"/virtual/path/that/has/beach")),
},
},
Expand All @@ -81,7 +81,7 @@ var (
Version: "100.0.52",
Language: syftPkg.Ruby,
Type: syftPkg.GemPkg,
Locations: source.NewLocationSet(source.NewVirtualLocation("/real/path/with/speach",
Locations: file.NewLocationSet(file.NewVirtualLocation("/real/path/with/speach",
"/virtual/path/that/has/speach")),
},
},
Expand Down Expand Up @@ -337,9 +337,9 @@ var (
ID: pkg.ID(uuid.NewString()),
Name: "a-pkg",
Version: "1.0",
Locations: source.NewLocationSet(
source.NewLocation("/some/path"),
source.NewVirtualLocation("/some/path", "/some/virtual/path"),
Locations: file.NewLocationSet(
file.NewLocation("/some/path"),
file.NewVirtualLocation("/some/path", "/some/virtual/path"),
),
Type: "rpm",
},
Expand Down
2 changes: 1 addition & 1 deletion grype/pkg/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import (
)

type Context struct {
Source *source.Metadata
Source *source.Description
Distro *linux.Release
}
5 changes: 2 additions & 3 deletions grype/pkg/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/anchore/syft/syft/file"
syftFile "github.com/anchore/syft/syft/file"
syftPkg "github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -550,8 +549,8 @@ func TestFromCollection_DoesNotPanic(t *testing.T) {
examplePackage := syftPkg.Package{
Name: "test",
Version: "1.2.3",
Locations: source.NewLocationSet(
source.NewLocation("/test-path"),
Locations: file.NewLocationSet(
file.NewLocation("/test-path"),
),
Type: syftPkg.NpmPkg,
}
Expand Down
8 changes: 4 additions & 4 deletions grype/pkg/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/stretchr/testify/assert"

"github.com/anchore/stereoscope/pkg/imagetest"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg/cataloger"
"github.com/anchore/syft/syft/source"
)

func TestProviderLocationExcludes(t *testing.T) {
Expand Down Expand Up @@ -158,10 +158,10 @@ func Test_filterPackageExclusions(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
var packages []Package
for _, pkg := range test.locations {
locations := source.NewLocationSet()
locations := file.NewLocationSet()
for _, l := range pkg {
locations.Add(
source.NewVirtualLocation(l, l),
file.NewVirtualLocation(l, l),
)
}
packages = append(packages, Package{Locations: locations})
Expand Down Expand Up @@ -221,7 +221,7 @@ func Test_matchesLocation(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
matches, err := locationMatches(source.NewVirtualLocation(test.realPath, test.virtualPath), test.match)
matches, err := locationMatches(file.NewVirtualLocation(test.realPath, test.virtualPath), test.match)
assert.NoError(t, err)
assert.Equal(t, test.expected, matches)
})
Expand Down
51 changes: 39 additions & 12 deletions grype/pkg/syft_provider.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package pkg

import (
"github.com/anchore/stereoscope/pkg/image"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"
)

func syftProvider(userInput string, config ProviderConfig) ([]Package, Context, *sbom.SBOM, error) {
if config.CatalogingOptions.Search.Scope == "" {
return nil, Context{}, nil, errDoesNotProvide
}

sourceInput, err := source.ParseInputWithName(userInput, config.Platform, config.Name, config.DefaultImagePullSource)
src, err := getSource(userInput, config)
if err != nil {
return nil, Context{}, nil, err
}

src, cleanup, err := source.New(*sourceInput, config.RegistryOptions, config.Exclusions)
if err != nil {
return nil, Context{}, nil, err
}
defer cleanup()
defer src.Close()

catalog, relationships, theDistro, err := syft.CatalogPackages(src, config.CatalogingOptions)
if err != nil {
Expand All @@ -29,14 +22,16 @@ func syftProvider(userInput string, config ProviderConfig) ([]Package, Context,

catalog = removePackagesByOverlap(catalog, relationships)

srcDescription := src.Describe()

packages := FromCollection(catalog, config.SynthesisConfig)
context := Context{
Source: &src.Metadata,
Source: &srcDescription,
Distro: theDistro,
}

sbom := &sbom.SBOM{
Source: src.Metadata,
Source: srcDescription,
Relationships: relationships,
Artifacts: sbom.Artifacts{
Packages: catalog,
Expand All @@ -45,3 +40,35 @@ func syftProvider(userInput string, config ProviderConfig) ([]Package, Context,

return packages, context, sbom, nil
}

func getSource(userInput string, config ProviderConfig) (source.Source, error) {
if config.CatalogingOptions.Search.Scope == "" {
return nil, errDoesNotProvide
}

detection, err := source.Detect(userInput, source.DetectConfig{
DefaultImageSource: config.DefaultImagePullSource,
})
if err != nil {
return nil, err
}

var platform *image.Platform
if config.Platform != "" {
platform, err = image.NewPlatform(config.Platform)
if err != nil {
return nil, err
}
}

return detection.NewSource(source.DetectionSourceConfig{
Alias: source.Alias{
Name: config.Name,
},
RegistryOptions: config.RegistryOptions,
Platform: platform,
Exclude: source.ExcludeConfig{
Paths: config.Exclusions,
},
})
}
Loading