pkg/hubtest: extract methods + consistent error handling
This commit is contained in:
parent
45571cea08
commit
c4e86c34b2
4 changed files with 137 additions and 118 deletions
|
@ -50,36 +50,44 @@ func (t *HubTestItem) installAppsecRuleItem(hubAppsecRule *cwhub.Item) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (t *HubTestItem) installAppsecRuleCustomFrom(appsecrule string, customPath string) (bool, error) {
|
||||
// we check if its a custom appsec-rule
|
||||
customAppsecRulePath := filepath.Join(customPath, appsecrule)
|
||||
if _, err := os.Stat(customAppsecRulePath); os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
customAppsecRulePathSplit := strings.Split(customAppsecRulePath, "/")
|
||||
customAppsecRuleName := customAppsecRulePathSplit[len(customAppsecRulePathSplit)-1]
|
||||
|
||||
appsecRuleDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
|
||||
if err := os.MkdirAll(appsecRuleDirDest, os.ModePerm); err != nil {
|
||||
return false, fmt.Errorf("unable to create folder '%s': %s", appsecRuleDirDest, err)
|
||||
}
|
||||
|
||||
// runtime/appsec-rules/
|
||||
customAppsecRuleDest := fmt.Sprintf("%s/appsec-rules/%s", t.RuntimePath, customAppsecRuleName)
|
||||
// if path to postoverflow exist, copy it
|
||||
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 true, nil
|
||||
}
|
||||
|
||||
|
||||
func (t *HubTestItem) installAppsecRuleCustom(appsecrule string) error {
|
||||
customAppsecRuleExist := false
|
||||
for _, customPath := range t.CustomItemsLocation {
|
||||
// we check if its a custom appsec-rule
|
||||
customAppsecRulePath := filepath.Join(customPath, appsecrule)
|
||||
if _, err := os.Stat(customAppsecRulePath); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
customAppsecRulePathSplit := strings.Split(customAppsecRulePath, "/")
|
||||
customAppsecRuleName := customAppsecRulePathSplit[len(customAppsecRulePathSplit)-1]
|
||||
|
||||
appsecRuleDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
|
||||
if err := os.MkdirAll(appsecRuleDirDest, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("unable to create folder '%s': %s", appsecRuleDirDest, err)
|
||||
found, err := t.installAppsecRuleCustomFrom(appsecrule, customPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// runtime/appsec-rules/
|
||||
customAppsecRuleDest := fmt.Sprintf("%s/appsec-rules/%s", t.RuntimePath, customAppsecRuleName)
|
||||
// if path to postoverflow exist, copy it
|
||||
if err := Copy(customAppsecRulePath, customAppsecRuleDest); err != nil {
|
||||
continue
|
||||
if found {
|
||||
return nil
|
||||
}
|
||||
customAppsecRuleExist = true
|
||||
break
|
||||
}
|
||||
if !customAppsecRuleExist {
|
||||
return fmt.Errorf("couldn't find custom appsec-rule '%s' in the following location: %+v", appsecrule, t.CustomItemsLocation)
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
@ -48,50 +48,52 @@ func (t *HubTestItem) installParserItem(hubParser *cwhub.Item) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
customParserPathSplit, customParserName := filepath.Split(customParserPath)
|
||||
// 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]
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
parserDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, customParserStage)
|
||||
if err := os.MkdirAll(parserDirDest, os.ModePerm); err != nil {
|
||||
return false, fmt.Errorf("unable to create folder '%s': %s", parserDirDest, err)
|
||||
}
|
||||
|
||||
customParserDest := filepath.Join(parserDirDest, 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': %s", customParserPath, customParserDest, err)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (t *HubTestItem) installParserCustom(parser string) error {
|
||||
customParserExist := false
|
||||
for _, customPath := range t.CustomItemsLocation {
|
||||
// we check if its a custom parser
|
||||
customParserPath := filepath.Join(customPath, parser)
|
||||
if _, err := os.Stat(customParserPath); os.IsNotExist(err) {
|
||||
continue
|
||||
//return fmt.Errorf("parser '%s' doesn't exist in the hub and doesn't appear to be a custom one.", parser)
|
||||
found, err := t.installParserCustomFrom(parser, customPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
customParserPathSplit, customParserName := filepath.Split(customParserPath)
|
||||
// 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]
|
||||
|
||||
// check if stage exist
|
||||
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("parsers/%s", customParserStage))
|
||||
|
||||
if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
|
||||
continue
|
||||
//return fmt.Errorf("stage '%s' extracted from '%s' doesn't exist in the hub", customParserStage, hubStagePath)
|
||||
if found {
|
||||
return nil
|
||||
}
|
||||
|
||||
parserDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, customParserStage)
|
||||
if err := os.MkdirAll(parserDirDest, os.ModePerm); err != nil {
|
||||
continue
|
||||
//return fmt.Errorf("unable to create folder '%s': %s", parserDirDest, err)
|
||||
}
|
||||
|
||||
customParserDest := filepath.Join(parserDirDest, customParserName)
|
||||
// if path to parser exist, copy it
|
||||
if err := Copy(customParserPath, customParserDest); err != nil {
|
||||
continue
|
||||
//return fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customParserPath, customParserDest, err)
|
||||
}
|
||||
|
||||
customParserExist = true
|
||||
break
|
||||
}
|
||||
if !customParserExist {
|
||||
return fmt.Errorf("couldn't find custom parser '%s' in the following location: %+v", parser, t.CustomItemsLocation)
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
@ -48,49 +48,53 @@ func (t *HubTestItem) installPostoverflowItem(hubPostOverflow *cwhub.Item) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func (t *HubTestItem) installPostoverflowCustomFrom(postoverflow string, customPath string) (bool, error) {
|
||||
// we check if its a custom postoverflow
|
||||
customPostOverflowPath := filepath.Join(customPath, postoverflow)
|
||||
if _, err := os.Stat(customPostOverflowPath); os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
customPostOverflowPathSplit := strings.Split(customPostOverflowPath, "/")
|
||||
customPostoverflowName := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-1]
|
||||
// because path is postoverflows/<stage>/<author>/parser.yaml and we wan't the stage
|
||||
customPostoverflowStage := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-3]
|
||||
|
||||
// check if stage exist
|
||||
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("postoverflows/%s", customPostoverflowStage))
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
|
||||
if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
|
||||
return false, fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
|
||||
}
|
||||
|
||||
customPostoverflowDest := filepath.Join(postoverflowDirDest, customPostoverflowName)
|
||||
// if path to postoverflow exist, copy it
|
||||
if err := Copy(customPostOverflowPath, customPostoverflowDest); err != nil {
|
||||
return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customPostOverflowPath, customPostoverflowDest, err)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
|
||||
func (t *HubTestItem) installPostoverflowCustom(postoverflow string) error {
|
||||
customPostoverflowExist := false
|
||||
for _, customPath := range t.CustomItemsLocation {
|
||||
// we check if its a custom postoverflow
|
||||
customPostOverflowPath := filepath.Join(customPath, postoverflow)
|
||||
if _, err := os.Stat(customPostOverflowPath); os.IsNotExist(err) {
|
||||
continue
|
||||
//return fmt.Errorf("postoverflow '%s' doesn't exist in the hub and doesn't appear to be a custom one.", postoverflow)
|
||||
found, err := t.installPostoverflowCustomFrom(postoverflow, customPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
customPostOverflowPathSplit := strings.Split(customPostOverflowPath, "/")
|
||||
customPostoverflowName := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-1]
|
||||
// because path is postoverflows/<stage>/<author>/parser.yaml and we wan't the stage
|
||||
customPostoverflowStage := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-3]
|
||||
|
||||
// check if stage exist
|
||||
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("postoverflows/%s", customPostoverflowStage))
|
||||
|
||||
if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
|
||||
continue
|
||||
//return fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
|
||||
if found {
|
||||
return nil
|
||||
}
|
||||
|
||||
postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
|
||||
if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
|
||||
continue
|
||||
//return fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
|
||||
}
|
||||
|
||||
customPostoverflowDest := filepath.Join(postoverflowDirDest, customPostoverflowName)
|
||||
// if path to postoverflow exist, copy it
|
||||
if err := Copy(customPostOverflowPath, customPostoverflowDest); err != nil {
|
||||
continue
|
||||
//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 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 {
|
||||
|
|
|
@ -47,35 +47,40 @@ func (t *HubTestItem) installScenarioItem(hubScenario *cwhub.Item) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
scenarioDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
|
||||
if err := os.MkdirAll(scenarioDirDest, os.ModePerm); err != nil {
|
||||
return false, fmt.Errorf("unable to create folder '%s': %s", scenarioDirDest, err)
|
||||
}
|
||||
|
||||
scenarioFileName := filepath.Base(customScenarioPath)
|
||||
scenarioFileDest := filepath.Join(scenarioDirDest, scenarioFileName)
|
||||
if err := Copy(customScenarioPath, scenarioFileDest); err != nil {
|
||||
return false, fmt.Errorf("unable to copy scenario from '%s' to '%s': %s", customScenarioPath, scenarioFileDest, err)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (t *HubTestItem) installScenarioCustom(scenario string) error {
|
||||
customScenarioExist := false
|
||||
for _, customPath := range t.CustomItemsLocation {
|
||||
// we check if its a custom scenario
|
||||
customScenarioPath := filepath.Join(customPath, scenario)
|
||||
if _, err := os.Stat(customScenarioPath); os.IsNotExist(err) {
|
||||
continue
|
||||
//return fmt.Errorf("scenarios '%s' doesn't exist in the hub and doesn't appear to be a custom one.", scenario)
|
||||
found, err := t.installScenarioCustomFrom(scenario, customPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
scenarioDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
|
||||
if err := os.MkdirAll(scenarioDirDest, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("unable to create folder '%s': %s", scenarioDirDest, err)
|
||||
if found {
|
||||
return nil
|
||||
}
|
||||
|
||||
scenarioFileName := filepath.Base(customScenarioPath)
|
||||
scenarioFileDest := filepath.Join(scenarioDirDest, scenarioFileName)
|
||||
if err := Copy(customScenarioPath, scenarioFileDest); err != nil {
|
||||
continue
|
||||
//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 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 {
|
||||
|
|
Loading…
Add table
Reference in a new issue