2024-01-18 10:09:14 +00:00
|
|
|
package hubtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
|
|
)
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
func (t *HubTestItem) installScenarioItem(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/scenarios/crowdsecurity/
|
2024-02-14 10:53:12 +00:00
|
|
|
hubDirScenarioDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(item.RemotePath))
|
2024-01-18 10:09:14 +00:00
|
|
|
|
|
|
|
// runtime/parsers/scenarios/
|
2024-02-14 10:53:12 +00:00
|
|
|
itemTypeDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
|
2024-01-18 10:09:14 +00:00
|
|
|
|
|
|
|
if err := os.MkdirAll(hubDirScenarioDest, os.ModePerm); err != nil {
|
2024-02-14 10:53:12 +00:00
|
|
|
return fmt.Errorf("unable to create folder '%s': %w", hubDirScenarioDest, 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/scenarios/crowdsecurity/ssh-bf.yaml
|
2024-02-14 10:53:12 +00:00
|
|
|
hubDirScenarioPath := filepath.Join(hubDirScenarioDest, sourceFilename)
|
|
|
|
if err := Copy(sourcePath, hubDirScenarioPath); err != nil {
|
|
|
|
return fmt.Errorf("unable to copy '%s' to '%s': %w", sourcePath, hubDirScenarioPath, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runtime/scenarios/ssh-bf.yaml
|
2024-02-14 10:53:12 +00:00
|
|
|
scenarioDirParserPath := filepath.Join(itemTypeDirDest, sourceFilename)
|
2024-01-18 10:09:14 +00:00
|
|
|
if err := os.Symlink(hubDirScenarioPath, scenarioDirParserPath); err != nil {
|
|
|
|
if !os.IsExist(err) {
|
2024-02-14 10:53:12 +00:00
|
|
|
return fmt.Errorf("unable to symlink scenario '%s' to '%s': %w", hubDirScenarioPath, scenarioDirParserPath, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
func (t *HubTestItem) installScenarioCustomFrom(scenario string, customPath string) (bool, error) {
|
|
|
|
// we check if its a custom scenario
|
|
|
|
customScenarioPath := filepath.Join(customPath, scenario)
|
|
|
|
if _, err := os.Stat(customScenarioPath); os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
itemTypeDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
|
|
|
|
if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
|
|
|
|
return false, fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarioFileName := filepath.Base(customScenarioPath)
|
|
|
|
|
|
|
|
scenarioFileDest := filepath.Join(itemTypeDirDest, scenarioFileName)
|
|
|
|
if err := Copy(customScenarioPath, scenarioFileDest); err != nil {
|
|
|
|
return false, fmt.Errorf("unable to copy scenario from '%s' to '%s': %w", customScenarioPath, scenarioFileDest, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2024-01-18 10:09:14 +00:00
|
|
|
func (t *HubTestItem) installScenarioCustom(scenario string) error {
|
|
|
|
for _, customPath := range t.CustomItemsLocation {
|
2024-02-14 10:53:12 +00:00
|
|
|
found, err := t.installScenarioCustomFrom(scenario, 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 scenario '%s' in the following location: %+v", scenario, t.CustomItemsLocation)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HubTestItem) installScenario(name string) error {
|
|
|
|
if item := t.HubIndex.GetItem(cwhub.SCENARIOS, name); item != nil {
|
|
|
|
return t.installScenarioItem(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.installScenarioCustom(name)
|
|
|
|
}
|