postoverflow.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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) installPostoverflowCustomFrom(postoverflow string, customPath string) (bool, error) {
  40. // we check if its a custom postoverflow
  41. customPostOverflowPath := filepath.Join(customPath, postoverflow)
  42. if _, err := os.Stat(customPostOverflowPath); os.IsNotExist(err) {
  43. return false, nil
  44. }
  45. customPostOverflowPathSplit := strings.Split(customPostOverflowPath, "/")
  46. customPostoverflowName := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-1]
  47. // because path is postoverflows/<stage>/<author>/parser.yaml and we wan't the stage
  48. customPostoverflowStage := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-3]
  49. // check if stage exist
  50. hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("postoverflows/%s", customPostoverflowStage))
  51. if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
  52. return false, fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
  53. }
  54. postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
  55. if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
  56. return false, fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
  57. }
  58. customPostoverflowDest := filepath.Join(postoverflowDirDest, customPostoverflowName)
  59. // if path to postoverflow exist, copy it
  60. if err := Copy(customPostOverflowPath, customPostoverflowDest); err != nil {
  61. return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customPostOverflowPath, customPostoverflowDest, err)
  62. }
  63. return true, nil
  64. }
  65. func (t *HubTestItem) installPostoverflowCustom(postoverflow string) error {
  66. for _, customPath := range t.CustomItemsLocation {
  67. found, err := t.installPostoverflowCustomFrom(postoverflow, customPath)
  68. if err != nil {
  69. return err
  70. }
  71. if found {
  72. return nil
  73. }
  74. }
  75. return fmt.Errorf("couldn't find custom postoverflow '%s' in the following location: %+v", postoverflow, t.CustomItemsLocation)
  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. }