audit

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const AuditTableDDL = `` /* 839-byte string literal not displayed */

AuditTableDDL is the CREATE TABLE statement for the audit events table. Call PGStore.Migrate to execute it.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	ID         string         `json:"id"`
	ActorID    string         `json:"actor_id"`
	ActorEmail string         `json:"actor_email"`
	TenantID   string         `json:"tenant_id"`
	Action     string         `json:"action"`
	Resource   string         `json:"resource"`
	ResourceID string         `json:"resource_id"`
	OldValues  map[string]any `json:"old_values,omitempty"`
	NewValues  map[string]any `json:"new_values,omitempty"`
	Metadata   map[string]any `json:"metadata,omitempty"`
	IPAddress  string         `json:"ip_address"`
	UserAgent  string         `json:"user_agent"`
	RequestID  string         `json:"request_id"`
	CreatedAt  time.Time      `json:"created_at"`
}

type MemoryStore

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

func NewMemoryStore

func NewMemoryStore() *MemoryStore

func (*MemoryStore) List

func (s *MemoryStore) List(_ context.Context, query Query) ([]Event, error)

func (*MemoryStore) Record

func (s *MemoryStore) Record(_ context.Context, event Event) error

type PGStore

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

PGStore is a PostgreSQL-backed audit Store. Use NewPGStore to construct it, then call Migrate once at startup.

func NewPGStore

func NewPGStore(pool *pgxpool.Pool) *PGStore

NewPGStore creates a PGStore backed by the supplied connection pool.

func (*PGStore) List

func (s *PGStore) List(ctx context.Context, query Query) ([]Event, error)

List returns audit events matching the query in descending creation order. Limit defaults to 100 and is capped at 200.

func (*PGStore) Migrate

func (s *PGStore) Migrate(ctx context.Context) error

Migrate creates the audit events table and indexes if they do not exist. Call this once at application startup.

func (*PGStore) Record

func (s *PGStore) Record(ctx context.Context, event Event) error

Record inserts a new audit event. A random ID and current timestamp are generated automatically when the event does not provide them.

type Query

type Query struct {
	ActorID  string
	TenantID string
	Resource string
	Action   string
	From     time.Time
	To       time.Time
	Limit    int
}

type Reader

type Reader interface {
	List(ctx context.Context, query Query) ([]Event, error)
}

type Store

type Store interface {
	Writer
	Reader
}

type Writer

type Writer interface {
	Record(ctx context.Context, event Event) error
}

Jump to

Keyboard shortcuts

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