Documentation
¶
Overview ¶
Package audit persists control-plane mutations into a queryable, tenant-scoped log. Two scopes: tenant events are readable by the org's admins (/api/teams/{id}/audit); platform events (super-admin actions on orgs/users) are super-admin only (/api/admin/audit).
Writes are best-effort from the HTTP handlers (detached, logged on failure) — the audit trail is an operations/compliance surface, not a transactional ledger.
Index ¶
Constants ¶
const RetentionDays = 400
RetentionDays bounds audit retention (Mongo TTL). 400 days covers an annual compliance cycle with margin; longer retention belongs in an exported archive, not the live collection.
Variables ¶
var ErrNotFound = errors.New("audit: not found")
ErrNotFound is reserved for symmetric store semantics.
Functions ¶
Types ¶
type Event ¶
type Event struct {
ID string `bson:"_id" json:"id"`
Scope Scope `bson:"scope" json:"scope"`
TenantID string `bson:"tenant_id,omitempty" json:"tenant_id,omitempty"`
ActorID string `bson:"actor_id,omitempty" json:"actor_id,omitempty"`
ActorKind string `bson:"actor_kind,omitempty" json:"actor_kind,omitempty"` // user|super_admin|webhook|system
Action string `bson:"action" json:"action"`
Target string `bson:"target,omitempty" json:"target,omitempty"` // kind of object acted on (org|user|webhook|secret|binding|byok|member|invitation|token)
TargetID string `bson:"target_id,omitempty" json:"target_id,omitempty"`
Meta map[string]any `bson:"meta,omitempty" json:"meta,omitempty"`
IP string `bson:"ip,omitempty" json:"ip,omitempty"`
UserAgent string `bson:"user_agent,omitempty" json:"user_agent,omitempty"`
CreatedAt time.Time `bson:"created_at" json:"created_at"`
}
Event is one audit row. Action is a stable dotted token (e.g. "org.status_changed", "byok.created", "webhook.rotated") — the queryable contract; Meta carries small action-specific details (never secret material).
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is the in-process audit log for tests and local mode. Keep semantics in lock-step with MongoStore.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
func (*MemoryStore) ListByTenant ¶
func (*MemoryStore) ListPlatform ¶
type MongoStore ¶
type MongoStore struct {
// contains filtered or unexported fields
}
MongoStore is the production audit log.
func NewMongoStore ¶
func NewMongoStore(db *mongo.Database) *MongoStore
func (*MongoStore) ListByTenant ¶
func (*MongoStore) ListPlatform ¶
type Store ¶
type Store interface {
Insert(ctx context.Context, e Event) error
// ListByTenant returns tenant-scoped events for one org, newest
// first.
ListByTenant(ctx context.Context, tenantID string, p Page) ([]Event, error)
// ListPlatform returns platform-scoped events, newest first.
ListPlatform(ctx context.Context, p Page) ([]Event, error)
}
Store persists and lists audit events. Implementations: MongoStore (production) and MemoryStore (tests/local). Keep in lock-step.