Files
devvm/testdata/script/network_rules.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

140 lines
6.3 KiB
Plaintext

# network rules: devvm network shows the allowlist of this folder's VM,
# file by file, after its network policy; network allow, deny and rm
# change the VM's own rules, or with -g every VM's.
env GLOBAL=$XDG_CONFIG_HOME/devvm/policy
# No VM: nothing to show or change, except every VM's rules.
project net
! exec devvm network
stderr '^devvm: no_vm dir=\S*/net$'
! exec devvm network allow example.com
stderr '^devvm: no_vm_policy dir=\S*/net$'
! exec devvm network allow 'a b'
stderr '^devvm: not_a_rule rule=a b$'
! stderr no_vm_policy
exec devvm network allow -g '*.corp.test'
stdout '^==> rule_added_global rule=\*\.corp\.test$'
cmp $GLOBAL $WORK/global.policy
# allow, deny (a deny gets its !, once) and rm edit the VM's own file;
# a rule already there isn't added again, in any case.
exec devvm --network-policy --no-git-protect
vm_name NET net
exec devvm network allow example.com api.example.com:8443
stdout '^==> rule_added rule=example\.com vm=devvm-net-[0-9a-f]{14}$'
stdout '^==> rule_added rule=api\.example\.com:8443 vm=devvm-net-[0-9a-f]{14}$'
exec devvm network deny bad.example.com '!worse.example.com'
stdout '^==> rule_added rule=!bad\.example\.com vm=devvm-net-[0-9a-f]{14}$'
stdout '^==> rule_added rule=!worse\.example\.com vm=devvm-net-[0-9a-f]{14}$'
exec devvm network allow Example.com
stdout '^==> rule_present file=\S*/vms/[0-9a-f]{14}/policy rule=Example\.com$'
exec devvm network rm api.example.com:8443 '!worse.example.com'
stdout '^==> rule_removed rule=api\.example\.com:8443 vm=devvm-net-[0-9a-f]{14}$'
cmp $NET_dir/policy $WORK/vm.policy
# IPv6 rules, with a port in brackets.
exec devvm network allow '[fd00::1]:22' fd00::/8
exec devvm network deny '[::1]:8080'
exec devvm network rm '[fd00::1]:22' fd00::/8 '![::1]:8080'
cmp $NET_dir/policy $WORK/vm.policy
# What isn't a rule is refused, and nothing is added; allow doesn't take
# a deny.
! exec devvm network allow ok.example.com 'a b'
stderr '^devvm: not_a_rule rule=a b$'
! exec devvm network allow 'x;rm'
stderr '^devvm: not_a_rule rule=x;rm$'
! exec devvm network allow '!bad.example.com'
stderr '^devvm: not_a_rule rule=!bad\.example\.com$'
! exec devvm network deny 'example.com # c'
stderr '^devvm: not_a_rule rule='
! exec devvm network allow
stderr '^devvm: arg_missing args=RULE\.\.\. command=devvm network allow$'
! exec devvm network sideways
stderr '^devvm: arg_not_choice choices=on, off command=devvm network given=sideways$'
cmp $NET_dir/policy $WORK/vm.policy
# A rule that isn't there: a warning; the others still go.
exec devvm network rm nothere.test example.com
stderr '^devvm: rule_missing file=\S*/vms/[0-9a-f]{14}/policy rule=nothere\.test$'
stdout '^==> rule_removed rule=example\.com vm=devvm-net-[0-9a-f]{14}$'
exec devvm network allow example.com
exec devvm network rm -g '*.corp.test'
stdout '^==> rule_removed_global rule=\*\.corp\.test$'
exec devvm network allow --global '*.corp.test'
# The VM's network policy (on, or unenforced as the stubs leave it), then
# the rules in effect, file by file, comments left out: devvm's built-in
# policies first, by name, then your files, by path.
exec sh -c 'echo "# mine" >> "$GLOBAL"'
exec devvm network
stdout '\A(on|unenforced)\n# devvm: layers/base\.policy\n'
stdout '^ github\.com$'
stdout '^# \S*/config/devvm/policy\n \*\.corp\.test\n# \S*/vms/[0-9a-f]{14}/policy\n !bad\.example\.com\n example\.com\n'
! stdout mine
! stderr .
# --json: the same, each policy with its rules, a built-in one by name;
# it only shows.
exec devvm network --json
stdout '\A{\n "version": 1,\n "policy": "(on|unenforced)",\n "allowlist": \[\n {\n "builtin": "layers/base\.policy",\n "rules": \[\n'
! stdout '"file": "\S*/layers/base\.policy"'
stdout '^ "file": "\S*/vms/[0-9a-f]{14}/policy",\n "rules": \[\n "!bad\.example\.com",\n "example\.com"\n \],\n "not_rules": \[\]\n'
! stderr .
! exec devvm network --json off
stderr '^devvm: json_shows_only command=devvm network$'
# A line that isn't a rule: devvm network marks it with its number,
# --json lists it, and doctor warns of it.
exec sh -c 'echo "example.com:x # not a port" >> "$GLOBAL"'
exec devvm network
stdout '^# \S*/config/devvm/policy\n \*\.corp\.test\n example\.com:x # not a rule, skipped \(line 4\)\n'
exec devvm network --json
stdout '"rules": \[\n "\*\.corp\.test"\n \],\n "not_rules": \[\n {\n "line": 4,\n "text": "example\.com:x"\n }\n \]\n'
exec devvm doctor
stderr '^devvm: policy_not_rule file=\S*/config/devvm/policy line=4 rule=example\.com:x$'
# Changing the file warns of it too, and a line that reads as a deny but
# isn't a rule warns that the VMs' proxies deny everything: on a change,
# a launch and in doctor.
exec devvm network allow -g ok2.test
stdout '^==> rule_added_global rule=ok2\.test$'
stderr '^devvm: policy_not_rule file=\S*/config/devvm/policy line=4 rule=example\.com:x$'
exec sh -c 'echo "! worse.test" >> "$GLOBAL"'
exec devvm network rm -g ok2.test
stderr '^devvm: policy_broken_deny file=\S*/config/devvm/policy line=5 rule=! worse\.test$'
stderr '^devvm: policy_not_rule file=\S*/config/devvm/policy line=4 rule=example\.com:x$'
# (The stubs leave the VM unenforced, so the launch stops it after warning.)
! exec devvm
stderr '^devvm: policy_broken_deny file=\S*/config/devvm/policy line=5 rule=! worse\.test$'
exec devvm doctor
stderr '^devvm: policy_broken_deny file=\S*/config/devvm/policy line=5 rule=! worse\.test$'
exec sh -c 'grep -v -e "example.com:x" -e "worse.test" "$GLOBAL" > "$WORK/g" && cat "$WORK/g" > "$GLOBAL"'
exec devvm doctor
! stderr 'policy_not_rule|policy_broken_deny'
exec devvm network allow -g ok3.test
! stderr .
exec devvm network rm -g ok3.test
# A VM without network policy: devvm network shows it off, with its
# rules, and changing them says they don't apply.
project off
exec devvm --no-network-policy --no-git-protect
exec devvm network
stdout '\Aoff\n# devvm: layers/base\.policy\n'
! stderr .
exec devvm network allow example.com
stderr '^net_off_policy vm=devvm-off-[0-9a-f]{14}$'
# A new VM doesn't get the rules of one by its name deleted outside devvm.
vm_name OFF off
exists $OFF_dir/policy
exec limactl delete $OFF
exec devvm --no-network-policy --no-git-protect
stdout '^==> creating_from image=base vm=devvm-off-[0-9a-f]{14}$'
! exists $OFF_dir/policy
-- global.policy --
*.corp.test
-- vm.policy --
example.com
!bad.example.com