Skip to content
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
89 changes: 78 additions & 11 deletions atomic-remote/src/http/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use reqwest::header::CONTENT_TYPE;
use reqwest::StatusCode;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::HttpRemote;
Expand All @@ -36,14 +36,81 @@ pub enum IdentityFetch {
Unsupported,
}

/// The server's structured error body on a `409` bind rejection. Both fields
/// are optional and the body itself may be empty or non-JSON, in which case
/// the caller simply gets no reason to quote.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct BindServerError {
/// The server's machine-readable reason (e.g. `IDENTITY_CONFLICT`).
#[serde(default)]
pub code: Option<String>,
/// The server's human-readable reason, quoted verbatim to the operator.
#[serde(default)]
pub message: Option<String>,
}

impl BindServerError {
/// Parse a `409` response body; `None` when it is not a usable JSON
/// object. The flat `{"code", "message"}` spelling is expected, and the
/// nested `{"error": {…}}` spelling is accepted so either server shape
/// surfaces its reason.
fn from_body(body: &str) -> Option<Self> {
let parsed: Value = serde_json::from_str(body).ok()?;
let (code, message) = match parsed.get("error") {
Some(nested) => (
nested.get("code").and_then(|v| v.as_str()),
nested.get("message").and_then(|v| v.as_str()),
),
None => (
parsed.get("code").and_then(|v| v.as_str()),
parsed.get("message").and_then(|v| v.as_str()),
),
};
Some(Self {
code: code.map(str::to_owned),
message: message.map(str::to_owned),
})
}
}

/// What the follow-up read of the remote binding resolved to after a `409`,
/// so callers can tell a genuine divergence from a server that disagrees with
/// its own rejection.
#[derive(Debug, Clone, PartialEq)]
pub enum ConflictRemote {
/// The follow-up GET resolved to a bound identity document.
Bound(Value),
/// The follow-up GET succeeded but the server reports the project unbound
/// (server disagrees with its own 409).
Unbound,
/// The follow-up GET failed or the document was unusable.
Unknown,
}

impl ConflictRemote {
fn from_fetch(fetched: RemoteResult<IdentityFetch>) -> Self {
match fetched {
Ok(IdentityFetch::Bound(identity)) => Self::Bound(identity),
Ok(IdentityFetch::Unbound) => Self::Unbound,
// A pre-transport ping or any transport trouble leaves the
// binding unreadable.
Ok(IdentityFetch::Unsupported) | Err(_) => Self::Unknown,
}
}
}

/// The result of offering the local identity to a remote.
#[derive(Debug, Clone)]
pub enum BindOutcome {
/// Bound now, or already identically bound.
Bound,
/// A *different* identity is already bound; carries the remote's document
/// when it could be read.
Conflict { remote: Option<Value> },
/// A *different* identity is already bound; carries what the follow-up
/// read of the remote binding resolved to and the server's error body
/// when it sent one.
Conflict {
remote: ConflictRemote,
server: Option<BindServerError>,
},
/// The remote predates the identity transport; nothing to do.
Unsupported,
}
Expand Down Expand Up @@ -114,13 +181,13 @@ impl HttpRemote {
match response.status() {
StatusCode::OK | StatusCode::CREATED => Ok(BindOutcome::Bound),
StatusCode::CONFLICT => {
// Read the binding the server already holds so the caller can
// name the divergence; failure to read it is not fatal here.
let remote = match self.fetch_project_identity().await {
Ok(IdentityFetch::Bound(identity)) => Some(identity),
_ => None,
};
Ok(BindOutcome::Conflict { remote })
// Read the rejection body (the server's reason) and the
// binding the server already holds so the caller can name the
// divergence; neither read is fatal here.
let body = response.text().await.unwrap_or_default();
let server = BindServerError::from_body(&body);
let remote = ConflictRemote::from_fetch(self.fetch_project_identity().await);
Ok(BindOutcome::Conflict { remote, server })
}
StatusCode::NOT_FOUND
| StatusCode::METHOD_NOT_ALLOWED
Expand Down
92 changes: 89 additions & 3 deletions atomic-remote/tests/project_identity_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
//! Covers the four behaviors the daemon and CLI depend on: reading a bound
//! identity, distinguishing unbound from unsupported servers, binding on the
//! first push, and mapping the server's 409 to a typed conflict that carries
//! the remote's document.
//! the server's error body and what the follow-up read of the binding
//! resolved to.

use std::io::{Read, Write};
use std::net::TcpListener;

use atomic_remote::http::identity::ConflictRemote;
use atomic_remote::{HttpRemote, HttpRemoteConfig};
use serde_json::json;

Expand Down Expand Up @@ -144,8 +146,92 @@ async fn a_conflict_reports_the_remote_binding() {
.await
.expect("bind")
{
atomic_remote::http::identity::BindOutcome::Conflict { remote } => {
assert_eq!(remote, Some(remote_document));
atomic_remote::http::identity::BindOutcome::Conflict { remote, server } => {
assert_eq!(remote, ConflictRemote::Bound(remote_document));
assert_eq!(
server,
Some(atomic_remote::http::identity::BindServerError {
code: Some("IDENTITY_CONFLICT".into()),
message: None,
}),
"the nested error body's code is surfaced"
);
}
other => panic!("expected Conflict, got {other:?}"),
}
}

#[tokio::test]
async fn a_conflict_body_surfaces_the_server_code_and_message() {
let document = identity_document(&"A".repeat(52));
let (url, _) = serve(vec![
(
409,
json!({"code": "IDENTITY_CONFLICT", "message": "project already bound"}).to_string(),
),
(
200,
json!({"bound": true, "hash": "cd".repeat(32), "identity": document}).to_string(),
),
]);
match remote(&url)
.bind_project_identity(&document)
.await
.expect("bind")
{
atomic_remote::http::identity::BindOutcome::Conflict { remote, server } => {
assert!(matches!(remote, ConflictRemote::Bound(_)));
assert_eq!(
server,
Some(atomic_remote::http::identity::BindServerError {
code: Some("IDENTITY_CONFLICT".into()),
message: Some("project already bound".into()),
}),
);
}
other => panic!("expected Conflict, got {other:?}"),
}
}

#[tokio::test]
async fn a_conflict_followed_by_an_unbound_read_maps_to_unbound() {
// The server contradicts its own 409: the follow-up read says the project
// is unbound (the atomic-storage#113 ref-scoping bug).
let document = identity_document(&"A".repeat(52));
let (url, _) = serve(vec![
(409, json!({"code": "IDENTITY_CONFLICT"}).to_string()),
(200, json!({"bound": false}).to_string()),
]);
match remote(&url)
.bind_project_identity(&document)
.await
.expect("bind")
{
atomic_remote::http::identity::BindOutcome::Conflict { remote, server } => {
assert_eq!(remote, ConflictRemote::Unbound);
assert_eq!(
server.map(|s| s.code),
Some(Some("IDENTITY_CONFLICT".into())),
);
}
other => panic!("expected Conflict, got {other:?}"),
}
}

#[tokio::test]
async fn a_conflict_whose_follow_up_read_fails_maps_to_unknown() {
let document = identity_document(&"A".repeat(52));
let (url, _) = serve(vec![
(409, json!({"code": "IDENTITY_CONFLICT"}).to_string()),
(500, "storage error".to_string()),
]);
match remote(&url)
.bind_project_identity(&document)
.await
.expect("bind")
{
atomic_remote::http::identity::BindOutcome::Conflict { remote, .. } => {
assert_eq!(remote, ConflictRemote::Unknown);
}
other => panic!("expected Conflict, got {other:?}"),
}
Expand Down
Loading
Loading