github.com/crowdsecurity/crowdsec@v1.6.1/pkg/hubtest/scenario.go (about) 1 package hubtest 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/crowdsecurity/crowdsec/pkg/cwhub" 9 ) 10 11 func (t *HubTestItem) installScenarioItem(item *cwhub.Item) error { 12 sourcePath, err := filepath.Abs(filepath.Join(t.HubPath, item.RemotePath)) 13 if err != nil { 14 return fmt.Errorf("can't get absolute path of '%s': %w", sourcePath, err) 15 } 16 17 sourceFilename := filepath.Base(sourcePath) 18 19 // runtime/hub/scenarios/crowdsecurity/ 20 hubDirScenarioDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(item.RemotePath)) 21 22 // runtime/parsers/scenarios/ 23 itemTypeDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath) 24 25 if err := os.MkdirAll(hubDirScenarioDest, os.ModePerm); err != nil { 26 return fmt.Errorf("unable to create folder '%s': %w", hubDirScenarioDest, err) 27 } 28 29 if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil { 30 return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err) 31 } 32 33 // runtime/hub/scenarios/crowdsecurity/ssh-bf.yaml 34 hubDirScenarioPath := filepath.Join(hubDirScenarioDest, sourceFilename) 35 if err := Copy(sourcePath, hubDirScenarioPath); err != nil { 36 return fmt.Errorf("unable to copy '%s' to '%s': %w", sourcePath, hubDirScenarioPath, err) 37 } 38 39 // runtime/scenarios/ssh-bf.yaml 40 scenarioDirParserPath := filepath.Join(itemTypeDirDest, sourceFilename) 41 if err := os.Symlink(hubDirScenarioPath, scenarioDirParserPath); err != nil { 42 if !os.IsExist(err) { 43 return fmt.Errorf("unable to symlink scenario '%s' to '%s': %w", hubDirScenarioPath, scenarioDirParserPath, err) 44 } 45 } 46 47 return nil 48 } 49 50 func (t *HubTestItem) installScenarioCustomFrom(scenario string, customPath string) (bool, error) { 51 // we check if its a custom scenario 52 customScenarioPath := filepath.Join(customPath, scenario) 53 if _, err := os.Stat(customScenarioPath); os.IsNotExist(err) { 54 return false, nil 55 } 56 57 itemTypeDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath) 58 if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil { 59 return false, fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err) 60 } 61 62 scenarioFileName := filepath.Base(customScenarioPath) 63 64 scenarioFileDest := filepath.Join(itemTypeDirDest, scenarioFileName) 65 if err := Copy(customScenarioPath, scenarioFileDest); err != nil { 66 return false, fmt.Errorf("unable to copy scenario from '%s' to '%s': %w", customScenarioPath, scenarioFileDest, err) 67 } 68 69 return true, nil 70 } 71 72 func (t *HubTestItem) installScenarioCustom(scenario string) error { 73 for _, customPath := range t.CustomItemsLocation { 74 found, err := t.installScenarioCustomFrom(scenario, customPath) 75 if err != nil { 76 return err 77 } 78 79 if found { 80 return nil 81 } 82 } 83 84 return fmt.Errorf("couldn't find custom scenario '%s' in the following location: %+v", scenario, t.CustomItemsLocation) 85 } 86 87 func (t *HubTestItem) installScenario(name string) error { 88 if item := t.HubIndex.GetItem(cwhub.SCENARIOS, name); item != nil { 89 return t.installScenarioItem(item) 90 } 91 92 return t.installScenarioCustom(name) 93 }