Files
mdella 43615a6fda fix(onboarding): identity read that survives a rewrite, disk refused before the pull — 0.1.8
Three faults from one agent-managed onboarding (env-dev-08, 2026-09-09), each of which pointed
the diagnosis away from the actual fault.

1. install.sh granted the agent's read on the ziti identity with a POSIX ACL. ziti-edge-tunnel
   rewrites that file on a controller config update and the rewrite drops the ACL: the agent
   applied cleanly at 01:21 and was failing every tick by 01:32. Group membership survives the
   rewrite (the file stays ziti:ziti 0640), so install.sh and the package postinstall now add
   monky-deployd to the `ziti` group, and a default ACL on the identity directory carries the
   grant onto a newly created file. The explicit ACLs stay for the boxes that need them.

2. openziti.load() accepts an unreadable or malformed identity: the C SDK logs "configuration is
   invalid" and returns a context that only fails at dial, as a bare TypeError, which the
   transport reported as a missing intercept or a policy gap. The SDK transport now reads and
   parses the identity itself and names the real fault first.

3. The disk pre-flight ran only when the bundle declared disk_need_bytes, so a bundle without one
   died mid-pull with containerd's "no space left on device" — which reads as a registry fault.
   A bundle that declares no size now has to clear the headroom floor, and the pre-flight measures
   containerd's root as well as the docker data-root: docker 29 keeps image layers in the
   containerd image store, and on env-dev-08 those sat on different filesystems (93 GiB free where
   the agent looked, 2.8 GiB where the pull wrote).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KLB7jieMNRkTsJ2epr4Ds1
2026-09-09 01:48:49 +00:00

105 lines
3.6 KiB
Python

import json
from pathlib import Path
from monky_deployd import __version__, cli
from monky_deployd.agent import EX_OK
def write_cfg(cfg):
lines = [
f"env_id: {cfg.env_id}",
f"site: {cfg.site}",
"transport: system",
"tenancy:",
f" host: {cfg.tenancy.host}",
f" port: {cfg.tenancy.port}",
" scheme: http",
"bao:",
f" addr: {cfg.bao.addr}",
" ca_bundle: none",
f"state_dir: {cfg.state_dir}",
f"bootstrap_path: {cfg.bootstrap_path}",
f"deploy_dir: {cfg.deploy_dir}",
"healthy_timeout_s: 3",
]
Path(cfg.path).write_text("\n".join(lines) + "\n")
return cfg.path
def test_run_once_then_status(bootstrapped, tenancy, fake_docker, capsys):
path = write_cfg(bootstrapped)
assert cli.main(["-c", path, "run", "--once"]) == EX_OK
assert cli.main(["-c", path, "status", "--json"]) == EX_OK
out = json.loads(capsys.readouterr().out)
assert out["in_sync"] is True and out["applied_sha"] == tenancy.desired_sha and out["token_present"] is True
assert out["healthy"] is True and out["containers"][0]["name"] == "see"
assert cli.main(["-c", path, "status"]) == EX_OK
text = capsys.readouterr().out
assert "in sync" in text and "healthy" in text and "hvs." not in text
def test_bootstrap_command(bootstrapped, bao, capsys):
cfg = bootstrapped
path = write_cfg(cfg)
assert cli.main(["-c", path, "bootstrap"]) == EX_OK
assert cfg.token_path.exists() and not Path(cfg.bootstrap_path).exists()
assert cli.main(["-c", path, "bootstrap"]) == EX_OK
assert "already bootstrapped" in capsys.readouterr().out
def test_version_and_bad_config(capsys, tmp_path):
assert cli.main(["version"]) == 0
assert capsys.readouterr().out.strip() == __version__
bad = tmp_path / "c.yaml"
bad.write_text("env_id: nope\nsite: cbs\n")
assert cli.main(["-c", str(bad), "status"]) == 78
assert cli.main(["-c", str(tmp_path / "missing.yaml"), "status"]) == 1
def test_sdk_transport_uses_openziti_monkeypatch(monkeypatch, tmp_path):
"""The sdk transport loads the identity once and dials inside openziti.monkeypatch()."""
import contextlib
import socket
import sys
import types
calls = []
fake = types.ModuleType("openziti")
fake.load = lambda p: calls.append(("load", p)) or object()
@contextlib.contextmanager
def mp():
calls.append(("monkeypatch",))
yield
fake.monkeypatch = mp
monkeypatch.setitem(sys.modules, "openziti", fake)
from monky_deployd.transport import SdkTransport
srv = socket.socket()
srv.bind(("127.0.0.1", 0))
srv.listen(1)
# the transport pre-flights the identity file before handing it to the SDK
(tmp_path / "id.json").write_text('{"ztAPI": "https://example.invalid"}')
t = SdkTransport(str(tmp_path / "id.json"))
s = t.connect("127.0.0.1", srv.getsockname()[1], 2)
s.close()
srv.close()
assert calls == [("load", str(tmp_path / "id.json")), ("monkeypatch",)]
assert "sdk(identity=" in t.describe()
def test_proxy_transport_refuses_unmapped_hosts():
from monky_deployd.config import from_dict
from monky_deployd.transport import TransportError, build
cfg = from_dict({"env_id": "env-dev-06", "site": "cbs", "transport": "proxy"})
t = build(cfg)
assert t.mapping[("monky.tenancy.deploy", 443)] == ("127.0.0.1", 18443)
assert t.mapping[("bao.cbs.tikali.net", 8200)] == ("127.0.0.1", 18200)
try:
t.connect("example.com", 443, 1)
raise AssertionError("unmapped host must be refused")
except TransportError:
pass