mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-10 20:05:55 +02:00
db: force decisions to have an expiration timestamp
This commit is contained in:
parent
466f39b880
commit
c17a0276b6
8 changed files with 12 additions and 67 deletions
|
@ -799,7 +799,7 @@ func (a *apic) ShouldForcePullBlocklist(ctx context.Context, blocklist *modelsca
|
|||
return false, fmt.Errorf("while getting decision: %w", err)
|
||||
}
|
||||
|
||||
if firstDecision == nil || firstDecision.Until == nil || firstDecision.Until.Sub(time.Now().UTC()) < (a.pullInterval+15*time.Minute) {
|
||||
if firstDecision == nil || firstDecision.Until.Sub(time.Now().UTC()) < (a.pullInterval+15*time.Minute) {
|
||||
log.Debugf("at least one decision found for %s, expire soon, force refresh", *blocklist.Name)
|
||||
return true, nil
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ type Decision struct {
|
|||
// UpdatedAt holds the value of the "updated_at" field.
|
||||
UpdatedAt time.Time `json:"updated_at,omitempty"`
|
||||
// Until holds the value of the "until" field.
|
||||
Until *time.Time `json:"until,omitempty"`
|
||||
Until time.Time `json:"until,omitempty"`
|
||||
// Scenario holds the value of the "scenario" field.
|
||||
Scenario string `json:"scenario,omitempty"`
|
||||
// Type holds the value of the "type" field.
|
||||
|
@ -126,8 +126,7 @@ func (d *Decision) assignValues(columns []string, values []any) error {
|
|||
if value, ok := values[i].(*sql.NullTime); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field until", values[i])
|
||||
} else if value.Valid {
|
||||
d.Until = new(time.Time)
|
||||
*d.Until = value.Time
|
||||
d.Until = value.Time
|
||||
}
|
||||
case decision.FieldScenario:
|
||||
if value, ok := values[i].(*sql.NullString); !ok {
|
||||
|
@ -254,10 +253,8 @@ func (d *Decision) String() string {
|
|||
builder.WriteString("updated_at=")
|
||||
builder.WriteString(d.UpdatedAt.Format(time.ANSIC))
|
||||
builder.WriteString(", ")
|
||||
if v := d.Until; v != nil {
|
||||
builder.WriteString("until=")
|
||||
builder.WriteString(v.Format(time.ANSIC))
|
||||
}
|
||||
builder.WriteString("until=")
|
||||
builder.WriteString(d.Until.Format(time.ANSIC))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("scenario=")
|
||||
builder.WriteString(d.Scenario)
|
||||
|
|
|
@ -255,16 +255,6 @@ func UntilLTE(v time.Time) predicate.Decision {
|
|||
return predicate.Decision(sql.FieldLTE(FieldUntil, v))
|
||||
}
|
||||
|
||||
// UntilIsNil applies the IsNil predicate on the "until" field.
|
||||
func UntilIsNil() predicate.Decision {
|
||||
return predicate.Decision(sql.FieldIsNull(FieldUntil))
|
||||
}
|
||||
|
||||
// UntilNotNil applies the NotNil predicate on the "until" field.
|
||||
func UntilNotNil() predicate.Decision {
|
||||
return predicate.Decision(sql.FieldNotNull(FieldUntil))
|
||||
}
|
||||
|
||||
// ScenarioEQ applies the EQ predicate on the "scenario" field.
|
||||
func ScenarioEQ(v string) predicate.Decision {
|
||||
return predicate.Decision(sql.FieldEQ(FieldScenario, v))
|
||||
|
|
|
@ -55,14 +55,6 @@ func (dc *DecisionCreate) SetUntil(t time.Time) *DecisionCreate {
|
|||
return dc
|
||||
}
|
||||
|
||||
// SetNillableUntil sets the "until" field if the given value is not nil.
|
||||
func (dc *DecisionCreate) SetNillableUntil(t *time.Time) *DecisionCreate {
|
||||
if t != nil {
|
||||
dc.SetUntil(*t)
|
||||
}
|
||||
return dc
|
||||
}
|
||||
|
||||
// SetScenario sets the "scenario" field.
|
||||
func (dc *DecisionCreate) SetScenario(s string) *DecisionCreate {
|
||||
dc.mutation.SetScenario(s)
|
||||
|
@ -281,6 +273,9 @@ func (dc *DecisionCreate) check() error {
|
|||
if _, ok := dc.mutation.UpdatedAt(); !ok {
|
||||
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Decision.updated_at"`)}
|
||||
}
|
||||
if _, ok := dc.mutation.Until(); !ok {
|
||||
return &ValidationError{Name: "until", err: errors.New(`ent: missing required field "Decision.until"`)}
|
||||
}
|
||||
if _, ok := dc.mutation.Scenario(); !ok {
|
||||
return &ValidationError{Name: "scenario", err: errors.New(`ent: missing required field "Decision.scenario"`)}
|
||||
}
|
||||
|
@ -335,7 +330,7 @@ func (dc *DecisionCreate) createSpec() (*Decision, *sqlgraph.CreateSpec) {
|
|||
}
|
||||
if value, ok := dc.mutation.Until(); ok {
|
||||
_spec.SetField(decision.FieldUntil, field.TypeTime, value)
|
||||
_node.Until = &value
|
||||
_node.Until = value
|
||||
}
|
||||
if value, ok := dc.mutation.Scenario(); ok {
|
||||
_spec.SetField(decision.FieldScenario, field.TypeString, value)
|
||||
|
|
|
@ -49,12 +49,6 @@ func (du *DecisionUpdate) SetNillableUntil(t *time.Time) *DecisionUpdate {
|
|||
return du
|
||||
}
|
||||
|
||||
// ClearUntil clears the value of the "until" field.
|
||||
func (du *DecisionUpdate) ClearUntil() *DecisionUpdate {
|
||||
du.mutation.ClearUntil()
|
||||
return du
|
||||
}
|
||||
|
||||
// SetAlertDecisions sets the "alert_decisions" field.
|
||||
func (du *DecisionUpdate) SetAlertDecisions(i int) *DecisionUpdate {
|
||||
du.mutation.SetAlertDecisions(i)
|
||||
|
@ -156,9 +150,6 @@ func (du *DecisionUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
|||
if value, ok := du.mutation.Until(); ok {
|
||||
_spec.SetField(decision.FieldUntil, field.TypeTime, value)
|
||||
}
|
||||
if du.mutation.UntilCleared() {
|
||||
_spec.ClearField(decision.FieldUntil, field.TypeTime)
|
||||
}
|
||||
if du.mutation.StartIPCleared() {
|
||||
_spec.ClearField(decision.FieldStartIP, field.TypeInt64)
|
||||
}
|
||||
|
@ -246,12 +237,6 @@ func (duo *DecisionUpdateOne) SetNillableUntil(t *time.Time) *DecisionUpdateOne
|
|||
return duo
|
||||
}
|
||||
|
||||
// ClearUntil clears the value of the "until" field.
|
||||
func (duo *DecisionUpdateOne) ClearUntil() *DecisionUpdateOne {
|
||||
duo.mutation.ClearUntil()
|
||||
return duo
|
||||
}
|
||||
|
||||
// SetAlertDecisions sets the "alert_decisions" field.
|
||||
func (duo *DecisionUpdateOne) SetAlertDecisions(i int) *DecisionUpdateOne {
|
||||
duo.mutation.SetAlertDecisions(i)
|
||||
|
@ -383,9 +368,6 @@ func (duo *DecisionUpdateOne) sqlSave(ctx context.Context) (_node *Decision, err
|
|||
if value, ok := duo.mutation.Until(); ok {
|
||||
_spec.SetField(decision.FieldUntil, field.TypeTime, value)
|
||||
}
|
||||
if duo.mutation.UntilCleared() {
|
||||
_spec.ClearField(decision.FieldUntil, field.TypeTime)
|
||||
}
|
||||
if duo.mutation.StartIPCleared() {
|
||||
_spec.ClearField(decision.FieldStartIP, field.TypeInt64)
|
||||
}
|
||||
|
|
|
@ -101,7 +101,7 @@ var (
|
|||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
{Name: "created_at", Type: field.TypeTime},
|
||||
{Name: "updated_at", Type: field.TypeTime},
|
||||
{Name: "until", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"mysql": "datetime"}},
|
||||
{Name: "until", Type: field.TypeTime, SchemaType: map[string]string{"mysql": "datetime"}},
|
||||
{Name: "scenario", Type: field.TypeString},
|
||||
{Name: "type", Type: field.TypeString},
|
||||
{Name: "start_ip", Type: field.TypeInt64, Nullable: true},
|
||||
|
|
|
@ -4325,7 +4325,7 @@ func (m *DecisionMutation) Until() (r time.Time, exists bool) {
|
|||
// OldUntil returns the old "until" field's value of the Decision entity.
|
||||
// If the Decision 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 *DecisionMutation) OldUntil(ctx context.Context) (v *time.Time, err error) {
|
||||
func (m *DecisionMutation) OldUntil(ctx context.Context) (v time.Time, err error) {
|
||||
if !m.op.Is(OpUpdateOne) {
|
||||
return v, errors.New("OldUntil is only allowed on UpdateOne operations")
|
||||
}
|
||||
|
@ -4339,22 +4339,9 @@ func (m *DecisionMutation) OldUntil(ctx context.Context) (v *time.Time, err erro
|
|||
return oldValue.Until, nil
|
||||
}
|
||||
|
||||
// ClearUntil clears the value of the "until" field.
|
||||
func (m *DecisionMutation) ClearUntil() {
|
||||
m.until = nil
|
||||
m.clearedFields[decision.FieldUntil] = struct{}{}
|
||||
}
|
||||
|
||||
// UntilCleared returns if the "until" field was cleared in this mutation.
|
||||
func (m *DecisionMutation) UntilCleared() bool {
|
||||
_, ok := m.clearedFields[decision.FieldUntil]
|
||||
return ok
|
||||
}
|
||||
|
||||
// ResetUntil resets all changes to the "until" field.
|
||||
func (m *DecisionMutation) ResetUntil() {
|
||||
m.until = nil
|
||||
delete(m.clearedFields, decision.FieldUntil)
|
||||
}
|
||||
|
||||
// SetScenario sets the "scenario" field.
|
||||
|
@ -5439,9 +5426,6 @@ func (m *DecisionMutation) AddField(name string, value ent.Value) error {
|
|||
// mutation.
|
||||
func (m *DecisionMutation) ClearedFields() []string {
|
||||
var fields []string
|
||||
if m.FieldCleared(decision.FieldUntil) {
|
||||
fields = append(fields, decision.FieldUntil)
|
||||
}
|
||||
if m.FieldCleared(decision.FieldStartIP) {
|
||||
fields = append(fields, decision.FieldStartIP)
|
||||
}
|
||||
|
@ -5477,9 +5461,6 @@ func (m *DecisionMutation) FieldCleared(name string) bool {
|
|||
// error if the field is not defined in the schema.
|
||||
func (m *DecisionMutation) ClearField(name string) error {
|
||||
switch name {
|
||||
case decision.FieldUntil:
|
||||
m.ClearUntil()
|
||||
return nil
|
||||
case decision.FieldStartIP:
|
||||
m.ClearStartIP()
|
||||
return nil
|
||||
|
|
|
@ -24,7 +24,7 @@ func (Decision) Fields() []ent.Field {
|
|||
field.Time("updated_at").
|
||||
Default(types.UtcNow).
|
||||
UpdateDefault(types.UtcNow),
|
||||
field.Time("until").Nillable().Optional().SchemaType(map[string]string{
|
||||
field.Time("until").SchemaType(map[string]string{
|
||||
dialect.MySQL: "datetime",
|
||||
}),
|
||||
field.String("scenario").Immutable(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue