Skip to content

Commit

Permalink
refact: pkg/apiclient set and use default user agent (crowdsecurity#3219
Browse files Browse the repository at this point in the history
)

* default user agent

* DRY default user agent

* useragent.go

* moved to pkg/apiclient/useragent

* lint

* rename useragent.DefaultUserAgent() -> useragent.Default()
  • Loading branch information
mmetc authored Sep 9, 2024
1 parent 57dee1a commit c8750f6
Show file tree
Hide file tree
Showing 22 changed files with 82 additions and 95 deletions.
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.Default()
return &LokiClient{Logger: log.WithField("component", "lokiclient"), config: config, requestHeaders: headers}
}
17 changes: 6 additions & 11 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 @@ -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)
Expand All @@ -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 @@ -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")
}
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
19 changes: 9 additions & 10 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 All @@ -36,20 +35,22 @@ 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")
w.WriteHeader(http.StatusBadRequest)
}

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"}`
Expand All @@ -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)

Expand All @@ -87,7 +88,6 @@ func TestWatcherRegister(t *testing.T) {
clientconfig := Config{
MachineID: "test_login",
Password: "test_password",
UserAgent: cwversion.UserAgent(),
URL: apiURL,
VersionPrefix: "v1",
}
Expand All @@ -113,19 +113,18 @@ 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)

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"},
Expand Down Expand Up @@ -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")
Expand All @@ -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)

Expand All @@ -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"},
Expand All @@ -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()
Expand Down Expand Up @@ -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"},
Expand Down
23 changes: 19 additions & 4 deletions pkg/apiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/golang-jwt/jwt/v4"

"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
"github.com/crowdsecurity/crowdsec/pkg/models"
)

Expand Down Expand Up @@ -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(
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.Default()
}

c := &ApiClient{client: client, BaseURL: baseURL, UserAgent: userAgent, URLPrefix: prefix}
c.common.client = c
c.Decisions = (*DecisionsService)(&c.common)
Expand Down Expand Up @@ -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)
}
Expand Down
Loading

0 comments on commit c8750f6

Please sign in to comment.