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