feat(access): instance network rules at the capture receiver; policies reload without a restart
This commit is contained in:
33 files changed
+1682
-814
No files matched your search
@@ -581,3 +581,32 @@ func (s *EDA) TaskToolCalls(ctx context.Context, taskID int64) ([]ToolCall, erro
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// refusalRetention is how long a refusal is kept for the source page.
|
||||
const refusalRetention = 24 * time.Hour
|
||||
|
||||
// RecordCaptureRefusal counts one refused event and drops refusals older
|
||||
// than a day.
|
||||
func (s *EDA) RecordCaptureRefusal(ctx context.Context, source, addr string, at time.Time) error {
|
||||
if err := s.q.InsertCaptureRefusal(ctx, postgresdb.InsertCaptureRefusalParams{Source: source, Addr: addr, RefusedAt: at.UTC()}); err != nil {
|
||||
return fmt.Errorf("store: record capture refusal: %w", err)
|
||||
}
|
||||
if err := s.q.DeleteCaptureRefusalsBefore(ctx, at.Add(-refusalRetention).UTC()); err != nil {
|
||||
return fmt.Errorf("store: prune capture refusals: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CaptureRefusals summarises refusals since a time for every source that had
|
||||
// one, beside the captures it accepted in the same window.
|
||||
func (s *EDA) CaptureRefusals(ctx context.Context, since time.Time) ([]storecontract.CaptureRefusals, error) {
|
||||
rows, err := s.q.CaptureRefusalSummary(ctx, since.UTC())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: capture refusals: %w", err)
|
||||
}
|
||||
out := make([]storecontract.CaptureRefusals, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, storecontract.CaptureRefusals{Source: r.Source, Refused: r.Refused, Accepted: r.Accepted, Addrs: r.Addrs})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
-- Events the network rules refused at the capture receiver. Not captures:
|
||||
-- only the source, the sender address and when, kept for a day.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
CREATE TABLE capture_refusals (
|
||||
source text NOT NULL,
|
||||
addr text NOT NULL,
|
||||
refused_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX idx_capture_refusals_source ON capture_refusals (source, refused_at);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
DROP TABLE capture_refusals;
|
||||
@@ -69,6 +69,50 @@ func (q *Queries) ArmedBindingsForSource(ctx context.Context, source string) ([]
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const captureRefusalSummary = `-- name: CaptureRefusalSummary :many
|
||||
WITH r AS (
|
||||
SELECT source, count(*) AS refused, array_agg(DISTINCT addr ORDER BY addr)::text[] AS addrs
|
||||
FROM capture_refusals WHERE refused_at >= $1 GROUP BY source
|
||||
), a AS (
|
||||
SELECT source, count(*) AS accepted FROM captures WHERE received_at >= $1 GROUP BY source
|
||||
)
|
||||
SELECT r.source, r.refused, COALESCE(a.accepted, 0)::bigint AS accepted, r.addrs
|
||||
FROM r LEFT JOIN a ON a.source = r.source
|
||||
ORDER BY r.source
|
||||
`
|
||||
|
||||
type CaptureRefusalSummaryRow struct {
|
||||
Source string
|
||||
Refused int64
|
||||
Accepted int64
|
||||
Addrs []string
|
||||
}
|
||||
|
||||
func (q *Queries) CaptureRefusalSummary(ctx context.Context, refusedAt time.Time) ([]CaptureRefusalSummaryRow, error) {
|
||||
rows, err := q.db.Query(ctx, captureRefusalSummary, refusedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []CaptureRefusalSummaryRow
|
||||
for rows.Next() {
|
||||
var i CaptureRefusalSummaryRow
|
||||
if err := rows.Scan(
|
||||
&i.Source,
|
||||
&i.Refused,
|
||||
&i.Accepted,
|
||||
&i.Addrs,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const deleteBinding = `-- name: DeleteBinding :execrows
|
||||
DELETE FROM bindings WHERE id = $1
|
||||
`
|
||||
@@ -81,6 +125,15 @@ func (q *Queries) DeleteBinding(ctx context.Context, id string) (int64, error) {
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const deleteCaptureRefusalsBefore = `-- name: DeleteCaptureRefusalsBefore :exec
|
||||
DELETE FROM capture_refusals WHERE refused_at < $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteCaptureRefusalsBefore(ctx context.Context, refusedAt time.Time) error {
|
||||
_, err := q.db.Exec(ctx, deleteCaptureRefusalsBefore, refusedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteCapturesBeyondCount = `-- name: DeleteCapturesBeyondCount :exec
|
||||
DELETE FROM captures WHERE id NOT IN (
|
||||
SELECT id FROM captures ORDER BY received_at DESC LIMIT $1
|
||||
@@ -399,6 +452,21 @@ func (q *Queries) InsertCapture(ctx context.Context, arg InsertCaptureParams) er
|
||||
return err
|
||||
}
|
||||
|
||||
const insertCaptureRefusal = `-- name: InsertCaptureRefusal :exec
|
||||
INSERT INTO capture_refusals (source, addr, refused_at) VALUES ($1, $2, $3)
|
||||
`
|
||||
|
||||
type InsertCaptureRefusalParams struct {
|
||||
Source string
|
||||
Addr string
|
||||
RefusedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) InsertCaptureRefusal(ctx context.Context, arg InsertCaptureRefusalParams) error {
|
||||
_, err := q.db.Exec(ctx, insertCaptureRefusal, arg.Source, arg.Addr, arg.RefusedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertEventType = `-- name: InsertEventType :exec
|
||||
INSERT INTO event_types (id, source, name, rule, schema)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
|
||||
@@ -95,6 +95,12 @@ type Capture struct {
|
||||
WorkspaceID string
|
||||
}
|
||||
|
||||
type CaptureRefusal struct {
|
||||
Source string
|
||||
Addr string
|
||||
RefusedAt time.Time
|
||||
}
|
||||
|
||||
type ChannelStatus struct {
|
||||
ID string
|
||||
Name string
|
||||
|
||||
@@ -153,3 +153,20 @@ SELECT EXISTS (SELECT 1 FROM sources WHERE path = $1);
|
||||
|
||||
-- name: DeriveSources :exec
|
||||
SELECT derive_sources();
|
||||
|
||||
-- name: InsertCaptureRefusal :exec
|
||||
INSERT INTO capture_refusals (source, addr, refused_at) VALUES ($1, $2, $3);
|
||||
|
||||
-- name: DeleteCaptureRefusalsBefore :exec
|
||||
DELETE FROM capture_refusals WHERE refused_at < $1;
|
||||
|
||||
-- name: CaptureRefusalSummary :many
|
||||
WITH r AS (
|
||||
SELECT source, count(*) AS refused, array_agg(DISTINCT addr ORDER BY addr)::text[] AS addrs
|
||||
FROM capture_refusals WHERE refused_at >= $1 GROUP BY source
|
||||
), a AS (
|
||||
SELECT source, count(*) AS accepted FROM captures WHERE received_at >= $1 GROUP BY source
|
||||
)
|
||||
SELECT r.source, r.refused, COALESCE(a.accepted, 0)::bigint AS accepted, r.addrs
|
||||
FROM r LEFT JOIN a ON a.source = r.source
|
||||
ORDER BY r.source;
|
||||
Reference in new issue
Block a user