Skip to content

Commit

Permalink
lint: style, autofix (#3354)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Dec 5, 2024
1 parent bbe7752 commit 7a1ad83
Show file tree
Hide file tree
Showing 48 changed files with 177 additions and 169 deletions.
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/crowdsec-cli/setup.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//go:build !no_cscli_setup

package main

import (
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec/appsec.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !no_datasource_appsec
//go:build !no_datasource_appsec

package main

Expand Down
2 changes: 1 addition & 1 deletion cmd/notification-email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions pkg/acquisition/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions pkg/acquisition/modules/appsec/appsec_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -403,7 +402,6 @@ func TestAppsecPreEvalHooks(t *testing.T) {

require.Len(t, responses, 1)
require.True(t, responses[0].InBandInterrupt)

},
},
{
Expand Down Expand Up @@ -670,7 +668,6 @@ func TestAppsecPreEvalHooks(t *testing.T) {
}

func TestAppsecRemediationConfigHooks(t *testing.T) {

tests := []appsecRuleTest{
{
name: "Basic matching rule",
Expand Down Expand Up @@ -759,6 +756,7 @@ func TestAppsecRemediationConfigHooks(t *testing.T) {
})
}
}

func TestOnMatchRemediationHooks(t *testing.T) {
tests := []appsecRuleTest{
{
Expand Down
2 changes: 0 additions & 2 deletions pkg/acquisition/modules/appsec/appsec_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions pkg/acquisition/modules/appsec/appsec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions pkg/acquisition/modules/appsec/bodyprocessors/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
"github.com/crowdsecurity/coraza/v3/experimental/plugins/plugintypes"
)

type rawBodyProcessor struct {
}
type rawBodyProcessor struct{}

type setterInterface interface {
Set(string)
Expand All @@ -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() {
Expand Down
4 changes: 1 addition & 3 deletions pkg/acquisition/modules/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/types"
)

var (
dataSourceName = "http"
)
var dataSourceName = "http"

var linesRead = prometheus.NewCounterVec(
prometheus.CounterOpts{
Expand Down
3 changes: 1 addition & 2 deletions pkg/acquisition/modules/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -257,7 +257,6 @@ basic_auth:
h.Server.Close()
tomb.Kill(nil)
tomb.Wait()

}

func TestStreamingAcquisitionUnknownPath(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func WithStrictHostname() RFC3164Option {
}

func (r *RFC3164) parsePRI() error {

pri := 0

if r.buf[r.position] != '<' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func WithStrictHostname() RFC5424Option {
}

func (r *RFC5424) parsePRI() error {

pri := 0

if r.buf[r.position] != '<' {
Expand Down Expand Up @@ -94,7 +93,6 @@ func (r *RFC5424) parseVersion() error {
}

func (r *RFC5424) parseTimestamp() error {

timestamp := []byte{}

if r.buf[r.position] == NIL_VALUE {
Expand All @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,68 +94,92 @@ 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",
PID: "49340",
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",
PID: "49340",
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",
PID: "49340",
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",
Expand All @@ -167,7 +191,9 @@ func TestParse(t *testing.T) {
Hostname: "testhostname",
MsgID: `sn="msgid"`,
Message: `testmessage`,
}, "", []RFC5424Option{},
},
"",
[]RFC5424Option{},
},
{
"valid complex msg",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 0 additions & 1 deletion pkg/alertcontext/alertcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ func TestValidateContextExpr(t *testing.T) {
}

func TestAppsecEventToContext(t *testing.T) {

tests := []struct {
name string
contextToSend map[string][]string
Expand Down
3 changes: 0 additions & 3 deletions pkg/apiclient/auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -229,7 +227,6 @@ func (t *JWTTransport) RoundTrip(req *http.Request) (*http.Response, error) {
}
}
return resp, nil

}

func (t *JWTTransport) Client() *http.Client {
Expand Down
1 change: 0 additions & 1 deletion pkg/apiserver/apic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion pkg/apiserver/middlewares/v1/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 7a1ad83

Please sign in to comment.