Skip to content

Add read-only DB role for each tenant - #259

Draft
kiwitcms-bot wants to merge 1 commit into
read-only-database-role-modelfrom
read-only-database-role
Draft

kiwitcms-bot wants to merge 1 commit into
read-only-database-role-modelfrom
read-only-database-role

Conversation

@kiwitcms-bot

@kiwitcms-bot kiwitcms-bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

What

Adds a new ReadOnlyDatabaseRole model and a helper that creates a real read-only PostgreSQL role for a tenant schema.

Stacked on #260 (the model + migration + model tests). Review/merge that one first; this PR only contains the integration diff (2 files).

Fields: name, created_at, valid_until, password.

ReadOnlyDatabaseRole.create_for_schema(schema_name, valid_until):

  • name = ro_for_<schema_name> (READ_ONLY_ROLE_PREFIX is a class attribute)
  • password = random UUID
  • created_at = now
  • valid_until = passed in (tenant.paid_until)

It stores the model record and creates the DB role via raw SQL, escaping identifiers/values with psycopg's sql.Identifier / sql.Literal. The role gets LOGIN + ENCRYPTED PASSWORD + VALID UNTIL, USAGE on the schema, SELECT on all its tables, and ALTER DEFAULT PRIVILEGES ... GRANT SELECT ON TABLES for future tables. No write privileges.

This PR (integration)

  • create: CreateTenant.form_valid() creates the role after the tenant (and its schema) exists, so schema_name and paid_until are known.
  • update: on recurring billing the same block that updates Tenant.paid_until calls role.update_valid_until(tenant.paid_until), updating both the model and the DB role in one transaction.
  • tests/test_views_createview.py: creating a tenant creates ro_for_<schema_name>.

Follow-up (not in this PR)

Expose / propagate the role name + password to the customer in the subscription page, similar to how the Private Token is shown today.

black, flake8 and pylint (10.00/10) are clean; tests pass. The existing test_docker.py failures are unrelated (they need a real Quay.io token/network).

@atodorov
atodorov marked this pull request as draft October 6, 2026 15:21
@kiwitcms-bot
kiwitcms-bot force-pushed the read-only-database-role branch 4 times, most recently from 95c7478 to 498b0ca Compare October 6, 2026 16:25
@kiwitcms-bot
kiwitcms-bot changed the base branch from master to read-only-database-role-model October 6, 2026 16:25
@kiwitcms-bot
kiwitcms-bot force-pushed the read-only-database-role-model branch from 37782c8 to 7a681ff Compare October 6, 2026 16:36
CreateTenant.form_valid() creates the read-only DB role for the newly
created tenant schema, after the tenant exists so we know its
schema_name and paid_until. On recurring billing the role's `valid
until` attribute is updated together with Tenant.paid_until.
@kiwitcms-bot
kiwitcms-bot force-pushed the read-only-database-role branch from 498b0ca to 5a27ec0 Compare October 6, 2026 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants