mirror of
https://github.com/coder/coder.git
synced 2025-07-08 11:39:50 +00:00
This change adds support for workspace app auditing. To avoid audit log spam, we introduce the concept of app audit sessions. An audit session is unique per workspace app, user, ip, user agent and http status code. The sessions are stored in a separate table from audit logs to allow use-case specific optimizations. Sessions are ephemeral and the table does not function as a log. The logic for auditing is placed in the DBTokenProvider for workspace apps so that wsproxies are included. This is the final change affecting the API fo #15139. Updates #15139
42 lines
780 B
SQL
42 lines
780 B
SQL
-- name: UpsertWorkspaceAppAuditSession :one
|
|
--
|
|
-- Insert a new workspace app audit session or update an existing one, if
|
|
-- started_at is updated, it means the session has been restarted.
|
|
INSERT INTO
|
|
workspace_app_audit_sessions (
|
|
agent_id,
|
|
app_id,
|
|
user_id,
|
|
ip,
|
|
user_agent,
|
|
slug_or_port,
|
|
status_code,
|
|
started_at,
|
|
updated_at
|
|
)
|
|
VALUES
|
|
(
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9
|
|
)
|
|
ON CONFLICT
|
|
(agent_id, app_id, user_id, ip, user_agent, slug_or_port, status_code)
|
|
DO
|
|
UPDATE
|
|
SET
|
|
started_at = CASE
|
|
WHEN workspace_app_audit_sessions.updated_at > NOW() - (@stale_interval_ms::bigint || ' ms')::interval
|
|
THEN workspace_app_audit_sessions.started_at
|
|
ELSE EXCLUDED.started_at
|
|
END,
|
|
updated_at = EXCLUDED.updated_at
|
|
RETURNING
|
|
started_at;
|