Skip to content

Commit

Permalink
Fix lint and fail on error in the ci build
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorel-35 authored May 31, 2021
1 parent dbb9811 commit 1256f16
Show file tree
Hide file tree
Showing 51 changed files with 216 additions and 201 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
${{ runner.os }}-go-
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
continue-on-error: true
with:
version: latest
test:
Expand Down
26 changes: 18 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
linters:
enable:
- megacheck
- govet
- unparam
- unconvert
- misspell
- asciicheck
- bodyclose
- depguard
- dogsled
- durationcheck
- errcheck
- exportloopref
- gofmt
- golint
- gofumpt
- goimports
- gosec
- govet
- importas
- megacheck
- misspell
- nakedret
- dogsled
- depguard
- nolintlint
- revive
- unconvert
- unparam
- wastedassign
1 change: 0 additions & 1 deletion analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"reflect"
"regexp"
"strconv"

"strings"

"golang.org/x/tools/go/packages"
Expand Down
7 changes: 0 additions & 7 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
)

var _ = Describe("Analyzer", func() {

var (
analyzer *gosec.Analyzer
logger *log.Logger
Expand All @@ -30,7 +29,6 @@ var _ = Describe("Analyzer", func() {
})

Context("when processing a package", func() {

It("should not report an error if the package contains no Go files", func() {
analyzer.LoadRules(rules.Generate().Builders())
dir, err := ioutil.TempDir("", "empty")
Expand Down Expand Up @@ -118,7 +116,6 @@ var _ = Describe("Analyzer", func() {
Expect(err).ShouldNot(HaveOccurred())
controlIssues, _, _ := analyzer.Report()
Expect(controlIssues).Should(HaveLen(sample.Errors))

})

It("should report Go build errors and invalid files", func() {
Expand Down Expand Up @@ -262,7 +259,6 @@ var _ = Describe("Analyzer", func() {
Expect(err).ShouldNot(HaveOccurred())
nosecIssues, _, _ := customAnalyzer.Report()
Expect(nosecIssues).Should(HaveLen(sample.Errors))

})

It("should be possible to use an alternative nosec tag", func() {
Expand All @@ -286,7 +282,6 @@ var _ = Describe("Analyzer", func() {
Expect(err).ShouldNot(HaveOccurred())
nosecIssues, _, _ := customAnalyzer.Report()
Expect(nosecIssues).Should(HaveLen(0))

})

It("should ignore vulnerabilities when the default tag is found", func() {
Expand All @@ -310,7 +305,6 @@ var _ = Describe("Analyzer", func() {
Expect(err).ShouldNot(HaveOccurred())
nosecIssues, _, _ := customAnalyzer.Report()
Expect(nosecIssues).Should(HaveLen(0))

})

It("should be able to analyze Go test package", func() {
Expand Down Expand Up @@ -356,7 +350,6 @@ var _ = Describe("Analyzer", func() {
})

Context("when parsing errors from a package", func() {

It("should return no error when the error list is empty", func() {
pkg := &packages.Package{}
err := analyzer.ParseErrors(pkg)
Expand Down
4 changes: 1 addition & 3 deletions call_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
)

var _ = Describe("Call List", func() {
var (
calls gosec.CallList
)
var calls gosec.CallList
BeforeEach(func() {
calls = gosec.NewCallList()
})
Expand Down
8 changes: 3 additions & 5 deletions cmd/gosec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ var (
// stdout the results as well as write it in the output file
flagStdOut = flag.Bool("stdout", false, "Stdout the results as well as write it in the output file")

//print the text report with color, this is enabled by default
// print the text report with color, this is enabled by default
flagColor = flag.Bool("color", true, "Prints the text format report with colorization when it goes in the stdout")

// overrides the output format when stdout the results while saving them in the output file
Expand Down Expand Up @@ -209,15 +209,14 @@ func getRootPaths(paths []string) []string {
}

func getPrintedFormat(format string, verbose string) string {
var fileFormat = format
fileFormat := format
if format != "" && verbose != "" {
fileFormat = verbose
}
return fileFormat
}

func printReport(format string, color bool, rootPaths []string, reportInfo *gosec.ReportInfo) error {

err := report.CreateReport(os.Stdout, format, color, rootPaths, reportInfo)
if err != nil {
return err
Expand All @@ -226,7 +225,6 @@ func printReport(format string, color bool, rootPaths []string, reportInfo *gose
}

func saveReport(filename, format string, rootPaths []string, reportInfo *gosec.ReportInfo) error {

outfile, err := os.Create(filename)
if err != nil {
return err
Expand Down Expand Up @@ -386,7 +384,7 @@ func main() {
reportInfo := gosec.NewReportInfo(issues, metrics, errors).WithVersion(Version)

if *flagOutput == "" || *flagStdOut {
var fileFormat = getPrintedFormat(*flagOutput, *flagVerbose)
fileFormat := getPrintedFormat(*flagOutput, *flagVerbose)
if err := printReport(fileFormat, *flagColor, rootPaths, reportInfo); err != nil {
logger.Fatal((err))
}
Expand Down
1 change: 0 additions & 1 deletion cmd/gosec/sort_issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
func extractLineNumber(s string) int {
lineNumber, _ := strconv.Atoi(strings.Split(s, "-")[0])
return lineNumber

}

type sortBySeverity []*gosec.Issue
Expand Down
18 changes: 9 additions & 9 deletions cmd/gosecutil/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ import (
"strings"
)

type command func(args ...string)
type utilities struct {
commands map[string]command
call []string
}
type (
command func(args ...string)
utilities struct {
commands map[string]command
call []string
}
)

// Custom commands / utilities to run instead of default analyzer
func newUtils() *utilities {
Expand Down Expand Up @@ -58,7 +60,6 @@ func (u *utilities) String() string {
func (u *utilities) Set(opt string) error {
if _, ok := u.commands[opt]; !ok {
return fmt.Errorf("valid tools are: %s", u.String())

}
u.call = append(u.call, opt)
return nil
Expand Down Expand Up @@ -171,7 +172,6 @@ func checkContext(ctx *context, file string) bool {
}

func dumpCallObj(files ...string) {

for _, file := range files {
if shouldSkip(file) {
continue
Expand All @@ -184,9 +184,9 @@ func dumpCallObj(files ...string) {
var obj types.Object
switch node := n.(type) {
case *ast.Ident:
obj = context.info.ObjectOf(node) //context.info.Uses[node]
obj = context.info.ObjectOf(node) // context.info.Uses[node]
case *ast.SelectorExpr:
obj = context.info.ObjectOf(node.Sel) //context.info.Uses[node.Sel]
obj = context.info.ObjectOf(node.Sel) // context.info.Uses[node.Sel]
default:
obj = nil
}
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c Config) convertGlobals() {

// ReadFrom implements the io.ReaderFrom interface. This
// should be used with io.Reader to load configuration from
//file or from string etc.
// file or from string etc.
func (c Config) ReadFrom(r io.Reader) (int64, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var _ = Describe("Configuration", func() {
})

Context("when loading from disk", func() {

It("should be possible to load configuration from a file", func() {
json := `{"G101": {}}`
buffer := bytes.NewBufferString(json)
Expand All @@ -35,7 +34,6 @@ var _ = Describe("Configuration", func() {
_, err = configuration.ReadFrom(emptyBuffer)
Expect(err).Should(HaveOccurred())
})

})

Context("when saving to disk", func() {
Expand All @@ -49,7 +47,6 @@ var _ = Describe("Configuration", func() {
})

It("should be possible to save configuration to file", func() {

configuration.Set("G101", map[string]string{
"mode": "strict",
})
Expand All @@ -59,12 +56,10 @@ var _ = Describe("Configuration", func() {
Expect(int(nbytes)).ShouldNot(BeZero())
Expect(err).ShouldNot(HaveOccurred())
Expect(buffer.String()).Should(Equal(`{"G101":{"mode":"strict"},"global":{}}`))

})
})

Context("when configuring rules", func() {

It("should be possible to get configuration for a rule", func() {
settings := map[string]string{
"ciphers": "AES256-GCM",
Expand Down
4 changes: 2 additions & 2 deletions cwe/cwe_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cwe_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestCwe(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions cwe/data.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package cwe

const (
//Acronym is the acronym of CWE
// Acronym is the acronym of CWE
Acronym = "CWE"
//Version the CWE version
// Version the CWE version
Version = "4.4"
//ReleaseDateUtc the release Date of CWE Version
// ReleaseDateUtc the release Date of CWE Version
ReleaseDateUtc = "2021-03-15"
//Organization MITRE
// Organization MITRE
Organization = "MITRE"
//Description the description of CWE
// Description the description of CWE
Description = "The MITRE Common Weakness Enumeration"
)

Expand Down Expand Up @@ -126,7 +126,7 @@ func init() {
}
}

//Get Retrieves a CWE weakness by it's id
// Get Retrieves a CWE weakness by it's id
func Get(id string) *Weakness {
weakness, ok := data[id]
if ok && weakness != nil {
Expand Down
1 change: 0 additions & 1 deletion cwe/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ var _ = Describe("CWE data", func() {
Expect(weakness.Name).ShouldNot(BeNil())
Expect(weakness.Description).ShouldNot(BeNil())
})

})
})
10 changes: 5 additions & 5 deletions cwe/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ type Weakness struct {
Description string
}

//SprintURL format the CWE URL
// SprintURL format the CWE URL
func (w *Weakness) SprintURL() string {
return fmt.Sprintf("https://cwe.mitre.org/data/definitions/%s.html", w.ID)
}

//SprintID format the CWE ID
// SprintID format the CWE ID
func (w *Weakness) SprintID() string {
return fmt.Sprintf("%s-%s", Acronym, w.ID)
}

//MarshalJSON print only id and URL
// MarshalJSON print only id and URL
func (w *Weakness) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
ID string `json:"id"`
Expand All @@ -33,12 +33,12 @@ func (w *Weakness) MarshalJSON() ([]byte, error) {
})
}

//InformationURI link to the published CWE PDF
// InformationURI link to the published CWE PDF
func InformationURI() string {
return fmt.Sprintf("https://cwe.mitre.org/data/published/cwe_v%s.pdf/", Version)
}

//DownloadURI link to the zipped XML of the CWE list
// DownloadURI link to the zipped XML of the CWE list
func DownloadURI() string {
return fmt.Sprintf("https://cwe.mitre.org/data/xml/cwec_v%s.xml.zip", Version)
}
4 changes: 2 additions & 2 deletions gosec_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package gosec_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestGosec(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func GetCallInfo(n ast.Node, ctx *Context) (string, string, error) {
}
}
}

}
}
case *ast.Ident:
Expand Down Expand Up @@ -220,7 +219,6 @@ func GetIdentStringValues(ident *ast.Ident) []string {
}
}
}

}
return values
}
Expand Down Expand Up @@ -298,7 +296,7 @@ func Gopath() []string {
}

// Getenv returns the values of the environment variable, otherwise
//returns the default if variable is not set
// returns the default if variable is not set
func Getenv(key, userDefault string) string {
if val := os.Getenv(key); val != "" {
return val
Expand Down
3 changes: 2 additions & 1 deletion issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/securego/gosec/v2/cwe"
"go/ast"
"go/token"
"os"
"strconv"

"github.com/securego/gosec/v2/cwe"
)

// Score type used by severity and confidence values
Expand Down
Loading

0 comments on commit 1256f16

Please sign in to comment.