From 04729aec2a35e730068829f2863c64513272e247 Mon Sep 17 00:00:00 2001 From: marco Date: Fri, 31 May 2024 22:41:24 +0200 Subject: [PATCH] db: force decisions to have an expiration timestamp --- pkg/apiserver/apic.go | 2 +- pkg/database/ent/decision.go | 11 ++++------- pkg/database/ent/decision/where.go | 10 ---------- pkg/database/ent/decision_create.go | 13 ++++--------- pkg/database/ent/decision_update.go | 18 ------------------ pkg/database/ent/migrate/schema.go | 2 +- pkg/database/ent/mutation.go | 21 +-------------------- pkg/database/ent/schema/decision.go | 2 +- 8 files changed, 12 insertions(+), 67 deletions(-) diff --git a/pkg/apiserver/apic.go b/pkg/apiserver/apic.go index 5b850cbff0d..4138b1409cb 100644 --- a/pkg/apiserver/apic.go +++ b/pkg/apiserver/apic.go @@ -814,7 +814,7 @@ func (a *apic) ShouldForcePullBlocklist(blocklist *modelscapi.BlocklistLink) (bo 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 } diff --git a/pkg/database/ent/decision.go b/pkg/database/ent/decision.go index 4a6dc728509..9b479ba0e90 100644 --- a/pkg/database/ent/decision.go +++ b/pkg/database/ent/decision.go @@ -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) diff --git a/pkg/database/ent/decision/where.go b/pkg/database/ent/decision/where.go index 99a1889e63e..8420e7b573f 100644 --- a/pkg/database/ent/decision/where.go +++ b/pkg/database/ent/decision/where.go @@ -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)) diff --git a/pkg/database/ent/decision_create.go b/pkg/database/ent/decision_create.go index f30d5452120..80e50c5393f 100644 --- a/pkg/database/ent/decision_create.go +++ b/pkg/database/ent/decision_create.go @@ -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) diff --git a/pkg/database/ent/decision_update.go b/pkg/database/ent/decision_update.go index 68d0eb4ace7..244cd990ecc 100644 --- a/pkg/database/ent/decision_update.go +++ b/pkg/database/ent/decision_update.go @@ -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) } diff --git a/pkg/database/ent/migrate/schema.go b/pkg/database/ent/migrate/schema.go index 986f5bc8c67..81f8c7e833f 100644 --- a/pkg/database/ent/migrate/schema.go +++ b/pkg/database/ent/migrate/schema.go @@ -100,7 +100,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}, diff --git a/pkg/database/ent/mutation.go b/pkg/database/ent/mutation.go index 5c6596f3db4..f77b1dcf790 100644 --- a/pkg/database/ent/mutation.go +++ b/pkg/database/ent/mutation.go @@ -4271,7 +4271,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") } @@ -4285,22 +4285,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. @@ -5385,9 +5372,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) } @@ -5423,9 +5407,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 diff --git a/pkg/database/ent/schema/decision.go b/pkg/database/ent/schema/decision.go index 4089be38096..dd02ca5ff00 100644 --- a/pkg/database/ent/schema/decision.go +++ b/pkg/database/ent/schema/decision.go @@ -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(),