2022-12-22 11:22:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-10-06 11:59:51 +00:00
|
|
|
"encoding/json"
|
2024-02-22 10:04:36 +00:00
|
|
|
"errors"
|
2022-12-22 11:22:55 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2023-10-04 08:42:47 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
2023-12-19 16:20:09 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
2022-12-22 11:22:55 +00:00
|
|
|
)
|
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
func (cli *cliConfig) backupHub(dirPath string) error {
|
|
|
|
hub, err := require.Hub(cli.cfg(), nil, nil)
|
2023-11-24 14:57:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-06 11:59:51 +00:00
|
|
|
for _, itemType := range cwhub.ItemTypes {
|
|
|
|
clog := log.WithFields(log.Fields{
|
|
|
|
"type": itemType,
|
|
|
|
})
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
itemMap := hub.GetItemMap(itemType)
|
2023-10-06 11:59:51 +00:00
|
|
|
if itemMap == nil {
|
|
|
|
clog.Infof("No %s to backup.", itemType)
|
|
|
|
continue
|
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
|
|
|
itemDirectory := fmt.Sprintf("%s/%s/", dirPath, itemType)
|
2023-11-24 14:57:32 +00:00
|
|
|
if err = os.MkdirAll(itemDirectory, os.ModePerm); err != nil {
|
2024-02-22 10:04:36 +00:00
|
|
|
return fmt.Errorf("error while creating %s: %w", itemDirectory, err)
|
2023-10-06 11:59:51 +00:00
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
|
|
|
upstreamParsers := []string{}
|
|
|
|
|
2023-10-06 11:59:51 +00:00
|
|
|
for k, v := range itemMap {
|
|
|
|
clog = clog.WithFields(log.Fields{
|
|
|
|
"file": v.Name,
|
|
|
|
})
|
2024-02-22 10:04:36 +00:00
|
|
|
if !v.State.Installed { // only backup installed ones
|
|
|
|
clog.Debugf("[%s]: not installed", k)
|
2023-10-06 11:59:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
// for the local/tainted ones, we back up the full file
|
2023-12-05 12:38:52 +00:00
|
|
|
if v.State.Tainted || v.State.IsLocal() || !v.State.UpToDate {
|
2024-02-22 10:04:36 +00:00
|
|
|
// we need to backup stages for parsers
|
2023-11-24 14:57:32 +00:00
|
|
|
if itemType == cwhub.PARSERS || itemType == cwhub.POSTOVERFLOWS {
|
2023-10-06 11:59:51 +00:00
|
|
|
fstagedir := fmt.Sprintf("%s%s", itemDirectory, v.Stage)
|
2023-11-24 14:57:32 +00:00
|
|
|
if err = os.MkdirAll(fstagedir, os.ModePerm); err != nil {
|
2024-02-22 10:04:36 +00:00
|
|
|
return fmt.Errorf("error while creating stage dir %s: %w", fstagedir, err)
|
2023-10-06 11:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-12-05 12:38:52 +00:00
|
|
|
clog.Debugf("[%s]: backing up file (tainted:%t local:%t up-to-date:%t)", k, v.State.Tainted, v.State.IsLocal(), v.State.UpToDate)
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-10-06 11:59:51 +00:00
|
|
|
tfile := fmt.Sprintf("%s%s/%s", itemDirectory, v.Stage, v.FileName)
|
2023-11-24 14:57:32 +00:00
|
|
|
if err = CopyFile(v.State.LocalPath, tfile); err != nil {
|
2024-02-22 10:04:36 +00:00
|
|
|
return fmt.Errorf("failed copy %s %s to %s: %w", itemType, v.State.LocalPath, tfile, err)
|
2023-10-06 11:59:51 +00:00
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
clog.Infof("local/tainted saved %s to %s", v.State.LocalPath, tfile)
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-10-06 11:59:51 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
clog.Debugf("[%s]: from hub, just backup name (up-to-date:%t)", k, v.State.UpToDate)
|
2023-11-24 14:57:32 +00:00
|
|
|
clog.Infof("saving, version:%s, up-to-date:%t", v.Version, v.State.UpToDate)
|
2023-10-06 11:59:51 +00:00
|
|
|
upstreamParsers = append(upstreamParsers, v.Name)
|
|
|
|
}
|
2024-02-22 10:04:36 +00:00
|
|
|
// write the upstream items
|
2023-10-06 11:59:51 +00:00
|
|
|
upstreamParsersFname := fmt.Sprintf("%s/upstream-%s.json", itemDirectory, itemType)
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-10-06 11:59:51 +00:00
|
|
|
upstreamParsersContent, err := json.MarshalIndent(upstreamParsers, "", " ")
|
|
|
|
if err != nil {
|
2024-02-22 10:04:36 +00:00
|
|
|
return fmt.Errorf("failed marshaling upstream parsers: %w", err)
|
2023-10-06 11:59:51 +00:00
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-12-08 09:51:15 +00:00
|
|
|
err = os.WriteFile(upstreamParsersFname, upstreamParsersContent, 0o644)
|
2023-10-06 11:59:51 +00:00
|
|
|
if err != nil {
|
2024-02-22 10:04:36 +00:00
|
|
|
return fmt.Errorf("unable to write to %s %s: %w", itemType, upstreamParsersFname, err)
|
2023-10-06 11:59:51 +00:00
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-10-06 11:59:51 +00:00
|
|
|
clog.Infof("Wrote %d entries for %s to %s", len(upstreamParsers), itemType, upstreamParsersFname)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-02 11:31:04 +00:00
|
|
|
/*
|
|
|
|
Backup crowdsec configurations to directory <dirPath>:
|
2022-12-22 11:22:55 +00:00
|
|
|
|
|
|
|
- Main config (config.yaml)
|
|
|
|
- Profiles config (profiles.yaml)
|
|
|
|
- Simulation config (simulation.yaml)
|
|
|
|
- Backup of API credentials (local API and online API)
|
|
|
|
- List of scenarios, parsers, postoverflows and collections that are up-to-date
|
|
|
|
- Tainted/local/out-of-date scenarios, parsers, postoverflows and collections
|
2023-10-02 11:31:04 +00:00
|
|
|
- Acquisition files (acquis.yaml, acquis.d/*.yaml)
|
2022-12-22 11:22:55 +00:00
|
|
|
*/
|
2024-02-22 10:04:36 +00:00
|
|
|
func (cli *cliConfig) backup(dirPath string) error {
|
2022-12-22 11:22:55 +00:00
|
|
|
var err error
|
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
cfg := cli.cfg()
|
|
|
|
|
2022-12-22 11:22:55 +00:00
|
|
|
if dirPath == "" {
|
2024-02-22 10:04:36 +00:00
|
|
|
return errors.New("directory path can't be empty")
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Starting configuration backup")
|
|
|
|
|
|
|
|
/*if parent directory doesn't exist, bail out. create final dir with Mkdir*/
|
|
|
|
parentDir := filepath.Dir(dirPath)
|
2023-11-24 14:57:32 +00:00
|
|
|
if _, err = os.Stat(parentDir); err != nil {
|
2023-06-22 13:01:34 +00:00
|
|
|
return fmt.Errorf("while checking parent directory %s existence: %w", parentDir, err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Mkdir(dirPath, 0o700); err != nil {
|
2023-06-22 13:01:34 +00:00
|
|
|
return fmt.Errorf("while creating %s: %w", dirPath, err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
if cfg.ConfigPaths.SimulationFilePath != "" {
|
2022-12-22 11:22:55 +00:00
|
|
|
backupSimulation := filepath.Join(dirPath, "simulation.yaml")
|
2024-02-22 10:04:36 +00:00
|
|
|
if err = CopyFile(cfg.ConfigPaths.SimulationFilePath, backupSimulation); err != nil {
|
|
|
|
return fmt.Errorf("failed copy %s to %s: %w", cfg.ConfigPaths.SimulationFilePath, backupSimulation, err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Saved simulation to %s", backupSimulation)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
- backup AcquisitionFilePath
|
|
|
|
- backup the other files of acquisition directory
|
|
|
|
*/
|
2024-02-22 10:04:36 +00:00
|
|
|
if cfg.Crowdsec != nil && cfg.Crowdsec.AcquisitionFilePath != "" {
|
2022-12-22 11:22:55 +00:00
|
|
|
backupAcquisition := filepath.Join(dirPath, "acquis.yaml")
|
2024-02-22 10:04:36 +00:00
|
|
|
if err = CopyFile(cfg.Crowdsec.AcquisitionFilePath, backupAcquisition); err != nil {
|
|
|
|
return fmt.Errorf("failed copy %s to %s: %w", cfg.Crowdsec.AcquisitionFilePath, backupAcquisition, err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acquisBackupDir := filepath.Join(dirPath, "acquis")
|
|
|
|
if err = os.Mkdir(acquisBackupDir, 0o700); err != nil {
|
2024-02-22 10:04:36 +00:00
|
|
|
return fmt.Errorf("error while creating %s: %w", acquisBackupDir, err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
if cfg.Crowdsec != nil && len(cfg.Crowdsec.AcquisitionFiles) > 0 {
|
|
|
|
for _, acquisFile := range cfg.Crowdsec.AcquisitionFiles {
|
2022-12-22 11:22:55 +00:00
|
|
|
/*if it was the default one, it was already backup'ed*/
|
2024-02-22 10:04:36 +00:00
|
|
|
if cfg.Crowdsec.AcquisitionFilePath == acquisFile {
|
2022-12-22 11:22:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
targetFname, err := filepath.Abs(filepath.Join(acquisBackupDir, filepath.Base(acquisFile)))
|
|
|
|
if err != nil {
|
2023-06-22 13:01:34 +00:00
|
|
|
return fmt.Errorf("while saving %s to %s: %w", acquisFile, acquisBackupDir, err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
2023-06-08 13:08:51 +00:00
|
|
|
if err = CopyFile(acquisFile, targetFname); err != nil {
|
2023-06-22 13:01:34 +00:00
|
|
|
return fmt.Errorf("failed copy %s to %s: %w", acquisFile, targetFname, err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Saved acquis %s to %s", acquisFile, targetFname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ConfigFilePath != "" {
|
|
|
|
backupMain := fmt.Sprintf("%s/config.yaml", dirPath)
|
2023-06-08 13:08:51 +00:00
|
|
|
if err = CopyFile(ConfigFilePath, backupMain); err != nil {
|
2024-02-22 10:04:36 +00:00
|
|
|
return fmt.Errorf("failed copy %s to %s: %w", ConfigFilePath, backupMain, err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Saved default yaml to %s", backupMain)
|
|
|
|
}
|
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
if cfg.API != nil && cfg.API.Server != nil && cfg.API.Server.OnlineClient != nil && cfg.API.Server.OnlineClient.CredentialsFilePath != "" {
|
2022-12-22 11:22:55 +00:00
|
|
|
backupCAPICreds := fmt.Sprintf("%s/online_api_credentials.yaml", dirPath)
|
2024-02-22 10:04:36 +00:00
|
|
|
if err = CopyFile(cfg.API.Server.OnlineClient.CredentialsFilePath, backupCAPICreds); err != nil {
|
|
|
|
return fmt.Errorf("failed copy %s to %s: %w", cfg.API.Server.OnlineClient.CredentialsFilePath, backupCAPICreds, err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Saved online API credentials to %s", backupCAPICreds)
|
|
|
|
}
|
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
if cfg.API != nil && cfg.API.Client != nil && cfg.API.Client.CredentialsFilePath != "" {
|
2022-12-22 11:22:55 +00:00
|
|
|
backupLAPICreds := fmt.Sprintf("%s/local_api_credentials.yaml", dirPath)
|
2024-02-22 10:04:36 +00:00
|
|
|
if err = CopyFile(cfg.API.Client.CredentialsFilePath, backupLAPICreds); err != nil {
|
|
|
|
return fmt.Errorf("failed copy %s to %s: %w", cfg.API.Client.CredentialsFilePath, backupLAPICreds, err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Saved local API credentials to %s", backupLAPICreds)
|
|
|
|
}
|
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
if cfg.API != nil && cfg.API.Server != nil && cfg.API.Server.ProfilesPath != "" {
|
2022-12-22 11:22:55 +00:00
|
|
|
backupProfiles := fmt.Sprintf("%s/profiles.yaml", dirPath)
|
2024-02-22 10:04:36 +00:00
|
|
|
if err = CopyFile(cfg.API.Server.ProfilesPath, backupProfiles); err != nil {
|
|
|
|
return fmt.Errorf("failed copy %s to %s: %w", cfg.API.Server.ProfilesPath, backupProfiles, err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Saved profiles to %s", backupProfiles)
|
|
|
|
}
|
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
if err = cli.backupHub(dirPath); err != nil {
|
|
|
|
return fmt.Errorf("failed to backup hub config: %w", err)
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
func (cli *cliConfig) newBackupCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2022-12-22 11:22:55 +00:00
|
|
|
Use: `backup "directory"`,
|
|
|
|
Short: "Backup current config",
|
|
|
|
Long: `Backup the current crowdsec configuration including :
|
|
|
|
|
|
|
|
- Main config (config.yaml)
|
|
|
|
- Simulation config (simulation.yaml)
|
|
|
|
- Profiles config (profiles.yaml)
|
|
|
|
- List of scenarios, parsers, postoverflows and collections that are up-to-date
|
|
|
|
- Tainted/local/out-of-date scenarios, parsers, postoverflows and collections
|
|
|
|
- Backup of API credentials (local API and online API)`,
|
|
|
|
Example: `cscli config backup ./my-backup`,
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
2024-02-22 10:04:36 +00:00
|
|
|
RunE: func(_ *cobra.Command, args []string) error {
|
|
|
|
if err := cli.backup(args[0]); err != nil {
|
|
|
|
return fmt.Errorf("failed to backup config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 10:04:36 +00:00
|
|
|
return cmd
|
2022-12-22 11:22:55 +00:00
|
|
|
}
|