github.com/crowdsecurity/crowdsec@v1.6.1/pkg/appsec/loader.go (about)

     1  package appsec
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/crowdsecurity/crowdsec/pkg/cwhub"
     7  	log "github.com/sirupsen/logrus"
     8  	"gopkg.in/yaml.v2"
     9  )
    10  
    11  var appsecRules map[string]AppsecCollectionConfig = make(map[string]AppsecCollectionConfig) //FIXME: would probably be better to have a struct for this
    12  
    13  var hub *cwhub.Hub //FIXME: this is a temporary hack to make the hub available in the package
    14  
    15  func LoadAppsecRules(hubInstance *cwhub.Hub) error {
    16  
    17  	hub = hubInstance
    18  	appsecRules = make(map[string]AppsecCollectionConfig)
    19  
    20  	for _, hubAppsecRuleItem := range hub.GetItemMap(cwhub.APPSEC_RULES) {
    21  		if !hubAppsecRuleItem.State.Installed {
    22  			continue
    23  		}
    24  
    25  		content, err := os.ReadFile(hubAppsecRuleItem.State.LocalPath)
    26  
    27  		if err != nil {
    28  			log.Warnf("unable to read file %s : %s", hubAppsecRuleItem.State.LocalPath, err)
    29  			continue
    30  		}
    31  
    32  		var rule AppsecCollectionConfig
    33  
    34  		err = yaml.UnmarshalStrict(content, &rule)
    35  
    36  		if err != nil {
    37  			log.Warnf("unable to unmarshal file %s : %s", hubAppsecRuleItem.State.LocalPath, err)
    38  			continue
    39  		}
    40  
    41  		rule.hash = hubAppsecRuleItem.State.LocalHash
    42  		rule.version = hubAppsecRuleItem.Version
    43  
    44  		log.Infof("Adding %s to appsec rules", rule.Name)
    45  
    46  		appsecRules[rule.Name] = rule
    47  	}
    48  
    49  	if len(appsecRules) == 0 {
    50  		log.Debugf("No appsec rules found")
    51  	}
    52  	return nil
    53  }