Skip to content

Commit 206211c

Browse files
authored
lint: import statement order (#3085)
* lint: import statement order * lint
1 parent d4e6b75 commit 206211c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+151
-154
lines changed

cmd/crowdsec/lapiclient.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010

1111
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
1212
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
13-
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
1413
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
14+
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
1515
"github.com/crowdsecurity/crowdsec/pkg/models"
1616
)
1717

cmd/notification-file/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import (
1111
"sync"
1212
"time"
1313

14-
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
1514
"github.com/hashicorp/go-hclog"
1615
plugin "github.com/hashicorp/go-plugin"
1716
"gopkg.in/yaml.v3"
17+
18+
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
1819
)
1920

2021
var (
@@ -69,7 +70,7 @@ func (r *LogRotate) rotateLogs(cfg PluginConfig) {
6970
}
7071
// Reopen the FileWriter
7172
FileWriter.Close()
72-
FileWriter, err = os.OpenFile(cfg.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
73+
FileWriter, err = os.OpenFile(cfg.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
7374
if err != nil {
7475
logger.Error("Failed to reopen log file", "error", err)
7576
}
@@ -173,7 +174,7 @@ func WriteToFileWithCtx(ctx context.Context, cfg PluginConfig, log string) error
173174
// The file has been rotated outside our control
174175
logger.Info("Log file has been rotated or missing attempting to reopen it")
175176
FileWriter.Close()
176-
FileWriter, err = os.OpenFile(cfg.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
177+
FileWriter, err = os.OpenFile(cfg.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
177178
if err != nil {
178179
return err
179180
}
@@ -213,7 +214,7 @@ func (s *FilePlugin) Configure(ctx context.Context, config *protobufs.Config) (*
213214
return &protobufs.Empty{}, err
214215
}
215216
FileWriteMutex = &sync.Mutex{}
216-
FileWriter, err = os.OpenFile(d.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
217+
FileWriter, err = os.OpenFile(d.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
217218
if err != nil {
218219
logger.Error("Failed to open log file", "error", err)
219220
return &protobufs.Empty{}, err
@@ -230,7 +231,7 @@ func (s *FilePlugin) Configure(ctx context.Context, config *protobufs.Config) (*
230231
}
231232

232233
func main() {
233-
var handshake = plugin.HandshakeConfig{
234+
handshake := plugin.HandshakeConfig{
234235
ProtocolVersion: 1,
235236
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
236237
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),

cmd/notification-sentinel/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import (
1111
"strings"
1212
"time"
1313

14-
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
1514
"github.com/hashicorp/go-hclog"
1615
"github.com/hashicorp/go-plugin"
1716
"gopkg.in/yaml.v3"
17+
18+
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
1819
)
1920

2021
type PluginConfig struct {
@@ -54,7 +55,6 @@ func (s *SentinelPlugin) getAuthorizationHeader(now string, length int, pluginNa
5455
}
5556

5657
func (s *SentinelPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
57-
5858
if _, ok := s.PluginConfigByName[notification.Name]; !ok {
5959
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
6060
}
@@ -73,7 +73,6 @@ func (s *SentinelPlugin) Notify(ctx context.Context, notification *protobufs.Not
7373
now := time.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05 GMT")
7474

7575
authorization, err := s.getAuthorizationHeader(now, len(notification.Text), notification.Name)
76-
7776
if err != nil {
7877
return &protobufs.Empty{}, err
7978
}
@@ -113,7 +112,7 @@ func (s *SentinelPlugin) Configure(ctx context.Context, config *protobufs.Config
113112
}
114113

115114
func main() {
116-
var handshake = plugin.HandshakeConfig{
115+
handshake := plugin.HandshakeConfig{
117116
ProtocolVersion: 1,
118117
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
119118
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),

pkg/acquisition/acquisition.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ import (
3030
s3acquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/s3"
3131
syslogacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/syslog"
3232
wineventlogacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/wineventlog"
33-
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
34-
3533
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
34+
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
3635
"github.com/crowdsecurity/crowdsec/pkg/types"
3736
)
3837

@@ -124,7 +123,6 @@ func DataSourceConfigure(commonConfig configuration.DataSourceCommonCfg, metrics
124123
/* configure the actual datasource */
125124
if err := dataSrc.Configure(yamlConfig, subLogger, metricsLevel); err != nil {
126125
return nil, fmt.Errorf("failed to configure datasource %s: %w", commonConfig.Source, err)
127-
128126
}
129127
return &dataSrc, nil
130128
}
@@ -181,7 +179,6 @@ func LoadAcquisitionFromDSN(dsn string, labels map[string]string, transformExpr
181179
func GetMetricsLevelFromPromCfg(prom *csconfig.PrometheusCfg) int {
182180
if prom == nil {
183181
return configuration.METRICS_FULL
184-
185182
}
186183
if !prom.Enabled {
187184
return configuration.METRICS_NONE
@@ -194,7 +191,6 @@ func GetMetricsLevelFromPromCfg(prom *csconfig.PrometheusCfg) int {
194191
return configuration.METRICS_FULL
195192
}
196193
return configuration.METRICS_FULL
197-
198194
}
199195

200196
// LoadAcquisitionFromFile unmarshals the configuration item and checks its availability

pkg/acquisition/modules/appsec/appsec.go

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@ package appsecacquisition
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"net"
89
"net/http"
910
"os"
1011
"sync"
1112
"time"
1213

13-
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
14-
15-
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
16-
"github.com/crowdsecurity/crowdsec/pkg/appsec"
17-
"github.com/crowdsecurity/crowdsec/pkg/types"
18-
"github.com/crowdsecurity/go-cs-lib/trace"
1914
"github.com/google/uuid"
2015
"github.com/prometheus/client_golang/prometheus"
2116
log "github.com/sirupsen/logrus"
2217
"gopkg.in/tomb.v2"
2318
"gopkg.in/yaml.v2"
19+
20+
"github.com/crowdsecurity/go-cs-lib/trace"
21+
22+
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
23+
"github.com/crowdsecurity/crowdsec/pkg/appsec"
24+
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
25+
"github.com/crowdsecurity/crowdsec/pkg/types"
2426
)
2527

2628
const (
2729
InBand = "inband"
2830
OutOfBand = "outofband"
2931
)
3032

31-
var (
32-
DefaultAuthCacheDuration = (1 * time.Minute)
33-
)
33+
var DefaultAuthCacheDuration = (1 * time.Minute)
3434

3535
// configuration structure of the acquis for the application security engine
3636
type AppsecSourceConfig struct {
@@ -94,10 +94,9 @@ type BodyResponse struct {
9494
}
9595

9696
func (w *AppsecSource) UnmarshalConfig(yamlConfig []byte) error {
97-
9897
err := yaml.UnmarshalStrict(yamlConfig, &w.config)
9998
if err != nil {
100-
return fmt.Errorf("Cannot parse appsec configuration: %w", err)
99+
return fmt.Errorf("cannot parse appsec configuration: %w", err)
101100
}
102101

103102
if w.config.ListenAddr == "" && w.config.ListenSocket == "" {
@@ -122,7 +121,7 @@ func (w *AppsecSource) UnmarshalConfig(yamlConfig []byte) error {
122121
}
123122

124123
if w.config.AppsecConfig == "" && w.config.AppsecConfigPath == "" {
125-
return fmt.Errorf("appsec_config or appsec_config_path must be set")
124+
return errors.New("appsec_config or appsec_config_path must be set")
126125
}
127126

128127
if w.config.Name == "" {
@@ -177,26 +176,25 @@ func (w *AppsecSource) Configure(yamlConfig []byte, logger *log.Entry, MetricsLe
177176
if w.config.AppsecConfigPath != "" {
178177
err := appsecCfg.LoadByPath(w.config.AppsecConfigPath)
179178
if err != nil {
180-
return fmt.Errorf("unable to load appsec_config : %s", err)
179+
return fmt.Errorf("unable to load appsec_config: %w", err)
181180
}
182181
} else if w.config.AppsecConfig != "" {
183182
err := appsecCfg.Load(w.config.AppsecConfig)
184183
if err != nil {
185-
return fmt.Errorf("unable to load appsec_config : %s", err)
184+
return fmt.Errorf("unable to load appsec_config: %w", err)
186185
}
187186
} else {
188-
return fmt.Errorf("no appsec_config provided")
187+
return errors.New("no appsec_config provided")
189188
}
190189

191190
w.AppsecRuntime, err = appsecCfg.Build()
192191
if err != nil {
193-
return fmt.Errorf("unable to build appsec_config : %s", err)
192+
return fmt.Errorf("unable to build appsec_config: %w", err)
194193
}
195194

196195
err = w.AppsecRuntime.ProcessOnLoadRules()
197-
198196
if err != nil {
199-
return fmt.Errorf("unable to process on load rules : %s", err)
197+
return fmt.Errorf("unable to process on load rules: %w", err)
200198
}
201199

202200
w.AppsecRunners = make([]AppsecRunner, w.config.Routines)
@@ -207,15 +205,15 @@ func (w *AppsecSource) Configure(yamlConfig []byte, logger *log.Entry, MetricsLe
207205
wrt := *w.AppsecRuntime
208206
wrt.Logger = w.logger.Dup().WithField("runner_uuid", appsecRunnerUUID)
209207
runner := AppsecRunner{
210-
inChan: w.InChan,
211-
UUID: appsecRunnerUUID,
212-
logger: w.logger.WithField("runner_uuid", appsecRunnerUUID),
208+
inChan: w.InChan,
209+
UUID: appsecRunnerUUID,
210+
logger: w.logger.WithField("runner_uuid", appsecRunnerUUID),
213211
AppsecRuntime: &wrt,
214212
Labels: w.config.Labels,
215213
}
216214
err := runner.Init(appsecCfg.GetDataDir())
217215
if err != nil {
218-
return fmt.Errorf("unable to initialize runner : %s", err)
216+
return fmt.Errorf("unable to initialize runner: %w", err)
219217
}
220218
w.AppsecRunners[nbRoutine] = runner
221219
}
@@ -228,7 +226,7 @@ func (w *AppsecSource) Configure(yamlConfig []byte, logger *log.Entry, MetricsLe
228226
}
229227

230228
func (w *AppsecSource) ConfigureByDSN(dsn string, labels map[string]string, logger *log.Entry, uuid string) error {
231-
return fmt.Errorf("AppSec datasource does not support command line acquisition")
229+
return errors.New("AppSec datasource does not support command line acquisition")
232230
}
233231

234232
func (w *AppsecSource) GetMode() string {
@@ -240,7 +238,7 @@ func (w *AppsecSource) GetName() string {
240238
}
241239

242240
func (w *AppsecSource) OneShotAcquisition(out chan types.Event, t *tomb.Tomb) error {
243-
return fmt.Errorf("AppSec datasource does not support command line acquisition")
241+
return errors.New("AppSec datasource does not support command line acquisition")
244242
}
245243

246244
func (w *AppsecSource) StreamingAcquisition(out chan types.Event, t *tomb.Tomb) error {
@@ -262,7 +260,7 @@ func (w *AppsecSource) StreamingAcquisition(out chan types.Event, t *tomb.Tomb)
262260
_ = os.RemoveAll(w.config.ListenSocket)
263261
listener, err := net.Listen("unix", w.config.ListenSocket)
264262
if err != nil {
265-
return fmt.Errorf("Appsec server failed: %w", err)
263+
return fmt.Errorf("appsec server failed: %w", err)
266264
}
267265
defer listener.Close()
268266
if w.config.CertFilePath != "" && w.config.KeyFilePath != "" {
@@ -271,7 +269,7 @@ func (w *AppsecSource) StreamingAcquisition(out chan types.Event, t *tomb.Tomb)
271269
err = w.server.Serve(listener)
272270
}
273271
if err != nil && err != http.ErrServerClosed {
274-
return fmt.Errorf("Appsec server failed: %w", err)
272+
return fmt.Errorf("appsec server failed: %w", err)
275273
}
276274
}
277275
return nil
@@ -287,7 +285,7 @@ func (w *AppsecSource) StreamingAcquisition(out chan types.Event, t *tomb.Tomb)
287285
}
288286

289287
if err != nil && err != http.ErrServerClosed {
290-
return fmt.Errorf("Appsec server failed: %w", err)
288+
return fmt.Errorf("appsec server failed: %w", err)
291289
}
292290
}
293291
return nil
@@ -334,7 +332,6 @@ func (w *AppsecSource) IsAuth(apiKey string) bool {
334332
defer resp.Body.Close()
335333

336334
return resp.StatusCode == http.StatusOK
337-
338335
}
339336

340337
// should this be in the runner ?
@@ -401,5 +398,4 @@ func (w *AppsecSource) appsecHandler(rw http.ResponseWriter, r *http.Request) {
401398
} else {
402399
rw.Write(body)
403400
}
404-
405401
}

pkg/acquisition/modules/appsec/appsec_hooks_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"net/url"
66
"testing"
77

8-
"github.com/crowdsecurity/crowdsec/pkg/appsec"
9-
"github.com/crowdsecurity/crowdsec/pkg/appsec/appsec_rule"
10-
"github.com/crowdsecurity/crowdsec/pkg/types"
118
"github.com/davecgh/go-spew/spew"
129
log "github.com/sirupsen/logrus"
1310
"github.com/stretchr/testify/require"
11+
12+
"github.com/crowdsecurity/crowdsec/pkg/appsec"
13+
"github.com/crowdsecurity/crowdsec/pkg/appsec/appsec_rule"
14+
"github.com/crowdsecurity/crowdsec/pkg/types"
1415
)
1516

1617
func TestAppsecOnMatchHooks(t *testing.T) {
@@ -41,7 +42,6 @@ func TestAppsecOnMatchHooks(t *testing.T) {
4142
require.Equal(t, 403, responses[0].BouncerHTTPResponseCode)
4243
require.Equal(t, 403, responses[0].UserHTTPResponseCode)
4344
require.Equal(t, appsec.BanRemediation, responses[0].Action)
44-
4545
},
4646
},
4747
{

pkg/acquisition/modules/appsec/appsec_lnx_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ package appsecacquisition
66
import (
77
"testing"
88

9+
log "github.com/sirupsen/logrus"
10+
"github.com/stretchr/testify/require"
11+
912
"github.com/crowdsecurity/crowdsec/pkg/appsec"
1013
"github.com/crowdsecurity/crowdsec/pkg/appsec/appsec_rule"
1114
"github.com/crowdsecurity/crowdsec/pkg/types"
12-
log "github.com/sirupsen/logrus"
13-
"github.com/stretchr/testify/require"
1415
)
1516

1617
func TestAppsecRuleTransformsOthers(t *testing.T) {
17-
1818
log.SetLevel(log.TraceLevel)
1919
tests := []appsecRuleTest{
2020
{

pkg/acquisition/modules/appsec/appsec_remediation_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import (
55
"net/url"
66
"testing"
77

8+
"github.com/stretchr/testify/require"
9+
810
"github.com/crowdsecurity/crowdsec/pkg/appsec"
911
"github.com/crowdsecurity/crowdsec/pkg/appsec/appsec_rule"
1012
"github.com/crowdsecurity/crowdsec/pkg/types"
11-
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestAppsecDefaultPassRemediation(t *testing.T) {
15-
1616
tests := []appsecRuleTest{
1717
{
1818
name: "Basic non-matching rule",
@@ -152,7 +152,6 @@ func TestAppsecDefaultPassRemediation(t *testing.T) {
152152
}
153153

154154
func TestAppsecDefaultRemediation(t *testing.T) {
155-
156155
tests := []appsecRuleTest{
157156
{
158157
name: "Basic matching rule",

0 commit comments

Comments
 (0)