Source code for cadrumo.adapters.persistence.storage.blob_store.materialisation
"""Route-canonical :class:`SecretStore` factory.:func:`get_secret_store` is a route-aware lazy factory keyed by the resolvedsecret-store and blob-store roots from :class:`Settings`. Stores are cachedby normalized route so an explicit Settings route never inherits the firststore constructed by an unrelated profile root; callers needing an isolatedstore inject one directly at the consuming boundary (a ``store=`` parameteron the secret-store consumer) rather than through a process-wide override.This module previously also carried a path-shaped secret bridge(``materialise_secret`` / ``export_to_temp_path``) for third-party SDKs thatdemand a filesystem path rather than in-memory bytes. It was deleted asunexercised, security-adjacent dormant capability -- a route to writingdecrypted secrets to the OS tempdir that no production caller had evertaken, sitting in the public facade with an inviting docstring. Everycurrent consumer of a secret (the certificate backend, OAuth flow, custodysessions) reads bytes from :class:`SecretStore` directly. A future SDK thatgenuinely cannot consume in-memory bytes gets a purpose-built bridgedeclared and enrolled at the point it is needed, not one kept live "just incase.""""from__future__importannotationsimportosfrompathlibimportPathfromthreadingimportLockfromtypingimportTYPE_CHECKINGfrom.blob_storeimportEncryptedBlobStoreifTYPE_CHECKING:from.....core.configimportSettingsfrom..secret_store.storeimportSecretStore# ``secret_store`` imports this package to build its own blob store, so binding# ``SecretStore`` at module scope closes a real import cycle. It resolved before# only because the parent facade happened to import one side first; that# ordering disappeared when the facade became lazy. The single runtime use is# deferred into the factory below, and every other reference is an annotation._factory_lock=Lock()_stores_by_route:dict[tuple[str,str],SecretStore]={}
[docs]defget_secret_store(*,settings:Settings|None=None)->SecretStore:"""Return the route-canonical :class:`SecretStore`. Stores are cached by normalized secret-store and blob-store roots, so an explicit Settings route never inherits the first store constructed by an unrelated profile root. Callers needing an isolated store inject it directly at the consuming boundary (the ``store=`` / ``settings=`` parameters on the secret-store consumers) rather than through a process-wide override. Args: settings: Optional pre-built settings instance. When ``None``, :func:`load_settings` is consulted for the current route. Returns: The cached :class:`SecretStore` for the resolved route. """with_factory_lock:from.....core.configimportload_settingsresolved=settingsifsettingsisnotNoneelseload_settings()route=(os.path.normcase(str(Path(resolved.cadrumo_secret_store_dir).resolve())),os.path.normcase(str(Path(resolved.cadrumo_blob_store_dir).resolve())),)cached=_stores_by_route.get(route)ifcachedisnotNone:returncached# Stores fall through to ``get_active_master_key()`` (the# active :class:`BucketSession`) when ``master_key_provider`` is# absent. The CLI root callback opens the session before any# consumer reaches this factory.## ``EncryptedBlobStore.root_dir`` is documented as "the directory# containing the blobs/ subtree" -- the PARENT of ``blobs/``, i.e.# the storage root -- and the store appends its own ``blobs``# segment internally (``_shard_dir_for``). Passing# ``cadrumo_blob_store_dir`` here (already ``<storage_root>/blobs``)# doubled that segment: every blob landed at# ``<storage_root>/blobs/blobs/<hex[:2]>/<hash>`` instead of the# declared ``<storage_root>/blobs/<hex[:2]>/<hash>``. Passing the# storage root honours the class's existing documented contract# rather than redefining what ``root_dir`` means for every caller.from..secret_store.storeimportSecretStoreblob_store=EncryptedBlobStore(root_dir=Path(resolved.cadrumo_local_storage_root))store=SecretStore(store_dir=Path(resolved.cadrumo_secret_store_dir),blob_store=blob_store,)_stores_by_route[route]=storereturnstore