Projects STRLCPY syft Commits 9fd53224
🤬
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    README.md
    skipped 493 lines
    494 494   # same as -s ; SYFT_PACKAGE_CATALOGER_SCOPE env var
    495 495   scope: "squashed"
    496 496   
     497 +golang:
     498 + # search for go package licences in the GOPATH of the system running Syft, note that this is outside the
     499 + # container filesystem and potentially outside the root of a local directory scan
     500 + # SYFT_GOLANG_SEARCH_LOCAL_MOD_CACHE_LICENSES env var
     501 + search-local-mod-cache-licenses: false
     502 +
     503 + # specify an explicit go mod cache directory, if unset this defaults to $GOPATH/pkg/mod or $HOME/go/pkg/mod
     504 + # SYFT_GOLANG_LOCAL_MOD_CACHE_DIR env var
     505 + local-mod-cache-dir: ""
     506 + 
    497 507  # cataloging file contents is exposed through the power-user subcommand
    498 508  file-contents:
    499 509   cataloger:
    skipped 152 lines
  • ■ ■ ■ ■ ■
    go.mod
    skipped 54 lines
    55 55   github.com/anchore/stereoscope v0.0.0-20230317134707-7928713c391e
    56 56   github.com/docker/docker v23.0.1+incompatible
    57 57   github.com/google/go-containerregistry v0.14.0
     58 + github.com/google/licensecheck v0.3.1
    58 59   github.com/invopop/jsonschema v0.7.0
    59 60   github.com/knqyf263/go-rpmdb v0.0.0-20221030135625-4082a22221ce
    60 61   github.com/opencontainers/go-digest v1.0.0
    skipped 101 lines
  • ■ ■ ■ ■ ■ ■
    go.sum
    skipped 263 lines
    264 264  github.com/google/go-containerregistry v0.14.0 h1:z58vMqHxuwvAsVwvKEkmVBz2TlgBgH5k6koEXBtlYkw=
    265 265  github.com/google/go-containerregistry v0.14.0/go.mod h1:aiJ2fp/SXvkWgmYHioXnbMdlgB8eXiiYOY55gfN91Wk=
    266 266  github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
     267 +github.com/google/licensecheck v0.3.1 h1:QoxgoDkaeC4nFrtGN1jV7IPmDCHFNIVh54e5hSt6sPs=
     268 +github.com/google/licensecheck v0.3.1/go.mod h1:ORkR35t/JjW+emNKtfJDII0zlciG9JgbT7SmsohlHmY=
    267 269  github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
    268 270  github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
    269 271  github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
    skipped 874 lines
  • ■ ■ ■ ■ ■ ■
    internal/config/application.go
    skipped 17 lines
    18 18   "github.com/anchore/syft/internal"
    19 19   "github.com/anchore/syft/internal/log"
    20 20   "github.com/anchore/syft/syft/pkg/cataloger"
     21 + golangCataloger "github.com/anchore/syft/syft/pkg/cataloger/golang"
    21 22  )
    22 23   
    23 24  var (
    skipped 24 lines
    48 49   Log logging `yaml:"log" json:"log" mapstructure:"log"` // all logging-related options
    49 50   Catalogers []string `yaml:"catalogers" json:"catalogers" mapstructure:"catalogers"`
    50 51   Package pkg `yaml:"package" json:"package" mapstructure:"package"`
     52 + Golang golang `yaml:"golang" json:"golang" mapstructure:"golang"`
    51 53   Attest attest `yaml:"attest" json:"attest" mapstructure:"attest"`
    52 54   FileMetadata FileMetadata `yaml:"file-metadata" json:"file-metadata" mapstructure:"file-metadata"`
    53 55   FileClassification fileClassification `yaml:"file-classification" json:"file-classification" mapstructure:"file-classification"`
    skipped 15 lines
    69 71   },
    70 72   Catalogers: cfg.Catalogers,
    71 73   Parallelism: cfg.Parallelism,
     74 + Golang: golangCataloger.GoCatalogerOpts{
     75 + SearchLocalModCacheLicenses: cfg.Golang.SearchLocalModCacheLicenses,
     76 + LocalModCacheDir: cfg.Golang.LocalModCacheDir,
     77 + },
    72 78   }
    73 79  }
    74 80   
    skipped 214 lines
  • ■ ■ ■ ■ ■ ■
    internal/config/golang.go
     1 +package config
     2 + 
     3 +import "github.com/spf13/viper"
     4 + 
     5 +type golang struct {
     6 + SearchLocalModCacheLicenses bool `json:"search-local-mod-cache-licenses" yaml:"search-local-mod-cache-licenses" mapstructure:"search-local-mod-cache-licenses"`
     7 + LocalModCacheDir string `json:"local-mod-cache-dir" yaml:"local-mod-cache-dir" mapstructure:"local-mod-cache-dir"`
     8 +}
     9 + 
     10 +func (cfg golang) loadDefaultValues(v *viper.Viper) {
     11 + v.SetDefault("golang.search-local-mod-cache-licenses", false)
     12 + v.SetDefault("golang.local-mod-cache-dir", "")
     13 +}
     14 + 
  • ■ ■ ■ ■ ■ ■
    internal/licenses/list.go
     1 +package licenses
     2 + 
     3 +import "github.com/anchore/syft/internal"
     4 + 
     5 +// all of these taken from https://github.com/golang/pkgsite/blob/8996ff632abee854aef1b764ca0501f262f8f523/internal/licenses/licenses.go#L338
     6 +// which unfortunately is not exported. But fortunately is under BSD-style license.
     7 + 
     8 +var (
     9 + FileNames = []string{
     10 + "COPYING",
     11 + "COPYING.md",
     12 + "COPYING.markdown",
     13 + "COPYING.txt",
     14 + "LICENCE",
     15 + "LICENCE.md",
     16 + "LICENCE.markdown",
     17 + "LICENCE.txt",
     18 + "LICENSE",
     19 + "LICENSE.md",
     20 + "LICENSE.markdown",
     21 + "LICENSE.txt",
     22 + "LICENSE-2.0.txt",
     23 + "LICENCE-2.0.txt",
     24 + "LICENSE-APACHE",
     25 + "LICENCE-APACHE",
     26 + "LICENSE-APACHE-2.0.txt",
     27 + "LICENCE-APACHE-2.0.txt",
     28 + "LICENSE-MIT",
     29 + "LICENCE-MIT",
     30 + "LICENSE.MIT",
     31 + "LICENCE.MIT",
     32 + "LICENSE.code",
     33 + "LICENCE.code",
     34 + "LICENSE.docs",
     35 + "LICENCE.docs",
     36 + "LICENSE.rst",
     37 + "LICENCE.rst",
     38 + "MIT-LICENSE",
     39 + "MIT-LICENCE",
     40 + "MIT-LICENSE.md",
     41 + "MIT-LICENCE.md",
     42 + "MIT-LICENSE.markdown",
     43 + "MIT-LICENCE.markdown",
     44 + "MIT-LICENSE.txt",
     45 + "MIT-LICENCE.txt",
     46 + "MIT_LICENSE",
     47 + "MIT_LICENCE",
     48 + "UNLICENSE",
     49 + "UNLICENCE",
     50 + }
     51 + 
     52 + FileNameSet = internal.NewStringSet(FileNames...)
     53 +)
     54 + 
  • ■ ■ ■ ■ ■ ■
    internal/licenses/parser.go
     1 +package licenses
     2 + 
     3 +import (
     4 + "io"
     5 + 
     6 + "github.com/google/licensecheck"
     7 + "golang.org/x/exp/slices"
     8 +)
     9 + 
     10 +const (
     11 + coverageThreshold = 75
     12 + unknownLicenseType = "UNKNOWN"
     13 +)
     14 + 
     15 +// Parse scans the contents of a license file to attempt to determine the type of license it is
     16 +func Parse(reader io.Reader) (licenses []string, err error) {
     17 + contents, err := io.ReadAll(reader)
     18 + if err != nil {
     19 + return nil, err
     20 + }
     21 + cov := licensecheck.Scan(contents)
     22 + 
     23 + if cov.Percent < float64(coverageThreshold) {
     24 + licenses = append(licenses, unknownLicenseType)
     25 + }
     26 + for _, m := range cov.Match {
     27 + if slices.Contains(licenses, m.ID) {
     28 + continue
     29 + }
     30 + licenses = append(licenses, m.ID)
     31 + }
     32 + return
     33 +}
     34 + 
  • ■ ■ ■ ■ ■ ■
    syft/pkg/cataloger/cataloger.go
    skipped 47 lines
    48 48   java.NewJavaCataloger(cfg.Java()),
    49 49   java.NewNativeImageCataloger(),
    50 50   apkdb.NewApkdbCataloger(),
    51  - golang.NewGoModuleBinaryCataloger(),
     51 + golang.NewGoModuleBinaryCataloger(cfg.Go()),
    52 52   dotnet.NewDotnetDepsCataloger(),
    53 53   portage.NewPortageCataloger(),
    54 54   sbom.NewSBOMCataloger(),
    skipped 17 lines
    72 72   java.NewJavaPomCataloger(),
    73 73   java.NewNativeImageCataloger(),
    74 74   apkdb.NewApkdbCataloger(),
    75  - golang.NewGoModuleBinaryCataloger(),
    76  - golang.NewGoModFileCataloger(),
     75 + golang.NewGoModuleBinaryCataloger(cfg.Go()),
     76 + golang.NewGoModFileCataloger(cfg.Go()),
    77 77   rust.NewCargoLockCataloger(),
    78 78   dart.NewPubspecLockCataloger(),
    79 79   dotnet.NewDotnetDepsCataloger(),
    skipped 25 lines
    105 105   java.NewJavaPomCataloger(),
    106 106   java.NewNativeImageCataloger(),
    107 107   apkdb.NewApkdbCataloger(),
    108  - golang.NewGoModuleBinaryCataloger(),
    109  - golang.NewGoModFileCataloger(),
     108 + golang.NewGoModuleBinaryCataloger(cfg.Go()),
     109 + golang.NewGoModFileCataloger(cfg.Go()),
    110 110   rust.NewCargoLockCataloger(),
    111 111   rust.NewAuditBinaryCataloger(),
    112 112   dart.NewPubspecLockCataloger(),
    skipped 78 lines
  • ■ ■ ■ ■ ■ ■
    syft/pkg/cataloger/config.go
    1 1  package cataloger
    2 2   
    3 3  import (
     4 + "github.com/anchore/syft/syft/pkg/cataloger/golang"
    4 5   "github.com/anchore/syft/syft/pkg/cataloger/java"
    5 6  )
    6 7   
    7 8  type Config struct {
    8 9   Search SearchConfig
     10 + Golang golang.GoCatalogerOpts
    9 11   Catalogers []string
    10 12   Parallelism int
    11 13  }
    skipped 12 lines
    24 26   }
    25 27  }
    26 28   
     29 +func (c Config) Go() golang.GoCatalogerOpts {
     30 + return c.Golang
     31 +}
     32 + 
  • ■ ■ ■ ■ ■
    syft/pkg/cataloger/golang/cataloger.go
    skipped 7 lines
    8 8   "github.com/anchore/syft/syft/pkg/cataloger/generic"
    9 9  )
    10 10   
     11 +type GoCatalogerOpts struct {
     12 + SearchLocalModCacheLicenses bool
     13 + LocalModCacheDir string
     14 +}
     15 + 
    11 16  // NewGoModFileCataloger returns a new Go module cataloger object.
    12  -func NewGoModFileCataloger() *generic.Cataloger {
     17 +func NewGoModFileCataloger(opts GoCatalogerOpts) *generic.Cataloger {
     18 + c := goModCataloger{
     19 + licenses: newGoLicenses(opts),
     20 + }
    13 21   return generic.NewCataloger("go-mod-file-cataloger").
    14  - WithParserByGlobs(parseGoModFile, "**/go.mod")
     22 + WithParserByGlobs(c.parseGoModFile, "**/go.mod")
    15 23  }
    16 24   
    17 25  // NewGoModuleBinaryCataloger returns a new Golang cataloger object.
    18  -func NewGoModuleBinaryCataloger() *generic.Cataloger {
     26 +func NewGoModuleBinaryCataloger(opts GoCatalogerOpts) *generic.Cataloger {
     27 + c := goBinaryCataloger{
     28 + licenses: newGoLicenses(opts),
     29 + }
    19 30   return generic.NewCataloger("go-module-binary-cataloger").
    20  - WithParserByMimeTypes(parseGoBinary, internal.ExecutableMIMETypeSet.List()...)
     31 + WithParserByMimeTypes(c.parseGoBinary, internal.ExecutableMIMETypeSet.List()...)
    21 32  }
    22 33   
  • ■ ■ ■ ■ ■ ■
    syft/pkg/cataloger/golang/cataloger_test.go
    skipped 26 lines
    27 27   FromDirectory(t, test.fixture).
    28 28   ExpectsResolverContentQueries(test.expected).
    29 29   IgnoreUnfulfilledPathResponses("src/go.sum").
    30  - TestCataloger(t, NewGoModFileCataloger())
     30 + TestCataloger(t, NewGoModFileCataloger(GoCatalogerOpts{}))
    31 31   })
    32 32   }
    33 33  }
    skipped 18 lines
    52 52   pkgtest.NewCatalogTester().
    53 53   FromDirectory(t, test.fixture).
    54 54   ExpectsResolverContentQueries(test.expected).
    55  - TestCataloger(t, NewGoModuleBinaryCataloger())
     55 + TestCataloger(t, NewGoModuleBinaryCataloger(GoCatalogerOpts{}))
    56 56   })
    57 57   }
    58 58  }
    skipped 1 lines
  • ■ ■ ■ ■ ■ ■
    syft/pkg/cataloger/golang/licenses.go
     1 +package golang
     2 + 
     3 +import (
     4 + "fmt"
     5 + "os"
     6 + "path"
     7 + "regexp"
     8 + "strings"
     9 + 
     10 + "github.com/mitchellh/go-homedir"
     11 + 
     12 + "github.com/anchore/syft/internal/licenses"
     13 + "github.com/anchore/syft/internal/log"
     14 + "github.com/anchore/syft/syft/source"
     15 +)
     16 + 
     17 +type goLicenses struct {
     18 + searchLocalModCacheLicenses bool
     19 + localModCacheResolver source.FileResolver
     20 +}
     21 + 
     22 +func newGoLicenses(opts GoCatalogerOpts) goLicenses {
     23 + return goLicenses{
     24 + searchLocalModCacheLicenses: opts.SearchLocalModCacheLicenses,
     25 + localModCacheResolver: modCacheResolver(opts.LocalModCacheDir),
     26 + }
     27 +}
     28 + 
     29 +func defaultGoPath() string {
     30 + goPath := os.Getenv("GOPATH")
     31 + 
     32 + if goPath == "" {
     33 + homeDir, err := homedir.Dir()
     34 + if err != nil {
     35 + log.Debug("unable to determine user home dir: %v", err)
     36 + } else {
     37 + goPath = path.Join(homeDir, "go")
     38 + }
     39 + }
     40 + 
     41 + return goPath
     42 +}
     43 + 
     44 +// resolver needs to be shared between mod file & binary scanners so it's only scanned once
     45 +var modCacheResolvers = map[string]source.FileResolver{}
     46 + 
     47 +func modCacheResolver(modCacheDir string) source.FileResolver {
     48 + if modCacheDir == "" {
     49 + goPath := defaultGoPath()
     50 + if goPath != "" {
     51 + modCacheDir = path.Join(goPath, "pkg", "mod")
     52 + }
     53 + }
     54 + 
     55 + if r, ok := modCacheResolvers[modCacheDir]; ok {
     56 + return r
     57 + }
     58 + 
     59 + var r source.FileResolver
     60 + 
     61 + if modCacheDir == "" {
     62 + log.Trace("unable to determine mod cache directory, skipping mod cache resolver")
     63 + r = source.NewMockResolverForPaths()
     64 + } else {
     65 + stat, err := os.Stat(modCacheDir)
     66 + 
     67 + if os.IsNotExist(err) || stat == nil || !stat.IsDir() {
     68 + log.Tracef("unable to open mod cache directory: %s, skipping mod cache resolver", modCacheDir)
     69 + r = source.NewMockResolverForPaths()
     70 + } else {
     71 + r = source.NewDeferredResolverFromSource(func() (source.Source, error) {
     72 + return source.NewFromDirectory(modCacheDir)
     73 + })
     74 + }
     75 + }
     76 + 
     77 + modCacheResolvers[modCacheDir] = r
     78 + 
     79 + return r
     80 +}
     81 + 
     82 +func (c *goLicenses) getLicenses(resolver source.FileResolver, moduleName, moduleVersion string) (licenses []string, err error) {
     83 + moduleName = processCaps(moduleName)
     84 + 
     85 + licenses, err = findLicenses(resolver,
     86 + fmt.Sprintf(`**/go/pkg/mod/%s@%s/*`, moduleName, moduleVersion),
     87 + )
     88 + 
     89 + if c.searchLocalModCacheLicenses && err == nil && len(licenses) == 0 {
     90 + // if we're running against a directory on the filesystem, it may not include the
     91 + // user's homedir / GOPATH, so we defer to using the localModCacheResolver
     92 + licenses, err = findLicenses(c.localModCacheResolver,
     93 + fmt.Sprintf(`**/%s@%s/*`, moduleName, moduleVersion),
     94 + )
     95 + }
     96 + 
     97 + // always return a non-nil slice
     98 + if licenses == nil {
     99 + licenses = []string{}
     100 + }
     101 + 
     102 + return
     103 +}
     104 + 
     105 +func findLicenses(resolver source.FileResolver, globMatch string) (out []string, err error) {
     106 + if resolver == nil {
     107 + return
     108 + }
     109 + 
     110 + locations, err := resolver.FilesByGlob(globMatch)
     111 + if err != nil {
     112 + return nil, err
     113 + }
     114 + 
     115 + for _, l := range locations {
     116 + fileName := path.Base(l.RealPath)
     117 + if licenses.FileNameSet.Contains(fileName) {
     118 + contents, err := resolver.FileContentsByLocation(l)
     119 + if err != nil {
     120 + return nil, err
     121 + }
     122 + parsed, err := licenses.Parse(contents)
     123 + if err != nil {
     124 + return nil, err
     125 + }
     126 + 
     127 + out = append(out, parsed...)
     128 + }
     129 + }
     130 + 
     131 + return
     132 +}
     133 + 
     134 +var capReplacer = regexp.MustCompile("[A-Z]")
     135 + 
     136 +func processCaps(s string) string {
     137 + return capReplacer.ReplaceAllStringFunc(s, func(s string) string {
     138 + return "!" + strings.ToLower(s)
     139 + })
     140 +}
     141 + 
  • ■ ■ ■ ■ ■ ■
    syft/pkg/cataloger/golang/licenses_test.go
     1 +package golang
     2 + 
     3 +import (
     4 + "os"
     5 + "path"
     6 + "testing"
     7 + 
     8 + "github.com/stretchr/testify/require"
     9 + 
     10 + "github.com/anchore/syft/syft/source"
     11 +)
     12 + 
     13 +func Test_LicenseSearch(t *testing.T) {
     14 + tests := []struct {
     15 + name string
     16 + version string
     17 + expected string
     18 + }{
     19 + {
     20 + name: "github.com/someorg/somename",
     21 + version: "v0.3.2",
     22 + expected: "Apache-2.0",
     23 + },
     24 + {
     25 + name: "github.com/CapORG/CapProject",
     26 + version: "v4.111.5",
     27 + expected: "MIT",
     28 + },
     29 + }
     30 + 
     31 + wd, err := os.Getwd()
     32 + require.NoError(t, err)
     33 + 
     34 + for _, test := range tests {
     35 + t.Run(test.name, func(t *testing.T) {
     36 + l := newGoLicenses(GoCatalogerOpts{
     37 + SearchLocalModCacheLicenses: true,
     38 + LocalModCacheDir: path.Join(wd, "test-fixtures", "licenses"),
     39 + })
     40 + licenses, err := l.getLicenses(source.MockResolver{}, test.name, test.version)
     41 + require.NoError(t, err)
     42 + 
     43 + require.Len(t, licenses, 1)
     44 + 
     45 + require.Equal(t, test.expected, licenses[0])
     46 + })
     47 + }
     48 +}
     49 + 
     50 +func Test_processCaps(t *testing.T) {
     51 + tests := []struct {
     52 + name string
     53 + expected string
     54 + }{
     55 + {
     56 + name: "CycloneDX",
     57 + expected: "!cyclone!d!x",
     58 + },
     59 + {
     60 + name: "Azure",
     61 + expected: "!azure",
     62 + },
     63 + {
     64 + name: "xkcd",
     65 + expected: "xkcd",
     66 + },
     67 + }
     68 + 
     69 + for _, test := range tests {
     70 + t.Run(test.name, func(t *testing.T) {
     71 + got := processCaps(test.name)
     72 + 
     73 + require.Equal(t, test.expected, got)
     74 + })
     75 + }
     76 +}
     77 + 
  • ■ ■ ■ ■ ■
    syft/pkg/cataloger/golang/package.go
    skipped 5 lines
    6 6   "strings"
    7 7   
    8 8   "github.com/anchore/packageurl-go"
     9 + "github.com/anchore/syft/internal/log"
    9 10   "github.com/anchore/syft/syft/pkg"
    10 11   "github.com/anchore/syft/syft/source"
    11 12  )
    12 13   
    13  -func newGoBinaryPackage(dep *debug.Module, mainModule, goVersion, architecture string, buildSettings map[string]string, locations ...source.Location) pkg.Package {
     14 +func (c *goBinaryCataloger) newGoBinaryPackage(resolver source.FileResolver, dep *debug.Module, mainModule, goVersion, architecture string, buildSettings map[string]string, locations ...source.Location) pkg.Package {
    14 15   if dep.Replace != nil {
    15 16   dep = dep.Replace
    16 17   }
    17 18   
     19 + licenses, err := c.licenses.getLicenses(resolver, dep.Path, dep.Version)
     20 + if err != nil {
     21 + log.Tracef("error getting licenses for package: %s %v", dep.Path, err)
     22 + }
     23 + 
    18 24   p := pkg.Package{
    19 25   Name: dep.Path,
    20 26   Version: dep.Version,
     27 + Licenses: licenses,
    21 28   PURL: packageURL(dep.Path, dep.Version),
    22 29   Language: pkg.Go,
    23 30   Type: pkg.GoModulePkg,
    skipped 47 lines
  • ■ ■ ■ ■ ■ ■
    syft/pkg/cataloger/golang/parse_go_binary.go
    skipped 37 lines
    38 38   
    39 39  const devel = "(devel)"
    40 40   
     41 +type goBinaryCataloger struct {
     42 + licenses goLicenses
     43 +}
     44 + 
    41 45  // Catalog is given an object to resolve file references and content, this function returns any discovered Packages after analyzing rpm db installation.
    42  -func parseGoBinary(_ source.FileResolver, _ *generic.Environment, reader source.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
     46 +func (c *goBinaryCataloger) parseGoBinary(resolver source.FileResolver, _ *generic.Environment, reader source.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
    43 47   var pkgs []pkg.Package
    44 48   
    45 49   unionReader, err := unionreader.GetUnionReader(reader.ReadCloser)
    skipped 5 lines
    51 55   internal.CloseAndLogError(reader.ReadCloser, reader.RealPath)
    52 56   
    53 57   for i, mod := range mods {
    54  - pkgs = append(pkgs, buildGoPkgInfo(reader.Location, mod, archs[i])...)
     58 + pkgs = append(pkgs, c.buildGoPkgInfo(resolver, reader.Location, mod, archs[i])...)
    55 59   }
    56 60   return pkgs, nil, nil
    57 61  }
    58 62   
    59  -func makeGoMainPackage(mod *debug.BuildInfo, arch string, location source.Location) pkg.Package {
     63 +func (c *goBinaryCataloger) makeGoMainPackage(resolver source.FileResolver, mod *debug.BuildInfo, arch string, location source.Location) pkg.Package {
    60 64   gbs := getBuildSettings(mod.Settings)
    61  - main := newGoBinaryPackage(&mod.Main, mod.Main.Path, mod.GoVersion, arch, gbs, location)
     65 + main := c.newGoBinaryPackage(resolver, &mod.Main, mod.Main.Path, mod.GoVersion, arch, gbs, location)
    62 66   if main.Version == devel {
    63 67   if version, ok := gbs["vcs.revision"]; ok {
    64 68   if timestamp, ok := gbs["vcs.time"]; ok {
    skipped 120 lines
    185 189   return
    186 190  }
    187 191   
    188  -func buildGoPkgInfo(location source.Location, mod *debug.BuildInfo, arch string) []pkg.Package {
     192 +func (c *goBinaryCataloger) buildGoPkgInfo(resolver source.FileResolver, location source.Location, mod *debug.BuildInfo, arch string) []pkg.Package {
    189 193   var pkgs []pkg.Package
    190 194   if mod == nil {
    191 195   return pkgs
    skipped 8 lines
    200 204   if dep == nil {
    201 205   continue
    202 206   }
    203  - p := newGoBinaryPackage(dep, mod.Main.Path, mod.GoVersion, arch, nil, location)
     207 + p := c.newGoBinaryPackage(resolver, dep, mod.Main.Path, mod.GoVersion, arch, nil, location)
    204 208   if pkg.IsValid(&p) {
    205 209   pkgs = append(pkgs, p)
    206 210   }
    skipped 3 lines
    210 214   return pkgs
    211 215   }
    212 216   
    213  - main := makeGoMainPackage(mod, arch, location)
     217 + main := c.makeGoMainPackage(resolver, mod, arch, location)
    214 218   pkgs = append(pkgs, main)
    215 219   
    216 220   return pkgs
    skipped 2 lines
  • ■ ■ ■ ■ ■
    syft/pkg/cataloger/golang/parse_go_binary_test.go
    skipped 496 lines
    497 497   t.Run(test.name, func(t *testing.T) {
    498 498   for i := range test.expected {
    499 499   p := &test.expected[i]
     500 + if p.Licenses == nil {
     501 + p.Licenses = []string{}
     502 + }
    500 503   p.SetID()
    501 504   }
    502 505   location := source.Location{
    skipped 2 lines
    505 508   FileSystemID: "layer-id",
    506 509   },
    507 510   }
    508  - pkgs := buildGoPkgInfo(location, test.mod, test.arch)
     511 + 
     512 + c := goBinaryCataloger{}
     513 + pkgs := c.buildGoPkgInfo(source.NewMockResolverForPaths(), location, test.mod, test.arch)
    509 514   assert.Equal(t, test.expected, pkgs)
    510 515   })
    511 516   }
    skipped 2 lines
  • ■ ■ ■ ■ ■ ■
    syft/pkg/cataloger/golang/parse_go_mod.go
    skipped 15 lines
    16 16   "github.com/anchore/syft/syft/source"
    17 17  )
    18 18   
     19 +type goModCataloger struct {
     20 + licenses goLicenses
     21 +}
     22 + 
    19 23  // parseGoModFile takes a go.mod and lists all packages discovered.
    20  -func parseGoModFile(resolver source.FileResolver, _ *generic.Environment, reader source.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
     24 +//
     25 +//nolint:funlen
     26 +func (c *goModCataloger) parseGoModFile(resolver source.FileResolver, _ *generic.Environment, reader source.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
    21 27   packages := make(map[string]pkg.Package)
    22 28   
    23 29   contents, err := io.ReadAll(reader)
    skipped 12 lines
    36 42   }
    37 43   
    38 44   for _, m := range file.Require {
     45 + licenses, err := c.licenses.getLicenses(resolver, m.Mod.Path, m.Mod.Version)
     46 + if err != nil {
     47 + log.Tracef("error getting licenses for package: %s %v", m.Mod.Path, err)
     48 + }
     49 + 
    39 50   packages[m.Mod.Path] = pkg.Package{
    40 51   Name: m.Mod.Path,
    41 52   Version: m.Mod.Version,
     53 + Licenses: licenses,
    42 54   Locations: source.NewLocationSet(reader.Location),
    43 55   PURL: packageURL(m.Mod.Path, m.Mod.Version),
    44 56   Language: pkg.Go,
    skipped 7 lines
    52 64   
    53 65   // remove any old packages and replace with new ones...
    54 66   for _, m := range file.Replace {
     67 + licenses, err := c.licenses.getLicenses(resolver, m.New.Path, m.New.Version)
     68 + if err != nil {
     69 + log.Tracef("error getting licenses for package: %s %v", m.New.Path, err)
     70 + }
     71 + 
    55 72   packages[m.New.Path] = pkg.Package{
    56 73   Name: m.New.Path,
    57 74   Version: m.New.Version,
     75 + Licenses: licenses,
    58 76   Locations: source.NewLocationSet(reader.Location),
    59 77   PURL: packageURL(m.New.Path, m.New.Version),
    60 78   Language: pkg.Go,
    skipped 65 lines
  • ■ ■ ■ ■ ■
    syft/pkg/cataloger/golang/parse_go_mod_test.go
    skipped 87 lines
    88 88   
    89 89   for _, test := range tests {
    90 90   t.Run(test.fixture, func(t *testing.T) {
     91 + for i := range test.expected {
     92 + p := &test.expected[i]
     93 + if p.Licenses == nil {
     94 + p.Licenses = []string{}
     95 + }
     96 + }
     97 + c := goModCataloger{}
    91 98   pkgtest.NewCatalogTester().
    92 99   FromFile(t, test.fixture).
    93 100   Expects(test.expected, nil).
    94  - TestParser(t, parseGoModFile)
     101 + TestParser(t, c.parseGoModFile)
    95 102   })
    96 103   }
    97 104  }
    skipped 49 lines
    147 154   
    148 155   for _, test := range tests {
    149 156   t.Run(test.fixture, func(t *testing.T) {
     157 + for i := range test.expected {
     158 + p := &test.expected[i]
     159 + if p.Licenses == nil {
     160 + p.Licenses = []string{}
     161 + }
     162 + }
    150 163   pkgtest.NewCatalogTester().
    151 164   FromDirectory(t, test.fixture).
    152 165   Expects(test.expected, nil).
    153  - TestCataloger(t, NewGoModFileCataloger())
     166 + TestCataloger(t, NewGoModFileCataloger(GoCatalogerOpts{}))
    154 167   })
    155 168   }
    156 169  }
    skipped 1 lines
  • ■ ■ ■ ■ ■ ■
    syft/pkg/cataloger/golang/test-fixtures/licenses/pkg/mod/github.com/!cap!o!r!g/[email protected]/LICENSE.txt
     1 +The MIT License (MIT)
     2 + 
     3 +Copyright (c) 2014 Someone Cool <[email protected]>
     4 + 
     5 +Permission is hereby granted, free of charge, to any person obtaining a copy
     6 +of this software and associated documentation files (the "Software"), to deal
     7 +in the Software without restriction, including without limitation the rights
     8 +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9 +copies of the Software, and to permit persons to whom the Software is
     10 +furnished to do so, subject to the following conditions:
     11 + 
     12 +The above copyright notice and this permission notice shall be included in
     13 +all copies or substantial portions of the Software.
     14 + 
     15 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     18 +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19 +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20 +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     21 +THE SOFTWARE.
  • ■ ■ ■ ■ ■ ■
    syft/pkg/cataloger/golang/test-fixtures/licenses/pkg/mod/github.com/someorg/[email protected]/LICENSE
     1 + Apache License
     2 + Version 2.0, January 2004
     3 + http://www.apache.org/licenses/
     4 + 
     5 + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
     6 + 
     7 + 1. Definitions.
     8 + 
     9 + "License" shall mean the terms and conditions for use, reproduction,
     10 + and distribution as defined by Sections 1 through 9 of this document.
     11 + 
     12 + "Licensor" shall mean the copyright owner or entity authorized by
     13 + the copyright owner that is granting the License.
     14 + 
     15 + "Legal Entity" shall mean the union of the acting entity and all
     16 + other entities that control, are controlled by, or are under common
     17 + control with that entity. For the purposes of this definition,
     18 + "control" means (i) the power, direct or indirect, to cause the
     19 + direction or management of such entity, whether by contract or
     20 + otherwise, or (ii) ownership of fifty percent (50%) or more of the
     21 + outstanding shares, or (iii) beneficial ownership of such entity.
     22 + 
     23 + "You" (or "Your") shall mean an individual or Legal Entity
     24 + exercising permissions granted by this License.
     25 + 
     26 + "Source" form shall mean the preferred form for making modifications,
     27 + including but not limited to software source code, documentation
     28 + source, and configuration files.
     29 + 
     30 + "Object" form shall mean any form resulting from mechanical
     31 + transformation or translation of a Source form, including but
     32 + not limited to compiled object code, generated documentation,
     33 + and conversions to other media types.
     34 + 
     35 + "Work" shall mean the work of authorship, whether in Source or
     36 + Object form, made available under the License, as indicated by a
     37 + copyright notice that is included in or attached to the work
     38 + (an example is provided in the Appendix below).
     39 + 
     40 + "Derivative Works" shall mean any work, whether in Source or Object
     41 + form, that is based on (or derived from) the Work and for which the
     42 + editorial revisions, annotations, elaborations, or other modifications
     43 + represent, as a whole, an original work of authorship. For the purposes
     44 + of this License, Derivative Works shall not include works that remain
     45 + separable from, or merely link (or bind by name) to the interfaces of,
     46 + the Work and Derivative Works thereof.
     47 + 
     48 + "Contribution" shall mean any work of authorship, including
     49 + the original version of the Work and any modifications or additions
     50 + to that Work or Derivative Works thereof, that is intentionally
     51 + submitted to Licensor for inclusion in the Work by the copyright owner
     52 + or by an individual or Legal Entity authorized to submit on behalf of
     53 + the copyright owner. For the purposes of this definition, "submitted"
     54 + means any form of electronic, verbal, or written communication sent
     55 + to the Licensor or its representatives, including but not limited to
     56 + communication on electronic mailing lists, source code control systems,
     57 + and issue tracking systems that are managed by, or on behalf of, the
     58 + Licensor for the purpose of discussing and improving the Work, but
     59 + excluding communication that is conspicuously marked or otherwise
     60 + designated in writing by the copyright owner as "Not a Contribution."
     61 + 
     62 + "Contributor" shall mean Licensor and any individual or Legal Entity
     63 + on behalf of whom a Contribution has been received by Licensor and
     64 + subsequently incorporated within the Work.
     65 + 
     66 + 2. Grant of Copyright License. Subject to the terms and conditions of
     67 + this License, each Contributor hereby grants to You a perpetual,
     68 + worldwide, non-exclusive, no-charge, royalty-free, irrevocable
     69 + copyright license to reproduce, prepare Derivative Works of,
     70 + publicly display, publicly perform, sublicense, and distribute the
     71 + Work and such Derivative Works in Source or Object form.
     72 + 
     73 + 3. Grant of Patent License. Subject to the terms and conditions of
     74 + this License, each Contributor hereby grants to You a perpetual,
     75 + worldwide, non-exclusive, no-charge, royalty-free, irrevocable
     76 + (except as stated in this section) patent license to make, have made,
     77 + use, offer to sell, sell, import, and otherwise transfer the Work,
     78 + where such license applies only to those patent claims licensable
     79 + by such Contributor that are necessarily infringed by their
     80 + Contribution(s) alone or by combination of their Contribution(s)
     81 + with the Work to which such Contribution(s) was submitted. If You
     82 + institute patent litigation against any entity (including a
     83 + cross-claim or counterclaim in a lawsuit) alleging that the Work
     84 + or a Contribution incorporated within the Work constitutes direct
     85 + or contributory patent infringement, then any patent licenses
     86 + granted to You under this License for that Work shall terminate
     87 + as of the date such litigation is filed.
     88 + 
     89 + 4. Redistribution. You may reproduce and distribute copies of the
     90 + Work or Derivative Works thereof in any medium, with or without
     91 + modifications, and in Source or Object form, provided that You
     92 + meet the following conditions:
     93 + 
     94 + (a) You must give any other recipients of the Work or
     95 + Derivative Works a copy of this License; and
     96 + 
     97 + (b) You must cause any modified files to carry prominent notices
     98 + stating that You changed the files; and
     99 + 
     100 + (c) You must retain, in the Source form of any Derivative Works
     101 + that You distribute, all copyright, patent, trademark, and
     102 + attribution notices from the Source form of the Work,
     103 + excluding those notices that do not pertain to any part of
     104 + the Derivative Works; and
     105 + 
     106 + (d) If the Work includes a "NOTICE" text file as part of its
     107 + distribution, then any Derivative Works that You distribute must
     108 + include a readable copy of the attribution notices contained
     109 + within such NOTICE file, excluding those notices that do not
     110 + pertain to any part of the Derivative Works, in at least one
     111 + of the following places: within a NOTICE text file distributed
     112 + as part of the Derivative Works; within the Source form or
     113 + documentation, if provided along with the Derivative Works; or,
     114 + within a display generated by the Derivative Works, if and
     115 + wherever such third-party notices normally appear. The contents
     116 + of the NOTICE file are for informational purposes only and
     117 + do not modify the License. You may add Your own attribution
     118 + notices within Derivative Works that You distribute, alongside
     119 + or as an addendum to the NOTICE text from the Work, provided
     120 + that such additional attribution notices cannot be construed
     121 + as modifying the License.
     122 + 
     123 + You may add Your own copyright statement to Your modifications and
     124 + may provide additional or different license terms and conditions
     125 + for use, reproduction, or distribution of Your modifications, or
     126 + for any such Derivative Works as a whole, provided Your use,
     127 + reproduction, and distribution of the Work otherwise complies with
     128 + the conditions stated in this License.
     129 + 
     130 + 5. Submission of Contributions. Unless You explicitly state otherwise,
     131 + any Contribution intentionally submitted for inclusion in the Work
     132 + by You to the Licensor shall be under the terms and conditions of
     133 + this License, without any additional terms or conditions.
     134 + Notwithstanding the above, nothing herein shall supersede or modify
     135 + the terms of any separate license agreement you may have executed
     136 + with Licensor regarding such Contributions.
     137 + 
     138 + 6. Trademarks. This License does not grant permission to use the trade
     139 + names, trademarks, service marks, or product names of the Licensor,
     140 + except as required for reasonable and customary use in describing the
     141 + origin of the Work and reproducing the content of the NOTICE file.
     142 + 
     143 + 7. Disclaimer of Warranty. Unless required by applicable law or
     144 + agreed to in writing, Licensor provides the Work (and each
     145 + Contributor provides its Contributions) on an "AS IS" BASIS,
     146 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
     147 + implied, including, without limitation, any warranties or conditions
     148 + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
     149 + PARTICULAR PURPOSE. You are solely responsible for determining the
     150 + appropriateness of using or redistributing the Work and assume any
     151 + risks associated with Your exercise of permissions under this License.
     152 + 
     153 + 8. Limitation of Liability. In no event and under no legal theory,
     154 + whether in tort (including negligence), contract, or otherwise,
     155 + unless required by applicable law (such as deliberate and grossly
     156 + negligent acts) or agreed to in writing, shall any Contributor be
     157 + liable to You for damages, including any direct, indirect, special,
     158 + incidental, or consequential damages of any character arising as a
     159 + result of this License or out of the use or inability to use the
     160 + Work (including but not limited to damages for loss of goodwill,
     161 + work stoppage, computer failure or malfunction, or any and all
     162 + other commercial damages or losses), even if such Contributor
     163 + has been advised of the possibility of such damages.
     164 + 
     165 + 9. Accepting Warranty or Additional Liability. While redistributing
     166 + the Work or Derivative Works thereof, You may choose to offer,
     167 + and charge a fee for, acceptance of support, warranty, indemnity,
     168 + or other liability obligations and/or rights consistent with this
     169 + License. However, in accepting such obligations, You may act only
     170 + on Your own behalf and on Your sole responsibility, not on behalf
     171 + of any other Contributor, and only if You agree to indemnify,
     172 + defend, and hold each Contributor harmless for any liability
     173 + incurred by, or claims asserted against, such Contributor by reason
     174 + of your accepting any such warranty or additional liability.
     175 + 
     176 + END OF TERMS AND CONDITIONS
     177 + 
     178 + APPENDIX: How to apply the Apache License to your work.
     179 + 
     180 + To apply the Apache License to your work, attach the following
     181 + boilerplate notice, with the fields enclosed by brackets "[]"
     182 + replaced with your own identifying information. (Don't include
     183 + the brackets!) The text should be enclosed in the appropriate
     184 + comment syntax for the file format. We also recommend that a
     185 + file or class name and description of purpose be included on the
     186 + same "printed page" as the copyright notice for easier
     187 + identification within third-party archives.
     188 + 
     189 + Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
     190 + 
     191 + Licensed under the Apache License, Version 2.0 (the "License");
     192 + you may not use this file except in compliance with the License.
     193 + You may obtain a copy of the License at
     194 + 
     195 + http://www.apache.org/licenses/LICENSE-2.0
     196 + 
     197 + Unless required by applicable law or agreed to in writing, software
     198 + distributed under the License is distributed on an "AS IS" BASIS,
     199 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     200 + See the License for the specific language governing permissions and
     201 + limitations under the License.
     202 + 
  • ■ ■ ■ ■ ■ ■
    syft/source/deferred_resolver.go
     1 +package source
     2 + 
     3 +import (
     4 + "io"
     5 + 
     6 + "github.com/anchore/syft/internal/log"
     7 +)
     8 + 
     9 +func NewDeferredResolverFromSource(creator func() (Source, error)) *DeferredResolver {
     10 + return NewDeferredResolver(func() (FileResolver, error) {
     11 + s, err := creator()
     12 + if err != nil {
     13 + return nil, err
     14 + }
     15 + 
     16 + return s.FileResolver(SquashedScope)
     17 + })
     18 +}
     19 + 
     20 +func NewDeferredResolver(creator func() (FileResolver, error)) *DeferredResolver {
     21 + return &DeferredResolver{
     22 + creator: creator,
     23 + }
     24 +}
     25 + 
     26 +type DeferredResolver struct {
     27 + creator func() (FileResolver, error)
     28 + resolver FileResolver
     29 +}
     30 + 
     31 +func (d *DeferredResolver) getResolver() (FileResolver, error) {
     32 + if d.resolver == nil {
     33 + resolver, err := d.creator()
     34 + if err != nil {
     35 + return nil, err
     36 + }
     37 + d.resolver = resolver
     38 + }
     39 + return d.resolver, nil
     40 +}
     41 + 
     42 +func (d *DeferredResolver) FileContentsByLocation(location Location) (io.ReadCloser, error) {
     43 + r, err := d.getResolver()
     44 + if err != nil {
     45 + return nil, err
     46 + }
     47 + return r.FileContentsByLocation(location)
     48 +}
     49 + 
     50 +func (d *DeferredResolver) HasPath(s string) bool {
     51 + r, err := d.getResolver()
     52 + if err != nil {
     53 + log.Debug("unable to get resolver: %v", err)
     54 + return false
     55 + }
     56 + return r.HasPath(s)
     57 +}
     58 + 
     59 +func (d *DeferredResolver) FilesByPath(paths ...string) ([]Location, error) {
     60 + r, err := d.getResolver()
     61 + if err != nil {
     62 + return nil, err
     63 + }
     64 + return r.FilesByPath(paths...)
     65 +}
     66 + 
     67 +func (d *DeferredResolver) FilesByGlob(patterns ...string) ([]Location, error) {
     68 + r, err := d.getResolver()
     69 + if err != nil {
     70 + return nil, err
     71 + }
     72 + return r.FilesByGlob(patterns...)
     73 +}
     74 + 
     75 +func (d *DeferredResolver) FilesByMIMEType(types ...string) ([]Location, error) {
     76 + r, err := d.getResolver()
     77 + if err != nil {
     78 + return nil, err
     79 + }
     80 + return r.FilesByMIMEType(types...)
     81 +}
     82 + 
     83 +func (d *DeferredResolver) RelativeFileByPath(location Location, path string) *Location {
     84 + r, err := d.getResolver()
     85 + if err != nil {
     86 + return nil
     87 + }
     88 + return r.RelativeFileByPath(location, path)
     89 +}
     90 + 
     91 +func (d *DeferredResolver) AllLocations() <-chan Location {
     92 + r, err := d.getResolver()
     93 + if err != nil {
     94 + log.Debug("unable to get resolver: %v", err)
     95 + return nil
     96 + }
     97 + return r.AllLocations()
     98 +}
     99 + 
     100 +func (d *DeferredResolver) FileMetadataByLocation(location Location) (FileMetadata, error) {
     101 + r, err := d.getResolver()
     102 + if err != nil {
     103 + return FileMetadata{}, err
     104 + }
     105 + return r.FileMetadataByLocation(location)
     106 +}
     107 + 
     108 +var _ FileResolver = (*DeferredResolver)(nil)
     109 + 
  • ■ ■ ■ ■ ■ ■
    syft/source/deferred_resolver_test.go
     1 +package source
     2 + 
     3 +import (
     4 + "testing"
     5 + 
     6 + "github.com/stretchr/testify/require"
     7 +)
     8 + 
     9 +func Test_NewDeferredResolver(t *testing.T) {
     10 + creatorCalled := false
     11 + 
     12 + deferredResolver := NewDeferredResolver(func() (FileResolver, error) {
     13 + creatorCalled = true
     14 + return NewMockResolverForPaths(), nil
     15 + })
     16 + 
     17 + require.False(t, creatorCalled)
     18 + require.Nil(t, deferredResolver.resolver)
     19 + 
     20 + _, _ = deferredResolver.FilesByGlob("**/*")
     21 + 
     22 + require.True(t, creatorCalled)
     23 + require.NotNil(t, deferredResolver.resolver)
     24 +}
     25 + 
Please wait...
Page is in error, reload to recover