config_backup.go 5.7 KB

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