2024-01-18 10:09:14 +00:00
|
|
|
package hubtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
|
|
)
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
func (t *HubTestItem) installParserItem(item *cwhub.Item) error {
|
|
|
|
sourcePath, err := filepath.Abs(filepath.Join(t.HubPath, item.RemotePath))
|
2024-01-18 10:09:14 +00:00
|
|
|
if err != nil {
|
2024-02-14 10:53:12 +00:00
|
|
|
return fmt.Errorf("can't get absolute path of '%s': %w", sourcePath, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
sourceFilename := filepath.Base(sourcePath)
|
2024-01-18 10:09:14 +00:00
|
|
|
|
|
|
|
// runtime/hub/parsers/s00-raw/crowdsecurity/
|
2024-02-14 10:53:12 +00:00
|
|
|
hubDirParserDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(item.RemotePath))
|
2024-01-18 10:09:14 +00:00
|
|
|
|
|
|
|
// runtime/parsers/s00-raw/
|
2024-02-14 10:53:12 +00:00
|
|
|
itemTypeDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, item.Stage)
|
2024-01-18 10:09:14 +00:00
|
|
|
|
|
|
|
if err := os.MkdirAll(hubDirParserDest, os.ModePerm); err != nil {
|
2024-02-14 10:53:12 +00:00
|
|
|
return fmt.Errorf("unable to create folder '%s': %w", hubDirParserDest, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
|
|
|
|
return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runtime/hub/parsers/s00-raw/crowdsecurity/syslog-logs.yaml
|
2024-02-14 10:53:12 +00:00
|
|
|
hubDirParserPath := filepath.Join(hubDirParserDest, sourceFilename)
|
|
|
|
if err := Copy(sourcePath, hubDirParserPath); err != nil {
|
|
|
|
return fmt.Errorf("unable to copy '%s' to '%s': %w", sourcePath, hubDirParserPath, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runtime/parsers/s00-raw/syslog-logs.yaml
|
2024-02-14 10:53:12 +00:00
|
|
|
parserDirParserPath := filepath.Join(itemTypeDirDest, sourceFilename)
|
2024-01-18 10:09:14 +00:00
|
|
|
if err := os.Symlink(hubDirParserPath, parserDirParserPath); err != nil {
|
|
|
|
if !os.IsExist(err) {
|
2024-02-14 10:53:12 +00:00
|
|
|
return fmt.Errorf("unable to symlink parser '%s' to '%s': %w", hubDirParserPath, parserDirParserPath, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
func (t *HubTestItem) installParserCustomFrom(parser string, customPath string) (bool, error) {
|
|
|
|
// we check if its a custom parser
|
|
|
|
customParserPath := filepath.Join(customPath, parser)
|
|
|
|
if _, err := os.Stat(customParserPath); os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2024-01-18 10:09:14 +00:00
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
customParserPathSplit, customParserName := filepath.Split(customParserPath)
|
|
|
|
// because path is parsers/<stage>/<author>/parser.yaml and we wan't the stage
|
|
|
|
splitPath := strings.Split(customParserPathSplit, string(os.PathSeparator))
|
|
|
|
customParserStage := splitPath[len(splitPath)-3]
|
2024-01-18 10:09:14 +00:00
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
// check if stage exist
|
|
|
|
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("parsers/%s", customParserStage))
|
|
|
|
if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
|
|
|
|
return false, fmt.Errorf("stage '%s' extracted from '%s' doesn't exist in the hub", customParserStage, hubStagePath)
|
|
|
|
}
|
2024-01-18 10:09:14 +00:00
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
stageDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, customParserStage)
|
|
|
|
if err := os.MkdirAll(stageDirDest, os.ModePerm); err != nil {
|
|
|
|
return false, fmt.Errorf("unable to create folder '%s': %w", stageDirDest, err)
|
|
|
|
}
|
2024-01-18 10:09:14 +00:00
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
customParserDest := filepath.Join(stageDirDest, customParserName)
|
|
|
|
// if path to parser exist, copy it
|
|
|
|
if err := Copy(customParserPath, customParserDest); err != nil {
|
|
|
|
return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %w", customParserPath, customParserDest, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
2024-01-18 10:09:14 +00:00
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
func (t *HubTestItem) installParserCustom(parser string) error {
|
|
|
|
for _, customPath := range t.CustomItemsLocation {
|
|
|
|
found, err := t.installParserCustomFrom(parser, customPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
if found {
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
return fmt.Errorf("couldn't find custom parser '%s' in the following locations: %+v", parser, t.CustomItemsLocation)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HubTestItem) installParser(name string) error {
|
|
|
|
if item := t.HubIndex.GetItem(cwhub.PARSERS, name); item != nil {
|
|
|
|
return t.installParserItem(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.installParserCustom(name)
|
|
|
|
}
|