8000 refactor(minimald): move minimald RPC types to own crate by twitchyliquid64 · Pull Request #420 · gominimal/minimal · GitHub
[go: up one dir, main page]

Skip to content
8000
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [
"crates/minimal",
"crates/minimal2",
"crates/minimald",
"crates/minimald-rpc",
"crates/minvmd",
"crates/remote-client",
"crates/remote-proto",
Expand Down Expand Up @@ -134,6 +135,7 @@ checkouts = { version = "0.0.1", path = "crates/checkouts" }
common = { version = "0.0.1", path = "crates/common" }
decode = { version = "0.0.1", path = "crates/decode" }
mctx = { version = "0.0.1", path = "crates/mctx" }
minimald-rpc = { version = "0.0.1", path = "crates/minimald-rpc" }
mfile = { version = "0.0.1", path = "crates/mfile" }
op = { version = "0.0.1", path = "crates/op" }
ot = { version = "0.0.1", path = "crates/ot" }
Expand Down
11 changes: 11 additions & 0 deletions crates/minimald-rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "minimald-rpc"
version = "0.0.1"
edition.workspace = true
publish.workspace = true

[dependencies]
constcat.workspace = true
serde.workspace = true

sessions.workspace = true
166 changes: 166 additions & 0 deletions crates/minimald-rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
//! Wire contract for minimald's oneshot SSH RPCs.
//!
//! This crate holds the protocol surface shared between the minimald server
//! and its clients: the subsystem names, the request/response payload types,
//! and the [`OneshotSshRpc`] trait that pairs them. It deliberately carries no
//! transport or server dependencies (no `russh`, no `tokio`) so that clients
//! — including the test harness and cross-platform integration tests — encode
//! and decode requests through the very same types the server handles.
//!
//! The server-side serving glue lives in the `minimald` crate.

use serde::{Deserialize, Serialize, de::DeserializeOwned};
use sessions::SessionId;

pub const RPC_SUBSYSTEM_PREFIX: &str = "minimald-v1-";

/// Describes a minimal-specific RPC method sent over ssh.
///
/// Oneshot RPCs are not streaming. The trait pairs a subsystem name with its
/// request and response schemas; both the server (which decodes the request
/// and encodes the response) and clients (which do the reverse) implement
/// against this single contract.
pub trait OneshotSshRpc {
/// The subsystem name used to call for this RPC.
const NAME: &'static str;
/// The type schema of the request.
///
/// Bound on `Serialize` exists so that clients (including the test
/// harness) can encode requests through the same type the handler
/// decodes them with.
type Request<'a>: Deserialize<'a> + Serialize;
/// The type schema of the response.
///
/// Bound on `DeserializeOwned` exists for symmetry with `Request`:
/// clients decode the same type the handler emitted.
type Response: Serialize + DeserializeOwned;
}

/// A convinence wrapper to let a response type be able to carry an error.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
pub enum Errorable<S: std::fmt::Debug + PartialEq> {
Ok(S),
Err { error: String },
}

impl<S: std::fmt::Debug + PartialEq> Errorable<S> {
pub fn unwrap(self) -> S {
match self {
Self::Ok(s) => s,
Errorable::Err { error } => panic!("unwrap of error value: {error}"),
}
}

pub fn ok(self) -> Option<S> {
match self {
Self::Ok(s) => Some(s),
Errorable::Err { .. } => None,
}
}
pub fn err(self) -> Option<String> {
match self {
Self::Ok(_) => None,
Errorable::Err { error } => Some(error),
}
}
}

impl<T: std::fmt::Debug + PartialEq, E: ToString> From<Result<T, E>> for Errorable<T> {
fn from(value: Result<T, E>) -> Self {
match value {
Err(e) => Self::Err {
error: e.to_string(),
},
Ok(t) => Self::Ok(t),
}
}
}

/// An RPC to get the version of minimald.
pub struct GetVersion;

/// The response to the [`GetVersion`] RPC.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetVersionResponse {
pub version: String,
pub long_version: String,
pub stdlib_version: String,
}

impl OneshotSshRpc for GetVersion {
const NAME: &'static str = constcat::concat!(RPC_SUBSYSTEM_PREFIX, "GetVersion");
type Request<'a> = ();
type Response = GetVersionResponse;
}

/// An RPC to list sessions managed by this minimald.
pub struct ListSessions;

/// An entry in the ListSessions response.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ListSessionsEntry {
pub id: SessionId,
pub name: Option<String>,
}

/// The response to the [`ListSessions`] RPC.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListSessionsResponse {
pub sessions: Vec<ListSessionsEntry>,
}

impl OneshotSshRpc for ListSessions {
const NAME: &'static str = constcat::concat!(RPC_SUBSYSTEM_PREFIX, "ListSessions");
type Request<'a> = ();
type Response = ListSessionsResponse; 10000
}

/// An RPC to read the session record for a session corresponding to the request.
pub struct GetSessionRecord;

/// The request for a [`GetSessionRecord`] RPC.
///
/// Serialized examples:
///
/// * `{"name": "my-session"}`
/// * `{"id": "<some-uuid>"}`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GetSessionRecordRequest {
Name(String),
Id(SessionId),
}

/// The response for a [`GetSessionRecord`] RPC.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetSessionRecordResponse {
pub record: Option<sessions::Record>,
}

impl OneshotSshRpc for GetSessionRecord {
const NAME: &'static str = constcat::concat!(RPC_SUBSYSTEM_PREFIX, "GetSessionRecord");
type Request<'a> = GetSessionRecordRequest;
type Response = GetSessionRecordResponse;
}

/// An RPC to create a new session based on the given record.
pub struct CreateSession;

/// The request for a [`CreateSession`] RPC.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateSessionRequest {
pub record: sessions::Record,
}

/// The response for a [`CreateSession`] RPC.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CreateSessionResponse {
pub id: SessionId,
}

impl OneshotSshRpc for CreateSession {
const NAME: &'static str = constcat::concat!(RPC_SUBSYSTEM_PREFIX, "CreateSession");
type Request<'a> = CreateSessionRequest;
type Response = Errorable<CreateSessionResponse>;
}
1 change: 1 addition & 0 deletions crates/minimald/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ args.workspace = true
graph.workspace = true
mctx.workspace = true
mfile.workspace = true
minimald-rpc.workspace = true
ot.workspace = true
paths.workspace = true
sessions.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/minimald/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ impl russh::server::Handler for ConnectionHandler {
) -> Result<(), Self::Error> {
protocol_trace!("Got subsystem_request on channel {id}: subsystem={name}");

if name.starts_with(rpc::RPC_SUBSYSTEM_PREFIX) {
if name.starts_with(minimald_rpc::RPC_SUBSYSTEM_PREFIX) {
let c = self.0.clone();
let s = c.0.lock().await.serv.clone();
rpc::handle_ssh_rpc(s, c, name, id, session).await?;
Expand Down
3 changes: 2 additions & 1 deletion crates/minimald/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,8 +1205,9 @@ mod tests {
use paths::HostAbsPath;
use sessions::SessionId;

use minimald_rpc::{CreateSession, CreateSessionRequest};

use crate::MINIMAL_SESSION_ID_ENV;
use crate::rpc::{CreateSession, CreateSessionRequest};
use crate::sessions::SessionKeyPredicate;
use crate::test_harness::{TestClient, TestServer};

Expand Down
Loading
Loading
0