2020-11-30 09:37:17 +00:00
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
2022-05-17 10:14:59 +00:00
|
|
|
"path/filepath"
|
2020-11-30 09:37:17 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2021-04-07 09:39:24 +00:00
|
|
|
"time"
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-12-14 13:54:11 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/go-openapi/strfmt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-01-04 16:10:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-12-04 22:06:01 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
2024-01-04 16:10:36 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/version"
|
2023-05-23 08:52:47 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
2023-12-14 13:54:11 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
2020-11-30 09:37:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var testMachineID = "test"
|
|
|
|
var testPassword = strfmt.Password("test")
|
|
|
|
var MachineTest = models.WatcherAuthRequest{
|
|
|
|
MachineID: &testMachineID,
|
|
|
|
Password: &testPassword,
|
|
|
|
}
|
|
|
|
|
2023-05-23 08:52:47 +00:00
|
|
|
var UserAgent = fmt.Sprintf("crowdsec-test/%s", version.Version)
|
2022-03-29 12:20:26 +00:00
|
|
|
var emptyBody = strings.NewReader("")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-05-25 13:32:32 +00:00
|
|
|
func LoadTestConfig(t *testing.T) csconfig.Config {
|
2021-03-24 17:16:17 +00:00
|
|
|
config := csconfig.Config{}
|
2020-11-30 09:37:17 +00:00
|
|
|
maxAge := "1h"
|
|
|
|
flushConfig := csconfig.FlushDBCfg{
|
|
|
|
MaxAge: &maxAge,
|
|
|
|
}
|
2023-05-25 13:32:32 +00:00
|
|
|
|
2022-05-17 10:14:59 +00:00
|
|
|
tempDir, _ := os.MkdirTemp("", "crowdsec_tests")
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-05-25 13:32:32 +00:00
|
|
|
t.Cleanup(func() { os.RemoveAll(tempDir) })
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
dbconfig := csconfig.DatabaseCfg{
|
|
|
|
Type: "sqlite",
|
2022-05-17 10:14:59 +00:00
|
|
|
DbPath: filepath.Join(tempDir, "ent"),
|
2020-11-30 09:37:17 +00:00
|
|
|
Flush: &flushConfig,
|
|
|
|
}
|
|
|
|
apiServerConfig := csconfig.LocalApiServerCfg{
|
|
|
|
ListenURI: "http://127.0.0.1:8080",
|
|
|
|
DbConfig: &dbconfig,
|
|
|
|
ProfilesPath: "./tests/profiles.yaml",
|
2022-01-13 15:46:16 +00:00
|
|
|
ConsoleConfig: &csconfig.ConsoleConfig{
|
|
|
|
ShareManualDecisions: new(bool),
|
|
|
|
ShareTaintedScenarios: new(bool),
|
|
|
|
ShareCustomScenarios: new(bool),
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
apiConfig := csconfig.APICfg{
|
|
|
|
Server: &apiServerConfig,
|
|
|
|
}
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
config.API = &apiConfig
|
2024-01-04 16:10:36 +00:00
|
|
|
err := config.API.Server.LoadProfiles()
|
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2023-05-25 13:32:32 +00:00
|
|
|
func LoadTestConfigForwardedFor(t *testing.T) csconfig.Config {
|
2021-03-24 17:16:17 +00:00
|
|
|
config := csconfig.Config{}
|
2021-02-09 18:10:14 +00:00
|
|
|
maxAge := "1h"
|
|
|
|
flushConfig := csconfig.FlushDBCfg{
|
|
|
|
MaxAge: &maxAge,
|
|
|
|
}
|
2023-05-25 13:32:32 +00:00
|
|
|
|
2022-05-17 10:14:59 +00:00
|
|
|
tempDir, _ := os.MkdirTemp("", "crowdsec_tests")
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-05-25 13:32:32 +00:00
|
|
|
t.Cleanup(func() { os.RemoveAll(tempDir) })
|
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
dbconfig := csconfig.DatabaseCfg{
|
|
|
|
Type: "sqlite",
|
2022-05-17 10:14:59 +00:00
|
|
|
DbPath: filepath.Join(tempDir, "ent"),
|
2021-02-09 18:10:14 +00:00
|
|
|
Flush: &flushConfig,
|
|
|
|
}
|
|
|
|
apiServerConfig := csconfig.LocalApiServerCfg{
|
|
|
|
ListenURI: "http://127.0.0.1:8080",
|
|
|
|
DbConfig: &dbconfig,
|
|
|
|
ProfilesPath: "./tests/profiles.yaml",
|
|
|
|
UseForwardedForHeaders: true,
|
2022-01-17 16:18:12 +00:00
|
|
|
TrustedProxies: &[]string{"0.0.0.0/0"},
|
2022-01-13 15:46:16 +00:00
|
|
|
ConsoleConfig: &csconfig.ConsoleConfig{
|
|
|
|
ShareManualDecisions: new(bool),
|
|
|
|
ShareTaintedScenarios: new(bool),
|
|
|
|
ShareCustomScenarios: new(bool),
|
|
|
|
},
|
2021-02-09 18:10:14 +00:00
|
|
|
}
|
|
|
|
apiConfig := csconfig.APICfg{
|
|
|
|
Server: &apiServerConfig,
|
|
|
|
}
|
|
|
|
config.API = &apiConfig
|
2024-01-04 16:10:36 +00:00
|
|
|
err := config.API.Server.LoadProfiles()
|
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func NewAPIServer(t *testing.T) (*APIServer, csconfig.Config) {
|
2023-05-25 13:32:32 +00:00
|
|
|
config := LoadTestConfig(t)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
os.Remove("./ent")
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
apiServer, err := NewServer(config.API.Server)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
log.Printf("Creating new API server")
|
|
|
|
gin.SetMode(gin.TestMode)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
return apiServer, config
|
2021-08-25 09:43:29 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func NewAPITest(t *testing.T) (*gin.Engine, csconfig.Config) {
|
|
|
|
apiServer, config := NewAPIServer(t)
|
|
|
|
|
|
|
|
err := apiServer.InitController()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
router, err := apiServer.Router()
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
return router, config
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func NewAPITestForwardedFor(t *testing.T) (*gin.Engine, csconfig.Config) {
|
2023-05-25 13:32:32 +00:00
|
|
|
config := LoadTestConfigForwardedFor(t)
|
2021-02-09 18:10:14 +00:00
|
|
|
|
|
|
|
os.Remove("./ent")
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
apiServer, err := NewServer(config.API.Server)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-25 09:43:29 +00:00
|
|
|
err = apiServer.InitController()
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
log.Printf("Creating new API server")
|
|
|
|
gin.SetMode(gin.TestMode)
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
router, err := apiServer.Router()
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
return router, config
|
2021-02-09 18:10:14 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func ValidateMachine(t *testing.T, machineID string, config *csconfig.DatabaseCfg) {
|
2022-05-17 10:14:59 +00:00
|
|
|
dbClient, err := database.NewClient(config)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
err = dbClient.ValidateMachine(machineID)
|
|
|
|
require.NoError(t, err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func GetMachineIP(t *testing.T, machineID string, config *csconfig.DatabaseCfg) string {
|
2022-05-17 10:14:59 +00:00
|
|
|
dbClient, err := database.NewClient(config)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
machines, err := dbClient.ListMachines()
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
for _, machine := range machines {
|
|
|
|
if machine.MachineId == machineID {
|
2024-01-04 16:10:36 +00:00
|
|
|
return machine.IpAddress
|
2021-02-09 18:10:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
return ""
|
2021-02-09 18:10:14 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func GetAlertReaderFromFile(t *testing.T, path string) *strings.Reader {
|
2022-03-29 12:20:26 +00:00
|
|
|
alertContentBytes, err := os.ReadFile(path)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
alerts := make([]*models.Alert, 0)
|
2024-01-04 16:10:36 +00:00
|
|
|
err = json.Unmarshal(alertContentBytes, &alerts)
|
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
for _, alert := range alerts {
|
|
|
|
*alert.StartAt = time.Now().UTC().Format(time.RFC3339)
|
|
|
|
*alert.StopAt = time.Now().UTC().Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
|
|
|
|
alertContent, err := json.Marshal(alerts)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2023-12-14 13:54:11 +00:00
|
|
|
return strings.NewReader(string(alertContent))
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func readDecisionsGetResp(t *testing.T, resp *httptest.ResponseRecorder) ([]*models.Decision, int) {
|
2022-03-29 12:20:26 +00:00
|
|
|
var response []*models.Decision
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NotNil(t, resp)
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
err := json.Unmarshal(resp.Body.Bytes(), &response)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
return response, resp.Code
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func readDecisionsErrorResp(t *testing.T, resp *httptest.ResponseRecorder) (map[string]string, int) {
|
2022-03-29 12:20:26 +00:00
|
|
|
var response map[string]string
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NotNil(t, resp)
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
err := json.Unmarshal(resp.Body.Bytes(), &response)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
return response, resp.Code
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func readDecisionsDeleteResp(t *testing.T, resp *httptest.ResponseRecorder) (*models.DeleteDecisionResponse, int) {
|
2022-03-29 12:20:26 +00:00
|
|
|
var response models.DeleteDecisionResponse
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NotNil(t, resp)
|
2022-03-29 12:20:26 +00:00
|
|
|
err := json.Unmarshal(resp.Body.Bytes(), &response)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
return &response, resp.Code
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func readDecisionsStreamResp(t *testing.T, resp *httptest.ResponseRecorder) (map[string][]*models.Decision, int) {
|
2022-03-29 12:20:26 +00:00
|
|
|
response := make(map[string][]*models.Decision)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NotNil(t, resp)
|
2022-03-29 12:20:26 +00:00
|
|
|
err := json.Unmarshal(resp.Body.Bytes(), &response)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
return response, resp.Code
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func CreateTestMachine(t *testing.T, router *gin.Engine) string {
|
2020-11-30 09:37:17 +00:00
|
|
|
b, err := json.Marshal(MachineTest)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
body := string(b)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Set("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
return body
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
func CreateTestBouncer(t *testing.T, config *csconfig.DatabaseCfg) string {
|
2022-05-17 10:14:59 +00:00
|
|
|
dbClient, err := database.NewClient(config)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
apiKey, err := middlewares.GenerateAPIKey(keyLength)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2022-06-08 14:05:52 +00:00
|
|
|
_, err = dbClient.CreateBouncer("test", "127.0.0.1", middlewares.HashSHA512(apiKey), types.ApiKeyAuthType)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
return apiKey
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithWrongDBConfig(t *testing.T) {
|
2023-05-25 13:32:32 +00:00
|
|
|
config := LoadTestConfig(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
config.API.Server.DbConfig.Type = "test"
|
|
|
|
apiServer, err := NewServer(config.API.Server)
|
|
|
|
|
2023-12-04 22:06:01 +00:00
|
|
|
cstest.RequireErrorContains(t, err, "unable to init database client: unknown database type 'test'")
|
|
|
|
assert.Nil(t, apiServer)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithWrongFlushConfig(t *testing.T) {
|
2023-05-25 13:32:32 +00:00
|
|
|
config := LoadTestConfig(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
maxItems := -1
|
|
|
|
config.API.Server.DbConfig.Flush.MaxItems = &maxItems
|
|
|
|
apiServer, err := NewServer(config.API.Server)
|
|
|
|
|
2023-12-04 22:06:01 +00:00
|
|
|
cstest.RequireErrorContains(t, err, "max_items can't be zero or negative number")
|
|
|
|
assert.Nil(t, apiServer)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnknownPath(t *testing.T) {
|
2024-01-04 16:10:36 +00:00
|
|
|
router, _ := NewAPITest(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Set("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 404, w.Code)
|
|
|
|
}
|
2021-04-07 09:39:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
ListenURI string `yaml:"listen_uri,omitempty"` //127.0.0.1:8080
|
|
|
|
TLS *TLSCfg `yaml:"tls"`
|
|
|
|
DbConfig *DatabaseCfg `yaml:"-"`
|
|
|
|
LogDir string `yaml:"-"`
|
|
|
|
LogMedia string `yaml:"-"`
|
|
|
|
OnlineClient *OnlineApiClientCfg `yaml:"online_client"`
|
|
|
|
ProfilesPath string `yaml:"profiles_path,omitempty"`
|
|
|
|
Profiles []*ProfileCfg `yaml:"-"`
|
|
|
|
LogLevel *log.Level `yaml:"log_level"`
|
|
|
|
UseForwardedForHeaders bool `yaml:"use_forwarded_for_headers,omitempty"`
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
func TestLoggingDebugToFileConfig(t *testing.T) {
|
|
|
|
/*declare settings*/
|
|
|
|
maxAge := "1h"
|
|
|
|
flushConfig := csconfig.FlushDBCfg{
|
|
|
|
MaxAge: &maxAge,
|
|
|
|
}
|
2023-05-25 13:32:32 +00:00
|
|
|
|
2022-05-17 10:14:59 +00:00
|
|
|
tempDir, _ := os.MkdirTemp("", "crowdsec_tests")
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-05-25 13:32:32 +00:00
|
|
|
t.Cleanup(func() { os.RemoveAll(tempDir) })
|
|
|
|
|
2021-04-07 09:39:24 +00:00
|
|
|
dbconfig := csconfig.DatabaseCfg{
|
|
|
|
Type: "sqlite",
|
2022-05-17 10:14:59 +00:00
|
|
|
DbPath: filepath.Join(tempDir, "ent"),
|
2021-04-07 09:39:24 +00:00
|
|
|
Flush: &flushConfig,
|
|
|
|
}
|
|
|
|
cfg := csconfig.LocalApiServerCfg{
|
|
|
|
ListenURI: "127.0.0.1:8080",
|
|
|
|
LogMedia: "file",
|
2022-05-17 10:14:59 +00:00
|
|
|
LogDir: tempDir,
|
2021-04-07 09:39:24 +00:00
|
|
|
DbConfig: &dbconfig,
|
|
|
|
}
|
2022-05-17 10:14:59 +00:00
|
|
|
expectedFile := fmt.Sprintf("%s/crowdsec_api.log", tempDir)
|
2021-04-07 09:39:24 +00:00
|
|
|
expectedLines := []string{"/test42"}
|
2024-01-04 16:10:36 +00:00
|
|
|
cfg.LogLevel = ptr.Of(log.DebugLevel)
|
2021-04-07 09:39:24 +00:00
|
|
|
|
|
|
|
// Configure logging
|
2024-01-04 16:10:36 +00:00
|
|
|
err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel, cfg.LogMaxSize, cfg.LogMaxFiles, cfg.LogMaxAge, cfg.CompressLogs, false)
|
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2021-04-07 09:39:24 +00:00
|
|
|
api, err := NewServer(&cfg)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, api)
|
2021-04-07 09:39:24 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/test42", nil)
|
2021-04-07 09:39:24 +00:00
|
|
|
req.Header.Set("User-Agent", UserAgent)
|
|
|
|
api.router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, 404, w.Code)
|
|
|
|
//wait for the request to happen
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
//check file content
|
2022-03-29 12:20:26 +00:00
|
|
|
data, err := os.ReadFile(expectedFile)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2021-04-07 09:39:24 +00:00
|
|
|
|
|
|
|
for _, expectedStr := range expectedLines {
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Contains(t, string(data), expectedStr)
|
2021-04-07 09:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoggingErrorToFileConfig(t *testing.T) {
|
|
|
|
/*declare settings*/
|
|
|
|
maxAge := "1h"
|
|
|
|
flushConfig := csconfig.FlushDBCfg{
|
|
|
|
MaxAge: &maxAge,
|
|
|
|
}
|
2023-05-25 13:32:32 +00:00
|
|
|
|
2022-05-17 10:14:59 +00:00
|
|
|
tempDir, _ := os.MkdirTemp("", "crowdsec_tests")
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-05-25 13:32:32 +00:00
|
|
|
t.Cleanup(func() { os.RemoveAll(tempDir) })
|
|
|
|
|
2021-04-07 09:39:24 +00:00
|
|
|
dbconfig := csconfig.DatabaseCfg{
|
|
|
|
Type: "sqlite",
|
2022-05-17 10:14:59 +00:00
|
|
|
DbPath: filepath.Join(tempDir, "ent"),
|
2021-04-07 09:39:24 +00:00
|
|
|
Flush: &flushConfig,
|
|
|
|
}
|
|
|
|
cfg := csconfig.LocalApiServerCfg{
|
|
|
|
ListenURI: "127.0.0.1:8080",
|
|
|
|
LogMedia: "file",
|
2022-05-17 10:14:59 +00:00
|
|
|
LogDir: tempDir,
|
2021-04-07 09:39:24 +00:00
|
|
|
DbConfig: &dbconfig,
|
|
|
|
}
|
2022-05-17 10:14:59 +00:00
|
|
|
expectedFile := fmt.Sprintf("%s/crowdsec_api.log", tempDir)
|
2024-01-04 16:10:36 +00:00
|
|
|
cfg.LogLevel = ptr.Of(log.ErrorLevel)
|
2021-04-07 09:39:24 +00:00
|
|
|
|
|
|
|
// Configure logging
|
2024-01-04 16:10:36 +00:00
|
|
|
err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel, cfg.LogMaxSize, cfg.LogMaxFiles, cfg.LogMaxAge, cfg.CompressLogs, false)
|
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
api, err := NewServer(&cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, api)
|
2021-04-07 09:39:24 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/test42", nil)
|
2021-04-07 09:39:24 +00:00
|
|
|
req.Header.Set("User-Agent", UserAgent)
|
|
|
|
api.router.ServeHTTP(w, req)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2021-04-07 09:39:24 +00:00
|
|
|
//wait for the request to happen
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
//check file content
|
2022-03-29 12:20:26 +00:00
|
|
|
x, err := os.ReadFile(expectedFile)
|
2024-01-04 16:10:36 +00:00
|
|
|
if err == nil {
|
|
|
|
require.Empty(t, x)
|
2021-04-07 09:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Remove("./crowdsec.log")
|
|
|
|
os.Remove(expectedFile)
|
|
|
|
}
|