From bb2f0e938f872783ec76ac4f726b17a8040479d2 Mon Sep 17 00:00:00 2001 From: blotus Date: Wed, 19 Oct 2022 15:51:40 +0200 Subject: [PATCH] Blocklist: Do not duplicate decisions when pulling (#1796) --- pkg/database/alerts.go | 11 +++++++++-- pkg/database/machines.go | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/database/alerts.go b/pkg/database/alerts.go index 8f2f6731d..ebaeb4c2f 100644 --- a/pkg/database/alerts.go +++ b/pkg/database/alerts.go @@ -180,6 +180,12 @@ func (c *Client) UpdateCommunityBlocklist(alertItem *models.Alert) (int, int, in if len(alertItem.Decisions) > 0 { decisionBulk := make([]*ent.DecisionCreate, 0, decisionBulkSize) valueList := make([]string, 0, decisionBulkSize) + DecOrigin := CapiMachineID + if *alertItem.Decisions[0].Origin == CapiMachineID || *alertItem.Decisions[0].Origin == CapiListsMachineID { + DecOrigin = *alertItem.Decisions[0].Origin + } else { + log.Warningf("unexpected origin %s", *alertItem.Decisions[0].Origin) + } for i, decisionItem := range alertItem.Decisions { var start_ip, start_sfx, end_ip, end_sfx int64 var sz int @@ -235,7 +241,7 @@ func (c *Client) UpdateCommunityBlocklist(alertItem *models.Alert) (int, int, in /*Deleting older decisions from capi*/ deletedDecisions, err := c.Ent.Decision.Delete(). Where(decision.And( - decision.OriginEQ(CapiMachineID), + decision.OriginEQ(DecOrigin), decision.Not(decision.HasOwnerWith(alert.IDEQ(alertRef.ID))), decision.ValueIn(valueList...), )).Exec(c.CTX) @@ -259,6 +265,7 @@ func (c *Client) UpdateCommunityBlocklist(alertItem *models.Alert) (int, int, in } } + log.Debugf("deleted %d decisions for %s vs %s", deleted, DecOrigin, *alertItem.Decisions[0].Origin) insertedDecisions, err := c.Ent.Decision.CreateBulk(decisionBulk...).Save(c.CTX) if err != nil { return 0, 0, 0, errors.Wrapf(BulkError, "creating alert decisions: %s", err) @@ -268,7 +275,7 @@ func (c *Client) UpdateCommunityBlocklist(alertItem *models.Alert) (int, int, in if len(valueList) > 0 { deletedDecisions, err := c.Ent.Decision.Delete(). Where(decision.And( - decision.OriginEQ(CapiMachineID), + decision.OriginEQ(DecOrigin), decision.Not(decision.HasOwnerWith(alert.IDEQ(alertRef.ID))), decision.ValueIn(valueList...), )).Exec(c.CTX) diff --git a/pkg/database/machines.go b/pkg/database/machines.go index c5575685c..444d72674 100644 --- a/pkg/database/machines.go +++ b/pkg/database/machines.go @@ -13,6 +13,7 @@ import ( ) const CapiMachineID = "CAPI" +const CapiListsMachineID = "lists" func (c *Client) CreateMachine(machineID *string, password *strfmt.Password, ipAddress string, isValidated bool, force bool, authType string) (*ent.Machine, error) { hashPassword, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)