Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: pkg/apiclient set and use default user agent #3219

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cmd/crowdsec-cli/clialert/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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",
})
Expand Down
3 changes: 0 additions & 3 deletions cmd/crowdsec-cli/clicapi/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions cmd/crowdsec-cli/cliconsole/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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",
})
Expand Down
2 changes: 0 additions & 2 deletions cmd/crowdsec-cli/clidecision/decisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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",
})
Expand Down
4 changes: 1 addition & 3 deletions cmd/crowdsec-cli/clilapi/lapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions cmd/crowdsec-cli/clinotifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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",
})
Expand Down
2 changes: 0 additions & 2 deletions cmd/crowdsec/lapiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.DefaultUserAgent()
return &LokiClient{Logger: log.WithField("component", "lokiclient"), config: config, requestHeaders: headers}
}
5 changes: 0 additions & 5 deletions pkg/apiclient/alerts_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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",
})
Expand Down Expand Up @@ -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",
})
Expand Down Expand Up @@ -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",
})
Expand Down Expand Up @@ -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",
})
Expand Down
5 changes: 0 additions & 5 deletions pkg/apiclient/auth_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -87,7 +86,6 @@ func TestWatcherRegister(t *testing.T) {
clientconfig := Config{
MachineID: "test_login",
Password: "test_password",
UserAgent: cwversion.UserAgent(),
URL: apiURL,
VersionPrefix: "v1",
}
Expand Down Expand Up @@ -125,7 +123,6 @@ func TestWatcherAuth(t *testing.T) {
clientConfig := &Config{
MachineID: "test_login",
Password: "test_password",
UserAgent: cwversion.UserAgent(),
URL: apiURL,
VersionPrefix: "v1",
Scenarios: []string{"crowdsecurity/test"},
Expand Down Expand Up @@ -206,7 +203,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"},
Expand Down Expand Up @@ -260,7 +256,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"},
Expand Down
21 changes: 18 additions & 3 deletions pkg/apiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -66,11 +67,16 @@ type service struct {
}

func NewClient(config *Config) (*ApiClient, error) {
userAgent := config.UserAgent
if userAgent == "" {
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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -143,6 +149,10 @@ func NewDefaultClient(URL *url.URL, prefix string, userAgent string, client *htt
}
}

if userAgent == "" {
userAgent = useragent.DefaultUserAgent()
}

c := &ApiClient{client: client, BaseURL: baseURL, UserAgent: userAgent, URLPrefix: prefix}
c.common.client = c
c.Decisions = (*DecisionsService)(&c.common)
Expand Down Expand Up @@ -178,7 +188,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 = 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)
Expand Down
4 changes: 0 additions & 4 deletions pkg/apiclient/client_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -25,7 +23,6 @@ func TestNewRequestInvalid(t *testing.T) {
client, err := NewClient(&Config{
MachineID: "test_login",
Password: "test_password",
UserAgent: cwversion.UserAgent(),
URL: apiURL,
VersionPrefix: "v1",
})
Expand Down Expand Up @@ -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",
})
Expand Down
9 changes: 0 additions & 9 deletions pkg/apiclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down Expand Up @@ -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",
})
Expand Down Expand Up @@ -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",
})
Expand Down Expand Up @@ -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",
})
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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{})
Expand Down
3 changes: 0 additions & 3 deletions pkg/apiclient/decisions_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -402,7 +401,6 @@ func TestDeleteDecisions(t *testing.T) {
client, err := NewClient(&Config{
MachineID: "test_login",
Password: "test_password",
UserAgent: cwversion.UserAgent(),
URL: apiURL,
VersionPrefix: "v1",
})
Expand Down Expand Up @@ -502,7 +500,6 @@ func TestDecisionsStreamOpts_addQueryParamsToURL(t *testing.T) {
// client, err := NewClient(&Config{
// MachineID: "test_login",
// Password: "test_password",
// UserAgent: cwversion.UserAgent(),
// URL: apiURL,
// VersionPrefix: "v1",
// })
Expand Down
Loading