Set up the box firewall
This page describes the developer’s Mac, where check slots and bin/agentsctl up instances start boxes as Docker containers in Colima. gomoe.dev runs computers on Fargate and keeps them apart with security groups, which How people and computers reach gomoe.dev covers. Nothing on this page applies to gomoe.dev.
A box is a container where an agent runs code, with a browser on the open web. The firewall lets a box reach the internet, the Mac’s machine ports and the VM’s DNS, and no other private address. Boxes on the same Docker bridge can still reach each other, since the bridge passes their traffic without the firewall’s rules. Set it up before a check or an instance starts a box. Without it, containers can reach the other services on the Mac, such as Electric and the stream server.
ops/box-firewall.sh holds the rules. It runs inside the Colima VM each time Colima starts.
What the rules allow
Section titled “What the rules allow”Every container reaches the Mac only on the machine ports, 7700 and 7701. Port 7701 is where checks start their own API’s machine port. Boxes run on Docker’s default bridge, docker0, and get less. A box reaches no other address in these ranges, and nothing of the VM except DNS on port 53. The Mac’s own address in the VM, 192.168.5.2, is in 192.168.0.0/16, and its machine ports are let through before the ranges are dropped.
0.0.0.0/8 10.0.0.0/8 100.64.0.0/10169.254.0.0/16 172.16.0.0/12 192.168.0.0/16198.18.0.0/15 224.0.0.0/4 240.0.0.0/4Answers to connections made from outside a box still pass, so published ports keep working. The Compose services run on their own network and keep what they reach now, except for the Mac.
Apply the script
Section titled “Apply the script”-
Apply it to the running VM. It prints nothing.
Terminal window colima ssh -- sudo sh -s < ops/box-firewall.sh -
Make it last. Colima runs its provision steps at every start. Print the block that holds the script.
Terminal window { printf 'provision:\n - mode: system\n script: |\n'; sed 's/^/ /' ops/box-firewall.sh; } -
Open
~/.colima/default/colima.yamland keep a copy of it. In a new config the line readsprovision: null, so replace that line with the printed block. If the file already has aprovision:list, add the printed- mode: systemitem to the end of that list and keep the items it holds. If one of them is an older copy of the firewall script, replace that one item. -
Keep boxes apart from each other. The script does not do this, since Docker’s default bridge switches packets between two boxes without passing them to iptables. In the same file, set
icc: falseunder thedocker:key, keeping any other settings it holds.docker:icc: falseAfter the restart below, Docker loads
br_netfilterand drops traffic from one box to another ondocker0(-A DOCKER-FORWARD -i docker0 -o docker0 -j DROP). Containers on a network of their own, as Compose’s are, still reach each other by name, asdocs/design/aws.mdrecords. -
Restart Colima. This stops every container for a short time, so run it when no check or run is in progress.
Terminal window colima restart
Running the script again is safe. It empties its own chains and refills them. For a moment during the refill, packets from boxes pass unfiltered.
Check the rules
Section titled “Check the rules”-
List the filter rules. Expect
ACCEPTrules for ports 7700 and 7701 on192.168.5.2, then aDROPfor that address, then-A DOCKER-USER -i docker0 -j AGENTS-BOXES, in that order.Terminal window colima ssh -- sudo iptables -S DOCKER-USER -
List the input rules. Expect
-A INPUT -i docker0 -j AGENTS-BOXES-IN.Terminal window colima ssh -- sudo iptables -S INPUT -
Probe from a container on the default bridge. The first three lines print
dns ok,internet okandmachine port ok. The last line printsVM closed ok.Terminal window docker run --rm alpine:3.20 sh -c 'nslookup example.com >/dev/null 2>&1 && echo "dns ok"nc -z -w 5 example.com 443 && echo "internet ok"nc -z -w 3 192.168.5.2 7700 && echo "machine port ok"nc -z -w 3 172.17.0.1 22 || echo "VM closed ok"'
To remove the box filtering without a restart, delete the two jumps. The rules that limit every container to the machine ports stay.
colima ssh -- sudo sh -c 'iptables -D DOCKER-USER -i docker0 -j AGENTS-BOXES; iptables -D INPUT -i docker0 -j AGENTS-BOXES-IN'