use crate::receipt_verify::{self as rv, testkit::Hub as Signer};
use axum::body::Body;
use axum::extract::{Request, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use base64::Engine;
use serde_json::{json, Value};
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::{Arc, Mutex};
pub struct FakeBatch {
pub seq: u64,
pub inputs: Value,
pub status: String,
pub outputs: Vec<(String, String)>, pub receipt_hash: Option<String>,
}
pub struct FakeSpace {
pub id: String,
pub req: Value,
pub status: String,
pub mirror_polls_left: u32,
pub digest: String,
pub batches: Vec<FakeBatch>,
}
pub struct St {
pub base: String,
pub token: Option<String>,
pub spaces: Vec<FakeSpace>,
pub objects: HashMap<String, Vec<u8>>,
pub calls: Vec<String>,
pub fail: HashMap<String, VecDeque<u16>>,
pub apply_then_fail: HashSet<String>,
pub signer: Signer,
pub receipts: HashMap<String, Value>,
pub resolved_digest: String,
pub mirror_polls: u32,
pub auto_complete: bool,
pub with_receipt_hash: bool,
pub tamper_output: bool,
pub bad_receipt_outputs: bool,
pub fail_second_batch: bool,
pub object_auth_seen: bool,
}
pub struct FakeHub {
pub base: String,
pub st: Arc<Mutex<St>>,
}
fn ok(v: Value) -> (u16, Value) {
(200, v)
}
fn err(status: u16, detail: &str) -> (u16, Value) {
(status, json!({ "detail": detail }))
}
impl St {
pub fn count(&self, key: &str) -> usize {
self.calls.iter().filter(|c| c.as_str() == key).count()
}
fn space_json(&self, i: usize) -> Value {
let s = &self.spaces[i];
let n = |st: &str| s.batches.iter().filter(|b| b.status == st).count();
json!({
"id": s.id,
"name": s.req["name"],
"description": s.req["description"],
"image_digest": s.digest,
"mine": true,
"status": s.status,
"mirror": {"status": if s.mirror_polls_left == 0 {"ready"} else {"pending"}, "error": null},
"counts": {"pending": n("pending"), "claimed": n("claimed"),
"succeeded": n("succeeded"), "failed": n("failed")},
})
}
pub fn complete_all(&mut self, idx: usize) {
let space_id = self.spaces[idx].id.clone();
let fail2 = self.fail_second_batch;
let n = self.spaces[idx].batches.len();
for b in 0..n {
if !matches!(
self.spaces[idx].batches[b].status.as_str(),
"pending" | "claimed"
) {
continue;
}
let seq = self.spaces[idx].batches[b].seq;
if fail2 && seq == 2 {
self.spaces[idx].batches[b].status = "failed".into();
continue;
}
let content = format!("result-{seq}").into_bytes();
let sha = rv::sha256_hex(&content);
let stored = if self.tamper_output {
b"tampered".to_vec()
} else {
content
};
self.objects
.insert(format!("spaces/{space_id}/out/{seq}/out.txt"), stored);
let mut receipt_hash = None;
if self.with_receipt_hash {
let signed_hashes = if self.bad_receipt_outputs {
vec!["0".repeat(64)]
} else {
vec![sha.clone()]
};
let r = self.signer.batch_receipt(&signed_hashes, "50.000000");
let h = rv::sha256_hex(r.receipt_json.as_bytes());
self.receipts.insert(h.clone(), self.signer.public_view(&r));
receipt_hash = Some(h);
}
let batch = &mut self.spaces[idx].batches[b];
batch.status = "succeeded".into();
batch.outputs = vec![("out.txt".into(), sha)];
batch.receipt_hash = receipt_hash;
}
self.spaces[idx].status = "done".into();
}
fn route(
&mut self,
method: &str,
path: &str,
headers_auth: Option<String>,
body: Vec<u8>,
query: &str,
) -> (String, (u16, Value), Option<Vec<u8>>) {
let seg: Vec<&str> = path.trim_matches('/').split('/').collect();
let authed = || {
self.token.is_none()
|| headers_auth.as_deref()
== self
.token
.as_ref()
.map(|t| format!("Bearer {t}"))
.as_deref()
};
let find = |s: &St, id: &str| s.spaces.iter().position(|x| x.id == id);
match (method, seg.as_slice()) {
("GET", ["api", "receipts", "pubkeys"]) => {
("pubkeys".into(), ok(self.signer.pubkeys_doc()), None)
}
("GET", ["api", "receipts", h]) => (
"receipt".into(),
self.receipts
.get(*h)
.cloned()
.map(ok)
.unwrap_or_else(|| err(404, "receipt not found")),
None,
),
("PUT", ["obj", rest @ ..]) => {
let key = rest.join("/");
if headers_auth.is_some() {
self.object_auth_seen = true;
}
(
"put".into(),
ok(json!({})),
Some({
self.objects.insert(key, body);
vec![]
}),
)
}
("GET", ["obj", rest @ ..]) => {
if headers_auth.is_some() {
self.object_auth_seen = true;
}
match self.objects.get(&rest.join("/")) {
Some(b) => ("download".into(), ok(json!({})), Some(b.clone())),
None => ("download".into(), err(404, "no object"), None),
}
}
(_, ["api", "spaces", ..]) if !authed() => {
("auth".into(), err(401, "Invalid or expired token"), None)
}
("POST", ["api", "spaces"]) => {
let req: Value = serde_json::from_slice(&body).unwrap_or_default();
let image = req["image_ref"].as_str().unwrap_or("");
let digest = image
.split_once('@')
.map(|(_, d)| d.to_string())
.unwrap_or_else(|| self.resolved_digest.clone());
let id = format!("sp-{}", self.spaces.len() + 1);
self.spaces.push(FakeSpace {
id,
req,
status: "draft".into(),
mirror_polls_left: self.mirror_polls,
digest,
batches: vec![],
});
let i = self.spaces.len() - 1;
("create".into(), (201, self.space_json(i)), None)
}
("GET", ["api", "spaces"]) => {
let items: Vec<Value> =
(0..self.spaces.len()).map(|i| self.space_json(i)).collect();
("list".into(), ok(json!({ "items": items })), None)
}
("GET", ["api", "spaces", id]) => match find(self, id) {
Some(i) => {
if self.spaces[i].mirror_polls_left > 0 {
self.spaces[i].mirror_polls_left -= 1;
}
if self.auto_complete && self.spaces[i].status == "live" {
self.complete_all(i);
}
("get".into(), ok(self.space_json(i)), None)
}
None => ("get".into(), err(404, "No such Space"), None),
},
("POST", ["api", "spaces", id, "inputs:presign"]) => match find(self, id) {
Some(_) => {
let req: Value = serde_json::from_slice(&body).unwrap_or_default();
let ups: Vec<Value> = req["files"]
.as_array()
.into_iter()
.flatten()
.map(|f| {
let n = f["name"].as_str().unwrap_or("");
json!({"name": n, "key": format!("hub://spaces/{id}/in/{n}"),
"url": format!("{}/obj/spaces/{id}/in/{n}", self.base)})
})
.collect();
("presign".into(), ok(json!({ "uploads": ups })), None)
}
None => ("presign".into(), err(404, "No such Space"), None),
},
("POST", ["api", "spaces", id, "batches"]) => match find(self, id) {
Some(i) => {
let mut parsed = vec![];
for line in String::from_utf8_lossy(&body)
.lines()
.filter(|l| !l.trim().is_empty())
{
let doc: Value = serde_json::from_str(line).unwrap_or_default();
for uri in doc["inputs"]
.as_object()
.into_iter()
.flatten()
.map(|(_, v)| v.as_str().unwrap_or(""))
{
let key = uri.strip_prefix("hub://").unwrap_or("");
if !uri.starts_with(&format!("hub://spaces/{id}/in/"))
|| !self.objects.contains_key(key)
{
return (
"batches_post".into(),
err(400, &format!("object not found: {uri}")),
None,
);
}
}
parsed.push(doc["inputs"].clone());
}
let last = self.spaces[i].batches.len() as u64;
let n = parsed.len() as u64;
for (k, inputs) in parsed.into_iter().enumerate() {
self.spaces[i].batches.push(FakeBatch {
seq: last + k as u64 + 1,
inputs,
status: "pending".into(),
outputs: vec![],
receipt_hash: None,
});
}
(
"batches_post".into(),
ok(json!({"added": n, "first_seq": last + 1, "last_seq": last + n})),
None,
)
}
None => ("batches_post".into(), err(404, "No such Space"), None),
},
("GET", ["api", "spaces", id, "batches"]) => match find(self, id) {
Some(i) => {
let want = query.split('&').find_map(|p| p.strip_prefix("status="));
let sid = self.spaces[i].id.clone();
let items: Vec<Value> = self.spaces[i].batches.iter()
.filter(|b| want.is_none_or(|w| b.status == w))
.map(|b| {
let mut v = json!({
"seq": b.seq, "status": b.status, "inputs": [],
"outputs": b.outputs.iter().map(|(n, _)| (n.clone(), json!(format!("{}/obj/spaces/{sid}/out/{}/{n}", self.base, b.seq)))).collect::<serde_json::Map<_,_>>(),
"output_hashes": b.outputs.iter().map(|(n, h)| (n.clone(), json!(h))).collect::<serde_json::Map<_,_>>(),
});
if let Some(h) = &b.receipt_hash {
v["receipt_hash"] = json!(h);
}
v
}).collect();
(
"batches_list".into(),
ok(json!({"items": items, "next_cursor": null})),
None,
)
}
None => ("batches_list".into(), err(404, "No such Space"), None),
},
("POST", ["api", "spaces", id, "publish"]) => match find(self, id) {
Some(i) if self.spaces[i].status != "draft" => (
"publish".into(),
err(409, "Space is live, not a draft"),
None,
),
Some(i) if self.spaces[i].mirror_polls_left > 0 => (
"publish".into(),
err(409, "Image mirror is pending, not ready"),
None,
),
Some(i) => {
self.spaces[i].status = "live".into();
("publish".into(), ok(self.space_json(i)), None)
}
None => ("publish".into(), err(404, "No such Space"), None),
},
("POST", ["api", "spaces", id, "cancel"]) => match find(self, id) {
Some(i) if matches!(self.spaces[i].status.as_str(), "done" | "cancelled") => (
"cancel".into(),
err(409, "Cannot move a done Space to cancelled"),
None,
),
Some(i) => {
self.spaces[i].status = "cancelled".into();
("cancel".into(), ok(self.space_json(i)), None)
}
None => ("cancel".into(), err(404, "No such Space"), None),
},
_ => ("unknown".into(), err(404, "no such route"), None),
}
}
}
async fn handle(State(st): State<Arc<Mutex<St>>>, req: Request) -> Response {
let method = req.method().to_string();
let path = req.uri().path().to_string();
let query = req.uri().query().unwrap_or("").to_string();
let auth = req
.headers()
.get("authorization")
.and_then(|h| h.to_str().ok())
.map(str::to_string);
let checksum = req
.headers()
.get("x-amz-checksum-sha256")
.and_then(|h| h.to_str().ok())
.map(str::to_string);
let body = axum::body::to_bytes(req.into_body(), 64 << 20)
.await
.unwrap_or_default()
.to_vec();
let mut s = st.lock().unwrap();
if method == "PUT" {
let want = base64::engine::general_purpose::STANDARD
.encode(hex::decode(rv::sha256_hex(&body)).unwrap());
if checksum.as_deref() != Some(want.as_str()) {
s.calls.push("put_rejected".into());
return (StatusCode::BAD_REQUEST, "checksum mismatch").into_response();
}
}
let injected = |s: &mut St, key: &str| s.fail.get_mut(key).and_then(VecDeque::pop_front);
let (key, mut result, raw) = {
let probe = route_key(&method, &path);
if let Some(code) = (!s.apply_then_fail.contains(&probe))
.then(|| injected(&mut s, &probe))
.flatten()
{
s.calls.push(probe);
return (
StatusCode::from_u16(code).unwrap(),
"{\"detail\":\"injected\"}",
)
.into_response();
}
s.route(&method, &path, auth, body, &query)
};
s.calls.push(key.clone());
if s.apply_then_fail.contains(&key) {
if let Some(code) = injected(&mut s, &key) {
result = (code, json!({"detail": "answer lost"}));
}
}
let (status, v) = result;
let status = StatusCode::from_u16(status).unwrap();
match raw {
Some(bytes) if status.is_success() && method == "GET" => Response::builder()
.status(status)
.body(Body::from(bytes))
.unwrap(),
_ => (status, axum::Json(v)).into_response(),
}
}
fn route_key(method: &str, path: &str) -> String {
let seg: Vec<&str> = path.trim_matches('/').split('/').collect();
match (method, seg.as_slice()) {
("POST", ["api", "spaces"]) => "create",
("GET", ["api", "spaces"]) => "list",
("GET", ["api", "spaces", _]) => "get",
("POST", ["api", "spaces", _, "inputs:presign"]) => "presign",
("POST", ["api", "spaces", _, "batches"]) => "batches_post",
("GET", ["api", "spaces", _, "batches"]) => "batches_list",
("POST", ["api", "spaces", _, "publish"]) => "publish",
("POST", ["api", "spaces", _, "cancel"]) => "cancel",
("PUT", ["obj", ..]) => "put",
("GET", ["obj", ..]) => "download",
("GET", ["api", "receipts", "pubkeys"]) => "pubkeys",
("GET", ["api", "receipts", _]) => "receipt",
_ => "unknown",
}
.to_string()
}
impl FakeHub {
pub fn start(token: Option<&str>) -> FakeHub {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
listener.set_nonblocking(true).unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
let st = Arc::new(Mutex::new(St {
base: base.clone(),
token: token.map(str::to_string),
spaces: vec![],
objects: HashMap::new(),
calls: vec![],
fail: HashMap::new(),
apply_then_fail: HashSet::new(),
signer: Signer::new(),
receipts: HashMap::new(),
resolved_digest: format!("sha256:{}", "c".repeat(64)),
mirror_polls: 1,
auto_complete: true,
with_receipt_hash: true,
tamper_output: false,
bad_receipt_outputs: false,
fail_second_batch: false,
object_auth_seen: false,
}));
let app = axum::Router::new().fallback(handle).with_state(st.clone());
std::thread::spawn(move || {
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.unwrap();
rt.block_on(async move {
let l = tokio::net::TcpListener::from_std(listener).unwrap();
axum::serve(l, app).await.unwrap();
});
});
FakeHub { base, st }
}
pub fn with<R>(&self, f: impl FnOnce(&mut St) -> R) -> R {
f(&mut self.st.lock().unwrap())
}
pub fn count(&self, key: &str) -> usize {
self.with(|s| s.count(key))
}
}