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
|
//! HTTP API: router, the API description and shared response helpers (the store's
//! own are in `store`).
pub(crate) mod auth;
mod config;
#[cfg(feature = "dashboard")]
mod dashboard;
mod debug;
mod document;
mod health;
mod log;
mod peers;
mod stats;
mod store;
use axum::{
Json, Router,
body::Body,
extract::Request,
http::{
HeaderValue, StatusCode,
header::{CONTENT_LENGTH, CONTENT_TYPE},
},
middleware::{self, Next},
response::{IntoResponse, Response},
routing::{delete, get},
};
use serde_json::{Value as JsonValue, json};
use tracing::{error, warn};
use utoipa_swagger_ui::SwaggerUi;
use crate::api::auth::{AuthState, basic_auth};
use crate::document::store::StoreError;
use crate::state::AppState;
/// Builds the complete HTTP router. Everything except the API description (`/swagger-ui`,
/// `/openapi.yaml`), `/health` and `/ready` 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)
.post(store::post_store_entry)
.delete(store::delete_store_entry),
)
.route("/stream", get(store::stream_store_root))
.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(config::get_config))
.merge(crate::remake::router::<AppState>());
#[cfg(feature = "dashboard")]
{
api = api.merge(dashboard::router(state.clone()));
}
if debug_api {
warn!("Debug API enabled");
api = api.route(
"/debug/firewall",
get(debug::get_debug_firewall).put(debug::put_debug_firewall),
);
}
api.layer(middleware::from_fn_with_state(auth_state, basic_auth))
// The hand-written spec (`docs/openapi.yaml`), and the Swagger UI showing it.
.merge(
SwaggerUi::new("/swagger-ui").config(utoipa_swagger_ui::Config::from("/openapi.yaml")),
)
.route("/openapi.yaml", get(openapi_spec))
.route("/health", get(health::health))
.route("/ready", get(health::ready))
// Outermost, so authentication failures get the error body too.
.layer(middleware::from_fn(json_errors))
.with_state(state)
}
/// The HTTP router of a node that only relays (`--relay-only`): its figures and the log
/// filter (authenticated), and `/health`.
#[cfg_attr(not(feature = "libp2p"), allow(dead_code))]
pub(crate) fn relay_only_router(
transport: std::sync::Arc<dyn crate::transport::Transport>,
auth_state: AuthState,
) -> Router {
let relay = stats::RelayOnly {
transport,
started: tokio::time::Instant::now(),
};
Router::new()
.route(
"/admin/log",
get(log::get_log_filter).put(log::put_log_filter),
)
.route("/admin/stats", get(stats::get_relay_only_stats))
.layer(middleware::from_fn_with_state(auth_state, basic_auth))
.route("/health", get(health::health))
.layer(middleware::from_fn(json_errors))
.with_state(relay)
}
/// An error answer: `{"error": message}` with `details`' fields added (`docs/openapi.yaml`,
/// `Error`).
pub(crate) fn error_response(
status: StatusCode,
message: impl Into<String>,
details: Option<JsonValue>,
) -> Response {
let mut body = json!({ "error": message.into() });
if let (Some(JsonValue::Object(extra)), Some(object)) = (details, body.as_object_mut()) {
object.extend(extra);
}
(status, Json(body)).into_response()
}
/// Gives every error answer the same body (`error_response`). Handlers that fail with
/// a bare status or a text message, and axum's own rejections (a malformed query or
/// body), get it here: the text becomes the message, or the status's reason if there
/// is none. Errors that are JSON already pass unchanged, and so do their headers
/// (`WWW-Authenticate` on a 401).
async fn json_errors(request: Request, next: Next) -> Response {
let response = next.run(request).await;
let status = response.status();
if !(status.is_client_error() || status.is_server_error()) {
return response;
}
let is_json = response
.headers()
.get(CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.is_some_and(|value| value.starts_with("application/json"));
if is_json {
return response;
}
let (mut parts, body) = response.into_parts();
// Error texts are short; a larger one is cut to its reason.
let text = axum::body::to_bytes(body, 64 * 1024)
.await
.map(|bytes| String::from_utf8_lossy(&bytes).trim().to_owned())
.unwrap_or_default();
let message = if text.is_empty() {
status.canonical_reason().unwrap_or("error").to_lowercase()
} else {
text
};
let body = json!({ "error": message }).to_string();
parts.headers.remove(CONTENT_LENGTH);
parts
.headers
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
Response::from_parts(parts, Body::from(body))
}
/// `GET /openapi.yaml`: the API description (`docs/openapi.yaml`, built in).
async fn openapi_spec() -> impl IntoResponse {
(
[(CONTENT_TYPE, HeaderValue::from_static("application/yaml"))],
include_str!("../../docs/openapi.yaml"),
)
}
// --- shared helpers ----------------------------------------------------------
pub(crate) fn store_status(error: StoreError, context: &str) -> StatusCode {
match error {
StoreError::Conflict => StatusCode::CONFLICT,
StoreError::NotFound => StatusCode::NOT_FOUND,
StoreError::NotAList | StoreError::PastTheEnd { .. } => StatusCode::CONFLICT,
other => {
error!(error = %format!("{other:#}"), "{context}");
StatusCode::INTERNAL_SERVER_ERROR
}
}
}
|