web: public read-only GET /api/identity for fleet discovery
source-pr / source (pull_request) Successful in 3m26s

alkane-toolkit's fleet scan (asic-rs) recognises miners by their cgminer
socket or vendor web UI; an Alkane board has neither exposed, so it never
appeared and could not be right-clicked to flash. Add an open, read-only
identity endpoint beside /api/summary: os, os_version, board, sdk_revision,
hostname, mac, kernel - nothing a stock miner does not already expose, no
secret. The .afw packager stamps the bundle version into
/etc/alkane-os-version for it. Tests: served without a session; every field
read from the board files; absent files read as null.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B1W6qKwKxq3oUPDKWicM6i
This commit is contained in:
2026-11-12 15:40:02 -06:00
co-authored by Claude Opus 5.5
parent cc14543ced
commit 2d3417b7d3
2 changed files with 94 additions and 5 deletions
+4 -2
View File
@@ -58,10 +58,12 @@ command -v arm-linux-gnueabihf-strip >/dev/null 2>&1 && arm-linux-gnueabihf-stri
echo "repacking the SDK rootfs with the console + overlay (under fakeroot)"
cat > "$WORK/pack.sh" <<'PACK'
set -e
RFS=$1; SDK_OUT=$2; WORK=$3; OVERLAY=$4
RFS=$1; SDK_OUT=$2; WORK=$3; OVERLAY=$4; VERSION=$5
mkdir -p "$RFS"
( cd "$RFS" && zcat "$SDK_OUT/rootfs.cpio.gz" | cpio -idm --quiet )
install -D -m 0755 "$WORK/alkane-os" "$RFS/usr/bin/alkane-os"
# The .afw version, reported by GET /api/identity (fleet discovery).
printf '%s\n' "$VERSION" > "$RFS/etc/alkane-os-version"
if [ -d "$OVERLAY" ]; then
( cd "$OVERLAY" && find . -type f ) | while IFS= read -r f; do
install -D -m 0755 "$OVERLAY/${f#./}" "$RFS/${f#./}"
@@ -69,7 +71,7 @@ if [ -d "$OVERLAY" ]; then
fi
( cd "$RFS" && find . | cpio -o -H newc --owner=0:0 --quiet | gzip -9 > "$WORK/in/rootfs.cpio.gz" )
PACK
fakeroot -- bash "$WORK/pack.sh" "$WORK/rootfs" "$SDK_OUT" "$WORK" "$ROOT/image/overlay"
fakeroot -- bash "$WORK/pack.sh" "$WORK/rootfs" "$SDK_OUT" "$WORK" "$ROOT/image/overlay" "$VERSION"
cp "$SDK_OUT/zImage" "$WORK/in/zImage"
cp "$SDK_OUT/zynq-alkane.dtb" "$WORK/in/zynq-alkane.dtb"
+90 -3
View File
@@ -221,8 +221,11 @@ fn route(stream: &mut TcpStream, ctx: &WebContext, request: &Request) -> io::Res
// The read-only miner-state JSON stays open (it is the interface of record
// and is already exposed on the cgminer socket). Everything else - the HTML
// console, host reads, and every mutation - needs a live session.
let open_api =
method == "GET" && matches!(path, "/api/hardware" | "/api/summary" | "/api/control");
let open_api = method == "GET"
&& matches!(
path,
"/api/hardware" | "/api/summary" | "/api/control" | "/api/identity"
);
let authed = request.wsid.as_deref().is_some_and(|w| ctx.auth.valid(w));
if !authed && !open_api {
if method == "GET" && !path.starts_with("/api/") {
@@ -252,6 +255,7 @@ fn route(stream: &mut TcpStream, ctx: &WebContext, request: &Request) -> io::Res
("GET", "/api/summary") => {
json_response(stream, &bus.read_state_or(topics::SUMMARY, json!({})))
}
("GET", "/api/identity") => json_response(stream, &identity(Path::new("/"))),
("GET", "/api/control") => json_response(
stream,
&bus.read_state_or(
@@ -519,6 +523,39 @@ fn auth_local(stream: &mut TcpStream, ctx: &WebContext, request: &Request) -> io
}
}
/// Read-only identity for network discovery (alkane-toolkit's fleet scan finds
/// Alkane boards by it: they have no cgminer socket or vendor web UI for a
/// generic scanner to recognise). Public like the other read-only state. It
/// carries no secret, only what a stock miner also exposes: OS and version, the
/// board family, SDK revision, hostname, MAC and kernel. `root` is the
/// filesystem root (the real one on a board; a temp dir in tests).
fn identity(root: &Path) -> Value {
let read = |rel: &str| {
fs::read_to_string(root.join(rel))
.ok()
.map(|text| text.trim().to_string())
.filter(|text| !text.is_empty())
};
let os_release = read("etc/os-release").unwrap_or_default();
let release_field = |key: &str| {
os_release.lines().find_map(|line| {
line.strip_prefix(key)
.and_then(|rest| rest.strip_prefix('='))
.map(|value| value.trim_matches('"').to_string())
})
};
json!({
"os": "alkane-os",
"os_version": read("etc/alkane-os-version")
.unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string()),
"board": release_field("VARIANT_ID"),
"sdk_revision": release_field("VERSION_ID"),
"hostname": read("proc/sys/kernel/hostname"),
"mac": read("sys/class/net/eth0/address"),
"kernel": read("proc/sys/kernel/osrelease"),
})
}
fn json_response(stream: &mut TcpStream, value: &Value) -> io::Result<()> {
respond(
stream,
@@ -1019,13 +1056,63 @@ mod tests {
#[test]
fn the_api_serves_state_from_the_bus() {
for path in ["/api/hardware", "/api/summary", "/api/control"] {
for path in [
"/api/hardware",
"/api/summary",
"/api/control",
"/api/identity",
] {
let reply = client(&format!("GET {path} HTTP/1.1\r\nHost: x\r\n\r\n"));
assert!(reply.starts_with("HTTP/1.1 200 OK"), "{path}");
assert!(reply.contains("application/json"));
}
}
#[test]
fn the_identity_api_is_public_and_names_alkane() {
// No session cookie: discovery must work before anyone signs in.
let reply = client("GET /api/identity HTTP/1.1\r\nHost: x\r\n\r\n");
assert!(reply.starts_with("HTTP/1.1 200 OK"), "{reply}");
let body = reply.split("\r\n\r\n").nth(1).unwrap_or("");
let doc: Value = serde_json::from_str(body).unwrap_or_else(|_| json!({}));
assert_eq!(doc["os"], "alkane-os");
assert!(doc["os_version"].as_str().is_some_and(|v| !v.is_empty()));
}
#[test]
fn identity_reads_the_board_files() {
let root = std::env::temp_dir().join(format!("alkane-identity-{}", std::process::id()));
for dir in ["etc", "proc/sys/kernel", "sys/class/net/eth0"] {
fs::create_dir_all(root.join(dir)).unwrap();
}
fs::write(
root.join("etc/os-release"),
"NAME=\"Alkane OS\"\nID=alkane\nVERSION_ID=6a9080ac82c1\nVARIANT_ID=zynq-7010\n",
)
.unwrap();
fs::write(root.join("etc/alkane-os-version"), "0.1.2\n").unwrap();
fs::write(root.join("proc/sys/kernel/hostname"), "alkane-zynq\n").unwrap();
fs::write(root.join("proc/sys/kernel/osrelease"), "6.18.48\n").unwrap();
fs::write(
root.join("sys/class/net/eth0/address"),
"26:4e:52:8b:e2:48\n",
)
.unwrap();
let doc = identity(&root);
let _ = fs::remove_dir_all(&root);
assert_eq!(doc["os"], "alkane-os");
assert_eq!(doc["os_version"], "0.1.2");
assert_eq!(doc["board"], "zynq-7010");
assert_eq!(doc["sdk_revision"], "6a9080ac82c1");
assert_eq!(doc["hostname"], "alkane-zynq");
assert_eq!(doc["mac"], "26:4e:52:8b:e2:48");
assert_eq!(doc["kernel"], "6.18.48");
// Absent files read as null, never a panic.
let bare = identity(Path::new("/nonexistent-alkane-root"));
assert_eq!(bare["os"], "alkane-os");
assert!(bare["board"].is_null() && bare["mac"].is_null());
}
#[test]
fn the_control_api_reports_operator_state() {
let reply = client("GET /api/control HTTP/1.1\r\nHost: x\r\n\r\n");