lib.ec.secret_store.KvFile: Use lib.pf.PkgSecrets
Do the filtering outside of KvFile, in a standard way. KvFile currently instantiates its backend distro with a self-instantiated backend filter, and doesn't propagate the filter passed to its ctor up to its base class. This arrangement makes it hard to ensure the class behaves in a standardish way. Allowing the base class filter to do its work, and not pass any filter to the backend context, is the more straightforward architecture, this is what this commit does. The base class still has some freedom to create a filter. Override the base class's _create_path_filter() to make sure that filter is a PkgSecrets / pkg-secrets: filter, anything else is not thought through at this point. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
fc79545c30
commit
f717fe2174
1 changed files with 136 additions and 63 deletions
|
|
@ -15,6 +15,7 @@ from jw.pkg.lib.ec.secret_store.layout import FT_SECRET, FT_SECRET_FILE, FT_SECR
|
|||
from jw.pkg.lib.file_types import FT_DIR
|
||||
from jw.pkg.lib.FileContext import FileContext
|
||||
from jw.pkg.lib.Package import Package
|
||||
from jw.pkg.lib.pf.PkgSecrets import PkgSecrets
|
||||
|
||||
async def run_tests() -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
|
|
@ -346,49 +347,121 @@ async def run_tests() -> None:
|
|||
assert not os.path.exists(os.path.join(tree, 'etc', 'sub', 'conf.jw-secret'))
|
||||
await sstore.close()
|
||||
|
||||
# -- The autodetected distro applies the app's documented
|
||||
# filter fallback: JW_DEFAULT_PATH_FILTER reaches
|
||||
# Distro.instantiate() as the default path filter
|
||||
astore = KvFile('kvfile://', default_path_filter = 'pkg:url=~jw.dev')
|
||||
assert set(await astore.readdir('')) == {'tmp'}
|
||||
flt = captured['kwargs'].get('default_path_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')
|
||||
)
|
||||
|
||||
# No query, no parameter: no filter
|
||||
s = KvFile('kvfile://')
|
||||
await s.readdir('')
|
||||
assert captured['kwargs'].get('default_path_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_path_filter'))
|
||||
# An unrelated query parameter is not a filter spec
|
||||
s = KvFile('kvfile://?foo=bar')
|
||||
await s.readdir('')
|
||||
assert captured['kwargs'].get('default_path_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_path_filter')
|
||||
assert flt is not None
|
||||
assert not flt.match_pkg(Package('fixture', url = 'https://jw.dev/fixture.rpm'))
|
||||
|
||||
patcher.stop()
|
||||
|
||||
# -- The address space is scoped by a pkg-secrets filter,
|
||||
# explicit or from the store URI's query: it scopes the
|
||||
# operations and the enumeration alike, and a foreign
|
||||
# filter kind is refused
|
||||
class FilteredFake(SuseDistro):
|
||||
|
||||
def __init__(self, pkgs: list[tuple[str, str, list[str]]]) -> None:
|
||||
super().__init__(Local(), id = 'suse')
|
||||
self.pkgs = pkgs
|
||||
|
||||
# -- The package enumeration seam: Distro.paths() goes
|
||||
# through _select(), which bottoms out here
|
||||
@override
|
||||
async def _select_by_name(self, names: Any) -> list[Package]:
|
||||
ret = [Package(name = n, url = u) for n, u, _ in self.pkgs]
|
||||
if names:
|
||||
ret = [p for p in ret if p.name in names]
|
||||
return ret
|
||||
|
||||
@override
|
||||
async def _pkg_paths(self, names: list[str] | None) -> list[str]:
|
||||
ret: list[str] = []
|
||||
for n, _, files in self.pkgs:
|
||||
if names is None or n in names:
|
||||
ret.extend(files)
|
||||
return ret
|
||||
|
||||
ffake = FilteredFake(
|
||||
[
|
||||
(
|
||||
'fixture',
|
||||
'https://jw.dev/fixture.rpm',
|
||||
[
|
||||
f'{tree}/etc/foo.conf.jw-tmpl',
|
||||
f'{tree}/etc/cert.pem.jw-tmpl',
|
||||
f'{tree}/etc/ghost.conf.jw-tmpl',
|
||||
],
|
||||
),
|
||||
(
|
||||
'other',
|
||||
'https://elsewhere.org/other.rpm',
|
||||
[f'{tree}/etc/zzz.conf.jw-tmpl'],
|
||||
),
|
||||
]
|
||||
)
|
||||
captured2: dict[str, Any] = {}
|
||||
|
||||
async def __instantiate2(*args: Any, **kwargs: Any) -> FilteredFake:
|
||||
captured2['kwargs'] = kwargs
|
||||
return ffake
|
||||
|
||||
patcher2 = mock.patch.object(
|
||||
Distro, 'instantiate', new = classmethod(__instantiate2)
|
||||
)
|
||||
patcher2.start()
|
||||
with open(os.path.join(tree, 'etc', 'zzz.conf.jw-secret'), 'w') as f:
|
||||
f.write('z=1\n')
|
||||
|
||||
# -- Raw: no filter, every installed template is a config
|
||||
# path, and the distro derivation carries no filter
|
||||
rstore = KvFile('kvfile://')
|
||||
assert rstore.path_filter is None
|
||||
assert set(await rstore.readdir(f'{base}/etc')) == {
|
||||
'cert.pem.jw-secret-file', 'foo.conf.jw-secret', 'zzz.conf.jw-secret'
|
||||
}
|
||||
# -- The distro derivation carries no filter of its own
|
||||
assert captured2['kwargs'].get('default_path_filter') is None
|
||||
|
||||
# -- Scoped: only the matching packages back the address
|
||||
# space; an unbacked entry is invisible and unwritable
|
||||
fstore = KvFile('kvfile://', default_path_filter = 'pkg-secrets:url=~jw.dev')
|
||||
assert isinstance(fstore.path_filter, PkgSecrets)
|
||||
assert set(await fstore.readdir(f'{base}/etc')) == \
|
||||
{'cert.pem.jw-secret-file', 'foo.conf.jw-secret'}
|
||||
try:
|
||||
await fstore.get(f'{base}/etc/zzz.conf')
|
||||
raise AssertionError('expected FileNotFoundError')
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
await fstore.put(f'{base}/etc/zzz.conf', b'z=2\n', ftype = FT_SECRET)
|
||||
with open(os.path.join(tree, 'etc', 'zzz.conf.jw-secret')) as f:
|
||||
assert f.read() == 'z=1\n'
|
||||
# -- A backed address writes through
|
||||
await fstore.put(f'{base}/etc/foo.conf/k9', b'v9', ftype = FT_SECRET_KEY)
|
||||
assert (await fstore.get(f'{base}/etc/foo.conf/k9')).stdout == b'v9'
|
||||
|
||||
# -- The query's path-filter parameter
|
||||
qstore = KvFile('kvfile://?path-filter=pkg-secrets%3Aurl%3D~elsewhere')
|
||||
assert isinstance(qstore.path_filter, PkgSecrets)
|
||||
assert set(await qstore.readdir(f'{base}/etc')) == {'zzz.conf.jw-secret'}
|
||||
|
||||
# -- An unrelated query parameter is not a filter spec
|
||||
s = KvFile('kvfile://?foo=bar')
|
||||
assert s.path_filter is None
|
||||
|
||||
# -- The query wins over the constructor parameter
|
||||
wstore = KvFile(
|
||||
'kvfile://?path-filter=pkg-secrets%3Aurl%3D~jw.dev',
|
||||
default_path_filter = 'pkg-secrets:url=~elsewhere',
|
||||
)
|
||||
assert set(await wstore.readdir(f'{base}/etc')
|
||||
) == {'cert.pem.jw-secret-file', 'foo.conf.jw-secret'}
|
||||
|
||||
# -- A foreign filter kind is refused at resolution
|
||||
xstore = KvFile('kvfile://', default_path_filter = 'pkg:url=~jw.dev')
|
||||
try:
|
||||
xstore.path_filter
|
||||
raise AssertionError('expected Exception')
|
||||
except Exception as e:
|
||||
assert 'pkg-secrets' in str(e)
|
||||
|
||||
patcher2.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
|
||||
|
|
|
|||
Loading…
Reference in a new issue