cmds.secrets.lib.FilesContext: Write the compile output unfiltered #173

Merged
Jan Lindemann merged 1 commit from jan/fix/20261109-cmds-secrets-lib-filescontext-write-the-compile-output-unfiltered into master 2026-11-09 20:13:28 +01:00 AGit
5 changed files with 104 additions and 5 deletions

View file

@ -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 = '<missing>'
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

View file

@ -0,0 +1,4 @@
TOPDIR = ../../../../../../..
include $(TOPDIR)/make/proj.mk
include $(JWBDIR)/make/dirs.mk

View file

@ -0,0 +1,8 @@
TOPDIR = ../../../../../../../../..
include $(TOPDIR)/make/proj.mk
include $(JWBDIR)/make/py-run.mk
all:
test: run

View file

@ -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()

View file

@ -0,0 +1,4 @@
TOPDIR = ../../../../../../../..
include $(TOPDIR)/make/proj.mk
include $(JWBDIR)/make/dirs.mk