2020-04-19 17:59:59 +00:00
|
|
|
use std::net::SocketAddr;
|
2020-04-10 20:01:48 +00:00
|
|
|
use std::sync::Arc;
|
2020-04-06 17:55:39 +00:00
|
|
|
|
2022-02-18 16:05:19 +00:00
|
|
|
use chrono::{DateTime, NaiveDateTime, Utc};
|
2020-04-10 20:01:48 +00:00
|
|
|
use futures::future::Future;
|
2022-02-18 16:05:19 +00:00
|
|
|
use futures::prelude::*;
|
2021-11-11 10:26:02 +00:00
|
|
|
use hyper::header;
|
2020-04-07 22:39:07 +00:00
|
|
|
use hyper::server::conn::AddrStream;
|
2020-04-10 20:01:48 +00:00
|
|
|
use hyper::service::{make_service_fn, service_fn};
|
2022-01-07 15:23:04 +00:00
|
|
|
use hyper::{Body, Method, Request, Response, Server};
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2022-02-17 22:28:23 +00:00
|
|
|
use opentelemetry::{
|
2022-02-22 13:52:41 +00:00
|
|
|
global,
|
|
|
|
metrics::{Counter, ValueRecorder},
|
2022-02-17 22:28:23 +00:00
|
|
|
trace::{FutureExt, TraceContextExt, Tracer},
|
2022-02-18 19:39:55 +00:00
|
|
|
Context, KeyValue,
|
2022-02-17 22:28:23 +00:00
|
|
|
};
|
|
|
|
|
2021-12-14 12:55:11 +00:00
|
|
|
use garage_util::data::*;
|
2020-11-08 14:04:30 +00:00
|
|
|
use garage_util::error::Error as GarageError;
|
2022-02-22 14:21:06 +00:00
|
|
|
use garage_util::metrics::{gen_trace_id, RecordDuration};
|
2020-04-23 17:05:46 +00:00
|
|
|
|
2020-07-07 11:59:22 +00:00
|
|
|
use garage_model::garage::Garage;
|
2021-12-14 12:55:11 +00:00
|
|
|
use garage_model::key_table::Key;
|
2020-04-23 17:05:46 +00:00
|
|
|
|
2022-01-07 15:23:04 +00:00
|
|
|
use garage_table::util::*;
|
|
|
|
|
2020-11-08 14:04:30 +00:00
|
|
|
use crate::error::*;
|
2022-02-18 16:05:19 +00:00
|
|
|
use crate::signature::compute_scope;
|
2022-01-17 09:55:31 +00:00
|
|
|
use crate::signature::payload::check_payload_signature;
|
2022-02-18 16:05:19 +00:00
|
|
|
use crate::signature::streaming::SignedPayloadStream;
|
|
|
|
use crate::signature::LONG_DATETIME;
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2021-11-11 10:26:02 +00:00
|
|
|
use crate::helpers::*;
|
2021-04-27 23:05:40 +00:00
|
|
|
use crate::s3_bucket::*;
|
2020-04-28 10:18:14 +00:00
|
|
|
use crate::s3_copy::*;
|
2022-01-07 15:23:04 +00:00
|
|
|
use crate::s3_cors::*;
|
2020-04-28 10:18:14 +00:00
|
|
|
use crate::s3_delete::*;
|
2020-04-26 20:39:32 +00:00
|
|
|
use crate::s3_get::*;
|
|
|
|
use crate::s3_list::*;
|
2022-02-21 22:02:30 +00:00
|
|
|
use crate::s3_post_object::handle_post_object;
|
2020-04-26 20:39:32 +00:00
|
|
|
use crate::s3_put::*;
|
2021-12-06 14:17:47 +00:00
|
|
|
use crate::s3_router::{Authorization, Endpoint};
|
2021-12-15 09:41:39 +00:00
|
|
|
use crate::s3_website::*;
|
2020-04-10 20:01:48 +00:00
|
|
|
|
2022-02-22 13:52:41 +00:00
|
|
|
struct ApiMetrics {
|
|
|
|
request_counter: Counter<u64>,
|
|
|
|
error_counter: Counter<u64>,
|
|
|
|
request_duration: ValueRecorder<f64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ApiMetrics {
|
|
|
|
fn new() -> Self {
|
|
|
|
let meter = global::meter("garage/api");
|
|
|
|
Self {
|
|
|
|
request_counter: meter
|
|
|
|
.u64_counter("api.request_counter")
|
|
|
|
.with_description("Number of API calls to the various S3 API endpoints")
|
|
|
|
.init(),
|
|
|
|
error_counter: meter
|
|
|
|
.u64_counter("api.error_counter")
|
|
|
|
.with_description(
|
|
|
|
"Number of API calls to the various S3 API endpoints that resulted in errors",
|
|
|
|
)
|
|
|
|
.init(),
|
|
|
|
request_duration: meter
|
|
|
|
.f64_value_recorder("api.request_duration")
|
|
|
|
.with_description("Duration of API calls to the various S3 API endpoints")
|
|
|
|
.init(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 21:32:09 +00:00
|
|
|
/// Run the S3 API server
|
2020-04-10 20:01:48 +00:00
|
|
|
pub async fn run_api_server(
|
|
|
|
garage: Arc<Garage>,
|
|
|
|
shutdown_signal: impl Future<Output = ()>,
|
2020-11-08 14:04:30 +00:00
|
|
|
) -> Result<(), GarageError> {
|
2020-04-24 17:46:52 +00:00
|
|
|
let addr = &garage.config.s3_api.api_bind_addr;
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2022-02-22 13:52:41 +00:00
|
|
|
let metrics = Arc::new(ApiMetrics::new());
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
let service = make_service_fn(|conn: &AddrStream| {
|
2020-04-08 20:00:41 +00:00
|
|
|
let garage = garage.clone();
|
2022-02-22 13:52:41 +00:00
|
|
|
let metrics = metrics.clone();
|
|
|
|
|
2020-04-07 22:39:07 +00:00
|
|
|
let client_addr = conn.remote_addr();
|
2020-04-06 17:55:39 +00:00
|
|
|
async move {
|
2020-11-08 14:04:30 +00:00
|
|
|
Ok::<_, GarageError>(service_fn(move |req: Request<Body>| {
|
2020-04-08 20:00:41 +00:00
|
|
|
let garage = garage.clone();
|
2022-02-22 13:52:41 +00:00
|
|
|
let metrics = metrics.clone();
|
2022-02-17 22:28:23 +00:00
|
|
|
|
2022-02-22 13:52:41 +00:00
|
|
|
handler(garage, metrics, req, client_addr)
|
2020-04-06 17:55:39 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
});
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2021-10-26 08:20:05 +00:00
|
|
|
let server = Server::bind(addr).serve(service);
|
2020-04-05 21:33:42 +00:00
|
|
|
|
|
|
|
let graceful = server.with_graceful_shutdown(shutdown_signal);
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("API server listening on http://{}", addr);
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
graceful.await?;
|
|
|
|
Ok(())
|
2020-04-05 21:33:42 +00:00
|
|
|
}
|
2020-04-07 22:39:07 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
async fn handler(
|
|
|
|
garage: Arc<Garage>,
|
2022-02-22 13:52:41 +00:00
|
|
|
metrics: Arc<ApiMetrics>,
|
2020-04-10 20:01:48 +00:00
|
|
|
req: Request<Body>,
|
|
|
|
addr: SocketAddr,
|
2020-11-08 14:04:30 +00:00
|
|
|
) -> Result<Response<Body>, GarageError> {
|
2021-04-27 23:05:40 +00:00
|
|
|
let uri = req.uri().clone();
|
|
|
|
info!("{} {} {}", addr, req.method(), uri);
|
2020-04-24 17:46:52 +00:00
|
|
|
debug!("{:?}", req);
|
2022-02-22 13:52:41 +00:00
|
|
|
|
|
|
|
let tracer = opentelemetry::global::tracer("garage");
|
|
|
|
let span = tracer
|
|
|
|
.span_builder("S3 API call (unknown)")
|
2022-02-22 14:21:06 +00:00
|
|
|
.with_trace_id(gen_trace_id())
|
2022-02-22 13:52:41 +00:00
|
|
|
.with_attributes(vec![
|
|
|
|
KeyValue::new("method", format!("{}", req.method())),
|
2022-02-22 14:21:06 +00:00
|
|
|
KeyValue::new("uri", req.uri().to_string()),
|
2022-02-22 13:52:41 +00:00
|
|
|
])
|
|
|
|
.start(&tracer);
|
|
|
|
|
|
|
|
let res = handler_stage2(garage.clone(), metrics, req)
|
|
|
|
.with_context(Context::current_with_span(span))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
match res {
|
2020-04-24 17:46:52 +00:00
|
|
|
Ok(x) => {
|
|
|
|
debug!("{} {:?}", x.status(), x.headers());
|
|
|
|
Ok(x)
|
|
|
|
}
|
2020-04-07 22:39:07 +00:00
|
|
|
Err(e) => {
|
2021-04-27 23:05:40 +00:00
|
|
|
let body: Body = Body::from(e.aws_xml(&garage.config.s3_api.s3_region, uri.path()));
|
2021-11-29 10:52:42 +00:00
|
|
|
let mut http_error_builder = Response::builder()
|
2021-04-27 23:05:40 +00:00
|
|
|
.status(e.http_status_code())
|
2021-11-29 10:52:42 +00:00
|
|
|
.header("Content-Type", "application/xml");
|
|
|
|
|
|
|
|
if let Some(header_map) = http_error_builder.headers_mut() {
|
|
|
|
e.add_headers(header_map)
|
|
|
|
}
|
|
|
|
|
|
|
|
let http_error = http_error_builder.body(body)?;
|
2021-04-27 23:05:40 +00:00
|
|
|
|
2021-02-19 23:30:39 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-04-09 15:32:28 +00:00
|
|
|
Ok(http_error)
|
2020-04-07 22:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 13:52:41 +00:00
|
|
|
async fn handler_stage2(
|
|
|
|
garage: Arc<Garage>,
|
|
|
|
metrics: Arc<ApiMetrics>,
|
|
|
|
req: Request<Body>,
|
|
|
|
) -> Result<Response<Body>, Error> {
|
2021-11-11 10:26:02 +00:00
|
|
|
let authority = req
|
|
|
|
.headers()
|
|
|
|
.get(header::HOST)
|
2022-02-18 16:05:19 +00:00
|
|
|
.ok_or_bad_request("Host header required")?
|
2021-11-11 10:26:02 +00:00
|
|
|
.to_str()?;
|
|
|
|
|
|
|
|
let host = authority_to_host(authority)?;
|
|
|
|
|
2022-01-07 15:23:04 +00:00
|
|
|
let bucket_name = garage
|
2021-11-11 13:12:22 +00:00
|
|
|
.config
|
|
|
|
.s3_api
|
|
|
|
.root_domain
|
|
|
|
.as_ref()
|
2021-11-11 14:37:48 +00:00
|
|
|
.and_then(|root_domain| host_to_bucket(&host, root_domain));
|
2021-11-11 13:12:22 +00:00
|
|
|
|
2022-01-07 15:23:04 +00:00
|
|
|
let (endpoint, bucket_name) = Endpoint::from_request(&req, bucket_name.map(ToOwned::to_owned))?;
|
2022-01-05 16:07:36 +00:00
|
|
|
debug!("Endpoint: {:?}", endpoint);
|
2021-12-14 12:55:11 +00:00
|
|
|
|
2022-02-22 14:21:06 +00:00
|
|
|
let current_context = Context::current();
|
|
|
|
let current_span = current_context.span();
|
|
|
|
current_span.update_name::<String>(format!("S3 API {}", endpoint.name()));
|
|
|
|
current_span.set_attribute(KeyValue::new("endpoint", endpoint.name()));
|
|
|
|
current_span.set_attribute(KeyValue::new(
|
|
|
|
"bucket",
|
|
|
|
bucket_name.clone().unwrap_or_default(),
|
|
|
|
));
|
2022-02-17 22:28:23 +00:00
|
|
|
|
2022-02-22 13:52:41 +00:00
|
|
|
let metrics_tags = &[KeyValue::new("api_endpoint", endpoint.name())];
|
|
|
|
|
|
|
|
let res = handler_stage3(garage, req, endpoint, bucket_name)
|
|
|
|
.record_duration(&metrics.request_duration, &metrics_tags[..])
|
|
|
|
.await;
|
|
|
|
|
|
|
|
metrics.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() {
|
|
|
|
metrics.error_counter.add(
|
|
|
|
1,
|
|
|
|
&[
|
|
|
|
metrics_tags[0].clone(),
|
|
|
|
KeyValue::new("status_code", status_code.as_str().to_string()),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handler_stage3(
|
|
|
|
garage: Arc<Garage>,
|
|
|
|
req: Request<Body>,
|
|
|
|
endpoint: Endpoint,
|
|
|
|
bucket_name: Option<String>,
|
|
|
|
) -> Result<Response<Body>, Error> {
|
2022-02-28 10:15:53 +00:00
|
|
|
// Some endpoints are processed early, before we even check for an API key
|
|
|
|
if let Endpoint::PostObject = endpoint {
|
2022-02-21 22:02:30 +00:00
|
|
|
return handle_post_object(garage, req, bucket_name.unwrap()).await;
|
|
|
|
}
|
2022-02-28 10:15:53 +00:00
|
|
|
if let Endpoint::Options = endpoint {
|
2022-03-01 10:15:16 +00:00
|
|
|
return handle_options_s3api(garage, &req, bucket_name).await;
|
2022-02-28 10:15:53 +00:00
|
|
|
}
|
2022-02-21 22:02:30 +00:00
|
|
|
|
2022-02-18 16:05:19 +00:00
|
|
|
let (api_key, mut content_sha256) = check_payload_signature(&garage, &req).await?;
|
2022-02-21 22:02:30 +00:00
|
|
|
let api_key = api_key.ok_or_else(|| {
|
|
|
|
Error::Forbidden("Garage does not support anonymous access yet".to_string())
|
|
|
|
})?;
|
|
|
|
|
2022-02-18 16:05:19 +00:00
|
|
|
let req = match req.headers().get("x-amz-content-sha256") {
|
|
|
|
Some(header) if header == "STREAMING-AWS4-HMAC-SHA256-PAYLOAD" => {
|
|
|
|
let signature = content_sha256
|
|
|
|
.take()
|
|
|
|
.ok_or_bad_request("No signature provided")?;
|
|
|
|
|
|
|
|
let secret_key = &api_key
|
|
|
|
.state
|
|
|
|
.as_option()
|
|
|
|
.ok_or_internal_error("Deleted key state")?
|
|
|
|
.secret_key;
|
|
|
|
|
|
|
|
let date = req
|
|
|
|
.headers()
|
|
|
|
.get("x-amz-date")
|
|
|
|
.ok_or_bad_request("Missing X-Amz-Date field")?
|
|
|
|
.to_str()?;
|
|
|
|
let date: NaiveDateTime = NaiveDateTime::parse_from_str(date, LONG_DATETIME)
|
|
|
|
.ok_or_bad_request("Invalid date")?;
|
|
|
|
let date: DateTime<Utc> = DateTime::from_utc(date, Utc);
|
|
|
|
|
|
|
|
let scope = compute_scope(&date, &garage.config.s3_api.s3_region);
|
|
|
|
let signing_hmac = crate::signature::signing_hmac(
|
|
|
|
&date,
|
|
|
|
secret_key,
|
|
|
|
&garage.config.s3_api.s3_region,
|
|
|
|
"s3",
|
|
|
|
)
|
|
|
|
.ok_or_internal_error("Unable to build signing HMAC")?;
|
|
|
|
|
|
|
|
req.map(move |body| {
|
|
|
|
Body::wrap_stream(
|
|
|
|
SignedPayloadStream::new(
|
|
|
|
body.map_err(Error::from),
|
|
|
|
signing_hmac,
|
|
|
|
date,
|
|
|
|
&scope,
|
|
|
|
signature,
|
|
|
|
)
|
|
|
|
.map_err(Error::from),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => req,
|
|
|
|
};
|
|
|
|
|
2022-01-07 15:23:04 +00:00
|
|
|
let bucket_name = match bucket_name {
|
2021-12-17 10:53:13 +00:00
|
|
|
None => return handle_request_without_bucket(garage, req, api_key, endpoint).await,
|
|
|
|
Some(bucket) => bucket.to_string(),
|
2021-12-14 12:55:11 +00:00
|
|
|
};
|
|
|
|
|
2022-01-11 18:14:14 +00:00
|
|
|
// Special code path for CreateBucket API endpoint
|
|
|
|
if let Endpoint::CreateBucket {} = endpoint {
|
|
|
|
return handle_create_bucket(&garage, req, content_sha256, api_key, bucket_name).await;
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:55:11 +00:00
|
|
|
let bucket_id = resolve_bucket(&garage, &bucket_name, &api_key).await?;
|
2022-01-07 15:23:04 +00:00
|
|
|
let bucket = garage
|
|
|
|
.bucket_table
|
|
|
|
.get(&EmptyKey, &bucket_id)
|
|
|
|
.await?
|
|
|
|
.filter(|b| !b.state.is_deleted())
|
|
|
|
.ok_or(Error::NoSuchBucket)?;
|
2021-12-14 12:55:11 +00:00
|
|
|
|
2021-12-06 14:17:47 +00:00
|
|
|
let allowed = match endpoint.authorization_type() {
|
2022-01-11 18:14:14 +00:00
|
|
|
Authorization::Read => api_key.allow_read(&bucket_id),
|
|
|
|
Authorization::Write => api_key.allow_write(&bucket_id),
|
|
|
|
Authorization::Owner => api_key.allow_owner(&bucket_id),
|
2021-12-14 12:55:11 +00:00
|
|
|
_ => unreachable!(),
|
2020-04-24 17:46:52 +00:00
|
|
|
};
|
2021-12-06 14:17:47 +00:00
|
|
|
|
2020-04-24 17:46:52 +00:00
|
|
|
if !allowed {
|
2021-04-23 20:18:00 +00:00
|
|
|
return Err(Error::Forbidden(
|
|
|
|
"Operation is not allowed for this key.".to_string(),
|
|
|
|
));
|
2020-04-24 17:46:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 15:23:04 +00:00
|
|
|
// Look up what CORS rule might apply to response.
|
|
|
|
// Requests for methods different than GET, HEAD or POST
|
|
|
|
// are always preflighted, i.e. the browser should make
|
|
|
|
// an OPTIONS call before to check it is allowed
|
|
|
|
let matching_cors_rule = match *req.method() {
|
|
|
|
Method::GET | Method::HEAD | Method::POST => find_matching_cors_rule(&bucket, &req)?,
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let resp = match endpoint {
|
2022-01-12 11:43:33 +00:00
|
|
|
Endpoint::HeadObject {
|
|
|
|
key, part_number, ..
|
|
|
|
} => handle_head(garage, &req, bucket_id, &key, part_number).await,
|
|
|
|
Endpoint::GetObject {
|
|
|
|
key, part_number, ..
|
|
|
|
} => handle_get(garage, &req, bucket_id, &key, part_number).await,
|
2021-12-06 14:17:47 +00:00
|
|
|
Endpoint::UploadPart {
|
|
|
|
key,
|
|
|
|
part_number,
|
|
|
|
upload_id,
|
|
|
|
} => {
|
|
|
|
handle_put_part(
|
|
|
|
garage,
|
|
|
|
req,
|
2021-12-14 12:55:11 +00:00
|
|
|
bucket_id,
|
2021-12-06 14:17:47 +00:00
|
|
|
&key,
|
|
|
|
part_number,
|
|
|
|
&upload_id,
|
|
|
|
content_sha256,
|
|
|
|
)
|
|
|
|
.await
|
2020-04-26 20:39:32 +00:00
|
|
|
}
|
2022-01-11 18:14:14 +00:00
|
|
|
Endpoint::CopyObject { key } => handle_copy(garage, &api_key, &req, bucket_id, &key).await,
|
2022-01-11 16:31:09 +00:00
|
|
|
Endpoint::UploadPartCopy {
|
|
|
|
key,
|
|
|
|
part_number,
|
|
|
|
upload_id,
|
|
|
|
} => {
|
|
|
|
handle_upload_part_copy(
|
|
|
|
garage,
|
|
|
|
&api_key,
|
|
|
|
&req,
|
|
|
|
bucket_id,
|
|
|
|
&key,
|
|
|
|
part_number,
|
|
|
|
&upload_id,
|
|
|
|
)
|
|
|
|
.await
|
2021-12-06 14:17:47 +00:00
|
|
|
}
|
2022-01-11 18:14:14 +00:00
|
|
|
Endpoint::PutObject { key } => {
|
2022-02-18 16:05:19 +00:00
|
|
|
handle_put(garage, req, bucket_id, &key, content_sha256).await
|
2021-12-06 14:17:47 +00:00
|
|
|
}
|
2022-01-11 18:14:14 +00:00
|
|
|
Endpoint::AbortMultipartUpload { key, upload_id } => {
|
2021-12-14 12:55:11 +00:00
|
|
|
handle_abort_multipart_upload(garage, bucket_id, &key, &upload_id).await
|
|
|
|
}
|
|
|
|
Endpoint::DeleteObject { key, .. } => handle_delete(garage, bucket_id, &key).await,
|
2022-01-11 18:14:14 +00:00
|
|
|
Endpoint::CreateMultipartUpload { key } => {
|
|
|
|
handle_create_multipart_upload(garage, &req, &bucket_name, bucket_id, &key).await
|
2021-12-06 14:17:47 +00:00
|
|
|
}
|
2022-01-11 18:14:14 +00:00
|
|
|
Endpoint::CompleteMultipartUpload { key, upload_id } => {
|
2021-12-14 12:55:11 +00:00
|
|
|
handle_complete_multipart_upload(
|
|
|
|
garage,
|
|
|
|
req,
|
2022-01-11 18:14:14 +00:00
|
|
|
&bucket_name,
|
2021-12-14 12:55:11 +00:00
|
|
|
bucket_id,
|
|
|
|
&key,
|
|
|
|
&upload_id,
|
|
|
|
content_sha256,
|
|
|
|
)
|
|
|
|
.await
|
2021-12-06 14:17:47 +00:00
|
|
|
}
|
2022-01-11 18:14:14 +00:00
|
|
|
Endpoint::CreateBucket {} => unreachable!(),
|
|
|
|
Endpoint::HeadBucket {} => {
|
2021-12-06 14:17:47 +00:00
|
|
|
let empty_body: Body = Body::from(vec![]);
|
|
|
|
let response = Response::builder().body(empty_body).unwrap();
|
|
|
|
Ok(response)
|
|
|
|
}
|
2022-01-11 18:14:14 +00:00
|
|
|
Endpoint::DeleteBucket {} => {
|
2022-01-05 15:23:09 +00:00
|
|
|
handle_delete_bucket(&garage, bucket_id, bucket_name, api_key).await
|
|
|
|
}
|
2022-01-11 18:14:14 +00:00
|
|
|
Endpoint::GetBucketLocation {} => handle_get_bucket_location(garage),
|
|
|
|
Endpoint::GetBucketVersioning {} => handle_get_bucket_versioning(),
|
2021-12-06 14:17:47 +00:00
|
|
|
Endpoint::ListObjects {
|
|
|
|
delimiter,
|
|
|
|
encoding_type,
|
|
|
|
marker,
|
|
|
|
max_keys,
|
|
|
|
prefix,
|
|
|
|
} => {
|
|
|
|
handle_list(
|
|
|
|
garage,
|
|
|
|
&ListObjectsQuery {
|
2022-01-12 18:04:55 +00:00
|
|
|
common: ListQueryCommon {
|
2022-01-11 18:14:14 +00:00
|
|
|
bucket_name,
|
2022-01-12 18:04:55 +00:00
|
|
|
bucket_id,
|
|
|
|
delimiter: delimiter.map(|d| d.to_string()),
|
2022-01-20 20:27:17 +00:00
|
|
|
page_size: max_keys.map(|p| p.clamp(1, 1000)).unwrap_or(1000),
|
2022-01-12 18:04:55 +00:00
|
|
|
prefix: prefix.unwrap_or_default(),
|
|
|
|
urlencode_resp: encoding_type.map(|e| e == "url").unwrap_or(false),
|
|
|
|
},
|
2021-12-06 14:17:47 +00:00
|
|
|
is_v2: false,
|
|
|
|
marker,
|
|
|
|
continuation_token: None,
|
|
|
|
start_after: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
Endpoint::ListObjectsV2 {
|
|
|
|
delimiter,
|
|
|
|
encoding_type,
|
|
|
|
max_keys,
|
|
|
|
prefix,
|
|
|
|
continuation_token,
|
|
|
|
start_after,
|
|
|
|
list_type,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
if list_type == "2" {
|
|
|
|
handle_list(
|
|
|
|
garage,
|
|
|
|
&ListObjectsQuery {
|
2022-01-12 18:04:55 +00:00
|
|
|
common: ListQueryCommon {
|
2022-01-11 18:14:14 +00:00
|
|
|
bucket_name,
|
2022-01-12 18:04:55 +00:00
|
|
|
bucket_id,
|
|
|
|
delimiter: delimiter.map(|d| d.to_string()),
|
2022-01-20 20:27:17 +00:00
|
|
|
page_size: max_keys.map(|p| p.clamp(1, 1000)).unwrap_or(1000),
|
2022-01-12 18:04:55 +00:00
|
|
|
urlencode_resp: encoding_type.map(|e| e == "url").unwrap_or(false),
|
|
|
|
prefix: prefix.unwrap_or_default(),
|
|
|
|
},
|
2021-12-06 14:17:47 +00:00
|
|
|
is_v2: true,
|
|
|
|
marker: None,
|
|
|
|
continuation_token,
|
|
|
|
start_after,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
} else {
|
|
|
|
Err(Error::BadRequest(format!(
|
|
|
|
"Invalid endpoint: list-type={}",
|
|
|
|
list_type
|
|
|
|
)))
|
2020-04-26 20:39:32 +00:00
|
|
|
}
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
2022-01-12 18:04:55 +00:00
|
|
|
Endpoint::ListMultipartUploads {
|
|
|
|
delimiter,
|
|
|
|
encoding_type,
|
|
|
|
key_marker,
|
|
|
|
max_uploads,
|
|
|
|
prefix,
|
|
|
|
upload_id_marker,
|
|
|
|
} => {
|
|
|
|
handle_list_multipart_upload(
|
|
|
|
garage,
|
|
|
|
&ListMultipartUploadsQuery {
|
|
|
|
common: ListQueryCommon {
|
2022-01-11 18:14:14 +00:00
|
|
|
bucket_name,
|
2022-01-12 18:04:55 +00:00
|
|
|
bucket_id,
|
|
|
|
delimiter: delimiter.map(|d| d.to_string()),
|
2022-01-20 20:27:17 +00:00
|
|
|
page_size: max_uploads.map(|p| p.clamp(1, 1000)).unwrap_or(1000),
|
2022-01-12 18:04:55 +00:00
|
|
|
prefix: prefix.unwrap_or_default(),
|
|
|
|
urlencode_resp: encoding_type.map(|e| e == "url").unwrap_or(false),
|
|
|
|
},
|
|
|
|
key_marker,
|
|
|
|
upload_id_marker,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2022-01-19 16:16:00 +00:00
|
|
|
Endpoint::ListParts {
|
|
|
|
key,
|
|
|
|
max_parts,
|
|
|
|
part_number_marker,
|
|
|
|
upload_id,
|
|
|
|
} => {
|
|
|
|
handle_list_parts(
|
|
|
|
garage,
|
|
|
|
&ListPartsQuery {
|
|
|
|
bucket_name,
|
|
|
|
bucket_id,
|
|
|
|
key,
|
|
|
|
upload_id,
|
|
|
|
part_number_marker: part_number_marker.map(|p| p.clamp(1, 10000)),
|
|
|
|
max_parts: max_parts.map(|p| p.clamp(1, 1000)).unwrap_or(1000),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2022-01-11 18:14:14 +00:00
|
|
|
Endpoint::DeleteObjects {} => {
|
2021-12-14 12:55:11 +00:00
|
|
|
handle_delete_objects(garage, bucket_id, req, content_sha256).await
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
2022-01-24 11:03:57 +00:00
|
|
|
Endpoint::GetBucketWebsite {} => handle_get_website(&bucket).await,
|
2022-01-11 18:14:14 +00:00
|
|
|
Endpoint::PutBucketWebsite {} => {
|
2021-12-16 10:47:58 +00:00
|
|
|
handle_put_website(garage, bucket_id, req, content_sha256).await
|
2021-12-15 09:41:39 +00:00
|
|
|
}
|
2022-01-11 18:14:14 +00:00
|
|
|
Endpoint::DeleteBucketWebsite {} => handle_delete_website(garage, bucket_id).await,
|
2022-01-24 11:03:57 +00:00
|
|
|
Endpoint::GetBucketCors {} => handle_get_cors(&bucket).await,
|
2022-01-07 15:23:04 +00:00
|
|
|
Endpoint::PutBucketCors {} => handle_put_cors(garage, bucket_id, req, content_sha256).await,
|
|
|
|
Endpoint::DeleteBucketCors {} => handle_delete_cors(garage, bucket_id).await,
|
2021-12-06 14:17:47 +00:00
|
|
|
endpoint => Err(Error::NotImplemented(endpoint.name().to_owned())),
|
2022-01-07 15:23:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// If request was a success and we have a CORS rule that applies to it,
|
|
|
|
// add the corresponding CORS headers to the response
|
|
|
|
let mut resp_ok = resp?;
|
|
|
|
if let Some(rule) = matching_cors_rule {
|
|
|
|
add_cors_headers(&mut resp_ok, rule)
|
|
|
|
.ok_or_internal_error("Invalid bucket CORS configuration")?;
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
2022-01-07 15:23:04 +00:00
|
|
|
|
|
|
|
Ok(resp_ok)
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2021-12-14 12:55:11 +00:00
|
|
|
async fn handle_request_without_bucket(
|
|
|
|
garage: Arc<Garage>,
|
|
|
|
_req: Request<Body>,
|
|
|
|
api_key: Key,
|
|
|
|
endpoint: Endpoint,
|
|
|
|
) -> Result<Response<Body>, Error> {
|
|
|
|
match endpoint {
|
|
|
|
Endpoint::ListBuckets => handle_list_buckets(&garage, &api_key).await,
|
|
|
|
endpoint => Err(Error::NotImplemented(endpoint.name().to_owned())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::ptr_arg)]
|
2022-01-11 16:31:09 +00:00
|
|
|
pub async fn resolve_bucket(
|
2021-12-14 12:55:11 +00:00
|
|
|
garage: &Garage,
|
|
|
|
bucket_name: &String,
|
|
|
|
api_key: &Key,
|
|
|
|
) -> Result<Uuid, Error> {
|
|
|
|
let api_key_params = api_key
|
|
|
|
.state
|
|
|
|
.as_option()
|
2022-01-05 14:56:48 +00:00
|
|
|
.ok_or_internal_error("Key should not be deleted at this point")?;
|
2021-12-14 12:55:11 +00:00
|
|
|
|
2022-01-03 17:32:15 +00:00
|
|
|
if let Some(Some(bucket_id)) = api_key_params.local_aliases.get(bucket_name) {
|
2021-12-14 12:55:11 +00:00
|
|
|
Ok(*bucket_id)
|
|
|
|
} else {
|
|
|
|
Ok(garage
|
|
|
|
.bucket_helper()
|
|
|
|
.resolve_global_bucket_name(bucket_name)
|
|
|
|
.await?
|
2022-01-05 16:07:36 +00:00
|
|
|
.ok_or(Error::NoSuchBucket)?)
|
2021-12-14 12:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 16:39:36 +00:00
|
|
|
/// Extract the bucket name and the key name from an HTTP path and possibly a bucket provided in
|
|
|
|
/// the host header of the request
|
2020-11-07 12:53:32 +00:00
|
|
|
///
|
|
|
|
/// S3 internally manages only buckets and keys. This function splits
|
|
|
|
/// an HTTP path to get the corresponding bucket name and key.
|
2022-01-11 16:31:09 +00:00
|
|
|
pub fn parse_bucket_key<'a>(
|
2021-11-11 10:26:02 +00:00
|
|
|
path: &'a str,
|
2021-11-11 13:12:22 +00:00
|
|
|
host_bucket: Option<&'a str>,
|
2021-11-11 10:26:02 +00:00
|
|
|
) -> Result<(&'a str, Option<&'a str>), Error> {
|
2020-04-28 10:35:04 +00:00
|
|
|
let path = path.trim_start_matches('/');
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2021-11-11 13:12:22 +00:00
|
|
|
if let Some(bucket) = host_bucket {
|
|
|
|
if !path.is_empty() {
|
|
|
|
return Ok((bucket, Some(path)));
|
|
|
|
} else {
|
|
|
|
return Ok((bucket, None));
|
2021-11-11 10:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:39:44 +00:00
|
|
|
let (bucket, key) = match path.find('/') {
|
2020-05-01 14:30:50 +00:00
|
|
|
Some(i) => {
|
|
|
|
let key = &path[i + 1..];
|
2021-04-23 20:18:00 +00:00
|
|
|
if !key.is_empty() {
|
2020-11-08 12:39:44 +00:00
|
|
|
(&path[..i], Some(key))
|
2020-05-01 14:30:50 +00:00
|
|
|
} else {
|
2020-11-08 12:39:44 +00:00
|
|
|
(&path[..i], None)
|
2020-05-01 14:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-08 12:39:44 +00:00
|
|
|
None => (path, None),
|
|
|
|
};
|
2021-04-23 20:18:00 +00:00
|
|
|
if bucket.is_empty() {
|
|
|
|
return Err(Error::BadRequest("No bucket specified".to_string()));
|
2020-04-28 10:18:14 +00:00
|
|
|
}
|
2020-11-08 12:39:44 +00:00
|
|
|
Ok((bucket, key))
|
2020-04-28 10:18:14 +00:00
|
|
|
}
|
2020-11-07 12:53:32 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2020-11-07 14:34:53 +00:00
|
|
|
fn parse_bucket_containing_a_key() -> Result<(), Error> {
|
2021-11-11 13:12:22 +00:00
|
|
|
let (bucket, key) = parse_bucket_key("/my_bucket/a/super/file.jpg", None)?;
|
2020-11-07 12:53:32 +00:00
|
|
|
assert_eq!(bucket, "my_bucket");
|
2020-11-07 12:59:30 +00:00
|
|
|
assert_eq!(key.expect("key must be set"), "a/super/file.jpg");
|
2020-11-07 12:53:32 +00:00
|
|
|
Ok(())
|
2020-11-08 12:39:44 +00:00
|
|
|
}
|
2020-11-07 14:34:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_bucket_containing_no_key() -> Result<(), Error> {
|
2021-11-11 13:12:22 +00:00
|
|
|
let (bucket, key) = parse_bucket_key("/my_bucket/", None)?;
|
2020-11-07 14:34:53 +00:00
|
|
|
assert_eq!(bucket, "my_bucket");
|
|
|
|
assert!(key.is_none());
|
2021-11-11 13:12:22 +00:00
|
|
|
let (bucket, key) = parse_bucket_key("/my_bucket", None)?;
|
2020-11-07 14:34:53 +00:00
|
|
|
assert_eq!(bucket, "my_bucket");
|
|
|
|
assert!(key.is_none());
|
|
|
|
Ok(())
|
2020-11-07 12:53:32 +00:00
|
|
|
}
|
2020-11-08 12:39:44 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_bucket_containing_no_bucket() {
|
2021-11-11 13:12:22 +00:00
|
|
|
let parsed = parse_bucket_key("", None);
|
2020-11-08 12:39:44 +00:00
|
|
|
assert!(parsed.is_err());
|
2021-11-11 13:12:22 +00:00
|
|
|
let parsed = parse_bucket_key("/", None);
|
2020-11-08 12:39:44 +00:00
|
|
|
assert!(parsed.is_err());
|
2021-11-11 13:12:22 +00:00
|
|
|
let parsed = parse_bucket_key("////", None);
|
2020-11-08 12:39:44 +00:00
|
|
|
assert!(parsed.is_err());
|
|
|
|
}
|
2021-11-11 10:26:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_bucket_with_vhost_and_key() -> Result<(), Error> {
|
2021-11-11 13:12:22 +00:00
|
|
|
let (bucket, key) = parse_bucket_key("/a/super/file.jpg", Some("my-bucket"))?;
|
2021-11-11 10:26:02 +00:00
|
|
|
assert_eq!(bucket, "my-bucket");
|
|
|
|
assert_eq!(key.expect("key must be set"), "a/super/file.jpg");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_bucket_with_vhost_no_key() -> Result<(), Error> {
|
2021-11-11 13:12:22 +00:00
|
|
|
let (bucket, key) = parse_bucket_key("", Some("my-bucket"))?;
|
2021-11-11 10:26:02 +00:00
|
|
|
assert_eq!(bucket, "my-bucket");
|
|
|
|
assert!(key.is_none());
|
2021-11-11 13:12:22 +00:00
|
|
|
let (bucket, key) = parse_bucket_key("/", Some("my-bucket"))?;
|
2021-11-11 10:26:02 +00:00
|
|
|
assert_eq!(bucket, "my-bucket");
|
|
|
|
assert!(key.is_none());
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-11-07 12:53:32 +00:00
|
|
|
}
|