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

feat(go): add main module #6574

Merged
merged 1 commit into from
Apr 27, 2024
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
17 changes: 15 additions & 2 deletions pkg/dependency/parser/golang/mod/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,29 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency,
skipIndirect = lessThan117(modFileParsed.Go.Version)
}

// Main module
if m := modFileParsed.Module; m != nil {
ver := strings.TrimPrefix(m.Mod.Version, "v")
libs[m.Mod.Path] = types.Library{
ID: packageID(m.Mod.Path, ver),
Name: m.Mod.Path,
Version: ver,
ExternalReferences: p.GetExternalRefs(m.Mod.Path),
Relationship: types.RelationshipRoot,
}
}

// Required modules
for _, require := range modFileParsed.Require {
// Skip indirect dependencies less than Go 1.17
if skipIndirect && require.Indirect {
continue
}
ver := strings.TrimPrefix(require.Mod.Version, "v")
libs[require.Mod.Path] = types.Library{
ID: packageID(require.Mod.Path, require.Mod.Version[1:]),
ID: packageID(require.Mod.Path, ver),
Name: require.Mod.Path,
Version: require.Mod.Version[1:],
Version: ver,
Relationship: lo.Ternary(require.Indirect, types.RelationshipIndirect, types.RelationshipDirect),
ExternalReferences: p.GetExternalRefs(require.Mod.Path),
}
Expand Down
110 changes: 110 additions & 0 deletions pkg/dependency/parser/golang/mod/parse_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import "github.com/aquasecurity/trivy/pkg/dependency/types"
var (
// execute go mod tidy in normal folder
GoModNormal = []types.Library{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
ExternalReferences: []types.ExternalRef{
{
Type: types.RefVCS,
URL: "https://github.com/org/repo",
},
},
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20211224170007-df43bca6b6ff",
Name: "github.com/aquasecurity/go-dep-parser",
Expand Down Expand Up @@ -39,6 +50,17 @@ var (

// execute go mod tidy in replaced folder
GoModReplaced = []types.Library{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
ExternalReferences: []types.ExternalRef{
{
Type: types.RefVCS,
URL: "https://github.com/org/repo",
},
},
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20220406074731-71021a481237",
Name: "github.com/aquasecurity/go-dep-parser",
Expand All @@ -61,6 +83,17 @@ var (

// execute go mod tidy in replaced folder
GoModUnreplaced = []types.Library{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
ExternalReferences: []types.ExternalRef{
{
Type: types.RefVCS,
URL: "https://github.com/org/repo",
},
},
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20211110174639-8257534ffed3",
Name: "github.com/aquasecurity/go-dep-parser",
Expand All @@ -83,6 +116,17 @@ var (

// execute go mod tidy in replaced-with-version folder
GoModReplacedWithVersion = []types.Library{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
ExternalReferences: []types.ExternalRef{
{
Type: types.RefVCS,
URL: "https://github.com/org/repo",
},
},
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20220406074731-71021a481237",
Name: "github.com/aquasecurity/go-dep-parser",
Expand All @@ -105,6 +149,17 @@ var (

// execute go mod tidy in replaced-with-version-mismatch folder
GoModReplacedWithVersionMismatch = []types.Library{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
ExternalReferences: []types.ExternalRef{
{
Type: types.RefVCS,
URL: "https://github.com/org/repo",
},
},
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20211224170007-df43bca6b6ff",
Name: "github.com/aquasecurity/go-dep-parser",
Expand Down Expand Up @@ -139,6 +194,17 @@ var (

// execute go mod tidy in replaced-with-local-path folder
GoModReplacedWithLocalPath = []types.Library{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
ExternalReferences: []types.ExternalRef{
{
Type: types.RefVCS,
URL: "https://github.com/org/repo",
},
},
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20211224170007-df43bca6b6ff",
Name: "github.com/aquasecurity/go-dep-parser",
Expand Down Expand Up @@ -167,6 +233,17 @@ var (

// execute go mod tidy in replaced-with-local-path-and-version folder
GoModReplacedWithLocalPathAndVersion = []types.Library{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
ExternalReferences: []types.ExternalRef{
{
Type: types.RefVCS,
URL: "https://github.com/org/repo",
},
},
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20211224170007-df43bca6b6ff",
Name: "github.com/aquasecurity/go-dep-parser",
Expand Down Expand Up @@ -195,6 +272,17 @@ var (

// execute go mod tidy in replaced-with-local-path-and-version-mismatch folder
GoModReplacedWithLocalPathAndVersionMismatch = []types.Library{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
ExternalReferences: []types.ExternalRef{
{
Type: types.RefVCS,
URL: "https://github.com/org/repo",
},
},
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20211224170007-df43bca6b6ff",
Name: "github.com/aquasecurity/go-dep-parser",
Expand Down Expand Up @@ -229,6 +317,17 @@ var (

// execute go mod tidy in go116 folder
GoMod116 = []types.Library{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
ExternalReferences: []types.ExternalRef{
{
Type: types.RefVCS,
URL: "https://github.com/org/repo",
},
},
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20211224170007-df43bca6b6ff",
Name: "github.com/aquasecurity/go-dep-parser",
Expand All @@ -245,6 +344,17 @@ var (

// execute go mod tidy in no-go-version folder
GoModNoGoVersion = []types.Library{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
ExternalReferences: []types.ExternalRef{
{
Type: types.RefVCS,
URL: "https://github.com/org/repo",
},
},
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20211224170007-df43bca6b6ff",
Name: "github.com/aquasecurity/go-dep-parser",
Expand Down
20 changes: 20 additions & 0 deletions pkg/fanal/analyzer/language/golang/mod/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func Test_gomodAnalyzer_Analyze(t *testing.T) {
Type: types.GoModule,
FilePath: "go.mod",
Libraries: types.Packages{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20220406074731-71021a481237",
Name: "github.com/aquasecurity/go-dep-parser",
Expand Down Expand Up @@ -67,6 +72,11 @@ func Test_gomodAnalyzer_Analyze(t *testing.T) {
Type: types.GoModule,
FilePath: "go.mod",
Libraries: types.Packages{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
},
{
ID: "github.com/sad/sad@v0.0.1",
Name: "github.com/sad/sad",
Expand All @@ -90,6 +100,11 @@ func Test_gomodAnalyzer_Analyze(t *testing.T) {
Type: types.GoModule,
FilePath: "go.mod",
Libraries: types.Packages{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20230219131432-590b1dfb6edd",
Name: "github.com/aquasecurity/go-dep-parser",
Expand Down Expand Up @@ -125,6 +140,11 @@ func Test_gomodAnalyzer_Analyze(t *testing.T) {
Type: types.GoModule,
FilePath: "go.mod",
Libraries: types.Packages{
{
ID: "github.com/org/repo",
Name: "github.com/org/repo",
Relationship: types.RelationshipRoot,
},
{
ID: "github.com/aquasecurity/go-dep-parser@v0.0.0-20230219131432-590b1dfb6edd",
Name: "github.com/aquasecurity/go-dep-parser",
Expand Down