From 1f5298f0f1467c8566f1e0932d5fde000852ee48 Mon Sep 17 00:00:00 2001 From: marco Date: Thu, 25 Apr 2024 15:18:15 +0200 Subject: [PATCH] lint (intrange) --- cmd/crowdsec-cli/decisions.go | 2 +- cmd/crowdsec-cli/machines.go | 2 +- cmd/crowdsec/crowdsec.go | 6 +++--- pkg/acquisition/acquisition.go | 4 ++-- pkg/acquisition/acquisition_test.go | 6 +++--- pkg/acquisition/modules/appsec/appsec.go | 2 +- pkg/acquisition/modules/file/file_test.go | 2 +- pkg/acquisition/modules/kinesis/kinesis.go | 4 ++-- pkg/acquisition/modules/kinesis/kinesis_test.go | 8 ++++---- pkg/acquisition/modules/loki/loki_test.go | 2 +- .../modules/syslog/internal/parser/utils/utils.go | 2 +- pkg/apiclient/auth_retry.go | 2 +- pkg/apiserver/apic_test.go | 2 +- pkg/apiserver/controllers/v1/alerts.go | 2 +- pkg/csplugin/watcher_test.go | 2 +- pkg/exprhelpers/debugger.go | 2 +- pkg/exprhelpers/helpers.go | 2 +- pkg/leakybucket/manager_run.go | 2 +- pkg/parser/parsing_test.go | 4 ++-- 19 files changed, 29 insertions(+), 29 deletions(-) diff --git a/cmd/crowdsec-cli/decisions.go b/cmd/crowdsec-cli/decisions.go index 3fb790633..4c7ccf98c 100644 --- a/cmd/crowdsec-cli/decisions.go +++ b/cmd/crowdsec-cli/decisions.go @@ -31,7 +31,7 @@ func (cli *cliDecisions) decisionsToTable(alerts *models.GetAlertsResponse, prin spamLimit := make(map[string]bool) skipped := 0 - for aIdx := 0; aIdx < len(*alerts); aIdx++ { + for aIdx := range len(*alerts) { alertItem := (*alerts)[aIdx] newDecisions := make([]*models.Decision, 0) diff --git a/cmd/crowdsec-cli/machines.go b/cmd/crowdsec-cli/machines.go index 1457fb5a0..35444ae8d 100644 --- a/cmd/crowdsec-cli/machines.go +++ b/cmd/crowdsec-cli/machines.go @@ -41,7 +41,7 @@ func generatePassword(length int) string { buf := make([]byte, length) - for i := 0; i < length; i++ { + for i := range length { rInt, err := saferand.Int(saferand.Reader, big.NewInt(int64(charsetLength))) if err != nil { log.Fatalf("failed getting data from prng for password generation : %s", err) diff --git a/cmd/crowdsec/crowdsec.go b/cmd/crowdsec/crowdsec.go index 8f07d165f..dd0203eca 100644 --- a/cmd/crowdsec/crowdsec.go +++ b/cmd/crowdsec/crowdsec.go @@ -65,7 +65,7 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H parsersTomb.Go(func() error { parserWg.Add(1) - for i := 0; i < cConfig.Crowdsec.ParserRoutinesCount; i++ { + for range cConfig.Crowdsec.ParserRoutinesCount { parsersTomb.Go(func() error { defer trace.CatchPanic("crowdsec/runParse") @@ -97,7 +97,7 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H } } - for i := 0; i < cConfig.Crowdsec.BucketsRoutinesCount; i++ { + for range cConfig.Crowdsec.BucketsRoutinesCount { bucketsTomb.Go(func() error { defer trace.CatchPanic("crowdsec/runPour") @@ -128,7 +128,7 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H outputsTomb.Go(func() error { outputWg.Add(1) - for i := 0; i < cConfig.Crowdsec.OutputRoutinesCount; i++ { + for range cConfig.Crowdsec.OutputRoutinesCount { outputsTomb.Go(func() error { defer trace.CatchPanic("crowdsec/runOutput") diff --git a/pkg/acquisition/acquisition.go b/pkg/acquisition/acquisition.go index 677bf664e..8456fc30c 100644 --- a/pkg/acquisition/acquisition.go +++ b/pkg/acquisition/acquisition.go @@ -269,7 +269,7 @@ func LoadAcquisitionFromFile(config *csconfig.CrowdsecServiceCfg, prom *csconfig func GetMetrics(sources []DataSource, aggregated bool) error { var metrics []prometheus.Collector - for i := 0; i < len(sources); i++ { + for i := range len(sources) { if aggregated { metrics = sources[i].GetMetrics() } else { @@ -343,7 +343,7 @@ func StartAcquisition(sources []DataSource, output chan types.Event, AcquisTomb return nil } - for i := 0; i < len(sources); i++ { + for i := range len(sources) { subsrc := sources[i] //ensure its a copy log.Debugf("starting one source %d/%d ->> %T", i, len(sources), subsrc) diff --git a/pkg/acquisition/acquisition_test.go b/pkg/acquisition/acquisition_test.go index 33e494855..4205d3192 100644 --- a/pkg/acquisition/acquisition_test.go +++ b/pkg/acquisition/acquisition_test.go @@ -322,7 +322,7 @@ func (f *MockCat) UnmarshalConfig(cfg []byte) error { return nil } func (f *MockCat) GetName() string { return "mock_cat" } func (f *MockCat) GetMode() string { return "cat" } func (f *MockCat) OneShotAcquisition(out chan types.Event, tomb *tomb.Tomb) error { - for i := 0; i < 10; i++ { + for range 10 { evt := types.Event{} evt.Line.Src = "test" out <- evt @@ -369,7 +369,7 @@ func (f *MockTail) OneShotAcquisition(out chan types.Event, tomb *tomb.Tomb) err return fmt.Errorf("can't run in cat mode") } func (f *MockTail) StreamingAcquisition(out chan types.Event, t *tomb.Tomb) error { - for i := 0; i < 10; i++ { + for range 10 { evt := types.Event{} evt.Line.Src = "test" out <- evt @@ -452,7 +452,7 @@ type MockTailError struct { } func (f *MockTailError) StreamingAcquisition(out chan types.Event, t *tomb.Tomb) error { - for i := 0; i < 10; i++ { + for range 10 { evt := types.Event{} evt.Line.Src = "test" out <- evt diff --git a/pkg/acquisition/modules/appsec/appsec.go b/pkg/acquisition/modules/appsec/appsec.go index f97905406..6fc3a60bc 100644 --- a/pkg/acquisition/modules/appsec/appsec.go +++ b/pkg/acquisition/modules/appsec/appsec.go @@ -202,7 +202,7 @@ func (w *AppsecSource) Configure(yamlConfig []byte, logger *log.Entry, MetricsLe w.AppsecRunners = make([]AppsecRunner, w.config.Routines) - for nbRoutine := 0; nbRoutine < w.config.Routines; nbRoutine++ { + for nbRoutine := range w.config.Routines { appsecRunnerUUID := uuid.New().String() //we copy AppsecRutime for each runner wrt := *w.AppsecRuntime diff --git a/pkg/acquisition/modules/file/file_test.go b/pkg/acquisition/modules/file/file_test.go index ad5fe8bfa..dcd6a297f 100644 --- a/pkg/acquisition/modules/file/file_test.go +++ b/pkg/acquisition/modules/file/file_test.go @@ -413,7 +413,7 @@ force_inotify: true`, testPattern), fd, err := os.Create("test_files/stream.log") require.NoError(t, err, "could not create test file") - for i := 0; i < 5; i++ { + for i := range 5 { _, err = fmt.Fprintf(fd, "%d\n", i) if err != nil { t.Fatalf("could not write test file : %s", err) diff --git a/pkg/acquisition/modules/kinesis/kinesis.go b/pkg/acquisition/modules/kinesis/kinesis.go index a86816244..763f6e7a9 100644 --- a/pkg/acquisition/modules/kinesis/kinesis.go +++ b/pkg/acquisition/modules/kinesis/kinesis.go @@ -208,7 +208,7 @@ func (k *KinesisSource) decodeFromSubscription(record []byte) ([]CloudwatchSubsc func (k *KinesisSource) WaitForConsumerDeregistration(consumerName string, streamARN string) error { maxTries := k.Config.MaxRetries - for i := 0; i < maxTries; i++ { + for i := range maxTries { _, err := k.kClient.DescribeStreamConsumer(&kinesis.DescribeStreamConsumerInput{ ConsumerName: aws.String(consumerName), StreamARN: aws.String(streamARN), @@ -249,7 +249,7 @@ func (k *KinesisSource) DeregisterConsumer() error { func (k *KinesisSource) WaitForConsumerRegistration(consumerARN string) error { maxTries := k.Config.MaxRetries - for i := 0; i < maxTries; i++ { + for i := range maxTries { describeOutput, err := k.kClient.DescribeStreamConsumer(&kinesis.DescribeStreamConsumerInput{ ConsumerARN: aws.String(consumerARN), }) diff --git a/pkg/acquisition/modules/kinesis/kinesis_test.go b/pkg/acquisition/modules/kinesis/kinesis_test.go index a4e4f2f73..c81821cc9 100644 --- a/pkg/acquisition/modules/kinesis/kinesis_test.go +++ b/pkg/acquisition/modules/kinesis/kinesis_test.go @@ -71,7 +71,7 @@ func WriteToStream(streamName string, count int, shards int, sub bool) { } sess := session.Must(session.NewSession()) kinesisClient := kinesis.New(sess, aws.NewConfig().WithEndpoint(endpoint).WithRegion("us-east-1")) - for i := 0; i < count; i++ { + for i := range count { partition := "partition" if shards != 1 { partition = fmt.Sprintf("partition-%d", i%shards) @@ -186,7 +186,7 @@ stream_name: stream-1-shard`, //Allow the datasource to start listening to the stream time.Sleep(4 * time.Second) WriteToStream(f.Config.StreamName, test.count, test.shards, false) - for i := 0; i < test.count; i++ { + for i := range test.count { e := <-out assert.Equal(t, fmt.Sprintf("%d", i), e.Line.Raw) } @@ -233,7 +233,7 @@ stream_name: stream-2-shards`, time.Sleep(4 * time.Second) WriteToStream(f.Config.StreamName, test.count, test.shards, false) c := 0 - for i := 0; i < test.count; i++ { + for range test.count { <-out c += 1 } @@ -281,7 +281,7 @@ from_subscription: true`, //Allow the datasource to start listening to the stream time.Sleep(4 * time.Second) WriteToStream(f.Config.StreamName, test.count, test.shards, true) - for i := 0; i < test.count; i++ { + for i := range test.count { e := <-out assert.Equal(t, fmt.Sprintf("%d", i), e.Line.Raw) } diff --git a/pkg/acquisition/modules/loki/loki_test.go b/pkg/acquisition/modules/loki/loki_test.go index 9ac3ccbd3..dbec973db 100644 --- a/pkg/acquisition/modules/loki/loki_test.go +++ b/pkg/acquisition/modules/loki/loki_test.go @@ -276,7 +276,7 @@ func feedLoki(logger *log.Entry, n int, title string) error { }, }, } - for i := 0; i < n; i++ { + for i := range n { streams.Streams[0].Values[i] = LogValue{ Time: time.Now(), Line: fmt.Sprintf("Log line #%d %v", i, title), diff --git a/pkg/acquisition/modules/syslog/internal/parser/utils/utils.go b/pkg/acquisition/modules/syslog/internal/parser/utils/utils.go index 8fe717a6a..5e0bf8fe7 100644 --- a/pkg/acquisition/modules/syslog/internal/parser/utils/utils.go +++ b/pkg/acquisition/modules/syslog/internal/parser/utils/utils.go @@ -34,7 +34,7 @@ func isValidHostname(s string) bool { last := byte('.') nonNumeric := false // true once we've seen a letter or hyphen partlen := 0 - for i := 0; i < len(s); i++ { + for i := range len(s) { c := s[i] switch { default: diff --git a/pkg/apiclient/auth_retry.go b/pkg/apiclient/auth_retry.go index 8ec8823f6..a17725439 100644 --- a/pkg/apiclient/auth_retry.go +++ b/pkg/apiclient/auth_retry.go @@ -41,7 +41,7 @@ func (r retryRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) maxAttempts = 1 } - for i := 0; i < maxAttempts; i++ { + for i := range maxAttempts { if i > 0 { if r.withBackOff { //nolint:gosec diff --git a/pkg/apiserver/apic_test.go b/pkg/apiserver/apic_test.go index 74c627cd0..5f8faa0d8 100644 --- a/pkg/apiserver/apic_test.go +++ b/pkg/apiserver/apic_test.go @@ -1076,7 +1076,7 @@ func TestAPICPush(t *testing.T) { expectedCalls: 2, alerts: func() []*models.Alert { alerts := make([]*models.Alert, 100) - for i := 0; i < 100; i++ { + for i := range 100 { alerts[i] = &models.Alert{ Scenario: ptr.Of("crowdsec/test"), ScenarioHash: ptr.Of("certified"), diff --git a/pkg/apiserver/controllers/v1/alerts.go b/pkg/apiserver/controllers/v1/alerts.go index 19dbf8d0c..c86c8a2c1 100644 --- a/pkg/apiserver/controllers/v1/alerts.go +++ b/pkg/apiserver/controllers/v1/alerts.go @@ -109,7 +109,7 @@ func FormatAlerts(result []*ent.Alert) models.AddAlertsRequest { func (c *Controller) sendAlertToPluginChannel(alert *models.Alert, profileID uint) { if c.PluginChannel != nil { RETRY: - for try := 0; try < 3; try++ { + for try := range 3 { select { case c.PluginChannel <- csplugin.ProfileAlert{ProfileID: profileID, Alert: alert}: log.Debugf("alert sent to Plugin channel") diff --git a/pkg/csplugin/watcher_test.go b/pkg/csplugin/watcher_test.go index d0bb7b2f1..b76c3c4ea 100644 --- a/pkg/csplugin/watcher_test.go +++ b/pkg/csplugin/watcher_test.go @@ -34,7 +34,7 @@ func resetWatcherAlertCounter(pw *PluginWatcher) { } func insertNAlertsToPlugin(pw *PluginWatcher, n int, pluginName string) { - for i := 0; i < n; i++ { + for range n { pw.Inserts <- pluginName } } diff --git a/pkg/exprhelpers/debugger.go b/pkg/exprhelpers/debugger.go index 432bb737e..17bd98e7c 100644 --- a/pkg/exprhelpers/debugger.go +++ b/pkg/exprhelpers/debugger.go @@ -346,7 +346,7 @@ func (erp ExprRuntimeDebug) ipDebug(ip int, vm *vm.VM, program *vm.Program, part } func (erp ExprRuntimeDebug) ipSeek(ip int) []string { - for i := 0; i < len(erp.Lines); i++ { + for i := range len(erp.Lines) { parts := strings.Split(erp.Lines[i], "\t") if parts[0] == strconv.Itoa(ip) { return parts diff --git a/pkg/exprhelpers/helpers.go b/pkg/exprhelpers/helpers.go index 79a621c7d..2884a197d 100644 --- a/pkg/exprhelpers/helpers.go +++ b/pkg/exprhelpers/helpers.go @@ -216,7 +216,7 @@ func flatten(args []interface{}, v reflect.Value) []interface{} { } if v.Kind() == reflect.Array || v.Kind() == reflect.Slice { - for i := 0; i < v.Len(); i++ { + for i := range v.Len() { args = flatten(args, v.Index(i)) } } else { diff --git a/pkg/leakybucket/manager_run.go b/pkg/leakybucket/manager_run.go index ae7a86a4e..275ac5d6a 100644 --- a/pkg/leakybucket/manager_run.go +++ b/pkg/leakybucket/manager_run.go @@ -298,7 +298,7 @@ func PourItemToHolders(parsed types.Event, holders []BucketFactory, buckets *Buc BucketPourCache["OK"] = append(BucketPourCache["OK"], evt.(types.Event)) } //find the relevant holders (scenarios) - for idx := 0; idx < len(holders); idx++ { + for idx := range len(holders) { //for idx, holder := range holders { //evaluate bucket's condition diff --git a/pkg/parser/parsing_test.go b/pkg/parser/parsing_test.go index 04d08cc27..3330443cd 100644 --- a/pkg/parser/parsing_test.go +++ b/pkg/parser/parsing_test.go @@ -129,7 +129,7 @@ func testOneParser(pctx *UnixParserCtx, ectx EnricherCtx, dir string, b *testing count = b.N b.ResetTimer() } - for n := 0; n < count; n++ { + for range count { if testFile(tests, *pctx, pnodes) != true { return fmt.Errorf("test failed !") } @@ -239,7 +239,7 @@ func matchEvent(expected types.Event, out types.Event, debug bool) ([]string, bo valid = true } - for mapIdx := 0; mapIdx < len(expectMaps); mapIdx++ { + for mapIdx := range len(expectMaps) { for expKey, expVal := range expectMaps[mapIdx] { if outVal, ok := outMaps[mapIdx][expKey]; ok { if outVal == expVal { //ok entry