Skip to content

Commit

Permalink
cscli support: collect profiling data and logs (#2987)
Browse files Browse the repository at this point in the history
* extract methods, avoid globals
* collect logs to file dump.log
* include pprof data
* include latest logs
  • Loading branch information
mmetc authored May 7, 2024
1 parent a2dcc0e commit 11da728
Show file tree
Hide file tree
Showing 6 changed files with 489 additions and 336 deletions.
58 changes: 36 additions & 22 deletions cmd/crowdsec-cli/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,11 @@ func (cli *cliCapi) newRegisterCmd() *cobra.Command {
return cmd
}

func (cli *cliCapi) status() error {
cfg := cli.cfg()

if err := require.CAPIRegistered(cfg); err != nil {
return err
}

password := strfmt.Password(cfg.API.Server.OnlineClient.Credentials.Password)

apiurl, err := url.Parse(cfg.API.Server.OnlineClient.Credentials.URL)
// QueryCAPIStatus checks if the Local API is reachable, and if the credentials are correct
func QueryCAPIStatus(hub *cwhub.Hub, credURL string, login string, password string) error {
apiURL, err := url.Parse(credURL)
if err != nil {
return fmt.Errorf("parsing api url ('%s'): %w", cfg.API.Server.OnlineClient.Credentials.URL, err)
}

hub, err := require.Hub(cfg, nil, nil)
if err != nil {
return err
return fmt.Errorf("parsing api url: %w", err)
}

scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS)
Expand All @@ -183,22 +171,48 @@ func (cli *cliCapi) status() error {
return errors.New("no scenarios installed, abort")
}

Client, err = apiclient.NewDefaultClient(apiurl, CAPIURLPrefix, fmt.Sprintf("crowdsec/%s", version.String()), nil)
Client, err = apiclient.NewDefaultClient(apiURL,
CAPIURLPrefix,
fmt.Sprintf("crowdsec/%s", version.String()),
nil)
if err != nil {
return fmt.Errorf("init default client: %w", err)
}

pw := strfmt.Password(password)

t := models.WatcherAuthRequest{
MachineID: &cfg.API.Server.OnlineClient.Credentials.Login,
Password: &password,
MachineID: &login,
Password: &pw,
Scenarios: scenarios,
}

log.Infof("Loaded credentials from %s", cfg.API.Server.OnlineClient.CredentialsFilePath)
log.Infof("Trying to authenticate with username %s on %s", cfg.API.Server.OnlineClient.Credentials.Login, apiurl)

_, _, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
if err != nil {
return err
}

return nil
}

func (cli *cliCapi) status() error {
cfg := cli.cfg()

if err := require.CAPIRegistered(cfg); err != nil {
return err
}

cred := cfg.API.Server.OnlineClient.Credentials

hub, err := require.Hub(cfg, nil, nil)
if err != nil {
return err
}

log.Infof("Loaded credentials from %s", cfg.API.Server.OnlineClient.CredentialsFilePath)
log.Infof("Trying to authenticate with username %s on %s", cred.Login, cred.URL)

if err := QueryCAPIStatus(hub, cred.URL, cred.Login, cred.Password); err != nil {
return fmt.Errorf("failed to authenticate to Central API (CAPI): %w", err)
}

Expand Down
44 changes: 26 additions & 18 deletions cmd/crowdsec-cli/lapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,13 @@ func NewCLILapi(cfg configGetter) *cliLapi {
}
}

func (cli *cliLapi) status() error {
cfg := cli.cfg()
password := strfmt.Password(cfg.API.Client.Credentials.Password)
login := cfg.API.Client.Credentials.Login

origURL := cfg.API.Client.Credentials.URL

apiURL, err := url.Parse(origURL)
// QueryLAPIStatus checks if the Local API is reachable, and if the credentials are correct
func QueryLAPIStatus(hub *cwhub.Hub, credURL string, login string, password string) error {
apiURL, err := url.Parse(credURL)
if err != nil {
return fmt.Errorf("parsing api url: %w", err)
}

hub, err := require.Hub(cfg, nil, nil)
if err != nil {
return err
}

scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS)
if err != nil {
return fmt.Errorf("failed to get scenarios: %w", err)
Expand All @@ -69,18 +59,36 @@ func (cli *cliLapi) status() error {
return fmt.Errorf("init default client: %w", err)
}

pw := strfmt.Password(password)

t := models.WatcherAuthRequest{
MachineID: &login,
Password: &password,
Password: &pw,
Scenarios: scenarios,
}

log.Infof("Loaded credentials from %s", cfg.API.Client.CredentialsFilePath)
// use the original string because apiURL would print 'http://unix/'
log.Infof("Trying to authenticate with username %s on %s", login, origURL)

_, _, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
if err != nil {
return err
}

return nil
}

func (cli *cliLapi) status() error {
cfg := cli.cfg()

cred := cfg.API.Client.Credentials

hub, err := require.Hub(cfg, nil, nil)
if err != nil {
return err
}

log.Infof("Loaded credentials from %s", cfg.API.Client.CredentialsFilePath)
log.Infof("Trying to authenticate with username %s on %s", cred.Login, cred.URL)

if err := QueryLAPIStatus(hub, cred.URL, cred.Login, cred.Password); err != nil {
return fmt.Errorf("failed to authenticate to Local API (LAPI): %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
cmd.AddCommand(NewCLIExplain(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIHubTest(cli.cfg).NewCommand())
cmd.AddCommand(NewCLINotifications(cli.cfg).NewCommand())
cmd.AddCommand(NewCLISupport().NewCommand())
cmd.AddCommand(NewCLISupport(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIPapi(cli.cfg).NewCommand())
cmd.AddCommand(NewCLICollection(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIParser(cli.cfg).NewCommand())
Expand Down
Loading

0 comments on commit 11da728

Please sign in to comment.