Skip to content
Open
54 changes: 52 additions & 2 deletions apps/craft-cloud-agent/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ use std::time::Duration;
use async_trait::async_trait;
use bytes::{Bytes, BytesMut};
use craft_cloud_proto::api::{
CommitRequest, CommitResponse, DeviceInfo, ErrorBody, InfoResponse, JoinRequest, LoginRequest, LoginResponse, MAX_MISSING_IDS, MissingRequest,
MissingResponse, PROTOCOL_MAX, PROTOCOL_MIN, PairingCreated, PairingStatus, SpaceResponse, codes,
CommitRequest, CommitResponse, DeviceInfo, ErrorBody, HistoryResponse, InfoResponse, JoinRequest, LoginRequest, LoginResponse, MAX_MISSING_IDS,
MissingRequest, MissingResponse, PROTOCOL_MAX, PROTOCOL_MIN, PairingCreated, PairingStatus, RestoreRequest, RestoreResponse, SpaceResponse,
codes,
};
use craft_cloud_proto::limits::{MAX_BATCH_OPS, MAX_CHUNK_BYTES, MAX_MANIFEST_BYTES, MAX_PAGE_NODES};
use craft_cloud_proto::local::MAX_MACHINE_BYTES;
Expand Down Expand Up @@ -582,6 +583,28 @@ pub struct HttpTransport<C = Reqwest> {
auth: HeaderValue,
}

/// A history or restore refusal, retaining the server's HTTP status and stable error code.
#[derive(Debug)]
pub enum HistoryCallError {
Transport(TransportError),
Refused { status: StatusCode, body: ErrorBody },
}

async fn history_answer<T: DeserializeOwned>(answer: Answer) -> Result<T, HistoryCallError> {
if !answer.status.is_success() {
let status = answer.status;
let body = body_capped(answer, MAX_SMALL_JSON_BYTES)
.await
.ok()
.and_then(|bytes| serde_json::from_slice::<ErrorBody>(&bytes).ok())
.unwrap_or_else(|| ErrorBody { error: codes::INTERNAL.into(), message: status.to_string(), ..ErrorBody::default() });
return Err(HistoryCallError::Refused { status, body });
}
let bytes = body_capped(answer, MAX_SMALL_JSON_BYTES).await.map_err(HistoryCallError::Transport)?;
serde_json::from_slice(&bytes)
.map_err(|e| HistoryCallError::Transport(TransportError::Protocol(format!("the server's answer doesn't parse: {e}"))))
}

impl HttpTransport {
pub fn new(server: ServerUrl, token: &DeviceToken) -> Result<Self, TransportError> {
Self::over(Reqwest::new(&server)?, server, token)
Expand All @@ -598,6 +621,33 @@ impl<C: Connection> HttpTransport<C> {
&self.server
}

/// The server's newest-first history for one file node.
pub async fn file_history(&self, node: NodeId, cursor: Option<&str>, limit: Option<u32>) -> Result<HistoryResponse, HistoryCallError> {
let mut url = self.server.endpoint(&format!("nodes/{node}/history"));
{
let mut query = url.query_pairs_mut();
if let Some(cursor) = cursor {
query.append_pair("cursor", cursor);
}
if let Some(limit) = limit {
query.append_pair("limit", &limit.to_string());
}
}
let req = request(Method::GET, url, Some(&self.auth), MAX_SMALL_JSON_BYTES);
let answer = self.send(req).await.map_err(HistoryCallError::Transport)?;
history_answer(answer).await
}

/// Makes a prior version the file's content as a new version.
pub async fn restore_file(&self, node: NodeId, version: VersionId, expected_version: VersionId) -> Result<RestoreResponse, HistoryCallError> {
let req = self
.request(Method::POST, &format!("nodes/{node}/restore"), MAX_SMALL_JSON_BYTES)
.json(&RestoreRequest { version, expected_version })
.map_err(HistoryCallError::Transport)?;
let answer = self.send(req).await.map_err(HistoryCallError::Transport)?;
history_answer(answer).await
}

fn request(&self, method: Method, path: &str, max_body: usize) -> Request {
request(method, self.server.endpoint(path), Some(&self.auth), max_body)
}
Expand Down
81 changes: 76 additions & 5 deletions apps/craft-cloud-agent/src/local_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ use std::path::PathBuf;
use std::sync::{Arc, Mutex, PoisonError};

use axum::extract::rejection::{JsonRejection, PathRejection};
use axum::extract::{DefaultBodyLimit, Path, Request, State};
use axum::extract::{DefaultBodyLimit, Path, Query, Request, State};
use axum::http::StatusCode;
use axum::http::header::{AUTHORIZATION, HOST, ORIGIN};
use axum::middleware::{self, Next};
use axum::response::{IntoResponse, Response};
use axum::routing::{delete, get, post};
use axum::{Json, Router};
use craft_cloud_proto::DeviceId;
use craft_cloud_proto::api::{DeviceInfo, ErrorBody, PairingStatus, codes as api_codes};
use craft_cloud_proto::api::{DeviceInfo, ErrorBody, HistoryResponse, PairingStatus, RestoreResponse, codes as api_codes};
use craft_cloud_proto::join::{Code, JoinString, MAX_JOIN_BYTES, PAIRING_CHARS};
use craft_cloud_proto::local::{
AutostartRequest, JoinRequest, LOCAL_API_FILE, LOCAL_API_MAX, LOCAL_API_SCHEMA, LocalApiFile, LoginRequest, LogoutRequest, MAX_LOCAL_JSON_BYTES,
Expand Down Expand Up @@ -195,19 +195,23 @@ impl Control {
#[derive(Debug)]
pub struct LocalError {
pub status: StatusCode,
pub code: &'static str,
pub code: String,
pub message: String,
}

impl LocalError {
pub fn new(status: StatusCode, code: &'static str, message: impl Into<String>) -> Self {
Self { status, code: code.to_owned(), message: message.into() }
}

fn with_code(status: StatusCode, code: String, message: impl Into<String>) -> Self {
Self { status, code, message: message.into() }
}
}

impl IntoResponse for LocalError {
fn into_response(self) -> Response {
(self.status, Json(ErrorBody { error: self.code.to_owned(), message: self.message, ..ErrorBody::default() })).into_response()
(self.status, Json(ErrorBody { error: self.code, message: self.message, ..ErrorBody::default() })).into_response()
}
}

Expand All @@ -217,7 +221,7 @@ impl From<AgentError> for LocalError {
// A setup that failed after the code was spent answers as what failed, with the whole message.
if let AgentError::JoinSetup(inner) = e {
let inner = LocalError::from(*inner);
return Self::new(inner.status, inner.code, message);
return Self::with_code(inner.status, inner.code, message);
}
let (status, code) = match &e {
AgentError::WrongPassword => (StatusCode::UNAUTHORIZED, codes::WRONG_PASSWORD),
Expand Down Expand Up @@ -355,6 +359,8 @@ fn router(state: Api) -> Router {
.route(paths::PAUSE, post(pause))
.route(paths::RESUME, post(resume))
.route(paths::QUIT, post(quit))
.route(paths::FILE_HISTORY, get(file_history))
.route(paths::FILE_RESTORE, post(file_restore))
.route(paths::BLOBS, axum::routing::put(put_blob).layer(DefaultBodyLimit::max(MAX_BLOB_BYTES)))
.route(&format!("{}/{{hash}}", paths::BLOBS), get(get_blob))
.fallback(|| async { LocalError::new(StatusCode::NOT_FOUND, api_codes::NOT_FOUND, "no such call") })
Expand All @@ -363,6 +369,71 @@ fn router(state: Api) -> Router {
.with_state(state)
}

#[derive(serde::Deserialize)]
struct HistoryQuery {
path: String,
cursor: Option<String>,
limit: Option<u32>,
}

fn file_node(state: &ApiState, path: &str) -> Result<craft_cloud_proto::NodeId, LocalError> {
if path.is_empty() || path.starts_with('/') || path.contains('\\') || path.contains(':') || path.as_bytes().contains(&0) {
return Err(LocalError::new(StatusCode::BAD_REQUEST, api_codes::INVALID, "path must be relative to the sync root"));
}
let names = path
.split('/')
.map(craft_cloud_proto::Name::new)
.collect::<Result<Vec<_>, _>>()
.map_err(|_| LocalError::new(StatusCode::BAD_REQUEST, api_codes::INVALID, "path contains an invalid name"))?;
let saved =
DataDir::new(&state.data).load_state().map_err(|e| LocalError::new(StatusCode::INTERNAL_SERVER_ERROR, api_codes::INTERNAL, e.to_string()))?;
let rel = craft_cloud_sync::RelPath::from_names(names);
let node =
saved.state.base_at(&rel).ok_or_else(|| LocalError::new(StatusCode::NOT_FOUND, api_codes::NOT_FOUND, "no synced file at that path"))?;
if !matches!(saved.state.base.get(&node).map(|entry| &entry.kind), Some(craft_cloud_sync::BaseKind::File { .. })) {
return Err(LocalError::new(StatusCode::BAD_REQUEST, api_codes::INVALID, "history is only available for files"));
}
Ok(node)
}

fn history_error(error: crate::http::HistoryCallError) -> LocalError {
match error {
crate::http::HistoryCallError::Refused { status: StatusCode::UNAUTHORIZED, body } => {
LocalError::new(StatusCode::CONFLICT, codes::NEEDS_YOU, body.message)
}
crate::http::HistoryCallError::Refused { status, body } => LocalError::with_code(status, body.error, body.message),
crate::http::HistoryCallError::Transport(craft_cloud_sync::TransportError::NotFound) => {
LocalError::new(StatusCode::NOT_FOUND, api_codes::NOT_FOUND, "no such file or version")
}
crate::http::HistoryCallError::Transport(craft_cloud_sync::TransportError::Unauthorized) => {
LocalError::new(StatusCode::CONFLICT, codes::NEEDS_YOU, "sign in again to continue")
}
crate::http::HistoryCallError::Transport(error) => AgentError::from(error).into(),
}
}

async fn file_history(
State(state): State<Api>,
query: Result<Query<HistoryQuery>, axum::extract::rejection::QueryRejection>,
) -> Result<Json<HistoryResponse>, LocalError> {
let Query(query) = query.map_err(|e| LocalError::new(StatusCode::BAD_REQUEST, api_codes::INVALID, e.body_text()))?;
let node = file_node(&state, &query.path)?;
let (_, transport) = server_api(&state)?;
Ok(Json(transport.file_history(node, query.cursor.as_deref(), query.limit).await.map_err(history_error)?))
}

async fn file_restore(
State(state): State<Api>,
body: Result<Json<craft_cloud_proto::local::RestoreRequest>, JsonRejection>,
) -> Result<Json<RestoreResponse>, LocalError> {
let Json(request) = body?;
let node = file_node(&state, &request.path)?;
let (_, transport) = server_api(&state)?;
let answer = transport.restore_file(node, request.version, request.expected_version).await.map_err(history_error)?;
state.control.request_pass();
Ok(Json(answer))
}

/// Equal strings, compared in time that doesn't depend on where they differ.
fn same_secret(a: &str, b: &str) -> bool {
a.len() == b.len() && a.bytes().zip(b.bytes()).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0
Expand Down
96 changes: 95 additions & 1 deletion apps/craft-cloud-agent/tests/local_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use craft_cloud_agent::cli::{self, AgentError, Exit};
use craft_cloud_agent::daemon::{RunOptions, run_loop};
use craft_cloud_agent::schedule::Timing;
use craft_cloud_meta::{Meta, Role};
use craft_cloud_proto::api::{DeviceInfo, ErrorBody, PairingState, PairingStatus};
use craft_cloud_proto::api::{DeviceInfo, ErrorBody, HistoryResponse, PairingState, PairingStatus};
use craft_cloud_proto::local::{JoinRequest, LOCAL_API_FILE, LocalApiFile, LoginRequest, PairingInfo, RunState, Status, codes};
use craft_cloud_server::AppState;
use craft_cloud_server::auth::hash_password;
Expand Down Expand Up @@ -173,6 +173,8 @@ const ROUTES: &[(&str, &str)] = &[
("DELETE", "/v1/pairings/0192f0c4-0000-7000-8000-000000000000"),
("GET", "/v1/devices"),
("DELETE", "/v1/devices/0192f0c4-0000-7000-8000-000000000000"),
("GET", "/v1/files/history?path=Notes%2Ftodo.txt"),
("POST", "/v1/files/restore"),
("POST", "/v1/autostart"),
];

Expand Down Expand Up @@ -530,6 +532,98 @@ async fn reaches_the_server(server: &Server, name: &str) {
}
}

async fn history_page(api: &LocalApiFile, path: &str, cursor: Option<&str>, limit: u32) -> HistoryResponse {
let mut query = format!("path={path}&limit={limit}");
if let Some(cursor) = cursor {
query.push_str("&cursor=");
query.push_str(cursor);
}
let (code, body) = call(api, "GET", &format!("/v1/files/history?{query}"), "");
assert_eq!(code, 200, "{body}");
serde_json::from_str(&body).unwrap()
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn local_file_history_pages_restores_and_syncs_to_disk() {
let server = server().await;
let dir = tempfile::tempdir().unwrap();
let data = DataDir::new(dir.path().join("data"));
let folder = dir.path().join("Craft Cloud");
let (running, api) = signed_in(&server, &data, &folder).await;
let subdir = folder.join("Notes");
std::fs::create_dir(&subdir).unwrap();
let file = subdir.join("story.txt");

let mut previous = None;
for content in [b"first version".as_slice(), b"second version".as_slice(), b"third version".as_slice()] {
std::fs::write(&file, content).unwrap();
let deadline = Instant::now() + PATIENCE;
loop {
let (code, body) = call(&api, "GET", "/v1/files/history?path=Notes%2Fstory.txt&limit=1", "");
if code == 200 {
let page: HistoryResponse = serde_json::from_str(&body).unwrap();
if page.versions.first().is_some_and(|version| Some(version.id) != previous) {
previous = page.versions.first().map(|version| version.id);
break;
}
}
assert!(Instant::now() < deadline, "version reaches server history");
tokio::time::sleep(Duration::from_millis(50)).await;
}
}

let newest = history_page(&api, "Notes%2Fstory.txt", None, 1).await;
let cursor = newest.next.clone().expect("more than one history page");
let older = history_page(&api, "Notes%2Fstory.txt", Some(&cursor), 1).await;
let current = newest.versions.first().expect("current version").id;
let restore = older.versions.first().expect("older version").id;
let body = serde_json::json!({ "path": "Notes/story.txt", "version": restore, "expected_version": current }).to_string();
let (code, answer) = call(&api, "POST", "/v1/files/restore", &body);
assert_eq!(code, 200, "{answer}");
let restored: craft_cloud_proto::api::RestoreResponse = serde_json::from_str(&answer).unwrap();
assert_eq!(restored.parent_version, current);

let stale = serde_json::json!({ "path": "Notes/story.txt", "version": current, "expected_version": current }).to_string();
let (code, answer) = call(&api, "POST", "/v1/files/restore", &stale);
assert_eq!((code, error_code(&answer).as_str()), (409, "conflict"), "{answer}");

let deadline = Instant::now() + PATIENCE;
loop {
if std::fs::read(&file).is_ok_and(|bytes| bytes.as_slice() == b"second version") {
break;
}
assert!(Instant::now() < deadline, "the change feed syncs restored bytes to disk");
tokio::time::sleep(Duration::from_millis(50)).await;
}
let (exit, output) = running.stop().await;
assert_eq!(exit.unwrap(), Exit::Done, "{output}");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn local_file_history_rejects_unknown_outside_and_directory_paths() {
let server = server().await;
let dir = tempfile::tempdir().unwrap();
let data = DataDir::new(dir.path().join("data"));
let folder = dir.path().join("Craft Cloud");
let (running, api) = signed_in(&server, &data, &folder).await;
std::fs::create_dir(folder.join("Folder")).unwrap();
let deadline = Instant::now() + PATIENCE;
loop {
let (code, body) = call(&api, "GET", "/v1/files/history?path=Folder", "");
if code == 400 {
assert_eq!(error_code(&body), "invalid");
break;
}
assert!(Instant::now() < deadline, "directory enters sync state");
tokio::time::sleep(Duration::from_millis(50)).await;
}
let (code, body) = call(&api, "GET", "/v1/files/history?path=missing.txt", "");
assert_eq!((code, error_code(&body).as_str()), (404, "not_found"), "{body}");
let (code, body) = call(&api, "GET", "/v1/files/history?path=..%2Foutside.txt", "");
assert_eq!((code, error_code(&body).as_str()), (400, "invalid"), "{body}");
let _ = running.stop().await;
}

/// Paused, nothing syncs and the status says so (not as a pause that needs someone); it stays paused after `run`
/// starts again, until it is resumed. Quit then ends `run` cleanly, and its `local-api.json` with it.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
Expand Down
22 changes: 21 additions & 1 deletion apps/craft-cloud-desktop/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use std::sync::mpsc::{Receiver, Sender, channel};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;

use craft_cloud_app::{Client, DeviceId, DeviceInfo, Error, JoinRequest, PairingInfo, PairingStatus, Status, Uuid};
use craft_cloud_app::{
Client, DeviceId, DeviceInfo, Error, HistoryResponse, JoinRequest, PairingInfo, PairingStatus, RestoreResponse, Status, Uuid, VersionId,
};

use crate::model::{Effect, Event};

Expand All @@ -33,6 +35,8 @@ pub trait Agent: Send + Sync {
fn resume(&self) -> Result<Status, Error>;
fn quit(&self) -> Result<Status, Error>;
fn set_autostart(&self, on: bool) -> Result<Status, Error>;
fn file_history(&self, path: &str, cursor: Option<&str>) -> Result<HistoryResponse, Error>;
fn restore_file(&self, path: &str, version: VersionId, expected_version: VersionId) -> Result<RestoreResponse, Error>;
}

/// The agent through `craft_cloud_app::Client`. It finds the agent again whenever a call finds it gone (the agent
Expand Down Expand Up @@ -113,6 +117,12 @@ impl Agent for ClientAgent {
fn set_autostart(&self, on: bool) -> Result<Status, Error> {
self.with(|c| c.set_autostart(on))
}
fn file_history(&self, path: &str, cursor: Option<&str>) -> Result<HistoryResponse, Error> {
self.with(|c| c.file_history(path, cursor, Some(50)))
}
fn restore_file(&self, path: &str, version: VersionId, expected_version: VersionId) -> Result<RestoreResponse, Error> {
self.with(|c| c.restore_file(path, version, expected_version))
}
}

/// Starts the agent and waits for it ([`crate::start_agent::start_and_wait`]), for [`Effect::StartAgent`].
Expand Down Expand Up @@ -199,6 +209,10 @@ pub fn call(agent: &dyn Agent, start: &Starter, effect: Effect) -> Option<Event>
}
Effect::Devices => Event::Devices(agent.devices()),
Effect::RemoveDevice(id) => Event::Devices(agent.remove_device(id)),
Effect::History { path, cursor, ticket } => Event::History { ticket, result: agent.file_history(&path, cursor.as_deref()) },
Effect::Restore { path, version, expected_version, ticket } => {
Event::Restored { ticket, result: agent.restore_file(&path, version, expected_version) }
}
Effect::Logout { local } => Event::SignedOut(agent.logout(local)),
Effect::Pause => Event::Answered(agent.pause()),
Effect::Resume => Event::Answered(agent.resume()),
Expand Down Expand Up @@ -287,6 +301,12 @@ pub(crate) mod fake {
fn set_autostart(&self, on: bool) -> Result<Status, Error> {
self.answer("set_autostart", |s| Status { autostart: Some(on), ..s.clone() })
}
fn file_history(&self, _path: &str, _cursor: Option<&str>) -> Result<HistoryResponse, Error> {
self.answer("file_history", |_| HistoryResponse { versions: Vec::new(), next: None })
}
fn restore_file(&self, _path: &str, _version: VersionId, _expected_version: VersionId) -> Result<RestoreResponse, Error> {
Err(Error::Refused { code: "invalid".into(), message: "not configured".into() })
}
}
}

Expand Down
Loading