Skip to content

Commit 95be192

Browse files
authored
fix: world-readable owned /etc/hosts for non-root images (#81)
mktemp creates the script-owned /etc/hosts 0600; a non-root service image could not read the bind-mounted file, so name resolution fell through to DNS and failed ("could not translate host name: Temporary failure in name resolution"). Emit chmod 644 on the hosts file so any container uid can read it. Reproduced and fixed against real Podman 5.8.1. Bundled: require_string_keys now returns the validated keys as set[str] so the 11 unsupported-key sorted() paths type-check under ty 0.0.62 (no behavior change).
1 parent 3961b7d commit 95be192

8 files changed

Lines changed: 107 additions & 29 deletions

File tree

‎architecture/supported-subset.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,11 @@ flags. compose2pod hoists them onto `podman pod create` instead
595595
prefixed with `127.0.0.1 localhost` and `::1 localhost`. The script writes
596596
those lines to a `mktemp` path (`hostsfile=$(mktemp)`) once, before the
597597
first container starts, and removes it in the `trap ... EXIT` teardown
598-
(`rm -f "$hostsfile"`). `podman pod create` and every `podman run` —
598+
(`rm -f "$hostsfile"`). `mktemp` creates the file `0600`; the script then
599+
`chmod 644`s it so a service image running as a non-root user can read the
600+
bind-mounted `/etc/hosts` — without it, glibc cannot read the file, falls
601+
through to DNS, and resolution fails with "Temporary failure in name
602+
resolution". `podman pod create` and every `podman run` —
599603
target, `--rm` completion dependency, and `-d` long-running alike — pass
600604
`--no-hosts` and bind-mount the file read-only:
601605
`-v "$hostsfile":/etc/hosts:ro,z`. `--no-hosts` and `--add-host` conflict,

‎compose2pod/emit.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ def _plan(compose: dict[str, Any], options: EmitOptions) -> PlannedScript:
361361
lines.append(pod_create)
362362
_collect_vars(host_tokens, names)
363363
lines.append(f"printf '%s\\n' {_render(host_tokens)} > \"$hostsfile\"")
364+
# mktemp creates the file 0600; make it world-readable so a service image
365+
# running as a non-root user can read the bind-mounted /etc/hosts. Without
366+
# this, glibc falls through to DNS and name resolution fails.
367+
lines.append('chmod 644 "$hostsfile"')
364368
lines.extend(stores.create_lines(compose, order, options.pod, options.project_dir))
365369
names |= stores.referenced_variables(compose, order, options.project_dir)
366370
waited: set[str] = set()

‎compose2pod/keys.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,27 @@ def is_number(value: Any) -> bool: # noqa: ANN401 - Compose values are untyped
111111
return not isinstance(value, bool) and isinstance(value, int | float | str)
112112

113113

114-
def require_string_keys(where: str, mapping: dict[Any, Any]) -> None:
115-
"""Check every key of a raw YAML/JSON mapping is a string.
114+
def require_string_keys(where: str, mapping: dict[Any, Any]) -> set[str]:
115+
"""Check every key of a raw YAML/JSON mapping is a string; return them.
116116
117117
PyYAML routinely produces non-string keys (a bare `3:` is an int; under
118118
YAML 1.1, a bare `on:`/`off:` is a bool). Every mapping-key consumer
119119
downstream (`sorted()`, `str.startswith`, a compiled regex) assumes
120120
`str` and crashes raw otherwise. This is the one shared check the gate
121121
runs before it reads any of `mapping`'s keys, so a non-string key fails
122122
clean regardless of which mapping it turned up in.
123+
124+
Returns the validated keys as a `set[str]` so an unsupported-key check can
125+
build on it directly (`require_string_keys(...) - KNOWN`) with a statically
126+
sortable type, instead of re-deriving `set(mapping)` as `set[object]`.
123127
"""
128+
keys: set[str] = set()
124129
for key in mapping:
125130
if not isinstance(key, str):
126131
msg = f"{where}: key {key!r} must be a string"
127132
raise UnsupportedComposeError(msg)
133+
keys.add(key)
134+
return keys
128135

129136

130137
def _validate_list_elements(name: str, key: str, value: list[Any]) -> None:

‎compose2pod/parsing.py‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ def _validate_volume_long_form(name: str, entry: dict[str, Any]) -> None:
216216
here) because the check itself needs `vtype` to know which sub-map key --
217217
`bind`/`volume`/`tmpfs` -- the entry is allowed to carry alongside it.
218218
"""
219-
require_string_keys(f"service {name!r}: volume", entry)
219+
keys = require_string_keys(f"service {name!r}: volume", entry)
220220
vtype = entry.get("type")
221221
if vtype not in _VOLUME_LONG_TYPES:
222222
msg = f"service {name!r}: volume 'type' must be one of {list(_VOLUME_LONG_TYPES)}"
223223
raise UnsupportedComposeError(msg)
224-
unknown = set(entry) - _VOLUME_LONG_KEYS - {vtype}
224+
unknown = keys - _VOLUME_LONG_KEYS - {vtype}
225225
if unknown:
226226
msg = f"service {name!r}: volume: unsupported keys {sorted(unknown)}"
227227
raise UnsupportedComposeError(msg)
@@ -333,8 +333,8 @@ def _validate_volume_options(name: str, vtype: str, options: Any) -> None: # no
333333
if not isinstance(options, dict):
334334
msg = f"service {name!r}: {vtype} options must be a mapping"
335335
raise UnsupportedComposeError(msg)
336-
require_string_keys(f"service {name!r}: {vtype} options", options)
337-
unknown = set(options) - _VOLUME_OPTION_KEYS[vtype]
336+
keys = require_string_keys(f"service {name!r}: {vtype} options", options)
337+
unknown = keys - _VOLUME_OPTION_KEYS[vtype]
338338
if unknown:
339339
msg = f"service {name!r}: {vtype} options: unsupported keys {sorted(unknown)}"
340340
raise UnsupportedComposeError(msg)
@@ -529,8 +529,8 @@ def _validate_build_secret_entry(name: str, entry: Any) -> None: # noqa: ANN401
529529
if not isinstance(entry, dict):
530530
msg = f"service {name!r}: build 'secrets' entries must be a string or mapping"
531531
raise UnsupportedComposeError(msg)
532-
require_string_keys(f"service {name!r}: build 'secrets' entry", entry)
533-
unknown = set(entry) - _BUILD_SECRET_REF_KEYS
532+
keys = require_string_keys(f"service {name!r}: build 'secrets' entry", entry)
533+
unknown = keys - _BUILD_SECRET_REF_KEYS
534534
if unknown:
535535
msg = f"service {name!r}: build 'secrets' entry: unsupported keys {sorted(unknown)}"
536536
raise UnsupportedComposeError(msg)
@@ -683,8 +683,8 @@ def _validate_env_file(name: str, svc: dict[str, Any]) -> None:
683683

684684
def _validate_env_file_mapping(name: str, entry: dict[str, Any]) -> None:
685685
"""Check one long-form env_file mapping against Docker's strict schema (measured, v5.1.2)."""
686-
require_string_keys(f"service {name!r}: 'env_file'", entry)
687-
unknown = set(entry) - _ENV_FILE_ENTRY_KEYS
686+
keys = require_string_keys(f"service {name!r}: 'env_file'", entry)
687+
unknown = keys - _ENV_FILE_ENTRY_KEYS
688688
if unknown:
689689
msg = f"service {name!r}: 'env_file' entry: unsupported keys {sorted(unknown)}"
690690
raise UnsupportedComposeError(msg)
@@ -1240,8 +1240,8 @@ def validate(ident: str, key: str, value: Any) -> None: # noqa: ANN401 - untype
12401240
if values.is_bool_like(value):
12411241
return
12421242
if isinstance(value, dict):
1243-
require_string_keys(f"{label} {ident!r}: {key!r}", value)
1244-
unknown = set(value) - _EXTERNAL_MAP_KEYS
1243+
keys = require_string_keys(f"{label} {ident!r}: {key!r}", value)
1244+
unknown = keys - _EXTERNAL_MAP_KEYS
12451245
if unknown:
12461246
msg = f"{label} {ident!r}: {key!r}: unsupported keys {sorted(unknown)}"
12471247
raise UnsupportedComposeError(msg)
@@ -1278,8 +1278,8 @@ def _validate_ipam_config_entry(label: str, ident: str, entry: Any) -> None: #
12781278
if not isinstance(entry, dict):
12791279
msg = f"{label} {ident!r}: ipam 'config' entries must be a mapping"
12801280
raise UnsupportedComposeError(msg)
1281-
require_string_keys(f"{label} {ident!r}: ipam config entry", entry)
1282-
unknown = set(entry) - _IPAM_CONFIG_ENTRY_KEYS
1281+
keys = require_string_keys(f"{label} {ident!r}: ipam config entry", entry)
1282+
unknown = keys - _IPAM_CONFIG_ENTRY_KEYS
12831283
if unknown:
12841284
msg = f"{label} {ident!r}: ipam config entry: unsupported keys {sorted(unknown)}"
12851285
raise UnsupportedComposeError(msg)
@@ -1309,8 +1309,8 @@ def validate(ident: str, key: str, value: Any) -> None: # noqa: ANN401 - untype
13091309
if not isinstance(value, dict):
13101310
msg = f"{label} {ident!r}: {key!r} must be a mapping"
13111311
raise UnsupportedComposeError(msg)
1312-
require_string_keys(f"{label} {ident!r}: {key!r}", value)
1313-
unknown = set(value) - _IPAM_KEYS
1312+
keys = require_string_keys(f"{label} {ident!r}: {key!r}", value)
1313+
unknown = keys - _IPAM_KEYS
13141314
if unknown:
13151315
msg = f"{label} {ident!r}: {key!r}: unsupported keys {sorted(unknown)}"
13161316
raise UnsupportedComposeError(msg)

‎compose2pod/resources.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def _validate_limits(name: str, svc: dict[str, Any], limits: Any) -> None: # no
3636
if not isinstance(limits, dict):
3737
msg = f"service {name!r}: deploy.resources.limits must be a mapping"
3838
raise UnsupportedComposeError(msg)
39-
require_string_keys(f"service {name!r}: deploy.resources.limits", limits)
40-
unknown = set(limits) - set(_LIMITS)
39+
keys = require_string_keys(f"service {name!r}: deploy.resources.limits", limits)
40+
unknown = keys - set(_LIMITS)
4141
if unknown:
4242
msg = f"service {name!r}: deploy.resources.limits: unsupported keys {sorted(unknown)}"
4343
raise UnsupportedComposeError(msg)
@@ -54,8 +54,8 @@ def _validate_reservations(name: str, svc: dict[str, Any], reservations: Any) ->
5454
if not isinstance(reservations, dict):
5555
msg = f"service {name!r}: deploy.resources.reservations must be a mapping"
5656
raise UnsupportedComposeError(msg)
57-
require_string_keys(f"service {name!r}: deploy.resources.reservations", reservations)
58-
unknown = set(reservations) - {"cpus", "memory", "devices"}
57+
keys = require_string_keys(f"service {name!r}: deploy.resources.reservations", reservations)
58+
unknown = keys - {"cpus", "memory", "devices"}
5959
if unknown:
6060
msg = f"service {name!r}: deploy.resources.reservations: unsupported keys {sorted(unknown)}"
6161
raise UnsupportedComposeError(msg)
@@ -91,8 +91,8 @@ def validate_deploy(name: str, svc: dict[str, Any]) -> None:
9191
if not isinstance(deploy, dict):
9292
msg = f"service {name!r}: 'deploy' must be a mapping"
9393
raise UnsupportedComposeError(msg)
94-
require_string_keys(f"service {name!r}: deploy", deploy)
95-
unknown = set(deploy) - {"resources"}
94+
keys = require_string_keys(f"service {name!r}: deploy", deploy)
95+
unknown = keys - {"resources"}
9696
if unknown:
9797
msg = f"service {name!r}: deploy: only 'resources' is supported (got {sorted(unknown)})"
9898
raise UnsupportedComposeError(msg)
@@ -105,8 +105,8 @@ def validate_deploy(name: str, svc: dict[str, Any]) -> None:
105105
raise UnsupportedComposeError(msg)
106106
_reject_null_block(name, "deploy.resources.limits", resources, "limits")
107107
_reject_null_block(name, "deploy.resources.reservations", resources, "reservations")
108-
require_string_keys(f"service {name!r}: deploy.resources", resources)
109-
unknown = set(resources) - {"limits", "reservations"}
108+
keys = require_string_keys(f"service {name!r}: deploy.resources", resources)
109+
unknown = keys - {"limits", "reservations"}
110110
if unknown:
111111
msg = f"service {name!r}: deploy.resources: unsupported keys {sorted(unknown)}"
112112
raise UnsupportedComposeError(msg)

‎compose2pod/stores.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def _validate_def(name: str, definition: Any, kind: StoreKind) -> None: # noqa:
7373
if not isinstance(definition, dict):
7474
msg = f"{kind.label} {name!r} must be a mapping"
7575
raise UnsupportedComposeError(msg)
76-
require_string_keys(f"{kind.label} {name!r}", definition)
77-
unknown = set(definition) - kind.sources
76+
keys = require_string_keys(f"{kind.label} {name!r}", definition)
77+
unknown = keys - kind.sources
7878
if unknown:
7979
if "external" in unknown:
8080
msg = (
@@ -131,8 +131,8 @@ def _ref_source(name: str, ref: Any, kind: StoreKind) -> str: # noqa: ANN401 -
131131
if not isinstance(ref, dict):
132132
msg = f"service {name!r}: {kind.label} entry must be a string or mapping"
133133
raise UnsupportedComposeError(msg)
134-
require_string_keys(f"service {name!r}: {kind.label} entry", ref)
135-
unknown = set(ref) - _LONG_FORM_KEYS
134+
keys = require_string_keys(f"service {name!r}: {kind.label} entry", ref)
135+
unknown = keys - _LONG_FORM_KEYS
136136
if unknown:
137137
msg = f"service {name!r}: unsupported {kind.label} keys {sorted(unknown)}"
138138
raise UnsupportedComposeError(msg)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
summary: Generated scripts chmod the owned /etc/hosts to 644 so non-root service images can read it; without it name resolution fell through to DNS and failed.
3+
---
4+
5+
# Change: chmod the owned /etc/hosts world-readable
6+
7+
**Lane:** lightweight — ≲30 LOC net, ≤2 files, no new file, no public-API
8+
change, a single straightforward test.
9+
10+
## Goal
11+
12+
Fix pod-internal name resolution for service images that run as a **non-root**
13+
user. On Podman 5.x/6.x, a 0.4.0 script emits `hostsfile=$(mktemp)` — mode
14+
`0600`, owned by the invoking user — and bind-mounts it read-only as
15+
`/etc/hosts` into every container. A non-root process cannot read a `0600`
16+
root-owned file, so glibc skips `/etc/hosts`, falls through to DNS, and every
17+
lookup fails with `could not translate host name "db": Temporary failure in
18+
name resolution`. Root-user images were unaffected, which is why the 0.4.0
19+
mechanism passed validation (all probes ran as root).
20+
21+
## Approach
22+
23+
Emit one line, `chmod 644 "$hostsfile"`, immediately after the `printf` that
24+
writes the file and before the first `podman run` mounts it. The file holds
25+
only `hostname → 127.0.0.1` and `extra_hosts` (`hostname → ip`) lines — nothing
26+
secret — so world-readable is correct. `umask` cannot substitute: `mktemp`
27+
forces `0600` regardless. Truth home: `architecture/supported-subset.md`
28+
(networking / owned-`/etc/hosts` bullet).
29+
30+
Reproduced on real Podman 5.8.1 (via `quay.io/podman/stable:v5.8.1`): a
31+
non-root member gets `cat: /etc/hosts: Permission denied` and
32+
`socket.gaierror` without the chmod; resolves `db → 127.0.0.1` with it.
33+
34+
## Files
35+
36+
- `compose2pod/emit.py` — append the `chmod 644 "$hostsfile"` line after the
37+
hosts-file write.
38+
- `tests/test_emit.py` — assert the chmod line is emitted, ordered after the
39+
write and before the first `podman run`.
40+
41+
## Verification
42+
43+
- [x] Failing test first — `pytest -k test_hosts_file_made_world_readable`
44+
fails: `assert 'chmod 644 "$hostsfile"' in lines`.
45+
- [x] Apply the change.
46+
- [x] Test passes.
47+
- [x] `just test-ci` — 1402 passed, 100% coverage.
48+
- [x] `just lint-ci` — clean.
49+
- [x] End-to-end on Podman 5.8.1 — fixed-generator script runs a non-root app
50+
that resolves `db`.

‎tests/test_emit.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,19 @@ def test_hosts_file_written_before_first_run(self, chats_compose: dict) -> None:
661661
first_run_index = next(i for i, line in enumerate(lines) if line.startswith("podman run"))
662662
assert write_index < first_run_index
663663

664+
def test_hosts_file_made_world_readable(self, chats_compose: dict) -> None:
665+
# `mktemp` creates the file 0600, owned by the invoking user. A service
666+
# image running as a non-root user then cannot read the bind-mounted
667+
# /etc/hosts, so name resolution falls through to DNS and fails with
668+
# "Temporary failure in name resolution". `chmod` makes it readable to
669+
# every container uid. Ordered after the write, before the first mount.
670+
lines = self.make_script(chats_compose).splitlines()
671+
assert 'chmod 644 "$hostsfile"' in lines
672+
write_index = next(i for i, line in enumerate(lines) if line.startswith("printf '%s\\n'"))
673+
chmod_index = next(i for i, line in enumerate(lines) if line == 'chmod 644 "$hostsfile"')
674+
first_run_index = next(i for i, line in enumerate(lines) if line.startswith("podman run"))
675+
assert write_index < chmod_index < first_run_index
676+
664677
def test_hostsfile_removed_on_teardown(self, chats_compose: dict) -> None:
665678
script = self.make_script(chats_compose)
666679
trap = next(line for line in script.splitlines() if line.startswith("trap "))

0 commit comments

Comments
 (0)