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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// type ApiAlerts service
|
|
|
|
|
|
|
|
type AuthService service
|
|
|
|
|
2021-06-28 15:34:19 +00:00
|
|
|
// Don't add it to the models, as they are used with LAPI, but the enroll endpoint is specific to CAPI
|
|
|
|
type enrollRequest struct {
|
2021-10-26 13:33:17 +00:00
|
|
|
EnrollKey string `json:"attachment_key"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Tags []string `json:"tags"`
|
2022-04-20 11:34:17 +00:00
|
|
|
Overwrite bool `json:"overwrite"`
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func (s *AuthService) UnregisterWatcher(ctx context.Context) (*Response, error) {
|
|
|
|
u := fmt.Sprintf("%s/watchers", 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 *AuthService) RegisterWatcher(ctx context.Context, registration models.WatcherRegistrationRequest) (*Response, error) {
|
|
|
|
u := fmt.Sprintf("%s/watchers", s.client.URLPrefix)
|
|
|
|
|
2022-08-16 07:46:10 +00:00
|
|
|
req, err := s.client.NewRequest(http.MethodPost, u, ®istration)
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-16 15:16:26 +00:00
|
|
|
func (s *AuthService) AuthenticateWatcher(ctx context.Context, auth models.WatcherAuthRequest) (models.WatcherAuthResponse, *Response, error) {
|
|
|
|
var authResp models.WatcherAuthResponse
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
u := fmt.Sprintf("%s/watchers/login", 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.MethodPost, u, &auth)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2023-02-16 15:16:26 +00:00
|
|
|
return authResp, nil, err
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 15:16:26 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, &authResp)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2023-02-16 15:16:26 +00:00
|
|
|
return authResp, resp, err
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2023-02-16 15:16:26 +00:00
|
|
|
return authResp, resp, nil
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2021-06-28 15:34:19 +00:00
|
|
|
|
2022-04-20 11:34:17 +00:00
|
|
|
func (s *AuthService) EnrollWatcher(ctx context.Context, enrollKey string, name string, tags []string, overwrite bool) (*Response, error) {
|
2021-06-28 15:34:19 +00:00
|
|
|
u := fmt.Sprintf("%s/watchers/enroll", 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.MethodPost, u, &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags, Overwrite: overwrite})
|
2021-06-28 15:34:19 +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
|
|
|
|
2021-06-28 15:34:19 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|