Skip to content

Commit

Permalink
alerts wip/5
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Sep 23, 2024
1 parent 4cc9406 commit ce0e8ab
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
11 changes: 8 additions & 3 deletions pkg/apiserver/controllers/v1/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func (c *Controller) FindAlerts(gctx *gin.Context) {

// FindAlertByID returns the alert associated with the ID
func (c *Controller) FindAlertByID(gctx *gin.Context) {
ctx := gctx.Request.Context()
alertIDStr := gctx.Param("alert_id")

alertID, err := strconv.Atoi(alertIDStr)
Expand All @@ -289,7 +290,7 @@ func (c *Controller) FindAlertByID(gctx *gin.Context) {
return
}

result, err := c.DBClient.GetAlertByID(alertID)
result, err := c.DBClient.GetAlertByID(ctx, alertID)
if err != nil {
c.HandleDBErrors(gctx, err)
return
Expand All @@ -309,6 +310,8 @@ func (c *Controller) FindAlertByID(gctx *gin.Context) {
func (c *Controller) DeleteAlertByID(gctx *gin.Context) {
var err error

ctx := gctx.Request.Context()

incomingIP := gctx.ClientIP()
if incomingIP != "127.0.0.1" && incomingIP != "::1" && !networksContainIP(c.TrustedIPs, incomingIP) && !isUnixSocket(gctx) {
gctx.JSON(http.StatusForbidden, gin.H{"message": fmt.Sprintf("access forbidden from this IP (%s)", incomingIP)})
Expand All @@ -323,7 +326,7 @@ func (c *Controller) DeleteAlertByID(gctx *gin.Context) {
return
}

err = c.DBClient.DeleteAlertByID(decisionID)
err = c.DBClient.DeleteAlertByID(ctx, decisionID)
if err != nil {
c.HandleDBErrors(gctx, err)
return
Expand All @@ -336,13 +339,15 @@ func (c *Controller) DeleteAlertByID(gctx *gin.Context) {

// DeleteAlerts deletes alerts from the database based on the specified filter
func (c *Controller) DeleteAlerts(gctx *gin.Context) {
ctx := gctx.Request.Context()

incomingIP := gctx.ClientIP()
if incomingIP != "127.0.0.1" && incomingIP != "::1" && !networksContainIP(c.TrustedIPs, incomingIP) && !isUnixSocket(gctx) {
gctx.JSON(http.StatusForbidden, gin.H{"message": fmt.Sprintf("access forbidden from this IP (%s)", incomingIP)})
return
}

nbDeleted, err := c.DBClient.DeleteAlertWithFilter(gctx.Request.URL.Query())
nbDeleted, err := c.DBClient.DeleteAlertWithFilter(ctx, gctx.Request.URL.Query())
if err != nil {
c.HandleDBErrors(gctx, err)
return
Expand Down
24 changes: 12 additions & 12 deletions pkg/database/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1127,33 +1127,33 @@ func (c *Client) DeleteAlertGraphBatch(ctx context.Context, alertItems []*ent.Al
return deleted, nil
}

func (c *Client) DeleteAlertGraph(alertItem *ent.Alert) error {
func (c *Client) DeleteAlertGraph(ctx context.Context, alertItem *ent.Alert) error {
// delete the associated events
_, err := c.Ent.Event.Delete().
Where(event.HasOwnerWith(alert.IDEQ(alertItem.ID))).Exec(c.CTX)
Where(event.HasOwnerWith(alert.IDEQ(alertItem.ID))).Exec(ctx)
if err != nil {
c.Log.Warningf("DeleteAlertGraph : %s", err)
return errors.Wrapf(DeleteFail, "event with alert ID '%d'", alertItem.ID)
}

// delete the associated meta
_, err = c.Ent.Meta.Delete().
Where(meta.HasOwnerWith(alert.IDEQ(alertItem.ID))).Exec(c.CTX)
Where(meta.HasOwnerWith(alert.IDEQ(alertItem.ID))).Exec(ctx)
if err != nil {
c.Log.Warningf("DeleteAlertGraph : %s", err)
return errors.Wrapf(DeleteFail, "meta with alert ID '%d'", alertItem.ID)
}

// delete the associated decisions
_, err = c.Ent.Decision.Delete().
Where(decision.HasOwnerWith(alert.IDEQ(alertItem.ID))).Exec(c.CTX)
Where(decision.HasOwnerWith(alert.IDEQ(alertItem.ID))).Exec(ctx)
if err != nil {
c.Log.Warningf("DeleteAlertGraph : %s", err)
return errors.Wrapf(DeleteFail, "decision with alert ID '%d'", alertItem.ID)
}

// delete the alert
err = c.Ent.Alert.DeleteOne(alertItem).Exec(c.CTX)
err = c.Ent.Alert.DeleteOne(alertItem).Exec(ctx)
if err != nil {
c.Log.Warningf("DeleteAlertGraph : %s", err)
return errors.Wrapf(DeleteFail, "alert with ID '%d'", alertItem.ID)
Expand All @@ -1162,26 +1162,26 @@ func (c *Client) DeleteAlertGraph(alertItem *ent.Alert) error {
return nil
}

func (c *Client) DeleteAlertByID(id int) error {
alertItem, err := c.Ent.Alert.Query().Where(alert.IDEQ(id)).Only(c.CTX)
func (c *Client) DeleteAlertByID(ctx context.Context, id int) error {
alertItem, err := c.Ent.Alert.Query().Where(alert.IDEQ(id)).Only(ctx)
if err != nil {
return err
}

return c.DeleteAlertGraph(alertItem)
return c.DeleteAlertGraph(ctx, alertItem)
}

func (c *Client) DeleteAlertWithFilter(filter map[string][]string) (int, error) {
func (c *Client) DeleteAlertWithFilter(ctx context.Context, filter map[string][]string) (int, error) {
preds, err := AlertPredicatesFromFilter(filter)
if err != nil {
return 0, err
}

return c.Ent.Alert.Delete().Where(preds...).Exec(c.CTX)
return c.Ent.Alert.Delete().Where(preds...).Exec(ctx)
}

func (c *Client) GetAlertByID(alertID int) (*ent.Alert, error) {
alert, err := c.Ent.Alert.Query().Where(alert.IDEQ(alertID)).WithDecisions().WithEvents().WithMetas().WithOwner().First(c.CTX)
func (c *Client) GetAlertByID(ctx context.Context, alertID int) (*ent.Alert, error) {
alert, err := c.Ent.Alert.Query().Where(alert.IDEQ(alertID)).WithDecisions().WithEvents().WithMetas().WithOwner().First(ctx)
if err != nil {
/*record not found, 404*/
if ent.IsNotFound(err) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (c *Client) FlushAlerts(ctx context.Context, MaxAge string, MaxItems int) e
"created_before": {MaxAge},
}

nbDeleted, err := c.DeleteAlertWithFilter(filter)
nbDeleted, err := c.DeleteAlertWithFilter(ctx, filter)
if err != nil {
c.Log.Warningf("FlushAlerts (max age): %s", err)
return fmt.Errorf("unable to flush alerts with filter until=%s: %w", MaxAge, err)
Expand Down

0 comments on commit ce0e8ab

Please sign in to comment.