From 508bbd613d0b6ddce6a333224a47aadac5f84e04 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Mon, 9 Nov 2026 20:02:46 +0100 Subject: [PATCH] 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 '' 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 --- .../jw/pkg/cmds/secrets/lib/FilesContext.py | 16 ++-- test/unit/python/jw/pkg/cmds/secrets/Makefile | 4 + .../cmds/secrets/lib/FilesContext/Makefile | 8 ++ .../pkg/cmds/secrets/lib/FilesContext/test.py | 77 +++++++++++++++++++ .../python/jw/pkg/cmds/secrets/lib/Makefile | 4 + 5 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 test/unit/python/jw/pkg/cmds/secrets/Makefile create mode 100644 test/unit/python/jw/pkg/cmds/secrets/lib/FilesContext/Makefile create mode 100644 test/unit/python/jw/pkg/cmds/secrets/lib/FilesContext/test.py create mode 100644 test/unit/python/jw/pkg/cmds/secrets/lib/Makefile diff --git a/src/python/jw/pkg/cmds/secrets/lib/FilesContext.py b/src/python/jw/pkg/cmds/secrets/lib/FilesContext.py index 0f778df3..2033fe76 100644 --- a/src/python/jw/pkg/cmds/secrets/lib/FilesContext.py +++ b/src/python/jw/pkg/cmds/secrets/lib/FilesContext.py @@ -148,6 +148,9 @@ class FilesContext: tmp_path: str | None = None + # -- The filter scopes the distro's package selection, not + # this command's own output: the compiled file and its + # temp are written unfiltered try: tmp_path = await self.ctx.mktemp(dst + '.jw-pkg.XXXXX') await self.ctx.put( @@ -156,6 +159,7 @@ class FilesContext: owner = owner, group = group, mode = mode, + filtered = False, ) content_changed = True @@ -163,13 +167,15 @@ class FilesContext: old_meta = '' try: - st = await self.ctx.stat(dst) + st = await self.ctx.stat(dst, filtered = False) except FileNotFoundError: pass else: old_mode = stat.S_IMODE(st.mode) old_meta = self._format_metadata(st.owner, st.group, old_mode) - old_content = (await self.ctx.get(dst)).stdout_str_or_none + old_content = ( + await self.ctx.get(dst, filtered = False) + ).stdout_str_or_none content_changed = old_content is None or old_content != new_content metadata_changed = ( @@ -186,17 +192,17 @@ class FilesContext: log(NOTICE, f'Applying macros in {src} to {dst}: {details}') if not changes: - await self.ctx.unlink(tmp_path) + await self.ctx.unlink(tmp_path, filtered = False) tmp_path = None return - await self.ctx.rename(tmp_path, dst) + await self.ctx.rename(tmp_path, dst, filtered = False) tmp_path = None finally: if tmp_path is not None: with suppress(FileNotFoundError): - await self.ctx.unlink(tmp_path) + await self.ctx.unlink(tmp_path, filtered = False) async def compile_template_file( self, target_path: str, default_attrs: Attrs | None = None diff --git a/test/unit/python/jw/pkg/cmds/secrets/Makefile b/test/unit/python/jw/pkg/cmds/secrets/Makefile new file mode 100644 index 00000000..c87020cb --- /dev/null +++ b/test/unit/python/jw/pkg/cmds/secrets/Makefile @@ -0,0 +1,4 @@ +TOPDIR = ../../../../../../.. + +include $(TOPDIR)/make/proj.mk +include $(JWBDIR)/make/dirs.mk diff --git a/test/unit/python/jw/pkg/cmds/secrets/lib/FilesContext/Makefile b/test/unit/python/jw/pkg/cmds/secrets/lib/FilesContext/Makefile new file mode 100644 index 00000000..6a0e35b2 --- /dev/null +++ b/test/unit/python/jw/pkg/cmds/secrets/lib/FilesContext/Makefile @@ -0,0 +1,8 @@ +TOPDIR = ../../../../../../../../.. + +include $(TOPDIR)/make/proj.mk +include $(JWBDIR)/make/py-run.mk + +all: + +test: run diff --git a/test/unit/python/jw/pkg/cmds/secrets/lib/FilesContext/test.py b/test/unit/python/jw/pkg/cmds/secrets/lib/FilesContext/test.py new file mode 100644 index 00000000..90e7944f --- /dev/null +++ b/test/unit/python/jw/pkg/cmds/secrets/lib/FilesContext/test.py @@ -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() diff --git a/test/unit/python/jw/pkg/cmds/secrets/lib/Makefile b/test/unit/python/jw/pkg/cmds/secrets/lib/Makefile new file mode 100644 index 00000000..3b96da8e --- /dev/null +++ b/test/unit/python/jw/pkg/cmds/secrets/lib/Makefile @@ -0,0 +1,4 @@ +TOPDIR = ../../../../../../../.. + +include $(TOPDIR)/make/proj.mk +include $(JWBDIR)/make/dirs.mk -- 2.56.1