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