lib.ec.secret_store.KvFile: Construct from a URI

KvFile.__init__() accepts a host FileContext plus an optional Distro, and
the two may live on different hosts. In effect, files are read from one
host based on another host's package set, which may have its use cases but
is generally undesirable. The context parameter also breaks the uniformity
of the store constructors: Sops, TarGpg, and Vault are constructed from a
URI alone, and FileContext.create() needs a special case that builds the
kvfile store's inner context before handing it to the constructor.

This commit has KvFile constructed from a URI alone like the other stores.
The URI's authority names the host, no authority means "local", and the
constructor builds the inner context through FileContext.create() with
chroot off, forwarding the store's constructor parameters to it. The inner
context is always an exec context, which the distro derivation needs: it is
lazy, Distro.instantiate() runs on the first file listing, with the
JW_DEFAULT_PKG_FILTER environment variable as the default package filter,
as before.

Adapt all call sites:

- FileContext.create(): the kvfile case returns KvFile(uri) directly.
- FilesContext: the distro parameter goes; the store URI is derived from
  the exec context's scheme, authority, and path, so a local context with a
  hostname in its URI stays local.
- DistroContext.install(): the kvfile source wrap goes; the compounds read
  directly through the source context with parse_kv().
- The store tests monkey-patch Distro.instantiate() for the stores that
  list something

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-11-07 10:54:15 +01:00
commit e96bfca0e1
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
4 changed files with 103 additions and 108 deletions

View file

@ -5,7 +5,9 @@ import stat
import tempfile
from typing import Any, override
from unittest import mock
from jw.pkg.lib.Distro import Distro
from jw.pkg.lib.distros.suse.Distro import Distro as SuseDistro
from jw.pkg.lib.ec.Local import Local
from jw.pkg.lib.ec.secret_store.KvFile import KvFile
@ -30,7 +32,7 @@ async def run_tests() -> None:
assert isinstance(created, KvFile)
assert created.uri.scheme == 'kvfile'
store = KvFile(Local())
store = KvFile('kvfile://')
assert set(await store.readdir(target)) == {'key1', 'key2', 'key3'}
assert (await store.get(f'{target}/key1')).stdout == b'value1'
assert (await store.get(f'{target}/key3')).stdout == b''
@ -224,8 +226,22 @@ async def run_tests() -> None:
)
# -- The address space is rooted at the host root; the
# package file list reports host-absolute paths, so the
# fixture tree sits at its path below the root
bstore = KvFile(Local(), distro = fake)
# fixture tree sits at its path below the root. The
# store derives its distro from the host context, so the
# derivation is patched to the fake for the sections
# below
captured: dict[str, Any] = {}
async def __instantiate(*args: Any, **kwargs: Any) -> FakeDistro:
captured['kwargs'] = kwargs
return fake
patcher = mock.patch.object(
Distro, 'instantiate', new = classmethod(__instantiate)
)
patcher.start()
bstore = KvFile('kvfile://')
base = tree.lstrip('/')
# -- is_dir answers from the host state, without consulting
# the package set
@ -281,7 +297,7 @@ async def run_tests() -> None:
# without a double path
created = FileContext.create(f'kvfile://{tree}/etc')
assert created.uri.path == f'{tree}/etc'
sstore = KvFile(Local(), uri = f'kvfile://{tree}/etc', distro = fake)
sstore = KvFile(f'kvfile://{tree}/etc')
await sstore.open()
assert set(await sstore.readdir('')) == \
{'cert.pem.jw-secret-file', 'foo.conf.jw-secret'}
@ -322,32 +338,19 @@ 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
from unittest import mock
from jw.pkg.lib.Distro import Distro
captured: dict[str, Any] = {}
async def __instantiate(*args: Any, **kwargs: Any) -> FakeDistro:
captured['kwargs'] = kwargs
return fake
os.environ['JW_DEFAULT_PKG_FILTER'] = 'url=~jw.dev'
try:
with mock.patch.object(Distro,
'instantiate',
new = classmethod(__instantiate)):
astore = KvFile(Local())
assert set(await astore.readdir('')) == {'tmp'}
flt = captured['kwargs'].get('default_pkg_filter')
assert flt is not None
assert flt.match(Package('fixture', url = 'https://jw.dev/fixture.rpm'))
assert not flt.match(
Package('other', url = 'https://elsewhere/other.rpm')
)
astore = KvFile('kvfile://')
assert set(await astore.readdir('')) == {'tmp'}
flt = captured['kwargs'].get('default_pkg_filter')
assert flt is not None
assert flt.match(Package('fixture', url = 'https://jw.dev/fixture.rpm'))
assert not flt.match(Package('other', url = 'https://elsewhere/other.rpm'))
finally:
del os.environ['JW_DEFAULT_PKG_FILTER']
patcher.stop()
# -- The lifecycle propagates to the host context: an
# open() pin on the store keeps the host's transport
# open for its duration, and the run()s within it share
@ -370,7 +373,10 @@ async def run_tests() -> None:
await super()._close()
cl = CountingLocal()
pstore = KvFile(cl)
with mock.patch.object(FileContext,
'create',
new = classmethod(lambda *a, **k: cl)):
pstore = KvFile('kvfile://')
await pstore.open()
assert cl.opens == 1
await cl.run(['true'])

View file

@ -6,8 +6,10 @@ import subprocess
import tempfile
from glob import glob
from typing import override
from typing import Any, override
from unittest import mock
from jw.pkg.lib.Distro import Distro
from jw.pkg.lib.distros.suse.Distro import Distro as SuseDistro
from jw.pkg.lib.ec.Local import Local
from jw.pkg.lib.ec.secret_store.KvFile import KvFile
@ -343,11 +345,18 @@ async def run_tests() -> None:
f'{host_tree}/etc/cert.pem.jw-tmpl',
]
)
# -- The package file list reports host-absolute paths, so
# the fixture tree sits at its path below the host root
src = KvFile(Local(), distro = fake)
# the fixture tree sits at its path below the host root.
# The store derives its distro from the host context, so
# the derivation is patched to the fake for the copy
async def __instantiate(*args: Any, **kwargs: Any) -> FakeDistro:
return fake
src = KvFile('kvfile://')
base = host_tree.lstrip('/')
ret = await copy(src, 'sops://fetch.example.com', recursive = True)
with mock.patch.object(Distro, 'instantiate', new = classmethod(__instantiate)):
ret = await copy(src, 'sops://fetch.example.com', recursive = True)
assert isinstance(ret, list)
# -- The copy transports the on-host file names unchanged
assert set(ret) == {

View file

@ -7,8 +7,10 @@ import subprocess
import tarfile
import tempfile
from typing import override
from typing import Any, override
from unittest import mock
from jw.pkg.lib.Distro import Distro
from jw.pkg.lib.distros.suse.Distro import Distro as SuseDistro
from jw.pkg.lib.ec.Local import Local
from jw.pkg.lib.ec.secret_store.KvFile import KvFile
@ -328,9 +330,14 @@ async def run_tests() -> None:
f'{host3}/etc/cert.pem.jw-tmpl',
]
)
src = KvFile(Local(), distro = fake)
async def __instantiate(*args: Any, **kwargs: Any) -> FakeDistro:
return fake
src = KvFile('kvfile://')
base = host3.lstrip('/')
ret = await copy(src, 'targpg://host3.example.com', recursive = True)
with mock.patch.object(Distro, 'instantiate', new = classmethod(__instantiate)):
ret = await copy(src, 'targpg://host3.example.com', recursive = True)
assert isinstance(ret, list)
# -- The copy transports the on-host file names unchanged
assert set(ret) == {

View file

@ -9,7 +9,9 @@ import uuid
from pathlib import Path
from typing import Any, override
from unittest import mock
from jw.pkg.lib.Distro import Distro
from jw.pkg.lib.distros.suse.Distro import Distro as SuseDistro
from jw.pkg.lib.ec.Local import Local
from jw.pkg.lib.ec.secret_store.KvFile import KvFile
@ -358,9 +360,14 @@ async def run_tests() -> None:
f'{host_tree}/etc/cert.pem.jw-tmpl',
]
)
src = KvFile(Local(), distro = fake)
async def __instantiate(*args: Any, **kwargs: Any) -> FakeDistro:
return fake
src = KvFile('kvfile://')
base = host_tree.lstrip('/')
ret = await copy(src, f'vault://{fetch_host}', recursive = True)
with mock.patch.object(Distro, 'instantiate', new = classmethod(__instantiate)):
ret = await copy(src, f'vault://{fetch_host}', recursive = True)
assert isinstance(ret, list)
# -- The copy transports the on-host file names unchanged
assert set(ret) == {