Files
Primora/apps/backend/internal/database/db/api_keys.sql.go
T
Tomas DvorakandDevin 55edbf8b56 feat: phase 6 production hardening + DB-to-DB transfer
Security:
- Graceful shutdown: http.Server + signal.NotifyContext, 10s drain,
  retention sweeper cancelled before pool close
- Scoped API keys (migration 00006): ingest/read/write/admin enforced in
  auth middleware; scope picker + badges in Settings UI; scopeless legacy
  keys act as admin. Read-only keys now get 403 on ingest (verified live)
- nginx: baseline security headers on all responses, CSP scoped to SPA;
  fixed add_header inheritance shadowing
- better-auth rate limit enabled unconditionally (was gated on NODE_ENV,
  which the shipped .env sets to development)
- Mailpit moved to docker-compose.dev.yml; prod compose cannot expose it
- setup.sh generates PRIMORA_ENCRYPTION_KEY

Reliability/ops:
- Retention: per-project retention_{events,audit,webhook}_days
  (migration 00007, 0=off) + hourly RetentionSweeper; edit UI in Projects
- GET /api/v1/metrics Prometheus exposition
- compose: healthchecks + restart policies everywhere, dragonfly pinned
  to v2.0.0, depends_on service_healthy
- backup.sh/restore.sh (pg_dump -Fc + storage volume); restore verified
  against scratch postgres+volume
- .dockerignore: exclude target dirs (build context was 4.7GB)

Correctness/testing:
- DBX transfer endpoint: POST /db-connections/:id/transfer — SQL or Redis
  target, 1000-row bound, server-side creds (verified live pg→dragonfly)
- Pagination audit: ListComponents capped at 500 (auto-registers)
- CI: goose up/reset/up job + compose e2e-smoke job (smoke-e2e.sh
  verified locally: signup, bootstrap, scoped keys, ingest, metrics)
- k6 load sanity script: 6650 reqs, ~217/s, 0 real failures, p95 221ms
  (429s = configured rate limiter working)

Docs: DEPLOYMENT_GUIDE.md, PRODUCTION_READINESS.md; verify script
repaired (stale refs + set -e arithmetic abort). ROADMAP: phase 2 +
phase 6 marked complete.

Authored By: TDvorak <[email protected]>

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-17 09:15:42 +02:00

211 lines
5.1 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
// source: api_keys.sql
package db
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
const createAPIKey = `-- name: CreateAPIKey :one
INSERT INTO core.api_keys (
project_id,
name,
prefix,
secret_hash,
scopes,
created_by_user_id
) VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, project_id, name, prefix, secret_hash, created_by_user_id, last_used_at, revoked_at, created_at, scopes
`
type CreateAPIKeyParams struct {
ProjectID uuid.UUID `json:"project_id"`
Name string `json:"name"`
Prefix string `json:"prefix"`
SecretHash []byte `json:"secret_hash"`
Scopes []string `json:"scopes"`
CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
}
func (q *Queries) CreateAPIKey(ctx context.Context, arg CreateAPIKeyParams) (CoreApiKey, error) {
row := q.db.QueryRow(ctx, createAPIKey,
arg.ProjectID,
arg.Name,
arg.Prefix,
arg.SecretHash,
arg.Scopes,
arg.CreatedByUserID,
)
var i CoreApiKey
err := row.Scan(
&i.ID,
&i.ProjectID,
&i.Name,
&i.Prefix,
&i.SecretHash,
&i.CreatedByUserID,
&i.LastUsedAt,
&i.RevokedAt,
&i.CreatedAt,
&i.Scopes,
)
return i, err
}
const getAPIKeyByIDForProject = `-- name: GetAPIKeyByIDForProject :one
SELECT id, project_id, name, prefix, secret_hash, created_by_user_id, last_used_at, revoked_at, created_at, scopes FROM core.api_keys
WHERE project_id = $1
AND id = $2
`
type GetAPIKeyByIDForProjectParams struct {
ProjectID uuid.UUID `json:"project_id"`
ID uuid.UUID `json:"id"`
}
func (q *Queries) GetAPIKeyByIDForProject(ctx context.Context, arg GetAPIKeyByIDForProjectParams) (CoreApiKey, error) {
row := q.db.QueryRow(ctx, getAPIKeyByIDForProject, arg.ProjectID, arg.ID)
var i CoreApiKey
err := row.Scan(
&i.ID,
&i.ProjectID,
&i.Name,
&i.Prefix,
&i.SecretHash,
&i.CreatedByUserID,
&i.LastUsedAt,
&i.RevokedAt,
&i.CreatedAt,
&i.Scopes,
)
return i, err
}
const getAPIKeyByPrefix = `-- name: GetAPIKeyByPrefix :one
SELECT
ak.id, ak.project_id, ak.name, ak.prefix, ak.secret_hash, ak.created_by_user_id, ak.last_used_at, ak.revoked_at, ak.created_at, ak.scopes,
p.organization_id
FROM core.api_keys ak
JOIN core.projects p ON p.id = ak.project_id
WHERE ak.prefix = $1
`
type GetAPIKeyByPrefixRow struct {
ID uuid.UUID `json:"id"`
ProjectID uuid.UUID `json:"project_id"`
Name string `json:"name"`
Prefix string `json:"prefix"`
SecretHash []byte `json:"secret_hash"`
CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
LastUsedAt pgtype.Timestamptz `json:"last_used_at"`
RevokedAt pgtype.Timestamptz `json:"revoked_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
Scopes []string `json:"scopes"`
OrganizationID uuid.UUID `json:"organization_id"`
}
func (q *Queries) GetAPIKeyByPrefix(ctx context.Context, prefix string) (GetAPIKeyByPrefixRow, error) {
row := q.db.QueryRow(ctx, getAPIKeyByPrefix, prefix)
var i GetAPIKeyByPrefixRow
err := row.Scan(
&i.ID,
&i.ProjectID,
&i.Name,
&i.Prefix,
&i.SecretHash,
&i.CreatedByUserID,
&i.LastUsedAt,
&i.RevokedAt,
&i.CreatedAt,
&i.Scopes,
&i.OrganizationID,
)
return i, err
}
const listAPIKeysForProject = `-- name: ListAPIKeysForProject :many
SELECT id, project_id, name, prefix, secret_hash, created_by_user_id, last_used_at, revoked_at, created_at, scopes FROM core.api_keys
WHERE project_id = $1
ORDER BY created_at DESC
`
func (q *Queries) ListAPIKeysForProject(ctx context.Context, projectID uuid.UUID) ([]CoreApiKey, error) {
rows, err := q.db.Query(ctx, listAPIKeysForProject, projectID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []CoreApiKey
for rows.Next() {
var i CoreApiKey
if err := rows.Scan(
&i.ID,
&i.ProjectID,
&i.Name,
&i.Prefix,
&i.SecretHash,
&i.CreatedByUserID,
&i.LastUsedAt,
&i.RevokedAt,
&i.CreatedAt,
&i.Scopes,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const revokeAPIKey = `-- name: RevokeAPIKey :one
UPDATE core.api_keys
SET revoked_at = NOW()
WHERE project_id = $1
AND id = $2
AND revoked_at IS NULL
RETURNING id, project_id, name, prefix, secret_hash, created_by_user_id, last_used_at, revoked_at, created_at, scopes
`
type RevokeAPIKeyParams struct {
ProjectID uuid.UUID `json:"project_id"`
ID uuid.UUID `json:"id"`
}
func (q *Queries) RevokeAPIKey(ctx context.Context, arg RevokeAPIKeyParams) (CoreApiKey, error) {
row := q.db.QueryRow(ctx, revokeAPIKey, arg.ProjectID, arg.ID)
var i CoreApiKey
err := row.Scan(
&i.ID,
&i.ProjectID,
&i.Name,
&i.Prefix,
&i.SecretHash,
&i.CreatedByUserID,
&i.LastUsedAt,
&i.RevokedAt,
&i.CreatedAt,
&i.Scopes,
)
return i, err
}
const touchAPIKey = `-- name: TouchAPIKey :exec
UPDATE core.api_keys
SET last_used_at = NOW()
WHERE id = $1
`
func (q *Queries) TouchAPIKey(ctx context.Context, id uuid.UUID) error {
_, err := q.db.Exec(ctx, touchAPIKey, id)
return err
}