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

lint: gocritic/captLocal (don't capitalize local variables) #3402

Merged
merged 3 commits into from
Jan 16, 2025
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
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ linters-settings:
- unnamedResult
- sloppyReassign
- appendCombine
- captLocal
- typeUnparen
- commentFormatting
- deferInLoop #
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/clidecision/decisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (cli *cliDecisions) NewCommand() *cobra.Command {
return cmd
}

func (cli *cliDecisions) list(ctx context.Context, filter apiclient.AlertsListOpts, NoSimu *bool, contained *bool, printMachine bool) error {
func (cli *cliDecisions) list(ctx context.Context, filter apiclient.AlertsListOpts, noSimu *bool, contained *bool, printMachine bool) error {
var err error

*filter.ScopeEquals, err = clialert.SanitizeScope(*filter.ScopeEquals, *filter.IPEquals, *filter.RangeEquals)
Expand All @@ -181,7 +181,7 @@ func (cli *cliDecisions) list(ctx context.Context, filter apiclient.AlertsListOp
filter.ActiveDecisionEquals = new(bool)
*filter.ActiveDecisionEquals = true

if NoSimu != nil && *NoSimu {
if noSimu != nil && *noSimu {
filter.IncludeSimulated = new(bool)
}
/* nullify the empty entries to avoid bad filter */
Expand Down
20 changes: 10 additions & 10 deletions pkg/acquisition/acquisition.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,13 @@
return evtCopy
}

func transform(transformChan chan types.Event, output chan types.Event, AcquisTomb *tomb.Tomb, transformRuntime *vm.Program, logger *log.Entry) {
func transform(transformChan chan types.Event, output chan types.Event, acquisTomb *tomb.Tomb, transformRuntime *vm.Program, logger *log.Entry) {

Check warning on line 368 in pkg/acquisition/acquisition.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/acquisition.go#L368

Added line #L368 was not covered by tests
defer trace.CatchPanic("crowdsec/acquis")
logger.Infof("transformer started")

for {
select {
case <-AcquisTomb.Dying():
case <-acquisTomb.Dying():

Check warning on line 374 in pkg/acquisition/acquisition.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/acquisition.go#L374

Added line #L374 was not covered by tests
logger.Debugf("transformer is dying")
return
case evt := <-transformChan:
Expand Down Expand Up @@ -420,7 +420,7 @@
}
}

func StartAcquisition(ctx context.Context, sources []DataSource, output chan types.Event, AcquisTomb *tomb.Tomb) error {
func StartAcquisition(ctx context.Context, sources []DataSource, output chan types.Event, acquisTomb *tomb.Tomb) error {
// Don't wait if we have no sources, as it will hang forever
if len(sources) == 0 {
return nil
Expand All @@ -430,7 +430,7 @@
subsrc := sources[i] // ensure its a copy
log.Debugf("starting one source %d/%d ->> %T", i, len(sources), subsrc)

AcquisTomb.Go(func() error {
acquisTomb.Go(func() error {
defer trace.CatchPanic("crowdsec/acquis")

var err error
Expand All @@ -449,29 +449,29 @@
"datasource": subsrc.GetName(),
})

AcquisTomb.Go(func() error {
transform(outChan, output, AcquisTomb, transformRuntime, transformLogger)
acquisTomb.Go(func() error {
transform(outChan, output, acquisTomb, transformRuntime, transformLogger)

Check warning on line 453 in pkg/acquisition/acquisition.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/acquisition.go#L452-L453

Added lines #L452 - L453 were not covered by tests
return nil
})
}

if subsrc.GetMode() == configuration.TAIL_MODE {
err = subsrc.StreamingAcquisition(ctx, outChan, AcquisTomb)
err = subsrc.StreamingAcquisition(ctx, outChan, acquisTomb)
} else {
err = subsrc.OneShotAcquisition(ctx, outChan, AcquisTomb)
err = subsrc.OneShotAcquisition(ctx, outChan, acquisTomb)
}

if err != nil {
// if one of the acqusition returns an error, we kill the others to properly shutdown
AcquisTomb.Kill(err)
acquisTomb.Kill(err)
}

return nil
})
}

/*return only when acquisition is over (cat) or never (tail)*/
err := AcquisTomb.Wait()
err := acquisTomb.Wait()

return err
}
22 changes: 19 additions & 3 deletions pkg/acquisition/modules/appsec/appsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@
return []prometheus.Collector{AppsecReqCounter, AppsecBlockCounter, AppsecRuleHits, AppsecOutbandParsingHistogram, AppsecInbandParsingHistogram, AppsecGlobalParsingHistogram}
}

func (w *AppsecSource) Configure(yamlConfig []byte, logger *log.Entry, MetricsLevel int) error {
func (w *AppsecSource) Configure(yamlConfig []byte, logger *log.Entry, metricsLevel int) error {

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L158

Added line #L158 was not covered by tests
err := w.UnmarshalConfig(yamlConfig)
if err != nil {
return fmt.Errorf("unable to parse appsec configuration: %w", err)
}

w.logger = logger
w.metricsLevel = MetricsLevel
w.metricsLevel = metricsLevel

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L165

Added line #L165 was not covered by tests
w.logger.Tracef("Appsec configuration: %+v", w.config)

if w.config.AuthCacheDuration == nil {
Expand All @@ -180,7 +180,7 @@
w.InChan = make(chan appsec.ParsedRequest)
appsecCfg := appsec.AppsecConfig{Logger: w.logger.WithField("component", "appsec_config")}

//we keep the datasource name
// we keep the datasource name

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L183

Added line #L183 was not covered by tests
appsecCfg.Name = w.config.Name

// let's load the associated appsec_config:
Expand Down Expand Up @@ -275,6 +275,7 @@

for _, runner := range w.AppsecRunners {
runner.outChan = out

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L278

Added line #L278 was not covered by tests
t.Go(func() error {
defer trace.CatchPanic("crowdsec/acquis/appsec/live/runner")
return runner.Run(t)
Expand All @@ -285,16 +286,20 @@
if w.config.ListenSocket != "" {
w.logger.Infof("creating unix socket %s", w.config.ListenSocket)
_ = os.RemoveAll(w.config.ListenSocket)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L289 was not covered by tests
listener, err := net.Listen("unix", w.config.ListenSocket)
if err != nil {
return fmt.Errorf("appsec server failed: %w", err)
}

defer listener.Close()

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L296

Added line #L296 was not covered by tests
if w.config.CertFilePath != "" && w.config.KeyFilePath != "" {
err = w.server.ServeTLS(listener, w.config.CertFilePath, w.config.KeyFilePath)
} else {
err = w.server.Serve(listener)
}

if err != nil && !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("appsec server failed: %w", err)
}
Expand All @@ -304,8 +309,10 @@
})
t.Go(func() error {
var err error

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L312

Added line #L312 was not covered by tests
if w.config.ListenAddr != "" {
w.logger.Infof("creating TCP server on %s", w.config.ListenAddr)

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L315

Added line #L315 was not covered by tests
if w.config.CertFilePath != "" && w.config.KeyFilePath != "" {
err = w.server.ListenAndServeTLS(w.config.CertFilePath, w.config.KeyFilePath)
} else {
Expand All @@ -324,6 +331,7 @@
// xx let's clean up the appsec runners :)
appsec.AppsecRulesDetails = make(map[int]appsec.RulesDetails)
w.server.Shutdown(ctx)

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L334

Added line #L334 was not covered by tests
return nil
})

Expand Down Expand Up @@ -354,11 +362,13 @@
}

req.Header.Add("X-Api-Key", apiKey)

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L365

Added line #L365 was not covered by tests
resp, err := client.Do(req)
if err != nil {
log.Errorf("Error performing request: %s", err)
return false
}

defer resp.Body.Close()

return resp.StatusCode == http.StatusOK
Expand All @@ -371,17 +381,21 @@
apiKey := r.Header.Get(appsec.APIKeyHeaderName)
clientIP := r.Header.Get(appsec.IPHeaderName)
remoteIP := r.RemoteAddr

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L384

Added line #L384 was not covered by tests
if apiKey == "" {
w.logger.Errorf("Unauthorized request from '%s' (real IP = %s)", remoteIP, clientIP)
rw.WriteHeader(http.StatusUnauthorized)

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L388

Added line #L388 was not covered by tests
return
}

expiration, exists := w.AuthCache.Get(apiKey)
// if the apiKey is not in cache or has expired, just recheck the auth
if !exists || time.Now().After(expiration) {
if !w.IsAuth(apiKey) {
rw.WriteHeader(http.StatusUnauthorized)
w.logger.Errorf("Unauthorized request from '%s' (real IP = %s)", remoteIP, clientIP)

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L398

Added line #L398 was not covered by tests
return
}

Expand All @@ -394,8 +408,10 @@
if err != nil {
w.logger.Errorf("%s", err)
rw.WriteHeader(http.StatusInternalServerError)

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

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L411

Added line #L411 was not covered by tests
return
}

parsedRequest.AppsecEngine = w.config.Name

logger := w.logger.WithFields(log.Fields{
Expand Down
Loading
Loading