Skip to content

Commit 153a866

Browse files
committed
refactor(rust): enable requests to be messages
1 parent a5c7586 commit 153a866

File tree

112 files changed

+2777
-1016
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+2777
-1016
lines changed

Diff for: examples/rust/file_transfer/src/messages.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
1-
use ockam::Message;
1+
use ockam::{deserialize, serialize, Decodable, Encodable, Encoded, Message};
22
use serde::{Deserialize, Serialize};
33

4-
#[derive(Serialize, Deserialize)]
4+
#[derive(Serialize, Deserialize, Message)]
55
pub struct FileDescription {
66
pub name: String,
77
pub size: usize,
88
}
9-
impl Message for FileDescription {}
109

11-
#[derive(Serialize, Deserialize)]
10+
impl Encodable for FileDescription {
11+
fn encode(self) -> ockam::Result<Encoded> {
12+
serialize(self)
13+
}
14+
}
15+
16+
impl Decodable for FileDescription {
17+
fn decode(v: &[u8]) -> ockam::Result<Self> {
18+
deserialize(v)
19+
}
20+
}
21+
22+
#[derive(Serialize, Deserialize, Message)]
1223
pub enum FileData {
1324
Description(FileDescription),
1425
Data(Vec<u8>),
1526
Quit,
1627
}
1728

18-
impl Message for FileData {}
29+
impl Encodable for FileData {
30+
fn encode(self) -> ockam::Result<Encoded> {
31+
serialize(self)
32+
}
33+
}
34+
35+
impl Decodable for FileData {
36+
fn decode(v: &[u8]) -> ockam::Result<Self> {
37+
deserialize(v)
38+
}
39+
}

Diff for: examples/rust/get_started/examples/04-routing-over-transport-initiator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async fn main(ctx: Context) -> Result<()> {
1717
// Send a message to the "echoer" worker on a different node, over a tcp transport.
1818
// Wait to receive a reply and print it.
1919
let r = route![connection_to_responder, "echoer"];
20-
let reply = node.send_and_receive::<String>(r, "Hello Ockam!".to_string()).await?;
20+
let reply: String = node.send_and_receive(r, "Hello Ockam!".to_string()).await?;
2121

2222
println!("App Received: {}", reply); // should print "Hello Ockam!"
2323

Diff for: examples/rust/get_started/examples/04-routing-over-transport-two-hops-initiator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async fn main(ctx: Context) -> Result<()> {
1717
// Send a message to the "echoer" worker, on a different node, over two tcp hops.
1818
// Wait to receive a reply and print it.
1919
let r = route![connection_to_middle_node, "forward_to_responder", "echoer"];
20-
let reply = node.send_and_receive::<String>(r, "Hello Ockam!".to_string()).await?;
20+
let reply: String = node.send_and_receive(r, "Hello Ockam!".to_string()).await?;
2121
println!("App Received: {}", reply); // should print "Hello Ockam!"
2222

2323
// Stop all workers, stop the node, cleanup and return.

Diff for: examples/rust/get_started/examples/04-udp-transport-initiator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async fn main(ctx: Context) -> Result<()> {
1616
// Send a message to the "echoer" worker on a different node, over a udp transport.
1717
// Wait to receive a reply and print it.
1818
let r = route![bind, (UDP, "localhost:4000"), "echoer"];
19-
let reply = node.send_and_receive::<String>(r, "Hello Ockam!".to_string()).await?;
19+
let reply: String = node.send_and_receive(r, "Hello Ockam!".to_string()).await?;
2020

2121
println!("App Received: {}", reply); // should print "Hello Ockam!"
2222

Diff for: examples/rust/get_started/examples/05-secure-channel-over-two-transport-hops-initiator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ async fn main(ctx: Context) -> Result<()> {
2525

2626
// Send a message to the echoer worker via the channel.
2727
// Wait to receive a reply and print it.
28-
let reply = node
29-
.send_and_receive::<String>(route![channel, "echoer"], "Hello Ockam!".to_string())
28+
let reply: String = node
29+
.send_and_receive(route![channel, "echoer"], "Hello Ockam!".to_string())
3030
.await?;
3131
println!("App Received: {}", reply); // should print "Hello Ockam!"
3232

Diff for: examples/rust/get_started/examples/05-secure-channel-over-two-udp-hops-initiator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ async fn main(ctx: Context) -> Result<()> {
2929

3030
// Send a message to the echoer worker via the channel.
3131
// Wait to receive a reply and print it.
32-
let reply = node
33-
.send_and_receive::<String>(route![channel, "echoer"], "Hello Ockam!".to_string())
32+
let reply: String = node
33+
.send_and_receive(route![channel, "echoer"], "Hello Ockam!".to_string())
3434
.await?;
3535
println!("App Received: {}", reply); // should print "Hello Ockam!"
3636

Diff for: examples/rust/get_started/examples/06-credentials-exchange-client.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ async fn main(ctx: Context) -> Result<()> {
8181

8282
// Send a message to the worker at address "echoer".
8383
// Wait to receive a reply and print it.
84-
let reply = node
85-
.send_and_receive::<String>(
84+
let reply: String = node
85+
.send_and_receive(
8686
route![channel, DefaultAddress::ECHO_SERVICE],
8787
"Hello Ockam!".to_string(),
8888
)

Diff for: implementations/rust/ockam/ockam/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ pub use ockam_core::processor;
6666
/// may be changed in the future to a [`Worker`](crate::Worker)-specific macro.
6767
pub use ockam_core::worker;
6868
pub use ockam_core::{
69-
allow, deny, errcode, route, Address, Any, Encoded, Error, LocalMessage, Mailbox, Mailboxes,
70-
Message, Processor, ProtocolId, Result, Route, Routed, TransportMessage, TryClone, Worker,
69+
allow, deny, deserialize, errcode, route, serialize, Address, Any, Decodable, Encodable,
70+
Encoded, Error, LocalMessage, Mailbox, Mailboxes, Message, Processor, ProtocolId, Result,
71+
Route, Routed, TransportMessage, TryClone, Worker,
7172
};
7273
pub use ockam_identity as identity;
7374
// ---

Diff for: implementations/rust/ockam/ockam/src/remote/info.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::Message;
22
use ockam_core::compat::string::String;
33
use ockam_core::flow_control::FlowControlId;
4-
use ockam_core::{Address, Route};
4+
use ockam_core::{deserialize, serialize, Address, Decodable, Encodable, Encoded, Route};
55
use serde::{Deserialize, Serialize};
66

77
/// Information about a remotely forwarded worker.
@@ -13,6 +13,18 @@ pub struct RemoteRelayInfo {
1313
flow_control_id: Option<FlowControlId>,
1414
}
1515

16+
impl Encodable for RemoteRelayInfo {
17+
fn encode(self) -> ockam_core::Result<Encoded> {
18+
serialize(self)
19+
}
20+
}
21+
22+
impl Decodable for RemoteRelayInfo {
23+
fn decode(v: &[u8]) -> ockam_core::Result<Self> {
24+
deserialize(v)
25+
}
26+
}
27+
1628
impl RemoteRelayInfo {
1729
/// Constructor
1830
pub fn new(

Diff for: implementations/rust/ockam/ockam/tests/message/test.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
1-
use ockam::Message;
1+
use ockam::*;
22
use serde::{Deserialize, Serialize};
33

44
#[derive(Message, Deserialize, Serialize)]
55
pub struct Tmp {
66
a: String,
77
}
88

9+
impl Encodable for Tmp {
10+
fn encode(self) -> Result<Encoded> {
11+
serialize(self)
12+
}
13+
}
14+
impl Decodable for Tmp {
15+
fn decode(e: &[u8]) -> Result<Tmp> {
16+
deserialize(e)
17+
}
18+
}
19+
920
#[derive(Message, Deserialize, Serialize)]
1021
pub struct Tmp1 {
1122
a: Vec<u8>,
1223
b: Vec<Tmp>,
1324
}
1425

26+
impl Encodable for Tmp1 {
27+
fn encode(self) -> Result<Encoded> {
28+
serialize(self)
29+
}
30+
}
31+
impl Decodable for Tmp1 {
32+
fn decode(e: &[u8]) -> Result<Tmp1> {
33+
deserialize(e)
34+
}
35+
}
36+
1537
fn assert_impl<T: Message>() {}
1638
fn main() {
1739
assert_impl::<String>();

Diff for: implementations/rust/ockam/ockam/tests/relay.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ async fn test1(ctx: &mut Context) -> Result<()> {
1616

1717
let remote_info = RemoteRelay::create(ctx, route![], RemoteRelayOptions::new()).await?;
1818

19-
let resp = ctx
20-
.send_and_receive::<String>(
19+
let resp: String = ctx
20+
.send_and_receive(
2121
route![remote_info.remote_address(), "echoer"],
2222
"Hello".to_string(),
2323
)
@@ -62,8 +62,8 @@ async fn test2(ctx: &mut Context) -> Result<()> {
6262
.connect(cloud_listener.socket_string(), TcpConnectionOptions::new())
6363
.await?;
6464

65-
let resp = ctx
66-
.send_and_receive::<String>(
65+
let resp: String = ctx
66+
.send_and_receive(
6767
route![cloud_connection, remote_info.remote_address(), "echoer"],
6868
"Hello".to_string(),
6969
)
@@ -238,8 +238,8 @@ async fn test4(ctx: &mut Context) -> Result<()> {
238238
)
239239
.await?;
240240

241-
let resp = ctx
242-
.send_and_receive::<String>(route![tunnel_channel, "echoer"], "Hello".to_string())
241+
let resp: String = ctx
242+
.send_and_receive(route![tunnel_channel, "echoer"], "Hello".to_string())
243243
.await?;
244244

245245
assert_eq!(resp, "Hello");

Diff for: implementations/rust/ockam/ockam_api/src/authenticator/credential_issuer/credential_issuer_worker.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use core::time::Duration;
2-
use minicbor::Decoder;
32
use tracing::trace;
43

54
use crate::authenticator::credential_issuer::CredentialIssuer;
65
use crate::authenticator::direct::AccountAuthorityInfo;
76
use crate::authenticator::AuthorityMembersRepository;
87
use ockam::identity::{Credentials, Identifier, IdentitiesAttributes};
9-
use ockam_core::api::{Method, RequestHeader, Response};
8+
use ockam_core::api::{Method, Request, Response};
109
use ockam_core::compat::boxed::Box;
1110
use ockam_core::compat::sync::Arc;
1211
use ockam_core::compat::vec::Vec;
@@ -49,41 +48,46 @@ impl CredentialIssuerWorker {
4948
#[ockam_core::worker]
5049
impl Worker for CredentialIssuerWorker {
5150
type Context = Context;
52-
type Message = Vec<u8>;
51+
type Message = Request<Vec<u8>>;
5352

5453
async fn handle_message(&mut self, c: &mut Context, m: Routed<Self::Message>) -> Result<()> {
5554
let secure_channel_info = match SecureChannelLocalInfo::find_info(m.local_message()) {
5655
Ok(secure_channel_info) => secure_channel_info,
5756
Err(_e) => {
58-
let resp = Response::bad_request_no_request("secure channel required").to_vec()?;
57+
let resp =
58+
Response::bad_request_no_request("secure channel required").encode_body()?;
5959
c.send(m.return_route().clone(), resp).await?;
6060
return Ok(());
6161
}
6262
};
6363

6464
let from = Identifier::from(secure_channel_info.their_identifier());
6565
let return_route = m.return_route().clone();
66-
let body = m.into_body()?;
67-
let mut dec = Decoder::new(&body);
68-
let req: RequestHeader = dec.decode()?;
66+
let request = m.into_body()?;
67+
let header = request.header();
6968
trace! {
7069
target: "credential_issuer",
7170
from = %from,
72-
id = %req.id(),
73-
method = ?req.method(),
74-
path = %req.path(),
75-
body = %req.has_body(),
71+
id = %header.id(),
72+
method = ?header.method(),
73+
path = %header.path(),
74+
body = %header.has_body(),
7675
"request"
7776
}
78-
let res = match (req.method(), req.path()) {
77+
let res = match (header.method(), header.path()) {
7978
(Some(Method::Post), "/") | (Some(Method::Post), "/credential") => {
8079
match self.credential_issuer.issue_credential(&from).await {
81-
Ok(Some(crd)) => Response::ok().with_headers(&req).body(crd).to_vec()?,
82-
Ok(None) => Response::forbidden(&req, "unauthorized member").to_vec()?,
83-
Err(error) => Response::internal_error(&req, &error.to_string()).to_vec()?,
80+
Ok(Some(crd)) => Response::ok()
81+
.with_headers(header)
82+
.body(crd)
83+
.encode_body()?,
84+
Ok(None) => Response::forbidden(header, "unauthorized member").encode_body()?,
85+
Err(error) => {
86+
Response::internal_error(header, &error.to_string()).encode_body()?
87+
}
8488
}
8589
}
86-
_ => Response::unknown_path(&req).to_vec()?,
90+
_ => Response::unknown_path(header).encode_body()?,
8791
};
8892

8993
c.send(return_route, res).await

Diff for: implementations/rust/ockam/ockam_api/src/authenticator/direct/client.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use miette::IntoDiagnostic;
22
use std::collections::{BTreeMap, HashMap};
33

4+
use ockam::identity::models::IdentifierList;
45
use ockam::identity::AttributesEntry;
56
use ockam::identity::Identifier;
67
use ockam_core::api::Request;
78
use ockam_core::async_trait;
89
use ockam_node::Context;
910

10-
use crate::authenticator::direct::types::AddMember;
11+
use crate::authenticator::direct::types::{AddMember, MemberList};
1112
use crate::nodes::service::default_address::DefaultAddress;
1213
use crate::orchestrator::{AuthorityNodeClient, HasSecureClient};
1314

@@ -90,24 +91,28 @@ impl Members for AuthorityNodeClient {
9091

9192
async fn list_member_ids(&self, ctx: &Context) -> miette::Result<Vec<Identifier>> {
9293
let req = Request::get("/member_ids");
93-
self.get_secure_client()
94+
let identifiers: IdentifierList = self
95+
.get_secure_client()
9496
.ask(ctx, DefaultAddress::DIRECT_AUTHENTICATOR, req)
9597
.await
9698
.into_diagnostic()?
9799
.success()
98-
.into_diagnostic()
100+
.into_diagnostic()?;
101+
Ok(identifiers.0)
99102
}
100103

101104
async fn list_members(
102105
&self,
103106
ctx: &Context,
104107
) -> miette::Result<HashMap<Identifier, AttributesEntry>> {
105108
let req = Request::get("/");
106-
self.get_secure_client()
109+
let member_list: MemberList = self
110+
.get_secure_client()
107111
.ask(ctx, DefaultAddress::DIRECT_AUTHENTICATOR, req)
108112
.await
109113
.into_diagnostic()?
110114
.success()
111-
.into_diagnostic()
115+
.into_diagnostic()?;
116+
Ok(member_list.0)
112117
}
113118
}

0 commit comments

Comments
 (0)