12 Commits
Author SHA1 Message Date
dsql f150f7f957 fix: clean mongo errors, null-meta list crash, empty-friendly revoke, key passphrase, storage path env expansion (v0.1.5)
envauth-2: catch PyMongoError in cli.py (guarded import) so an unreachable
mongo backend fails fast with a clean [x] line instead of an uncaught
traceback after the driver's server-selection timeout.

envauth-3: coerce a non-dict meta block to {} before rendering `list`, so a
"meta": null doc no longer crashes the table mid-render.

envauth-4: revoke checks args.friendly is not None instead of truthiness, so
--friendly "" correctly routes to the friendly-name lookup instead of
misrouting to the fingerprint branch.

envauth-5: add an optional [keys].password config field and thread it through
boot_local's decrypt_aes_key_with_rsa and verify's self_test, so an encrypted
local private key can be unwrapped.

envauth-6: add Config.storage_path (routed through the existing _expand
helper) so the JSON storage path expands $ENV_VARS the same way the key
paths already do, not just ~.

envauth-7: verified CLAUDE.md and README already correctly describe
authorize's re-run-is-refused behavior (fixed under envauth-1); no doc
change needed.

Also compresses several essay-length docstrings/comments (init's TOCTOU
note, authorize's replace-guard note, mongo_store's module docstring,
json_store's _write docstring, list_keys' _can_authorize docstring) with no
behavior change; re-verified the full CLI flow (JSON + live mongod) after.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-07-02 23:32:10 -04:00
dsql 0d0558d11b chore: unpin envelope_crypto dependency (track latest release)
envauth used envelope_crypto pinned at v0.1.0, two data-loss fixes behind (the lib
is now v0.1.4). envauth uses only stable crypto primitives (initialize, encrypt_data/
decrypt_data, create_aes_key, *_aes_key_with_rsa, get_rsa_key_fingerprint, self_test)
— never the record-rotation functions whose contract changed — so tracking latest is
safe and keeps the crypto fixes flowing without a manual bump each release.

Verified: full CLI round-trip (init -> authorize server -> privilege gate -> envauth-1
self-authorize refusal -> list) against envelope_crypto v0.1.4 source; all 8 used
primitives present. v0.1.4.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-07-02 16:50:55 -04:00
dsql bfeee80712 fix: EA-1 refuse authorize of an already-recorded key (v0.1.3)
authorize never checked the target fingerprint against existing docs before
save()'s upsert-by-_id, so authorizing the local machine's own public key
under a new friendly name silently replaced the local authorizer record
(can_authorize demoted to False) while printing a success banner. With a
sole authorizer this bricks the CLI: authorize refuses (not permitted),
init refuses (already initialized), and revoke of the local key refuses
(refusing to revoke the local key) -- no in-CLI recovery. Mirror revoke's
local-key guard and extend it to any existing _id, so a duplicate target
is refused with a clear message instead of silently replacing the record.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-07-02 16:41:09 -04:00
dsql f2e9e5fe35 chore: ignore .claude/ dir (CLAUDE.md now lives under .claude/)
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 21:55:13 -04:00
dsql 88e1eaef39 fix: EA-1 clean error on malformed flag/doc; EA-2 fingerprint ambiguity + fd-leak
EA-1: main dispatch catches KeyError/TypeError so a structurally-malformed flag/doc prints
a clean [x] line instead of a traceback. EA-2: fingerprint revoke rejects an empty prefix
and an ambiguous prefix (was: silently revoked the first match). json_store closes the raw
fd if os.fdopen raises before taking ownership (was: leaked). init TOCTOU documented as
by-design (trusted-DEK model, save upserts by _id). list '?' wording clarified.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 21:35:03 -04:00
dsql 130c62e31c docs: pin install line to release, note unpinned-latest option
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 18:13:52 -04:00
dsql 09e6d15e48 docs: show unpinned install line; note tag-pinning for reproducibility
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 18:07:37 -04:00
dsql a40a7432ef fix: clean error on OS-level write failures in config init and dispatch (v0.1.2)
- config init catches OSError (read-only dir, ENOSPC, gone cwd) alongside CommandError
  and prints a clean [x] line; the main dispatch catches the full OSError family instead
  of only FileNotFoundError (L13)
- document read_flag's fail-closed (non-dict -> not allowed) as a deliberate privilege-
  gate default (nit).

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 17:58:09 -04:00
dsql eced5333d6 fix: surface a tampered capability flag as a clean error, not a traceback
a tampered or foreign GCM capability flag raises cryptography's InvalidTag (subclasses Exception, not ValueError/RuntimeError), which escaped the CLI's catch tuple as a raw traceback on the authorize/verify paths. main() now catches InvalidTag and surfaces '[\xe2\x9c\x98] capability flag failed authentication — tampered or wrong DEK'. also corrected the stale CLAUDE.md storage note that still described the swallow-wrapped mongo methods instead of the fail-loud raw-collection path.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 01:10:25 -04:00
dsql 13bf77f0f4 fix: mongo backend — sync close() + fail-loud via raw collection
two regressions in the [mongo] storage backend: (1) the four finally blocks did 'await db.close()' but the mongo lib's close() became synchronous this session, so await None raised TypeError on every op — dropped the await. (2) the backend consumed mongo's swallow-and-return-default wrapped methods raw, conflating a driver error with 'no document / not initialized' in the lib that gates authority; it now goes through the raw db.collection(name) escape hatch (the motor collection, which raises) and raises on a no-op upsert, matching the CLI's fail-loud stance.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-28 18:45:25 -04:00
dsql 2d01805427 docs: correct capability-flag threat-model boundary; add detection guidance
the docs claimed 'you cannot grant yourself authority without already having it', which is false in the shared-DEK model: a DEK-holder with write access can copy a sealed True flag onto its own doc. replaced with the honest boundary (the flag is unforgeable WITHOUT the DEK, but is not a defense against a malicious DEK-holder, which is out of scope by design) and added operational guidance to detect a self-grant by auditing authorization state. no code or storage-format change.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-28 17:18:28 -04:00
dsql a0824c4b1a fix: unique temp on JSON write + tolerant created_at render (v0.1.1)
- JsonStore._write used a fixed '<path>.tmp' name with no lock, so two concurrent
  authorizer invocations could clobber each other's temp and corrupt/lose the key
  store. use tempfile.mkstemp in the same dir (unique per write) then os.replace
  (atomic), cleaning up the temp on failure.
- list 'created_at' formatting did int(raw) unguarded; one hand-edited/legacy doc
  with a bad timestamp aborted the whole table. guard per-row, fall back to '-'.

verified by execution: 20 concurrent writers -> 0 errors, file stays valid JSON,
no leftover .tmp; upsert still dedupes/updates; bad/absent created_at -> '-'.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-28 15:47:58 -04:00
16 changed files with 218 additions and 72 deletions
+1 -1
View File
@@ -5,4 +5,4 @@ dist/
build/
.venv/
.pytest_cache/
CLAUDE.md
.claude/
+49 -15
View File
@@ -13,25 +13,27 @@ authorization system and the key-document schema; the crypto primitives live in
## Install
```
envelope_authorizer @ git+ssh://git@git.rethinkstudios.io/rethink-public/envelope_authorizer.git@v0.1.0
envelope_authorizer @ git+ssh://git@git.rethinkstudios.io/rethink-public/envelope_authorizer.git@v0.1.5
```
Direct:
```bash
pip install "envelope_authorizer @ git+ssh://git@git.rethinkstudios.io/rethink-public/envelope_authorizer.git@v0.1.0"
pip install "envelope_authorizer @ git+ssh://git@git.rethinkstudios.io/rethink-public/envelope_authorizer.git@v0.1.5"
```
The base install uses a local JSON file for storage (stdlib only). For shared
dev→server storage, install the mongo extra:
```bash
pip install "envelope_authorizer[mongo] @ git+ssh://git@git.rethinkstudios.io/rethink-public/envelope_authorizer.git@v0.1.0"
pip install "envelope_authorizer[mongo] @ git+ssh://git@git.rethinkstudios.io/rethink-public/envelope_authorizer.git@v0.1.5"
```
Installing pulls `envelope_crypto` (and `mongo` with the extra). After install,
the `authorizer` command is on your PATH; `python -m envelope_authorizer` also works.
Drop the `@v0.1.5` suffix from the line above to install the latest unpinned.
## Trust model (read this)
There is one shared **AES data-encryption key (DEK)** per project. Each key doc
@@ -47,12 +49,39 @@ Each key carries an **encrypted capability flag** — `meta.authorizer` =
- **Servers** are authorized with `allowed: False` → they can boot and use the DEK
(decrypt project data) but **cannot authorize anything else**.
The flag is GCM-sealed under the DEK on purpose: a server **cannot flip its own
flag in the database to escalate**, because forging a valid flag requires already
holding the DEK, and editing the stored ciphertext breaks the auth tag. The
guarantee is precisely: *you cannot grant yourself authority without already
having it.* (A legitimate authorizer can of course mint new flags — that is its
job. This stops passive DB tampering, not an authorizer.)
The flag is GCM-sealed under the DEK on purpose. What that buys, stated honestly:
- **Without the DEK, the flag is inert.** A stolen database at rest cannot be
escalated — the flags are ciphertext, unforgeable and unflippable without the key
(editing the stored ciphertext breaks the GCM auth tag). This stops passive DB
tampering.
- **It does not defend against a party that already holds the DEK and can write
storage.** The flag is sealed under the *shared* DEK and is **not bound to key
identity**, so such a party can copy a valid sealed `True` flag from an
authorizer's doc onto its own and self-grant authority. This is **out of scope by
design**: the trust model assumes DEK-holders are trusted. A DEK-holder already
has full data access (DEK + write access is game over), so self-granting authority
exposes no additional data. We deliberately do not add per-key signing to prevent
it — the residual is handled operationally (below).
So the precise boundary is: **the capability flag is unforgeable without the DEK; it
is not a defense against a malicious DEK-holder.** (A legitimate authorizer can of
course mint new flags — that is its job.)
### Operational guidance (the residual control)
Because the DEK-holder case above is **not** prevented cryptographically in the
shared-DEK model, the intended mitigation is **detection, not crypto**. Consumers
should periodically audit the authorization state in storage:
- enumerate every key doc's capability flag (`list` shows authorizer status),
- compare the set of `allowed: True` keys against a known-good list of expected
authorizers,
- alert on any unexpected authorizer.
An unexpected `allowed: True` appearing in storage is the signal that a DEK-holder
self-granted — catch it by review, then revoke and re-authorize from a trusted
machine.
`revoke` is **bookkeeping, not rotation**: it deletes the record so the key can no
longer unwrap at boot, but it does not rotate the DEK or scrub it from a machine
@@ -100,7 +129,11 @@ initialized or the friendly name is taken.
Boots the local DEK, verifies the local key is itself an authorizer, then wraps
the same DEK to the target public key and stores a new key doc. Omit
`--can-authorize` for servers (`allowed: False`); pass it only for trusted
dev/home machines.
dev/home machines. Refuses a target key whose fingerprint already has a record
(most importantly the local key itself) — `save` upserts by `_id`, so
authorizing an already-known key would silently replace its existing doc
(capability flag and friendly name) under a success banner instead of adding a
new key. Revoke the existing record first if you intend to re-authorize it.
```
[✔] Authorized Jy7k2ey7... | friendly: server1 [can_authorize=False]
@@ -141,9 +174,10 @@ rotation.
## Config reference
TOML, searched cwd-first then `~`: `.authorizer.toml`. Paths expand `~`. No
defaults are baked in — a missing required field raises an error naming the field
and the config path.
TOML, searched cwd-first then `~`: `.authorizer.toml`. Paths (`keys.public`,
`keys.private`, `storage.path`) expand both `~` and `$ENV_VARS`. No defaults are
baked in — a missing required field raises an error naming the field and the
config path.
### JSON backend (default, stdlib only)
@@ -152,6 +186,7 @@ and the config path.
public = "~/.ssh/id_rsa.pub"
private = "~/.ssh/id_rsa"
identity = "user@hostname" # stamped as created_by on every key doc
# password = "..." # only if the private key above is encrypted
[storage]
backend = "json"
@@ -195,5 +230,4 @@ Owned by this lib (not `envelope_crypto`):
## Versioning
Tagged `vX.Y.Z`. Pin the tag. `envelope_crypto` is pinned at `v0.1.0` in
`pyproject.toml`; to change it, edit the pin and re-test.
Releases are tagged `vX.Y.Z`. The install line above pins a release; drop the `@vX.Y.Z` suffix to install the latest unpinned. Pin deliberately for reproducible installs.
+2 -2
View File
@@ -4,11 +4,11 @@ build-backend = "hatchling.build"
[project]
name = "envelope_authorizer"
version = "0.1.0"
version = "0.1.5"
description = "CLI key-authorization manager for envelope_crypto"
requires-python = ">=3.10"
dependencies = [
"envelope_crypto @ git+ssh://git@git.rethinkstudios.io/rethink-public/envelope_crypto.git@v0.1.0",
"envelope_crypto @ git+ssh://git@git.rethinkstudios.io/rethink-public/envelope_crypto.git",
"tomli>=2.0; python_version<'3.11'",
]
+1 -1
View File
@@ -1 +1 @@
__version__ = "0.1.0"
__version__ = "0.1.5"
+19 -2
View File
@@ -9,11 +9,19 @@ command loads config and resolves a storage backend first. expected failures
import argparse
import sys
from cryptography.exceptions import InvalidTag
from . import __version__
from .config import ConfigError, load_config
from .commands import CommandError, authorize, config_init, init, list_keys, revoke, verify
from .storage import resolve
try:
from pymongo.errors import PyMongoError
except ImportError:
class PyMongoError(Exception):
"""stand-in when the [mongo] extra is not installed; never actually raised"""
def _build_parser() -> argparse.ArgumentParser:
"""construct the argument parser with all subcommands"""
@@ -70,7 +78,9 @@ def main() -> int:
try:
config_init.run(None, None, args)
return 0
except CommandError as error:
except (CommandError, OSError) as error:
# config_init writes a file (cwd may be read-only, full, or gone) — an
# OSError must print a clean [✘] line, not a raw traceback
return _fail(str(error))
parser.parse_args(["config", "--help"])
return 0
@@ -88,7 +98,14 @@ def main() -> int:
storage = resolve(config)
handlers[args.cmd](config, storage, args)
return 0
except (ConfigError, CommandError, RuntimeError, ValueError, FileNotFoundError) as error:
except InvalidTag:
return _fail("capability flag failed authentication — tampered or wrong DEK")
except PyMongoError as error:
return _fail(f"storage backend error: {error}")
except (ConfigError, CommandError, RuntimeError, ValueError, OSError, KeyError, TypeError) as error:
# OSError covers the FileNotFoundError/PermissionError/IsADirectoryError family;
# KeyError/TypeError cover a structurally-malformed flag/doc (unguarded indexing
# of ['iv']/['meta']['authorizer']/['key']) — all print a clean [✘] line, not a traceback
return _fail(str(error))
+8 -2
View File
@@ -22,7 +22,11 @@ def make_flag(crypto: EnvelopeCrypto, allowed: bool) -> dict:
def read_flag(crypto: EnvelopeCrypto, blob: dict) -> bool:
"""decrypt a capability flag; return the `allowed` bool"""
"""decrypt a capability flag; return the `allowed` bool
fails closed: a non-dict / unexpected plaintext reads as not-allowed rather than
raising — a privilege gate must default to deny on a malformed flag.
"""
data = crypto.decrypt_data(blob)
return bool(data.get("allowed", False)) if isinstance(data, dict) else False
@@ -59,7 +63,9 @@ def boot_local(config, storage) -> Tuple[EnvelopeCrypto, dict]:
raise CommandError(
"not initialized on this machine (no key doc for the local public key)"
)
aes_key = crypto.decrypt_aes_key_with_rsa(doc["key"], config.private_key)
aes_key = crypto.decrypt_aes_key_with_rsa(
doc["key"], config.private_key, password=config.private_key_password
)
crypto.initialize(aes_key)
return crypto, doc
+22 -4
View File
@@ -2,7 +2,10 @@
boots the local DEK, verifies the local key is itself an authorizer, then wraps
the same DEK to the target public key and stores a new key doc. `--can-authorize`
decides whether the new key may authorize others (omit it for servers).
decides whether the new key may authorize others (omit it for servers). Refuses
a target key that already has a record — `save` upserts by `_id`, so re-authorizing
a known key (most dangerously the local key itself) would silently overwrite its
flag/friendly under a success banner instead of adding a new key.
"""
from . import (
@@ -10,6 +13,7 @@ from . import (
boot_local,
build_doc,
find_by_friendly,
local_fingerprint,
make_flag,
read_flag,
)
@@ -25,9 +29,23 @@ def run(config, storage, args) -> None:
if not read_flag(crypto, local_doc["meta"]["authorizer"]):
raise CommandError("this key is not permitted to authorize others")
new_fp, new_wrapped = crypto.encrypt_aes_key_with_rsa(
crypto.master_key, args.key
)
new_fp = crypto.get_rsa_key_fingerprint(args.key)
if new_fp == local_fingerprint(crypto, config):
raise CommandError(
"target key is the local key; authorize would silently replace the "
"local authorizer record — use a different keypair, or `authorizer "
"list` if you meant to check its status"
)
existing = storage.get(new_fp)
if existing:
existing_friendly = existing.get("meta", {}).get("friendly", "?")
raise CommandError(
f"target key is already authorized as '{existing_friendly}'; "
f"authorize would silently replace that record — revoke it first "
f"if you intend to re-authorize it"
)
_, new_wrapped = crypto.encrypt_aes_key_with_rsa(crypto.master_key, args.key)
flag = make_flag(crypto, args.can_authorize)
doc = build_doc(new_fp, new_wrapped, flag, config.identity, args.friendly)
storage.save(doc)
@@ -10,6 +10,7 @@ _TEMPLATE = """[keys]
public = "~/.ssh/id_rsa.pub"
private = "~/.ssh/id_rsa"
identity = "user@hostname"
# password = "..." # only if the private key above is encrypted
[storage]
backend = "json"
+6 -1
View File
@@ -11,7 +11,12 @@ from . import CommandError, build_doc, find_by_friendly, make_flag
def run(config, storage, args) -> None:
"""initialize the key system on this machine as the first authorizer"""
"""initialize the key system on this machine as the first authorizer
the already-initialized / duplicate-friendly checks are non-atomic (TOCTOU under two
concurrent CLIs) by design — one-shot admin tool, `save` upserts by `_id`, so the
worst case is a cosmetic double-init with no security consequence.
"""
if storage.get_all():
raise CommandError(
"already initialized; use `authorizer list` to see existing keys"
+22 -7
View File
@@ -11,19 +11,34 @@ from . import boot_local, read_flag
def _can_authorize(crypto, doc) -> str:
"""decrypted authority of a doc as Yes/No, or `?` if not readable here"""
"""decrypted authority of a doc as Yes/No, or `?` if unreadable here
`?` covers any failure (unwrap mismatch, missing/malformed doc) so the table
always renders instead of crashing on one bad row.
"""
try:
return "Yes" if read_flag(crypto, doc["meta"]["authorizer"]) else "No"
except Exception:
return "?"
def _created(doc) -> str:
"""format created_at as a UTC timestamp string, or '-' if absent"""
raw = doc.get("meta", {}).get("created_at")
def _meta(doc) -> dict:
"""the doc's meta block as a dict, coercing a missing/null/malformed one to {}"""
meta = doc.get("meta")
return meta if isinstance(meta, dict) else {}
def _created(meta: dict) -> str:
"""format created_at as a UTC timestamp string, or '-' if absent/unparseable"""
raw = meta.get("created_at")
if not raw:
return "-"
return datetime.fromtimestamp(int(raw), tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
try:
return datetime.fromtimestamp(int(raw), tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
except (TypeError, ValueError, OverflowError, OSError):
# one hand-edited/legacy doc with a bad created_at shouldn't abort the
# whole table render
return "-"
def run(config, storage, args) -> None:
@@ -35,12 +50,12 @@ def run(config, storage, args) -> None:
print(header)
print("-" * len(header))
for doc in docs:
meta = doc.get("meta", {})
meta = _meta(doc)
print(
f"{doc.get('_id', '')[:16]:<18} "
f"{str(meta.get('friendly', '-')):<16} "
f"{str(meta.get('created_by', '-')):<18} "
f"{_created(doc):<21} "
f"{_created(meta):<21} "
f"{_can_authorize(crypto, doc):<8}"
)
print(f"\n{len(docs)} key(s) authorized")
+13 -6
View File
@@ -18,16 +18,23 @@ _WARNING = (
def _find_by_fingerprint(storage, prefix: str):
"""return the doc whose `_id` starts with the given prefix, or None"""
for doc in storage.get_all():
if doc.get("_id", "").startswith(prefix):
return doc
return None
"""return the single doc whose `_id` starts with the given prefix, or None
rejects an empty prefix (matches everything) and an ambiguous one (matches
more than one key) instead of silently revoking the first hit.
"""
if not prefix:
raise CommandError("fingerprint prefix must not be empty")
matches = [doc for doc in storage.get_all() if doc.get("_id", "").startswith(prefix)]
if len(matches) > 1:
ids = ", ".join(d["_id"][:16] for d in matches)
raise CommandError(f"fingerprint prefix '{prefix}' is ambiguous; matches: {ids}")
return matches[0] if matches else None
def run(config, storage, args) -> None:
"""delete a key record by friendly or fingerprint, guarding the local key"""
if args.friendly:
if args.friendly is not None:
doc = find_by_friendly(storage, args.friendly)
label = args.friendly
else:
+1 -1
View File
@@ -13,7 +13,7 @@ def run(config, storage, args) -> None:
crypto, _ = boot_local(config, storage)
if hasattr(crypto, "self_test"):
crypto.self_test(config.public_key, config.private_key)
crypto.self_test(config.public_key, config.private_key, password=config.private_key_password)
else:
sample = {"_authorizer_verify": "ok", "n": 12345}
if crypto.decrypt_data(crypto.encrypt_data(sample)) != sample:
+17 -6
View File
@@ -1,16 +1,17 @@
"""TOML config loader for the authorizer CLI
resolves a `.authorizer.toml` (cwd first, then ~), expands user paths, and
exposes typed accessors. no defaults are baked in: a missing required field
raises a clear error naming the field and the config path that was searched.
the loaded config is the only place key paths, identity, and storage live —
this lib never imports a host `config` module.
resolves a `.authorizer.toml` (cwd first, then ~), expands `~` and `$ENV_VARS` in
every path field (key paths and the JSON storage path alike), and exposes typed
accessors. no defaults are baked in: a missing required field raises a clear
error naming the field and the config path that was searched. the loaded config
is the only place key paths, identity, and storage live — this lib never imports
a host `config` module.
"""
import os
import sys
from pathlib import Path
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional
if sys.version_info >= (3, 11):
import tomllib
@@ -72,6 +73,11 @@ class Config:
"""path to the local RSA private key (expanded)"""
return _expand(self.require("keys", "private"))
@property
def private_key_password(self) -> Optional[str]:
"""passphrase for an encrypted local private key, or None if unset"""
return self.optional("keys", "password")
@property
def identity(self) -> str:
"""human identity stamped as created_by on every key doc"""
@@ -82,6 +88,11 @@ class Config:
"""selected storage backend name ("json" or "mongo")"""
return self.require("storage", "backend")
@property
def storage_path(self) -> str:
"""path to the JSON storage file (expanded, same as the key paths)"""
return _expand(self.require("storage", "path"))
def load_config() -> Config:
"""find and parse the authorizer config, or raise with guidance
+1 -2
View File
@@ -13,8 +13,7 @@ def resolve(config) -> StorageBackend:
"""build the storage backend named by the config, or raise on unknown"""
backend = config.storage_backend
if backend == "json":
path = config.require("storage", "path")
return JsonStore(path)
return JsonStore(config.storage_path)
if backend == "mongo":
from .mongo_store import MongoStore
return MongoStore(
+29 -6
View File
@@ -8,6 +8,7 @@ setups; for dev->server flows across machines, use the mongo backend.
import json
import os
import tempfile
from pathlib import Path
from typing import List, Optional
@@ -34,13 +35,35 @@ class JsonStore(StorageBackend):
return data
def _write(self, docs: List[dict]) -> None:
"""atomically write the docs list (temp file then replace)"""
"""atomically write the docs list (unique temp file then os.replace)
a unique temp per write (tempfile.mkstemp) keeps concurrent writers from
clobbering a shared `.tmp`; the temp is cleaned up if the write fails
before replace.
"""
self.path.parent.mkdir(parents=True, exist_ok=True)
tmp = self.path.with_suffix(self.path.suffix + ".tmp")
with open(tmp, "w", encoding="utf-8") as handle:
json.dump(docs, handle, indent=2)
handle.write("\n")
os.replace(tmp, self.path)
fd, tmp = tempfile.mkstemp(
dir=self.path.parent, prefix=self.path.name + ".", suffix=".tmp"
)
wrapped = False
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
wrapped = True # fdopen owns fd now; its close() handles it
json.dump(docs, handle, indent=2)
handle.write("\n")
os.replace(tmp, self.path)
except BaseException:
if not wrapped:
# fdopen raised before taking ownership — close the raw fd ourselves
try:
os.close(fd)
except OSError:
pass
try:
os.unlink(tmp)
except OSError:
pass
raise
def get(self, fingerprint: str) -> Optional[dict]:
"""return the doc whose `_id` matches, or None"""
+26 -16
View File
@@ -1,11 +1,16 @@
"""mongo storage backend (behind the [mongo] extra)
bridges the async rethink-public `mongo` lib to the sync StorageBackend by
wrapping each operation in its own asyncio.run: connect -> op -> close, fully
self-contained per call. no module-level client and no shared event loop, so it
never collides with a running loop. a per-call connect is an accepted tradeoff
for a one-shot admin CLI. requires envelope_authorizer[mongo]; without it every
method raises a clear RuntimeError.
bridges the async rethink-public `mongo` lib to the sync StorageBackend: each op
gets its own asyncio.run (connect -> op -> close), no module-level client or
shared loop, so it never collides with a running loop. per-call connect is an
accepted tradeoff for a one-shot admin CLI. requires envelope_authorizer[mongo];
without it every method raises a clear RuntimeError.
fail-loud: ops go through the mongo lib's raw collection escape hatch (the motor
collection, which raises), not the swallow-and-default wrapped methods — this is
an auth backend, so a driver error must never read as "not initialized," and a
no-op upsert raises instead of silently reporting success. `close()` is
synchronous on the mongo lib — not awaited.
"""
import asyncio
@@ -36,33 +41,38 @@ class MongoStore(StorageBackend):
async def _get(self, fingerprint: str) -> Optional[dict]:
db = Mongo(self.uri, self.database)
try:
return await db.get_document(self.collection, {"_id": fingerprint})
return await db.collection(self.collection).find_one({"_id": fingerprint})
finally:
await db.close()
db.close()
async def _get_all(self) -> List[dict]:
db = Mongo(self.uri, self.database)
try:
return await db.get_documents(self.collection, {})
cursor = db.collection(self.collection).find({})
return await cursor.to_list(length=None)
finally:
await db.close()
db.close()
async def _save(self, doc: dict) -> None:
db = Mongo(self.uri, self.database)
try:
await db.update_document(
self.collection, {"_id": doc["_id"]}, doc, do_upsert=True
result = await db.collection(self.collection).replace_one(
{"_id": doc["_id"]}, doc, upsert=True
)
if not (result.matched_count or result.upserted_id):
raise RuntimeError(
f"mongo upsert affected no document for _id={doc['_id']!r}"
)
finally:
await db.close()
db.close()
async def _delete(self, fingerprint: str) -> bool:
db = Mongo(self.uri, self.database)
try:
removed = await db.delete_document(self.collection, {"_id": fingerprint})
return bool(removed)
result = await db.collection(self.collection).delete_one({"_id": fingerprint})
return result.deleted_count > 0
finally:
await db.close()
db.close()
def get(self, fingerprint: str) -> Optional[dict]:
"""return the key doc with this `_id`, or None"""