Skip to content

Commit

Permalink
rpc: Segregate Services-specific requests and response enum variants
Browse files Browse the repository at this point in the history
We are going to add new variants for the history server, and it's probably cleaner
to keep them sorted this way.

Note that it changes the wire format.

Actual changes are in `sable_network/src/rpc/network_message.rs`,
everything else is just churn.
  • Loading branch information
progval authored and spb committed Oct 20, 2024
1 parent a42dd09 commit 3c6bc2f
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 115 deletions.
11 changes: 8 additions & 3 deletions sable_ircd/src/command/handlers/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,18 @@ async fn do_register_user(
}

let message =
rpc::RemoteServerRequestType::RegisterUser(requested_account, password.to_owned());
rpc::RemoteServicesServerRequestType::RegisterUser(requested_account, password.to_owned())
.into();

match server
.node()
.sync_log()
.send_remote_request(services_name, message)
.await
{
Ok(rpc::RemoteServerResponse::LogUserIn(account)) => {
Ok(rpc::RemoteServerResponse::Services(rpc::RemoteServicesServerResponse::LogUserIn(
account,
))) => {
server.add_action(CommandAction::state_change(
source.id(),
event::UserLogin {
Expand All @@ -94,7 +97,9 @@ async fn do_register_user(
"You have successfully registered",
));
}
Ok(rpc::RemoteServerResponse::AlreadyExists) => {
Ok(rpc::RemoteServerResponse::Services(
rpc::RemoteServicesServerResponse::AlreadyExists,
)) => {
response_to.send(message::Fail::new(
"REGISTER",
"ACCOUNT_EXISTS",
Expand Down
16 changes: 9 additions & 7 deletions sable_ircd/src/command/handlers/services/cs/access.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use sable_network::{
network::state::ChannelRoleName,
policy::RegistrationPolicyService,
rpc::{RemoteServerRequestType, RemoteServerResponse},
rpc::{RemoteServerResponse, RemoteServicesServerRequestType, RemoteServicesServerResponse},
};

use super::*;
Expand Down Expand Up @@ -87,11 +87,12 @@ async fn access_modify(

let target_access_id = ChannelAccessId::new(source.account.id(), chan.id());

let request = RemoteServerRequestType::ModifyAccess {
let request = RemoteServicesServerRequestType::ModifyAccess {
source: source.account.id(),
id: target_access_id,
role: Some(new_role.id()),
};
}
.into();
let registration_response = cmd
.server()
.node()
Expand All @@ -104,7 +105,7 @@ async fn access_modify(
Ok(RemoteServerResponse::Success) => {
cmd.notice("Access successfully updated");
}
Ok(RemoteServerResponse::AccessDenied) => {
Ok(RemoteServerResponse::Services(RemoteServicesServerResponse::AccessDenied)) => {
cmd.notice("Access denied");
}
Ok(response) => {
Expand Down Expand Up @@ -145,19 +146,20 @@ async fn access_delete(
return Ok(());
};

let request = RemoteServerRequestType::ModifyAccess {
let request = RemoteServicesServerRequestType::ModifyAccess {
source: source.account.id(),
id: target_access.id(),
role: None,
};
}
.into();
let registration_response = services_target.send_remote_request(request).await;

tracing::debug!(?registration_response, "Got registration response");
match registration_response {
Ok(RemoteServerResponse::Success) => {
cmd.notice("Access successfully updated");
}
Ok(RemoteServerResponse::AccessDenied) => {
Ok(RemoteServerResponse::Services(RemoteServicesServerResponse::AccessDenied)) => {
cmd.notice("Access denied");
}
Ok(response) => {
Expand Down
9 changes: 6 additions & 3 deletions sable_ircd/src/command/handlers/services/cs/register.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use sable_network::rpc::{RemoteServerRequestType, RemoteServerResponse};
use sable_network::rpc::{
RemoteServerResponse, RemoteServicesServerRequestType, RemoteServicesServerResponse,
};

use super::*;

Expand All @@ -17,7 +19,8 @@ async fn handle_register(
return Ok(());
}

let request = RemoteServerRequestType::RegisterChannel(source.account.id(), channel.id());
let request =
RemoteServicesServerRequestType::RegisterChannel(source.account.id(), channel.id()).into();
let registration_response = services_target.send_remote_request(request).await;

tracing::debug!(?registration_response, "Got registration response");
Expand All @@ -28,7 +31,7 @@ async fn handle_register(
channel.name()
));
}
Ok(RemoteServerResponse::AlreadyExists) => {
Ok(RemoteServerResponse::Services(RemoteServicesServerResponse::AlreadyExists)) => {
cmd.notice(format_args!(
"Channel {} is already registered",
channel.name()
Expand Down
23 changes: 13 additions & 10 deletions sable_ircd/src/command/handlers/services/cs/role.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use sable_network::{
network::state::ChannelAccessFlag,
policy::RegistrationPolicyService,
rpc::{RemoteServerRequestType, RemoteServerResponse},
rpc::{RemoteServerResponse, RemoteServicesServerRequestType, RemoteServicesServerResponse},
};

use super::*;
Expand Down Expand Up @@ -98,19 +98,20 @@ async fn role_edit(
.policy()
.can_create_role(&source.account, &chan, &flags)?;

let request = RemoteServerRequestType::ModifyRole {
let request = RemoteServicesServerRequestType::ModifyRole {
source: source.account.id(),
id: target_role.id(),
flags: Some(flags),
};
}
.into();
let registration_response = services_target.send_remote_request(request).await;

tracing::debug!(?registration_response, "Got registration response");
match registration_response {
Ok(RemoteServerResponse::Success) => {
cmd.notice("Role successfully updated");
}
Ok(RemoteServerResponse::AccessDenied) => {
Ok(RemoteServerResponse::Services(RemoteServicesServerResponse::AccessDenied)) => {
cmd.notice("Access denied");
}
Ok(response) => {
Expand Down Expand Up @@ -150,20 +151,21 @@ async fn role_add(
.policy()
.can_create_role(&source.account, &chan, &flags)?;

let request = RemoteServerRequestType::CreateRole {
let request = RemoteServicesServerRequestType::CreateRole {
source: source.account.id(),
channel: chan.id(),
name: target_role_name,
flags,
};
}
.into();
let registration_response = services_target.send_remote_request(request).await;

tracing::debug!(?registration_response, "Got registration response");
match registration_response {
Ok(RemoteServerResponse::Success) => {
cmd.notice("Role successfully updated");
}
Ok(RemoteServerResponse::AccessDenied) => {
Ok(RemoteServerResponse::Services(RemoteServicesServerResponse::AccessDenied)) => {
cmd.notice("Access denied");
}
Ok(response) => {
Expand Down Expand Up @@ -196,19 +198,20 @@ async fn role_delete(
.policy()
.can_edit_role(&source.account, &chan, &target_role)?;

let request = RemoteServerRequestType::ModifyRole {
let request = RemoteServicesServerRequestType::ModifyRole {
source: source.account.id(),
id: target_role.id(),
flags: None,
};
}
.into();
let registration_response = services_target.send_remote_request(request).await;

tracing::debug!(?registration_response, "Got registration response");
match registration_response {
Ok(RemoteServerResponse::Success) => {
cmd.notice("Role successfully updated");
}
Ok(RemoteServerResponse::AccessDenied) => {
Ok(RemoteServerResponse::Services(RemoteServicesServerResponse::AccessDenied)) => {
cmd.notice("Access denied");
}
Ok(response) => {
Expand Down
14 changes: 9 additions & 5 deletions sable_ircd/src/command/handlers/services/ns/cert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sable_network::rpc::{RemoteServerRequestType, RemoteServerResponse};
use sable_network::rpc::{RemoteServerResponse, RemoteServicesServerRequestType};

use super::*;

Expand Down Expand Up @@ -70,8 +70,11 @@ async fn cert_add(
return Ok(());
}

let req =
RemoteServerRequestType::AddAccountFingerprint(source.account.id(), fingerprint.to_owned());
let req = RemoteServicesServerRequestType::AddAccountFingerprint(
source.account.id(),
fingerprint.to_owned(),
)
.into();

match services.send_remote_request(req).await {
Ok(RemoteServerResponse::Success) => {
Expand Down Expand Up @@ -101,10 +104,11 @@ async fn cert_del(
) -> CommandResult {
let fingerprint = param.require()?;

let req = RemoteServerRequestType::RemoveAccountFingerprint(
let req = RemoteServicesServerRequestType::RemoveAccountFingerprint(
source.account.id(),
fingerprint.to_owned(),
);
)
.into();

match services.send_remote_request(req).await {
Ok(RemoteServerResponse::Success) => {
Expand Down
10 changes: 6 additions & 4 deletions sable_ircd/src/command/handlers/services/ns/login.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use sable_network::{network::event::UserLogin, rpc::RemoteServerResponse};
use sable_network::network::event::UserLogin;
use sable_network::rpc::{RemoteServerResponse, RemoteServicesServerResponse};

use super::*;

Expand All @@ -21,11 +22,12 @@ async fn handle_login(
};

let login_request =
rpc::RemoteServerRequestType::UserLogin(target_account.id(), password.to_string());
rpc::RemoteServicesServerRequestType::UserLogin(target_account.id(), password.to_string())
.into();
let login_result = services.send_remote_request(login_request).await;

match login_result {
Ok(RemoteServerResponse::LogUserIn(account)) => {
Ok(RemoteServerResponse::Services(RemoteServicesServerResponse::LogUserIn(account))) => {
if account == target_account.id() {
cmd.new_event(
source.id(),
Expand All @@ -44,7 +46,7 @@ async fn handle_login(
cmd.notice("Login failed (internal error)");
}
}
Ok(RemoteServerResponse::InvalidCredentials) => {
Ok(RemoteServerResponse::Services(RemoteServicesServerResponse::InvalidCredentials)) => {
let msg = format!("Invalid credentials for {}", target_account.name());
cmd.notice(msg);
}
Expand Down
12 changes: 6 additions & 6 deletions sable_ircd/src/command/handlers/services/sasl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use sable_network::{
network::ban::*,
rpc::{RemoteServerRequestType, RemoteServerResponse},
rpc::{RemoteServerResponse, RemoteServicesServerRequestType, RemoteServicesServerResponse},
};

use super::*;
Expand All @@ -19,14 +19,14 @@ async fn handle_authenticate(
let authenticate_request = if let Some(session) = source.sasl_session.get() {
// A session already exists, so the argument is "*" or base64-encoded session data
if text == "*" {
RemoteServerRequestType::AbortAuthenticate(*session)
RemoteServicesServerRequestType::AbortAuthenticate(*session)
} else {
let Ok(data) = BASE64_STANDARD.decode(text) else {
response.notice("Invalid base64");
return Ok(());
};

RemoteServerRequestType::Authenticate(*session, data)
RemoteServicesServerRequestType::Authenticate(*session, data)
}
} else {
// No session, so the argument is the mechanism name
Expand Down Expand Up @@ -54,15 +54,15 @@ async fn handle_authenticate(
let session = server.ids().next();
source.sasl_session.set(session).ok();

RemoteServerRequestType::BeginAuthenticate(session, mechanism)
RemoteServicesServerRequestType::BeginAuthenticate(session, mechanism)
};

match services
.require()?
.send_remote_request(authenticate_request)
.send_remote_request(authenticate_request.into())
.await
{
Ok(RemoteServerResponse::Authenticate(status)) => {
Ok(RemoteServerResponse::Services(RemoteServicesServerResponse::Authenticate(status))) => {
use rpc::AuthenticateStatus::*;

match status {
Expand Down
31 changes: 28 additions & 3 deletions sable_network/src/rpc/network_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,23 @@ pub struct RemoteServerRequest {
pub response: oneshot::Sender<RemoteServerResponse>,
}

/// A message to be handled by a services node
/// A message to be handled by a specific node
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum RemoteServerRequestType {
/// Simple ping for communication tests
Ping,
Services(RemoteServicesServerRequestType),
}

impl From<RemoteServicesServerRequestType> for RemoteServerRequestType {
fn from(req: RemoteServicesServerRequestType) -> Self {
RemoteServerRequestType::Services(req)
}
}

/// A message to be handled by a services node
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum RemoteServicesServerRequestType {
/// User attempting registration
/// Parameters: account name being registered, password provided
RegisterUser(Nickname, String),
Expand Down Expand Up @@ -97,6 +109,21 @@ pub enum RemoteServerResponse {
Success,
/// Operation not supported by this server
NotSupported,
/// Operation failed, with error message
Error(String),
/// Response type specific to services servers
Services(RemoteServicesServerResponse),
}

impl From<RemoteServicesServerResponse> for RemoteServerResponse {
fn from(resp: RemoteServicesServerResponse) -> Self {
RemoteServerResponse::Services(resp)
}
}

/// Remote services server response type
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum RemoteServicesServerResponse {
/// Operation succeeded, user should be logged in to account
LogUserIn(AccountId),
/// SASL response
Expand All @@ -111,6 +138,4 @@ pub enum RemoteServerResponse {
NoAccount,
/// Channel isn't registered
ChannelNotRegistered,
/// Operation failed, with error message
Error(String),
}
Loading

0 comments on commit 3c6bc2f

Please sign in to comment.