2023-09-29 16:41:00 +00:00
|
|
|
use std::fs::{self, Permissions};
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2022-05-10 11:16:57 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
|
|
use futures::future::Future;
|
|
|
|
|
2024-02-05 17:49:54 +00:00
|
|
|
use http_body_util::BodyExt;
|
2022-05-24 10:16:39 +00:00
|
|
|
use hyper::header::HeaderValue;
|
2024-02-05 17:49:54 +00:00
|
|
|
use hyper::server::conn::http1;
|
|
|
|
use hyper::service::service_fn;
|
|
|
|
use hyper::{body::Incoming as IncomingBody, Request, Response};
|
2022-05-24 10:16:39 +00:00
|
|
|
use hyper::{HeaderMap, StatusCode};
|
2024-02-05 17:49:54 +00:00
|
|
|
use hyper_util::rt::TokioIo;
|
2022-05-10 11:16:57 +00:00
|
|
|
|
2024-02-05 17:49:54 +00:00
|
|
|
use tokio::io::{AsyncRead, AsyncWrite};
|
|
|
|
use tokio::net::{TcpListener, UnixListener};
|
2023-09-29 16:41:00 +00:00
|
|
|
|
2022-05-10 11:16:57 +00:00
|
|
|
use opentelemetry::{
|
|
|
|
global,
|
|
|
|
metrics::{Counter, ValueRecorder},
|
|
|
|
trace::{FutureExt, SpanRef, TraceContextExt, Tracer},
|
|
|
|
Context, KeyValue,
|
|
|
|
};
|
|
|
|
|
|
|
|
use garage_util::error::Error as GarageError;
|
2023-02-09 13:49:43 +00:00
|
|
|
use garage_util::forwarded_headers;
|
2022-05-10 11:16:57 +00:00
|
|
|
use garage_util::metrics::{gen_trace_id, RecordDuration};
|
2023-09-29 16:41:00 +00:00
|
|
|
use garage_util::socket_address::UnixOrTCPSocketAddress;
|
2022-05-10 11:16:57 +00:00
|
|
|
|
2024-02-05 17:49:54 +00:00
|
|
|
use crate::helpers::{BoxBody, BytesBody};
|
|
|
|
|
2022-05-10 11:16:57 +00:00
|
|
|
pub(crate) trait ApiEndpoint: Send + Sync + 'static {
|
|
|
|
fn name(&self) -> &'static str;
|
|
|
|
fn add_span_attributes(&self, span: SpanRef<'_>);
|
|
|
|
}
|
|
|
|
|
2022-05-24 10:16:39 +00:00
|
|
|
pub trait ApiError: std::error::Error + Send + Sync + 'static {
|
|
|
|
fn http_status_code(&self) -> StatusCode;
|
|
|
|
fn add_http_headers(&self, header_map: &mut HeaderMap<HeaderValue>);
|
2024-02-05 17:49:54 +00:00
|
|
|
fn http_body(&self, garage_region: &str, path: &str) -> BytesBody;
|
2022-05-24 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:16:57 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub(crate) trait ApiHandler: Send + Sync + 'static {
|
|
|
|
const API_NAME: &'static str;
|
|
|
|
const API_NAME_DISPLAY: &'static str;
|
|
|
|
|
|
|
|
type Endpoint: ApiEndpoint;
|
2022-05-24 10:16:39 +00:00
|
|
|
type Error: ApiError;
|
2022-05-10 11:16:57 +00:00
|
|
|
|
2024-02-05 17:49:54 +00:00
|
|
|
fn parse_endpoint(&self, r: &Request<IncomingBody>) -> Result<Self::Endpoint, Self::Error>;
|
2022-05-10 11:16:57 +00:00
|
|
|
async fn handle(
|
|
|
|
&self,
|
2024-02-05 17:49:54 +00:00
|
|
|
req: Request<IncomingBody>,
|
2022-05-10 11:16:57 +00:00
|
|
|
endpoint: Self::Endpoint,
|
2024-02-05 17:49:54 +00:00
|
|
|
) -> Result<Response<BoxBody<Self::Error>>, Self::Error>;
|
2022-05-10 11:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct ApiServer<A: ApiHandler> {
|
|
|
|
region: String,
|
|
|
|
api_handler: A,
|
|
|
|
|
|
|
|
// Metrics
|
|
|
|
request_counter: Counter<u64>,
|
|
|
|
error_counter: Counter<u64>,
|
|
|
|
request_duration: ValueRecorder<f64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: ApiHandler> ApiServer<A> {
|
|
|
|
pub fn new(region: String, api_handler: A) -> Arc<Self> {
|
|
|
|
let meter = global::meter("garage/api");
|
|
|
|
Arc::new(Self {
|
|
|
|
region,
|
|
|
|
api_handler,
|
|
|
|
request_counter: meter
|
|
|
|
.u64_counter(format!("api.{}.request_counter", A::API_NAME))
|
|
|
|
.with_description(format!(
|
|
|
|
"Number of API calls to the various {} API endpoints",
|
|
|
|
A::API_NAME_DISPLAY
|
|
|
|
))
|
|
|
|
.init(),
|
|
|
|
error_counter: meter
|
|
|
|
.u64_counter(format!("api.{}.error_counter", A::API_NAME))
|
|
|
|
.with_description(format!(
|
|
|
|
"Number of API calls to the various {} API endpoints that resulted in errors",
|
|
|
|
A::API_NAME_DISPLAY
|
|
|
|
))
|
|
|
|
.init(),
|
|
|
|
request_duration: meter
|
|
|
|
.f64_value_recorder(format!("api.{}.request_duration", A::API_NAME))
|
|
|
|
.with_description(format!(
|
|
|
|
"Duration of API calls to the various {} API endpoints",
|
|
|
|
A::API_NAME_DISPLAY
|
|
|
|
))
|
|
|
|
.init(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_server(
|
|
|
|
self: Arc<Self>,
|
2023-09-29 16:41:00 +00:00
|
|
|
bind_addr: UnixOrTCPSocketAddress,
|
2023-10-03 15:31:40 +00:00
|
|
|
unix_bind_addr_mode: Option<u32>,
|
2022-05-10 11:16:57 +00:00
|
|
|
shutdown_signal: impl Future<Output = ()>,
|
|
|
|
) -> Result<(), GarageError> {
|
|
|
|
info!(
|
2023-09-29 16:41:00 +00:00
|
|
|
"{} API server listening on {}",
|
2022-05-10 11:16:57 +00:00
|
|
|
A::API_NAME_DISPLAY,
|
|
|
|
bind_addr
|
|
|
|
);
|
|
|
|
|
2024-02-05 17:49:54 +00:00
|
|
|
tokio::pin!(shutdown_signal);
|
|
|
|
|
2023-09-29 16:41:00 +00:00
|
|
|
match bind_addr {
|
|
|
|
UnixOrTCPSocketAddress::TCPSocket(addr) => {
|
2024-02-05 17:49:54 +00:00
|
|
|
let listener = TcpListener::bind(addr).await?;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let (stream, client_addr) = tokio::select! {
|
|
|
|
acc = listener.accept() => acc?,
|
|
|
|
_ = &mut shutdown_signal => break,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.launch_handler(stream, client_addr.to_string());
|
|
|
|
}
|
2023-09-29 16:41:00 +00:00
|
|
|
}
|
|
|
|
UnixOrTCPSocketAddress::UnixSocket(ref path) => {
|
|
|
|
if path.exists() {
|
|
|
|
fs::remove_file(path)?
|
|
|
|
}
|
|
|
|
|
2024-02-05 17:49:54 +00:00
|
|
|
let listener = UnixListener::bind(path)?;
|
2023-09-29 16:41:00 +00:00
|
|
|
|
2023-10-03 15:31:40 +00:00
|
|
|
fs::set_permissions(
|
|
|
|
path,
|
|
|
|
Permissions::from_mode(unix_bind_addr_mode.unwrap_or(0o222)),
|
|
|
|
)?;
|
2023-09-29 16:41:00 +00:00
|
|
|
|
2024-02-05 17:49:54 +00:00
|
|
|
loop {
|
|
|
|
let (stream, _) = tokio::select! {
|
|
|
|
acc = listener.accept() => acc?,
|
|
|
|
_ = &mut shutdown_signal => break,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.launch_handler(stream, path.display().to_string());
|
|
|
|
}
|
2023-09-29 16:41:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-10 11:16:57 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-05 17:49:54 +00:00
|
|
|
fn launch_handler<S>(self: &Arc<Self>, stream: S, client_addr: String)
|
|
|
|
where
|
|
|
|
S: AsyncRead + AsyncWrite + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
let this = self.clone();
|
|
|
|
let io = TokioIo::new(stream);
|
|
|
|
|
|
|
|
let serve =
|
|
|
|
move |req: Request<IncomingBody>| this.clone().handler(req, client_addr.to_string());
|
|
|
|
|
|
|
|
tokio::task::spawn(async move {
|
|
|
|
let io = Box::pin(io);
|
|
|
|
if let Err(e) = http1::Builder::new()
|
|
|
|
.serve_connection(io, service_fn(serve))
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
debug!("Error handling HTTP connection: {}", e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:16:57 +00:00
|
|
|
async fn handler(
|
|
|
|
self: Arc<Self>,
|
2024-02-05 17:49:54 +00:00
|
|
|
req: Request<IncomingBody>,
|
2023-09-29 16:41:00 +00:00
|
|
|
addr: String,
|
2024-02-05 17:49:54 +00:00
|
|
|
) -> Result<Response<BoxBody<A::Error>>, GarageError> {
|
2022-05-10 11:16:57 +00:00
|
|
|
let uri = req.uri().clone();
|
2023-02-04 09:49:56 +00:00
|
|
|
|
2023-02-09 13:49:43 +00:00
|
|
|
if let Ok(forwarded_for_ip_addr) =
|
2023-05-09 19:49:34 +00:00
|
|
|
forwarded_headers::handle_forwarded_for_headers(req.headers())
|
2023-02-09 13:49:43 +00:00
|
|
|
{
|
2023-02-04 09:49:56 +00:00
|
|
|
info!(
|
|
|
|
"{} (via {}) {} {}",
|
|
|
|
forwarded_for_ip_addr,
|
|
|
|
addr,
|
|
|
|
req.method(),
|
|
|
|
uri
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
info!("{} {} {}", addr, req.method(), uri);
|
|
|
|
}
|
2022-05-10 11:16:57 +00:00
|
|
|
debug!("{:?}", req);
|
|
|
|
|
|
|
|
let tracer = opentelemetry::global::tracer("garage");
|
|
|
|
let span = tracer
|
|
|
|
.span_builder(format!("{} API call (unknown)", A::API_NAME_DISPLAY))
|
|
|
|
.with_trace_id(gen_trace_id())
|
|
|
|
.with_attributes(vec![
|
|
|
|
KeyValue::new("method", format!("{}", req.method())),
|
|
|
|
KeyValue::new("uri", req.uri().to_string()),
|
|
|
|
])
|
|
|
|
.start(&tracer);
|
|
|
|
|
|
|
|
let res = self
|
|
|
|
.handler_stage2(req)
|
|
|
|
.with_context(Context::current_with_span(span))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok(x) => {
|
|
|
|
debug!("{} {:?}", x.status(), x.headers());
|
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2024-02-05 17:49:54 +00:00
|
|
|
let body = e.http_body(&self.region, uri.path());
|
2022-05-25 15:05:56 +00:00
|
|
|
let mut http_error_builder = Response::builder().status(e.http_status_code());
|
2022-05-10 11:16:57 +00:00
|
|
|
|
|
|
|
if let Some(header_map) = http_error_builder.headers_mut() {
|
2022-05-24 10:16:39 +00:00
|
|
|
e.add_http_headers(header_map)
|
2022-05-10 11:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let http_error = http_error_builder.body(body)?;
|
|
|
|
|
|
|
|
if e.http_status_code().is_server_error() {
|
|
|
|
warn!("Response: error {}, {}", e.http_status_code(), e);
|
|
|
|
} else {
|
|
|
|
info!("Response: error {}, {}", e.http_status_code(), e);
|
|
|
|
}
|
2024-02-05 17:49:54 +00:00
|
|
|
Ok(http_error.map(|body| BoxBody::new(body.map_err(|_| unreachable!()))))
|
2022-05-10 11:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 17:49:54 +00:00
|
|
|
async fn handler_stage2(
|
|
|
|
&self,
|
|
|
|
req: Request<IncomingBody>,
|
|
|
|
) -> Result<Response<BoxBody<A::Error>>, A::Error> {
|
2022-05-10 11:16:57 +00:00
|
|
|
let endpoint = self.api_handler.parse_endpoint(&req)?;
|
|
|
|
debug!("Endpoint: {}", endpoint.name());
|
|
|
|
|
|
|
|
let current_context = Context::current();
|
|
|
|
let current_span = current_context.span();
|
2022-09-26 14:20:30 +00:00
|
|
|
current_span.update_name::<String>(format!(
|
|
|
|
"{} API {}",
|
|
|
|
A::API_NAME_DISPLAY,
|
|
|
|
endpoint.name()
|
|
|
|
));
|
2022-05-10 11:16:57 +00:00
|
|
|
current_span.set_attribute(KeyValue::new("endpoint", endpoint.name()));
|
|
|
|
endpoint.add_span_attributes(current_span);
|
|
|
|
|
|
|
|
let metrics_tags = &[KeyValue::new("api_endpoint", endpoint.name())];
|
|
|
|
|
|
|
|
let res = self
|
|
|
|
.api_handler
|
|
|
|
.handle(req, endpoint)
|
|
|
|
.record_duration(&self.request_duration, &metrics_tags[..])
|
|
|
|
.await;
|
|
|
|
|
|
|
|
self.request_counter.add(1, &metrics_tags[..]);
|
|
|
|
|
|
|
|
let status_code = match &res {
|
|
|
|
Ok(r) => r.status(),
|
|
|
|
Err(e) => e.http_status_code(),
|
|
|
|
};
|
|
|
|
if status_code.is_client_error() || status_code.is_server_error() {
|
|
|
|
self.error_counter.add(
|
|
|
|
1,
|
|
|
|
&[
|
|
|
|
metrics_tags[0].clone(),
|
|
|
|
KeyValue::new("status_code", status_code.as_str().to_string()),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|