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

fix(dotnet): show nuget package dir not found log only when checking nuget packages #7194

Merged
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
6 changes: 6 additions & 0 deletions pkg/fanal/analyzer/language/dotnet/nuget/nuget.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer/language"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/utils/fsutils"
)

Expand All @@ -39,19 +40,24 @@ type nugetLibraryAnalyzer struct {
lockParser language.Parser
configParser language.Parser
licenseParser nuspecParser
logger *log.Logger
}

func newNugetLibraryAnalyzer(_ analyzer.AnalyzerOptions) (analyzer.PostAnalyzer, error) {
return &nugetLibraryAnalyzer{
lockParser: lock.NewParser(),
configParser: config.NewParser(),
licenseParser: newNuspecParser(),
logger: log.WithPrefix("nuget"),
}, nil
}

func (a *nugetLibraryAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAnalysisInput) (*analyzer.AnalysisResult, error) {
var apps []types.Application
foundLicenses := make(map[string][]string)
if a.licenseParser.packagesDir == "" {
a.logger.Debug("The nuget packages directory couldn't be found. License search disabled")
}

// We saved only config and lock files in the FS,
// so we need to parse all saved files
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/language/dotnet/nuget/nuget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/aquasecurity/trivy/pkg/fanal/types"
)

func Test_nugetibraryAnalyzer_Analyze(t *testing.T) {
func Test_nugetLibraryAnalyzer_Analyze(t *testing.T) {
tests := []struct {
name string
dir string
Expand Down
6 changes: 0 additions & 6 deletions pkg/fanal/analyzer/language/dotnet/nuget/nuspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/utils/fsutils"
)

Expand All @@ -30,26 +29,21 @@ type License struct {
}

type nuspecParser struct {
logger *log.Logger
packagesDir string // global packages folder - https: //learn.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders
}

func newNuspecParser() nuspecParser {
logger := log.WithPrefix("nuget")

// cf. https: //learn.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders
packagesDir := os.Getenv("NUGET_PACKAGES")
if packagesDir == "" {
packagesDir = filepath.Join(os.Getenv("HOME"), ".nuget", "packages")
}

if !fsutils.DirExists(packagesDir) {
logger.Debug("The nuget packages directory couldn't be found. License search disabled")
return nuspecParser{}
}

return nuspecParser{
logger: logger,
packagesDir: packagesDir,
}
}
Expand Down