2020-05-15 09:39:16 +00:00
|
|
|
package leakybucket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HiddenKey struct {
|
|
|
|
key string
|
|
|
|
expiration time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type Blackhole struct {
|
|
|
|
duration time.Duration
|
|
|
|
hiddenKeys []HiddenKey
|
|
|
|
DumbProcessor
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func NewBlackhole(bucketFactory *BucketFactory) (*Blackhole, error) {
|
2022-02-01 21:08:06 +00:00
|
|
|
duration, err := time.ParseDuration(bucketFactory.Blackhole)
|
|
|
|
if err != nil {
|
2020-11-30 09:37:17 +00:00
|
|
|
bucketFactory.logger.Warning("Blackhole duration not valid, using 1h")
|
|
|
|
return nil, fmt.Errorf("blackhole duration not valid '%s'", bucketFactory.Blackhole)
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
|
|
|
return &Blackhole{
|
|
|
|
duration: duration,
|
|
|
|
hiddenKeys: []HiddenKey{},
|
|
|
|
DumbProcessor: DumbProcessor{},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-11-24 10:10:54 +00:00
|
|
|
func (bl *Blackhole) OnBucketOverflow(bucketFactory *BucketFactory) func(*Leaky, types.RuntimeAlert, *types.Queue) (types.RuntimeAlert, *types.Queue) {
|
|
|
|
return func(leaky *Leaky, alert types.RuntimeAlert, queue *types.Queue) (types.RuntimeAlert, *types.Queue) {
|
2023-03-09 10:56:02 +00:00
|
|
|
var blackholed = false
|
2020-05-15 09:39:16 +00:00
|
|
|
var tmp []HiddenKey
|
|
|
|
// search if we are blackholed and refresh the slice
|
|
|
|
for _, element := range bl.hiddenKeys {
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
if element.key == leaky.Mapkey {
|
|
|
|
if element.expiration.After(leaky.Ovflw_ts) {
|
|
|
|
leaky.logger.Debugf("Overflow discarded, still blackholed for %s", element.expiration.Sub(leaky.Ovflw_ts))
|
2020-05-15 09:39:16 +00:00
|
|
|
blackholed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
if element.expiration.After(leaky.Ovflw_ts) {
|
2020-05-15 09:39:16 +00:00
|
|
|
tmp = append(tmp, element)
|
|
|
|
} else {
|
2020-11-30 09:37:17 +00:00
|
|
|
leaky.logger.Debugf("%s left blackhole %s ago", element.key, leaky.Ovflw_ts.Sub(element.expiration))
|
2020-05-15 09:39:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bl.hiddenKeys = tmp
|
|
|
|
|
|
|
|
if blackholed {
|
2020-11-30 09:37:17 +00:00
|
|
|
leaky.logger.Tracef("Event is blackholed (%s)", leaky.First_ts)
|
|
|
|
return types.RuntimeAlert{
|
|
|
|
Mapkey: leaky.Mapkey,
|
2020-05-15 09:39:16 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
bl.hiddenKeys = append(bl.hiddenKeys, HiddenKey{leaky.Mapkey, leaky.Ovflw_ts.Add(bl.duration)})
|
|
|
|
leaky.logger.Debugf("Adding overflow to blackhole (%s)", leaky.First_ts)
|
|
|
|
return alert, queue
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|