2020-11-30 09:37:17 +00:00
|
|
|
package apiclient
|
|
|
|
|
|
|
|
import (
|
2023-02-06 13:06:14 +00:00
|
|
|
"bufio"
|
2020-11-30 09:37:17 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-08-16 07:46:10 +00:00
|
|
|
"net/http"
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-06-22 13:01:34 +00:00
|
|
|
qs "github.com/google/go-querystring/query"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
2023-05-25 08:08:52 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
2023-02-06 13:06:14 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/modelscapi"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
2020-11-30 09:37:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DecisionsService service
|
|
|
|
|
|
|
|
type DecisionsListOpts struct {
|
|
|
|
ScopeEquals *string `url:"scope,omitempty"`
|
|
|
|
ValueEquals *string `url:"value,omitempty"`
|
|
|
|
TypeEquals *string `url:"type,omitempty"`
|
|
|
|
IPEquals *string `url:"ip,omitempty"`
|
|
|
|
RangeEquals *string `url:"range,omitempty"`
|
2021-01-14 15:27:45 +00:00
|
|
|
Contains *bool `url:"contains,omitempty"`
|
2020-11-30 09:37:17 +00:00
|
|
|
ListOpts
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:37:42 +00:00
|
|
|
type DecisionsStreamOpts struct {
|
|
|
|
Startup bool `url:"startup,omitempty"`
|
|
|
|
Scopes string `url:"scopes,omitempty"`
|
|
|
|
ScenariosContaining string `url:"scenarios_containing,omitempty"`
|
|
|
|
ScenariosNotContaining string `url:"scenarios_not_containing,omitempty"`
|
2022-04-07 10:40:27 +00:00
|
|
|
Origins string `url:"origins,omitempty"`
|
2022-03-16 13:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *DecisionsStreamOpts) addQueryParamsToURL(url string) (string, error) {
|
|
|
|
params, err := qs.Values(o)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2022-03-16 13:37:42 +00:00
|
|
|
return fmt.Sprintf("%s?%s", url, params.Encode()), nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
type DecisionsDeleteOpts struct {
|
2023-03-08 17:29:20 +00:00
|
|
|
ScopeEquals *string `url:"scope,omitempty"`
|
|
|
|
ValueEquals *string `url:"value,omitempty"`
|
|
|
|
TypeEquals *string `url:"type,omitempty"`
|
|
|
|
IPEquals *string `url:"ip,omitempty"`
|
|
|
|
RangeEquals *string `url:"range,omitempty"`
|
|
|
|
Contains *bool `url:"contains,omitempty"`
|
|
|
|
OriginEquals *string `url:"origin,omitempty"`
|
2022-10-19 12:37:27 +00:00
|
|
|
//
|
|
|
|
ScenarioEquals *string `url:"scenario,omitempty"`
|
2020-11-30 09:37:17 +00:00
|
|
|
ListOpts
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:37:27 +00:00
|
|
|
// to demo query arguments
|
2020-11-30 09:37:17 +00:00
|
|
|
func (s *DecisionsService) List(ctx context.Context, opts DecisionsListOpts) (*models.GetDecisionsResponse, *Response, error) {
|
|
|
|
var decisions models.GetDecisionsResponse
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
params, err := qs.Values(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
u := fmt.Sprintf("%s/decisions?%s", s.client.URLPrefix, 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, u, nil)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(ctx, req, &decisions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2023-02-06 13:06:14 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return &decisions, resp, nil
|
|
|
|
}
|
|
|
|
|
2023-02-06 13:06:14 +00:00
|
|
|
func (s *DecisionsService) FetchV2Decisions(ctx context.Context, url string) (*models.DecisionsStreamResponse, *Response, error) {
|
2020-11-30 09:37:17 +00:00
|
|
|
var decisions models.DecisionsStreamResponse
|
2023-02-06 13:06:14 +00:00
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(ctx, req, &decisions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &decisions, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DecisionsService) GetDecisionsFromGroups(decisionsGroups []*modelscapi.GetDecisionsStreamResponseNewItem) []*models.Decision {
|
|
|
|
var decisions []*models.Decision
|
|
|
|
|
|
|
|
for _, decisionsGroup := range decisionsGroups {
|
|
|
|
partialDecisions := make([]*models.Decision, len(decisionsGroup.Decisions))
|
|
|
|
for idx, decision := range decisionsGroup.Decisions {
|
|
|
|
partialDecisions[idx] = &models.Decision{
|
|
|
|
Scenario: decisionsGroup.Scenario,
|
|
|
|
Scope: decisionsGroup.Scope,
|
2023-05-25 08:08:52 +00:00
|
|
|
Type: ptr.Of(types.DecisionTypeBan),
|
2023-02-06 13:06:14 +00:00
|
|
|
Value: decision.Value,
|
|
|
|
Duration: decision.Duration,
|
2023-05-25 08:08:52 +00:00
|
|
|
Origin: ptr.Of(types.CAPIOrigin),
|
2023-02-06 13:06:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-06 13:06:14 +00:00
|
|
|
decisions = append(decisions, partialDecisions...)
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-06 13:06:14 +00:00
|
|
|
return decisions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DecisionsService) FetchV3Decisions(ctx context.Context, url string) (*models.DecisionsStreamResponse, *Response, error) {
|
2023-12-14 13:54:11 +00:00
|
|
|
var (
|
|
|
|
decisions modelscapi.GetDecisionsStreamResponse
|
|
|
|
v2Decisions models.DecisionsStreamResponse
|
|
|
|
)
|
2023-02-06 13:06:14 +00:00
|
|
|
|
|
|
|
scenarioDeleted := "deleted"
|
|
|
|
durationDeleted := "1h"
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(ctx, req, &decisions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v2Decisions.New = s.GetDecisionsFromGroups(decisions.New)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-06 13:06:14 +00:00
|
|
|
for _, decisionsGroup := range decisions.Deleted {
|
|
|
|
partialDecisions := make([]*models.Decision, len(decisionsGroup.Decisions))
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-06 13:06:14 +00:00
|
|
|
for idx, decision := range decisionsGroup.Decisions {
|
|
|
|
decision := decision // fix exportloopref linter message
|
|
|
|
partialDecisions[idx] = &models.Decision{
|
|
|
|
Scenario: &scenarioDeleted,
|
|
|
|
Scope: decisionsGroup.Scope,
|
2023-05-25 08:08:52 +00:00
|
|
|
Type: ptr.Of(types.DecisionTypeBan),
|
2023-02-06 13:06:14 +00:00
|
|
|
Value: &decision,
|
|
|
|
Duration: &durationDeleted,
|
2023-05-25 08:08:52 +00:00
|
|
|
Origin: ptr.Of(types.CAPIOrigin),
|
2023-02-06 13:06:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-06 13:06:14 +00:00
|
|
|
v2Decisions.Deleted = append(v2Decisions.Deleted, partialDecisions...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v2Decisions, resp, nil
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
func (s *DecisionsService) GetDecisionsFromBlocklist(ctx context.Context, blocklist *modelscapi.BlocklistLink, lastPullTimestamp *string) ([]*models.Decision, bool, error) {
|
2023-02-06 13:06:14 +00:00
|
|
|
if blocklist.URL == nil {
|
2023-02-13 14:06:14 +00:00
|
|
|
return nil, false, errors.New("blocklist URL is nil")
|
2023-02-06 13:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Fetching blocklist %s", *blocklist.URL)
|
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
client := http.Client{}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, *blocklist.URL, nil)
|
2023-02-06 13:06:14 +00:00
|
|
|
if err != nil {
|
2023-02-13 14:06:14 +00:00
|
|
|
return nil, false, err
|
2023-02-06 13:06:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
if lastPullTimestamp != nil {
|
|
|
|
req.Header.Set("If-Modified-Since", *lastPullTimestamp)
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
req = req.WithContext(ctx)
|
|
|
|
log.Debugf("[URL] %s %s", req.Method, req.URL)
|
2024-01-10 11:00:22 +00:00
|
|
|
// we don't use client_http Do method because we need the reader and is not provided.
|
|
|
|
// We would be forced to use Pipe and goroutine, etc
|
2023-02-13 14:06:14 +00:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if resp != nil && resp.Body != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// If we got an error, and the context has been canceled,
|
|
|
|
// the context's error is probably more useful.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, false, ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the error type is *url.Error, sanitize its URL before returning.
|
|
|
|
log.Errorf("Error fetching blocklist %s: %s", *blocklist.URL, err)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotModified {
|
|
|
|
if lastPullTimestamp != nil {
|
|
|
|
log.Debugf("Blocklist %s has not been modified since %s", *blocklist.URL, *lastPullTimestamp)
|
|
|
|
} else {
|
|
|
|
log.Debugf("Blocklist %s has not been modified (decisions about to expire)", *blocklist.URL)
|
2023-02-06 13:06:14 +00:00
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
return nil, false, nil
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
log.Debugf("Received nok status code %d for blocklist %s", resp.StatusCode, *blocklist.URL)
|
2024-01-10 11:00:22 +00:00
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
return nil, false, nil
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-06 13:06:14 +00:00
|
|
|
decisions := make([]*models.Decision, 0)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
2023-02-06 13:06:14 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
decision := scanner.Text()
|
|
|
|
decisions = append(decisions, &models.Decision{
|
|
|
|
Scenario: blocklist.Name,
|
|
|
|
Scope: blocklist.Scope,
|
|
|
|
Type: blocklist.Remediation,
|
|
|
|
Value: &decision,
|
|
|
|
Duration: blocklist.Duration,
|
2023-05-25 08:08:52 +00:00
|
|
|
Origin: ptr.Of(types.ListOrigin),
|
2023-02-06 13:06:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
// here the upper go routine is finished because scanner.Scan() is blocking until pw.Close() is called
|
|
|
|
// so it's safe to use the isModified variable here
|
|
|
|
return decisions, true, nil
|
2023-02-06 13:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DecisionsService) GetStream(ctx context.Context, opts DecisionsStreamOpts) (*models.DecisionsStreamResponse, *Response, error) {
|
2022-03-16 13:37:42 +00:00
|
|
|
u, err := opts.addQueryParamsToURL(s.client.URLPrefix + "/decisions/stream")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2021-05-31 13:07:09 +00:00
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-06 13:06:14 +00:00
|
|
|
if s.client.URLPrefix == "v3" {
|
|
|
|
return s.FetchV3Decisions(ctx, u)
|
|
|
|
} else {
|
|
|
|
return s.FetchV2Decisions(ctx, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DecisionsService) GetStreamV3(ctx context.Context, opts DecisionsStreamOpts) (*modelscapi.GetDecisionsStreamResponse, *Response, error) {
|
|
|
|
u, err := opts.addQueryParamsToURL(s.client.URLPrefix + "/decisions/stream")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-06 13:06:14 +00:00
|
|
|
var decisions modelscapi.GetDecisionsStreamResponse
|
|
|
|
|
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, &decisions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &decisions, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DecisionsService) StopStream(ctx context.Context) (*Response, error) {
|
|
|
|
u := fmt.Sprintf("%s/decisions", s.client.URLPrefix)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
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, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(ctx, req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DecisionsService) Delete(ctx context.Context, opts DecisionsDeleteOpts) (*models.DeleteDecisionResponse, *Response, error) {
|
|
|
|
var deleteDecisionResponse models.DeleteDecisionResponse
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
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/decisions?%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, &deleteDecisionResponse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return &deleteDecisionResponse, resp, nil
|
|
|
|
}
|
|
|
|
|
2023-12-14 13:54:11 +00:00
|
|
|
func (s *DecisionsService) DeleteOne(ctx context.Context, decisionID string) (*models.DeleteDecisionResponse, *Response, error) {
|
2020-11-30 09:37:17 +00:00
|
|
|
var deleteDecisionResponse models.DeleteDecisionResponse
|
2023-12-14 13:54:11 +00:00
|
|
|
|
|
|
|
u := fmt.Sprintf("%s/decisions/%s", s.client.URLPrefix, decisionID)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
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, &deleteDecisionResponse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return &deleteDecisionResponse, resp, nil
|
|
|
|
}
|