config_backup.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. log "github.com/sirupsen/logrus"
  7. "github.com/spf13/cobra"
  8. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  9. )
  10. /* Backup crowdsec configurations to directory <dirPath> :
  11. - Main config (config.yaml)
  12. - Profiles config (profiles.yaml)
  13. - Simulation config (simulation.yaml)
  14. - Backup of API credentials (local API and online API)
  15. - List of scenarios, parsers, postoverflows and collections that are up-to-date
  16. - Tainted/local/out-of-date scenarios, parsers, postoverflows and collections
  17. */
  18. func backupConfigToDirectory(dirPath string) error {
  19. var err error
  20. if dirPath == "" {
  21. return fmt.Errorf("directory path can't be empty")
  22. }
  23. log.Infof("Starting configuration backup")
  24. /*if parent directory doesn't exist, bail out. create final dir with Mkdir*/
  25. parentDir := filepath.Dir(dirPath)
  26. if _, err := os.Stat(parentDir); err != nil {
  27. return fmt.Errorf("while checking parent directory %s existence: %w", parentDir, err)
  28. }
  29. if err = os.Mkdir(dirPath, 0o700); err != nil {
  30. return fmt.Errorf("while creating %s: %w", dirPath, err)
  31. }
  32. if csConfig.ConfigPaths.SimulationFilePath != "" {
  33. backupSimulation := filepath.Join(dirPath, "simulation.yaml")
  34. if err = CopyFile(csConfig.ConfigPaths.SimulationFilePath, backupSimulation); err != nil {
  35. return fmt.Errorf("failed copy %s to %s: %w", csConfig.ConfigPaths.SimulationFilePath, backupSimulation, err)
  36. }
  37. log.Infof("Saved simulation to %s", backupSimulation)
  38. }
  39. /*
  40. - backup AcquisitionFilePath
  41. - backup the other files of acquisition directory
  42. */
  43. if csConfig.Crowdsec != nil && csConfig.Crowdsec.AcquisitionFilePath != "" {
  44. backupAcquisition := filepath.Join(dirPath, "acquis.yaml")
  45. if err = CopyFile(csConfig.Crowdsec.AcquisitionFilePath, backupAcquisition); err != nil {
  46. return fmt.Errorf("failed copy %s to %s: %s", csConfig.Crowdsec.AcquisitionFilePath, backupAcquisition, err)
  47. }
  48. }
  49. acquisBackupDir := filepath.Join(dirPath, "acquis")
  50. if err = os.Mkdir(acquisBackupDir, 0o700); err != nil {
  51. return fmt.Errorf("error while creating %s: %s", acquisBackupDir, err)
  52. }
  53. if csConfig.Crowdsec != nil && len(csConfig.Crowdsec.AcquisitionFiles) > 0 {
  54. for _, acquisFile := range csConfig.Crowdsec.AcquisitionFiles {
  55. /*if it was the default one, it was already backup'ed*/
  56. if csConfig.Crowdsec.AcquisitionFilePath == acquisFile {
  57. continue
  58. }
  59. targetFname, err := filepath.Abs(filepath.Join(acquisBackupDir, filepath.Base(acquisFile)))
  60. if err != nil {
  61. return fmt.Errorf("while saving %s to %s: %w", acquisFile, acquisBackupDir, err)
  62. }
  63. if err = CopyFile(acquisFile, targetFname); err != nil {
  64. return fmt.Errorf("failed copy %s to %s: %w", acquisFile, targetFname, err)
  65. }
  66. log.Infof("Saved acquis %s to %s", acquisFile, targetFname)
  67. }
  68. }
  69. if ConfigFilePath != "" {
  70. backupMain := fmt.Sprintf("%s/config.yaml", dirPath)
  71. if err = CopyFile(ConfigFilePath, backupMain); err != nil {
  72. return fmt.Errorf("failed copy %s to %s: %s", ConfigFilePath, backupMain, err)
  73. }
  74. log.Infof("Saved default yaml to %s", backupMain)
  75. }
  76. if csConfig.API != nil && csConfig.API.Server != nil && csConfig.API.Server.OnlineClient != nil && csConfig.API.Server.OnlineClient.CredentialsFilePath != "" {
  77. backupCAPICreds := fmt.Sprintf("%s/online_api_credentials.yaml", dirPath)
  78. if err = CopyFile(csConfig.API.Server.OnlineClient.CredentialsFilePath, backupCAPICreds); err != nil {
  79. return fmt.Errorf("failed copy %s to %s: %s", csConfig.API.Server.OnlineClient.CredentialsFilePath, backupCAPICreds, err)
  80. }
  81. log.Infof("Saved online API credentials to %s", backupCAPICreds)
  82. }
  83. if csConfig.API != nil && csConfig.API.Client != nil && csConfig.API.Client.CredentialsFilePath != "" {
  84. backupLAPICreds := fmt.Sprintf("%s/local_api_credentials.yaml", dirPath)
  85. if err = CopyFile(csConfig.API.Client.CredentialsFilePath, backupLAPICreds); err != nil {
  86. return fmt.Errorf("failed copy %s to %s: %s", csConfig.API.Client.CredentialsFilePath, backupLAPICreds, err)
  87. }
  88. log.Infof("Saved local API credentials to %s", backupLAPICreds)
  89. }
  90. if csConfig.API != nil && csConfig.API.Server != nil && csConfig.API.Server.ProfilesPath != "" {
  91. backupProfiles := fmt.Sprintf("%s/profiles.yaml", dirPath)
  92. if err = CopyFile(csConfig.API.Server.ProfilesPath, backupProfiles); err != nil {
  93. return fmt.Errorf("failed copy %s to %s: %s", csConfig.API.Server.ProfilesPath, backupProfiles, err)
  94. }
  95. log.Infof("Saved profiles to %s", backupProfiles)
  96. }
  97. if err = BackupHub(dirPath); err != nil {
  98. return fmt.Errorf("failed to backup hub config: %s", err)
  99. }
  100. return nil
  101. }
  102. func runConfigBackup(cmd *cobra.Command, args []string) error {
  103. if err := csConfig.LoadHub(); err != nil {
  104. return err
  105. }
  106. if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
  107. log.Info("Run 'sudo cscli hub update' to get the hub index")
  108. return fmt.Errorf("failed to get Hub index: %w", err)
  109. }
  110. if err := backupConfigToDirectory(args[0]); err != nil {
  111. return fmt.Errorf("failed to backup config: %w", err)
  112. }
  113. return nil
  114. }
  115. func NewConfigBackupCmd() *cobra.Command {
  116. cmdConfigBackup := &cobra.Command{
  117. Use: `backup "directory"`,
  118. Short: "Backup current config",
  119. Long: `Backup the current crowdsec configuration including :
  120. - Main config (config.yaml)
  121. - Simulation config (simulation.yaml)
  122. - Profiles config (profiles.yaml)
  123. - List of scenarios, parsers, postoverflows and collections that are up-to-date
  124. - Tainted/local/out-of-date scenarios, parsers, postoverflows and collections
  125. - Backup of API credentials (local API and online API)`,
  126. Example: `cscli config backup ./my-backup`,
  127. Args: cobra.ExactArgs(1),
  128. DisableAutoGenTag: true,
  129. RunE: runConfigBackup,
  130. }
  131. return cmdConfigBackup
  132. }