1mod host;
65mod link;
66mod names;
68mod peer;
70mod spec;
71mod upcall;
73mod proven;
75#[cfg(feature = "child-ws")]
76mod ws;
77#[cfg(feature = "clap")]
78mod args;
79mod dial_in;
82#[cfg(feature = "child-ws")]
83mod dial_out;
84
85use std::pin::Pin;
86use std::sync::atomic::Ordering;
87use std::sync::Arc;
88use std::task::{Context, Poll};
89use std::time::Instant;
90
91use async_trait::async_trait;
92use futures::Stream;
93use plexus_auth_core::grants::{GrantsGeneration, Reach};
94use serde_json::{json, Value};
95use tokio::sync::mpsc;
96
97pub use host::{host, serve_child, serve_child_on, serve_child_proven, serve_child_stdio, serve_child_with, ChildPeer, HostTransport, ServeChild};
98pub use upcall::{Consented, HOST_CALL, HOST_CALL_UNSUB};
99pub use link::{MountTimings, KEYED_MOUNT_NEEDS_A_KEY};
100pub use spec::{check_ws_url, is_insecure_ws, is_loopback_host, parse_duration, Backoff, ChildError, ChildOptions, ChildSpec, INSECURE_TRANSPORT};
101#[cfg(feature = "clap")]
102pub use args::ChildArgs;
103pub use dial_in::{PeerEvent, PeerInfo, PeerRoles, PeerSocket, Peers, PeersMethods};
104#[cfg(feature = "child-ws")]
105pub use dial_out::{key_principal, DialOut, DialState, Exposed};
106#[cfg(feature = "child-ws")]
107pub use ws::serve_child_ws;
108pub use proven::{dial_proof, KeyedUpgrade, Proven, CHILD_DIAL_AGENT, UPGRADE_LEDGER_MAX};
109
110use link::{meta, too_many, unavailable, Conn, Counters, Shared, SubKey};
111
112use super::auth::AuthContext;
113use super::method_enum::MethodEnumSchema;
114use super::plexus::{Activation, ChildRouter, PlexusError};
115use super::schema::{MethodRole, PluginSchema};
116use super::streaming::PlexusStream;
117use super::types::PlexusStreamItem;
118
119#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
127pub struct PeerIdentity {
128 pub backend: String,
130 #[serde(default, skip_serializing_if = "Option::is_none")]
132 pub tls_key: Option<String>,
133 #[serde(default, skip_serializing_if = "Option::is_none")]
136 pub key: Option<String>,
137}
138
139impl PeerIdentity {
140 fn of(conn: &Conn) -> Self {
141 Self { backend: conn.ns().to_string(), tls_key: conn.tls_key.get().cloned(), key: conn.peer_key.get().cloned() }
142 }
143}
144
145impl std::fmt::Display for PeerIdentity {
146 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
147 write!(f, "backend `{}`", self.backend)?;
148 match &self.tls_key {
149 Some(k) => write!(f, ", TLS key {}", &k[..k.len().min(16)]),
150 None => write!(f, ", no TLS key"),
151 }?;
152 match &self.key {
153 Some(k) => write!(f, ", key {}", &k[..k.len().min(20)]),
154 None => Ok(()),
155 }
156 }
157}
158
159#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
161pub struct ChildStats {
162 pub open: usize,
164 pub unasked: u64,
166 pub restarts: u64,
168 pub calls: u64,
170 pub name_schemas: u64,
173 pub name_schema_hits: u64,
175 pub names_kept: usize,
178 pub calls_back: u64,
180 pub up: bool,
182}
183
184#[derive(Debug, Clone, Copy)]
187pub enum ChildMethods {}
188
189impl schemars::JsonSchema for ChildMethods {
190 fn schema_name() -> std::borrow::Cow<'static, str> {
191 "ChildMethods".into()
192 }
193
194 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
195 schemars::json_schema!({
196 "description": "A child service's methods are data: its schema, asked when it was mounted (`<mount>.schema`)."
197 })
198 }
199}
200
201impl MethodEnumSchema for ChildMethods {
202 fn method_names() -> &'static [&'static str] {
203 &[]
204 }
205
206 fn schema_with_consts() -> Value {
207 serde_json::to_value(schemars::schema_for!(ChildMethods)).unwrap_or(Value::Null)
208 }
209}
210
211struct Inner {
212 shared: Arc<Shared>,
213 schema: PluginSchema,
214 nodes: Vec<(String, PluginSchema)>,
219 timings: MountTimings,
220 names: names::Names,
222 needs: Option<Vec<crate::plexus::schema::Need>>,
225}
226
227impl Inner {
228 pub(super) fn new(shared: Arc<Shared>, schema: PluginSchema, nodes: Vec<(String, PluginSchema)>, timings: MountTimings) -> Self {
229 let names = names::Names::new(shared.opts.schema_cache);
230 let declared = schema.needs.is_some() || nodes.iter().any(|(_, n)| n.needs.is_some());
231 let mut needs: Vec<crate::plexus::schema::Need> = schema.needs.iter().chain(nodes.iter().filter_map(|(_, n)| n.needs.as_ref())).flatten().cloned().collect();
232 needs.sort();
233 needs.dedup_by(|a, b| a.method == b.method);
234 let needs = declared.then_some(needs);
235 Self { shared, schema, nodes, timings, names, needs }
236 }
237}
238
239#[derive(Clone)]
243pub struct RemoteChild {
244 inner: Arc<Inner>,
245}
246
247impl std::fmt::Debug for RemoteChild {
248 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
249 f.debug_struct("RemoteChild").field("spec", &self.inner.shared.spec.to_string()).field("insecure", &self.insecure()).field("stats", &self.stats()).finish()
250 }
251}
252
253fn read_schema(raw: Value) -> Result<PluginSchema, String> {
255 serde_json::from_value(peer::schema(raw)?).map_err(|e| format!("its schema does not read: {e}"))
256}
257
258pub(super) fn node_schema(path: &str, raw: Value) -> Result<PluginSchema, String> {
262 read_schema(raw).map_err(|e| format!("its child `{path}`: {e}"))
263}
264
265fn mount_schema(name: &str, raw: Value) -> Result<PluginSchema, String> {
271 let mut schema = read_schema(raw)?;
272 if schema.namespace != name {
273 tracing::info!(target: "plexus::child", child = %name, declared = %schema.namespace, "mounted under its mount name, not the namespace it declares");
274 }
275 let prefix = format!("{}.", schema.namespace);
276 let requalify = |g: &String| match g.strip_prefix(&prefix) {
277 Some(m) => format!("{name}.{m}"),
278 None => format!("{name}.{g}"),
279 };
280 schema.roles = schema.roles.iter().map(|(r, gs)| (r.clone(), gs.iter().map(requalify).collect())).collect();
281 for m in schema.methods.iter_mut() {
282 if let Some(g) = m.grant.as_ref() {
283 m.grant = Some(requalify(g));
284 }
285 }
286 schema.namespace = name.to_string();
287 Ok(schema)
288}
289
290impl RemoteChild {
291 pub async fn connect(spec: ChildSpec, opts: ChildOptions) -> Result<Self, ChildError> {
296 let name = spec.name().to_string();
297 let start = |why: String| ChildError::Start(name.clone(), why);
298 if opts.max_calls == 0 || opts.buffer == 0 {
299 return Err(start("max_calls and buffer are at least 1".into()));
300 }
301 let principal = Self::principal_for(&spec, &opts).map_err(start)?;
302 let shared = Shared::new(spec, opts, principal);
303 let (conn, kept, mut timings) = shared.connect_once().await.map_err(start)?;
304 let t = Instant::now();
305 let hash = kept.hash();
306 let read = mount_schema(&name, kept.top).and_then(|schema| {
307 let nodes = kept.nodes.into_iter().map(|(path, raw)| node_schema(&path, raw).map(|s| (path, s))).collect::<Result<Vec<_>, _>>()?;
308 Ok((schema, nodes))
309 });
310 let (schema, nodes) = match read {
311 Ok(s) => s,
312 Err(why) => {
313 conn.shutdown.notify_one();
314 return Err(start(why));
315 }
316 };
317 let _ = shared.mounted_hash.set(hash);
318 let _ = shared.identity.set(PeerIdentity::of(&conn));
319 shared.install(conn);
320 timings.merge_ms = t.elapsed().as_secs_f64() * 1e3;
321 tracing::info!(target: "plexus::child", child = %name, spawn_ms = timings.spawn_ms, schema_ms = timings.schema_ms, merge_ms = timings.merge_ms, methods = schema.methods.len(), children = nodes.len(), "child mounted");
322 if let ChildSpec::Ws { url, .. } = &shared.spec {
323 if is_insecure_ws(url) {
324 tracing::warn!(target: "plexus::child", child = %name, %url, "insecure: mounted over plain ws:// beyond loopback (transport.insecure_ws); its hops and key proof cross in clear");
325 }
326 }
327 let inner = Inner::new(shared, schema, nodes, timings);
328 inner.shared.upcalls.set_needs(inner.needs.as_deref());
330 Ok(Self { inner: Arc::new(inner) })
331 }
332
333 #[allow(unused_variables)]
334 fn principal_for(spec: &ChildSpec, opts: &ChildOptions) -> Result<String, String> {
335 #[cfg(feature = "child-ws")]
336 if let (ChildSpec::Ws { .. }, Some(path)) = (spec, &opts.key) {
337 return Ok(ws::principal_of(&ws::read_seed(path)?));
338 }
339 Ok(opts.principal.clone())
340 }
341
342 pub fn stats(&self) -> ChildStats {
344 let c: &Counters = &self.inner.shared.counters;
345 ChildStats {
346 open: c.open.load(Ordering::Relaxed),
347 unasked: c.unasked.load(Ordering::Relaxed),
348 restarts: c.restarts.load(Ordering::Relaxed),
349 calls: c.calls.load(Ordering::Relaxed),
350 name_schemas: self.inner.names.hops.load(Ordering::Relaxed),
351 name_schema_hits: self.inner.names.hits.load(Ordering::Relaxed),
352 names_kept: self.inner.names.len(c.restarts.load(Ordering::Relaxed)),
353 calls_back: self.inner.shared.upcalls.made.load(Ordering::Relaxed),
354 up: self.inner.shared.is_up(),
355 }
356 }
357
358 pub fn identity(&self) -> Option<&PeerIdentity> {
362 self.inner.shared.identity.get()
363 }
364
365 pub fn set_consent(&self, consent: Option<Vec<Consented>>) {
370 self.inner.shared.upcalls.set_consent(consent);
371 }
372
373 pub fn consent(&self) -> Option<Vec<Consented>> {
375 self.inner.shared.upcalls.consent().map(|c| c.to_vec())
376 }
377
378 pub fn revoke(&self, why: &str) {
383 self.inner.shared.upcalls.revoke(why);
384 tracing::warn!(target: "plexus::audit", child = %self.name(), %why, "child revoked");
385 }
386
387 pub fn revoked(&self) -> Option<String> {
389 self.inner.shared.upcalls.revoked()
390 }
391
392 pub fn may_exchange(&self, need: &str, forwarded: &str, presented: &crate::plexus::peer_token::PeerClaims) -> Result<Vec<String>, String> {
401 if self.peer_key() != Some(presented.aud.as_str()) {
402 return Err(format!("the token presented is not `{}`'s", self.name()));
403 }
404 let up = &self.inner.shared.upcalls;
405 let svc = up.host().and_then(|h| h.upgrade()).ok_or_else(|| format!("`{}`'s service is not known yet", self.name()))?;
406 let decided = up.decide(need, Some((&svc, presented))).map_err(|(_, said)| said)?;
407 let Some(upcall::Decided { held: Some(held), .. }) = decided else {
408 return Err(format!("`{need}` is not among `{}`'s needs", self.name()));
409 };
410 Ok(held.iter().map(|s| format!("{forwarded}@{s}")).collect())
411 }
412
413 pub fn peer_key(&self) -> Option<&str> {
416 self.identity().and_then(|i| i.key.as_deref())
417 }
418
419 pub fn is_up(&self) -> bool {
421 self.inner.shared.is_up()
422 }
423
424 pub fn dead(&self) -> Option<String> {
429 self.inner.shared.dead()
430 }
431
432 pub fn why_down(&self) -> Option<String> {
436 self.inner.shared.why_down()
437 }
438
439 pub async fn up(&self) -> Result<(), String> {
443 match self.inner.shared.ensure().await {
444 Ok(_) => Ok(()),
445 Err(PlexusStreamItem::Error { message, error, .. }) => Err(match error {
446 plexus_client::RemoteError::ChildUnavailable { reason, .. } => reason,
447 _ => message,
448 }),
449 Err(other) => Err(format!("{other:?}")),
450 }
451 }
452
453 pub fn needs(&self) -> Option<&[crate::plexus::schema::Need]> {
456 self.inner.needs.as_deref()
457 }
458
459 pub fn calls_open_back(&self) -> usize {
461 self.inner.shared.upcalls.open_now()
462 }
463
464 pub fn mounted_hash(&self) -> &str {
467 self.inner.shared.mounted_hash.get().map(String::as_str).unwrap_or_default()
468 }
469
470 pub fn pid(&self) -> Option<u32> {
472 self.inner.shared.current().and_then(|c| c.pid)
473 }
474
475 pub fn name(&self) -> &str {
477 &self.inner.shared.name
478 }
479
480 pub fn insecure(&self) -> bool {
484 matches!(&self.inner.shared.spec, ChildSpec::Ws { url, .. } if is_insecure_ws(url))
485 }
486
487 pub fn spec(&self) -> &ChildSpec {
489 &self.inner.shared.spec
490 }
491
492 pub fn principal(&self) -> &str {
494 &self.inner.shared.principal
495 }
496
497 pub fn mount_timings(&self) -> MountTimings {
499 self.inner.timings
500 }
501
502 pub fn routed(&self) -> usize {
504 self.inner.shared.current().map_or(0, |c| c.routed())
505 }
506
507 fn hop(&self, conn: &Conn, auth: Option<&AuthContext>, reach: &Reach, method: &str) -> (Value, String) {
511 let for_: Vec<String> = auth.and_then(|a| a.principal()).map(|p| p.to_string()).into_iter().collect();
512 let limit = reach.grant().map(|g| {
513 let grant = match g.as_str().strip_prefix(&format!("{}.", self.name())) {
514 Some(m) => format!("{}.{m}", conn.ns()),
515 None => format!("{}.{method}", conn.ns()),
520 };
521 let held_on: Vec<String> = reach.held_on().iter().map(|s| s.to_string()).collect();
522 json!({ "grant": grant, "held_on": held_on })
523 });
524 let call = uuid::Uuid::new_v4().to_string();
525 (json!({ "principal": self.inner.shared.principal, "for": for_, "limit": limit, "call": call }), call)
526 }
527
528 fn mint(&self, auth: Option<&AuthContext>, reach: &Reach, hop: &Value, path: &str, call: &str) -> Result<Option<upcall::Minted>, String> {
533 use crate::plexus::peer_token::{mint_scope, onward_scope, MintRequest};
534 let Some(issuer) = self.inner.shared.opts.tokens.as_ref() else { return Ok(None) };
535 if let Some(why) = self.inner.shared.upcalls.revoked() {
538 return Err(format!("`{}` is revoked ({why})", self.name()));
539 }
540 let Some(sub) = auth.and_then(|a| a.principal()).map(|p| p.to_string()) else { return Ok(None) };
541 let peer_key = self.peer_key().map(str::to_string);
542 if peer_key.is_none() && !matches!(self.spec(), ChildSpec::Stdio { .. }) {
543 return Ok(None);
544 }
545 if peer_key.is_some() && crate::plexus::peer_token::key_of(&self.inner.shared.principal).is_none() {
548 return Err(format!("{KEYED_MOUNT_NEEDS_A_KEY}: this service dials as `{}`, not a key", self.inner.shared.principal));
549 }
550 let held: Vec<String> = if reach.is_everywhere() { vec!["*".into()] } else { reach.held_on().iter().map(|s| s.to_string()).collect() };
551 let needs = crate::plexus::service_scope::current()
552 .and_then(|w| w.upgrade())
553 .map(|svc| self.inner.shared.upcalls.need_grants(&svc))
554 .unwrap_or_default();
555 let forwarded = hop.get("limit").and_then(|l| l.get("grant")).and_then(Value::as_str);
556 let presented = upcall::presented();
557 let scope = match &presented {
559 Some(p) => onward_scope(p, reach.grant().map(|g| g.as_str()), forwarded, &held, &needs),
560 None => mint_scope(forwarded, &held, &needs),
561 };
562 let req = MintRequest { sub, peer_key, name: self.name().to_string(), scope, call: call.to_string(), method: path.to_string(), presented };
563 let (token, claims) = issuer.mint(req).map_err(|e| e.to_string())?;
564 Ok(Some(upcall::Minted { token, claims }))
565 }
566
567 fn node(&self, path: &str) -> Option<&PluginSchema> {
569 self.inner.nodes.iter().find(|(p, _)| p == path).map(|(_, s)| s)
570 }
571
572 fn slot_of(&self, path: &str) -> Option<(String, String)> {
578 let segs: Vec<&str> = path.split('.').collect();
579 let (n, schema) = (1..segs.len())
580 .rev()
581 .find_map(|n| self.node(&segs[..n].join(".")).map(|s| (n, s)))
582 .unwrap_or((0, &self.inner.schema));
583 let parent = std::iter::once(self.name()).chain(segs[..n].iter().copied()).collect::<Vec<_>>().join(".");
584 let next = segs[n];
585 if schema.methods.iter().any(|m| m.name == next && matches!(m.role, MethodRole::Peer { .. })) {
587 return Some((crate::plexus::slots::slot_path(&parent, next), segs[..(n + 2).min(segs.len())].join(".")));
588 }
589 let slot = schema.methods.iter().find(|m| matches!(m.role, MethodRole::DynamicChild { .. }))?;
590 Some((crate::plexus::slots::slot_path(&parent, &slot.name), segs[..=n].join(".")))
591 }
592
593 fn unreached(&self, method: &str) -> PlexusError {
595 PlexusError::MethodNotFound { activation: self.name().to_string(), method: method.to_string() }
596 }
597
598 async fn name_schema(&self, path: &str, params: &Value, auth: Option<&AuthContext>) -> PlexusStream {
605 let epoch = self.inner.shared.counters.restarts.load(Ordering::Relaxed);
606 if let Some(s) = self.inner.names.get(path, epoch) {
607 return self.schema_stream_of(&s, &s.namespace, params);
608 }
609 self.inner.names.crossed();
610 let items: Vec<PlexusStreamItem> = futures::StreamExt::collect(self.forward(&format!("{path}.schema"), json!({}), auth, None).await).await;
611 let kept = items.iter().find_map(|i| match i {
612 PlexusStreamItem::Data { metadata, content, .. } => Some((metadata.plexus_hash.clone(), content.clone().into_value())),
613 _ => None,
614 });
615 let Some((seen, raw)) = kept else {
616 return Box::pin(futures::stream::iter(items));
617 };
618 match read_schema(raw) {
619 Ok(schema) => {
620 let epoch = self.inner.shared.counters.restarts.load(Ordering::Relaxed);
622 self.inner.names.put(path, schema.clone(), seen, epoch);
623 self.schema_stream_of(&schema, &schema.namespace, params)
624 }
625 Err(_) => Box::pin(futures::stream::iter(items)),
626 }
627 }
628
629 fn schema_stream(&self, params: &Value) -> PlexusStream {
630 self.schema_stream_of(&self.inner.schema, self.name(), params)
631 }
632
633 fn schema_stream_of(&self, schema: &PluginSchema, label: &str, params: &Value) -> PlexusStream {
636 let ns = label.to_string();
637 let content = match params.get("method").and_then(Value::as_str) {
638 Some(m) => match schema.methods.iter().find(|x| x.name == m) {
639 Some(ms) => serde_json::to_value(ms).unwrap_or_default(),
640 None => {
641 let error = plexus_client::RemoteError::MethodNotFound { activation: ns.clone(), method: m.to_string() };
642 let mut e = PlexusStreamItem::error(meta(&ns), error, false);
643 if let PlexusStreamItem::Error { message, code, .. } = &mut e {
644 *message = format!("Method '{m}' not found");
645 *code = Some("-32602".into());
646 }
647 return Box::pin(futures::stream::iter([e]));
648 }
649 },
650 None => serde_json::to_value(schema).unwrap_or_default(),
651 };
652 Box::pin(futures::stream::iter([
653 PlexusStreamItem::Data { metadata: meta(&ns).into(), content_type: format!("{ns}.schema").into(), content: content.into() },
654 PlexusStreamItem::Done { metadata: meta(&ns) },
655 ]))
656 }
657
658 async fn forward(&self, method: &str, params: Value, auth: Option<&AuthContext>, instance: Option<String>) -> PlexusStream {
661 let shared = &self.inner.shared;
662 let one = |item: PlexusStreamItem| -> PlexusStream { Box::pin(futures::stream::iter([item])) };
663 if let Some(why) = shared.upcalls.revoked() {
665 let e = PlexusError::Unauthenticated(format!("`{}` is revoked ({why}): no call is forwarded to it", self.name()));
666 return one(PlexusStreamItem::from_plexus_error(meta(self.name()), &e, false));
667 }
668 let reach = match super::grant_gate::current_reach() {
671 Ok(reach) => reach,
672 Err(e) => return one(PlexusStreamItem::from_plexus_error(meta(self.name()), &e, false)),
673 };
674 let max = shared.opts.max_calls;
675 if shared.counters.open.fetch_add(1, Ordering::AcqRel) >= max {
676 shared.counters.open.fetch_sub(1, Ordering::AcqRel);
677 return one(too_many(self.name(), max));
678 }
679 let guard = OpenGuard(Arc::clone(&shared.counters));
680 let conn = match shared.ensure().await {
681 Ok(c) => c,
682 Err(item) => return one(item),
683 };
684 shared.counters.calls.fetch_add(1, Ordering::Relaxed);
685 let (mut hop, call_id) = self.hop(&conn, auth, &reach, method);
686 let path = format!("{}.{method}", conn.ns());
687 let minted = match self.mint(auth, &reach, &hop, &path, &call_id) {
689 Ok(m) => m,
690 Err(why) => {
691 let e = PlexusError::Unauthenticated(format!("no token could be minted for `{}`: {why}", self.name()));
692 return one(PlexusStreamItem::from_plexus_error(meta(self.name()), &e, false));
693 }
694 };
695 if let Some(m) = &minted {
696 hop["token"] = json!(m.token);
697 if let Some(issuer) = shared.opts.tokens.as_ref() {
701 match issuer.prove_forward(&m.token, &m.claims) {
702 Ok(p) => hop["proof"] = json!(p),
703 Err(why) => {
704 let e = PlexusError::Unauthenticated(format!("no proof could be signed for `{}`'s token: {why}", self.name()));
705 return one(PlexusStreamItem::from_plexus_error(meta(self.name()), &e, false));
706 }
707 }
708 }
709 }
710 let under = shared.upcalls.open(&call_id, auth, &reach, minted);
712 if let Some(why) = shared.upcalls.revoked() {
715 drop(under);
716 let e = PlexusError::Unauthenticated(format!("`{}` is revoked ({why}): no call is forwarded to it", self.name()));
717 return one(PlexusStreamItem::from_plexus_error(meta(self.name()), &e, false));
718 }
719 let call = json!({ "method": path, "params": params, "hop": hop });
720 match conn.open(&format!("{}.call", conn.ns()), call, shared.opts.timeout).await {
721 Ok((sid, rx)) => {
722 let generation = reach.generation().cloned();
723 let seen = generation.as_ref().map_or(0, GrantsGeneration::current);
724 let instance = instance.map(|path| (Arc::clone(&self.inner), path));
725 Box::pin(CallStream { rx, sid, conn, over: false, _guard: guard, reach, generation, seen, name: self.name().to_string(), instance, under })
726 }
727 Err(item) => one(item),
728 }
729 }
730}
731
732struct OpenGuard(Arc<Counters>);
734
735impl Drop for OpenGuard {
736 fn drop(&mut self) {
737 self.0.open.fetch_sub(1, Ordering::AcqRel);
738 }
739}
740
741struct CallStream {
744 rx: mpsc::Receiver<PlexusStreamItem>,
745 sid: SubKey,
746 conn: Arc<Conn>,
747 over: bool,
748 _guard: OpenGuard,
749 reach: Reach,
752 generation: Option<GrantsGeneration>,
753 seen: u64,
754 name: String,
755 instance: Option<(Arc<Inner>, String)>,
758 under: Option<upcall::UnderGuard>,
761}
762
763impl CallStream {
764 fn finish(&mut self, why: &str) {
766 self.over = true;
767 if let Some(u) = self.under.take() {
768 u.end(why);
769 }
770 }
771
772 fn revoked(&self) -> PlexusStreamItem {
774 let grant = self.reach.grant().map(|g| g.as_str().to_string()).unwrap_or_default();
775 let error = plexus_client::RemoteError::Forbidden {
776 reason: plexus_client::DenyReason::MissingGrant {
777 grant: grant.clone(),
778 scope: "*".into(),
779 roles: Vec::new(),
780 held_on: Vec::new(),
781 },
782 };
783 let mut item = PlexusStreamItem::error(meta(&self.name), error, false);
784 if let PlexusStreamItem::Error { message, .. } = &mut item {
785 *message = format!("Forbidden: `{grant}` is no longer held — the call's grant moved while it ran");
786 }
787 item
788 }
789}
790
791impl Stream for CallStream {
792 type Item = PlexusStreamItem;
793
794 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
795 if self.over {
796 return Poll::Ready(None);
797 }
798 match self.rx.poll_recv(cx) {
799 Poll::Ready(Some(item)) => {
800 if let (Some((inner, path)), Some(m)) = (&self.instance, item.metadata()) {
801 inner.names.observe(path, &m.plexus_hash);
802 }
803 let terminal = matches!(item, PlexusStreamItem::Done { .. } | PlexusStreamItem::Error { .. });
804 let moved = self.generation.as_ref().map(GrantsGeneration::current).filter(|g| *g != self.seen);
805 if let (false, Some(g)) = (terminal, moved) {
806 self.seen = g;
807 if self.reach.current().is_none() {
808 let sid = self.sid.clone();
809 self.conn.unsubscribe(&sid);
810 self.finish("its grant was revoked");
811 return Poll::Ready(Some(self.revoked()));
812 }
813 }
814 if terminal {
815 self.finish("it ended");
816 }
817 Poll::Ready(Some(item))
818 }
819 Poll::Ready(None) => {
820 self.finish("the connection closed");
822 Poll::Ready(Some(unavailable(&self.name, "the connection closed")))
823 }
824 Poll::Pending => Poll::Pending,
825 }
826 }
827}
828
829impl Drop for CallStream {
830 fn drop(&mut self) {
831 if !self.over {
832 self.conn.unsubscribe(&self.sid);
833 }
834 }
835}
836
837#[async_trait]
838impl Activation for RemoteChild {
839 type Methods = ChildMethods;
840
841 fn needs(&self) -> Option<Vec<crate::plexus::schema::Need>> {
842 self.inner.needs.clone()
843 }
844
845 fn namespace(&self) -> &str {
846 self.name()
847 }
848
849 fn version(&self) -> &str {
850 &self.inner.schema.version
851 }
852
853 fn description(&self) -> &str {
854 &self.inner.schema.description
855 }
856
857 fn long_description(&self) -> Option<&str> {
858 self.inner.schema.long_description.as_deref()
859 }
860
861 fn methods(&self) -> Vec<&str> {
862 self.inner.schema.methods.iter().map(|m| m.name.as_str()).collect()
863 }
864
865 fn method_help(&self, method: &str) -> Option<String> {
866 self.inner.schema.methods.iter().find(|m| m.name == method).map(|m| m.description.clone())
867 }
868
869 async fn call(&self, method: &str, params: Value, auth: Option<&AuthContext>, _raw: Option<&crate::request::RawRequestContext>) -> Result<PlexusStream, PlexusError> {
870 if method == "schema" {
871 return Ok(self.schema_stream(¶ms));
872 }
873 if let Some(path) = method.strip_suffix(".schema") {
875 if let Some(s) = self.node(path) {
876 return Ok(self.schema_stream_of(s, &s.namespace, ¶ms));
877 }
878 return match self.slot_of(path) {
881 Some(_) => Ok(self.name_schema(path, ¶ms, auth).await),
882 None => Err(self.unreached(method)),
883 };
884 }
885 let found = match method.rsplit_once('.') {
886 Some((path, name)) => match self.node(path) {
888 Some(s) => s.methods.iter().find(|m| m.name == name && m.role == MethodRole::Rpc),
889 None => match self.slot_of(path) {
894 Some((_, instance)) => return Ok(self.forward(method, params, auth, Some(instance)).await),
895 None => return Err(self.unreached(method)),
896 },
897 },
898 None => self.inner.schema.methods.iter().find(|m| m.name == method && m.role == MethodRole::Rpc),
899 };
900 let Some(m) = found else {
901 return Err(PlexusError::MethodNotFound { activation: self.name().to_string(), method: method.to_string() });
902 };
903 if m.everywhere {
907 crate::call::admits_everywhere(&format!("{}.{method}", self.name()))?;
908 }
909 if m.input.is_some() {
912 return Err(PlexusError::ExecutionError(format!(
913 "`{}.{method}` takes an input stream, and a hop to a remote child does not forward one yet",
914 self.name()
915 )));
916 }
917 Ok(self.forward(method, params, auth, None).await)
918 }
919
920 fn into_rpc_methods(self) -> jsonrpsee::Methods {
923 jsonrpsee::Methods::new()
924 }
925
926 fn plugin_schema(&self) -> PluginSchema {
927 self.inner.schema.clone()
928 }
929
930 fn static_child_schemas(&self) -> Vec<(String, PluginSchema)> {
934 self.inner.nodes.clone()
935 }
936}
937
938#[async_trait]
939impl ChildRouter for RemoteChild {
940 fn router_namespace(&self) -> &str {
941 self.name()
942 }
943
944 async fn router_call(&self, method: &str, params: Value, auth: Option<&AuthContext>, raw: Option<&crate::request::RawRequestContext>) -> Result<PlexusStream, PlexusError> {
945 Activation::call(self, method, params, auth, raw).await
946 }
947
948 async fn get_child(&self, _name: &str) -> Option<Box<dyn ChildRouter>> {
949 None
950 }
951
952 fn router_schema(&self) -> Option<PluginSchema> {
953 Some(self.inner.schema.clone())
954 }
955
956 fn router_static_child_schemas(&self) -> Vec<(String, PluginSchema)> {
957 self.inner.nodes.clone()
958 }
959}
960
961#[cfg(test)]
962mod tests {
963 use super::*;
964
965 fn raw(methods: Value, roles: Value) -> Value {
966 json!({ "namespace": "stub", "version": "0.1.0", "description": "d", "self_hash": "h", "hash": "h", "methods": methods, "roles": roles })
967 }
968
969 fn m(name: &str) -> Value {
970 json!({ "name": name, "description": "", "hash": "x", "streaming": false, "bidirectional": false })
971 }
972
973 #[test]
977 fn a_method_stating_neither_cardinality_nor_streaming_is_refused() {
978 let silent = json!({ "name": "quiet", "description": "", "hash": "x" });
979 let e = mount_schema("echo", raw(json!([m("echo"), silent]), json!({}))).unwrap_err();
980 assert!(e.contains("`quiet`") && e.contains("neither cardinality"), "{e}");
981 }
982
983 #[test]
984 fn the_mount_name_wins_and_roles_are_requalified() {
985 let s = mount_schema("echo", raw(json!([m("echo"), m("shout")]), json!({ "viewer": ["stub.echo"], "editor": ["stub.echo", "stub.shout"] }))).unwrap();
986 assert_eq!(s.namespace, "echo");
987 assert_eq!(s.roles["viewer"], vec!["echo.echo".to_string()]);
988 assert_eq!(s.roles["editor"], vec!["echo.echo".to_string(), "echo.shout".to_string()]);
989 }
990
991 #[test]
993 fn a_child_with_children_mounts_and_its_method_grants_are_requalified() {
994 let mut inner = m("inner");
995 inner["role"] = json!({ "kind": "static_child" });
996 let mut own = m("echo");
997 own["grant"] = json!("stub.echo");
998 let s = mount_schema("echo", raw(json!([own, inner]), json!({}))).unwrap();
999 assert_eq!(s.methods[0].grant.as_deref(), Some("echo.echo"));
1000 assert_eq!(s.methods[1].role, MethodRole::StaticChild);
1001 let mut slot = m("item");
1002 slot["role"] = json!({ "kind": "dynamic_child", "list_method": "names" });
1003 let nested = raw(json!([m("echo"), s_inner(), slot]), json!({}));
1004 assert_eq!(link::static_children(&nested), vec!["inner".to_string()], "a dynamic child is a slot, not walked");
1005 let mut root = raw(json!([m("call")]), json!({}));
1007 root["children"] = json!([{ "namespace": "greeter", "description": "", "hash": "g" }]);
1008 assert_eq!(link::static_children(&root), vec!["greeter".to_string()]);
1009 }
1010
1011 fn s_inner() -> Value {
1012 let mut inner = m("inner");
1013 inner["role"] = json!({ "kind": "static_child" });
1014 inner
1015 }
1016
1017 #[test]
1019 fn the_tree_hash_covers_every_level() {
1020 let top = raw(json!([m("echo")]), json!({}));
1021 let flat = link::Kept { top: top.clone(), nodes: vec![] };
1022 assert_eq!(flat.hash(), link::schema_hash(&top), "a flat child's hash is its schema's, as before");
1023 let mut c = raw(json!([m("m")]), json!({}));
1024 let one = link::Kept { top: top.clone(), nodes: vec![("a.b".into(), c.clone())] }.hash();
1025 c["hash"] = json!("other");
1026 let two = link::Kept { top, nodes: vec![("a.b".into(), c)] }.hash();
1027 assert_ne!(one, two);
1028 }
1029
1030 #[test]
1031 fn typed_error_fields_cross_whole() {
1032 let item = unavailable("echo", "gone");
1033 let v = serde_json::to_value(&item).unwrap();
1034 assert_eq!(v["error_kind"], "child_unavailable");
1035 assert_eq!(v["code"], "child_unavailable");
1036 assert_eq!(v["child"], "echo");
1037 let back: PlexusStreamItem = serde_json::from_value(v.clone()).unwrap();
1038 assert_eq!(back.error_kind(), Some("child_unavailable"));
1039 assert_eq!(serde_json::to_value(&back).unwrap(), v);
1040 let plain = PlexusStreamItem::error(meta("x"), plexus_client::RemoteError::Execution { message: "m".into() }, false);
1043 let pv = serde_json::to_value(&plain).unwrap();
1044 let keys: Vec<_> = pv.as_object().unwrap().keys().cloned().collect();
1045 assert_eq!(keys, ["code", "error", "message", "metadata", "recoverable", "type"]);
1046 }
1047}