2020-11-30 09:37:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"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/go-openapi/strfmt"
|
2022-05-31 08:01:30 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-11-30 09:37:17 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var CAPIURLPrefix string = "v2"
|
|
|
|
var CAPIBaseURL string = "https://api.crowdsec.net/"
|
2022-03-24 10:07:54 +00:00
|
|
|
var capiUserPrefix string
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
func NewCapiCmd() *cobra.Command {
|
|
|
|
var cmdCapi = &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "capi [action]",
|
|
|
|
Short: "Manage interaction with Central API (CAPI)",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
2020-11-30 09:37:17 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2021-04-16 08:50:08 +00:00
|
|
|
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
2022-05-31 08:01:30 +00:00
|
|
|
return errors.Wrap(err, "Local API is disabled, please run this command on the local API machine")
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
if csConfig.API.Server.OnlineClient == nil {
|
2021-08-25 16:30:05 +00:00
|
|
|
log.Fatalf("no configuration for Central API in '%s'", *csConfig.FilePath)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmdCapiRegister = &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "register",
|
|
|
|
Short: "Register to Central API (CAPI)",
|
|
|
|
Args: cobra.MinimumNArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2020-11-30 09:37:17 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
var err error
|
2022-03-24 10:07:54 +00:00
|
|
|
capiUser, err := generateID(capiUserPrefix)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to generate machine id: %s", err)
|
|
|
|
}
|
|
|
|
password := strfmt.Password(generatePassword(passwordLength))
|
|
|
|
apiurl, err := url.Parse(CAPIBaseURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to parse api url %s : %s", CAPIBaseURL, err)
|
|
|
|
}
|
|
|
|
_, err = apiclient.RegisterClient(&apiclient.Config{
|
2022-03-24 10:07:54 +00:00
|
|
|
MachineID: capiUser,
|
2020-11-30 09:37:17 +00:00
|
|
|
Password: password,
|
|
|
|
UserAgent: fmt.Sprintf("crowdsec/%s", cwversion.VersionStr()),
|
|
|
|
URL: apiurl,
|
|
|
|
VersionPrefix: CAPIURLPrefix,
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("api client register ('%s'): %s", CAPIBaseURL, err)
|
|
|
|
}
|
|
|
|
log.Printf("Successfully registered to Central API (CAPI)")
|
|
|
|
|
|
|
|
var dumpFile string
|
|
|
|
|
|
|
|
if outputFile != "" {
|
|
|
|
dumpFile = outputFile
|
|
|
|
} else if csConfig.API.Server.OnlineClient.CredentialsFilePath != "" {
|
|
|
|
dumpFile = csConfig.API.Server.OnlineClient.CredentialsFilePath
|
|
|
|
} else {
|
|
|
|
dumpFile = ""
|
|
|
|
}
|
|
|
|
apiCfg := csconfig.ApiCredentialsCfg{
|
2022-03-24 10:07:54 +00:00
|
|
|
Login: capiUser,
|
2020-11-30 09:37:17 +00:00
|
|
|
Password: password.String(),
|
|
|
|
URL: CAPIBaseURL,
|
|
|
|
}
|
|
|
|
apiConfigDump, err := yaml.Marshal(apiCfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to marshal api credentials: %s", err)
|
|
|
|
}
|
|
|
|
if dumpFile != "" {
|
|
|
|
err = ioutil.WriteFile(dumpFile, apiConfigDump, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("write api credentials in '%s' failed: %s", dumpFile, err)
|
|
|
|
}
|
2021-02-25 10:20:36 +00:00
|
|
|
log.Printf("Central API credentials dumped to '%s'", dumpFile)
|
2020-11-30 09:37:17 +00:00
|
|
|
} else {
|
|
|
|
fmt.Printf("%s\n", string(apiConfigDump))
|
|
|
|
}
|
|
|
|
|
2021-03-26 16:42:56 +00:00
|
|
|
log.Warningf(ReloadMessage())
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cmdCapiRegister.Flags().StringVarP(&outputFile, "file", "f", "", "output file destination")
|
2022-03-24 10:07:54 +00:00
|
|
|
cmdCapiRegister.Flags().StringVar(&capiUserPrefix, "schmilblick", "", "set a schmilblick (use in tests only)")
|
|
|
|
cmdCapiRegister.Flags().MarkHidden("schmilblick")
|
2020-11-30 09:37:17 +00:00
|
|
|
cmdCapi.AddCommand(cmdCapiRegister)
|
|
|
|
|
|
|
|
var cmdCapiStatus = &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "status",
|
|
|
|
Short: "Check status with the Central API (CAPI)",
|
|
|
|
Args: cobra.MinimumNArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2020-11-30 09:37:17 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
var err error
|
|
|
|
if csConfig.API.Server == nil {
|
2022-05-31 08:01:30 +00:00
|
|
|
log.Fatalln("There is no configuration on 'api.server:'")
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
if csConfig.API.Server.OnlineClient == nil {
|
2021-08-25 16:30:05 +00:00
|
|
|
log.Fatalf("Please provide credentials for the Central API (CAPI) in '%s'", csConfig.API.Server.OnlineClient.CredentialsFilePath)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if csConfig.API.Server.OnlineClient.Credentials == nil {
|
2021-08-25 16:30:05 +00:00
|
|
|
log.Fatalf("no credentials for Central API (CAPI) in '%s'", csConfig.API.Server.OnlineClient.CredentialsFilePath)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
password := strfmt.Password(csConfig.API.Server.OnlineClient.Credentials.Password)
|
|
|
|
apiurl, err := url.Parse(csConfig.API.Server.OnlineClient.Credentials.URL)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("parsing api url ('%s'): %s", csConfig.API.Server.OnlineClient.Credentials.URL, err)
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
|
|
|
|
if err := csConfig.LoadHub(); err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
|
2020-12-01 11:33:14 +00:00
|
|
|
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
|
2022-05-31 08:01:30 +00:00
|
|
|
log.Fatalf("Failed to load hub index : %s", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2022-04-13 15:48:29 +00:00
|
|
|
scenarios, err := cwhub.GetInstalledScenariosAsString()
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to get scenarios : %s", err.Error())
|
|
|
|
}
|
2021-02-18 08:59:28 +00:00
|
|
|
if len(scenarios) == 0 {
|
|
|
|
log.Fatalf("no scenarios installed, abort")
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
Client, err = apiclient.NewDefaultClient(apiurl, CAPIURLPrefix, fmt.Sprintf("crowdsec/%s", cwversion.VersionStr()), nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("init default client: %s", err)
|
|
|
|
}
|
|
|
|
t := models.WatcherAuthRequest{
|
|
|
|
MachineID: &csConfig.API.Server.OnlineClient.Credentials.Login,
|
|
|
|
Password: &password,
|
|
|
|
Scenarios: scenarios,
|
|
|
|
}
|
|
|
|
log.Infof("Loaded credentials from %s", csConfig.API.Server.OnlineClient.CredentialsFilePath)
|
|
|
|
log.Infof("Trying to authenticate with username %s on %s", csConfig.API.Server.OnlineClient.Credentials.Login, apiurl)
|
2022-01-19 13:56:05 +00:00
|
|
|
_, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to authenticate to Central API (CAPI) : %s", err)
|
|
|
|
}
|
2022-02-01 21:08:06 +00:00
|
|
|
log.Infof("You can successfully interact with Central API (CAPI)")
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cmdCapi.AddCommand(cmdCapiStatus)
|
2021-06-28 15:34:19 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return cmdCapi
|
|
|
|
}
|