Skip to content

Commit 2d090ef

Browse files
authored
feat(go): add main module (aquasecurity#6574)
Signed-off-by: knqyf263 <[email protected]>
1 parent 6343e4f commit 2d090ef

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

pkg/dependency/parser/golang/mod/parse.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,29 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency,
8585
skipIndirect = lessThan117(modFileParsed.Go.Version)
8686
}
8787

88+
// Main module
89+
if m := modFileParsed.Module; m != nil {
90+
ver := strings.TrimPrefix(m.Mod.Version, "v")
91+
libs[m.Mod.Path] = types.Library{
92+
ID: packageID(m.Mod.Path, ver),
93+
Name: m.Mod.Path,
94+
Version: ver,
95+
ExternalReferences: p.GetExternalRefs(m.Mod.Path),
96+
Relationship: types.RelationshipRoot,
97+
}
98+
}
99+
88100
// Required modules
89101
for _, require := range modFileParsed.Require {
90102
// Skip indirect dependencies less than Go 1.17
91103
if skipIndirect && require.Indirect {
92104
continue
93105
}
106+
ver := strings.TrimPrefix(require.Mod.Version, "v")
94107
libs[require.Mod.Path] = types.Library{
95-
ID: packageID(require.Mod.Path, require.Mod.Version[1:]),
108+
ID: packageID(require.Mod.Path, ver),
96109
Name: require.Mod.Path,
97-
Version: require.Mod.Version[1:],
110+
Version: ver,
98111
Relationship: lo.Ternary(require.Indirect, types.RelationshipIndirect, types.RelationshipDirect),
99112
ExternalReferences: p.GetExternalRefs(require.Mod.Path),
100113
}

pkg/dependency/parser/golang/mod/parse_testcase.go

+110
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ import "github.com/aquasecurity/trivy/pkg/dependency/types"
55
var (
66
// execute go mod tidy in normal folder
77
GoModNormal = []types.Library{
8+
{
9+
ID: "github.com/org/repo",
10+
Name: "github.com/org/repo",
11+
Relationship: types.RelationshipRoot,
12+
ExternalReferences: []types.ExternalRef{
13+
{
14+
Type: types.RefVCS,
15+
URL: "https://github.com/org/repo",
16+
},
17+
},
18+
},
819
{
920
ID: "github.com/aquasecurity/[email protected]",
1021
Name: "github.com/aquasecurity/go-dep-parser",
@@ -39,6 +50,17 @@ var (
3950

4051
// execute go mod tidy in replaced folder
4152
GoModReplaced = []types.Library{
53+
{
54+
ID: "github.com/org/repo",
55+
Name: "github.com/org/repo",
56+
Relationship: types.RelationshipRoot,
57+
ExternalReferences: []types.ExternalRef{
58+
{
59+
Type: types.RefVCS,
60+
URL: "https://github.com/org/repo",
61+
},
62+
},
63+
},
4264
{
4365
ID: "github.com/aquasecurity/[email protected]",
4466
Name: "github.com/aquasecurity/go-dep-parser",
@@ -61,6 +83,17 @@ var (
6183

6284
// execute go mod tidy in replaced folder
6385
GoModUnreplaced = []types.Library{
86+
{
87+
ID: "github.com/org/repo",
88+
Name: "github.com/org/repo",
89+
Relationship: types.RelationshipRoot,
90+
ExternalReferences: []types.ExternalRef{
91+
{
92+
Type: types.RefVCS,
93+
URL: "https://github.com/org/repo",
94+
},
95+
},
96+
},
6497
{
6598
ID: "github.com/aquasecurity/[email protected]",
6699
Name: "github.com/aquasecurity/go-dep-parser",
@@ -83,6 +116,17 @@ var (
83116

84117
// execute go mod tidy in replaced-with-version folder
85118
GoModReplacedWithVersion = []types.Library{
119+
{
120+
ID: "github.com/org/repo",
121+
Name: "github.com/org/repo",
122+
Relationship: types.RelationshipRoot,
123+
ExternalReferences: []types.ExternalRef{
124+
{
125+
Type: types.RefVCS,
126+
URL: "https://github.com/org/repo",
127+
},
128+
},
129+
},
86130
{
87131
ID: "github.com/aquasecurity/[email protected]",
88132
Name: "github.com/aquasecurity/go-dep-parser",
@@ -105,6 +149,17 @@ var (
105149

106150
// execute go mod tidy in replaced-with-version-mismatch folder
107151
GoModReplacedWithVersionMismatch = []types.Library{
152+
{
153+
ID: "github.com/org/repo",
154+
Name: "github.com/org/repo",
155+
Relationship: types.RelationshipRoot,
156+
ExternalReferences: []types.ExternalRef{
157+
{
158+
Type: types.RefVCS,
159+
URL: "https://github.com/org/repo",
160+
},
161+
},
162+
},
108163
{
109164
ID: "github.com/aquasecurity/[email protected]",
110165
Name: "github.com/aquasecurity/go-dep-parser",
@@ -139,6 +194,17 @@ var (
139194

140195
// execute go mod tidy in replaced-with-local-path folder
141196
GoModReplacedWithLocalPath = []types.Library{
197+
{
198+
ID: "github.com/org/repo",
199+
Name: "github.com/org/repo",
200+
Relationship: types.RelationshipRoot,
201+
ExternalReferences: []types.ExternalRef{
202+
{
203+
Type: types.RefVCS,
204+
URL: "https://github.com/org/repo",
205+
},
206+
},
207+
},
142208
{
143209
ID: "github.com/aquasecurity/[email protected]",
144210
Name: "github.com/aquasecurity/go-dep-parser",
@@ -167,6 +233,17 @@ var (
167233

168234
// execute go mod tidy in replaced-with-local-path-and-version folder
169235
GoModReplacedWithLocalPathAndVersion = []types.Library{
236+
{
237+
ID: "github.com/org/repo",
238+
Name: "github.com/org/repo",
239+
Relationship: types.RelationshipRoot,
240+
ExternalReferences: []types.ExternalRef{
241+
{
242+
Type: types.RefVCS,
243+
URL: "https://github.com/org/repo",
244+
},
245+
},
246+
},
170247
{
171248
ID: "github.com/aquasecurity/[email protected]",
172249
Name: "github.com/aquasecurity/go-dep-parser",
@@ -195,6 +272,17 @@ var (
195272

196273
// execute go mod tidy in replaced-with-local-path-and-version-mismatch folder
197274
GoModReplacedWithLocalPathAndVersionMismatch = []types.Library{
275+
{
276+
ID: "github.com/org/repo",
277+
Name: "github.com/org/repo",
278+
Relationship: types.RelationshipRoot,
279+
ExternalReferences: []types.ExternalRef{
280+
{
281+
Type: types.RefVCS,
282+
URL: "https://github.com/org/repo",
283+
},
284+
},
285+
},
198286
{
199287
ID: "github.com/aquasecurity/[email protected]",
200288
Name: "github.com/aquasecurity/go-dep-parser",
@@ -229,6 +317,17 @@ var (
229317

230318
// execute go mod tidy in go116 folder
231319
GoMod116 = []types.Library{
320+
{
321+
ID: "github.com/org/repo",
322+
Name: "github.com/org/repo",
323+
Relationship: types.RelationshipRoot,
324+
ExternalReferences: []types.ExternalRef{
325+
{
326+
Type: types.RefVCS,
327+
URL: "https://github.com/org/repo",
328+
},
329+
},
330+
},
232331
{
233332
ID: "github.com/aquasecurity/[email protected]",
234333
Name: "github.com/aquasecurity/go-dep-parser",
@@ -245,6 +344,17 @@ var (
245344

246345
// execute go mod tidy in no-go-version folder
247346
GoModNoGoVersion = []types.Library{
347+
{
348+
ID: "github.com/org/repo",
349+
Name: "github.com/org/repo",
350+
Relationship: types.RelationshipRoot,
351+
ExternalReferences: []types.ExternalRef{
352+
{
353+
Type: types.RefVCS,
354+
URL: "https://github.com/org/repo",
355+
},
356+
},
357+
},
248358
{
249359
ID: "github.com/aquasecurity/[email protected]",
250360
Name: "github.com/aquasecurity/go-dep-parser",

pkg/fanal/analyzer/language/golang/mod/mod_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func Test_gomodAnalyzer_Analyze(t *testing.T) {
3232
Type: types.GoModule,
3333
FilePath: "go.mod",
3434
Libraries: types.Packages{
35+
{
36+
ID: "github.com/org/repo",
37+
Name: "github.com/org/repo",
38+
Relationship: types.RelationshipRoot,
39+
},
3540
{
3641
ID: "github.com/aquasecurity/[email protected]",
3742
Name: "github.com/aquasecurity/go-dep-parser",
@@ -67,6 +72,11 @@ func Test_gomodAnalyzer_Analyze(t *testing.T) {
6772
Type: types.GoModule,
6873
FilePath: "go.mod",
6974
Libraries: types.Packages{
75+
{
76+
ID: "github.com/org/repo",
77+
Name: "github.com/org/repo",
78+
Relationship: types.RelationshipRoot,
79+
},
7080
{
7181
ID: "github.com/sad/[email protected]",
7282
Name: "github.com/sad/sad",
@@ -90,6 +100,11 @@ func Test_gomodAnalyzer_Analyze(t *testing.T) {
90100
Type: types.GoModule,
91101
FilePath: "go.mod",
92102
Libraries: types.Packages{
103+
{
104+
ID: "github.com/org/repo",
105+
Name: "github.com/org/repo",
106+
Relationship: types.RelationshipRoot,
107+
},
93108
{
94109
ID: "github.com/aquasecurity/[email protected]",
95110
Name: "github.com/aquasecurity/go-dep-parser",
@@ -125,6 +140,11 @@ func Test_gomodAnalyzer_Analyze(t *testing.T) {
125140
Type: types.GoModule,
126141
FilePath: "go.mod",
127142
Libraries: types.Packages{
143+
{
144+
ID: "github.com/org/repo",
145+
Name: "github.com/org/repo",
146+
Relationship: types.RelationshipRoot,
147+
},
128148
{
129149
ID: "github.com/aquasecurity/[email protected]",
130150
Name: "github.com/aquasecurity/go-dep-parser",

0 commit comments

Comments
 (0)