mirror of
https://github.com/jmcorgan/fips.git
synced 2026-10-05 19:18:25 +00:00
A client process opens a flow to a peer's public key on a chosen port and sends and receives datagrams on a file descriptor the daemon hands it. No IPv6 emulation, no TUN device, no DNS: a datagram travels from key to key. The feature is off by default and is not a stable interface. The wire needs no change and gets none. Every FSP data packet has carried a port pair inside its AEAD envelope since v0.2.0, and port 256 is simply the IPv6 shim. What was missing was a way for a program to ask for a port of its own and be handed the traffic. Addressing is the part worth reading twice, because the obvious design is wrong. The x-only public key is the address. An npub is that key written in bech32, so converting between them is a local encoding rather than a lookup or a name service. The 16-byte node address that travels on the wire is the first half of a SHA-256 of the key: it is a truncated hash, it does not invert, and it appears nowhere a client can see. An earlier iteration of this work reported a peer by that hash and could supply a key only sometimes, which is what treating a wire identifier as an identity produces. An accepted flow therefore always knows its peer. The key is captured where the peer is authenticated rather than looked up when a report is rendered: every inbound datagram passes one call site inside a handler that refuses anything whose session is not established, and the responder has already rejected the session unless the claimed address derives from the key it proved. Reaching for the identity cache instead gives a best-effort answer from a structure that evicts. A listener is a descriptor. The daemon writes one message per arrival to it, carrying the new flow's descriptor and the peer's address, so poll, select and epoll work on a listener and accepting is a recvmsg. That is what lets the API be used from a program that already has an event loop, which a command-and-reply listener could not support: an arrival could not be waited on beside anything else. There is no accept command and no reject command. Refusing a flow is closing the descriptor you were handed. The Rust surface mirrors std::net. FipsStream::connect, FipsListener::bind, incoming, accept, io::Result and an errno mapping rather than a bespoke error type. An address is given as an npub, as a key, or as a pair, through one parameter, the way ToSocketAddrs takes several spellings of one thing. Each type holds its descriptor and copies of what setup told it and nothing else, so a stream that outlives its setup connection is not representable. set_nonblocking, AsFd and the four deadline methods carry the names and signatures std::net uses for the same jobs. They were asked for by a user integrating the API with tokio: AsyncFd requires a non-blocking descriptor, and anything receiving from a peer needs a bounded wait. AsFd is the better of the two descriptor accessors, because the borrow cannot outlive the value that owns the descriptor, so a reactor cannot hold a registration for a descriptor that has since been closed and its number reused by the next open. The non-blocking flag is read, modified and written back rather than assigned, since the flag word carries more than that one bit and a caller may have set O_ASYNC. A zero timeout is refused with EINVAL, because the kernel reads a zero timeval as "wait for ever", which inverts what a caller passing zero means; std::net refuses it for the same reason. The two directions are separate options and stay that way. FipsListener gets no timeout methods, matching TcpListener: bounding an accept is set_nonblocking plus the caller's own poll, which the reactor how-to builds. A flow taken from accept is blocking whatever the listener was set to, because the two are separate sockets and the daemon hands over a fresh one. One rule has no counterpart in Berkeley sockets and a client author must know it: the v1 wire carries no half-close, so nothing peer-driven ever closes a flow. A server written to read until the flow ends waits for a signal that cannot arrive, holding a thread and a flow per peer until its process exits. A program decides its own termination, and the example serves one datagram per flow. The tests reach a live daemon rather than a stand-in. Every public item had a unit test against a hand-written stand-in with canned replies, and the five entry points a program actually calls first, connect, connect_from, connect_at, bind and the SOCKET constant, had no coverage of any kind, because the tests that appear to cover them build a Wire over a socket pair and hand it to the private open and hold, so nothing ever resolved a socket path or mapped its errors. examples/native-surface.rs walks all thirty-eight items against a running daemon and reports the number of assertions it made. The count is read from the recorder rather than written as a literal, and the harness asserts the exit status, the completion marker and the count together, so deleting an assertion fails the check rather than quietly shrinking it. Watchdogs turn a hang into a named failure, which several of the walked behaviours would otherwise produce. The shared Docker image is built once for every integration leg, so the new binary is staged at all ten places the existing one is, the interop builder included, which gets a stub because those images exercise the wire between daemon versions and older refs do not carry the example. The platform gating was tested rather than reasoned about: flipping all eleven gates so the native API is excluded leaves the crate compiling clean across the workspace, every target and the profiling feature. The shipped docs tree gains what only the LaTeX manual under design/ had, which is not published with the daemon. A reference entry covers the whole surface: addressing and the port tiers, the Berkeley mapping, every method on FipsAddr, FipsStream, FipsListener and Incoming, the errno table, the ceilings, the four places data disappears with nothing reported, the line protocol and the command reference. The errno table gives names rather than numbers, since the client maps each name onto the libc constant for the platform it was built for and the supported platforms disagree on the numbers. A tutorial side trip stands up two throwaway nodes on one machine, peered over loopback UDP with no TUN and no DNS, then writes a listening program and a connecting program against them; it needs neither the public mesh nor root, because the native path is the one that does not go through the IPv6 adapter. The obligations a client in another language carries are a how-to of their own, since they are a task rather than a description: reading the setup connection with recvmsg, associating a descriptor with the last complete line, telling an empty datagram from a close, and six others. Serving many peers from one poll loop is another, with the whole program, because the straightforward listener spawns a thread per flow and that is wrong at the node's ceiling of 256. The drop causes are a table mapping each of the seven texts DropReason::as_str produces to the counter it increments, with drop_oversize called out as the ninth counter that is not in the table. What a daemon restart costs is a section of its own: every flow and listener ends, descriptors do not survive, there is no resumption, and datagrams sent but not yet forwarded are lost through a window nothing bounds. A stack comparison diagram places the interface against the stack a reader already knows: the same application over HTTP, TLS, TCP, IP and Ethernet on one side, and over its own format, FSP, FMP and a FIPS transport on the other, aligned so each row is one concern. The two columns are not alternatives and are not drawn as such. An unmodified IPv6 program's packets reach fips0, and the adapter hands each one to FSP as a payload, so the left stack runs inside the right one; the left column ends at a fork, eth0 for the ordinary internet and fips0 for the mesh, and an arrow leaves fips0 and runs back up into FSP's input. The row where TCP would be is empty on purpose and names Reliable Object Delivery, which is where that capability is expected to land. ROD is a v2 capability, the box is dashed because none of it exists yet, and the design entry says the part a reader needs most: nothing on the surface anticipates it, so a program written today should assume it does not exist. Both endpoints carry a scheme and a worked port, https://<npub>.fips:443 and fips://<npub>:443, with a footnote saying the two ports are not the same kind of thing, a TCP port inside the tunnel on the left and an FSP port on the right. The fips:// form is a coinage: nothing in the tree parses it, nothing registers the scheme, and the API takes a key and a port as separate arguments rather than a URL. The diagram also says where the right column stops, since FIPS over UDP still rides IP and Ethernet beneath. It appears in fips-concepts.md and fips-ipv6-adapter.md, which were making its argument in prose without a picture, and deliberately not in fips-architecture.md, which already carries the OSI mapping and makes the same point about the transport row. The gateway's control socket moves onto the same bind policy this API uses, which is the one change here that touches deployed behaviour: fips-gateway now tightens /run/fips to 0750. That is unreachable under the packaged deployment, where fips.service has already created the directory at that mode, and reachable for a source build or a container that starts the gateway alone. One changelog entry under Added, describing the released state: what a client opens and reads, the addressing and why the node address is not it, the listener being a descriptor, the std::net shape of the Rust surface, and the one rule Berkeley sockets have no counterpart for. It says in as many words that the wire is unchanged.
247 lines
9.2 KiB
Bash
Executable File
247 lines
9.2 KiB
Bash
Executable File
#!/bin/bash
|
|
# Mixed-version interop harness: build three Docker images, one per git ref.
|
|
#
|
|
# Given three git refs, this script builds the FIPS daemon from each ref and
|
|
# produces three distinct Docker images:
|
|
#
|
|
# fips-interop:a <- ref A ("version under test")
|
|
# fips-interop:b <- ref B ("parent")
|
|
# fips-interop:c <- ref C ("release")
|
|
#
|
|
# It deliberately does NOT reuse the fips-test:latest tag — that tag is owned
|
|
# by the flake-lab and the static suite, which bake a single binary set for
|
|
# all nodes. This harness needs a different binary set per node, so each ref
|
|
# gets its own tag.
|
|
#
|
|
# Mechanism:
|
|
# 1. For each ref, `git worktree add --detach` a temp checkout of this repo.
|
|
# 2. `cargo build --release` the four binaries in that worktree.
|
|
# 3. Copy fips/fipsctl/fipstop/fips-gateway into a per-ref build context.
|
|
# 4. `docker build` that context with the testing/docker Dockerfile, tagging
|
|
# fips-interop:<slot>.
|
|
# 5. Remove the temp worktree.
|
|
#
|
|
# The harness reuses the testing/docker/Dockerfile (and its entrypoint.sh,
|
|
# resolv.conf), so the runtime environment is identical to the static and
|
|
# rekey suites — only the daemon binaries differ between the three images.
|
|
#
|
|
# Idempotent and safe to re-run: each invocation rebuilds all three images
|
|
# from scratch and cleans up its worktrees, including stale worktrees from a
|
|
# previous interrupted run.
|
|
#
|
|
# Usage:
|
|
# ./build-images.sh <ref-a> <ref-b> <ref-c>
|
|
#
|
|
# Example (the motivating run):
|
|
# ./build-images.sh fix/fsp-rekey-overlapping-epoch 79975d72 v0.3.0
|
|
#
|
|
# Environment:
|
|
# FIPS_INTEROP_KEEP_WORKTREES=1 Keep temp worktrees after build (debug).
|
|
# CARGO_BUILD_JOBS=N Passed through to cargo if set.
|
|
# FIPS_INTEROP_RUNS_DIR=DIR Root for harness scratch dirs (.build/,
|
|
# .stress-runs/, generated-configs/). When
|
|
# unset, falls back to in-tree paths under
|
|
# testing/interop/ and prints a warning to
|
|
# stderr; set it to a path outside the
|
|
# source tree to keep generated artefacts
|
|
# out of the checkout.
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
|
DOCKER_CTX_SRC="$REPO_ROOT/testing/docker"
|
|
|
|
# ── Scratch-dir root ─────────────────────────────────────────────────
|
|
#
|
|
# FIPS_INTEROP_RUNS_DIR controls where the harness writes its scratch
|
|
# directories (.build/, .stress-runs/, generated-configs/). When unset
|
|
# we fall back to in-tree paths under testing/interop/ and warn the
|
|
# operator, so the warning fires exactly once per invocation. When a
|
|
# parent script has already warned it exports _FIPS_INTEROP_WARNED=1
|
|
# to suppress duplicate warnings in child scripts.
|
|
if [[ -n "${FIPS_INTEROP_RUNS_DIR:-}" ]]; then
|
|
RUNS_BASE="$FIPS_INTEROP_RUNS_DIR"
|
|
mkdir -p "$RUNS_BASE"
|
|
else
|
|
RUNS_BASE="$SCRIPT_DIR"
|
|
if [[ -z "${_FIPS_INTEROP_WARNED:-}" ]]; then
|
|
echo >&2 "WARNING: FIPS_INTEROP_RUNS_DIR not set; harness output will be written under the source tree at $RUNS_BASE. Set FIPS_INTEROP_RUNS_DIR to a path outside the source tree to avoid this."
|
|
export _FIPS_INTEROP_WARNED=1
|
|
fi
|
|
fi
|
|
|
|
# Per-ref tag slots. Order matters: slot[i] <- ref[i].
|
|
SLOTS=(a b c)
|
|
|
|
# ── Args ─────────────────────────────────────────────────────────────
|
|
|
|
if [ "$#" -ne 3 ]; then
|
|
echo "Usage: $0 <ref-a> <ref-b> <ref-c>" >&2
|
|
echo "" >&2
|
|
echo " ref-a version under test -> fips-interop:a" >&2
|
|
echo " ref-b parent / comparison -> fips-interop:b" >&2
|
|
echo " ref-c release baseline -> fips-interop:c" >&2
|
|
exit 1
|
|
fi
|
|
|
|
REFS=("$1" "$2" "$3")
|
|
|
|
# ── Preflight ────────────────────────────────────────────────────────
|
|
|
|
if ! docker info >/dev/null 2>&1; then
|
|
echo "ERROR: Docker daemon is not reachable" >&2
|
|
exit 2
|
|
fi
|
|
|
|
if ! command -v cargo >/dev/null 2>&1; then
|
|
echo "ERROR: cargo not found on PATH" >&2
|
|
exit 2
|
|
fi
|
|
|
|
for ref in "${REFS[@]}"; do
|
|
if ! git -C "$REPO_ROOT" rev-parse --verify --quiet "${ref}^{commit}" >/dev/null; then
|
|
echo "ERROR: git ref '$ref' does not resolve to a commit" >&2
|
|
exit 2
|
|
fi
|
|
done
|
|
|
|
# ── Worktree + build-context scratch space ───────────────────────────
|
|
|
|
WORK_BASE="$RUNS_BASE/.build"
|
|
mkdir -p "$WORK_BASE"
|
|
|
|
# Track worktree paths for cleanup.
|
|
CREATED_WORKTREES=()
|
|
|
|
cleanup() {
|
|
if [ "${FIPS_INTEROP_KEEP_WORKTREES:-}" = "1" ]; then
|
|
echo ""
|
|
echo "FIPS_INTEROP_KEEP_WORKTREES=1 — leaving worktrees in place:"
|
|
for wt in "${CREATED_WORKTREES[@]:-}"; do
|
|
[ -n "$wt" ] && echo " $wt"
|
|
done
|
|
return
|
|
fi
|
|
for wt in "${CREATED_WORKTREES[@]:-}"; do
|
|
[ -n "$wt" ] || continue
|
|
if [ -d "$wt" ]; then
|
|
git -C "$REPO_ROOT" worktree remove --force "$wt" 2>/dev/null \
|
|
|| rm -rf "$wt"
|
|
fi
|
|
done
|
|
git -C "$REPO_ROOT" worktree prune 2>/dev/null || true
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
# Prune any stale interop worktrees from a prior interrupted run before we
|
|
# start, so re-running the script is clean.
|
|
git -C "$REPO_ROOT" worktree prune 2>/dev/null || true
|
|
|
|
# ── Per-ref build ────────────────────────────────────────────────────
|
|
|
|
build_one() {
|
|
local slot="$1"
|
|
local ref="$2"
|
|
local sha
|
|
sha="$(git -C "$REPO_ROOT" rev-parse --short "$ref")"
|
|
|
|
echo ""
|
|
echo "=== Building slot '$slot' ref='$ref' sha=$sha ==="
|
|
|
|
local wt="$WORK_BASE/worktree-$slot"
|
|
local ctx="$WORK_BASE/ctx-$slot"
|
|
|
|
# Fresh worktree per build (remove a stale one first).
|
|
if [ -d "$wt" ]; then
|
|
git -C "$REPO_ROOT" worktree remove --force "$wt" 2>/dev/null \
|
|
|| rm -rf "$wt"
|
|
fi
|
|
git -C "$REPO_ROOT" worktree add --detach "$wt" "$ref"
|
|
CREATED_WORKTREES+=("$wt")
|
|
|
|
# Build the four binaries from this ref.
|
|
local cargo_jobs_arg=()
|
|
if [ -n "${CARGO_BUILD_JOBS:-}" ]; then
|
|
cargo_jobs_arg=(--jobs "$CARGO_BUILD_JOBS")
|
|
fi
|
|
(
|
|
cd "$wt"
|
|
cargo build --release "${cargo_jobs_arg[@]}" \
|
|
--bin fips --bin fipsctl --bin fipstop --bin fips-gateway
|
|
)
|
|
|
|
# Assemble a build context: the testing/docker Dockerfile + support
|
|
# files from the MAIN checkout, with the per-ref binaries layered in.
|
|
rm -rf "$ctx"
|
|
mkdir -p "$ctx"
|
|
cp "$DOCKER_CTX_SRC/Dockerfile" "$ctx/Dockerfile"
|
|
cp "$DOCKER_CTX_SRC/entrypoint.sh" "$ctx/entrypoint.sh"
|
|
cp "$DOCKER_CTX_SRC/resolv.conf" "$ctx/resolv.conf"
|
|
for bin in fips fipsctl fipstop fips-gateway; do
|
|
cp "$wt/target/release/$bin" "$ctx/$bin"
|
|
chmod +x "$ctx/$bin"
|
|
done
|
|
|
|
# The shared Dockerfile COPYs the native datagram API's example programs,
|
|
# which are cargo examples that older refs do not carry and no interop
|
|
# check runs: these images exercise the wire between daemon versions. The
|
|
# cargo invocation above names only the four bins for that reason. Stage a
|
|
# stub for each so the image still builds, and so anything that does reach
|
|
# for one says why it is not there.
|
|
for example in native-echo native-surface; do
|
|
printf '%s\n' \
|
|
'#!/bin/sh' \
|
|
"echo \"$example is not built into interop images\" >&2" \
|
|
'exit 1' > "$ctx/$example"
|
|
chmod +x "$ctx/$example"
|
|
done
|
|
|
|
docker build \
|
|
--label "fips.interop.slot=$slot" \
|
|
--label "fips.interop.ref=$ref" \
|
|
--label "fips.interop.sha=$sha" \
|
|
-t "fips-interop:$slot" \
|
|
"$ctx"
|
|
|
|
# The worktree is large (target/ dir); remove it now rather than at
|
|
# exit so peak disk use stays at one worktree, not three.
|
|
if [ "${FIPS_INTEROP_KEEP_WORKTREES:-}" != "1" ]; then
|
|
git -C "$REPO_ROOT" worktree remove --force "$wt" 2>/dev/null \
|
|
|| rm -rf "$wt"
|
|
rm -rf "$ctx"
|
|
fi
|
|
|
|
echo "=== Built fips-interop:$slot ($ref @ $sha) ==="
|
|
}
|
|
|
|
# Record the ref->sha mapping so the driver and README can report what
|
|
# actually ran. Written before the builds so a partial failure still
|
|
# leaves a breadcrumb.
|
|
MANIFEST="$WORK_BASE/refs.env"
|
|
{
|
|
echo "# Generated by build-images.sh on $(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
for i in 0 1 2; do
|
|
slot="${SLOTS[$i]}"
|
|
ref="${REFS[$i]}"
|
|
sha="$(git -C "$REPO_ROOT" rev-parse --short "$ref")"
|
|
upper="$(echo "$slot" | tr '[:lower:]' '[:upper:]')"
|
|
echo "INTEROP_REF_${upper}=$ref"
|
|
echo "INTEROP_SHA_${upper}=$sha"
|
|
done
|
|
} > "$MANIFEST"
|
|
|
|
for i in 0 1 2; do
|
|
build_one "${SLOTS[$i]}" "${REFS[$i]}"
|
|
done
|
|
|
|
echo ""
|
|
echo "=== All three interop images built ==="
|
|
docker image ls --filter 'reference=fips-interop' \
|
|
--format 'table {{.Repository}}:{{.Tag}}\t{{.ID}}\t{{.CreatedSince}}'
|
|
echo ""
|
|
echo "Ref manifest: $MANIFEST"
|
|
cat "$MANIFEST"
|
|
echo ""
|
|
echo "Next: bash testing/interop/interop-test.sh"
|