2020-11-30 09:37:17 +00:00
|
|
|
package apiclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-08-16 07:46:10 +00:00
|
|
|
"net/http"
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
|
|
|
qs "github.com/google/go-querystring/query"
|
2020-11-30 15:15:07 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-11-30 09:37:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// type ApiAlerts service
|
|
|
|
|
|
|
|
type AlertsService service
|
|
|
|
|
|
|
|
type AlertsListOpts struct {
|
|
|
|
ScopeEquals *string `url:"scope,omitempty"`
|
|
|
|
ValueEquals *string `url:"value,omitempty"`
|
|
|
|
ScenarioEquals *string `url:"scenario,omitempty"`
|
|
|
|
IPEquals *string `url:"ip,omitempty"`
|
|
|
|
RangeEquals *string `url:"range,omitempty"`
|
2022-01-11 13:31:51 +00:00
|
|
|
OriginEquals *string `url:"origin,omitempty"`
|
2020-11-30 09:37:17 +00:00
|
|
|
Since *string `url:"since,omitempty"`
|
|
|
|
TypeEquals *string `url:"decision_type,omitempty"`
|
|
|
|
Until *string `url:"until,omitempty"`
|
|
|
|
IncludeSimulated *bool `url:"simulated,omitempty"`
|
|
|
|
ActiveDecisionEquals *bool `url:"has_active_decision,omitempty"`
|
|
|
|
IncludeCAPI *bool `url:"include_capi,omitempty"`
|
|
|
|
Limit *int `url:"limit,omitempty"`
|
2021-01-14 15:27:45 +00:00
|
|
|
Contains *bool `url:"contains,omitempty"`
|
2020-11-30 09:37:17 +00:00
|
|
|
ListOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
type AlertsDeleteOpts struct {
|
|
|
|
ScopeEquals *string `url:"scope,omitempty"`
|
|
|
|
ValueEquals *string `url:"value,omitempty"`
|
|
|
|
ScenarioEquals *string `url:"scenario,omitempty"`
|
|
|
|
IPEquals *string `url:"ip,omitempty"`
|
|
|
|
RangeEquals *string `url:"range,omitempty"`
|
|
|
|
Since *string `url:"since,omitempty"`
|
|
|
|
Until *string `url:"until,omitempty"`
|
2022-01-11 13:31:51 +00:00
|
|
|
OriginEquals *string `url:"origin,omitempty"`
|
2020-11-30 09:37:17 +00:00
|
|
|
ActiveDecisionEquals *bool `url:"has_active_decision,omitempty"`
|
|
|
|
SourceEquals *string `url:"alert_source,omitempty"`
|
2021-01-14 15:27:45 +00:00
|
|
|
Contains *bool `url:"contains,omitempty"`
|
2021-04-27 09:59:18 +00:00
|
|
|
Limit *int `url:"limit,omitempty"`
|
2020-11-30 09:37:17 +00:00
|
|
|
ListOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AlertsService) Add(ctx context.Context, alerts models.AddAlertsRequest) (*models.AddAlertsResponse, *Response, error) {
|
|
|
|
|
|
|
|
var added_ids models.AddAlertsResponse
|
|
|
|
|
|
|
|
u := fmt.Sprintf("%s/alerts", s.client.URLPrefix)
|
2022-08-16 07:46:10 +00:00
|
|
|
req, err := s.client.NewRequest(http.MethodPost, u, &alerts)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(ctx, req, &added_ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
return &added_ids, resp, nil
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:37:27 +00:00
|
|
|
// to demo query arguments
|
2020-11-30 09:37:17 +00:00
|
|
|
func (s *AlertsService) List(ctx context.Context, opts AlertsListOpts) (*models.GetAlertsResponse, *Response, error) {
|
|
|
|
var alerts models.GetAlertsResponse
|
|
|
|
var URI string
|
|
|
|
u := fmt.Sprintf("%s/alerts", s.client.URLPrefix)
|
|
|
|
params, err := qs.Values(opts)
|
|
|
|
if err != nil {
|
2020-11-30 15:15:07 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "building query")
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
if len(params) > 0 {
|
|
|
|
URI = fmt.Sprintf("%s?%s", u, params.Encode())
|
|
|
|
} else {
|
|
|
|
URI = u
|
|
|
|
}
|
|
|
|
|
2022-08-16 07:46:10 +00:00
|
|
|
req, err := s.client.NewRequest(http.MethodGet, URI, nil)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2020-11-30 15:15:07 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "building request")
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(ctx, req, &alerts)
|
|
|
|
if err != nil {
|
2020-11-30 15:15:07 +00:00
|
|
|
return nil, resp, errors.Wrap(err, "performing request")
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
return &alerts, resp, nil
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:37:27 +00:00
|
|
|
// to demo query arguments
|
2020-11-30 09:37:17 +00:00
|
|
|
func (s *AlertsService) Delete(ctx context.Context, opts AlertsDeleteOpts) (*models.DeleteAlertsResponse, *Response, error) {
|
|
|
|
var alerts models.DeleteAlertsResponse
|
|
|
|
params, err := qs.Values(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
u := fmt.Sprintf("%s/alerts?%s", s.client.URLPrefix, params.Encode())
|
|
|
|
|
2022-08-16 07:46:10 +00:00
|
|
|
req, err := s.client.NewRequest(http.MethodDelete, u, nil)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(ctx, req, &alerts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
return &alerts, resp, nil
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:37:27 +00:00
|
|
|
func (s *AlertsService) DeleteOne(ctx context.Context, alert_id string) (*models.DeleteAlertsResponse, *Response, error) {
|
|
|
|
var alerts models.DeleteAlertsResponse
|
|
|
|
u := fmt.Sprintf("%s/alerts/%s", s.client.URLPrefix, alert_id)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodDelete, u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(ctx, req, &alerts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
return &alerts, resp, nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func (s *AlertsService) GetByID(ctx context.Context, alertID int) (*models.Alert, *Response, error) {
|
|
|
|
var alert models.Alert
|
|
|
|
u := fmt.Sprintf("%s/alerts/%d", s.client.URLPrefix, alertID)
|
|
|
|
|
2022-08-16 07:46:10 +00:00
|
|
|
req, err := s.client.NewRequest(http.MethodGet, u, nil)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(ctx, req, &alert)
|
|
|
|
if err != nil {
|
2020-11-30 15:15:07 +00:00
|
|
|
return nil, nil, err
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
return &alert, resp, nil
|
|
|
|
}
|