config_restore.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. log "github.com/sirupsen/logrus"
  9. "github.com/spf13/cobra"
  10. "gopkg.in/yaml.v2"
  11. "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
  12. "github.com/crowdsecurity/crowdsec/pkg/csconfig"
  13. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  14. )
  15. type OldAPICfg struct {
  16. MachineID string `json:"machine_id"`
  17. Password string `json:"password"`
  18. }
  19. // it's a rip of the cli version, but in silent-mode
  20. func silentInstallItem(name string, obtype string) (string, error) {
  21. var item = cwhub.GetItem(obtype, name)
  22. if item == nil {
  23. return "", fmt.Errorf("error retrieving item")
  24. }
  25. err := cwhub.DownloadLatest(csConfig.Hub, item, false, false)
  26. if err != nil {
  27. return "", fmt.Errorf("error while downloading %s : %v", item.Name, err)
  28. }
  29. if err := cwhub.AddItem(obtype, *item); err != nil {
  30. return "", err
  31. }
  32. err = cwhub.EnableItem(csConfig.Hub, item)
  33. if err != nil {
  34. return "", fmt.Errorf("error while enabling %s : %v", item.Name, err)
  35. }
  36. if err := cwhub.AddItem(obtype, *item); err != nil {
  37. return "", err
  38. }
  39. return fmt.Sprintf("Enabled %s", item.Name), nil
  40. }
  41. func restoreHub(dirPath string) error {
  42. var err error
  43. cwhub.SetHubBranch()
  44. for _, itype := range cwhub.ItemTypes {
  45. itemDirectory := fmt.Sprintf("%s/%s/", dirPath, itype)
  46. if _, err = os.Stat(itemDirectory); err != nil {
  47. log.Infof("no %s in backup", itype)
  48. continue
  49. }
  50. /*restore the upstream items*/
  51. upstreamListFN := fmt.Sprintf("%s/upstream-%s.json", itemDirectory, itype)
  52. file, err := os.ReadFile(upstreamListFN)
  53. if err != nil {
  54. return fmt.Errorf("error while opening %s : %s", upstreamListFN, err)
  55. }
  56. var upstreamList []string
  57. err = json.Unmarshal(file, &upstreamList)
  58. if err != nil {
  59. return fmt.Errorf("error unmarshaling %s : %s", upstreamListFN, err)
  60. }
  61. for _, toinstall := range upstreamList {
  62. label, err := silentInstallItem(toinstall, itype)
  63. if err != nil {
  64. log.Errorf("Error while installing %s : %s", toinstall, err)
  65. } else if label != "" {
  66. log.Infof("Installed %s : %s", toinstall, label)
  67. } else {
  68. log.Printf("Installed %s : ok", toinstall)
  69. }
  70. }
  71. /*restore the local and tainted items*/
  72. files, err := os.ReadDir(itemDirectory)
  73. if err != nil {
  74. return fmt.Errorf("failed enumerating files of %s : %s", itemDirectory, err)
  75. }
  76. for _, file := range files {
  77. //this was the upstream data
  78. if file.Name() == fmt.Sprintf("upstream-%s.json", itype) {
  79. continue
  80. }
  81. if itype == cwhub.PARSERS || itype == cwhub.POSTOVERFLOWS {
  82. //we expect a stage here
  83. if !file.IsDir() {
  84. continue
  85. }
  86. stage := file.Name()
  87. stagedir := fmt.Sprintf("%s/%s/%s/", csConfig.ConfigPaths.ConfigDir, itype, stage)
  88. log.Debugf("Found stage %s in %s, target directory : %s", stage, itype, stagedir)
  89. if err = os.MkdirAll(stagedir, os.ModePerm); err != nil {
  90. return fmt.Errorf("error while creating stage directory %s : %s", stagedir, err)
  91. }
  92. /*find items*/
  93. ifiles, err := os.ReadDir(itemDirectory + "/" + stage + "/")
  94. if err != nil {
  95. return fmt.Errorf("failed enumerating files of %s : %s", itemDirectory+"/"+stage, err)
  96. }
  97. //finally copy item
  98. for _, tfile := range ifiles {
  99. log.Infof("Going to restore local/tainted [%s]", tfile.Name())
  100. sourceFile := fmt.Sprintf("%s/%s/%s", itemDirectory, stage, tfile.Name())
  101. destinationFile := fmt.Sprintf("%s%s", stagedir, tfile.Name())
  102. if err = CopyFile(sourceFile, destinationFile); err != nil {
  103. return fmt.Errorf("failed copy %s %s to %s : %s", itype, sourceFile, destinationFile, err)
  104. }
  105. log.Infof("restored %s to %s", sourceFile, destinationFile)
  106. }
  107. } else {
  108. log.Infof("Going to restore local/tainted [%s]", file.Name())
  109. sourceFile := fmt.Sprintf("%s/%s", itemDirectory, file.Name())
  110. destinationFile := fmt.Sprintf("%s/%s/%s", csConfig.ConfigPaths.ConfigDir, itype, file.Name())
  111. if err = CopyFile(sourceFile, destinationFile); err != nil {
  112. return fmt.Errorf("failed copy %s %s to %s : %s", itype, sourceFile, destinationFile, err)
  113. }
  114. log.Infof("restored %s to %s", sourceFile, destinationFile)
  115. }
  116. }
  117. }
  118. return nil
  119. }
  120. /*
  121. Restore crowdsec configurations to directory <dirPath>:
  122. - Main config (config.yaml)
  123. - Profiles config (profiles.yaml)
  124. - Simulation config (simulation.yaml)
  125. - Backup of API credentials (local API and online API)
  126. - List of scenarios, parsers, postoverflows and collections that are up-to-date
  127. - Tainted/local/out-of-date scenarios, parsers, postoverflows and collections
  128. - Acquisition files (acquis.yaml, acquis.d/*.yaml)
  129. */
  130. func restoreConfigFromDirectory(dirPath string, oldBackup bool) error {
  131. var err error
  132. if !oldBackup {
  133. backupMain := fmt.Sprintf("%s/config.yaml", dirPath)
  134. if _, err = os.Stat(backupMain); err == nil {
  135. if csConfig.ConfigPaths != nil && csConfig.ConfigPaths.ConfigDir != "" {
  136. if err = CopyFile(backupMain, fmt.Sprintf("%s/config.yaml", csConfig.ConfigPaths.ConfigDir)); err != nil {
  137. return fmt.Errorf("failed copy %s to %s : %s", backupMain, csConfig.ConfigPaths.ConfigDir, err)
  138. }
  139. }
  140. }
  141. // Now we have config.yaml, we should regenerate config struct to have rights paths etc
  142. ConfigFilePath = fmt.Sprintf("%s/config.yaml", csConfig.ConfigPaths.ConfigDir)
  143. initConfig()
  144. backupCAPICreds := fmt.Sprintf("%s/online_api_credentials.yaml", dirPath)
  145. if _, err = os.Stat(backupCAPICreds); err == nil {
  146. if err = CopyFile(backupCAPICreds, csConfig.API.Server.OnlineClient.CredentialsFilePath); err != nil {
  147. return fmt.Errorf("failed copy %s to %s : %s", backupCAPICreds, csConfig.API.Server.OnlineClient.CredentialsFilePath, err)
  148. }
  149. }
  150. backupLAPICreds := fmt.Sprintf("%s/local_api_credentials.yaml", dirPath)
  151. if _, err = os.Stat(backupLAPICreds); err == nil {
  152. if err = CopyFile(backupLAPICreds, csConfig.API.Client.CredentialsFilePath); err != nil {
  153. return fmt.Errorf("failed copy %s to %s : %s", backupLAPICreds, csConfig.API.Client.CredentialsFilePath, err)
  154. }
  155. }
  156. backupProfiles := fmt.Sprintf("%s/profiles.yaml", dirPath)
  157. if _, err = os.Stat(backupProfiles); err == nil {
  158. if err = CopyFile(backupProfiles, csConfig.API.Server.ProfilesPath); err != nil {
  159. return fmt.Errorf("failed copy %s to %s : %s", backupProfiles, csConfig.API.Server.ProfilesPath, err)
  160. }
  161. }
  162. } else {
  163. var oldAPICfg OldAPICfg
  164. backupOldAPICfg := fmt.Sprintf("%s/api_creds.json", dirPath)
  165. jsonFile, err := os.Open(backupOldAPICfg)
  166. if err != nil {
  167. log.Warningf("failed to open %s : %s", backupOldAPICfg, err)
  168. } else {
  169. byteValue, _ := io.ReadAll(jsonFile)
  170. err = json.Unmarshal(byteValue, &oldAPICfg)
  171. if err != nil {
  172. return fmt.Errorf("failed to load json file %s : %s", backupOldAPICfg, err)
  173. }
  174. apiCfg := csconfig.ApiCredentialsCfg{
  175. Login: oldAPICfg.MachineID,
  176. Password: oldAPICfg.Password,
  177. URL: CAPIBaseURL,
  178. }
  179. apiConfigDump, err := yaml.Marshal(apiCfg)
  180. if err != nil {
  181. return fmt.Errorf("unable to dump api credentials: %s", err)
  182. }
  183. apiConfigDumpFile := fmt.Sprintf("%s/online_api_credentials.yaml", csConfig.ConfigPaths.ConfigDir)
  184. if csConfig.API.Server.OnlineClient != nil && csConfig.API.Server.OnlineClient.CredentialsFilePath != "" {
  185. apiConfigDumpFile = csConfig.API.Server.OnlineClient.CredentialsFilePath
  186. }
  187. err = os.WriteFile(apiConfigDumpFile, apiConfigDump, 0o644)
  188. if err != nil {
  189. return fmt.Errorf("write api credentials in '%s' failed: %s", apiConfigDumpFile, err)
  190. }
  191. log.Infof("Saved API credentials to %s", apiConfigDumpFile)
  192. }
  193. }
  194. backupSimulation := fmt.Sprintf("%s/simulation.yaml", dirPath)
  195. if _, err = os.Stat(backupSimulation); err == nil {
  196. if err = CopyFile(backupSimulation, csConfig.ConfigPaths.SimulationFilePath); err != nil {
  197. return fmt.Errorf("failed copy %s to %s : %s", backupSimulation, csConfig.ConfigPaths.SimulationFilePath, err)
  198. }
  199. }
  200. /*if there is a acquisition dir, restore its content*/
  201. if csConfig.Crowdsec.AcquisitionDirPath != "" {
  202. if err = os.MkdirAll(csConfig.Crowdsec.AcquisitionDirPath, 0o700); err != nil {
  203. return fmt.Errorf("error while creating %s : %s", csConfig.Crowdsec.AcquisitionDirPath, err)
  204. }
  205. }
  206. // if there was a single one
  207. backupAcquisition := fmt.Sprintf("%s/acquis.yaml", dirPath)
  208. if _, err = os.Stat(backupAcquisition); err == nil {
  209. log.Debugf("restoring backup'ed %s", backupAcquisition)
  210. if err = CopyFile(backupAcquisition, csConfig.Crowdsec.AcquisitionFilePath); err != nil {
  211. return fmt.Errorf("failed copy %s to %s : %s", backupAcquisition, csConfig.Crowdsec.AcquisitionFilePath, err)
  212. }
  213. }
  214. // if there is files in the acquis backup dir, restore them
  215. acquisBackupDir := filepath.Join(dirPath, "acquis", "*.yaml")
  216. if acquisFiles, err := filepath.Glob(acquisBackupDir); err == nil {
  217. for _, acquisFile := range acquisFiles {
  218. targetFname, err := filepath.Abs(csConfig.Crowdsec.AcquisitionDirPath + "/" + filepath.Base(acquisFile))
  219. if err != nil {
  220. return fmt.Errorf("while saving %s to %s: %w", acquisFile, targetFname, err)
  221. }
  222. log.Debugf("restoring %s to %s", acquisFile, targetFname)
  223. if err = CopyFile(acquisFile, targetFname); err != nil {
  224. return fmt.Errorf("failed copy %s to %s : %s", acquisFile, targetFname, err)
  225. }
  226. }
  227. }
  228. if csConfig.Crowdsec != nil && len(csConfig.Crowdsec.AcquisitionFiles) > 0 {
  229. for _, acquisFile := range csConfig.Crowdsec.AcquisitionFiles {
  230. log.Infof("backup filepath from dir -> %s", acquisFile)
  231. // if it was the default one, it has already been backed up
  232. if csConfig.Crowdsec.AcquisitionFilePath == acquisFile {
  233. log.Infof("skip this one")
  234. continue
  235. }
  236. targetFname, err := filepath.Abs(filepath.Join(acquisBackupDir, filepath.Base(acquisFile)))
  237. if err != nil {
  238. return fmt.Errorf("while saving %s to %s: %w", acquisFile, acquisBackupDir, err)
  239. }
  240. if err = CopyFile(acquisFile, targetFname); err != nil {
  241. return fmt.Errorf("failed copy %s to %s : %s", acquisFile, targetFname, err)
  242. }
  243. log.Infof("Saved acquis %s to %s", acquisFile, targetFname)
  244. }
  245. }
  246. if err = restoreHub(dirPath); err != nil {
  247. return fmt.Errorf("failed to restore hub config : %s", err)
  248. }
  249. return nil
  250. }
  251. func runConfigRestore(cmd *cobra.Command, args []string) error {
  252. flags := cmd.Flags()
  253. oldBackup, err := flags.GetBool("old-backup")
  254. if err != nil {
  255. return err
  256. }
  257. if err := require.Hub(csConfig); err != nil {
  258. return err
  259. }
  260. if err := restoreConfigFromDirectory(args[0], oldBackup); err != nil {
  261. return fmt.Errorf("failed to restore config from %s: %w", args[0], err)
  262. }
  263. return nil
  264. }
  265. func NewConfigRestoreCmd() *cobra.Command {
  266. cmdConfigRestore := &cobra.Command{
  267. Use: `restore "directory"`,
  268. Short: `Restore config in backup "directory"`,
  269. Long: `Restore the crowdsec configuration from specified backup "directory" including:
  270. - Main config (config.yaml)
  271. - Simulation config (simulation.yaml)
  272. - Profiles config (profiles.yaml)
  273. - List of scenarios, parsers, postoverflows and collections that are up-to-date
  274. - Tainted/local/out-of-date scenarios, parsers, postoverflows and collections
  275. - Backup of API credentials (local API and online API)`,
  276. Args: cobra.ExactArgs(1),
  277. DisableAutoGenTag: true,
  278. RunE: runConfigRestore,
  279. }
  280. flags := cmdConfigRestore.Flags()
  281. 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")
  282. return cmdConfigRestore
  283. }