git clone https://garage44.eu/jeroen/midas.git.. /node/entrypoint.sh
#!/bin/bash
# Fence the node off and serve ssh. Runs as the container's root, which in a
# rootless container is the node owner's own uid on the host: no host privilege.
#
# The fence goes up before sshd listens, so there is no moment in which the node
# can reach the machine's LAN.
set -euo pipefail
CONFIG=/config/node.env
[ -r "$CONFIG" ] || { echo "midas node: $CONFIG is missing (install.sh writes it)" >&2; exit 1; }
# shellcheck source=/dev/null
. "$CONFIG"
hub=${MIDAS_HUB#http://}; hub_ip=${hub%:*}; hub_port=${hub##*:}
# The resolvers podman wrote are usually private addresses (pasta's forwarder):
# DNS to them, and only DNS, stays open.
dns4=$(awk '$1=="nameserver" && $2 ~ /^[0-9.]+$/ {printf "%s%s", s, $2; s=", "}' /etc/resolv.conf)
[ -n "$dns4" ] || { echo "midas node: no IPv4 nameserver in /etc/resolv.conf" >&2; exit 1; }
# The machine's own public addresses (install.sh): its web ports, which is what
# anybody on the internet reaches, and nothing else on it. The VPS listens on
# 22, Forgejo's 2222 and more on every address, and the internet's view of it is
# filtered upstream of the machine, so a node going through the machine itself
# would otherwise see past that.
host_rules=""
if [ -n "${MIDAS_HOST_PUBLIC:-}" ]; then
host_rules=" ip daddr { $MIDAS_HOST_PUBLIC } tcp dport { 80, 443 } accept
ip daddr { $MIDAS_HOST_PUBLIC } drop"
fi
# Outbound: the internet yes (crates.io, git, and the tile, aerial and register
# servers a capture fetches from); the hub's lease service yes; nothing else
# private — not this machine, not its LAN, not the carrier's network.
# Inbound: ssh, which pasta only ever delivers from the machine's WireGuard
# address; replies on connections that already exist.
nft -f - <<EOF
flush ruleset
table inet midas {
set private4 {
type ipv4_addr; flags interval
elements = { 0.0.0.0/8, 10.0.0.0/8, 100.64.0.0/10, 127.0.0.0/8,
169.254.0.0/16, 172.16.0.0/12, 192.0.0.0/24,
192.168.0.0/16, 198.18.0.0/15, 224.0.0.0/4, 240.0.0.0/4 }
}
set private6 {
type ipv6_addr; flags interval
elements = { ::1/128, fc00::/7, fe80::/10, ff00::/8 }
}
chain output {
type filter hook output priority 0; policy accept;
oif lo accept
ct state established,related accept
ip daddr { $dns4 } meta l4proto { udp, tcp } th dport 53 accept
ip daddr $hub_ip tcp dport $hub_port accept
ip daddr $hub_ip tcp dport 22 accept
$host_rules
ip daddr @private4 drop
ip6 daddr @private6 drop
}
chain input {
type filter hook input priority 0; policy drop;
iif lo accept
ct state established,related accept
tcp dport 2222 accept
}
}
EOF
# The checkout was /work/slot until 2026-10-13: move it, build directory and all.
[ -d /work/slot ] && [ ! -e /work/checkout ] && mv /work/slot /work/checkout
mkdir -p /work/.ssh-host /work/checkout /run/sshd
[ -f /work/.ssh-host/ssh_host_ed25519_key ] || \
ssh-keygen -q -t ed25519 -N '' -f /work/.ssh-host/ssh_host_ed25519_key
chown build:build /work /work/checkout
# The node's own key for the hub's git mirror. The hub accepts it only with a
# forced `midas hub git` command: git fetches and LFS downloads, nothing else.
# The node registers it (and its host key) with the hub itself, below.
su build -c '
mkdir -p /work/.ssh-node ~/.ssh && chmod 700 /work/.ssh-node ~/.ssh
[ -f /work/.ssh-node/id_ed25519 ] || ssh-keygen -q -t ed25519 -N "" \
-C "midas-node-'"${MIDAS_NODE:-?}"'" -f /work/.ssh-node/id_ed25519
cat > ~/.ssh/config <<EOF
Host hub
HostName '"$hub_ip"'
User midas
IdentityFile /work/.ssh-node/id_ed25519
IdentitiesOnly yes
StrictHostKeyChecking accept-new
UserKnownHostsFile /work/.ssh-node/known_hosts
EOF
git config --global init.defaultBranch main
git config --global safe.directory /work/checkout
'
echo "midas node ${MIDAS_NODE:-?}: git key $(cat /work/.ssh-node/id_ed25519.pub)"
# Tell the hub both keys; it believes them because they come from this node's
# own WireGuard address. Again every minute until the hub has answered.
( set +e
until /usr/local/libexec/midas-owner node register; do sleep 60; done
) &
# The checkout, cloned from the hub's mirror the first time — in the background,
# and again every minute until it works, because the registration above may not
# have reached the hub yet and sshd should not wait for that.
( set +e
until [ -d /work/checkout/.git ]; do
# A clone cut off by a restart leaves this behind, and git will not clone into it.
rm -rf /work/checkout.new
su build -c 'git clone --quiet hub:'"${MIDAS_REPO:?no repository}"'.git /work/checkout.new \
&& mv /work/checkout.new/.git /work/checkout/ && rm -rf /work/checkout.new \
&& git -C /work/checkout reset --quiet --hard' \
&& echo "midas node: /work/checkout cloned from the hub" && break
rm -rf /work/checkout.new
sleep 60
done
) &
# The GPU memory guard (DESIGN.md §9), as `build` — the user every session runs
# as. That is least privilege and also the only way it works: reading another
# user's /proc/<pid>/fdinfo needs CAP_SYS_PTRACE, which the container's root
# does not have, and as root the guard saw no process holding GPU memory at all
# (2026-10-11). Restarted if it dies.
# `set +e` inside: the script runs under -e, and without it the loop itself
# exited the first time the guard did (a deploy's stop-guard left both nodes
# unguarded until this was found, 2026-10-11).
( set +e; while :; do su build -s /bin/sh -c '/usr/local/bin/midas node guard'; sleep 5; done ) &
# The midas the hub runs, taken while nobody holds the node (DESIGN.md §3):
# from the owner's copy, as root, which runs the hub's only as `build`. This
# script, the fence above and the container around it stay the owner's.
( set +e; sleep 30; while :; do /usr/local/libexec/midas-owner node update; sleep 300; done ) &
echo "midas node ${MIDAS_NODE:-?}: fenced, sshd starting"
exec /usr/bin/sshd -D -e -f /etc/ssh/sshd_config