Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable linter: revive (indent-error-flow) #3068

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ linters-settings:
- "!**/pkg/database/*.go"
- "!**/pkg/exprhelpers/*.go"
- "!**/pkg/acquisition/modules/appsec/appsec.go"
- "!**/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go"
- "!**/pkg/apiserver/controllers/v1/errors.go"
yaml:
files:
Expand Down Expand Up @@ -147,8 +146,6 @@ linters-settings:
disabled: true
- name: increment-decrement
disabled: true
- name: indent-error-flow
disabled: true
- name: import-alias-naming
disabled: true
- name: import-shadowing
Expand Down
4 changes: 2 additions & 2 deletions pkg/acquisition/modules/appsec/rx_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
tx.CaptureField(i, c)
}
return true
} else {
return o.re.MatchString(value)
}

return o.re.MatchString(value)

Check warning on line 55 in pkg/acquisition/modules/appsec/rx_operator.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/rx_operator.go#L55

Added line #L55 was not covered by tests
}

// RegisterRX registers the rx operator using a WASI implementation instead of Go.
Expand Down
22 changes: 11 additions & 11 deletions pkg/acquisition/modules/kinesis/kinesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -95,7 +96,7 @@
}

if sess == nil {
return fmt.Errorf("failed to create aws session")
return errors.New("failed to create aws session")

Check warning on line 99 in pkg/acquisition/modules/kinesis/kinesis.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/kinesis/kinesis.go#L99

Added line #L99 was not covered by tests
}
config := aws.NewConfig()
if k.Config.AwsRegion != "" {
Expand All @@ -106,7 +107,7 @@
}
k.kClient = kinesis.New(sess, config)
if k.kClient == nil {
return fmt.Errorf("failed to create kinesis client")
return errors.New("failed to create kinesis client")

Check warning on line 110 in pkg/acquisition/modules/kinesis/kinesis.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/kinesis/kinesis.go#L110

Added line #L110 was not covered by tests
}
return nil
}
Expand All @@ -124,24 +125,24 @@

err := yaml.UnmarshalStrict(yamlConfig, &k.Config)
if err != nil {
return fmt.Errorf("Cannot parse kinesis datasource configuration: %w", err)
return fmt.Errorf("cannot parse kinesis datasource configuration: %w", err)

Check warning on line 128 in pkg/acquisition/modules/kinesis/kinesis.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/kinesis/kinesis.go#L128

Added line #L128 was not covered by tests
}

if k.Config.Mode == "" {
k.Config.Mode = configuration.TAIL_MODE
}

if k.Config.StreamName == "" && !k.Config.UseEnhancedFanOut {
return fmt.Errorf("stream_name is mandatory when use_enhanced_fanout is false")
return errors.New("stream_name is mandatory when use_enhanced_fanout is false")
}
if k.Config.StreamARN == "" && k.Config.UseEnhancedFanOut {
return fmt.Errorf("stream_arn is mandatory when use_enhanced_fanout is true")
return errors.New("stream_arn is mandatory when use_enhanced_fanout is true")
}
if k.Config.ConsumerName == "" && k.Config.UseEnhancedFanOut {
return fmt.Errorf("consumer_name is mandatory when use_enhanced_fanout is true")
return errors.New("consumer_name is mandatory when use_enhanced_fanout is true")
}
if k.Config.StreamARN != "" && k.Config.StreamName != "" {
return fmt.Errorf("stream_arn and stream_name are mutually exclusive")
return errors.New("stream_arn and stream_name are mutually exclusive")
}
if k.Config.MaxRetries <= 0 {
k.Config.MaxRetries = 10
Expand Down Expand Up @@ -169,7 +170,7 @@
}

func (k *KinesisSource) ConfigureByDSN(string, map[string]string, *log.Entry, string) error {
return fmt.Errorf("kinesis datasource does not support command-line acquisition")
return errors.New("kinesis datasource does not support command-line acquisition")

Check warning on line 173 in pkg/acquisition/modules/kinesis/kinesis.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/kinesis/kinesis.go#L173

Added line #L173 was not covered by tests
}

func (k *KinesisSource) GetMode() string {
Expand All @@ -181,7 +182,7 @@
}

func (k *KinesisSource) OneShotAcquisition(out chan types.Event, t *tomb.Tomb) error {
return fmt.Errorf("kinesis datasource does not support one-shot acquisition")
return errors.New("kinesis datasource does not support one-shot acquisition")

Check warning on line 185 in pkg/acquisition/modules/kinesis/kinesis.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/kinesis/kinesis.go#L185

Added line #L185 was not covered by tests
}

func (k *KinesisSource) decodeFromSubscription(record []byte) ([]CloudwatchSubscriptionLogEvent, error) {
Expand Down Expand Up @@ -524,9 +525,8 @@
defer trace.CatchPanic("crowdsec/acquis/kinesis/streaming")
if k.Config.UseEnhancedFanOut {
return k.EnhancedRead(out, t)
} else {
return k.ReadFromStream(out, t)
}
return k.ReadFromStream(out, t)
})
return nil
}
Expand Down
25 changes: 11 additions & 14 deletions pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -13,7 +14,6 @@

"github.com/crowdsecurity/crowdsec/pkg/cwversion"
"github.com/gorilla/websocket"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gopkg.in/tomb.v2"
)
Expand Down Expand Up @@ -120,34 +120,31 @@
resp, err := lc.Get(uri)
if err != nil {
if ok := lc.shouldRetry(); !ok {
return errors.Wrapf(err, "error querying range")
} else {
lc.increaseTicker(ticker)
continue
return fmt.Errorf("error querying range: %w", err)

Check warning on line 123 in pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go#L123

Added line #L123 was not covered by tests
}
lc.increaseTicker(ticker)
continue

Check warning on line 126 in pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go#L125-L126

Added lines #L125 - L126 were not covered by tests
}

if resp.StatusCode != http.StatusOK {
lc.Logger.Warnf("bad HTTP response code for query range: %d", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if ok := lc.shouldRetry(); !ok {
return errors.Wrapf(err, "bad HTTP response code: %d: %s", resp.StatusCode, string(body))
} else {
lc.increaseTicker(ticker)
continue
return fmt.Errorf("bad HTTP response code: %d: %s: %w", resp.StatusCode, string(body), err)

Check warning on line 134 in pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go#L134

Added line #L134 was not covered by tests
}
lc.increaseTicker(ticker)
continue

Check warning on line 137 in pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go#L136-L137

Added lines #L136 - L137 were not covered by tests
}

var lq LokiQueryRangeResponse
if err := json.NewDecoder(resp.Body).Decode(&lq); err != nil {
resp.Body.Close()
if ok := lc.shouldRetry(); !ok {
return errors.Wrapf(err, "error decoding Loki response")
} else {
lc.increaseTicker(ticker)
continue
return fmt.Errorf("error decoding Loki response: %w", err)

Check warning on line 144 in pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go#L144

Added line #L144 was not covered by tests
}
lc.increaseTicker(ticker)
continue

Check warning on line 147 in pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go#L146-L147

Added lines #L146 - L147 were not covered by tests
}
resp.Body.Close()
lc.Logger.Tracef("Got response: %+v", lq)
Expand Down Expand Up @@ -261,7 +258,7 @@

if err != nil {
lc.Logger.Errorf("Error connecting to websocket, err: %s", err)
return responseChan, fmt.Errorf("error connecting to websocket")
return responseChan, errors.New("error connecting to websocket")

Check warning on line 261 in pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go#L261

Added line #L261 was not covered by tests
}

lc.t.Go(func() error {
Expand Down
21 changes: 11 additions & 10 deletions pkg/acquisition/modules/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
if eventBody.Detail.Bucket.Name != "" {
return eventBody.Detail.Bucket.Name, eventBody.Detail.Object.Key, nil
}
return "", "", fmt.Errorf("invalid event body for event bridge format")
return "", "", errors.New("invalid event body for event bridge format")
}

func extractBucketAndPrefixFromS3Notif(message *string) (string, string, error) {
Expand All @@ -286,7 +286,7 @@
return "", "", err
}
if len(s3notifBody.Records) == 0 {
return "", "", fmt.Errorf("no records found in S3 notification")
return "", "", errors.New("no records found in S3 notification")

Check warning on line 289 in pkg/acquisition/modules/s3/s3.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/s3/s3.go#L289

Added line #L289 was not covered by tests
}
if !strings.HasPrefix(s3notifBody.Records[0].EventName, "ObjectCreated:") {
return "", "", fmt.Errorf("event %s is not supported", s3notifBody.Records[0].EventName)
Expand All @@ -295,19 +295,20 @@
}

func (s *S3Source) extractBucketAndPrefix(message *string) (string, string, error) {
if s.Config.SQSFormat == SQSFormatEventBridge {
switch s.Config.SQSFormat {
case SQSFormatEventBridge:

Check warning on line 299 in pkg/acquisition/modules/s3/s3.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/s3/s3.go#L299

Added line #L299 was not covered by tests
bucket, key, err := extractBucketAndPrefixFromEventBridge(message)
if err != nil {
return "", "", err
}
return bucket, key, nil
} else if s.Config.SQSFormat == SQSFormatS3Notification {
case SQSFormatS3Notification:

Check warning on line 305 in pkg/acquisition/modules/s3/s3.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/s3/s3.go#L305

Added line #L305 was not covered by tests
bucket, key, err := extractBucketAndPrefixFromS3Notif(message)
if err != nil {
return "", "", err
}
return bucket, key, nil
} else {
default:
bucket, key, err := extractBucketAndPrefixFromEventBridge(message)
if err == nil {
s.Config.SQSFormat = SQSFormatEventBridge
Expand All @@ -318,7 +319,7 @@
s.Config.SQSFormat = SQSFormatS3Notification
return bucket, key, nil
}
return "", "", fmt.Errorf("SQS message format not supported")
return "", "", errors.New("SQS message format not supported")

Check warning on line 322 in pkg/acquisition/modules/s3/s3.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/s3/s3.go#L322

Added line #L322 was not covered by tests
}
}

Expand Down Expand Up @@ -496,15 +497,15 @@
}

if s.Config.BucketName != "" && s.Config.SQSName != "" {
return fmt.Errorf("bucket_name and sqs_name are mutually exclusive")
return errors.New("bucket_name and sqs_name are mutually exclusive")
}

if s.Config.PollingMethod == PollMethodSQS && s.Config.SQSName == "" {
return fmt.Errorf("sqs_name is required when using sqs polling method")
return errors.New("sqs_name is required when using sqs polling method")
}

if s.Config.BucketName == "" && s.Config.PollingMethod == PollMethodList {
return fmt.Errorf("bucket_name is required")
return errors.New("bucket_name is required")
}

if s.Config.SQSFormat != "" && s.Config.SQSFormat != SQSFormatEventBridge && s.Config.SQSFormat != SQSFormatS3Notification {
Expand Down Expand Up @@ -567,7 +568,7 @@
dsn = strings.TrimPrefix(dsn, "s3://")
args := strings.Split(dsn, "?")
if len(args[0]) == 0 {
return fmt.Errorf("empty s3:// DSN")
return errors.New("empty s3:// DSN")

Check warning on line 571 in pkg/acquisition/modules/s3/s3.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/s3/s3.go#L571

Added line #L571 was not covered by tests
}

if len(args) == 2 && len(args[1]) != 0 {
Expand Down
7 changes: 4 additions & 3 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,18 @@ func SetKey(cacheName string, key string, value string, expiration *time.Duratio
func GetKey(cacheName string, key string) (string, error) {
for i, name := range CacheNames {
if name == cacheName {
if value, err := Caches[i].Get(key); err != nil {
value, err := Caches[i].Get(key)
if err != nil {
// do not warn or log if key not found
if errors.Is(err, gcache.KeyNotFoundError) {
return "", nil
}
CacheConfig[i].Logger.Warningf("While getting key %s in cache %s: %s", key, cacheName, err)

return "", err
} else {
return value.(string), nil
}

return value.(string), nil
}
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/hubtest/hubtest_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,6 @@
return t.RunWithLogFile()
mmetc marked this conversation as resolved.
Show resolved Hide resolved
} else if t.Config.NucleiTemplate != "" {
return t.RunWithNucleiTemplate()
} else {
return fmt.Errorf("log file or nuclei template must be set in '%s'", t.Name)
}
return fmt.Errorf("log file or nuclei template must be set in '%s'", t.Name)

Check warning on line 649 in pkg/hubtest/hubtest_item.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L649

Added line #L649 was not covered by tests
}
32 changes: 16 additions & 16 deletions pkg/leakybucket/reset_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,22 @@
cancelExprCacheLock.Unlock()
u.CancelOnFilter = compiled.CancelOnFilter
return nil
} else {
cancelExprCacheLock.Unlock()
//release the lock during compile
}

compiledExpr.CancelOnFilter, err = expr.Compile(bucketFactory.CancelOnFilter, exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
if err != nil {
bucketFactory.logger.Errorf("reset_filter compile error : %s", err)
return err
}
u.CancelOnFilter = compiledExpr.CancelOnFilter
if bucketFactory.Debug {
u.Debug = true
}
cancelExprCacheLock.Lock()
cancelExprCache[bucketFactory.CancelOnFilter] = compiledExpr
cancelExprCacheLock.Unlock()
cancelExprCacheLock.Unlock()
//release the lock during compile

compiledExpr.CancelOnFilter, err = expr.Compile(bucketFactory.CancelOnFilter, exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
if err != nil {
bucketFactory.logger.Errorf("reset_filter compile error : %s", err)
return err

Check warning on line 93 in pkg/leakybucket/reset_filter.go

View check run for this annotation

Codecov / codecov/patch

pkg/leakybucket/reset_filter.go#L92-L93

Added lines #L92 - L93 were not covered by tests
}
return err
u.CancelOnFilter = compiledExpr.CancelOnFilter
if bucketFactory.Debug {
u.Debug = true
}
cancelExprCacheLock.Lock()
cancelExprCache[bucketFactory.CancelOnFilter] = compiledExpr
cancelExprCacheLock.Unlock()
return nil
}
6 changes: 2 additions & 4 deletions pkg/leakybucket/uniq.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ func (u *Uniq) OnBucketPour(bucketFactory *BucketFactory) func(types.Event, *Lea
leaky.logger.Debugf("Uniq(%s) : ok", element)
u.KeyCache[element] = true
return &msg

} else {
leaky.logger.Debugf("Uniq(%s) : ko, discard event", element)
return nil
}
leaky.logger.Debugf("Uniq(%s) : ko, discard event", element)
return nil
}
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/types/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@
}

func (e *Event) GetType() string {
if e.Type == OVFLW {
switch e.Type {
case OVFLW:

Check warning on line 67 in pkg/types/event.go

View check run for this annotation

Codecov / codecov/patch

pkg/types/event.go#L66-L67

Added lines #L66 - L67 were not covered by tests
return "overflow"
} else if e.Type == LOG {
case LOG:

Check warning on line 69 in pkg/types/event.go

View check run for this annotation

Codecov / codecov/patch

pkg/types/event.go#L69

Added line #L69 was not covered by tests
return "log"
} else {
default:

Check warning on line 71 in pkg/types/event.go

View check run for this annotation

Codecov / codecov/patch

pkg/types/event.go#L71

Added line #L71 was not covered by tests
log.Warningf("unknown event type for %+v", e)
return "unknown"
}
Expand Down
Loading