~ knotless Search/
↑↓ move↵ open⇧↵ all results

socialweb.computer/socialweb-computer-kcp

socialweb.computer: a compute market on atproto (kcp)

1 collaborator
0
0
222 lines · 8.2 KB Raw History
//! PDS, PLC and gateway XRPC through atlibs-host: app-password sessions saved per account under
//! ~/.tmp/swc-system-tests/sessions (createSession is rate limited), service auth, getRecord at computed keys.

use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context, Result, anyhow, bail};
use atlibs_host::host::{Fetch, Session, mint};
use atlibs_host::registry::Net;
use atlibs_host::store::Store;
use serde_json::{Value, json};

use crate::target::{Account, Target};

pub struct Atproto {
    pub net: Net,
    pub http: reqwest::Client,
    plc: String,
    pds: String,
}

/// Where runs write their dumps (ticks, state, failure, resources: no secret): SWC_RUNS (a CI workflow's `runs/`,
/// its declared artifacts), else ~/.tmp/swc-system-tests/runs.
pub fn runs_dir() -> std::path::PathBuf {
    match std::env::var("SWC_RUNS") {
        Ok(d) if !d.is_empty() => std::path::absolute(d).unwrap_or_default(),
        _ => home_tmp("runs"),
    }
}

pub fn home_tmp(sub: &str) -> std::path::PathBuf {
    std::path::PathBuf::from(std::env::var("HOME").unwrap_or_default())
        .join(".tmp/swc-system-tests")
        .join(sub)
}

/// Every name to the network's XRPC front, which serves its PLC, relay and PDSes by name (the TLS SNI and the Host
/// stay the URL's): a network has one entry, no per-host routes. An IP literal never asks a resolver.
struct FrontResolver(std::net::SocketAddr);

impl reqwest::dns::Resolve for FrontResolver {
    fn resolve(&self, _: reqwest::dns::Name) -> reqwest::dns::Resolving {
        let front = self.0;
        Box::pin(async move { Ok(Box::new(std::iter::once(front)) as reqwest::dns::Addrs) })
    }
}

/// The HTTP client of a run against `t`: with a network's xrpcFront, every named request connects to the front,
/// trusting the network CA bundle.
pub fn http_client(t: &Target) -> reqwest::Client {
    atlibs_host::tls();
    let mut b = reqwest::Client::builder().timeout(Duration::from_secs(30));
    if let Some(f) = &t.xrpc_front {
        b = b.dns_resolver(Arc::new(FrontResolver(f.address)));
        let pem = std::fs::read(crate::target::expand(&f.ca)).unwrap_or_default();
        for c in reqwest::Certificate::from_pem_bundle(&pem).unwrap_or_default() {
            b = b.add_root_certificate(c);
        }
    }
    b.build().unwrap()
}

impl Atproto {
    pub fn new(t: &Target) -> Self {
        let http = http_client(t);
        let mut net = Net::new(Arc::new(http.clone()) as Arc<dyn Fetch>);
        net.plc = t.plc.clone();
        // staging lives on private overlay addresses
        net.allow_private = t.name != "prod";
        Self {
            net,
            http,
            plc: t.plc.trim_end_matches('/').into(),
            pds: t.pds.clone(),
        }
    }

    /// The record's value; None for RecordNotFound.
    pub async fn record(&self, uri: &str) -> Result<Option<Value>> {
        Ok(self
            .net
            .record(uri)
            .await
            .map_err(|e| anyhow!(e))?
            .map(|(_, v)| v))
    }

    pub async fn record_of(
        &self,
        did: &str,
        collection: &str,
        rkey: &str,
    ) -> Result<Option<Value>> {
        self.record(&format!("at://{did}/{collection}/{rkey}"))
            .await
    }

    /// The URIs that still answer (each read three times, as the runbooks do). Empty: all RecordNotFound.
    pub async fn still_present(&self, uris: &[String]) -> Result<Vec<String>> {
        let mut left = vec![];
        for uri in uris {
            for _ in 0..3 {
                if self.record(uri).await?.is_some() {
                    left.push(uri.clone());
                    break;
                }
            }
        }
        Ok(left)
    }

    async fn plc_get(&self, path: &str) -> Result<(u16, Value)> {
        let r = self.http.get(format!("{}/{path}", self.plc)).send().await?;
        let status = r.status().as_u16();
        Ok((status, r.json().await.unwrap_or(Value::Null)))
    }

    /// The PLC document (None: 404, a tombstone on plc.directory).
    pub async fn did_doc(&self, did: &str) -> Result<Option<Value>> {
        let (s, v) = self.plc_get(did).await?;
        Ok((s == 200).then_some(v))
    }

    /// The type of the DID's last PLC op: plc_operation, plc_tombstone, ...
    pub async fn plc_last_type(&self, did: &str) -> Result<String> {
        let (_, v) = self.plc_get(&format!("{did}/log/last")).await?;
        Ok(v["type"].as_str().unwrap_or_default().into())
    }

    pub async fn tombstoned(&self, did: &str) -> Result<bool> {
        Ok(self.plc_last_type(did).await? == "plc_tombstone" && self.did_doc(did).await?.is_none())
    }

    pub async fn also_known_as(&self, did: &str) -> Result<Vec<String>> {
        let doc = self
            .did_doc(did)
            .await?
            .with_context(|| format!("{did} has no PLC document"))?;
        Ok(serde_json::from_value(doc["alsoKnownAs"].clone()).unwrap_or_default())
    }

    pub async fn rotation_keys(&self, did: &str) -> Result<Vec<String>> {
        let (_, v) = self.plc_get(&format!("{did}/data")).await?;
        Ok(serde_json::from_value(v["rotationKeys"].clone()).unwrap_or_default())
    }

    /// The account's app-password session, saved under ~/.tmp/swc-system-tests/sessions/<handle>.
    pub async fn session(&self, account: &Account) -> Result<Arc<Session>> {
        let store = Store::open(&home_tmp("sessions").join(&account.handle), "testkit")
            .map_err(|e| anyhow!(e))?;
        let password = account.password()?;
        let fetch: Arc<dyn Fetch> = Arc::new(self.http.clone());
        Session::login(
            &account.handle,
            Some(password.expose().to_string()),
            &self.pds,
            fetch,
            Some(store),
        )
        .await
        .map_err(|e| anyhow!("signing in {}: {e}", account.handle))
    }

    pub async fn delete_record(&self, s: &Session, collection: &str, rkey: &str) -> Result<()> {
        s.xrpc(
            "com.atproto.repo.deleteRecord",
            None,
            Some(&json!({ "repo": s.did(), "collection": collection, "rkey": rkey })),
            vec![],
        )
        .await
        .map_err(|e| anyhow!(e))?;
        Ok(())
    }

    /// An XRPC call to a service, authenticated by a service-auth token the session mints for `aud` + the method. `base` is
    /// an https origin, or `iroh://<ticket>` for a bidder's iroh market endpoint (`irohfetch`).
    pub async fn service_call(
        &self,
        s: &Session,
        base: &str,
        aud: &str,
        nsid: &str,
        input: Option<&Value>,
        query: &[(&str, &str)],
    ) -> Result<Value> {
        let tok = mint(s, aud, nsid).await.map_err(|e| anyhow!(e))?;
        let tok = tok["token"]
            .as_str()
            .context("getServiceAuth answered no token")?
            .to_string();
        let base = base.trim_end_matches('/');
        let (status, v) = if let Some(ticket) = base.strip_prefix("iroh://") {
            let mut headers = vec![("authorization".to_string(), format!("Bearer {tok}"))];
            let (method, body) = match input {
                Some(body) => {
                    headers.push(("content-type".into(), "application/json".into()));
                    ("POST", serde_json::to_vec(body)?)
                }
                None => ("GET", vec![]),
            };
            let target = format!("/xrpc/{nsid}{}", crate::irohfetch::query(if input.is_some() { &[] } else { query }));
            let (status, body) = crate::irohfetch::request(ticket, method, &target, headers, body, Duration::from_secs(120))
                .await
                .map_err(|e| anyhow!("{nsid}: {e}"))?;
            (reqwest::StatusCode::from_u16(status)?, serde_json::from_slice(&body).unwrap_or(Value::Null))
        } else {
            let url = format!("{base}/xrpc/{nsid}");
            let req = match input {
                Some(body) => self.http.post(url).json(body),
                None => self.http.get(url).query(query),
            };
            let r = req.bearer_auth(tok).send().await?;
            let status = r.status();
            (status, r.json().await.unwrap_or(Value::Null))
        };
        if !status.is_success() {
            bail!("{nsid}: HTTP {status} {}", v);
        }
        Ok(v)
    }
}