marco 1 年間 前
コミット
8f4d94b7f9
4 ファイル変更31 行追加34 行削除
  1. 8 9
      pkg/hubtest/appsecrule.go
  2. 9 10
      pkg/hubtest/parser.go
  3. 7 9
      pkg/hubtest/postoverflow.go
  4. 7 6
      pkg/hubtest/scenario.go

+ 8 - 9
pkg/hubtest/appsecrule.go

@@ -14,7 +14,7 @@ import (
 func (t *HubTestItem) installAppsecRuleItem(hubAppsecRule *cwhub.Item) error {
 func (t *HubTestItem) installAppsecRuleItem(hubAppsecRule *cwhub.Item) error {
 	appsecRuleSource, err := filepath.Abs(filepath.Join(t.HubPath, hubAppsecRule.RemotePath))
 	appsecRuleSource, err := filepath.Abs(filepath.Join(t.HubPath, hubAppsecRule.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", appsecRuleSource, err)
 	}
 	}
 
 
 	appsecRuleFilename := filepath.Base(appsecRuleSource)
 	appsecRuleFilename := filepath.Base(appsecRuleSource)
@@ -26,24 +26,24 @@ func (t *HubTestItem) installAppsecRuleItem(hubAppsecRule *cwhub.Item) error {
 	appsecRuleDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
 	appsecRuleDirDest := 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(appsecRuleDirDest, os.ModePerm); err != nil {
-		return fmt.Errorf("unable to create folder '%s': %s", appsecRuleDirDest, err)
+		return fmt.Errorf("unable to create folder '%s': %w", appsecRuleDirDest, err)
 	}
 	}
 
 
 	// runtime/hub/appsec-rules/crowdsecurity/rule.yaml
 	// runtime/hub/appsec-rules/crowdsecurity/rule.yaml
 	hubDirAppsecRulePath := filepath.Join(appsecRuleDirDest, appsecRuleFilename)
 	hubDirAppsecRulePath := filepath.Join(appsecRuleDirDest, appsecRuleFilename)
 	if err := Copy(appsecRuleSource, hubDirAppsecRulePath); err != nil {
 	if err := Copy(appsecRuleSource, 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", appsecRuleSource, hubDirAppsecRulePath, err)
 	}
 	}
 
 
 	// runtime/appsec-rules/rule.yaml
 	// runtime/appsec-rules/rule.yaml
 	appsecRulePath := filepath.Join(appsecRuleDirDest, appsecRuleFilename)
 	appsecRulePath := filepath.Join(appsecRuleDirDest, appsecRuleFilename)
 	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)
 		}
 		}
 	}
 	}
 
 
@@ -56,25 +56,24 @@ func (t *HubTestItem) installAppsecRuleCustomFrom(appsecrule string, customPath
 	if _, err := os.Stat(customAppsecRulePath); os.IsNotExist(err) {
 	if _, err := os.Stat(customAppsecRulePath); os.IsNotExist(err) {
 		return false, nil
 		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)
 	appsecRuleDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
 	if err := os.MkdirAll(appsecRuleDirDest, os.ModePerm); err != nil {
 	if err := os.MkdirAll(appsecRuleDirDest, os.ModePerm); err != nil {
-		return false, fmt.Errorf("unable to create folder '%s': %s", appsecRuleDirDest, err)
+		return false, fmt.Errorf("unable to create folder '%s': %w", appsecRuleDirDest, 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 path to postoverflow exist, copy it
 	if err := Copy(customAppsecRulePath, customAppsecRuleDest); err != nil {
 	if err := Copy(customAppsecRulePath, customAppsecRuleDest); err != nil {
-		return false, fmt.Errorf("unable to copy appsec-rule from '%s' to '%s': %s", customAppsecRulePath, customAppsecRuleDest, err)
+		return false, fmt.Errorf("unable to copy appsec-rule from '%s' to '%s': %w", customAppsecRulePath, customAppsecRuleDest, err)
 	}
 	}
 
 
 	return true, nil
 	return true, nil
 }
 }
 
 
-
 func (t *HubTestItem) installAppsecRuleCustom(appsecrule string) error {
 func (t *HubTestItem) installAppsecRuleCustom(appsecrule string) error {
 	for _, customPath := range t.CustomItemsLocation {
 	for _, customPath := range t.CustomItemsLocation {
 		found, err := t.installAppsecRuleCustomFrom(appsecrule, customPath)
 		found, err := t.installAppsecRuleCustomFrom(appsecrule, customPath)

+ 9 - 10
pkg/hubtest/parser.go

@@ -12,7 +12,7 @@ import (
 func (t *HubTestItem) installParserItem(hubParser *cwhub.Item) error {
 func (t *HubTestItem) installParserItem(hubParser *cwhub.Item) error {
 	parserSource, err := filepath.Abs(filepath.Join(t.HubPath, hubParser.RemotePath))
 	parserSource, err := filepath.Abs(filepath.Join(t.HubPath, hubParser.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", parserSource, err)
 	}
 	}
 
 
 	parserFileName := filepath.Base(parserSource)
 	parserFileName := filepath.Base(parserSource)
@@ -24,24 +24,24 @@ func (t *HubTestItem) installParserItem(hubParser *cwhub.Item) error {
 	parserDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, hubParser.Stage)
 	parserDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, hubParser.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(parserDirDest, os.ModePerm); err != nil {
-		return fmt.Errorf("unable to create folder '%s': %s", parserDirDest, err)
+		return fmt.Errorf("unable to create folder '%s': %w", parserDirDest, 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, parserFileName)
 	if err := Copy(parserSource, hubDirParserPath); err != nil {
 	if err := Copy(parserSource, 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", parserSource, hubDirParserPath, err)
 	}
 	}
 
 
 	// runtime/parsers/s00-raw/syslog-logs.yaml
 	// runtime/parsers/s00-raw/syslog-logs.yaml
 	parserDirParserPath := filepath.Join(parserDirDest, parserFileName)
 	parserDirParserPath := filepath.Join(parserDirDest, parserFileName)
 	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)
 		}
 		}
 	}
 	}
 
 
@@ -57,25 +57,24 @@ func (t *HubTestItem) installParserCustomFrom(parser string, customPath string)
 
 
 	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))
-	customParserStage := splittedPath[len(splittedPath)-3]
+	splitPath := strings.Split(customParserPathSplit, string(os.PathSeparator))
+	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) {
 		return false, fmt.Errorf("stage '%s' extracted from '%s' doesn't exist in the hub", customParserStage, hubStagePath)
 		return false, fmt.Errorf("stage '%s' extracted from '%s' doesn't exist in the hub", customParserStage, hubStagePath)
 	}
 	}
 
 
 	parserDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, customParserStage)
 	parserDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, customParserStage)
 	if err := os.MkdirAll(parserDirDest, os.ModePerm); err != nil {
 	if err := os.MkdirAll(parserDirDest, os.ModePerm); err != nil {
-		return false, fmt.Errorf("unable to create folder '%s': %s", parserDirDest, err)
+		return false, fmt.Errorf("unable to create folder '%s': %w", parserDirDest, err)
 	}
 	}
 
 
 	customParserDest := filepath.Join(parserDirDest, customParserName)
 	customParserDest := filepath.Join(parserDirDest, 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 {
-		return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customParserPath, customParserDest, err)
+		return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %w", customParserPath, customParserDest, err)
 	}
 	}
 
 
 	return true, nil
 	return true, nil

+ 7 - 9
pkg/hubtest/postoverflow.go

@@ -12,7 +12,7 @@ import (
 func (t *HubTestItem) installPostoverflowItem(hubPostOverflow *cwhub.Item) error {
 func (t *HubTestItem) installPostoverflowItem(hubPostOverflow *cwhub.Item) error {
 	postoverflowSource, err := filepath.Abs(filepath.Join(t.HubPath, hubPostOverflow.RemotePath))
 	postoverflowSource, err := filepath.Abs(filepath.Join(t.HubPath, hubPostOverflow.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", postoverflowSource, err)
 	}
 	}
 
 
 	postoverflowFileName := filepath.Base(postoverflowSource)
 	postoverflowFileName := filepath.Base(postoverflowSource)
@@ -24,24 +24,24 @@ func (t *HubTestItem) installPostoverflowItem(hubPostOverflow *cwhub.Item) error
 	postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, hubPostOverflow.Stage)
 	postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, hubPostOverflow.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(postoverflowDirDest, os.ModePerm); err != nil {
-		return fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
+		return fmt.Errorf("unable to create folder '%s': %w", postoverflowDirDest, 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, postoverflowFileName)
 	if err := Copy(postoverflowSource, hubDirPostoverflowPath); err != nil {
 	if err := Copy(postoverflowSource, 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", postoverflowSource, hubDirPostoverflowPath, err)
 	}
 	}
 
 
 	// runtime/postoverflows/s00-enrich/rdns.yaml
 	// runtime/postoverflows/s00-enrich/rdns.yaml
 	postoverflowDirParserPath := filepath.Join(postoverflowDirDest, postoverflowFileName)
 	postoverflowDirParserPath := filepath.Join(postoverflowDirDest, postoverflowFileName)
 	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)
 		}
 		}
 	}
 	}
 
 
@@ -62,26 +62,24 @@ func (t *HubTestItem) installPostoverflowCustomFrom(postoverflow string, customP
 
 
 	// 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) {
 		return false, fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
 		return false, fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
 	}
 	}
 
 
 	postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
 	postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
 	if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
 	if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
-		return false, fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
+		return false, fmt.Errorf("unable to create folder '%s': %w", postoverflowDirDest, err)
 	}
 	}
 
 
 	customPostoverflowDest := filepath.Join(postoverflowDirDest, customPostoverflowName)
 	customPostoverflowDest := filepath.Join(postoverflowDirDest, 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 {
-		return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customPostOverflowPath, customPostoverflowDest, err)
+		return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %w", customPostOverflowPath, customPostoverflowDest, err)
 	}
 	}
 
 
 	return true, nil
 	return true, nil
 }
 }
 
 
-
 func (t *HubTestItem) installPostoverflowCustom(postoverflow string) error {
 func (t *HubTestItem) installPostoverflowCustom(postoverflow string) error {
 	for _, customPath := range t.CustomItemsLocation {
 	for _, customPath := range t.CustomItemsLocation {
 		found, err := t.installPostoverflowCustomFrom(postoverflow, customPath)
 		found, err := t.installPostoverflowCustomFrom(postoverflow, customPath)

+ 7 - 6
pkg/hubtest/scenario.go

@@ -23,24 +23,24 @@ func (t *HubTestItem) installScenarioItem(hubScenario *cwhub.Item) error {
 	scenarioDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
 	scenarioDirDest := 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(scenarioDirDest, os.ModePerm); err != nil {
-		return fmt.Errorf("unable to create folder '%s': %s", scenarioDirDest, err)
+		return fmt.Errorf("unable to create folder '%s': %w", scenarioDirDest, err)
 	}
 	}
 
 
 	// runtime/hub/scenarios/crowdsecurity/ssh-bf.yaml
 	// runtime/hub/scenarios/crowdsecurity/ssh-bf.yaml
 	hubDirScenarioPath := filepath.Join(hubDirScenarioDest, scenarioFileName)
 	hubDirScenarioPath := filepath.Join(hubDirScenarioDest, scenarioFileName)
 	if err := Copy(scenarioSource, hubDirScenarioPath); err != nil {
 	if err := Copy(scenarioSource, 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", scenarioSource, hubDirScenarioPath, err)
 	}
 	}
 
 
 	// runtime/scenarios/ssh-bf.yaml
 	// runtime/scenarios/ssh-bf.yaml
 	scenarioDirParserPath := filepath.Join(scenarioDirDest, scenarioFileName)
 	scenarioDirParserPath := filepath.Join(scenarioDirDest, scenarioFileName)
 	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)
 		}
 		}
 	}
 	}
 
 
@@ -56,13 +56,14 @@ func (t *HubTestItem) installScenarioCustomFrom(scenario string, customPath stri
 
 
 	scenarioDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
 	scenarioDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
 	if err := os.MkdirAll(scenarioDirDest, os.ModePerm); err != nil {
 	if err := os.MkdirAll(scenarioDirDest, os.ModePerm); err != nil {
-		return false, fmt.Errorf("unable to create folder '%s': %s", scenarioDirDest, err)
+		return false, fmt.Errorf("unable to create folder '%s': %w", scenarioDirDest, err)
 	}
 	}
 
 
 	scenarioFileName := filepath.Base(customScenarioPath)
 	scenarioFileName := filepath.Base(customScenarioPath)
+
 	scenarioFileDest := filepath.Join(scenarioDirDest, scenarioFileName)
 	scenarioFileDest := filepath.Join(scenarioDirDest, scenarioFileName)
 	if err := Copy(customScenarioPath, scenarioFileDest); err != nil {
 	if err := Copy(customScenarioPath, scenarioFileDest); err != nil {
-		return false, fmt.Errorf("unable to copy scenario from '%s' to '%s': %s", customScenarioPath, scenarioFileDest, err)
+		return false, fmt.Errorf("unable to copy scenario from '%s' to '%s': %w", customScenarioPath, scenarioFileDest, err)
 	}
 	}
 
 
 	return true, nil
 	return true, nil