2020-11-30 09:37:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2022-09-06 11:55:03 +00:00
|
|
|
"os"
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2022-10-13 10:28:24 +00:00
|
|
|
"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"
|
2023-01-31 13:47:44 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
"github.com/go-openapi/strfmt"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/yaml.v2"
|
2020-11-30 09:37:17 +00:00
|
|
|
)
|
|
|
|
|
2023-01-06 22:14:02 +00:00
|
|
|
const CAPIBaseURL string = "https://api.crowdsec.net/"
|
2023-02-06 13:06:14 +00:00
|
|
|
const CAPIURLPrefix = "v3"
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-06 22:14:02 +00:00
|
|
|
cmdCapi.AddCommand(NewCapiRegisterCmd())
|
|
|
|
cmdCapi.AddCommand(NewCapiStatusCmd())
|
|
|
|
|
|
|
|
return cmdCapi
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCapiRegisterCmd() *cobra.Command {
|
|
|
|
var capiUserPrefix string
|
|
|
|
var outputFile string
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
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))
|
2023-01-31 13:47:44 +00:00
|
|
|
apiurl, err := url.Parse(types.CAPIBaseURL)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:47:44 +00:00
|
|
|
log.Fatalf("unable to parse api url %s : %s", types.CAPIBaseURL, err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
_, 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 {
|
2023-01-31 13:47:44 +00:00
|
|
|
log.Fatalf("api client register ('%s'): %s", types.CAPIBaseURL, err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
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(),
|
2023-01-31 13:47:44 +00:00
|
|
|
URL: types.CAPIBaseURL,
|
|
|
|
PapiURL: types.PAPIBaseURL,
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
apiConfigDump, err := yaml.Marshal(apiCfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to marshal api credentials: %s", err)
|
|
|
|
}
|
|
|
|
if dumpFile != "" {
|
2022-09-06 11:55:03 +00:00
|
|
|
err = os.WriteFile(dumpFile, apiConfigDump, 0600)
|
2020-11-30 09:37:17 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2022-06-22 07:38:23 +00:00
|
|
|
log.Warning(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)")
|
2022-06-06 13:24:48 +00:00
|
|
|
if err := cmdCapiRegister.Flags().MarkHidden("schmilblick"); err != nil {
|
|
|
|
log.Fatalf("failed to hide flag: %s", err)
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-01-06 22:14:02 +00:00
|
|
|
return cmdCapiRegister
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCapiStatusCmd() *cobra.Command {
|
2020-11-30 09:37:17 +00:00
|
|
|
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 {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Info("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 {
|
2022-06-22 13:53:53 +00:00
|
|
|
log.Fatalf("failed to get scenarios : %s", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
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
|
|
|
},
|
|
|
|
}
|
2021-06-28 15:34:19 +00:00
|
|
|
|
2023-01-06 22:14:02 +00:00
|
|
|
return cmdCapiStatus
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|