2020-11-30 09:37:17 +00:00
package apiserver
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
2021-08-25 09:43:29 +00:00
"sync"
2020-11-30 09:37:17 +00:00
"testing"
"time"
2021-08-25 09:43:29 +00:00
"github.com/crowdsecurity/crowdsec/pkg/csplugin"
2020-11-30 09:37:17 +00:00
"github.com/crowdsecurity/crowdsec/pkg/models"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func InitMachineTest ( ) ( * gin . Engine , models . WatcherAuthResponse , error ) {
router , err := NewAPITest ( )
if err != nil {
return nil , models . WatcherAuthResponse { } , fmt . Errorf ( "unable to run local API: %s" , err )
}
2021-08-25 09:43:29 +00:00
loginResp , err := LoginToTestAPI ( router )
2020-11-30 09:37:17 +00:00
if err != nil {
return nil , models . WatcherAuthResponse { } , fmt . Errorf ( "%s" , err . Error ( ) )
}
2021-08-25 09:43:29 +00:00
return router , loginResp , nil
}
2020-11-30 09:37:17 +00:00
2021-08-25 09:43:29 +00:00
func LoginToTestAPI ( router * gin . Engine ) ( models . WatcherAuthResponse , error ) {
body , err := CreateTestMachine ( router )
if err != nil {
return models . WatcherAuthResponse { } , fmt . Errorf ( "%s" , err . Error ( ) )
}
2020-11-30 09:37:17 +00:00
err = ValidateMachine ( "test" )
if err != nil {
log . Fatalln ( err . Error ( ) )
}
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "POST" , "/v1/watchers/login" , strings . NewReader ( body ) )
req . Header . Add ( "User-Agent" , UserAgent )
router . ServeHTTP ( w , req )
loginResp := models . WatcherAuthResponse { }
err = json . NewDecoder ( w . Body ) . Decode ( & loginResp )
if err != nil {
2021-08-25 09:43:29 +00:00
return models . WatcherAuthResponse { } , fmt . Errorf ( "%s" , err . Error ( ) )
2020-11-30 09:37:17 +00:00
}
2021-08-25 09:43:29 +00:00
return loginResp , nil
}
2020-11-30 09:37:17 +00:00
2021-08-25 09:43:29 +00:00
func AddAuthHeaders ( request * http . Request , authResponse models . WatcherAuthResponse ) {
request . Header . Add ( "User-Agent" , UserAgent )
request . Header . Add ( "Authorization" , fmt . Sprintf ( "Bearer %s" , authResponse . Token ) )
2020-11-30 09:37:17 +00:00
}
func TestSimulatedAlert ( t * testing . T ) {
router , loginResp , err := InitMachineTest ( )
if err != nil {
log . Fatalln ( err . Error ( ) )
}
alertContentBytes , err := ioutil . ReadFile ( "./tests/alert_minibulk+simul.json" )
if err != nil {
log . Fatal ( err )
}
alertContent := string ( alertContentBytes )
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( alertContent ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
//exclude decision in simulation mode
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?simulated=false" , strings . NewReader ( alertContent ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , ` "message":"Ip 91.121.79.178 performed crowdsecurity/ssh-bf (6 events over ` )
assert . NotContains ( t , w . Body . String ( ) , ` "message":"Ip 91.121.79.179 performed crowdsecurity/ssh-bf (6 events over ` )
//include decision in simulation mode
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?simulated=true" , strings . NewReader ( alertContent ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , ` "message":"Ip 91.121.79.178 performed crowdsecurity/ssh-bf (6 events over ` )
assert . Contains ( t , w . Body . String ( ) , ` "message":"Ip 91.121.79.179 performed crowdsecurity/ssh-bf (6 events over ` )
}
func TestCreateAlert ( t * testing . T ) {
router , loginResp , err := InitMachineTest ( )
if err != nil {
log . Fatalln ( err . Error ( ) )
}
// Create Alert with invalid format
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( "test" ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
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
alertContentBytes , err := ioutil . ReadFile ( "./tests/invalidAlert_sample.json" )
if err != nil {
log . Fatal ( err )
}
alertContent := string ( alertContentBytes )
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( alertContent ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 500 , w . Code )
2022-01-11 15:45:34 +00:00
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 ( ) )
2020-11-30 09:37:17 +00:00
// Create Valid Alert
alertContentBytes , err = ioutil . ReadFile ( "./tests/alert_sample.json" )
if err != nil {
log . Fatal ( err )
}
alertContent = string ( alertContentBytes )
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( alertContent ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
2020-11-30 15:15:07 +00:00
assert . Equal ( t , 201 , w . Code )
2020-11-30 09:37:17 +00:00
assert . Equal ( t , "[\"1\"]" , w . Body . String ( ) )
}
2021-08-25 09:43:29 +00:00
func TestCreateAlertChannels ( t * testing . T ) {
apiServer , err := NewAPIServer ( )
if err != nil {
log . Fatalln ( err . Error ( ) )
}
apiServer . controller . PluginChannel = make ( chan csplugin . ProfileAlert )
apiServer . InitController ( )
loginResp , err := LoginToTestAPI ( apiServer . router )
if err != nil {
log . Fatalln ( err . Error ( ) )
}
alertContentBytes , err := ioutil . ReadFile ( "./tests/alert_ssh-bf.json" )
if err != nil {
log . Fatal ( err )
}
alertContent := string ( alertContentBytes )
var pd csplugin . ProfileAlert
var wg sync . WaitGroup
wg . Add ( 1 )
go func ( ) {
pd = <- apiServer . controller . PluginChannel
wg . Done ( )
} ( )
go func ( ) {
for {
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( alertContent ) )
AddAuthHeaders ( req , loginResp )
apiServer . controller . Router . ServeHTTP ( w , req )
break
}
} ( )
wg . Wait ( )
assert . Equal ( t , len ( pd . Alert . Decisions ) , 1 )
apiServer . Close ( )
}
2020-11-30 09:37:17 +00:00
func TestAlertListFilters ( t * testing . T ) {
router , loginResp , err := InitMachineTest ( )
if err != nil {
log . Fatalln ( err . Error ( ) )
}
alertContentBytes , err := ioutil . ReadFile ( "./tests/alert_ssh-bf.json" )
if err != nil {
log . Fatal ( err )
}
alerts := make ( [ ] * models . Alert , 0 )
if err := json . Unmarshal ( alertContentBytes , & alerts ) ; err != nil {
log . Fatal ( err )
}
for _ , alert := range alerts {
2022-01-19 13:56:05 +00:00
* alert . StartAt = time . Now ( ) . UTC ( ) . Format ( time . RFC3339 )
* alert . StopAt = time . Now ( ) . UTC ( ) . Format ( time . RFC3339 )
2020-11-30 09:37:17 +00:00
}
alertContent , err := json . Marshal ( alerts )
if err != nil {
log . Fatal ( err )
}
//create one alert
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( string ( alertContent ) ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
//bad filter
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?test=test" , strings . NewReader ( string ( alertContent ) ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 500 , w . Code )
assert . Equal ( t , "{\"message\":\"Filter parameter 'test' is unknown (=test): invalid filter\"}" , w . Body . String ( ) )
//get without filters
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
//check alert and decision
assert . Contains ( t , w . Body . String ( ) , "Ip 91.121.79.195 performed 'crowdsecurity/ssh-bf' (6 events over " )
2021-01-14 15:27:45 +00:00
assert . Contains ( t , w . Body . String ( ) , ` scope":"Ip","simulated":false,"type":"ban","value":"91.121.79.195" ` )
2020-11-30 09:37:17 +00:00
//test decision_type filter (ok)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?decision_type=ban" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , "Ip 91.121.79.195 performed 'crowdsecurity/ssh-bf' (6 events over " )
2021-01-14 15:27:45 +00:00
assert . Contains ( t , w . Body . String ( ) , ` scope":"Ip","simulated":false,"type":"ban","value":"91.121.79.195" ` )
2020-11-30 09:37:17 +00:00
//test decision_type filter (bad value)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?decision_type=ratata" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Equal ( t , "null" , w . Body . String ( ) )
//test scope (ok)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?scope=Ip" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , "Ip 91.121.79.195 performed 'crowdsecurity/ssh-bf' (6 events over " )
2021-01-14 15:27:45 +00:00
assert . Contains ( t , w . Body . String ( ) , ` scope":"Ip","simulated":false,"type":"ban","value":"91.121.79.195" ` )
2020-11-30 09:37:17 +00:00
//test scope (bad value)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?scope=rarara" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Equal ( t , "null" , w . Body . String ( ) )
//test scenario (ok)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?scenario=crowdsecurity/ssh-bf" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , "Ip 91.121.79.195 performed 'crowdsecurity/ssh-bf' (6 events over " )
2021-01-14 15:27:45 +00:00
assert . Contains ( t , w . Body . String ( ) , ` scope":"Ip","simulated":false,"type":"ban","value":"91.121.79.195" ` )
2020-11-30 09:37:17 +00:00
//test scenario (bad value)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?scenario=crowdsecurity/nope" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Equal ( t , "null" , w . Body . String ( ) )
//test ip (ok)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?ip=91.121.79.195" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , "Ip 91.121.79.195 performed 'crowdsecurity/ssh-bf' (6 events over " )
2021-01-14 15:27:45 +00:00
assert . Contains ( t , w . Body . String ( ) , ` scope":"Ip","simulated":false,"type":"ban","value":"91.121.79.195" ` )
2020-11-30 09:37:17 +00:00
//test ip (bad value)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?ip=99.122.77.195" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Equal ( t , "null" , w . Body . String ( ) )
//test ip (invalid value)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?ip=gruueq" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 500 , w . Code )
2021-01-14 15:27:45 +00:00
assert . Equal ( t , ` { "message":"unable to convert 'gruueq' to int: invalid address: invalid ip address / range"} ` , w . Body . String ( ) )
2020-11-30 09:37:17 +00:00
//test range (ok)
w = httptest . NewRecorder ( )
2021-01-14 15:27:45 +00:00
req , _ = http . NewRequest ( "GET" , "/v1/alerts?range=91.121.79.0/24&contains=false" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , "Ip 91.121.79.195 performed 'crowdsecurity/ssh-bf' (6 events over " )
2021-01-14 15:27:45 +00:00
assert . Contains ( t , w . Body . String ( ) , ` scope":"Ip","simulated":false,"type":"ban","value":"91.121.79.195" ` )
2020-11-30 09:37:17 +00:00
//test range
w = httptest . NewRecorder ( )
2021-01-14 15:27:45 +00:00
req , _ = http . NewRequest ( "GET" , "/v1/alerts?range=99.122.77.0/24&contains=false" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Equal ( t , "null" , w . Body . String ( ) )
//test range (invalid value)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?range=ratata" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 500 , w . Code )
2021-01-14 15:27:45 +00:00
assert . Equal ( t , ` { "message":"unable to convert 'ratata' to int: invalid address: invalid ip address / range"} ` , w . Body . String ( ) )
2020-11-30 09:37:17 +00:00
//test since (ok)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?since=1h" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , "Ip 91.121.79.195 performed 'crowdsecurity/ssh-bf' (6 events over " )
2021-01-14 15:27:45 +00:00
assert . Contains ( t , w . Body . String ( ) , ` scope":"Ip","simulated":false,"type":"ban","value":"91.121.79.195" ` )
2020-11-30 09:37:17 +00:00
//test since (ok but yelds no results)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?since=1ns" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Equal ( t , "null" , w . Body . String ( ) )
//test since (invalid value)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?since=1zuzu" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 500 , w . Code )
assert . Contains ( t , w . Body . String ( ) , ` { "message":"while parsing duration: time: unknown unit ` )
//test until (ok)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?until=1ns" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , "Ip 91.121.79.195 performed 'crowdsecurity/ssh-bf' (6 events over " )
2021-01-14 15:27:45 +00:00
assert . Contains ( t , w . Body . String ( ) , ` scope":"Ip","simulated":false,"type":"ban","value":"91.121.79.195" ` )
2020-11-30 09:37:17 +00:00
//test until (ok but no return)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?until=1m" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Equal ( t , "null" , w . Body . String ( ) )
//test until (invalid value)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?until=1zuzu" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 500 , w . Code )
assert . Contains ( t , w . Body . String ( ) , ` { "message":"while parsing duration: time: unknown unit ` )
//test simulated (ok)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?simulated=true" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , "Ip 91.121.79.195 performed 'crowdsecurity/ssh-bf' (6 events over " )
2021-01-14 15:27:45 +00:00
assert . Contains ( t , w . Body . String ( ) , ` scope":"Ip","simulated":false,"type":"ban","value":"91.121.79.195" ` )
2020-11-30 09:37:17 +00:00
//test simulated (ok)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?simulated=false" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , "Ip 91.121.79.195 performed 'crowdsecurity/ssh-bf' (6 events over " )
2021-01-14 15:27:45 +00:00
assert . Contains ( t , w . Body . String ( ) , ` scope":"Ip","simulated":false,"type":"ban","value":"91.121.79.195" ` )
2020-11-30 09:37:17 +00:00
//test has active decision
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?has_active_decision=true" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , "Ip 91.121.79.195 performed 'crowdsecurity/ssh-bf' (6 events over " )
2021-01-14 15:27:45 +00:00
assert . Contains ( t , w . Body . String ( ) , ` scope":"Ip","simulated":false,"type":"ban","value":"91.121.79.195" ` )
2020-11-30 09:37:17 +00:00
//test has active decision
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?has_active_decision=false" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Equal ( t , "null" , w . Body . String ( ) )
//test has active decision (invalid value)
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?has_active_decision=ratatqata" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 500 , w . Code )
assert . Equal ( t , ` { "message":"'ratatqata' is not a boolean: strconv.ParseBool: parsing \"ratatqata\": invalid syntax: unable to parse type"} ` , w . Body . String ( ) )
}
func TestAlertBulkInsert ( t * testing . T ) {
router , loginResp , err := InitMachineTest ( )
if err != nil {
log . Fatalln ( err . Error ( ) )
}
//insert a bulk of 20 alerts to trigger bulk insert
alertContentBytes , err := ioutil . ReadFile ( "./tests/alert_bulk.json" )
if err != nil {
log . Fatal ( err )
}
alertContent := string ( alertContentBytes )
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( alertContent ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts" , strings . NewReader ( alertContent ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
}
func TestListAlert ( t * testing . T ) {
router , loginResp , err := InitMachineTest ( )
if err != nil {
log . Fatalln ( err . Error ( ) )
}
alertContentBytes , err := ioutil . ReadFile ( "./tests/alert_sample.json" )
if err != nil {
log . Fatal ( err )
}
alertContent := string ( alertContentBytes )
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( alertContent ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
// List Alert with invalid filter
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts?test=test" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 500 , w . Code )
assert . Equal ( t , "{\"message\":\"Filter parameter 'test' is unknown (=test): invalid filter\"}" , w . Body . String ( ) )
// List Alert
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "GET" , "/v1/alerts" , nil )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Contains ( t , w . Body . String ( ) , "crowdsecurity/test" )
}
func TestCreateAlertErrors ( t * testing . T ) {
router , loginResp , err := InitMachineTest ( )
if err != nil {
log . Fatalln ( err . Error ( ) )
}
alertContentBytes , err := ioutil . ReadFile ( "./tests/alert_sample.json" )
if err != nil {
log . Fatal ( err )
}
alertContent := string ( alertContentBytes )
//test invalid bearer
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( alertContent ) )
req . Header . Add ( "User-Agent" , UserAgent )
req . Header . Add ( "Authorization" , fmt . Sprintf ( "Bearer %s" , "ratata" ) )
router . ServeHTTP ( w , req )
assert . Equal ( t , 401 , w . Code )
//test invalid bearer
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( alertContent ) )
req . Header . Add ( "User-Agent" , UserAgent )
req . Header . Add ( "Authorization" , fmt . Sprintf ( "Bearer %s" , loginResp . Token + "s" ) )
router . ServeHTTP ( w , req )
assert . Equal ( t , 401 , w . Code )
}
func TestDeleteAlert ( t * testing . T ) {
router , loginResp , err := InitMachineTest ( )
if err != nil {
log . Fatalln ( err . Error ( ) )
}
alertContentBytes , err := ioutil . ReadFile ( "./tests/alert_sample.json" )
if err != nil {
log . Fatal ( err )
}
alertContent := string ( alertContentBytes )
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( alertContent ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
router . ServeHTTP ( w , req )
// Fail Delete Alert
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "DELETE" , "/v1/alerts" , strings . NewReader ( "" ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
req . RemoteAddr = "127.0.0.2:4242"
router . ServeHTTP ( w , req )
assert . Equal ( t , 403 , w . Code )
assert . Equal ( t , ` { "message":"access forbidden from this IP (127.0.0.2)"} ` , w . Body . String ( ) )
// Delete Alert
w = httptest . NewRecorder ( )
req , _ = http . NewRequest ( "DELETE" , "/v1/alerts" , strings . NewReader ( "" ) )
2021-08-25 09:43:29 +00:00
AddAuthHeaders ( req , loginResp )
2020-11-30 09:37:17 +00:00
req . RemoteAddr = "127.0.0.1:4242"
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
2020-11-30 15:15:07 +00:00
assert . Equal ( t , ` { "nbDeleted":"1"} ` , w . Body . String ( ) )
2020-11-30 09:37:17 +00:00
}
2022-03-16 16:28:34 +00:00
func TestDeleteAlertTrustedIPS ( t * testing . T ) {
cfg := LoadTestConfig ( )
// IPv6 mocking doesn't seem to work.
// cfg.API.Server.TrustedIPs = []string{"1.2.3.4", "1.2.4.0/24", "::"}
cfg . API . Server . TrustedIPs = [ ] string { "1.2.3.4" , "1.2.4.0/24" }
cfg . API . Server . ListenURI = "::8080"
server , err := NewServer ( cfg . API . Server )
if err != nil {
log . Fatal ( err . Error ( ) )
}
err = server . InitController ( )
if err != nil {
log . Fatal ( err . Error ( ) )
}
router , err := server . Router ( )
if err != nil {
log . Fatal ( err . Error ( ) )
}
loginResp , err := LoginToTestAPI ( router )
if err != nil {
log . Fatal ( err . Error ( ) )
}
insertAlert := func ( ) {
alertContentBytes , err := ioutil . ReadFile ( "./tests/alert_sample.json" )
if err != nil {
log . Fatal ( err )
}
alertContent := string ( alertContentBytes )
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "POST" , "/v1/alerts" , strings . NewReader ( alertContent ) )
AddAuthHeaders ( req , loginResp )
router . ServeHTTP ( w , req )
}
assertAlertDeleteFailedFromIP := func ( ip string ) {
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "DELETE" , "/v1/alerts" , strings . NewReader ( "" ) )
AddAuthHeaders ( req , loginResp )
req . RemoteAddr = ip + ":1234"
router . ServeHTTP ( w , req )
assert . Equal ( t , 403 , w . Code )
assert . Contains ( t , w . Body . String ( ) , fmt . Sprintf ( ` { "message":"access forbidden from this IP (%s)"} ` , ip ) )
}
assertAlertDeletedFromIP := func ( ip string ) {
w := httptest . NewRecorder ( )
req , _ := http . NewRequest ( "DELETE" , "/v1/alerts" , strings . NewReader ( "" ) )
AddAuthHeaders ( req , loginResp )
req . RemoteAddr = ip + ":1234"
router . ServeHTTP ( w , req )
assert . Equal ( t , 200 , w . Code )
assert . Equal ( t , ` { "nbDeleted":"1"} ` , w . Body . String ( ) )
}
insertAlert ( )
assertAlertDeleteFailedFromIP ( "4.3.2.1" )
assertAlertDeletedFromIP ( "1.2.3.4" )
insertAlert ( )
assertAlertDeletedFromIP ( "1.2.4.0" )
insertAlert ( )
assertAlertDeletedFromIP ( "1.2.4.1" )
insertAlert ( )
assertAlertDeletedFromIP ( "1.2.4.255" )
insertAlert ( )
assertAlertDeletedFromIP ( "127.0.0.1" )
}