feat(backend): add content-addressed gzip blob store (D-012)

Two on-disk pools per D-012:
- `MIMIC_BLOB_ROOT` (default `/var/lib/mimic/blobs/`) holds C2 polling
  output blobs, content-addressed gzip layout `<aa>/<bb>/<sha256>.gz`.
- `MIMIC_EVIDENCE_ROOT` (default `/var/lib/mimic/evidence/`) reserved for
  user-uploaded evidence (flat per-engagement, no compression). Wired only
  in config + .env.example here; F8 endpoint lands later.

`mimic.storage.blob`:
- `blob_path(root, sha256_hex)` validates the digest and returns the CAS
  path. Raises ValueError on a malformed digest (length != 64 or non-hex).
- `store_blob(root, data)` hashes, gzip-compresses, atomically writes to
  `<aa>/<bb>/<sha256>.gz` (0o750 dir perms, 0o640 file perms). Idempotent:
  duplicate writes leave mtime untouched.

5 new unit tests cover happy path, deduplication, idempotency, malformed
digest, and the two-byte-pair directory layout.
This commit is contained in:
knacky
2026-05-21 20:44:59 +02:00
parent 162b6988f8
commit 12d131c826
5 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
"""Content-addressed gzip blob store (D-012)."""
from __future__ import annotations
import gzip
import hashlib
import pytest
from mimic.storage.blob import blob_path, store_blob
def test_blob_path_uses_two_byte_pairs(tmp_path) -> None:
digest = "ab" + "cd" + "ef" * 30
path = blob_path(tmp_path, digest)
assert path == tmp_path / "ab" / "cd" / f"{digest}.gz"
def test_blob_path_rejects_invalid_digest(tmp_path) -> None:
with pytest.raises(ValueError, match="invalid sha256"):
blob_path(tmp_path, "not-a-digest")
def test_store_blob_writes_gzip_and_returns_digest(tmp_path) -> None:
payload = b"hello world\n"
expected = hashlib.sha256(payload).hexdigest()
digest, path = store_blob(tmp_path, payload)
assert digest == expected
assert path == tmp_path / expected[0:2] / expected[2:4] / f"{expected}.gz"
with gzip.open(path, "rb") as fh:
assert fh.read() == payload
def test_store_blob_is_idempotent(tmp_path) -> None:
payload = b"same content"
digest1, path1 = store_blob(tmp_path, payload)
mtime_before = path1.stat().st_mtime_ns
digest2, path2 = store_blob(tmp_path, payload)
assert digest1 == digest2
assert path1 == path2
assert path2.stat().st_mtime_ns == mtime_before
def test_store_blob_dedupes_distinct_payloads(tmp_path) -> None:
_, p1 = store_blob(tmp_path, b"alpha")
_, p2 = store_blob(tmp_path, b"beta")
assert p1 != p2
assert p1.exists()
assert p2.exists()