From 7a1ad8376a6bc36e0007d5c7c75630ac8c81508c Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:40:48 +0100 Subject: [PATCH] lint: style, autofix (#3354) --- cmd/crowdsec-cli/dashboard.go | 4 +- cmd/crowdsec-cli/setup.go | 1 + cmd/crowdsec/appsec.go | 2 +- cmd/notification-email/main.go | 2 +- .../configuration/configuration.go | 10 ++-- .../modules/appsec/appsec_hooks_test.go | 4 +- .../modules/appsec/appsec_runner.go | 2 - pkg/acquisition/modules/appsec/appsec_test.go | 6 +- .../modules/appsec/bodyprocessors/raw.go | 7 +-- pkg/acquisition/modules/http/http.go | 4 +- pkg/acquisition/modules/http/http_test.go | 3 +- .../syslog/internal/parser/rfc3164/parse.go | 1 - .../syslog/internal/parser/rfc5424/parse.go | 3 - .../internal/parser/rfc5424/parse_test.go | 58 +++++++++++++----- .../syslog/internal/server/syslogserver.go | 1 - pkg/alertcontext/alertcontext_test.go | 1 - pkg/apiclient/auth_jwt.go | 3 - pkg/apiserver/apic.go | 1 - pkg/apiserver/middlewares/v1/api_key.go | 1 - pkg/appsec/appsec.go | 1 - pkg/appsec/appsec_rule/appsec_rule.go | 1 - pkg/appsec/appsec_rule/modsec_rule_test.go | 2 - pkg/appsec/request_test.go | 3 - pkg/csplugin/broker.go | 4 +- pkg/cticlient/types.go | 2 - pkg/exprhelpers/crowdsec_cti.go | 20 ++++--- pkg/exprhelpers/geoip.go | 3 - pkg/fflag/crowdsec.go | 14 +++-- pkg/leakybucket/blackhole.go | 2 - pkg/leakybucket/bucket.go | 1 - pkg/leakybucket/buckets.go | 1 - pkg/leakybucket/conditional.go | 6 +- pkg/leakybucket/manager_load_test.go | 59 ++++++++----------- pkg/leakybucket/manager_run.go | 13 ++-- pkg/leakybucket/processor.go | 3 +- pkg/leakybucket/reset_filter.go | 10 ++-- pkg/leakybucket/uniq.go | 6 +- pkg/parser/enrich.go | 6 +- pkg/parser/enrich_geoip.go | 3 - pkg/parser/parsing_test.go | 4 +- pkg/parser/runtime.go | 14 +++-- pkg/types/appsec_event.go | 1 - pkg/types/constants.go | 34 ++++++----- pkg/types/event_test.go | 2 - pkg/types/getfstype.go | 1 - pkg/types/ip.go | 3 +- pkg/types/ip_test.go | 5 +- pkg/types/utils.go | 8 ++- 48 files changed, 177 insertions(+), 169 deletions(-) diff --git a/cmd/crowdsec-cli/dashboard.go b/cmd/crowdsec-cli/dashboard.go index 53a7dff85a0..7ddac093dcd 100644 --- a/cmd/crowdsec-cli/dashboard.go +++ b/cmd/crowdsec-cli/dashboard.go @@ -243,7 +243,8 @@ func (cli *cliDashboard) newStopCmd() *cobra.Command { } func (cli *cliDashboard) newShowPasswordCmd() *cobra.Command { - cmd := &cobra.Command{Use: "show-password", + cmd := &cobra.Command{ + Use: "show-password", Short: "displays password of metabase.", Args: cobra.NoArgs, DisableAutoGenTag: true, @@ -457,7 +458,6 @@ func checkGroups(forceYes *bool) (*user.Group, error) { func (cli *cliDashboard) chownDatabase(gid string) error { cfg := cli.cfg() intID, err := strconv.Atoi(gid) - if err != nil { return fmt.Errorf("unable to convert group ID to int: %s", err) } diff --git a/cmd/crowdsec-cli/setup.go b/cmd/crowdsec-cli/setup.go index 66c0d71e777..3581d69f052 100644 --- a/cmd/crowdsec-cli/setup.go +++ b/cmd/crowdsec-cli/setup.go @@ -1,4 +1,5 @@ //go:build !no_cscli_setup + package main import ( diff --git a/cmd/crowdsec/appsec.go b/cmd/crowdsec/appsec.go index cb02b137dcd..4320133b063 100644 --- a/cmd/crowdsec/appsec.go +++ b/cmd/crowdsec/appsec.go @@ -1,4 +1,4 @@ -// +build !no_datasource_appsec +//go:build !no_datasource_appsec package main diff --git a/cmd/notification-email/main.go b/cmd/notification-email/main.go index 5fc02cdd1d7..b61644611b4 100644 --- a/cmd/notification-email/main.go +++ b/cmd/notification-email/main.go @@ -68,7 +68,7 @@ func (n *EmailPlugin) Configure(ctx context.Context, config *protobufs.Config) ( EncryptionType: "ssltls", AuthType: "login", SenderEmail: "crowdsec@crowdsec.local", - HeloHost: "localhost", + HeloHost: "localhost", } if err := yaml.Unmarshal(config.Config, &d); err != nil { diff --git a/pkg/acquisition/configuration/configuration.go b/pkg/acquisition/configuration/configuration.go index 3e27da1b9e6..a9d570d2788 100644 --- a/pkg/acquisition/configuration/configuration.go +++ b/pkg/acquisition/configuration/configuration.go @@ -13,12 +13,14 @@ type DataSourceCommonCfg struct { UseTimeMachine bool `yaml:"use_time_machine,omitempty"` UniqueId string `yaml:"unique_id,omitempty"` TransformExpr string `yaml:"transform,omitempty"` - Config map[string]interface{} `yaml:",inline"` //to keep the datasource-specific configuration directives + Config map[string]interface{} `yaml:",inline"` // to keep the datasource-specific configuration directives } -var TAIL_MODE = "tail" -var CAT_MODE = "cat" -var SERVER_MODE = "server" // No difference with tail, just a bit more verbose +var ( + TAIL_MODE = "tail" + CAT_MODE = "cat" + SERVER_MODE = "server" // No difference with tail, just a bit more verbose +) const ( METRICS_NONE = iota diff --git a/pkg/acquisition/modules/appsec/appsec_hooks_test.go b/pkg/acquisition/modules/appsec/appsec_hooks_test.go index c549d2ef1d1..d87384a0189 100644 --- a/pkg/acquisition/modules/appsec/appsec_hooks_test.go +++ b/pkg/acquisition/modules/appsec/appsec_hooks_test.go @@ -341,7 +341,6 @@ func TestAppsecOnMatchHooks(t *testing.T) { } func TestAppsecPreEvalHooks(t *testing.T) { - tests := []appsecRuleTest{ { name: "Basic pre_eval hook to disable inband rule", @@ -403,7 +402,6 @@ func TestAppsecPreEvalHooks(t *testing.T) { require.Len(t, responses, 1) require.True(t, responses[0].InBandInterrupt) - }, }, { @@ -670,7 +668,6 @@ func TestAppsecPreEvalHooks(t *testing.T) { } func TestAppsecRemediationConfigHooks(t *testing.T) { - tests := []appsecRuleTest{ { name: "Basic matching rule", @@ -759,6 +756,7 @@ func TestAppsecRemediationConfigHooks(t *testing.T) { }) } } + func TestOnMatchRemediationHooks(t *testing.T) { tests := []appsecRuleTest{ { diff --git a/pkg/acquisition/modules/appsec/appsec_runner.go b/pkg/acquisition/modules/appsec/appsec_runner.go index c6ca4fa5870..d4535d3f9a2 100644 --- a/pkg/acquisition/modules/appsec/appsec_runner.go +++ b/pkg/acquisition/modules/appsec/appsec_runner.go @@ -90,7 +90,6 @@ func (r *AppsecRunner) Init(datadir string) error { outbandCfg = outbandCfg.WithRequestBodyInMemoryLimit(*r.AppsecRuntime.Config.OutOfBandOptions.RequestBodyInMemoryLimit) } r.AppsecOutbandEngine, err = coraza.NewWAF(outbandCfg) - if err != nil { return fmt.Errorf("unable to initialize outband engine : %w", err) } @@ -379,7 +378,6 @@ func (r *AppsecRunner) handleRequest(request *appsec.ParsedRequest) { // time spent to process inband AND out of band rules globalParsingElapsed := time.Since(startGlobalParsing) AppsecGlobalParsingHistogram.With(prometheus.Labels{"source": request.RemoteAddrNormalized, "appsec_engine": request.AppsecEngine}).Observe(globalParsingElapsed.Seconds()) - } func (r *AppsecRunner) Run(t *tomb.Tomb) error { diff --git a/pkg/acquisition/modules/appsec/appsec_test.go b/pkg/acquisition/modules/appsec/appsec_test.go index 142b5174940..c0af1002f49 100644 --- a/pkg/acquisition/modules/appsec/appsec_test.go +++ b/pkg/acquisition/modules/appsec/appsec_test.go @@ -66,7 +66,8 @@ func loadAppSecEngine(test appsecRuleTest, t *testing.T) { outofbandRules = append(outofbandRules, strRule) } - appsecCfg := appsec.AppsecConfig{Logger: logger, + appsecCfg := appsec.AppsecConfig{ + Logger: logger, OnLoad: test.on_load, PreEval: test.pre_eval, PostEval: test.post_eval, @@ -75,7 +76,8 @@ func loadAppSecEngine(test appsecRuleTest, t *testing.T) { UserBlockedHTTPCode: test.UserBlockedHTTPCode, UserPassedHTTPCode: test.UserPassedHTTPCode, DefaultRemediation: test.DefaultRemediation, - DefaultPassAction: test.DefaultPassAction} + DefaultPassAction: test.DefaultPassAction, + } AppsecRuntime, err := appsecCfg.Build() if err != nil { t.Fatalf("unable to build appsec runtime : %s", err) diff --git a/pkg/acquisition/modules/appsec/bodyprocessors/raw.go b/pkg/acquisition/modules/appsec/bodyprocessors/raw.go index e2e23eb57ae..aa467ecf048 100644 --- a/pkg/acquisition/modules/appsec/bodyprocessors/raw.go +++ b/pkg/acquisition/modules/appsec/bodyprocessors/raw.go @@ -9,8 +9,7 @@ import ( "github.com/crowdsecurity/coraza/v3/experimental/plugins/plugintypes" ) -type rawBodyProcessor struct { -} +type rawBodyProcessor struct{} type setterInterface interface { Set(string) @@ -33,9 +32,7 @@ func (*rawBodyProcessor) ProcessResponse(reader io.Reader, v plugintypes.Transac return nil } -var ( - _ plugintypes.BodyProcessor = &rawBodyProcessor{} -) +var _ plugintypes.BodyProcessor = &rawBodyProcessor{} //nolint:gochecknoinits //Coraza recommends to use init() for registering plugins func init() { diff --git a/pkg/acquisition/modules/http/http.go b/pkg/acquisition/modules/http/http.go index 98af134c84e..6bb8228f32c 100644 --- a/pkg/acquisition/modules/http/http.go +++ b/pkg/acquisition/modules/http/http.go @@ -26,9 +26,7 @@ import ( "github.com/crowdsecurity/crowdsec/pkg/types" ) -var ( - dataSourceName = "http" -) +var dataSourceName = "http" var linesRead = prometheus.NewCounterVec( prometheus.CounterOpts{ diff --git a/pkg/acquisition/modules/http/http_test.go b/pkg/acquisition/modules/http/http_test.go index f89ba7aa8ba..4d99134419f 100644 --- a/pkg/acquisition/modules/http/http_test.go +++ b/pkg/acquisition/modules/http/http_test.go @@ -18,9 +18,9 @@ import ( "github.com/crowdsecurity/go-cs-lib/cstest" "github.com/prometheus/client_golang/prometheus" log "github.com/sirupsen/logrus" - "gopkg.in/tomb.v2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "gopkg.in/tomb.v2" ) const ( @@ -257,7 +257,6 @@ basic_auth: h.Server.Close() tomb.Kill(nil) tomb.Wait() - } func TestStreamingAcquisitionUnknownPath(t *testing.T) { diff --git a/pkg/acquisition/modules/syslog/internal/parser/rfc3164/parse.go b/pkg/acquisition/modules/syslog/internal/parser/rfc3164/parse.go index 66d842ed519..04c7053ef27 100644 --- a/pkg/acquisition/modules/syslog/internal/parser/rfc3164/parse.go +++ b/pkg/acquisition/modules/syslog/internal/parser/rfc3164/parse.go @@ -48,7 +48,6 @@ func WithStrictHostname() RFC3164Option { } func (r *RFC3164) parsePRI() error { - pri := 0 if r.buf[r.position] != '<' { diff --git a/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse.go b/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse.go index 639e91e1224..c9aa89f7256 100644 --- a/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse.go +++ b/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse.go @@ -48,7 +48,6 @@ func WithStrictHostname() RFC5424Option { } func (r *RFC5424) parsePRI() error { - pri := 0 if r.buf[r.position] != '<' { @@ -94,7 +93,6 @@ func (r *RFC5424) parseVersion() error { } func (r *RFC5424) parseTimestamp() error { - timestamp := []byte{} if r.buf[r.position] == NIL_VALUE { @@ -121,7 +119,6 @@ func (r *RFC5424) parseTimestamp() error { } date, err := time.Parse(VALID_TIMESTAMP, string(timestamp)) - if err != nil { return errors.New("timestamp is not valid") } diff --git a/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse_test.go b/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse_test.go index 0938e947fe7..d3a68c196db 100644 --- a/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse_test.go +++ b/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse_test.go @@ -94,7 +94,8 @@ func TestParse(t *testing.T) { }{ { "valid msg", - `<13>1 2021-05-18T11:58:40.828081+02:42 mantis sshd 49340 - [timeQuality isSynced="0" tzKnown="1"] blabla`, expected{ + `<13>1 2021-05-18T11:58:40.828081+02:42 mantis sshd 49340 - [timeQuality isSynced="0" tzKnown="1"] blabla`, + expected{ Timestamp: time.Date(2021, 5, 18, 11, 58, 40, 828081000, time.FixedZone("+0242", 9720)), Hostname: "mantis", Tag: "sshd", @@ -102,11 +103,14 @@ func TestParse(t *testing.T) { MsgID: "", Message: "blabla", PRI: 13, - }, "", []RFC5424Option{}, + }, + "", + []RFC5424Option{}, }, { "valid msg with msgid", - `<13>1 2021-05-18T11:58:40.828081+02:42 mantis foobar 49340 123123 [timeQuality isSynced="0" tzKnown="1"] blabla`, expected{ + `<13>1 2021-05-18T11:58:40.828081+02:42 mantis foobar 49340 123123 [timeQuality isSynced="0" tzKnown="1"] blabla`, + expected{ Timestamp: time.Date(2021, 5, 18, 11, 58, 40, 828081000, time.FixedZone("+0242", 9720)), Hostname: "mantis", Tag: "foobar", @@ -114,11 +118,14 @@ func TestParse(t *testing.T) { MsgID: "123123", Message: "blabla", PRI: 13, - }, "", []RFC5424Option{}, + }, + "", + []RFC5424Option{}, }, { "valid msg with repeating SD", - `<13>1 2021-05-18T11:58:40.828081+02:42 mantis foobar 49340 123123 [timeQuality isSynced="0" tzKnown="1"][foo="bar][a] blabla`, expected{ + `<13>1 2021-05-18T11:58:40.828081+02:42 mantis foobar 49340 123123 [timeQuality isSynced="0" tzKnown="1"][foo="bar][a] blabla`, + expected{ Timestamp: time.Date(2021, 5, 18, 11, 58, 40, 828081000, time.FixedZone("+0242", 9720)), Hostname: "mantis", Tag: "foobar", @@ -126,36 +133,53 @@ func TestParse(t *testing.T) { MsgID: "123123", Message: "blabla", PRI: 13, - }, "", []RFC5424Option{}, + }, + "", + []RFC5424Option{}, }, { "invalid SD", - `<13>1 2021-05-18T11:58:40.828081+02:00 mantis foobar 49340 123123 [timeQuality asd`, expected{}, "structured data must end with ']'", []RFC5424Option{}, + `<13>1 2021-05-18T11:58:40.828081+02:00 mantis foobar 49340 123123 [timeQuality asd`, + expected{}, + "structured data must end with ']'", + []RFC5424Option{}, }, { "invalid version", - `<13>42 2021-05-18T11:58:40.828081+02:00 mantis foobar 49340 123123 [timeQuality isSynced="0" tzKnown="1"] blabla`, expected{}, "version must be 1", []RFC5424Option{}, + `<13>42 2021-05-18T11:58:40.828081+02:00 mantis foobar 49340 123123 [timeQuality isSynced="0" tzKnown="1"] blabla`, + expected{}, + "version must be 1", + []RFC5424Option{}, }, { "invalid message", - `<13>1`, expected{}, "version must be followed by a space", []RFC5424Option{}, + `<13>1`, + expected{}, + "version must be followed by a space", + []RFC5424Option{}, }, { "valid msg with empty fields", - `<13>1 - foo - - - - blabla`, expected{ + `<13>1 - foo - - - - blabla`, + expected{ Timestamp: time.Now().UTC(), Hostname: "foo", PRI: 13, Message: "blabla", - }, "", []RFC5424Option{}, + }, + "", + []RFC5424Option{}, }, { "valid msg with empty fields", - `<13>1 - - - - - - blabla`, expected{ + `<13>1 - - - - - - blabla`, + expected{ Timestamp: time.Now().UTC(), PRI: 13, Message: "blabla", - }, "", []RFC5424Option{}, + }, + "", + []RFC5424Option{}, }, { "valid msg with escaped SD", @@ -167,7 +191,9 @@ func TestParse(t *testing.T) { Hostname: "testhostname", MsgID: `sn="msgid"`, Message: `testmessage`, - }, "", []RFC5424Option{}, + }, + "", + []RFC5424Option{}, }, { "valid complex msg", @@ -179,7 +205,9 @@ func TestParse(t *testing.T) { PRI: 13, MsgID: `sn="msgid"`, Message: `source: sn="www.foobar.com" | message: 1.1.1.1 - - [24/May/2022:10:57:37 +0200] "GET /dist/precache-manifest.58b57debe6bc4f96698da0dc314461e9.js HTTP/2.0" 304 0 "https://www.foobar.com/sw.js" "Mozilla/5.0 (Linux; Android 9; ANE-LX1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.61 Mobile Safari/537.36" "-" "www.foobar.com" sn="www.foobar.com" rt=0.000 ua="-" us="-" ut="-" ul="-" cs=HIT { request: /dist/precache-manifest.58b57debe6bc4f96698da0dc314461e9.js | src_ip_geo_country: DE | MONTH: May | COMMONAPACHELOG: 1.1.1.1 - - [24/May/2022:10:57:37 +0200] "GET /dist/precache-manifest.58b57debe6bc4f96698da0dc314461e9.js HTTP/2.0" 304 0 | auth: - | HOUR: 10 | gl2_remote_ip: 172.31.32.142 | ident: - | gl2_remote_port: 43375 | BASE10NUM: [2.0, 304, 0] | pid: -1 | program: nginx | gl2_source_input: 623ed3440183476d61cff974 | INT: +0200 | is_private_ip: false | YEAR: 2022 | src_ip_geo_city: Achern | clientip: 1.1.1.1 | USERNAME:`, - }, "", []RFC5424Option{}, + }, + "", + []RFC5424Option{}, }, { "partial message", diff --git a/pkg/acquisition/modules/syslog/internal/server/syslogserver.go b/pkg/acquisition/modules/syslog/internal/server/syslogserver.go index 7118c295b54..83f5e5a57e5 100644 --- a/pkg/acquisition/modules/syslog/internal/server/syslogserver.go +++ b/pkg/acquisition/modules/syslog/internal/server/syslogserver.go @@ -25,7 +25,6 @@ type SyslogMessage struct { } func (s *SyslogServer) Listen(listenAddr string, port int) error { - s.listenAddr = listenAddr s.port = port udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", s.listenAddr, s.port)) diff --git a/pkg/alertcontext/alertcontext_test.go b/pkg/alertcontext/alertcontext_test.go index dc752ba8b09..284ff451bc2 100644 --- a/pkg/alertcontext/alertcontext_test.go +++ b/pkg/alertcontext/alertcontext_test.go @@ -239,7 +239,6 @@ func TestValidateContextExpr(t *testing.T) { } func TestAppsecEventToContext(t *testing.T) { - tests := []struct { name string contextToSend map[string][]string diff --git a/pkg/apiclient/auth_jwt.go b/pkg/apiclient/auth_jwt.go index 193486ff065..c43e9fc291c 100644 --- a/pkg/apiclient/auth_jwt.go +++ b/pkg/apiclient/auth_jwt.go @@ -62,7 +62,6 @@ func (t *JWTTransport) refreshJwtToken() error { enc := json.NewEncoder(buf) enc.SetEscapeHTML(false) err = enc.Encode(auth) - if err != nil { return fmt.Errorf("could not encode jwt auth body: %w", err) } @@ -169,7 +168,6 @@ func (t *JWTTransport) prepareRequest(req *http.Request) (*http.Request, error) // RoundTrip implements the RoundTripper interface. func (t *JWTTransport) RoundTrip(req *http.Request) (*http.Response, error) { - var resp *http.Response attemptsCount := make(map[int]int) @@ -229,7 +227,6 @@ func (t *JWTTransport) RoundTrip(req *http.Request) (*http.Response, error) { } } return resp, nil - } func (t *JWTTransport) Client() *http.Client { diff --git a/pkg/apiserver/apic.go b/pkg/apiserver/apic.go index 51a85b1ea23..32847f7489a 100644 --- a/pkg/apiserver/apic.go +++ b/pkg/apiserver/apic.go @@ -332,7 +332,6 @@ func getScenarioTrustOfAlert(alert *models.Alert) string { } func shouldShareAlert(alert *models.Alert, consoleConfig *csconfig.ConsoleConfig, shareSignals bool) bool { - if !shareSignals { log.Debugf("sharing signals is disabled") return false diff --git a/pkg/apiserver/middlewares/v1/api_key.go b/pkg/apiserver/middlewares/v1/api_key.go index 3c154be4fab..df2f68930d6 100644 --- a/pkg/apiserver/middlewares/v1/api_key.go +++ b/pkg/apiserver/middlewares/v1/api_key.go @@ -174,7 +174,6 @@ func (a *APIKey) authPlain(c *gin.Context, logger *log.Entry) *ent.Bouncer { logger.Infof("Creating bouncer %s", bouncerName) bouncer, err = a.DbClient.CreateBouncer(ctx, bouncerName, clientIP, hashStr, types.ApiKeyAuthType, true) - if err != nil { logger.Errorf("while creating bouncer db entry: %s", err) return nil diff --git a/pkg/appsec/appsec.go b/pkg/appsec/appsec.go index 553db205b5d..5f01f76d993 100644 --- a/pkg/appsec/appsec.go +++ b/pkg/appsec/appsec.go @@ -158,7 +158,6 @@ func (wc *AppsecConfig) SetUpLogger() { /* wc.Name is actually the datasource name.*/ wc.Logger = wc.Logger.Dup().WithField("name", wc.Name) wc.Logger.Logger.SetLevel(*wc.LogLevel) - } func (wc *AppsecConfig) LoadByPath(file string) error { diff --git a/pkg/appsec/appsec_rule/appsec_rule.go b/pkg/appsec/appsec_rule/appsec_rule.go index 136d8b11cb7..9d47c0eed5c 100644 --- a/pkg/appsec/appsec_rule/appsec_rule.go +++ b/pkg/appsec/appsec_rule/appsec_rule.go @@ -47,7 +47,6 @@ type CustomRule struct { } func (v *CustomRule) Convert(ruleType string, appsecRuleName string) (string, []uint32, error) { - if v.Zones == nil && v.And == nil && v.Or == nil { return "", nil, errors.New("no zones defined") } diff --git a/pkg/appsec/appsec_rule/modsec_rule_test.go b/pkg/appsec/appsec_rule/modsec_rule_test.go index ffb8a15ff1f..74e9b85426e 100644 --- a/pkg/appsec/appsec_rule/modsec_rule_test.go +++ b/pkg/appsec/appsec_rule/modsec_rule_test.go @@ -88,7 +88,6 @@ func TestVPatchRuleString(t *testing.T) { rule: CustomRule{ And: []CustomRule{ { - Zones: []string{"ARGS"}, Variables: []string{"foo"}, Match: Match{Type: "regex", Value: "[^a-zA-Z]"}, @@ -161,7 +160,6 @@ SecRule ARGS_GET:foo "@rx [^a-zA-Z]" "id:1519945803,phase:2,deny,log,msg:'OR AND for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { actual, _, err := tt.rule.Convert(ModsecurityRuleType, tt.name) - if err != nil { t.Errorf("Error converting rule: %s", err) } diff --git a/pkg/appsec/request_test.go b/pkg/appsec/request_test.go index f8333e4e5f9..8b457e24dab 100644 --- a/pkg/appsec/request_test.go +++ b/pkg/appsec/request_test.go @@ -3,7 +3,6 @@ package appsec import "testing" func TestBodyDumper(t *testing.T) { - tests := []struct { name string req *ParsedRequest @@ -159,7 +158,6 @@ func TestBodyDumper(t *testing.T) { } for idx, test := range tests { - t.Run(test.name, func(t *testing.T) { orig_dr := test.req.DumpRequest() result := test.filter(orig_dr).GetFilteredRequest() @@ -177,5 +175,4 @@ func TestBodyDumper(t *testing.T) { } }) } - } diff --git a/pkg/csplugin/broker.go b/pkg/csplugin/broker.go index e996fa9b68c..f53c831e186 100644 --- a/pkg/csplugin/broker.go +++ b/pkg/csplugin/broker.go @@ -91,7 +91,6 @@ func (pb *PluginBroker) Init(ctx context.Context, pluginCfg *csconfig.PluginCfg, pb.watcher = PluginWatcher{} pb.watcher.Init(pb.pluginConfigByName, pb.alertsByPluginName) return nil - } func (pb *PluginBroker) Kill() { @@ -166,6 +165,7 @@ func (pb *PluginBroker) addProfileAlert(profileAlert ProfileAlert) { pb.watcher.Inserts <- pluginName } } + func (pb *PluginBroker) profilesContainPlugin(pluginName string) bool { for _, profileCfg := range pb.profileConfigs { for _, name := range profileCfg.Notifications { @@ -176,6 +176,7 @@ func (pb *PluginBroker) profilesContainPlugin(pluginName string) bool { } return false } + func (pb *PluginBroker) loadConfig(path string) error { files, err := listFilesAtPath(path) if err != nil { @@ -277,7 +278,6 @@ func (pb *PluginBroker) loadPlugins(ctx context.Context, path string) error { } func (pb *PluginBroker) loadNotificationPlugin(name string, binaryPath string) (protobufs.NotifierServer, error) { - handshake, err := getHandshake() if err != nil { return nil, err diff --git a/pkg/cticlient/types.go b/pkg/cticlient/types.go index 2ad0a6eb34e..954d24641b4 100644 --- a/pkg/cticlient/types.go +++ b/pkg/cticlient/types.go @@ -210,7 +210,6 @@ func (c *SmokeItem) GetFalsePositives() []string { } func (c *SmokeItem) IsFalsePositive() bool { - if c.Classifications.FalsePositives != nil { if len(c.Classifications.FalsePositives) > 0 { return true @@ -284,7 +283,6 @@ func (c *FireItem) GetFalsePositives() []string { } func (c *FireItem) IsFalsePositive() bool { - if c.Classifications.FalsePositives != nil { if len(c.Classifications.FalsePositives) > 0 { return true diff --git a/pkg/exprhelpers/crowdsec_cti.go b/pkg/exprhelpers/crowdsec_cti.go index ccd67b27a49..9b9eac4b95c 100644 --- a/pkg/exprhelpers/crowdsec_cti.go +++ b/pkg/exprhelpers/crowdsec_cti.go @@ -12,16 +12,20 @@ import ( "github.com/crowdsecurity/crowdsec/pkg/types" ) -var CTIUrl = "https://cti.api.crowdsec.net" -var CTIUrlSuffix = "/v2/smoke/" -var CTIApiKey = "" +var ( + CTIUrl = "https://cti.api.crowdsec.net" + CTIUrlSuffix = "/v2/smoke/" + CTIApiKey = "" +) // this is set for non-recoverable errors, such as 403 when querying API or empty API key var CTIApiEnabled = false // when hitting quotas or auth errors, we temporarily disable the API -var CTIBackOffUntil time.Time -var CTIBackOffDuration = 5 * time.Minute +var ( + CTIBackOffUntil time.Time + CTIBackOffDuration = 5 * time.Minute +) var ctiClient *cticlient.CrowdsecCTIClient @@ -62,8 +66,10 @@ func ShutdownCrowdsecCTI() { } // Cache for responses -var CTICache gcache.Cache -var CacheExpiration time.Duration +var ( + CTICache gcache.Cache + CacheExpiration time.Duration +) func CrowdsecCTIInitCache(size int, ttl time.Duration) { CTICache = gcache.New(size).LRU().Build() diff --git a/pkg/exprhelpers/geoip.go b/pkg/exprhelpers/geoip.go index fb0c344d884..6d8813dc0ad 100644 --- a/pkg/exprhelpers/geoip.go +++ b/pkg/exprhelpers/geoip.go @@ -14,7 +14,6 @@ func GeoIPEnrich(params ...any) (any, error) { parsedIP := net.ParseIP(ip) city, err := geoIPCityReader.City(parsedIP) - if err != nil { return nil, err } @@ -31,7 +30,6 @@ func GeoIPASNEnrich(params ...any) (any, error) { parsedIP := net.ParseIP(ip) asn, err := geoIPASNReader.ASN(parsedIP) - if err != nil { return nil, err } @@ -50,7 +48,6 @@ func GeoIPRangeEnrich(params ...any) (any, error) { parsedIP := net.ParseIP(ip) rangeIP, ok, err := geoIPRangeReader.LookupNetwork(parsedIP, &dummy) - if err != nil { return nil, err } diff --git a/pkg/fflag/crowdsec.go b/pkg/fflag/crowdsec.go index d42d6a05ef6..ea397bfe5bc 100644 --- a/pkg/fflag/crowdsec.go +++ b/pkg/fflag/crowdsec.go @@ -2,12 +2,14 @@ package fflag var Crowdsec = FeatureRegister{EnvPrefix: "CROWDSEC_FEATURE_"} -var CscliSetup = &Feature{Name: "cscli_setup", Description: "Enable cscli setup command (service detection)"} -var DisableHttpRetryBackoff = &Feature{Name: "disable_http_retry_backoff", Description: "Disable http retry backoff"} -var ChunkedDecisionsStream = &Feature{Name: "chunked_decisions_stream", Description: "Enable chunked decisions stream"} -var PapiClient = &Feature{Name: "papi_client", Description: "Enable Polling API client", State: DeprecatedState} -var Re2GrokSupport = &Feature{Name: "re2_grok_support", Description: "Enable RE2 support for GROK patterns"} -var Re2RegexpInfileSupport = &Feature{Name: "re2_regexp_in_file_support", Description: "Enable RE2 support for RegexpInFile expr helper"} +var ( + CscliSetup = &Feature{Name: "cscli_setup", Description: "Enable cscli setup command (service detection)"} + DisableHttpRetryBackoff = &Feature{Name: "disable_http_retry_backoff", Description: "Disable http retry backoff"} + ChunkedDecisionsStream = &Feature{Name: "chunked_decisions_stream", Description: "Enable chunked decisions stream"} + PapiClient = &Feature{Name: "papi_client", Description: "Enable Polling API client", State: DeprecatedState} + Re2GrokSupport = &Feature{Name: "re2_grok_support", Description: "Enable RE2 support for GROK patterns"} + Re2RegexpInfileSupport = &Feature{Name: "re2_regexp_in_file_support", Description: "Enable RE2 support for RegexpInFile expr helper"} +) func RegisterAllFeatures() error { err := Crowdsec.RegisterFeature(CscliSetup) diff --git a/pkg/leakybucket/blackhole.go b/pkg/leakybucket/blackhole.go index b12f169acd9..bda2e7c9ed1 100644 --- a/pkg/leakybucket/blackhole.go +++ b/pkg/leakybucket/blackhole.go @@ -49,7 +49,6 @@ func (bl *Blackhole) OnBucketOverflow(bucketFactory *BucketFactory) func(*Leaky, tmp = append(tmp, element) } else { leaky.logger.Debugf("%s left blackhole %s ago", element.key, leaky.Ovflw_ts.Sub(element.expiration)) - } } bl.hiddenKeys = tmp @@ -64,5 +63,4 @@ func (bl *Blackhole) OnBucketOverflow(bucketFactory *BucketFactory) func(*Leaky, leaky.logger.Debugf("Adding overflow to blackhole (%s)", leaky.First_ts) return alert, queue } - } diff --git a/pkg/leakybucket/bucket.go b/pkg/leakybucket/bucket.go index e981551af8f..bc81a505925 100644 --- a/pkg/leakybucket/bucket.go +++ b/pkg/leakybucket/bucket.go @@ -204,7 +204,6 @@ func FromFactory(bucketFactory BucketFactory) *Leaky { /* for now mimic a leak routine */ //LeakRoutine us the life of a bucket. It dies when the bucket underflows or overflows func LeakRoutine(leaky *Leaky) error { - var ( durationTickerChan = make(<-chan time.Time) durationTicker *time.Ticker diff --git a/pkg/leakybucket/buckets.go b/pkg/leakybucket/buckets.go index cfe8d7c302e..72948da1ad7 100644 --- a/pkg/leakybucket/buckets.go +++ b/pkg/leakybucket/buckets.go @@ -25,5 +25,4 @@ func NewBuckets() *Buckets { func GetKey(bucketCfg BucketFactory, stackkey string) string { return fmt.Sprintf("%x", sha1.Sum([]byte(bucketCfg.Filter+stackkey+bucketCfg.Name))) - } diff --git a/pkg/leakybucket/conditional.go b/pkg/leakybucket/conditional.go index a203a639743..b3a84b07c21 100644 --- a/pkg/leakybucket/conditional.go +++ b/pkg/leakybucket/conditional.go @@ -11,8 +11,10 @@ import ( "github.com/crowdsecurity/crowdsec/pkg/types" ) -var conditionalExprCache map[string]vm.Program -var conditionalExprCacheLock sync.Mutex +var ( + conditionalExprCache map[string]vm.Program + conditionalExprCacheLock sync.Mutex +) type ConditionalOverflow struct { ConditionalFilter string diff --git a/pkg/leakybucket/manager_load_test.go b/pkg/leakybucket/manager_load_test.go index 513f11ff373..9d207da164e 100644 --- a/pkg/leakybucket/manager_load_test.go +++ b/pkg/leakybucket/manager_load_test.go @@ -51,93 +51,86 @@ func TestBadBucketsConfig(t *testing.T) { } func TestLeakyBucketsConfig(t *testing.T) { - var CfgTests = []cfgTest{ - //leaky with bad capacity + CfgTests := []cfgTest{ + // leaky with bad capacity {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 0}, false, false}, - //leaky with empty leakspeed + // leaky with empty leakspeed {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1}, false, false}, - //leaky with missing filter + // leaky with missing filter {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "1s"}, false, true}, - //leaky with invalid leakspeed + // leaky with invalid leakspeed {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "abs", Filter: "true"}, false, false}, - //leaky with valid filter + // leaky with valid filter {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "1s", Filter: "true"}, true, true}, - //leaky with invalid filter + // leaky with invalid filter {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "1s", Filter: "xu"}, false, true}, - //leaky with valid filter + // leaky with valid filter {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "1s", Filter: "true"}, true, true}, - //leaky with bad overflow filter + // leaky with bad overflow filter {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "1s", Filter: "true", OverflowFilter: "xu"}, false, true}, } if err := runTest(CfgTests); err != nil { t.Fatalf("%s", err) } - } func TestBlackholeConfig(t *testing.T) { - var CfgTests = []cfgTest{ - //basic bh + CfgTests := []cfgTest{ + // basic bh {BucketFactory{Name: "test", Description: "test1", Type: "trigger", Filter: "true", Blackhole: "15s"}, true, true}, - //bad bh + // bad bh {BucketFactory{Name: "test", Description: "test1", Type: "trigger", Filter: "true", Blackhole: "abc"}, false, true}, } if err := runTest(CfgTests); err != nil { t.Fatalf("%s", err) } - } func TestTriggerBucketsConfig(t *testing.T) { - var CfgTests = []cfgTest{ - //basic valid counter + CfgTests := []cfgTest{ + // basic valid counter {BucketFactory{Name: "test", Description: "test1", Type: "trigger", Filter: "true"}, true, true}, } if err := runTest(CfgTests); err != nil { t.Fatalf("%s", err) } - } func TestCounterBucketsConfig(t *testing.T) { - var CfgTests = []cfgTest{ - - //basic valid counter + CfgTests := []cfgTest{ + // basic valid counter {BucketFactory{Name: "test", Description: "test1", Type: "counter", Capacity: -1, Duration: "5s", Filter: "true"}, true, true}, - //missing duration + // missing duration {BucketFactory{Name: "test", Description: "test1", Type: "counter", Capacity: -1, Filter: "true"}, false, false}, - //bad duration + // bad duration {BucketFactory{Name: "test", Description: "test1", Type: "counter", Capacity: -1, Duration: "abc", Filter: "true"}, false, false}, - //capacity must be -1 + // capacity must be -1 {BucketFactory{Name: "test", Description: "test1", Type: "counter", Capacity: 0, Duration: "5s", Filter: "true"}, false, false}, } if err := runTest(CfgTests); err != nil { t.Fatalf("%s", err) } - } func TestBayesianBucketsConfig(t *testing.T) { - var CfgTests = []cfgTest{ - - //basic valid counter + CfgTests := []cfgTest{ + // basic valid counter {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: -1, Filter: "true", BayesianPrior: 0.5, BayesianThreshold: 0.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, true, true}, - //bad capacity + // bad capacity {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: 1, Filter: "true", BayesianPrior: 0.5, BayesianThreshold: 0.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, false, false}, - //missing prior + // missing prior {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: -1, Filter: "true", BayesianThreshold: 0.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, false, false}, - //missing threshold + // missing threshold {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: -1, Filter: "true", BayesianPrior: 0.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, false, false}, - //bad prior + // bad prior {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: -1, Filter: "true", BayesianPrior: 1.5, BayesianThreshold: 0.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, false, false}, - //bad threshold + // bad threshold {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: -1, Filter: "true", BayesianPrior: 0.5, BayesianThreshold: 1.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, false, false}, } if err := runTest(CfgTests); err != nil { t.Fatalf("%s", err) } - } diff --git a/pkg/leakybucket/manager_run.go b/pkg/leakybucket/manager_run.go index 2858d8b5635..e6712e6e47e 100644 --- a/pkg/leakybucket/manager_run.go +++ b/pkg/leakybucket/manager_run.go @@ -17,9 +17,11 @@ import ( "github.com/crowdsecurity/crowdsec/pkg/types" ) -var serialized map[string]Leaky -var BucketPourCache map[string][]types.Event -var BucketPourTrack bool +var ( + serialized map[string]Leaky + BucketPourCache map[string][]types.Event + BucketPourTrack bool +) /* The leaky routines lifecycle are based on "real" time. @@ -243,7 +245,6 @@ func PourItemToBucket(bucket *Leaky, holder BucketFactory, buckets *Buckets, par } func LoadOrStoreBucketFromHolder(partitionKey string, buckets *Buckets, holder BucketFactory, expectMode int) (*Leaky, error) { - biface, ok := buckets.Bucket_map.Load(partitionKey) /* the bucket doesn't exist, create it !*/ @@ -283,9 +284,7 @@ func LoadOrStoreBucketFromHolder(partitionKey string, buckets *Buckets, holder B var orderEvent map[string]*sync.WaitGroup func PourItemToHolders(parsed types.Event, holders []BucketFactory, buckets *Buckets) (bool, error) { - var ( - ok, condition, poured bool - ) + var ok, condition, poured bool if BucketPourTrack { if BucketPourCache == nil { diff --git a/pkg/leakybucket/processor.go b/pkg/leakybucket/processor.go index 81af3000c1c..dc5330a612e 100644 --- a/pkg/leakybucket/processor.go +++ b/pkg/leakybucket/processor.go @@ -10,8 +10,7 @@ type Processor interface { AfterBucketPour(Bucket *BucketFactory) func(types.Event, *Leaky) *types.Event } -type DumbProcessor struct { -} +type DumbProcessor struct{} func (d *DumbProcessor) OnBucketInit(bucketFactory *BucketFactory) error { return nil diff --git a/pkg/leakybucket/reset_filter.go b/pkg/leakybucket/reset_filter.go index 452ccc085b1..3b9b876aff4 100644 --- a/pkg/leakybucket/reset_filter.go +++ b/pkg/leakybucket/reset_filter.go @@ -23,10 +23,12 @@ type CancelOnFilter struct { Debug bool } -var cancelExprCacheLock sync.Mutex -var cancelExprCache map[string]struct { - CancelOnFilter *vm.Program -} +var ( + cancelExprCacheLock sync.Mutex + cancelExprCache map[string]struct { + CancelOnFilter *vm.Program + } +) func (u *CancelOnFilter) OnBucketPour(bucketFactory *BucketFactory) func(types.Event, *Leaky) *types.Event { return func(msg types.Event, leaky *Leaky) *types.Event { diff --git a/pkg/leakybucket/uniq.go b/pkg/leakybucket/uniq.go index 0cc0583390b..3a4683ae309 100644 --- a/pkg/leakybucket/uniq.go +++ b/pkg/leakybucket/uniq.go @@ -16,8 +16,10 @@ import ( // on overflow // on leak -var uniqExprCache map[string]vm.Program -var uniqExprCacheLock sync.Mutex +var ( + uniqExprCache map[string]vm.Program + uniqExprCacheLock sync.Mutex +) type Uniq struct { DistinctCompiled *vm.Program diff --git a/pkg/parser/enrich.go b/pkg/parser/enrich.go index 661410d20d3..a69cd963813 100644 --- a/pkg/parser/enrich.go +++ b/pkg/parser/enrich.go @@ -7,8 +7,10 @@ import ( ) /* should be part of a package shared with enrich/geoip.go */ -type EnrichFunc func(string, *types.Event, *log.Entry) (map[string]string, error) -type InitFunc func(map[string]string) (interface{}, error) +type ( + EnrichFunc func(string, *types.Event, *log.Entry) (map[string]string, error) + InitFunc func(map[string]string) (interface{}, error) +) type EnricherCtx struct { Registered map[string]*Enricher diff --git a/pkg/parser/enrich_geoip.go b/pkg/parser/enrich_geoip.go index 1756927bc4b..79a70077283 100644 --- a/pkg/parser/enrich_geoip.go +++ b/pkg/parser/enrich_geoip.go @@ -18,7 +18,6 @@ func IpToRange(field string, p *types.Event, plog *log.Entry) (map[string]string } r, err := exprhelpers.GeoIPRangeEnrich(field) - if err != nil { plog.Errorf("Unable to enrich ip '%s'", field) return nil, nil //nolint:nilerr @@ -47,7 +46,6 @@ func GeoIpASN(field string, p *types.Event, plog *log.Entry) (map[string]string, } r, err := exprhelpers.GeoIPASNEnrich(field) - if err != nil { plog.Debugf("Unable to enrich ip '%s'", field) return nil, nil //nolint:nilerr @@ -81,7 +79,6 @@ func GeoIpCity(field string, p *types.Event, plog *log.Entry) (map[string]string } r, err := exprhelpers.GeoIPEnrich(field) - if err != nil { plog.Debugf("Unable to enrich ip '%s'", field) return nil, nil //nolint:nilerr diff --git a/pkg/parser/parsing_test.go b/pkg/parser/parsing_test.go index 269d51a1ba2..5f6f924e7df 100644 --- a/pkg/parser/parsing_test.go +++ b/pkg/parser/parsing_test.go @@ -151,7 +151,7 @@ func testOneParser(pctx *UnixParserCtx, ectx EnricherCtx, dir string, b *testing b.ResetTimer() } - for range(count) { + for range count { if !testFile(tests, *pctx, pnodes) { return errors.New("test failed") } @@ -285,7 +285,7 @@ func matchEvent(expected types.Event, out types.Event, debug bool) ([]string, bo valid = true - for mapIdx := range(len(expectMaps)) { + for mapIdx := range len(expectMaps) { for expKey, expVal := range expectMaps[mapIdx] { outVal, ok := outMaps[mapIdx][expKey] if !ok { diff --git a/pkg/parser/runtime.go b/pkg/parser/runtime.go index 8068690b68f..db7f467615a 100644 --- a/pkg/parser/runtime.go +++ b/pkg/parser/runtime.go @@ -248,14 +248,18 @@ func stageidx(stage string, stages []string) int { return -1 } -var ParseDump bool -var DumpFolder string +var ( + ParseDump bool + DumpFolder string +) -var StageParseCache dumps.ParserResults -var StageParseMutex sync.Mutex +var ( + StageParseCache dumps.ParserResults + StageParseMutex sync.Mutex +) func Parse(ctx UnixParserCtx, xp types.Event, nodes []Node) (types.Event, error) { - var event = xp + event := xp /* the stage is undefined, probably line is freshly acquired, set to first stage !*/ if event.Stage == "" && len(ctx.Stages) > 0 { diff --git a/pkg/types/appsec_event.go b/pkg/types/appsec_event.go index 11d70ad368d..54163f53fef 100644 --- a/pkg/types/appsec_event.go +++ b/pkg/types/appsec_event.go @@ -60,7 +60,6 @@ func (w AppsecEvent) GetVar(varName string) string { } log.Infof("var %s not found. Available variables: %+v", varName, w.Vars) return "" - } // getters diff --git a/pkg/types/constants.go b/pkg/types/constants.go index acb5b5bfacf..2421b076b97 100644 --- a/pkg/types/constants.go +++ b/pkg/types/constants.go @@ -1,23 +1,29 @@ package types -const ApiKeyAuthType = "api-key" -const TlsAuthType = "tls" -const PasswordAuthType = "password" +const ( + ApiKeyAuthType = "api-key" + TlsAuthType = "tls" + PasswordAuthType = "password" +) -const PAPIBaseURL = "https://papi.api.crowdsec.net/" -const PAPIVersion = "v1" -const PAPIPollUrl = "/decisions/stream/poll" -const PAPIPermissionsUrl = "/permissions" +const ( + PAPIBaseURL = "https://papi.api.crowdsec.net/" + PAPIVersion = "v1" + PAPIPollUrl = "/decisions/stream/poll" + PAPIPermissionsUrl = "/permissions" +) const CAPIBaseURL = "https://api.crowdsec.net/" -const CscliOrigin = "cscli" -const CrowdSecOrigin = "crowdsec" -const ConsoleOrigin = "console" -const CscliImportOrigin = "cscli-import" -const ListOrigin = "lists" -const CAPIOrigin = "CAPI" -const CommunityBlocklistPullSourceScope = "crowdsecurity/community-blocklist" +const ( + CscliOrigin = "cscli" + CrowdSecOrigin = "crowdsec" + ConsoleOrigin = "console" + CscliImportOrigin = "cscli-import" + ListOrigin = "lists" + CAPIOrigin = "CAPI" + CommunityBlocklistPullSourceScope = "crowdsecurity/community-blocklist" +) const DecisionTypeBan = "ban" diff --git a/pkg/types/event_test.go b/pkg/types/event_test.go index 97b13f96d9a..638e42fe757 100644 --- a/pkg/types/event_test.go +++ b/pkg/types/event_test.go @@ -46,7 +46,6 @@ func TestSetParsed(t *testing.T) { assert.Equal(t, tt.value, tt.evt.Parsed[tt.key]) }) } - } func TestSetMeta(t *testing.T) { @@ -86,7 +85,6 @@ func TestSetMeta(t *testing.T) { assert.Equal(t, tt.value, tt.evt.GetMeta(tt.key)) }) } - } func TestParseIPSources(t *testing.T) { diff --git a/pkg/types/getfstype.go b/pkg/types/getfstype.go index 728e986bed0..c16fe86ec9c 100644 --- a/pkg/types/getfstype.go +++ b/pkg/types/getfstype.go @@ -100,7 +100,6 @@ func GetFSType(path string) (string, error) { var buf unix.Statfs_t err := unix.Statfs(path, &buf) - if err != nil { return "", err } diff --git a/pkg/types/ip.go b/pkg/types/ip.go index 9d08afd8809..47fb3fc83a5 100644 --- a/pkg/types/ip.go +++ b/pkg/types/ip.go @@ -23,7 +23,8 @@ func LastAddress(n net.IPNet) net.IP { ip[6] | ^n.Mask[6], ip[7] | ^n.Mask[7], ip[8] | ^n.Mask[8], ip[9] | ^n.Mask[9], ip[10] | ^n.Mask[10], ip[11] | ^n.Mask[11], ip[12] | ^n.Mask[12], ip[13] | ^n.Mask[13], ip[14] | ^n.Mask[14], - ip[15] | ^n.Mask[15]} + ip[15] | ^n.Mask[15], + } } return net.IPv4( diff --git a/pkg/types/ip_test.go b/pkg/types/ip_test.go index f8c14b12e3c..b9298ba487f 100644 --- a/pkg/types/ip_test.go +++ b/pkg/types/ip_test.go @@ -8,21 +8,20 @@ import ( ) func TestIP2Int(t *testing.T) { - tEmpty := net.IP{} _, _, _, err := IP2Ints(tEmpty) if !strings.Contains(err.Error(), "unexpected len 0 for ") { t.Fatalf("unexpected: %s", err) } } + func TestRange2Int(t *testing.T) { tEmpty := net.IPNet{} - //empty item + // empty item _, _, _, _, _, err := Range2Ints(tEmpty) if !strings.Contains(err.Error(), "converting first ip in range") { t.Fatalf("unexpected: %s", err) } - } func TestAdd2Int(t *testing.T) { diff --git a/pkg/types/utils.go b/pkg/types/utils.go index 712d44ba12d..3e1ae4f7547 100644 --- a/pkg/types/utils.go +++ b/pkg/types/utils.go @@ -10,9 +10,11 @@ import ( "gopkg.in/natefinch/lumberjack.v2" ) -var logFormatter log.Formatter -var LogOutput *lumberjack.Logger //io.Writer -var logLevel log.Level +var ( + logFormatter log.Formatter + LogOutput *lumberjack.Logger // io.Writer + logLevel log.Level +) func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool, forceColors bool) error { /*Configure logs*/