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 committed Sep 23, 2022
1 parent bd6821c commit 0a683a9
Show file tree
Hide file tree
Showing 12 changed files with 381 additions and 107 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
1 change: 1 addition & 0 deletions implementations/rust/ockam/ockam_api/src/nodes/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ impl NodeManager {
.start_credentials_service(ctx, req, dec)
.await?
.to_vec()?,
(Get, ["node", "services"]) => self.list_services(req).to_vec()?,

// ==*== Forwarder commands ==*==
(Post, ["node", "forwarder"]) => self.create_forwarder(ctx, req, dec).await?,
Expand Down
23 changes: 10 additions & 13 deletions implementations/rust/ockam/ockam_api/src/nodes/service/portals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl NodeManager {
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 @@ -41,13 +41,7 @@ impl NodeManager {
.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 @@ -81,7 +75,7 @@ impl NodeManager {
};

let access_control = self.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 = self.tcp_transport.create_inlet_extended(options).await;

Expand All @@ -90,27 +84,30 @@ impl NodeManager {
// TODO: Use better way to store inlets?
self.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?
self.registry
.inlets
.insert(alias.clone(), InletInfo::new(&bind_addr, None));
self.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
49 changes: 46 additions & 3 deletions implementations/rust/ockam/ockam_api/src/nodes/service/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ 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::NodeManager;
Expand Down Expand Up @@ -323,4 +323,47 @@ impl NodeManager {
.insert(addr, AuthenticatorServiceInfo::default());
Ok(())
}

pub(super) fn list_services(&mut self, req: &Request<'_>) -> ResponseBuilder<ServiceList> {
let mut list = Vec::new();

self.registry
.vault_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "vault")));
self.registry
.identity_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "identity")));
self.registry
.authenticated_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "authenticated")));
self.registry
.uppercase_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "uppercase")));
self.registry
.echoer_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "echoer")));
self.registry
.verifier_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "verifier")));
self.registry
.credentials_services
.keys()
.for_each(|addr| list.push(ServiceStatus::new(addr.address(), "credentials")));

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

let res_body = ServiceList::new(list);

Response::ok(req.id()).body(res_body)
}
}
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 @@ -152,6 +152,12 @@ pub fn route_to_multiaddr(r: &Route) -> Option<MultiAddr> {
Some(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 0a683a9

Please sign in to comment.