From 683d441efc9259a9f35f10b9e32d385f4702e0b2 Mon Sep 17 00:00:00 2001 From: marco Date: Sun, 8 Sep 2024 23:28:36 +0200 Subject: [PATCH 1/6] default user agent --- cmd/crowdsec-cli/clialert/alerts.go | 3 +-- cmd/crowdsec-cli/clicapi/capi.go | 5 ++--- cmd/crowdsec-cli/cliconsole/console.go | 3 +-- cmd/crowdsec-cli/clidecision/decisions.go | 3 +-- cmd/crowdsec-cli/clilapi/lapi.go | 5 ++--- .../clinotifications/notifications.go | 3 +-- cmd/crowdsec/lapiclient.go | 2 -- .../loki/internal/lokiclient/loki_client.go | 4 ++-- pkg/apiclient/alerts_service_test.go | 9 ++++----- pkg/apiclient/auth_service_test.go | 9 ++++----- pkg/apiclient/client.go | 20 ++++++++++++++++--- pkg/apiclient/client_http_test.go | 6 ++---- pkg/apiclient/client_test.go | 16 +++++++-------- pkg/apiclient/decisions_service_test.go | 5 ++--- pkg/apiserver/apic.go | 3 +-- pkg/apiserver/apic_metrics_test.go | 3 +-- pkg/apiserver/apic_test.go | 15 +++++++------- pkg/cticlient/client.go | 4 ++-- pkg/cwhub/cwhub.go | 4 ++-- pkg/cwversion/version.go | 8 +++----- pkg/metabase/api.go | 4 ++-- 21 files changed, 64 insertions(+), 70 deletions(-) diff --git a/cmd/crowdsec-cli/clialert/alerts.go b/cmd/crowdsec-cli/clialert/alerts.go index 13013153a79..699104bba5d 100644 --- a/cmd/crowdsec-cli/clialert/alerts.go +++ b/cmd/crowdsec-cli/clialert/alerts.go @@ -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,7 @@ 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(), + UserAgent: apiclient.DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) diff --git a/cmd/crowdsec-cli/clicapi/capi.go b/cmd/crowdsec-cli/clicapi/capi.go index 4d658e3a602..26a3865a2fc 100644 --- a/cmd/crowdsec-cli/clicapi/capi.go +++ b/cmd/crowdsec-cli/clicapi/capi.go @@ -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,7 @@ func (cli *cliCapi) register(capiUserPrefix string, outputFile string) error { _, err = apiclient.RegisterClient(&apiclient.Config{ MachineID: capiUser, Password: password, - UserAgent: cwversion.UserAgent(), + UserAgent: apiclient.DefaultUserAgent(), URL: apiurl, VersionPrefix: "v3", }, nil) @@ -168,7 +167,7 @@ func queryCAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri MachineID: login, Password: passwd, Scenarios: itemsForAPI, - UserAgent: cwversion.UserAgent(), + UserAgent: apiclient.DefaultUserAgent(), URL: apiURL, // I don't believe papi is neede to check enrollement // PapiURL: papiURL, diff --git a/cmd/crowdsec-cli/cliconsole/console.go b/cmd/crowdsec-cli/cliconsole/console.go index d15f25eaf69..18a16247d13 100644 --- a/cmd/crowdsec-cli/cliconsole/console.go +++ b/cmd/crowdsec-cli/cliconsole/console.go @@ -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,7 @@ 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(), + UserAgent: apiclient.DefaultUserAgent(), URL: apiURL, VersionPrefix: "v3", }) diff --git a/cmd/crowdsec-cli/clidecision/decisions.go b/cmd/crowdsec-cli/clidecision/decisions.go index 5ecb3fc3304..b5e494d6aa7 100644 --- a/cmd/crowdsec-cli/clidecision/decisions.go +++ b/cmd/crowdsec-cli/clidecision/decisions.go @@ -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,7 @@ 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(), + UserAgent: apiclient.DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) diff --git a/cmd/crowdsec-cli/clilapi/lapi.go b/cmd/crowdsec-cli/clilapi/lapi.go index a6b88101cbf..8fdfe8fce40 100644 --- a/cmd/crowdsec-cli/clilapi/lapi.go +++ b/cmd/crowdsec-cli/clilapi/lapi.go @@ -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(), + apiclient.DefaultUserAgent(), nil) if err != nil { return false, err @@ -118,7 +117,7 @@ func (cli *cliLapi) register(apiURL string, outputFile string, machine string, t _, err = apiclient.RegisterClient(&apiclient.Config{ MachineID: lapiUser, Password: password, - UserAgent: cwversion.UserAgent(), + UserAgent: apiclient.DefaultUserAgent(), RegistrationToken: token, URL: apiurl, VersionPrefix: LAPIURLPrefix, diff --git a/cmd/crowdsec-cli/clinotifications/notifications.go b/cmd/crowdsec-cli/clinotifications/notifications.go index 04be09354c2..ab781abf36b 100644 --- a/cmd/crowdsec-cli/clinotifications/notifications.go +++ b/cmd/crowdsec-cli/clinotifications/notifications.go @@ -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,7 @@ 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(), + UserAgent: apiclient.DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) diff --git a/cmd/crowdsec/lapiclient.go b/cmd/crowdsec/lapiclient.go index cbafb460042..4556306825c 100644 --- a/cmd/crowdsec/lapiclient.go +++ b/cmd/crowdsec/lapiclient.go @@ -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", diff --git a/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go b/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go index 420da6e391c..129241a8a39 100644 --- a/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go +++ b/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go @@ -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" ) 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"] = apiclient.DefaultUserAgent() return &LokiClient{Logger: log.WithField("component", "lokiclient"), config: config, requestHeaders: headers} } diff --git a/pkg/apiclient/alerts_service_test.go b/pkg/apiclient/alerts_service_test.go index 12ef2d295f4..c307109b789 100644 --- a/pkg/apiclient/alerts_service_test.go +++ b/pkg/apiclient/alerts_service_test.go @@ -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,7 @@ func TestAlertsListAsMachine(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -214,7 +213,7 @@ func TestAlertsGetAsMachine(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -388,7 +387,7 @@ func TestAlertsCreateAsMachine(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -430,7 +429,7 @@ func TestAlertsDeleteAsMachine(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) diff --git a/pkg/apiclient/auth_service_test.go b/pkg/apiclient/auth_service_test.go index 6c9abc0edef..8418054b181 100644 --- a/pkg/apiclient/auth_service_test.go +++ b/pkg/apiclient/auth_service_test.go @@ -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" ) @@ -87,7 +86,7 @@ func TestWatcherRegister(t *testing.T) { clientconfig := Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", } @@ -125,7 +124,7 @@ func TestWatcherAuth(t *testing.T) { clientConfig := &Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", Scenarios: []string{"crowdsecurity/test"}, @@ -206,7 +205,7 @@ func TestWatcherUnregister(t *testing.T) { mycfg := &Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", Scenarios: []string{"crowdsecurity/test"}, @@ -260,7 +259,7 @@ func TestWatcherEnroll(t *testing.T) { mycfg := &Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", Scenarios: []string{"crowdsecurity/test"}, diff --git a/pkg/apiclient/client.go b/pkg/apiclient/client.go index 2cb68f597f3..ce6edf97ba0 100644 --- a/pkg/apiclient/client.go +++ b/pkg/apiclient/client.go @@ -66,11 +66,16 @@ type service struct { } func NewClient(config *Config) (*ApiClient, error) { + userAgent := config.UserAgent + if userAgent == "" { + userAgent = DefaultUserAgent() + } + 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 +110,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 +148,10 @@ func NewDefaultClient(URL *url.URL, prefix string, userAgent string, client *htt } } + if userAgent == "" { + userAgent = DefaultUserAgent() + } + c := &ApiClient{client: client, BaseURL: baseURL, UserAgent: userAgent, URLPrefix: prefix} c.common.client = c c.Decisions = (*DecisionsService)(&c.common) @@ -178,7 +187,12 @@ 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 = DefaultUserAgent() + } + + 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) diff --git a/pkg/apiclient/client_http_test.go b/pkg/apiclient/client_http_test.go index 4bdfe1d0da5..4f67e8c07d1 100644 --- a/pkg/apiclient/client_http_test.go +++ b/pkg/apiclient/client_http_test.go @@ -10,8 +10,6 @@ import ( "github.com/stretchr/testify/require" "github.com/crowdsecurity/go-cs-lib/cstest" - - "github.com/crowdsecurity/crowdsec/pkg/cwversion" ) func TestNewRequestInvalid(t *testing.T) { @@ -25,7 +23,7 @@ func TestNewRequestInvalid(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -57,7 +55,7 @@ func TestNewRequestTimeout(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) diff --git a/pkg/apiclient/client_test.go b/pkg/apiclient/client_test.go index bd83e512afc..146e8ac18d7 100644 --- a/pkg/apiclient/client_test.go +++ b/pkg/apiclient/client_test.go @@ -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,7 @@ func TestNewClientOk(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -134,7 +132,7 @@ func TestNewClientOk_UnixSocket(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -172,7 +170,7 @@ func TestNewClientKo(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -250,7 +248,7 @@ func TestNewClientRegisterKO(t *testing.T) { _, err = RegisterClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }, &http.Client{}) @@ -281,7 +279,7 @@ func TestNewClientRegisterOK(t *testing.T) { client, err := RegisterClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }, &http.Client{}) @@ -314,7 +312,7 @@ func TestNewClientRegisterOK_UnixSocket(t *testing.T) { client, err := RegisterClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }, &http.Client{}) @@ -344,7 +342,7 @@ func TestNewClientBadAnswer(t *testing.T) { _, err = RegisterClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }, &http.Client{}) diff --git a/pkg/apiclient/decisions_service_test.go b/pkg/apiclient/decisions_service_test.go index 6942cfc9d85..3f18bc5a7c4 100644 --- a/pkg/apiclient/decisions_service_test.go +++ b/pkg/apiclient/decisions_service_test.go @@ -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" ) @@ -402,7 +401,7 @@ func TestDeleteDecisions(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: cwversion.UserAgent(), + UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -502,7 +501,7 @@ func TestDecisionsStreamOpts_addQueryParamsToURL(t *testing.T) { // client, err := NewClient(&Config{ // MachineID: "test_login", // Password: "test_password", -// UserAgent: cwversion.UserAgent(), +// UserAgent: DefaultUserAgent(), // URL: apiURL, // VersionPrefix: "v1", // }) diff --git a/pkg/apiserver/apic.go b/pkg/apiserver/apic.go index 5b850cbff0d..cbe0741de99 100644 --- a/pkg/apiserver/apic.go +++ b/pkg/apiserver/apic.go @@ -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,7 @@ 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(), + UserAgent: apiclient.DefaultUserAgent(), URL: apiURL, PapiURL: papiURL, VersionPrefix: "v3", diff --git a/pkg/apiserver/apic_metrics_test.go b/pkg/apiserver/apic_metrics_test.go index d1e48ac90a3..25f0e586ccf 100644 --- a/pkg/apiserver/apic_metrics_test.go +++ b/pkg/apiserver/apic_metrics_test.go @@ -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(), + apiclient.DefaultUserAgent(), nil, ) require.NoError(t, err) diff --git a/pkg/apiserver/apic_test.go b/pkg/apiserver/apic_test.go index 546a236251f..5474b3f7449 100644 --- a/pkg/apiserver/apic_test.go +++ b/pkg/apiserver/apic_test.go @@ -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(), + apiclient.DefaultUserAgent(), nil, ) require.NoError(t, err) @@ -817,7 +816,7 @@ func TestAPICPullTop(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - cwversion.UserAgent(), + apiclient.DefaultUserAgent(), nil, ) require.NoError(t, err) @@ -905,7 +904,7 @@ func TestAPICPullTopBLCacheFirstCall(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - cwversion.UserAgent(), + apiclient.DefaultUserAgent(), nil, ) require.NoError(t, err) @@ -997,7 +996,7 @@ func TestAPICPullTopBLCacheForceCall(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - cwversion.UserAgent(), + apiclient.DefaultUserAgent(), nil, ) require.NoError(t, err) @@ -1024,7 +1023,7 @@ func TestAPICPullBlocklistCall(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - cwversion.UserAgent(), + apiclient.DefaultUserAgent(), nil, ) require.NoError(t, err) @@ -1107,7 +1106,7 @@ func TestAPICPush(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - cwversion.UserAgent(), + apiclient.DefaultUserAgent(), nil, ) require.NoError(t, err) @@ -1171,7 +1170,7 @@ func TestAPICPull(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - cwversion.UserAgent(), + apiclient.DefaultUserAgent(), nil, ) require.NoError(t, err) diff --git a/pkg/cticlient/client.go b/pkg/cticlient/client.go index b817121e222..a4ead80550c 100644 --- a/pkg/cticlient/client.go +++ b/pkg/cticlient/client.go @@ -8,7 +8,7 @@ import ( "net/http" "strings" - "github.com/crowdsecurity/crowdsec/pkg/cwversion" + "github.com/crowdsecurity/crowdsec/pkg/apiclient" log "github.com/sirupsen/logrus" ) @@ -46,7 +46,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", apiclient.DefaultUserAgent()) resp, err := c.httpClient.Do(req) if err != nil { diff --git a/pkg/cwhub/cwhub.go b/pkg/cwhub/cwhub.go index d8607e7e562..5530ce57e5d 100644 --- a/pkg/cwhub/cwhub.go +++ b/pkg/cwhub/cwhub.go @@ -7,7 +7,7 @@ import ( "strings" "time" - "github.com/crowdsecurity/crowdsec/pkg/cwversion" + "github.com/crowdsecurity/crowdsec/pkg/apiclient" ) // 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", apiclient.DefaultUserAgent()) return t.RoundTripper.RoundTrip(req) } diff --git a/pkg/cwversion/version.go b/pkg/cwversion/version.go index 28d5c2a621c..97c359d6ba3 100644 --- a/pkg/cwversion/version.go +++ b/pkg/cwversion/version.go @@ -7,6 +7,8 @@ import ( goversion "github.com/hashicorp/go-version" "github.com/crowdsecurity/go-cs-lib/version" + + "github.com/crowdsecurity/crowdsec/pkg/apiclient" ) 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", apiclient.DefaultUserAgent()) 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, "~") diff --git a/pkg/metabase/api.go b/pkg/metabase/api.go index 387e8d151e0..19324b70916 100644 --- a/pkg/metabase/api.go +++ b/pkg/metabase/api.go @@ -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" ) 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", apiclient.DefaultUserAgent()), Client: httpClient, }, nil } From 9ed4b253e8293a469a048b54013ec7d139977b38 Mon Sep 17 00:00:00 2001 From: marco Date: Sun, 8 Sep 2024 23:41:54 +0200 Subject: [PATCH 2/6] DRY default user agent --- cmd/crowdsec-cli/clialert/alerts.go | 1 - cmd/crowdsec-cli/clicapi/capi.go | 2 -- cmd/crowdsec-cli/cliconsole/console.go | 1 - cmd/crowdsec-cli/clidecision/decisions.go | 1 - cmd/crowdsec-cli/clilapi/lapi.go | 3 +-- cmd/crowdsec-cli/clinotifications/notifications.go | 1 - pkg/apiclient/alerts_service_test.go | 4 ---- pkg/apiclient/auth_service_test.go | 4 ---- pkg/apiclient/client_http_test.go | 2 -- pkg/apiclient/client_test.go | 7 ------- pkg/apiclient/decisions_service_test.go | 2 -- pkg/apiserver/apic.go | 1 - pkg/apiserver/apic_metrics_test.go | 2 +- pkg/apiserver/apic_test.go | 14 +++++++------- 14 files changed, 9 insertions(+), 36 deletions(-) diff --git a/cmd/crowdsec-cli/clialert/alerts.go b/cmd/crowdsec-cli/clialert/alerts.go index 699104bba5d..c5e27394f77 100644 --- a/cmd/crowdsec-cli/clialert/alerts.go +++ b/cmd/crowdsec-cli/clialert/alerts.go @@ -216,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: apiclient.DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) diff --git a/cmd/crowdsec-cli/clicapi/capi.go b/cmd/crowdsec-cli/clicapi/capi.go index 26a3865a2fc..49f68dd6b9e 100644 --- a/cmd/crowdsec-cli/clicapi/capi.go +++ b/cmd/crowdsec-cli/clicapi/capi.go @@ -76,7 +76,6 @@ func (cli *cliCapi) register(capiUserPrefix string, outputFile string) error { _, err = apiclient.RegisterClient(&apiclient.Config{ MachineID: capiUser, Password: password, - UserAgent: apiclient.DefaultUserAgent(), URL: apiurl, VersionPrefix: "v3", }, nil) @@ -167,7 +166,6 @@ func queryCAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri MachineID: login, Password: passwd, Scenarios: itemsForAPI, - UserAgent: apiclient.DefaultUserAgent(), URL: apiURL, // I don't believe papi is neede to check enrollement // PapiURL: papiURL, diff --git a/cmd/crowdsec-cli/cliconsole/console.go b/cmd/crowdsec-cli/cliconsole/console.go index 18a16247d13..e4b4039bdd2 100644 --- a/cmd/crowdsec-cli/cliconsole/console.go +++ b/cmd/crowdsec-cli/cliconsole/console.go @@ -123,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: apiclient.DefaultUserAgent(), URL: apiURL, VersionPrefix: "v3", }) diff --git a/cmd/crowdsec-cli/clidecision/decisions.go b/cmd/crowdsec-cli/clidecision/decisions.go index b5e494d6aa7..b82ebe3086e 100644 --- a/cmd/crowdsec-cli/clidecision/decisions.go +++ b/cmd/crowdsec-cli/clidecision/decisions.go @@ -151,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: apiclient.DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) diff --git a/cmd/crowdsec-cli/clilapi/lapi.go b/cmd/crowdsec-cli/clilapi/lapi.go index 8fdfe8fce40..fa229002512 100644 --- a/cmd/crowdsec-cli/clilapi/lapi.go +++ b/cmd/crowdsec-cli/clilapi/lapi.go @@ -52,7 +52,7 @@ func queryLAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri client, err := apiclient.NewDefaultClient(apiURL, LAPIURLPrefix, - apiclient.DefaultUserAgent(), + "", nil) if err != nil { return false, err @@ -117,7 +117,6 @@ func (cli *cliLapi) register(apiURL string, outputFile string, machine string, t _, err = apiclient.RegisterClient(&apiclient.Config{ MachineID: lapiUser, Password: password, - UserAgent: apiclient.DefaultUserAgent(), RegistrationToken: token, URL: apiurl, VersionPrefix: LAPIURLPrefix, diff --git a/cmd/crowdsec-cli/clinotifications/notifications.go b/cmd/crowdsec-cli/clinotifications/notifications.go index ab781abf36b..87a94243c5d 100644 --- a/cmd/crowdsec-cli/clinotifications/notifications.go +++ b/cmd/crowdsec-cli/clinotifications/notifications.go @@ -463,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: apiclient.DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) diff --git a/pkg/apiclient/alerts_service_test.go b/pkg/apiclient/alerts_service_test.go index c307109b789..84c0aa36b64 100644 --- a/pkg/apiclient/alerts_service_test.go +++ b/pkg/apiclient/alerts_service_test.go @@ -34,7 +34,6 @@ func TestAlertsListAsMachine(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -213,7 +212,6 @@ func TestAlertsGetAsMachine(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -387,7 +385,6 @@ func TestAlertsCreateAsMachine(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -429,7 +426,6 @@ func TestAlertsDeleteAsMachine(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) diff --git a/pkg/apiclient/auth_service_test.go b/pkg/apiclient/auth_service_test.go index 8418054b181..10330ef3170 100644 --- a/pkg/apiclient/auth_service_test.go +++ b/pkg/apiclient/auth_service_test.go @@ -86,7 +86,6 @@ func TestWatcherRegister(t *testing.T) { clientconfig := Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", } @@ -124,7 +123,6 @@ func TestWatcherAuth(t *testing.T) { clientConfig := &Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", Scenarios: []string{"crowdsecurity/test"}, @@ -205,7 +203,6 @@ func TestWatcherUnregister(t *testing.T) { mycfg := &Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", Scenarios: []string{"crowdsecurity/test"}, @@ -259,7 +256,6 @@ func TestWatcherEnroll(t *testing.T) { mycfg := &Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", Scenarios: []string{"crowdsecurity/test"}, diff --git a/pkg/apiclient/client_http_test.go b/pkg/apiclient/client_http_test.go index 4f67e8c07d1..7435c447f07 100644 --- a/pkg/apiclient/client_http_test.go +++ b/pkg/apiclient/client_http_test.go @@ -23,7 +23,6 @@ func TestNewRequestInvalid(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -55,7 +54,6 @@ func TestNewRequestTimeout(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) diff --git a/pkg/apiclient/client_test.go b/pkg/apiclient/client_test.go index 146e8ac18d7..e632ff428c0 100644 --- a/pkg/apiclient/client_test.go +++ b/pkg/apiclient/client_test.go @@ -95,7 +95,6 @@ func TestNewClientOk(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -132,7 +131,6 @@ func TestNewClientOk_UnixSocket(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -170,7 +168,6 @@ func TestNewClientKo(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -248,7 +245,6 @@ func TestNewClientRegisterKO(t *testing.T) { _, err = RegisterClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }, &http.Client{}) @@ -279,7 +275,6 @@ func TestNewClientRegisterOK(t *testing.T) { client, err := RegisterClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }, &http.Client{}) @@ -312,7 +307,6 @@ func TestNewClientRegisterOK_UnixSocket(t *testing.T) { client, err := RegisterClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }, &http.Client{}) @@ -342,7 +336,6 @@ func TestNewClientBadAnswer(t *testing.T) { _, err = RegisterClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }, &http.Client{}) diff --git a/pkg/apiclient/decisions_service_test.go b/pkg/apiclient/decisions_service_test.go index 3f18bc5a7c4..d8d982d5a67 100644 --- a/pkg/apiclient/decisions_service_test.go +++ b/pkg/apiclient/decisions_service_test.go @@ -401,7 +401,6 @@ func TestDeleteDecisions(t *testing.T) { client, err := NewClient(&Config{ MachineID: "test_login", Password: "test_password", - UserAgent: DefaultUserAgent(), URL: apiURL, VersionPrefix: "v1", }) @@ -501,7 +500,6 @@ func TestDecisionsStreamOpts_addQueryParamsToURL(t *testing.T) { // client, err := NewClient(&Config{ // MachineID: "test_login", // Password: "test_password", -// UserAgent: DefaultUserAgent(), // URL: apiURL, // VersionPrefix: "v1", // }) diff --git a/pkg/apiserver/apic.go b/pkg/apiserver/apic.go index cbe0741de99..73061637ad9 100644 --- a/pkg/apiserver/apic.go +++ b/pkg/apiserver/apic.go @@ -220,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: apiclient.DefaultUserAgent(), URL: apiURL, PapiURL: papiURL, VersionPrefix: "v3", diff --git a/pkg/apiserver/apic_metrics_test.go b/pkg/apiserver/apic_metrics_test.go index 25f0e586ccf..78b16f9c8b7 100644 --- a/pkg/apiserver/apic_metrics_test.go +++ b/pkg/apiserver/apic_metrics_test.go @@ -69,7 +69,7 @@ func TestAPICSendMetrics(t *testing.T) { apiClient, err := apiclient.NewDefaultClient( url, "/api", - apiclient.DefaultUserAgent(), + "", nil, ) require.NoError(t, err) diff --git a/pkg/apiserver/apic_test.go b/pkg/apiserver/apic_test.go index 5474b3f7449..eb765d26e13 100644 --- a/pkg/apiserver/apic_test.go +++ b/pkg/apiserver/apic_test.go @@ -675,7 +675,7 @@ func TestAPICWhitelists(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - apiclient.DefaultUserAgent(), + "", nil, ) require.NoError(t, err) @@ -816,7 +816,7 @@ func TestAPICPullTop(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - apiclient.DefaultUserAgent(), + "", nil, ) require.NoError(t, err) @@ -904,7 +904,7 @@ func TestAPICPullTopBLCacheFirstCall(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - apiclient.DefaultUserAgent(), + "", nil, ) require.NoError(t, err) @@ -996,7 +996,7 @@ func TestAPICPullTopBLCacheForceCall(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - apiclient.DefaultUserAgent(), + "", nil, ) require.NoError(t, err) @@ -1023,7 +1023,7 @@ func TestAPICPullBlocklistCall(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - apiclient.DefaultUserAgent(), + "", nil, ) require.NoError(t, err) @@ -1106,7 +1106,7 @@ func TestAPICPush(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - apiclient.DefaultUserAgent(), + "", nil, ) require.NoError(t, err) @@ -1170,7 +1170,7 @@ func TestAPICPull(t *testing.T) { apic, err := apiclient.NewDefaultClient( url, "/api", - apiclient.DefaultUserAgent(), + "", nil, ) require.NoError(t, err) From f8b9b8487f3a44f6797292f9441b77c901655a76 Mon Sep 17 00:00:00 2001 From: marco Date: Mon, 9 Sep 2024 08:58:32 +0200 Subject: [PATCH 3/6] useragent.go --- pkg/apiclient/useragent.go | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 pkg/apiclient/useragent.go diff --git a/pkg/apiclient/useragent.go b/pkg/apiclient/useragent.go new file mode 100644 index 00000000000..02978846dc1 --- /dev/null +++ b/pkg/apiclient/useragent.go @@ -0,0 +1,10 @@ +package apiclient + +import ( + "github.com/crowdsecurity/go-cs-lib/version" +) + +func DefaultUserAgent() string { + return "crowdsec/" + version.String() + "-" + version.System +} + From 6f76a2859c7a0884ea1e0a22024da6ca845c593c Mon Sep 17 00:00:00 2001 From: marco Date: Mon, 9 Sep 2024 09:52:17 +0200 Subject: [PATCH 4/6] moved to pkg/apiclient/useragent --- .../modules/loki/internal/lokiclient/loki_client.go | 4 ++-- pkg/apiclient/client.go | 7 ++++--- pkg/apiclient/{ => useragent}/useragent.go | 2 +- pkg/cticlient/client.go | 5 +++-- pkg/cwhub/cwhub.go | 4 ++-- pkg/cwversion/version.go | 4 ++-- pkg/metabase/api.go | 4 ++-- 7 files changed, 16 insertions(+), 14 deletions(-) rename pkg/apiclient/{ => useragent}/useragent.go (89%) diff --git a/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go b/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go index 129241a8a39..b747d229ffb 100644 --- a/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go +++ b/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go @@ -16,7 +16,7 @@ import ( log "github.com/sirupsen/logrus" "gopkg.in/tomb.v2" - "github.com/crowdsecurity/crowdsec/pkg/apiclient" + "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"] = apiclient.DefaultUserAgent() + headers["User-Agent"] = useragent.DefaultUserAgent() return &LokiClient{Logger: log.WithField("component", "lokiclient"), config: config, requestHeaders: headers} } diff --git a/pkg/apiclient/client.go b/pkg/apiclient/client.go index ce6edf97ba0..6ca8a80389c 100644 --- a/pkg/apiclient/client.go +++ b/pkg/apiclient/client.go @@ -13,6 +13,7 @@ import ( "github.com/golang-jwt/jwt/v4" "github.com/crowdsecurity/crowdsec/pkg/models" + "github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent" ) var ( @@ -68,7 +69,7 @@ type service struct { func NewClient(config *Config) (*ApiClient, error) { userAgent := config.UserAgent if userAgent == "" { - userAgent = DefaultUserAgent() + userAgent = useragent.DefaultUserAgent() } t := &JWTTransport{ @@ -149,7 +150,7 @@ func NewDefaultClient(URL *url.URL, prefix string, userAgent string, client *htt } if userAgent == "" { - userAgent = DefaultUserAgent() + userAgent = useragent.DefaultUserAgent() } c := &ApiClient{client: client, BaseURL: baseURL, UserAgent: userAgent, URLPrefix: prefix} @@ -189,7 +190,7 @@ func RegisterClient(config *Config, client *http.Client) (*ApiClient, error) { userAgent := config.UserAgent if userAgent == "" { - userAgent = DefaultUserAgent() + userAgent = useragent.DefaultUserAgent() } c := &ApiClient{client: client, BaseURL: baseURL, UserAgent: userAgent, URLPrefix: config.VersionPrefix} diff --git a/pkg/apiclient/useragent.go b/pkg/apiclient/useragent/useragent.go similarity index 89% rename from pkg/apiclient/useragent.go rename to pkg/apiclient/useragent/useragent.go index 02978846dc1..43438b31acb 100644 --- a/pkg/apiclient/useragent.go +++ b/pkg/apiclient/useragent/useragent.go @@ -1,4 +1,4 @@ -package apiclient +package useragent import ( "github.com/crowdsecurity/go-cs-lib/version" diff --git a/pkg/cticlient/client.go b/pkg/cticlient/client.go index a4ead80550c..03029941779 100644 --- a/pkg/cticlient/client.go +++ b/pkg/cticlient/client.go @@ -8,8 +8,9 @@ import ( "net/http" "strings" - "github.com/crowdsecurity/crowdsec/pkg/apiclient" 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", apiclient.DefaultUserAgent()) + req.Header.Set("User-Agent", useragent.DefaultUserAgent()) resp, err := c.httpClient.Do(req) if err != nil { diff --git a/pkg/cwhub/cwhub.go b/pkg/cwhub/cwhub.go index 5530ce57e5d..831421c35d4 100644 --- a/pkg/cwhub/cwhub.go +++ b/pkg/cwhub/cwhub.go @@ -7,7 +7,7 @@ import ( "strings" "time" - "github.com/crowdsecurity/crowdsec/pkg/apiclient" + "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", apiclient.DefaultUserAgent()) + req.Header.Set("User-Agent", useragent.DefaultUserAgent()) return t.RoundTripper.RoundTrip(req) } diff --git a/pkg/cwversion/version.go b/pkg/cwversion/version.go index 97c359d6ba3..bab7b760834 100644 --- a/pkg/cwversion/version.go +++ b/pkg/cwversion/version.go @@ -8,7 +8,7 @@ import ( "github.com/crowdsecurity/go-cs-lib/version" - "github.com/crowdsecurity/crowdsec/pkg/apiclient" + "github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent" ) var ( @@ -30,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", apiclient.DefaultUserAgent()) + ret += fmt.Sprintf("User-Agent: %s\n", useragent.DefaultUserAgent()) 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) diff --git a/pkg/metabase/api.go b/pkg/metabase/api.go index 19324b70916..03261dcc711 100644 --- a/pkg/metabase/api.go +++ b/pkg/metabase/api.go @@ -9,7 +9,7 @@ import ( "github.com/dghubble/sling" log "github.com/sirupsen/logrus" - "github.com/crowdsecurity/crowdsec/pkg/apiclient" + "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", apiclient.DefaultUserAgent()), + CTX: sling.New().Client(httpClient).Base(url).Set("User-Agent", useragent.DefaultUserAgent()), Client: httpClient, }, nil } From 20cbbe65d4986d922ab0447f76a3c0f9e949c809 Mon Sep 17 00:00:00 2001 From: marco Date: Mon, 9 Sep 2024 10:27:08 +0200 Subject: [PATCH 5/6] lint --- pkg/apiclient/alerts_service_test.go | 12 ++++++------ pkg/apiclient/auth_service_test.go | 14 ++++++++----- pkg/apiclient/client.go | 4 ++-- pkg/apiclient/client_http_test.go | 2 +- pkg/apiclient/decisions_service_test.go | 26 +++++++++++++++---------- pkg/apiclient/useragent/useragent.go | 1 - pkg/apiserver/apic_test.go | 3 +-- 7 files changed, 35 insertions(+), 27 deletions(-) diff --git a/pkg/apiclient/alerts_service_test.go b/pkg/apiclient/alerts_service_test.go index 84c0aa36b64..0d1ff41685f 100644 --- a/pkg/apiclient/alerts_service_test.go +++ b/pkg/apiclient/alerts_service_test.go @@ -178,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) @@ -357,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") } diff --git a/pkg/apiclient/auth_service_test.go b/pkg/apiclient/auth_service_test.go index 10330ef3170..344e377ad0f 100644 --- a/pkg/apiclient/auth_service_test.go +++ b/pkg/apiclient/auth_service_test.go @@ -35,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") @@ -47,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"}` @@ -75,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) @@ -111,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) @@ -119,7 +121,7 @@ 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", @@ -171,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") @@ -181,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) @@ -225,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() diff --git a/pkg/apiclient/client.go b/pkg/apiclient/client.go index 6ca8a80389c..9cead700e33 100644 --- a/pkg/apiclient/client.go +++ b/pkg/apiclient/client.go @@ -12,8 +12,8 @@ import ( "github.com/golang-jwt/jwt/v4" - "github.com/crowdsecurity/crowdsec/pkg/models" "github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent" + "github.com/crowdsecurity/crowdsec/pkg/models" ) var ( @@ -200,8 +200,8 @@ func RegisterClient(config *Config, client *http.Client) (*ApiClient, error) { 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) } diff --git a/pkg/apiclient/client_http_test.go b/pkg/apiclient/client_http_test.go index 7435c447f07..45cd8410a8e 100644 --- a/pkg/apiclient/client_http_test.go +++ b/pkg/apiclient/client_http_test.go @@ -16,7 +16,7 @@ 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) diff --git a/pkg/apiclient/decisions_service_test.go b/pkg/apiclient/decisions_service_test.go index d8d982d5a67..54c44f43eda 100644 --- a/pkg/apiclient/decisions_service_test.go +++ b/pkg/apiclient/decisions_service_test.go @@ -25,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")) @@ -33,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", } @@ -67,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) @@ -84,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) @@ -98,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) } @@ -106,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", } @@ -133,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) @@ -155,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) @@ -169,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", } @@ -219,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"]}], @@ -230,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", } @@ -304,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", } @@ -390,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) @@ -466,6 +471,7 @@ func TestDecisionsStreamOpts_addQueryParamsToURL(t *testing.T) { got, err := o.addQueryParamsToURL(baseURLString) cstest.RequireErrorContains(t, err, tt.expectedErr) + if tt.expectedErr != "" { return } diff --git a/pkg/apiclient/useragent/useragent.go b/pkg/apiclient/useragent/useragent.go index 43438b31acb..01520449724 100644 --- a/pkg/apiclient/useragent/useragent.go +++ b/pkg/apiclient/useragent/useragent.go @@ -7,4 +7,3 @@ import ( func DefaultUserAgent() string { return "crowdsec/" + version.String() + "-" + version.System } - diff --git a/pkg/apiserver/apic_test.go b/pkg/apiserver/apic_test.go index eb765d26e13..51887006ad4 100644 --- a/pkg/apiserver/apic_test.go +++ b/pkg/apiserver/apic_test.go @@ -831,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) From 1f458702f333f78d76027423770e0585f90cdcf5 Mon Sep 17 00:00:00 2001 From: marco Date: Mon, 9 Sep 2024 10:45:19 +0200 Subject: [PATCH 6/6] rename useragent.DefaultUserAgent() -> useragent.Default() --- .../modules/loki/internal/lokiclient/loki_client.go | 2 +- pkg/apiclient/client.go | 6 +++--- pkg/apiclient/useragent/useragent.go | 2 +- pkg/cticlient/client.go | 2 +- pkg/cwhub/cwhub.go | 2 +- pkg/cwversion/version.go | 2 +- pkg/metabase/api.go | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go b/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go index b747d229ffb..846e833abea 100644 --- a/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go +++ b/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go @@ -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"] = useragent.DefaultUserAgent() + headers["User-Agent"] = useragent.Default() return &LokiClient{Logger: log.WithField("component", "lokiclient"), config: config, requestHeaders: headers} } diff --git a/pkg/apiclient/client.go b/pkg/apiclient/client.go index 9cead700e33..02a99037a04 100644 --- a/pkg/apiclient/client.go +++ b/pkg/apiclient/client.go @@ -69,7 +69,7 @@ type service struct { func NewClient(config *Config) (*ApiClient, error) { userAgent := config.UserAgent if userAgent == "" { - userAgent = useragent.DefaultUserAgent() + userAgent = useragent.Default() } t := &JWTTransport{ @@ -150,7 +150,7 @@ func NewDefaultClient(URL *url.URL, prefix string, userAgent string, client *htt } if userAgent == "" { - userAgent = useragent.DefaultUserAgent() + userAgent = useragent.Default() } c := &ApiClient{client: client, BaseURL: baseURL, UserAgent: userAgent, URLPrefix: prefix} @@ -190,7 +190,7 @@ func RegisterClient(config *Config, client *http.Client) (*ApiClient, error) { userAgent := config.UserAgent if userAgent == "" { - userAgent = useragent.DefaultUserAgent() + userAgent = useragent.Default() } c := &ApiClient{client: client, BaseURL: baseURL, UserAgent: userAgent, URLPrefix: config.VersionPrefix} diff --git a/pkg/apiclient/useragent/useragent.go b/pkg/apiclient/useragent/useragent.go index 01520449724..5a62ce1ac06 100644 --- a/pkg/apiclient/useragent/useragent.go +++ b/pkg/apiclient/useragent/useragent.go @@ -4,6 +4,6 @@ import ( "github.com/crowdsecurity/go-cs-lib/version" ) -func DefaultUserAgent() string { +func Default() string { return "crowdsec/" + version.String() + "-" + version.System } diff --git a/pkg/cticlient/client.go b/pkg/cticlient/client.go index 03029941779..90112d80abf 100644 --- a/pkg/cticlient/client.go +++ b/pkg/cticlient/client.go @@ -47,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", useragent.DefaultUserAgent()) + req.Header.Set("User-Agent", useragent.Default()) resp, err := c.httpClient.Do(req) if err != nil { diff --git a/pkg/cwhub/cwhub.go b/pkg/cwhub/cwhub.go index 831421c35d4..683f1853b43 100644 --- a/pkg/cwhub/cwhub.go +++ b/pkg/cwhub/cwhub.go @@ -16,7 +16,7 @@ type hubTransport struct { } func (t *hubTransport) RoundTrip(req *http.Request) (*http.Response, error) { - req.Header.Set("User-Agent", useragent.DefaultUserAgent()) + req.Header.Set("User-Agent", useragent.Default()) return t.RoundTripper.RoundTrip(req) } diff --git a/pkg/cwversion/version.go b/pkg/cwversion/version.go index bab7b760834..b208467aef5 100644 --- a/pkg/cwversion/version.go +++ b/pkg/cwversion/version.go @@ -30,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.DefaultUserAgent()) + 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) diff --git a/pkg/metabase/api.go b/pkg/metabase/api.go index 03261dcc711..08e10188678 100644 --- a/pkg/metabase/api.go +++ b/pkg/metabase/api.go @@ -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", useragent.DefaultUserAgent()), + CTX: sling.New().Client(httpClient).Base(url).Set("User-Agent", useragent.Default()), Client: httpClient, }, nil }