Skip to content

Commit 8a478c4

Browse files
committed
Prepare for #164: rename GAS to gosec
1. Rename in a backward compatible way 2. Remove gosec default exclude list because gosec is already disabled by default. 3. Warn about unmatched linter names in //nolint directives 4. Process linter names in //nolint directives in upper case 5. Disable gosec for golangci-lint in .golangci.yml
1 parent 47440bc commit 8a478c4

File tree

29 files changed

+361
-107
lines changed

29 files changed

+361
-107
lines changed

.golangci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ linters:
2525
enable-all: true
2626
disable:
2727
- maligned
28-
- prealloc
28+
- prealloc
29+
- gosec

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ assets:
1414
readme:
1515
go run ./scripts/gen_readme/main.go
1616

17+
gen:
18+
go generate ./...
19+
1720
check_generated:
1821
make readme && git diff --exit-code # check no changes
1922

README.tmpl.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ We compare golangci-lint and gometalinter in default mode, but explicitly enable
166166
$ golangci-lint run --no-config --issues-exit-code=0 --deadline=30m \
167167
--disable-all --enable=deadcode --enable=gocyclo --enable=golint --enable=varcheck \
168168
--enable=structcheck --enable=maligned --enable=errcheck --enable=dupl --enable=ineffassign \
169-
--enable=interfacer --enable=unconvert --enable=goconst --enable=gas --enable=megacheck
169+
--enable=interfacer --enable=unconvert --enable=goconst --enable=gosec --enable=megacheck
170170
$ gometalinter --deadline=30m --vendor --cyclo-over=30 --dupl-threshold=150 \
171171
--exclude=<defaul golangci-lint excludes> --skip=testdata --skip=builtin \
172172
--disable-all --enable=deadcode --enable=gocyclo --enable=golint --enable=varcheck \
173173
--enable=structcheck --enable=maligned --enable=errcheck --enable=dupl --enable=ineffassign \
174-
--enable=interfacer --enable=unconvert --enable=goconst --enable=gas --enable=megacheck
174+
--enable=interfacer --enable=unconvert --enable=goconst --enable=gosec --enable=megacheck
175175
./...
176176
```
177177

pkg/commands/help.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ func (e *Executor) initHelp() {
3333

3434
func printLinterConfigs(lcs []linter.Config) {
3535
for _, lc := range lcs {
36-
fmt.Fprintf(logutils.StdOut, "%s: %s [fast: %t]\n", color.YellowString(lc.Linter.Name()),
37-
lc.Linter.Desc(), !lc.DoesFullImport)
36+
altNamesStr := ""
37+
if len(lc.AlternativeNames) != 0 {
38+
altNamesStr = fmt.Sprintf(" (%s)", strings.Join(lc.AlternativeNames, ", "))
39+
}
40+
fmt.Fprintf(logutils.StdOut, "%s%s: %s [fast: %t]\n", color.YellowString(lc.Name()),
41+
altNamesStr, lc.Linter.Desc(), !lc.DoesFullImport)
3842
}
3943
}
4044

@@ -58,7 +62,7 @@ func (e Executor) executeLintersHelp(cmd *cobra.Command, args []string) {
5862
linters := e.DBManager.GetAllLinterConfigsForPreset(p)
5963
linterNames := []string{}
6064
for _, lc := range linters {
61-
linterNames = append(linterNames, lc.Linter.Name())
65+
linterNames = append(linterNames, lc.Name())
6266
}
6367
fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", "))
6468
}

pkg/commands/linters.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func (e *Executor) initLinters() {
2020
}
2121

2222
func IsLinterInConfigsList(name string, linters []linter.Config) bool {
23-
for _, linter := range linters {
24-
if linter.Linter.Name() == name {
23+
for _, lc := range linters {
24+
if lc.Name() == name {
2525
return true
2626
}
2727
}
@@ -40,7 +40,7 @@ func (e *Executor) executeLinters(cmd *cobra.Command, args []string) {
4040

4141
var disabledLCs []linter.Config
4242
for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() {
43-
if !IsLinterInConfigsList(lc.Linter.Name(), enabledLCs) {
43+
if !IsLinterInConfigsList(lc.Name(), enabledLCs) {
4444
disabledLCs = append(disabledLCs, lc)
4545
}
4646
}

pkg/commands/run.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,23 +238,23 @@ func fixSlicesFlags(fs *pflag.FlagSet) {
238238
func (e *Executor) runAnalysis(ctx context.Context, args []string) (<-chan result.Issue, error) {
239239
e.cfg.Run.Args = args
240240

241-
linters, err := e.EnabledLintersSet.Get()
241+
enabledLinters, err := e.EnabledLintersSet.Get()
242242
if err != nil {
243243
return nil, err
244244
}
245245

246246
for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() {
247247
isEnabled := false
248-
for _, linter := range linters {
249-
if linter.Linter.Name() == lc.Linter.Name() {
248+
for _, enabledLC := range enabledLinters {
249+
if enabledLC.Name() == lc.Name() {
250250
isEnabled = true
251251
break
252252
}
253253
}
254-
e.reportData.AddLinter(lc.Linter.Name(), isEnabled, lc.EnabledByDefault)
254+
e.reportData.AddLinter(lc.Name(), isEnabled, lc.EnabledByDefault)
255255
}
256256

257-
lintCtx, err := lint.LoadContext(linters, e.cfg, e.log.Child("load"))
257+
lintCtx, err := lint.LoadContext(enabledLinters, e.cfg, e.log.Child("load"))
258258
if err != nil {
259259
return nil, errors.Wrap(err, "context loading failed")
260260
}
@@ -264,7 +264,7 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) (<-chan resul
264264
return nil, err
265265
}
266266

267-
return runner.Run(ctx, linters, lintCtx), nil
267+
return runner.Run(ctx, enabledLinters, lintCtx), nil
268268
}
269269

270270
func (e *Executor) setOutputToDevNull() (savedStdout, savedStderr *os.File) {

pkg/config/config.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,6 @@ var DefaultExcludePatterns = []ExcludePattern{
4444
Linter: "golint",
4545
Why: "False positive when tests are defined in package 'test'",
4646
},
47-
{
48-
Pattern: "Use of unsafe calls should be audited",
49-
Linter: "gas",
50-
Why: "Too many false-positives on 'unsafe' usage",
51-
},
52-
{
53-
Pattern: "Subprocess launch(ed with variable|ing should be audited)",
54-
Linter: "gas",
55-
Why: "Too many false-positives for parametrized shell calls",
56-
},
57-
{
58-
Pattern: "G104",
59-
Linter: "gas",
60-
Why: "Duplicated errcheck checks",
61-
},
62-
{
63-
Pattern: "(Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)",
64-
Linter: "gas",
65-
Why: "Too many issues in popular repos",
66-
},
67-
{
68-
Pattern: "Potential file inclusion via variable",
69-
Linter: "gas",
70-
Why: "False positive is triggered by 'src, err := ioutil.ReadFile(filename)'",
71-
},
7247
{
7348
Pattern: "(possible misuse of unsafe.Pointer|should have signature)",
7449
Linter: "govet",

pkg/golinters/gas.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ import (
1414
"github.com/golangci/golangci-lint/pkg/result"
1515
)
1616

17-
type Gas struct{}
17+
type Gosec struct{}
1818

19-
func (Gas) Name() string {
20-
return "gas"
19+
func (Gosec) Name() string {
20+
return "gosec"
2121
}
2222

23-
func (Gas) Desc() string {
23+
func (Gosec) Desc() string {
2424
return "Inspects source code for security problems"
2525
}
2626

27-
func (lint Gas) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
27+
func (lint Gosec) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
2828
gasConfig := gas.NewConfig()
2929
enabledRules := rules.Generate()
3030
logger := log.New(ioutil.Discard, "", 0)
@@ -45,7 +45,7 @@ func (lint Gas) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issu
4545
if err != nil {
4646
r = &result.Range{}
4747
if n, rerr := fmt.Sscanf(i.Line, "%d-%d", &r.From, &r.To); rerr != nil || n != 2 {
48-
lintCtx.Log.Warnf("Can't convert gas line number %q of %v to int: %s", i.Line, i, err)
48+
lintCtx.Log.Warnf("Can't convert gosec line number %q of %v to int: %s", i.Line, i, err)
4949
continue
5050
}
5151
line = r.From

pkg/golinters/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var replacePatterns = []replacePattern{
5656
{`^(\S+) arg list ends with redundant newline$`, "`${1}` arg list ends with redundant newline"},
5757
{`^(\S+) composite literal uses unkeyed fields$`, "`${1}` composite literal uses unkeyed fields"},
5858

59-
// gas
59+
// gosec
6060
{`^Blacklisted import (\S+): weak cryptographic primitive$`,
6161
"Blacklisted import `${1}`: weak cryptographic primitive"},
6262
{`^TLS InsecureSkipVerify set true.$`, "TLS `InsecureSkipVerify` set true."},

pkg/lint/linter/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Config struct {
1616
NeedsSSARepr bool
1717
InPresets []string
1818
Speed int // more value means faster execution of linter
19+
AlternativeNames []string
1920

2021
OriginalURL string // URL of original (not forked) repo, needed for autogenerated README
2122
}
@@ -46,6 +47,11 @@ func (lc Config) WithURL(url string) Config {
4647
return lc
4748
}
4849

50+
func (lc Config) WithAlternativeNames(names ...string) Config {
51+
lc.AlternativeNames = names
52+
return lc
53+
}
54+
4955
func (lc Config) NeedsProgramLoading() bool {
5056
return lc.DoesFullImport
5157
}
@@ -58,6 +64,14 @@ func (lc Config) GetSpeed() int {
5864
return lc.Speed
5965
}
6066

67+
func (lc Config) AllNames() []string {
68+
return append([]string{lc.Name()}, lc.AlternativeNames...)
69+
}
70+
71+
func (lc Config) Name() string {
72+
return lc.Linter.Name()
73+
}
74+
6175
func NewConfig(linter Linter) *Config {
6276
return &Config{
6377
Linter: linter,

pkg/lint/lintersdb/enabled_set.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (es EnabledSet) build(lcfg *config.Linters, enabledByDefaultLinters []linte
4343
for _, p := range lcfg.Presets {
4444
for _, lc := range es.m.GetAllLinterConfigsForPreset(p) {
4545
lc := lc
46-
resultLintersSet[lc.Linter.Name()] = &lc
46+
resultLintersSet[lc.Name()] = &lc
4747
}
4848
}
4949

@@ -52,14 +52,16 @@ func (es EnabledSet) build(lcfg *config.Linters, enabledByDefaultLinters []linte
5252
// It should be before --enable and --disable to be able to enable or disable specific linter.
5353
if lcfg.Fast {
5454
for name := range resultLintersSet {
55-
if es.m.getLinterConfig(name).DoesFullImport {
55+
if es.m.GetLinterConfig(name).DoesFullImport {
5656
delete(resultLintersSet, name)
5757
}
5858
}
5959
}
6060

6161
for _, name := range lcfg.Enable {
62-
resultLintersSet[name] = es.m.getLinterConfig(name)
62+
lc := es.m.GetLinterConfig(name)
63+
// it's important to use lc.Name() nor name because name can be alias
64+
resultLintersSet[lc.Name()] = lc
6365
}
6466

6567
for _, name := range lcfg.Disable {
@@ -68,7 +70,10 @@ func (es EnabledSet) build(lcfg *config.Linters, enabledByDefaultLinters []linte
6870
delete(resultLintersSet, ln)
6971
}
7072
}
71-
delete(resultLintersSet, name)
73+
74+
lc := es.m.GetLinterConfig(name)
75+
// it's important to use lc.Name() nor name because name can be alias
76+
delete(resultLintersSet, lc.Name())
7277
}
7378

7479
es.optimizeLintersSet(resultLintersSet)
@@ -111,7 +116,7 @@ func (es EnabledSet) optimizeLintersSet(linters map[string]*linter.Config) {
111116
delete(linters, n)
112117
}
113118

114-
lc := *es.m.getLinterConfig("megacheck")
119+
lc := *es.m.GetLinterConfig("megacheck")
115120
lc.Linter = mega
116121
linters[mega.Name()] = &lc
117122
}
@@ -135,7 +140,7 @@ func (es EnabledSet) Get() ([]linter.Config, error) {
135140
func (es EnabledSet) verbosePrintLintersStatus(lcs []linter.Config) {
136141
var linterNames []string
137142
for _, lc := range lcs {
138-
linterNames = append(linterNames, lc.Linter.Name())
143+
linterNames = append(linterNames, lc.Name())
139144
}
140145
sort.StringSlice(linterNames).Sort()
141146
es.log.Infof("Active %d linters: %s", len(linterNames), linterNames)

pkg/lint/lintersdb/enabled_set_test.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,41 @@ func TestGetEnabledLintersSet(t *testing.T) {
4242
def: []string{"gofmt", "govet"},
4343
exp: []string{"gofmt", "govet"},
4444
},
45+
{
46+
name: "enable gosec by gas alias",
47+
cfg: config.Linters{
48+
Enable: []string{"gas"},
49+
},
50+
exp: []string{"gosec"},
51+
},
52+
{
53+
name: "enable gosec by primary name",
54+
cfg: config.Linters{
55+
Enable: []string{"gosec"},
56+
},
57+
exp: []string{"gosec"},
58+
},
59+
{
60+
name: "enable gosec by both names",
61+
cfg: config.Linters{
62+
Enable: []string{"gosec", "gas"},
63+
},
64+
exp: []string{"gosec"},
65+
},
66+
{
67+
name: "disable gosec by gas alias",
68+
cfg: config.Linters{
69+
Disable: []string{"gas"},
70+
},
71+
def: []string{"gosec"},
72+
},
73+
{
74+
name: "disable gosec by primary name",
75+
cfg: config.Linters{
76+
Disable: []string{"gosec"},
77+
},
78+
def: []string{"gosec"},
79+
},
4580
}
4681

4782
m := NewManager()
@@ -50,12 +85,12 @@ func TestGetEnabledLintersSet(t *testing.T) {
5085
t.Run(c.name, func(t *testing.T) {
5186
defaultLinters := []linter.Config{}
5287
for _, ln := range c.def {
53-
defaultLinters = append(defaultLinters, *m.getLinterConfig(ln))
88+
defaultLinters = append(defaultLinters, *m.GetLinterConfig(ln))
5489
}
5590
els := es.build(&c.cfg, defaultLinters)
5691
var enabledLinters []string
5792
for ln, lc := range els {
58-
assert.Equal(t, ln, lc.Linter.Name())
93+
assert.Equal(t, ln, lc.Name())
5994
enabledLinters = append(enabledLinters, ln)
6095
}
6196

pkg/lint/lintersdb/manager.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ func NewManager() *Manager {
1515
m := &Manager{}
1616
nameToLC := make(map[string]linter.Config)
1717
for _, lc := range m.GetAllSupportedLinterConfigs() {
18-
nameToLC[lc.Linter.Name()] = lc
18+
for _, name := range lc.AllNames() {
19+
nameToLC[name] = lc
20+
}
1921
}
2022

2123
m.nameToLC = nameToLC
@@ -35,7 +37,7 @@ func (m Manager) allPresetsSet() map[string]bool {
3537
return ret
3638
}
3739

38-
func (m Manager) getLinterConfig(name string) *linter.Config {
40+
func (m Manager) GetLinterConfig(name string) *linter.Config {
3941
lc, ok := m.nameToLC[name]
4042
if !ok {
4143
return nil
@@ -87,11 +89,12 @@ func (Manager) GetAllSupportedLinterConfigs() []linter.Config {
8789
WithSpeed(5).
8890
WithURL("https://github.com/dominikh/go-tools/tree/master/cmd/gosimple"),
8991

90-
linter.NewConfig(golinters.Gas{}).
92+
linter.NewConfig(golinters.Gosec{}).
9193
WithFullImport().
9294
WithPresets(linter.PresetBugs).
9395
WithSpeed(8).
94-
WithURL("https://github.com/GoASTScanner/gas"),
96+
WithURL("https://github.com/securego/gosec").
97+
WithAlternativeNames("gas"),
9598
linter.NewConfig(golinters.Structcheck{}).
9699
WithFullImport().
97100
WithPresets(linter.PresetUnused).
@@ -202,7 +205,7 @@ func (Manager) GetAllSupportedLinterConfigs() []linter.Config {
202205
golinters.TypeCheck{}.Name(): isLocalRun,
203206
}
204207
return enableLinterConfigs(lcs, func(lc *linter.Config) bool {
205-
return enabled[lc.Linter.Name()]
208+
return enabled[lc.Name()]
206209
})
207210
}
208211

@@ -221,7 +224,7 @@ func linterConfigsToMap(lcs []linter.Config) map[string]*linter.Config {
221224
ret := map[string]*linter.Config{}
222225
for _, lc := range lcs {
223226
lc := lc // local copy
224-
ret[lc.Linter.Name()] = &lc
227+
ret[lc.Name()] = &lc
225228
}
226229

227230
return ret

0 commit comments

Comments
 (0)