FORGE-503 Refuse or detach, not 500, when deleting a database credential secret #1331
No reviewers
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
gmackie/ForgeGraph!1331
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "FORGE-503-secret-rm-500"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fixes FORGE-503 (https://tasks.gmac.io/gmacko/issue/FORGE-503):
forge secret rm DATABASE_URL_LOCAL --stage productionfor app fabforge returned a bodyless HTTP 500.Root cause
forge db create(andforge db rotate-password) store the stage'sDATABASE_URL_LOCALand link it fromnode_database_bindings.credential_secret_id. That foreign key has no ON DELETE action, soDELETE /api/fg/secrets/:keydid a baredb.delete(secrets), Postgres raised 23503 (node_database_bindings_credential_secret_id_secrets_id_fk), and the unhandled throw became a 500 with no body.Evidence (read-only, metadata only): fabforge production's
DATABASE_URL_LOCALrow (id 59952bbd…) is thecredential_secret_idof binding 3426d2ae… (fabforge_production, created 2026-05-23 08:36:49, one second before the secret). It is the only FK intosecrets; 38 bindings in production carry a credential, so everyforge db createapp hits this. All 15 fabforge production rows use the sameage:format, so decryption, key versions and row shape are not the cause. The regression test reproduces the exact 23503 against the old route.Fix
packages/api/src/lib/secret-delete.ts:deleteSecret()looks up bindings that use the secret as their credential. Without consent it returnsin_use; withdetachDatabaseBindingsit nullscredential_secret_idand deletes the row in oneatomic()write. A 23503 from a race is mapped toin_usetoo.DELETE /api/fg/secrets/:key: 409 with an error naming the binding (db, node, stage) and the way forward;?detachDatabase=truedetaches and deletes (200 withdetachedBindings).forge secret rm --detach-databasesends that flag.secret.deleteuses the same helper (CONFLICT instead of an internal error).GET /api/fg/secrets/:key: 422 naming the key when the row cannot be decrypted, instead of a 500.forge secret get FORGEJO_API_TOKEN --stage productionreturns 0 today (checked 2026-12-05, value not printed), so the GET symptom did not reproduce; the GET change is defensive.Tests
apps/web/src/app/api/fg/secrets/[id]/route.database.test.ts: 409 + row kept, 422 on undecryptable row, detach + delete. Fails on the old route (23503 / decrypt throw), passes now on Postgres and the D1 lane.cli.database.test.tswith the CLI built from this branch, api/web secret unit tests, api secret database tests,go test ./cmd/fg/commands -run Secret, typecheck for api and web: all pass.Data remediation after deploy (not run):
forge secret rm DATABASE_URL_LOCAL --stage production --detach-databasefrom the fabforge repo. fabforge is on D1 now; the bindingfabforge_productionthen falls back to the stage's DATABASE_URL, which fabforge doesn't have, soforge db url/connectfor it stops working, which is the intent.🤖 Generated with Claude Code
WIP: FORGE-503 Refuse or detach, not 500, when deleting a database credential secretto FORGE-503 Refuse or detach, not 500, when deleting a database credential secret