From bc54e4f3b2d5c884568d339d30d1b2aec17f1932 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sat, 7 Nov 2026 18:52:37 +0100 Subject: [PATCH] lib.ec.secret_store.KvFile: Init filter in FC-style Drop the re-read of JW_DEFAULT_PKG_FILTER, and derive the filter from the ctor and URI like all other FileContext instances would. Use that to filter the backend for the packages provinding secrets. Signed-off-by: Jan Lindemann Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0 --- src/python/jw/pkg/lib/CopyContext.py | 4 +- .../jw/pkg/lib/ec/secret_store/KvFile.py | 61 +++++++++++++------ .../jw/pkg/lib/ec/secret_store/KvFile/test.py | 41 ++++++++++--- 3 files changed, 79 insertions(+), 27 deletions(-) diff --git a/src/python/jw/pkg/lib/CopyContext.py b/src/python/jw/pkg/lib/CopyContext.py index 649a9614..ef78dcbf 100644 --- a/src/python/jw/pkg/lib/CopyContext.py +++ b/src/python/jw/pkg/lib/CopyContext.py @@ -50,10 +50,12 @@ class CopyContext: if self.__dst is None: if self.__dst_uri is None: raise Exception('Tried to create destination context without URI') + # -- The destination is the target of the copy, not part + # of the source's address space: the filter bounds the + # source only self.__dst = FileContext.create( self.__dst_uri, chroot = self.__chroot, - default_path_filter = self.__default_path_filter, ) await self.__dst.open() return self 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 87a0afe0..6985aef3 100644 --- a/src/python/jw/pkg/lib/ec/secret_store/KvFile.py +++ b/src/python/jw/pkg/lib/ec/secret_store/KvFile.py @@ -3,19 +3,21 @@ from __future__ import annotations import errno import os -from typing import TYPE_CHECKING, Any, Iterable, cast, override +from typing import TYPE_CHECKING, cast, override if TYPE_CHECKING: - from ...Distro import Distro + from typing import Any, Iterable, Literal from ...ExecContext import ExecContext from ...Uri import Uri from ...base import StatResult from ...file_types import FileType + from ...Distro import Distro from ...FcCache import FcCache from ...file_types import FT_COMPOUND, FT_DIR from ...FileContext import FileContext from ...kv import parse_kv, serialize_kv +from ...PathFilter import PathFilter from ...Result import Result from .Base import Base from .layout import BLOB_SUFFIX, FT_SECRET, FT_SECRET_FILE, FT_SECRET_KEY, SECRET_SUFFIX @@ -23,8 +25,34 @@ from .layout import BLOB_SUFFIX, FT_SECRET, FT_SECRET_FILE, FT_SECRET_KEY, SECRE class KvFile(Base): """The on-host store of a host's package secrets. """ - def __init__(self, uri: str | Uri, *args: Any, **kwargs: Any) -> None: - super().__init__(uri, *args, **kwargs) + def __init__( + self, + uri: str | Uri, + *args: Any, + default_path_filter: str | PathFilter | None | Literal[False] = None, + **kwargs: Any, + ) -> None: + + def __create_backend_path_filter() -> PathFilter | None: + + def __spec() -> str | None: + if isinstance(default_path_filter, str): + return default_path_filter + d = self.uri.query_dict + if d is None: + return None + return d.get('path-filter') + + if default_path_filter is False: + return None + if isinstance(default_path_filter, PathFilter): + return default_path_filter + spec: str | None = __spec() + if spec is None: + return None + return PathFilter.create(spec = spec, file_context = self.__inner) + + super().__init__(uri, *args, default_path_filter = False, **kwargs) kwargs.pop('chroot', None) scheme = 'ssh' if self.uri.hostname else 'local' self.__inner: ExecContext = cast( @@ -33,7 +61,8 @@ class KvFile(Base): f'{scheme}://{self.uri.authority}', chroot = False, **kwargs ), ) - self.__distro: Distro | None = None + self.__backend_path_filter = __create_backend_path_filter() + self.___distro: Distro | None = None self.___config_paths: list[str] | None = None self.__root = self.uri.path.lstrip('/') self.__ctx = FcCache(self.__inner) @@ -62,22 +91,18 @@ class KvFile(Base): return path[:-len(BLOB_SUFFIX)], True return path, False + async def __distro(self) -> Distro: + if self.___distro is None: + from ...Distro import Distro + self.___distro = await Distro.instantiate( + ec = self.__inner, default_pkg_filter = self.__backend_path_filter + ) + return self.___distro + async def __config_paths(self) -> list[str]: if self.___config_paths is None: - if self.__distro is None: - from ...Distro import Distro - from ...pf.Pkg import Pkg - - pkg_filter_str = os.environ.get('JW_DEFAULT_PKG_FILTER') - self.__distro = await Distro.instantiate( - ec = self.__inner, - default_pkg_filter = ( - Pkg(file_context = self, spec = pkg_filter_str) - if pkg_filter_str else None - ), - ) ret: set[str] = set() - for f in await self.__distro.pkg_files(): + for f in await (await self.__distro()).pkg_files(): if not f.endswith('.jw-tmpl'): continue ret.add(f.lstrip('/')[:-len('.jw-tmpl')]) diff --git a/test/unit/python/jw/pkg/lib/ec/secret_store/KvFile/test.py b/test/unit/python/jw/pkg/lib/ec/secret_store/KvFile/test.py index b9b11437..5e2830a9 100644 --- a/test/unit/python/jw/pkg/lib/ec/secret_store/KvFile/test.py +++ b/test/unit/python/jw/pkg/lib/ec/secret_store/KvFile/test.py @@ -349,18 +349,43 @@ async def run_tests() -> None: # -- The autodetected distro applies the app's documented # filter fallback: JW_DEFAULT_PKG_FILTER reaches # Distro.instantiate() as the default package filter - os.environ['JW_DEFAULT_PKG_FILTER'] = 'pkg:url=~jw.dev' - try: - astore = KvFile('kvfile://') - assert set(await astore.readdir('')) == {'tmp'} - flt = captured['kwargs'].get('default_pkg_filter') - assert flt is not None + astore = KvFile('kvfile://', default_path_filter = 'pkg:url=~jw.dev') + assert set(await astore.readdir('')) == {'tmp'} + flt = captured['kwargs'].get('default_pkg_filter') + assert flt is not None + assert flt.match_pkg(Package('fixture', url = 'https://jw.dev/fixture.rpm')) + assert not flt.match_pkg(Package('other', url = 'https://elsewhere/other.rpm')) + + # -- The backend filter also comes from the store URI's + # query's path-filter parameter; the constructor + # parameter wins + async def __flt_matches(flt: Any) -> None: assert flt.match_pkg(Package('fixture', url = 'https://jw.dev/fixture.rpm')) assert not flt.match_pkg( Package('other', url = 'https://elsewhere/other.rpm') ) - finally: - del os.environ['JW_DEFAULT_PKG_FILTER'] + + # No query, no parameter: no filter + s = KvFile('kvfile://') + await s.readdir('') + assert captured['kwargs'].get('default_pkg_filter') is None + # The query's path-filter parameter + s = KvFile('kvfile://?path-filter=pkg%3Aurl%3D~jw.dev') + await s.readdir('') + await __flt_matches(captured['kwargs'].get('default_pkg_filter')) + # An unrelated query parameter is not a filter spec + s = KvFile('kvfile://?foo=bar') + await s.readdir('') + assert captured['kwargs'].get('default_pkg_filter') is None + # The constructor parameter wins over the query + s = KvFile( + 'kvfile://?path-filter=pkg%3Aurl%3D~jw.dev', + default_path_filter = 'pkg:url=~zzz-nomatch', + ) + await s.readdir('') + flt = captured['kwargs'].get('default_pkg_filter') + assert flt is not None + assert not flt.match_pkg(Package('fixture', url = 'https://jw.dev/fixture.rpm')) patcher.stop()