db

package
v0.4.9 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyStillActive = errors.New("api key is still active")

ErrKeyStillActive is returned by PurgeAPIKey for a key that has not been revoked.

Functions

func APIKeyNames added in v0.4.7

func APIKeyNames(ctx context.Context, db *sqlx.DB, actors []string) (map[string]string, error)

APIKeyNames maps actor identifiers (API key UUIDs, or "master") to key names. Identifiers that are not UUIDs or belong to a deleted key are left out, so callers fall back to showing the raw identifier.

func AddOrgMember

func AddOrgMember(ctx context.Context, db *sqlx.DB, m *models.OrgMember) error

func AuditPredsBySeq added in v0.4.6

func AuditPredsBySeq(ctx context.Context, q ExecQuerier, seqs []int64) (map[int64]AuditPred, error)

AuditPredsBySeq returns the signature and timestamp stored for each requested sequence number.

func Connect

func Connect(cfg config.DatabaseConfig) (*sqlx.DB, error)

func CountAPIKeys

func CountAPIKeys(ctx context.Context, db *sqlx.DB) (int, error)

func CountActiveSessionsByActor

func CountActiveSessionsByActor(ctx context.Context, db *sqlx.DB, actor string) (int, error)

CountActiveSessionsByActor returns the number of active (not stopped) sessions for a given actor. Used to enforce per-actor session limits.

func CountArtifacts

func CountArtifacts(ctx context.Context, db *sqlx.DB, actor string) (int, error)

func CountAuditLogs

func CountAuditLogs(ctx context.Context, db *sqlx.DB, sessionID *uuid.UUID, actor string) (int, error)

CountAuditLogs mirrors the filter logic of ListAuditLogs for accurate totals.

func CountAuditLogsFiltered added in v0.4.2

func CountAuditLogsFiltered(ctx context.Context, db *sqlx.DB, f AuditFilter) (int, error)

func CountFiles

func CountFiles(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID) (int, error)

func CountOrgSessions

func CountOrgSessions(ctx context.Context, db *sqlx.DB, orgID uuid.UUID) (int, error)

func CountOrgs

func CountOrgs(ctx context.Context, db *sqlx.DB) (int, error)

func CountRuns

func CountRuns(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID) (int, error)

func CountRunsGlobal

func CountRunsGlobal(ctx context.Context, db *sqlx.DB) (int, error)

func CountSessions

func CountSessions(ctx context.Context, db *sqlx.DB) (int, error)

func CountSessionsFiltered

func CountSessionsFiltered(ctx context.Context, db *sqlx.DB, actor, labelKey, labelValue string) (int, error)

CountSessionsFiltered returns the number of active sessions matching the same filters as ListSessions (actor + optional label key/value).

func CountSessionsForActor

func CountSessionsForActor(ctx context.Context, db *sqlx.DB, actor, labelKey, labelValue string) (int, error)

CountSessionsForActor mirrors ListSessionsForActor for pagination totals.

func CreateAPIKey

func CreateAPIKey(ctx context.Context, db *sqlx.DB, k *models.APIKey) error

func CreateArtifact

func CreateArtifact(ctx context.Context, db *sqlx.DB, a *models.SharedArtifact) error

func CreateAuditLog

func CreateAuditLog(ctx context.Context, db sqlx.ExtContext, a *models.AuditLog) error

func CreateOrg

func CreateOrg(ctx context.Context, db *sqlx.DB, o *models.Organization) error

func CreateRun

func CreateRun(ctx context.Context, db *sqlx.DB, r *models.Run) error

func CreateSession

func CreateSession(ctx context.Context, db *sqlx.DB, s *models.Session) error

func CreateSnapshot

func CreateSnapshot(ctx context.Context, db *sqlx.DB, s *models.Snapshot) error

func DeleteArtifact

func DeleteArtifact(ctx context.Context, db *sqlx.DB, id uuid.UUID) (string, error)

func DeleteAuditSeqThrough added in v0.4.6

func DeleteAuditSeqThrough(ctx context.Context, q ExecQuerier, seq int64) (int64, error)

DeleteAuditSeqThrough deletes the chained entries numbered 1 through seq and returns how many rows went. The caller has already checked that they form an intact, old prefix of the chain.

func DeleteFile

func DeleteFile(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID, path string) error

func DeleteOldAuditLogs

func DeleteOldAuditLogs(ctx context.Context, db *sqlx.DB, before time.Time) (int64, error)

DeleteOldAuditLogs removes audit log entries whose timestamp is older than the given cutoff. Returns the number of rows deleted. Called by the background cleanup goroutine when AUDIT_LOG_RETENTION_DAYS > 0.

func DeleteOrg

func DeleteOrg(ctx context.Context, db *sqlx.DB, id uuid.UUID) error

func DeleteSnapshot

func DeleteSnapshot(ctx context.Context, db *sqlx.DB, id uuid.UUID) (string, error)

func DeleteUnchainedAuditBefore added in v0.4.6

func DeleteUnchainedAuditBefore(ctx context.Context, q ExecQuerier, before time.Time, chainStart *time.Time) (int64, error)

DeleteUnchainedAuditBefore deletes entries that never had a chain number and are older than before. When chainStart is set, only those written before chaining began count: an unnumbered entry from later is not legacy, it is an entry whose number was removed, and it is evidence.

func FailSession added in v0.4.1

func FailSession(ctx context.Context, db *sqlx.DB, id uuid.UUID) error

FailSession marks a session whose container could not be created. It sets stopped_at as well so the session no longer counts as active (quota, listings).

func FailStalePendingRuns

func FailStalePendingRuns(ctx context.Context, db *sqlx.DB, before time.Time) (int64, error)

FailStalePendingRuns marks as "failed" any runs that are still in the "pending" state and were created before the given cutoff timestamp.

This is called at startup to clean up runs that were mid-flight when the previous server instance was killed (e.g., a crash or SIGKILL during an in-memory queue drain). Without this cleanup, such runs sit in "pending" forever and are never surfaced to callers as failures.

For the Redis Streams backend, the reaper goroutine handles re-delivery; this function is a last-resort safety net for both queue backends.

func FirstPrefixedAuditTime added in v0.4.4

func FirstPrefixedAuditTime(ctx context.Context, db *sqlx.DB, prefix string) (*time.Time, error)

FirstPrefixedAuditTime returns the timestamp of the earliest audit entry whose signature starts with prefix, or nil when there is none.

func GetAPIKeyAllowedSecrets added in v0.4.1

func GetAPIKeyAllowedSecrets(ctx context.Context, db *sqlx.DB, id uuid.UUID) ([]string, error)

GetAPIKeyAllowedSecrets returns the key's secret allowlist; a nil slice means the key is unrestricted.

func GetAPIKeyByHash

func GetAPIKeyByHash(ctx context.Context, db *sqlx.DB, keyHash string) (*models.APIKey, error)

func GetActorOrgs

func GetActorOrgs(ctx context.Context, db *sqlx.DB, principal string) ([]*models.OrgMember, error)

GetActorOrgs returns all org memberships for the given actor.

func GetArtifact

func GetArtifact(ctx context.Context, db *sqlx.DB, id uuid.UUID) (*models.SharedArtifact, error)

func GetFile

func GetFile(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID, path string) (*models.File, error)

func GetOrg

func GetOrg(ctx context.Context, db *sqlx.DB, id uuid.UUID) (*models.Organization, error)

func GetOrgBySlug

func GetOrgBySlug(ctx context.Context, db *sqlx.DB, slug string) (*models.Organization, error)

func GetOrgMember

func GetOrgMember(ctx context.Context, db *sqlx.DB, orgID uuid.UUID, principal string) (*models.OrgMember, error)

func GetOrgMemberRole

func GetOrgMemberRole(ctx context.Context, db *sqlx.DB, orgID uuid.UUID, principal string) (string, error)

GetOrgMemberRole returns the role for the given actor in the given org, or sql.ErrNoRows if they are not a member.

func GetRun

func GetRun(ctx context.Context, db *sqlx.DB, id uuid.UUID) (*models.Run, error)

func GetSession

func GetSession(ctx context.Context, db *sqlx.DB, id uuid.UUID) (*models.Session, error)

func GetSnapshot

func GetSnapshot(ctx context.Context, db *sqlx.DB, id uuid.UUID) (*models.Snapshot, error)

func InsertAuditChainHead added in v0.4.5

func InsertAuditChainHead(ctx context.Context, q ExecQuerier, h *AuditChainHead) error

InsertAuditChainHead creates the head row unless it exists.

func LastChainedAuditEntry added in v0.4.5

func LastChainedAuditEntry(ctx context.Context, q ExecQuerier) (seq int64, sig string, err error)

LastChainedAuditEntry returns the highest-numbered chained entry, or sql.ErrNoRows.

func ListAPIKeys

func ListAPIKeys(ctx context.Context, db *sqlx.DB) ([]*models.APIKey, error)

func ListActiveSessions

func ListActiveSessions(ctx context.Context, db *sqlx.DB) ([]*models.Session, error)

ListActiveSessions returns sessions that are still running (for cleanup jobs).

func ListArtifacts

func ListArtifacts(ctx context.Context, db *sqlx.DB, actor string, limit, offset int) ([]*models.SharedArtifact, error)

func ListAuditLogs

func ListAuditLogs(ctx context.Context, db *sqlx.DB, sessionID *uuid.UUID, actor string, limit, offset int) ([]*models.AuditLog, error)

ListAuditLogs returns audit log entries. When actor is non-empty only that actor's log entries are returned (tenant isolation). When sessionID is set it takes precedence. Master key passes actor="". Initialises the slice to non-nil so JSON always encodes [] instead of null.

func ListAuditLogsFiltered added in v0.4.2

func ListAuditLogsFiltered(ctx context.Context, db *sqlx.DB, f AuditFilter, limit, offset int) ([]*models.AuditLog, error)

func ListAuditSince

func ListAuditSince(ctx context.Context, db *sqlx.DB, since time.Time, limit int) ([]models.AuditLog, error)

ListAuditSince returns up to limit audit entries created after since, ordered by timestamp ASC.

func ListFiles

func ListFiles(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID) ([]*models.File, error)

func ListOrgMembers

func ListOrgMembers(ctx context.Context, db *sqlx.DB, orgID uuid.UUID) ([]*models.OrgMember, error)

func ListOrgSessions

func ListOrgSessions(ctx context.Context, db *sqlx.DB, orgID uuid.UUID, limit, offset int) ([]*models.Session, error)

ListOrgSessions returns active sessions belonging to the given org.

func ListOrgs

func ListOrgs(ctx context.Context, db *sqlx.DB, limit, offset int) ([]*models.Organization, error)

func ListRuns

func ListRuns(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID, limit, offset int) ([]*models.Run, error)

func ListSessions

func ListSessions(ctx context.Context, db *sqlx.DB, actor, labelKey, labelValue string, limit, offset int) ([]*models.Session, error)

ListSessions returns active sessions. When actor is non-empty only that actor's sessions are returned (tenant isolation). Master key passes "". labelKey/labelValue optionally filter by a specific label (both must be set). Initialises the slice to non-nil so JSON always encodes [] instead of null.

func ListSessionsForActor

func ListSessionsForActor(ctx context.Context, db *sqlx.DB, actor, labelKey, labelValue string, limit, offset int) ([]*models.Session, error)

ListSessionsForActor returns all active sessions visible to the actor: own sessions UNION sessions in any org the actor belongs to. When actor is empty (master), all sessions are returned.

func ListSnapshots

func ListSnapshots(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID) ([]*models.Snapshot, error)

func PurgeAPIKey added in v0.4.4

func PurgeAPIKey(ctx context.Context, db *sqlx.DB, id uuid.UUID) error

PurgeAPIKey deletes a revoked key's row (freeing its name for reuse). Active keys are refused: revoking first keeps deletion from being a one-step way to lose a key that is still in use.

func RemoveOrgMember

func RemoveOrgMember(ctx context.Context, db *sqlx.DB, orgID uuid.UUID, principal string) error

func RevokeAPIKey

func RevokeAPIKey(ctx context.Context, db *sqlx.DB, id uuid.UUID) error

func RunMigrations

func RunMigrations(db *sqlx.DB, migrationsPath string) error

func SetAPIKeyAllowedSecrets added in v0.4.1

func SetAPIKeyAllowedSecrets(ctx context.Context, db *sqlx.DB, id uuid.UUID, allowed []string) error

SetAPIKeyAllowedSecrets replaces the key's secret allowlist (nil = unrestricted).

func StopSession

func StopSession(ctx context.Context, db *sqlx.DB, id uuid.UUID) error

func SumWorkspaceBytes

func SumWorkspaceBytes(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID) (int64, error)

SumWorkspaceBytes returns the total size in bytes of all files tracked for the given session. Used by the upload handler to enforce MAX_WORKSPACE_MB.

func TotalArtifactBytes

func TotalArtifactBytes(ctx context.Context, db *sqlx.DB, actor string) (int64, error)

TotalArtifactBytes returns the sum of size_bytes for all artifacts owned by actor. Pass an empty actor to sum across all actors (master use).

func UpdateAPIKeyLastUsed

func UpdateAPIKeyLastUsed(ctx context.Context, db *sqlx.DB, id uuid.UUID) error

func UpdateAuditChainHead added in v0.4.5

func UpdateAuditChainHead(ctx context.Context, q ExecQuerier, h *AuditChainHead) error

func UpdateRun

func UpdateRun(ctx context.Context, db *sqlx.DB, p UpdateRunParams) error

func UpdateSessionLabels

func UpdateSessionLabels(ctx context.Context, db *sqlx.DB, id uuid.UUID, labels models.JSONB) error

UpdateSessionLabels replaces the labels map for a session.

func UpdateSessionStatus

func UpdateSessionStatus(ctx context.Context, db *sqlx.DB, id uuid.UUID, status string, containerID *string) error

func UpsertFile

func UpsertFile(ctx context.Context, db *sqlx.DB, f *models.File) error

Types

type AuditChainHead added in v0.4.5

type AuditChainHead struct {
	Seq           int64     `db:"seq"`
	LastSig       string    `db:"last_sig"`
	PrunedThrough int64     `db:"pruned_through"`
	StartedAt     time.Time `db:"started_at"`
	MAC           string    `db:"mac"`
}

AuditChainHead is the single audit_chain row: the newest chained entry, how far retention has pruned the chain and when chaining began. MAC is computed by the audit package.

func GetAuditChainHead added in v0.4.5

func GetAuditChainHead(ctx context.Context, q ExecQuerier, lock bool) (*AuditChainHead, error)

GetAuditChainHead reads the chain head; lock takes a row lock (inside a tx). sql.ErrNoRows means chaining has not started.

type AuditFilter added in v0.4.2

type AuditFilter struct {
	SessionID    *uuid.UUID
	Actor        string
	ActionPrefix string
	From, To     *time.Time // From inclusive, To exclusive
	// Seq and ID pick out a single entry (the dashboard links to the entries an
	// integrity warning names). BySeq orders by chain position instead of time.
	Seq   *int64
	ID    *uuid.UUID
	BySeq bool
}

AuditFilter narrows an audit log query. SessionID and Actor are the access scope (SessionID wins, matching the long-standing behaviour); ActionPrefix, From and To are optional user filters.

type AuditPred added in v0.4.6

type AuditPred struct {
	Sig       string
	Timestamp time.Time
}

AuditPred is what a chained entry needs to know about its predecessor.

type ExecQuerier added in v0.4.5

type ExecQuerier interface {
	sqlx.ExtContext
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}

ExecQuerier is the part of *sqlx.DB and *sqlx.Tx the audit chain needs.

type OrgWithRole added in v0.3.2

type OrgWithRole struct {
	models.Organization
	Role string `db:"role" json:"role"`
}

OrgWithRole is an organization plus the caller's membership role.

func ListOrgsForActor added in v0.3.2

func ListOrgsForActor(ctx context.Context, db *sqlx.DB, principal string) ([]OrgWithRole, error)

ListOrgsForActor returns organizations the principal belongs to, with role.

type UpdateRunParams

type UpdateRunParams struct {
	ID         uuid.UUID
	Status     string
	ExitCode   *int
	Stdout     *string
	Stderr     *string
	DurationMS *int64
	StartedAt  *time.Time
	FinishedAt *time.Time
}

Jump to

Keyboard shortcuts

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