fix: harden rule and forwarding edge cases
This commit is contained in:
@@ -25,14 +25,17 @@ func validateSyslogRule(rule *models.SyslogRule) error {
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(rule.SeverityMappingJSON); raw != "" {
|
||||
var mapping map[string]string
|
||||
var mapping map[string]any
|
||||
if err := json.Unmarshal([]byte(raw), &mapping); err != nil {
|
||||
return fmt.Errorf("severity_mapping_json 必须是字符串到字符串的 JSON 对象:%v;请修正 severity_mapping_json 后重试", err)
|
||||
}
|
||||
if mapping == nil {
|
||||
return fmt.Errorf("severity_mapping_json 必须是字符串到字符串的 JSON 对象,不能是 null;请改为 JSON 对象后重试")
|
||||
}
|
||||
for pattern := range mapping {
|
||||
for pattern, severity := range mapping {
|
||||
if _, ok := severity.(string); !ok {
|
||||
return fmt.Errorf("severity_mapping_json 的映射键 %q 对应值必须是字符串;请修正该映射值后重试", pattern)
|
||||
}
|
||||
if _, err := regexp.Compile(pattern); err != nil {
|
||||
return fmt.Errorf("severity_mapping_json 的映射键 %q 不是有效正则表达式:%v;请修正该映射键后重试", pattern, err)
|
||||
}
|
||||
@@ -49,8 +52,10 @@ func validateSyslogRule(rule *models.SyslogRule) error {
|
||||
}
|
||||
|
||||
func validateTrapRule(rule *models.TrapRule) error {
|
||||
if err := validateOptionalRegex("varbind_match_regex", rule.VarbindMatchRegex); err != nil {
|
||||
return err
|
||||
if strings.TrimSpace(rule.VarbindMatchRegex) != "" {
|
||||
if _, err := regexp.Compile(rule.VarbindMatchRegex); err != nil {
|
||||
return fmt.Errorf("varbind_match_regex 不是有效正则表达式:%v;请修正 varbind_match_regex 后重试", err)
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(rule.OIDPrefix) == "" && strings.TrimSpace(rule.VarbindMatchRegex) == "" {
|
||||
return fmt.Errorf("Trap 规则的匹配条件全部为空,运行时永远不会命中;请至少填写 oid_prefix、varbind_match_regex 中的一项")
|
||||
|
||||
Reference in New Issue
Block a user