|
@@ -3,25 +3,17 @@ package main
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
- "io"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
"github.com/spf13/cobra"
|
|
|
- "gopkg.in/yaml.v2"
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
|
|
- "github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
|
)
|
|
|
|
|
|
-type OldAPICfg struct {
|
|
|
- MachineID string `json:"machine_id"`
|
|
|
- Password string `json:"password"`
|
|
|
-}
|
|
|
-
|
|
|
-func restoreHub(dirPath string) error {
|
|
|
+func (cli *cliConfig) restoreHub(dirPath string) error {
|
|
|
hub, err := require.Hub(csConfig, require.RemoteHub(csConfig), nil)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -38,14 +30,14 @@ func restoreHub(dirPath string) error {
|
|
|
|
|
|
file, err := os.ReadFile(upstreamListFN)
|
|
|
if err != nil {
|
|
|
- return fmt.Errorf("error while opening %s : %s", upstreamListFN, err)
|
|
|
+ return fmt.Errorf("error while opening %s: %w", upstreamListFN, err)
|
|
|
}
|
|
|
|
|
|
var upstreamList []string
|
|
|
|
|
|
err = json.Unmarshal(file, &upstreamList)
|
|
|
if err != nil {
|
|
|
- return fmt.Errorf("error unmarshaling %s : %s", upstreamListFN, err)
|
|
|
+ return fmt.Errorf("error unmarshaling %s: %w", upstreamListFN, err)
|
|
|
}
|
|
|
|
|
|
for _, toinstall := range upstreamList {
|
|
@@ -55,8 +47,7 @@ func restoreHub(dirPath string) error {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- err := item.Install(false, false)
|
|
|
- if err != nil {
|
|
|
+ if err = item.Install(false, false); err != nil {
|
|
|
log.Errorf("Error while installing %s : %s", toinstall, err)
|
|
|
}
|
|
|
}
|
|
@@ -64,17 +55,17 @@ func restoreHub(dirPath string) error {
|
|
|
/*restore the local and tainted items*/
|
|
|
files, err := os.ReadDir(itemDirectory)
|
|
|
if err != nil {
|
|
|
- return fmt.Errorf("failed enumerating files of %s : %s", itemDirectory, err)
|
|
|
+ return fmt.Errorf("failed enumerating files of %s: %w", itemDirectory, err)
|
|
|
}
|
|
|
|
|
|
for _, file := range files {
|
|
|
- //this was the upstream data
|
|
|
+ // this was the upstream data
|
|
|
if file.Name() == fmt.Sprintf("upstream-%s.json", itype) {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
if itype == cwhub.PARSERS || itype == cwhub.POSTOVERFLOWS {
|
|
|
- //we expect a stage here
|
|
|
+ // we expect a stage here
|
|
|
if !file.IsDir() {
|
|
|
continue
|
|
|
}
|
|
@@ -84,22 +75,23 @@ func restoreHub(dirPath string) error {
|
|
|
log.Debugf("Found stage %s in %s, target directory : %s", stage, itype, stagedir)
|
|
|
|
|
|
if err = os.MkdirAll(stagedir, os.ModePerm); err != nil {
|
|
|
- return fmt.Errorf("error while creating stage directory %s : %s", stagedir, err)
|
|
|
+ return fmt.Errorf("error while creating stage directory %s: %w", stagedir, err)
|
|
|
}
|
|
|
|
|
|
// find items
|
|
|
ifiles, err := os.ReadDir(itemDirectory + "/" + stage + "/")
|
|
|
if err != nil {
|
|
|
- return fmt.Errorf("failed enumerating files of %s : %s", itemDirectory+"/"+stage, err)
|
|
|
+ return fmt.Errorf("failed enumerating files of %s: %w", itemDirectory+"/"+stage, err)
|
|
|
}
|
|
|
- //finally copy item
|
|
|
+
|
|
|
+ // finally copy item
|
|
|
for _, tfile := range ifiles {
|
|
|
log.Infof("Going to restore local/tainted [%s]", tfile.Name())
|
|
|
sourceFile := fmt.Sprintf("%s/%s/%s", itemDirectory, stage, tfile.Name())
|
|
|
|
|
|
destinationFile := fmt.Sprintf("%s%s", stagedir, tfile.Name())
|
|
|
if err = CopyFile(sourceFile, destinationFile); err != nil {
|
|
|
- return fmt.Errorf("failed copy %s %s to %s : %s", itype, sourceFile, destinationFile, err)
|
|
|
+ return fmt.Errorf("failed copy %s %s to %s: %w", itype, sourceFile, destinationFile, err)
|
|
|
}
|
|
|
|
|
|
log.Infof("restored %s to %s", sourceFile, destinationFile)
|
|
@@ -108,9 +100,11 @@ func restoreHub(dirPath string) error {
|
|
|
log.Infof("Going to restore local/tainted [%s]", file.Name())
|
|
|
sourceFile := fmt.Sprintf("%s/%s", itemDirectory, file.Name())
|
|
|
destinationFile := fmt.Sprintf("%s/%s/%s", csConfig.ConfigPaths.ConfigDir, itype, file.Name())
|
|
|
+
|
|
|
if err = CopyFile(sourceFile, destinationFile); err != nil {
|
|
|
- return fmt.Errorf("failed copy %s %s to %s : %s", itype, sourceFile, destinationFile, err)
|
|
|
+ return fmt.Errorf("failed copy %s %s to %s: %w", itype, sourceFile, destinationFile, err)
|
|
|
}
|
|
|
+
|
|
|
log.Infof("restored %s to %s", sourceFile, destinationFile)
|
|
|
}
|
|
|
}
|
|
@@ -130,95 +124,60 @@ func restoreHub(dirPath string) error {
|
|
|
- Tainted/local/out-of-date scenarios, parsers, postoverflows and collections
|
|
|
- Acquisition files (acquis.yaml, acquis.d/*.yaml)
|
|
|
*/
|
|
|
-func restoreConfigFromDirectory(dirPath string, oldBackup bool) error {
|
|
|
+func (cli *cliConfig) restore(dirPath string) error {
|
|
|
var err error
|
|
|
|
|
|
- if !oldBackup {
|
|
|
- backupMain := fmt.Sprintf("%s/config.yaml", dirPath)
|
|
|
- if _, err = os.Stat(backupMain); err == nil {
|
|
|
- if csConfig.ConfigPaths != nil && csConfig.ConfigPaths.ConfigDir != "" {
|
|
|
- if err = CopyFile(backupMain, fmt.Sprintf("%s/config.yaml", csConfig.ConfigPaths.ConfigDir)); err != nil {
|
|
|
- return fmt.Errorf("failed copy %s to %s : %s", backupMain, csConfig.ConfigPaths.ConfigDir, err)
|
|
|
- }
|
|
|
+ backupMain := fmt.Sprintf("%s/config.yaml", dirPath)
|
|
|
+ if _, err = os.Stat(backupMain); err == nil {
|
|
|
+ if csConfig.ConfigPaths != nil && csConfig.ConfigPaths.ConfigDir != "" {
|
|
|
+ if err = CopyFile(backupMain, fmt.Sprintf("%s/config.yaml", csConfig.ConfigPaths.ConfigDir)); err != nil {
|
|
|
+ return fmt.Errorf("failed copy %s to %s: %w", backupMain, csConfig.ConfigPaths.ConfigDir, err)
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // Now we have config.yaml, we should regenerate config struct to have rights paths etc
|
|
|
- ConfigFilePath = fmt.Sprintf("%s/config.yaml", csConfig.ConfigPaths.ConfigDir)
|
|
|
-
|
|
|
- log.Debug("Reloading configuration")
|
|
|
+ // Now we have config.yaml, we should regenerate config struct to have rights paths etc
|
|
|
+ ConfigFilePath = fmt.Sprintf("%s/config.yaml", csConfig.ConfigPaths.ConfigDir)
|
|
|
|
|
|
- csConfig, _, err = loadConfigFor("config")
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("failed to reload configuration: %s", err)
|
|
|
- }
|
|
|
+ log.Debug("Reloading configuration")
|
|
|
|
|
|
- backupCAPICreds := fmt.Sprintf("%s/online_api_credentials.yaml", dirPath)
|
|
|
- if _, err = os.Stat(backupCAPICreds); err == nil {
|
|
|
- if err = CopyFile(backupCAPICreds, csConfig.API.Server.OnlineClient.CredentialsFilePath); err != nil {
|
|
|
- return fmt.Errorf("failed copy %s to %s : %s", backupCAPICreds, csConfig.API.Server.OnlineClient.CredentialsFilePath, err)
|
|
|
- }
|
|
|
- }
|
|
|
+ csConfig, _, err = loadConfigFor("config")
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("failed to reload configuration: %w", err)
|
|
|
+ }
|
|
|
|
|
|
- backupLAPICreds := fmt.Sprintf("%s/local_api_credentials.yaml", dirPath)
|
|
|
- if _, err = os.Stat(backupLAPICreds); err == nil {
|
|
|
- if err = CopyFile(backupLAPICreds, csConfig.API.Client.CredentialsFilePath); err != nil {
|
|
|
- return fmt.Errorf("failed copy %s to %s : %s", backupLAPICreds, csConfig.API.Client.CredentialsFilePath, err)
|
|
|
- }
|
|
|
+ backupCAPICreds := fmt.Sprintf("%s/online_api_credentials.yaml", dirPath)
|
|
|
+ if _, err = os.Stat(backupCAPICreds); err == nil {
|
|
|
+ if err = CopyFile(backupCAPICreds, csConfig.API.Server.OnlineClient.CredentialsFilePath); err != nil {
|
|
|
+ return fmt.Errorf("failed copy %s to %s: %w", backupCAPICreds, csConfig.API.Server.OnlineClient.CredentialsFilePath, err)
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- backupProfiles := fmt.Sprintf("%s/profiles.yaml", dirPath)
|
|
|
- if _, err = os.Stat(backupProfiles); err == nil {
|
|
|
- if err = CopyFile(backupProfiles, csConfig.API.Server.ProfilesPath); err != nil {
|
|
|
- return fmt.Errorf("failed copy %s to %s : %s", backupProfiles, csConfig.API.Server.ProfilesPath, err)
|
|
|
- }
|
|
|
+ backupLAPICreds := fmt.Sprintf("%s/local_api_credentials.yaml", dirPath)
|
|
|
+ if _, err = os.Stat(backupLAPICreds); err == nil {
|
|
|
+ if err = CopyFile(backupLAPICreds, csConfig.API.Client.CredentialsFilePath); err != nil {
|
|
|
+ return fmt.Errorf("failed copy %s to %s: %w", backupLAPICreds, csConfig.API.Client.CredentialsFilePath, err)
|
|
|
}
|
|
|
- } else {
|
|
|
- var oldAPICfg OldAPICfg
|
|
|
- backupOldAPICfg := fmt.Sprintf("%s/api_creds.json", dirPath)
|
|
|
-
|
|
|
- jsonFile, err := os.Open(backupOldAPICfg)
|
|
|
- if err != nil {
|
|
|
- log.Warningf("failed to open %s : %s", backupOldAPICfg, err)
|
|
|
- } else {
|
|
|
- byteValue, _ := io.ReadAll(jsonFile)
|
|
|
- err = json.Unmarshal(byteValue, &oldAPICfg)
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("failed to load json file %s : %s", backupOldAPICfg, err)
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- apiCfg := csconfig.ApiCredentialsCfg{
|
|
|
- Login: oldAPICfg.MachineID,
|
|
|
- Password: oldAPICfg.Password,
|
|
|
- URL: CAPIBaseURL,
|
|
|
- }
|
|
|
- apiConfigDump, err := yaml.Marshal(apiCfg)
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("unable to dump api credentials: %s", err)
|
|
|
- }
|
|
|
- apiConfigDumpFile := fmt.Sprintf("%s/online_api_credentials.yaml", csConfig.ConfigPaths.ConfigDir)
|
|
|
- if csConfig.API.Server.OnlineClient != nil && csConfig.API.Server.OnlineClient.CredentialsFilePath != "" {
|
|
|
- apiConfigDumpFile = csConfig.API.Server.OnlineClient.CredentialsFilePath
|
|
|
- }
|
|
|
- err = os.WriteFile(apiConfigDumpFile, apiConfigDump, 0o600)
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("write api credentials in '%s' failed: %s", apiConfigDumpFile, err)
|
|
|
- }
|
|
|
- log.Infof("Saved API credentials to %s", apiConfigDumpFile)
|
|
|
+ backupProfiles := fmt.Sprintf("%s/profiles.yaml", dirPath)
|
|
|
+ if _, err = os.Stat(backupProfiles); err == nil {
|
|
|
+ if err = CopyFile(backupProfiles, csConfig.API.Server.ProfilesPath); err != nil {
|
|
|
+ return fmt.Errorf("failed copy %s to %s: %w", backupProfiles, csConfig.API.Server.ProfilesPath, err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
backupSimulation := fmt.Sprintf("%s/simulation.yaml", dirPath)
|
|
|
if _, err = os.Stat(backupSimulation); err == nil {
|
|
|
if err = CopyFile(backupSimulation, csConfig.ConfigPaths.SimulationFilePath); err != nil {
|
|
|
- return fmt.Errorf("failed copy %s to %s : %s", backupSimulation, csConfig.ConfigPaths.SimulationFilePath, err)
|
|
|
+ return fmt.Errorf("failed copy %s to %s: %w", backupSimulation, csConfig.ConfigPaths.SimulationFilePath, err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*if there is a acquisition dir, restore its content*/
|
|
|
if csConfig.Crowdsec.AcquisitionDirPath != "" {
|
|
|
if err = os.MkdirAll(csConfig.Crowdsec.AcquisitionDirPath, 0o700); err != nil {
|
|
|
- return fmt.Errorf("error while creating %s : %s", csConfig.Crowdsec.AcquisitionDirPath, err)
|
|
|
+ return fmt.Errorf("error while creating %s: %w", csConfig.Crowdsec.AcquisitionDirPath, err)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -228,7 +187,7 @@ func restoreConfigFromDirectory(dirPath string, oldBackup bool) error {
|
|
|
log.Debugf("restoring backup'ed %s", backupAcquisition)
|
|
|
|
|
|
if err = CopyFile(backupAcquisition, csConfig.Crowdsec.AcquisitionFilePath); err != nil {
|
|
|
- return fmt.Errorf("failed copy %s to %s : %s", backupAcquisition, csConfig.Crowdsec.AcquisitionFilePath, err)
|
|
|
+ return fmt.Errorf("failed copy %s to %s: %w", backupAcquisition, csConfig.Crowdsec.AcquisitionFilePath, err)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -244,7 +203,7 @@ func restoreConfigFromDirectory(dirPath string, oldBackup bool) error {
|
|
|
log.Debugf("restoring %s to %s", acquisFile, targetFname)
|
|
|
|
|
|
if err = CopyFile(acquisFile, targetFname); err != nil {
|
|
|
- return fmt.Errorf("failed copy %s to %s : %s", acquisFile, targetFname, err)
|
|
|
+ return fmt.Errorf("failed copy %s to %s: %w", acquisFile, targetFname, err)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -265,37 +224,22 @@ func restoreConfigFromDirectory(dirPath string, oldBackup bool) error {
|
|
|
}
|
|
|
|
|
|
if err = CopyFile(acquisFile, targetFname); err != nil {
|
|
|
- return fmt.Errorf("failed copy %s to %s : %s", acquisFile, targetFname, err)
|
|
|
+ return fmt.Errorf("failed copy %s to %s: %w", acquisFile, targetFname, err)
|
|
|
}
|
|
|
|
|
|
log.Infof("Saved acquis %s to %s", acquisFile, targetFname)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if err = restoreHub(dirPath); err != nil {
|
|
|
- return fmt.Errorf("failed to restore hub config : %s", err)
|
|
|
+ if err = cli.restoreHub(dirPath); err != nil {
|
|
|
+ return fmt.Errorf("failed to restore hub config: %w", err)
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func runConfigRestore(cmd *cobra.Command, args []string) error {
|
|
|
- flags := cmd.Flags()
|
|
|
-
|
|
|
- oldBackup, err := flags.GetBool("old-backup")
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- if err := restoreConfigFromDirectory(args[0], oldBackup); err != nil {
|
|
|
- return fmt.Errorf("failed to restore config from %s: %w", args[0], err)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func NewConfigRestoreCmd() *cobra.Command {
|
|
|
- cmdConfigRestore := &cobra.Command{
|
|
|
+func (cli *cliConfig) newRestoreCmd() *cobra.Command {
|
|
|
+ cmd := &cobra.Command{
|
|
|
Use: `restore "directory"`,
|
|
|
Short: `Restore config in backup "directory"`,
|
|
|
Long: `Restore the crowdsec configuration from specified backup "directory" including:
|
|
@@ -308,11 +252,16 @@ func NewConfigRestoreCmd() *cobra.Command {
|
|
|
- Backup of API credentials (local API and online API)`,
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
DisableAutoGenTag: true,
|
|
|
- RunE: runConfigRestore,
|
|
|
- }
|
|
|
+ RunE: func(_ *cobra.Command, args []string) error {
|
|
|
+ dirPath := args[0]
|
|
|
|
|
|
- flags := cmdConfigRestore.Flags()
|
|
|
- flags.BoolP("old-backup", "", false, "To use when you are upgrading crowdsec v0.X to v1.X and you need to restore backup from v0.X")
|
|
|
+ if err := cli.restore(dirPath); err != nil {
|
|
|
+ return fmt.Errorf("failed to restore config from %s: %w", dirPath, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ },
|
|
|
+ }
|
|
|
|
|
|
- return cmdConfigRestore
|
|
|
+ return cmd
|
|
|
}
|