Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4a1a84941c | |||
| c8d3c17b13 | |||
| f5c3d77a61 | |||
| 5e05627c4e | |||
| 1e087bb158 | |||
| 29bcf4403e | |||
| 08ca67861f |
@@ -50,6 +50,12 @@ it **first** in the morning note. A decision you cannot write in that shape is o
|
|||||||
on Tester 1, demo-hp and demo-felhom through the normal update path and the box walk's checks are green
|
on Tester 1, demo-hp and demo-felhom through the normal update path and the box walk's checks are green
|
||||||
on all three — not at night unless the night brief says so. **Agent releases still stop for the
|
on all three — not at night unless the night brief says so. **Agent releases still stop for the
|
||||||
operator's vouch.** The golden is baked weekly and vouched by the operator; CC never vouches. **No `--no-verify`.**
|
operator's vouch.** The golden is baked weekly and vouched by the operator; CC never vouches. **No `--no-verify`.**
|
||||||
|
**A version number is pushed once** — `felhom-controller/controller/build.sh` now refuses a second
|
||||||
|
push of a tag that exists, and a registry it cannot reach is a refusal too. This is a FACT about
|
||||||
|
what the build does, not a new rule: a fix for something already pushed is the next patch version,
|
||||||
|
and a box on a bad image is moved by the normal update path, never by re-tagging. Earned on
|
||||||
|
2026-10-15, when 0.315.0 was pushed three times from three different commits and a cache kept
|
||||||
|
serving the first one (`09-update-architecture.md` §0).
|
||||||
7. **An enumerated gap becomes a row in the same session — or, if it is small, is fixed in it** (the size rule).
|
7. **An enumerated gap becomes a row in the same session — or, if it is small, is fixed in it** (the size rule).
|
||||||
Prose is not a record.
|
Prose is not a record.
|
||||||
8. **Hungarian text is searched with ASCII fragments**, with a positive and a negative control.
|
8. **Hungarian text is searched with ASCII fragments**, with a positive and a negative control.
|
||||||
|
|||||||
+56
-1
@@ -1,4 +1,59 @@
|
|||||||
## Unreleased (next: v0.164.0; 2026-10-24 attended release run) — the weekly upload line test (R-1247)
|
## Unreleased (next: v0.165.0) — D43 = A: BYO hosts keep their own SSH settings (R-1257) (2026-10-24 night) — committed, NOT released (the release + the operator's vouch are Monday's)
|
||||||
|
|
||||||
|
- **The root-key-only sshd drop-in (`/etc/ssh/sshd_config.d/10-felhom-root-key-only.conf`, R-982 / D4) goes only to an
|
||||||
|
appliance** — operator ruling D43 = A (2026-10-24): a BYO host belongs to its owner (`11` §1). The table entry's policy
|
||||||
|
is now `appliance`: written only when the ROOT-OWNED install record (`/var/lib/felhom-install/state.json`) says
|
||||||
|
`appliance`. On a `byo` record nothing is written, and a copy WE wrote (this exact path, beginning with our header
|
||||||
|
line) is REMOVED, its previous copy kept for the undo, then `sshd -t` and a reload. A box whose record is absent or
|
||||||
|
agent-writable proves nothing and is left exactly as it is. The bundle report lists `removed`; the record says
|
||||||
|
`removed` / `skipped`. The installer records the mode before it installs the bundle, so a new appliance still gets it.
|
||||||
|
- **X3: the „in force" check asks sshd about each of the host's own LAN addresses** (`ip -4 -o addr show scope
|
||||||
|
global`), not only 192.0.2.1 — a `Match Address <LAN>` block that re-allows root passwords no longer reads as in force.
|
||||||
|
- **X4: a failing `sshd -t` puts back ONLY the SSH step** (`undo_sshd_step`: the previous copy, or none; sshd is not
|
||||||
|
reloaded, so it never ran the refused state) and the rest of the bundle stays; it used to refuse and undo the whole
|
||||||
|
bundle. The report carries `sshd_step_undone`; the record names what is on disk.
|
||||||
|
- Tests (`configs/test_felhom_config_bundle.py`, class `ByoKeepsItsSSH`, 7 tests; each rule seen failing with it taken
|
||||||
|
out): a BYO host gets no drop-in; our copy comes off a BYO host; the owner's own file stays; no record → left as it
|
||||||
|
is; a LAN `Match` block is not in force; every address key-only passes; a failing `sshd -t` undoes only the SSH step.
|
||||||
|
The fake box now carries an appliance record by default (every earlier test unchanged).
|
||||||
|
|
||||||
|
## 2026-10-24 (review round 2, unattended) — `.claude/rules/unprompted-work.md` aligned with its four siblings
|
||||||
|
|
||||||
|
- The copy lacked item 6's paragraph „A version number is pushed once …" that the felhom.eu and felhom-controller copies
|
||||||
|
have carried since 2026-10-15; the file's header says „change all five or none". The paragraph states a fact
|
||||||
|
(`felhom-controller/controller/build.sh` refuses an existing tag and an unreachable registry). Now byte-identical to
|
||||||
|
the other four (md5 b1891bd4). No code change. Record: felhom.eu `documentation/audits/review-round-2-2026-10-24/EDITS.md`.
|
||||||
|
|
||||||
|
## Unreleased (next: v0.165.0) — the line test for a box that seeds all day (R-1250) (2026-10-24) — committed, not released
|
||||||
|
|
||||||
|
Attended brief 2026-10-24, Part 4 (commit only; the release rides the next normal train). The weekly test (v0.164.0)
|
||||||
|
starts only after 10 quiet minutes; a torrent app that seeds all day never gives them, so that box never got a tested
|
||||||
|
line and qBittorrent never got its 50 % limit. Now:
|
||||||
|
|
||||||
|
- **`linetest.Tester.WaitingForQuiet`** — due, inside the window, tries left, tunnel up, and only the quiet missing.
|
||||||
|
`GET /host/line-test` carries it as `waiting_for_quiet`; a controller with a seeding torrent app acts only on it.
|
||||||
|
It needs the full watched window (11 minute samples): a freshly started agent is not known to be busy, so it never
|
||||||
|
invites a hold (found in the live proof, 2026-10-24: the first build said `true` before one minute was watched;
|
||||||
|
`TestFreshTesterIsNotWaiting`).
|
||||||
|
- **`linetest.Tester.RunNow`** and **`POST /host/line-test/run`** (synchronous, about 55 s) — a test on request while the
|
||||||
|
controller holds its app's upload low: every weekly rule holds (due, window, `MaxTriesPerDay`, tunnel, the one heavy-op
|
||||||
|
gate) except the 10 quiet minutes, which become a 5 s settle and a 10 s quiet check (below 2 Mbit/s, so a request
|
||||||
|
never measures the family's own upload); the three runs are 5 s apart instead of 30. A refusal answers its code
|
||||||
|
(`not-due`, `outside-window`, `tries-used`, `no-tunnel`, `heavy-op`, `not-quiet`, `server-busy`); a failed request
|
||||||
|
spends one of the day's tries. `Tick` and `RunNow` now share one `ready()` check.
|
||||||
|
- Tests (each seen failing): `TestSeedingBoxIsWaitingForQuietAndRunNowMeasuresIt` (the sleeps, the 60 s bound),
|
||||||
|
`TestRunNowRefusesAnUplinkThatIsStillBusy`, `TestRunNowKeepsTheWeeklyRules`, `TestRunNowFailuresSpendTheDaysTries`,
|
||||||
|
`TestQuietBoxIsNotWaiting`, `TestLineTestRunRoute`; a compile-time assertion that the Tester serves the request half.
|
||||||
|
|
||||||
|
**No sudoers or config-bundle change. MinController: none** — an older controller never calls the route.
|
||||||
|
|
||||||
|
## v0.164.0 — the weekly upload line test (R-1247) (2026-10-24) — RELEASED on the operator's ruling (attended release run); vouched by the operator 09:44:58 CEST
|
||||||
|
|
||||||
|
Binary sha256 `1b803d8b70f8afbe1546f6989f35b6b0e29c9ed63f6acb3843d3c04ef0d24b2b`; config bundle sha256
|
||||||
|
`1d7b54d39d0c1aff682ba0c0a7f91aea0f9407a10d2f5a8bbfdc858d32c13341` (configs unchanged — no bundle job sent). Built from
|
||||||
|
`f7e1dcb` (CI 2277 green) in a fresh clone by `scripts/release-agent.sh`. Delivered by one signed `agent_update` per box
|
||||||
|
(07:25Z): Tester 1, demo-hp and demo-felhom — COMPLETED, self-check 76/76, committed after the 1-minute dwell, the line
|
||||||
|
test started (felhom.eu `audits/line-test-2026-10-24/agent-0164-boxes.txt`).
|
||||||
|
|
||||||
Operator ruling 2026-10-24 07:26: once a week the box's HOST measures its upload line with a 10-second iperf3 upload to an
|
Operator ruling 2026-10-24 07:26: once a week the box's HOST measures its upload line with a 10-second iperf3 upload to an
|
||||||
iperf3 server on ep0 that answers **only inside the WireGuard tunnel** (felhom.eu `scripts/ep0-iperf3/`; installed the same
|
iperf3 server on ep0 that answers **only inside the WireGuard tunnel** (felhom.eu `scripts/ep0-iperf3/`; installed the same
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
# CONTEXT — felhom-agent working state
|
# CONTEXT — felhom-agent working state
|
||||||
|
|
||||||
|
> **2026-10-24 night (unattended) — D43 = A on `main` (`c8d3c17`), NOT released (next v0.165.0, with R-1250).** The sshd
|
||||||
|
> drop-in's bundle policy is `appliance` (install record `/var/lib/felhom-install/state.json` mode); BYO: nothing written,
|
||||||
|
> our copy removed; no proof: untouched. X3: `lan_addresses()` for `sshd -T`. X4: `undo_sshd_step` — only the SSH file is
|
||||||
|
> put back. Release + the operator's vouch: Monday's list (felhom.eu STATUS).
|
||||||
|
|
||||||
> **2026-10-24 (attended release run) — v0.164.0: the weekly upload line test (R-1247; operator ruling 07:26).**
|
> **2026-10-24 (attended release run) — v0.164.0: the weekly upload line test (R-1247; operator ruling 07:26).**
|
||||||
> `internal/linetest`: the agent's own iperf3 client (operator's pick — nothing installed on hosts) to ep0's tunnel-only
|
> `internal/linetest`: the agent's own iperf3 client (operator's pick — nothing installed on hosts) to ep0's tunnel-only
|
||||||
> server `10.77.0.1:5201`, Tuesday 10:00-15:59, after 10 quiet minutes, under the heavy-op gate, best of 3; `line_test`
|
> server `10.77.0.1:5201`, Tuesday 10:00-15:59, after 10 quiet minutes, under the heavy-op gate, best of 3; `line_test`
|
||||||
|
|||||||
@@ -1,36 +1,16 @@
|
|||||||
# REPORT — day 2026-10-23: agent 0.163.0 released, delivered to the three test boxes, vouched by the operator
|
# REPORT — D43 = A: BYO hosts keep their own SSH settings (R-1257), 2026-10-24 night (unattended; commit only)
|
||||||
|
|
||||||
**What:** the day brief's Part 2. Design read: `03-host-agent.md` (the tunnel, the bundle), `11` §5.4.2 (the step
|
Brief: the restore-holes run, Part 4. Starting the run was the operator's ruling **D43 = A**. Previous report (R-1250's
|
||||||
bundle, R-880).
|
line hold, attended, same day): `git show f5c3d77:REPORT.md`.
|
||||||
|
|
||||||
## Release
|
- **`configs/felhom-os-apply` (commit `c8d3c17`):** the root-key-only sshd drop-in is an `appliance`-policy file —
|
||||||
|
written only when the root-owned install record says `appliance`; on `byo` nothing is written and a copy we wrote
|
||||||
- Code: `1109092` (D4 v2: the sshd drop-in `/etc/ssh/sshd_config.d/10-felhom-root-key-only.conf`,
|
(exact path, our header line) is removed (the previous copy kept for the undo, then `sshd -t` and a reload); a box
|
||||||
`PermitRootLogin prohibit-password`, `try-reload-or-restart`, an `sshd -T` read-back; RootKeyOnly 6 tests). CI 2246
|
whose record proves nothing is left as it is. **X3:** the in-force check asks `sshd -T` for each of the host's own
|
||||||
success. CHANGELOG `b4721ee` (v0.163.0 header, both sha256), CI 2249 success.
|
global IPv4 addresses. **X4:** a failing `sshd -t` puts back only the SSH file (`sshd_step_undone` in the report);
|
||||||
- `scripts/release-agent.sh 0.163.0`: binary sha256 `561b5316cd511de9fa9d8b8be4553da7add684f715d3bea533ca330ff7f3763c`,
|
the rest of the bundle stays.
|
||||||
config bundle sha256 `3afb88d9fbbc4adc16083fa52a98cd4974583c6378a77a1a6e0ed8ae6d1b80c8`; tag `v0.163.0`; verified by
|
- **Tests:** `configs/test_felhom_config_bundle.py` class `ByoKeepsItsSSH` (7), each rule seen failing with it taken out
|
||||||
an independent download.
|
(policy back to `replace`; the probe back to 192.0.2.1; `sshd -t` back to `must`). The fake box now carries an
|
||||||
- **Vouched by the operator** 07:58:46 UTC — read back from the hub DB (with its WAL) and from the running hub's
|
appliance record by default; all 68 bundle tests and 146 os-apply tests pass; `agent_gates.py --fast` OK.
|
||||||
`/configuration` page: agent 0.163.0, both sha256 as above.
|
- **Not done here (Monday):** the agent release v0.165.0 and the operator's vouch; a signed bundle on a BYO test host.
|
||||||
|
- Docs: felhom.eu `03` (the D4 paragraph) and `11` §1 carry the ruling; register R-1257 → VERIFY.
|
||||||
## Delivery — three signed jobs per box (felhom-opsign, key felhom-op-1)
|
|
||||||
|
|
||||||
The D4 drop-in is a NEW bundle path, and the boxes ran bundle 0.160.0 (sha256 `6b0609…`), so: `agent_update 0.163.0`
|
|
||||||
→ `agent_config_update 0.163.0-step1` (built with `scripts/build-step-bundle.py` from 0.160.0: only
|
|
||||||
`felhom-os-apply` replaced; sha256 `6cbc0669…`, uploaded and read back) → `agent_config_update 0.163.0`.
|
|
||||||
|
|
||||||
| Box | agent | bundle | self-check | swap = RAM (pool guest 9201) | drop-in, `sshd -T` | sshd reloaded | key login | felhom-sshd |
|
|
||||||
|---|---|---|---|---|---|---|---|---|
|
|
||||||
| Tester 1 | 0.163.0 | 0.163.0 | ok | 4096 / 4096 | in place, `without-password` | 09:27:38 SIGHUP | ok | active, listening, `sshd -t` ok |
|
|
||||||
| demo-hp | 0.163.0 | 0.163.0 | ok | 25898 / 25898 | in place, `without-password` | 09:19:44 SIGHUP | ok | active, listening, `sshd -t` ok |
|
|
||||||
| demo-felhom | 0.163.0 | 0.163.0 | ok | 12288 / 12288 | in place, `without-password` | 09:24:42 SIGHUP | ok | active, listening, `sshd -t` ok |
|
|
||||||
|
|
||||||
**Not proven, said plainly:** the real root password over SSH was not tried (CC does not hold it); a dummy password is
|
|
||||||
refused, but OpenSSH logs that only as „Failed password", so the policy proof is `sshd -T` + the reload. felhom-sshd
|
|
||||||
(:8822) admits only the operator peer over the tunnel, so a connection from DooPlex times out by design. The console
|
|
||||||
login is not touched by the drop-in (`passwd -S root` = P on all three); not tried at a physical console.
|
|
||||||
|
|
||||||
Installer 1.36.0 (the uninstall removes the drop-in) was published the same day (felhom.eu). R-982 and R-1204 closed.
|
|
||||||
Evidence: felhom.eu `documentation/audits/day-2026-10-23/07-agent-0163-boxes.txt`, `10-vouch-readback.txt`,
|
|
||||||
`11-installer-1360.txt`.
|
|
||||||
|
|||||||
@@ -5,8 +5,16 @@ import (
|
|||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gitea.dooplex.hu/admin/felhom-agent/internal/linetest"
|
||||||
|
"gitea.dooplex.hu/admin/felhom-agent/internal/localapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// R-1250: SetLineTest takes a LineTestReader and finds the request half by a type assertion, so a Tester that lost
|
||||||
|
// WaitingForQuiet or RunNow would compile and silently answer 503 to every seeding box. This line makes it a build
|
||||||
|
// failure instead.
|
||||||
|
var _ localapi.LineTestRequester = (*linetest.Tester)(nil)
|
||||||
|
|
||||||
// R-1247: the weekly line test has the fstrim shape (component + reporter seam + goroutine) plus a local-API reader,
|
// R-1247: the weekly line test has the fstrim shape (component + reporter seam + goroutine) plus a local-API reader,
|
||||||
// so its wiring is asserted from the AST. It must share the ONE heavy-op gate (heavyOps) — or it could run beside a
|
// so its wiring is asserted from the AST. It must share the ONE heavy-op gate (heavyOps) — or it could run beside a
|
||||||
// backup — and take its server from the tunnel manager, never a literal address.
|
// backup — and take its server from the tunnel manager, never a literal address.
|
||||||
|
|||||||
+103
-16
@@ -168,7 +168,10 @@ KERNEL_ACTIVE = ("staged", "oneshot", "judging", "reverting")
|
|||||||
# agent-unit → User=felhom-agent; dropin → a [Unit] section (SF-3); nft → nft -c -f; plain → none;
|
# agent-unit → User=felhom-agent; dropin → a [Unit] section (SF-3); nft → nft -c -f; plain → none;
|
||||||
# sshd → `sshd -t -f` on the snippet alone (R-982).
|
# sshd → `sshd -t -f` on the snippet alone (R-982).
|
||||||
# policy: replace → always written when it differs; if-absent → only when the box has none (a setting the operator may
|
# policy: replace → always written when it differs; if-absent → only when the box has none (a setting the operator may
|
||||||
# have tuned); oob → only on a box with the OOB belt (/etc/felhom-sshd exists — the installer's --no-oob leaves none).
|
# have tuned); oob → only on a box with the OOB belt (/etc/felhom-sshd exists — the installer's --no-oob leaves none);
|
||||||
|
# appliance → only on a box whose ROOT-OWNED install record says appliance (D43 = A, operator 2026-10-24: a BYO
|
||||||
|
# host keeps its own settings, `11` §1). On a BYO box nothing is written and a copy WE wrote (this exact path, our
|
||||||
|
# first line) is removed; a box with no readable record is left exactly as it is (it cannot prove which it is).
|
||||||
# Order matters: the sudoers files come LAST, so a referenced wrapper is in place before the line that allows it.
|
# Order matters: the sudoers files come LAST, so a referenced wrapper is in place before the line that allows it.
|
||||||
# R-861 (agent v0.146.0): the signed agent update, verified here (mode agent_update), then the A/B flip.
|
# R-861 (agent v0.146.0): the signed agent update, verified here (mode agent_update), then the A/B flip.
|
||||||
SELFUPDATE_OP = "agent_update"
|
SELFUPDATE_OP = "agent_update"
|
||||||
@@ -187,6 +190,8 @@ BUNDLE_RE = re.compile(r"^bundle-[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.]+)?\.json$"
|
|||||||
OOB_DIR = "/etc/felhom-sshd"
|
OOB_DIR = "/etc/felhom-sshd"
|
||||||
# R-982: the host's own sshd drop-in (after_install reloads sshd for it; undo reloads after putting it back).
|
# R-982: the host's own sshd drop-in (after_install reloads sshd for it; undo reloads after putting it back).
|
||||||
SSHD_DROPIN = "/etc/ssh/sshd_config.d/10-felhom-root-key-only.conf"
|
SSHD_DROPIN = "/etc/ssh/sshd_config.d/10-felhom-root-key-only.conf"
|
||||||
|
# D43 = A (R-1257): the first line every copy of the drop-in we ship carries — what proves a file on a BYO host is ours.
|
||||||
|
SSHD_DROPIN_HEADER = b"# MANAGED BY FELHOM (the agent's config bundle) \xe2\x80\x94 R-982"
|
||||||
|
|
||||||
BUNDLE_FILES = [
|
BUNDLE_FILES = [
|
||||||
("/usr/local/sbin/felhom-mkfs-guarded", "felhom-mkfs-guarded.sh", 0o755, "bash", "replace"),
|
("/usr/local/sbin/felhom-mkfs-guarded", "felhom-mkfs-guarded.sh", 0o755, "bash", "replace"),
|
||||||
@@ -227,7 +232,8 @@ BUNDLE_FILES = [
|
|||||||
("/etc/systemd/system/felhom-oob-nft.service", "felhom-oob-nft.service", 0o644, "unit", "oob"),
|
("/etc/systemd/system/felhom-oob-nft.service", "felhom-oob-nft.service", 0o644, "unit", "oob"),
|
||||||
# R-982, operator ruling D4 (2026-10-22): the host's own sshd (port 22, the LAN): root by key only. The console
|
# R-982, operator ruling D4 (2026-10-22): the host's own sshd (port 22, the LAN): root by key only. The console
|
||||||
# password stays; felhom-sshd (the operator's door) reads nothing from sshd_config.d. Reloaded in after_install.
|
# password stays; felhom-sshd (the operator's door) reads nothing from sshd_config.d. Reloaded in after_install.
|
||||||
("/etc/ssh/sshd_config.d/10-felhom-root-key-only.conf", "felhom-root-ssh-key-only.conf", 0o644, "sshd", "replace"),
|
# D43 = A (operator 2026-10-24, R-1257): appliances only — a BYO host keeps its own SSH settings (`11` §1).
|
||||||
|
("/etc/ssh/sshd_config.d/10-felhom-root-key-only.conf", "felhom-root-ssh-key-only.conf", 0o644, "sshd", "appliance"),
|
||||||
("/etc/sudoers.d/felhom-op", "felhom-op.sudoers", 0o440, "sudoers", "oob"),
|
("/etc/sudoers.d/felhom-op", "felhom-op.sudoers", 0o440, "sudoers", "oob"),
|
||||||
("/etc/sudoers.d/felhom-agent", "felhom-agent.sudoers", 0o440, "sudoers", "replace"),
|
("/etc/sudoers.d/felhom-agent", "felhom-agent.sudoers", 0o440, "sudoers", "replace"),
|
||||||
]
|
]
|
||||||
@@ -1873,6 +1879,7 @@ class Bundle:
|
|||||||
self.a, self.r = apply, apply.r
|
self.a, self.r = apply, apply.r
|
||||||
self.report = apply.report
|
self.report = apply.report
|
||||||
self.sshd_dropin_not_in_force = False
|
self.sshd_dropin_not_in_force = False
|
||||||
|
self.sshd_step_undone = None # X4 (R-1257): why the SSH step alone was put back ("" = not), the rest stays
|
||||||
|
|
||||||
# ---------- reading and checking ----------
|
# ---------- reading and checking ----------
|
||||||
def load_file(self, path):
|
def load_file(self, path):
|
||||||
@@ -1982,14 +1989,40 @@ class Bundle:
|
|||||||
return b"", None, None # a symlink or a directory: always replaced by the real file
|
return b"", None, None # a symlink or a directory: always replaced by the real file
|
||||||
return self.r.read_bytes(dest), stat.S_IMODE(st.st_mode), st.st_uid
|
return self.r.read_bytes(dest), stat.S_IMODE(st.st_mode), st.st_uid
|
||||||
|
|
||||||
|
def install_mode(self):
|
||||||
|
"""The box's ROOT-OWNED install record's mode ("appliance", "byo", …), or None when it proves nothing (absent,
|
||||||
|
agent-writable, unreadable) — the same proof check_appliance asks for the host layer (R12)."""
|
||||||
|
try:
|
||||||
|
st = self.r.stat(INSTALL_STATE)
|
||||||
|
if st.st_uid != 0 or (st.st_mode & 0o022):
|
||||||
|
return None
|
||||||
|
mode = json.loads(self.r.read_file(INSTALL_STATE)).get("mode")
|
||||||
|
except (OSError, ValueError, AttributeError):
|
||||||
|
return None
|
||||||
|
return mode if isinstance(mode, str) and mode else None
|
||||||
|
|
||||||
def plan_writes(self, files):
|
def plan_writes(self, files):
|
||||||
"""Decide per file: write / same / kept (if-absent and present) / skipped (oob on a box without the belt)."""
|
"""Decide per file: write / same / kept (if-absent and present) / skipped (oob on a box without the belt; an
|
||||||
|
appliance-only file on a box that is not one) / remove (an appliance-only file WE wrote, on a BYO box)."""
|
||||||
oob = self.r.isdir(OOB_DIR)
|
oob = self.r.isdir(OOB_DIR)
|
||||||
|
install_mode = self.install_mode()
|
||||||
plan = []
|
plan = []
|
||||||
for dest, content, mode, check, policy in files:
|
for dest, content, mode, check, policy in files:
|
||||||
if policy == "oob" and not oob:
|
if policy == "oob" and not oob:
|
||||||
plan.append((dest, content, mode, "skipped", None, None))
|
plan.append((dest, content, mode, "skipped", None, None))
|
||||||
continue
|
continue
|
||||||
|
if policy == "appliance" and install_mode != "appliance":
|
||||||
|
old, old_mode, _ = self.live(dest)
|
||||||
|
ours = old is not None and old.startswith(SSHD_DROPIN_HEADER)
|
||||||
|
if install_mode == "byo" and ours:
|
||||||
|
self.r.log(f"os-apply: BUNDLE {dest}: a BYO host keeps its own settings (D43) — removing the copy we wrote")
|
||||||
|
plan.append((dest, content, mode, "remove", old, old_mode))
|
||||||
|
else:
|
||||||
|
why = "a BYO host keeps its own settings (D43)" if install_mode == "byo" else \
|
||||||
|
f"the install record proves no appliance (mode={install_mode!r}) — left as it is"
|
||||||
|
self.r.log(f"os-apply: BUNDLE {dest} skipped: {why}")
|
||||||
|
plan.append((dest, content, mode, "skipped", None, None))
|
||||||
|
continue
|
||||||
old, old_mode, old_uid = self.live(dest)
|
old, old_mode, old_uid = self.live(dest)
|
||||||
if policy == "if-absent" and old is not None:
|
if policy == "if-absent" and old is not None:
|
||||||
plan.append((dest, content, mode, "kept", old, old_mode))
|
plan.append((dest, content, mode, "kept", old, old_mode))
|
||||||
@@ -2031,7 +2064,7 @@ class Bundle:
|
|||||||
def install(self, ver, sha, files, authority, bootstrap):
|
def install(self, ver, sha, files, authority, bootstrap):
|
||||||
log = self.r.log
|
log = self.r.log
|
||||||
plan = self.plan_writes(files)
|
plan = self.plan_writes(files)
|
||||||
counts = {k: sum(1 for p in plan if p[3] == k) for k in ("write", "same", "kept", "skipped")}
|
counts = {k: sum(1 for p in plan if p[3] == k) for k in ("write", "same", "kept", "skipped", "remove")}
|
||||||
log(f"os-apply: BUNDLE START agent={ver} sha={sha[:16]} authority={authority} files={len(plan)} "
|
log(f"os-apply: BUNDLE START agent={ver} sha={sha[:16]} authority={authority} files={len(plan)} "
|
||||||
f"write={counts['write']} same={counts['same']} kept={counts['kept']} skipped={counts['skipped']}")
|
f"write={counts['write']} same={counts['same']} kept={counts['kept']} skipped={counts['skipped']}")
|
||||||
stamp = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(self.r.now()))
|
stamp = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(self.r.now()))
|
||||||
@@ -2039,10 +2072,17 @@ class Bundle:
|
|||||||
done = [] # (dest, old bytes or None, old mode)
|
done = [] # (dest, old bytes or None, old mode)
|
||||||
rep = {"agent_version": ver, "sha256": sha, "authority": authority, "prev_dir": prev,
|
rep = {"agent_version": ver, "sha256": sha, "authority": authority, "prev_dir": prev,
|
||||||
"written": [p[0] for p in plan if p[3] == "write"], "kept": [p[0] for p in plan if p[3] == "kept"],
|
"written": [p[0] for p in plan if p[3] == "write"], "kept": [p[0] for p in plan if p[3] == "kept"],
|
||||||
"skipped": [p[0] for p in plan if p[3] == "skipped"], "same": counts["same"]}
|
"skipped": [p[0] for p in plan if p[3] == "skipped"], "same": counts["same"],
|
||||||
|
"removed": [p[0] for p in plan if p[3] == "remove"]}
|
||||||
self.report["bundle"] = rep
|
self.report["bundle"] = rep
|
||||||
try:
|
try:
|
||||||
for dest, content, mode, action, old, old_mode in plan:
|
for dest, content, mode, action, old, old_mode in plan:
|
||||||
|
if action == "remove": # D43 = A: our copy off a BYO host (the previous copy kept, the undo restores it)
|
||||||
|
self.r.put_file(prev + dest, old, 0o600)
|
||||||
|
done.append((dest, old, old_mode))
|
||||||
|
self.r.remove(dest)
|
||||||
|
log(f"os-apply: BUNDLE REMOVED {dest} (ours, on a BYO host — D43)")
|
||||||
|
continue
|
||||||
if action != "write":
|
if action != "write":
|
||||||
continue
|
continue
|
||||||
if old is not None:
|
if old is not None:
|
||||||
@@ -2062,22 +2102,56 @@ class Bundle:
|
|||||||
rep["signers_created"] = False
|
rep["signers_created"] = False
|
||||||
if self.sshd_dropin_not_in_force:
|
if self.sshd_dropin_not_in_force:
|
||||||
rep["sshd_dropin_not_in_force"] = True # R-982 A2: written and reloaded, but sshd -T shows root passwords allowed
|
rep["sshd_dropin_not_in_force"] = True # R-982 A2: written and reloaded, but sshd -T shows root passwords allowed
|
||||||
|
if self.sshd_step_undone:
|
||||||
|
rep["sshd_step_undone"] = self.sshd_step_undone[:300] # X4: only the SSH step was put back
|
||||||
if bootstrap and not self.r.lexists(TRUST_SIGNERS):
|
if bootstrap and not self.r.lexists(TRUST_SIGNERS):
|
||||||
self.r.put_file(TRUST_SIGNERS, pinned_signers_line().encode(), 0o644)
|
self.r.put_file(TRUST_SIGNERS, pinned_signers_line().encode(), 0o644)
|
||||||
rep["signers_created"] = True
|
rep["signers_created"] = True
|
||||||
log(f"os-apply: BUNDLE created {TRUST_SIGNERS} with exactly the pinned key {PINNED_OPERATOR_KEY_ID}")
|
log(f"os-apply: BUNDLE created {TRUST_SIGNERS} with exactly the pinned key {PINNED_OPERATOR_KEY_ID}")
|
||||||
record = {"format": BUNDLE_FORMAT, "agent_version": ver, "bundle_sha256": sha, "authority": authority,
|
record = {"format": BUNDLE_FORMAT, "agent_version": ver, "bundle_sha256": sha, "authority": authority,
|
||||||
"installed_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(self.r.now())),
|
"installed_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(self.r.now())),
|
||||||
"files": {p[0]: (sha256_hex(p[1]) if p[3] in ("write", "same") else
|
"files": {p[0]: self.record_entry(p) for p in plan}}
|
||||||
(sha256_hex(p[4]) if p[3] == "kept" else "skipped")) for p in plan}}
|
|
||||||
self.r.put_file(BUNDLE_RECORD, (json.dumps(record, indent=2, sort_keys=True) + "\n").encode(), 0o644)
|
self.r.put_file(BUNDLE_RECORD, (json.dumps(record, indent=2, sort_keys=True) + "\n").encode(), 0o644)
|
||||||
self.prune()
|
self.prune()
|
||||||
log(f"os-apply: BUNDLE DONE agent={ver} written={counts['write']} same={counts['same']} "
|
log(f"os-apply: BUNDLE DONE agent={ver} written={counts['write']} same={counts['same']} "
|
||||||
f"self-check=ok signers-created={rep['signers_created']}")
|
f"self-check=ok signers-created={rep['signers_created']}")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def record_entry(self, p):
|
||||||
|
"""The record's word for one plan entry: the sha it installed, the sha it kept, or what it did instead."""
|
||||||
|
dest, content, _, action, old, _ = p
|
||||||
|
if dest == SSHD_DROPIN and self.sshd_step_undone:
|
||||||
|
return sha256_hex(old) if old is not None else "absent" # X4: the SSH step was put back
|
||||||
|
if action in ("write", "same"):
|
||||||
|
return sha256_hex(content)
|
||||||
|
if action == "kept":
|
||||||
|
return sha256_hex(old)
|
||||||
|
return "removed" if action == "remove" else "skipped"
|
||||||
|
|
||||||
|
def lan_addresses(self):
|
||||||
|
"""X3 (R-1257): the host's own global IPv4 addresses — the ones a LAN client reaches sshd on."""
|
||||||
|
rc, out, _ = self.r.host(["ip", "-4", "-o", "addr", "show", "scope", "global"], 30)
|
||||||
|
if rc != 0:
|
||||||
|
return []
|
||||||
|
return sorted(set(re.findall(r"\binet (\d+\.\d+\.\d+\.\d+)/", out)))
|
||||||
|
|
||||||
|
def undo_sshd_step(self, plan, why):
|
||||||
|
"""X4 (R-1257): `sshd -t` refused the host's whole config with our step in it — put back ONLY that file (the
|
||||||
|
previous copy, or none) and keep the rest of the bundle. sshd was not reloaded, so it never ran the refused state."""
|
||||||
|
for dest, _, _, action, old, old_mode in plan:
|
||||||
|
if dest != SSHD_DROPIN or action not in ("write", "remove"):
|
||||||
|
continue
|
||||||
|
if old is None:
|
||||||
|
self.r.remove(dest)
|
||||||
|
else:
|
||||||
|
self.r.put_file(dest, old, old_mode if old_mode is not None else 0o644)
|
||||||
|
self.sshd_step_undone = why
|
||||||
|
self.r.log(f"os-apply: BUNDLE WARNING sshd -t refused the host's config with the SSH step — ONLY that step is put "
|
||||||
|
f"back, the rest of the bundle stays: {why[:200]} (R-1257)")
|
||||||
|
|
||||||
def after_install(self, plan):
|
def after_install(self, plan):
|
||||||
written = {p[0] for p in plan if p[3] == "write"}
|
written = {p[0] for p in plan if p[3] == "write"}
|
||||||
|
removed = {p[0] for p in plan if p[3] == "remove"}
|
||||||
dests = {p[0] for p in plan if p[3] != "skipped"}
|
dests = {p[0] for p in plan if p[3] != "skipped"}
|
||||||
if any(d.startswith("/etc/systemd/system/") for d in written):
|
if any(d.startswith("/etc/systemd/system/") for d in written):
|
||||||
self.must(["systemctl", "daemon-reload"], "systemctl daemon-reload")
|
self.must(["systemctl", "daemon-reload"], "systemctl daemon-reload")
|
||||||
@@ -2098,16 +2172,29 @@ class Bundle:
|
|||||||
# felhom-sshd is not touched. Then the POSITIVE check: sshd's own effective view for root. A box whose
|
# felhom-sshd is not touched. Then the POSITIVE check: sshd's own effective view for root. A box whose
|
||||||
# sshd_config has no Include of sshd_config.d, an earlier drop-in or a Match block can keep the password route
|
# sshd_config has no Include of sshd_config.d, an earlier drop-in or a Match block can keep the password route
|
||||||
# — that is said loudly in the log and the report, never silently recorded as done (A2).
|
# — that is said loudly in the log and the report, never silently recorded as done (A2).
|
||||||
if SSHD_DROPIN in written:
|
# R-1257 X4: a failing `sshd -t` puts back ONLY the SSH step (undo_sshd_step); it used to refuse and undo the
|
||||||
self.must(["/usr/sbin/sshd", "-t"], "sshd -t (the host's whole sshd config)")
|
# whole bundle. X3: the in-force check asks sshd about each of the host's own LAN addresses — a probe from a
|
||||||
|
# documentation address (192.0.2.1) read a `Match Address <LAN>` block that re-allows passwords as in force.
|
||||||
|
if SSHD_DROPIN in written or SSHD_DROPIN in removed:
|
||||||
|
rc, out, err = self.r.host(["/usr/sbin/sshd", "-t"], 120)
|
||||||
|
if rc != 0:
|
||||||
|
self.undo_sshd_step(plan, f"sshd -t failed (rc={rc}): {(out + err).strip()[-200:]}")
|
||||||
|
return
|
||||||
self.must(["systemctl", "try-reload-or-restart", "ssh"], "systemctl try-reload-or-restart ssh")
|
self.must(["systemctl", "try-reload-or-restart", "ssh"], "systemctl try-reload-or-restart ssh")
|
||||||
rc, out, err = self.r.host(["/usr/sbin/sshd", "-T", "-C", "user=root,host=felhom-check,addr=192.0.2.1"], 60)
|
if SSHD_DROPIN in written:
|
||||||
eff = re.findall(r"^permitrootlogin\s+(\S+)", out, re.M | re.I)
|
addrs = self.lan_addresses()
|
||||||
if rc != 0 or eff != ["without-password"]:
|
if not addrs:
|
||||||
self.r.log(f"os-apply: BUNDLE WARNING the root-key-only drop-in is NOT in force: sshd -T says "
|
self.r.log("os-apply: BUNDLE WARNING no global IPv4 address read for the sshd in-force check — "
|
||||||
f"permitrootlogin={eff or '?'} (rc={rc}) — check sshd_config's Include, an earlier drop-in "
|
"probing 192.0.2.1 only (R-1257)")
|
||||||
f"or a Match block (R-982)")
|
addrs = ["192.0.2.1"]
|
||||||
self.sshd_dropin_not_in_force = True
|
for addr in addrs:
|
||||||
|
rc, out, err = self.r.host(["/usr/sbin/sshd", "-T", "-C", f"user=root,host=felhom-check,addr={addr}"], 60)
|
||||||
|
eff = re.findall(r"^permitrootlogin\s+(\S+)", out, re.M | re.I)
|
||||||
|
if rc != 0 or eff != ["without-password"]:
|
||||||
|
self.r.log(f"os-apply: BUNDLE WARNING the root-key-only drop-in is NOT in force for {addr}: sshd -T "
|
||||||
|
f"says permitrootlogin={eff or '?'} (rc={rc}) — check sshd_config's Include, an earlier "
|
||||||
|
f"drop-in or a Match block (R-982, R-1257)")
|
||||||
|
self.sshd_dropin_not_in_force = True
|
||||||
|
|
||||||
def must(self, argv, what):
|
def must(self, argv, what):
|
||||||
rc, out, err = self.r.host(argv, 120)
|
rc, out, err = self.r.host(argv, 120)
|
||||||
|
|||||||
@@ -45,8 +45,10 @@ class St:
|
|||||||
class Box:
|
class Box:
|
||||||
"""An in-memory host. files: path -> bytes; modes/uids per path; dirs: a set."""
|
"""An in-memory host. files: path -> bytes; modes/uids per path; dirs: a set."""
|
||||||
|
|
||||||
def __init__(self, bundle_bytes, signed, oob=False, signers=True):
|
def __init__(self, bundle_bytes, signed, oob=False, signers=True, install_mode="appliance"):
|
||||||
self.files, self.modes, self.uids = {}, {}, {}
|
self.files, self.modes, self.uids = {}, {}, {}
|
||||||
|
if install_mode is not None: # the installer's ROOT-OWNED record (D43 = A reads it for the sshd drop-in)
|
||||||
|
self.put(osapply.INSTALL_STATE, json.dumps({"completed": [], "mode": install_mode}).encode(), 0o644)
|
||||||
self.dirs = {osapply.OOB_DIR} if oob else set()
|
self.dirs = {osapply.OOB_DIR} if oob else set()
|
||||||
self.put(osapply.TRUST_FILE, json.dumps({"host_id": HOST, "ring0_slow_lane": False}).encode(), 0o644)
|
self.put(osapply.TRUST_FILE, json.dumps({"host_id": HOST, "ring0_slow_lane": False}).encode(), 0o644)
|
||||||
if signers:
|
if signers:
|
||||||
@@ -513,6 +515,116 @@ class RootKeyOnly(unittest.TestCase):
|
|||||||
reloads = [i for i, c in enumerate(box.calls) if c == ["systemctl", "try-reload-or-restart", "ssh"]]
|
reloads = [i for i, c in enumerate(box.calls) if c == ["systemctl", "try-reload-or-restart", "ssh"]]
|
||||||
self.assertEqual(len(reloads), 2, box.calls) # after_install, then the undo
|
self.assertEqual(len(reloads), 2, box.calls) # after_install, then the undo
|
||||||
|
|
||||||
|
class ByoKeepsItsSSH(unittest.TestCase):
|
||||||
|
"""D43 = A (operator 2026-10-24, R-1257): the root-key-only drop-in goes ONLY to a box whose root-owned install record
|
||||||
|
says appliance; on a BYO host nothing is written and a copy WE wrote is removed; a box with no record is left as it
|
||||||
|
is. X3: the in-force check asks sshd about each of the host's own LAN addresses. X4: a failing `sshd -t` puts back
|
||||||
|
only the SSH step. RED-PROOF (2026-10-24, felhom-agent f5c3d77 + these tests): with the drop-in's policy back to
|
||||||
|
`replace`, test_a_byo_host_gets_no_dropin and test_our_copy_comes_off_a_byo_host fail; with the probe back to
|
||||||
|
192.0.2.1 only, test_a_match_block_for_the_lan_is_not_in_force fails; with `sshd -t` back to `must`,
|
||||||
|
test_a_failing_sshd_t_undoes_only_the_ssh_step fails (rc 3, the whole bundle undone)."""
|
||||||
|
DEST = "/etc/ssh/sshd_config.d/10-felhom-root-key-only.conf"
|
||||||
|
OURS = (REPO / "configs" / "felhom-root-ssh-key-only.conf").read_bytes()
|
||||||
|
|
||||||
|
def test_a_byo_host_gets_no_dropin(self):
|
||||||
|
data, sha = real_bundle()
|
||||||
|
box = Box(data, signed_job(sha), install_mode="byo")
|
||||||
|
rc, rep = run(box)
|
||||||
|
self.assertEqual(rc, 0, rep)
|
||||||
|
self.assertNotIn(self.DEST, box.files)
|
||||||
|
self.assertNotIn(["/usr/sbin/sshd", "-t"], box.calls)
|
||||||
|
self.assertNotIn(["systemctl", "try-reload-or-restart", "ssh"], box.calls)
|
||||||
|
self.assertIn(self.DEST, rep["bundle"]["skipped"])
|
||||||
|
rec = json.loads(box.files[osapply.BUNDLE_RECORD])
|
||||||
|
self.assertEqual(rec["files"][self.DEST], "skipped")
|
||||||
|
|
||||||
|
def test_our_copy_comes_off_a_byo_host(self):
|
||||||
|
data, sha = real_bundle()
|
||||||
|
box = Box(data, signed_job(sha), install_mode="byo")
|
||||||
|
box.put(self.DEST, self.OURS, 0o644) # an earlier bundle wrote it (before D43)
|
||||||
|
rc, rep = run(box)
|
||||||
|
self.assertEqual(rc, 0, rep)
|
||||||
|
self.assertNotIn(self.DEST, box.files)
|
||||||
|
self.assertEqual(rep["bundle"]["removed"], [self.DEST])
|
||||||
|
self.assertTrue(any(k.endswith(self.DEST) and k.startswith(osapply.BUNDLE_PREV_DIR) for k in box.files),
|
||||||
|
"the previous copy must be kept for the undo")
|
||||||
|
self.assertLess(box.calls.index(["/usr/sbin/sshd", "-t"]), box.calls.index(["systemctl", "try-reload-or-restart", "ssh"]))
|
||||||
|
self.assertEqual(json.loads(box.files[osapply.BUNDLE_RECORD])["files"][self.DEST], "removed")
|
||||||
|
|
||||||
|
def test_the_owners_own_file_stays_on_a_byo_host(self):
|
||||||
|
data, sha = real_bundle()
|
||||||
|
box = Box(data, signed_job(sha), install_mode="byo")
|
||||||
|
box.put(self.DEST, b"# my own\nPermitRootLogin yes\n", 0o644)
|
||||||
|
rc, rep = run(box)
|
||||||
|
self.assertEqual(rc, 0, rep)
|
||||||
|
self.assertEqual(box.files[self.DEST], b"# my own\nPermitRootLogin yes\n")
|
||||||
|
self.assertNotIn(["systemctl", "try-reload-or-restart", "ssh"], box.calls)
|
||||||
|
|
||||||
|
def test_a_box_with_no_record_is_left_as_it_is(self):
|
||||||
|
for setup in ("none", "agent-writable"):
|
||||||
|
data, sha = real_bundle()
|
||||||
|
box = Box(data, signed_job(sha), install_mode=None if setup == "none" else "byo")
|
||||||
|
if setup == "agent-writable":
|
||||||
|
box.modes[osapply.INSTALL_STATE] = 0o666 # proves nothing
|
||||||
|
box.put(self.DEST, self.OURS, 0o644)
|
||||||
|
rc, rep = run(box)
|
||||||
|
self.assertEqual(rc, 0, rep)
|
||||||
|
self.assertEqual(box.files.get(self.DEST), self.OURS, setup)
|
||||||
|
self.assertIn(self.DEST, rep["bundle"]["skipped"], setup)
|
||||||
|
|
||||||
|
def _fake(self, box, addrs=("192.168.0.50",), match=None, sshd_t_rc=0):
|
||||||
|
orig = box.host
|
||||||
|
def host(argv, timeout=600, stdin=None):
|
||||||
|
if argv[:3] == ["ip", "-4", "-o"]:
|
||||||
|
box.calls.append(argv)
|
||||||
|
return 0, "".join(f"2: vmbr0 inet {a}/24 brd x scope global vmbr0\\ valid_lft forever\n" for a in addrs), ""
|
||||||
|
if argv[:2] == ["/usr/sbin/sshd", "-T"]:
|
||||||
|
box.calls.append(argv)
|
||||||
|
val = "yes" if match and f"addr={match}" in argv[-1] else "without-password"
|
||||||
|
return 0, f"port 22\npermitrootlogin {val}\n", ""
|
||||||
|
if argv == ["/usr/sbin/sshd", "-t"]:
|
||||||
|
box.calls.append(argv)
|
||||||
|
return (sshd_t_rc, "", "/etc/ssh/sshd_config line 9: Bad configuration option") if sshd_t_rc else (0, "", "")
|
||||||
|
return orig(argv, timeout, stdin)
|
||||||
|
box.host = host
|
||||||
|
|
||||||
|
def test_a_match_block_for_the_lan_is_not_in_force(self):
|
||||||
|
data, sha = real_bundle()
|
||||||
|
box = Box(data, signed_job(sha))
|
||||||
|
self._fake(box, addrs=("192.168.0.50", "10.77.0.4"), match="192.168.0.50")
|
||||||
|
rc, rep = run(box)
|
||||||
|
self.assertEqual(rc, 0, rep)
|
||||||
|
probed = [c[-1] for c in box.calls if c[:2] == ["/usr/sbin/sshd", "-T"]]
|
||||||
|
self.assertTrue(any("addr=192.168.0.50" in p for p in probed) and any("addr=10.77.0.4" in p for p in probed), probed)
|
||||||
|
self.assertFalse(any("addr=192.0.2.1" in p for p in probed), probed)
|
||||||
|
self.assertTrue(rep["bundle"].get("sshd_dropin_not_in_force"), rep)
|
||||||
|
self.assertTrue(any("NOT in force for 192.168.0.50" in l for l in box.logs), box.logs)
|
||||||
|
|
||||||
|
def test_the_lan_check_passes_when_every_address_is_key_only(self):
|
||||||
|
data, sha = real_bundle()
|
||||||
|
box = Box(data, signed_job(sha))
|
||||||
|
self._fake(box, addrs=("192.168.0.50", "10.77.0.4"))
|
||||||
|
rc, rep = run(box)
|
||||||
|
self.assertEqual(rc, 0, rep)
|
||||||
|
self.assertNotIn("sshd_dropin_not_in_force", rep["bundle"])
|
||||||
|
|
||||||
|
def test_a_failing_sshd_t_undoes_only_the_ssh_step(self):
|
||||||
|
for before in (None, b"# MANAGED BY FELHOM (the agent's config bundle) \xe2\x80\x94 R-982 old\nPermitRootLogin prohibit-password\n"):
|
||||||
|
data, sha = real_bundle()
|
||||||
|
box = Box(data, signed_job(sha))
|
||||||
|
if before is not None:
|
||||||
|
box.put(self.DEST, before, 0o644)
|
||||||
|
self._fake(box, sshd_t_rc=255)
|
||||||
|
rc, rep = run(box)
|
||||||
|
self.assertEqual(rc, 0, rep) # the bundle stays
|
||||||
|
self.assertEqual(box.files.get(self.DEST), before, "only the SSH step is put back")
|
||||||
|
self.assertIn("/usr/local/sbin/felhom-os-apply", box.files, "the rest of the bundle stays")
|
||||||
|
self.assertIn("sshd -t failed", rep["bundle"].get("sshd_step_undone", ""), rep)
|
||||||
|
self.assertNotIn(["systemctl", "try-reload-or-restart", "ssh"], box.calls, "sshd never ran the refused state")
|
||||||
|
want = "absent" if before is None else hashlib.sha256(before).hexdigest()
|
||||||
|
self.assertEqual(json.loads(box.files[osapply.BUNDLE_RECORD])["files"][self.DEST], want)
|
||||||
|
|
||||||
|
|
||||||
class TrustBootstrap(unittest.TestCase):
|
class TrustBootstrap(unittest.TestCase):
|
||||||
def test_missing_signers_verifies_against_the_pinned_key_and_creates_it(self):
|
def test_missing_signers_verifies_against_the_pinned_key_and_creates_it(self):
|
||||||
data, sha = real_bundle()
|
data, sha = real_bundle()
|
||||||
|
|||||||
+113
-11
@@ -19,6 +19,10 @@
|
|||||||
// - No tunnel (no operational wg-felhom with a server address) → no test and no value; the controller then keeps its
|
// - No tunnel (no operational wg-felhom with a server address) → no test and no value; the controller then keeps its
|
||||||
// passive floor and sets no qBittorrent limit.
|
// passive floor and sets no qBittorrent limit.
|
||||||
// - The last result is persisted, so a restart neither loses it nor re-tests.
|
// - The last result is persisted, so a restart neither loses it nor re-tests.
|
||||||
|
// - A box whose torrent app seeds all day is never quiet for 10 minutes (R-1250). For it the controller holds the
|
||||||
|
// app's upload limit very low and asks for a test on request (RunNow): every rule above except the 10 quiet
|
||||||
|
// minutes still holds, the quiet is checked over RequestQuietWindow instead, and the runs are RequestRunGap
|
||||||
|
// apart, so the whole request ends in about 55 s.
|
||||||
package linetest
|
package linetest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -56,6 +60,11 @@ const (
|
|||||||
TickInterval = time.Minute
|
TickInterval = time.Minute
|
||||||
FirstTickDelay = 5 * time.Minute
|
FirstTickDelay = 5 * time.Minute
|
||||||
GateName = "line-test"
|
GateName = "line-test"
|
||||||
|
|
||||||
|
// A test on request (R-1250): the controller holds the app's limit while it waits, so it is short.
|
||||||
|
RequestSettle = 5 * time.Second // the held app's last packets leave before the quiet is read
|
||||||
|
RequestQuietWindow = 10 * time.Second // the uplink must send below QuietMbps over this window
|
||||||
|
RequestRunGap = 5 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
// ScheduleText is carried on the host report.
|
// ScheduleText is carried on the host report.
|
||||||
@@ -258,30 +267,52 @@ func (t *Tester) Run(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tick is one look.
|
// Refusal is why a test on request did not run (RunNow). Code is stable for the controller.
|
||||||
func (t *Tester) Tick(ctx context.Context) {
|
type Refusal struct {
|
||||||
|
Code string // not-due | outside-window | tries-used | no-tunnel | heavy-op | not-quiet
|
||||||
|
Msg string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Refusal) Error() string { return "linetest: " + r.Code + ": " + r.Msg }
|
||||||
|
|
||||||
|
// ready applies every rule except the quiet one: due this week, inside the window (the slot on the due day), tries
|
||||||
|
// left today, the tunnel up. It answers the server address and the local day.
|
||||||
|
func (t *Tester) ready() (addr, day string, refusal *Refusal) {
|
||||||
lt := t.now().In(t.loc)
|
lt := t.now().In(t.loc)
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
st := t.st
|
st := t.st
|
||||||
samples := append([]txSample(nil), t.samples...)
|
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
if !Due(st, lt) {
|
if !Due(st, lt) {
|
||||||
return
|
return "", "", &Refusal{"not-due", "measured this week"}
|
||||||
}
|
}
|
||||||
anchor := weekAnchor(lt)
|
anchor := weekAnchor(lt)
|
||||||
anchorDay := lt.YearDay() == anchor.YearDay() && lt.Year() == anchor.Year()
|
anchorDay := lt.YearDay() == anchor.YearDay() && lt.Year() == anchor.Year()
|
||||||
if !EligibleAt(lt, t.slot, anchorDay) {
|
if !EligibleAt(lt, t.slot, anchorDay) {
|
||||||
return
|
return "", "", &Refusal{"outside-window", "a test starts 10:00-15:59 local, from this host's slot on the due day"}
|
||||||
}
|
}
|
||||||
day := lt.Format("2006-01-02")
|
day = lt.Format("2006-01-02")
|
||||||
if st.TriesDay == day && st.TriesToday >= MaxTriesPerDay {
|
if st.TriesDay == day && st.TriesToday >= MaxTriesPerDay {
|
||||||
return
|
return "", "", &Refusal{"tries-used", fmt.Sprintf("%d tries today", st.TriesToday)}
|
||||||
}
|
}
|
||||||
ip, ok := t.target()
|
ip, ok := t.target()
|
||||||
if !ok {
|
if !ok {
|
||||||
t.logger.Debug("linetest: no operational tunnel — no test")
|
return "", "", &Refusal{"no-tunnel", "no operational tunnel"}
|
||||||
|
}
|
||||||
|
return netip.AddrPortFrom(ip, ServerPort).String(), day, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tick is one look.
|
||||||
|
func (t *Tester) Tick(ctx context.Context) {
|
||||||
|
addr, day, refusal := t.ready()
|
||||||
|
if refusal != nil {
|
||||||
|
if refusal.Code == "no-tunnel" {
|
||||||
|
t.logger.Debug("linetest: no operational tunnel — no test")
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
t.mu.Lock()
|
||||||
|
samples := append([]txSample(nil), t.samples...)
|
||||||
|
t.mu.Unlock()
|
||||||
if q, worst := quiet(samples); !q {
|
if q, worst := quiet(samples); !q {
|
||||||
t.logger.Debug("linetest: uplink not quiet — waiting", "worst_minute_mbps", fmt.Sprintf("%.1f", worst), "samples", len(samples))
|
t.logger.Debug("linetest: uplink not quiet — waiting", "worst_minute_mbps", fmt.Sprintf("%.1f", worst), "samples", len(samples))
|
||||||
return
|
return
|
||||||
@@ -292,16 +323,80 @@ func (t *Tester) Tick(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer release()
|
defer release()
|
||||||
t.measure(ctx, netip.AddrPortFrom(ip, ServerPort).String(), day)
|
t.measure(ctx, addr, day, RunGap)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tester) measure(ctx context.Context, addr, day string) {
|
// WaitingForQuiet reports that a measurement is due and could start now but the uplink has not been quiet for
|
||||||
|
// QuietMinutes — the case a seeding torrent app makes permanent (R-1250). The controller reads it from
|
||||||
|
// GET /host/line-test and only then holds its app's limit and calls RunNow.
|
||||||
|
func (t *Tester) WaitingForQuiet() bool {
|
||||||
|
if _, _, refusal := t.ready(); refusal != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
t.mu.Lock()
|
||||||
|
samples := append([]txSample(nil), t.samples...)
|
||||||
|
t.mu.Unlock()
|
||||||
|
if len(samples) < QuietMinutes+1 {
|
||||||
|
return false // not yet watched for 10 minutes (a fresh start): not known to be busy, so no hold
|
||||||
|
}
|
||||||
|
q, _ := quiet(samples)
|
||||||
|
return !q
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunNow is a test on request (POST /host/line-test/run, R-1250): every rule of the weekly test except the 10 quiet
|
||||||
|
// minutes, which the caller replaces by holding its own upload down. The uplink must still be quiet over
|
||||||
|
// RequestQuietWindow (after RequestSettle), so a request never measures the family's upload against itself. A
|
||||||
|
// *Refusal says why nothing ran; any other error is a failed measurement (the same as a failed weekly attempt).
|
||||||
|
func (t *Tester) RunNow(ctx context.Context) (mbps float64, runs []float64, err error) {
|
||||||
|
addr, day, refusal := t.ready()
|
||||||
|
if refusal != nil {
|
||||||
|
return 0, nil, refusal
|
||||||
|
}
|
||||||
|
release, busy, ok := t.gate.TryAcquire(GateName)
|
||||||
|
if !ok {
|
||||||
|
return 0, nil, &Refusal{"heavy-op", busy}
|
||||||
|
}
|
||||||
|
defer release()
|
||||||
|
if t.uplink == nil {
|
||||||
|
return 0, nil, &Refusal{"not-quiet", "no uplink counter"}
|
||||||
|
}
|
||||||
|
if !t.sleep(ctx, RequestSettle) {
|
||||||
|
return 0, nil, ctx.Err()
|
||||||
|
}
|
||||||
|
tx0, err0 := t.uplink()
|
||||||
|
at0 := t.now()
|
||||||
|
if !t.sleep(ctx, RequestQuietWindow) {
|
||||||
|
return 0, nil, ctx.Err()
|
||||||
|
}
|
||||||
|
tx1, err1 := t.uplink()
|
||||||
|
if err0 != nil || err1 != nil || tx1 < tx0 {
|
||||||
|
return 0, nil, &Refusal{"not-quiet", "uplink counter unreadable or reset"}
|
||||||
|
}
|
||||||
|
secs := t.now().Sub(at0).Seconds()
|
||||||
|
if secs <= 0 {
|
||||||
|
secs = RequestQuietWindow.Seconds()
|
||||||
|
}
|
||||||
|
if m := float64(tx1-tx0) * 8 / secs / 1e6; m >= QuietMbps {
|
||||||
|
t.logger.Info("linetest: test on request refused — the uplink is not quiet", "mbps", fmt.Sprintf("%.1f", m))
|
||||||
|
return 0, nil, &Refusal{"not-quiet", fmt.Sprintf("the uplink sent %.1f Mbit/s while held", m)}
|
||||||
|
}
|
||||||
|
t.logger.Info("linetest: test on request (a held torrent app, R-1250)", "server", addr)
|
||||||
|
if err := t.measure(ctx, addr, day, RequestRunGap); err != nil {
|
||||||
|
return 0, nil, err
|
||||||
|
}
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
return t.st.Mbps, append([]float64(nil), t.st.RunsMbps...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// measure runs the runs, gap apart, and stores the result. It returns nil only when a value was stored.
|
||||||
|
func (t *Tester) measure(ctx context.Context, addr, day string, gap time.Duration) error {
|
||||||
start := t.now()
|
start := t.now()
|
||||||
var runs []float64
|
var runs []float64
|
||||||
var lastErr error
|
var lastErr error
|
||||||
busy := false
|
busy := false
|
||||||
for i := 0; i < RunsPerMeasurement; i++ {
|
for i := 0; i < RunsPerMeasurement; i++ {
|
||||||
if i > 0 && !t.sleep(ctx, RunGap) {
|
if i > 0 && !t.sleep(ctx, gap) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
rctx, cancel := context.WithTimeout(ctx, RunTimeout)
|
rctx, cancel := context.WithTimeout(ctx, RunTimeout)
|
||||||
@@ -356,6 +451,13 @@ func (t *Tester) measure(ctx context.Context, addr, day string) {
|
|||||||
if err := t.saveLocked(); err != nil {
|
if err := t.saveLocked(); err != nil {
|
||||||
t.logger.Warn("linetest: state save failed — the result will not survive a restart", "path", t.statePath, "err", err)
|
t.logger.Warn("linetest: state save failed — the result will not survive a restart", "path", t.statePath, "err", err)
|
||||||
}
|
}
|
||||||
|
if len(runs) > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if lastErr == nil {
|
||||||
|
lastErr = errors.New("linetest: no run succeeded")
|
||||||
|
}
|
||||||
|
return lastErr
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value is the measured line (ok=false: never measured).
|
// Value is the measured line (ok=false: never measured).
|
||||||
|
|||||||
@@ -431,3 +431,144 @@ func TestQuietNeedsTenFullMinutes(t *testing.T) {
|
|||||||
t.Fatal("a gap in the samples must not read quiet")
|
t.Fatal("a gap in the samples must not read quiet")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── a test on request (R-1250): a torrent app that seeds all day ───────────────────────────────────────────────────
|
||||||
|
|
||||||
|
// seedingRig: due and eligible, but every minute of the last ten sent at the line's speed — the weekly Tick never runs.
|
||||||
|
func seedingRig(t *testing.T, outs ...runOut) *rig {
|
||||||
|
r := newRig(t, outs...)
|
||||||
|
for i := 0; i < QuietMinutes+1; i++ {
|
||||||
|
r.minute(600_000_000) // 80 Mbit/s: a seed at the line's speed
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSeedingBoxIsWaitingForQuietAndRunNowMeasuresIt(t *testing.T) {
|
||||||
|
r := seedingRig(t, runOut{mbps: 88}, runOut{mbps: 92}, runOut{mbps: 90})
|
||||||
|
r.t.Tick(context.Background())
|
||||||
|
if r.run.calls != 0 {
|
||||||
|
t.Fatal("the weekly Tick tested a seeding box")
|
||||||
|
}
|
||||||
|
if !r.t.WaitingForQuiet() {
|
||||||
|
t.Fatal("a due, eligible, seeding box must report waiting_for_quiet")
|
||||||
|
}
|
||||||
|
var slept []time.Duration
|
||||||
|
r.t.sleep = func(_ context.Context, d time.Duration) bool { slept = append(slept, d); return true }
|
||||||
|
mbps, runs, err := r.t.RunNow(context.Background()) // the controller holds the app: the counter stands still
|
||||||
|
if err != nil || mbps != 92 || len(runs) != RunsPerMeasurement || r.run.calls != RunsPerMeasurement {
|
||||||
|
t.Fatalf("RunNow = %.1f %v %v, calls %d; want the best of 3 = 92", mbps, runs, err, r.run.calls)
|
||||||
|
}
|
||||||
|
want := []time.Duration{RequestSettle, RequestQuietWindow, RequestRunGap, RequestRunGap}
|
||||||
|
if len(slept) != len(want) {
|
||||||
|
t.Fatalf("slept %v, want %v", slept, want)
|
||||||
|
}
|
||||||
|
var total time.Duration
|
||||||
|
for i := range want {
|
||||||
|
if slept[i] != want[i] {
|
||||||
|
t.Fatalf("slept %v, want %v", slept, want)
|
||||||
|
}
|
||||||
|
total += slept[i]
|
||||||
|
}
|
||||||
|
if total+RunsPerMeasurement*TestTime > 60*time.Second {
|
||||||
|
t.Fatalf("a request holds the app %v + runs; it must end inside 60 s", total)
|
||||||
|
}
|
||||||
|
if v, _, ok := r.t.Value(); !ok || v != 92 {
|
||||||
|
t.Fatalf("the requested measurement was not stored: %.1f %v", v, ok)
|
||||||
|
}
|
||||||
|
if r.t.WaitingForQuiet() {
|
||||||
|
t.Fatal("measured this week: no longer waiting")
|
||||||
|
}
|
||||||
|
if _, _, err := r.t.RunNow(context.Background()); refusalCode(err) != "not-due" {
|
||||||
|
t.Fatalf("a second request this week: %v, want not-due", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func refusalCode(err error) string {
|
||||||
|
var ref *Refusal
|
||||||
|
if errors.As(err, &ref) {
|
||||||
|
return ref.Code
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// The held app is not the only uploader: a request still refuses to measure a busy uplink.
|
||||||
|
func TestRunNowRefusesAnUplinkThatIsStillBusy(t *testing.T) {
|
||||||
|
r := seedingRig(t, runOut{mbps: 90}, runOut{mbps: 90}, runOut{mbps: 90})
|
||||||
|
r.t.uplink = func() (uint64, error) { r.tx += 20_000_000; return r.tx, nil } // 20 MB per 10 s = 16 Mbit/s
|
||||||
|
if _, _, err := r.t.RunNow(context.Background()); refusalCode(err) != "not-quiet" || r.run.calls != 0 {
|
||||||
|
t.Fatalf("RunNow on a busy uplink: %v, calls %d; want not-quiet and no run", err, r.run.calls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Every other rule of the weekly test holds for a request.
|
||||||
|
func TestRunNowKeepsTheWeeklyRules(t *testing.T) {
|
||||||
|
r := seedingRig(t)
|
||||||
|
r.gate.busy = "backup:felhom-pbs"
|
||||||
|
if _, _, err := r.t.RunNow(context.Background()); refusalCode(err) != "heavy-op" {
|
||||||
|
t.Fatalf("beside a backup: %v, want heavy-op", err)
|
||||||
|
}
|
||||||
|
r.gate.busy = ""
|
||||||
|
r.tunnel = false
|
||||||
|
if _, _, err := r.t.RunNow(context.Background()); refusalCode(err) != "no-tunnel" {
|
||||||
|
t.Fatalf("no tunnel: %v, want no-tunnel", err)
|
||||||
|
}
|
||||||
|
if r.t.WaitingForQuiet() {
|
||||||
|
t.Fatal("no tunnel: a request could not run, so it must not be invited")
|
||||||
|
}
|
||||||
|
r.tunnel = true
|
||||||
|
r.now = time.Date(2026, 10, 20, 3, 0, 0, 0, time.UTC) // Tuesday 03:00 — the night chain
|
||||||
|
if _, _, err := r.t.RunNow(context.Background()); refusalCode(err) != "outside-window" {
|
||||||
|
t.Fatalf("at night: %v, want outside-window", err)
|
||||||
|
}
|
||||||
|
if r.run.calls != 0 {
|
||||||
|
t.Fatalf("%d runs on refused requests", r.run.calls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A failed request counts as a try, so a controller cannot request more than MaxTriesPerDay tests a day.
|
||||||
|
func TestRunNowFailuresSpendTheDaysTries(t *testing.T) {
|
||||||
|
var outs []runOut
|
||||||
|
for i := 0; i < MaxTriesPerDay*RunsPerMeasurement; i++ {
|
||||||
|
outs = append(outs, runOut{err: errors.New("connection refused")})
|
||||||
|
}
|
||||||
|
r := seedingRig(t, outs...)
|
||||||
|
for i := 0; i < MaxTriesPerDay; i++ {
|
||||||
|
if _, _, err := r.t.RunNow(context.Background()); err == nil || refusalCode(err) != "" {
|
||||||
|
t.Fatalf("try %d: %v, want a failed measurement", i+1, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, _, err := r.t.RunNow(context.Background()); refusalCode(err) != "tries-used" {
|
||||||
|
t.Fatalf("after %d failures: %v, want tries-used", MaxTriesPerDay, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A quiet box tests itself on the weekly Tick: it must not invite the controller to hold its app.
|
||||||
|
func TestQuietBoxIsNotWaiting(t *testing.T) {
|
||||||
|
r := newRig(t)
|
||||||
|
if r.t.WaitingForQuiet() {
|
||||||
|
t.Fatal("ten quiet minutes: the weekly Tick runs; waiting_for_quiet must be false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Live proof 2026-10-24 (demo-hp): a freshly started tester answered waiting_for_quiet before it had watched a single
|
||||||
|
// minute, so a quiet box's torrent app would be held for nothing. Waiting needs the full window, seen busy.
|
||||||
|
func TestFreshTesterIsNotWaiting(t *testing.T) {
|
||||||
|
r := &rig{run: &fakeRunner{}, gate: &fakeGate{}, now: time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC), tunnel: true}
|
||||||
|
r.t = New(r.run, func() (netip.Addr, bool) { return netip.MustParseAddr("10.77.0.1"), r.tunnel }, r.gate,
|
||||||
|
func() (uint64, error) { return r.tx, nil }, "host-x", filepath.Join(t.TempDir(), "line-test.json"), nil)
|
||||||
|
r.t.loc = time.UTC
|
||||||
|
r.t.now = func() time.Time { return r.now }
|
||||||
|
if r.t.WaitingForQuiet() {
|
||||||
|
t.Fatal("no minute watched yet: waiting_for_quiet must be false")
|
||||||
|
}
|
||||||
|
for i := 0; i < QuietMinutes; i++ {
|
||||||
|
r.minute(600_000_000)
|
||||||
|
}
|
||||||
|
if r.t.WaitingForQuiet() {
|
||||||
|
t.Fatal("10 samples are 9 minutes: still not a full window")
|
||||||
|
}
|
||||||
|
r.minute(600_000_000)
|
||||||
|
if !r.t.WaitingForQuiet() {
|
||||||
|
t.Fatal("a full busy window: waiting_for_quiet must be true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
package localapi
|
package localapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.dooplex.hu/admin/felhom-agent/internal/linetest"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LineTestReader is the R-1247 weekly upload line test (internal/linetest.Tester) as the local API reads it.
|
// LineTestReader is the R-1247 weekly upload line test (internal/linetest.Tester) as the local API reads it.
|
||||||
@@ -10,6 +14,13 @@ type LineTestReader interface {
|
|||||||
Value() (mbps float64, at time.Time, ok bool)
|
Value() (mbps float64, at time.Time, ok bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LineTestRequester is the R-1250 half: a due test the uplink never lets start, and a test on request. The Tester
|
||||||
|
// satisfies it; a reader without it answers waiting_for_quiet:false and 503 on the POST.
|
||||||
|
type LineTestRequester interface {
|
||||||
|
WaitingForQuiet() bool
|
||||||
|
RunNow(ctx context.Context) (mbps float64, runs []float64, err error)
|
||||||
|
}
|
||||||
|
|
||||||
// LineTestResponse is GET /host/line-test. Present=false: never measured, or the job is not wired (no tunnel, disabled,
|
// LineTestResponse is GET /host/line-test. Present=false: never measured, or the job is not wired (no tunnel, disabled,
|
||||||
// or an agent before the route answers 404 — the controller reads all three as „unknown").
|
// or an agent before the route answers 404 — the controller reads all three as „unknown").
|
||||||
type LineTestResponse struct {
|
type LineTestResponse struct {
|
||||||
@@ -17,6 +28,19 @@ type LineTestResponse struct {
|
|||||||
Mbps float64 `json:"mbps,omitempty"`
|
Mbps float64 `json:"mbps,omitempty"`
|
||||||
MeasuredAt string `json:"measured_at,omitempty"` // RFC3339 UTC
|
MeasuredAt string `json:"measured_at,omitempty"` // RFC3339 UTC
|
||||||
Method string `json:"method,omitempty"` // "iperf3-tunnel": a 10-second upload to ep0 inside the tunnel
|
Method string `json:"method,omitempty"` // "iperf3-tunnel": a 10-second upload to ep0 inside the tunnel
|
||||||
|
// WaitingForQuiet: a measurement is due and could start now, but the uplink has not been quiet for 10 minutes
|
||||||
|
// (R-1250). The controller with a seeding torrent app then holds its limit and POSTs /host/line-test/run.
|
||||||
|
WaitingForQuiet bool `json:"waiting_for_quiet,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// LineTestRunResponse is POST /host/line-test/run. OK=false with Refused: nothing ran (Refused is linetest.Refusal's
|
||||||
|
// code, or "server-busy"); OK=false without it: the runs failed.
|
||||||
|
type LineTestRunResponse struct {
|
||||||
|
OK bool `json:"ok"`
|
||||||
|
Mbps float64 `json:"mbps,omitempty"`
|
||||||
|
RunsMbps []float64 `json:"runs_mbps,omitempty"`
|
||||||
|
Refused string `json:"refused,omitempty"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLineTest wires the weekly line test (main.go builds it after the local API server).
|
// SetLineTest wires the weekly line test (main.go builds it after the local API server).
|
||||||
@@ -27,11 +51,39 @@ func (s *Server) handleLineTest(w http.ResponseWriter, r *http.Request, vmid int
|
|||||||
writeOK(w, LineTestResponse{Present: false})
|
writeOK(w, LineTestResponse{Present: false})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var waiting bool
|
||||||
|
if q, ok := s.lineTest.(LineTestRequester); ok {
|
||||||
|
waiting = q.WaitingForQuiet()
|
||||||
|
}
|
||||||
mbps, at, ok := s.lineTest.Value()
|
mbps, at, ok := s.lineTest.Value()
|
||||||
if !ok {
|
if !ok {
|
||||||
writeOK(w, LineTestResponse{Present: false})
|
writeOK(w, LineTestResponse{Present: false, WaitingForQuiet: waiting})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.logger.Debug("local-api: /host/line-test served", "vmid", vmid, "mbps", mbps, "measured_at", at.UTC().Format(time.RFC3339))
|
s.logger.Debug("local-api: /host/line-test served", "vmid", vmid, "mbps", mbps, "measured_at", at.UTC().Format(time.RFC3339), "waiting_for_quiet", waiting)
|
||||||
writeOK(w, LineTestResponse{Present: true, Mbps: mbps, MeasuredAt: at.UTC().Format(time.RFC3339), Method: "iperf3-tunnel"})
|
writeOK(w, LineTestResponse{Present: true, Mbps: mbps, MeasuredAt: at.UTC().Format(time.RFC3339), Method: "iperf3-tunnel", WaitingForQuiet: waiting})
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleLineTestRun is POST /host/line-test/run (R-1250): a test on request, synchronous (about 55 s). The answer is
|
||||||
|
// always 200 with the outcome in the body, except 503 when no line test is wired.
|
||||||
|
func (s *Server) handleLineTestRun(w http.ResponseWriter, r *http.Request, vmid int) {
|
||||||
|
q, ok := s.lineTest.(LineTestRequester)
|
||||||
|
if s.lineTest == nil || !ok {
|
||||||
|
writeErr(w, http.StatusServiceUnavailable, "line test not configured")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.logger.Info("local-api: line test requested", "vmid", vmid)
|
||||||
|
mbps, runs, err := q.RunNow(r.Context())
|
||||||
|
var ref *linetest.Refusal
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
writeOK(w, LineTestRunResponse{OK: true, Mbps: mbps, RunsMbps: runs})
|
||||||
|
case errors.As(err, &ref):
|
||||||
|
s.logger.Info("local-api: line test request refused", "vmid", vmid, "reason", ref.Code, "detail", ref.Msg)
|
||||||
|
writeOK(w, LineTestRunResponse{Refused: ref.Code, Error: ref.Msg})
|
||||||
|
case errors.Is(err, linetest.ErrBusy):
|
||||||
|
writeOK(w, LineTestRunResponse{Refused: "server-busy", Error: err.Error()})
|
||||||
|
default:
|
||||||
|
writeOK(w, LineTestRunResponse{Error: err.Error()})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
package localapi
|
package localapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.dooplex.hu/admin/felhom-agent/internal/linetest"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fixedLine struct {
|
type fixedLine struct {
|
||||||
@@ -49,3 +53,64 @@ func TestLineTestRoute(t *testing.T) {
|
|||||||
t.Fatalf("measured: %+v", got)
|
t.Fatalf("measured: %+v", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type requestableLine struct {
|
||||||
|
fixedLine
|
||||||
|
waiting bool
|
||||||
|
mbps float64
|
||||||
|
err error
|
||||||
|
calls int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *requestableLine) WaitingForQuiet() bool { return r.waiting }
|
||||||
|
func (r *requestableLine) RunNow(context.Context) (float64, []float64, error) {
|
||||||
|
r.calls++
|
||||||
|
if r.err != nil {
|
||||||
|
return 0, nil, r.err
|
||||||
|
}
|
||||||
|
return r.mbps, []float64{r.mbps - 1, r.mbps}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func lineTestRun(t *testing.T, s *Server) (int, LineTestRunResponse) {
|
||||||
|
t.Helper()
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
s.handleLineTestRun(rec, httptest.NewRequest("POST", "/host/line-test/run", nil), 9201)
|
||||||
|
var env struct {
|
||||||
|
OK bool `json:"ok"`
|
||||||
|
Data LineTestRunResponse `json:"data"`
|
||||||
|
}
|
||||||
|
_ = json.Unmarshal(rec.Body.Bytes(), &env)
|
||||||
|
return rec.Code, env.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
// R-1250: the controller acts only on waiting_for_quiet, and reads a refusal by its code — never as a measurement.
|
||||||
|
func TestLineTestRunRoute(t *testing.T) {
|
||||||
|
s := &Server{logger: slog.Default()}
|
||||||
|
if code, _ := lineTestRun(t, s); code != 503 {
|
||||||
|
t.Fatalf("not wired: HTTP %d, want 503", code)
|
||||||
|
}
|
||||||
|
s.SetLineTest(fixedLine{ok: true, mbps: 90})
|
||||||
|
if code, _ := lineTestRun(t, s); code != 503 {
|
||||||
|
t.Fatalf("a reader without the request half: HTTP %d, want 503", code)
|
||||||
|
}
|
||||||
|
l := &requestableLine{fixedLine: fixedLine{ok: false}, waiting: true, mbps: 93.5}
|
||||||
|
s.SetLineTest(l)
|
||||||
|
if got := lineTestBody(t, s); got.Present || !got.WaitingForQuiet {
|
||||||
|
t.Fatalf("GET while waiting: %+v, want present:false waiting_for_quiet:true", got)
|
||||||
|
}
|
||||||
|
if code, got := lineTestRun(t, s); code != 200 || !got.OK || got.Mbps != 93.5 || len(got.RunsMbps) != 2 {
|
||||||
|
t.Fatalf("a measured request: HTTP %d %+v", code, got)
|
||||||
|
}
|
||||||
|
l.err = &linetest.Refusal{Code: "not-quiet", Msg: "the uplink sent 16.0 Mbit/s while held"}
|
||||||
|
if _, got := lineTestRun(t, s); got.OK || got.Refused != "not-quiet" || got.Mbps != 0 {
|
||||||
|
t.Fatalf("a refusal: %+v, want ok:false refused:not-quiet", got)
|
||||||
|
}
|
||||||
|
l.err = linetest.ErrBusy
|
||||||
|
if _, got := lineTestRun(t, s); got.OK || got.Refused != "server-busy" {
|
||||||
|
t.Fatalf("server busy: %+v, want refused:server-busy", got)
|
||||||
|
}
|
||||||
|
l.err = errors.New("dial tcp 10.77.0.1:5201: i/o timeout")
|
||||||
|
if _, got := lineTestRun(t, s); got.OK || got.Refused != "" || got.Error == "" {
|
||||||
|
t.Fatalf("a failed run: %+v, want ok:false, no refusal, the error", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -577,6 +577,7 @@ func (s *Server) Handler() http.Handler {
|
|||||||
// R-1247: the weekly upload line test's last measurement — the controller's qBittorrent limit and upload watch
|
// R-1247: the weekly upload line test's last measurement — the controller's qBittorrent limit and upload watch
|
||||||
// read it. Read-only; never measured (or the job not wired) = present:false.
|
// read it. Read-only; never measured (or the job not wired) = present:false.
|
||||||
mux.HandleFunc("GET /host/line-test", s.withGuest(s.handleLineTest))
|
mux.HandleFunc("GET /host/line-test", s.withGuest(s.handleLineTest))
|
||||||
|
mux.HandleFunc("POST /host/line-test/run", s.withGuest(s.handleLineTestRun)) // R-1250: a test on request
|
||||||
// Disk management (slice 8C) — self-scoped; format routes through the data-bearing classifier+gate.
|
// Disk management (slice 8C) — self-scoped; format routes through the data-bearing classifier+gate.
|
||||||
mux.HandleFunc("GET /disks", s.withGuest(s.handleDisks))
|
mux.HandleFunc("GET /disks", s.withGuest(s.handleDisks))
|
||||||
mux.HandleFunc("GET /disks/candidates", s.withGuest(s.handleDiskCandidates))
|
mux.HandleFunc("GET /disks/candidates", s.withGuest(s.handleDiskCandidates))
|
||||||
|
|||||||
Reference in New Issue
Block a user