feat(fence): CELLD_FENCE_NETNS for a rootless engine's network namespace
Jason Schwarzenberger authored 1 parent 5b019f8commit 6db89b96f4faf98413dbc5fa55d8a08bf105ca03Browse files
2 files changed+48−2
modifiedHOMEFLARE.md+1−0View file
| @@ -28,6 +28,7 @@ | ||
| 28 | 28 | | `ratelimits` binding | Diffinery limits sign-ins and API calls with `env.RL_*.limit({ key })`. Each entry becomes a binding counted on the node by a `__ratelimit` op: fixed windows of the period (10 or 60 s), keyed by namespace id and key and shared by the node's isolates, as each Cloudflare location counts on its own. | |
| 29 | 29 | | `send_email` binding (Email Sending) | Diffinery sends sign-up codes, invitations and notifications with `env.EMAIL.send({ from, to, subject, html, text })`. The binding checks the message and the binding's `allowed_sender_addresses` / `destination_address` / `allowed_destination_addresses`, then posts it in the REST API's shape (`{address, name}`, `reply_to`, base64 attachments) to `CELLD_EMAIL_URL/send` with `CELLD_EMAIL_TOKEN` as a bearer. Homeflare relays it to Cloudflare's Email Sending API. The result is `{ messageId, delivered, queued, permanent_bounces, suppressed_recipients }`; API errors become the binding's `E_*` codes. The legacy raw `EmailMessage` isn't supported (celld has no `cloudflare:email`). | |
| 30 | 30 | | Container images in parts | `celld deploy` read the whole `docker save` output into memory and wrote it in one `PUT`, which has to finish within the bucket client's 30 s; Diffinery's multi-GB runner image timed out against RustFS, failing every redeploy. The save now streams into a multipart upload of 16 MiB parts (`Bucket::put_large`), completed only once the save exits successfully. A node loading the image downloads it in 16 MiB ranges to a temporary file (`Bucket::get_large`) and streams that to the engine's `images/load` (`Docker::post_file`). Test: `CELLD_TEST_S3_ENDPOINT=... cargo test -p celld --lib large_object_round_trip -- --ignored` against any S3 (moto_server works). | |
| 31 | +| Fence in another network namespace (`CELLD_FENCE_NETNS`) | Under rootless Podman the fence's `--network host` container can't load nftables rules (`netlink: Operation not permitted`), and the container bridges aren't in the host's namespace anyway: they're in Podman's rootless-netns, which the user owns. With `CELLD_FENCE_NETNS=<path>` the fence joins that namespace (`ns:<path>`) and loads `FENCE_RULES_NETNS`: as `FENCE_RULES`, plus `ct state established,related accept` in forward (the node is another container there, so its replies are forwarded) and port 53 to the bridge gateways (aardvark-dns). Unset keeps upstream's behaviour. | | |
| 31 | 32 | | `homeflare:` line in `celld --version` | Lists what this build adds (`HOMEFLARE_FEATURES` in `lib.rs`), so Homeflare sends a config key only to a celld that accepts it. Add a feature there with each patch. | |
| 32 | 33 | |
| 33 | 34 | ## Building |
modifiedcrates/celld/container.rs+47−2View file
| @@ -159,6 +159,45 @@ | ||
| 159 | 159 | } |
| 160 | 160 | "#; |
| 161 | 161 | |
| 162 | +/// homeflare: the fence for a rootless Podman engine, installed in the | |
| 163 | +/// engine's rootless network namespace (`CELLD_FENCE_NETNS`), where the | |
| 164 | +/// bridges live and the engine's user holds `CAP_NET_ADMIN`. Two changes from | |
| 165 | +/// [`FENCE_RULES`], because the node is not that namespace's host but | |
| 166 | +/// another container on another bridge in it. Its traffic to a container is | |
| 167 | +/// forwarded, not input, so the replies are forwarded too and pass on | |
| 168 | +/// `ct state`; the node sits in a private range the forward chain otherwise | |
| 169 | +/// rejects. And Podman's resolver (aardvark-dns) answers on each bridge's | |
| 170 | +/// gateway, an input to the namespace, so port 53 there stays open; the | |
| 171 | +/// containers have no other resolver. Everything else is the same: no new | |
| 172 | +/// connection into the namespace, nothing to the private ranges (the node, | |
| 173 | +/// Homeflare, the LAN through pasta's 169.254 gateway). | |
| 174 | +const FENCE_RULES_NETNS: &str = r#"table inet celld | |
| 175 | +delete table inet celld | |
| 176 | +table inet celld { | |
| 177 | + chain input { | |
| 178 | + type filter hook input priority -10; policy accept; | |
| 179 | + iifname { "celld0", "celld1" } ct state established,related accept | |
| 180 | + iifname { "celld0", "celld1" } meta l4proto { udp, tcp } th dport 53 accept | |
| 181 | + iifname { "celld0", "celld1" } reject | |
| 182 | + } | |
| 183 | + chain forward { | |
| 184 | + type filter hook forward priority -10; policy accept; | |
| 185 | + iifname { "celld0", "celld1" } ct state established,related accept | |
| 186 | + iifname { "celld0", "celld1" } ip daddr { 169.254.0.0/16, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10 } reject | |
| 187 | + iifname { "celld0", "celld1" } ip6 daddr { fe80::/10, fc00::/7 } reject | |
| 188 | + } | |
| 189 | +} | |
| 190 | +"#; | |
| 191 | + | |
| 192 | +/// homeflare: the network namespace the fence joins instead of the host's, | |
| 193 | +/// as the engine names it (`ns:<path>`): a rootless Podman's rootless-netns, | |
| 194 | +/// `$XDG_RUNTIME_DIR/containers/networks/rootless-netns/rootless-netns`. | |
| 195 | +fn fence_netns() -> Option<String> { | |
| 196 | + std::env::var("CELLD_FENCE_NETNS") | |
| 197 | + .ok() | |
| 198 | + .filter(|path| !path.trim().is_empty()) | |
| 199 | +} | |
| 200 | + | |
| 162 | 201 | /// Docker's bridge option that forbids traffic between two containers on |
| 163 | 202 | /// the same bridge. The node still reaches every container, because it |
| 164 | 203 | /// speaks from the host side of the bridge. |
| @@ -704,12 +743,17 @@ | ||
| 704 | 743 | })?; |
| 705 | 744 | self.ensure_image(&image).await?; |
| 706 | 745 | self.networks().await?; |
| 746 | + let netns = fence_netns(); | |
| 747 | + let (rules, network_mode) = match &netns { | |
| 748 | + Some(path) => (FENCE_RULES_NETNS, format!("ns:{path}")), | |
| 749 | + None => (FENCE_RULES, "host".to_string()), | |
| 750 | + }; | |
| 707 | 751 | let body = json!({ |
| 708 | 752 | "Image": image, |
| 709 | - "Env": [format!("CELLD_NFT={FENCE_RULES}")], | |
| 753 | + "Env": [format!("CELLD_NFT={rules}")], | |
| 710 | 754 | "Cmd": ["sh", "-c", "printf '%s' \"$CELLD_NFT\" | nft -f -"], |
| 711 | 755 | "Labels": { "celld.node": self.node, "celld.fence": "1" }, |
| 712 | - "HostConfig": { "NetworkMode": "host", "CapAdd": ["NET_ADMIN"] }, | |
| 756 | + "HostConfig": { "NetworkMode": network_mode, "CapAdd": ["NET_ADMIN"] }, | |
| 713 | 757 | }); |
| 714 | 758 | let created = self |
| 715 | 759 | .docker |
| @@ -767,6 +811,7 @@ | ||
| 767 | 811 | tracing::info!( |
| 768 | 812 | event = "container_bridges_fenced", |
| 769 | 813 | bridges = format!("{OPEN_BRIDGE},{INTERNAL_BRIDGE}"), |
| 814 | + netns = netns.as_deref().unwrap_or("host"), | |
| 770 | 815 | "containers can reach the Internet and nothing of the node's own" |
| 771 | 816 | ); |
| 772 | 817 | *fenced = true; |