Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context propagation: pkg/database/config #127

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
16 changes: 11 additions & 5 deletions pkg/acquisition/modules/loki/loki_test.go
Original file line number Diff line number Diff line change
@@ -261,7 +261,7 @@ func TestConfigureDSN(t *testing.T) {
}
}

func feedLoki(logger *log.Entry, n int, title string) error {
func feedLoki(ctx context.Context, logger *log.Entry, n int, title string) error {
streams := LogStreams{
Streams: []LogStream{
{
@@ -286,7 +286,7 @@ func feedLoki(logger *log.Entry, n int, title string) error {
return err
}

req, err := http.NewRequest(http.MethodPost, "http://127.0.0.1:3100/loki/api/v1/push", bytes.NewBuffer(buff))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://127.0.0.1:3100/loki/api/v1/push", bytes.NewBuffer(buff))
if err != nil {
return err
}
@@ -349,7 +349,9 @@ since: 1h
t.Fatalf("Unexpected error : %s", err)
}

err = feedLoki(subLogger, 20, title)
ctx := context.Background()

err = feedLoki(ctx, subLogger, 20, title)
if err != nil {
t.Fatalf("Unexpected error : %s", err)
}
@@ -421,6 +423,8 @@ query: >
},
}

ctx := context.Background()

for _, ts := range tests {
t.Run(ts.name, func(t *testing.T) {
logger := log.New()
@@ -472,7 +476,7 @@ query: >
}
})

err = feedLoki(subLogger, ts.expectedLines, title)
err = feedLoki(ctx, subLogger, ts.expectedLines, title)
if err != nil {
t.Fatalf("Unexpected error : %s", err)
}
@@ -525,7 +529,9 @@ query: >

time.Sleep(time.Second * 2)

err = feedLoki(subLogger, 1, title)
ctx := context.Background()

err = feedLoki(ctx, subLogger, 1, title)
if err != nil {
t.Fatalf("Unexpected error : %s", err)
}
31 changes: 21 additions & 10 deletions pkg/apiserver/alerts_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apiserver

import (
"context"
"encoding/json"
"fmt"
"net/http"
@@ -45,8 +46,9 @@ func (l *LAPI) InsertAlertFromFile(t *testing.T, path string) *httptest.Response
}

func (l *LAPI) RecordResponse(t *testing.T, verb string, url string, body *strings.Reader, authType string) *httptest.ResponseRecorder {
ctx := context.Background()
w := httptest.NewRecorder()
req, err := http.NewRequest(verb, url, body)
req, err := http.NewRequestWithContext(ctx, verb, url, body)
require.NoError(t, err)

switch authType {
@@ -74,8 +76,9 @@ func LoginToTestAPI(t *testing.T, router *gin.Engine, config csconfig.Config) mo
body := CreateTestMachine(t, router, "")
ValidateMachine(t, "test", config.API.Server.DbConfig)

ctx := context.Background()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
req.Header.Add("User-Agent", UserAgent)
router.ServeHTTP(w, req)

@@ -355,17 +358,19 @@ func TestCreateAlertErrors(t *testing.T) {
lapi := SetupLAPITest(t)
alertContent := GetAlertReaderFromFile(t, "./tests/alert_sample.json")

ctx := context.Background()

//test invalid bearer
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPost, "/v1/alerts", alertContent)
req, _ := http.NewRequestWithContext(ctx, 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)
assert.Equal(t, 401, w.Code)

//test invalid bearer
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPost, "/v1/alerts", alertContent)
req, _ = http.NewRequestWithContext(ctx, 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)
@@ -376,9 +381,11 @@ func TestDeleteAlert(t *testing.T) {
lapi := SetupLAPITest(t)
lapi.InsertAlertFromFile(t, "./tests/alert_sample.json")

ctx := context.Background()

// Fail Delete Alert
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodDelete, "/v1/alerts", strings.NewReader(""))
req, _ := http.NewRequestWithContext(ctx, http.MethodDelete, "/v1/alerts", strings.NewReader(""))
AddAuthHeaders(req, lapi.loginResp)
req.RemoteAddr = "127.0.0.2:4242"
lapi.router.ServeHTTP(w, req)
@@ -387,7 +394,7 @@ func TestDeleteAlert(t *testing.T) {

// Delete Alert
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodDelete, "/v1/alerts", strings.NewReader(""))
req, _ = http.NewRequestWithContext(ctx, http.MethodDelete, "/v1/alerts", strings.NewReader(""))
AddAuthHeaders(req, lapi.loginResp)
req.RemoteAddr = "127.0.0.1:4242"
lapi.router.ServeHTTP(w, req)
@@ -399,9 +406,11 @@ func TestDeleteAlertByID(t *testing.T) {
lapi := SetupLAPITest(t)
lapi.InsertAlertFromFile(t, "./tests/alert_sample.json")

ctx := context.Background()

// Fail Delete Alert
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodDelete, "/v1/alerts/1", strings.NewReader(""))
req, _ := http.NewRequestWithContext(ctx, http.MethodDelete, "/v1/alerts/1", strings.NewReader(""))
AddAuthHeaders(req, lapi.loginResp)
req.RemoteAddr = "127.0.0.2:4242"
lapi.router.ServeHTTP(w, req)
@@ -410,7 +419,7 @@ func TestDeleteAlertByID(t *testing.T) {

// Delete Alert
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodDelete, "/v1/alerts/1", strings.NewReader(""))
req, _ = http.NewRequestWithContext(ctx, http.MethodDelete, "/v1/alerts/1", strings.NewReader(""))
AddAuthHeaders(req, lapi.loginResp)
req.RemoteAddr = "127.0.0.1:4242"
lapi.router.ServeHTTP(w, req)
@@ -439,9 +448,11 @@ func TestDeleteAlertTrustedIPS(t *testing.T) {
loginResp: loginResp,
}

ctx := context.Background()

assertAlertDeleteFailedFromIP := func(ip string) {
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodDelete, "/v1/alerts", strings.NewReader(""))
req, _ := http.NewRequestWithContext(ctx, http.MethodDelete, "/v1/alerts", strings.NewReader(""))

AddAuthHeaders(req, loginResp)
req.RemoteAddr = ip + ":1234"
@@ -453,7 +464,7 @@ func TestDeleteAlertTrustedIPS(t *testing.T) {

assertAlertDeletedFromIP := func(ip string) {
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodDelete, "/v1/alerts", strings.NewReader(""))
req, _ := http.NewRequestWithContext(ctx, http.MethodDelete, "/v1/alerts", strings.NewReader(""))
AddAuthHeaders(req, loginResp)
req.RemoteAddr = ip + ":1234"

9 changes: 6 additions & 3 deletions pkg/apiserver/api_key_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apiserver

import (
"context"
"net/http"
"net/http/httptest"
"strings"
@@ -12,11 +13,13 @@ import (
func TestAPIKey(t *testing.T) {
router, config := NewAPITest(t)

ctx := context.Background()

APIKey := CreateTestBouncer(t, config.API.Server.DbConfig)

// Login with empty token
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/v1/decisions", strings.NewReader(""))
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/v1/decisions", strings.NewReader(""))
req.Header.Add("User-Agent", UserAgent)
router.ServeHTTP(w, req)

@@ -25,7 +28,7 @@ func TestAPIKey(t *testing.T) {

// Login with invalid token
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodGet, "/v1/decisions", strings.NewReader(""))
req, _ = http.NewRequestWithContext(ctx, http.MethodGet, "/v1/decisions", strings.NewReader(""))
req.Header.Add("User-Agent", UserAgent)
req.Header.Add("X-Api-Key", "a1b2c3d4e5f6")
router.ServeHTTP(w, req)
@@ -35,7 +38,7 @@ func TestAPIKey(t *testing.T) {

// Login with valid token
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodGet, "/v1/decisions", strings.NewReader(""))
req, _ = http.NewRequestWithContext(ctx, http.MethodGet, "/v1/decisions", strings.NewReader(""))
req.Header.Add("User-Agent", UserAgent)
req.Header.Add("X-Api-Key", APIKey)
router.ServeHTTP(w, req)
16 changes: 12 additions & 4 deletions pkg/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
@@ -278,8 +278,10 @@ func CreateTestMachine(t *testing.T, router *gin.Engine, token string) string {

body := string(b)

ctx := context.Background()

w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body))
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers", strings.NewReader(body))
req.Header.Set("User-Agent", UserAgent)
router.ServeHTTP(w, req)

@@ -323,8 +325,10 @@ func TestWithWrongFlushConfig(t *testing.T) {
func TestUnknownPath(t *testing.T) {
router, _ := NewAPITest(t)

ctx := context.Background()

w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/test", nil)
req.Header.Set("User-Agent", UserAgent)
router.ServeHTTP(w, req)

@@ -380,8 +384,10 @@ func TestLoggingDebugToFileConfig(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, api)

ctx := context.Background()

w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/test42", nil)
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/test42", nil)
req.Header.Set("User-Agent", UserAgent)
api.router.ServeHTTP(w, req)
assert.Equal(t, 404, w.Code)
@@ -430,8 +436,10 @@ func TestLoggingErrorToFileConfig(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, api)

ctx := context.Background()

w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/test42", nil)
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/test42", nil)
req.Header.Set("User-Agent", UserAgent)
api.router.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
17 changes: 10 additions & 7 deletions pkg/apiserver/jwt_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apiserver

import (
"context"
"net/http"
"net/http/httptest"
"strings"
@@ -12,11 +13,13 @@ import (
func TestLogin(t *testing.T) {
router, config := NewAPITest(t)

ctx := context.Background()

body := CreateTestMachine(t, router, "")

// Login with machine not validated yet
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
req.Header.Add("User-Agent", UserAgent)
router.ServeHTTP(w, req)

@@ -25,7 +28,7 @@ func TestLogin(t *testing.T) {

// Login with machine not exist
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test1", "password": "test1"}`))
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test1", "password": "test1"}`))
req.Header.Add("User-Agent", UserAgent)
router.ServeHTTP(w, req)

@@ -34,7 +37,7 @@ func TestLogin(t *testing.T) {

// Login with invalid body
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader("test"))
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader("test"))
req.Header.Add("User-Agent", UserAgent)
router.ServeHTTP(w, req)

@@ -43,7 +46,7 @@ func TestLogin(t *testing.T) {

// Login with invalid format
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test1"}`))
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test1"}`))
req.Header.Add("User-Agent", UserAgent)
router.ServeHTTP(w, req)

@@ -55,7 +58,7 @@ func TestLogin(t *testing.T) {

// Login with invalid password
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test", "password": "test1"}`))
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test", "password": "test1"}`))
req.Header.Add("User-Agent", UserAgent)
router.ServeHTTP(w, req)

@@ -64,7 +67,7 @@ func TestLogin(t *testing.T) {

// Login with valid machine
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
req.Header.Add("User-Agent", UserAgent)
router.ServeHTTP(w, req)

@@ -74,7 +77,7 @@ func TestLogin(t *testing.T) {

// Login with valid machine + scenarios
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test", "password": "test", "scenarios": ["crowdsecurity/test", "crowdsecurity/test2"]}`))
req, _ = http.NewRequestWithContext(ctx, 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)

Loading