mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-10 20:05:55 +02:00
refact: pkg/apiclient set and use default user agent (#3219)
* default user agent * DRY default user agent * useragent.go * moved to pkg/apiclient/useragent * lint * rename useragent.DefaultUserAgent() -> useragent.Default()
This commit is contained in:
parent
57dee1abf9
commit
c8750f604c
22 changed files with 82 additions and 95 deletions
|
@ -25,7 +25,6 @@ import (
|
|||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||
)
|
||||
|
@ -217,7 +216,6 @@ func (cli *cliAlerts) NewCommand() *cobra.Command {
|
|||
cli.client, err = apiclient.NewClient(&apiclient.Config{
|
||||
MachineID: cfg.API.Client.Credentials.Login,
|
||||
Password: strfmt.Password(cfg.API.Client.Credentials.Password),
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
|
|
@ -20,7 +20,6 @@ import (
|
|||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||
)
|
||||
|
@ -77,7 +76,6 @@ func (cli *cliCapi) register(capiUserPrefix string, outputFile string) error {
|
|||
_, err = apiclient.RegisterClient(&apiclient.Config{
|
||||
MachineID: capiUser,
|
||||
Password: password,
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiurl,
|
||||
VersionPrefix: "v3",
|
||||
}, nil)
|
||||
|
@ -168,7 +166,6 @@ func queryCAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri
|
|||
MachineID: login,
|
||||
Password: passwd,
|
||||
Scenarios: itemsForAPI,
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
// I don't believe papi is neede to check enrollement
|
||||
// PapiURL: papiURL,
|
||||
|
|
|
@ -24,7 +24,6 @@ import (
|
|||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||
)
|
||||
|
||||
|
@ -124,7 +123,6 @@ func (cli *cliConsole) enroll(key string, name string, overwrite bool, tags []st
|
|||
MachineID: cli.cfg().API.Server.OnlineClient.Credentials.Login,
|
||||
Password: password,
|
||||
Scenarios: hub.GetInstalledListForAPI(),
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v3",
|
||||
})
|
||||
|
|
|
@ -20,7 +20,6 @@ import (
|
|||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clialert"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||
)
|
||||
|
@ -152,7 +151,6 @@ func (cli *cliDecisions) NewCommand() *cobra.Command {
|
|||
cli.client, err = apiclient.NewClient(&apiclient.Config{
|
||||
MachineID: cfg.API.Client.Credentials.Login,
|
||||
Password: strfmt.Password(cfg.API.Client.Credentials.Password),
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
|
|
@ -24,7 +24,6 @@ import (
|
|||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/parser"
|
||||
|
@ -53,7 +52,7 @@ func queryLAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri
|
|||
|
||||
client, err := apiclient.NewDefaultClient(apiURL,
|
||||
LAPIURLPrefix,
|
||||
cwversion.UserAgent(),
|
||||
"",
|
||||
nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
@ -118,7 +117,6 @@ func (cli *cliLapi) register(apiURL string, outputFile string, machine string, t
|
|||
_, err = apiclient.RegisterClient(&apiclient.Config{
|
||||
MachineID: lapiUser,
|
||||
Password: password,
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
RegistrationToken: token,
|
||||
URL: apiurl,
|
||||
VersionPrefix: LAPIURLPrefix,
|
||||
|
|
|
@ -29,7 +29,6 @@ import (
|
|||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/csplugin"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/csprofiles"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||
)
|
||||
|
@ -464,7 +463,6 @@ func (cli *cliNotifications) fetchAlertFromArgString(toParse string) (*models.Al
|
|||
client, err := apiclient.NewClient(&apiclient.Config{
|
||||
MachineID: cfg.API.Client.Credentials.Login,
|
||||
Password: strfmt.Password(cfg.API.Client.Credentials.Password),
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||
)
|
||||
|
||||
|
@ -34,7 +33,6 @@ func AuthenticatedLAPIClient(credentials csconfig.ApiCredentialsCfg, hub *cwhub.
|
|||
MachineID: credentials.Login,
|
||||
Password: password,
|
||||
Scenarios: itemsForAPI,
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
PapiURL: papiURL,
|
||||
VersionPrefix: "v1",
|
||||
|
|
|
@ -16,7 +16,7 @@ import (
|
|||
log "github.com/sirupsen/logrus"
|
||||
"gopkg.in/tomb.v2"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
|
||||
)
|
||||
|
||||
type LokiClient struct {
|
||||
|
@ -319,6 +319,6 @@ func NewLokiClient(config Config) *LokiClient {
|
|||
if config.Username != "" || config.Password != "" {
|
||||
headers["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(config.Username+":"+config.Password))
|
||||
}
|
||||
headers["User-Agent"] = cwversion.UserAgent()
|
||||
headers["User-Agent"] = useragent.Default()
|
||||
return &LokiClient{Logger: log.WithField("component", "lokiclient"), config: config, requestHeaders: headers}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@ import (
|
|||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
"github.com/crowdsecurity/go-cs-lib/ptr"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||
)
|
||||
|
||||
|
@ -35,7 +34,6 @@ func TestAlertsListAsMachine(t *testing.T) {
|
|||
client, err := NewClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
@ -180,16 +178,16 @@ func TestAlertsListAsMachine(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
//log.Debugf("data : -> %s", spew.Sdump(alerts))
|
||||
//log.Debugf("resp : -> %s", spew.Sdump(resp))
|
||||
//log.Debugf("expected : -> %s", spew.Sdump(expected))
|
||||
//first one returns data
|
||||
// log.Debugf("data : -> %s", spew.Sdump(alerts))
|
||||
// log.Debugf("resp : -> %s", spew.Sdump(resp))
|
||||
// log.Debugf("expected : -> %s", spew.Sdump(expected))
|
||||
// first one returns data
|
||||
alerts, resp, err := client.Alerts.List(context.Background(), AlertsListOpts{})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.Response.StatusCode)
|
||||
assert.Equal(t, expected, *alerts)
|
||||
|
||||
//this one doesn't
|
||||
// this one doesn't
|
||||
filter := AlertsListOpts{IPEquals: ptr.Of("1.2.3.4")}
|
||||
|
||||
alerts, resp, err = client.Alerts.List(context.Background(), filter)
|
||||
|
@ -214,7 +212,6 @@ func TestAlertsGetAsMachine(t *testing.T) {
|
|||
client, err := NewClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
@ -360,7 +357,7 @@ func TestAlertsGetAsMachine(t *testing.T) {
|
|||
assert.Equal(t, http.StatusOK, resp.Response.StatusCode)
|
||||
assert.Equal(t, *expected, *alerts)
|
||||
|
||||
//fail
|
||||
// fail
|
||||
_, _, err = client.Alerts.GetByID(context.Background(), 2)
|
||||
cstest.RequireErrorMessage(t, err, "API error: object not found")
|
||||
}
|
||||
|
@ -388,7 +385,6 @@ func TestAlertsCreateAsMachine(t *testing.T) {
|
|||
client, err := NewClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
@ -430,7 +426,6 @@ func TestAlertsDeleteAsMachine(t *testing.T) {
|
|||
client, err := NewClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
|
|
@ -14,7 +14,6 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||
)
|
||||
|
||||
|
@ -36,11 +35,13 @@ func initBasicMuxMock(t *testing.T, mux *http.ServeMux, path string) {
|
|||
|
||||
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, "POST")
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
_, _ = buf.ReadFrom(r.Body)
|
||||
newStr := buf.String()
|
||||
|
||||
var payload BasicMockPayload
|
||||
|
||||
err := json.Unmarshal([]byte(newStr), &payload)
|
||||
if err != nil || payload.MachineID == "" || payload.Password == "" {
|
||||
log.Printf("Bad payload")
|
||||
|
@ -48,8 +49,8 @@ func initBasicMuxMock(t *testing.T, mux *http.ServeMux, path string) {
|
|||
}
|
||||
|
||||
var responseBody string
|
||||
responseCode, hasFoundErrorMock := loginsForMockErrorCases[payload.MachineID]
|
||||
|
||||
responseCode, hasFoundErrorMock := loginsForMockErrorCases[payload.MachineID]
|
||||
if !hasFoundErrorMock {
|
||||
responseCode = http.StatusOK
|
||||
responseBody = `{"code":200,"expire":"2029-11-30T14:14:24+01:00","token":"toto"}`
|
||||
|
@ -76,7 +77,7 @@ func TestWatcherRegister(t *testing.T) {
|
|||
mux, urlx, teardown := setup()
|
||||
defer teardown()
|
||||
|
||||
//body: models.WatcherRegistrationRequest{MachineID: &config.MachineID, Password: &config.Password}
|
||||
// body: models.WatcherRegistrationRequest{MachineID: &config.MachineID, Password: &config.Password}
|
||||
initBasicMuxMock(t, mux, "/watchers")
|
||||
log.Printf("URL is %s", urlx)
|
||||
|
||||
|
@ -87,7 +88,6 @@ func TestWatcherRegister(t *testing.T) {
|
|||
clientconfig := Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ func TestWatcherAuth(t *testing.T) {
|
|||
|
||||
mux, urlx, teardown := setup()
|
||||
defer teardown()
|
||||
//body: models.WatcherRegistrationRequest{MachineID: &config.MachineID, Password: &config.Password}
|
||||
// body: models.WatcherRegistrationRequest{MachineID: &config.MachineID, Password: &config.Password}
|
||||
|
||||
initBasicMuxMock(t, mux, "/watchers/login")
|
||||
log.Printf("URL is %s", urlx)
|
||||
|
@ -121,11 +121,10 @@ func TestWatcherAuth(t *testing.T) {
|
|||
apiURL, err := url.Parse(urlx + "/")
|
||||
require.NoError(t, err)
|
||||
|
||||
//ok auth
|
||||
// ok auth
|
||||
clientConfig := &Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
Scenarios: []string{"crowdsecurity/test"},
|
||||
|
@ -174,7 +173,7 @@ func TestWatcherUnregister(t *testing.T) {
|
|||
|
||||
mux, urlx, teardown := setup()
|
||||
defer teardown()
|
||||
//body: models.WatcherRegistrationRequest{MachineID: &config.MachineID, Password: &config.Password}
|
||||
// body: models.WatcherRegistrationRequest{MachineID: &config.MachineID, Password: &config.Password}
|
||||
|
||||
mux.HandleFunc("/watchers", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, "DELETE")
|
||||
|
@ -184,6 +183,7 @@ func TestWatcherUnregister(t *testing.T) {
|
|||
|
||||
mux.HandleFunc("/watchers/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, "POST")
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
_, _ = buf.ReadFrom(r.Body)
|
||||
|
||||
|
@ -206,7 +206,6 @@ func TestWatcherUnregister(t *testing.T) {
|
|||
mycfg := &Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
Scenarios: []string{"crowdsecurity/test"},
|
||||
|
@ -229,6 +228,7 @@ func TestWatcherEnroll(t *testing.T) {
|
|||
|
||||
mux.HandleFunc("/watchers/enroll", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, "POST")
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
_, _ = buf.ReadFrom(r.Body)
|
||||
newStr := buf.String()
|
||||
|
@ -260,7 +260,6 @@ func TestWatcherEnroll(t *testing.T) {
|
|||
mycfg := &Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
Scenarios: []string{"crowdsecurity/test"},
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||
)
|
||||
|
||||
|
@ -66,11 +67,16 @@ type service struct {
|
|||
}
|
||||
|
||||
func NewClient(config *Config) (*ApiClient, error) {
|
||||
userAgent := config.UserAgent
|
||||
if userAgent == "" {
|
||||
userAgent = useragent.Default()
|
||||
}
|
||||
|
||||
t := &JWTTransport{
|
||||
MachineID: &config.MachineID,
|
||||
Password: &config.Password,
|
||||
Scenarios: config.Scenarios,
|
||||
UserAgent: config.UserAgent,
|
||||
UserAgent: userAgent,
|
||||
VersionPrefix: config.VersionPrefix,
|
||||
UpdateScenario: config.UpdateScenario,
|
||||
RetryConfig: NewRetryConfig(
|
||||
|
@ -105,7 +111,7 @@ func NewClient(config *Config) (*ApiClient, error) {
|
|||
t.Transport.(*http.Transport).TLSClientConfig = &tlsconfig
|
||||
}
|
||||
|
||||
c := &ApiClient{client: t.Client(), BaseURL: baseURL, UserAgent: config.UserAgent, URLPrefix: config.VersionPrefix, PapiURL: config.PapiURL}
|
||||
c := &ApiClient{client: t.Client(), BaseURL: baseURL, UserAgent: userAgent, URLPrefix: config.VersionPrefix, PapiURL: config.PapiURL}
|
||||
c.common.client = c
|
||||
c.Decisions = (*DecisionsService)(&c.common)
|
||||
c.Alerts = (*AlertsService)(&c.common)
|
||||
|
@ -143,6 +149,10 @@ func NewDefaultClient(URL *url.URL, prefix string, userAgent string, client *htt
|
|||
}
|
||||
}
|
||||
|
||||
if userAgent == "" {
|
||||
userAgent = useragent.Default()
|
||||
}
|
||||
|
||||
c := &ApiClient{client: client, BaseURL: baseURL, UserAgent: userAgent, URLPrefix: prefix}
|
||||
c.common.client = c
|
||||
c.Decisions = (*DecisionsService)(&c.common)
|
||||
|
@ -178,15 +188,20 @@ func RegisterClient(config *Config, client *http.Client) (*ApiClient, error) {
|
|||
client.Transport = transport
|
||||
}
|
||||
|
||||
c := &ApiClient{client: client, BaseURL: baseURL, UserAgent: config.UserAgent, URLPrefix: config.VersionPrefix}
|
||||
userAgent := config.UserAgent
|
||||
if userAgent == "" {
|
||||
userAgent = useragent.Default()
|
||||
}
|
||||
|
||||
c := &ApiClient{client: client, BaseURL: baseURL, UserAgent: userAgent, URLPrefix: config.VersionPrefix}
|
||||
c.common.client = c
|
||||
c.Decisions = (*DecisionsService)(&c.common)
|
||||
c.Alerts = (*AlertsService)(&c.common)
|
||||
c.Auth = (*AuthService)(&c.common)
|
||||
|
||||
resp, err := c.Auth.RegisterWatcher(context.Background(), models.WatcherRegistrationRequest{MachineID: &config.MachineID, Password: &config.Password, RegistrationToken: config.RegistrationToken})
|
||||
/*if we have http status, return it*/
|
||||
if err != nil {
|
||||
/*if we have http status, return it*/
|
||||
if resp != nil && resp.Response != nil {
|
||||
return nil, fmt.Errorf("api register (%s) http %s: %w", c.BaseURL, resp.Response.Status, err)
|
||||
}
|
||||
|
|
|
@ -10,22 +10,19 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
)
|
||||
|
||||
func TestNewRequestInvalid(t *testing.T) {
|
||||
mux, urlx, teardown := setup()
|
||||
defer teardown()
|
||||
|
||||
//missing slash in uri
|
||||
// missing slash in uri
|
||||
apiURL, err := url.Parse(urlx)
|
||||
require.NoError(t, err)
|
||||
|
||||
client, err := NewClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
@ -57,7 +54,6 @@ func TestNewRequestTimeout(t *testing.T) {
|
|||
client, err := NewClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
|
|
@ -17,8 +17,6 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
)
|
||||
|
||||
/*this is a ripoff of google/go-github approach :
|
||||
|
@ -97,7 +95,6 @@ func TestNewClientOk(t *testing.T) {
|
|||
client, err := NewClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
@ -134,7 +131,6 @@ func TestNewClientOk_UnixSocket(t *testing.T) {
|
|||
client, err := NewClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
@ -172,7 +168,6 @@ func TestNewClientKo(t *testing.T) {
|
|||
client, err := NewClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
@ -250,7 +245,6 @@ func TestNewClientRegisterKO(t *testing.T) {
|
|||
_, err = RegisterClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
}, &http.Client{})
|
||||
|
@ -281,7 +275,6 @@ func TestNewClientRegisterOK(t *testing.T) {
|
|||
client, err := RegisterClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
}, &http.Client{})
|
||||
|
@ -314,7 +307,6 @@ func TestNewClientRegisterOK_UnixSocket(t *testing.T) {
|
|||
client, err := RegisterClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
}, &http.Client{})
|
||||
|
@ -344,7 +336,6 @@ func TestNewClientBadAnswer(t *testing.T) {
|
|||
_, err = RegisterClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
}, &http.Client{})
|
||||
|
|
|
@ -13,7 +13,6 @@ import (
|
|||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
"github.com/crowdsecurity/go-cs-lib/ptr"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/modelscapi"
|
||||
)
|
||||
|
@ -26,6 +25,7 @@ func TestDecisionsList(t *testing.T) {
|
|||
|
||||
mux.HandleFunc("/decisions", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, "GET")
|
||||
|
||||
if r.URL.RawQuery == "ip=1.2.3.4" {
|
||||
assert.Equal(t, "ip=1.2.3.4", r.URL.RawQuery)
|
||||
assert.Equal(t, "ixu", r.Header.Get("X-Api-Key"))
|
||||
|
@ -34,14 +34,14 @@ func TestDecisionsList(t *testing.T) {
|
|||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`null`))
|
||||
//no results
|
||||
// no results
|
||||
}
|
||||
})
|
||||
|
||||
apiURL, err := url.Parse(urlx + "/")
|
||||
require.NoError(t, err)
|
||||
|
||||
//ok answer
|
||||
// ok answer
|
||||
auth := &APIKeyTransport{
|
||||
APIKey: "ixu",
|
||||
}
|
||||
|
@ -68,7 +68,7 @@ func TestDecisionsList(t *testing.T) {
|
|||
assert.Equal(t, http.StatusOK, resp.Response.StatusCode)
|
||||
assert.Equal(t, *expected, *decisions)
|
||||
|
||||
//Empty return
|
||||
// Empty return
|
||||
decisionsFilter = DecisionsListOpts{IPEquals: ptr.Of("1.2.3.5")}
|
||||
decisions, resp, err = newcli.Decisions.List(context.Background(), decisionsFilter)
|
||||
require.NoError(t, err)
|
||||
|
@ -85,6 +85,7 @@ func TestDecisionsStream(t *testing.T) {
|
|||
mux.HandleFunc("/decisions/stream", func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "ixu", r.Header.Get("X-Api-Key"))
|
||||
testMethod(t, r, http.MethodGet)
|
||||
|
||||
if r.Method == http.MethodGet {
|
||||
if r.URL.RawQuery == "startup=true" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
@ -99,6 +100,7 @@ func TestDecisionsStream(t *testing.T) {
|
|||
mux.HandleFunc("/decisions", func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "ixu", r.Header.Get("X-Api-Key"))
|
||||
testMethod(t, r, http.MethodDelete)
|
||||
|
||||
if r.Method == http.MethodDelete {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
@ -107,7 +109,7 @@ func TestDecisionsStream(t *testing.T) {
|
|||
apiURL, err := url.Parse(urlx + "/")
|
||||
require.NoError(t, err)
|
||||
|
||||
//ok answer
|
||||
// ok answer
|
||||
auth := &APIKeyTransport{
|
||||
APIKey: "ixu",
|
||||
}
|
||||
|
@ -134,14 +136,14 @@ func TestDecisionsStream(t *testing.T) {
|
|||
assert.Equal(t, http.StatusOK, resp.Response.StatusCode)
|
||||
assert.Equal(t, *expected, *decisions)
|
||||
|
||||
//and second call, we get empty lists
|
||||
// and second call, we get empty lists
|
||||
decisions, resp, err = newcli.Decisions.GetStream(context.Background(), DecisionsStreamOpts{Startup: false})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.Response.StatusCode)
|
||||
assert.Empty(t, decisions.New)
|
||||
assert.Empty(t, decisions.Deleted)
|
||||
|
||||
//delete stream
|
||||
// delete stream
|
||||
resp, err = newcli.Decisions.StopStream(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.Response.StatusCode)
|
||||
|
@ -156,6 +158,7 @@ func TestDecisionsStreamV3Compatibility(t *testing.T) {
|
|||
mux.HandleFunc("/decisions/stream", func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "ixu", r.Header.Get("X-Api-Key"))
|
||||
testMethod(t, r, http.MethodGet)
|
||||
|
||||
if r.Method == http.MethodGet {
|
||||
if r.URL.RawQuery == "startup=true" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
@ -170,7 +173,7 @@ func TestDecisionsStreamV3Compatibility(t *testing.T) {
|
|||
apiURL, err := url.Parse(urlx + "/")
|
||||
require.NoError(t, err)
|
||||
|
||||
//ok answer
|
||||
// ok answer
|
||||
auth := &APIKeyTransport{
|
||||
APIKey: "ixu",
|
||||
}
|
||||
|
@ -220,6 +223,7 @@ func TestDecisionsStreamV3(t *testing.T) {
|
|||
mux.HandleFunc("/decisions/stream", func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "ixu", r.Header.Get("X-Api-Key"))
|
||||
testMethod(t, r, http.MethodGet)
|
||||
|
||||
if r.Method == http.MethodGet {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"deleted":[{"scope":"ip","decisions":["1.2.3.5"]}],
|
||||
|
@ -231,7 +235,7 @@ func TestDecisionsStreamV3(t *testing.T) {
|
|||
apiURL, err := url.Parse(urlx + "/")
|
||||
require.NoError(t, err)
|
||||
|
||||
//ok answer
|
||||
// ok answer
|
||||
auth := &APIKeyTransport{
|
||||
APIKey: "ixu",
|
||||
}
|
||||
|
@ -305,7 +309,7 @@ func TestDecisionsFromBlocklist(t *testing.T) {
|
|||
apiURL, err := url.Parse(urlx + "/")
|
||||
require.NoError(t, err)
|
||||
|
||||
//ok answer
|
||||
// ok answer
|
||||
auth := &APIKeyTransport{
|
||||
APIKey: "ixu",
|
||||
}
|
||||
|
@ -391,7 +395,7 @@ func TestDeleteDecisions(t *testing.T) {
|
|||
assert.Equal(t, "ip=1.2.3.4", r.URL.RawQuery)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"nbDeleted":"1"}`))
|
||||
//w.Write([]byte(`{"message":"0 deleted alerts"}`))
|
||||
// w.Write([]byte(`{"message":"0 deleted alerts"}`))
|
||||
})
|
||||
|
||||
log.Printf("URL is %s", urlx)
|
||||
|
@ -402,7 +406,6 @@ func TestDeleteDecisions(t *testing.T) {
|
|||
client, err := NewClient(&Config{
|
||||
MachineID: "test_login",
|
||||
Password: "test_password",
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
VersionPrefix: "v1",
|
||||
})
|
||||
|
@ -468,6 +471,7 @@ func TestDecisionsStreamOpts_addQueryParamsToURL(t *testing.T) {
|
|||
|
||||
got, err := o.addQueryParamsToURL(baseURLString)
|
||||
cstest.RequireErrorContains(t, err, tt.expectedErr)
|
||||
|
||||
if tt.expectedErr != "" {
|
||||
return
|
||||
}
|
||||
|
@ -502,7 +506,6 @@ func TestDecisionsStreamOpts_addQueryParamsToURL(t *testing.T) {
|
|||
// client, err := NewClient(&Config{
|
||||
// MachineID: "test_login",
|
||||
// Password: "test_password",
|
||||
// UserAgent: cwversion.UserAgent(),
|
||||
// URL: apiURL,
|
||||
// VersionPrefix: "v1",
|
||||
// })
|
||||
|
|
9
pkg/apiclient/useragent/useragent.go
Normal file
9
pkg/apiclient/useragent/useragent.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package useragent
|
||||
|
||||
import (
|
||||
"github.com/crowdsecurity/go-cs-lib/version"
|
||||
)
|
||||
|
||||
func Default() string {
|
||||
return "crowdsec/" + version.String() + "-" + version.System
|
||||
}
|
|
@ -23,7 +23,6 @@ import (
|
|||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
|
||||
|
@ -221,7 +220,6 @@ func NewAPIC(config *csconfig.OnlineApiClientCfg, dbClient *database.Client, con
|
|||
ret.apiClient, err = apiclient.NewClient(&apiclient.Config{
|
||||
MachineID: config.Credentials.Login,
|
||||
Password: password,
|
||||
UserAgent: cwversion.UserAgent(),
|
||||
URL: apiURL,
|
||||
PapiURL: papiURL,
|
||||
VersionPrefix: "v3",
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
)
|
||||
|
||||
func TestAPICSendMetrics(t *testing.T) {
|
||||
|
@ -70,7 +69,7 @@ func TestAPICSendMetrics(t *testing.T) {
|
|||
apiClient, err := apiclient.NewDefaultClient(
|
||||
url,
|
||||
"/api",
|
||||
cwversion.UserAgent(),
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
|
|
@ -26,7 +26,6 @@ import (
|
|||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent/decision"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
|
||||
|
@ -676,7 +675,7 @@ func TestAPICWhitelists(t *testing.T) {
|
|||
apic, err := apiclient.NewDefaultClient(
|
||||
url,
|
||||
"/api",
|
||||
cwversion.UserAgent(),
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
@ -817,7 +816,7 @@ func TestAPICPullTop(t *testing.T) {
|
|||
apic, err := apiclient.NewDefaultClient(
|
||||
url,
|
||||
"/api",
|
||||
cwversion.UserAgent(),
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
@ -832,8 +831,7 @@ func TestAPICPullTop(t *testing.T) {
|
|||
alerts := api.dbClient.Ent.Alert.Query().AllX(context.Background())
|
||||
validDecisions := api.dbClient.Ent.Decision.Query().Where(
|
||||
decision.UntilGT(time.Now())).
|
||||
AllX(context.Background(),
|
||||
)
|
||||
AllX(context.Background())
|
||||
|
||||
decisionScenarioFreq := make(map[string]int)
|
||||
alertScenario := make(map[string]int)
|
||||
|
@ -905,7 +903,7 @@ func TestAPICPullTopBLCacheFirstCall(t *testing.T) {
|
|||
apic, err := apiclient.NewDefaultClient(
|
||||
url,
|
||||
"/api",
|
||||
cwversion.UserAgent(),
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
@ -997,7 +995,7 @@ func TestAPICPullTopBLCacheForceCall(t *testing.T) {
|
|||
apic, err := apiclient.NewDefaultClient(
|
||||
url,
|
||||
"/api",
|
||||
cwversion.UserAgent(),
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
@ -1024,7 +1022,7 @@ func TestAPICPullBlocklistCall(t *testing.T) {
|
|||
apic, err := apiclient.NewDefaultClient(
|
||||
url,
|
||||
"/api",
|
||||
cwversion.UserAgent(),
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
@ -1107,7 +1105,7 @@ func TestAPICPush(t *testing.T) {
|
|||
apic, err := apiclient.NewDefaultClient(
|
||||
url,
|
||||
"/api",
|
||||
cwversion.UserAgent(),
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
@ -1171,7 +1169,7 @@ func TestAPICPull(t *testing.T) {
|
|||
apic, err := apiclient.NewDefaultClient(
|
||||
url,
|
||||
"/api",
|
||||
cwversion.UserAgent(),
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
|
|
@ -8,8 +8,9 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -46,7 +47,7 @@ func (c *CrowdsecCTIClient) doRequest(method string, endpoint string, params map
|
|||
}
|
||||
|
||||
req.Header.Set("X-Api-Key", c.apiKey)
|
||||
req.Header.Set("User-Agent", cwversion.UserAgent())
|
||||
req.Header.Set("User-Agent", useragent.Default())
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
|
||||
)
|
||||
|
||||
// hubTransport wraps a Transport to set a custom User-Agent.
|
||||
|
@ -16,7 +16,7 @@ type hubTransport struct {
|
|||
}
|
||||
|
||||
func (t *hubTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Set("User-Agent", cwversion.UserAgent())
|
||||
req.Header.Set("User-Agent", useragent.Default())
|
||||
return t.RoundTripper.RoundTrip(req)
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@ import (
|
|||
goversion "github.com/hashicorp/go-version"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/version"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -28,7 +30,7 @@ func FullString() string {
|
|||
ret += fmt.Sprintf("GoVersion: %s\n", version.GoVersion)
|
||||
ret += fmt.Sprintf("Platform: %s\n", version.System)
|
||||
ret += fmt.Sprintf("libre2: %s\n", Libre2)
|
||||
ret += fmt.Sprintf("User-Agent: %s\n", UserAgent())
|
||||
ret += fmt.Sprintf("User-Agent: %s\n", useragent.Default())
|
||||
ret += fmt.Sprintf("Constraint_parser: %s\n", Constraint_parser)
|
||||
ret += fmt.Sprintf("Constraint_scenario: %s\n", Constraint_scenario)
|
||||
ret += fmt.Sprintf("Constraint_api: %s\n", Constraint_api)
|
||||
|
@ -37,10 +39,6 @@ func FullString() string {
|
|||
return ret
|
||||
}
|
||||
|
||||
func UserAgent() string {
|
||||
return "crowdsec/" + version.String() + "-" + version.System
|
||||
}
|
||||
|
||||
// VersionStrip remove the tag from the version string, used to match with a hub branch
|
||||
func VersionStrip() string {
|
||||
ret := strings.Split(version.Version, "~")
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/dghubble/sling"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
|
||||
)
|
||||
|
||||
type MBClient struct {
|
||||
|
@ -38,7 +38,7 @@ var (
|
|||
func NewMBClient(url string) (*MBClient, error) {
|
||||
httpClient := &http.Client{Timeout: 20 * time.Second}
|
||||
return &MBClient{
|
||||
CTX: sling.New().Client(httpClient).Base(url).Set("User-Agent", cwversion.UserAgent()),
|
||||
CTX: sling.New().Client(httpClient).Base(url).Set("User-Agent", useragent.Default()),
|
||||
Client: httpClient,
|
||||
}, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue