diff --git a/docs/06_ssh_access.md b/docs/06_ssh_access.md index e14e9f36..0d3210a2 100644 --- a/docs/06_ssh_access.md +++ b/docs/06_ssh_access.md @@ -1,5 +1,6 @@ --- log: +2026-11-23: Updated `integration/repro_windows_ssh/test.py` to check cached OAuth2 credentials via the OS `keyring` (`KEYRING_SERVICE_NAME` / `KEYRING_USERNAME`) instead of the removed `TOKEN_CONFIG_PATH`, and added portable import and prerequisite-skip regression tests in `tests/test_windows_ssh.py`. 2026-11-09: `--identity/-i` now accepts either a `.pub` public key or a private key path: `_resolve_pubkey` reads the `.pub` file directly when present before falling back to `_pubkey_from_identity` (`ssh-keygen -y -f`), and `_ssh_base_args` strips `.pub` for `ssh -i` when the sibling private key exists on disk. 2026-10-29: Removed the `colab console` subcommand, so `colab ssh` is the interactive shell on the runtime. Dropped console behavior from the Windows validation caveat. 2026-10-29: Added `colab ssh [-s NAME] -- CMD`, which runs one command on an existing session and exits with its status (a PTY only when stdin and stdout are terminals; neither it nor its ProxyCommand child creates a runtime). Its handshake carries `ssh-mode-cmd`, and the update banner is skipped for `ssh` when stdout is not a terminal. Added `tests/test_ssh_cmd.py`. @@ -191,18 +192,20 @@ interpreter installed under a path with spaces. Assertions check exact argv preservation for spaces, quotes, shell punctuation, backslashes, Unicode, and identity paths. A separate test launches the real Windows OpenSSH client with a local stub ProxyCommand and verifies its argv, without contacting Colab. -Portable tests also check registration without SIGHUP and the local null -device. Existing POSIX shell quoting and teardown tests remain in the suite. +Portable tests also check registration without SIGHUP, the local null +device, and `integration/repro_windows_ssh/test.py` import and prerequisite +skip paths across platforms. Existing POSIX shell quoting and teardown tests +remain in the suite. These and the existing SSH tests passed on Windows, Linux, and macOS with Python 3.12. This covers SSH compatibility only; local file editing and self-update require separate platform validation. `uv run python integration/repro_windows_ssh/test.py` provides a native Windows -live test using cached OAuth2 credentials. It creates a CPU runtime, executes a -remote command through the generated ProxyCommand with a key path containing -spaces, exercises `--rm`, and cleans up the recorded endpoint on failure. Exit -77 is a prerequisite skip, not a successful live validation. +live test using OAuth2 credentials cached in the OS keyring. It creates a CPU +runtime, executes a remote command through the generated ProxyCommand with a key +path containing spaces, exercises `--rm`, and cleans up the recorded endpoint on +failure. Exit 77 is a prerequisite skip, not a successful live validation. The native Windows live test passed on 2026-10-20 with Python 3.12.14 and the Windows OpenSSH client. This validates remote command execution and cleanup diff --git a/integration/README.md b/integration/README.md index 5ad646d2..199429c7 100644 --- a/integration/README.md +++ b/integration/README.md @@ -17,7 +17,7 @@ End-to-end tests that run against a **live Colab backend** (unlike the mocked un | `repro_bundled_oauth/` | Fast smoke test (~5s): verifies that the fallback OAuth configuration is loaded and starts the OAuth flow with the default client ID when local config is missing. | | `repro_legacy_token_migration/` | Fast smoke test (~5s, no VM): a pre-keyring `~/.config/colab-cli/token.json` is migrated into the keyring and deleted, deleted when the keyring already has a token, and kept (with a stderr warning) when the keyring is inaccessible. Uses a throwaway HOME and file-backed test keyring. | | `repro_ssh/` | Fast smoke test (~5s): `--help` advertises the flags and an unknown session exits. Slow soak test (~95s): Live e2e allocates a CPU VM, runs a real remote command over `colab ssh --proxy-mode` | -| `repro_windows_ssh/` | Native Windows live CPU test: Windows OpenSSH launches the generated ProxyCommand, executes a remote marker command, and releases the recorded runtime. Requires cached OAuth2 credentials; exits 77 if prerequisites are missing. | +| `repro_windows_ssh/` | Native Windows live CPU test: Windows OpenSSH launches the generated ProxyCommand, executes a remote marker command, and releases the recorded runtime. Requires OAuth2 credentials cached in the OS keyring; exits 77 if prerequisites are missing. | ## Running diff --git a/integration/repro_windows_ssh/test.py b/integration/repro_windows_ssh/test.py index cc16a985..211527ce 100644 --- a/integration/repro_windows_ssh/test.py +++ b/integration/repro_windows_ssh/test.py @@ -15,7 +15,8 @@ """Native Windows live SSH check; allocates and releases one CPU runtime. Run from the repository with `uv run python integration/repro_windows_ssh/test.py`. -Exit 77 means cached OAuth credentials are missing; no runtime is allocated. +Exit 77 means prerequisites (native Windows, keyring OAuth2 token, or OpenSSH) +are missing; no runtime is allocated. """ from pathlib import Path @@ -25,11 +26,18 @@ import tempfile import uuid -from colab_cli.auth import TOKEN_CONFIG_PATH +import keyring +from keyring.errors import KeyringError + +from colab_cli.auth import KEYRING_SERVICE_NAME, KEYRING_USERNAME from colab_cli.commands.ssh import _proxy_command, _ssh_base_args from colab_cli.common import State +def has_oauth2_token() -> bool: + return bool(keyring.get_password(KEYRING_SERVICE_NAME, KEYRING_USERNAME)) + + def colab(*args, check=True): return subprocess.run( [sys.executable, "-m", "colab_cli.cli", "--auth=oauth2", *args], @@ -43,7 +51,12 @@ def main(): if sys.platform != "win32": print("SKIP: requires native Windows and Windows OpenSSH.") return 77 - if not Path(TOKEN_CONFIG_PATH).is_file(): + try: + has_token = has_oauth2_token() + except KeyringError as exc: + print(f"SKIP: could not read OAuth2 credentials from OS keyring ({exc}).") + return 77 + if not has_token: print("SKIP: no cached OAuth2 credentials. First run:") print(" uv run colab --auth=oauth2 sessions") return 77 diff --git a/tests/test_windows_ssh.py b/tests/test_windows_ssh.py index 7309b0d1..20c024d3 100644 --- a/tests/test_windows_ssh.py +++ b/tests/test_windows_ssh.py @@ -14,8 +14,10 @@ """Native Windows regressions for the OpenSSH transport.""" +import importlib.util import json import os +from pathlib import Path import signal import shutil import subprocess @@ -23,8 +25,10 @@ from types import SimpleNamespace import venv +from keyring.errors import KeyringError import pytest +from colab_cli.auth import KEYRING_SERVICE_NAME, KEYRING_USERNAME from colab_cli.commands import ssh @@ -163,3 +167,71 @@ def test_rm_handlers_register_available_signals(mocker): if hasattr(signal, name) } assert {call.args[0] for call in register.call_args_list} == expected + + +def _load_repro_windows_ssh(): + path = ( + Path(__file__).resolve().parents[1] + / "integration" + / "repro_windows_ssh" + / "test.py" + ) + spec = importlib.util.spec_from_file_location("repro_windows_ssh", path) + assert spec is not None and spec.loader is not None + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def test_repro_windows_ssh_skips_on_non_windows(monkeypatch, capsys): + module = _load_repro_windows_ssh() + monkeypatch.setattr(module.sys, "platform", "linux") + + assert module.main() == 77 + assert "SKIP: requires native Windows" in capsys.readouterr().out + + +@pytest.mark.parametrize("token", [None, ""]) +def test_repro_windows_ssh_skips_without_keyring_token( + monkeypatch, mocker, capsys, token +): + module = _load_repro_windows_ssh() + monkeypatch.setattr(module.sys, "platform", "win32") + get_password = mocker.patch("keyring.get_password", return_value=token) + which = mocker.patch.object(module.shutil, "which") + + assert module.main() == 77 + get_password.assert_called_once_with(KEYRING_SERVICE_NAME, KEYRING_USERNAME) + which.assert_not_called() + assert "SKIP: no cached OAuth2 credentials" in capsys.readouterr().out + + +def test_repro_windows_ssh_skips_on_keyring_error(monkeypatch, mocker, capsys): + module = _load_repro_windows_ssh() + monkeypatch.setattr(module.sys, "platform", "win32") + get_password = mocker.patch( + "keyring.get_password", side_effect=KeyringError("keyring locked") + ) + which = mocker.patch.object(module.shutil, "which") + + assert module.main() == 77 + get_password.assert_called_once_with(KEYRING_SERVICE_NAME, KEYRING_USERNAME) + which.assert_not_called() + out = capsys.readouterr().out + assert "SKIP:" in out and "keyring locked" in out + + +def test_repro_windows_ssh_proceeds_past_credential_check(monkeypatch, mocker, capsys): + module = _load_repro_windows_ssh() + monkeypatch.setattr(module.sys, "platform", "win32") + get_password = mocker.patch( + "keyring.get_password", return_value='{"token": "cached"}' + ) + which = mocker.patch.object(module.shutil, "which", return_value=None) + colab = mocker.patch.object(module, "colab") + + assert module.main() == 77 + get_password.assert_called_once_with(KEYRING_SERVICE_NAME, KEYRING_USERNAME) + which.assert_called_once_with("ssh") + colab.assert_not_called() + assert "Windows OpenSSH Client" in capsys.readouterr().out