Skip to content

template-transform on Postgres (CP-126) - #1

Merged
vallab76 merged 2 commits into
mainfrom
template
Nov 25, 2026
Merged

vallab76 merged 2 commits into
mainfrom
template

Conversation

@vallab76

Copy link
Copy Markdown
Contributor

CP-126, step 1: Postgres. The template for the new transform role: dbt models over the reporting store, published only when every check passes. Snowflake follows in a second PR. The tool side is CP-127.

Built from template-job's harness, unchanged: run lock, window and catch-up, time limit, job-missed and job-failed, audit and tracing. src/job/work.py calls src/transform/run.py.

A run

  1. Revokes expired break-glass access (audited).
  2. Builds in <schema>_build, with each incremental mart's live table copied in.
  3. Runs dbt build: models and tests, all blocking, control totals included.
  4. Checks data classes on what was built.
  5. Applies retention.
  6. Publishes: the build schema is swapped in, in one transaction. Readers' grants, tenant row security and security_invoker views are re-applied. A failure anywhere before this publishes nothing, and readers keep the last good marts.
  7. Keeps evidence: manifest, run results, the data-class report and a summary. It logs a valxel.transform.run line and a transform.published audit record.

The design's acceptance criteria

# Criterion Where Test
1 Data classes through SQL; a downgrade fails transform/classes.py (sqlglot column lineage): valxel_tokenize, valxel_generalize_year, and count/sum/avg under valxel_min_group(11) de-identify; secret never does. transform-check in CI test_classes.py (17 cases); a downgraded mart publishes nothing
2 Control totals, blocking valxel_control_total (equal / at_most, tolerance) the example marts' 3 totals
3 A role per transform reading only what it declares transform-admin grants renders it from sources.yml test_the_grants_name_only_the_declared_sources
4 Only writer; people read-only; break-glass time-boxed and audited pg.reader_grants, transform-admin break-glass (max 240 min) reader sees its tenant only and can't read staging; break-glass is revoked by the next run
5 Query logs to the audit topic platform: pgaudit on the managed Postgres —
6 Deletions through every model valxel_deletions hook a generic test deletes a source row and proves it's gone from every mart carrying its key
7 Evidence per run run._evidence files present; the tokenize key isn't in any of them

Worth a careful look

  • Tokens: HMAC-SHA256 in valxel.tokenize. The key reaches each session only through libpq's PGOPTIONS, from a mounted secret, so it isn't in SQL, dbt's logs or artifacts. A session with no key, or a short one, fails instead of producing guessable tokens.
  • Two problems found while building, fixed and guarded:
    • A deletions hook written unquoted renders at parse time, compares the model with itself, and deletes nothing. The control totals caught it; transform-check now fails it.
    • A staging view readable by report logins would bypass tenant row security, since views run as their owner, and expose raw PHI. Now only marts are granted, and every view is security_invoker.
  • Conformance: the image does a real seeded run. TRANSFORM_SEED_SYNTHETIC is refused outside ci and dev. The flags check is dropped because a transform reads no flag.

Checks run here

  • ruff, mypy and pytest pass (95.6% coverage with Postgres).
  • Conformance passes in local mode.
  • The image build couldn't run here (no Docker daemon); CI runs it.

Needs a repo setting

Please mark the repo as a template repository (Settings → Template repository), like the other template-* repos. This session can't change repo settings.


Generated by Claude Code

…ly when every check passes (CP-126)

Built from template-job's harness, unchanged (run lock, window and
catch-up, time limit, job-missed and job-failed alerts, audit, tracing).

- A run builds every model in <schema>_build, runs dbt's tests and the
  template's control totals (all blocking), checks data classes, applies
  retention, then swaps the build schema in, in one transaction, with
  readers' grants and tenant row security re-applied.
- Data classes through SQL (sqlglot column lineage): a column must be at
  least as sensitive as what it's made from, unless it goes through
  valxel_tokenize, valxel_generalize_year or an aggregate under
  valxel_min_group(11). transform-check prints the report; CI runs it.
- Tokens: HMAC with a key from a mounted secret, through PGOPTIONS, never
  in SQL or dbt's artifacts; no key, no tokens.
- Deletions: incremental marts remove what's gone at the source; a
  generic test proves a deleted record leaves every mart carrying it; an
  unquoted hook (rendered at parse time, removing nothing) fails the check.
- Least privilege (transform-admin grants), marts-only reader access,
  security_invoker views, time-boxed audited break-glass.
- Evidence per run (manifest, run results, data-class report, summary)
  and a valxel.transform.run line; transform.published audit record.
- transform-dev on a seeded Postgres; conformance runs a real seeded run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FsRHvp6Xroi64RED1Rqpyq
@github-advanced-security

Copy link
Copy Markdown

You are seeing this message because GitHub Code Scanning has recently been set up for this repository, or this pull request contains the workflow file for the Code Scanning tool.

What Enabling Code Scanning Means:

  • The 'Security' tab will display more code scanning analysis results (e.g., for the default branch).
  • Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results.
  • You will be able to see the analysis results for the pull request's branch on this overview once the scans have completed and the checks have passed.

For more information about GitHub Code Scanning, check out the documentation.

Comment thread src/transform/seed.py Fixed
Comment thread src/transform/seed.py Fixed
Comment thread src/transform/seed.py Fixed
Comment thread src/transform/seed.py Fixed
Comment thread src/transform/pg.py Fixed
Comment thread src/transform/pg.py Fixed
Comment thread src/transform/pg.py Fixed
Comment thread src/transform/pg.py Fixed
Comment thread .github/scripts/report.py
raise SystemExit(f"GITHUB_API_URL must be an https URL, not {api.geturl()!r}")
body = json.dumps({"state": state, "context": args.context, "description": text[:140],
"target_url": args.url})
conn = http.client.HTTPSConnection(api.hostname, api.port or 443, timeout=30)
raise ValueError(f"not a file name: {name!r}")
Path(d, name).write_text(str(value), encoding="utf-8")
os.chmod(Path(d, name), 0o640) # the image's user reads them through this group (App adds it)
os.chmod(d, 0o750)
- Semgrep's SQLAlchemy raw-query rule flags psycopg's sql composition.
  Every statement now goes through pg.execute, which takes only fixed
  text or psycopg-composed SQL, with one nosemgrep saying why.
- The tenant test builds the reader's connection with make_conninfo, so
  it works with CI's postgres:postgres URL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FsRHvp6Xroi64RED1Rqpyq
@vallab76
vallab76 merged commit 3429151 into main Nov 25, 2026
10 checks passed
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