1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
//! HTTP API: router, OpenAPI document and shared response helpers.
pub(crate) mod auth;
mod debug;
mod document;
mod log;
mod peers;
mod stats;
mod store;
use axum::{
Json, Router,
extract::State,
http::{
HeaderValue, StatusCode,
header::{CONTENT_TYPE, ETAG},
},
response::{IntoResponse, Response},
routing::{delete, get, put},
};
use serde_json::Value as JsonValue;
use sha2::{Digest, Sha256};
use tracing::{error, warn};
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;
use crate::api::auth::{AuthState, AuthenticatedUser, basic_auth};
use crate::document::store::StoreError;
use crate::state::AppState;
/// Builds the complete HTTP router. Everything except Swagger is authenticated.
pub(crate) fn router(state: AppState, auth_state: AuthState, debug_api: bool) -> Router {
let mut api: Router<AppState> = Router::new()
.route("/store", get(store::get_store_root))
.route(
"/store/{*key}",
get(store::get_store_entry)
.put(store::put_store_entry)
.delete(store::delete_store_entry),
)
.route("/stream/{*key}", get(store::stream_store_entry))
.route("/peers", get(peers::list_peers))
.route("/peers/{peer}", delete(peers::delete_peer))
.route(
"/admin/log",
get(log::get_log_filter).put(log::put_log_filter),
)
.route("/admin/stats", get(stats::get_stats))
.route("/admin/document", get(document::get_document))
.route("/admin/config", get(get_config))
.merge(crate::remake::router::<AppState>());
if debug_api {
warn!("Debug API enabled");
api = api
.route(
"/debug/firewall",
get(debug::get_debug_firewall).put(debug::put_debug_firewall),
)
.route("/debug/blacklist", put(debug::put_debug_blacklist));
}
api.layer(axum::middleware::from_fn_with_state(auth_state, basic_auth))
.merge(SwaggerUi::new("/swagger-ui").url("/openapi.json", ApiDoc::openapi()))
.with_state(state)
}
/// `GET /admin/config`: the effective configuration with each value's source;
/// imported values are left out.
async fn get_config(State(state): State<AppState>) -> Json<JsonValue> {
Json((*state.config).clone())
}
// --- shared helpers ----------------------------------------------------------
pub(crate) fn store_status(error: StoreError, context: &str) -> StatusCode {
match error {
StoreError::Conflict => StatusCode::CONFLICT,
StoreError::NotFound => StatusCode::NOT_FOUND,
other => {
error!(error = %format!("{other:#}"), "{context}");
StatusCode::INTERNAL_SERVER_ERROR
}
}
}
fn etag_for(bytes: &[u8]) -> String {
let digest = Sha256::digest(bytes);
let hex = digest
.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<String>();
format!("\"{hex}\"")
}
fn parse_key_path(key: &str) -> Result<Vec<String>, StatusCode> {
let segments: Vec<String> = key.split('/').map(str::to_owned).collect();
if segments.is_empty() || segments.iter().any(|segment| segment.is_empty()) {
return Err(StatusCode::BAD_REQUEST);
}
Ok(segments)
}
/// A path for a write. Protocol data (`_continuum`: remake records, members, forget
/// records, epoch metadata) is what every node acts on, so a write there acts on the
/// whole swarm: only users listed in `--protocol-writers` may, and each such write is
/// logged. Epoch metadata (and `_continuum` as a whole, which contains it) is never
/// writable: the protocol reads it at the snapshot change, so an edit only misleads
/// whoever reads it later. `403` otherwise.
fn parse_write_path(
state: &AppState,
user: &AuthenticatedUser,
key: &str,
method: &str,
) -> Result<Vec<String>, StatusCode> {
let path = parse_key_path(key)?;
if path.first().map(String::as_str) != Some(crate::remake::snapshot::META_ROOT) {
return Ok(path);
}
// `_continuum` itself, epoch metadata and the swarm ID: nobody.
let fixed = path.len() < 2
|| path[1] == crate::remake::snapshot::EPOCHS
|| path[1] == crate::remake::snapshot::SWARM;
if fixed || !state.protocol_writers.contains(&user.0) {
warn!(user = %user.0, method, path = %key, "Refused a write to protocol data");
return Err(StatusCode::FORBIDDEN);
}
warn!(user = %user.0, method, path = %key, "Protocol data written through the store API; this acts on the whole swarm");
Ok(path)
}
fn response_for_value(
value: &JsonValue,
accept: Option<&HeaderValue>,
) -> Result<Response, StatusCode> {
let wants_text = accept
.and_then(|value| value.to_str().ok())
.is_some_and(|value| {
value
.split(',')
.any(|item| item.trim().starts_with("text/plain"))
});
let (content_type, body) = if wants_text {
let body = match value {
JsonValue::String(value) => value.clone(),
other => serde_json::to_string(other).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
};
("text/plain; charset=utf-8", body)
} else {
(
"application/json",
serde_json::to_string(value).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
)
};
let etag = etag_for(body.as_bytes());
let mut response = body.into_response();
response
.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static(content_type));
response.headers_mut().insert(
ETAG,
HeaderValue::from_str(&etag).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
);
Ok(response)
}
// --- openapi spec ------------------------------------------------------------
// TODO spec is mostly out of sync
#[derive(OpenApi)]
#[openapi(
paths(
store::get_store_root,
store::get_store_entry,
store::put_store_entry,
store::delete_store_entry,
peers::list_peers,
peers::delete_peer,
),
components(
schemas(
peers::PeerStatus,
peers::PeerListResponse,
peers::PeerLivenessStatus,
peers::EpochInfo,
crate::transport::traffic::PeerTraffic,
crate::transport::traffic::ByKind,
crate::transport::traffic::Counter,
crate::state::Incidents,
peers::ThroughputInfo,
)
),
tags(
(name = "Store", description = "Key-value store operations"),
(name = "Peers", description = "Peer discovery and retirement")
),
info(
title = "Automerge Replication API",
version = "1.0.0",
description = "HTTP API for a replicated Automerge key-value store"
)
)]
struct ApiDoc;
|