2020-11-30 15:15:07 +00:00
|
|
|
package apiclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-17 11:36:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-06-22 13:01:34 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
|
|
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/version"
|
2023-06-22 13:01:34 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
2020-11-30 15:15:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlertsListAsMachine(t *testing.T) {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
|
|
|
|
mux, urlx, teardown := setup()
|
|
|
|
mux.HandleFunc("/watchers/login", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(`{"code": 200, "expire": "2030-01-02T15:04:05Z", "token": "oklol"}`))
|
|
|
|
})
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
log.Printf("URL is %s", urlx)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
apiURL, err := url.Parse(urlx + "/")
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
client, err := NewClient(&Config{
|
|
|
|
MachineID: "test_login",
|
|
|
|
Password: "test_password",
|
2023-05-23 08:52:47 +00:00
|
|
|
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
2020-11-30 15:15:07 +00:00
|
|
|
URL: apiURL,
|
|
|
|
VersionPrefix: "v1",
|
|
|
|
})
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-30 15:15:07 +00:00
|
|
|
|
|
|
|
defer teardown()
|
|
|
|
|
|
|
|
mux.HandleFunc("/alerts", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.RawQuery == "ip=1.2.3.4" {
|
|
|
|
testMethod(t, r, "GET")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprintf(w, `null`)
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
testMethod(t, r, "GET")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprint(w, `[
|
|
|
|
{"capacity":5,"created_at":"2020-11-28T10:20:47+01:00",
|
|
|
|
"decisions":[
|
|
|
|
{"duration":"59m49.264032632s",
|
|
|
|
"id":1,
|
|
|
|
"origin":"crowdsec",
|
|
|
|
"scenario":"crowdsecurity/ssh-bf",
|
|
|
|
"scope":"Ip",
|
|
|
|
"simulated":false,
|
|
|
|
"type":"ban",
|
|
|
|
"value":"1.1.1.172"}
|
|
|
|
],
|
|
|
|
"events":[
|
|
|
|
{"meta":[
|
|
|
|
{"key":"target_user","value":"netflix"},
|
|
|
|
{"key":"service","value":"ssh"}
|
|
|
|
],
|
|
|
|
"timestamp":"2020-11-28 10:20:46 +0000 UTC"},
|
|
|
|
{"meta":[
|
|
|
|
{"key":"target_user","value":"netflix"},
|
|
|
|
{"key":"service","value":"ssh"}
|
|
|
|
],
|
|
|
|
"timestamp":"2020-11-28 10:20:46 +0000 UTC"}
|
|
|
|
],
|
|
|
|
"events_count":6,
|
|
|
|
"id":1,
|
|
|
|
"labels":null,
|
|
|
|
"leakspeed":"10s",
|
|
|
|
"machine_id":"test",
|
|
|
|
"message":"Ip 1.1.1.172 performed 'crowdsecurity/ssh-bf' (6 events over 2.920062ms) at 2020-11-28 10:20:46.845619968 +0100 CET m=+5.903899761",
|
|
|
|
"scenario":"crowdsecurity/ssh-bf",
|
|
|
|
"scenario_hash":"4441dcff07020f6690d998b7101e642359ba405c2abb83565bbbdcee36de280f",
|
|
|
|
"scenario_version":"0.1",
|
|
|
|
"simulated":false,
|
|
|
|
"source":{
|
|
|
|
"as_name":"Cloudflare Inc",
|
|
|
|
"cn":"AU",
|
|
|
|
"ip":"1.1.1.172",
|
|
|
|
"latitude":-37.7,
|
|
|
|
"longitude":145.1833,
|
|
|
|
"range":"1.1.1.0/24",
|
|
|
|
"scope":"Ip",
|
|
|
|
"value":"1.1.1.172"
|
|
|
|
},
|
|
|
|
"start_at":"2020-11-28 10:20:46.842701127 +0100 +0100",
|
|
|
|
"stop_at":"2020-11-28 10:20:46.845621385 +0100 +0100"
|
|
|
|
}
|
|
|
|
]`)
|
|
|
|
})
|
|
|
|
|
|
|
|
tscenario := "crowdsecurity/ssh-bf"
|
|
|
|
tscope := "Ip"
|
|
|
|
tvalue := "1.1.1.172"
|
|
|
|
ttimestamp := "2020-11-28 10:20:46 +0000 UTC"
|
|
|
|
tmessage := "Ip 1.1.1.172 performed 'crowdsecurity/ssh-bf' (6 events over 2.920062ms) at 2020-11-28 10:20:46.845619968 +0100 CET m=+5.903899761"
|
|
|
|
|
|
|
|
expected := models.GetAlertsResponse{
|
|
|
|
&models.Alert{
|
2024-01-04 16:10:36 +00:00
|
|
|
Capacity: ptr.Of(int32(5)),
|
2020-11-30 15:15:07 +00:00
|
|
|
CreatedAt: "2020-11-28T10:20:47+01:00",
|
|
|
|
Decisions: []*models.Decision{
|
2023-03-09 10:56:02 +00:00
|
|
|
{
|
2024-01-04 16:10:36 +00:00
|
|
|
Duration: ptr.Of("59m49.264032632s"),
|
2020-11-30 15:15:07 +00:00
|
|
|
ID: 1,
|
2024-01-04 16:10:36 +00:00
|
|
|
Origin: ptr.Of("crowdsec"),
|
2020-11-30 15:15:07 +00:00
|
|
|
Scenario: &tscenario,
|
|
|
|
|
|
|
|
Scope: &tscope,
|
2024-01-04 16:10:36 +00:00
|
|
|
Simulated: ptr.Of(false),
|
|
|
|
Type: ptr.Of("ban"),
|
2020-11-30 15:15:07 +00:00
|
|
|
Value: &tvalue,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Events: []*models.Event{
|
2023-03-09 10:56:02 +00:00
|
|
|
{
|
2020-11-30 15:15:07 +00:00
|
|
|
Meta: models.Meta{
|
|
|
|
&models.MetaItems0{
|
|
|
|
Key: "target_user",
|
|
|
|
Value: "netflix",
|
|
|
|
},
|
|
|
|
&models.MetaItems0{
|
|
|
|
Key: "service",
|
|
|
|
Value: "ssh",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Timestamp: &ttimestamp,
|
2023-03-09 10:56:02 +00:00
|
|
|
}, {
|
2020-11-30 15:15:07 +00:00
|
|
|
Meta: models.Meta{
|
|
|
|
&models.MetaItems0{
|
|
|
|
Key: "target_user",
|
|
|
|
Value: "netflix",
|
|
|
|
},
|
|
|
|
&models.MetaItems0{
|
|
|
|
Key: "service",
|
|
|
|
Value: "ssh",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Timestamp: &ttimestamp,
|
|
|
|
},
|
|
|
|
},
|
2024-01-04 16:10:36 +00:00
|
|
|
EventsCount: ptr.Of(int32(6)),
|
2020-11-30 15:15:07 +00:00
|
|
|
ID: 1,
|
2024-01-04 16:10:36 +00:00
|
|
|
Leakspeed: ptr.Of("10s"),
|
2020-11-30 15:15:07 +00:00
|
|
|
MachineID: "test",
|
|
|
|
Message: &tmessage,
|
|
|
|
Remediation: false,
|
|
|
|
Scenario: &tscenario,
|
2024-01-04 16:10:36 +00:00
|
|
|
ScenarioHash: ptr.Of("4441dcff07020f6690d998b7101e642359ba405c2abb83565bbbdcee36de280f"),
|
|
|
|
ScenarioVersion: ptr.Of("0.1"),
|
|
|
|
Simulated: ptr.Of(false),
|
2020-11-30 15:15:07 +00:00
|
|
|
Source: &models.Source{
|
|
|
|
AsName: "Cloudflare Inc",
|
|
|
|
AsNumber: "",
|
|
|
|
Cn: "AU",
|
|
|
|
IP: "1.1.1.172",
|
|
|
|
Latitude: -37.7,
|
|
|
|
Longitude: 145.1833,
|
|
|
|
Range: "1.1.1.0/24",
|
|
|
|
Scope: &tscope,
|
|
|
|
Value: &tvalue,
|
|
|
|
},
|
2024-01-04 16:10:36 +00:00
|
|
|
StartAt: ptr.Of("2020-11-28 10:20:46.842701127 +0100 +0100"),
|
|
|
|
StopAt: ptr.Of("2020-11-28 10:20:46.845621385 +0100 +0100"),
|
2020-11-30 15:15:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
//log.Debugf("data : -> %s", spew.Sdump(alerts))
|
|
|
|
//log.Debugf("resp : -> %s", spew.Sdump(resp))
|
|
|
|
//log.Debugf("expected : -> %s", spew.Sdump(expected))
|
|
|
|
//first one returns data
|
|
|
|
alerts, resp, err := client.Alerts.List(context.Background(), AlertsListOpts{})
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, resp.Response.StatusCode)
|
|
|
|
assert.Equal(t, expected, *alerts)
|
2020-11-30 15:15:07 +00:00
|
|
|
|
|
|
|
//this one doesn't
|
2024-01-04 16:10:36 +00:00
|
|
|
filter := AlertsListOpts{IPEquals: ptr.Of("1.2.3.4")}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
alerts, resp, err = client.Alerts.List(context.Background(), filter)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, resp.Response.StatusCode)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Empty(t, *alerts)
|
2020-11-30 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlertsGetAsMachine(t *testing.T) {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
|
|
|
|
mux, urlx, teardown := setup()
|
|
|
|
mux.HandleFunc("/watchers/login", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(`{"code": 200, "expire": "2030-01-02T15:04:05Z", "token": "oklol"}`))
|
|
|
|
})
|
|
|
|
log.Printf("URL is %s", urlx)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
apiURL, err := url.Parse(urlx + "/")
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
client, err := NewClient(&Config{
|
|
|
|
MachineID: "test_login",
|
|
|
|
Password: "test_password",
|
2023-05-23 08:52:47 +00:00
|
|
|
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
2020-11-30 15:15:07 +00:00
|
|
|
URL: apiURL,
|
|
|
|
VersionPrefix: "v1",
|
|
|
|
})
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-30 15:15:07 +00:00
|
|
|
|
|
|
|
defer teardown()
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
mux.HandleFunc("/alerts/2", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
testMethod(t, r, "GET")
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Fprintf(w, `{"message":"object not found"}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/alerts/1", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
testMethod(t, r, "GET")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprint(w, `{"capacity":5,"created_at":"2020-11-28T10:20:47+01:00",
|
|
|
|
"decisions":[
|
|
|
|
{"duration":"59m49.264032632s",
|
|
|
|
"end_ip":16843180,
|
|
|
|
"id":1,
|
|
|
|
"origin":"crowdsec",
|
|
|
|
"scenario":"crowdsecurity/ssh-bf",
|
|
|
|
"scope":"Ip",
|
|
|
|
"simulated":false,
|
|
|
|
"start_ip":16843180,
|
|
|
|
"type":"ban",
|
|
|
|
"value":"1.1.1.172"}
|
|
|
|
],
|
|
|
|
"events":[
|
|
|
|
{"meta":[
|
|
|
|
{"key":"target_user","value":"netflix"},
|
|
|
|
{"key":"service","value":"ssh"}
|
|
|
|
],
|
|
|
|
"timestamp":"2020-11-28 10:20:46 +0000 UTC"},
|
|
|
|
{"meta":[
|
|
|
|
{"key":"target_user","value":"netflix"},
|
|
|
|
{"key":"service","value":"ssh"}
|
|
|
|
],
|
|
|
|
"timestamp":"2020-11-28 10:20:46 +0000 UTC"}
|
|
|
|
],
|
|
|
|
"events_count":6,
|
|
|
|
"id":1,
|
|
|
|
"labels":null,
|
|
|
|
"leakspeed":"10s",
|
|
|
|
"machine_id":"test",
|
|
|
|
"message":"Ip 1.1.1.172 performed 'crowdsecurity/ssh-bf' (6 events over 2.920062ms) at 2020-11-28 10:20:46.845619968 +0100 CET m=+5.903899761",
|
|
|
|
"scenario":"crowdsecurity/ssh-bf",
|
|
|
|
"scenario_hash":"4441dcff07020f6690d998b7101e642359ba405c2abb83565bbbdcee36de280f",
|
|
|
|
"scenario_version":"0.1",
|
|
|
|
"simulated":false,
|
|
|
|
"source":{
|
|
|
|
"as_name":"Cloudflare Inc",
|
|
|
|
"cn":"AU",
|
|
|
|
"ip":"1.1.1.172",
|
|
|
|
"latitude":-37.7,
|
|
|
|
"longitude":145.1833,
|
|
|
|
"range":"1.1.1.0/24",
|
|
|
|
"scope":"Ip",
|
|
|
|
"value":"1.1.1.172"
|
|
|
|
},
|
|
|
|
"start_at":"2020-11-28 10:20:46.842701127 +0100 +0100",
|
|
|
|
"stop_at":"2020-11-28 10:20:46.845621385 +0100 +0100"
|
|
|
|
}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
tscenario := "crowdsecurity/ssh-bf"
|
|
|
|
tscope := "Ip"
|
|
|
|
ttype := "ban"
|
|
|
|
tvalue := "1.1.1.172"
|
|
|
|
ttimestamp := "2020-11-28 10:20:46 +0000 UTC"
|
|
|
|
|
|
|
|
expected := &models.Alert{
|
2024-01-04 16:10:36 +00:00
|
|
|
Capacity: ptr.Of(int32(5)),
|
2020-11-30 15:15:07 +00:00
|
|
|
CreatedAt: "2020-11-28T10:20:47+01:00",
|
|
|
|
Decisions: []*models.Decision{
|
2023-03-09 10:56:02 +00:00
|
|
|
{
|
2024-01-04 16:10:36 +00:00
|
|
|
Duration: ptr.Of("59m49.264032632s"),
|
2020-11-30 15:15:07 +00:00
|
|
|
ID: 1,
|
2024-01-04 16:10:36 +00:00
|
|
|
Origin: ptr.Of("crowdsec"),
|
2020-11-30 15:15:07 +00:00
|
|
|
Scenario: &tscenario,
|
|
|
|
|
|
|
|
Scope: &tscope,
|
2024-01-04 16:10:36 +00:00
|
|
|
Simulated: ptr.Of(false),
|
2020-11-30 15:15:07 +00:00
|
|
|
Type: &ttype,
|
|
|
|
Value: &tvalue,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Events: []*models.Event{
|
2023-03-09 10:56:02 +00:00
|
|
|
{
|
2020-11-30 15:15:07 +00:00
|
|
|
Meta: models.Meta{
|
|
|
|
&models.MetaItems0{
|
|
|
|
Key: "target_user",
|
|
|
|
Value: "netflix",
|
|
|
|
},
|
|
|
|
&models.MetaItems0{
|
|
|
|
Key: "service",
|
|
|
|
Value: "ssh",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Timestamp: &ttimestamp,
|
2023-03-09 10:56:02 +00:00
|
|
|
}, {
|
2020-11-30 15:15:07 +00:00
|
|
|
Meta: models.Meta{
|
|
|
|
&models.MetaItems0{
|
|
|
|
Key: "target_user",
|
|
|
|
Value: "netflix",
|
|
|
|
},
|
|
|
|
&models.MetaItems0{
|
|
|
|
Key: "service",
|
|
|
|
Value: "ssh",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Timestamp: &ttimestamp,
|
|
|
|
},
|
|
|
|
},
|
2024-01-04 16:10:36 +00:00
|
|
|
EventsCount: ptr.Of(int32(6)),
|
2020-11-30 15:15:07 +00:00
|
|
|
ID: 1,
|
2024-01-04 16:10:36 +00:00
|
|
|
Leakspeed: ptr.Of("10s"),
|
2020-11-30 15:15:07 +00:00
|
|
|
MachineID: "test",
|
2024-01-04 16:10:36 +00:00
|
|
|
Message: ptr.Of("Ip 1.1.1.172 performed 'crowdsecurity/ssh-bf' (6 events over 2.920062ms) at 2020-11-28 10:20:46.845619968 +0100 CET m=+5.903899761"),
|
2020-11-30 15:15:07 +00:00
|
|
|
Remediation: false,
|
|
|
|
Scenario: &tscenario,
|
2024-01-04 16:10:36 +00:00
|
|
|
ScenarioHash: ptr.Of("4441dcff07020f6690d998b7101e642359ba405c2abb83565bbbdcee36de280f"),
|
|
|
|
ScenarioVersion: ptr.Of("0.1"),
|
|
|
|
Simulated: ptr.Of(false),
|
2020-11-30 15:15:07 +00:00
|
|
|
Source: &models.Source{
|
|
|
|
AsName: "Cloudflare Inc",
|
|
|
|
AsNumber: "",
|
|
|
|
Cn: "AU",
|
|
|
|
IP: "1.1.1.172",
|
|
|
|
Latitude: -37.7,
|
|
|
|
Longitude: 145.1833,
|
|
|
|
Range: "1.1.1.0/24",
|
|
|
|
Scope: &tscope,
|
|
|
|
Value: &tvalue,
|
|
|
|
},
|
2024-01-04 16:10:36 +00:00
|
|
|
StartAt: ptr.Of("2020-11-28 10:20:46.842701127 +0100 +0100"),
|
|
|
|
StopAt: ptr.Of("2020-11-28 10:20:46.845621385 +0100 +0100"),
|
2020-11-30 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
alerts, resp, err := client.Alerts.GetByID(context.Background(), 1)
|
2021-03-17 11:36:47 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, http.StatusOK, resp.Response.StatusCode)
|
|
|
|
assert.Equal(t, *expected, *alerts)
|
2020-11-30 15:15:07 +00:00
|
|
|
|
|
|
|
//fail
|
2022-10-14 14:12:21 +00:00
|
|
|
_, _, err = client.Alerts.GetByID(context.Background(), 2)
|
2024-01-04 16:10:36 +00:00
|
|
|
cstest.RequireErrorMessage(t, err, "API error: object not found")
|
2020-11-30 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlertsCreateAsMachine(t *testing.T) {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
|
|
|
|
mux, urlx, teardown := setup()
|
|
|
|
mux.HandleFunc("/watchers/login", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(`{"code": 200, "expire": "2030-01-02T15:04:05Z", "token": "oklol"}`))
|
|
|
|
})
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
mux.HandleFunc("/alerts", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
testMethod(t, r, "POST")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(`["3"]`))
|
|
|
|
})
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
log.Printf("URL is %s", urlx)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
apiURL, err := url.Parse(urlx + "/")
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
client, err := NewClient(&Config{
|
|
|
|
MachineID: "test_login",
|
|
|
|
Password: "test_password",
|
2023-05-23 08:52:47 +00:00
|
|
|
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
2020-11-30 15:15:07 +00:00
|
|
|
URL: apiURL,
|
|
|
|
VersionPrefix: "v1",
|
|
|
|
})
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-30 15:15:07 +00:00
|
|
|
|
|
|
|
defer teardown()
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
alert := models.AddAlertsRequest{}
|
|
|
|
alerts, resp, err := client.Alerts.Add(context.Background(), alert)
|
2021-03-17 11:36:47 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
expected := &models.AddAlertsResponse{"3"}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, http.StatusOK, resp.Response.StatusCode)
|
|
|
|
assert.Equal(t, *expected, *alerts)
|
2020-11-30 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlertsDeleteAsMachine(t *testing.T) {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
|
|
|
|
mux, urlx, teardown := setup()
|
|
|
|
mux.HandleFunc("/watchers/login", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(`{"code": 200, "expire": "2030-01-02T15:04:05Z", "token": "oklol"}`))
|
|
|
|
})
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
mux.HandleFunc("/alerts", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
testMethod(t, r, "DELETE")
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Equal(t, "ip=1.2.3.4", r.URL.RawQuery)
|
2020-11-30 15:15:07 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(`{"message":"0 deleted alerts"}`))
|
|
|
|
})
|
2024-01-04 16:10:36 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
log.Printf("URL is %s", urlx)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
apiURL, err := url.Parse(urlx + "/")
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
client, err := NewClient(&Config{
|
|
|
|
MachineID: "test_login",
|
|
|
|
Password: "test_password",
|
2023-05-23 08:52:47 +00:00
|
|
|
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
2020-11-30 15:15:07 +00:00
|
|
|
URL: apiURL,
|
|
|
|
VersionPrefix: "v1",
|
|
|
|
})
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-30 15:15:07 +00:00
|
|
|
|
|
|
|
defer teardown()
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
alert := AlertsDeleteOpts{IPEquals: ptr.Of("1.2.3.4")}
|
2020-11-30 15:15:07 +00:00
|
|
|
alerts, resp, err := client.Alerts.Delete(context.Background(), alert)
|
2021-03-17 11:36:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-16 12:41:54 +00:00
|
|
|
expected := &models.DeleteAlertsResponse{NbDeleted: ""}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, http.StatusOK, resp.Response.StatusCode)
|
|
|
|
assert.Equal(t, *expected, *alerts)
|
2020-11-30 15:15:07 +00:00
|
|
|
}
|