mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 20:36:12 +02:00
Support WithUserAgent in cti client (#3542)
This commit is contained in:
parent
d64f196b3f
commit
78a6179566
1 changed files with 19 additions and 8 deletions
|
@ -9,9 +9,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -21,17 +20,19 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrUnauthorized = errors.New("unauthorized")
|
ErrUnauthorized = errors.New("unauthorized")
|
||||||
ErrLimit = errors.New("request quota exceeded, please reduce your request rate")
|
ErrLimit = errors.New("request quota exceeded, please reduce your request rate")
|
||||||
ErrNotFound = errors.New("ip not found")
|
ErrNotFound = errors.New("ip not found")
|
||||||
ErrDisabled = errors.New("cti is disabled")
|
ErrDisabled = errors.New("cti is disabled")
|
||||||
ErrUnknown = errors.New("unknown error")
|
ErrUnknown = errors.New("unknown error")
|
||||||
|
defaultUserAgent = useragent.Default()
|
||||||
)
|
)
|
||||||
|
|
||||||
type CrowdsecCTIClient struct {
|
type CrowdsecCTIClient struct {
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
apiKey string
|
apiKey string
|
||||||
Logger *log.Entry
|
Logger *log.Entry
|
||||||
|
UserAgent string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CrowdsecCTIClient) doRequest(ctx context.Context, method string, endpoint string, params map[string]string) ([]byte, error) {
|
func (c *CrowdsecCTIClient) doRequest(ctx context.Context, method string, endpoint string, params map[string]string) ([]byte, error) {
|
||||||
|
@ -48,7 +49,7 @@ func (c *CrowdsecCTIClient) doRequest(ctx context.Context, method string, endpoi
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("X-Api-Key", c.apiKey)
|
req.Header.Set("X-Api-Key", c.apiKey)
|
||||||
req.Header.Set("User-Agent", useragent.Default())
|
req.Header.Set("User-Agent", c.UserAgent)
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -169,6 +170,10 @@ func NewCrowdsecCTIClient(options ...func(*CrowdsecCTIClient)) *CrowdsecCTIClien
|
||||||
client.Logger = log.NewEntry(log.New())
|
client.Logger = log.NewEntry(log.New())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if client.UserAgent == "" {
|
||||||
|
client.UserAgent = defaultUserAgent
|
||||||
|
}
|
||||||
|
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,3 +194,9 @@ func WithAPIKey(apiKey string) func(*CrowdsecCTIClient) {
|
||||||
c.apiKey = apiKey
|
c.apiKey = apiKey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithUserAgent(userAgent string) func(*CrowdsecCTIClient) {
|
||||||
|
return func(c *CrowdsecCTIClient) {
|
||||||
|
c.UserAgent = userAgent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue