Skip to content

Commit

Permalink
- Make local NuGet cache folder configurable.
Browse files Browse the repository at this point in the history
Signed-off-by: HeyeOpenSource <[email protected]>
  • Loading branch information
HeyeOpenSource committed Oct 23, 2024
1 parent fda1188 commit a745223
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/syft/internal/options/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (cfg Catalog) ToPackagesConfig() pkgcataloging.Config {
),
DotNet: dotnet.DefaultCatalogerConfig().
WithSearchLocalLicenses(cfg.DotNet.SearchLocalLicenses).
WithLocalCachePaths(cfg.DotNet.LocalCachePaths).
WithSearchRemoteLicenses(cfg.DotNet.SearchRemoteLicenses).
WithProviders(cfg.DotNet.Providers).
WithCredentials(cfg.DotNet.ProviderCredentials),
Expand Down
4 changes: 4 additions & 0 deletions cmd/syft/internal/options/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type dotnetConfig struct {
SearchLocalLicenses bool `yaml:"search-local-licenses" json:"search-local-licenses" mapstructure:"search-local-licenses"`
LocalCachePaths string `yaml:"local-cache-paths" json:"local-cache-paths" mapstructure:"local-cache-paths"`
SearchRemoteLicenses bool `yaml:"search-remote-licenses" json:"search-remote-licenses" mapstructure:"search-remote-licenses"`
Providers string `yaml:"package-providers,omitempty" json:"package-providers,omitempty" mapstructure:"package-providers"`
ProviderCredentials string `yaml:"package-provider-credentials,omitempty" json:"package-provider-credentials,omitempty" mapstructure:"package-provider-credentials"`
Expand All @@ -21,6 +22,8 @@ var _ interface {
func (o *dotnetConfig) DescribeFields(descriptions clio.FieldDescriptionSet) {
descriptions.Add(&o.SearchLocalLicenses, `search for NuGet package licences in the local cache of the system running Syft, note that this is outside the
container filesystem and probably outside the root of a local directory scan`)
descriptions.Add(&o.LocalCachePaths, `local cache folders (comma-separated) to use when retrieving NuGet packages locally,
if unset this defaults to the NuGet cache folders known to the DotNet environment`)
descriptions.Add(&o.SearchRemoteLicenses, `search for NuGet package licences by retrieving the package from a network proxy`)
descriptions.Add(&o.Providers, `remote NuGet package providers (comma-separated) to use when retrieving NuGet packages from the network,
if unset this defaults to the NuGet-repositories known to the DotNet environment`)
Expand All @@ -40,6 +43,7 @@ func defaultDotnetConfig() dotnetConfig {
}
return dotnetConfig{
SearchLocalLicenses: def.SearchLocalLicenses,
LocalCachePaths: strings.Join(def.LocalCachePaths, ","),
SearchRemoteLicenses: def.SearchRemoteLicenses,
Providers: strings.Join(def.Providers, ","),
ProviderCredentials: providerCredentials,
Expand Down
9 changes: 9 additions & 0 deletions syft/pkg/cataloger/dotnet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type nugetProviderCredential struct {

type CatalogerConfig struct {
SearchLocalLicenses bool `yaml:"search-local-licenses" json:"search-local-licenses" mapstructure:"search-local-licenses"`
LocalCachePaths []string `yaml:"local-cache-paths" json:"local-cache-paths" mapstructure:"local-cache-paths"`
SearchRemoteLicenses bool `yaml:"search-remote-licenses" json:"search-remote-licenses" mapstructure:"search-remote-licenses"`
Providers []string `yaml:"package-providers,omitempty" json:"package-providers,omitempty" mapstructure:"package-providers"`
ProviderCredentials []nugetProviderCredential `yaml:"package-provider-credentials,omitempty" json:"package-provider-credentials,omitempty" mapstructure:"package-provider-credentials"`
Expand All @@ -49,6 +50,14 @@ func (g CatalogerConfig) WithSearchLocalLicenses(input bool) CatalogerConfig {
return g
}

func (g CatalogerConfig) WithLocalCachePaths(input string) CatalogerConfig {
if input == "" {
return g
}
g.LocalCachePaths = strings.Split(input, ",")
return g
}

func (g CatalogerConfig) WithSearchRemoteLicenses(input bool) CatalogerConfig {
g.SearchRemoteLicenses = input
return g
Expand Down
11 changes: 6 additions & 5 deletions syft/pkg/cataloger/dotnet/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"
"io/fs"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
Expand Down Expand Up @@ -74,7 +73,7 @@ func (c *nugetLicenseResolver) getLicenses(ctx context.Context, scanner licenses
if c.opts.SearchLocalLicenses {
if c.localNuGetCacheResolvers == nil {
// Try to determine NuGet package folder resolvers
c.localNuGetCacheResolvers = getLocalNugetFolderResolvers(c.assetDefinitions)
c.localNuGetCacheResolvers = c.getLocalNugetFolderResolvers(c.assetDefinitions)
}

// if we're running against a directory on the filesystem, it may not include the
Expand Down Expand Up @@ -589,10 +588,12 @@ func getNuGetCachesFromSDK() []string {
return paths
}

func getLocalNugetFolderResolvers(assetDefinitions []projectAssets) []file.Resolver {
func (c *nugetLicenseResolver) getLocalNugetFolderResolvers(assetDefinitions []projectAssets) []file.Resolver {
nugetPackagePaths := []string{}
if injectedCachePath := os.Getenv("TEST_PARSE_DOTNET_DEPS_INJECT_CACHE_LOCATION"); injectedCachePath != "" {
nugetPackagePaths = append(nugetPackagePaths, injectedCachePath)
if len(c.opts.LocalCachePaths) > 0 {
for _, cachePath := range c.opts.LocalCachePaths {
nugetPackagePaths = append(nugetPackagePaths, cachePath)
}
} else {
nugetPackagePaths = append(nugetPackagePaths, getNuGetCachesFromProjectAssets(assetDefinitions)...)

Expand Down
4 changes: 1 addition & 3 deletions syft/pkg/cataloger/dotnet/parse_dotnet_deps_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dotnet

import (
"os"
"testing"

"github.com/anchore/syft/syft/artifact"
Expand Down Expand Up @@ -400,13 +399,12 @@ func TestParseDotnetDeps(t *testing.T) {
}

t.Run(fixture, func(t *testing.T) {
os.Setenv("TEST_PARSE_DOTNET_DEPS_INJECT_CACHE_LOCATION", "test-fixtures/NuGetCache")
defer os.Setenv("TEST_PARSE_DOTNET_DEPS_INJECT_CACHE_LOCATION", "")
pkgtest.NewCatalogTester().
FromDirectory(t, "test-fixtures").
Expects(expectedPkgs, expectedRelationships).
TestCataloger(t, NewDotnetDepsCataloger(CatalogerConfig{
SearchLocalLicenses: true,
LocalCachePaths: []string{"test-fixtures/NuGetCache"},
}))
})
}

0 comments on commit a745223

Please sign in to comment.