Skip to content

Commit 6a8d97c

Browse files
rustyconoverclaude
andcommitted
catalog: rename attach_id/transaction_id to *_opaque_data, redact + AEAD-bind
The catalog `attach_id` / `transaction_id` wire fields are not framework identifiers — they are arbitrary implementation-chosen bytes the client round-trips back to the worker, and may carry credentials. Three fixes, applied to both fields: - Rename to `attach_opaque_data` / `transaction_opaque_data` everywhere (wire protocol, NewTypes, SQL columns, CLI options, generated C++/Go/TS schemas, docs) so the name reflects what they are. - Redact both in `_log_catalog_lifecycle` — short-hashed at a single chokepoint before they reach the log record, breadcrumb data, or Sentry scope tags. - On HTTP transport the worker AEAD-seals each value in an envelope whose AAD binds the caller's `(domain, principal)`; the transaction envelope additionally binds its parent attach. A value sealed for one principal — or one attach — cannot be opened by another. Subprocess/unix transports pass through unchanged (OS process ownership already enforces identity there). Also fixes a pre-existing `create_app` bug: `make_wsgi_app` takes `token_key`, not `signing_key`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent a73e03c commit 6a8d97c

80 files changed

Lines changed: 3494 additions & 2642 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/catalog-interface.md‎

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ Catalog methods are dispatched via `vgi_rpc` typed protocol methods. Each method
3838
### Type Aliases
3939

4040
```python
41-
from vgi.catalog import AttachId, TransactionId, SerializedSchema, SqlExpression
41+
from vgi.catalog import AttachOpaqueData, TransactionOpaqueData, SerializedSchema, SqlExpression
4242

43-
AttachId = NewType("AttachId", bytes) # Unique attachment identifier
44-
TransactionId = NewType("TransactionId", bytes) # Transaction identifier
43+
AttachOpaqueData = NewType("AttachOpaqueData", bytes) # Unique attachment identifier
44+
TransactionOpaqueData = NewType("TransactionOpaqueData", bytes) # Transaction identifier
4545
SerializedSchema = NewType("SerializedSchema", bytes) # Arrow schema bytes
4646
SqlExpression = NewType("SqlExpression", str) # SQL expression string
4747
```
@@ -52,21 +52,31 @@ Returned by `catalog_attach()` with attachment metadata:
5252

5353
| Field | Type | Description |
5454
|-------|------|-------------|
55-
| `attach_id` | `AttachId` | Unique identifier for this attachment |
55+
| `attach_opaque_data` | `AttachOpaqueData` | Opaque per-attachment state the implementation owns (see note below) |
5656
| `supports_transactions` | `bool` | Whether transactions are supported |
5757
| `supports_time_travel` | `bool` | Whether time travel queries work |
5858
| `catalog_version_frozen` | `bool` | Whether metadata will change |
5959
| `catalog_version` | `int` | Current version (increments on changes) |
60-
| `attach_id_required` | `bool` | Whether attach_id must be persisted |
60+
| `attach_opaque_data_required` | `bool` | Whether attach_opaque_data must be persisted |
6161
| `default_schema` | `str` | Name of the default schema (usually "main") |
6262

63+
> **`attach_opaque_data` / `transaction_opaque_data` are opaque, implementation-owned, and may carry secrets.**
64+
> They are *not* framework identifiers — they are arbitrary `bytes` your implementation returns from
65+
> `catalog_attach()` / `catalog_transaction_begin()`, and the client round-trips them back verbatim on
66+
> every subsequent call. An implementation may pack connection handles, credentials, or any state into
67+
> them. **Never log either value raw** — treat them like the `options` dict. The worker already enforces
68+
> this for its own catalog-lifecycle logs (it short-hashes both fields at a single chokepoint), and on
69+
> HTTP transport it additionally seals each value in an AEAD envelope bound to the caller's identity, so
70+
> a value minted for one principal cannot be replayed by another. Your implementation only ever sees the
71+
> plaintext; the sealing and unsealing happen transparently in the worker.
72+
6373
### SchemaInfo
6474

6575
Information about a schema in a catalog:
6676

6777
| Field | Type | Description |
6878
|-------|------|-------------|
69-
| `attach_id` | `AttachId` | Parent attachment |
79+
| `attach_opaque_data` | `AttachOpaqueData` | Parent attachment |
7080
| `name` | `str` | Schema name |
7181
| `comment` | `str \| None` | Optional description |
7282
| `tags` | `dict[str, str]` | Key-value metadata |
@@ -140,8 +150,8 @@ Result from `table_scan_function_get()` that tells the VGI DuckDB extension whic
140150
def table_scan_function_get(
141151
self,
142152
*,
143-
attach_id: AttachId,
144-
transaction_id: TransactionId | None,
153+
attach_opaque_data: AttachOpaqueData,
154+
transaction_opaque_data: TransactionOpaqueData | None,
145155
schema_name: str,
146156
name: str,
147157
at_unit: str | None,
@@ -178,15 +188,15 @@ class MyCatalog(CatalogInterface):
178188
"""Attach to a catalog, returning attachment metadata."""
179189

180190
@abstractmethod
181-
def schema_get(self, *, attach_id: AttachId, transaction_id: TransactionId | None, name: str) -> SchemaInfo | None:
191+
def schema_get(self, *, attach_opaque_data: AttachOpaqueData, transaction_opaque_data: TransactionOpaqueData | None, name: str) -> SchemaInfo | None:
182192
"""Get schema info, or None if not found."""
183193

184194
@abstractmethod
185-
def table_get(self, *, attach_id: AttachId, transaction_id: TransactionId | None, schema_name: str, name: str) -> TableInfo | None:
195+
def table_get(self, *, attach_opaque_data: AttachOpaqueData, transaction_opaque_data: TransactionOpaqueData | None, schema_name: str, name: str) -> TableInfo | None:
186196
"""Get table info, or None if not found."""
187197

188198
@abstractmethod
189-
def view_get(self, *, attach_id: AttachId, transaction_id: TransactionId | None, schema_name: str, name: str) -> ViewInfo | None:
199+
def view_get(self, *, attach_opaque_data: AttachOpaqueData, transaction_opaque_data: TransactionOpaqueData | None, schema_name: str, name: str) -> ViewInfo | None:
190200
"""Get view info, or None if not found."""
191201
```
192202

@@ -228,7 +238,7 @@ class MyCatalog(CatalogInterface):
228238

229239
## Logging Attach Options Safely
230240

231-
The worker emits structured `_logger.info` records and Sentry breadcrumbs for catalog lifecycle events (`catalog.attach`, `catalog.detach`, `catalog.create`, `catalog.transaction.begin`, `catalog.transaction.commit`, `catalog.transaction.rollback`). These let an operator correlate an opaque `attach_id` (a hex-encoded random byte string used in error scope tags) back to the catalog it represents.
241+
The worker emits structured `_logger.info` records and Sentry breadcrumbs for catalog lifecycle events (`catalog.attach`, `catalog.detach`, `catalog.create`, `catalog.transaction.begin`, `catalog.transaction.commit`, `catalog.transaction.rollback`). `attach_opaque_data` and `transaction_opaque_data` are short-hashed (12-char SHA-256 prefixes) before they reach the log record, the breadcrumb data, or the Sentry scope tags — the raw values never appear in observability output, since they may carry secrets. An operator correlates the short hash back to the catalog via these breadcrumbs.
232242

233243
The `options` dict passed to `catalog_attach()` and `catalog_create()` routinely carries credentials — passwords, tokens, OAuth secrets, connection strings. To avoid leaking these to logs and Sentry, the worker **does not** log option fields by default. Implementers opt in by overriding `loggable_attach_options()`:
234244

@@ -279,17 +289,17 @@ class MyReadOnlyCatalog(ReadOnlyCatalogInterface):
279289

280290
def catalog_attach(self, *, name: str, options: dict[str, Any]) -> CatalogAttachResult:
281291
return CatalogAttachResult(
282-
attach_id=AttachId(b"fixed-id"),
292+
attach_opaque_data=AttachOpaqueData(b"fixed-id"),
283293
supports_transactions=False,
284294
supports_time_travel=False,
285295
catalog_version_frozen=True,
286296
catalog_version=1,
287-
attach_id_required=False,
297+
attach_opaque_data_required=False,
288298
)
289299

290-
def schema_get(self, *, attach_id, transaction_id, name) -> SchemaInfo | None:
300+
def schema_get(self, *, attach_opaque_data, transaction_opaque_data, name) -> SchemaInfo | None:
291301
if name == "main":
292-
return SchemaInfo(attach_id=attach_id, name="main", comment=None, tags={})
302+
return SchemaInfo(attach_opaque_data=attach_opaque_data, name="main", comment=None, tags={})
293303
return None
294304

295305
# table_get, view_get return None by default
@@ -501,14 +511,14 @@ catalogs = client.catalogs() # ["my_catalog"]
501511

502512
# Attach to a catalog
503513
result = client.catalog_attach(name="my_catalog", options={})
504-
attach_id = result.attach_id
514+
attach_opaque_data = result.attach_opaque_data
505515

506516
# List schemas
507-
for schema in client.schemas(attach_id=attach_id):
517+
for schema in client.schemas(attach_opaque_data=attach_opaque_data):
508518
print(f"Schema: {schema.name}")
509519

510520
# Get schema contents (tables, views, functions)
511-
for obj in client.schema_contents(attach_id=attach_id, name="main"):
521+
for obj in client.schema_contents(attach_opaque_data=attach_opaque_data, name="main"):
512522
if isinstance(obj, TableInfo):
513523
print(f"Table: {obj.name}")
514524
elif isinstance(obj, ViewInfo):
@@ -519,12 +529,12 @@ for obj in client.schema_contents(attach_id=attach_id, name="main"):
519529
# Get only scalar functions using type filter
520530
from vgi.catalog import SchemaObjectType
521531
for obj in client.schema_contents(
522-
attach_id=attach_id, name="main", type=SchemaObjectType.SCALAR_FUNCTION
532+
attach_opaque_data=attach_opaque_data, name="main", type=SchemaObjectType.SCALAR_FUNCTION
523533
):
524534
print(f"Scalar Function: {obj.name}")
525535

526536
# Detach when done
527-
client.catalog_detach(attach_id=attach_id)
537+
client.catalog_detach(attach_opaque_data=attach_opaque_data)
528538
```
529539

530540
### Available Client Methods
@@ -571,7 +581,7 @@ For stateful catalogs, VGI provides `CatalogStorage` for persisting attachment a
571581
SQLite-backed storage with WAL mode for concurrent access:
572582

573583
```python
574-
from vgi.catalog import CatalogStorageSqlite, AttachId
584+
from vgi.catalog import CatalogStorageSqlite, AttachOpaqueData
575585

576586
# Use default location (~/.local/state/vgi/vgi_catalog.db)
577587
storage = CatalogStorageSqlite()
@@ -580,36 +590,36 @@ storage = CatalogStorageSqlite()
580590
storage = CatalogStorageSqlite("/path/to/catalog.db")
581591

582592
# Store attachment
583-
attach_id = storage.generate_attach_id()
584-
storage.attach_put(attach_id, catalog_name="mydb", options={"key": "value"})
593+
attach_opaque_data = storage.generate_attach_opaque_data()
594+
storage.attach_put(attach_opaque_data, catalog_name="mydb", options={"key": "value"})
585595

586596
# Retrieve attachment
587-
result = storage.attach_get(attach_id) # ("mydb", {"key": "value"})
597+
result = storage.attach_get(attach_opaque_data) # ("mydb", {"key": "value"})
588598

589599
# List all attachments
590600
all_ids = storage.attach_list()
591601

592602
# Delete attachment
593-
storage.attach_delete(attach_id)
603+
storage.attach_delete(attach_opaque_data)
594604
```
595605

596606
### Storage Protocol
597607

598608
```python
599609
from typing import Protocol
600-
from vgi.catalog import AttachId, TransactionId
610+
from vgi.catalog import AttachOpaqueData, TransactionOpaqueData
601611

602612
class CatalogStorage(Protocol):
603613
# Attachment state
604-
def attach_put(self, attach_id: AttachId, catalog_name: str, options: dict) -> None: ...
605-
def attach_get(self, attach_id: AttachId) -> tuple[str, dict] | None: ...
606-
def attach_delete(self, attach_id: AttachId) -> None: ...
607-
def attach_list(self) -> list[AttachId]: ...
614+
def attach_put(self, attach_opaque_data: AttachOpaqueData, catalog_name: str, options: dict) -> None: ...
615+
def attach_get(self, attach_opaque_data: AttachOpaqueData) -> tuple[str, dict] | None: ...
616+
def attach_delete(self, attach_opaque_data: AttachOpaqueData) -> None: ...
617+
def attach_list(self) -> list[AttachOpaqueData]: ...
608618

609619
# Transaction state
610-
def transaction_put(self, transaction_id: TransactionId, attach_id: AttachId, state: bytes) -> None: ...
611-
def transaction_get(self, transaction_id: TransactionId) -> tuple[AttachId, bytes] | None: ...
612-
def transaction_delete(self, transaction_id: TransactionId) -> None: ...
620+
def transaction_put(self, transaction_opaque_data: TransactionOpaqueData, attach_opaque_data: AttachOpaqueData, state: bytes) -> None: ...
621+
def transaction_get(self, transaction_opaque_data: TransactionOpaqueData) -> tuple[AttachOpaqueData, bytes] | None: ...
622+
def transaction_delete(self, transaction_opaque_data: TransactionOpaqueData) -> None: ...
613623
```
614624

615625
---
@@ -622,26 +632,26 @@ Catalogs can optionally support transactions:
622632
class TransactionalCatalog(CatalogInterface):
623633
def catalog_attach(self, *, name, options) -> CatalogAttachResult:
624634
return CatalogAttachResult(
625-
attach_id=...,
635+
attach_opaque_data=...,
626636
supports_transactions=True, # Enable transactions
627637
...
628638
)
629639

630-
def catalog_transaction_begin(self, *, attach_id) -> TransactionId:
631-
txn_id = self._create_transaction(attach_id)
640+
def catalog_transaction_begin(self, *, attach_opaque_data) -> TransactionOpaqueData:
641+
txn_id = self._create_transaction(attach_opaque_data)
632642
return txn_id
633643

634-
def catalog_transaction_commit(self, *, attach_id, transaction_id) -> None:
635-
self._commit_transaction(transaction_id)
644+
def catalog_transaction_commit(self, *, attach_opaque_data, transaction_opaque_data) -> None:
645+
self._commit_transaction(transaction_opaque_data)
636646

637-
def catalog_transaction_rollback(self, *, attach_id, transaction_id) -> None:
638-
self._rollback_transaction(transaction_id)
647+
def catalog_transaction_rollback(self, *, attach_opaque_data, transaction_opaque_data) -> None:
648+
self._rollback_transaction(transaction_opaque_data)
639649
```
640650

641651
**Transaction Guarantees:**
642652

643653
- Transactions MAY span multiple worker processes
644-
- Workers MUST treat `transaction_id` as opaque bytes
654+
- Workers MUST treat `transaction_opaque_data` as opaque bytes
645655
- Workers MUST ensure idempotency of commit/rollback
646656
- If `supports_transactions=False`, transaction methods won't be called
647657

@@ -679,12 +689,12 @@ import uuid
679689

680690
from vgi import Worker
681691
from vgi.catalog import (
682-
AttachId,
692+
AttachOpaqueData,
683693
CatalogAttachResult,
684694
CatalogInterface,
685695
SchemaInfo,
686696
TableInfo,
687-
TransactionId,
697+
TransactionOpaqueData,
688698
ViewInfo,
689699
)
690700

@@ -693,7 +703,7 @@ class SimpleCatalog(CatalogInterface):
693703
"""A minimal catalog with a single schema."""
694704

695705
def __init__(self):
696-
self._attachments: dict[AttachId, str] = {}
706+
self._attachments: dict[AttachOpaqueData, str] = {}
697707

698708
def catalogs(self) -> Iterable[str]:
699709
return ["simple_db"]
@@ -702,41 +712,41 @@ class SimpleCatalog(CatalogInterface):
702712
if name != "simple_db":
703713
raise ValueError(f"Unknown catalog: {name}")
704714

705-
attach_id = AttachId(uuid.uuid4().bytes)
706-
self._attachments[attach_id] = name
715+
attach_opaque_data = AttachOpaqueData(uuid.uuid4().bytes)
716+
self._attachments[attach_opaque_data] = name
707717

708718
return CatalogAttachResult(
709-
attach_id=attach_id,
719+
attach_opaque_data=attach_opaque_data,
710720
supports_transactions=False,
711721
supports_time_travel=False,
712722
catalog_version_frozen=True,
713723
catalog_version=1,
714-
attach_id_required=False,
724+
attach_opaque_data_required=False,
715725
)
716726

717-
def catalog_detach(self, *, attach_id: AttachId) -> None:
718-
self._attachments.pop(attach_id, None)
727+
def catalog_detach(self, *, attach_opaque_data: AttachOpaqueData) -> None:
728+
self._attachments.pop(attach_opaque_data, None)
719729

720730
def schema_get(
721-
self, *, attach_id: AttachId, transaction_id: TransactionId | None, name: str
731+
self, *, attach_opaque_data: AttachOpaqueData, transaction_opaque_data: TransactionOpaqueData | None, name: str
722732
) -> SchemaInfo | None:
723733
if name == "main":
724734
return SchemaInfo(
725-
attach_id=attach_id,
735+
attach_opaque_data=attach_opaque_data,
726736
name="main",
727737
comment="Default schema",
728738
tags={},
729739
)
730740
return None
731741

732742
def table_get(
733-
self, *, attach_id: AttachId, transaction_id: TransactionId | None,
743+
self, *, attach_opaque_data: AttachOpaqueData, transaction_opaque_data: TransactionOpaqueData | None,
734744
schema_name: str, name: str
735745
) -> TableInfo | None:
736746
return None # No tables
737747

738748
def view_get(
739-
self, *, attach_id: AttachId, transaction_id: TransactionId | None,
749+
self, *, attach_opaque_data: AttachOpaqueData, transaction_opaque_data: TransactionOpaqueData | None,
740750
schema_name: str, name: str
741751
) -> ViewInfo | None:
742752
return None # No views

0 commit comments

Comments
 (0)