`coder secret list` now shows the current workspace build's secrets next to your user secrets, and which user secrets they replace. Part of [RELAY-7](https://linear.app/codercom/issue/RELAY-7/agent-relay-deliver-provider-credentials-via-coder-secrets-instead-of). **Stack** (design decisions in #30248) 1. #30248 Database schema, queries, and RBAC 2. #30249 Encryption at rest 3. #30250 Set secrets on workspace builds 4. #30251 Deliver secrets to the agent 5. #30252 Docs 6. **#30388 List a build's secrets (this PR)** 7. #30459 Audit a build's secrets - New `GET /workspacebuilds/{workspacebuild}/secrets` lists a build's secrets (metadata only, never values) and which user secret each one replaces. - Inside a workspace, `coder secret list` includes the current build's secrets. Outside a workspace, use `--workspace` or `--build-id`. - The agent now exposes `CODER_WORKSPACE_BUILD_ID`, so the CLI knows which build it is running in. - The agent manifest and the new endpoint use one shared resolver, so they always agree on which secret wins. - The user secret API is unchanged. <details> <summary>Implementation details</summary> - The manifest carries `workspace_build_id`; the agent sets it as `CODER_WORKSPACE_BUILD_ID`. - `coderd/workspacesecrets.Resolve(user, build, policy)` decides which secret is delivered on each env var and file. The manifest no longer relies on entry order. - `codersdk.WorkspaceSecret` embeds `UserSecret` and adds `workspace_build_id`, `ephemeral`, `cleared_at`, `env_replaces`, and `file_replaces`. Secrets whose values a later build cleared are still listed, with `cleared_at` set. - CLI columns: `source` and `replaced by` are shown by default only when a build is included; `ephemeral` is opt-in. The CLI makes two requests: user secrets, then build secrets. - Agents that predate the build ID fall back to `CODER_WORKSPACE_ID` and its latest build. </details> <details> <summary>Decision log</summary> (existing decision log, unchanged) </details> --- *Created by Coder Agents on behalf of @Emyrk.*
115 lines
3.9 KiB
SQL
115 lines
3.9 KiB
SQL
-- name: ListActiveWorkspaceSecrets :many
|
|
-- Returns the live rows (value not yet cleared) linked to a build. Only the
|
|
-- latest build of a workspace has live rows, so an older build returns none.
|
|
-- Includes decrypted values, so this is used only by the agent manifest and
|
|
-- by the build transaction that copies secrets forward; there is no REST
|
|
-- endpoint that reads workspace secrets.
|
|
SELECT *
|
|
FROM workspace_secrets
|
|
WHERE workspace_build_id = @workspace_build_id
|
|
AND cleared_at IS NULL
|
|
ORDER BY name ASC;
|
|
|
|
-- name: InsertWorkspaceSecret :one
|
|
INSERT INTO workspace_secrets (
|
|
id,
|
|
workspace_id,
|
|
workspace_build_id,
|
|
name,
|
|
value,
|
|
value_key_id,
|
|
env_name,
|
|
file_path,
|
|
ephemeral,
|
|
source
|
|
) VALUES (
|
|
@id,
|
|
@workspace_id,
|
|
@workspace_build_id,
|
|
@name,
|
|
@value,
|
|
@value_key_id,
|
|
@env_name,
|
|
@file_path,
|
|
@ephemeral,
|
|
@source
|
|
)
|
|
RETURNING *;
|
|
|
|
-- name: ClearWorkspaceSecretsBeforeBuild :exec
|
|
-- Drops the values of every live row that belongs to an earlier build of the
|
|
-- workspace (lower build number than the given build), keeping the rows so
|
|
-- the history of which secrets earlier builds received stays inspectable.
|
|
-- Rows of the given build and of any later build are left untouched, so a
|
|
-- build that clears out of order cannot wipe a newer build's secrets. The
|
|
-- cutoff build must belong to the workspace; otherwise nothing is cleared.
|
|
UPDATE workspace_secrets
|
|
SET
|
|
value = NULL,
|
|
value_key_id = NULL,
|
|
cleared_at = CURRENT_TIMESTAMP
|
|
FROM workspace_builds
|
|
WHERE workspace_secrets.workspace_build_id = workspace_builds.id
|
|
AND workspace_secrets.workspace_id = @workspace_id
|
|
AND workspace_secrets.cleared_at IS NULL
|
|
AND workspace_builds.build_number < (
|
|
SELECT current_build.build_number
|
|
FROM workspace_builds AS current_build
|
|
WHERE current_build.id = @workspace_build_id
|
|
AND current_build.workspace_id = @workspace_id
|
|
);
|
|
|
|
-- name: ClearWorkspaceSecretsByWorkspaceID :exec
|
|
-- Drops the values of every live row of the workspace, keeping the rows as
|
|
-- history. Used when the workspace is deleted.
|
|
UPDATE workspace_secrets
|
|
SET
|
|
value = NULL,
|
|
value_key_id = NULL,
|
|
cleared_at = CURRENT_TIMESTAMP
|
|
WHERE workspace_id = @workspace_id
|
|
AND cleared_at IS NULL;
|
|
|
|
-- name: GetWorkspaceSecrets :many
|
|
-- Returns a page of workspace secrets that still hold a value across the
|
|
-- deployment, ordered by id. Pass the last returned id as after_id to fetch
|
|
-- the next page. Used only by the dbcrypt key rotation utility.
|
|
SELECT *
|
|
FROM workspace_secrets
|
|
WHERE value IS NOT NULL
|
|
AND id > @after_id::uuid
|
|
ORDER BY id
|
|
LIMIT @limit_count::int;
|
|
|
|
-- name: UpdateEncryptedWorkspaceSecretValue :one
|
|
-- Updates only the encrypted columns on a row. Used by the dbcrypt key
|
|
-- rotation utility to re-encrypt or decrypt rows in place.
|
|
-- Cleared rows are skipped: they hold no value, and a row cleared between
|
|
-- the rotation's list and this update must not have a value written back.
|
|
UPDATE workspace_secrets
|
|
SET
|
|
value = @value,
|
|
value_key_id = @value_key_id
|
|
WHERE id = @id
|
|
AND cleared_at IS NULL
|
|
RETURNING *;
|
|
|
|
-- name: GetWorkspaceSecretsHistory :many
|
|
-- Returns metadata for every workspace secret row of a workspace, including
|
|
-- cleared rows, so the secrets each build received can be inspected. Values
|
|
-- are never selected. The workspace owner and organization are included for
|
|
-- authorization. A non-nil workspace_build_id limits the rows to that build.
|
|
SELECT
|
|
ws.id, ws.workspace_id, ws.workspace_build_id, ws.name,
|
|
ws.env_name, ws.file_path, ws.ephemeral, ws.source, ws.created_at, ws.cleared_at,
|
|
w.owner_id AS workspace_owner_id,
|
|
w.organization_id AS workspace_organization_id
|
|
FROM workspace_secrets ws
|
|
JOIN workspaces w ON w.id = ws.workspace_id
|
|
WHERE ws.workspace_id = @workspace_id
|
|
AND (
|
|
@workspace_build_id::uuid = '00000000-0000-0000-0000-000000000000'::uuid
|
|
OR ws.workspace_build_id = @workspace_build_id
|
|
)
|
|
ORDER BY ws.created_at ASC, ws.name ASC;
|