minor ruleset improvements
This commit is contained in:
@@ -66,7 +66,7 @@ func NewRulesetFromEnv() RuleSet {
|
|||||||
}
|
}
|
||||||
ruleSet, err := NewRuleset(rulesPath)
|
ruleSet, err := NewRuleset(rulesPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln(ruleSet)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
return ruleSet
|
return ruleSet
|
||||||
}
|
}
|
||||||
@@ -79,11 +79,12 @@ func NewRuleset(rulePaths string) (RuleSet, error) {
|
|||||||
errs := []error{}
|
errs := []error{}
|
||||||
|
|
||||||
rp := strings.Split(rulePaths, ";")
|
rp := strings.Split(rulePaths, ";")
|
||||||
|
var remoteRegex = regexp.MustCompile(`^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)`)
|
||||||
for _, rule := range rp {
|
for _, rule := range rp {
|
||||||
rulePath := strings.Trim(rule, " ")
|
rulePath := strings.Trim(rule, " ")
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
isRemote, _ := regexp.MatchString(`^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)`, rulePath)
|
isRemote := remoteRegex.MatchString(rulePath)
|
||||||
if isRemote {
|
if isRemote {
|
||||||
err = ruleSet.loadRulesFromRemoteFile(rulePath)
|
err = ruleSet.loadRulesFromRemoteFile(rulePath)
|
||||||
} else {
|
} else {
|
||||||
@@ -91,14 +92,14 @@ func NewRuleset(rulePaths string) (RuleSet, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e := errors.New(fmt.Sprintf("WARN: failed to load ruleset from ''%s", rulePath))
|
e := fmt.Errorf("WARN: failed to load ruleset from '%s'", rulePath)
|
||||||
errs = append(errs, errors.Join(e, err))
|
errs = append(errs, errors.Join(e, err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(errs) != 0 {
|
if len(errs) != 0 {
|
||||||
e := errors.New(fmt.Sprintf("WARN: failed to load %d rulesets", len(rp)))
|
e := fmt.Errorf("WARN: failed to load %d rulesets", len(rp))
|
||||||
errs = append(errs, e)
|
errs = append(errs, e)
|
||||||
// panic if the user specified a local ruleset, but it wasn't found on disk
|
// panic if the user specified a local ruleset, but it wasn't found on disk
|
||||||
// don't fail silently
|
// don't fail silently
|
||||||
@@ -160,14 +161,14 @@ func (rs *RuleSet) loadRulesFromLocalDir(path string) error {
|
|||||||
func (rs *RuleSet) loadRulesFromLocalFile(path string) error {
|
func (rs *RuleSet) loadRulesFromLocalFile(path string) error {
|
||||||
yamlFile, err := os.ReadFile(path)
|
yamlFile, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e := errors.New(fmt.Sprintf("failed to read rules from local file: '%s'", path))
|
e := fmt.Errorf("failed to read rules from local file: '%s'", path)
|
||||||
return errors.Join(e, err)
|
return errors.Join(e, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var r RuleSet
|
var r RuleSet
|
||||||
err = yaml.Unmarshal(yamlFile, &r)
|
err = yaml.Unmarshal(yamlFile, &r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e := errors.New(fmt.Sprintf("failed to load rules from local file, possible syntax error in '%s'", path))
|
e := fmt.Errorf("failed to load rules from local file, possible syntax error in '%s'", path)
|
||||||
ee := errors.Join(e, err)
|
ee := errors.Join(e, err)
|
||||||
if _, ok := os.LookupEnv("DEBUG"); ok {
|
if _, ok := os.LookupEnv("DEBUG"); ok {
|
||||||
debugPrintRule(string(yamlFile), ee)
|
debugPrintRule(string(yamlFile), ee)
|
||||||
@@ -185,13 +186,13 @@ func (rs *RuleSet) loadRulesFromRemoteFile(rulesUrl string) error {
|
|||||||
var r RuleSet
|
var r RuleSet
|
||||||
resp, err := http.Get(rulesUrl)
|
resp, err := http.Get(rulesUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e := errors.New(fmt.Sprintf("failed to load rules from remote url '%s'", rulesUrl))
|
e := fmt.Errorf("failed to load rules from remote url '%s'", rulesUrl)
|
||||||
return errors.Join(e, err)
|
return errors.Join(e, err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode >= 400 {
|
if resp.StatusCode >= 400 {
|
||||||
e := errors.New(fmt.Sprintf("failed to load rules from remote url (%s) on '%s'", resp.Status, rulesUrl))
|
e := fmt.Errorf("failed to load rules from remote url (%s) on '%s'", resp.Status, rulesUrl)
|
||||||
return errors.Join(e, err)
|
return errors.Join(e, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +211,7 @@ func (rs *RuleSet) loadRulesFromRemoteFile(rulesUrl string) error {
|
|||||||
err = yaml.NewDecoder(reader).Decode(&r)
|
err = yaml.NewDecoder(reader).Decode(&r)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e := errors.New(fmt.Sprintf("failed to load rules from remote url '%s' with status code '%s' and possible syntax error", rulesUrl, resp.Status))
|
e := fmt.Errorf("failed to load rules from remote url '%s' with status code '%s' and possible syntax error", rulesUrl, resp.Status)
|
||||||
ee := errors.Join(e, err)
|
ee := errors.Join(e, err)
|
||||||
return ee
|
return ee
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user