|
@@ -9,88 +9,90 @@ import (
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
)
|
|
)
|
|
|
|
|
|
-func (t *HubTestItem) installPostoverflowItem(hubPostOverflow *cwhub.Item) error {
|
|
|
|
- postoverflowSource, err := filepath.Abs(filepath.Join(t.HubPath, hubPostOverflow.RemotePath))
|
|
|
|
|
|
+func (t *HubTestItem) installPostoverflowItem(item *cwhub.Item) error {
|
|
|
|
+ sourcePath, err := filepath.Abs(filepath.Join(t.HubPath, item.RemotePath))
|
|
if err != nil {
|
|
if err != nil {
|
|
- return fmt.Errorf("can't get absolute path of '%s': %s", postoverflowSource, err)
|
|
|
|
|
|
+ return fmt.Errorf("can't get absolute path of '%s': %w", sourcePath, err)
|
|
}
|
|
}
|
|
|
|
|
|
- postoverflowFileName := filepath.Base(postoverflowSource)
|
|
|
|
|
|
+ sourceFilename := filepath.Base(sourcePath)
|
|
|
|
|
|
// runtime/hub/postoverflows/s00-enrich/crowdsecurity/
|
|
// runtime/hub/postoverflows/s00-enrich/crowdsecurity/
|
|
- hubDirPostoverflowDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(hubPostOverflow.RemotePath))
|
|
|
|
|
|
+ hubDirPostoverflowDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(item.RemotePath))
|
|
|
|
|
|
// runtime/postoverflows/s00-enrich
|
|
// runtime/postoverflows/s00-enrich
|
|
- postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, hubPostOverflow.Stage)
|
|
|
|
|
|
+ itemTypeDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, item.Stage)
|
|
|
|
|
|
if err := os.MkdirAll(hubDirPostoverflowDest, os.ModePerm); err != nil {
|
|
if err := os.MkdirAll(hubDirPostoverflowDest, os.ModePerm); err != nil {
|
|
- return fmt.Errorf("unable to create folder '%s': %s", hubDirPostoverflowDest, err)
|
|
|
|
|
|
+ return fmt.Errorf("unable to create folder '%s': %w", hubDirPostoverflowDest, err)
|
|
}
|
|
}
|
|
|
|
|
|
- if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
|
|
|
|
- return fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
|
|
|
|
|
|
+ if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
|
|
|
|
+ return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err)
|
|
}
|
|
}
|
|
|
|
|
|
// runtime/hub/postoverflows/s00-enrich/crowdsecurity/rdns.yaml
|
|
// runtime/hub/postoverflows/s00-enrich/crowdsecurity/rdns.yaml
|
|
- hubDirPostoverflowPath := filepath.Join(hubDirPostoverflowDest, postoverflowFileName)
|
|
|
|
- if err := Copy(postoverflowSource, hubDirPostoverflowPath); err != nil {
|
|
|
|
- return fmt.Errorf("unable to copy '%s' to '%s': %s", postoverflowSource, hubDirPostoverflowPath, err)
|
|
|
|
|
|
+ hubDirPostoverflowPath := filepath.Join(hubDirPostoverflowDest, sourceFilename)
|
|
|
|
+ if err := Copy(sourcePath, hubDirPostoverflowPath); err != nil {
|
|
|
|
+ return fmt.Errorf("unable to copy '%s' to '%s': %w", sourcePath, hubDirPostoverflowPath, err)
|
|
}
|
|
}
|
|
|
|
|
|
// runtime/postoverflows/s00-enrich/rdns.yaml
|
|
// runtime/postoverflows/s00-enrich/rdns.yaml
|
|
- postoverflowDirParserPath := filepath.Join(postoverflowDirDest, postoverflowFileName)
|
|
|
|
|
|
+ postoverflowDirParserPath := filepath.Join(itemTypeDirDest, sourceFilename)
|
|
if err := os.Symlink(hubDirPostoverflowPath, postoverflowDirParserPath); err != nil {
|
|
if err := os.Symlink(hubDirPostoverflowPath, postoverflowDirParserPath); err != nil {
|
|
if !os.IsExist(err) {
|
|
if !os.IsExist(err) {
|
|
- return fmt.Errorf("unable to symlink postoverflow '%s' to '%s': %s", hubDirPostoverflowPath, postoverflowDirParserPath, err)
|
|
|
|
|
|
+ return fmt.Errorf("unable to symlink postoverflow '%s' to '%s': %w", hubDirPostoverflowPath, postoverflowDirParserPath, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (t *HubTestItem) installPostoverflowCustom(postoverflow string) error {
|
|
|
|
- customPostoverflowExist := false
|
|
|
|
- for _, customPath := range t.CustomItemsLocation {
|
|
|
|
- // we check if its a custom postoverflow
|
|
|
|
- customPostOverflowPath := filepath.Join(customPath, postoverflow)
|
|
|
|
- if _, err := os.Stat(customPostOverflowPath); os.IsNotExist(err) {
|
|
|
|
- continue
|
|
|
|
- //return fmt.Errorf("postoverflow '%s' doesn't exist in the hub and doesn't appear to be a custom one.", postoverflow)
|
|
|
|
- }
|
|
|
|
|
|
+func (t *HubTestItem) installPostoverflowCustomFrom(postoverflow string, customPath string) (bool, error) {
|
|
|
|
+ // we check if its a custom postoverflow
|
|
|
|
+ customPostOverflowPath := filepath.Join(customPath, postoverflow)
|
|
|
|
+ if _, err := os.Stat(customPostOverflowPath); os.IsNotExist(err) {
|
|
|
|
+ return false, nil
|
|
|
|
+ }
|
|
|
|
|
|
- customPostOverflowPathSplit := strings.Split(customPostOverflowPath, "/")
|
|
|
|
- customPostoverflowName := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-1]
|
|
|
|
- // because path is postoverflows/<stage>/<author>/parser.yaml and we wan't the stage
|
|
|
|
- customPostoverflowStage := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-3]
|
|
|
|
|
|
+ customPostOverflowPathSplit := strings.Split(customPostOverflowPath, "/")
|
|
|
|
+ customPostoverflowName := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-1]
|
|
|
|
+ // because path is postoverflows/<stage>/<author>/parser.yaml and we wan't the stage
|
|
|
|
+ customPostoverflowStage := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-3]
|
|
|
|
|
|
- // check if stage exist
|
|
|
|
- hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("postoverflows/%s", customPostoverflowStage))
|
|
|
|
|
|
+ // check if stage exist
|
|
|
|
+ hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("postoverflows/%s", customPostoverflowStage))
|
|
|
|
+ if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
|
|
|
|
+ return false, fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
|
|
|
|
+ }
|
|
|
|
|
|
- if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
|
|
|
|
- continue
|
|
|
|
- //return fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
|
|
|
|
- }
|
|
|
|
|
|
+ stageDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
|
|
|
|
+ if err := os.MkdirAll(stageDirDest, os.ModePerm); err != nil {
|
|
|
|
+ return false, fmt.Errorf("unable to create folder '%s': %w", stageDirDest, err)
|
|
|
|
+ }
|
|
|
|
|
|
- postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
|
|
|
|
- if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
|
|
|
|
- continue
|
|
|
|
- //return fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
|
|
|
|
|
|
+ customPostoverflowDest := filepath.Join(stageDirDest, customPostoverflowName)
|
|
|
|
+ // if path to postoverflow exist, copy it
|
|
|
|
+ if err := Copy(customPostOverflowPath, customPostoverflowDest); err != nil {
|
|
|
|
+ return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %w", customPostOverflowPath, customPostoverflowDest, err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (t *HubTestItem) installPostoverflowCustom(postoverflow string) error {
|
|
|
|
+ for _, customPath := range t.CustomItemsLocation {
|
|
|
|
+ found, err := t.installPostoverflowCustomFrom(postoverflow, customPath)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
}
|
|
}
|
|
|
|
|
|
- customPostoverflowDest := filepath.Join(postoverflowDirDest, customPostoverflowName)
|
|
|
|
- // if path to postoverflow exist, copy it
|
|
|
|
- if err := Copy(customPostOverflowPath, customPostoverflowDest); err != nil {
|
|
|
|
- continue
|
|
|
|
- //return fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customPostOverflowPath, customPostoverflowDest, err)
|
|
|
|
|
|
+ if found {
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
- customPostoverflowExist = true
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- if !customPostoverflowExist {
|
|
|
|
- return fmt.Errorf("couldn't find custom postoverflow '%s' in the following location: %+v", postoverflow, t.CustomItemsLocation)
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- return nil
|
|
|
|
|
|
+ return fmt.Errorf("couldn't find custom postoverflow '%s' in the following location: %+v", postoverflow, t.CustomItemsLocation)
|
|
}
|
|
}
|
|
|
|
|
|
func (t *HubTestItem) installPostoverflow(name string) error {
|
|
func (t *HubTestItem) installPostoverflow(name string) error {
|