Compare commits

...

4 commits

Author SHA1 Message Date
marco
82d3ba61e4 comment + drop var declaration + explicit zero return 2024-04-29 15:44:00 +02:00
marco
dbec5b7c9c drop redundant else 2024-04-29 15:41:25 +02:00
marco
64b793c3da if/else -> switch 2024-04-29 15:26:49 +02:00
marco
26faf6fb13 simplify a couple loops 2024-04-29 15:22:38 +02:00
5 changed files with 35 additions and 38 deletions

View file

@ -91,10 +91,7 @@ func truncate(values []string, contextValueLen int) (string, error) {
return "", fmt.Errorf("unable to dump metas: %s", err) return "", fmt.Errorf("unable to dump metas: %s", err)
} }
ret = string(valueByte) ret = string(valueByte)
for { for len(ret) > contextValueLen {
if len(ret) <= contextValueLen {
break
}
// if there is only 1 value left and that the size is too big, truncate it // if there is only 1 value left and that the size is too big, truncate it
if len(values) == 1 { if len(values) == 1 {
valueToTruncate := values[0] valueToTruncate := values[0]

View file

@ -911,12 +911,17 @@ func (a *apic) UpdateBlocklists(links *modelscapi.GetDecisionsStreamResponseLink
} }
func setAlertScenario(alert *models.Alert, addCounters map[string]map[string]int, deleteCounters map[string]map[string]int) { func setAlertScenario(alert *models.Alert, addCounters map[string]map[string]int, deleteCounters map[string]map[string]int) {
if *alert.Source.Scope == types.CAPIOrigin { switch *alert.Source.Scope {
case types.CAPIOrigin:
*alert.Source.Scope = types.CommunityBlocklistPullSourceScope *alert.Source.Scope = types.CommunityBlocklistPullSourceScope
alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs", addCounters[types.CAPIOrigin]["all"], deleteCounters[types.CAPIOrigin]["all"])) alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs",
} else if *alert.Source.Scope == types.ListOrigin { addCounters[types.CAPIOrigin]["all"],
deleteCounters[types.CAPIOrigin]["all"]))
case types.ListOrigin:
*alert.Source.Scope = fmt.Sprintf("%s:%s", types.ListOrigin, *alert.Scenario) *alert.Source.Scope = fmt.Sprintf("%s:%s", types.ListOrigin, *alert.Scenario)
alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs", addCounters[types.ListOrigin][*alert.Scenario], deleteCounters[types.ListOrigin][*alert.Scenario])) alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs",
addCounters[types.ListOrigin][*alert.Scenario],
deleteCounters[types.ListOrigin][*alert.Scenario]))
} }
} }
@ -988,11 +993,12 @@ func makeAddAndDeleteCounters() (map[string]map[string]int, map[string]map[strin
} }
func updateCounterForDecision(counter map[string]map[string]int, origin *string, scenario *string, totalDecisions int) { func updateCounterForDecision(counter map[string]map[string]int, origin *string, scenario *string, totalDecisions int) {
if *origin == types.CAPIOrigin { switch *origin {
case types.CAPIOrigin:
counter[*origin]["all"] += totalDecisions counter[*origin]["all"] += totalDecisions
} else if *origin == types.ListOrigin { case types.ListOrigin:
counter[*origin][*scenario] += totalDecisions counter[*origin][*scenario] += totalDecisions
} else { default:
log.Warningf("Unknown origin %s", *origin) log.Warningf("Unknown origin %s", *origin)
} }
} }

View file

@ -49,20 +49,17 @@ func LastAddress(n *net.IPNet) net.IP {
ip[3]|^n.Mask[3]) ip[3]|^n.Mask[3])
} }
// GetIpsFromIpRange takes a CIDR range and returns the start and end IP
func GetIpsFromIpRange(host string) (int64, int64, error) { func GetIpsFromIpRange(host string) (int64, int64, error) {
var ipStart int64 _, parsedRange, err := net.ParseCIDR(host)
var ipEnd int64 if err != nil {
var err error return 0, 0, fmt.Errorf("'%s' is not a valid CIDR", host)
var parsedRange *net.IPNet
if _, parsedRange, err = net.ParseCIDR(host); err != nil {
return ipStart, ipEnd, fmt.Errorf("'%s' is not a valid CIDR", host)
} }
if parsedRange == nil { if parsedRange == nil {
return ipStart, ipEnd, fmt.Errorf("unable to parse network : %s", err) return 0, 0, fmt.Errorf("unable to parse network : %s", err)
} }
ipStart = int64(IP2Int(parsedRange.IP)) ipStart := int64(IP2Int(parsedRange.IP))
ipEnd = int64(IP2Int(LastAddress(parsedRange))) ipEnd := int64(IP2Int(LastAddress(parsedRange)))
return ipStart, ipEnd, nil return ipStart, ipEnd, nil
} }

View file

@ -380,19 +380,17 @@ func LoadBucket(bucketFactory *BucketFactory, tomb *tomb.Tomb) error {
bucketFactory.processors = append(bucketFactory.processors, &BayesianBucket{}) bucketFactory.processors = append(bucketFactory.processors, &BayesianBucket{})
} }
if len(bucketFactory.Data) > 0 { for _, data := range bucketFactory.Data {
for _, data := range bucketFactory.Data { if data.DestPath == "" {
if data.DestPath == "" { bucketFactory.logger.Errorf("no dest_file provided for '%s'", bucketFactory.Name)
bucketFactory.logger.Errorf("no dest_file provided for '%s'", bucketFactory.Name) continue
continue }
} err = exprhelpers.FileInit(bucketFactory.DataDir, data.DestPath, data.Type)
err = exprhelpers.FileInit(bucketFactory.DataDir, data.DestPath, data.Type) if err != nil {
if err != nil { bucketFactory.logger.Errorf("unable to init data for file '%s': %s", data.DestPath, err)
bucketFactory.logger.Errorf("unable to init data for file '%s': %s", data.DestPath, err) }
} if data.Type == "regexp" { //cache only makes sense for regexp
if data.Type == "regexp" { //cache only makes sense for regexp exprhelpers.RegexpCacheInit(data.DestPath, *data)
exprhelpers.RegexpCacheInit(data.DestPath, *data)
}
} }
} }

View file

@ -97,13 +97,12 @@ func IP2Ints(pip net.IP) (int, int64, int64, error) {
if pip4 != nil { if pip4 != nil {
ip_nw32 := binary.BigEndian.Uint32(pip4) ip_nw32 := binary.BigEndian.Uint32(pip4)
return 4, uint2int(uint64(ip_nw32)), uint2int(ip_sfx), nil return 4, uint2int(uint64(ip_nw32)), uint2int(ip_sfx), nil
} else if pip16 != nil { }
if pip16 != nil {
ip_nw = binary.BigEndian.Uint64(pip16[0:8]) ip_nw = binary.BigEndian.Uint64(pip16[0:8])
ip_sfx = binary.BigEndian.Uint64(pip16[8:16]) ip_sfx = binary.BigEndian.Uint64(pip16[8:16])
return 16, uint2int(ip_nw), uint2int(ip_sfx), nil return 16, uint2int(ip_nw), uint2int(ip_sfx), nil
} else {
return -1, 0, 0, fmt.Errorf("unexpected len %d for %s", len(pip), pip)
} }
return -1, 0, 0, fmt.Errorf("unexpected len %d for %s", len(pip), pip)
} }