MCPcopy
hub / github.com/crowdsecurity/crowdsec / ExpireDecisionsWithFilter

Method ExpireDecisionsWithFilter

pkg/database/decisions.go:241–298  ·  view source on GitHub ↗

ExpireDecisionsWithFilter updates the expiration time to now() for the decisions matching the filter, and returns the updated items

(ctx context.Context, filter map[string][]string)

Source from the content-addressed store, hash-verified

239
240// ExpireDecisionsWithFilter updates the expiration time to now() for the decisions matching the filter, and returns the updated items
241func (c *Client) ExpireDecisionsWithFilter(ctx context.Context, filter map[string][]string) (int, []*ent.Decision, error) {
242 var (
243 err error
244 rng csnet.Range
245 )
246
247 contains := true
248 // if contains is true, return bans that *contains* the given value (value is the inner)
249 // else, return bans that are *contained* by the given value (value is the outer)
250 decisions := c.Ent.Decision.Query().Where(decision.UntilGT(time.Now().UTC()))
251
252 for param, value := range filter {
253 switch param {
254 case "contains":
255 contains, err = strconv.ParseBool(value[0])
256 if err != nil {
257 return 0, nil, fmt.Errorf("invalid contains value: %w: %w", err, InvalidFilter)
258 }
259 case "scopes":
260 decisions = decisions.Where(decision.ScopeEQ(value[0]))
261 case "uuid":
262 decisions = decisions.Where(decision.UUIDIn(value...))
263 case "origin":
264 decisions = decisions.Where(decision.OriginEQ(value[0]))
265 case "value":
266 decisions = decisions.Where(decision.ValueEQ(value[0]))
267 case "type":
268 decisions = decisions.Where(decision.TypeEQ(value[0]))
269 case "ip", "range":
270 rng, err = csnet.NewRange(value[0])
271 if err != nil {
272 return 0, nil, fmt.Errorf("unable to convert '%s' to int: %w: %w", value[0], err, InvalidIPOrRange)
273 }
274 case "scenario":
275 decisions = decisions.Where(decision.ScenarioEQ(value[0]))
276 default:
277 return 0, nil, fmt.Errorf("'%s' doesn't exist: %w", param, InvalidFilter)
278 }
279 }
280
281 decisions, err = decisionIPFilter(decisions, contains, rng)
282 if err != nil {
283 return 0, nil, err
284 }
285
286 decisionsToDelete, err := decisions.All(ctx)
287 if err != nil {
288 c.Log.Warningf("ExpireDecisionsWithFilter : %s", err)
289 return 0, nil, fmt.Errorf("expire decisions with provided filter: %w", DeleteFail)
290 }
291
292 count, err := c.ExpireDecisions(ctx, decisionsToDelete)
293 if err != nil {
294 return 0, nil, fmt.Errorf("expire decisions with provided filter: %w: %w", err, DeleteFail)
295 }
296
297 return count, decisionsToDelete, err
298}

Callers 4

DecisionCmdFunction · 0.80
ManagementCmdFunction · 0.80
DeleteDecisionsMethod · 0.80

Calls 13

ExpireDecisionsMethod · 0.95
UntilGTFunction · 0.92
ScopeEQFunction · 0.92
UUIDInFunction · 0.92
OriginEQFunction · 0.92
ValueEQFunction · 0.92
TypeEQFunction · 0.92
NewRangeFunction · 0.92
ScenarioEQFunction · 0.92
decisionIPFilterFunction · 0.85
WhereMethod · 0.45
QueryMethod · 0.45

Tested by

no test coverage detected