Browse Source

Blocklist: Do not duplicate decisions when pulling (#1796)

blotus 2 years ago
parent
commit
bb2f0e938f
2 changed files with 10 additions and 2 deletions
  1. 9 2
      pkg/database/alerts.go
  2. 1 0
      pkg/database/machines.go

+ 9 - 2
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)

+ 1 - 0
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)