Compare commits
10 Commits
main
...
fix/askpas
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2b1d30b19 | ||
|
|
51edaeb2a6 | ||
|
|
f2e48a7568 | ||
|
|
5cd15ca82f | ||
|
|
388a4183c6 | ||
|
|
6d1c72c488 | ||
|
|
f76d10b745 | ||
|
|
d18389c4a5 | ||
|
|
1cea423073 | ||
|
|
f40d1045ef |
@ -22,8 +22,13 @@ All notable changes to Raven are documented here.
|
||||
logins (`~/.raven/keys/<id>.password`, readable by the owner alone), handed
|
||||
to ssh on every connection through its askpass hook, and never on a command
|
||||
line, in a log or in an answer; such a machine gets no ssh alias and no jump
|
||||
host, its password answers the registered account's own password prompt and
|
||||
no other, Oncall campaigns run on it as Raven's own connections do and at
|
||||
host, its password answers the registered account's own password prompt (at
|
||||
the address as ssh resolves it, so a `Host` alias from the owner's ssh config
|
||||
works too, though not one Raven wrote for another machine: such a pair is
|
||||
refused at registration in either order and again on connecting, and a
|
||||
password connection never canonicalizes the host)
|
||||
and no other, Oncall campaigns run on it as Raven's own
|
||||
connections do and at
|
||||
the registered address alone, and the mode is not offered on Windows. A failed
|
||||
or cancelled registration preserves the password written by another pending
|
||||
registration of the same machine, including when both passwords are identical.
|
||||
|
||||
@ -442,8 +442,12 @@ key, gets no ssh alias, no jump host and no shared connection, and is refused on
|
||||
whose OpenSSH cannot run Raven's POSIX askpass helper; a password under six characters is
|
||||
refused, since the scrubber would pass it as ordinary text, one with a line break too, since
|
||||
ssh would take only its first line, and the id rule is the one owner `connections.password_path`;
|
||||
the askpass helper answers the registered account's own password prompt (`user@host`, named by
|
||||
`transport.ASKPASS_FOR_ENV`) and no other, another account's, a second factor's and a one-time
|
||||
the askpass helper answers the registered account's own password prompt (`user@host`, the host as
|
||||
`ssh -G` resolves it -- the `HostKeyAlias`, else the `HostName` in canonical form -- named by
|
||||
`transport.ASKPASS_FOR_ENV`; a host that is the id of a `Host` alias Raven keeps for a key machine,
|
||||
`transport.ALIAS_MARK`, is refused rather than followed, at registration in either order by
|
||||
`connection_add.alias_collision` and again on connecting; one reader, `transport.resolve_with_ssh`,
|
||||
serves both this and a key machine's defaults, and a password connection never canonicalizes) and no other, another account's, a second factor's and a one-time
|
||||
password's included; Oncall's ssh and rsync log in to such a row the same way, at the registry's
|
||||
address alone, `oncall_flow.transport.password_row`), install the node in the background, doctor,
|
||||
and remove (`raven ops connection remove`, refused while a connected agent entry names the
|
||||
|
||||
@ -263,7 +263,9 @@ async def take(
|
||||
return []
|
||||
cdir = Path(cdir).expanduser()
|
||||
if runner is None:
|
||||
runner = _runner(meta)
|
||||
# Off the loop, like the commands below: a password machine's runner
|
||||
# asks a local ``ssh -G`` which host its prompt will name.
|
||||
runner = await asyncio.to_thread(_runner, meta)
|
||||
if runner is None:
|
||||
return [_record(cdir, r, when, trial, error="the campaign's machine could not be reached") for r in due]
|
||||
|
||||
|
||||
@ -17,6 +17,7 @@ oncall_flow.tune`` (D5).
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import dataclasses as _dataclasses
|
||||
import os
|
||||
from datetime import datetime
|
||||
@ -1155,7 +1156,7 @@ class OpsTuneStatusTool(Tool):
|
||||
try:
|
||||
from oncall_flow.instrument import log_event
|
||||
|
||||
backend = billing_only(backend_from_meta(meta), led)
|
||||
backend = billing_only(await asyncio.to_thread(backend_from_meta, meta), led)
|
||||
try:
|
||||
budget_spent = await backend.spent_minutes()
|
||||
budget_remaining = await backend.remaining_minutes()
|
||||
@ -2157,10 +2158,12 @@ class OpsSubmitTool(_OpsScheduler):
|
||||
# from there on every round, because the wake message that carries it is
|
||||
# written by a turn that no longer has the task statement in front of it.
|
||||
objective = objective or str(meta.get("objective_words") or "") or metric
|
||||
# Off the loop, like every backend built here: a password machine's
|
||||
# runner asks a local ``ssh -G`` which host its prompt will name.
|
||||
if round == 0:
|
||||
prepare_from_meta(meta, app_dir=app_dir)
|
||||
await asyncio.to_thread(prepare_from_meta, meta, app_dir=app_dir)
|
||||
|
||||
backend = billing_only(backend_from_meta(meta), Ledger(ledger))
|
||||
backend = billing_only(await asyncio.to_thread(backend_from_meta, meta), Ledger(ledger))
|
||||
|
||||
# The apparatus, checked against what this campaign was set up with. The
|
||||
# first submit is where the baseline is taken -- it is the last moment the
|
||||
@ -2514,10 +2517,9 @@ class OpsSubmitTool(_OpsScheduler):
|
||||
f"with ops_note rather than acting twice."
|
||||
)
|
||||
try:
|
||||
runner = runner_from(_resolve_meta(meta), what="campaign", cap_seconds=120.0)
|
||||
runner = await asyncio.to_thread(runner_from, _resolve_meta(meta), what="campaign", cap_seconds=120.0)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
return f"Could not reach the campaign's machine to act: {exc}. Nothing was done."
|
||||
import asyncio
|
||||
|
||||
led.record(key, campaign=campaign, config={"action": name, "values": dict(values or {})})
|
||||
try:
|
||||
@ -2786,7 +2788,8 @@ class OpsCampaignsTool(Tool):
|
||||
from oncall_flow.backends import backend_from_meta, billing_only
|
||||
from oncall_flow.ledger import Ledger
|
||||
|
||||
spent = await billing_only(backend_from_meta(meta), Ledger(d / "ledger.json")).spent_minutes()
|
||||
backend = await asyncio.to_thread(backend_from_meta, meta)
|
||||
spent = await billing_only(backend, Ledger(d / "ledger.json")).spent_minutes()
|
||||
total = (meta.get("budget") or {}).get("total")
|
||||
unit = (meta.get("budget") or {}).get("unit") or ""
|
||||
bits.append(f"spent {spent:.2f}" + (f" of {total} {unit}" if total else ""))
|
||||
@ -2949,7 +2952,7 @@ class OpsKillTool(Tool):
|
||||
if refusal:
|
||||
return refusal
|
||||
meta = _json.loads(meta_path.read_text(encoding="utf-8"))
|
||||
backend = billing_only(backend_from_meta(meta), Ledger(ledger_path))
|
||||
backend = billing_only(await asyncio.to_thread(backend_from_meta, meta), Ledger(ledger_path))
|
||||
led = Ledger(ledger_path)
|
||||
killed, skipped, notes = [], [], []
|
||||
for key in trials:
|
||||
|
||||
@ -34,6 +34,7 @@ syntax only.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json as _json
|
||||
import shlex
|
||||
from pathlib import Path
|
||||
@ -185,7 +186,7 @@ class OpsEditCaseDictTool(Tool):
|
||||
) -> str:
|
||||
from oncall_flow.instrument import log_event
|
||||
|
||||
resolved = _campaign(campaign, ledger)
|
||||
resolved = await asyncio.to_thread(_campaign, campaign, ledger)
|
||||
if isinstance(resolved, str):
|
||||
return resolved
|
||||
backend, led, cdir, _meta = resolved # _meta carries the solver env path
|
||||
@ -303,7 +304,7 @@ class OpsCaseChangesTool(Tool):
|
||||
}
|
||||
|
||||
async def execute(self, campaign: str, trial: str | None = None, ledger: str | None = None, **kwargs: Any) -> str:
|
||||
resolved = _campaign(campaign, ledger)
|
||||
resolved = await asyncio.to_thread(_campaign, campaign, ledger)
|
||||
if isinstance(resolved, str):
|
||||
return resolved
|
||||
backend, led, _cdir, meta = resolved
|
||||
|
||||
@ -16,6 +16,7 @@ them are healthy, is the judgement under measurement.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json as _json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
@ -69,7 +70,7 @@ class OpsOutputsTool(Tool):
|
||||
}
|
||||
|
||||
async def execute(self, campaign: str, trial: str, ledger: str | None = None, **kwargs: Any) -> str:
|
||||
resolved = _campaign_backend(campaign, ledger)
|
||||
resolved = await asyncio.to_thread(_campaign_backend, campaign, ledger)
|
||||
if isinstance(resolved, str):
|
||||
return resolved
|
||||
backend, led, _ = resolved
|
||||
|
||||
@ -82,10 +82,25 @@ What gets recorded, and where:
|
||||
the jump's own ssh would be handed this machine's password; a machine
|
||||
reached only through one has to be registered with a key. The helper
|
||||
answers one prompt: ssh's own, for the password of the registered account
|
||||
at the registered address (`user@host`). Another account's prompt, a
|
||||
second factor's (a verification code, a passcode) and a one-time
|
||||
password's get no answer, so a machine whose login also asks for one is
|
||||
registered with a key as well. The row carries `auth: password` and no key
|
||||
at the registered address (`user@host`), named as ssh resolves it -- the
|
||||
`HostName` or `HostKeyAlias` your ssh config gives an alias, an address in
|
||||
its canonical form -- so the address can be an alias from your ssh config.
|
||||
Not one of the `Host <id>` aliases Raven itself writes for key machines,
|
||||
though: a password machine whose address is such an id would log in at that
|
||||
other machine's address, so it is refused by name and its password is not
|
||||
sent. Registration refuses the pair in either order -- a password machine at
|
||||
a key machine's id, or a key machine whose id is a password machine's
|
||||
address -- and connecting refuses it again for a pair registered before.
|
||||
A password connection also turns off `CanonicalizeHostname`, so it reaches
|
||||
the host `ssh -G` names rather than one a DNS lookup picks later; a machine
|
||||
that relied on canonicalization is registered at its full name. When ssh
|
||||
cannot say how it resolves the address, the helper waits for the registered
|
||||
spelling, and a refusal then says the resolution failed.
|
||||
Another account's prompt, a second factor's (a verification code, a
|
||||
passcode) and a one-time password's get no answer, so a machine whose login
|
||||
also asks for one is registered with a key as well; a registration refused
|
||||
that way says the password was not sent. The row carries `auth: password`
|
||||
and no key
|
||||
path. `raven ops connection doctor` counts the row usable, and Oncall
|
||||
campaigns run on it too, logging in the same way at the registered address
|
||||
alone: a campaign cannot point such a connection at another host, port or
|
||||
|
||||
@ -61,8 +61,15 @@ raven ops connection add --id gpu1 --name "GPU box 1" \
|
||||
askpass)也会走助手而不是在终端上要密码。跳板机不行:密码连接关掉了 `ProxyJump` 和
|
||||
`ProxyCommand`,因为跳板自己的 ssh 会拿到这台机器的密码;只能经跳板到达的机器请用
|
||||
密钥登记。助手只回答一种提示:ssh 自己向登记的账号、登记的地址(`user@host`)索要密码的
|
||||
那一句。别的账号的提示、第二重验证的提示(验证码、passcode)、一次性密码的提示都拿不到
|
||||
回答,所以登录时还要第二重验证的机器也请用密钥登记。登记表里这一行记 `auth: password`,
|
||||
那一句,地址按 ssh 解析后的写法来认(别名取你 ssh 配置里的 `HostName` 或 `HostKeyAlias`,
|
||||
IP 地址取规范写法),所以地址也可以填 ssh 配置里的别名。但不能是 Raven 自己为密钥机器写的
|
||||
`Host <id>` 别名:地址写成这样一个 id 的密码机器会登录到另一台机器的地址上,所以会被点名拒绝,
|
||||
密码不会发出去。登记时两种顺序都会被拒绝(密码机器的地址是某台密钥机器的 id,或者密钥机器的
|
||||
id 是某台密码机器的地址),登记早于这项检查的此类组合在连接时仍会被拒绝。密码连接还会关掉
|
||||
`CanonicalizeHostname`,所以登录的就是 `ssh -G` 给出的主机,而不是之后 DNS 查询改出来的那台;依赖
|
||||
这项设置的机器请用完整主机名登记。ssh 说不出地址怎么解析时,助手按登记的写法等提示,被拒时会说明是解析失败。别的账号的提示、第二重验证的提示
|
||||
(验证码、passcode)、一次性密码的提示都拿不到回答,所以登录时还要第二重验证的机器也请用
|
||||
密钥登记;这样被拒的登记会说明密码没有发出去。登记表里这一行记 `auth: password`,
|
||||
没有密钥路径。`raven ops connection doctor` 把它算作可用,Oncall 的 campaign 也能在上面跑,
|
||||
登录方式相同,而且只登录到登记的地址:campaign 不能把这样一条连接指到别的主机、端口或
|
||||
用户名上。机器的 sshd 必须允许
|
||||
|
||||
@ -5312,6 +5312,7 @@
|
||||
"gui.machine.file_problems": {"en": "The registry file", "zh": "登记表文件"},
|
||||
"gui.machine.why_unreachable": {"en": "The machine could not be reached: check the address and port, and that the machine is on and reachable from this computer.", "zh": "连不上这台机器:检查地址和端口,确认机器开着、从这台电脑能连到它。"},
|
||||
"gui.machine.why_login": {"en": "The machine refused the login. With a key: check the user name and the private key path, and that the key's public half is on the machine. With a password: check it, and that the machine accepts password logins at all.", "zh": "机器拒绝了登录。用密钥时:检查用户名和私钥路径,确认对应的公钥已经放到机器上。用密码时:检查密码,并确认这台机器允许密码登录。"},
|
||||
"gui.machine.why_prompt": {"en": "The machine asked something the kept password does not answer -- a one-time code, another account's password, or a password prompt that names a host other than the registered one -- so the password was not sent: see the line below.", "zh": "机器问的不是这个账号自己的密码提示(比如一次性验证码、别的账号的密码,或者提示里的主机不是登记的那台),所以没有发送密码:详见下一行。"},
|
||||
"gui.machine.why_hostkey": {"en": "The machine's host key differs from the one on record: check the machine before trusting it again, then take the old line out of ~/.ssh/known_hosts.", "zh": "机器的主机密钥和记录的不一样:先确认机器没问题,再把 ~/.ssh/known_hosts 里的旧记录删掉。"},
|
||||
"gui.machine.why_timeout": {"en": "The machine did not answer in time: check that it is on and that the port is open.", "zh": "机器没有按时应答:确认机器开着、端口放行。"},
|
||||
"gui.machine.why_nossh": {"en": "ssh is not installed on this computer: install OpenSSH, then try again.", "zh": "这台电脑没有 ssh:装好 OpenSSH 再试。"},
|
||||
|
||||
@ -45,7 +45,16 @@ from typing import Any
|
||||
from urllib.parse import urlsplit, urlunsplit
|
||||
|
||||
from raven.ops import connections, remote_path
|
||||
from raven.ops.transport import TIMED_OUT_RC, TransportError, runner_from, ssh_argv, ssh_env, ssh_target
|
||||
from raven.ops.transport import (
|
||||
ASKPASS_REFUSED,
|
||||
ASKPASS_UNRESOLVED,
|
||||
TIMED_OUT_RC,
|
||||
TransportError,
|
||||
runner_from,
|
||||
ssh_argv,
|
||||
ssh_env,
|
||||
ssh_target,
|
||||
)
|
||||
|
||||
#: Where an agent works on its machine when its entry names nowhere else
|
||||
#: (:data:`raven.ops.remote_path.DEFAULT_ROOT`, which the file tools read from
|
||||
@ -432,7 +441,9 @@ async def tunnel(
|
||||
raise RemoteMachineError(f"machine {target.label}: the endpoint's URL names no host to reach from it")
|
||||
host, port, key, user = _target(target)
|
||||
password = connections.password_auth(target._row)
|
||||
env = {**os.environ, **ssh_environment(target)} if password else None
|
||||
# Off the loop: the environment asks a local ``ssh -G`` which host the
|
||||
# password prompt will name (``transport.ssh_env``).
|
||||
env = {**os.environ, **(await asyncio.to_thread(ssh_environment, target))} if password else None
|
||||
# An IPv6 address is bracketed in the forward as it is in the URL, which
|
||||
# ``hostname`` gives bare; ssh refuses the bare one.
|
||||
far = f"[{parts.hostname}]" if ":" in parts.hostname else parts.hostname
|
||||
@ -580,6 +591,22 @@ def explain(target: Machine, rc: int, output: str) -> str | None:
|
||||
return agent_missing(target)
|
||||
if rc != SSH_FAILED_RC:
|
||||
return None
|
||||
if connections.password_auth(target._row) and ASKPASS_REFUSED in output:
|
||||
# The helper gave ssh nothing, so the refusal after it is no verdict
|
||||
# on the password (``connection_add.probe`` reads it the same way).
|
||||
unresolved = next((line for line in output.splitlines() if line.startswith(ASKPASS_UNRESOLVED)), "")
|
||||
if unresolved:
|
||||
why = unresolved[len(ASKPASS_UNRESOLVED) :].split(", so it waited", 1)[0].strip(" ()")
|
||||
return (
|
||||
f"machine {target.label} was not sent its password: ssh could not say which host its password "
|
||||
f"prompt names ({why}), and the prompt it asked for did not name the registered address; check that "
|
||||
"ssh -G for that address works on this computer"
|
||||
)
|
||||
return (
|
||||
f"machine {target.label} asked something other than the password prompt the password kept for it "
|
||||
"answers (a one-time code, another account's password, or a question worded another way), so the "
|
||||
"password was not sent"
|
||||
)
|
||||
for pattern, said in _SSH_FAILURES:
|
||||
if pattern.search(output):
|
||||
if said is _LOGIN_REFUSED and connections.password_auth(target._row):
|
||||
|
||||
@ -116,6 +116,9 @@ class ConnectionAddTool(Tool):
|
||||
kind = connections.LOCAL if str(transport).strip().lower() == connections.LOCAL else connections.SSH
|
||||
conn_id = str(id).strip() or mint_slug(label)
|
||||
row: dict[str, Any] = {"id": conn_id, "display_name": label, "transport": kind}
|
||||
# Asked before anything is resolved or reached; the registry write asks again.
|
||||
if collision := adder.alias_collision(row, connections.load()):
|
||||
return f"REFUSED: {collision}. Nothing was written."
|
||||
if kind == connections.LOCAL:
|
||||
reached, message, found = await asyncio.to_thread(adder.probe, row)
|
||||
else:
|
||||
|
||||
@ -456,8 +456,10 @@ async def run_on_machine(command: str, *, connection: str, cwd: str | None = Non
|
||||
|
||||
# The runner's own cap is a backstop behind the wrapper's: the wrapper
|
||||
# fires first, keeps the output and reports 124; the runner's group kill is
|
||||
# for a wrapper that could not run at all (a machine with no bash).
|
||||
runner, row = _runner_for_connection(connection, cap_seconds=_TIMEOUT_S + 15)
|
||||
# for a wrapper that could not run at all (a machine with no bash). Built
|
||||
# off the loop, like the run below: a password machine's runner asks a
|
||||
# local ``ssh -G`` which host its prompt will name (``transport.ssh_env``).
|
||||
runner, row = await asyncio.to_thread(_runner_for_connection, connection, cap_seconds=_TIMEOUT_S + 15)
|
||||
if runner is None:
|
||||
return row # the message explaining why not
|
||||
|
||||
|
||||
@ -38,6 +38,7 @@ from rich.table import Table
|
||||
# ``add`` reads them as its own globals -- which is also what a test that
|
||||
# stands in a reachable machine patches.
|
||||
from raven.ops.connection_add import NODE_FIELD, probe
|
||||
from raven.ops.connection_add import alias_collision as _alias_collision
|
||||
from raven.ops.connection_add import forget_password as _forget_password
|
||||
from raven.ops.connection_add import forget_unowned_password as _forget_unowned_password
|
||||
from raven.ops.connection_add import parse_probe as _parse_probe # noqa: F401 -- re-export, the tests read it here
|
||||
@ -186,6 +187,9 @@ def add( # noqa: PLR0913 -- one option per field of the row; a dict would hide
|
||||
f"(raven ops connection remove {conn_id}). Nothing was written.[/red]"
|
||||
)
|
||||
raise typer.Exit(1)
|
||||
if collision := _alias_collision(row, list(registry.rows)):
|
||||
console.print(f"[red]{collision}. Nothing was written.[/red]")
|
||||
raise typer.Exit(1)
|
||||
|
||||
kept = None
|
||||
written = None
|
||||
|
||||
@ -27,7 +27,6 @@ from __future__ import annotations
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
@ -37,7 +36,7 @@ from typing import Any
|
||||
# and a test that stands a scratch registry in patches it there.
|
||||
from raven.ops import connections
|
||||
from raven.ops.connections import LOCAL, SSH, UNREADABLE, password_auth, password_path, transport_of
|
||||
from raven.ops.transport import TransportError
|
||||
from raven.ops.transport import ALIAS_MARK, ASKPASS_REFUSED, ASKPASS_UNRESOLVED, TransportError
|
||||
from raven.utils.atomic_io import atomic_replace, write_transaction
|
||||
|
||||
# What the owner has to be asked for, when the registry lists nothing that
|
||||
@ -83,24 +82,23 @@ def ssh_defaults(host: str, *, port: int = 0, user: str = "") -> dict[str, Any]:
|
||||
included -- so an owner who left the port, the user or the key path out
|
||||
gets the values their own ssh would pick, not a guess. ``keys`` lists only
|
||||
identity files that exist here, in ssh's own order; the caller probes each
|
||||
and keeps the one that connects. Empty when ssh is not on this computer.
|
||||
and keeps the one that connects. Empty when ssh is not on this computer,
|
||||
or cannot say: the same reader a password machine's prompt is resolved by
|
||||
(``transport.resolve_with_ssh``), with plain ssh's command line.
|
||||
"""
|
||||
argv = ["ssh", "-G"] + (["-p", str(port)] if port else []) + [f"{user}@{host}" if user else host]
|
||||
try:
|
||||
done = subprocess.run(argv, capture_output=True, text=True, timeout=10, check=False)
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired):
|
||||
return {}
|
||||
if done.returncode != 0:
|
||||
from raven.ops.transport import resolve_with_ssh
|
||||
|
||||
resolved = resolve_with_ssh(["ssh"] + (["-p", str(port)] if port else []) + [f"{user}@{host}" if user else host])
|
||||
if resolved.failure:
|
||||
return {}
|
||||
out: dict[str, Any] = {"keys": []}
|
||||
for line in done.stdout.splitlines():
|
||||
key, _, value = line.partition(" ")
|
||||
if key == "port" and value.strip().isdigit():
|
||||
out["port"] = int(value)
|
||||
elif key == "user" and value.strip():
|
||||
out["user"] = value.strip()
|
||||
elif key == "identityfile" and value.strip() and Path(os.path.expanduser(value.strip())).exists():
|
||||
out["keys"].append(value.strip())
|
||||
if resolved.first("port").isdigit():
|
||||
out["port"] = int(resolved.first("port"))
|
||||
if resolved.first("user"):
|
||||
out["user"] = resolved.first("user")
|
||||
for path in resolved.values.get("identityfile", ()):
|
||||
if path and Path(os.path.expanduser(path)).exists():
|
||||
out["keys"].append(path)
|
||||
return out
|
||||
|
||||
|
||||
@ -196,7 +194,25 @@ def probe(
|
||||
return False, f"could not reach {where}: no answer within {timeout:.0f}s", {}
|
||||
if code != 0:
|
||||
said = out.strip() or f"exit {code}"
|
||||
if password_auth(row) and re.search(r"permission denied|password|authentication", said, re.IGNORECASE):
|
||||
if password_auth(row) and ASKPASS_UNRESOLVED in said:
|
||||
# The helper waited for the row's spelling because ssh could not
|
||||
# resolve the host; the machine's prompt may name the host ssh
|
||||
# did resolve when it connected, so this is no other question.
|
||||
said += (
|
||||
"\nRaven could not learn from ssh which host this machine's password prompt names (above), so the "
|
||||
"kept password waited for the prompt at the registered address, and this prompt was not that one, so "
|
||||
"it was not sent. Check that `ssh -G` for this address works on this computer, or register the "
|
||||
"machine at the address ssh logs in to."
|
||||
)
|
||||
elif password_auth(row) and ASKPASS_REFUSED in said:
|
||||
# The helper gave ssh nothing, so the refusal that follows is no
|
||||
# verdict on the password: the machine asked another question.
|
||||
said += (
|
||||
"\nssh asked something other than this account's password (a one-time code, another account's "
|
||||
"password, or a question worded another way); the kept password answers only its own prompt, so it "
|
||||
"was not sent. A machine that asks that is registered with a key."
|
||||
)
|
||||
elif password_auth(row) and re.search(r"permission denied|password|authentication", said, re.IGNORECASE):
|
||||
# ssh says only that the login was refused; the other cause is a
|
||||
# sshd that takes no password at all, which looks the same.
|
||||
said += (
|
||||
@ -353,6 +369,55 @@ def parse_probe(out: str) -> dict[str, Any]:
|
||||
return found
|
||||
|
||||
|
||||
def alias_collision(row: dict[str, Any], rows: list[dict[str, Any]]) -> str:
|
||||
"""Why registering ``row`` beside ``rows`` would let a ``Host`` alias Raven writes decide where a password
|
||||
machine logs in; empty when it would not.
|
||||
|
||||
Raven writes ``Host <id>`` for every key machine (:func:`write_ssh_alias`),
|
||||
and ssh follows it for any connection to that name, case-blind. A password
|
||||
machine registered at such a name would log in at the key machine's
|
||||
address, with its password (reviewed 2026-10-20). Both orders are refused:
|
||||
a key machine whose id is a registered password machine's address, and a
|
||||
password machine at a registered key machine's id or at an alias Raven
|
||||
keeps with no machine left behind it. Connecting refuses the pair again by
|
||||
name (``transport.ssh_env``), which is what still covers a pair written
|
||||
before this check existed.
|
||||
"""
|
||||
from raven.ops.transport import managed_aliases
|
||||
|
||||
if transport_of(row) == LOCAL:
|
||||
return ""
|
||||
conn_id = str(row.get("id") or "").strip()
|
||||
if not password_auth(row):
|
||||
for other in rows:
|
||||
if transport_of(other) == SSH and password_auth(other):
|
||||
host = str(other.get("host") or "").strip()
|
||||
if host.lower() == conn_id.lower():
|
||||
return (
|
||||
f"id {conn_id!r} is the address password machine {str(other.get('id') or '?')!r} is "
|
||||
f"registered at ({host!r}): the ssh alias Raven writes for this id would take that "
|
||||
"machine's logins, and its password, to this machine. Pick another id"
|
||||
)
|
||||
return ""
|
||||
host = str(row.get("host") or "").strip()
|
||||
for other in rows:
|
||||
other_id = str(other.get("id") or "").strip()
|
||||
if transport_of(other) == SSH and not password_auth(other) and other_id.lower() == host.lower():
|
||||
return (
|
||||
f"machine {conn_id!r} would log in at {host!r}, the id of key machine {other_id!r}: the ssh alias "
|
||||
f"Raven keeps for {other_id!r} would take this machine's logins, and its password, to "
|
||||
f"{other_id!r}'s address. Register {conn_id!r} at its own address, or remove {other_id!r} first"
|
||||
)
|
||||
if host.lower() in managed_aliases():
|
||||
return (
|
||||
f"machine {conn_id!r} would log in at {host!r}, the ssh alias Raven keeps in ~/.ssh/config for a "
|
||||
f"machine {host.lower()!r} that is no longer registered: ssh would follow it to that address. Take "
|
||||
f"the '{ALIAS_MARK.format(conn_id=host.lower())}' block out of ~/.ssh/config, or register "
|
||||
f"{conn_id!r} at its own address"
|
||||
)
|
||||
return ""
|
||||
|
||||
|
||||
def write(row: dict[str, Any], *, password: str | None = None) -> Path:
|
||||
"""Append one machine, keeping whatever shape the file already had.
|
||||
|
||||
@ -377,6 +442,10 @@ def write(row: dict[str, Any], *, password: str | None = None) -> Path:
|
||||
rows = list(found.rows)
|
||||
if any(str(r.get("id")).strip() == row["id"] for r in rows):
|
||||
raise ValueError(f"a machine with id {row['id']!r} is already listed in {path}")
|
||||
# Judged here too, under the registry's lock: the callers ask before
|
||||
# they reach the machine, and a registration can land in between.
|
||||
if collision := alias_collision(row, rows):
|
||||
raise ValueError(collision)
|
||||
rows.append(row)
|
||||
payload = json.dumps({"connections": rows}, indent=2, ensure_ascii=False) + "\n"
|
||||
if password is None:
|
||||
@ -427,8 +496,6 @@ def set_fields(conn_id: str, fields: dict[str, Any]) -> Path:
|
||||
#: reaching it.
|
||||
NODE_FIELD = "node_install"
|
||||
|
||||
ALIAS_MARK = "# raven connection {conn_id} (managed; rewritten on every add)"
|
||||
|
||||
|
||||
def write_ssh_alias(row: dict[str, Any]) -> str | None:
|
||||
"""Keep a ``Host <id>`` alias in ``~/.ssh/config`` for an ssh row.
|
||||
|
||||
@ -15,7 +15,9 @@ own filesystem.
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import dataclasses
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import signal
|
||||
import subprocess
|
||||
@ -179,12 +181,28 @@ PASSWORD_AUTH = (
|
||||
"ProxyJump=none",
|
||||
"-o",
|
||||
"ProxyCommand=none",
|
||||
# ``ssh -G`` makes none of the DNS lookups canonicalization makes, and a
|
||||
# connection that canonicalizes re-reads the config under the new name --
|
||||
# where a ``Host <id>`` block Raven wrote for another machine can apply.
|
||||
# Off, the connection reaches the host ``ssh -G`` named, the one the
|
||||
# helper's prompt is for (reviewed 2026-10-20).
|
||||
"-o",
|
||||
"CanonicalizeHostname=no",
|
||||
)
|
||||
|
||||
#: The variable the askpass helper reads the password file's path from.
|
||||
ASKPASS_FILE_ENV = "RAVEN_MACHINE_PASSWORD_FILE"
|
||||
#: The variable that tells the helper whose prompt it may answer: the registered ``user@host``.
|
||||
#: The variable that tells the helper whose prompt it may answer: the registered account, at the host ssh resolves.
|
||||
ASKPASS_FOR_ENV = "RAVEN_MACHINE_PASSWORD_FOR"
|
||||
#: What the helper writes on ssh's stderr when the question is not that prompt, so it gave ssh nothing.
|
||||
ASKPASS_REFUSED = (
|
||||
"raven: the password kept for this machine answers its own password prompt only, and this prompt is not one"
|
||||
)
|
||||
#: The variable that tells the helper why the prompt it waits for names the row's own spelling: ssh could not
|
||||
#: say how it resolves the host. Empty when it could.
|
||||
ASKPASS_UNRESOLVED_ENV = "RAVEN_MACHINE_PROMPT_UNRESOLVED"
|
||||
#: What the helper adds to that refusal when the variable is set, with the reason and the prompt it waited for.
|
||||
ASKPASS_UNRESOLVED = "raven: ssh could not say which host this machine's password prompt names"
|
||||
|
||||
# The helper itself: OpenSSH runs it when ``SSH_ASKPASS_REQUIRE=force`` and
|
||||
# reads the password from its stdout. It prints the one line of the file the
|
||||
@ -197,7 +215,9 @@ ASKPASS_FOR_ENV = "RAVEN_MACHINE_PASSWORD_FOR"
|
||||
# verification code, a passcode) and a one-time password's -- "password" is
|
||||
# in that wording too -- get nothing, since the password is the machine's
|
||||
# alone and a PAM module may pass what it is given on to its vendor. The
|
||||
# question is ssh's first argument; trailing blanks and case are ignored.
|
||||
# question is ssh's first argument; trailing blanks and case are ignored. A
|
||||
# refusal says so on stderr, and also why the prompt it waited for is the
|
||||
# row's own spelling when ssh could not resolve the host.
|
||||
_ASKPASS = (
|
||||
"#!/bin/sh\n"
|
||||
"# Raven: hands ssh the password kept for a registered machine, at that account's own password prompt only.\n"
|
||||
@ -207,7 +227,9 @@ _ASKPASS = (
|
||||
'case "$asked" in\n'
|
||||
f' "$who\'s password:"|"($who) password:") exec cat "${ASKPASS_FILE_ENV}" ;;\n'
|
||||
"esac\n"
|
||||
"echo 'raven: the password kept for this machine answers its own password prompt only, and this prompt is not one' >&2\n"
|
||||
f"echo '{ASKPASS_REFUSED}' >&2\n"
|
||||
f"[ -n \"${ASKPASS_UNRESOLVED_ENV}\" ] && printf '%s (%s), so it waited for the prompt of %s\\n' "
|
||||
f'"{ASKPASS_UNRESOLVED}" "${ASKPASS_UNRESOLVED_ENV}" "${ASKPASS_FOR_ENV}" >&2\n'
|
||||
"exit 1\n"
|
||||
)
|
||||
|
||||
@ -317,18 +339,150 @@ def ssh_env(row: dict[str, Any], *, password_file: Path | None = None) -> dict[s
|
||||
raise TransportError(
|
||||
f"no password is kept here for machine {label!r} ({secret}); remove the machine and register it again"
|
||||
)
|
||||
host, _port, _key, user = ssh_target(row)
|
||||
host, port, _key, user = ssh_target(row)
|
||||
if host.lower() in managed_aliases():
|
||||
# ssh would follow the block Raven wrote for that other machine, and
|
||||
# the helper would then wait for that machine's prompt: the password
|
||||
# goes only where the owner registered it, whatever ssh resolves.
|
||||
raise TransportError(
|
||||
f"machine {label!r} is registered at {host!r}, which is also the ssh alias Raven keeps in "
|
||||
f"~/.ssh/config for machine {host.lower()!r}: ssh would log in at that machine's address, so the "
|
||||
f"password kept for {label!r} is not sent; remove one of the two machines, or register {label!r} "
|
||||
"again at its own address"
|
||||
)
|
||||
target, unresolved = _prompt_target(host, port, user)
|
||||
return {
|
||||
"SSH_ASKPASS": str(askpass_helper(secret.parent)),
|
||||
"SSH_ASKPASS_REQUIRE": "force",
|
||||
ASKPASS_FILE_ENV: str(secret),
|
||||
# Whose prompt the helper answers: the account and address the row
|
||||
# registers, which is also the destination on the command line.
|
||||
ASKPASS_FOR_ENV: f"{user}@{host}",
|
||||
ASKPASS_FOR_ENV: target,
|
||||
ASKPASS_UNRESOLVED_ENV: unresolved,
|
||||
"DISPLAY": os.environ.get("DISPLAY") or "raven-askpass",
|
||||
}
|
||||
|
||||
|
||||
#: The line before and after each ``Host <id>`` block Raven keeps in ``~/.ssh/config`` for a key machine
|
||||
#: (``connection_add.write_ssh_alias``).
|
||||
ALIAS_MARK = "# raven connection {conn_id} (managed; rewritten on every add)"
|
||||
_ALIAS_MARK_LINE = re.compile(
|
||||
"^" + re.escape(ALIAS_MARK).replace(re.escape("{conn_id}"), r"(\S+)") + r"[ \t]*\r?$", re.MULTILINE
|
||||
)
|
||||
|
||||
|
||||
def passwd_home() -> Path | None:
|
||||
"""The home ssh reads ``~/.ssh/config`` under: the account's passwd entry, which ``$HOME`` does not move."""
|
||||
try:
|
||||
import pwd
|
||||
|
||||
return Path(pwd.getpwuid(os.getuid()).pw_dir)
|
||||
except (ImportError, KeyError):
|
||||
return None
|
||||
|
||||
|
||||
def managed_aliases() -> set[str]:
|
||||
"""The ids, lowercased, of the ``Host`` blocks Raven keeps in the ssh config ssh reads or the one Raven writes.
|
||||
|
||||
A mark counts on its own: a block the owner cut short is still not theirs.
|
||||
A file that cannot be read adds nothing: ssh, run as the same account,
|
||||
skips a user config it cannot open, so it follows none of its blocks.
|
||||
"""
|
||||
homes = {Path(os.path.expanduser("~")), passwd_home()}
|
||||
found: set[str] = set()
|
||||
for home in homes - {None}:
|
||||
try:
|
||||
text = (home / ".ssh" / "config").read_text(encoding="utf-8", errors="replace")
|
||||
except OSError:
|
||||
continue
|
||||
found.update(conn_id.lower() for conn_id in _ALIAS_MARK_LINE.findall(text))
|
||||
return found
|
||||
|
||||
|
||||
#: How long ``ssh -G`` may take to say how it resolves a host (config, and DNS
|
||||
#: when the owner's config canonicalizes) before the row's spelling stands.
|
||||
_RESOLVE_TIMEOUT_S = 10.0
|
||||
|
||||
|
||||
def _prompt_target(host: str, port: int, user: str) -> tuple[str, str]:
|
||||
"""The ``user@host`` ssh's password prompt names for this destination, and why not when ssh could not say.
|
||||
|
||||
The first is whose prompt the helper answers.
|
||||
|
||||
ssh names the host it resolved, not the one on its command line: the
|
||||
``HostName`` the owner's ssh config gives an alias, the canonical form of
|
||||
a numeric address (``::1`` for ``0:0:0:0:0:0:0:1``), or the
|
||||
``HostKeyAlias`` in place of either. A helper waiting for the row's own
|
||||
spelling refused every such machine, and its registration then blamed the
|
||||
password (reviewed 2026-10-20). ``ssh -G`` prints that resolution without
|
||||
connecting, and is asked with the password connection's own options. The
|
||||
row's spelling when ssh cannot say, which sends the password nowhere else;
|
||||
the reason then goes to the helper (:data:`ASKPASS_UNRESOLVED_ENV`), whose
|
||||
refusal would otherwise read as a machine asking another question.
|
||||
|
||||
Asked here, before the connection, never by the helper: a helper that ran
|
||||
``ssh -G`` itself recursed into thousands of processes against a stand-in
|
||||
ssh that called askpass (2026-10-19). Nothing this call starts finds an
|
||||
askpass to call either (:func:`resolve_with_ssh`).
|
||||
"""
|
||||
resolved = resolve_with_ssh(ssh_argv(host, port, "", user=user, password=True))
|
||||
if resolved.failure:
|
||||
return f"{user}@{host}", resolved.failure
|
||||
named = resolved.first("hostkeyalias") or resolved.first("hostname")
|
||||
return (f"{user}@{named}", "") if named else (f"{user}@{host}", "ssh -G named no host")
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class SshResolution:
|
||||
"""What ``ssh -G`` printed for one command line: each key's values in ssh's order, or why there are none."""
|
||||
|
||||
values: dict[str, tuple[str, ...]]
|
||||
#: Empty when ssh answered; otherwise what went wrong, in words a refusal can quote.
|
||||
failure: str
|
||||
|
||||
def first(self, key: str) -> str:
|
||||
return next(iter(self.values.get(key, ())), "")
|
||||
|
||||
|
||||
def resolve_with_ssh(argv: list[str]) -> SshResolution:
|
||||
"""The configuration ssh resolves for ``argv`` -- an ssh command line up to its destination -- without connecting.
|
||||
|
||||
The one reader of ``ssh -G``: a password machine's prompt
|
||||
(:func:`_prompt_target`) and a key machine's registration defaults
|
||||
(``connection_add.ssh_defaults``) both resolve here, each with its own
|
||||
command line. Two readers disagreed (reviewed 2026-10-20): the key one
|
||||
raised on a byte that is not UTF-8 and on an ssh it could not start, and
|
||||
handed its ``ssh -G`` the caller's terminal and askpass. Here nothing the
|
||||
call starts finds an askpass or a terminal to read, a value keeps every
|
||||
occurrence (``identityfile`` repeats, in the order ssh offers them), and
|
||||
a failure is a reason rather than an exception.
|
||||
"""
|
||||
probe = [argv[0], "-G", *argv[1:]]
|
||||
env = {**os.environ, "SSH_ASKPASS": "", "SSH_ASKPASS_REQUIRE": "never"}
|
||||
try:
|
||||
# ``errors``: ssh prints config values as they are on disk, in no
|
||||
# promised encoding, and one undecodable byte must not cost the resolution.
|
||||
done = subprocess.run(
|
||||
probe,
|
||||
stdin=subprocess.DEVNULL,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
errors="replace",
|
||||
timeout=_RESOLVE_TIMEOUT_S,
|
||||
env=env,
|
||||
)
|
||||
except OSError:
|
||||
return SshResolution({}, "ssh -G could not be started")
|
||||
except subprocess.TimeoutExpired:
|
||||
return SshResolution({}, f"ssh -G took over {_RESOLVE_TIMEOUT_S:g} s")
|
||||
if done.returncode != 0:
|
||||
return SshResolution({}, f"ssh -G exited {done.returncode}")
|
||||
values: dict[str, list[str]] = {}
|
||||
for line in done.stdout.splitlines():
|
||||
key, _, value = line.partition(" ")
|
||||
if key:
|
||||
values.setdefault(key, []).append(value.strip())
|
||||
return SshResolution({key: tuple(found) for key, found in values.items()}, "")
|
||||
|
||||
|
||||
def make_ssh_runner(
|
||||
host: str,
|
||||
port: int,
|
||||
|
||||
@ -48,6 +48,7 @@ from raven.node.client import MachineStatus, NodeError, get_node_pool
|
||||
from raven.ops import connection_add, connections
|
||||
from raven.ops.connection_add import NODE_FIELD
|
||||
from raven.ops.connections import SSH, UNREADABLE, password_auth, row_problems, transport_of
|
||||
from raven.ops.transport import ASKPASS_REFUSED
|
||||
from raven.rpc.errors import ConfigValidationError
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@ -214,6 +215,10 @@ async def machines_list(params: dict) -> dict:
|
||||
# language; the sentence itself stays, for the reader who wants ssh's words.
|
||||
_REASONS = (
|
||||
(re.compile(r"ssh(-keygen)? not found on this computer", re.IGNORECASE), "nossh"),
|
||||
# Before ``login``: the askpass helper declined the prompt, so the refusal
|
||||
# ssh reports after it says nothing about the password.
|
||||
(re.compile(re.escape(ASKPASS_REFUSED)), "prompt"),
|
||||
(re.compile(r"which is also the ssh alias Raven keeps"), "invalid"),
|
||||
(re.compile(r"REMOTE HOST IDENTIFICATION HAS CHANGED|Host key verification failed", re.IGNORECASE), "hostkey"),
|
||||
(re.compile(r"Permission denied|Too many authentication failures|publickey", re.IGNORECASE), "login"),
|
||||
(re.compile(r"no answer within|timed out", re.IGNORECASE), "timeout"),
|
||||
@ -283,6 +288,8 @@ async def machines_add(params: dict) -> dict:
|
||||
return _refused(f"{found.detail} Give those entries an id (or remove them) first.", "registry")
|
||||
if any(str(r.get("id") or "").strip() == conn_id for r in found.rows):
|
||||
return _refused(f"a machine with id {conn_id!r} is registered already", "duplicate")
|
||||
if collision := connection_add.alias_collision(row, list(found.rows)):
|
||||
return _refused(collision, "invalid")
|
||||
kept = None
|
||||
if by_password:
|
||||
# Kept before the probe, which reaches the machine the way every later
|
||||
|
||||
@ -3100,7 +3100,17 @@ class MachinesAddResult(_Strict):
|
||||
ok: bool
|
||||
detail: str
|
||||
reason: Literal[
|
||||
"", "unreachable", "login", "hostkey", "timeout", "nossh", "platform", "duplicate", "invalid", "registry"
|
||||
"",
|
||||
"unreachable",
|
||||
"login",
|
||||
"prompt",
|
||||
"hostkey",
|
||||
"timeout",
|
||||
"nossh",
|
||||
"platform",
|
||||
"duplicate",
|
||||
"invalid",
|
||||
"registry",
|
||||
]
|
||||
row: MachineRow | None = None
|
||||
|
||||
|
||||
@ -3233,6 +3233,7 @@
|
||||
"",
|
||||
"unreachable",
|
||||
"login",
|
||||
"prompt",
|
||||
"hostkey",
|
||||
"timeout",
|
||||
"nossh",
|
||||
@ -3241,7 +3242,7 @@
|
||||
"invalid",
|
||||
"registry"
|
||||
],
|
||||
"description": "Why the row was not written, as a kind the page can explain: the machine could not be reached, refused the login, presented a changed host key, did not answer in time, ssh is missing here, this computer cannot use the login picked (a password on Windows), the id is taken, the fields are not usable, or the registry file cannot be changed. Empty when it was written."
|
||||
"description": "Why the row was not written, as a kind the page can explain: the machine could not be reached, refused the login, asked a question the kept password does not answer (so the password was not sent), presented a changed host key, did not answer in time, ssh is missing here, this computer cannot use the login picked (a password on Windows), the id is taken, the fields are not usable, or the registry file cannot be changed. Empty when it was written."
|
||||
},
|
||||
"row": {
|
||||
"$ref": "#/components/schemas/MachineRow"
|
||||
|
||||
@ -6,7 +6,9 @@ Raven reads a home two ways: through ``Path.home`` / ``os.path.expanduser``
|
||||
first way reads ``USERPROFILE`` and ignores ``HOME`` (``ntpath.expanduser``,
|
||||
since Python 3.8), so a test that moves only ``HOME`` keeps writing into
|
||||
the profile of whoever runs it -- on a native Windows run, the operator's
|
||||
own ``~/.ssh/config``. :func:`point_home` moves both.
|
||||
own ``~/.ssh/config``. :func:`point_home` moves both, and the third reader
|
||||
too: the account's passwd home, where ssh finds ``~/.ssh/config`` and which
|
||||
:func:`raven.ops.transport.passwd_home` reads for the aliases Raven keeps there.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@ -20,6 +22,7 @@ def point_home(monkeypatch: pytest.MonkeyPatch, path: Path | str) -> Path:
|
||||
"""Make ``path`` the home every reader sees, for the test that holds ``monkeypatch``."""
|
||||
monkeypatch.setenv("HOME", str(path))
|
||||
monkeypatch.setenv("USERPROFILE", str(path))
|
||||
monkeypatch.setattr("raven.ops.transport.passwd_home", lambda: Path(path))
|
||||
return Path(path)
|
||||
|
||||
|
||||
|
||||
@ -1218,3 +1218,28 @@ def _this_process_goes_on() -> Iterator[None]:
|
||||
remote = sys.modules.get("raven.acp_client.remote")
|
||||
if remote is not None:
|
||||
remote.staying()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def no_real_ssh_g(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Fail a test that runs the computer's own ``ssh -G``.
|
||||
|
||||
It reads the ssh config of whoever runs the suite -- found through the
|
||||
passwd entry, which the suite's ``HOME`` does not move -- and a ``Match
|
||||
exec`` there runs. A stand-in under the test's own directory, or an ``-F``
|
||||
naming the test's config, is what a test resolves with; with no ssh on
|
||||
PATH at all nothing runs, and the lookup's own error stands. Used by the
|
||||
modules whose code asks ``ssh -G`` (``transport.resolve_with_ssh``).
|
||||
"""
|
||||
import subprocess
|
||||
|
||||
real = subprocess.run
|
||||
|
||||
def run(argv, *args, **kwargs):
|
||||
if isinstance(argv, list) and argv[:1] == ["ssh"] and "-G" in argv and "-F" not in argv:
|
||||
found = shutil.which("ssh", path=(kwargs.get("env") or os.environ).get("PATH"))
|
||||
if found is not None and not Path(found).resolve().is_relative_to(tmp_path.resolve()):
|
||||
raise AssertionError(f"the real ssh -G ran against the owner's own config: {found}")
|
||||
return real(argv, *args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(subprocess, "run", run)
|
||||
|
||||
@ -20,6 +20,8 @@ import pytest
|
||||
from raven.acp_client import remote
|
||||
from raven.acp_client.remote import Machine, RemoteMachineError
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("no_real_ssh_g")
|
||||
|
||||
HOST = "203.0.113.7"
|
||||
PORT = 58717
|
||||
KEY = "/keys/id_raven_test"
|
||||
@ -574,6 +576,15 @@ def _pw() -> Machine:
|
||||
return Machine(id="box", display_name="Lab box", _row=dict(PASSWORD_ROW))
|
||||
|
||||
|
||||
def _resolving_ssh(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""An ssh first on PATH that answers ``-G`` with the row's address, as a machine with no config would."""
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir(exist_ok=True)
|
||||
(bin_dir / "ssh").write_text(f'#!/bin/sh\n[ "$1" = -G ] && echo "hostname {HOST}"\nexit 0\n', encoding="utf-8")
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
|
||||
|
||||
def test_the_ssh_command_for_a_password_machine_offers_no_key_and_the_link_gets_the_askpass_environment(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, supported_host
|
||||
):
|
||||
@ -581,6 +592,7 @@ def test_the_ssh_command_for_a_password_machine_offers_no_key_and_the_link_gets_
|
||||
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("box", "hunter2")
|
||||
_resolving_ssh(tmp_path, monkeypatch)
|
||||
|
||||
argv = remote.ssh_command(_pw(), "node --stdio", ssh_log=tmp_path / "box.log")
|
||||
|
||||
@ -612,6 +624,9 @@ def test_push_hands_ssh_the_askpass_environment_for_a_password_machine_and_nothi
|
||||
seen: list[tuple[list[str], dict | None, bool]] = []
|
||||
|
||||
def run(argv, **kwargs):
|
||||
if "-G" in argv:
|
||||
# How ssh resolves the host for the helper's prompt: a config dump, not a connection.
|
||||
return subprocess.CompletedProcess(argv, 0, "hostname 203.0.113.7\n", "")
|
||||
seen.append((argv, kwargs.get("env"), kwargs.get("start_new_session", False)))
|
||||
return subprocess.CompletedProcess(argv, 0, b"ok", b"")
|
||||
|
||||
@ -636,6 +651,7 @@ async def test_the_tunnel_hands_ssh_the_askpass_environment(
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
monkeypatch.setattr("raven.config.paths.get_logs_dir", lambda: tmp_path / "logs")
|
||||
store_password("box", "hunter2")
|
||||
_resolving_ssh(tmp_path, monkeypatch)
|
||||
spawned: list[dict] = []
|
||||
|
||||
class _Proc:
|
||||
@ -671,6 +687,109 @@ async def test_the_tunnel_hands_ssh_the_askpass_environment(
|
||||
assert one["session"] is True, "no controlling terminal for the tunnel's ssh either"
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="the stand-in ssh is a POSIX script")
|
||||
async def test_the_tunnel_asks_ssh_whose_prompt_to_answer_off_the_event_loop(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, supported_host
|
||||
):
|
||||
"""The askpass environment runs ``ssh -G`` to learn which host ssh's prompt will name. On the loop, a
|
||||
resolution waiting on DNS or a slow ``Match exec`` would hold every other turn the loop serves."""
|
||||
import asyncio
|
||||
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
monkeypatch.setattr("raven.config.paths.get_logs_dir", lambda: tmp_path / "logs")
|
||||
store_password("box", "hunter2")
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
(bin_dir / "ssh").write_text(f'#!/bin/sh\nsleep 1\necho "hostname {HOST}"\n', encoding="utf-8")
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
|
||||
class _Proc:
|
||||
returncode = None
|
||||
|
||||
async def wait(self):
|
||||
self.returncode = 0
|
||||
return 0
|
||||
|
||||
def terminate(self):
|
||||
self.returncode = 0
|
||||
|
||||
def kill(self):
|
||||
self.returncode = -9
|
||||
|
||||
async def spawn(*argv, **kwargs):
|
||||
return _Proc()
|
||||
|
||||
async def up(*args, **kwargs):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(remote.asyncio, "create_subprocess_exec", spawn)
|
||||
monkeypatch.setattr(remote, "_tunnel_up", up)
|
||||
ticks = 0
|
||||
|
||||
async def tick():
|
||||
nonlocal ticks
|
||||
while True:
|
||||
await asyncio.sleep(0.05)
|
||||
ticks += 1
|
||||
|
||||
ticker = asyncio.create_task(tick())
|
||||
try:
|
||||
async with remote.tunnel(_pw(), "http://127.0.0.1:8000/v1", ssh_log=tmp_path / "tunnel.log"):
|
||||
pass
|
||||
finally:
|
||||
ticker.cancel()
|
||||
|
||||
assert ticks >= 5, f"the loop ran {ticks} ticks of 50 ms while ssh -G took a second"
|
||||
|
||||
|
||||
def test_a_prompt_the_kept_password_does_not_answer_is_not_blamed_on_the_password():
|
||||
"""The helper gave ssh nothing, so the refusal that follows says nothing about the password; telling the
|
||||
owner to check it sent them after a fault it did not have (reviewed 2026-10-20)."""
|
||||
from raven.ops.transport import ASKPASS_REFUSED
|
||||
|
||||
refused = f"{ASKPASS_REFUSED}\nalice@203.0.113.7: Permission denied (keyboard-interactive)."
|
||||
|
||||
said = remote.explain(_pw(), remote.SSH_FAILED_RC, refused)
|
||||
|
||||
assert said is not None and "check the password" not in said and "was not sent" in said
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="the askpass helper and the stand-in ssh are POSIX scripts")
|
||||
def test_a_host_ssh_could_not_resolve_is_named_when_the_helper_then_gives_the_prompt_nothing(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, supported_host
|
||||
):
|
||||
"""With ``ssh -G`` failing the helper waits for the row's own spelling; when the machine's prompt names the
|
||||
host ssh did resolve, the helper declines. Saying the machine asked another question, and to use a key,
|
||||
sent the owner the wrong way: the failed resolution is what is named."""
|
||||
from raven.ops import transport
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
(bin_dir / "ssh").write_text("#!/bin/sh\necho 'ssh: Could not resolve hostname' >&2; exit 255\n")
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}:/usr/bin:/bin")
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("box", "hunter2")
|
||||
env = transport.ssh_env({**PASSWORD_ROW, "host": "ownerbox"})
|
||||
helper = subprocess.run(
|
||||
[env["SSH_ASKPASS"], "worker@192.0.2.8's password: "],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={**os.environ, **env},
|
||||
timeout=30,
|
||||
)
|
||||
assert (helper.returncode, helper.stdout) == (1, "")
|
||||
|
||||
said = remote.explain(_pw(), remote.SSH_FAILED_RC, helper.stderr + "worker@192.0.2.8: Permission denied.\n")
|
||||
|
||||
assert said is not None and "check the password" not in said and "a key" not in said
|
||||
assert "could not say which host" in said and "exited 255" in said
|
||||
|
||||
|
||||
def test_a_refused_login_names_the_password_for_a_password_machine_and_the_key_for_a_key_machine():
|
||||
out = "worker@203.0.113.7: Permission denied (publickey,password)."
|
||||
|
||||
|
||||
@ -298,3 +298,29 @@ async def test_an_action_of_none_is_pointed_at_the_door_that_already_exists(camp
|
||||
|
||||
assert "ops_check_later" in out
|
||||
assert not (campaign / "ledger.json").exists(), "no trial, no action, no record of one"
|
||||
|
||||
|
||||
async def test_an_action_s_runner_is_built_off_the_loop(campaign, monkeypatch) -> None:
|
||||
"""A password machine's runner asks a local ``ssh -G`` which host its prompt names; built on the loop, that
|
||||
wait held every other turn (reviewed 2026-10-20). Only the command was run in a thread."""
|
||||
import asyncio
|
||||
|
||||
from oncall_flow import transport
|
||||
|
||||
where: list[bool] = []
|
||||
built = transport.runner_from
|
||||
|
||||
def runner_from(meta, **kw):
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
where.append(True)
|
||||
except RuntimeError:
|
||||
where.append(False)
|
||||
return built(meta, **kw)
|
||||
|
||||
monkeypatch.setattr(transport, "runner_from", runner_from)
|
||||
|
||||
out = await _act(campaign, action="note_it", values={})
|
||||
|
||||
assert "REFUSED" not in out
|
||||
assert where == [False], "the runner was built on the event loop"
|
||||
|
||||
@ -112,6 +112,36 @@ def _spend(monkeypatch, minutes=115.47):
|
||||
monkeypatch.setattr("oncall_flow.backends.backend_from_meta", lambda meta: _Backend())
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_live_campaign_s_backend_is_built_off_the_loop(monkeypatch, tmp_path):
|
||||
"""A password machine's runner asks a local ``ssh -G`` which host its prompt names; built on the loop for
|
||||
every live campaign listed, that wait held every other turn (reviewed 2026-10-20)."""
|
||||
import asyncio
|
||||
|
||||
home = _home(monkeypatch, tmp_path)
|
||||
_campaign(home, "dambreak-legA", case="/home/cfd/work/case_legA", trials=2)
|
||||
where: list[bool] = []
|
||||
|
||||
class _Backend:
|
||||
async def spent_minutes(self):
|
||||
return 1.0
|
||||
|
||||
def build(meta):
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
where.append(True)
|
||||
except RuntimeError:
|
||||
where.append(False)
|
||||
return _Backend()
|
||||
|
||||
monkeypatch.setattr("oncall_flow.backends.backend_from_meta", build)
|
||||
|
||||
out = await OpsCampaignsTool().execute()
|
||||
|
||||
assert "spent 1.00" in out
|
||||
assert where == [False], "the backend was built on the event loop"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_it_lists_every_campaign_with_what_it_is(monkeypatch, tmp_path):
|
||||
home = _home(monkeypatch, tmp_path)
|
||||
|
||||
@ -185,6 +185,33 @@ def _with_reference(tmp_path, body: str, monkeypatch):
|
||||
return cdir, backend
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reading_the_case_s_changes_builds_its_backend_off_the_loop(tmp_path, monkeypatch):
|
||||
"""A password machine's runner asks a local ``ssh -G`` which host its prompt names; built on the loop, that
|
||||
wait held every other turn (reviewed 2026-10-20)."""
|
||||
import asyncio
|
||||
|
||||
from oncall_flow.tools.ops_case_dict import OpsCaseChangesTool
|
||||
|
||||
cdir, backend = _with_reference(tmp_path, DIFF, monkeypatch)
|
||||
where: list[bool] = []
|
||||
|
||||
def build(meta):
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
where.append(True)
|
||||
except RuntimeError:
|
||||
where.append(False)
|
||||
return backend
|
||||
|
||||
monkeypatch.setattr("oncall_flow.backends.backend_from_meta", build)
|
||||
|
||||
out = await OpsCaseChangesTool().execute(campaign="c", ledger=str(cdir / "ledger.json"))
|
||||
|
||||
assert REF in out
|
||||
assert where == [False], "the backend was built on the event loop"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_it_reports_the_one_entry_that_moved(tmp_path, monkeypatch):
|
||||
"""Seven watch rounds read the settings one at a time and none noticed the
|
||||
|
||||
@ -28,6 +28,8 @@ from oncall_flow.tools import base as tools_base # noqa: E402
|
||||
from raven.ops import connections as trunk # noqa: E402
|
||||
from raven.ops.transport import PASSWORD_AUTH # noqa: E402
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("no_real_ssh_g")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _campaign_root(tmp_path):
|
||||
@ -247,9 +249,14 @@ PASSWORD_ROW = {
|
||||
|
||||
# Stands in for ssh and rsync alike: one JSON line a call saying what it was
|
||||
# handed, whether it has a session of its own, and -- as ssh, under
|
||||
# SSH_ASKPASS_REQUIRE=force -- what the askpass helper answered.
|
||||
# SSH_ASKPASS_REQUIRE=force -- what the askpass helper answered. ssh's ``-G``,
|
||||
# the config dump a password login reads first, names the host as given and
|
||||
# is no call.
|
||||
_STAND_IN = """#!{python}
|
||||
import json, os, subprocess, sys
|
||||
if os.path.basename(sys.argv[0]) == "ssh" and "-G" in sys.argv[1:]:
|
||||
print("hostname " + sys.argv[-1].rpartition("@")[2])
|
||||
sys.exit(0)
|
||||
saw = {{
|
||||
"name": os.path.basename(sys.argv[0]),
|
||||
"argv": sys.argv[1:],
|
||||
@ -385,3 +392,58 @@ def test_a_docker_campaign_on_a_key_machine_is_reached_as_it_always_was(store, t
|
||||
"root@14.103.100.27:/srv/ops/app",
|
||||
]
|
||||
assert sync["require"] == "" and not sync["own_session"]
|
||||
|
||||
|
||||
def _password_campaign(tmp_path: Path) -> Path:
|
||||
"""A docker campaign on the password machine, one trial running: what ops_tune_status builds a backend for."""
|
||||
cdir = tmp_path / "ops" / "lab-run"
|
||||
cdir.mkdir(parents=True)
|
||||
meta = {"connection": "conn_lab", "remote_dir": "/srv/ops", "budget": {"unit": "core-minute", "total": 60}}
|
||||
(cdir / "meta.json").write_text(json.dumps(meta), encoding="utf-8")
|
||||
trial = {
|
||||
"idem_key": "t1",
|
||||
"status": "running",
|
||||
"campaign": "lab-run",
|
||||
"handle": {"backend": "docker", "job_id": "ops-t1"},
|
||||
"result": None,
|
||||
"attempts": 0,
|
||||
"escalated": False,
|
||||
}
|
||||
(cdir / "ledger.json").write_text(json.dumps({"version": 1, "records": {"t1": trial}}), encoding="utf-8")
|
||||
return cdir
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="password machines are refused on Windows; the stand-ins are POSIX")
|
||||
async def test_the_status_of_a_password_machine_campaign_leaves_the_loop_free_while_ssh_resolves_the_host(
|
||||
password_machine, tmp_path, monkeypatch
|
||||
):
|
||||
"""A password machine's runner asks a local ``ssh -G`` which host its prompt names. Built on the loop, one
|
||||
status call held every other turn for as long as that took: 2.06 s for a 2 s resolution (reviewed
|
||||
2026-10-20), and up to the 10 s cap per build with a slow ``Match exec``."""
|
||||
from oncall_flow.tools.ops import OpsTuneStatusTool
|
||||
|
||||
tools_base.set_home(tmp_path / "ops")
|
||||
cdir = _password_campaign(tmp_path)
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
(bin_dir / "ssh").write_text(
|
||||
'#!/bin/sh\nif [ "$1" = -G ]; then sleep 1; echo "hostname 203.0.113.7"; fi\nexit 0\n', encoding="utf-8"
|
||||
)
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
ticks = 0
|
||||
|
||||
async def tick():
|
||||
nonlocal ticks
|
||||
while True:
|
||||
await asyncio.sleep(0.05)
|
||||
ticks += 1
|
||||
|
||||
ticker = asyncio.create_task(tick())
|
||||
try:
|
||||
out = await OpsTuneStatusTool().execute(ledger=str(cdir / "ledger.json"))
|
||||
finally:
|
||||
ticker.cancel()
|
||||
|
||||
assert "t1" in out
|
||||
assert ticks >= 5, f"the loop ran {ticks} ticks of 50 ms while ssh -G took a second"
|
||||
|
||||
@ -121,6 +121,31 @@ async def test_outputs_prints_the_whole_result_even_with_no_metrics(monkeypatch)
|
||||
assert '"metrics": {}' in out
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_outputs_builds_its_backend_off_the_loop(monkeypatch):
|
||||
"""A password machine's runner asks a local ``ssh -G`` which host its prompt names; built on the loop, that
|
||||
wait held every other turn (reviewed 2026-10-20)."""
|
||||
import asyncio
|
||||
|
||||
backend = FakeBackend(TIMESTEP, JobResult(JobStatus.SUCCEEDED, metrics={}, output={"cores": 4}))
|
||||
where: list[bool] = []
|
||||
|
||||
def resolved(campaign, ledger):
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
where.append(True)
|
||||
except RuntimeError:
|
||||
where.append(False)
|
||||
return backend, FakeLedger(["t1"]), None
|
||||
|
||||
monkeypatch.setattr(mod, "_campaign_backend", resolved)
|
||||
|
||||
out = await OpsOutputsTool().execute(campaign="c", trial="t1")
|
||||
|
||||
assert '"cores": 4' in out
|
||||
assert where == [False], "the backend was built on the event loop"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("tool", [OpsOutputsTool()])
|
||||
def test_descriptions_suggest_no_verdict_and_no_action(tool):
|
||||
text = tool.description.lower()
|
||||
|
||||
@ -372,3 +372,27 @@ async def test_check_later_refuses_a_concluded_campaign(home) -> None:
|
||||
text = getattr(out, "model_text", None) or str(out)
|
||||
assert "REFUSED" in text and "concluded" in text.lower()
|
||||
assert scheduled == []
|
||||
|
||||
|
||||
async def test_a_reading_s_runner_is_built_off_the_loop(home, monkeypatch) -> None:
|
||||
"""A password machine's runner asks a local ``ssh -G`` which host its prompt names; built on the loop, that
|
||||
wait held every other turn (reviewed 2026-10-20). Only the commands were run in a thread."""
|
||||
import asyncio
|
||||
|
||||
where: list[bool] = []
|
||||
built = r._runner
|
||||
|
||||
def runner(meta):
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
where.append(True)
|
||||
except RuntimeError:
|
||||
where.append(False)
|
||||
return built(meta)
|
||||
|
||||
monkeypatch.setattr(r, "_runner", runner)
|
||||
|
||||
out = await _declare(readings=[{"name": "free", "command": "echo 41.5", "when": "at_declare"}])
|
||||
|
||||
assert "-> now 41.5" in out
|
||||
assert where == [False], "the runner was built on the event loop"
|
||||
|
||||
@ -6,6 +6,8 @@ import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
REPO = Path(__file__).resolve().parent.parent
|
||||
PLUGIN_DIR = REPO / "agents" / "raven-oncall" / "plugins" / "oncall-flow"
|
||||
sys.path.insert(0, str(PLUGIN_DIR))
|
||||
@ -25,6 +27,8 @@ from oncall_flow.runner import ( # noqa: E402
|
||||
|
||||
from raven.ops.transport import PASSWORD_AUTH # noqa: E402
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("no_real_ssh_g")
|
||||
|
||||
|
||||
class _FakeRun:
|
||||
def __init__(self, image_present: bool) -> None:
|
||||
|
||||
@ -130,6 +130,49 @@ async def test_submit_records_ledger_and_schedules_wake(monkeypatch, tmp_path: P
|
||||
assert "Submitted 1 job(s)" in out and "Scheduled a wake" in out
|
||||
|
||||
|
||||
async def test_round_zero_sets_up_and_builds_its_backend_off_the_loop(monkeypatch, tmp_path: Path) -> None:
|
||||
"""A password machine's setup and runner ask a local ``ssh -G`` which host its prompt names; built on the
|
||||
loop, that wait held every other turn (reviewed 2026-10-20)."""
|
||||
from oncall_flow import backends
|
||||
|
||||
_patch_remote(monkeypatch)
|
||||
where: list[tuple[str, bool]] = []
|
||||
for name in ("prepare_from_meta", "backend_from_meta"):
|
||||
real = getattr(backends, name)
|
||||
|
||||
def recorded(*a, _real=real, _name=name, **k):
|
||||
where.append((_name, _on_the_loop()))
|
||||
return _real(*a, **k)
|
||||
|
||||
monkeypatch.setattr(backends, name, recorded)
|
||||
tool = _bound(OpsSubmitTool(), _FakeCron())
|
||||
tool.set_context("cli", "direct")
|
||||
|
||||
out = await tool.execute(
|
||||
host="1.2.3.4",
|
||||
configs=[{"k1": 1.5}],
|
||||
objective="max ndcg",
|
||||
ledger=str(tmp_path / "l.json"),
|
||||
eta_seconds=120,
|
||||
round=0,
|
||||
metric="ndcg",
|
||||
campaign="bm25",
|
||||
)
|
||||
|
||||
assert "Submitted 1 job(s)" in out
|
||||
assert where == [("prepare_from_meta", False), ("backend_from_meta", False)]
|
||||
|
||||
|
||||
def _on_the_loop() -> bool:
|
||||
import asyncio
|
||||
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
async def test_submit_batch_runs_all_configs(monkeypatch, tmp_path: Path) -> None:
|
||||
_patch_remote(monkeypatch)
|
||||
tool = _bound(OpsSubmitTool(), _FakeCron())
|
||||
|
||||
@ -243,6 +243,36 @@ async def test_kill_without_a_fresh_observation_is_refused(tmp_path: Path) -> No
|
||||
assert "basis_refused" in [e["kind"] for e in read_events(cdir)]
|
||||
|
||||
|
||||
async def test_kill_builds_its_backend_off_the_loop(tmp_path: Path) -> None:
|
||||
"""A password machine's runner asks a local ``ssh -G`` which host its prompt names; built on the loop, that
|
||||
wait held every other turn (reviewed 2026-10-20)."""
|
||||
import asyncio
|
||||
|
||||
cdir = _campaign(tmp_path)
|
||||
backend = _Backend()
|
||||
where: list[bool] = []
|
||||
|
||||
def build(meta):
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
where.append(True)
|
||||
except RuntimeError:
|
||||
where.append(False)
|
||||
return backend
|
||||
|
||||
register_backend("mock", build)
|
||||
led = Ledger(cdir / "ledger.json")
|
||||
led.record("t1", campaign="camp-a")
|
||||
led.set_handle("t1", JobHandle("mock", "job-1"))
|
||||
led.set_status("t1", JobStatus.RUNNING)
|
||||
_fresh_probe(cdir)
|
||||
|
||||
out = await OpsKillTool().execute(trials=["t1"], campaign="camp-a", basis="looked again; diverging")
|
||||
|
||||
assert out.startswith("Killed 1 trial(s): t1")
|
||||
assert where == [False], "the backend was built on the event loop"
|
||||
|
||||
|
||||
async def test_kill_with_a_fresh_observation_cancels_and_records(tmp_path: Path) -> None:
|
||||
cdir = _campaign(tmp_path)
|
||||
backend = _Backend()
|
||||
|
||||
@ -32,6 +32,8 @@ from oncall_flow.transport import (
|
||||
|
||||
from raven.ops.connections import password_path # noqa: E402
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("no_real_ssh_g")
|
||||
|
||||
|
||||
def test_a_campaign_that_says_nothing_is_still_reached_over_ssh():
|
||||
# Every campaign written before this seam existed says nothing, and has to go
|
||||
@ -105,9 +107,14 @@ PASSWORD_ROW = {
|
||||
}
|
||||
|
||||
# An ssh that logs what it was handed, one JSON line a call, and asks the
|
||||
# askpass helper the way OpenSSH does under SSH_ASKPASS_REQUIRE=force.
|
||||
# askpass helper the way OpenSSH does under SSH_ASKPASS_REQUIRE=force. Its
|
||||
# ``-G``, the config dump a password login reads first, names the host as
|
||||
# given and is no call.
|
||||
_STAND_IN_SSH = """#!{python}
|
||||
import json, os, subprocess, sys
|
||||
if "-G" in sys.argv[1:]:
|
||||
print("hostname " + sys.argv[-1].rpartition("@")[2])
|
||||
sys.exit(0)
|
||||
saw = {{
|
||||
"argv": sys.argv[1:],
|
||||
"askpass": os.environ.get("SSH_ASKPASS", ""),
|
||||
|
||||
@ -15,6 +15,7 @@ has to still have the last word in it.
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
@ -24,6 +25,8 @@ from raven.cli.ops_connection_commands import connection_app
|
||||
from raven.ops import connections
|
||||
from tests._home import point_home
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("no_real_ssh_g")
|
||||
|
||||
FULL = [
|
||||
"--non-interactive",
|
||||
"--software",
|
||||
@ -1004,6 +1007,11 @@ def test_an_aborted_add_keeps_the_same_password_of_a_registration_that_has_not_w
|
||||
row = {"id": "box", "host": "192.0.2.10", "port": 22, "user": "alice", "auth": "password"}
|
||||
connection_add.write(row)
|
||||
assert connection_add.password_path("box").is_file(), "the pending registration still owns the password"
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir(exist_ok=True)
|
||||
(bin_dir / "ssh").write_text('#!/bin/sh\n[ "$1" = -G ] && echo "hostname 192.0.2.10"\n', encoding="utf-8")
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
assert ssh_env(row)
|
||||
|
||||
|
||||
@ -1217,3 +1225,37 @@ def test_password_stdin_with_a_key_login_is_refused_and_nothing_is_written(store
|
||||
assert "--password-stdin goes with --auth password" in said(out) and "Nothing was written" in out.output
|
||||
assert _nothing_kept(rhome) and not store.exists()
|
||||
assert "hunter2" not in out.output
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="password machines are refused on Windows")
|
||||
@pytest.mark.parametrize("order", ["key-after-password", "password-after-key"])
|
||||
def test_add_refuses_a_pair_one_machine_s_alias_would_redirect_even_without_a_probe(
|
||||
store, monkeypatch, tmp_path, order
|
||||
):
|
||||
"""``--skip-probe`` writes with no connection at all, so the registry write is where the pair is judged:
|
||||
a key machine whose ``Host`` alias would take a registered password machine's logins, or a password machine
|
||||
at a registered key machine's id. Nothing is kept or written (reviewed 2026-10-20)."""
|
||||
point_home(monkeypatch, tmp_path)
|
||||
rhome = tmp_path / "rhome"
|
||||
monkeypatch.setenv("RAVEN_HOME", str(rhome))
|
||||
monkeypatch.setattr(sys, "platform", "linux")
|
||||
monkeypatch.setattr("raven.cli.ops_connection_commands.probe", lambda row, **kw: pytest.fail("reached it"))
|
||||
if order == "key-after-password":
|
||||
theirs = {"id": "lab", "transport": "ssh", "host": "LabBox", "user": "alice", "auth": "password"}
|
||||
argv = ["add", "--id", "labbox", "--name", "Lab box", *SSH, "--skip-probe", *FULL]
|
||||
given = None
|
||||
named = "password machine 'lab'"
|
||||
else:
|
||||
theirs = {"id": "labbox", "transport": "ssh", "host": "198.51.100.66", "user": "root", "key": "~/k"}
|
||||
argv = [*PW_ADD[:6], "labbox", *PW_ADD[7:], "--auth", "password", "--password-stdin", "--skip-probe", *FULL]
|
||||
given = "hunter2\n"
|
||||
named = "key machine 'labbox'"
|
||||
store.write_text(json.dumps({"connections": [theirs]}), encoding="utf-8")
|
||||
|
||||
out = CliRunner().invoke(connection_app, argv, input=given)
|
||||
|
||||
assert out.exit_code == 1, out.output
|
||||
assert named in " ".join(out.output.split()) and "Nothing was written" in out.output
|
||||
assert [r["id"] for r in json.loads(store.read_text())["connections"]] == [theirs["id"]]
|
||||
assert not (rhome / "keys" / "box.password").exists()
|
||||
assert not (tmp_path / ".ssh" / "config").exists()
|
||||
|
||||
@ -38,6 +38,8 @@ from raven.node import bundle, protocol
|
||||
from raven.node import client as node_client
|
||||
from raven.node.client import DEFAULT_NODE_DIR, ChannelEndpoint, NodeError, NodePool, _Node
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("no_real_ssh_g")
|
||||
|
||||
HOST = "203.0.113.7"
|
||||
PORT = 58717
|
||||
|
||||
@ -45,6 +47,10 @@ _FAKE_SSH = """#!{python}
|
||||
import json, os, sys
|
||||
here = os.path.dirname(os.path.abspath(__file__))
|
||||
args = sys.argv[1:]
|
||||
if "-G" in args:
|
||||
# How a password machine's prompt resolves (transport.resolve_with_ssh): a config dump, not a launch.
|
||||
print("hostname {host}")
|
||||
sys.exit(0)
|
||||
with open(os.path.join(here, "calls"), "a") as f:
|
||||
f.write(json.dumps(args) + "\\n")
|
||||
mode = open(os.path.join(here, "mode")).read().strip()
|
||||
@ -1691,6 +1697,7 @@ async def test_the_link_to_a_password_machine_logs_in_through_askpass_and_offers
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
monkeypatch.setenv("RAVEN_HOME", str(box.root / "rhome"))
|
||||
monkeypatch.setenv("PATH", f"{box.bin}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
box.rows([_password_row()])
|
||||
box.password("hunter2")
|
||||
store_password("box", "hunter2")
|
||||
@ -1709,6 +1716,7 @@ async def test_a_password_the_machine_refuses_ends_the_link_with_the_login_named
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
monkeypatch.setenv("RAVEN_HOME", str(box.root / "rhome"))
|
||||
monkeypatch.setenv("PATH", f"{box.bin}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
box.rows([_password_row()])
|
||||
box.password("hunter2")
|
||||
store_password("box", "wrong-one")
|
||||
|
||||
@ -24,6 +24,8 @@ from raven.ops import connection_add as adder
|
||||
from raven.ops import connections
|
||||
from tests._home import point_home
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("no_real_ssh_g")
|
||||
|
||||
# Taken before the autouse fixture below replaces it for every test.
|
||||
_REAL_STORE_PATH = connections.store_path
|
||||
|
||||
@ -357,6 +359,86 @@ def test_ssh_defaults_is_empty_when_ssh_cannot_answer(tmp_path, monkeypatch):
|
||||
assert adder.ssh_defaults("203.0.113.7", port=22, user="root") == {}
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="the stand-in ssh is a POSIX shell script")
|
||||
def test_ssh_defaults_reads_a_config_dump_carrying_bytes_that_are_not_utf_8(tmp_path, monkeypatch):
|
||||
"""ssh prints the owner's config values as they are on disk. One that is not UTF-8 raised out of the key
|
||||
registration while the password machine's reader of the same dump went on (reviewed 2026-10-20)."""
|
||||
real = tmp_path / "id_real"
|
||||
real.write_text("")
|
||||
_fake_ssh(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
f"printf 'remotecommand caf\\351\\nuser cfd\\nport 58717\\nidentityfile {real}\\n'",
|
||||
)
|
||||
|
||||
assert adder.ssh_defaults("203.0.113.7") == {"user": "cfd", "port": 58717, "keys": [str(real)]}
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="the stand-in ssh is a POSIX shell script")
|
||||
def test_ssh_defaults_is_empty_when_ssh_is_there_and_cannot_be_started(tmp_path, monkeypatch):
|
||||
"""An ssh that cannot be executed answers nothing, as a missing one does, rather than raising out of the
|
||||
registration: the password machine's reader already read it that way."""
|
||||
_fake_ssh(tmp_path, monkeypatch, "echo 'user cfd'")
|
||||
(tmp_path / "bin" / "ssh").chmod(0o644)
|
||||
# Alone on PATH: a lookup skips an entry it cannot execute and would go on to the computer's own ssh.
|
||||
monkeypatch.setenv("PATH", str(tmp_path / "bin"))
|
||||
|
||||
assert adder.ssh_defaults("203.0.113.7") == {}
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="the stand-in ssh is a POSIX shell script")
|
||||
def test_ssh_defaults_gives_what_it_starts_no_askpass_and_no_terminal_input(tmp_path, monkeypatch):
|
||||
"""The key machine's resolution is the same call as the password machine's: nothing it starts finds an
|
||||
askpass to call, and it reads no input from whoever started Raven."""
|
||||
seen = tmp_path / "seen"
|
||||
_fake_ssh(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
f'printf "%s|%s|%s|" "$1" "$SSH_ASKPASS" "$SSH_ASKPASS_REQUIRE" > {seen}\n'
|
||||
f'if read -r line; then printf "read:%s" "$line" >> {seen}; else printf "eof" >> {seen}; fi\n'
|
||||
"echo 'user cfd'",
|
||||
)
|
||||
monkeypatch.setenv("SSH_ASKPASS", str(tmp_path / "an-askpass-already-set"))
|
||||
monkeypatch.setenv("SSH_ASKPASS_REQUIRE", "force")
|
||||
typed, feed = os.pipe()
|
||||
os.write(feed, b"typed at the terminal\n")
|
||||
os.close(feed)
|
||||
saved = os.dup(0)
|
||||
os.dup2(typed, 0)
|
||||
try:
|
||||
adder.ssh_defaults("203.0.113.7")
|
||||
finally:
|
||||
os.dup2(saved, 0)
|
||||
os.close(saved)
|
||||
os.close(typed)
|
||||
|
||||
assert seen.read_text(encoding="utf-8") == "-G||never|eof"
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="password machines are refused on Windows")
|
||||
def test_key_and_password_machines_resolve_through_one_reader_of_ssh_g(tmp_path, monkeypatch, supported_host):
|
||||
"""Two readers of ``ssh -G`` disagreed on decoding and on which failures they survived (reviewed
|
||||
2026-10-20). Both machines' resolutions are now one call, with each caller's own command line."""
|
||||
from raven.ops import transport
|
||||
|
||||
asked: list[list[str]] = []
|
||||
|
||||
def resolve(argv):
|
||||
asked.append(list(argv))
|
||||
return transport.SshResolution({"hostname": ("203.0.113.7",), "user": ("cfd",), "port": ("58717",)}, "")
|
||||
|
||||
monkeypatch.setattr(transport, "resolve_with_ssh", resolve, raising=False)
|
||||
_fake_ssh(tmp_path, monkeypatch, "echo 'a reader of its own ran ssh' >&2; exit 99")
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
adder.store_password("box", "hunter2")
|
||||
|
||||
assert adder.ssh_defaults("203.0.113.7") == {"user": "cfd", "port": 58717, "keys": []}
|
||||
transport.ssh_env(PASSWORD_ROW)
|
||||
|
||||
assert asked[0] == ["ssh", "203.0.113.7"]
|
||||
assert asked[1][0] == "ssh" and asked[1][-1] == "root@203.0.113.7" and "PubkeyAuthentication=no" in asked[1]
|
||||
|
||||
|
||||
def test_the_probe_reads_the_c_library_line_into_the_note():
|
||||
assert adder.parse_probe("CORES=8\nLIBC=ldd (GNU libc) 2.31\n")["note"] == "ldd (GNU libc) 2.31"
|
||||
|
||||
@ -839,6 +921,74 @@ def test_a_refused_password_is_one_sentence_that_names_the_other_cause_too_and_n
|
||||
assert "wrong-one" not in said
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="the stand-in ssh is a POSIX shell script")
|
||||
def test_a_question_the_kept_password_does_not_answer_is_named_and_the_password_is_not_blamed(tmp_path, monkeypatch):
|
||||
"""The helper gives a second factor's question nothing, and ssh then reports the login refused; saying the
|
||||
password was refused sent the owner after a fault the password did not have."""
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
adder.store_password("box", "hunter2")
|
||||
_fake_ssh(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
"""
|
||||
[ "$1" = -G ] && { echo 'hostname 203.0.113.7'; exit 0; }
|
||||
"$SSH_ASKPASS" "(root@203.0.113.7) Verification code: " > /dev/null
|
||||
echo 'root@203.0.113.7: Permission denied (keyboard-interactive).' >&2; exit 255
|
||||
""",
|
||||
)
|
||||
|
||||
reached, said, _ = adder.probe(PASSWORD_ROW, timeout=10)
|
||||
|
||||
assert not reached
|
||||
assert "Permission denied (keyboard-interactive)." in said
|
||||
assert "The password was refused" not in said
|
||||
assert "so it was not sent" in said
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="the stand-in ssh is a POSIX shell script")
|
||||
def test_a_host_ssh_could_not_resolve_is_named_and_the_owner_is_not_sent_to_a_key(tmp_path, monkeypatch):
|
||||
"""``ssh -G`` fails, so the helper waits for the row's spelling ``ownerbox``; the machine then asks exactly
|
||||
for its password, at the address ssh did resolve, and the helper declines. That is no other question: the
|
||||
registration says the host could not be resolved, keeps that the password was not sent, and does not tell
|
||||
the owner to register with a key (reviewed 2026-10-20)."""
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
adder.store_password("box", "hunter2")
|
||||
_fake_ssh(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
"""
|
||||
[ "$1" = -G ] && { echo 'ssh: Could not resolve hostname ownerbox' >&2; exit 255; }
|
||||
given=$("$SSH_ASKPASS" "root@192.0.2.8's password: ")
|
||||
echo "given=$given" >&2
|
||||
echo 'root@192.0.2.8: Permission denied (password).' >&2; exit 255
|
||||
""",
|
||||
)
|
||||
|
||||
reached, said, _ = adder.probe(dict(PASSWORD_ROW, host="ownerbox"), timeout=10)
|
||||
|
||||
assert not reached
|
||||
assert "given=\n" in said and "hunter2" not in said
|
||||
assert "could not say which host" in said and "ssh -G exited 255" in said and "so it was not sent" in said
|
||||
assert "registered with a key" not in said and "The password was refused" not in said
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="the stand-in ssh is a POSIX shell script")
|
||||
def test_a_password_machine_at_another_machine_s_alias_is_refused_before_ssh_is_started(tmp_path, monkeypatch):
|
||||
"""The key machine ``labbox`` was registered first, so ``Host labbox`` in ~/.ssh/config is Raven's. A password
|
||||
machine registered at ``labbox`` would log in at the key machine's address; it is refused by name, before
|
||||
ssh starts, and the password is offered to nothing."""
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
assert adder.write_ssh_alias(dict(SSH_ROW, id="labbox", host="198.51.100.66")) == "labbox"
|
||||
adder.store_password("lab", "hunter2")
|
||||
_fake_ssh(tmp_path, monkeypatch, f"echo started >> {tmp_path / 'ssh-ran'}; exit 0")
|
||||
|
||||
reached, said, _ = adder.probe(dict(PASSWORD_ROW, id="lab", host="labbox"), timeout=10)
|
||||
|
||||
assert not reached
|
||||
assert "ssh alias Raven keeps" in said and "'labbox'" in said and "not sent" in said
|
||||
assert not (tmp_path / "ssh-ran").exists()
|
||||
|
||||
|
||||
def test_a_password_row_with_no_password_kept_here_is_refused_before_ssh_is_started(
|
||||
tmp_path, monkeypatch, supported_host
|
||||
):
|
||||
@ -859,3 +1009,80 @@ def test_a_password_row_is_refused_on_windows_before_ssh_is_started(tmp_path, mo
|
||||
reached, said, _ = adder.probe(PASSWORD_ROW, timeout=10)
|
||||
|
||||
assert not reached and "Windows" in said and "use a key" in said
|
||||
|
||||
|
||||
# ---- a registration that would let one machine's ssh alias take another's password is refused up front ----
|
||||
|
||||
KEY_LAB = {"id": "labbox", "transport": "ssh", "host": "198.51.100.66", "port": 22, "user": "root", "key": "~/k"}
|
||||
PASSWORD_LAB = {"id": "lab", "transport": "ssh", "host": "LabBox", "port": 22, "user": "alice", "auth": "password"}
|
||||
|
||||
|
||||
def _registered(_home, *rows):
|
||||
(_home / "connections.json").write_text(json.dumps({"connections": list(rows)}), encoding="utf-8")
|
||||
|
||||
|
||||
def test_a_key_machine_whose_id_is_a_password_machine_s_address_is_not_written(_home):
|
||||
"""Its ``Host labbox`` alias would take the password machine at ``LabBox`` (ssh matches ``Host`` case-blind)
|
||||
to this machine's address. Connecting refuses that pair by name, but only once the alias exists; the
|
||||
writer every registration goes through refuses it before (reviewed 2026-10-20)."""
|
||||
_registered(_home, PASSWORD_LAB)
|
||||
|
||||
with pytest.raises(ValueError, match="password machine 'lab'") as refused:
|
||||
adder.write(dict(KEY_LAB))
|
||||
|
||||
assert "Pick another id" in str(refused.value)
|
||||
assert [r["id"] for r in connections.load()] == ["lab"]
|
||||
|
||||
|
||||
def test_a_password_machine_at_a_key_machine_s_id_is_not_written_even_before_its_alias_exists(_home):
|
||||
"""The key machine's alias may not be written yet (it is written after its row, and a failed write is only a
|
||||
warning), so the registry is asked as well as ~/.ssh/config."""
|
||||
_registered(_home, KEY_LAB)
|
||||
|
||||
with pytest.raises(ValueError, match="key machine 'labbox'") as refused:
|
||||
adder.write(dict(PASSWORD_LAB), password="hunter2")
|
||||
|
||||
assert "its own address" in str(refused.value)
|
||||
assert [r["id"] for r in connections.load()] == ["labbox"]
|
||||
assert not connections.password_path("lab").exists()
|
||||
|
||||
|
||||
def test_a_password_machine_at_an_alias_left_with_one_mark_and_no_machine_is_not_written(_home):
|
||||
"""A block whose machine is gone, cut short to one mark, is still Raven's alias and still decides where ssh
|
||||
goes for that name."""
|
||||
(_home / ".ssh").mkdir()
|
||||
(_home / ".ssh" / "config").write_text(
|
||||
adder.ALIAS_MARK.format(conn_id="labbox") + "\nHost labbox\n HostName 198.51.100.66\n", encoding="utf-8"
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="ssh alias Raven keeps"):
|
||||
adder.write(dict(PASSWORD_LAB, host="labbox"))
|
||||
|
||||
assert not (_home / "connections.json").exists()
|
||||
|
||||
|
||||
def test_rows_that_do_not_meet_register_as_before(_home):
|
||||
"""Only the colliding pair is refused: a key machine with another id, a password machine elsewhere, a local
|
||||
machine named like an address."""
|
||||
_registered(_home, PASSWORD_LAB)
|
||||
|
||||
adder.write(dict(KEY_LAB, id="labbox2"))
|
||||
adder.write(dict(PASSWORD_LAB, id="lab2", host="203.0.113.9"))
|
||||
adder.write({"id": "labbox-local", "transport": "local"})
|
||||
|
||||
assert [r["id"] for r in connections.load()] == ["lab", "labbox2", "lab2", "labbox-local"]
|
||||
|
||||
|
||||
def test_the_model_cannot_register_a_key_machine_whose_id_is_a_password_machine_s_address(_home, monkeypatch):
|
||||
"""The reviewed path: ops_connection_add, callable by the model, registered the key machine whose alias then
|
||||
redirected the password machine. Refused before anything is reached or resolved."""
|
||||
_registered(_home, PASSWORD_LAB)
|
||||
monkeypatch.setattr(adder, "probe", lambda row, **kw: pytest.fail("probed a machine that cannot be registered"))
|
||||
monkeypatch.setattr(
|
||||
adder, "ssh_defaults", lambda host, **kw: pytest.fail("resolved a machine that cannot be registered")
|
||||
)
|
||||
|
||||
out = add(id="labbox", host="198.51.100.66", key="", port=0, user="")
|
||||
|
||||
assert out.startswith("REFUSED") and "password machine 'lab'" in out and "Nothing was written" in out
|
||||
assert not (_home / ".ssh" / "config").exists()
|
||||
|
||||
@ -7,6 +7,7 @@ so these pin what the runner sent before the two shared it.
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
@ -16,6 +17,8 @@ import pytest
|
||||
from raven.ops import transport
|
||||
from raven.ops.transport import ISOLATE_IDENTITY, TransportError, make_ssh_runner, ssh_argv, ssh_target
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("no_real_ssh_g")
|
||||
|
||||
BASE = [
|
||||
"ssh",
|
||||
"-i",
|
||||
@ -115,6 +118,7 @@ def test_the_askpass_environment_names_the_helper_and_the_password_file_never_th
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
monkeypatch.delenv("DISPLAY", raising=False)
|
||||
store_password("box", "hunter2")
|
||||
_owner_ssh_config(tmp_path, monkeypatch, "")
|
||||
|
||||
env = transport.ssh_env(PASSWORD_ROW)
|
||||
|
||||
@ -171,6 +175,9 @@ def test_the_runner_hands_ssh_the_askpass_environment_for_a_password_row_and_not
|
||||
sent: list[tuple[list[str], dict | None, bool]] = []
|
||||
|
||||
def run(argv, **kwargs):
|
||||
if "-G" in argv:
|
||||
# How ssh resolves the host for the helper's prompt: a config dump, not a connection.
|
||||
return subprocess.CompletedProcess(argv, 0, "hostname 203.0.113.7\n", "")
|
||||
sent.append((argv, kwargs.get("env"), kwargs.get("start_new_session", False)))
|
||||
return subprocess.CompletedProcess(argv, 0, "out", "")
|
||||
|
||||
@ -274,6 +281,363 @@ def test_the_askpass_helper_gives_every_other_prompt_nothing(tmp_path, prompt):
|
||||
assert done.stderr.startswith("raven: ") and "hunter2" not in done.stderr
|
||||
|
||||
|
||||
# ---- the prompt the helper waits for is the one ssh writes, for the host ssh resolves ----
|
||||
|
||||
_REAL_SSH = shutil.which("ssh")
|
||||
|
||||
|
||||
def _owner_ssh_config(tmp_path: Path, monkeypatch, config: str) -> None:
|
||||
"""The real ssh first on PATH, reading ``config`` as the owner's ``~/.ssh/config``."""
|
||||
conf = tmp_path / "ssh_config"
|
||||
conf.write_text(config, encoding="utf-8")
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
wrapper = bin_dir / "ssh"
|
||||
wrapper.write_text(f'#!/bin/sh\nexec "{_REAL_SSH}" -F "{conf}" "$@"\n', encoding="utf-8")
|
||||
wrapper.chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
|
||||
|
||||
@_posix_helper
|
||||
@pytest.mark.skipif(_REAL_SSH is None, reason="an OpenSSH client resolves the host")
|
||||
@pytest.mark.parametrize(
|
||||
("host", "prompt"),
|
||||
[
|
||||
("devbox", "alice@10.0.0.5's password: "),
|
||||
("devbox", "(alice@10.0.0.5) Password: "),
|
||||
("aliasbox", "alice@labbox's password: "),
|
||||
("0:0:0:0:0:0:0:1", "alice@::1's password: "),
|
||||
],
|
||||
ids=["config-hostname", "config-hostname-keyboard-interactive", "host-key-alias", "non-canonical-address"],
|
||||
)
|
||||
def test_the_helper_answers_the_prompt_ssh_writes_for_the_host_it_resolves(
|
||||
tmp_path, monkeypatch, supported_host, host, prompt
|
||||
):
|
||||
"""ssh's prompt names the host ssh resolved, not the row's spelling of it: an alias's HostName, a
|
||||
HostKeyAlias in place of either, an address in its canonical form. Waiting for the row's spelling
|
||||
refused every such machine, and its registration then blamed the password (reviewed 2026-10-20)."""
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
_owner_ssh_config(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
"Host devbox\n HostName 10.0.0.5\nHost aliasbox\n HostName 10.0.0.6\n HostKeyAlias labbox\n",
|
||||
)
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("box", "hunter2")
|
||||
|
||||
env = transport.ssh_env({**PASSWORD_ROW, "host": host})
|
||||
done = subprocess.run(
|
||||
[env["SSH_ASKPASS"], prompt], capture_output=True, text=True, env={**os.environ, **env}, timeout=30
|
||||
)
|
||||
|
||||
assert (done.returncode, done.stdout) == (0, "hunter2\n"), done.stderr
|
||||
|
||||
|
||||
@_posix_helper
|
||||
@pytest.mark.parametrize(
|
||||
"stand_in",
|
||||
[
|
||||
"echo 'hostname 10.9.9.9'; echo 'ssh: Could not resolve hostname: Name or service not known' >&2; exit 255",
|
||||
"echo 'usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] destination [command]'",
|
||||
None,
|
||||
"/bin/sleep 3; echo 'hostname 10.9.9.9'",
|
||||
],
|
||||
ids=["ssh-fails", "nothing-resolved", "no-ssh-here", "ssh-hangs"],
|
||||
)
|
||||
def test_when_ssh_cannot_say_how_it_resolves_the_host_the_helper_waits_for_the_row_s_own_address(
|
||||
tmp_path, monkeypatch, supported_host, stand_in
|
||||
):
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
if stand_in is not None:
|
||||
(bin_dir / "ssh").write_text(f"#!/bin/sh\n{stand_in}\n", encoding="utf-8")
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", str(bin_dir))
|
||||
monkeypatch.setattr(transport, "_RESOLVE_TIMEOUT_S", 0.3)
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("box", "hunter2")
|
||||
|
||||
env = transport.ssh_env(PASSWORD_ROW)
|
||||
done = subprocess.run(
|
||||
[env["SSH_ASKPASS"], "alice@203.0.113.7's password: "],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={**os.environ, **env, "PATH": "/usr/bin:/bin"},
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
assert (done.returncode, done.stdout) == (0, "hunter2\n"), done.stderr
|
||||
|
||||
|
||||
@_posix_helper
|
||||
@pytest.mark.parametrize(
|
||||
("stand_in", "why"),
|
||||
[
|
||||
("echo 'ssh: Could not resolve hostname: Name or service not known' >&2; exit 255", "ssh -G exited 255"),
|
||||
("echo 'usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] destination [command]'", "ssh -G named no host"),
|
||||
(None, "ssh -G could not be started"),
|
||||
("/bin/sleep 3; echo 'hostname 10.9.9.9'", "ssh -G took over 0.3 s"),
|
||||
],
|
||||
ids=["ssh-fails", "nothing-resolved", "no-ssh-here", "ssh-hangs"],
|
||||
)
|
||||
def test_when_ssh_cannot_say_how_it_resolves_the_host_the_helper_s_refusal_says_so(
|
||||
tmp_path, monkeypatch, supported_host, stand_in, why
|
||||
):
|
||||
"""The fallback to the row's spelling is safe and was silent: a machine whose prompt names the host ssh did
|
||||
resolve was then declined, and its registration read as a machine asking another question. The helper's
|
||||
refusal names the failed resolution, which is no secret, and still gives the prompt nothing."""
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
if stand_in is not None:
|
||||
(bin_dir / "ssh").write_text(f"#!/bin/sh\n{stand_in}\n", encoding="utf-8")
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", str(bin_dir))
|
||||
monkeypatch.setattr(transport, "_RESOLVE_TIMEOUT_S", 0.3)
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("box", "hunter2")
|
||||
|
||||
env = transport.ssh_env({**PASSWORD_ROW, "host": "ownerbox"})
|
||||
done = subprocess.run(
|
||||
[env["SSH_ASKPASS"], "alice@192.0.2.8's password: "],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={**os.environ, **env, "PATH": "/usr/bin:/bin"},
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
assert (done.returncode, done.stdout) == (1, "")
|
||||
assert done.stderr.splitlines() == [
|
||||
transport.ASKPASS_REFUSED,
|
||||
f"{transport.ASKPASS_UNRESOLVED} ({why}), so it waited for the prompt of alice@ownerbox",
|
||||
]
|
||||
|
||||
|
||||
@_posix_helper
|
||||
def test_a_host_ssh_resolves_adds_nothing_to_the_helper_s_refusal(tmp_path, monkeypatch, supported_host):
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
(bin_dir / "ssh").write_text("#!/bin/sh\necho 'hostname 203.0.113.7'\n", encoding="utf-8")
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", str(bin_dir))
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("box", "hunter2")
|
||||
|
||||
env = transport.ssh_env(PASSWORD_ROW)
|
||||
done = subprocess.run(
|
||||
[env["SSH_ASKPASS"], "(alice@203.0.113.7) Verification code: "],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={**os.environ, **env, "PATH": "/usr/bin:/bin"},
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
assert (done.returncode, done.stdout, done.stderr) == (1, "", transport.ASKPASS_REFUSED + "\n")
|
||||
|
||||
|
||||
@_posix_helper
|
||||
def test_a_config_dump_carrying_bytes_that_are_not_utf_8_still_names_the_host(tmp_path, monkeypatch, supported_host):
|
||||
"""ssh prints the owner's config values as they are on disk. One that is not UTF-8 must not take the
|
||||
resolution down, and with it every connection to the machine."""
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
(bin_dir / "ssh").write_text(
|
||||
"#!/bin/sh\nprintf 'remotecommand caf\\351\\nhostname 10.9.9.9\\n'\n", encoding="utf-8"
|
||||
)
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("box", "hunter2")
|
||||
|
||||
env = transport.ssh_env(PASSWORD_ROW)
|
||||
done = subprocess.run(
|
||||
[env["SSH_ASKPASS"], "alice@10.9.9.9's password: "],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={**os.environ, **env},
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
assert (done.returncode, done.stdout) == (0, "hunter2\n"), done.stderr
|
||||
|
||||
|
||||
@_posix_helper
|
||||
def test_resolving_the_host_hands_ssh_no_askpass_so_nothing_it_starts_reaches_the_helper(
|
||||
tmp_path, monkeypatch, supported_host
|
||||
):
|
||||
"""A helper that ran ``ssh -G`` itself, against a stand-in ssh that called askpass, recursed into
|
||||
thousands of processes (2026-10-19). The resolution now runs before ssh is started, and whatever it
|
||||
starts finds no askpass to call."""
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
seen = tmp_path / "seen"
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
(bin_dir / "ssh").write_text(
|
||||
f'#!/bin/sh\nprintf "%s|%s|%s\\n" "$1" "$SSH_ASKPASS" "$SSH_ASKPASS_REQUIRE" >> "{seen}"\n'
|
||||
'echo "hostname 203.0.113.7"\n',
|
||||
encoding="utf-8",
|
||||
)
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
monkeypatch.setenv("SSH_ASKPASS", str(tmp_path / "an-askpass-already-set"))
|
||||
monkeypatch.setenv("SSH_ASKPASS_REQUIRE", "force")
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("box", "hunter2")
|
||||
|
||||
transport.ssh_env(PASSWORD_ROW)
|
||||
|
||||
assert seen.read_text(encoding="utf-8").splitlines() == ["-G||never"]
|
||||
|
||||
|
||||
@_posix_helper
|
||||
@pytest.mark.skipif(_REAL_SSH is None, reason="an OpenSSH client resolves the host")
|
||||
@pytest.mark.parametrize("setting", ["yes", "always"])
|
||||
def test_a_password_connection_never_canonicalizes_so_it_logs_in_where_ssh_g_said(tmp_path, monkeypatch, setting):
|
||||
"""``ssh -G`` prints the configuration without the DNS lookups ``CanonicalizeHostname`` makes, and a real
|
||||
connection then re-reads the config under the canonical name -- where a ``Host <id>`` block Raven wrote
|
||||
for another machine can apply (measured with OpenSSH 8.9: ``-G -v`` reports applying it in that pass).
|
||||
So the host the helper is told to answer for could differ from the one the connection reaches. A password
|
||||
connection turns canonicalization off on its command line, which outranks the owner's config, and the
|
||||
resolution, asked with the same options, says so."""
|
||||
_owner_ssh_config(tmp_path, monkeypatch, f"CanonicalizeHostname {setting}\nHost devbox\n HostName 10.0.0.5\n")
|
||||
argv = ssh_argv("devbox", 22, "", user="alice", password=True)
|
||||
|
||||
resolved = transport.resolve_with_ssh(argv)
|
||||
|
||||
assert resolved.failure == "" and resolved.first("canonicalizehostname") == "false"
|
||||
assert resolved.first("hostname") == "10.0.0.5"
|
||||
assert "CanonicalizeHostname=no" in argv
|
||||
|
||||
|
||||
@_posix_helper
|
||||
@pytest.mark.skipif(_REAL_SSH is None, reason="an OpenSSH client resolves the host")
|
||||
def test_a_managed_block_a_final_pass_applies_never_supplies_the_address(tmp_path, monkeypatch, supported_host):
|
||||
"""With canonicalization off, the one later pass left is ``Match final``, which re-reads the config under
|
||||
the name the owner's ``HostName`` gave: a ``Host <id>`` block Raven wrote then applies (OpenSSH 8.9 says
|
||||
so under ``-v``), but ssh keeps the first ``HostName`` it read, the owner's. A Raven block can only
|
||||
supply the address when it matches the row's own host, and that row is refused by name."""
|
||||
from raven.ops.connection_add import ALIAS_MARK, store_password
|
||||
|
||||
mark = ALIAS_MARK.format(conn_id="labbox")
|
||||
_owner_ssh_config(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
f"Match final\nHost lab\n HostName labbox\n\n{mark}\nHost labbox\n HostName 198.51.100.66\n{mark}\n",
|
||||
)
|
||||
monkeypatch.setattr(transport, "passwd_home", lambda: tmp_path / "nobody")
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("lab", "hunter2")
|
||||
|
||||
env = transport.ssh_env({"id": "lab", "transport": "ssh", "host": "lab", "user": "alice", "auth": "password"})
|
||||
|
||||
assert env[transport.ASKPASS_FOR_ENV] == "alice@labbox"
|
||||
|
||||
|
||||
# ---- an alias Raven keeps for one machine never picks who gets another machine's password ----
|
||||
|
||||
|
||||
def _key_machine_alias(conn_id: str, host: str) -> Path:
|
||||
"""The ``Host <conn_id>`` block Raven writes into ``~/.ssh/config`` when a key machine is registered."""
|
||||
from raven.ops.connection_add import write_ssh_alias
|
||||
|
||||
write_ssh_alias({"id": conn_id, "transport": "ssh", "host": host, "port": 22, "user": "root", "key": "~/k"})
|
||||
return Path(os.path.expanduser("~/.ssh/config"))
|
||||
|
||||
|
||||
@_posix_helper
|
||||
@pytest.mark.skipif(_REAL_SSH is None, reason="an OpenSSH client resolves the host")
|
||||
@pytest.mark.parametrize("spelled", ["labbox", "LabBox"], ids=["as-written", "another-case"])
|
||||
def test_a_password_machine_whose_host_is_another_machine_s_managed_alias_is_refused_and_its_password_stays(
|
||||
tmp_path, monkeypatch, supported_host, spelled
|
||||
):
|
||||
"""A key machine registered under the id ``labbox`` makes Raven write ``Host labbox`` / ``HostName <its
|
||||
address>``. Following ssh's resolution through that block handed the password kept for ``lab`` -- a password
|
||||
machine at ``labbox`` -- to the key machine's address, and the model can register such a machine (reviewed
|
||||
2026-10-20). ssh matches ``Host`` case-blind, so the refusal does too."""
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
config = _key_machine_alias("labbox", "198.51.100.66")
|
||||
wrapper_dir = tmp_path / "bin"
|
||||
wrapper_dir.mkdir()
|
||||
(wrapper_dir / "ssh").write_text(f'#!/bin/sh\nexec "{_REAL_SSH}" -F "{config}" "$@"\n', encoding="utf-8")
|
||||
(wrapper_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{wrapper_dir}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("lab", "hunter2")
|
||||
|
||||
with pytest.raises(TransportError) as refused:
|
||||
transport.ssh_env({"id": "lab", "transport": "ssh", "host": spelled, "user": "alice", "auth": "password"})
|
||||
|
||||
said = str(refused.value)
|
||||
assert "'lab'" in said and "'labbox'" in said and "not sent" in said
|
||||
assert "198.51.100.66" not in said and "hunter2" not in said
|
||||
|
||||
|
||||
@_posix_helper
|
||||
@pytest.mark.parametrize("where", ["passwd-home", "raven-home"])
|
||||
@pytest.mark.parametrize("whole", [True, False], ids=["block", "one-mark-left"])
|
||||
def test_a_managed_alias_counts_in_the_config_ssh_reads_and_in_the_one_raven_writes_even_cut_short(
|
||||
tmp_path, monkeypatch, supported_host, where, whole
|
||||
):
|
||||
"""ssh reads ``~/.ssh/config`` from the account's passwd entry, Raven writes the one under ``$HOME``; a block
|
||||
in either may be the one ssh follows. A block with one of its marks gone is still Raven's, not the owner's.
|
||||
The ssh stand-in names the managed address, as the real one would, and is never what decides."""
|
||||
from raven.ops.connection_add import ALIAS_MARK, store_password
|
||||
|
||||
passwd_home = tmp_path / "passwd-home"
|
||||
monkeypatch.setattr(transport, "passwd_home", lambda: passwd_home, raising=False)
|
||||
home = passwd_home if where == "passwd-home" else Path(os.path.expanduser("~"))
|
||||
mark = ALIAS_MARK.format(conn_id="labbox")
|
||||
block = [mark, "Host labbox", " HostName 198.51.100.66"] + ([mark] if whole else [])
|
||||
(home / ".ssh").mkdir(parents=True, exist_ok=True)
|
||||
(home / ".ssh" / "config").write_text("Host ownerbox\n HostName 192.0.2.8\n\n" + "\n".join(block) + "\n")
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
(bin_dir / "ssh").write_text("#!/bin/sh\necho 'hostname 198.51.100.66'\n", encoding="utf-8")
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", str(bin_dir))
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("lab", "hunter2")
|
||||
|
||||
with pytest.raises(TransportError, match="'labbox'"):
|
||||
transport.ssh_env({"id": "lab", "transport": "ssh", "host": "labbox", "user": "alice", "auth": "password"})
|
||||
|
||||
|
||||
@_posix_helper
|
||||
@pytest.mark.skipif(_REAL_SSH is None, reason="an OpenSSH client resolves the host")
|
||||
def test_the_owner_s_own_alias_beside_raven_s_blocks_still_names_the_prompt_it_resolves(
|
||||
tmp_path, monkeypatch, supported_host
|
||||
):
|
||||
"""What the resolution exists for keeps working with Raven's blocks in the same file: only a host that is a
|
||||
managed id is refused."""
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
config = _key_machine_alias("labbox", "198.51.100.66")
|
||||
with config.open("a", encoding="utf-8") as fh:
|
||||
fh.write("\nHost ownerbox\n HostName 192.0.2.8\n")
|
||||
monkeypatch.setattr(transport, "passwd_home", lambda: config.parent.parent, raising=False)
|
||||
wrapper_dir = tmp_path / "bin"
|
||||
wrapper_dir.mkdir()
|
||||
(wrapper_dir / "ssh").write_text(f'#!/bin/sh\nexec "{_REAL_SSH}" -F "{config}" "$@"\n', encoding="utf-8")
|
||||
(wrapper_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{wrapper_dir}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("own", "hunter2")
|
||||
|
||||
env = transport.ssh_env({"id": "own", "transport": "ssh", "host": "ownerbox", "user": "alice", "auth": "password"})
|
||||
|
||||
assert env[transport.ASKPASS_FOR_ENV] == "alice@192.0.2.8"
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name == "nt", reason="POSIX file modes are not available on Windows")
|
||||
def test_a_stale_askpass_helper_is_replaced_whole_and_never_rewritten_in_place(tmp_path):
|
||||
keys = tmp_path / "keys"
|
||||
@ -290,3 +654,23 @@ def test_a_stale_askpass_helper_is_replaced_whole_and_never_rewritten_in_place(t
|
||||
assert helper.read_text(encoding="utf-8") == transport._ASKPASS
|
||||
assert oct(helper.stat().st_mode & 0o777) == "0o700"
|
||||
assert sorted(path.name for path in keys.iterdir()) == ["askpass"], "nothing is left beside it"
|
||||
|
||||
|
||||
def test_one_function_builds_the_environment_that_hands_ssh_a_kept_password():
|
||||
"""Every way a kept password reaches ssh -- registration probes, the CLI, ``machines.add``, exec's machine
|
||||
channel, the ACP tunnel and node link, on-call's runners and rsync -- takes it from :func:`transport.ssh_env`,
|
||||
so the refusals there (no password kept, Windows, a host that is one of Raven's own aliases) stand on every
|
||||
one of them. A second module naming the helper's password-file variable, or ordering askpass by force, would
|
||||
be a path around them."""
|
||||
import re
|
||||
|
||||
repo = Path(__file__).resolve().parent.parent
|
||||
builds = re.compile(r"RAVEN_MACHINE_PASSWORD_FILE|ASKPASS_FILE_ENV|SSH_ASKPASS_REQUIRE[\"']?\s*[:=,]\s*[\"']force")
|
||||
found = sorted(
|
||||
path.relative_to(repo).as_posix()
|
||||
for root in ("raven", "agents", "plugins-dist")
|
||||
for path in (repo / root).rglob("*.py")
|
||||
if builds.search(path.read_text(encoding="utf-8", errors="replace"))
|
||||
)
|
||||
|
||||
assert found == ["raven/ops/transport.py"]
|
||||
|
||||
@ -10,6 +10,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import threading
|
||||
|
||||
@ -20,11 +21,14 @@ from raven.acp_client import remote
|
||||
from raven.node import install as node_install
|
||||
from raven.node.client import MachineStatus, NodeError
|
||||
from raven.ops import connection_add, connections
|
||||
from raven.ops.transport import ASKPASS_REFUSED
|
||||
from raven.rpc.errors import ConfigValidationError
|
||||
from raven.rpc.methods import machines as rpc
|
||||
from raven.rpc.models import METHOD_MODELS
|
||||
from tests._home import point_home
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("no_real_ssh_g")
|
||||
|
||||
|
||||
def _check(method: str, payload: dict) -> None:
|
||||
"""A handler's real answer against its declared result model."""
|
||||
@ -377,6 +381,11 @@ async def test_add_refuses_before_reaching_anything_over_a_missing_field_a_bad_i
|
||||
("could not reach alice@203.0.113.7:2222: Host key verification failed.", "hostkey"),
|
||||
("could not reach alice@203.0.113.7:2222: no answer within 30s", "timeout"),
|
||||
("could not reach alice@203.0.113.7:2222: ssh not found on this computer", "nossh"),
|
||||
(
|
||||
f"could not reach alice@203.0.113.7:2222: {ASKPASS_REFUSED}\n"
|
||||
"alice@203.0.113.7: Permission denied (keyboard-interactive).",
|
||||
"prompt",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_a_machine_that_cannot_be_reached_is_not_written_and_the_refusal_names_its_kind_not_the_address(
|
||||
@ -906,10 +915,51 @@ async def test_a_refusal_after_the_same_machine_registered_meanwhile_leaves_the_
|
||||
assert connection_add.password_path("box").read_text(encoding="utf-8") == BY_PASSWORD["password"] + "\n"
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="password machines are refused on Windows before this point")
|
||||
async def test_a_password_machine_at_another_machine_s_alias_is_refused_as_a_field_that_cannot_be_used(
|
||||
supported_host, pool, rhome, monkeypatch
|
||||
):
|
||||
"""``Host labbox`` in ~/.ssh/config is the alias Raven keeps for the key machine ``labbox``; a password machine
|
||||
registered at ``labbox`` would log in at that machine's address. The sheet names the host field, not the
|
||||
network, and the password is forgotten again."""
|
||||
connection_add.write_ssh_alias({"id": "labbox", "transport": "ssh", "host": "198.51.100.66", "key": "~/k"})
|
||||
|
||||
out = await rpc.machines_add(BY_PASSWORD | {"host": "labbox"})
|
||||
|
||||
_check("machines.add", out)
|
||||
assert (out["ok"], out["reason"]) == (False, "invalid")
|
||||
assert "ssh alias Raven keeps" in out["detail"] and "hunter2" not in out["detail"]
|
||||
assert not connection_add.password_path("box").exists()
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="password machines are refused on Windows before this point")
|
||||
@pytest.mark.parametrize("order", ["key-after-password", "password-after-key"])
|
||||
async def test_a_pair_one_machine_s_alias_would_redirect_is_refused_before_anything_is_kept_or_reached(
|
||||
supported_host, pool, rhome, monkeypatch, order
|
||||
):
|
||||
"""Either order: a key machine whose id is a registered password machine's address (``LabBox``, ssh matches
|
||||
``Host`` case-blind), or a password machine at a registered key machine's id whose alias is not written.
|
||||
Refused as a field that cannot be used, naming the other machine, before a password is kept or a probe
|
||||
runs (reviewed 2026-10-20)."""
|
||||
monkeypatch.setattr(connection_add, "probe", lambda row, **kw: pytest.fail("reached a pair that cannot register"))
|
||||
if order == "key-after-password":
|
||||
_store({"id": "lab", "transport": "ssh", "host": "LabBox", "user": "alice", "auth": "password"})
|
||||
out = await rpc.machines_add({**FORM, "id": "labbox"})
|
||||
named = "password machine 'lab'"
|
||||
else:
|
||||
_store({"id": "labbox", "transport": "ssh", "host": "198.51.100.66", "user": "root", "key": "~/k"})
|
||||
out = await rpc.machines_add(BY_PASSWORD | {"host": "labbox"})
|
||||
named = "key machine 'labbox'"
|
||||
|
||||
_check("machines.add", out)
|
||||
assert (out["ok"], out["reason"]) == (False, "invalid") and named in out["detail"]
|
||||
assert len(_rows()) == 1 and not connection_add.password_path("box").exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("cancelled", [False, True])
|
||||
@pytest.mark.parametrize("failed_index", [0, 1])
|
||||
async def test_a_failed_registration_keeps_the_same_password_of_another_registration_still_probing(
|
||||
supported_host, pool, rhome, monkeypatch, cancelled, failed_index
|
||||
supported_host, pool, rhome, monkeypatch, tmp_path, cancelled, failed_index
|
||||
):
|
||||
from raven.ops.transport import ssh_env
|
||||
|
||||
@ -948,6 +998,11 @@ async def test_a_failed_registration_keeps_the_same_password_of_another_registra
|
||||
assert out["ok"]
|
||||
(row,) = _rows()
|
||||
assert connection_add.password_path("box").is_file(), "the successful registration must keep its password"
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir(exist_ok=True)
|
||||
(bin_dir / "ssh").write_text('#!/bin/sh\n[ "$1" = -G ] && echo "hostname 203.0.113.7"\n', encoding="utf-8")
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
assert ssh_env(row), "the registered machine must still be able to start its next connection"
|
||||
|
||||
|
||||
|
||||
@ -10,6 +10,8 @@ stand.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
import pytest
|
||||
@ -93,6 +95,45 @@ async def test_a_named_machine_routes_to_the_channel(registry, monkeypatch, tmp_
|
||||
assert seen == {"command": "nvidia-smi", "connection": "conn_gpu", "cwd": "/root/ws"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="the stand-in ssh is a POSIX script")
|
||||
async def test_a_password_machine_s_runner_is_built_off_the_event_loop(monkeypatch, tmp_path, supported_host):
|
||||
"""A password machine's runner asks a local ``ssh -G`` which host its prompt will name. On the loop, a
|
||||
resolution waiting on DNS or a slow ``Match exec`` would hold every other turn the loop serves."""
|
||||
import asyncio
|
||||
|
||||
from raven.ops.connection_add import store_password
|
||||
|
||||
row = {**{k: v for k, v in ROW.items() if k != "key"}, "auth": "password"}
|
||||
monkeypatch.setattr("raven.ops.connections.load", lambda: [dict(row)])
|
||||
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "rhome"))
|
||||
store_password("conn_gpu", "hunter2")
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
(bin_dir / "ssh").write_text(
|
||||
'#!/bin/sh\nif [ "$1" = -G ]; then sleep 1; echo "hostname 203.0.113.7"; exit 0; fi\necho looked\n',
|
||||
encoding="utf-8",
|
||||
)
|
||||
(bin_dir / "ssh").chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
|
||||
ticks = 0
|
||||
|
||||
async def tick():
|
||||
nonlocal ticks
|
||||
while True:
|
||||
await asyncio.sleep(0.05)
|
||||
ticks += 1
|
||||
|
||||
ticker = asyncio.create_task(tick())
|
||||
try:
|
||||
out = await machine_exec.run_on_machine("true", connection="conn_gpu")
|
||||
finally:
|
||||
ticker.cancel()
|
||||
|
||||
assert "looked" in out
|
||||
assert ticks >= 5, f"the loop ran {ticks} ticks of 50 ms while ssh -G took a second"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_an_unknown_id_answers_with_the_list(registry):
|
||||
out = await machine_exec.run_on_machine("ls", connection="conn-typo")
|
||||
|
||||
@ -1588,6 +1588,8 @@ export const UI_TEXT: Record<Locale, Record<string, string>> = {
|
||||
'The machine could not be reached: check the address and port, and that the machine is on and reachable from this computer.',
|
||||
'gui.machine.why_login':
|
||||
"The machine refused the login. With a key: check the user name and the private key path, and that the key's public half is on the machine. With a password: check it, and that the machine accepts password logins at all.",
|
||||
'gui.machine.why_prompt':
|
||||
"The machine asked something the kept password does not answer -- a one-time code, another account's password, or a password prompt that names a host other than the registered one -- so the password was not sent: see the line below.",
|
||||
'gui.machine.why_hostkey':
|
||||
"The machine's host key differs from the one on record: check the machine before trusting it again, then take the old line out of ~/.ssh/known_hosts.",
|
||||
'gui.machine.why_timeout': 'The machine did not answer in time: check that it is on and that the port is open.',
|
||||
@ -4962,6 +4964,8 @@ export const UI_TEXT: Record<Locale, Record<string, string>> = {
|
||||
'gui.machine.why_unreachable': '连不上这台机器:检查地址和端口,确认机器开着、从这台电脑能连到它。',
|
||||
'gui.machine.why_login':
|
||||
'机器拒绝了登录。用密钥时:检查用户名和私钥路径,确认对应的公钥已经放到机器上。用密码时:检查密码,并确认这台机器允许密码登录。',
|
||||
'gui.machine.why_prompt':
|
||||
'机器问的不是这个账号自己的密码提示(比如一次性验证码、别的账号的密码,或者提示里的主机不是登记的那台),所以没有发送密码:详见下一行。',
|
||||
'gui.machine.why_hostkey':
|
||||
'机器的主机密钥和记录的不一样:先确认机器没问题,再把 ~/.ssh/known_hosts 里的旧记录删掉。',
|
||||
'gui.machine.why_timeout': '机器没有按时应答:确认机器开着、端口放行。',
|
||||
|
||||
@ -4576,12 +4576,13 @@ export interface MachinesAddResult {
|
||||
*/
|
||||
detail: string;
|
||||
/**
|
||||
* Why the row was not written, as a kind the page can explain: the machine could not be reached, refused the login, presented a changed host key, did not answer in time, ssh is missing here, this computer cannot use the login picked (a password on Windows), the id is taken, the fields are not usable, or the registry file cannot be changed. Empty when it was written.
|
||||
* Why the row was not written, as a kind the page can explain: the machine could not be reached, refused the login, asked a question the kept password does not answer (so the password was not sent), presented a changed host key, did not answer in time, ssh is missing here, this computer cannot use the login picked (a password on Windows), the id is taken, the fields are not usable, or the registry file cannot be changed. Empty when it was written.
|
||||
*/
|
||||
reason:
|
||||
| ''
|
||||
| 'unreachable'
|
||||
| 'login'
|
||||
| 'prompt'
|
||||
| 'hostkey'
|
||||
| 'timeout'
|
||||
| 'nossh'
|
||||
|
||||
@ -2468,6 +2468,18 @@ describe('the machines module', () => {
|
||||
expect(sent[1]).toEqual({ id: 'gpu1', name: 'GPU box 1', host: '203.0.113.7', port: 0, user: 'alice', auth: 'key', key: '~/.ssh/id_gpu1', password: '', paths: ['/data/alice/projects', '/scratch'] })
|
||||
})
|
||||
|
||||
it('names a question the kept password does not answer as its own kind, not as a refused password', async () => {
|
||||
const refused = 'could not reach the machine: raven: the password kept for this machine answers its own password prompt only, and this prompt is not one'
|
||||
install([row()], { machinesOver: { list: async () => [], add: async () => ({ ok: false, detail: refused, reason: 'prompt' as const }) } })
|
||||
await mount()
|
||||
await openMachines()
|
||||
await click(rowNamed('gui.machine.register'))
|
||||
for (const [label, value] of [['gui.machine.f_name', 'GPU box 1'], ['gui.machine.f_id', 'gpu1'], ['gui.machine.f_host', '203.0.113.7'], ['gui.machine.f_user', 'alice'], ['gui.machine.f_key', '~/.ssh/id_gpu1']] as const) await typeInto(field(label), value)
|
||||
await click(sheet()!.querySelector<HTMLButtonElement>('.extAgents-act button')!)
|
||||
expect(sheetStatus()).toBe('gui.machine.why_prompt')
|
||||
expect(note()).toEqual({ t: 'gui.machine.add_bad', said: refused })
|
||||
})
|
||||
|
||||
it('registers with a password once the login pick says so, and sends the password in place of a key path', async () => {
|
||||
const rows: MachineRow[] = []
|
||||
const { mcalls } = install([row()], {
|
||||
|
||||
@ -39,6 +39,7 @@ const PLUS = 'M12 5v14M5 12h14'
|
||||
const WHY: Record<Exclude<RegisterReason, ''>, string> = {
|
||||
unreachable: 'gui.machine.why_unreachable',
|
||||
login: 'gui.machine.why_login',
|
||||
prompt: 'gui.machine.why_prompt',
|
||||
hostkey: 'gui.machine.why_hostkey',
|
||||
timeout: 'gui.machine.why_timeout',
|
||||
nossh: 'gui.machine.why_nossh',
|
||||
|
||||
@ -233,7 +233,7 @@ export interface MachineProblem {
|
||||
|
||||
/* Why a machine was not registered, as a kind the page explains in the
|
||||
reader's language; '' when it was. */
|
||||
export type RegisterReason = '' | 'unreachable' | 'login' | 'hostkey' | 'timeout' | 'nossh' | 'platform' | 'duplicate' | 'invalid' | 'registry'
|
||||
export type RegisterReason = '' | 'unreachable' | 'login' | 'prompt' | 'hostkey' | 'timeout' | 'nossh' | 'platform' | 'duplicate' | 'invalid' | 'registry'
|
||||
|
||||
export interface RegisterForm {
|
||||
id: string
|
||||
|
||||
@ -3530,12 +3530,13 @@ export interface MachinesAddResult {
|
||||
*/
|
||||
detail: string;
|
||||
/**
|
||||
* Why the row was not written, as a kind the page can explain: the machine could not be reached, refused the login, presented a changed host key, did not answer in time, ssh is missing here, this computer cannot use the login picked (a password on Windows), the id is taken, the fields are not usable, or the registry file cannot be changed. Empty when it was written.
|
||||
* Why the row was not written, as a kind the page can explain: the machine could not be reached, refused the login, asked a question the kept password does not answer (so the password was not sent), presented a changed host key, did not answer in time, ssh is missing here, this computer cannot use the login picked (a password on Windows), the id is taken, the fields are not usable, or the registry file cannot be changed. Empty when it was written.
|
||||
*/
|
||||
reason:
|
||||
| ''
|
||||
| 'unreachable'
|
||||
| 'login'
|
||||
| 'prompt'
|
||||
| 'hostkey'
|
||||
| 'timeout'
|
||||
| 'nossh'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user