mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 16:27:33 -10:00
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]>
264 lines
11 KiB
Plaintext
264 lines
11 KiB
Plaintext
# Network policy for a new VM: the choice, the VM's proxy (devvm _proxy,
|
|
# started before the VM), the confined start, the self-check, and the
|
|
# VM's secrets as the proxy sees them.
|
|
|
|
# --no-network-policy: off, with no proxy and no CA.
|
|
project off
|
|
exec devvm --no-network-policy --no-git-protect
|
|
stdout '^==> creating_from image=base vm=devvm-off-[0-9a-f]{14}$'
|
|
vm_name OFF off
|
|
exec cat $OFF_dir/state
|
|
stdout '^net=off$'
|
|
! stdout 'proxy_|net_check'
|
|
stub_calls
|
|
! stdout curl
|
|
! exists $DATA/ca
|
|
|
|
# --network-policy: devvm's CA (kept out of Time Machine on macOS), the
|
|
# VM's proxy, then the VM started confined, its environment pointing at
|
|
# the proxy with the VM's key, then the self-check, the setup and the
|
|
# shell.
|
|
project net
|
|
stub_reset
|
|
exec devvm --network-policy --no-git-protect
|
|
stdout '^==> starting vm=devvm-net-[0-9a-f]{14}$'
|
|
exists $DATA/ca/ca.pem $DATA/ca/ca.key
|
|
vm_name NET net
|
|
exec cat $NET_dir/state
|
|
stdout '^net=on$'
|
|
stdout '^net_check=ok$'
|
|
stdout '^proxy_key=[0-9a-f]{64}$'
|
|
stdout '^proxy_salt=[0-9a-f]{32}$'
|
|
stdout '^ssh_port=[0-9]+$'
|
|
stdout '^proxy_port=[0-9]+$'
|
|
stub_calls
|
|
[darwin] stdout '^tmutil addexclusion \S*/data/devvm/ca$'
|
|
[darwin] stdout '^limactl edit --tty=false devvm-net-[0-9a-f]{14} --set \.env = \{"HTTPS_PROXY":"http://devvm:devvm-proxy-password@192\.168\.5\.2:[0-9]+",.*"NODE_EXTRA_CA_CERTS":"/etc/ssl/devvm\.pem",.*"NO_PROXY":"localhost,127\.0\.0\.1,::1",.*\} --set \.propagateProxyEnv=false --set \.hostResolver\.enabled=false --set \.dns=\["127\.0\.0\.1"\]$'
|
|
[linux] stdout '^limactl edit --tty=false devvm-net-[0-9a-f]{14} --set \.env = \{"HTTPS_PROXY":"http://devvm:devvm-proxy-password@192\.168\.5\.4:3128",.*\} --set \.propagateProxyEnv=false --set \.hostResolver\.enabled=false --set \.dns=\["127\.0\.0\.1"\]$'
|
|
stdout '^limactl start --tty=false devvm-net-[0-9a-f]{14}\nlimactl shell --workdir / devvm-net-[0-9a-f]{14} sh -c .*/etc/ssh/ssh_host_.*\n(limactl list --json\n)*ssh -F none -M .*\nlimactl shell --workdir / devvm-net-[0-9a-f]{14} sh -c cd ''/'' && exec ''curl'' ''--noproxy'' ''\*'' ''-sS'' ''-m'' ''3'' ''-o'' ''/dev/null'' ''https://1\.1\.1\.1''\n'
|
|
stdout '^limactl shell --workdir /\S*/net devvm-net-[0-9a-f]{14}\n\z'
|
|
# The password is in no command's arguments: devvm wrote it into lima.yaml,
|
|
# the user's alone.
|
|
exec sh -c 'key=$(sed -n "s/^proxy_key=//p" "$NET_dir/state"); ! grep -qF "$key" "$STUB_LOG" && grep -qF "\"HTTPS_PROXY\":\"http://devvm:$key@" "$LIMA_HOME"/devvm-net-*/lima.yaml && ! grep -q devvm-proxy-password "$LIMA_HOME"/devvm-net-*/lima.yaml && ls -l "$LIMA_HOME"/devvm-net-*/lima.yaml'
|
|
stdout '^-rw-------\S* '
|
|
# macOS: the hostagent's profile lets it reach the proxy's port.
|
|
[darwin] exec sh -c 'grep -F "localhost:$(sed -n "s/^proxy_port=//p" "$NET_dir/state")" "$NET_dir/run/sandbox.sb"'
|
|
# The proxy said it listens on that port, for that VM; its password is in
|
|
# a file, never in its arguments.
|
|
exec sh -c 'grep -F "listening on 127.0.0.1:$(sed -n "s/^proxy_port=//p" "$NET_dir/state") for devvm-net-" "$NET_dir/logs/proxy.log"'
|
|
exec cat $NET_dir/run/proxy.auth
|
|
stdout '^[0-9a-f]{64}$'
|
|
exec cat $NET_dir/run/secrets.json
|
|
stdout '^\[\]$'
|
|
# The run log never holds the key.
|
|
exec sh -c 'cat "$DATA"/logs/runs/*.log'
|
|
stdout 'devvm:devvm-proxy-password@'
|
|
! stdout 'devvm:[0-9a-f]{64}@'
|
|
|
|
# A VM devvm started confined that still reaches the internet directly:
|
|
# stopped, no setup, no shell.
|
|
project leak
|
|
stub_reset
|
|
exec sh -c ': > "$STUB_DIR/net-direct"'
|
|
! exec devvm --network-policy --no-git-protect
|
|
stderr '^devvm: net_direct_route vm=devvm-leak-[0-9a-f]{14}$'
|
|
stub_calls
|
|
stdout '^limactl stop devvm-leak-[0-9a-f]{14}$'
|
|
! stdout 'tar -xf'
|
|
! stdout 'limactl shell --workdir /\S*/leak'
|
|
vm_name LEAK leak
|
|
exec cat $LEAK_dir/state
|
|
stdout '^net_check=unenforced$'
|
|
rm $STUB_DIR/net-direct
|
|
|
|
# A running VM whose proxy is gone and can't start again (another program
|
|
# took its port) is stopped, never left running without it.
|
|
cd $WORK/net
|
|
exec sh -c 'pp=$(sed -n "s/^proxy_port=//p" "$NET_dir/state"); pkill -f -- "_proxy -listen 127.0.0.1:$pp "; sleep 0.5; echo k > "$WORK/squat.key"; devvm _proxy -listen "127.0.0.1:$pp" -auth-file "$WORK/squat.key" -lifeline "$WORK/squat.none" >/dev/null 2>&1 & echo $! > "$WORK/squat.pid"; sleep 1'
|
|
stub_reset
|
|
! exec devvm
|
|
stderr '^devvm: net_proxy_down_stopped vm=devvm-net-[0-9a-f]{14}$'
|
|
stderr '^devvm: proxy_port_taken port=[0-9]+ vm=devvm-net-[0-9a-f]{14}$'
|
|
stub_calls
|
|
stdout '^limactl stop devvm-net-[0-9a-f]{14}$'
|
|
! stdout 'limactl shell --workdir /\S*/net'
|
|
exec sh -c 'kill "$(cat "$WORK/squat.pid")"'
|
|
|
|
# Secrets: claude comes with Claude Code. The proxy gets its binding, with
|
|
# the VM's own placeholder and never the token, and its host on 443.
|
|
project agent
|
|
env CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-testfake
|
|
exec devvm --network-policy --no-git-protect --agents claude
|
|
vm_name AGENT agent
|
|
exec cat $AGENT_dir/run/secrets.json
|
|
stdout '^\[\{"name":"claude","hosts":\["api\.anthropic\.com"\],"header":"Authorization","placeholder":"sk-ant-oat01-devvm-proxy-managed-[0-9a-f]{32}"\}\]$'
|
|
! stdout testfake
|
|
exec sh -c 'grep -F "$(sed -n "s/^proxy_salt=//p" "$AGENT_dir/state")" "$AGENT_dir/run/secrets.json"'
|
|
exec cat $AGENT_dir/run/secrets.policy
|
|
stdout '^api\.anthropic\.com:443$'
|
|
# Its proxy's policies: base's and claude's built in, read from the
|
|
# binary, then the files: the user's, the VM's own and its secrets' hosts.
|
|
exec cat $AGENT_dir/run/proxy.policy
|
|
stdout '\Alayers/base\.policy,shared-tools/claude\.policy\n\S*/config/devvm/policy,\S*/data/devvm/vms/[0-9a-f]{14}/policy,\S*/data/devvm/vms/[0-9a-f]{14}/run/secrets\.policy\n\z'
|
|
! exists $XDG_DATA_HOME/devvm/assets
|
|
env CLAUDE_CODE_OAUTH_TOKEN=
|
|
|
|
# A Codex login on the host: its bindings and hosts, with the VM's
|
|
# placeholders.
|
|
mkdir $HOME/.codex
|
|
cp $WORK/codex.json $HOME/.codex/auth.json
|
|
project codex
|
|
exec devvm --network-policy --no-git-protect --agents codex
|
|
vm_name CODEX codex
|
|
exec cat $CODEX_dir/run/secrets.json
|
|
stdout '"name":"codex-key","hosts":\["api\.openai\.com"\]'
|
|
! stdout sk-testfake
|
|
exec cat $CODEX_dir/run/secrets.policy
|
|
stdout '^api\.openai\.com:443$'
|
|
|
|
# devvm network: on, off or unenforced, as devvm ls shows it. on and off
|
|
# change the VM's state, applied when it next starts.
|
|
project none
|
|
! exec devvm network
|
|
stderr '^devvm: no_vm dir=\S*/none$'
|
|
! exec devvm network sideways
|
|
stderr '^devvm: arg_not_choice choices=on, off command=devvm network given=sideways$'
|
|
cd $WORK/off
|
|
exec devvm network
|
|
stdout '^off$'
|
|
exec devvm network on
|
|
stdout '^==> net_set_running state=on vm=devvm-off-[0-9a-f]{14}$'
|
|
stderr '^net_on_old_tokens vm=devvm-off-[0-9a-f]{14}$'
|
|
exec cat $OFF_dir/state
|
|
stdout '^net=on$'
|
|
exec devvm network on
|
|
stdout '^==> net_already state=on vm=devvm-off-[0-9a-f]{14}$'
|
|
! stderr .
|
|
# Running, but not started confined: unenforced until devvm restart.
|
|
exec devvm network
|
|
stdout '^unenforced$'
|
|
exec devvm network off
|
|
stdout '^==> net_set_running state=off vm=devvm-off-[0-9a-f]{14}$'
|
|
! stderr .
|
|
# Stopped, it changes at its next start.
|
|
exec limactl stop $OFF
|
|
exec devvm network on
|
|
stdout '^==> net_set_stopped state=on vm=devvm-off-[0-9a-f]{14}$'
|
|
exec devvm network
|
|
stdout '^on$'
|
|
|
|
# Without a flag, and without a terminal to ask in: on, unasked.
|
|
project default
|
|
exec devvm --no-git-protect
|
|
stdout '^==> creating_from image=base vm=devvm-default-[0-9a-f]{14}$'
|
|
! stderr 'net_about|net_ask|net_off_'
|
|
vm_name DEFAULT default
|
|
exec cat $DEFAULT_dir/state
|
|
stdout '^net=on$'
|
|
stdout '^net_check=ok$'
|
|
|
|
# devvm network -g: what new VMs get, ask until it's saved; ask is only
|
|
# for new VMs.
|
|
exec devvm network -g
|
|
stdout '\Aask # devvm: default\n\z'
|
|
! grep network $DATA/settings.json
|
|
exec devvm network -g --json
|
|
stdout '\A{\n "version": 1,\n "policy": "ask",\n "source": "built-in"\n}\n\z'
|
|
! exec devvm network ask
|
|
stderr '^devvm: net_ask_global$'
|
|
! exec devvm network -g sideways
|
|
stderr '^devvm: arg_not_choice choices=ask, on, off command=devvm network given=sideways$'
|
|
! exec devvm network -g on off
|
|
stderr '^devvm: args_too_many args=\[on\|off\] command=devvm network given=on off$'
|
|
|
|
# A saved off: new VMs get none, unasked and unwarned; --network-policy
|
|
# beats it. The VMs that exist keep theirs.
|
|
exec devvm network -g off
|
|
stdout '^==> network_default mode=off$'
|
|
grep '^ "network": "off",?$' $DATA/settings.json
|
|
exec devvm network -g
|
|
stdout '\Aoff # devvm: settings\.json\n\z'
|
|
exec devvm network -g --json
|
|
stdout '\A{\n "version": 1,\n "policy": "off",\n "source": "settings.json"\n}\n\z'
|
|
project offsaved
|
|
exec devvm --no-git-protect
|
|
! stderr 'net_about|net_ask|net_off_'
|
|
vm_name OFFSAVED offsaved
|
|
exec cat $OFFSAVED_dir/state
|
|
stdout '^net=off$'
|
|
project offflag
|
|
exec devvm --network-policy --no-git-protect
|
|
vm_name OFFFLAG offflag
|
|
exec cat $OFFFLAG_dir/state
|
|
stdout '^net=on$'
|
|
cd $WORK/default
|
|
exec limactl stop $DEFAULT
|
|
exec devvm
|
|
exec cat $DEFAULT_dir/state
|
|
stdout '^net=on$'
|
|
stdout '^net_check=ok$'
|
|
|
|
# A saved on: new VMs get it; --no-network-policy beats it.
|
|
exec devvm network -g on
|
|
stdout '^==> network_default mode=on$'
|
|
project onsaved
|
|
exec devvm --no-git-protect
|
|
! stderr 'net_about|net_ask|net_off_'
|
|
vm_name ONSAVED onsaved
|
|
exec cat $ONSAVED_dir/state
|
|
stdout '^net=on$'
|
|
project onflag
|
|
exec devvm --no-network-policy --no-git-protect
|
|
vm_name ONFLAG onflag
|
|
exec cat $ONFLAG_dir/state
|
|
stdout '^net=off$'
|
|
|
|
# ask again. config.toml's network wins over what devvm saved, devvm
|
|
# network -g shows it came from there, and devvm network -g refuses to
|
|
# change it, naming the file and the key.
|
|
exec devvm network -g ask
|
|
stdout '^==> network_default mode=ask$'
|
|
grep '^ "network": "ask",?$' $DATA/settings.json
|
|
cp $WORK/config-on.toml $CFG/config.toml
|
|
exec devvm network -g
|
|
stdout '\Aon # \S*/config/devvm/config\.toml\n\z'
|
|
exec devvm network -g --json
|
|
stdout '"source": "config.toml"'
|
|
! exec devvm network -g off
|
|
stderr '^devvm: set_in_config file=\S*/config/devvm/config\.toml key=network$'
|
|
grep '^ "network": "ask",?$' $DATA/settings.json
|
|
project configon
|
|
exec devvm --no-git-protect
|
|
! stderr 'net_about|net_ask|net_off_'
|
|
vm_name CONFIGON configon
|
|
catg $CONFIGON_dir/state
|
|
stdout '^net=on$'
|
|
|
|
# A config.toml edited by hand with a typo: every command that reads it is
|
|
# refused, naming the file and the line, with a flag or without.
|
|
cp $WORK/config-typo.toml $CFG/config.toml
|
|
! exec devvm network -g
|
|
stderr '^devvm: net_default_failed reason=\S*/config/devvm/config\.toml:2: unknown key "netwrk"$'
|
|
project badsaved
|
|
! exec devvm --no-git-protect
|
|
stderr '\S*/config/devvm/config\.toml:2: '
|
|
! exec devvm --network-policy --no-git-protect
|
|
stderr '\S*/config/devvm/config\.toml:2: '
|
|
cp $WORK/config-maybe.toml $CFG/config.toml
|
|
! exec devvm network -g
|
|
stderr '^devvm: net_default_failed reason=\S*/config/devvm/config\.toml:1: "maybe" isn''t one of ask, on, off$'
|
|
rm $CFG/config.toml
|
|
exec devvm --network-policy --no-git-protect
|
|
|
|
-- config-on.toml --
|
|
# new VMs get network policy, whatever devvm network -g says
|
|
network = "on"
|
|
-- config-typo.toml --
|
|
# a typo
|
|
netwrk = "off"
|
|
-- config-maybe.toml --
|
|
network = "maybe"
|
|
|
|
-- codex.json --
|
|
{"OPENAI_API_KEY":"sk-testfake-codex","last_refresh":null}
|