config_restore.go 11 KB

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