use std::collections::BTreeMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant};
use async_trait::async_trait;
use futures::future::BoxFuture;
use futures::stream::BoxStream;
use futures::StreamExt;
use plexus_auth_core::grants::{Gate, Grant, GrantSet, Refusal, RoleTable, Roles};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use tokio::sync::{broadcast, mpsc};
use super::link::{gone, meta, Conn, Kept, Shared};
use super::{mount_schema, node_schema, Backoff, ChildOptions, ChildSpec, Inner, MountTimings, PeerIdentity, RemoteChild};
use crate::plexus::auth::AuthContext;
use crate::plexus::method_enum::MethodEnumSchema;
use crate::plexus::plexus::{route_to_child, Activation, ChildRouter, PlexusError};
use crate::plexus::schema::{Cardinality, MethodRole, MethodSchema, PluginSchema};
use crate::plexus::streaming::PlexusStream;
use crate::plexus::types::PlexusStreamItem;
pub const OFFER_PENDING: &str = "_offer.pending";
const POSSESSION_CONTEXT: &str = "plexus-dial-offer/v1";
pub fn possession_input(host_key: &str, node_key: &str, node_nonce: &str, host_nonce: &str, service: &str, mount: &str) -> String {
format!("{POSSESSION_CONTEXT}\n{host_key}\n{node_key}\n{node_nonce}\n{host_nonce}\n{service}\n{mount}")
}
#[derive(Debug, Clone)]
pub struct Offer {
pub socket: u64,
pub name: String,
pub namespace: String,
pub principal: String,
pub key_proven: bool,
pub from: Option<String>,
pub code: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OfferRefused {
pub kind: String,
pub message: String,
}
impl OfferRefused {
pub fn new(kind: impl Into<String>, message: impl Into<String>) -> Self {
Self { kind: kind.into(), message: message.into() }
}
}
pub enum Decision {
Admit,
Refuse(OfferRefused),
Pending(BoxFuture<'static, Result<(), OfferRefused>>),
}
pub trait Admission: Send + Sync + 'static {
fn decide(&self, offer: &Offer) -> Decision;
fn vet(&self, offer: &Offer, child: &RemoteChild) -> Result<(), OfferRefused>;
fn mounted(&self, offer: &Offer, child: &RemoteChild, socket: &Arc<PeerSocket>) -> Result<(), OfferRefused>;
fn closed(&self, socket: u64, mount: Option<&str>, why: &str);
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct PeerInfo {
pub name: String,
pub principal: String,
pub namespace: String,
pub methods: Vec<String>,
pub connected_secs: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum PeerEvent {
Mounted { name: String, principal: String, namespace: String, methods: Vec<String> },
Unmounted { name: String, why: String },
}
#[derive(Debug, Clone, Copy)]
pub enum PeersMethods {}
impl JsonSchema for PeersMethods {
fn schema_name() -> std::borrow::Cow<'static, str> {
"PeersMethods".into()
}
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({ "description": "list, watch, offer โ and every peer connected, by its mount name" })
}
}
impl MethodEnumSchema for PeersMethods {
fn method_names() -> &'static [&'static str] {
&["list", "watch", "offer"]
}
fn schema_with_consts() -> Value {
serde_json::to_value(schemars::schema_for!(PeersMethods)).unwrap_or(Value::Null)
}
}
struct Entry {
child: RemoteChild,
principal: String,
namespace: String,
socket: u64,
since: Instant,
holder: std::sync::Weak<PeerSocket>,
}
impl Entry {
fn info(&self, name: &str) -> PeerInfo {
PeerInfo {
name: name.to_string(),
principal: self.principal.clone(),
namespace: self.namespace.clone(),
methods: self.child.inner.schema.methods.iter().map(|m| m.name.clone()).collect(),
connected_secs: self.since.elapsed().as_secs(),
}
}
}
struct PeersInner {
ns: String,
service: String,
principal: String,
gate: RwLock<Option<Arc<Gate>>>,
opts: ChildOptions,
table: RwLock<BTreeMap<String, Entry>>,
events: broadcast::Sender<PeerEvent>,
next_socket: AtomicU64,
on_change: RwLock<Vec<Box<dyn Fn() + Send + Sync>>>,
key: Option<Arc<crate::plexus::peer_token::PeerKey>>,
admission: RwLock<Option<Arc<dyn Admission>>>,
}
#[derive(Clone)]
pub struct Peers {
inner: Arc<PeersInner>,
}
impl std::fmt::Debug for Peers {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Peers").field("ns", &self.inner.ns).field("names", &self.names()).finish()
}
}
fn valid_name(name: &str) -> Result<(), String> {
if name.is_empty() || name.len() > 64 {
return Err("a mount name is 1โ64 characters".into());
}
if let Some(bad) = name.chars().find(|c| !(c.is_ascii_alphanumeric() || *c == '_' || *c == '-')) {
return Err(format!("the mount name `{name}` holds `{bad}`: only letters, digits, `_` and `-`"));
}
if matches!(name, "list" | "watch" | "offer" | "schema") {
return Err(format!("`{name}` is one of the peers' own methods"));
}
Ok(())
}
impl Peers {
pub fn new(ns: impl Into<String>, service: impl Into<String>, principal: impl Into<String>) -> Self {
let service = service.into();
let opts = ChildOptions {
backoff: Backoff { initial: Duration::ZERO, max: Duration::ZERO, per_minute: 0 },
..ChildOptions::for_service(&service)
};
Self {
inner: Arc::new(PeersInner {
ns: ns.into(),
service,
principal: principal.into(),
gate: RwLock::new(None),
opts,
table: RwLock::new(BTreeMap::new()),
events: broadcast::channel(256).0,
next_socket: AtomicU64::new(1),
on_change: RwLock::new(Vec::new()),
key: None,
admission: RwLock::new(None),
}),
}
}
pub fn with_gate(self, gate: Arc<Gate>) -> Self {
*self.inner.gate.write().unwrap() = Some(gate);
self
}
pub fn set_gate(&self, gate: Arc<Gate>) {
*self.inner.gate.write().unwrap() = Some(gate);
}
pub fn with_timeout(self, timeout: Duration) -> Self {
let mut s = self;
if let Some(inner) = Arc::get_mut(&mut s.inner) {
inner.opts.timeout = timeout;
}
s
}
pub fn with_options(self, opts: ChildOptions) -> Self {
let mut s = self;
if let Some(inner) = Arc::get_mut(&mut s.inner) {
inner.opts = ChildOptions { backoff: Backoff { initial: Duration::ZERO, max: Duration::ZERO, per_minute: 0 }, ..opts };
}
s
}
pub fn with_admission(self, admission: Arc<dyn Admission>) -> Self {
*self.inner.admission.write().unwrap() = Some(admission);
self
}
pub fn with_key(self, key: crate::plexus::peer_token::PeerKey) -> Self {
let mut s = self;
if let Some(inner) = Arc::get_mut(&mut s.inner) {
inner.principal = key.kid().to_string();
inner.key = Some(Arc::new(key));
}
s
}
pub fn on_change(&self, f: impl Fn() + Send + Sync + 'static) {
self.inner.on_change.write().unwrap().push(Box::new(f));
}
fn moved(&self) {
if let Some(g) = self.inner.gate.read().unwrap().as_ref() {
g.generation().bump();
}
for f in self.inner.on_change.read().unwrap().iter() {
f();
}
}
pub fn names(&self) -> Vec<String> {
self.inner.table.read().unwrap().keys().cloned().collect()
}
pub fn list(&self) -> Vec<PeerInfo> {
self.inner.table.read().unwrap().iter().map(|(n, e)| e.info(n)).collect()
}
pub fn get(&self, name: &str) -> Option<RemoteChild> {
self.inner.table.read().unwrap().get(name).map(|e| e.child.clone())
}
pub fn events(&self) -> broadcast::Receiver<PeerEvent> {
self.inner.events.subscribe()
}
pub fn roles<R: Roles>(&self, base: R, every: Option<&str>) -> PeerRoles<R> {
PeerRoles { base, peers: self.clone(), every: every.map(str::to_string) }
}
pub fn socket(&self, auth: Option<AuthContext>) -> (Arc<PeerSocket>, mpsc::UnboundedReceiver<Vec<u8>>) {
self.socket_from(auth, None, None)
}
pub fn socket_from(&self, auth: Option<AuthContext>, from: Option<String>, close: Option<Box<dyn Fn() + Send + Sync>>) -> (Arc<PeerSocket>, mpsc::UnboundedReceiver<Vec<u8>>) {
let (out, rx) = mpsc::unbounded_channel();
let (inbound, mut in_rx) = mpsc::unbounded_channel::<Vec<u8>>();
let socket = Arc::new(PeerSocket {
peers: self.clone(),
id: self.inner.next_socket.fetch_add(1, Ordering::Relaxed),
principal: auth.as_ref().and_then(|a| a.principal()).map(|p| p.to_string()),
auth,
out,
inbound,
link: Mutex::new(None),
mount: Mutex::new(None),
from,
close,
closed: AtomicBool::new(false),
offering: AtomicBool::new(false),
pairing: Mutex::new(None),
});
let weak = Arc::downgrade(&socket);
tokio::spawn(async move {
while let Some(frame) = in_rx.recv().await {
let link = weak.upgrade().and_then(|s| s.link.lock().unwrap().clone());
if let Some((conn, _)) = link {
conn.on_message(&frame).await;
}
}
});
(socket, rx)
}
fn unmount(&self, name: &str, socket: u64, why: &str) {
let removed = {
let mut t = self.inner.table.write().unwrap();
match t.get(name) {
Some(e) if e.socket == socket => t.remove(name),
_ => None,
}
};
if let Some(e) = removed {
tracing::info!(target: "plexus::peers", peer = %name, "peer unmounted: {why}");
e.child.stop(why);
self.moved();
let _ = self.inner.events.send(PeerEvent::Unmounted { name: name.to_string(), why: why.to_string() });
}
}
fn data(&self, method: &str, content: Value) -> PlexusStreamItem {
let ns = &self.inner.ns;
PlexusStreamItem::Data { metadata: meta(ns).into(), content_type: format!("{ns}.{method}").into(), content: content.into() }
}
fn done(&self) -> PlexusStreamItem {
PlexusStreamItem::Done { metadata: meta(&self.inner.ns) }
}
fn own_methods(&self) -> Vec<MethodSchema> {
let ns = &self.inner.ns;
vec![
MethodSchema::new("list", "The peers connected now: each one's mount name, principal, namespace and methods", "")
.with_cardinality(Cardinality::Many)
.with_returns(schemars::schema_for!(PeerInfo))
.with_grant(format!("{ns}.list"))
.with_content_hash(),
MethodSchema::new("watch", "Peers as they come and go: those mounted now, then each mount and unmount", "")
.with_cardinality(Cardinality::Many)
.with_returns(schemars::schema_for!(PeerEvent))
.with_grant(format!("{ns}.watch"))
.with_content_hash(),
MethodSchema::new("offer", "Offered on a peer's own socket (`{service}.offer`, spec/14), not called: its grant decides who may be mounted, on the mount name", "")
.with_cardinality(Cardinality::One)
.with_grant(format!("{ns}.offer"))
.with_content_hash(),
]
}
}
pub struct PeerRoles<R = RoleTable> {
base: R,
peers: Peers,
every: Option<String>,
}
#[async_trait]
impl<R: crate::plexus::RoleStore> crate::plexus::RoleStore for PeerRoles<R> {
async fn set(&self, role: &plexus_auth_core::audit::RoleName, grants: GrantSet) -> Result<(), crate::plexus::StoreError> {
self.base.set(role, grants).await
}
}
impl<R: Roles> Roles for PeerRoles<R> {
fn grants(&self, role: &plexus_auth_core::audit::RoleName) -> Option<GrantSet> {
let base = self.base.grants(role);
if matches!(base, Some(GrantSet::All)) {
return base;
}
let mut set = match base {
Some(GrantSet::Listed(s)) => s,
_ => Default::default(),
};
let every = self.every.as_deref() == Some(role.as_str());
let ns = &self.peers.inner.ns;
if every {
for m in ["list", "watch", "offer"] {
set.insert(Grant::new(format!("{ns}.{m}")));
}
}
let mut known = every || self.base.grants(role).is_some();
for (name, e) in self.peers.inner.table.read().unwrap().iter() {
let schema = &e.child.inner.schema;
if every {
set.extend(schema.methods.iter().map(|m| Grant::new(format!("{ns}.{name}.{}", m.name))));
}
if let Some(gs) = schema.roles.get(role.as_str()) {
known = true;
set.extend(gs.iter().map(Grant::new));
}
if every || schema.roles.get(role.as_str()).is_some_and(|g| !g.is_empty()) {
set.insert(Grant::new(format!("{ns}.{name}")));
}
}
known.then_some(GrantSet::Listed(set))
}
fn names(&self) -> Vec<plexus_auth_core::audit::RoleName> {
let mut names = self.base.names();
if let Some(e) = &self.every {
names.push(plexus_auth_core::audit::RoleName::new(e));
}
names
}
}
pub struct PeerSocket {
peers: Peers,
id: u64,
auth: Option<AuthContext>,
principal: Option<String>,
out: mpsc::UnboundedSender<Vec<u8>>,
inbound: mpsc::UnboundedSender<Vec<u8>>,
link: Mutex<Option<(Arc<Conn>, Arc<Shared>)>>,
mount: Mutex<Option<String>>,
from: Option<String>,
close: Option<Box<dyn Fn() + Send + Sync>>,
closed: AtomicBool,
offering: AtomicBool,
pairing: Mutex<Option<(String, String)>>,
}
#[derive(Deserialize)]
struct Shape {
#[serde(default)]
id: Option<Value>,
#[serde(default)]
method: Option<String>,
#[serde(default)]
params: Option<Value>,
}
fn refusal(id: &Value, code: i64, kind: &str, message: &str) -> Vec<u8> {
serde_json::to_vec(&json!({ "jsonrpc": "2.0", "id": id, "error": { "code": code, "message": message, "data": { "error_kind": kind } } })).unwrap_or_default()
}
impl PeerSocket {
pub fn id(&self) -> u64 {
self.id
}
pub fn is_closed(&self) -> bool {
self.closed.load(Ordering::Acquire)
}
pub fn close(&self, why: &str) {
self.ended(why);
if let Some(close) = &self.close {
close();
}
}
pub fn principal(&self) -> Option<&str> {
self.principal.as_deref()
}
pub fn mount(&self) -> Option<String> {
self.mount.lock().unwrap().clone()
}
pub fn on_frame(self: &Arc<Self>, frame: &[u8]) -> bool {
let Ok(shape) = serde_json::from_slice::<Shape>(frame) else { return false };
match (&shape.method, &shape.id) {
(Some(m), Some(id)) if m == "_info" && self.peers.inner.key.is_some() && shape.params.as_ref().and_then(|p| p.get("nonce")).is_some_and(Value::is_string) => {
let nonce = shape.params.as_ref().and_then(|p| p.get("nonce")).and_then(Value::as_str).unwrap_or_default();
let commit = shape.params.as_ref().and_then(|p| p.get("commit")).and_then(Value::as_str);
self.prove(id, nonce, commit);
true
}
(Some(m), Some(_)) if m == super::upcall::HOST_CALL || m == super::upcall::HOST_CALL_UNSUB => {
self.mount.lock().unwrap().is_some() && self.link.lock().unwrap().is_some() && self.inbound.send(frame.to_vec()).is_ok()
}
(Some(m), Some(id)) if *m == format!("{}.offer", self.peers.inner.service) => {
let (me, id, params) = (Arc::clone(self), id.clone(), shape.params.unwrap_or(Value::Null));
tokio::spawn(async move {
let answer = me.offer(&id, params).await;
let _ = me.out.send(answer);
});
true
}
(None, Some(_)) => self.link.lock().unwrap().is_some() && self.inbound.send(frame.to_vec()).is_ok(),
(Some(_), None) => {
let item = shape.params.as_ref().is_some_and(|p| p.get("subscription").is_some() && p.get("result").is_some());
item && self.link.lock().unwrap().is_some() && self.inbound.send(frame.to_vec()).is_ok()
}
_ => false,
}
}
fn prove(&self, id: &Value, nonce: &str, commit: Option<&str>) {
let Some(key) = &self.peers.inner.key else { return };
let service = &self.peers.inner.service;
let sid = format!("info-{}", self.id);
let asker = self.principal.clone().unwrap_or_default();
let mut content = json!({ "backend": service, "key": key.kid(), "proof": key.key_proof_for(nonce, service, &asker), "asker": asker });
if let Some(commit) = commit {
let mut pairing = self.pairing.lock().unwrap();
if pairing.is_none() {
let host_nonce = crate::plexus::peer_token::random_nonce();
content["host_nonce"] = json!(host_nonce);
*pairing = Some((commit.to_string(), host_nonce));
}
}
let item = PlexusStreamItem::data(meta(service), "_info".into(), content);
let frames = [
json!({ "jsonrpc": "2.0", "id": id, "result": sid }),
json!({ "jsonrpc": "2.0", "method": "_info", "params": { "subscription": sid, "result": item } }),
json!({ "jsonrpc": "2.0", "method": "_info", "params": { "subscription": sid, "result": PlexusStreamItem::done(meta(service)) } }),
];
for f in frames {
let _ = self.out.send(serde_json::to_vec(&f).unwrap_or_default());
}
}
pub fn on_close(&self) {
self.ended("its connection closed");
}
fn ended(&self, why: &str) {
let (first, name) = {
let mut m = self.mount.lock().unwrap();
(!self.closed.swap(true, Ordering::AcqRel), m.take())
};
if let Some((conn, shared)) = self.link.lock().unwrap().take() {
gone(&Arc::downgrade(&shared), &conn, why);
}
if let Some(name) = &name {
self.peers.unmount(name, self.id, why);
}
if first {
let admission = self.peers.inner.admission.read().unwrap().clone();
if let Some(a) = admission {
a.closed(self.id, name.as_deref(), why);
}
}
}
fn paired(&self, params: &Value, principal: &str, name: &str) -> Result<(Option<String>, bool), OfferRefused> {
let Some((commit, host_nonce)) = self.pairing.lock().unwrap().clone() else { return Ok((None, false)) };
let Some(node_nonce) = params.get("pairing_nonce").and_then(Value::as_str) else { return Ok((None, false)) };
if !super::pairing::commits_to(&commit, node_nonce) {
return Err(OfferRefused::new("pairing_mismatch", "the offer's nonce is not the one committed to at `_info`"));
}
let host_key = self.peers.inner.key.as_ref().map(|k| k.kid().to_string()).unwrap_or_default();
let code = super::pairing::pairing_code(&host_key, principal, node_nonce, &host_nonce);
let proven = match (params.get("possession").and_then(Value::as_str), crate::plexus::peer_token::key_of(principal)) {
(Some(sig), Some(vk)) => {
let input = possession_input(&host_key, principal, node_nonce, &host_nonce, &self.peers.inner.service, name);
let sig = base64::Engine::decode(&base64::engine::general_purpose::URL_SAFE_NO_PAD, sig).ok().and_then(|b| <[u8; 64]>::try_from(b.as_slice()).ok());
match sig {
Some(sig) if vk.verify_strict(input.as_bytes(), &ed25519_dalek::Signature::from_bytes(&sig)).is_ok() => true,
_ => return Err(OfferRefused::new("unauthenticated", format!("the offer's `possession` is not {principal}'s signature over this connection"))),
}
}
_ => false,
};
Ok((Some(code), proven))
}
async fn offer(self: &Arc<Self>, id: &Value, params: Value) -> Vec<u8> {
let peers = &self.peers;
let Some(namespace) = params.get("namespace").and_then(Value::as_str).map(str::to_string) else {
return refusal(id, -32602, "invalid_params", "an offer names its `namespace`");
};
let name = params.get("mount").and_then(Value::as_str).unwrap_or(&namespace).to_string();
if let Err(why) = valid_name(&name).and_then(|_| valid_name(&namespace).map_err(|w| format!("its namespace: {w}"))) {
return refusal(id, -32602, "invalid_params", &why);
}
let Some(principal) = self.principal.clone() else {
return refusal(id, -32001, "unauthenticated", "sign in first: an anonymous socket offers nothing");
};
if self.offering.swap(true, Ordering::AcqRel) {
return refusal(id, -32602, "invalid_params", "this socket has offered already");
}
let (code, key_proven) = match self.paired(¶ms, &principal, &name) {
Ok(p) => p,
Err(r) => return refusal(id, -32001, &r.kind, &r.message),
};
let offer = Offer { socket: self.id, name: name.clone(), namespace: namespace.clone(), principal: principal.clone(), key_proven, from: self.from.clone(), code: code.clone() };
let admission = peers.inner.admission.read().unwrap().clone();
if let Some(a) = &admission {
let decided = match a.decide(&offer) {
Decision::Admit => Ok(()),
Decision::Refuse(r) => Err(r),
Decision::Pending(wait) => {
let notice = json!({ "jsonrpc": "2.0", "method": OFFER_PENDING, "params": { "id": id, "code": code } });
let _ = self.out.send(serde_json::to_vec(¬ice).unwrap_or_default());
tracing::info!(target: "plexus::audit", peer = %name, %principal, from = ?self.from, "dial-in offer pending");
wait.await
}
};
if let Err(r) = decided {
tracing::warn!(target: "plexus::audit", peer = %name, %principal, from = ?self.from, kind = %r.kind, "dial-in offer refused: {}", r.message);
return refusal(id, -32003, &r.kind, &r.message);
}
if self.is_closed() {
return refusal(id, -32000, "child_unavailable", "the socket closed while its offer waited");
}
}
let gate = peers.inner.gate.read().unwrap().clone();
if let Some(gate) = gate {
let grant = Grant::new(format!("{}.offer", peers.inner.ns));
if let Err(r) = gate.check(self.auth.as_ref(), &grant, Some(&name)) {
let (code, kind, message) = match r {
Refusal::Unauthenticated | Refusal::Ended { .. } => (-32001, "unauthenticated", r.to_string()),
Refusal::MissingGrant { .. } => (-32003, "forbidden", format!("{principal} holds no `{grant}` on `{name}`")),
};
return refusal(id, code, kind, &message);
}
}
if let Some(e) = peers.inner.table.read().unwrap().get(&name) {
if e.principal != principal {
return refusal(id, -32010, "name_taken", &format!("`{name}` is held by another principal's connection"));
}
}
let path = format!("{}.{name}", peers.inner.ns);
let spec = ChildSpec::Ws { name: path.clone(), url: format!("dial-in:{principal}") };
let shared = Shared::new(spec, peers.inner.opts.clone(), peers.inner.principal.clone());
let conn = Conn::new(&path, None, self.out.clone(), peers.inner.opts.buffer, Arc::clone(&shared.counters));
let _ = conn.ns.set(namespace.clone());
let _ = conn.up.set(Arc::clone(&shared.upcalls));
if key_proven {
let _ = conn.peer_key.set(principal.clone());
}
let _ = shared.identity.set(PeerIdentity::of(&conn));
shared.install(Arc::clone(&conn));
*self.link.lock().unwrap() = Some((Arc::clone(&conn), Arc::clone(&shared)));
let t0 = Instant::now();
let hop = json!({ "principal": peers.inner.principal, "for": [], "limit": null, "call": uuid::Uuid::new_v4().to_string() });
let asked = ask(&conn, &format!("{namespace}.call"), json!({ "method": format!("{namespace}.schema"), "params": {}, "hop": hop }), peers.inner.opts.timeout).await;
let mounted = match asked {
Ok(raw) => {
let deadline = t0 + peers.inner.opts.timeout;
shared.walk(&conn, &namespace, &raw, deadline).await.map(|nodes| Kept { top: raw, nodes })
}
Err(e) => Err(e),
};
let mounted = mounted.and_then(|kept| {
let hash = kept.hash();
let schema = mount_schema(&path, kept.top)?;
let nodes = kept.nodes.into_iter().map(|(p, raw)| node_schema(&p, raw).map(|s| (p, s))).collect::<Result<Vec<_>, _>>()?;
let _ = shared.mounted_hash.set(hash.clone());
Ok((schema, nodes, hash))
});
let (schema, nodes, hash) = match mounted {
Ok(m) => m,
Err(why) => {
self.link.lock().unwrap().take();
return refusal(id, -32000, "invalid_params", &format!("not mounted: {why}"));
}
};
let timings = MountTimings { spawn_ms: 0.0, schema_ms: t0.elapsed().as_secs_f64() * 1e3, merge_ms: 0.0 };
let methods: Vec<String> = schema.methods.iter().map(|m| m.name.clone()).collect();
let child = RemoteChild { inner: Arc::new(Inner::new(shared, schema, nodes, timings)) };
child.inner.shared.upcalls.set_needs(child.inner.needs.as_deref());
if let Some(a) = &admission {
if let Err(r) = a.vet(&offer, &child) {
self.link.lock().unwrap().take();
tracing::warn!(target: "plexus::audit", peer = %name, %principal, kind = %r.kind, "dial-in offer refused at its schema: {}", r.message);
return refusal(id, -32003, &r.kind, &r.message);
}
}
let replaced = {
let mut m = self.mount.lock().unwrap();
if self.is_closed() {
drop(m);
self.link.lock().unwrap().take();
return refusal(id, -32000, "child_unavailable", "the socket closed while it was mounted");
}
let replaced = peers.inner.table.write().unwrap().insert(name.clone(), Entry { child: child.clone(), principal: principal.clone(), namespace: namespace.clone(), socket: self.id, since: Instant::now(), holder: Arc::downgrade(self) });
*m = Some(name.clone());
replaced
};
if let Some(a) = &admission {
if let Err(r) = a.mounted(&offer, &child, self) {
self.mount.lock().unwrap().take();
if let Some((conn, shared)) = self.link.lock().unwrap().take() {
gone(&Arc::downgrade(&shared), &conn, &r.message);
}
{
let mut t = peers.inner.table.write().unwrap();
if t.get(&name).is_some_and(|e| e.socket == self.id) {
t.remove(&name);
}
if let Some(old) = replaced.filter(|e| e.holder.upgrade().is_some_and(|s| !s.is_closed())) {
t.entry(name.clone()).or_insert(old);
}
}
tracing::warn!(target: "plexus::audit", peer = %name, %principal, kind = %r.kind, "dial-in offer refused as it was mounted: {}", r.message);
return refusal(id, -32003, &r.kind, &r.message);
}
}
if replaced.is_some() {
let _ = peers.inner.events.send(PeerEvent::Unmounted { name: name.clone(), why: "a new connection of the same principal took the name".into() });
}
tracing::info!(target: "plexus::peers", peer = %name, %principal, %namespace, methods = methods.len(), "peer mounted");
peers.moved();
let _ = peers.inner.events.send(PeerEvent::Mounted { name: name.clone(), principal, namespace: namespace.clone(), methods });
serde_json::to_vec(&json!({ "jsonrpc": "2.0", "id": id, "result": {
"offered": namespace,
"mount": name,
"path": path,
"schema_hash": params.get("schema_hash").cloned().unwrap_or(Value::String(hash)),
"service": { "namespace": peers.inner.service, "principal": peers.inner.principal },
} }))
.unwrap_or_default()
}
}
async fn ask(conn: &Conn, method: &str, params: Value, timeout: Duration) -> Result<Value, String> {
let (_, mut rx) = conn.open(method, params, timeout, None).await.map_err(|e| match e {
PlexusStreamItem::Error { message, .. } => message,
other => format!("{other:?}"),
})?;
let deadline = tokio::time::Instant::now() + timeout;
let mut first = None;
loop {
match tokio::time::timeout_at(deadline, rx.recv()).await {
Ok(Some(PlexusStreamItem::Data { content, .. })) => {
first.get_or_insert(content.into_value());
}
Ok(Some(PlexusStreamItem::Error { message, .. })) => return Err(message),
Ok(Some(PlexusStreamItem::Done { .. })) | Ok(None) => break,
Ok(Some(_)) => {}
Err(_) => return Err(format!("no answer in {timeout:?}")),
}
}
first.ok_or_else(|| "it answered no data".to_string())
}
#[async_trait]
impl Activation for Peers {
type Methods = PeersMethods;
fn namespace(&self) -> &str {
&self.inner.ns
}
fn version(&self) -> &str {
env!("CARGO_PKG_VERSION")
}
fn description(&self) -> &str {
"The peers connected now: services that dialed in and offered a surface, each mounted by name while its socket lives"
}
fn methods(&self) -> Vec<&str> {
vec!["list", "watch", "offer"]
}
fn method_help(&self, method: &str) -> Option<String> {
self.own_methods().into_iter().find(|m| m.name == method).map(|m| m.description)
}
async fn call(&self, method: &str, params: Value, auth: Option<&AuthContext>, raw: Option<&crate::request::RawRequestContext>) -> Result<PlexusStream, PlexusError> {
match method {
"schema" => {
let schema = self.plugin_schema();
let content = match params.get("method").and_then(Value::as_str) {
Some(m) => match schema.methods.iter().find(|x| x.name == m) {
Some(ms) => serde_json::to_value(ms).unwrap_or_default(),
None => return Err(PlexusError::MethodNotFound { activation: self.inner.ns.clone(), method: m.to_string() }),
},
None => serde_json::to_value(&schema).unwrap_or_default(),
};
let ns = &self.inner.ns;
let item = PlexusStreamItem::Data { metadata: meta(ns).into(), content_type: format!("{ns}.schema").into(), content: content.into() };
Ok(Box::pin(futures::stream::iter([item, self.done()])))
}
"list" => {
let items: Vec<PlexusStreamItem> = self.list().into_iter().map(|p| self.data("list", serde_json::to_value(p).unwrap_or_default())).chain([self.done()]).collect();
Ok(Box::pin(futures::stream::iter(items)))
}
"watch" => {
let mut rx = self.events();
let now: Vec<PeerEvent> = self
.list()
.into_iter()
.map(|p| PeerEvent::Mounted { name: p.name, principal: p.principal, namespace: p.namespace, methods: p.methods })
.collect();
let me = self.clone();
Ok(Box::pin(async_stream::stream! {
for e in now {
yield me.data("watch", serde_json::to_value(e).unwrap_or_default());
}
loop {
match rx.recv().await {
Ok(e) => yield me.data("watch", serde_json::to_value(e).unwrap_or_default()),
Err(broadcast::error::RecvError::Lagged(_)) => continue,
Err(broadcast::error::RecvError::Closed) => break,
}
}
yield me.done();
}))
}
"offer" => Err(PlexusError::ExecutionError(format!(
"an offer is made on the peer's own socket, as `{}.offer` (spec/14), not called",
self.inner.service
))),
m if m.contains('.') => route_to_child(self, m, params, auth, raw).await,
m if self.get(m).is_some() => Err(PlexusError::MethodNotFound { activation: format!("{}.{m}", self.inner.ns), method: String::new() }),
m => Err(PlexusError::MethodNotFound { activation: self.inner.ns.clone(), method: m.to_string() }),
}
}
fn into_rpc_methods(self) -> jsonrpsee::Methods {
jsonrpsee::Methods::new()
}
fn plugin_schema(&self) -> PluginSchema {
let mut methods = self.own_methods();
let mut children = Vec::new();
for (name, e) in self.inner.table.read().unwrap().iter() {
let s = &e.child.inner.schema;
let description = format!("a peer: {} ({}, connected as {})", s.description, e.namespace, e.principal);
methods.push(MethodSchema::new(name.clone(), description.clone(), s.hash.clone()).with_role(MethodRole::StaticChild));
children.push(crate::plexus::schema::ChildSummary { namespace: name.clone(), description, hash: s.hash.clone() });
}
let mut schema = PluginSchema::leaf(self.inner.ns.clone(), self.version(), self.description(), methods);
#[allow(deprecated)]
if !children.is_empty() {
schema.children = Some(children);
}
let ns = &self.inner.ns;
schema.roles.insert("viewer".into(), vec![format!("{ns}.list"), format!("{ns}.watch")]);
schema
}
}
#[async_trait]
impl ChildRouter for Peers {
fn router_namespace(&self) -> &str {
&self.inner.ns
}
async fn router_call(&self, method: &str, params: Value, auth: Option<&AuthContext>, raw: Option<&crate::request::RawRequestContext>) -> Result<PlexusStream, PlexusError> {
Activation::call(self, method, params, auth, raw).await
}
async fn get_child(&self, name: &str) -> Option<Box<dyn ChildRouter>> {
self.get(name).map(|c| Box::new(c) as Box<dyn ChildRouter>)
}
async fn list_children(&self) -> Option<BoxStream<'_, String>> {
Some(futures::stream::iter(self.names()).boxed())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_mount_name_is_one_segment_and_not_a_method() {
assert!(valid_name("alice").is_ok());
assert!(valid_name("a-b_9").is_ok());
assert!(valid_name("a.b").is_err());
assert!(valid_name("").is_err());
assert!(valid_name("list").is_err());
}
}