Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ below corresponds to a tag of the same name.
### Added

- **ssh:** `colab ssh [-s NAME] -- CMD` runs one command on an existing runtime and exits with its status. Output is byte-exact when piped (a PTY only when stdin and stdout are terminals), and it never creates a runtime. Its handshake is tagged `ssh-mode-cmd`.
- **ssh:** `colab ssh` uses `~/.ssh/colab_cli_ed25519` when it exists, ahead of `id_ed25519` and `id_ecdsa`, for the interactive shell, `-- CMD` and `--proxy-mode`.
- **ssh:** Without `--identity` or a usable key, `colab ssh` creates `~/.ssh/colab_cli_ed25519` on first use and reuses it, and tells users with only an RSA key why it did not use that key.
- **ssh:** `colab ssh` names the `IdentityFile` line to add to `~/.ssh/config`
whenever it creates or, in `--proxy-mode`, picks `~/.ssh/colab_cli_ed25519`.

### Removed

Expand All @@ -44,6 +48,12 @@ below corresponds to a tag of the same name.
older versions may contain credentials and can be deleted; if other users
could read an old log, revoke the CLI's access in your Google Account and
sign in again.
- **ssh:** `colab ssh` now checks its key before creating a runtime: a missing
`--identity` file, an empty `--identity` value, or an empty `.pub` with no
private key, or a blocked default key write, exits 2 before creating a
runtime.
- **ssh:** Parallel first runs, such as several ProxyCommands started at once,
share one generated key instead of failing.

## [0.7.1] - 2026-09-14

Expand Down
29 changes: 23 additions & 6 deletions docs/06_ssh_access.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
log:
2026-12-03: `colab ssh` now checks its key before resolving or creating a session (exiting 2 on a missing `--identity` file, accepting either `.pub` or private paths), prefers `~/.ssh/colab_cli_ed25519` ahead of `id_ed25519` and `id_ecdsa`, and generates it on first use when no usable key exists (sharing one key across parallel runs, explaining why `id_rsa` is skipped, and noting the `IdentityFile` line for `~/.ssh/config` users).
2026-11-27: Fixed `colab ssh` under Git for Windows' MSYS `ssh` (`/usr/bin/ssh` in Git Bash): ProxyCommand path arguments now use forward slashes on Windows (bash stripped the backslashes, failing with `D:Projects...python.exe: not found`), and `UserKnownHostsFile` is `/dev/null` when `ssh.exe` is an MSYS/Cygwin build (it wrote the host key to a `./nul` file). Native Windows OpenSSH keeps `nul`. Verified live from Git Bash and PowerShell (OpenSSH_for_Windows 9.5), and `integration/repro_windows_ssh/test.py` passed.
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.
Expand Down Expand Up @@ -38,7 +39,7 @@ colab ssh [OPTIONS] [-- CMD [ARGS]...]
|---|---|---|---|
| `-s`, `--session` | str | auto | Session to connect to. If omitted, uses your only active session, auto-creates one when you have none (not with `-- CMD`), or errors when you have several. |
| `--proxy-mode` | bool | False | Act as an OpenSSH `ProxyCommand`-compatible WebSocket↔stdio bridge (reads stdin, writes stdout) for `~/.ssh/config`. Every other flag still applies. |
| `-i`, `--identity` | str | auto | SSH key (`.pub` public key or private key path) for the public key sent to Colab. Default: first of `~/.ssh/id_ed25519.pub`, `id_ecdsa.pub`. |
| `-i`, `--identity` | str | auto | SSH key (`.pub` public key or private key path) for the public key sent to Colab. Default: first of `~/.ssh/colab_cli_ed25519`, `id_ed25519`, `id_ecdsa` (else `~/.ssh/colab_cli_ed25519` is generated). |
| `--gpu` | str | None | GPU accelerator for a runtime this command creates (T4, L4, G4, H100, A100). |
| `--tpu` | str | None | TPU accelerator for a runtime this command creates (v5e1, v6e1). |
| `--high-mem` | bool | False | Request high-RAM when this command auto-creates a runtime (ignored when connecting to an existing session). |
Expand All @@ -62,6 +63,12 @@ where a bare `colab` may not be on `PATH`. External SSH tools run their own
remote command, so to also land in `/content` add `RequestTTY yes` and
`RemoteCommand cd /content 2>/dev/null; exec bash -l`.

If `colab ssh` created `~/.ssh/colab_cli_ed25519` for you, also add
`IdentityFile ~/.ssh/colab_cli_ed25519`. A config `IdentityFile` works even on
the first connect, when the ProxyCommand creates the key; `ssh -i` with a
missing file does not. `--proxy-mode` prints this line when it picks that key;
adding `--identity ~/.ssh/colab_cli_ed25519` to the ProxyCommand silences it.

### Remote command
`colab ssh [-s NAME] -- CMD [ARGS]...` runs CMD in `/content` on an existing
session (never creating one); every word from CMD on goes to CMD, flags too.
Expand All @@ -88,7 +95,10 @@ session (never creating one); every word from CMD on goes to CMD, flags too.
`colab ssh` uses your only active session; with **no** session it auto-creates
one (mirrors `colab new`: assign → persist `SessionState`; never with
`-- CMD`); with **multiple**
it errors and asks you to pick one with `-s`.
it errors and asks you to pick one with `-s`. Before that, it checks the
key: a missing `--identity` file, an empty `--identity` value, or an empty `.pub` with no private key, or an unusable or uncreatable
default key (such as a stray path or missing `ssh-keygen`), exits 2 before
any session is resolved or created.
2. **Connect**: Opens the WebSocket to `wss://<netloc>/colab/ssh?colab-runtime-proxy-token=<token>`
and sends the resolved public key verbatim in the `X-Colab-Ssh-Pubkey` header
(no transformation -- the bytes the user controls are exactly what the server
Expand All @@ -97,6 +107,11 @@ session (never creating one); every word from CMD on goes to CMD, flags too.
`&colab-client-agent=colab-cli&colab-client-agent=ssh-mode-<mode>`: `interactive`
for plain `colab ssh`, `cmd` for `colab ssh -- CMD`, `proxy` for a
user-written `ProxyCommand` or an IDE.
Without `--identity`, the key is `~/.ssh/colab_cli_ed25519` (with
its `.pub`), then `id_ed25519`, then `id_ecdsa` (each with its `.pub`). The
first is passed to `ssh` as `-i`; the other two leave `ssh` its own key choice.
With none of them, `colab ssh` generates `~/.ssh/colab_cli_ed25519`;
parallel first runs share one key.
3. **Interactive shell**: Spawns the system `ssh` binary with the CLI re-invoked
as its own `ProxyCommand` (`python -m colab_cli.cli ssh --proxy-mode --colab-internal-mode interactive`), so the
WebSocket bridge and the interactive shell share one code path. It forces a
Expand Down Expand Up @@ -149,10 +164,12 @@ session (never creating one); every word from CMD on goes to CMD, flags too.

### Unit tests (`tests/test_ssh.py`)
1. WebSocket URL construction (`wss` for https, `ws` for http; token query param).
2. Pubkey resolution — `--identity` (reads the matching `.pub` file first,
whether passed with or without `.pub`, before falling back to `ssh-keygen -y
-f`) and the `~/.ssh` default scan; missing-key and missing-identity exit
paths.
2. Pubkey resolution & key setup — `--identity` (reads the matching `.pub` file
first, whether passed with or without `.pub`, before falling back to
`ssh-keygen -y -f`), `resolve_default_identity()` (`~/.ssh/colab_cli_ed25519`
-> `id_ed25519` -> `id_ecdsa`), automatic generation and reuse of
`~/.ssh/colab_cli_ed25519` (with RSA notice, `IdentityFile` hint, write-error
cleanup, and parallel first-run sharing), and missing-identity early exits.
3. The full status→message map (400/401/403/404/429/502/other/none).
4. Shell quoting for the `ProxyCommand` string.
5. Session resolution (existing vs missing).
Expand Down
9 changes: 6 additions & 3 deletions integration/repro_ssh/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ done

echo "== A2: an unknown session exits 2 with an actionable message (offline) =="
OFFLINE_CFG="$(mktemp -d)/sessions.json"
A2_OUT="$(uv run colab --config "$OFFLINE_CFG" ssh -s ghost-no-such-session 2>&1)"
A2_KEY="$(mktemp)"
A2_OUT="$(uv run colab --config "${OFFLINE_CFG}" ssh -s ghost-no-such-session \
-i "${A2_KEY}" 2>&1)"
A2_RC=$?
rm -f "${A2_KEY}"
echo "$A2_OUT"
if [ "$A2_RC" -ne 2 ]; then
echo "FAIL: expected exit 2 for an unknown session, got $A2_RC"
fail=1
fi
if ! printf '%s' "$A2_OUT" | grep -q "not found"; then
echo "FAIL: expected a 'not found' message for an unknown session"
if ! printf '%s' "${A2_OUT}" | grep -q "session 'ghost-no-such-session' not found"; then
echo "FAIL: expected the unknown-session message"
fail=1
fi

Expand Down
2 changes: 1 addition & 1 deletion skills/colab-operator/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ by running `uv tool install google-colab-cli` or `pip install google-colab-cli`.
- **Notebooks**: `colab exec -s <name> -f nb.ipynb` runs each code cell and writes results to `<basename>_output.ipynb` next to the input. A `# @title Foo` first line labels the cell in progress output.
- **Exit status**: `colab exec` propagates exit codes with CPython semantics, matching `colab run`: uncaught exceptions exit `1`, `sys.exit()`/`sys.exit(0)` exit `0` silently, `sys.exit(N)` exits `N`, and `sys.exit("msg")` prints `msg` to `stderr` and exits `1` (the persistent IPython kernel reports `SystemExit` as an error message to stay alive, so the CLI translates it back rather than failing `sys.exit(0)` scripts like `argparse`/`unittest`). For `.ipynb` notebooks, execution stops at the first non-zero cell and still writes `<basename>_output.ipynb`. IPython `!cmd` shell escapes do **not** emit a kernel error on a non-zero shell exit — use `colab ssh -s <name> -- <cmd>` or `subprocess.run(..., check=True)` when shell exit status matters.
- **Plots/images**: PNG/JPEG outputs are intercepted. Use `--output-image <path>` on `exec`/`repl` to save to a known location (otherwise a temp path is printed). Inline terminal-image escapes are auto-suppressed when stdout isn't a TTY, so piped/captured output stays clean.
- **Shell**: run a one-shot shell command with `colab ssh -s <name> -- <cmd>`: it runs in `/content` and exits with the command's status. Output is byte-exact when stdout is piped or redirected; ssh allocates a PTY only when stdin and stdout are both terminals, so a shell tool that runs commands in a terminal should add `</dev/null` and redirect the output to a file (a pipe keeps `colab ssh`'s exit status only under `set -o pipefail`). One argument is a shell command line (quote it for pipes or globs: `colab ssh -s <name> -- 'nvidia-smi | head -5'`); several arguments are an exact argv, so pass untrusted values as separate arguments after `--` where the program accepts it (e.g. `colab ssh -s <name> -- ls -- "$f"`), never inside a shell line or a piped script. Pipe a multi-line script to `colab ssh -s <name> -- bash`. Use `colab exec` for Python code. When ssh can't work (no local `ssh` client, no `~/.ssh/id_ed25519` or `id_ecdsa` key and no `-i KEY`, a tool not on the minimal ssh environment's `PATH` such as `gcloud` or `node`, or the runtime's limited concurrent SSH connections in use, which the server answers with 429, e.g. while a person or an IDE is connected), fall back to `exec` with IPython's `!` prefix: `echo '!df -h /content' | colab exec -s <name>` (single quotes stop your local shell expanding `!`). Each `!` line runs in its own shell with no input (a command that prompts hangs), and IPython evaluates `{expr}` as Python even inside quotes and expands `$name` outside single quotes. Start a multi-line script with a `%%bash` line (not with `--env`, which prepends Python lines), use non-interactive flags such as `apt-get -y`, and never put untrusted text in a `%%bash` script or a `!` line (even via `{var}` or `$var`): pass it with `--env NAME=VALUE` and run `subprocess.run(["cmd", "--", os.environ["NAME"]])` from Python. For an interactive shell on the VM, a human runs `colab ssh -s <name>`.
- **Shell**: run a one-shot shell command with `colab ssh -s <name> -- <cmd>`: it runs in `/content` and exits with the command's status. Output is byte-exact when stdout is piped or redirected; ssh allocates a PTY only when stdin and stdout are both terminals, so a shell tool that runs commands in a terminal should add `</dev/null` and redirect the output to a file (a pipe keeps `colab ssh`'s exit status only under `set -o pipefail`). One argument is a shell command line (quote it for pipes or globs: `colab ssh -s <name> -- 'nvidia-smi | head -5'`); several arguments are an exact argv, so pass untrusted values as separate arguments after `--` where the program accepts it (e.g. `colab ssh -s <name> -- ls -- "$f"`), never inside a shell line or a piped script. Pipe a multi-line script to `colab ssh -s <name> -- bash`. Use `colab exec` for Python code. When ssh can't work (no local `ssh` client (or `ssh-keygen` when no usable key exists), a tool not on the minimal ssh environment's `PATH` such as `gcloud` or `node`, or the runtime's limited concurrent SSH connections in use, which the server answers with 429, e.g. while a person or an IDE is connected), fall back to `exec` with IPython's `!` prefix: `echo '!df -h /content' | colab exec -s <name>` (single quotes stop your local shell expanding `!`). Each `!` line runs in its own shell with no input (a command that prompts hangs), and IPython evaluates `{expr}` as Python even inside quotes and expands `$name` outside single quotes. Start a multi-line script with a `%%bash` line (not with `--env`, which prepends Python lines), use non-interactive flags such as `apt-get -y`, and never put untrusted text in a `%%bash` script or a `!` line (even via `{var}` or `$var`): pass it with `--env NAME=VALUE` and run `subprocess.run(["cmd", "--", os.environ["NAME"]])` from Python. For an interactive shell on the VM, a human runs `colab ssh -s <name>`.
- **Never run `colab repl`, `colab ssh` without `-- CMD`, `colab auth`, or `colab drivemount` interactively from an agent** — `repl`, `auth` and `drivemount` expect a TTY and will hang or fail, and bare `colab ssh` with no saved session creates a billable runtime; an interactive shell is for a human. `repl` accepts piped stdin and exits on EOF; `auth`/`drivemount` genuinely require a human at the terminal.

### Ephemeral one-shot jobs (`colab run`)
Expand Down
Loading