diff --git a/src/python/jw/pkg/lib/ec/secret_store/KvFile.py b/src/python/jw/pkg/lib/ec/secret_store/KvFile.py index 2edea5d3..d4a747e4 100644 --- a/src/python/jw/pkg/lib/ec/secret_store/KvFile.py +++ b/src/python/jw/pkg/lib/ec/secret_store/KvFile.py @@ -20,44 +20,7 @@ from .Base import Base from .layout import BLOB_SUFFIX, FT_SECRET, FT_SECRET_FILE, FT_SECRET_KEY, SECRET_SUFFIX class KvFile(Base): - """The on-host store of a host's package secrets. - - A config path X is either an entry, the file X.jw-secret with - KEY=VALUE lines, whose children in the address space are its - keys, or a blob, the file X.jw-secret-file, whose content is the - file itself, a leaf. Any other path is a branch: a real - directory on the host, with no file of its own. - - Rooted at the host: addresses are config paths relative to the - host root, absolute on the host with the leading slash. A URI - may root the store at a subtree of the host tree; its path is - composed into every address, and the URI is the store's - identity. A suffixed address names the on-host file of its - config path, the suffix picking the kind: .jw-secret the - entry, .jw-secret-file the blob. - - get reads a key below an entry or the blob at its config path; - put writes a key into its entry file, or a blob as a - .jw-secret-file at its config path, and accepts entry, key, and - blob content only; unlink drops the file the address names. No - default fallback. - - An entry lists its keys. A branch lists the config paths below - it, the root included: the host's package set is the bounded - base, the .jw-tmpl files of the installed packages, presence- - checked on the host. The distro is autodetected from the host - on first use, or given in the constructor; the autodetected - distro applies the JW_DEFAULT_PKG_FILTER package filter, if - set, so a URI-built context honors the app's documented - filter fallback. is_dir() answers from the entry file and the - host tree, so the write path never pays for the enumeration. - - The on-host tree is a precondition: mkdir materializes a missing - entry file below an existing directory, and mkdir and put fail - with EROFS below a missing one. KvFile never creates - directories. Entry and blob files are mode 0400; - owner and group follow the put request, if given. - """ + """The on-host store of a host's package secrets. """ def __init__( self, @@ -65,10 +28,6 @@ class KvFile(Base): uri: str | Uri | None = None, distro: Distro | None = None, ) -> None: - # -- The URI roots the namespace at the host tree's root, or - # at a subtree of it: its path is composed into every - # address, and it is the store's identity. A direct - # construction names the context's host if uri is None: uri = f'kvfile://{ctx.uri.hostname or "target"}' super().__init__(uri = uri) @@ -76,17 +35,10 @@ class KvFile(Base): self.__distro: Distro | None = distro self.___config_paths: list[str] | None = None self.__subtree = self.uri.path.lstrip('/') - # -- The context is read through a cache: the read path - # probes the same handful of paths again and again, and - # the cache pays the transport once per path self.__ctx = FcCache(ctx) @override async def _open(self) -> None: - # -- The lifecycle propagates to the host context: an - # open() pin on the store, the CopyContext of a copy - # walk, keeps the host's transport open for its - # duration, and the run()s within it share it await self.__raw.open() @override @@ -94,8 +46,6 @@ class KvFile(Base): await self.__raw.close() def __addr(self, path: str) -> str: - # -- The host-relative address: the subtree the URI roots - # the store at, then the address relative to it path = self._unchroot(path) if not path: return self.__subtree @@ -105,10 +55,6 @@ class KvFile(Base): @staticmethod def __resolve(path: str) -> tuple[str, bool]: - # -- The config path of the address, and whether the address - # names a blob file: a suffixed address is the on-host - # file name of its config path, the suffix picking the - # kind if path.endswith(SECRET_SUFFIX): return path[:-len(SECRET_SUFFIX)], False if path.endswith(BLOB_SUFFIX): @@ -116,27 +62,16 @@ class KvFile(Base): return path, False async def __config_paths(self) -> list[str]: - # -- The config paths of the host's package secrets: the - # templates of the installed packages, relative to the - # host root. The package database is the bounded base, a - # filesystem scan is not. Computed once: the package set - # is stable for the lifetime of the session if self.___config_paths is None: if self.__distro is None: from ...Distro import Distro from ...ExecContext import ExecContext - # -- The distro probes the host through the raw - # context: the cache is a plain file context, no - # exec of its own if not isinstance(self.__raw, ExecContext): raise ValueError( 'KvFile: a distro must be given when the host' ' context is not an exec context' ) - # -- A URI-built context has no app distro to - # inherit the package filter from: apply the - # app's documented fallback from ...PackageFilter import PackageFilterString pkg_filter_str = os.environ.get('JW_DEFAULT_PKG_FILTER') @@ -146,7 +81,6 @@ class KvFile(Base): PackageFilterString(pkg_filter_str) if pkg_filter_str else None ), ) - # -- The packages' file paths, filtered to the templates ret: set[str] = set() for f in await self.__distro.pkg_files(): if not f.endswith('.jw-tmpl'): @@ -189,11 +123,6 @@ class KvFile(Base): @override async def _stat(self, path: str, follow_symlinks: bool) -> StatResult: - # -- The metadata of the host file behind the node, the type - # overridden: an entry is a file that enumerates its - # keys, a key a leaf below it; a branch is a plain - # directory. A key has no file of its own, so it keeps - # the size 0 path = self.__addr(path) config, is_blob = self.__resolve(path) @@ -240,12 +169,6 @@ class KvFile(Base): @override async def _is_dir(self, path: str, follow_symlinks: bool) -> bool: - # -- An entry is a directory of its keys, a key below an - # entry is a leaf: the entry is a file on the host, so - # there is no host path below it to stat. A branch is a - # real directory on the host. The package set is not - # consulted: the write path never pays for the - # enumeration path = self.__addr(path) config, is_blob = self.__resolve(path) if not config: @@ -263,10 +186,6 @@ class KvFile(Base): @override async def _readdir(self, path: str) -> Iterable[str]: - # -- An entry lists its keys; a branch, the root included, - # lists the config paths below it, presence-checked on - # the host, under the on-host file name of the file that - # backs it path = self.__addr(path) config, is_blob = self.__resolve(path) if config and await self.__is_entry(config): @@ -315,7 +234,6 @@ class KvFile(Base): return await self.__ctx.get(real) real = f'/{config}' if await self.__ctx.file_exists(real + SECRET_SUFFIX): - # -- The entry file is the serialized entry return await self.__ctx.get(real + SECRET_SUFFIX) if await self.__ctx.file_exists(real + BLOB_SUFFIX): return await self.__ctx.get(real + BLOB_SUFFIX) @@ -331,11 +249,6 @@ class KvFile(Base): @override async def _mkdir(self, path: str, mode: int, ftype: FileType) -> None: - # -- The address space has no directories of its own: a node - # is a branch (a real directory) or an entry (a suffixed - # file); the type says which to build. Below a missing - # branch, mkdir fails with EROFS: the caller creates the - # levels path = self.__addr(path) config, _ = self.__resolve(path) real = f'/{config}' @@ -378,9 +291,6 @@ class KvFile(Base): path = self.__addr(path) config, is_blob = self.__resolve(path) if ftype.is_subtype(FT_SECRET): - # -- The whole entry: the content is the serialized - # entry, written over the entry file; the branch is - # a precondition, as for a blob if is_blob: raise ValueError( f'put: "{path}" names a blob file, but the ftype is an entry' @@ -412,10 +322,6 @@ class KvFile(Base): entry, _ = self.__resolve(entry) real_entry = f'/{entry}' if ftype.is_subtype(FT_SECRET_KEY): - # -- A key: merge into the entry file, which must exist; - # mkdir() is the materialization. The put is atomic, - # so it also overwrites an existing entry file, which - # is mode 0400 if not await self.__ctx.file_exists(f'{real_entry}{SECRET_SUFFIX}'): raise OSError(errno.EROFS, f'put: "{entry}" has no entry file') kv = await self.__entry_kv(entry) @@ -433,7 +339,4 @@ class KvFile(Base): @override async def _unlink(self, path: str) -> None: - # -- Unlink the file the address names: the address is the - # on-host file name below the subtree, so the host path - # is the composed address itself await self.__ctx.unlink(f'/{self.__addr(path)}')