Skip to content

Commit

Permalink
enable linter "revive" (#2978)
Browse files Browse the repository at this point in the history
* enable linter "revive"

* enable some revive checks

* pointer receiver for crzLogger

* fix "range-loop variables always have the same address"

* lint (whitespace)
  • Loading branch information
mmetc authored May 2, 2024
1 parent 529d3b2 commit 3392e5e
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 19 deletions.
135 changes: 134 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,118 @@ linters-settings:
- pkg: "gopkg.in/yaml.v2"
desc: "yaml.v2 is deprecated for new code in favor of yaml.v3"

revive:
ignore-generated-header: true
severity: error
enable-all-rules: true
rules:
- name: add-constant
disabled: true
- name: argument-limit
disabled: true
- name: bare-return
disabled: true
- name: blank-imports
disabled: true
- name: bool-literal-in-expr
disabled: true
- name: cognitive-complexity
disabled: true
- name: comment-spacings
disabled: true
- name: confusing-results
disabled: true
- name: context-as-argument
disabled: true
- name: cyclomatic
disabled: true
- name: deep-exit
disabled: true
- name: defer
disabled: true
- name: duplicated-imports
disabled: true
- name: early-return
disabled: true
- name: empty-block
disabled: true
- name: empty-lines
disabled: true
- name: error-naming
disabled: true
- name: error-strings
disabled: true
- name: flag-parameter
disabled: true
- name: function-result-limit
disabled: true
- name: function-length
disabled: true
- name: get-return
disabled: true
- name: if-return
disabled: true
- name: increment-decrement
disabled: true
- name: indent-error-flow
disabled: true
- name: import-alias-naming
disabled: true
- name: import-shadowing
disabled: true
- name: line-length-limit
disabled: true
- name: max-control-nesting
disabled: true
- name: max-public-structs
disabled: true
- name: modifies-parameter
disabled: true
- name: optimize-operands-order
disabled: true
- name: nested-structs
disabled: true
- name: package-comments
disabled: true
- name: redundant-import-alias
disabled: true
- name: struct-tag
disabled: true
- name: superfluous-else
disabled: true
- name: time-equal
disabled: true
- name: var-naming
disabled: true
- name: var-declaration
disabled: true
- name: unchecked-type-assertion
disabled: true
- name: exported
disabled: true
- name: unexported-naming
disabled: true
- name: unexported-return
disabled: true
- name: unhandled-error
disabled: true
arguments:
- "fmt.Print"
- "fmt.Printf"
- "fmt.Println"
- name: unnecessary-stmt
disabled: true
- name: unreachable-code
disabled: true
- name: unused-parameter
disabled: true
- name: unused-receiver
disabled: true
- name: use-any
disabled: true
- name: useless-break
disabled: true

wsl:
# Allow blocks to end with comments
allow-trailing-comment: true
Expand Down Expand Up @@ -191,6 +303,7 @@ linters:
# - perfsprint # Checks that fmt.Sprintf can be replaced with a faster alternative.
# - predeclared # find code that shadows one of Go's predeclared identifiers
# - reassign # Checks that package variables are not reassigned
# - revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.
# - rowserrcheck # checks whether Rows.Err of rows is checked successfully
# - sloglint # ensure consistent code style when using log/slog
# - spancheck # Checks for mistakes with OpenTelemetry/Census spans.
Expand Down Expand Up @@ -222,7 +335,6 @@ linters:
- musttag # enforce field tags in (un)marshaled structs
- promlinter # Check Prometheus metrics naming via promlint
- protogetter # Reports direct reads from proto message fields when getters should be used
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.
- tagalign # check that struct tags are well aligned
- thelper # thelper detects tests helpers which is not start with t.Helper() method.
- wrapcheck # Checks that errors returned from external packages are wrapped
Expand Down Expand Up @@ -373,3 +485,24 @@ issues:
- linters:
- nonamedreturns
text: "named return .* with type .* found"

- linters:
- revive
path: pkg/leakybucket/manager_load.go
text: "confusing-naming: Field '.*' differs only by capitalization to other field in the struct type BucketFactory"

- linters:
- revive
path: pkg/exprhelpers/helpers.go
text: "confusing-naming: Method 'flatten' differs only by capitalization to function 'Flatten' in the same source file"

- linters:
- revive
path: pkg/appsec/query_utils.go
text: "confusing-naming: Method 'parseQuery' differs only by capitalization to function 'ParseQuery' in the same source file"

- linters:
- revive
path: pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go
text: "confusing-naming: Method 'QueryRange' differs only by capitalization to method 'queryRange' in the same source file"

5 changes: 3 additions & 2 deletions cmd/crowdsec/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ func dedupAlerts(alerts []types.RuntimeAlert) ([]*models.Alert, error) {
continue
}

for k, src := range alert.Sources {
for k := range alert.Sources {
refsrc := *alert.Alert // copy

log.Tracef("source[%s]", k)

src := alert.Sources[k]
refsrc.Source = &src
dedupCache = append(dedupCache, &refsrc)
}
Expand All @@ -45,8 +46,8 @@ func dedupAlerts(alerts []types.RuntimeAlert) ([]*models.Alert, error) {

func PushAlerts(alerts []types.RuntimeAlert, client *apiclient.ApiClient) error {
ctx := context.Background()
alertsToPush, err := dedupAlerts(alerts)

alertsToPush, err := dedupAlerts(alerts)
if err != nil {
return fmt.Errorf("failed to transform alerts for api: %w", err)
}
Expand Down
56 changes: 40 additions & 16 deletions pkg/appsec/coraza_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"io"

dbg "github.com/crowdsecurity/coraza/v3/debuglog"
log "github.com/sirupsen/logrus"

dbg "github.com/crowdsecurity/coraza/v3/debuglog"
)

var DebugRules map[int]bool = map[int]bool{}
Expand All @@ -18,6 +19,7 @@ func GetRuleDebug(id int) bool {
if val, ok := DebugRules[id]; ok {
return val
}

return false
}

Expand Down Expand Up @@ -60,29 +62,35 @@ func (e *crzLogEvent) Str(key, val string) dbg.Event {
if e.muted {
return e
}

e.fields[key] = val

return e
}

func (e *crzLogEvent) Err(err error) dbg.Event {
if e.muted {
return e
}

e.fields["error"] = err

return e
}

func (e *crzLogEvent) Bool(key string, b bool) dbg.Event {
if e.muted {
return e
}

e.fields[key] = b

return e
}

func (e *crzLogEvent) Int(key string, i int) dbg.Event {
if e.muted {
//this allows us to have per-rule debug logging
// this allows us to have per-rule debug logging
if key == "rule_id" && GetRuleDebug(i) {
e.muted = false
e.fields = map[string]interface{}{}
Expand All @@ -91,23 +99,29 @@ func (e *crzLogEvent) Int(key string, i int) dbg.Event {
return e
}
}

e.fields[key] = i

return e
}

func (e *crzLogEvent) Uint(key string, i uint) dbg.Event {
if e.muted {
return e
}

e.fields[key] = i

return e
}

func (e *crzLogEvent) Stringer(key string, val fmt.Stringer) dbg.Event {
if e.muted {
return e
}

e.fields[key] = val

return e
}

Expand All @@ -121,74 +135,84 @@ type crzLogger struct {
logLevel log.Level
}

func NewCrzLogger(logger *log.Entry) crzLogger {
return crzLogger{logger: logger, logLevel: logger.Logger.GetLevel()}
func NewCrzLogger(logger *log.Entry) *crzLogger {
return &crzLogger{logger: logger, logLevel: logger.Logger.GetLevel()}
}

func (c crzLogger) NewMutedEvt(lvl log.Level) dbg.Event {
func (c *crzLogger) NewMutedEvt(lvl log.Level) dbg.Event {
return &crzLogEvent{muted: true, logger: c.logger, level: lvl}
}
func (c crzLogger) NewEvt(lvl log.Level) dbg.Event {

func (c *crzLogger) NewEvt(lvl log.Level) dbg.Event {
evt := &crzLogEvent{fields: map[string]interface{}{}, logger: c.logger, level: lvl}

if c.defaultFields != nil {
for k, v := range c.defaultFields {
evt.fields[k] = v
}
}

return evt
}

func (c crzLogger) WithOutput(w io.Writer) dbg.Logger {
func (c *crzLogger) WithOutput(w io.Writer) dbg.Logger {
return c
}

func (c crzLogger) WithLevel(lvl dbg.Level) dbg.Logger {
func (c *crzLogger) WithLevel(lvl dbg.Level) dbg.Logger {
c.logLevel = log.Level(lvl)
c.logger.Logger.SetLevel(c.logLevel)

return c
}

func (c crzLogger) With(fs ...dbg.ContextField) dbg.Logger {
var e dbg.Event = c.NewEvt(c.logLevel)
func (c *crzLogger) With(fs ...dbg.ContextField) dbg.Logger {
e := c.NewEvt(c.logLevel)
for _, f := range fs {
e = f(e)
}

c.defaultFields = e.(*crzLogEvent).fields

return c
}

func (c crzLogger) Trace() dbg.Event {
func (c *crzLogger) Trace() dbg.Event {
if c.logLevel < log.TraceLevel {
return c.NewMutedEvt(log.TraceLevel)
}

return c.NewEvt(log.TraceLevel)
}

func (c crzLogger) Debug() dbg.Event {
func (c *crzLogger) Debug() dbg.Event {
if c.logLevel < log.DebugLevel {
return c.NewMutedEvt(log.DebugLevel)

}

return c.NewEvt(log.DebugLevel)
}

func (c crzLogger) Info() dbg.Event {
func (c *crzLogger) Info() dbg.Event {
if c.logLevel < log.InfoLevel {
return c.NewMutedEvt(log.InfoLevel)
}

return c.NewEvt(log.InfoLevel)
}

func (c crzLogger) Warn() dbg.Event {
func (c *crzLogger) Warn() dbg.Event {
if c.logLevel < log.WarnLevel {
return c.NewMutedEvt(log.WarnLevel)
}

return c.NewEvt(log.WarnLevel)
}

func (c crzLogger) Error() dbg.Event {
func (c *crzLogger) Error() dbg.Event {
if c.logLevel < log.ErrorLevel {
return c.NewMutedEvt(log.ErrorLevel)
}

return c.NewEvt(log.ErrorLevel)
}

0 comments on commit 3392e5e

Please sign in to comment.