Skip to content

Commit 46e55b9

Browse files
committed
Fix the file path in the Sonarqube report
Add some test to validate the Sonarqube formatter. Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent 04dc713 commit 46e55b9

File tree

6 files changed

+172
-9
lines changed

6 files changed

+172
-9
lines changed

cmd/gosec/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,11 @@ func main() {
318318
os.Exit(0)
319319
}
320320

321-
rootPath := packages[0]
321+
rootPath, err := gosec.RootPath(flag.Args()[0])
322+
if err != nil {
323+
logger.Fatalf("Failed to get the root path of the project: %s", err)
324+
}
325+
322326
// Create output report
323327
if err := saveOutput(*flagOutput, *flagFormat, rootPath, issues, metrics, errors); err != nil {
324328
logger.Fatal(err)

helpers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,11 @@ func PackagePaths(root string, exclude *regexp.Regexp) ([]string, error) {
387387
}
388388
return result, nil
389389
}
390+
391+
// RootPath returns the absolute root path of a scan
392+
func RootPath(root string) (string, error) {
393+
if strings.HasSuffix(root, "...") {
394+
root = root[0 : len(root)-3]
395+
}
396+
return filepath.Abs(root)
397+
}

helpers_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gosec_test
33
import (
44
"io/ioutil"
55
"os"
6+
"path/filepath"
67
"regexp"
78

89
. "github.com/onsi/ginkgo"
@@ -53,4 +54,23 @@ var _ = Describe("Helpers", func() {
5354
Expect(paths).Should(BeEmpty())
5455
})
5556
})
57+
58+
Context("when getting the root path", func() {
59+
It("should return the absolute path from relative path", func() {
60+
base := "test"
61+
cwd, err := os.Getwd()
62+
Expect(err).ShouldNot(HaveOccurred())
63+
root, err := gosec.RootPath(base)
64+
Expect(err).ShouldNot(HaveOccurred())
65+
Expect(root).Should(Equal(filepath.Join(cwd, base)))
66+
})
67+
It("should retrun the absolute path from ellipsis path", func() {
68+
base := "test"
69+
cwd, err := os.Getwd()
70+
Expect(err).ShouldNot(HaveOccurred())
71+
root, err := gosec.RootPath(filepath.Join(base, "..."))
72+
Expect(err).ShouldNot(HaveOccurred())
73+
Expect(root).Should(Equal(filepath.Join(cwd, base)))
74+
})
75+
})
5676
})

output/formatter.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,32 @@ func CreateReport(w io.Writer, format, rootPath string, issues []*gosec.Issue, m
105105
}
106106

107107
func reportSonarqube(rootPath string, w io.Writer, data *reportInfo) error {
108+
si, err := convertToSonarIssues(rootPath, data)
109+
if err != nil {
110+
return err
111+
}
112+
raw, err := json.MarshalIndent(si, "", "\t")
113+
if err != nil {
114+
return err
115+
}
116+
_, err = w.Write(raw)
117+
return err
118+
}
119+
120+
func convertToSonarIssues(rootPath string, data *reportInfo) (sonarIssues, error) {
108121
var si sonarIssues
109122
for _, issue := range data.Issues {
110123
lines := strings.Split(issue.Line, "-")
111124

112125
startLine, err := strconv.Atoi(lines[0])
113126
if err != nil {
114-
return err
127+
return si, err
115128
}
116129
endLine := startLine
117130
if len(lines) > 1 {
118131
endLine, err = strconv.Atoi(lines[1])
119132
if err != nil {
120-
return err
133+
return si, err
121134
}
122135
}
123136
s := sonarIssue{
@@ -134,12 +147,7 @@ func reportSonarqube(rootPath string, w io.Writer, data *reportInfo) error {
134147
}
135148
si.SonarIssues = append(si.SonarIssues, s)
136149
}
137-
raw, err := json.MarshalIndent(si, "", "\t")
138-
if err != nil {
139-
return err
140-
}
141-
_, err = w.Write(raw)
142-
return err
150+
return si, nil
143151
}
144152

145153
func reportJSON(w io.Writer, data *reportInfo) error {

output/formatter_suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package output
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
7+
"testing"
8+
)
9+
10+
func TestRules(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Formatters Suite")
13+
}

output/formatter_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package output
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
7+
"github.com/securego/gosec"
8+
)
9+
10+
var _ = Describe("Formatter", func() {
11+
BeforeEach(func() {
12+
})
13+
Context("when converting to Sonarqube issues", func() {
14+
It("it should parse the report info", func() {
15+
data := &reportInfo{
16+
Errors: map[string][]gosec.Error{},
17+
Issues: []*gosec.Issue{
18+
&gosec.Issue{
19+
Severity: 2,
20+
Confidence: 0,
21+
RuleID: "test",
22+
What: "test",
23+
File: "/home/src/project/test.go",
24+
Code: "",
25+
Line: "1-2",
26+
},
27+
},
28+
Stats: &gosec.Metrics{
29+
NumFiles: 0,
30+
NumLines: 0,
31+
NumNosec: 0,
32+
NumFound: 0,
33+
},
34+
}
35+
want := sonarIssues{
36+
SonarIssues: []sonarIssue{
37+
{
38+
EngineID: "gosec",
39+
RuleID: "test",
40+
PrimaryLocation: location{
41+
Message: "test",
42+
FilePath: "test.go",
43+
TextRange: textRange{
44+
StartLine: 1,
45+
EndLine: 2,
46+
},
47+
},
48+
Type: "VULNERABILITY",
49+
Severity: "BLOCKER",
50+
EffortMinutes: SonarqubeEffortMinutes,
51+
},
52+
},
53+
}
54+
55+
rootPath := "/home/src/project"
56+
57+
issues, err := convertToSonarIssues(rootPath, data)
58+
Expect(err).ShouldNot(HaveOccurred())
59+
Expect(issues).To(Equal(want))
60+
})
61+
62+
It("it should parse the report info with files in subfolders", func() {
63+
data := &reportInfo{
64+
Errors: map[string][]gosec.Error{},
65+
Issues: []*gosec.Issue{
66+
&gosec.Issue{
67+
Severity: 2,
68+
Confidence: 0,
69+
RuleID: "test",
70+
What: "test",
71+
File: "/home/src/project/subfolder/test.go",
72+
Code: "",
73+
Line: "1-2",
74+
},
75+
},
76+
Stats: &gosec.Metrics{
77+
NumFiles: 0,
78+
NumLines: 0,
79+
NumNosec: 0,
80+
NumFound: 0,
81+
},
82+
}
83+
want := sonarIssues{
84+
SonarIssues: []sonarIssue{
85+
{
86+
EngineID: "gosec",
87+
RuleID: "test",
88+
PrimaryLocation: location{
89+
Message: "test",
90+
FilePath: "subfolder/test.go",
91+
TextRange: textRange{
92+
StartLine: 1,
93+
EndLine: 2,
94+
},
95+
},
96+
Type: "VULNERABILITY",
97+
Severity: "BLOCKER",
98+
EffortMinutes: SonarqubeEffortMinutes,
99+
},
100+
},
101+
}
102+
103+
rootPath := "/home/src/project"
104+
105+
issues, err := convertToSonarIssues(rootPath, data)
106+
Expect(err).ShouldNot(HaveOccurred())
107+
Expect(issues).To(Equal(want))
108+
})
109+
})
110+
})

0 commit comments

Comments
 (0)