dbaudit

package
v1.5.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dbaudit records every database explorer action off the request path. A full queue drops the event and counts it: audit must never block a query, but the loss has to be visible.

Index

Constants

View Source
const (
	OutcomeOK        = "ok"
	OutcomeDenied    = "denied"
	OutcomeRejected  = "rejected"
	OutcomeTimeout   = "timeout"
	OutcomeCancelled = "cancelled"
	OutcomeError     = "error"
)

Outcomes and operation kinds the explorer reports.

Variables

This section is empty.

Functions

func RunSweeper

func RunSweeper(ctx context.Context, sink Sink, set Settings, firstDelay, interval time.Duration, log *slog.Logger)

RunSweeper sweeps once after firstDelay, then every interval until ctx ends.

func Sweep

func Sweep(ctx context.Context, sink Sink, set Settings, now time.Time) (int64, error)

Sweep deletes rows past the retention window, then the oldest rows beyond the row cap, once.

Types

type Event

type Event struct {
	At            time.Time
	DatabaseName  string
	DatabaseID    string
	Engine        string
	PrincipalType string
	PrincipalID   string
	PrincipalName string
	Ability       string
	Action        string
	OpKind        string
	Fingerprint   string
	StatementText string
	RowsReturned  int64
	Bytes         int64
	DurationMs    int64
	Outcome       string
	ErrorClass    string
	ClientIP      string
	ClientKind    string
	BranchName    string
	RequestID     string
}

Event is one explorer action. StatementText is dropped unless the setting allows it.

type Options

type Options struct {
	FlushInterval time.Duration
	FlushRows     int
	Logger        *slog.Logger
}

Options tune the writer; zero values use the defaults below, which callers seed from env.

type Reservation

type Reservation struct {
	// contains filtered or unexported fields
}

Reservation is one queue slot held for a request that must be audited.

func (*Reservation) Release

func (r *Reservation) Release()

Release gives the slot back; calling it after RecordReserved or twice is harmless.

type ServiceSettings

type ServiceSettings struct{ Svc *runtimesettings.Service }

ServiceSettings adapts the live settings service.

func (ServiceSettings) MaxRows

func (s ServiceSettings) MaxRows(ctx context.Context) int

MaxRows is the live row cap the sweeper enforces.

func (ServiceSettings) QueueSize

func (s ServiceSettings) QueueSize(ctx context.Context) int

QueueSize is the live queue bound.

func (ServiceSettings) RetentionDays

func (s ServiceSettings) RetentionDays(ctx context.Context) int

RetentionDays is the live retention window.

func (ServiceSettings) StoreText

func (s ServiceSettings) StoreText(ctx context.Context) bool

StoreText reports whether statement text may be stored.

func (ServiceSettings) TextMaxBytes

func (s ServiceSettings) TextMaxBytes(ctx context.Context) int

TextMaxBytes is the live bound on stored statement text.

type Settings

type Settings interface {
	StoreText(ctx context.Context) bool
	QueueSize(ctx context.Context) int
	RetentionDays(ctx context.Context) int
	MaxRows(ctx context.Context) int
	TextMaxBytes(ctx context.Context) int
}

Settings are read on every call so a live change applies at once.

type Sink

type Sink interface {
	InsertDatabaseStatementAudits(ctx context.Context, rows []store.DatabaseStatementAudit) error
	PruneDatabaseStatementAudits(ctx context.Context, cutoff time.Time, chunk int) (int64, error)
	TrimDatabaseStatementAudits(ctx context.Context, maxRows, chunk int) (int64, error)
}

Sink is the store surface the writer and sweeper need.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer batches events and flushes them on an interval or when enough rows wait.

func New

func New(sink Sink, set Settings, o Options) *Writer

New builds a writer; call Start before Record and Close at shutdown.

func (*Writer) Close

func (w *Writer) Close()

Close stops the loop and flushes what is left.

func (*Writer) CountUnauthenticated

func (w *Writer) CountUnauthenticated()

CountUnauthenticated counts a request that had no principal; no row is written for it.

func (*Writer) Dropped

func (w *Writer) Dropped() uint64

Dropped counts events lost to a full queue or a failed id; Failed counts rows a flush could not write.

func (*Writer) Failed

func (w *Writer) Failed() uint64

Failed counts rows lost to a failed flush.

func (*Writer) Flush

func (w *Writer) Flush(ctx context.Context)

Flush writes everything queued now. A failed batch is retried once, then written row by row so one bad row costs only itself; rows that still fail are counted and logged.

func (*Writer) HasRoom

func (w *Writer) HasRoom(ctx context.Context) bool

HasRoom reports whether the queue can take one more event; a write path asks before it acts.

func (*Writer) Record

func (w *Writer) Record(ctx context.Context, ev Event)

Record queues one event and never blocks. A full queue drops it and counts the drop.

func (*Writer) RecordReserved

func (w *Writer) RecordReserved(ctx context.Context, ev Event, r *Reservation)

RecordReserved queues the event into the slot the reservation holds.

func (*Writer) Reserve

func (w *Writer) Reserve(ctx context.Context) (*Reservation, bool)

Reserve holds one slot so a write that passed the room check cannot lose its event to readers filling the queue meanwhile. Release it when the request ends; RecordReserved consumes it.

func (*Writer) Start

func (w *Writer) Start()

Start runs the flush loop until Close.

func (*Writer) Unauthenticated

func (w *Writer) Unauthenticated() uint64

Unauthenticated counts explorer requests that carried no principal and wrote no row.

func (*Writer) WriteMetrics

func (w *Writer) WriteMetrics(out io.Writer, prefix string) error

WriteMetrics renders the loss counters in the Prometheus text format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL