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
|
|
|
|
|
|
|
qs "github.com/google/go-querystring/query"
|
2023-06-22 13:01:34 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
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) {
|
|
|
|
u := fmt.Sprintf("%s/alerts", s.client.URLPrefix)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-15 10:44:38 +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
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:38:31 +00:00
|
|
|
addedIds := models.AddAlertsResponse{}
|
2024-01-15 10:44:38 +00:00
|
|
|
|
2023-12-14 13:54:11 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, &addedIds)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
|
|
|
return &addedIds, resp, nil
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
u := fmt.Sprintf("%s/alerts", s.client.URLPrefix)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-15 10:44:38 +00:00
|
|
|
params, err := qs.Values(opts)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2023-06-22 13:01:34 +00:00
|
|
|
return nil, nil, fmt.Errorf("building query: %w", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2024-01-15 10:44:38 +00:00
|
|
|
URI := u
|
2020-11-30 09:37:17 +00:00
|
|
|
if len(params) > 0 {
|
2024-01-15 10:44:38 +00:00
|
|
|
URI = fmt.Sprintf("%s?%s", URI, params.Encode())
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-06-22 13:01:34 +00:00
|
|
|
return nil, nil, fmt.Errorf("building request: %w", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 10:44:38 +00:00
|
|
|
alerts := models.GetAlertsResponse{}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, &alerts)
|
|
|
|
if err != nil {
|
2023-06-22 13:01:34 +00:00
|
|
|
return nil, resp, fmt.Errorf("performing request: %w", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
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) {
|
|
|
|
params, err := qs.Values(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-15 10:44:38 +00:00
|
|
|
alerts := models.DeleteAlertsResponse{}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, &alerts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return &alerts, resp, nil
|
|
|
|
}
|
|
|
|
|
2023-12-14 13:54:11 +00:00
|
|
|
func (s *AlertsService) DeleteOne(ctx context.Context, alertID string) (*models.DeleteAlertsResponse, *Response, error) {
|
|
|
|
u := fmt.Sprintf("%s/alerts/%s", s.client.URLPrefix, alertID)
|
2022-10-19 12:37:27 +00:00
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodDelete, u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-15 10:44:38 +00:00
|
|
|
alerts := models.DeleteAlertsResponse{}
|
|
|
|
|
2022-10-19 12:37:27 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, &alerts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2022-10-19 12:37:27 +00:00
|
|
|
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) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-15 10:44:38 +00:00
|
|
|
alert := models.Alert{}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
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
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return &alert, resp, nil
|
|
|
|
}
|