pkg/hubtest: extract methods + consistent error handling (#2756)
* pkg/hubtest: extract methods + consistent error handling * lint * rename variables for further refactor
This commit is contained in:
parent
2bbf0b4762
commit
8de8bf0e06
|
@ -11,75 +11,81 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (t *HubTestItem) installAppsecRuleItem(hubAppsecRule *cwhub.Item) error {
|
func (t *HubTestItem) installAppsecRuleItem(item *cwhub.Item) error {
|
||||||
appsecRuleSource, err := filepath.Abs(filepath.Join(t.HubPath, hubAppsecRule.RemotePath))
|
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", appsecRuleSource, err)
|
return fmt.Errorf("can't get absolute path of '%s': %w", sourcePath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
appsecRuleFilename := filepath.Base(appsecRuleSource)
|
sourceFilename := filepath.Base(sourcePath)
|
||||||
|
|
||||||
// runtime/hub/appsec-rules/author/appsec-rule
|
// runtime/hub/appsec-rules/author/appsec-rule
|
||||||
hubDirAppsecRuleDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(hubAppsecRule.RemotePath))
|
hubDirAppsecRuleDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(item.RemotePath))
|
||||||
|
|
||||||
// runtime/appsec-rules/
|
// runtime/appsec-rules/
|
||||||
appsecRuleDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
|
itemTypeDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
|
||||||
|
|
||||||
if err := os.MkdirAll(hubDirAppsecRuleDest, os.ModePerm); err != nil {
|
if err := os.MkdirAll(hubDirAppsecRuleDest, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("unable to create folder '%s': %s", hubDirAppsecRuleDest, err)
|
return fmt.Errorf("unable to create folder '%s': %w", hubDirAppsecRuleDest, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(appsecRuleDirDest, os.ModePerm); err != nil {
|
if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("unable to create folder '%s': %s", appsecRuleDirDest, err)
|
return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runtime/hub/appsec-rules/crowdsecurity/rule.yaml
|
// runtime/hub/appsec-rules/crowdsecurity/rule.yaml
|
||||||
hubDirAppsecRulePath := filepath.Join(appsecRuleDirDest, appsecRuleFilename)
|
hubDirAppsecRulePath := filepath.Join(itemTypeDirDest, sourceFilename)
|
||||||
if err := Copy(appsecRuleSource, hubDirAppsecRulePath); err != nil {
|
if err := Copy(sourcePath, hubDirAppsecRulePath); err != nil {
|
||||||
return fmt.Errorf("unable to copy '%s' to '%s': %s", appsecRuleSource, hubDirAppsecRulePath, err)
|
return fmt.Errorf("unable to copy '%s' to '%s': %w", sourcePath, hubDirAppsecRulePath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runtime/appsec-rules/rule.yaml
|
// runtime/appsec-rules/rule.yaml
|
||||||
appsecRulePath := filepath.Join(appsecRuleDirDest, appsecRuleFilename)
|
appsecRulePath := filepath.Join(itemTypeDirDest, sourceFilename)
|
||||||
if err := os.Symlink(hubDirAppsecRulePath, appsecRulePath); err != nil {
|
if err := os.Symlink(hubDirAppsecRulePath, appsecRulePath); err != nil {
|
||||||
if !os.IsExist(err) {
|
if !os.IsExist(err) {
|
||||||
return fmt.Errorf("unable to symlink appsec-rule '%s' to '%s': %s", hubDirAppsecRulePath, appsecRulePath, err)
|
return fmt.Errorf("unable to symlink appsec-rule '%s' to '%s': %w", hubDirAppsecRulePath, appsecRulePath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HubTestItem) installAppsecRuleCustom(appsecrule string) error {
|
func (t *HubTestItem) installAppsecRuleCustomFrom(appsecrule string, customPath string) (bool, error) {
|
||||||
customAppsecRuleExist := false
|
|
||||||
for _, customPath := range t.CustomItemsLocation {
|
|
||||||
// we check if its a custom appsec-rule
|
// we check if its a custom appsec-rule
|
||||||
customAppsecRulePath := filepath.Join(customPath, appsecrule)
|
customAppsecRulePath := filepath.Join(customPath, appsecrule)
|
||||||
if _, err := os.Stat(customAppsecRulePath); os.IsNotExist(err) {
|
if _, err := os.Stat(customAppsecRulePath); os.IsNotExist(err) {
|
||||||
continue
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
customAppsecRulePathSplit := strings.Split(customAppsecRulePath, "/")
|
customAppsecRulePathSplit := strings.Split(customAppsecRulePath, "/")
|
||||||
customAppsecRuleName := customAppsecRulePathSplit[len(customAppsecRulePathSplit)-1]
|
customAppsecRuleName := customAppsecRulePathSplit[len(customAppsecRulePathSplit)-1]
|
||||||
|
|
||||||
appsecRuleDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
|
itemTypeDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
|
||||||
if err := os.MkdirAll(appsecRuleDirDest, os.ModePerm); err != nil {
|
if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("unable to create folder '%s': %s", appsecRuleDirDest, err)
|
return false, fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runtime/appsec-rules/
|
|
||||||
customAppsecRuleDest := fmt.Sprintf("%s/appsec-rules/%s", t.RuntimePath, customAppsecRuleName)
|
customAppsecRuleDest := fmt.Sprintf("%s/appsec-rules/%s", t.RuntimePath, customAppsecRuleName)
|
||||||
// if path to postoverflow exist, copy it
|
|
||||||
if err := Copy(customAppsecRulePath, customAppsecRuleDest); err != nil {
|
if err := Copy(customAppsecRulePath, customAppsecRuleDest); err != nil {
|
||||||
continue
|
return false, fmt.Errorf("unable to copy appsec-rule from '%s' to '%s': %w", customAppsecRulePath, customAppsecRuleDest, err)
|
||||||
}
|
|
||||||
customAppsecRuleExist = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if !customAppsecRuleExist {
|
|
||||||
return fmt.Errorf("couldn't find custom appsec-rule '%s' in the following location: %+v", appsecrule, t.CustomItemsLocation)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *HubTestItem) installAppsecRuleCustom(appsecrule string) error {
|
||||||
|
for _, customPath := range t.CustomItemsLocation {
|
||||||
|
found, err := t.installAppsecRuleCustomFrom(appsecrule, customPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if found {
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("couldn't find custom appsec-rule '%s' in the following location: %+v", appsecrule, t.CustomItemsLocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HubTestItem) installAppsecRule(name string) error {
|
func (t *HubTestItem) installAppsecRule(name string) error {
|
||||||
|
|
|
@ -9,89 +9,90 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (t *HubTestItem) installParserItem(hubParser *cwhub.Item) error {
|
func (t *HubTestItem) installParserItem(item *cwhub.Item) error {
|
||||||
parserSource, err := filepath.Abs(filepath.Join(t.HubPath, hubParser.RemotePath))
|
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", parserSource, err)
|
return fmt.Errorf("can't get absolute path of '%s': %w", sourcePath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
parserFileName := filepath.Base(parserSource)
|
sourceFilename := filepath.Base(sourcePath)
|
||||||
|
|
||||||
// runtime/hub/parsers/s00-raw/crowdsecurity/
|
// runtime/hub/parsers/s00-raw/crowdsecurity/
|
||||||
hubDirParserDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(hubParser.RemotePath))
|
hubDirParserDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(item.RemotePath))
|
||||||
|
|
||||||
// runtime/parsers/s00-raw/
|
// runtime/parsers/s00-raw/
|
||||||
parserDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, hubParser.Stage)
|
itemTypeDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, item.Stage)
|
||||||
|
|
||||||
if err := os.MkdirAll(hubDirParserDest, os.ModePerm); err != nil {
|
if err := os.MkdirAll(hubDirParserDest, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("unable to create folder '%s': %s", hubDirParserDest, err)
|
return fmt.Errorf("unable to create folder '%s': %w", hubDirParserDest, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(parserDirDest, os.ModePerm); err != nil {
|
if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("unable to create folder '%s': %s", parserDirDest, err)
|
return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runtime/hub/parsers/s00-raw/crowdsecurity/syslog-logs.yaml
|
// runtime/hub/parsers/s00-raw/crowdsecurity/syslog-logs.yaml
|
||||||
hubDirParserPath := filepath.Join(hubDirParserDest, parserFileName)
|
hubDirParserPath := filepath.Join(hubDirParserDest, sourceFilename)
|
||||||
if err := Copy(parserSource, hubDirParserPath); err != nil {
|
if err := Copy(sourcePath, hubDirParserPath); err != nil {
|
||||||
return fmt.Errorf("unable to copy '%s' to '%s': %s", parserSource, hubDirParserPath, err)
|
return fmt.Errorf("unable to copy '%s' to '%s': %w", sourcePath, hubDirParserPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runtime/parsers/s00-raw/syslog-logs.yaml
|
// runtime/parsers/s00-raw/syslog-logs.yaml
|
||||||
parserDirParserPath := filepath.Join(parserDirDest, parserFileName)
|
parserDirParserPath := filepath.Join(itemTypeDirDest, sourceFilename)
|
||||||
if err := os.Symlink(hubDirParserPath, parserDirParserPath); err != nil {
|
if err := os.Symlink(hubDirParserPath, parserDirParserPath); err != nil {
|
||||||
if !os.IsExist(err) {
|
if !os.IsExist(err) {
|
||||||
return fmt.Errorf("unable to symlink parser '%s' to '%s': %s", hubDirParserPath, parserDirParserPath, err)
|
return fmt.Errorf("unable to symlink parser '%s' to '%s': %w", hubDirParserPath, parserDirParserPath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HubTestItem) installParserCustom(parser string) error {
|
func (t *HubTestItem) installParserCustomFrom(parser string, customPath string) (bool, error) {
|
||||||
customParserExist := false
|
|
||||||
for _, customPath := range t.CustomItemsLocation {
|
|
||||||
// we check if its a custom parser
|
// we check if its a custom parser
|
||||||
customParserPath := filepath.Join(customPath, parser)
|
customParserPath := filepath.Join(customPath, parser)
|
||||||
if _, err := os.Stat(customParserPath); os.IsNotExist(err) {
|
if _, err := os.Stat(customParserPath); os.IsNotExist(err) {
|
||||||
continue
|
return false, nil
|
||||||
//return fmt.Errorf("parser '%s' doesn't exist in the hub and doesn't appear to be a custom one.", parser)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
customParserPathSplit, customParserName := filepath.Split(customParserPath)
|
customParserPathSplit, customParserName := filepath.Split(customParserPath)
|
||||||
// because path is parsers/<stage>/<author>/parser.yaml and we wan't the stage
|
// because path is parsers/<stage>/<author>/parser.yaml and we wan't the stage
|
||||||
splittedPath := strings.Split(customParserPathSplit, string(os.PathSeparator))
|
splitPath := strings.Split(customParserPathSplit, string(os.PathSeparator))
|
||||||
customParserStage := splittedPath[len(splittedPath)-3]
|
customParserStage := splitPath[len(splitPath)-3]
|
||||||
|
|
||||||
// check if stage exist
|
// check if stage exist
|
||||||
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("parsers/%s", customParserStage))
|
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("parsers/%s", customParserStage))
|
||||||
|
|
||||||
if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
|
if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
|
||||||
continue
|
return false, fmt.Errorf("stage '%s' extracted from '%s' doesn't exist in the hub", customParserStage, hubStagePath)
|
||||||
//return fmt.Errorf("stage '%s' extracted from '%s' doesn't exist in the hub", customParserStage, hubStagePath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parserDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, customParserStage)
|
stageDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, customParserStage)
|
||||||
if err := os.MkdirAll(parserDirDest, os.ModePerm); err != nil {
|
if err := os.MkdirAll(stageDirDest, os.ModePerm); err != nil {
|
||||||
continue
|
return false, fmt.Errorf("unable to create folder '%s': %w", stageDirDest, err)
|
||||||
//return fmt.Errorf("unable to create folder '%s': %s", parserDirDest, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
customParserDest := filepath.Join(parserDirDest, customParserName)
|
customParserDest := filepath.Join(stageDirDest, customParserName)
|
||||||
// if path to parser exist, copy it
|
// if path to parser exist, copy it
|
||||||
if err := Copy(customParserPath, customParserDest); err != nil {
|
if err := Copy(customParserPath, customParserDest); err != nil {
|
||||||
continue
|
return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %w", customParserPath, customParserDest, err)
|
||||||
//return fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customParserPath, customParserDest, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
customParserExist = true
|
return true, nil
|
||||||
break
|
}
|
||||||
}
|
|
||||||
if !customParserExist {
|
func (t *HubTestItem) installParserCustom(parser string) error {
|
||||||
return fmt.Errorf("couldn't find custom parser '%s' in the following location: %+v", parser, t.CustomItemsLocation)
|
for _, customPath := range t.CustomItemsLocation {
|
||||||
|
found, err := t.installParserCustomFrom(parser, customPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if found {
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("couldn't find custom parser '%s' in the following locations: %+v", parser, t.CustomItemsLocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HubTestItem) installParser(name string) error {
|
func (t *HubTestItem) installParser(name string) error {
|
||||||
|
|
|
@ -9,53 +9,50 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (t *HubTestItem) installPostoverflowItem(hubPostOverflow *cwhub.Item) error {
|
func (t *HubTestItem) installPostoverflowItem(item *cwhub.Item) error {
|
||||||
postoverflowSource, err := filepath.Abs(filepath.Join(t.HubPath, hubPostOverflow.RemotePath))
|
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 {
|
if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
|
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)
|
hubDirPostoverflowPath := filepath.Join(hubDirPostoverflowDest, sourceFilename)
|
||||||
if err := Copy(postoverflowSource, hubDirPostoverflowPath); err != nil {
|
if err := Copy(sourcePath, hubDirPostoverflowPath); err != nil {
|
||||||
return fmt.Errorf("unable to copy '%s' to '%s': %s", postoverflowSource, hubDirPostoverflowPath, err)
|
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 {
|
func (t *HubTestItem) installPostoverflowCustomFrom(postoverflow string, customPath string) (bool, error) {
|
||||||
customPostoverflowExist := false
|
|
||||||
for _, customPath := range t.CustomItemsLocation {
|
|
||||||
// we check if its a custom postoverflow
|
// we check if its a custom postoverflow
|
||||||
customPostOverflowPath := filepath.Join(customPath, postoverflow)
|
customPostOverflowPath := filepath.Join(customPath, postoverflow)
|
||||||
if _, err := os.Stat(customPostOverflowPath); os.IsNotExist(err) {
|
if _, err := os.Stat(customPostOverflowPath); os.IsNotExist(err) {
|
||||||
continue
|
return false, nil
|
||||||
//return fmt.Errorf("postoverflow '%s' doesn't exist in the hub and doesn't appear to be a custom one.", postoverflow)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
customPostOverflowPathSplit := strings.Split(customPostOverflowPath, "/")
|
customPostOverflowPathSplit := strings.Split(customPostOverflowPath, "/")
|
||||||
|
@ -65,32 +62,37 @@ func (t *HubTestItem) installPostoverflowCustom(postoverflow string) error {
|
||||||
|
|
||||||
// check if stage exist
|
// check if stage exist
|
||||||
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("postoverflows/%s", customPostoverflowStage))
|
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("postoverflows/%s", customPostoverflowStage))
|
||||||
|
|
||||||
if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
|
if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
|
||||||
continue
|
return false, fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
|
||||||
//return fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
|
stageDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
|
||||||
if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
|
if err := os.MkdirAll(stageDirDest, os.ModePerm); err != nil {
|
||||||
continue
|
return false, fmt.Errorf("unable to create folder '%s': %w", stageDirDest, err)
|
||||||
//return fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
customPostoverflowDest := filepath.Join(postoverflowDirDest, customPostoverflowName)
|
customPostoverflowDest := filepath.Join(stageDirDest, customPostoverflowName)
|
||||||
// if path to postoverflow exist, copy it
|
// if path to postoverflow exist, copy it
|
||||||
if err := Copy(customPostOverflowPath, customPostoverflowDest); err != nil {
|
if err := Copy(customPostOverflowPath, customPostoverflowDest); err != nil {
|
||||||
continue
|
return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %w", customPostOverflowPath, customPostoverflowDest, err)
|
||||||
//return fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customPostOverflowPath, customPostoverflowDest, err)
|
|
||||||
}
|
|
||||||
customPostoverflowExist = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if !customPostoverflowExist {
|
|
||||||
return fmt.Errorf("couldn't find custom postoverflow '%s' in the following location: %+v", postoverflow, t.CustomItemsLocation)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
if found {
|
||||||
return nil
|
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 {
|
||||||
|
|
|
@ -8,74 +8,80 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (t *HubTestItem) installScenarioItem(hubScenario *cwhub.Item) error {
|
func (t *HubTestItem) installScenarioItem(item *cwhub.Item) error {
|
||||||
scenarioSource, err := filepath.Abs(filepath.Join(t.HubPath, hubScenario.RemotePath))
|
sourcePath, err := filepath.Abs(filepath.Join(t.HubPath, item.RemotePath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't get absolute path to: %s", scenarioSource)
|
return fmt.Errorf("can't get absolute path of '%s': %w", sourcePath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
scenarioFileName := filepath.Base(scenarioSource)
|
sourceFilename := filepath.Base(sourcePath)
|
||||||
|
|
||||||
// runtime/hub/scenarios/crowdsecurity/
|
// runtime/hub/scenarios/crowdsecurity/
|
||||||
hubDirScenarioDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(hubScenario.RemotePath))
|
hubDirScenarioDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(item.RemotePath))
|
||||||
|
|
||||||
// runtime/parsers/scenarios/
|
// runtime/parsers/scenarios/
|
||||||
scenarioDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
|
itemTypeDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
|
||||||
|
|
||||||
if err := os.MkdirAll(hubDirScenarioDest, os.ModePerm); err != nil {
|
if err := os.MkdirAll(hubDirScenarioDest, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("unable to create folder '%s': %s", hubDirScenarioDest, err)
|
return fmt.Errorf("unable to create folder '%s': %w", hubDirScenarioDest, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(scenarioDirDest, os.ModePerm); err != nil {
|
if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("unable to create folder '%s': %s", scenarioDirDest, err)
|
return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runtime/hub/scenarios/crowdsecurity/ssh-bf.yaml
|
// runtime/hub/scenarios/crowdsecurity/ssh-bf.yaml
|
||||||
hubDirScenarioPath := filepath.Join(hubDirScenarioDest, scenarioFileName)
|
hubDirScenarioPath := filepath.Join(hubDirScenarioDest, sourceFilename)
|
||||||
if err := Copy(scenarioSource, hubDirScenarioPath); err != nil {
|
if err := Copy(sourcePath, hubDirScenarioPath); err != nil {
|
||||||
return fmt.Errorf("unable to copy '%s' to '%s': %s", scenarioSource, hubDirScenarioPath, err)
|
return fmt.Errorf("unable to copy '%s' to '%s': %w", sourcePath, hubDirScenarioPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runtime/scenarios/ssh-bf.yaml
|
// runtime/scenarios/ssh-bf.yaml
|
||||||
scenarioDirParserPath := filepath.Join(scenarioDirDest, scenarioFileName)
|
scenarioDirParserPath := filepath.Join(itemTypeDirDest, sourceFilename)
|
||||||
if err := os.Symlink(hubDirScenarioPath, scenarioDirParserPath); err != nil {
|
if err := os.Symlink(hubDirScenarioPath, scenarioDirParserPath); err != nil {
|
||||||
if !os.IsExist(err) {
|
if !os.IsExist(err) {
|
||||||
return fmt.Errorf("unable to symlink scenario '%s' to '%s': %s", hubDirScenarioPath, scenarioDirParserPath, err)
|
return fmt.Errorf("unable to symlink scenario '%s' to '%s': %w", hubDirScenarioPath, scenarioDirParserPath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HubTestItem) installScenarioCustom(scenario string) error {
|
func (t *HubTestItem) installScenarioCustomFrom(scenario string, customPath string) (bool, error) {
|
||||||
customScenarioExist := false
|
|
||||||
for _, customPath := range t.CustomItemsLocation {
|
|
||||||
// we check if its a custom scenario
|
// we check if its a custom scenario
|
||||||
customScenarioPath := filepath.Join(customPath, scenario)
|
customScenarioPath := filepath.Join(customPath, scenario)
|
||||||
if _, err := os.Stat(customScenarioPath); os.IsNotExist(err) {
|
if _, err := os.Stat(customScenarioPath); os.IsNotExist(err) {
|
||||||
continue
|
return false, nil
|
||||||
//return fmt.Errorf("scenarios '%s' doesn't exist in the hub and doesn't appear to be a custom one.", scenario)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scenarioDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
|
itemTypeDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
|
||||||
if err := os.MkdirAll(scenarioDirDest, os.ModePerm); err != nil {
|
if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("unable to create folder '%s': %s", scenarioDirDest, err)
|
return false, fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
scenarioFileName := filepath.Base(customScenarioPath)
|
scenarioFileName := filepath.Base(customScenarioPath)
|
||||||
scenarioFileDest := filepath.Join(scenarioDirDest, scenarioFileName)
|
|
||||||
|
scenarioFileDest := filepath.Join(itemTypeDirDest, scenarioFileName)
|
||||||
if err := Copy(customScenarioPath, scenarioFileDest); err != nil {
|
if err := Copy(customScenarioPath, scenarioFileDest); err != nil {
|
||||||
continue
|
return false, fmt.Errorf("unable to copy scenario from '%s' to '%s': %w", customScenarioPath, scenarioFileDest, err)
|
||||||
//return fmt.Errorf("unable to copy scenario from '%s' to '%s': %s", customScenarioPath, scenarioFileDest, err)
|
|
||||||
}
|
|
||||||
customScenarioExist = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if !customScenarioExist {
|
|
||||||
return fmt.Errorf("couldn't find custom scenario '%s' in the following location: %+v", scenario, t.CustomItemsLocation)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *HubTestItem) installScenarioCustom(scenario string) error {
|
||||||
|
for _, customPath := range t.CustomItemsLocation {
|
||||||
|
found, err := t.installScenarioCustomFrom(scenario, customPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if found {
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("couldn't find custom scenario '%s' in the following location: %+v", scenario, t.CustomItemsLocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HubTestItem) installScenario(name string) error {
|
func (t *HubTestItem) installScenario(name string) error {
|
||||||
|
|
Loading…
Reference in a new issue