Files
devvm/testdata/script/ssh_agent.txtar
T
w3cjandClaude Opus 5.5 d8a7d4de7f refactor(state): a folder per VM, keyed by its project folder's hash
Every file devvm keeps for a project VM is in one folder, vms/<hash>/,
where <hash> is the 14-hex hash of the project folder's path that the VM's
name ends with: its state, its own rules (policy), its backend's files
(backend/: record.json, key, known_hosts, proxy, ports and the port
forwarder's ports.*), what its runs leave (run/: proxy.*, sandbox.sb,
started, lifeline.pid, secrets.*, links.*) and its logs (logs/:
network.jsonl, proxy.log, links.log). Build VMs keep the same layout in
run/builds/<name>/. The top-level run/ holds only what isn't one VM's or
must be short: the hashed control sockets, links' link-<hash>.sock/.ctl,
the QEMU wrapper and the ports lock.

A folder's VM is found by its path's hash (state.ReadHash), with the name
and backend its state records, so devvm --backend docker then devvm in
the same folder opens the one VM, and a backend's name limit applies to
new VMs only. The state records the VM's name and its ssh alias, chosen
once at creation (sshconfig.ChooseAlias) and never recomputed. Links'
targets match recorded names, aliases and folders (project.Targets).

One teardown (vmFolders.teardown) replaces Lifecycle.forget,
undoCreate's own list, Leftovers.Forget's, Proxy.Forget and space's
leftoverFiles: it stops the proxy and link session, then the pieces
outside the folder (clone remote or .git locks, other VMs' links and the
group, host keys, the backend's leftovers), removes the folder, and
writes the ssh config. Each backend's Delete and Forget remove the VM's
backend/ folder (vmssh.Forwards.Forget). A new VM made where an earlier
one's folder was left clears it first. devvm space counts an orphan's
whole folder as leftover state and the other VMs' logs as network logs.

Path builders move into paths: Dirs.VMDir, Dirs.VM and paths.VM's files
(secrets' BindingsFile/PolicyFile, netpolicy's run, EventLog, ProxyLog,
LinkList, VMPolicyFile, Lima's profileFile, startedFile, forwards.file,
vmssh.Forwards.file, links' LinksLog/linksPID, Docker's vmFile and
lifeline), ToolLockConfig, ToolLockChecked and ToolsCheckLog; the
user's cache folder for Lima's and smolvm's caches is Dirs.UserCache,
from HOME, not os.UserCacheDir. Dirs.VMState, LimaVMs, SmolvmVM and
DockerVM go. vmname.HashOf and IsHash, project.Hash.

The proxy takes the links' sockets' folder (-link-sockets, devvm's run
folder) apart from the links' list, which is in the VM's run folder.

Tests: a VM made and removed (rm, gc --orphans) leaves nothing under
vms/, and with network policy has no file named for it outside its
folder (TestTeardownLeavesNothing); a new VM over an old folder starts
clean; lookup finds a VM made on another backend with a long folder name
and makes no second one (TestLookupOtherBackend, and in docker.txtar);
the alias stays put when a second VM shares the folder name
(TestChooseAlias, ssh.txtar); every socket fits under a 35-character
home with the longest VM name (TestSocketsFit); space counts an
orphan's whole folder (TestLeftoverFolders); paths.VM's files; state by
hash and name; the proxy refuses a socket in the list's own folder.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 08:16:39 -07:00

188 lines
7.7 KiB
Plaintext

# SSH agent forwarding: off unless asked for; --ssh-agent forwards yours,
# devvm ssh-agent devvm devvm's own, which holds only the keys added to it
# and not removed with devvm ssh-agent rm (the ssh-agent and ssh-add
# stubs). Every connection devvm opens to a VM
# carries the agent its mode says, or none. Your own agent is a made-up
# socket the stubs never answer on.
env SSH_AUTH_SOCK=$WORK/user.sock
# No VM yet.
project web
! exec devvm ssh-agent
stderr '^devvm: no_vm_ssh_agent dir=\S*/web$'
! exec devvm ssh-agent maybe
stderr '^devvm: arg_not_choice choices=on, devvm, off command=devvm ssh-agent given=maybe$'
# A new VM: no forwarding, and none of devvm's connections to it carries
# your agent. An SSH origin says so.
exec git init -q
exec git remote add origin [email protected]:o/r.git
exec devvm --no-network-policy --no-git-protect
stderr '^devvm: ssh_remote_hint vm=devvm-web-[0-9a-f]{14}$'
vm_name WEB web
exec cat $WEB_dir/state
stdout '^ssh_agent=off$'
stub_calls
stdout '^limactl clone .* --set \.ssh\.forwardAgent=false '
exec sh -c 'cat "$STUB_DIR"/start-agent-devvm-web-* "$STUB_DIR/shell-agent"'
stdout '\Aunset\nunset\n\z'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^ ForwardAgent no$'
! stdout IdentityAgent
exec devvm ssh-agent
stdout '\Aoff\n\z'
exec devvm ssh-agent --json
stdout '\A{\n "version": 1,\n "mode": "off",\n "keys": \[\]\n}\n\z'
exec devvm --ssh-agent
stderr '^devvm: ssh_agent_flag_ignored$'
# --ssh-agent: yours, checked first (not running here, with a key in
# ~/.ssh: it says so), on every connection; no hint for an SSH origin.
project fwd git
exec git remote add origin [email protected]:o/r.git
mkdir $HOME/.ssh
exec sh -c 'echo k > "$HOME/.ssh/id_ed25519"'
stub_reset
exec devvm --no-network-policy --no-git-protect --ssh-agent
! stderr ssh_remote_hint
stderr '^devvm: no_ssh_agent$'
vm_name FWD fwd
exec cat $FWD_dir/state
stdout '^ssh_agent=on$'
stub_calls
stdout '^limactl clone .* --set \.ssh\.forwardAgent=true '
stdout '^ssh-add -l SOCK=\S*/user\.sock$'
exec sh -c 'cat "$STUB_DIR"/start-agent-devvm-fwd-* "$STUB_DIR/shell-agent"'
stdout '\A\S*/user\.sock\n\S*/user\.sock\n\z'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-fwd devvm-fwd-[0-9a-f]{14}\n( .*\n)* ForwardAgent yes\n'
! stdout IdentityAgent
exec devvm ssh-agent
stdout '\Aon\n\z'
rm $HOME/.ssh
# devvm ssh-agent add: the key and its fingerprint recorded, devvm's agent
# started, the key loaded there, kept 12 hours.
mkdir $WORK/keys
exec sh -c 'echo deploy > "$WORK/keys/deploy"'
stub_reset
exec devvm ssh-agent add $WORK/keys/deploy
stdout '^==> agent_holds keys=deploy$'
stderr '^agent_holds_note$'
exec sh -c 'cat "$DATA/agent-keys.json"'
stdout '^ "file": "\S*/keys/deploy",\n "fingerprint": "SHA256:fp-deploy"\n }\n'
! stdout 'lifetime|confirm'
stub_calls
stdout '^ssh-agent -a \S*/data/devvm/agent\.sock$'
stdout '^ssh-add -t 12h key:deploy$'
! stdout 'user\.sock'
exec devvm ssh-agent add $WORK/keys/deploy --lifetime 30m
exec sh -c 'cat "$DATA/agent-keys.json"'
stdout '^ "fingerprint": "SHA256:fp-deploy",\n "lifetime": "30m"\n'
exec devvm ssh-agent add $WORK/keys/deploy
! exec devvm ssh-agent add $WORK/keys/none
stderr '^devvm: agent_key_not_found key=\S*/keys/none$'
! exec devvm ssh-agent add $WORK/keys/deploy --lifetime soon
stderr '^devvm: bad_duration value=soon$'
! exec devvm ssh-agent add $WORK/keys/deploy --lifetime ''
stderr '^devvm: flag_blank flag=lifetime$'
exec devvm ssh-agent add $WORK/keys/deploy --lifetime 2w
exec sh -c 'cat "$DATA/agent-keys.json"'
stdout '"lifetime": "2w"\n'
exec devvm ssh-agent add $WORK/keys/deploy
env SSH_ASKPASS=
! exec devvm ssh-agent add $WORK/keys/deploy --confirm
stderr '^devvm: agent_confirm_needs_askpass$'
# A key in a folder a VM mounts: that VM could read it.
exec sh -c 'echo inside > "$WORK/web/key"'
! exec devvm ssh-agent add $WORK/web/key
stderr '^devvm: agent_key_mounted key=\S*/web/key vm=devvm-web-[0-9a-f]{14}$'
# Bare, once a key is added: the VM's mode, then the keys devvm's agent
# holds and those added to it.
exec devvm ssh-agent
stdout '\Aon\n256 SHA256:fp-deploy deploy \(ED25519\)\n\S*/keys/deploy: SHA256:fp-deploy lifetime=12h\n\z'
exec devvm ssh-agent --json
stdout '\A{\n "version": 1,\n "mode": "on",\n "keys": \[\n {\n "file": "\S*/keys/deploy",\n "fingerprint": "SHA256:fp-deploy",\n "confirm": false,\n "lifetime": "12h",\n "loaded": true\n }\n \]\n}\n\z'
! exec devvm ssh-agent --json off
stderr '^devvm: json_shows_only command=devvm ssh-agent$'
# devvm ssh-agent devvm on the running VM: from its next start. Until
# then devvm's connections and its ssh config entry carry the agent it
# started with (none), a launch from another folder's included.
cd $WORK/web
exec devvm ssh-agent devvm
stderr '^devvm: agent_devvm_risk vm=devvm-web-[0-9a-f]{14}$'
stdout '^==> agent_set_running mode=devvm vm=devvm-web-[0-9a-f]{14}$'
exec devvm ssh-agent
stdout '\Adevvm\n256 SHA256:fp-deploy '
exec devvm ssh-agent devvm
stdout '^==> agent_already mode=devvm vm=devvm-web-[0-9a-f]{14}$'
exec devvm
stderr '^devvm: agent_next_start vm=devvm-web-[0-9a-f]{14}$'
exec sh -c 'cat "$STUB_DIR/shell-agent"'
stdout '\Aunset\n\z'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-web devvm-web-[0-9a-f]{14}\n( .*\n)* ForwardAgent no\n'
cd $WORK/fwd
exec devvm
cd $WORK/web
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-web devvm-web-[0-9a-f]{14}\n( .*\n)* ForwardAgent no\n'
stdout '^Host devvm-fwd devvm-fwd-[0-9a-f]{14}\n( .*\n)* ForwardAgent yes\n'
! stdout IdentityAgent
# Restarted: its backend forwards an agent, devvm's, with the keys its
# agent lacks loaded again (after a reboot here); so do devvm's
# connections, and its entry, which names it as the agent, so OpenSSH
# forwards it however the editor was started.
rm $STUB_DIR/agent-up
stub_reset
exec devvm restart
stub_calls
stdout '^limactl edit --tty=false devvm-web-[0-9a-f]{14} --set \.ssh\.forwardAgent=true$'
stdout '^ssh-agent -a \S*/data/devvm/agent\.sock$'
stdout '^ssh-add -t 12h key:deploy$'
exec sh -c 'cat "$STUB_DIR"/start-agent-devvm-web-* "$STUB_DIR/shell-agent"'
stdout '\A\S*/data/devvm/agent\.sock\n\S*/data/devvm/agent\.sock\n\z'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-web devvm-web-[0-9a-f]{14}\n( .*\n)* ForwardAgent "\S*/data/devvm/agent\.sock"\n ControlMaster no\n ControlPath none\n IdentityAgent "\S*/data/devvm/agent\.sock"\n'
# Off again: from its next start; until then devvm's connections and its
# entry keep devvm's agent, then carry none.
exec devvm ssh-agent off
stdout '^==> agent_set_running mode=off vm=devvm-web-[0-9a-f]{14}$'
! stderr risk
exec devvm
exec sh -c 'cat "$STUB_DIR/shell-agent"'
stdout '\A\S*/data/devvm/agent\.sock\n\z'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-web devvm-web-[0-9a-f]{14}\n( .*\n)* ForwardAgent "\S*/data/devvm/agent\.sock"\n'
exec devvm restart
exec sh -c 'cat "$STUB_DIR/shell-agent"'
stdout '\Aunset\n\z'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-web devvm-web-[0-9a-f]{14}\n( .*\n)* ForwardAgent no\n'
# devvm ssh-agent rm: the key out of devvm's agent and agent-keys, found
# by its fingerprint, the key file left as it is. One not added is
# refused, and changes nothing.
! exec devvm ssh-agent rm $WORK/keys/none
stderr '^devvm: agent_key_not_recorded key=\S*/keys/none$'
exec sh -c 'cat "$DATA/agent-keys.json"'
stdout 'keys/deploy'
stub_reset
exec devvm ssh-agent rm $WORK/keys/deploy
stdout '^==> agent_key_removed key=\S*/keys/deploy$'
stub_calls
stdout '^ssh-add -d key:deploy$'
exec sh -c 'cat "$DATA/agent-keys.json"'
stdout '"keys": \[\]'
! stdout deploy
exists $WORK/keys/deploy
exec sh -c 'cat "$STUB_DIR/agent-held"'
! stdout deploy
exec devvm ssh-agent
stdout '\Aoff\n\z'
! exec devvm ssh-agent rm $WORK/keys/deploy
stderr '^devvm: agent_key_not_recorded '