Repository navigation
feat(db): serve requests as a runtime role that can't change the audit or upload records (M3d, #107) - #140
Merged
Conversation
…t or upload records (M3d, #107) The API's request pool now connects as APP_DB_USER when it's set. The owner (PG_USER) is used only at startup, for migrations and grants. After each migration run, the runtime role gets read and write on ordinary tables, but only SELECT and INSERT on audit_log, uploads, upload_blobs, source_rows and alembic_version. It owns the per-upload csv_* tables it creates; ones the owner created before are handed over. - scripts/create_app_role.py creates the role with a random password, applies the grants, and writes the settings to ~/.te-migration/app-db.env (mode 600). It never prints the password. - start-api.ps1 loads that file and warns if the role isn't set up. - install.sh creates te_prod_runtime and writes APP_DB_* to its env file. Local CI replica 329/0/0, evals 26/26, lint and health clean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019YPoXzBSj89ZHgw7nfTmbB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #107. Part M3d, the last part of ART Milestone 3 (decision 2 on #107: the runtime role, with installer and setup changes).
What it does
APP_DB_USER. The owner (PGUSER) is used only at startup, for Alembic, tenant provisioning and grants (db.owner_connect()).control, the T&E schema and every tenant schema:audit_log,uploads,upload_blobs,source_rowsandalembic_version;csv_*tables it creates. Tables the owner created before M3d are handed to it.scripts/create_app_role.pycreates the role, or gives an existing one a new random password. It grants the role to the owner and applies the grants. It writesAPP_DB_USERandAPP_DB_PASSWORDto~/.te-migration/app-db.envwith mode 600, and never prints the password.start-api.ps1loads that file. Without it, the API warnsAPP_DB_USER not setand keeps today's behaviour (requests run as the owner).install.shcreateste_prod_runtime, idempotently, and addsAPP_DB_*to/opt/te-migration/env.Known limitation
The API process still holds the owner's password, because it runs migrations at startup. Moving migrations to a separate step would remove that. That's a deployment change, so it's not in this PR.
Verification
make lintmake healthtests/test_runtime_role.py(3 tests)create_app_role.py. The password isn't in its output and the env file is mode 600. As the runtime role, the API uploads, overwrites, deletes and verifies the chain, and the pool'scurrent_useris the runtime role. As that role, DELETE and TRUNCATE on the 4 tables, UPDATE onaudit_loganduploads, andALTER TABLE … DISABLE TRIGGERare all refused by privilege, not by the trigger. INSERT intoaudit_logstill worksinstall.sh's role commands run twice, which shows they can be re-run. The API started with the non-superuser ownerte_prod_userand the runtime rolete_prod_runtime. Upload, overwrite, delete and/api/audit/verify(intact) all worked, and every pooled connection waste_prod_runtime.create_tenant_admin.pyas that owner made a tenant, and a signed-in upload there workedstart-api.ps1pwsh). The env-file loader keeps a password containing=intactI broke the code twice to check that the tests catch it. Both times the expected test failed:
uploadswas then stopped only by the triggerNot run:
install.shend to end on an offline Ubuntu VM. That's the #60 dry run, which will exercise it.After merging, on the product owner's PC (once)
Stop the API, then run
python scripts\create_app_role.pywith the step-3PG*values set. Then start the API. Details are in QUICKSTART.Generated by Claude Code