diff --git a/.github/workflows/ci_golangci-lint-windows.yml b/.github/workflows/ci_golangci-lint-windows.yml index 5f0f0c550..2dccbf44b 100644 --- a/.github/workflows/ci_golangci-lint-windows.yml +++ b/.github/workflows/ci_golangci-lint-windows.yml @@ -25,7 +25,7 @@ jobs: uses: golangci/golangci-lint-action@v3 with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.46 + version: v1.48 # Optional: golangci-lint command line arguments. args: --issues-exit-code=0 --timeout 10m only-new-issues: true diff --git a/.github/workflows/ci_golangci-lint.yml b/.github/workflows/ci_golangci-lint.yml index 48a5a1360..32e9791e3 100644 --- a/.github/workflows/ci_golangci-lint.yml +++ b/.github/workflows/ci_golangci-lint.yml @@ -25,7 +25,7 @@ jobs: uses: golangci/golangci-lint-action@v3 with: # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version - version: v1.46 + version: v1.48 # Optional: golangci-lint command line arguments. args: --issues-exit-code=1 --timeout 5m # Optional: show only new issues if it's a pull request. The default value is `false`. diff --git a/.golangci.yml b/.golangci.yml index 88cce2bfb..10127d85a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -47,6 +47,7 @@ linters: # # Enabled # + # - asasalint # check for pass []any as any in variadic func(...any) # - asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers # - bidichk # Checks for dangerous unicode character sequences # - decorder # check declaration order and count of types, constants, variables and functions @@ -71,6 +72,7 @@ linters: # - tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes # - typecheck # Like the front-end of a Go compiler, parses and type-checks Go code # - unconvert # Remove unnecessary type conversions + # - usestdlibvars # A linter that detect the possibility to use variables/constants from the Go standard library. # - varcheck # Finds unused global variables and constants # @@ -161,6 +163,7 @@ linters: - exhaustruct # Checks if all structure fields are initialized - goconst # Finds repeated strings that could be replaced by a constant - stylecheck # Stylecheck is a replacement for golint + - nosnakecase # nosnakecase is a linter that detects snake case of variable naming and function name. # # Under evaluation @@ -168,7 +171,6 @@ linters: - prealloc # Finds slice declarations that could potentially be preallocated - issues: max-issues-per-linter: 0 max-same-issues: 10 diff --git a/pkg/apiclient/alerts_service.go b/pkg/apiclient/alerts_service.go index 5ff1d1f2f..e1088324d 100644 --- a/pkg/apiclient/alerts_service.go +++ b/pkg/apiclient/alerts_service.go @@ -3,6 +3,7 @@ package apiclient import ( "context" "fmt" + "net/http" "github.com/crowdsecurity/crowdsec/pkg/models" qs "github.com/google/go-querystring/query" @@ -52,7 +53,7 @@ func (s *AlertsService) Add(ctx context.Context, alerts models.AddAlertsRequest) var added_ids models.AddAlertsResponse u := fmt.Sprintf("%s/alerts", s.client.URLPrefix) - req, err := s.client.NewRequest("POST", u, &alerts) + req, err := s.client.NewRequest(http.MethodPost, u, &alerts) if err != nil { return nil, nil, err } @@ -79,7 +80,7 @@ func (s *AlertsService) List(ctx context.Context, opts AlertsListOpts) (*models. URI = u } - req, err := s.client.NewRequest("GET", URI, nil) + req, err := s.client.NewRequest(http.MethodGet, URI, nil) if err != nil { return nil, nil, errors.Wrap(err, "building request") } @@ -100,7 +101,7 @@ func (s *AlertsService) Delete(ctx context.Context, opts AlertsDeleteOpts) (*mod } u := fmt.Sprintf("%s/alerts?%s", s.client.URLPrefix, params.Encode()) - req, err := s.client.NewRequest("DELETE", u, nil) + req, err := s.client.NewRequest(http.MethodDelete, u, nil) if err != nil { return nil, nil, err } @@ -116,7 +117,7 @@ func (s *AlertsService) GetByID(ctx context.Context, alertID int) (*models.Alert var alert models.Alert u := fmt.Sprintf("%s/alerts/%d", s.client.URLPrefix, alertID) - req, err := s.client.NewRequest("GET", u, nil) + req, err := s.client.NewRequest(http.MethodGet, u, nil) if err != nil { return nil, nil, err } diff --git a/pkg/apiclient/auth.go b/pkg/apiclient/auth.go index 8759fb46d..87d725e80 100644 --- a/pkg/apiclient/auth.go +++ b/pkg/apiclient/auth.go @@ -118,7 +118,7 @@ func (t *JWTTransport) refreshJwtToken() error { if err != nil { return errors.Wrap(err, "could not encode jwt auth body") } - req, err := http.NewRequest("POST", fmt.Sprintf("%s%s/watchers/login", t.URL, t.VersionPrefix), buf) + req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s/watchers/login", t.URL, t.VersionPrefix), buf) if err != nil { return errors.Wrap(err, "could not create request") } diff --git a/pkg/apiclient/auth_service.go b/pkg/apiclient/auth_service.go index 9b007ee8d..bf02738d2 100644 --- a/pkg/apiclient/auth_service.go +++ b/pkg/apiclient/auth_service.go @@ -3,6 +3,7 @@ package apiclient import ( "context" "fmt" + "net/http" "github.com/crowdsecurity/crowdsec/pkg/models" ) @@ -22,7 +23,7 @@ type enrollRequest struct { func (s *AuthService) UnregisterWatcher(ctx context.Context) (*Response, error) { u := fmt.Sprintf("%s/watchers", s.client.URLPrefix) - req, err := s.client.NewRequest("DELETE", u, nil) + req, err := s.client.NewRequest(http.MethodDelete, u, nil) if err != nil { return nil, err } @@ -38,7 +39,7 @@ func (s *AuthService) RegisterWatcher(ctx context.Context, registration models.W u := fmt.Sprintf("%s/watchers", s.client.URLPrefix) - req, err := s.client.NewRequest("POST", u, ®istration) + req, err := s.client.NewRequest(http.MethodPost, u, ®istration) if err != nil { return nil, err } @@ -52,7 +53,7 @@ func (s *AuthService) RegisterWatcher(ctx context.Context, registration models.W func (s *AuthService) AuthenticateWatcher(ctx context.Context, auth models.WatcherAuthRequest) (*Response, error) { u := fmt.Sprintf("%s/watchers/login", s.client.URLPrefix) - req, err := s.client.NewRequest("POST", u, &auth) + req, err := s.client.NewRequest(http.MethodPost, u, &auth) if err != nil { return nil, err } @@ -66,7 +67,7 @@ func (s *AuthService) AuthenticateWatcher(ctx context.Context, auth models.Watch func (s *AuthService) EnrollWatcher(ctx context.Context, enrollKey string, name string, tags []string, overwrite bool) (*Response, error) { u := fmt.Sprintf("%s/watchers/enroll", s.client.URLPrefix) - req, err := s.client.NewRequest("POST", u, &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags, Overwrite: overwrite}) + req, err := s.client.NewRequest(http.MethodPost, u, &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags, Overwrite: overwrite}) if err != nil { return nil, err } diff --git a/pkg/apiclient/decisions_service.go b/pkg/apiclient/decisions_service.go index 447c10973..ba90a4354 100644 --- a/pkg/apiclient/decisions_service.go +++ b/pkg/apiclient/decisions_service.go @@ -3,6 +3,7 @@ package apiclient import ( "context" "fmt" + "net/http" "github.com/crowdsecurity/crowdsec/pkg/models" qs "github.com/google/go-querystring/query" @@ -55,7 +56,7 @@ func (s *DecisionsService) List(ctx context.Context, opts DecisionsListOpts) (*m } u := fmt.Sprintf("%s/decisions?%s", s.client.URLPrefix, params.Encode()) - req, err := s.client.NewRequest("GET", u, nil) + req, err := s.client.NewRequest(http.MethodGet, u, nil) if err != nil { return nil, nil, err } @@ -73,7 +74,7 @@ func (s *DecisionsService) GetStream(ctx context.Context, opts DecisionsStreamOp if err != nil { return nil, nil, err } - req, err := s.client.NewRequest("GET", u, nil) + req, err := s.client.NewRequest(http.MethodGet, u, nil) if err != nil { return nil, nil, err } @@ -89,7 +90,7 @@ func (s *DecisionsService) GetStream(ctx context.Context, opts DecisionsStreamOp func (s *DecisionsService) StopStream(ctx context.Context) (*Response, error) { u := fmt.Sprintf("%s/decisions", s.client.URLPrefix) - req, err := s.client.NewRequest("DELETE", u, nil) + req, err := s.client.NewRequest(http.MethodDelete, u, nil) if err != nil { return nil, err } @@ -109,7 +110,7 @@ func (s *DecisionsService) Delete(ctx context.Context, opts DecisionsDeleteOpts) } u := fmt.Sprintf("%s/decisions?%s", s.client.URLPrefix, params.Encode()) - req, err := s.client.NewRequest("DELETE", u, nil) + req, err := s.client.NewRequest(http.MethodDelete, u, nil) if err != nil { return nil, nil, err } @@ -125,7 +126,7 @@ func (s *DecisionsService) DeleteOne(ctx context.Context, decision_id string) (* var deleteDecisionResponse models.DeleteDecisionResponse u := fmt.Sprintf("%s/decisions/%s", s.client.URLPrefix, decision_id) - req, err := s.client.NewRequest("DELETE", u, nil) + req, err := s.client.NewRequest(http.MethodDelete, u, nil) if err != nil { return nil, nil, err } diff --git a/pkg/apiclient/heartbeat.go b/pkg/apiclient/heartbeat.go index 1cbef5472..dc7c256e3 100644 --- a/pkg/apiclient/heartbeat.go +++ b/pkg/apiclient/heartbeat.go @@ -17,7 +17,7 @@ func (h *HeartBeatService) Ping(ctx context.Context) (bool, *Response, error) { u := fmt.Sprintf("%s/heartbeat", h.client.URLPrefix) - req, err := h.client.NewRequest("GET", u, nil) + req, err := h.client.NewRequest(http.MethodGet, u, nil) if err != nil { return false, nil, err } diff --git a/pkg/apiclient/metrics.go b/pkg/apiclient/metrics.go index 3e655425c..ea447280a 100644 --- a/pkg/apiclient/metrics.go +++ b/pkg/apiclient/metrics.go @@ -3,6 +3,7 @@ package apiclient import ( "context" "fmt" + "net/http" "github.com/crowdsecurity/crowdsec/pkg/models" ) @@ -13,7 +14,7 @@ func (s *MetricsService) Add(ctx context.Context, metrics *models.Metrics) (inte var response interface{} u := fmt.Sprintf("%s/metrics/", s.client.URLPrefix) - req, err := s.client.NewRequest("POST", u, &metrics) + req, err := s.client.NewRequest(http.MethodPost, u, &metrics) if err != nil { return nil, nil, err } diff --git a/pkg/apiclient/signal.go b/pkg/apiclient/signal.go index bfe911699..4d974f8bc 100644 --- a/pkg/apiclient/signal.go +++ b/pkg/apiclient/signal.go @@ -3,6 +3,7 @@ package apiclient import ( "context" "fmt" + "net/http" log "github.com/sirupsen/logrus" @@ -16,7 +17,7 @@ func (s *SignalService) Add(ctx context.Context, signals *models.AddSignalsReque var response interface{} u := fmt.Sprintf("%s/signals", s.client.URLPrefix) - req, err := s.client.NewRequest("POST", u, &signals) + req, err := s.client.NewRequest(http.MethodPost, u, &signals) if err != nil { return nil, nil, errors.Wrap(err, "while building request") } diff --git a/pkg/apiserver/alerts_test.go b/pkg/apiserver/alerts_test.go index 9fa3396f9..0281dd504 100644 --- a/pkg/apiserver/alerts_test.go +++ b/pkg/apiserver/alerts_test.go @@ -48,7 +48,7 @@ func SetupLAPITest(t *testing.T) LAPI { func (l *LAPI) InsertAlertFromFile(path string) *httptest.ResponseRecorder { alertReader := GetAlertReaderFromFile(path) - return l.RecordResponse("POST", "/v1/alerts", alertReader, "password") + return l.RecordResponse(http.MethodPost, "/v1/alerts", alertReader, "password") } func (l *LAPI) RecordResponse(verb string, url string, body *strings.Reader, authType string) *httptest.ResponseRecorder { @@ -92,7 +92,7 @@ func LoginToTestAPI(router *gin.Engine, config csconfig.Config) (models.WatcherA } w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/v1/watchers/login", strings.NewReader(body)) + req, _ := http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(body)) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -132,14 +132,14 @@ func TestCreateAlert(t *testing.T) { lapi := SetupLAPITest(t) // Create Alert with invalid format - w := lapi.RecordResponse("POST", "/v1/alerts", strings.NewReader("test"), "password") + w := lapi.RecordResponse(http.MethodPost, "/v1/alerts", strings.NewReader("test"), "password") assert.Equal(t, 400, w.Code) assert.Equal(t, "{\"message\":\"invalid character 'e' in literal true (expecting 'r')\"}", w.Body.String()) // Create Alert with invalid input alertContent := GetAlertReaderFromFile("./tests/invalidAlert_sample.json") - w = lapi.RecordResponse("POST", "/v1/alerts", alertContent, "password") + w = lapi.RecordResponse(http.MethodPost, "/v1/alerts", alertContent, "password") assert.Equal(t, 500, w.Code) assert.Equal(t, "{\"message\":\"validation failure list:\\n0.scenario in body is required\\n0.scenario_hash in body is required\\n0.scenario_version in body is required\\n0.simulated in body is required\\n0.source in body is required\"}", w.Body.String()) @@ -380,7 +380,7 @@ func TestCreateAlertErrors(t *testing.T) { //test invalid bearer w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/v1/alerts", alertContent) + req, _ := http.NewRequest(http.MethodPost, "/v1/alerts", alertContent) req.Header.Add("User-Agent", UserAgent) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "ratata")) lapi.router.ServeHTTP(w, req) @@ -388,7 +388,7 @@ func TestCreateAlertErrors(t *testing.T) { //test invalid bearer w = httptest.NewRecorder() - req, _ = http.NewRequest("POST", "/v1/alerts", alertContent) + req, _ = http.NewRequest(http.MethodPost, "/v1/alerts", alertContent) req.Header.Add("User-Agent", UserAgent) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", lapi.loginResp.Token+"s")) lapi.router.ServeHTTP(w, req) @@ -402,7 +402,7 @@ func TestDeleteAlert(t *testing.T) { // Fail Delete Alert w := httptest.NewRecorder() - req, _ := http.NewRequest("DELETE", "/v1/alerts", strings.NewReader("")) + req, _ := http.NewRequest(http.MethodDelete, "/v1/alerts", strings.NewReader("")) AddAuthHeaders(req, lapi.loginResp) req.RemoteAddr = "127.0.0.2:4242" lapi.router.ServeHTTP(w, req) @@ -411,7 +411,7 @@ func TestDeleteAlert(t *testing.T) { // Delete Alert w = httptest.NewRecorder() - req, _ = http.NewRequest("DELETE", "/v1/alerts", strings.NewReader("")) + req, _ = http.NewRequest(http.MethodDelete, "/v1/alerts", strings.NewReader("")) AddAuthHeaders(req, lapi.loginResp) req.RemoteAddr = "127.0.0.1:4242" lapi.router.ServeHTTP(w, req) @@ -449,7 +449,7 @@ func TestDeleteAlertTrustedIPS(t *testing.T) { assertAlertDeleteFailedFromIP := func(ip string) { w := httptest.NewRecorder() - req, _ := http.NewRequest("DELETE", "/v1/alerts", strings.NewReader("")) + req, _ := http.NewRequest(http.MethodDelete, "/v1/alerts", strings.NewReader("")) AddAuthHeaders(req, loginResp) req.RemoteAddr = ip + ":1234" @@ -461,7 +461,7 @@ func TestDeleteAlertTrustedIPS(t *testing.T) { assertAlertDeletedFromIP := func(ip string) { w := httptest.NewRecorder() - req, _ := http.NewRequest("DELETE", "/v1/alerts", strings.NewReader("")) + req, _ := http.NewRequest(http.MethodDelete, "/v1/alerts", strings.NewReader("")) AddAuthHeaders(req, loginResp) req.RemoteAddr = ip + ":1234" diff --git a/pkg/apiserver/api_key_test.go b/pkg/apiserver/api_key_test.go index 0f278673e..d1db144af 100644 --- a/pkg/apiserver/api_key_test.go +++ b/pkg/apiserver/api_key_test.go @@ -22,7 +22,7 @@ func TestAPIKey(t *testing.T) { } // Login with empty token w := httptest.NewRecorder() - req, _ := http.NewRequest("GET", "/v1/decisions", strings.NewReader("")) + req, _ := http.NewRequest(http.MethodGet, "/v1/decisions", strings.NewReader("")) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -31,7 +31,7 @@ func TestAPIKey(t *testing.T) { // Login with invalid token w = httptest.NewRecorder() - req, _ = http.NewRequest("GET", "/v1/decisions", strings.NewReader("")) + req, _ = http.NewRequest(http.MethodGet, "/v1/decisions", strings.NewReader("")) req.Header.Add("User-Agent", UserAgent) req.Header.Add("X-Api-Key", "a1b2c3d4e5f6") router.ServeHTTP(w, req) @@ -41,7 +41,7 @@ func TestAPIKey(t *testing.T) { // Login with valid token w = httptest.NewRecorder() - req, _ = http.NewRequest("GET", "/v1/decisions", strings.NewReader("")) + req, _ = http.NewRequest(http.MethodGet, "/v1/decisions", strings.NewReader("")) req.Header.Add("User-Agent", UserAgent) req.Header.Add("X-Api-Key", APIKey) router.ServeHTTP(w, req) diff --git a/pkg/apiserver/apiserver_test.go b/pkg/apiserver/apiserver_test.go index 415d2660c..04e136549 100644 --- a/pkg/apiserver/apiserver_test.go +++ b/pkg/apiserver/apiserver_test.go @@ -260,7 +260,7 @@ func CreateTestMachine(router *gin.Engine) (string, error) { body := string(b) w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/v1/watchers", strings.NewReader(body)) + req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body)) req.Header.Set("User-Agent", UserAgent) router.ServeHTTP(w, req) return body, nil @@ -309,7 +309,7 @@ func TestUnknownPath(t *testing.T) { } w := httptest.NewRecorder() - req, _ := http.NewRequest("GET", "/test", nil) + req, _ := http.NewRequest(http.MethodGet, "/test", nil) req.Header.Set("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -369,7 +369,7 @@ func TestLoggingDebugToFileConfig(t *testing.T) { } w := httptest.NewRecorder() - req, _ := http.NewRequest("GET", "/test42", nil) + req, _ := http.NewRequest(http.MethodGet, "/test42", nil) req.Header.Set("User-Agent", UserAgent) api.router.ServeHTTP(w, req) assert.Equal(t, 404, w.Code) @@ -426,7 +426,7 @@ func TestLoggingErrorToFileConfig(t *testing.T) { } w := httptest.NewRecorder() - req, _ := http.NewRequest("GET", "/test42", nil) + req, _ := http.NewRequest(http.MethodGet, "/test42", nil) req.Header.Set("User-Agent", UserAgent) api.router.ServeHTTP(w, req) assert.Equal(t, 404, w.Code) diff --git a/pkg/apiserver/heartbeat_test.go b/pkg/apiserver/heartbeat_test.go index 01fac2f0b..0082f23ec 100644 --- a/pkg/apiserver/heartbeat_test.go +++ b/pkg/apiserver/heartbeat_test.go @@ -1,6 +1,7 @@ package apiserver import ( + "net/http" "testing" "github.com/stretchr/testify/assert" @@ -9,7 +10,7 @@ import ( func TestHeartBeat(t *testing.T) { lapi := SetupLAPITest(t) - w := lapi.RecordResponse("GET", "/v1/heartbeat", emptyBody, "password") + w := lapi.RecordResponse(http.MethodGet, "/v1/heartbeat", emptyBody, "password") assert.Equal(t, 200, w.Code) w = lapi.RecordResponse("POST", "/v1/heartbeat", emptyBody, "password") diff --git a/pkg/apiserver/jwt_test.go b/pkg/apiserver/jwt_test.go index 8a83d2289..b589213db 100644 --- a/pkg/apiserver/jwt_test.go +++ b/pkg/apiserver/jwt_test.go @@ -23,7 +23,7 @@ func TestLogin(t *testing.T) { // Login with machine not validated yet w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/v1/watchers/login", strings.NewReader(body)) + req, _ := http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(body)) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -32,7 +32,7 @@ func TestLogin(t *testing.T) { // Login with machine not exist w = httptest.NewRecorder() - req, _ = http.NewRequest("POST", "/v1/watchers/login", strings.NewReader("{\"machine_id\": \"test1\", \"password\": \"test1\"}")) + req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader("{\"machine_id\": \"test1\", \"password\": \"test1\"}")) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -41,7 +41,7 @@ func TestLogin(t *testing.T) { // Login with invalid body w = httptest.NewRecorder() - req, _ = http.NewRequest("POST", "/v1/watchers/login", strings.NewReader("test")) + req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader("test")) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -50,7 +50,7 @@ func TestLogin(t *testing.T) { // Login with invalid format w = httptest.NewRecorder() - req, _ = http.NewRequest("POST", "/v1/watchers/login", strings.NewReader("{\"machine_id\": \"test1\"}")) + req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader("{\"machine_id\": \"test1\"}")) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -65,7 +65,7 @@ func TestLogin(t *testing.T) { // Login with invalid password w = httptest.NewRecorder() - req, _ = http.NewRequest("POST", "/v1/watchers/login", strings.NewReader("{\"machine_id\": \"test\", \"password\": \"test1\"}")) + req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader("{\"machine_id\": \"test\", \"password\": \"test1\"}")) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -74,7 +74,7 @@ func TestLogin(t *testing.T) { // Login with valid machine w = httptest.NewRecorder() - req, _ = http.NewRequest("POST", "/v1/watchers/login", strings.NewReader(body)) + req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(body)) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -84,7 +84,7 @@ func TestLogin(t *testing.T) { // Login with valid machine + scenarios w = httptest.NewRecorder() - req, _ = http.NewRequest("POST", "/v1/watchers/login", strings.NewReader("{\"machine_id\": \"test\", \"password\": \"test\", \"scenarios\": [\"crowdsecurity/test\", \"crowdsecurity/test2\"]}")) + req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader("{\"machine_id\": \"test\", \"password\": \"test\", \"scenarios\": [\"crowdsecurity/test\", \"crowdsecurity/test2\"]}")) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) diff --git a/pkg/apiserver/machines_test.go b/pkg/apiserver/machines_test.go index 6dfe6c108..63c4e2597 100644 --- a/pkg/apiserver/machines_test.go +++ b/pkg/apiserver/machines_test.go @@ -19,7 +19,7 @@ func TestCreateMachine(t *testing.T) { // Create machine with invalid format w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/v1/watchers", strings.NewReader("test")) + req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader("test")) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -28,7 +28,7 @@ func TestCreateMachine(t *testing.T) { // Create machine with invalid input w = httptest.NewRecorder() - req, _ = http.NewRequest("POST", "/v1/watchers", strings.NewReader("{\"test\": \"test\"}")) + req, _ = http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader("{\"test\": \"test\"}")) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -43,7 +43,7 @@ func TestCreateMachine(t *testing.T) { body := string(b) w = httptest.NewRecorder() - req, _ = http.NewRequest("POST", "/v1/watchers", strings.NewReader(body)) + req, _ = http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body)) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -66,7 +66,7 @@ func TestCreateMachineWithForwardedFor(t *testing.T) { body := string(b) w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/v1/watchers", strings.NewReader(body)) + req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body)) req.Header.Add("User-Agent", UserAgent) req.Header.Add("X-Real-Ip", "1.1.1.1") router.ServeHTTP(w, req) @@ -95,7 +95,7 @@ func TestCreateMachineWithForwardedForNoConfig(t *testing.T) { body := string(b) w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/v1/watchers", strings.NewReader(body)) + req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body)) req.Header.Add("User-Agent", UserAgent) req.Header.Add("X-Real-IP", "1.1.1.1") router.ServeHTTP(w, req) @@ -126,7 +126,7 @@ func TestCreateMachineWithoutForwardedFor(t *testing.T) { body := string(b) w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/v1/watchers", strings.NewReader(body)) + req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body)) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) @@ -154,12 +154,12 @@ func TestCreateMachineAlreadyExist(t *testing.T) { } w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/v1/watchers", strings.NewReader(body)) + req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body)) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) w = httptest.NewRecorder() - req, _ = http.NewRequest("POST", "/v1/watchers", strings.NewReader(body)) + req, _ = http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body)) req.Header.Add("User-Agent", UserAgent) router.ServeHTTP(w, req) diff --git a/pkg/cwhub/download.go b/pkg/cwhub/download.go index 753e69aa7..024fe68e4 100644 --- a/pkg/cwhub/download.go +++ b/pkg/cwhub/download.go @@ -40,7 +40,7 @@ func UpdateHubIdx(hub *csconfig.Hub) error { func DownloadHubIdx(hub *csconfig.Hub) ([]byte, error) { log.Debugf("fetching index from branch %s (%s)", HubBranch, fmt.Sprintf(RawFileURLTemplate, HubBranch, HubIndexFile)) - req, err := http.NewRequest("GET", fmt.Sprintf(RawFileURLTemplate, HubBranch, HubIndexFile), nil) + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(RawFileURLTemplate, HubBranch, HubIndexFile), nil) if err != nil { return nil, errors.Wrap(err, "failed to build request for hub index") } @@ -144,7 +144,7 @@ func DownloadItem(hub *csconfig.Hub, target Item, overwrite bool) (Item, error) // We still have to check if data files are present } } - req, err := http.NewRequest("GET", fmt.Sprintf(RawFileURLTemplate, HubBranch, target.RemotePath), nil) + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(RawFileURLTemplate, HubBranch, target.RemotePath), nil) if err != nil { return target, errors.Wrap(err, fmt.Sprintf("while downloading %s", req.URL.String())) } diff --git a/pkg/metabase/metabase.go b/pkg/metabase/metabase.go index 58d5dcc93..0220fc16a 100644 --- a/pkg/metabase/metabase.go +++ b/pkg/metabase/metabase.go @@ -319,7 +319,7 @@ func (m *Metabase) DownloadDatabase(force bool) error { return fmt.Errorf("failed to create %s : %s", metabaseDBSubpath, err) } - req, err := http.NewRequest("GET", m.InternalDBURL, nil) + req, err := http.NewRequest(http.MethodGet, m.InternalDBURL, nil) if err != nil { return fmt.Errorf("failed to build request to fetch metabase db : %s", err) } diff --git a/pkg/types/dataset.go b/pkg/types/dataset.go index de3a215da..adb665e54 100644 --- a/pkg/types/dataset.go +++ b/pkg/types/dataset.go @@ -22,7 +22,7 @@ type DataSet struct { func downloadFile(url string, destPath string) error { log.Debugf("downloading %s in %s", url, destPath) - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return err }