Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 10, 2024
1 parent 437a975 commit c1bd717
Show file tree
Hide file tree
Showing 23 changed files with 356 additions and 170 deletions.
32 changes: 14 additions & 18 deletions pkg/apiclient/alerts_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ type AlertsDeleteOpts struct {
}

func (s *AlertsService) Add(ctx context.Context, alerts models.AddAlertsRequest) (*models.AddAlertsResponse, *Response, error) {
var addedIds models.AddAlertsResponse

u := fmt.Sprintf("%s/alerts", s.client.URLPrefix)
req, err := s.client.NewRequest(http.MethodPost, u, &alerts)

req, err := s.client.NewRequest(http.MethodPost, u, &alerts)
if err != nil {
return nil, nil, err
}

addedIds := models.AddAlertsResponse{}

resp, err := s.client.Do(ctx, req, &addedIds)
if err != nil {
return nil, resp, err
Expand All @@ -68,29 +68,25 @@ func (s *AlertsService) Add(ctx context.Context, alerts models.AddAlertsRequest)

// to demo query arguments
func (s *AlertsService) List(ctx context.Context, opts AlertsListOpts) (*models.GetAlertsResponse, *Response, error) {
var (
alerts models.GetAlertsResponse
URI string
)

u := fmt.Sprintf("%s/alerts", s.client.URLPrefix)
params, err := qs.Values(opts)

params, err := qs.Values(opts)
if err != nil {
return nil, nil, fmt.Errorf("building query: %w", err)
}

URI := u
if len(params) > 0 {
URI = fmt.Sprintf("%s?%s", u, params.Encode())
} else {
URI = u
URI = fmt.Sprintf("%s?%s", URI, params.Encode())
}

req, err := s.client.NewRequest(http.MethodGet, URI, nil)
if err != nil {
return nil, nil, fmt.Errorf("building request: %w", err)
}

alerts := models.GetAlertsResponse{}

resp, err := s.client.Do(ctx, req, &alerts)
if err != nil {
return nil, resp, fmt.Errorf("performing request: %w", err)
Expand All @@ -101,8 +97,6 @@ func (s *AlertsService) List(ctx context.Context, opts AlertsListOpts) (*models.

// to demo query arguments
func (s *AlertsService) Delete(ctx context.Context, opts AlertsDeleteOpts) (*models.DeleteAlertsResponse, *Response, error) {
var alerts models.DeleteAlertsResponse

params, err := qs.Values(opts)
if err != nil {
return nil, nil, err
Expand All @@ -115,6 +109,8 @@ func (s *AlertsService) Delete(ctx context.Context, opts AlertsDeleteOpts) (*mod
return nil, nil, err
}

alerts := models.DeleteAlertsResponse{}

resp, err := s.client.Do(ctx, req, &alerts)
if err != nil {
return nil, resp, err
Expand All @@ -124,15 +120,15 @@ func (s *AlertsService) Delete(ctx context.Context, opts AlertsDeleteOpts) (*mod
}

func (s *AlertsService) DeleteOne(ctx context.Context, alertID string) (*models.DeleteAlertsResponse, *Response, error) {
var alerts models.DeleteAlertsResponse

u := fmt.Sprintf("%s/alerts/%s", s.client.URLPrefix, alertID)

req, err := s.client.NewRequest(http.MethodDelete, u, nil)
if err != nil {
return nil, nil, err
}

alerts := models.DeleteAlertsResponse{}

resp, err := s.client.Do(ctx, req, &alerts)
if err != nil {
return nil, resp, err
Expand All @@ -142,15 +138,15 @@ func (s *AlertsService) DeleteOne(ctx context.Context, alertID string) (*models.
}

func (s *AlertsService) GetByID(ctx context.Context, alertID int) (*models.Alert, *Response, error) {
var alert models.Alert

u := fmt.Sprintf("%s/alerts/%d", s.client.URLPrefix, alertID)

req, err := s.client.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}

alert := models.Alert{}

resp, err := s.client.Do(ctx, req, &alert)
if err != nil {
return nil, nil, err
Expand Down
22 changes: 11 additions & 11 deletions pkg/apiclient/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (r retryRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

select {
case <-req.Context().Done():
return resp, req.Context().Err()
return nil, req.Context().Err()

Check warning on line 128 in pkg/apiclient/auth.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiclient/auth.go#L128

Added line #L128 was not covered by tests
case <-time.After(time.Duration(backoff) * time.Second):
}
}
Expand All @@ -135,8 +135,8 @@ func (r retryRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
}

clonedReq := cloneRequest(req)
resp, err = r.next.RoundTrip(clonedReq)

resp, err = r.next.RoundTrip(clonedReq)
if err != nil {
if left := maxAttempts - i - 1; left > 0 {
log.Errorf("error while performing request: %s; %d retries left", err, left)
Expand Down Expand Up @@ -171,10 +171,11 @@ type JWTTransport struct {

func (t *JWTTransport) refreshJwtToken() error {
var err error

if t.UpdateScenario != nil {
t.Scenarios, err = t.UpdateScenario()
if err != nil {
return fmt.Errorf("can't update scenario list: %s", err)
return fmt.Errorf("can't update scenario list: %w", err)

Check warning on line 178 in pkg/apiclient/auth.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiclient/auth.go#L178

Added line #L178 was not covered by tests
}

log.Debugf("scenarios list updated for '%s'", *t.MachineID)
Expand All @@ -186,8 +187,6 @@ func (t *JWTTransport) refreshJwtToken() error {
Scenarios: t.Scenarios,
}

var response models.WatcherAuthResponse

/*
we don't use the main client, so let's build the body
*/
Expand Down Expand Up @@ -250,6 +249,8 @@ func (t *JWTTransport) refreshJwtToken() error {
}
}

var response models.WatcherAuthResponse

if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return fmt.Errorf("unable to decode response: %w", err)
}
Expand Down Expand Up @@ -300,7 +301,7 @@ func (t *JWTTransport) RoundTrip(req *http.Request) (*http.Response, error) {
}

if err != nil {
// we had an error (network error for example, or 401 because token is refused), reset the token ?
// we had an error (network error for example, or 401 because token is refused), reset the token?
t.Token = ""

return resp, fmt.Errorf("performing jwt auth: %w", err)
Expand All @@ -324,14 +325,13 @@ func (t *JWTTransport) ResetToken() {
t.refreshTokenMutex.Unlock()
}

// transport() returns a round tripper that retries once when the status is unauthorized, and 5 times when infrastructure is overloaded.
func (t *JWTTransport) transport() http.RoundTripper {
var transport http.RoundTripper
if t.Transport != nil {
transport = t.Transport
} else {
transport := t.Transport
if transport == nil {
transport = http.DefaultTransport
}
// a round tripper that retries once when the status is unauthorized and 5 times when infrastructure is overloaded

return &retryRoundTripper{
next: &retryRoundTripper{
next: transport,
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiclient/client_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (c *ApiClient) Do(ctx context.Context, req *http.Request, v interface{}) (*

if log.GetLevel() >= log.DebugLevel {
for k, v := range resp.Header {
log.Debugf("[headers] %s : %s", k, v)
log.Debugf("[headers] %s: %s", k, v)
}

dump, err := httputil.DumpResponse(resp, true)
Expand Down
39 changes: 19 additions & 20 deletions pkg/apiclient/decisions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package apiclient
import (
"bufio"
"context"
"errors"
"fmt"
"net/http"

qs "github.com/google/go-querystring/query"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/crowdsecurity/go-cs-lib/ptr"
Expand Down Expand Up @@ -61,8 +61,6 @@ type DecisionsDeleteOpts struct {

// to demo query arguments
func (s *DecisionsService) List(ctx context.Context, opts DecisionsListOpts) (*models.GetDecisionsResponse, *Response, error) {
var decisions models.GetDecisionsResponse

params, err := qs.Values(opts)
if err != nil {
return nil, nil, err
Expand All @@ -75,6 +73,8 @@ func (s *DecisionsService) List(ctx context.Context, opts DecisionsListOpts) (*m
return nil, nil, err
}

var decisions models.GetDecisionsResponse

resp, err := s.client.Do(ctx, req, &decisions)
if err != nil {
return nil, resp, err
Expand All @@ -84,13 +84,13 @@ func (s *DecisionsService) List(ctx context.Context, opts DecisionsListOpts) (*m
}

func (s *DecisionsService) FetchV2Decisions(ctx context.Context, url string) (*models.DecisionsStreamResponse, *Response, error) {
var decisions models.DecisionsStreamResponse

req, err := s.client.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}

var decisions models.DecisionsStreamResponse

resp, err := s.client.Do(ctx, req, &decisions)
if err != nil {
return nil, resp, err
Expand All @@ -100,7 +100,7 @@ func (s *DecisionsService) FetchV2Decisions(ctx context.Context, url string) (*m
}

func (s *DecisionsService) GetDecisionsFromGroups(decisionsGroups []*modelscapi.GetDecisionsStreamResponseNewItem) []*models.Decision {
var decisions []*models.Decision
decisions := make([]*models.Decision, 0)

for _, decisionsGroup := range decisionsGroups {
partialDecisions := make([]*models.Decision, len(decisionsGroup.Decisions))
Expand All @@ -122,11 +122,6 @@ func (s *DecisionsService) GetDecisionsFromGroups(decisionsGroups []*modelscapi.
}

func (s *DecisionsService) FetchV3Decisions(ctx context.Context, url string) (*models.DecisionsStreamResponse, *Response, error) {
var (
decisions modelscapi.GetDecisionsStreamResponse
v2Decisions models.DecisionsStreamResponse
)

scenarioDeleted := "deleted"
durationDeleted := "1h"

Expand All @@ -135,11 +130,14 @@ func (s *DecisionsService) FetchV3Decisions(ctx context.Context, url string) (*m
return nil, nil, err
}

decisions := modelscapi.GetDecisionsStreamResponse{}

resp, err := s.client.Do(ctx, req, &decisions)
if err != nil {
return nil, resp, err
}

v2Decisions := models.DecisionsStreamResponse{}
v2Decisions.New = s.GetDecisionsFromGroups(decisions.New)

for _, decisionsGroup := range decisions.Deleted {
Expand Down Expand Up @@ -183,6 +181,7 @@ func (s *DecisionsService) GetDecisionsFromBlocklist(ctx context.Context, blockl

req = req.WithContext(ctx)
log.Debugf("[URL] %s %s", req.Method, req.URL)

// we don't use client_http Do method because we need the reader and is not provided.
// We would be forced to use Pipe and goroutine, etc
resp, err := client.Do(req)
Expand Down Expand Up @@ -247,11 +246,11 @@ func (s *DecisionsService) GetStream(ctx context.Context, opts DecisionsStreamOp
return nil, nil, err
}

if s.client.URLPrefix == "v3" {
return s.FetchV3Decisions(ctx, u)
} else {
if s.client.URLPrefix != "v3" {
return s.FetchV2Decisions(ctx, u)
}

return s.FetchV3Decisions(ctx, u)
}

func (s *DecisionsService) GetStreamV3(ctx context.Context, opts DecisionsStreamOpts) (*modelscapi.GetDecisionsStreamResponse, *Response, error) {
Expand All @@ -260,13 +259,13 @@ func (s *DecisionsService) GetStreamV3(ctx context.Context, opts DecisionsStream
return nil, nil, err
}

var decisions modelscapi.GetDecisionsStreamResponse

req, err := s.client.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}

decisions := modelscapi.GetDecisionsStreamResponse{}

resp, err := s.client.Do(ctx, req, &decisions)
if err != nil {
return nil, resp, err
Expand All @@ -292,8 +291,6 @@ func (s *DecisionsService) StopStream(ctx context.Context) (*Response, error) {
}

func (s *DecisionsService) Delete(ctx context.Context, opts DecisionsDeleteOpts) (*models.DeleteDecisionResponse, *Response, error) {
var deleteDecisionResponse models.DeleteDecisionResponse

params, err := qs.Values(opts)
if err != nil {
return nil, nil, err
Expand All @@ -306,6 +303,8 @@ func (s *DecisionsService) Delete(ctx context.Context, opts DecisionsDeleteOpts)
return nil, nil, err
}

deleteDecisionResponse := models.DeleteDecisionResponse{}

resp, err := s.client.Do(ctx, req, &deleteDecisionResponse)
if err != nil {
return nil, resp, err
Expand All @@ -315,15 +314,15 @@ func (s *DecisionsService) Delete(ctx context.Context, opts DecisionsDeleteOpts)
}

func (s *DecisionsService) DeleteOne(ctx context.Context, decisionID string) (*models.DeleteDecisionResponse, *Response, error) {
var deleteDecisionResponse models.DeleteDecisionResponse

u := fmt.Sprintf("%s/decisions/%s", s.client.URLPrefix, decisionID)

req, err := s.client.NewRequest(http.MethodDelete, u, nil)
if err != nil {
return nil, nil, err
}

deleteDecisionResponse := models.DeleteDecisionResponse{}

Check warning on line 325 in pkg/apiclient/decisions_service.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiclient/decisions_service.go#L324-L325

Added lines #L324 - L325 were not covered by tests
resp, err := s.client.Do(ctx, req, &deleteDecisionResponse)
if err != nil {
return nil, resp, err
Expand Down
8 changes: 4 additions & 4 deletions pkg/apiclient/decisions_sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ type DecisionDeleteService service

// DecisionDeleteService purposely reuses AddSignalsRequestItemDecisions model
func (d *DecisionDeleteService) Add(ctx context.Context, deletedDecisions *models.DecisionsDeleteRequest) (interface{}, *Response, error) {
var response interface{}

u := fmt.Sprintf("%s/decisions/delete", d.client.URLPrefix)

req, err := d.client.NewRequest(http.MethodPost, u, &deletedDecisions)
if err != nil {
return nil, nil, fmt.Errorf("while building request: %w", err)
}

var response interface{}

Check warning on line 25 in pkg/apiclient/decisions_sync_service.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiclient/decisions_sync_service.go#L24-L25

Added lines #L24 - L25 were not covered by tests
resp, err := d.client.Do(ctx, req, &response)
if err != nil {
return nil, resp, fmt.Errorf("while performing request: %w", err)
}

if resp.Response.StatusCode != http.StatusOK {
log.Warnf("Decisions delete response : http %s", resp.Response.Status)
log.Warnf("Decisions delete response: http %s", resp.Response.Status)

Check warning on line 32 in pkg/apiclient/decisions_sync_service.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiclient/decisions_sync_service.go#L32

Added line #L32 was not covered by tests
} else {
log.Debugf("Decisions delete response : http %s", resp.Response.Status)
log.Debugf("Decisions delete response: http %s", resp.Response.Status)

Check warning on line 34 in pkg/apiclient/decisions_sync_service.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiclient/decisions_sync_service.go#L34

Added line #L34 was not covered by tests
}

return &response, resp, nil
Expand Down
Loading

0 comments on commit c1bd717

Please sign in to comment.