mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 12:25:53 +02:00
Store alert remediations status in DB (#3115)
This commit is contained in:
parent
84c214a6f0
commit
c4bfdf1991
12 changed files with 155 additions and 9 deletions
|
@ -241,7 +241,8 @@ func (c *Client) UpdateCommunityBlocklist(alertItem *models.Alert) (int, int, in
|
|||
SetLeakSpeed(*alertItem.Leakspeed).
|
||||
SetSimulated(*alertItem.Simulated).
|
||||
SetScenarioVersion(*alertItem.ScenarioVersion).
|
||||
SetScenarioHash(*alertItem.ScenarioHash)
|
||||
SetScenarioHash(*alertItem.ScenarioHash).
|
||||
SetRemediation(true) // it's from CAPI, we always have decisions
|
||||
|
||||
alertRef, err := alertB.Save(c.CTX)
|
||||
if err != nil {
|
||||
|
@ -554,7 +555,6 @@ func (c *Client) createAlertChunk(machineID string, owner *ent.Machine, alerts [
|
|||
|
||||
if len(metaItem.Value) > 4095 {
|
||||
c.Log.Warningf("truncated meta %s : value too long", metaItem.Key)
|
||||
|
||||
value = value[:4095]
|
||||
}
|
||||
|
||||
|
@ -618,6 +618,7 @@ func (c *Client) createAlertChunk(machineID string, owner *ent.Machine, alerts [
|
|||
SetSimulated(*alertItem.Simulated).
|
||||
SetScenarioVersion(*alertItem.ScenarioVersion).
|
||||
SetScenarioHash(*alertItem.ScenarioHash).
|
||||
SetRemediation(alertItem.Remediation).
|
||||
SetUUID(alertItem.UUID).
|
||||
AddEvents(events...).
|
||||
AddMetas(metas...)
|
||||
|
@ -677,7 +678,6 @@ func (c *Client) createAlertChunk(machineID string, owner *ent.Machine, alerts [
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -64,6 +64,8 @@ type Alert struct {
|
|||
Simulated bool `json:"simulated,omitempty"`
|
||||
// UUID holds the value of the "uuid" field.
|
||||
UUID string `json:"uuid,omitempty"`
|
||||
// Remediation holds the value of the "remediation" field.
|
||||
Remediation bool `json:"remediation,omitempty"`
|
||||
// Edges holds the relations/edges for other nodes in the graph.
|
||||
// The values are being populated by the AlertQuery when eager-loading is set.
|
||||
Edges AlertEdges `json:"edges"`
|
||||
|
@ -129,7 +131,7 @@ func (*Alert) scanValues(columns []string) ([]any, error) {
|
|||
values := make([]any, len(columns))
|
||||
for i := range columns {
|
||||
switch columns[i] {
|
||||
case alert.FieldSimulated:
|
||||
case alert.FieldSimulated, alert.FieldRemediation:
|
||||
values[i] = new(sql.NullBool)
|
||||
case alert.FieldSourceLatitude, alert.FieldSourceLongitude:
|
||||
values[i] = new(sql.NullFloat64)
|
||||
|
@ -300,6 +302,12 @@ func (a *Alert) assignValues(columns []string, values []any) error {
|
|||
} else if value.Valid {
|
||||
a.UUID = value.String
|
||||
}
|
||||
case alert.FieldRemediation:
|
||||
if value, ok := values[i].(*sql.NullBool); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field remediation", values[i])
|
||||
} else if value.Valid {
|
||||
a.Remediation = value.Bool
|
||||
}
|
||||
case alert.ForeignKeys[0]:
|
||||
if value, ok := values[i].(*sql.NullInt64); !ok {
|
||||
return fmt.Errorf("unexpected type %T for edge-field machine_alerts", value)
|
||||
|
@ -431,6 +439,9 @@ func (a *Alert) String() string {
|
|||
builder.WriteString(", ")
|
||||
builder.WriteString("uuid=")
|
||||
builder.WriteString(a.UUID)
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("remediation=")
|
||||
builder.WriteString(fmt.Sprintf("%v", a.Remediation))
|
||||
builder.WriteByte(')')
|
||||
return builder.String()
|
||||
}
|
||||
|
|
|
@ -60,6 +60,8 @@ const (
|
|||
FieldSimulated = "simulated"
|
||||
// FieldUUID holds the string denoting the uuid field in the database.
|
||||
FieldUUID = "uuid"
|
||||
// FieldRemediation holds the string denoting the remediation field in the database.
|
||||
FieldRemediation = "remediation"
|
||||
// EdgeOwner holds the string denoting the owner edge name in mutations.
|
||||
EdgeOwner = "owner"
|
||||
// EdgeDecisions holds the string denoting the decisions edge name in mutations.
|
||||
|
@ -126,6 +128,7 @@ var Columns = []string{
|
|||
FieldScenarioHash,
|
||||
FieldSimulated,
|
||||
FieldUUID,
|
||||
FieldRemediation,
|
||||
}
|
||||
|
||||
// ForeignKeys holds the SQL foreign-keys that are owned by the "alerts"
|
||||
|
@ -293,6 +296,11 @@ func ByUUID(opts ...sql.OrderTermOption) OrderOption {
|
|||
return sql.OrderByField(FieldUUID, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByRemediation orders the results by the remediation field.
|
||||
func ByRemediation(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldRemediation, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByOwnerField orders the results by owner field.
|
||||
func ByOwnerField(field string, opts ...sql.OrderTermOption) OrderOption {
|
||||
return func(s *sql.Selector) {
|
||||
|
|
|
@ -170,6 +170,11 @@ func UUID(v string) predicate.Alert {
|
|||
return predicate.Alert(sql.FieldEQ(FieldUUID, v))
|
||||
}
|
||||
|
||||
// Remediation applies equality check predicate on the "remediation" field. It's identical to RemediationEQ.
|
||||
func Remediation(v bool) predicate.Alert {
|
||||
return predicate.Alert(sql.FieldEQ(FieldRemediation, v))
|
||||
}
|
||||
|
||||
// CreatedAtEQ applies the EQ predicate on the "created_at" field.
|
||||
func CreatedAtEQ(v time.Time) predicate.Alert {
|
||||
return predicate.Alert(sql.FieldEQ(FieldCreatedAt, v))
|
||||
|
@ -1600,6 +1605,26 @@ func UUIDContainsFold(v string) predicate.Alert {
|
|||
return predicate.Alert(sql.FieldContainsFold(FieldUUID, v))
|
||||
}
|
||||
|
||||
// RemediationEQ applies the EQ predicate on the "remediation" field.
|
||||
func RemediationEQ(v bool) predicate.Alert {
|
||||
return predicate.Alert(sql.FieldEQ(FieldRemediation, v))
|
||||
}
|
||||
|
||||
// RemediationNEQ applies the NEQ predicate on the "remediation" field.
|
||||
func RemediationNEQ(v bool) predicate.Alert {
|
||||
return predicate.Alert(sql.FieldNEQ(FieldRemediation, v))
|
||||
}
|
||||
|
||||
// RemediationIsNil applies the IsNil predicate on the "remediation" field.
|
||||
func RemediationIsNil() predicate.Alert {
|
||||
return predicate.Alert(sql.FieldIsNull(FieldRemediation))
|
||||
}
|
||||
|
||||
// RemediationNotNil applies the NotNil predicate on the "remediation" field.
|
||||
func RemediationNotNil() predicate.Alert {
|
||||
return predicate.Alert(sql.FieldNotNull(FieldRemediation))
|
||||
}
|
||||
|
||||
// HasOwner applies the HasEdge predicate on the "owner" edge.
|
||||
func HasOwner() predicate.Alert {
|
||||
return predicate.Alert(func(s *sql.Selector) {
|
||||
|
|
|
@ -338,6 +338,20 @@ func (ac *AlertCreate) SetNillableUUID(s *string) *AlertCreate {
|
|||
return ac
|
||||
}
|
||||
|
||||
// SetRemediation sets the "remediation" field.
|
||||
func (ac *AlertCreate) SetRemediation(b bool) *AlertCreate {
|
||||
ac.mutation.SetRemediation(b)
|
||||
return ac
|
||||
}
|
||||
|
||||
// SetNillableRemediation sets the "remediation" field if the given value is not nil.
|
||||
func (ac *AlertCreate) SetNillableRemediation(b *bool) *AlertCreate {
|
||||
if b != nil {
|
||||
ac.SetRemediation(*b)
|
||||
}
|
||||
return ac
|
||||
}
|
||||
|
||||
// SetOwnerID sets the "owner" edge to the Machine entity by ID.
|
||||
func (ac *AlertCreate) SetOwnerID(id int) *AlertCreate {
|
||||
ac.mutation.SetOwnerID(id)
|
||||
|
@ -603,6 +617,10 @@ func (ac *AlertCreate) createSpec() (*Alert, *sqlgraph.CreateSpec) {
|
|||
_spec.SetField(alert.FieldUUID, field.TypeString, value)
|
||||
_node.UUID = value
|
||||
}
|
||||
if value, ok := ac.mutation.Remediation(); ok {
|
||||
_spec.SetField(alert.FieldRemediation, field.TypeBool, value)
|
||||
_node.Remediation = value
|
||||
}
|
||||
if nodes := ac.mutation.OwnerIDs(); len(nodes) > 0 {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.M2O,
|
||||
|
|
|
@ -281,6 +281,9 @@ func (au *AlertUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
|||
if au.mutation.UUIDCleared() {
|
||||
_spec.ClearField(alert.FieldUUID, field.TypeString)
|
||||
}
|
||||
if au.mutation.RemediationCleared() {
|
||||
_spec.ClearField(alert.FieldRemediation, field.TypeBool)
|
||||
}
|
||||
if au.mutation.OwnerCleared() {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.M2O,
|
||||
|
@ -744,6 +747,9 @@ func (auo *AlertUpdateOne) sqlSave(ctx context.Context) (_node *Alert, err error
|
|||
if auo.mutation.UUIDCleared() {
|
||||
_spec.ClearField(alert.FieldUUID, field.TypeString)
|
||||
}
|
||||
if auo.mutation.RemediationCleared() {
|
||||
_spec.ClearField(alert.FieldRemediation, field.TypeBool)
|
||||
}
|
||||
if auo.mutation.OwnerCleared() {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.M2O,
|
||||
|
|
|
@ -34,6 +34,7 @@ var (
|
|||
{Name: "scenario_hash", Type: field.TypeString, Nullable: true},
|
||||
{Name: "simulated", Type: field.TypeBool, Default: false},
|
||||
{Name: "uuid", Type: field.TypeString, Nullable: true},
|
||||
{Name: "remediation", Type: field.TypeBool, Nullable: true},
|
||||
{Name: "machine_alerts", Type: field.TypeInt, Nullable: true},
|
||||
}
|
||||
// AlertsTable holds the schema information for the "alerts" table.
|
||||
|
@ -44,7 +45,7 @@ var (
|
|||
ForeignKeys: []*schema.ForeignKey{
|
||||
{
|
||||
Symbol: "alerts_machines_alerts",
|
||||
Columns: []*schema.Column{AlertsColumns[24]},
|
||||
Columns: []*schema.Column{AlertsColumns[25]},
|
||||
RefColumns: []*schema.Column{MachinesColumns[0]},
|
||||
OnDelete: schema.SetNull,
|
||||
},
|
||||
|
|
|
@ -77,6 +77,7 @@ type AlertMutation struct {
|
|||
scenarioHash *string
|
||||
simulated *bool
|
||||
uuid *string
|
||||
remediation *bool
|
||||
clearedFields map[string]struct{}
|
||||
owner *int
|
||||
clearedowner bool
|
||||
|
@ -1351,6 +1352,55 @@ func (m *AlertMutation) ResetUUID() {
|
|||
delete(m.clearedFields, alert.FieldUUID)
|
||||
}
|
||||
|
||||
// SetRemediation sets the "remediation" field.
|
||||
func (m *AlertMutation) SetRemediation(b bool) {
|
||||
m.remediation = &b
|
||||
}
|
||||
|
||||
// Remediation returns the value of the "remediation" field in the mutation.
|
||||
func (m *AlertMutation) Remediation() (r bool, exists bool) {
|
||||
v := m.remediation
|
||||
if v == nil {
|
||||
return
|
||||
}
|
||||
return *v, true
|
||||
}
|
||||
|
||||
// OldRemediation returns the old "remediation" field's value of the Alert entity.
|
||||
// If the Alert object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *AlertMutation) OldRemediation(ctx context.Context) (v bool, err error) {
|
||||
if !m.op.Is(OpUpdateOne) {
|
||||
return v, errors.New("OldRemediation is only allowed on UpdateOne operations")
|
||||
}
|
||||
if m.id == nil || m.oldValue == nil {
|
||||
return v, errors.New("OldRemediation requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldRemediation: %w", err)
|
||||
}
|
||||
return oldValue.Remediation, nil
|
||||
}
|
||||
|
||||
// ClearRemediation clears the value of the "remediation" field.
|
||||
func (m *AlertMutation) ClearRemediation() {
|
||||
m.remediation = nil
|
||||
m.clearedFields[alert.FieldRemediation] = struct{}{}
|
||||
}
|
||||
|
||||
// RemediationCleared returns if the "remediation" field was cleared in this mutation.
|
||||
func (m *AlertMutation) RemediationCleared() bool {
|
||||
_, ok := m.clearedFields[alert.FieldRemediation]
|
||||
return ok
|
||||
}
|
||||
|
||||
// ResetRemediation resets all changes to the "remediation" field.
|
||||
func (m *AlertMutation) ResetRemediation() {
|
||||
m.remediation = nil
|
||||
delete(m.clearedFields, alert.FieldRemediation)
|
||||
}
|
||||
|
||||
// SetOwnerID sets the "owner" edge to the Machine entity by id.
|
||||
func (m *AlertMutation) SetOwnerID(id int) {
|
||||
m.owner = &id
|
||||
|
@ -1586,7 +1636,7 @@ func (m *AlertMutation) Type() string {
|
|||
// order to get all numeric fields that were incremented/decremented, call
|
||||
// AddedFields().
|
||||
func (m *AlertMutation) Fields() []string {
|
||||
fields := make([]string, 0, 23)
|
||||
fields := make([]string, 0, 24)
|
||||
if m.created_at != nil {
|
||||
fields = append(fields, alert.FieldCreatedAt)
|
||||
}
|
||||
|
@ -1656,6 +1706,9 @@ func (m *AlertMutation) Fields() []string {
|
|||
if m.uuid != nil {
|
||||
fields = append(fields, alert.FieldUUID)
|
||||
}
|
||||
if m.remediation != nil {
|
||||
fields = append(fields, alert.FieldRemediation)
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
|
@ -1710,6 +1763,8 @@ func (m *AlertMutation) Field(name string) (ent.Value, bool) {
|
|||
return m.Simulated()
|
||||
case alert.FieldUUID:
|
||||
return m.UUID()
|
||||
case alert.FieldRemediation:
|
||||
return m.Remediation()
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
@ -1765,6 +1820,8 @@ func (m *AlertMutation) OldField(ctx context.Context, name string) (ent.Value, e
|
|||
return m.OldSimulated(ctx)
|
||||
case alert.FieldUUID:
|
||||
return m.OldUUID(ctx)
|
||||
case alert.FieldRemediation:
|
||||
return m.OldRemediation(ctx)
|
||||
}
|
||||
return nil, fmt.Errorf("unknown Alert field %s", name)
|
||||
}
|
||||
|
@ -1935,6 +1992,13 @@ func (m *AlertMutation) SetField(name string, value ent.Value) error {
|
|||
}
|
||||
m.SetUUID(v)
|
||||
return nil
|
||||
case alert.FieldRemediation:
|
||||
v, ok := value.(bool)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T for field %s", value, name)
|
||||
}
|
||||
m.SetRemediation(v)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("unknown Alert field %s", name)
|
||||
}
|
||||
|
@ -2073,6 +2137,9 @@ func (m *AlertMutation) ClearedFields() []string {
|
|||
if m.FieldCleared(alert.FieldUUID) {
|
||||
fields = append(fields, alert.FieldUUID)
|
||||
}
|
||||
if m.FieldCleared(alert.FieldRemediation) {
|
||||
fields = append(fields, alert.FieldRemediation)
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
|
@ -2144,6 +2211,9 @@ func (m *AlertMutation) ClearField(name string) error {
|
|||
case alert.FieldUUID:
|
||||
m.ClearUUID()
|
||||
return nil
|
||||
case alert.FieldRemediation:
|
||||
m.ClearRemediation()
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("unknown Alert nullable field %s", name)
|
||||
}
|
||||
|
@ -2221,6 +2291,9 @@ func (m *AlertMutation) ResetField(name string) error {
|
|||
case alert.FieldUUID:
|
||||
m.ResetUUID()
|
||||
return nil
|
||||
case alert.FieldRemediation:
|
||||
m.ResetRemediation()
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("unknown Alert field %s", name)
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@ func (Alert) Fields() []ent.Field {
|
|||
field.String("scenarioHash").Optional().Immutable(),
|
||||
field.Bool("simulated").Default(false).Immutable(),
|
||||
field.String("uuid").Optional().Immutable(), // this uuid is mostly here to ensure that CAPI/PAPI has a unique id for each alert
|
||||
field.Bool("remediation").Optional().Immutable(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue