postoverflow.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package hubtest
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  8. )
  9. func (t *HubTestItem) installPostoverflowItem(hubPostOverflow *cwhub.Item) error {
  10. postoverflowSource, err := filepath.Abs(filepath.Join(t.HubPath, hubPostOverflow.RemotePath))
  11. if err != nil {
  12. return fmt.Errorf("can't get absolute path of '%s': %s", postoverflowSource, err)
  13. }
  14. postoverflowFileName := filepath.Base(postoverflowSource)
  15. // runtime/hub/postoverflows/s00-enrich/crowdsecurity/
  16. hubDirPostoverflowDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(hubPostOverflow.RemotePath))
  17. // runtime/postoverflows/s00-enrich
  18. postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, hubPostOverflow.Stage)
  19. if err := os.MkdirAll(hubDirPostoverflowDest, os.ModePerm); err != nil {
  20. return fmt.Errorf("unable to create folder '%s': %s", hubDirPostoverflowDest, err)
  21. }
  22. if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
  23. return fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
  24. }
  25. // runtime/hub/postoverflows/s00-enrich/crowdsecurity/rdns.yaml
  26. hubDirPostoverflowPath := filepath.Join(hubDirPostoverflowDest, postoverflowFileName)
  27. if err := Copy(postoverflowSource, hubDirPostoverflowPath); err != nil {
  28. return fmt.Errorf("unable to copy '%s' to '%s': %s", postoverflowSource, hubDirPostoverflowPath, err)
  29. }
  30. // runtime/postoverflows/s00-enrich/rdns.yaml
  31. postoverflowDirParserPath := filepath.Join(postoverflowDirDest, postoverflowFileName)
  32. if err := os.Symlink(hubDirPostoverflowPath, postoverflowDirParserPath); err != nil {
  33. if !os.IsExist(err) {
  34. return fmt.Errorf("unable to symlink postoverflow '%s' to '%s': %s", hubDirPostoverflowPath, postoverflowDirParserPath, err)
  35. }
  36. }
  37. return nil
  38. }
  39. func (t *HubTestItem) installPostoverflowCustom(postoverflow string) error {
  40. customPostoverflowExist := false
  41. for _, customPath := range t.CustomItemsLocation {
  42. // we check if its a custom postoverflow
  43. customPostOverflowPath := filepath.Join(customPath, postoverflow)
  44. if _, err := os.Stat(customPostOverflowPath); os.IsNotExist(err) {
  45. continue
  46. //return fmt.Errorf("postoverflow '%s' doesn't exist in the hub and doesn't appear to be a custom one.", postoverflow)
  47. }
  48. customPostOverflowPathSplit := strings.Split(customPostOverflowPath, "/")
  49. customPostoverflowName := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-1]
  50. // because path is postoverflows/<stage>/<author>/parser.yaml and we wan't the stage
  51. customPostoverflowStage := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-3]
  52. // check if stage exist
  53. hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("postoverflows/%s", customPostoverflowStage))
  54. if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
  55. continue
  56. //return fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
  57. }
  58. postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
  59. if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
  60. continue
  61. //return fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
  62. }
  63. customPostoverflowDest := filepath.Join(postoverflowDirDest, customPostoverflowName)
  64. // if path to postoverflow exist, copy it
  65. if err := Copy(customPostOverflowPath, customPostoverflowDest); err != nil {
  66. continue
  67. //return fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customPostOverflowPath, customPostoverflowDest, err)
  68. }
  69. customPostoverflowExist = true
  70. break
  71. }
  72. if !customPostoverflowExist {
  73. return fmt.Errorf("couldn't find custom postoverflow '%s' in the following location: %+v", postoverflow, t.CustomItemsLocation)
  74. }
  75. return nil
  76. }
  77. func (t *HubTestItem) installPostoverflow(name string) error {
  78. if hubPostOverflow := t.HubIndex.GetItem(cwhub.POSTOVERFLOWS, name); hubPostOverflow != nil {
  79. return t.installPostoverflowItem(hubPostOverflow)
  80. }
  81. return t.installPostoverflowCustom(name)
  82. }