Skip to content

Commit 3c24a37

Browse files
committed
lint
1 parent e0ea94a commit 3c24a37

File tree

7 files changed

+60
-13
lines changed

7 files changed

+60
-13
lines changed

pkg/acquisition/modules/appsec/appsec.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,16 @@ func (w *AppsecSource) Configure(yamlConfig []byte, logger *log.Entry, metricsLe
194194

195195
// let's load the associated appsec_config:
196196
if w.config.AppsecConfigPath != "" {
197-
err := appsecCfg.LoadByPath(w.config.AppsecConfigPath)
198-
if err != nil {
197+
if err = appsecCfg.LoadByPath(w.config.AppsecConfigPath); err != nil {
199198
return fmt.Errorf("unable to load appsec_config: %w", err)
200199
}
201200
} else if w.config.AppsecConfig != "" {
202-
err := appsecCfg.Load(w.config.AppsecConfig)
203-
if err != nil {
201+
if err = appsecCfg.Load(w.config.AppsecConfig); err != nil {
204202
return fmt.Errorf("unable to load appsec_config: %w", err)
205203
}
206204
} else if len(w.config.AppsecConfigs) > 0 {
207205
for _, appsecConfig := range w.config.AppsecConfigs {
208-
err := appsecCfg.Load(appsecConfig)
209-
if err != nil {
206+
if err = appsecCfg.Load(appsecConfig); err != nil {
210207
return fmt.Errorf("unable to load appsec_config: %w", err)
211208
}
212209
}
@@ -233,6 +230,7 @@ func (w *AppsecSource) Configure(yamlConfig []byte, logger *log.Entry, metricsLe
233230
if err != nil {
234231
return fmt.Errorf("unable to get authenticated LAPI client: %w", err)
235232
}
233+
236234
w.appsecAllowlistClient = allowlists.NewAppsecAllowlist(w.apiClient, w.logger)
237235

238236
for nbRoutine := range w.config.Routines {

pkg/acquisition/modules/http/http_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ headers:
392392
key: test`), 2)
393393

394394
time.Sleep(1 * time.Second)
395+
395396
rawEvt := `{"test": "test"}`
396397

397398
errChan := make(chan error)
@@ -435,6 +436,7 @@ custom_headers:
435436

436437
rawEvt := `{"test": "test"}`
437438
errChan := make(chan error)
439+
438440
go assertEvents(out, []string{rawEvt}, errChan)
439441

440442
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/test", testHTTPServerAddr), strings.NewReader(rawEvt))
@@ -468,9 +470,11 @@ func (sr *slowReader) Read(p []byte) (int, error) {
468470
if sr.index >= len(sr.body) {
469471
return 0, io.EOF
470472
}
473+
471474
time.Sleep(sr.delay) // Simulate a delay in reading
472475
n := copy(p, sr.body[sr.index:])
473476
sr.index += n
477+
474478
return n, nil
475479
}
476480

@@ -497,10 +501,12 @@ func assertEvents(out chan types.Event, expected []string, errChan chan error) {
497501
errChan <- fmt.Errorf(`expected %s, got '%+v'`, expected, evt.Line.Raw)
498502
return
499503
}
504+
500505
if evt.Line.Src != "127.0.0.1" {
501506
errChan <- fmt.Errorf("expected '127.0.0.1', got '%s'", evt.Line.Src)
502507
return
503508
}
509+
504510
if evt.Line.Module != "http" {
505511
errChan <- fmt.Errorf("expected 'http', got '%s'", evt.Line.Module)
506512
return
@@ -606,6 +612,7 @@ tls:
606612

607613
rawEvt := `{"test": "test"}`
608614
errChan := make(chan error)
615+
609616
go assertEvents(out, []string{rawEvt}, errChan)
610617

611618
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/test", testHTTPServerAddrTLS), strings.NewReader(rawEvt))
@@ -666,6 +673,7 @@ tls:
666673

667674
rawEvt := `{"test": "test"}`
668675
errChan := make(chan error)
676+
669677
go assertEvents(out, []string{rawEvt}, errChan)
670678

671679
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/test", testHTTPServerAddrTLS), strings.NewReader(rawEvt))
@@ -702,6 +710,7 @@ headers:
702710

703711
rawEvt := `{"test": "test"}`
704712
errChan := make(chan error)
713+
705714
go assertEvents(out, []string{rawEvt, rawEvt}, errChan)
706715

707716
var b strings.Builder
@@ -753,9 +762,10 @@ headers:
753762
key: test`), 2)
754763

755764
time.Sleep(1 * time.Second)
756-
rawEvt := `{"test": "test"}`
757765

766+
rawEvt := `{"test": "test"}`
758767
errChan := make(chan error)
768+
759769
go assertEvents(out, []string{rawEvt, rawEvt}, errChan)
760770

761771
client := &http.Client{}
@@ -786,10 +796,13 @@ func assertMetrics(t *testing.T, reg *prometheus.Registry, metrics []prometheus.
786796
require.NoError(t, err)
787797

788798
isExist := false
799+
789800
for _, metricFamily := range promMetrics {
790801
if metricFamily.GetName() == "cs_httpsource_hits_total" {
791802
isExist = true
803+
792804
assert.Len(t, metricFamily.GetMetric(), 1)
805+
793806
for _, metric := range metricFamily.GetMetric() {
794807
assert.InDelta(t, float64(expected), metric.GetCounter().GetValue(), 0.000001)
795808
labels := metric.GetLabel()
@@ -801,6 +814,7 @@ func assertMetrics(t *testing.T, reg *prometheus.Registry, metrics []prometheus.
801814
}
802815
}
803816
}
817+
804818
if !isExist && expected > 0 {
805819
t.Fatalf("expected metric cs_httpsource_hits_total not found")
806820
}

pkg/apiclient/auth_jwt.go

+4
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func (t *JWTTransport) prepareRequest(req *http.Request) (*http.Request, error)
170170
// RoundTrip implements the RoundTripper interface.
171171
func (t *JWTTransport) RoundTrip(req *http.Request) (*http.Response, error) {
172172
var resp *http.Response
173+
173174
attemptsCount := make(map[int]int)
174175

175176
for {
@@ -213,6 +214,7 @@ func (t *JWTTransport) RoundTrip(req *http.Request) (*http.Response, error) {
213214
}
214215

215216
log.Debugf("retrying request to %s", req.URL.String())
217+
216218
attemptsCount[resp.StatusCode]++
217219
log.Infof("attempt %d out of %d", attemptsCount[resp.StatusCode], config.MaxAttempts)
218220

@@ -222,6 +224,7 @@ func (t *JWTTransport) RoundTrip(req *http.Request) (*http.Response, error) {
222224
time.Sleep(time.Duration(backoff) * time.Second)
223225
}
224226
}
227+
225228
return resp, nil
226229
}
227230

@@ -242,5 +245,6 @@ func (t *JWTTransport) transport() http.RoundTripper {
242245
if t.Transport != nil {
243246
return t.Transport
244247
}
248+
245249
return http.DefaultTransport
246250
}

pkg/apiclient/decisions_service.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func (o *DecisionsStreamOpts) addQueryParamsToURL(url string) (string, error) {
4545
return "", err
4646
}
4747

48-
//Those 2 are a bit different
49-
//They default to true, and we only want to include them if they are false
48+
// Those 2 are a bit different
49+
// They default to true, and we only want to include them if they are false
5050

5151
if params.Get("community_pull") == "true" {
5252
params.Del("community_pull")

pkg/apiclient/heartbeat.go

+4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ func (h *HeartBeatService) Ping(ctx context.Context) (bool, *Response, error) {
3333
func (h *HeartBeatService) StartHeartBeat(ctx context.Context, t *tomb.Tomb) {
3434
t.Go(func() error {
3535
defer trace.CatchPanic("crowdsec/apiClient/heartbeat")
36+
3637
hbTimer := time.NewTicker(1 * time.Minute)
38+
3739
for {
3840
select {
3941
case <-hbTimer.C:
@@ -46,6 +48,7 @@ func (h *HeartBeatService) StartHeartBeat(ctx context.Context, t *tomb.Tomb) {
4648
}
4749

4850
resp.Response.Body.Close()
51+
4952
if resp.Response.StatusCode != http.StatusOK {
5053
log.Errorf("heartbeat unexpected return code: %d", resp.Response.StatusCode)
5154
continue
@@ -58,6 +61,7 @@ func (h *HeartBeatService) StartHeartBeat(ctx context.Context, t *tomb.Tomb) {
5861
case <-t.Dying():
5962
log.Debugf("heartbeat: stopping")
6063
hbTimer.Stop()
64+
6165
return nil
6266
}
6367
}

pkg/apiserver/middlewares/v1/tls_auth.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ func (ta *TLSAuth) ValidateCert(c *gin.Context) (string, error) {
131131

132132
okToCache := true
133133

134-
var validErr error
135-
136-
var couldCheck bool
134+
var (
135+
validErr error
136+
couldCheck bool
137+
)
137138

138139
for _, chain := range c.Request.TLS.VerifiedChains {
139140
validErr, couldCheck = ta.checkRevocationPath(c.Request.Context(), chain)

pkg/cticlient/client.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (c *CrowdsecCTIClient) doRequest(ctx context.Context, method string, endpoi
4242
url += fmt.Sprintf("%s=%s&", k, v)
4343
}
4444
}
45+
4546
req, err := http.NewRequestWithContext(ctx, method, url, nil)
4647
if err != nil {
4748
return nil, err
@@ -54,68 +55,87 @@ func (c *CrowdsecCTIClient) doRequest(ctx context.Context, method string, endpoi
5455
if err != nil {
5556
return nil, err
5657
}
58+
5759
defer resp.Body.Close()
60+
5861
if resp.StatusCode != http.StatusOK {
5962
if resp.StatusCode == http.StatusForbidden {
6063
return nil, ErrUnauthorized
6164
}
65+
6266
if resp.StatusCode == http.StatusTooManyRequests {
6367
return nil, ErrLimit
6468
}
69+
6570
if resp.StatusCode == http.StatusNotFound {
6671
return nil, ErrNotFound
6772
}
73+
6874
return nil, fmt.Errorf("unexpected http code : %s", resp.Status)
6975
}
76+
7077
respBody, err := io.ReadAll(resp.Body)
7178
if err != nil {
7279
return nil, err
7380
}
81+
7482
return respBody, nil
7583
}
7684

7785
func (c *CrowdsecCTIClient) GetIPInfo(ip string) (*SmokeItem, error) {
7886
ctx := context.TODO()
87+
7988
body, err := c.doRequest(ctx, http.MethodGet, smokeEndpoint+"/"+ip, nil)
8089
if err != nil {
8190
if errors.Is(err, ErrNotFound) {
8291
return &SmokeItem{}, nil
8392
}
93+
8494
return nil, err
8595
}
96+
8697
item := SmokeItem{}
98+
8799
err = json.Unmarshal(body, &item)
88100
if err != nil {
89101
return nil, err
90102
}
103+
91104
return &item, nil
92105
}
93106

94107
func (c *CrowdsecCTIClient) SearchIPs(ips []string) (*SearchIPResponse, error) {
95108
ctx := context.TODO()
96109
params := make(map[string]string)
97110
params["ips"] = strings.Join(ips, ",")
111+
98112
body, err := c.doRequest(ctx, http.MethodGet, smokeEndpoint, params)
99113
if err != nil {
100114
return nil, err
101115
}
116+
102117
searchIPResponse := SearchIPResponse{}
118+
103119
err = json.Unmarshal(body, &searchIPResponse)
104120
if err != nil {
105121
return nil, err
106122
}
123+
107124
return &searchIPResponse, nil
108125
}
109126

110127
func (c *CrowdsecCTIClient) Fire(params FireParams) (*FireResponse, error) {
111128
ctx := context.TODO()
112129
paramsMap := make(map[string]string)
130+
113131
if params.Page != nil {
114132
paramsMap["page"] = fmt.Sprintf("%d", *params.Page)
115133
}
134+
116135
if params.Since != nil {
117136
paramsMap["since"] = *params.Since
118137
}
138+
119139
if params.Limit != nil {
120140
paramsMap["limit"] = fmt.Sprintf("%d", *params.Limit)
121141
}
@@ -124,11 +144,14 @@ func (c *CrowdsecCTIClient) Fire(params FireParams) (*FireResponse, error) {
124144
if err != nil {
125145
return nil, err
126146
}
147+
127148
fireResponse := FireResponse{}
149+
128150
err = json.Unmarshal(body, &fireResponse)
129151
if err != nil {
130152
return nil, err
131153
}
154+
132155
return &fireResponse, nil
133156
}
134157

@@ -137,13 +160,16 @@ func NewCrowdsecCTIClient(options ...func(*CrowdsecCTIClient)) *CrowdsecCTIClien
137160
for _, option := range options {
138161
option(client)
139162
}
163+
140164
if client.httpClient == nil {
141165
client.httpClient = &http.Client{}
142166
}
143-
// we cannot return with a ni logger, so we set a default one
167+
168+
// we cannot return with a nil logger, so we set a default one
144169
if client.Logger == nil {
145170
client.Logger = log.NewEntry(log.New())
146171
}
172+
147173
return client
148174
}
149175

0 commit comments

Comments
 (0)