Files
ca/Documentation/api/internal_events.md
dissoupovandGitHub a3d0c6811c Events implementation (#5)
* Admin: list projects

* Events implementation
2026-10-17 12:46:22 -05:00

5.2 KiB

events

import "github.com/effective-security/trustyca/internal/events"

Package events records audit events for the actions users, API keys and the service take on orgs, projects, members and users.

New starts an event with its type, title and the emitting component; the builder methods scope it to an org and project, set the referenced entity and the details. Emit hands it to a Recorder, such as db.OrgsDb, which writes it in the background and logs a failure instead of returning it, so emit only after the action it records has succeeded. ParseTimeRange validates the time filters of an event list request.

events.New(ctx, ServiceName, pb.EventType_ProjectCreated, "Project created").
	Org(orgID).
	Project(project.ID).
	Reference(project.ID).
	Description("project %q created", project.Name).
	Emit(s.db)

Index

func ParseTimeRange

func ParseTimeRange(after, before string) (*time.Time, *time.Time, error)

ParseTimeRange parses the RFC3339 bounds of a list request. An empty bound is unbounded and returns nil.

type Event

Event builds a model.Event for one action

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

func New

func New(ctx context.Context, source string, typ pb.EventType_Enum, title string) *Event

New starts an event of the given type. The actor's email is taken from the caller identity in ctx, when there is one; source names the component that emits the event, such as a service name.

func (*Event) Description

func (e *Event) Description(format string, args ...any) *Event

Description sets the human readable details of the event

func (*Event) Email

func (e *Event) Email(email string) *Event

Email overrides the actor's email, for flows where the caller identity does not carry it

func (*Event) Emit

func (e *Event) Emit(r Recorder)

Emit records the event through r, in the background

func (*Event) Meta

func (e *Event) Meta(key, value string) *Event

Meta adds a metadata entry; an empty value is skipped

func (*Event) Model

func (e *Event) Model() *model.Event

Model returns the built event

func (*Event) Org

func (e *Event) Org(id xdb.ID) *Event

Org scopes the event to an organization

func (*Event) Project

func (e *Event) Project(id xdb.ID) *Event

Project scopes the event to a project

func (*Event) Reference

func (e *Event) Reference(id xdb.ID) *Event

Reference sets the ID of the entity the event is about, such as the org, project, user, invite or API key

type Recorder

Recorder records events without blocking the caller

type Recorder interface {
    // TryCreateEvent records an event in the background, logging a failure
    // instead of returning it
    TryCreateEvent(evt *model.Event)
}

Generated by gomarkdoc