cmds.secrets.lib.FilesContext: Write the compile output unfiltered
The path filter of the exec context filtered the compile's own file operations: put() and rename() silently no-op on a filtered path, stat() raises, and the compile swallows that into a false '<missing>' verdict. A compile-templates run with a path filter therefore reported '@content' for every template while installing nothing, and left the empty temp files behind. The filter scopes the distro's package selection, not the command's own output. Write the compiled file and its temp unfiltered: put, stat, the read of the previous output, rename, and unlink. The reads of the template and of the attribute files keep the filter. Add a unit test with a context whose package space holds only the template: the output must be written and no temp must leak. 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:
parent
90c27fca24
commit
508bbd613d
4 changed files with 104 additions and 5 deletions
4
test/unit/python/jw/pkg/cmds/secrets/Makefile
Normal file
4
test/unit/python/jw/pkg/cmds/secrets/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/dirs.mk
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
TOPDIR = ../../../../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/py-run.mk
|
||||
|
||||
all:
|
||||
|
||||
test: run
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import grp
|
||||
import os
|
||||
import pwd
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from typing import TYPE_CHECKING, override
|
||||
|
||||
from jw.pkg.cmds.secrets.lib.FilesContext import FilesContext
|
||||
from jw.pkg.cmds.secrets.lib.base import Attrs
|
||||
from jw.pkg.lib.PathFilter import Verdict
|
||||
from jw.pkg.lib.ec.Local import Local
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import os
|
||||
|
||||
# -- A context whose package space holds only the template, as on a
|
||||
# real host: the output file and the temp are not package files.
|
||||
# If the compile's own file operations honor the filter, the
|
||||
# output is never written; the test asserts that they bypass it.
|
||||
|
||||
class TemplateSpace(Local):
|
||||
|
||||
def __init__(self, uri: str, template: str) -> None:
|
||||
super().__init__(uri)
|
||||
self.__template = template
|
||||
|
||||
@override
|
||||
async def check_filtered(
|
||||
self,
|
||||
path: str,
|
||||
mode: int = os.F_OK,
|
||||
action: str = '',
|
||||
caller: tuple[str, str, int] | None = None,
|
||||
) -> Verdict:
|
||||
if path == self.__template:
|
||||
return Verdict.ALLOW
|
||||
return Verdict.IGNORE
|
||||
|
||||
def test_compile_writes_unfiltered() -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
src = f'{tmp}/x.conf.jw-tmpl'
|
||||
with open(src, 'w', encoding = 'utf-8') as f:
|
||||
f.write('FOO = __FOO__\n')
|
||||
dst = f'{tmp}/x.conf'
|
||||
|
||||
# -- The default attributes are root:root 400; attribute the
|
||||
# output to the test user so the put works unprivileged
|
||||
attrs = Attrs(
|
||||
owner = pwd.getpwuid(os.getuid()).pw_name,
|
||||
group = grp.getgrgid(os.getgid()).gr_name,
|
||||
mode = 0o440,
|
||||
)
|
||||
|
||||
ec = TemplateSpace(f'local://{tmp}', src)
|
||||
fc = FilesContext(ec)
|
||||
asyncio.run(
|
||||
fc._compile_one_template_file(
|
||||
src = src,
|
||||
dst = dst,
|
||||
default_attrs = attrs,
|
||||
replace = {'__FOO__': 'bar'},
|
||||
)
|
||||
)
|
||||
|
||||
assert os.path.exists(dst), 'the compile must write its output'
|
||||
with open(dst, encoding = 'utf-8') as f:
|
||||
assert f.read() == 'FOO = bar\n'
|
||||
leftovers = [n for n in os.listdir(tmp) if '.jw-pkg.' in n]
|
||||
assert leftovers == [], f'temp files leaked: {leftovers}'
|
||||
|
||||
print('All FilesContext tests passed')
|
||||
|
||||
test_compile_writes_unfiltered()
|
||||
4
test/unit/python/jw/pkg/cmds/secrets/lib/Makefile
Normal file
4
test/unit/python/jw/pkg/cmds/secrets/lib/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/dirs.mk
|
||||
Loading…
Reference in a new issue