2024-01-18 10:09:14 +00:00
|
|
|
package hubtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
|
|
)
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
func (t *HubTestItem) installAppsecRuleItem(item *cwhub.Item) error {
|
|
|
|
sourcePath, err := filepath.Abs(filepath.Join(t.HubPath, item.RemotePath))
|
2024-01-18 10:09:14 +00:00
|
|
|
if err != nil {
|
2024-02-14 10:53:12 +00:00
|
|
|
return fmt.Errorf("can't get absolute path of '%s': %w", sourcePath, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
sourceFilename := filepath.Base(sourcePath)
|
2024-01-18 10:09:14 +00:00
|
|
|
|
|
|
|
// runtime/hub/appsec-rules/author/appsec-rule
|
2024-02-14 10:53:12 +00:00
|
|
|
hubDirAppsecRuleDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(item.RemotePath))
|
2024-01-18 10:09:14 +00:00
|
|
|
|
|
|
|
// runtime/appsec-rules/
|
2024-02-14 10:53:12 +00:00
|
|
|
itemTypeDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
|
2024-01-18 10:09:14 +00:00
|
|
|
|
|
|
|
if err := os.MkdirAll(hubDirAppsecRuleDest, os.ModePerm); err != nil {
|
2024-02-14 10:53:12 +00:00
|
|
|
return fmt.Errorf("unable to create folder '%s': %w", hubDirAppsecRuleDest, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
|
|
|
|
return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runtime/hub/appsec-rules/crowdsecurity/rule.yaml
|
2024-02-14 10:53:12 +00:00
|
|
|
hubDirAppsecRulePath := filepath.Join(itemTypeDirDest, sourceFilename)
|
|
|
|
if err := Copy(sourcePath, hubDirAppsecRulePath); err != nil {
|
|
|
|
return fmt.Errorf("unable to copy '%s' to '%s': %w", sourcePath, hubDirAppsecRulePath, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runtime/appsec-rules/rule.yaml
|
2024-02-14 10:53:12 +00:00
|
|
|
appsecRulePath := filepath.Join(itemTypeDirDest, sourceFilename)
|
2024-01-18 10:09:14 +00:00
|
|
|
if err := os.Symlink(hubDirAppsecRulePath, appsecRulePath); err != nil {
|
|
|
|
if !os.IsExist(err) {
|
2024-02-14 10:53:12 +00:00
|
|
|
return fmt.Errorf("unable to symlink appsec-rule '%s' to '%s': %w", hubDirAppsecRulePath, appsecRulePath, err)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
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]
|
|
|
|
|
|
|
|
itemTypeDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
|
|
|
|
if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
|
|
|
|
return false, fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
customAppsecRuleDest := fmt.Sprintf("%s/appsec-rules/%s", t.RuntimePath, customAppsecRuleName)
|
|
|
|
if err := Copy(customAppsecRulePath, customAppsecRuleDest); err != nil {
|
|
|
|
return false, fmt.Errorf("unable to copy appsec-rule from '%s' to '%s': %w", customAppsecRulePath, customAppsecRuleDest, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2024-01-18 10:09:14 +00:00
|
|
|
func (t *HubTestItem) installAppsecRuleCustom(appsecrule string) error {
|
|
|
|
for _, customPath := range t.CustomItemsLocation {
|
2024-02-14 10:53:12 +00:00
|
|
|
found, err := t.installAppsecRuleCustomFrom(appsecrule, customPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
if found {
|
|
|
|
return nil
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 10:53:12 +00:00
|
|
|
return fmt.Errorf("couldn't find custom appsec-rule '%s' in the following location: %+v", appsecrule, t.CustomItemsLocation)
|
2024-01-18 10:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HubTestItem) installAppsecRule(name string) error {
|
|
|
|
log.Debugf("adding rule '%s'", name)
|
|
|
|
|
|
|
|
if item := t.HubIndex.GetItem(cwhub.APPSEC_RULES, name); item != nil {
|
|
|
|
return t.installAppsecRuleItem(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.installAppsecRuleCustom(name)
|
|
|
|
}
|