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 039d4342..2154594e 100644 --- a/src/python/jw/pkg/lib/ec/secret_store/KvFile.py +++ b/src/python/jw/pkg/lib/ec/secret_store/KvFile.py @@ -63,7 +63,7 @@ class KvFile(Base): ) self.__backend_path_filter = __create_backend_path_filter() self.___distro: Distro | None = None - self.___config_paths: list[str] | None = None + self.___target_paths: list[str] | None = None self.__root = self.uri.path.lstrip('/') self.__ctx = FcCache(self.__inner) @@ -99,44 +99,44 @@ class KvFile(Base): ) return self.___distro - async def __config_paths(self) -> list[str]: - if self.___config_paths is None: + async def __target_paths(self) -> list[str]: + if self.___target_paths is None: ret: set[str] = set() for f in await (await self.__distro()).pkg_paths(): if not f.endswith('.jw-tmpl'): continue ret.add(f.lstrip('/')[:-len('.jw-tmpl')]) - self.___config_paths = sorted(ret) + self.___target_paths = sorted(ret) warm = ['/'] - warm += [f'/{p}{SECRET_SUFFIX}' for p in self.___config_paths] - warm += [f'/{p}{BLOB_SUFFIX}' for p in self.___config_paths] + warm += [f'/{p}{SECRET_SUFFIX}' for p in self.___target_paths] + warm += [f'/{p}{BLOB_SUFFIX}' for p in self.___target_paths] dirs: set[str] = set() - for p in self.___config_paths: + for p in self.___target_paths: parts = p.split('/') for i in range(1, len(parts)): dirs.add('/'.join(parts[:i])) for d in sorted(dirs): warm += [f'/{d}', f'/{d}{SECRET_SUFFIX}', f'/{d}{BLOB_SUFFIX}'] await self.__ctx.warm(warm) - assert self.___config_paths is not None - return self.___config_paths + assert self.___target_paths is not None + return self.___target_paths - async def __compound_kv(self, config_path: str) -> dict[str, str]: - path = f'/{config_path.lstrip("/")}{SECRET_SUFFIX}' + async def __compound_kv(self, target_path: str) -> dict[str, str]: + path = f'/{target_path.lstrip("/")}{SECRET_SUFFIX}' if not await self.__ctx.file_exists(path): - raise FileNotFoundError(config_path) + raise FileNotFoundError(target_path) ret = await self.__ctx.get(path) return parse_kv(ret.stdout) - async def __is_compound(self, config_path: str) -> bool: - if not config_path: + async def __is_compound(self, target_path: str) -> bool: + if not target_path: return False - return await self.__ctx.file_exists(f'/{config_path}{SECRET_SUFFIX}') + return await self.__ctx.file_exists(f'/{target_path}{SECRET_SUFFIX}') @override async def _stat(self, path: str, follow_symlinks: bool) -> StatResult: path = self.__host_path(path) - config_path, is_blob = self.__strip_suffix(path) + target_path, is_blob = self.__strip_suffix(path) async def __stat_of(host_path: str, ftype: FileType) -> StatResult: st = await self.__ctx.stat(host_path, follow_symlinks) @@ -149,16 +149,16 @@ class KvFile(Base): size = st.size, ) - if not config_path: + if not target_path: return await __stat_of('/', FT_DIR) if is_blob: - secret_path = f'/{config_path}{BLOB_SUFFIX}' + secret_path = f'/{target_path}{BLOB_SUFFIX}' if not await self.__ctx.file_exists(secret_path): raise FileNotFoundError(errno.ENOENT, f'stat: {path}') return await __stat_of(secret_path, FT_SECRET_FILE) - if await self.__is_compound(config_path): - return await __stat_of(f'/{config_path}{SECRET_SUFFIX}', FT_SECRET) - parent, sep, key = config_path.rpartition('/') + if await self.__is_compound(target_path): + return await __stat_of(f'/{target_path}{SECRET_SUFFIX}', FT_SECRET) + parent, sep, key = target_path.rpartition('/') if sep and key: parent, _ = self.__strip_suffix(parent) if await self.__is_compound(parent) and \ @@ -171,41 +171,41 @@ class KvFile(Base): FT_SECRET_KEY, mode = st.mode, ) - secret_path = f'/{config_path}{BLOB_SUFFIX}' + secret_path = f'/{target_path}{BLOB_SUFFIX}' if await self.__ctx.file_exists(secret_path): return await __stat_of(secret_path, FT_SECRET_FILE) - if await self.__ctx.is_dir(f'/{config_path}'): - return await __stat_of(f'/{config_path}', FT_DIR) + if await self.__ctx.is_dir(f'/{target_path}'): + return await __stat_of(f'/{target_path}', FT_DIR) raise FileNotFoundError(errno.ENOENT, f'stat: {path}') @override async def _is_dir(self, path: str, follow_symlinks: bool) -> bool: path = self.__host_path(path) - config_path, is_blob = self.__strip_suffix(path) - if not config_path: + target_path, is_blob = self.__strip_suffix(path) + if not target_path: return True - parent, sep, _ = config_path.rpartition('/') + parent, sep, _ = target_path.rpartition('/') if sep: parent, _ = self.__strip_suffix(parent) if await self.__is_compound(parent): return False - if await self.__is_compound(config_path): + if await self.__is_compound(target_path): return True if is_blob: return False - return await self.__ctx.is_dir(f'/{config_path}') + return await self.__ctx.is_dir(f'/{target_path}') @override async def _readdir(self, path: str) -> Iterable[str]: path = self.__host_path(path) - config_path, is_blob = self.__strip_suffix(path) - if config_path and await self.__is_compound(config_path): - return (await self.__compound_kv(config_path)).keys() - if is_blob and await self.__ctx.file_exists(f'/{config_path}{BLOB_SUFFIX}'): + target_path, is_blob = self.__strip_suffix(path) + if target_path and await self.__is_compound(target_path): + return (await self.__compound_kv(target_path)).keys() + if is_blob and await self.__ctx.file_exists(f'/{target_path}{BLOB_SUFFIX}'): raise NotADirectoryError(path) children: set[str] = set() - prefix = f'{config_path}/' if config_path else '' - for cfg in await self.__config_paths(): + prefix = f'{target_path}/' if target_path else '' + for cfg in await self.__target_paths(): if not cfg.startswith(prefix): continue rest = cfg[len(prefix):] @@ -219,8 +219,8 @@ class KvFile(Base): children.add(f'{label}{SECRET_SUFFIX}') if await self.__ctx.file_exists(f'/{cfg}{BLOB_SUFFIX}'): children.add(f'{label}{BLOB_SUFFIX}') - if config_path and not children: - if not await self.__ctx.is_dir(f'/{config_path}'): + if target_path and not children: + if not await self.__ctx.is_dir(f'/{target_path}'): raise FileNotFoundError(path) return children @@ -229,23 +229,23 @@ class KvFile(Base): self, path: str, wd: str | None, throw: bool, verbose: bool | None, title: str ) -> Result: path = self.__host_path(path) - config_path, is_blob = self.__strip_suffix(path) + target_path, is_blob = self.__strip_suffix(path) if is_blob: - secret_path = f'/{config_path}{BLOB_SUFFIX}' + secret_path = f'/{target_path}{BLOB_SUFFIX}' if not await self.__ctx.file_exists(secret_path): raise FileNotFoundError(path) return await self.__ctx.get(secret_path) - secret_path = f'/{config_path}{SECRET_SUFFIX}' + secret_path = f'/{target_path}{SECRET_SUFFIX}' if await self.__ctx.file_exists(secret_path): return await self.__ctx.get(secret_path) - secret_path = f'/{config_path}{BLOB_SUFFIX}' + secret_path = f'/{target_path}{BLOB_SUFFIX}' if await self.__ctx.file_exists(secret_path): return await self.__ctx.get(secret_path) - if await self.__ctx.is_dir(f'/{config_path}'): + if await self.__ctx.is_dir(f'/{target_path}'): raise IsADirectoryError(errno.EISDIR, f'get: {path}') - if '/' not in config_path: + if '/' not in target_path: raise FileNotFoundError(path) - compound, key = config_path.rsplit('/', 1) + compound, key = target_path.rsplit('/', 1) compound, _ = self.__strip_suffix(compound) kv = await self.__compound_kv(compound) try: @@ -256,8 +256,8 @@ class KvFile(Base): @override async def _mkdir(self, path: str, mode: int, ftype: FileType) -> None: path = self.__host_path(path) - config_path, _ = self.__strip_suffix(path) - host_path = f'/{config_path}' + target_path, _ = self.__strip_suffix(path) + host_path = f'/{target_path}' if ftype.is_subtype(FT_COMPOUND): if await self.__ctx.file_exists(host_path + SECRET_SUFFIX): return @@ -295,7 +295,7 @@ class KvFile(Base): ftype: FileType, ) -> Result: path = self.__host_path(path) - config_path, is_blob = self.__strip_suffix(path) + target_path, is_blob = self.__strip_suffix(path) if ftype.is_subtype(FT_SECRET): if is_blob: raise ValueError( @@ -303,7 +303,7 @@ class KvFile(Base): ) m = 0o400 if mode is None else int(mode, 8) return await self.__ctx.put( - f'/{config_path}{SECRET_SUFFIX}', + f'/{target_path}{SECRET_SUFFIX}', content, owner = owner, group = group, @@ -313,14 +313,14 @@ class KvFile(Base): if ftype.is_subtype(FT_SECRET_FILE): m = 0o400 if mode is None else int(mode, 8) return await self.__ctx.put( - f'/{config_path}{BLOB_SUFFIX}', + f'/{target_path}{BLOB_SUFFIX}', content, owner = owner, group = group, mode = m, atomic = True, ) - compound, sep, key = config_path.rpartition('/') + compound, sep, key = target_path.rpartition('/') if not sep or not compound or not key or '/' in key: raise ValueError(f'put: "{path}" is not a compound/key path') compound, _ = self.__strip_suffix(compound) diff --git a/src/python/jw/pkg/lib/ec/secret_store/layout.py b/src/python/jw/pkg/lib/ec/secret_store/layout.py index 67956350..ba8b54cd 100644 --- a/src/python/jw/pkg/lib/ec/secret_store/layout.py +++ b/src/python/jw/pkg/lib/ec/secret_store/layout.py @@ -12,6 +12,9 @@ from ...file_types import FT_COMPOUND, FT_REG, file_types # # Host-specific entries take precedence over default ones. +# Suffix of the template file accompanying a compiled target +TMPL_SUFFIX = '.jw-tmpl' + # Suffix of the key-value secret file accompanying a compiled target SECRET_SUFFIX = '.jw-secret' @@ -20,11 +23,11 @@ BLOB_SUFFIX = '.jw-secret-file' DEFAULT_PREFIX = 'default' -def content_name(name: str) -> str | None: - # -- The config path of an on-host secret file name: the name - # with its content suffix removed, or None if the name - # carries neither suffix - for suffix in (SECRET_SUFFIX, BLOB_SUFFIX): +def target_name(name: str) -> str | None: + # -- The target path of a store or template file name: the + # name with its content suffix removed, or None if the + # name carries no suffix + for suffix in (TMPL_SUFFIX, SECRET_SUFFIX, BLOB_SUFFIX): if name.endswith(suffix): return name.removesuffix(suffix) return None diff --git a/test/unit/python/jw/pkg/lib/ec/secret_store/layout/test.py b/test/unit/python/jw/pkg/lib/ec/secret_store/layout/test.py index 17b8d90e..1ca50ad0 100644 --- a/test/unit/python/jw/pkg/lib/ec/secret_store/layout/test.py +++ b/test/unit/python/jw/pkg/lib/ec/secret_store/layout/test.py @@ -5,7 +5,7 @@ from jw.pkg.lib.ec.secret_store.layout import ( FT_SECRET_FILE, FT_SECRET_KEY, SECRET_SUFFIX, - content_name, + target_name, host_prefix, ) from jw.pkg.lib.file_types import FT_COMPOUND, FT_DIR, FT_REG @@ -15,11 +15,11 @@ assert DEFAULT_PREFIX == 'default' assert SECRET_SUFFIX == '.jw-secret' assert BLOB_SUFFIX == '.jw-secret-file' -# content_name strips the content suffix, and names without a +# target_name strips the content suffix, and names without a # content suffix are not store content -assert content_name('etc/a.jw-secret') == 'etc/a' -assert content_name('etc/c.pem.jw-secret-file') == 'etc/c.pem' -assert content_name('etc/README.md') is None +assert target_name('etc/a.jw-secret') == 'etc/a' +assert target_name('etc/c.pem.jw-secret-file') == 'etc/c.pem' +assert target_name('etc/README.md') is None # An FQDN maps to its reversed, dot-split host root assert host_prefix('host.example.com') == 'com/example/host'