2021-06-28 15:34:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
|
|
|
"github.com/go-openapi/strfmt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewConsoleCmd() *cobra.Command {
|
|
|
|
var cmdConsole = &cobra.Command{
|
2021-09-03 10:56:17 +00:00
|
|
|
Use: "console [action]",
|
|
|
|
Short: "Manage interaction with Crowdsec console (https://app.crowdsec.net)",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
2021-06-28 15:34:19 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
|
|
|
log.Fatal("Local API is disabled, please run this command on the local API machine")
|
|
|
|
}
|
|
|
|
if csConfig.API.Server.OnlineClient == nil {
|
2021-08-25 16:30:05 +00:00
|
|
|
log.Fatalf("no configuration for Central API (CAPI) in '%s'", *csConfig.FilePath)
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdEnroll := &cobra.Command{
|
|
|
|
Use: "enroll [enroll-key]",
|
|
|
|
Short: "Enroll this instance to https://app.crowdsec.net [requires local API]",
|
|
|
|
Long: `
|
|
|
|
Enroll this instance to https://app.crowdsec.net
|
|
|
|
|
|
|
|
You can get your enrollment key by creating an account on https://app.crowdsec.net.
|
|
|
|
After running this command your will need to validate the enrollment in the webapp.`,
|
2021-08-31 13:03:47 +00:00
|
|
|
Example: "cscli console enroll YOUR-ENROLL-KEY",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
2021-06-28 15:34:19 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
|
|
|
log.Fatal("Local API is disabled, please run this command on the local API machine")
|
|
|
|
}
|
|
|
|
if csConfig.API.Server.OnlineClient == nil {
|
2021-08-25 16:30:05 +00:00
|
|
|
log.Fatalf("no configuration for Central API (CAPI) in '%s'", *csConfig.FilePath)
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
|
|
|
if csConfig.API.Server.OnlineClient.Credentials == nil {
|
2021-08-25 16:30:05 +00:00
|
|
|
log.Fatal("You must configure Central API (CAPI) with `cscli capi register` before enrolling your instance")
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
password := strfmt.Password(csConfig.API.Server.OnlineClient.Credentials.Password)
|
|
|
|
apiURL, err := url.Parse(csConfig.API.Server.OnlineClient.Credentials.URL)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not parse CAPI URL : %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := csConfig.LoadHub(); err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
|
|
|
|
log.Fatalf("Failed to load hub index : %s", err)
|
|
|
|
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios, err := cwhub.GetUpstreamInstalledScenariosAsString()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to get scenarios : %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(scenarios) == 0 {
|
|
|
|
scenarios = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, _ := apiclient.NewClient(&apiclient.Config{
|
|
|
|
MachineID: csConfig.API.Server.OnlineClient.Credentials.Login,
|
|
|
|
Password: password,
|
|
|
|
Scenarios: scenarios,
|
|
|
|
UserAgent: fmt.Sprintf("crowdsec/%s", cwversion.VersionStr()),
|
|
|
|
URL: apiURL,
|
|
|
|
VersionPrefix: "v2",
|
|
|
|
})
|
|
|
|
_, err = c.Auth.EnrollWatcher(context.Background(), args[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not enroll instance: %s", err)
|
|
|
|
}
|
|
|
|
log.Infof("Watcher successfully enrolled. Visit https://app.crowdsec.net to accept it.")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdConsole.AddCommand(cmdEnroll)
|
|
|
|
return cmdConsole
|
|
|
|
}
|