hypdmz 0.1.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hypdmz/__init__.py +3 -0
- hypdmz/__main__.py +5 -0
- hypdmz/cli.py +165 -0
- hypdmz/doctor.py +76 -0
- hypdmz/egress.py +263 -0
- hypdmz/env.example +37 -0
- hypdmz/env.py +254 -0
- hypdmz/examples/first-swarm/scores.csv +51 -0
- hypdmz/examples/first-swarm/seats.toml +11 -0
- hypdmz/examples/first-swarm/task.toml +14 -0
- hypdmz/images/controller/Dockerfile +11 -0
- hypdmz/images/controller/apply-lockdown.sh +96 -0
- hypdmz/images/controller/entrypoint.sh +75 -0
- hypdmz/images/controller/inspect-network.sh +27 -0
- hypdmz/images/proxy/Dockerfile +26 -0
- hypdmz/images/proxy/app/__init__.py +0 -0
- hypdmz/images/proxy/app/audit.py +206 -0
- hypdmz/images/proxy/app/dlp.py +66 -0
- hypdmz/images/proxy/app/firewall.py +218 -0
- hypdmz/images/proxy/app/firewall_policy.py +89 -0
- hypdmz/images/proxy/app/injection.py +99 -0
- hypdmz/images/proxy/app/limits.py +63 -0
- hypdmz/images/proxy/app/main.py +493 -0
- hypdmz/images/proxy/app/request_gates.py +111 -0
- hypdmz/images/proxy/app/tls_mitm.py +242 -0
- hypdmz/images/proxy/app/vault.py +442 -0
- hypdmz/images/proxy/config/firewall.json +40 -0
- hypdmz/images/proxy/entrypoint.sh +29 -0
- hypdmz/images/proxy/requirements.txt +4 -0
- hypdmz/images/swarm/Dockerfile +26 -0
- hypdmz/images/swarm/entrypoint.sh +78 -0
- hypdmz/images/swarm/probe_dns.py +14 -0
- hypdmz/platforms/__init__.py +16 -0
- hypdmz/platforms/base.py +108 -0
- hypdmz/platforms/linux.py +146 -0
- hypdmz/platforms/macos.py +101 -0
- hypdmz/platforms/windows.py +135 -0
- hypdmz/redact.py +78 -0
- hypdmz/runner.py +388 -0
- hypdmz/status.py +75 -0
- hypdmz-0.1.0a1.dist-info/METADATA +134 -0
- hypdmz-0.1.0a1.dist-info/RECORD +45 -0
- hypdmz-0.1.0a1.dist-info/WHEEL +5 -0
- hypdmz-0.1.0a1.dist-info/entry_points.txt +2 -0
- hypdmz-0.1.0a1.dist-info/top_level.txt +1 -0
hypdmz/__init__.py
ADDED
hypdmz/__main__.py
ADDED
hypdmz/cli.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import shutil
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from hypdmz import __version__
|
|
10
|
+
from hypdmz.doctor import cmd_doctor
|
|
11
|
+
from hypdmz.egress import cmd_egress_check
|
|
12
|
+
from hypdmz.env import Config, load
|
|
13
|
+
from hypdmz.platforms import detect
|
|
14
|
+
from hypdmz.runner import bundled_examples, cmd_build, cmd_debug, cmd_down, cmd_logs, cmd_run, cmd_up, ensure_policy
|
|
15
|
+
from hypdmz.status import cmd_status
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def cmd_init(cfg: Config) -> int:
|
|
19
|
+
"""Create the state directory and its .env from the package's template; say what comes next."""
|
|
20
|
+
cfg.state_dir.mkdir(parents=True, exist_ok=True)
|
|
21
|
+
if cfg.env_file.exists():
|
|
22
|
+
print(f"[hypdmz] .env already there: {cfg.env_file}")
|
|
23
|
+
else:
|
|
24
|
+
shutil.copyfile(cfg.env_example, cfg.env_file)
|
|
25
|
+
print(f"[hypdmz] wrote {cfg.env_file}")
|
|
26
|
+
created = ensure_policy(cfg)
|
|
27
|
+
if not created:
|
|
28
|
+
print(f"[hypdmz] policy already there: {cfg.policy_path}")
|
|
29
|
+
print(f"""
|
|
30
|
+
[hypdmz] next:
|
|
31
|
+
1. edit {cfg.env_file}: MODEL_BASE_URL and MODEL (DeepInfra is the default)
|
|
32
|
+
2. put the model API key in a podman secret: podman secret create {cfg.key_secret} -
|
|
33
|
+
(paste the key, Enter, then Ctrl-D on Linux/macOS or Ctrl-Z Enter on Windows)
|
|
34
|
+
3. hypdmz doctor · hypdmz build · hypdmz up · hypdmz run first-swarm --python --budget-usd 0.20
|
|
35
|
+
the allowlist is {cfg.policy_path} (hot-reloaded; `hypdmz policy reset` restores the default)""")
|
|
36
|
+
return 0
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def cmd_policy(cfg: Config, action: str) -> int:
|
|
40
|
+
if action == "path":
|
|
41
|
+
print(cfg.policy_path)
|
|
42
|
+
return 0
|
|
43
|
+
if action == "reset":
|
|
44
|
+
cfg.policy_dir.mkdir(parents=True, exist_ok=True)
|
|
45
|
+
if cfg.policy_path.exists():
|
|
46
|
+
backup = cfg.policy_path.with_suffix(".json.bak")
|
|
47
|
+
shutil.copyfile(cfg.policy_path, backup)
|
|
48
|
+
print(f"[hypdmz] previous policy kept at {backup}")
|
|
49
|
+
shutil.copyfile(cfg.default_policy, cfg.policy_path)
|
|
50
|
+
print(f"[hypdmz] policy reset to the package default → {cfg.policy_path} (the proxy picks it up within a second)")
|
|
51
|
+
return 0
|
|
52
|
+
# show
|
|
53
|
+
src = cfg.policy_path if cfg.policy_path.exists() else cfg.default_policy
|
|
54
|
+
try:
|
|
55
|
+
pol = json.loads(src.read_text(encoding="utf-8"))
|
|
56
|
+
except Exception as e:
|
|
57
|
+
print(f"[hypdmz] {src} does not parse ({e}) — the proxy denies everything until it does")
|
|
58
|
+
return 1
|
|
59
|
+
print(f"[hypdmz] {src}{'' if src == cfg.policy_path else ' (package default; no operator policy yet)'}")
|
|
60
|
+
lim = pol.get("limits") or {}
|
|
61
|
+
print(f" limits: {lim.get('calls_per_hour') or '∞'} calls/h · {lim.get('megabytes_per_hour') or '∞'} MB/h")
|
|
62
|
+
for c in pol.get("cidr_blocklist") or []:
|
|
63
|
+
print(f" block {c.get('name')}: {', '.join(c.get('cidrs') or [])}")
|
|
64
|
+
for r in pol.get("rules") or []:
|
|
65
|
+
hosts = r.get("host") or ", ".join(r.get("hosts") or r.get("host_suffixes") or []) or r.get("host_regex") or "*"
|
|
66
|
+
flag = "" if r.get("enabled", True) else " (disabled)"
|
|
67
|
+
guard = f" guard={r['guard']}" if r.get("guard") else ""
|
|
68
|
+
exp = f" until {r['expires_at']}" if r.get("expires_at") else ""
|
|
69
|
+
print(f" rule {r.get('name')}: {r.get('scheme', 'https')}://{hosts} {r.get('ports') or ''} "
|
|
70
|
+
f"{'/'.join(r.get('methods') or ['*'])} {' '.join(r.get('path_prefixes') or ['/'])}"
|
|
71
|
+
f"{' injects ' + ','.join(r['injects']) if r.get('injects') else ''}{guard}{exp}{flag}")
|
|
72
|
+
return 0
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def cmd_examples(cfg: Config, name: str, dest: Path | None) -> int:
|
|
76
|
+
names = bundled_examples(cfg)
|
|
77
|
+
if not name:
|
|
78
|
+
for n in names:
|
|
79
|
+
print(n)
|
|
80
|
+
return 0
|
|
81
|
+
if name not in names:
|
|
82
|
+
print(f"[hypdmz] no bundled example '{name}' (have: {', '.join(names)})")
|
|
83
|
+
return 2
|
|
84
|
+
target = (dest or Path(name)).resolve()
|
|
85
|
+
if target.exists():
|
|
86
|
+
print(f"[hypdmz] {target} exists")
|
|
87
|
+
return 2
|
|
88
|
+
shutil.copytree(cfg.examples_dir / name, target)
|
|
89
|
+
print(f"[hypdmz] copied {name} → {target} (edit task.toml and seats.toml, then: hypdmz run {target})")
|
|
90
|
+
return 0
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def main(argv: list[str] | None = None) -> int:
|
|
94
|
+
if hasattr(sys.stdout, "reconfigure"):
|
|
95
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace") # Windows consoles default to cp1252
|
|
96
|
+
p = argparse.ArgumentParser(prog="hypdmz", description="Run a HypSwarm research swarm inside a governed-egress pod.")
|
|
97
|
+
p.add_argument("--version", action="version", version=f"hypdmz {__version__}")
|
|
98
|
+
sub = p.add_subparsers(dest="cmd", required=True)
|
|
99
|
+
|
|
100
|
+
sub.add_parser("init", help="create the state dir and .env; print what comes next")
|
|
101
|
+
s = sub.add_parser("doctor", help="preflight checks"); s.add_argument("--json", action="store_true")
|
|
102
|
+
s = sub.add_parser("build", help="build the three images from the package's contexts"); s.add_argument("--dry-run", action="store_true")
|
|
103
|
+
s = sub.add_parser("up", help="create the pod, start the containers, verify the egress floor")
|
|
104
|
+
s.add_argument("--dry-run", action="store_true"); s.add_argument("--skip-egress-check", action="store_true")
|
|
105
|
+
sub.add_parser("down", help="stop and remove the pod")
|
|
106
|
+
s = sub.add_parser("status", help="pod, lockdown and endpoint snapshot"); s.add_argument("--json", action="store_true")
|
|
107
|
+
s = sub.add_parser("logs", help="tail the containers"); s.add_argument("--no-follow", action="store_true")
|
|
108
|
+
sub.add_parser("debug", help="pod, lockdown state and iptables view")
|
|
109
|
+
s = sub.add_parser("egress-check", help="escape attempts as the swarm user; exit 1 on any bypass")
|
|
110
|
+
s.add_argument("--json", action="store_true")
|
|
111
|
+
s = sub.add_parser("run", help="run a swarm: a folder with task.toml (and seats.toml or --profile), or a bundled example's name")
|
|
112
|
+
s.add_argument("folder", type=Path)
|
|
113
|
+
s.add_argument("--task", default="task.toml"); s.add_argument("--seats", default="")
|
|
114
|
+
s.add_argument("--profile", default="", help="scientific | software | trading | knowledge | optimization")
|
|
115
|
+
s.add_argument("--model", default="", help="default model id (else MODEL from .env)")
|
|
116
|
+
s.add_argument("--budget-usd", type=float, default=0.0); s.add_argument("--budget-calls", type=int, default=150)
|
|
117
|
+
s.add_argument("--pricing", default="", help="input,cached,output $ per million tokens")
|
|
118
|
+
s.add_argument("--python", action="store_true", help="give the seats a python tool in the work folder (in the pod)")
|
|
119
|
+
s.add_argument("--record", default="", help="record file name under records/ (default: the served swarm.hypgraph)")
|
|
120
|
+
s.add_argument("--name", default="", help="name for this run's work folder")
|
|
121
|
+
s = sub.add_parser("policy", help="the egress allowlist: show, path, reset")
|
|
122
|
+
s.add_argument("action", nargs="?", default="show", choices=["show", "path", "reset"])
|
|
123
|
+
s = sub.add_parser("example", help="list the bundled examples, or copy one out to edit")
|
|
124
|
+
s.add_argument("name", nargs="?", default=""); s.add_argument("dest", nargs="?", type=Path, default=None)
|
|
125
|
+
|
|
126
|
+
a = p.parse_args(argv)
|
|
127
|
+
cfg = load()
|
|
128
|
+
try:
|
|
129
|
+
if a.cmd == "init":
|
|
130
|
+
return cmd_init(cfg)
|
|
131
|
+
if a.cmd == "policy":
|
|
132
|
+
return cmd_policy(cfg, a.action)
|
|
133
|
+
if a.cmd == "example":
|
|
134
|
+
return cmd_examples(cfg, a.name, a.dest)
|
|
135
|
+
platform = detect()
|
|
136
|
+
if a.cmd == "doctor":
|
|
137
|
+
return cmd_doctor(cfg, platform, json_output=a.json)
|
|
138
|
+
if a.cmd == "build":
|
|
139
|
+
return cmd_build(cfg, platform, dry_run=a.dry_run)
|
|
140
|
+
if a.cmd == "up":
|
|
141
|
+
return cmd_up(cfg, platform, dry_run=a.dry_run, skip_egress_check=a.skip_egress_check)
|
|
142
|
+
if a.cmd == "down":
|
|
143
|
+
return cmd_down(cfg, platform)
|
|
144
|
+
if a.cmd == "status":
|
|
145
|
+
return cmd_status(cfg, platform, json_output=a.json)
|
|
146
|
+
if a.cmd == "logs":
|
|
147
|
+
return cmd_logs(cfg, platform, follow=not a.no_follow)
|
|
148
|
+
if a.cmd == "debug":
|
|
149
|
+
return cmd_debug(cfg, platform)
|
|
150
|
+
if a.cmd == "egress-check":
|
|
151
|
+
return cmd_egress_check(cfg, platform, json_output=a.json)
|
|
152
|
+
if a.cmd == "run":
|
|
153
|
+
return cmd_run(cfg, platform, a.folder, task=a.task, seats=a.seats, profile=a.profile, model=a.model,
|
|
154
|
+
budget_usd=a.budget_usd, budget_calls=a.budget_calls, pricing=a.pricing,
|
|
155
|
+
python=a.python, record=a.record, name=a.name)
|
|
156
|
+
except KeyboardInterrupt:
|
|
157
|
+
return 130
|
|
158
|
+
except RuntimeError as e:
|
|
159
|
+
print(f"[hypdmz] {e}", file=sys.stderr)
|
|
160
|
+
return 1
|
|
161
|
+
return 2
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
if __name__ == "__main__":
|
|
165
|
+
sys.exit(main())
|
hypdmz/doctor.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""`hypdmz doctor` — preflight checks before the first `up`."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
from dataclasses import asdict
|
|
6
|
+
|
|
7
|
+
from hypdmz.env import Config, sync_folder_marker
|
|
8
|
+
from hypdmz.platforms.base import Platform, PreflightResult
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def cmd_doctor(cfg: Config, platform: Platform, *, json_output: bool = False) -> int:
|
|
12
|
+
app_results = [_check_env_file(cfg), _check_state_dir(cfg), _check_model_key(cfg), _check_package(cfg), _check_policy(cfg)]
|
|
13
|
+
plat_results = platform.preflight()
|
|
14
|
+
fail = sum(1 for r in app_results + plat_results if not r.ok)
|
|
15
|
+
if json_output:
|
|
16
|
+
print(json.dumps({"platform": platform.name, "ok": fail == 0, "fail_count": fail,
|
|
17
|
+
"application": [asdict(r) for r in app_results],
|
|
18
|
+
"platform_checks": [asdict(r) for r in plat_results]}, indent=2))
|
|
19
|
+
return 0 if fail == 0 else 1
|
|
20
|
+
print(f"[hypdmz] doctor — platform={platform.name}\n")
|
|
21
|
+
for r in app_results:
|
|
22
|
+
print(r.render())
|
|
23
|
+
print("\n── platform checks ──")
|
|
24
|
+
for r in plat_results:
|
|
25
|
+
print(r.render())
|
|
26
|
+
print()
|
|
27
|
+
print("[hypdmz] all checks passed." if fail == 0 else f"[hypdmz] {fail} check(s) failed.")
|
|
28
|
+
return 0 if fail == 0 else 1
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _check_env_file(cfg: Config) -> PreflightResult:
|
|
32
|
+
if cfg.env_file.exists():
|
|
33
|
+
return PreflightResult(".env present", True, str(cfg.env_file))
|
|
34
|
+
return PreflightResult(".env present", False, f"run `hypdmz init` (writes {cfg.env_file} from the package template)")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _check_state_dir(cfg: Config) -> PreflightResult:
|
|
38
|
+
marker = sync_folder_marker(cfg.state_dir)
|
|
39
|
+
if marker:
|
|
40
|
+
return PreflightResult("state dir is local", False,
|
|
41
|
+
f"{cfg.state_dir} is under a cloud-synced folder ({marker}); set HYPDMZ_STATE_DIR")
|
|
42
|
+
return PreflightResult("state dir is local", True, str(cfg.state_dir))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _check_model_key(cfg: Config) -> PreflightResult:
|
|
46
|
+
from hypdmz.runner import secret_exists
|
|
47
|
+
|
|
48
|
+
vault = cfg.proxy_data_dir / "vault.db"
|
|
49
|
+
if secret_exists(cfg.key_secret):
|
|
50
|
+
return PreflightResult("model API key", True, f"podman secret '{cfg.key_secret}' (mounted into the proxy only)")
|
|
51
|
+
if cfg.model_api_key:
|
|
52
|
+
return PreflightResult("model API key", True, "MODEL_API_KEY in .env; vaulted on the next `up` — then delete the "
|
|
53
|
+
f"line (better: podman secret create {cfg.key_secret} -)")
|
|
54
|
+
if vault.exists():
|
|
55
|
+
return PreflightResult("model API key", True, "no secret, not in .env; the vault exists (hypdmz status shows the count)")
|
|
56
|
+
return PreflightResult("model API key", False,
|
|
57
|
+
f"no key: `podman secret create {cfg.key_secret} -` (paste it), or MODEL_API_KEY in .env")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _check_package(cfg: Config) -> PreflightResult:
|
|
61
|
+
missing = [p for p in ("controller/Dockerfile", "proxy/Dockerfile", "swarm/Dockerfile", "proxy/config/firewall.json")
|
|
62
|
+
if not (cfg.images_dir / p).exists()]
|
|
63
|
+
if missing:
|
|
64
|
+
return PreflightResult("bundled images", False, f"missing under {cfg.images_dir}: {', '.join(missing)} (broken install?)")
|
|
65
|
+
return PreflightResult("bundled images", True, f"{cfg.images_dir} (controller, proxy, swarm)")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _check_policy(cfg: Config) -> PreflightResult:
|
|
69
|
+
src = cfg.policy_path if cfg.policy_path.exists() else cfg.default_policy
|
|
70
|
+
try:
|
|
71
|
+
pol = json.loads(src.read_text(encoding="utf-8"))
|
|
72
|
+
n = len(pol.get("rules") or [])
|
|
73
|
+
except Exception as e:
|
|
74
|
+
return PreflightResult("egress policy", False, f"{src} does not parse ({e}); the proxy denies everything until it does")
|
|
75
|
+
where = "operator policy" if src == cfg.policy_path else "package default (seeded on the first `up`)"
|
|
76
|
+
return PreflightResult("egress policy", True, f"{src}: {n} rule(s), {where}")
|
hypdmz/egress.py
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"""`hypdmz egress-check` — prove the egress floor holds, from the swarm's seat.
|
|
2
|
+
|
|
3
|
+
Runs a fixed battery of escape attempts *as the swarm user inside the swarm container*
|
|
4
|
+
and asserts each one is blocked, plus a few positive controls so the check can't pass
|
|
5
|
+
because networking is simply dead.
|
|
6
|
+
|
|
7
|
+
blocked — must fail. A success here means the swarm can bypass the proxy.
|
|
8
|
+
control — must succeed. A failure means the pod is broken (or the host is offline).
|
|
9
|
+
|
|
10
|
+
Exit codes: 0 all probes passed · 1 at least one failed · 2 pod not running.
|
|
11
|
+
`hypdmz up` runs this automatically and stops the swarm container on a bypass.
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
import time
|
|
17
|
+
from dataclasses import asdict, dataclass
|
|
18
|
+
from typing import Optional
|
|
19
|
+
|
|
20
|
+
from hypdmz.env import Config
|
|
21
|
+
from hypdmz.platforms.base import run_capture
|
|
22
|
+
|
|
23
|
+
SWARM_CONTAINER = "hypdmz-swarm"
|
|
24
|
+
CONTROLLER_CONTAINER = "hypdmz-controller"
|
|
25
|
+
SWARM_USER = "swarm"
|
|
26
|
+
|
|
27
|
+
_CURL = "curl -s -o /dev/null --max-time 5 -w '%{http_code}'"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class Probe:
|
|
32
|
+
name: str
|
|
33
|
+
kind: str # "blocked" | "control"
|
|
34
|
+
command: str # sh snippet; prints an HTTP code or ok/fail
|
|
35
|
+
description: str
|
|
36
|
+
# For "blocked" probes against a server that legitimately answers (the proxy): an HTTP
|
|
37
|
+
# error code counts as blocked; only a 2xx/3xx is a breach.
|
|
38
|
+
http_error_is_blocked: bool = False
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def probes(cfg: Config) -> tuple[Probe, ...]:
|
|
42
|
+
return (
|
|
43
|
+
Probe("direct-https-by-name", "blocked", f"{_CURL} --noproxy '*' https://example.com",
|
|
44
|
+
"HTTPS to a public hostname, ignoring HTTPS_PROXY"),
|
|
45
|
+
Probe("direct-https-by-ip", "blocked", f"{_CURL} --noproxy '*' -k https://1.1.1.1",
|
|
46
|
+
"HTTPS straight to an IP (no DNS needed)"),
|
|
47
|
+
Probe("direct-http-by-ip", "blocked", f"{_CURL} --noproxy '*' http://1.1.1.1",
|
|
48
|
+
"Plain HTTP straight to an IP"),
|
|
49
|
+
Probe("direct-ipv6", "blocked", f"{_CURL} -6 --noproxy '*' -k 'https://[2606:4700:4700::1111]'",
|
|
50
|
+
"HTTPS over IPv6"),
|
|
51
|
+
Probe("direct-dns-public", "blocked", "python /usr/local/lib/hypdmz/probe_dns.py 1.1.1.1",
|
|
52
|
+
"DNS query to a public resolver (an exfiltration channel)"),
|
|
53
|
+
Probe("pod-resolver-dns", "blocked",
|
|
54
|
+
"getent hosts example.com >/dev/null 2>&1 && echo ok || echo fail",
|
|
55
|
+
"DNS via the pod's configured resolver"),
|
|
56
|
+
Probe("proxy-control-api", "blocked", f"{_CURL} --noproxy '*' http://127.0.0.1:8002/healthz",
|
|
57
|
+
"The proxy's control API on :8002 (policy, vault, audit)"),
|
|
58
|
+
Probe("proxy-denies-unlisted", "blocked", f"{_CURL} -x http://127.0.0.1:8080 https://example.com",
|
|
59
|
+
"A host not in firewall.json, through the proxy", http_error_is_blocked=True),
|
|
60
|
+
Probe("proxy-relay-to-console", "blocked",
|
|
61
|
+
f"NO_PROXY= no_proxy= {_CURL} --noproxy '' -x http://127.0.0.1:8080 http://127.0.0.1:8002/healthz",
|
|
62
|
+
"Reach the control API by relaying through the proxy", http_error_is_blocked=True),
|
|
63
|
+
Probe("ca-private-key", "blocked",
|
|
64
|
+
"[ -r /etc/hypdmz-certs/hypdmz-ca.pem ] && echo ok || echo fail",
|
|
65
|
+
"Read the egress CA's private key (only the proxy holds it)"),
|
|
66
|
+
Probe("swarm-code-write", "blocked",
|
|
67
|
+
"d=\"$(python -c 'import hypswarm,os;print(os.path.dirname(hypswarm.__file__))')\"; "
|
|
68
|
+
"[ -w \"$d/run.py\" ] || [ -w \"$d\" ] || [ -w /entrypoint.sh ] && echo ok || echo fail",
|
|
69
|
+
"Write its own code (hypswarm, the entrypoint)"),
|
|
70
|
+
# ── the blast radius: what the swarm's process is allowed to consume and change ──
|
|
71
|
+
Probe("swarm-rootfs-readonly", "control",
|
|
72
|
+
"awk '$2 == \"/\" && $4 ~ /(^|,)ro(,|$)/ { f = 1 } END { exit !f }' /proc/mounts && echo ok || echo fail",
|
|
73
|
+
"The container's root filesystem is mounted read-only"),
|
|
74
|
+
Probe("swarm-memory-capped", "control",
|
|
75
|
+
"v=$(cat /sys/fs/cgroup/memory.max 2>/dev/null); [ -n \"$v\" ] && [ \"$v\" != max ] && echo ok || echo fail",
|
|
76
|
+
"A memory limit is set on the container's cgroup"),
|
|
77
|
+
Probe("swarm-pids-capped", "control",
|
|
78
|
+
"v=$(cat /sys/fs/cgroup/pids.max 2>/dev/null); [ -n \"$v\" ] && [ \"$v\" != max ] && echo ok || echo fail",
|
|
79
|
+
"A process-count limit is set on the container's cgroup"),
|
|
80
|
+
Probe("swarm-cpu-capped", "control",
|
|
81
|
+
"v=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max 2>/dev/null); [ -n \"$v\" ] && [ \"$v\" != max ] && echo ok || echo fail",
|
|
82
|
+
"A CPU quota is set on the container's cgroup"),
|
|
83
|
+
Probe("lockdown-gate", "control",
|
|
84
|
+
"curl -s --max-time 2 telnet://127.0.0.1:8009 </dev/null 2>/dev/null | grep -q '^LOCKED' && echo ok || echo fail",
|
|
85
|
+
"The controller reports LOCKED on the gate"),
|
|
86
|
+
Probe("record-ui", "control", f"{_CURL} --noproxy '*' http://127.0.0.1:8000/",
|
|
87
|
+
"The record UI answers on :8000"),
|
|
88
|
+
Probe("proxy-listener", "control", f"{_CURL} --noproxy '*' http://127.0.0.1:8080/",
|
|
89
|
+
"The proxy answers on :8080"),
|
|
90
|
+
Probe("proxy-upstream", "control", f"{_CURL} -x http://127.0.0.1:8080 {cfg.model_base_url}/models",
|
|
91
|
+
f"The model host through the proxy ({cfg.model_host}; the proxy UID has egress)"),
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@dataclass
|
|
96
|
+
class ProbeResult:
|
|
97
|
+
name: str
|
|
98
|
+
kind: str
|
|
99
|
+
passed: bool
|
|
100
|
+
observed: str
|
|
101
|
+
description: str
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclass
|
|
105
|
+
class EgressReport:
|
|
106
|
+
lockdown_state: str
|
|
107
|
+
results: list[ProbeResult]
|
|
108
|
+
error: Optional[str] = None
|
|
109
|
+
caps: Optional[dict] = None # the proxy's hourly caps and the last hour's use
|
|
110
|
+
|
|
111
|
+
@property
|
|
112
|
+
def ok(self) -> bool:
|
|
113
|
+
return self.error is None and self.lockdown_state.startswith("LOCKED") and all(r.passed for r in self.results)
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def bypass_found(self) -> bool:
|
|
117
|
+
return any(not r.passed for r in self.results if r.kind == "blocked")
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def lockdown_state() -> str:
|
|
121
|
+
rc, out, _ = run_capture(["podman", "exec", CONTROLLER_CONTAINER, "cat", "/run/hypdmz/lockdown.state"])
|
|
122
|
+
if rc != 0:
|
|
123
|
+
return "UNKNOWN (controller not running or lockdown never applied)"
|
|
124
|
+
return out.strip() or "UNKNOWN (empty state file)"
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _wait_for_ui(*, deadline_s: int) -> None:
|
|
128
|
+
deadline = time.monotonic() + deadline_s
|
|
129
|
+
while time.monotonic() < deadline:
|
|
130
|
+
rc, out, _ = run_capture(["podman", "exec", "-u", SWARM_USER, SWARM_CONTAINER, "sh", "-c",
|
|
131
|
+
f"{_CURL} --noproxy '*' http://127.0.0.1:8000/"])
|
|
132
|
+
if out.strip() not in ("", "000"):
|
|
133
|
+
return
|
|
134
|
+
time.sleep(1)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _succeeded(observed: str) -> bool:
|
|
138
|
+
if observed == "ok":
|
|
139
|
+
return True
|
|
140
|
+
if observed.isdigit():
|
|
141
|
+
return observed != "000"
|
|
142
|
+
return False
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _passed(probe: Probe, observed: str) -> bool:
|
|
146
|
+
if observed == "missing": # a probe that never reported proved nothing
|
|
147
|
+
return False
|
|
148
|
+
if probe.kind == "control":
|
|
149
|
+
return _succeeded(observed)
|
|
150
|
+
if probe.http_error_is_blocked:
|
|
151
|
+
return not (observed.isdigit() and 200 <= int(observed) < 400)
|
|
152
|
+
return not _succeeded(observed)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def run_check(cfg: Config, *, wait_for_lock_s: int = 20) -> EgressReport:
|
|
156
|
+
state = lockdown_state()
|
|
157
|
+
deadline = time.monotonic() + wait_for_lock_s
|
|
158
|
+
while not state.startswith("LOCKED") and time.monotonic() < deadline:
|
|
159
|
+
time.sleep(1)
|
|
160
|
+
state = lockdown_state()
|
|
161
|
+
_wait_for_ui(deadline_s=wait_for_lock_s + 30)
|
|
162
|
+
|
|
163
|
+
ps = probes(cfg)
|
|
164
|
+
script = "\n".join(f"printf '%s|%s\\n' {p.name} \"$({p.command} 2>/dev/null || true)\"" for p in ps)
|
|
165
|
+
rc, out, err = run_capture(["podman", "exec", "-u", SWARM_USER, SWARM_CONTAINER, "sh", "-c", script])
|
|
166
|
+
if rc != 0 and not out.strip():
|
|
167
|
+
return EgressReport(state, [], error=(err.strip() or f"podman exec exit {rc}"))
|
|
168
|
+
observed: dict[str, str] = {}
|
|
169
|
+
for line in out.splitlines():
|
|
170
|
+
name, _, value = line.partition("|")
|
|
171
|
+
observed[name.strip()] = value.strip() or "000"
|
|
172
|
+
results = [ProbeResult(p.name, p.kind, _passed(p, observed.get(p.name, "missing")),
|
|
173
|
+
observed.get(p.name, "missing"), p.description) for p in ps]
|
|
174
|
+
results += console_checks(cfg)
|
|
175
|
+
return EgressReport(state, results, caps=console_caps(cfg))
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _console_get(cfg: Config, path: str, token: str | None) -> int:
|
|
179
|
+
"""HTTP status of GET <console>/path from the host, with or without the bearer token (0 = unreachable)."""
|
|
180
|
+
import urllib.error
|
|
181
|
+
import urllib.request
|
|
182
|
+
req = urllib.request.Request(f"http://127.0.0.1:{cfg.console_port}{path}")
|
|
183
|
+
if token:
|
|
184
|
+
req.add_header("Authorization", f"Bearer {token}")
|
|
185
|
+
try:
|
|
186
|
+
with urllib.request.urlopen(req, timeout=5) as r:
|
|
187
|
+
return r.status
|
|
188
|
+
except urllib.error.HTTPError as e:
|
|
189
|
+
return e.code
|
|
190
|
+
except Exception:
|
|
191
|
+
return 0
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def console_checks(cfg: Config) -> list[ProbeResult]:
|
|
195
|
+
"""Run from the host: the control API must refuse a request without the token and answer one with it."""
|
|
196
|
+
tok = cfg.console_token()
|
|
197
|
+
without = _console_get(cfg, "/api/status", None)
|
|
198
|
+
with_tok = _console_get(cfg, "/api/status", tok) if tok else 0
|
|
199
|
+
return [
|
|
200
|
+
ProbeResult("console-requires-token", "blocked", without == 401, str(without),
|
|
201
|
+
"Control API without the bearer token (from the host)"),
|
|
202
|
+
ProbeResult("console-with-token", "control", with_tok == 200, str(with_tok),
|
|
203
|
+
f"Control API with the token from {cfg.console_token_path.name}"),
|
|
204
|
+
]
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def console_caps(cfg: Config) -> dict:
|
|
208
|
+
"""The proxy's hourly caps and the last hour's use, for the report header."""
|
|
209
|
+
import urllib.request
|
|
210
|
+
tok = cfg.console_token()
|
|
211
|
+
if not tok:
|
|
212
|
+
return {}
|
|
213
|
+
req = urllib.request.Request(f"http://127.0.0.1:{cfg.console_port}/api/status",
|
|
214
|
+
headers={"Authorization": f"Bearer {tok}"})
|
|
215
|
+
try:
|
|
216
|
+
with urllib.request.urlopen(req, timeout=5) as r:
|
|
217
|
+
return json.load(r).get("limits") or {}
|
|
218
|
+
except Exception:
|
|
219
|
+
return {}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def report_dict(report: EgressReport) -> dict:
|
|
223
|
+
return {"ok": report.ok, "bypass_found": report.bypass_found, "lockdown_state": report.lockdown_state,
|
|
224
|
+
"caps": report.caps or {}, "error": report.error, "results": [asdict(r) for r in report.results]}
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def print_report(report: EgressReport) -> None:
|
|
228
|
+
print(f"── lockdown ──\n {report.lockdown_state}")
|
|
229
|
+
if report.caps:
|
|
230
|
+
used = report.caps.get("last_hour") or {}
|
|
231
|
+
print(f" caps: {report.caps.get('calls_per_hour') or '∞'} calls/h · "
|
|
232
|
+
f"{report.caps.get('megabytes_per_hour') or '∞'} MB/h · last hour: {used.get('calls', 0)} calls, "
|
|
233
|
+
f"{used.get('megabytes', 0)} MB")
|
|
234
|
+
print()
|
|
235
|
+
if report.error:
|
|
236
|
+
print(f"[ERROR] could not run probes: {report.error}")
|
|
237
|
+
return
|
|
238
|
+
print("── probes (run as the swarm user; the two console probes from the host) ──")
|
|
239
|
+
for r in report.results:
|
|
240
|
+
glyph = "PASS" if r.passed else "FAIL"
|
|
241
|
+
expect = "blocked" if r.kind == "blocked" else "reachable"
|
|
242
|
+
print(f" [{glyph}] {r.name:24s} expect {expect:9s} got {r.observed:5s} {r.description}")
|
|
243
|
+
print()
|
|
244
|
+
if report.ok:
|
|
245
|
+
print("[hypdmz] egress floor verified: the swarm cannot bypass the proxy.")
|
|
246
|
+
elif report.bypass_found:
|
|
247
|
+
print("[hypdmz] EGRESS BYPASS: the swarm can reach the network without the proxy.")
|
|
248
|
+
else:
|
|
249
|
+
print("[hypdmz] the lockdown holds, but a control probe failed — the pod is degraded (or the host is offline).")
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def cmd_egress_check(cfg: Config, platform, *, json_output: bool = False) -> int:
|
|
253
|
+
rc, _, _ = run_capture(["podman", "container", "exists", SWARM_CONTAINER])
|
|
254
|
+
if rc != 0:
|
|
255
|
+
msg = "the swarm container is not running — start the pod with `hypdmz up`."
|
|
256
|
+
print(json.dumps({"ok": False, "error": msg}) if json_output else f"[ERROR] {msg}")
|
|
257
|
+
return 2
|
|
258
|
+
report = run_check(cfg)
|
|
259
|
+
if json_output:
|
|
260
|
+
print(json.dumps(report_dict(report), indent=2))
|
|
261
|
+
else:
|
|
262
|
+
print_report(report)
|
|
263
|
+
return 0 if report.ok else 1
|
hypdmz/env.example
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# HypDMZ — copy to <state dir>/.env (Windows: %USERPROFILE%\.hypdmz\.env; `hypdmz doctor` prints the path).
|
|
2
|
+
# Nothing here is read from the repo.
|
|
3
|
+
|
|
4
|
+
# The model endpoint the swarm talks to (OpenAI-compatible) and the default model id.
|
|
5
|
+
MODEL_BASE_URL=https://api.deepinfra.com/v1/openai
|
|
6
|
+
MODEL=deepseek-ai/DeepSeek-V4.1-Flash
|
|
7
|
+
|
|
8
|
+
# The API key. Preferred: a podman secret, which reaches the proxy container alone, as a file, never in any
|
|
9
|
+
# environment or `podman inspect`:
|
|
10
|
+
# podman secret create hypdmz_llm_api_key - (then paste the key, Enter, Ctrl-D / Ctrl-Z)
|
|
11
|
+
# Fallback: set it here; on the next `hypdmz up` the proxy stores it in its vault (AES-256-GCM, master key in
|
|
12
|
+
# proxy-data/) — then delete the line. Either way the swarm only ever sees a placeholder.
|
|
13
|
+
#HYPDMZ_KEY_SECRET=hypdmz_llm_api_key
|
|
14
|
+
MODEL_API_KEY=
|
|
15
|
+
|
|
16
|
+
# Host ports (loopback only). Change these if something else holds them.
|
|
17
|
+
HYPDMZ_UI_PORT=8000
|
|
18
|
+
HYPDMZ_CONSOLE_PORT=8002
|
|
19
|
+
HYPDMZ_PROXY_PORT=8080
|
|
20
|
+
|
|
21
|
+
# Where swarm records land (default <state dir>/records).
|
|
22
|
+
#HYPDMZ_RECORDS_DIR=
|
|
23
|
+
|
|
24
|
+
# The swarm container's blast radius (podman flags; needs cgroups v2, which Podman Desktop and modern Linux have).
|
|
25
|
+
# The root filesystem is always read-only; /tmp and /home/swarm are size-capped tmpfs; /records and /work persist.
|
|
26
|
+
#HYPDMZ_SWARM_MEMORY=4g
|
|
27
|
+
#HYPDMZ_SWARM_CPUS=2
|
|
28
|
+
#HYPDMZ_SWARM_PIDS=512
|
|
29
|
+
#HYPDMZ_SWARM_TMP=2g
|
|
30
|
+
|
|
31
|
+
# Hourly caps on what leaves (calls, request megabytes) live in proxy/config/firewall.json under "limits".
|
|
32
|
+
# The control API's bearer token is generated into <state dir>/tokens/console.token on the first `up`.
|
|
33
|
+
|
|
34
|
+
# Hardening knobs (defaults are the safe ones).
|
|
35
|
+
#DLP_MODE=block # block | log — secrets found in an outbound body/header
|
|
36
|
+
#AUDIT_SINK_URL= # every audit event is also POSTed here as JSON
|
|
37
|
+
#AUDIT_SINK_TOKEN=
|