Skip to content

Commit

Permalink
enable linter: revive (early-return)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jun 5, 2024
1 parent 9e859c0 commit 790d4ac
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 59 deletions.
4 changes: 1 addition & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ linters-settings:
- "!**/pkg/csplugin/broker.go"
- "!**/pkg/leakybucket/buckets_test.go"
- "!**/pkg/leakybucket/manager_load.go"
- "!**/pkg/metabase/metabase.go"
- "!**/pkg/parser/node.go"
- "!**/pkg/parser/node_test.go"
- "!**/pkg/parser/parsing_test.go"
Expand Down Expand Up @@ -132,8 +131,6 @@ linters-settings:
disabled: true
- name: defer
disabled: true
- name: early-return
disabled: true
- name: empty-block
disabled: true
- name: empty-lines
Expand Down Expand Up @@ -375,6 +372,7 @@ issues:

exclude-dirs:
- pkg/time/rate
- pkg/metabase

exclude-files:
- pkg/yamlpatch/merge.go
Expand Down
15 changes: 7 additions & 8 deletions pkg/acquisition/modules/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,15 @@ func (s *S3Source) listPoll() error {
continue
}
for i := len(bucketObjects) - 1; i >= 0; i-- {
if bucketObjects[i].LastModified.After(lastObjectDate) {
newObject = true
logger.Debugf("Found new object %s", *bucketObjects[i].Key)
s.readerChan <- S3Object{
Bucket: s.Config.BucketName,
Key: *bucketObjects[i].Key,
}
} else {
if !bucketObjects[i].LastModified.After(lastObjectDate) {
break
}
newObject = true
logger.Debugf("Found new object %s", *bucketObjects[i].Key)
s.readerChan <- S3Object{
Bucket: s.Config.BucketName,
Key: *bucketObjects[i].Key,
}
}
if newObject {
lastObjectDate = *bucketObjects[len(bucketObjects)-1].LastModified
Expand Down
17 changes: 9 additions & 8 deletions pkg/apiserver/papi.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,18 @@ func (p *Papi) handleEvent(event longpollclient.Event, sync bool) error {
return errors.New("no source user in header message, skipping")
}

if operationFunc, ok := operationMap[message.Header.OperationType]; ok {
logger.Debugf("Calling operation '%s'", message.Header.OperationType)

err := operationFunc(message, p, sync)
if err != nil {
return fmt.Errorf("'%s %s failed: %w", message.Header.OperationType, message.Header.OperationCmd, err)
}
} else {
operationFunc, ok := operationMap[message.Header.OperationType]
if !ok {

Check warning on line 145 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L144-L145

Added lines #L144 - L145 were not covered by tests
return fmt.Errorf("operation '%s' unknown, continue", message.Header.OperationType)
}

logger.Debugf("Calling operation '%s'", message.Header.OperationType)

err := operationFunc(message, p, sync)
if err != nil {
return fmt.Errorf("'%s %s failed: %w", message.Header.OperationType, message.Header.OperationCmd, err)
}

Check warning on line 154 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L149-L154

Added lines #L149 - L154 were not covered by tests

return nil
}

Expand Down
26 changes: 13 additions & 13 deletions pkg/appsec/appsec_rule/modsecurity.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ func (m *ModsecurityRule) buildRules(rule *CustomRule, appsecRuleName string, an
r.WriteByte(' ')

if rule.Match.Type != "" {
if match, ok := matchMap[rule.Match.Type]; ok {
prefix := ""
if rule.Match.Not {
prefix = "!"
}
r.WriteString(fmt.Sprintf(`"%s%s %s"`, prefix, match, rule.Match.Value))
} else {
match, ok := matchMap[rule.Match.Type]
if !ok {
return nil, fmt.Errorf("unknown match type '%s'", rule.Match.Type)
}
prefix := ""
if rule.Match.Not {
prefix = "!"
}
r.WriteString(fmt.Sprintf(`"%s%s %s"`, prefix, match, rule.Match.Value))
}

//Should phase:2 be configurable?
Expand All @@ -186,20 +186,20 @@ func (m *ModsecurityRule) buildRules(rule *CustomRule, appsecRuleName string, an
continue
}
r.WriteByte(',')
if mappedTransform, ok := transformMap[transform]; ok {
r.WriteString(mappedTransform)
} else {
mappedTransform, ok := transformMap[transform]
if !ok {
return nil, fmt.Errorf("unknown transform '%s'", transform)
}
r.WriteString(mappedTransform)
}
}

if rule.BodyType != "" {
if mappedBodyType, ok := bodyTypeMatch[rule.BodyType]; ok {
r.WriteString(fmt.Sprintf(",ctl:requestBodyProcessor=%s", mappedBodyType))
} else {
mappedBodyType, ok := bodyTypeMatch[rule.BodyType]
if !ok {

Check warning on line 199 in pkg/appsec/appsec_rule/modsecurity.go

View check run for this annotation

Codecov / codecov/patch

pkg/appsec/appsec_rule/modsecurity.go#L198-L199

Added lines #L198 - L199 were not covered by tests
return nil, fmt.Errorf("unknown body type '%s'", rule.BodyType)
}
r.WriteString(fmt.Sprintf(",ctl:requestBodyProcessor=%s", mappedBodyType))

Check warning on line 202 in pkg/appsec/appsec_rule/modsecurity.go

View check run for this annotation

Codecov / codecov/patch

pkg/appsec/appsec_rule/modsecurity.go#L202

Added line #L202 was not covered by tests
}

if and {
Expand Down
11 changes: 5 additions & 6 deletions pkg/appsec/coraza_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,13 @@ func (e *crzLogEvent) Bool(key string, b bool) dbg.Event {

func (e *crzLogEvent) Int(key string, i int) dbg.Event {
if e.muted {
// this allows us to have per-rule debug logging
if key == "rule_id" && GetRuleDebug(i) {
e.muted = false
e.fields = map[string]interface{}{}
e.level = log.DebugLevel
} else {
if key != "rule_id" || !GetRuleDebug(i) {
return e
}
// this allows us to have per-rule debug logging
e.muted = false
e.fields = map[string]interface{}{}
e.level = log.DebugLevel

Check warning on line 99 in pkg/appsec/coraza_logger.go

View check run for this annotation

Codecov / codecov/patch

pkg/appsec/coraza_logger.go#L97-L99

Added lines #L97 - L99 were not covered by tests
}

e.fields[key] = i
Expand Down
5 changes: 2 additions & 3 deletions pkg/csplugin/hclog_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,10 @@ func merge(dst map[string]interface{}, k, v interface{}) {
func safeString(str fmt.Stringer) (s string) {
defer func() {
if panicVal := recover(); panicVal != nil {
if v := reflect.ValueOf(str); v.Kind() == reflect.Ptr && v.IsNil() {
s = "NULL"
} else {
if v := reflect.ValueOf(str); v.Kind() != reflect.Ptr || !v.IsNil() {

Check warning on line 224 in pkg/csplugin/hclog_adapter.go

View check run for this annotation

Codecov / codecov/patch

pkg/csplugin/hclog_adapter.go#L224

Added line #L224 was not covered by tests
panic(panicVal)
}
s = "NULL"

Check warning on line 227 in pkg/csplugin/hclog_adapter.go

View check run for this annotation

Codecov / codecov/patch

pkg/csplugin/hclog_adapter.go#L227

Added line #L227 was not covered by tests
}
}()

Expand Down
7 changes: 3 additions & 4 deletions pkg/exprhelpers/crowdsec_cti.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ func CrowdsecCTI(params ...any) (any, error) {
if val, err := CTICache.Get(ip); err == nil && val != nil {
ctiClient.Logger.Debugf("cti cache fetch for %s", ip)
ret, ok := val.(*cticlient.SmokeItem)
if !ok {
ctiClient.Logger.Warningf("CrowdsecCTI: invalid type in cache, removing")
CTICache.Remove(ip)
} else {
if ok {
return ret, nil
}
ctiClient.Logger.Warningf("CrowdsecCTI: invalid type in cache, removing")
CTICache.Remove(ip)

Check warning on line 93 in pkg/exprhelpers/crowdsec_cti.go

View check run for this annotation

Codecov / codecov/patch

pkg/exprhelpers/crowdsec_cti.go#L92-L93

Added lines #L92 - L93 were not covered by tests
}

if !CTIBackOffUntil.IsZero() && time.Now().Before(CTIBackOffUntil) {
Expand Down
27 changes: 13 additions & 14 deletions pkg/parser/parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,26 +278,25 @@ func matchEvent(expected types.Event, out types.Event, debug bool) ([]string, bo

for mapIdx := 0; mapIdx < len(expectMaps); mapIdx++ {
for expKey, expVal := range expectMaps[mapIdx] {
if outVal, ok := outMaps[mapIdx][expKey]; ok {
if outVal == expVal { //ok entry
if debug {
retInfo = append(retInfo, fmt.Sprintf("ok %s[%s] %s == %s", outLabels[mapIdx], expKey, expVal, outVal))
}
valid = true
} else { //mismatch entry
if debug {
retInfo = append(retInfo, fmt.Sprintf("mismatch %s[%s] %s != %s", outLabels[mapIdx], expKey, expVal, outVal))
}
valid = false
goto checkFinished
}
} else { //missing entry
outVal, ok := outMaps[mapIdx][expKey]
if !ok {
if debug {
retInfo = append(retInfo, fmt.Sprintf("missing entry %s[%s]", outLabels[mapIdx], expKey))
}
valid = false
goto checkFinished
}
if outVal != expVal { //ok entry
if debug {
retInfo = append(retInfo, fmt.Sprintf("mismatch %s[%s] %s != %s", outLabels[mapIdx], expKey, expVal, outVal))
}
valid = false
goto checkFinished
}
if debug {
retInfo = append(retInfo, fmt.Sprintf("ok %s[%s] %s == %s", outLabels[mapIdx], expKey, expVal, outVal))
}
valid = true
}
}
checkFinished:
Expand Down

0 comments on commit 790d4ac

Please sign in to comment.