Skip to content

Commit

Permalink
feat(rust): ockam node show to use dynamic data from node
Browse files Browse the repository at this point in the history
  • Loading branch information
neil2468 authored and mergify[bot] committed Oct 5, 2022
1 parent 3ad498f commit ea1bd79
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub struct InletStatus<'a> {
#[b(3)] pub alias: Cow<'a, str>,
/// An optional status payload
#[b(4)] pub payload: Option<Cow<'a, str>>,
#[b(5)] pub outlet_route: Cow<'a, str>,
}

impl<'a> InletStatus<'a> {
Expand All @@ -102,6 +103,7 @@ impl<'a> InletStatus<'a> {
worker_addr: "".into(),
alias: "".into(),
payload: Some(reason.into()),
outlet_route: "".into(),
}
}

Expand All @@ -110,6 +112,7 @@ impl<'a> InletStatus<'a> {
worker_addr: impl Into<Cow<'a, str>>,
alias: impl Into<Cow<'a, str>>,
payload: impl Into<Option<Cow<'a, str>>>,
outlet_route: impl Into<Cow<'a, str>>,
) -> Self {
Self {
#[cfg(feature = "tag")]
Expand All @@ -118,6 +121,7 @@ impl<'a> InletStatus<'a> {
worker_addr: worker_addr.into(),
alias: alias.into(),
payload: payload.into(),
outlet_route: outlet_route.into(),
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions implementations/rust/ockam/ockam_api/src/nodes/models/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,44 @@ impl<'a> StartCredentialsService<'a> {
self.oneway
}
}

#[derive(Debug, Clone, Decode, Encode)]
#[rustfmt::skip]
#[cbor(map)]
pub struct ServiceStatus<'a> {
#[cfg(feature = "tag")]
#[n(0)] tag: TypeTag<8542064>,
#[n(2)] pub addr: Cow<'a, str>,
#[n(3)] pub service_type: Cow<'a, str>,
}

impl<'a> ServiceStatus<'a> {
pub fn new(addr: impl Into<Cow<'a, str>>, service_type: impl Into<Cow<'a, str>>) -> Self {
Self {
#[cfg(feature = "tag")]
tag: TypeTag,
addr: addr.into(),
service_type: service_type.into(),
}
}
}

/// Response body for listing services
#[derive(Debug, Clone, Decode, Encode)]
#[rustfmt::skip]
#[cbor(map)]
pub struct ServiceList<'a> {
#[cfg(feature = "tag")]
#[n(0)] tag: TypeTag<9587601>,
#[n(1)] pub list: Vec<ServiceStatus<'a>>
}

impl<'a> ServiceList<'a> {
pub fn new(list: Vec<ServiceStatus<'a>>) -> Self {
Self {
#[cfg(feature = "tag")]
tag: TypeTag,
list,
}
}
}
8 changes: 7 additions & 1 deletion implementations/rust/ockam/ockam_api/src/nodes/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,23 @@ pub(crate) struct AuthenticatorServiceInfo {}
pub(crate) struct InletInfo {
pub(crate) bind_addr: String,
pub(crate) worker_addr: Address,
pub(crate) outlet_route: Route,
}

impl InletInfo {
pub(crate) fn new(bind_addr: &str, worker_addr: Option<&Address>) -> Self {
pub(crate) fn new(
bind_addr: &str,
worker_addr: Option<&Address>,
outlet_route: &Route,
) -> Self {
let worker_addr = match worker_addr {
Some(addr) => addr.clone(),
None => Address::from_string(""),
};
Self {
bind_addr: bind_addr.to_owned(),
worker_addr,
outlet_route: outlet_route.to_owned(),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions implementations/rust/ockam/ockam_api/src/nodes/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ impl NodeManagerWorker {
.start_credentials_service(ctx, req, dec)
.await?
.to_vec()?,
(Get, ["node", "services"]) => {
let node_manager = self.node_manager.read().await;
self.list_services(req, &node_manager.registry).to_vec()?
}

// ==*== Forwarder commands ==*==
(Post, ["node", "forwarder"]) => self.create_forwarder(ctx, req.id(), dec).await?,
Expand Down
24 changes: 10 additions & 14 deletions implementations/rust/ockam/ockam_api/src/nodes/service/portals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl NodeManagerWorker {
info.worker_addr.to_string(),
alias,
None,
// FIXME route.as_ref().map(|r| r.to_string().into()),
info.outlet_route.to_string(),
)
})
.collect(),
Expand All @@ -68,13 +68,7 @@ impl NodeManagerWorker {
.outlets
.iter()
.map(|(alias, info)| {
OutletStatus::new(
&info.tcp_addr,
info.worker_addr.to_string(),
alias,
None,
// FIXME route.as_ref().map(|r| r.to_string().into()),
)
OutletStatus::new(&info.tcp_addr, info.worker_addr.to_string(), alias, None)
})
.collect(),
))
Expand Down Expand Up @@ -109,7 +103,7 @@ impl NodeManagerWorker {
};

let access_control = node_manager.access_control(check_credential)?;
let options = InletOptions::new(bind_addr.clone(), outlet_route, access_control);
let options = InletOptions::new(bind_addr.clone(), outlet_route.clone(), access_control);

let res = node_manager
.tcp_transport
Expand All @@ -121,28 +115,30 @@ impl NodeManagerWorker {
// TODO: Use better way to store inlets?
node_manager.registry.inlets.insert(
alias.clone(),
InletInfo::new(&bind_addr, Some(&worker_addr)),
InletInfo::new(&bind_addr, Some(&worker_addr), &outlet_route),
);

Response::ok(req.id()).body(InletStatus::new(
bind_addr,
worker_addr.to_string(),
alias,
None,
outlet_route.to_string(),
))
}
Err(e) => {
// TODO: Use better way to store inlets?
node_manager
.registry
.inlets
.insert(alias.clone(), InletInfo::new(&bind_addr, None));
node_manager.registry.inlets.insert(
alias.clone(),
InletInfo::new(&bind_addr, None, &outlet_route),
);

Response::bad_request(req.id()).body(InletStatus::new(
bind_addr,
"",
alias,
Some(e.to_string().into()),
outlet_route.to_string(),
))
}
})
Expand Down
52 changes: 48 additions & 4 deletions implementations/rust/ockam/ockam_api/src/nodes/service/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::echoer::Echoer;
use crate::error::ApiError;
use crate::identity::IdentityService;
use crate::nodes::models::services::{
StartAuthenticatedServiceRequest, StartAuthenticatorRequest, StartCredentialsService,
StartEchoerServiceRequest, StartIdentityServiceRequest, StartUppercaseServiceRequest,
StartVaultServiceRequest, StartVerifierService,
ServiceList, ServiceStatus, StartAuthenticatedServiceRequest, StartAuthenticatorRequest,
StartCredentialsService, StartEchoerServiceRequest, StartIdentityServiceRequest,
StartUppercaseServiceRequest, StartVaultServiceRequest, StartVerifierService,
};
use crate::nodes::registry::{CredentialsServiceInfo, VerifierServiceInfo};
use crate::nodes::registry::{CredentialsServiceInfo, Registry, VerifierServiceInfo};
use crate::nodes::NodeManager;
use crate::uppercase::Uppercase;
use crate::vault::VaultService;
Expand Down Expand Up @@ -342,4 +342,48 @@ impl NodeManagerWorker {

Ok(Response::ok(req.id()))
}

pub(super) fn list_services<'a>(
&self,
req: &Request<'a>,
registry: &'a Registry,
) -> ResponseBuilder<ServiceList<'a>> {
let mut list = Vec::new();
registry
.vault_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "vault")));
registry
.identity_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "identity")));
registry
.authenticated_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "authenticated")));
registry
.uppercase_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "uppercase")));
registry
.echoer_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "echoer")));
registry
.verifier_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "verifier")));
registry
.credentials_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "credentials")));

#[cfg(feature = "direct-authenticator")]
registry
.authenticator_service
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "authenticator")));

Response::ok(req.id()).body(ServiceList::new(list))
}
}
6 changes: 6 additions & 0 deletions implementations/rust/ockam/ockam_api/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ pub fn try_address_to_multiaddr(a: &Address) -> Result<MultiAddr, Error> {
Ok(ma)
}

/// Try to convert an Ockam Address into a MultiAddr.
pub fn addr_to_multiaddr<T: Into<Address>>(a: T) -> Option<MultiAddr> {
let r: Route = Route::from(a);
route_to_multiaddr(&r)
}

/// Tells whether the input MultiAddr references a local node or a remote node.
///
/// This should be called before cleaning the MultiAddr.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub async fn show_identity(
let resp: Vec<u8> = ctx
.send_and_receive(
base_route.modify().append(NODEMANAGER_ADDR),
api::short_identity()?,
api::short_identity().to_vec()?,
)
.await?;

Expand Down
Loading

0 comments on commit ea1bd79

Please sign in to comment.