lib.pf.PkgSecrets: Add
The on-host store scopes its address space to the config paths backed by installed packages, but the scope lives inside KvFile: it derives the set from the distro itself and consults it in the individual operations. A context that carries a filter must behave as the filter mandates, so the wanted set belongs in a filter of its own. Add the pkg-secrets path filter, a Pkg whose space is the config paths of the matching packages' template files - the path with the .jw-tmpl suffix stripped - plus the directories leading to them. A compound or blob address maps to its config path, a key address to the config path of the compound it lives under; any other address is judged on its own. The spec form follows pkg: a bare spec matches every installed package, and pkg-secrets:url=~REGEX narrows the backing set to packages whose url matches. Pkg parses its criterion through an overridable _parse_spec(), and its _match_pkg() matches every package on a None criterion, so both filters share the parsing and matching code. Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.2 Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
48ec9fdb95
commit
2416257cba
2 changed files with 247 additions and 4 deletions
7
test/unit/python/jw/pkg/lib/pf/PkgSecrets/Makefile
Normal file
7
test/unit/python/jw/pkg/lib/pf/PkgSecrets/Makefile
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
TOPDIR = ../../../../../../../../
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/py-run.mk
|
||||
|
||||
all:
|
||||
test: run
|
||||
138
test/unit/python/jw/pkg/lib/pf/PkgSecrets/test.py
Normal file
138
test/unit/python/jw/pkg/lib/pf/PkgSecrets/test.py
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from typing import TYPE_CHECKING, Any, override
|
||||
|
||||
from jw.pkg.lib.FileContext import FileContext
|
||||
from jw.pkg.lib.Package import Package
|
||||
from jw.pkg.lib.PathFilter import PathFilter, Verdict
|
||||
from jw.pkg.lib.pf.PkgSecrets import PkgSecrets
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from jw.pkg.lib.Result import Result
|
||||
|
||||
class _StubFC(FileContext):
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__(uri = 'file:///')
|
||||
|
||||
@override
|
||||
async def _get(
|
||||
self, path: str, wd: str | None, throw: bool, verbose: bool | None, title: str
|
||||
) -> Result:
|
||||
raise NotImplementedError()
|
||||
|
||||
# -- Unsupported specs raise at construction --
|
||||
|
||||
for bad in ['pkg-secrets', 'pkg-secrets:uri', 'pkg-secrets:name=~x']:
|
||||
try:
|
||||
PkgSecrets(spec = bad, file_context = _StubFC())
|
||||
assert False, f'Should have raised for "{bad}"'
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
class _FakeDistro:
|
||||
|
||||
def __init__(self, pkgs: list[tuple[str, str | None, list[str]]]) -> None:
|
||||
self.__pkgs = [
|
||||
(Package(name = name, url = url), files) for name, url, files in pkgs
|
||||
]
|
||||
|
||||
async def select(self,
|
||||
names: list[str] | None = None,
|
||||
filter: Any = None) -> list[Package]:
|
||||
return [p for p, _ in self.__pkgs if filter is None or filter.match_pkg(p)]
|
||||
|
||||
async def pkg_paths(self, names: list[str] | None = None) -> list[str]:
|
||||
ret: list[str] = []
|
||||
for p, files in self.__pkgs:
|
||||
if names is None or p.name in names:
|
||||
ret.extend(files)
|
||||
return ret
|
||||
|
||||
async def paths(self, filter: Any = None, pkg_names: Any = []) -> list[str]:
|
||||
ret: list[str] = []
|
||||
for p, files in self.__pkgs:
|
||||
if filter is not None and not filter.match_pkg(p):
|
||||
continue
|
||||
if pkg_names and p.name not in pkg_names:
|
||||
continue
|
||||
ret.extend(files)
|
||||
return ret
|
||||
|
||||
fake = _FakeDistro(
|
||||
[
|
||||
(
|
||||
'fixture',
|
||||
'https://example.com/fixture.rpm',
|
||||
['/etc/foo.conf.jw-tmpl', '/etc/fixture.conf', '/opt/jw-x/app.ini.jw-tmpl'],
|
||||
),
|
||||
('other', 'https://elsewhere.org/other.rpm', ['/etc/bar.conf.jw-tmpl']),
|
||||
]
|
||||
)
|
||||
|
||||
async def __run_tests() -> None:
|
||||
from unittest import mock
|
||||
|
||||
from jw.pkg.lib.Distro import Distro
|
||||
from jw.pkg.lib.ec.Local import Local
|
||||
|
||||
async def __instantiate(*args: Any, **kwargs: Any) -> _FakeDistro:
|
||||
return fake
|
||||
|
||||
with mock.patch.object(Distro, 'instantiate', new = classmethod(__instantiate)):
|
||||
# -- Unscoped: every installed package backs
|
||||
f = PkgSecrets(spec = 'pkg-secrets:', file_context = Local('local'))
|
||||
# A compound, a key below it, and a blob are all addressable
|
||||
assert await f.run('etc/foo.conf.jw-secret') == Verdict.ALLOW
|
||||
assert await f.run('etc/foo.conf.jw-secret/key') == Verdict.ALLOW
|
||||
# -- A key below the compound, its name unsuffixed
|
||||
assert await f.run('etc/foo.conf/key') == Verdict.ALLOW
|
||||
assert await f.run('opt/jw-x/app.ini/key') == Verdict.ALLOW
|
||||
assert await f.run('etc/plain/key') == Verdict.IGNORE
|
||||
assert await f.run('etc/foo.conf.jw-secret-file') == Verdict.ALLOW
|
||||
# A deep target path
|
||||
assert await f.run('opt/jw-x/app.ini.jw-secret') == Verdict.ALLOW
|
||||
# The other package backs too, unscoped
|
||||
assert await f.run('etc/bar.conf.jw-secret-file') == Verdict.ALLOW
|
||||
# A plain file is not a target path
|
||||
assert await f.run('etc/fixture.conf') == Verdict.IGNORE
|
||||
# No template for this target path: the orphan case
|
||||
assert await f.run('etc/fixture.conf.jw-secret') == Verdict.IGNORE
|
||||
assert await f.run('nope.jw-secret') == Verdict.IGNORE
|
||||
# Directories leading to target paths, and the root
|
||||
assert await f.run('etc') == Verdict.ALLOW
|
||||
assert await f.run('opt/jw-x') == Verdict.ALLOW
|
||||
assert await f.run('') == Verdict.ALLOW
|
||||
|
||||
# -- The backing set, as the filter computes it
|
||||
assert await f.target_paths() == [
|
||||
'etc/bar.conf', 'etc/foo.conf', 'opt/jw-x/app.ini'
|
||||
]
|
||||
|
||||
# -- Scoped: only the matching packages back
|
||||
f2 = PkgSecrets(
|
||||
spec = 'pkg-secrets:url=~example\\.com', file_context = Local('local')
|
||||
)
|
||||
assert await f2.run('etc/foo.conf.jw-secret') == Verdict.ALLOW
|
||||
assert await f2.run('etc/bar.conf.jw-secret') == Verdict.IGNORE
|
||||
assert await f2.target_paths() == ['etc/foo.conf', 'opt/jw-x/app.ini']
|
||||
|
||||
# -- PathFilter.create() resolves the kebab name
|
||||
f3 = PathFilter.create(
|
||||
spec = 'pkg-secrets:url=~example\\.com', file_context = Local('local')
|
||||
)
|
||||
assert isinstance(f3, PkgSecrets)
|
||||
|
||||
# -- A context that cannot be probed rejects
|
||||
f4 = PkgSecrets(spec = 'pkg-secrets:', file_context = _StubFC())
|
||||
try:
|
||||
await f4.run('etc/foo.conf.jw-secret')
|
||||
assert False, 'An unbacked context should reject path queries'
|
||||
except Exception as e:
|
||||
assert 'backed by distributions' in str(e)
|
||||
|
||||
asyncio.run(__run_tests())
|
||||
|
||||
print('All lib.pf.PkgSecrets tests passed')
|
||||
Loading…
Reference in a new issue