2020-04-24 18:47:11 +00:00
|
|
|
use std::collections::HashMap;
|
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
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
use futures::future::Future;
|
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};
|
2020-04-24 18:47:11 +00:00
|
|
|
use hyper::{Body, Method, Request, Response, Server};
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-24 10:10:01 +00:00
|
|
|
use garage_util::error::Error;
|
2020-04-23 17:05:46 +00:00
|
|
|
|
2020-07-07 11:59:22 +00:00
|
|
|
use garage_model::garage::Garage;
|
2020-04-23 17:05:46 +00:00
|
|
|
|
2020-04-24 10:10:01 +00:00
|
|
|
use crate::http_util::*;
|
2020-04-24 17:46:52 +00:00
|
|
|
use crate::signature::check_signature;
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-28 10:18:14 +00:00
|
|
|
use crate::s3_copy::*;
|
|
|
|
use crate::s3_delete::*;
|
2020-04-26 20:39:32 +00:00
|
|
|
use crate::s3_get::*;
|
|
|
|
use crate::s3_list::*;
|
|
|
|
use crate::s3_put::*;
|
2020-04-10 20:01:48 +00:00
|
|
|
|
|
|
|
pub async fn run_api_server(
|
|
|
|
garage: Arc<Garage>,
|
|
|
|
shutdown_signal: impl Future<Output = ()>,
|
2020-04-11 16:51:11 +00:00
|
|
|
) -> Result<(), Error> {
|
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
|
|
|
|
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();
|
2020-04-07 22:39:07 +00:00
|
|
|
let client_addr = conn.remote_addr();
|
2020-04-06 17:55:39 +00:00
|
|
|
async move {
|
|
|
|
Ok::<_, Error>(service_fn(move |req: Request<Body>| {
|
2020-04-08 20:00:41 +00:00
|
|
|
let garage = garage.clone();
|
|
|
|
handler(garage, req, client_addr)
|
2020-04-06 17:55:39 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
});
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-10 20:01:48 +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>,
|
|
|
|
req: Request<Body>,
|
|
|
|
addr: SocketAddr,
|
|
|
|
) -> Result<Response<BodyType>, Error> {
|
2020-04-24 17:46:52 +00:00
|
|
|
info!("{} {} {}", addr, req.method(), req.uri());
|
|
|
|
debug!("{:?}", req);
|
|
|
|
match handler_inner(garage, req).await {
|
|
|
|
Ok(x) => {
|
|
|
|
debug!("{} {:?}", x.status(), x.headers());
|
|
|
|
Ok(x)
|
|
|
|
}
|
2020-04-07 22:39:07 +00:00
|
|
|
Err(e) => {
|
2020-04-10 20:01:48 +00:00
|
|
|
let body: BodyType = Box::new(BytesBody::from(format!("{}\n", e)));
|
|
|
|
let mut http_error = Response::new(body);
|
2020-04-09 15:32:28 +00:00
|
|
|
*http_error.status_mut() = e.http_status_code();
|
2020-04-24 17:46:52 +00:00
|
|
|
warn!("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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
async fn handler_inner(
|
|
|
|
garage: Arc<Garage>,
|
|
|
|
req: Request<Body>,
|
|
|
|
) -> Result<Response<BodyType>, Error> {
|
2020-04-24 17:46:52 +00:00
|
|
|
let path = req.uri().path().to_string();
|
2020-05-04 13:09:23 +00:00
|
|
|
let path = percent_encoding::percent_decode_str(&path).decode_utf8()?;
|
2020-05-01 14:30:50 +00:00
|
|
|
|
|
|
|
let (bucket, key) = parse_bucket_key(&path)?;
|
2020-04-24 17:46:52 +00:00
|
|
|
if bucket.len() == 0 {
|
|
|
|
return Err(Error::Forbidden(format!(
|
|
|
|
"Operations on buckets not allowed"
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let api_key = check_signature(&garage, &req).await?;
|
|
|
|
let allowed = match req.method() {
|
|
|
|
&Method::HEAD | &Method::GET => api_key.allow_read(&bucket),
|
|
|
|
_ => api_key.allow_write(&bucket),
|
|
|
|
};
|
|
|
|
if !allowed {
|
|
|
|
return Err(Error::Forbidden(format!(
|
|
|
|
"Operation is not allowed for this key."
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2020-04-26 20:39:32 +00:00
|
|
|
let mut params = HashMap::new();
|
|
|
|
if let Some(query) = req.uri().query() {
|
|
|
|
let query_pairs = url::form_urlencoded::parse(query.as_bytes());
|
|
|
|
for (key, val) in query_pairs {
|
|
|
|
params.insert(key.to_lowercase(), val.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:46:52 +00:00
|
|
|
if let Some(key) = key {
|
|
|
|
match req.method() {
|
2020-04-26 20:39:32 +00:00
|
|
|
&Method::HEAD => {
|
|
|
|
// HeadObject query
|
|
|
|
Ok(handle_head(garage, &bucket, &key).await?)
|
|
|
|
}
|
|
|
|
&Method::GET => {
|
|
|
|
// GetObject query
|
2020-05-04 13:09:23 +00:00
|
|
|
Ok(handle_get(garage, &req, &bucket, &key).await?)
|
2020-04-26 20:39:32 +00:00
|
|
|
}
|
2020-04-24 17:46:52 +00:00
|
|
|
&Method::PUT => {
|
2020-04-28 10:18:14 +00:00
|
|
|
if params.contains_key(&"partnumber".to_string())
|
|
|
|
&& params.contains_key(&"uploadid".to_string())
|
2020-04-26 20:39:32 +00:00
|
|
|
{
|
2020-04-26 20:46:21 +00:00
|
|
|
// UploadPart query
|
2020-04-26 20:39:32 +00:00
|
|
|
let part_number = params.get("partnumber").unwrap();
|
|
|
|
let upload_id = params.get("uploadid").unwrap();
|
|
|
|
Ok(handle_put_part(garage, req, &bucket, &key, part_number, upload_id).await?)
|
2020-04-28 10:18:14 +00:00
|
|
|
} else if req.headers().contains_key("x-amz-copy-source") {
|
|
|
|
// CopyObject query
|
|
|
|
let copy_source = req.headers().get("x-amz-copy-source").unwrap().to_str()?;
|
2020-05-04 13:09:23 +00:00
|
|
|
let copy_source =
|
|
|
|
percent_encoding::percent_decode_str(©_source).decode_utf8()?;
|
2020-05-01 14:30:50 +00:00
|
|
|
let (source_bucket, source_key) = parse_bucket_key(©_source)?;
|
2020-04-28 10:18:14 +00:00
|
|
|
if !api_key.allow_read(&source_bucket) {
|
|
|
|
return Err(Error::Forbidden(format!(
|
|
|
|
"Reading from bucket {} not allowed for this key",
|
|
|
|
source_bucket
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
let source_key = match source_key {
|
|
|
|
None => return Err(Error::BadRequest(format!("No source key specified"))),
|
|
|
|
Some(x) => x,
|
|
|
|
};
|
|
|
|
Ok(handle_copy(garage, &bucket, &key, &source_bucket, &source_key).await?)
|
2020-04-26 20:39:32 +00:00
|
|
|
} else {
|
|
|
|
// PutObject query
|
|
|
|
Ok(handle_put(garage, req, &bucket, &key).await?)
|
|
|
|
}
|
2020-04-24 17:46:52 +00:00
|
|
|
}
|
|
|
|
&Method::DELETE => {
|
2020-04-26 20:46:21 +00:00
|
|
|
if params.contains_key(&"uploadid".to_string()) {
|
|
|
|
// AbortMultipartUpload query
|
|
|
|
let upload_id = params.get("uploadid").unwrap();
|
|
|
|
Ok(handle_abort_multipart_upload(garage, &bucket, &key, upload_id).await?)
|
|
|
|
} else {
|
|
|
|
// DeleteObject query
|
2020-05-01 15:52:35 +00:00
|
|
|
Ok(handle_delete(garage, &bucket, &key).await?)
|
2020-04-26 20:46:21 +00:00
|
|
|
}
|
2020-04-24 17:46:52 +00:00
|
|
|
}
|
2020-04-26 20:39:32 +00:00
|
|
|
&Method::POST => {
|
|
|
|
if params.contains_key(&"uploads".to_string()) {
|
|
|
|
// CreateMultipartUpload call
|
|
|
|
Ok(handle_create_multipart_upload(garage, &req, &bucket, &key).await?)
|
|
|
|
} else if params.contains_key(&"uploadid".to_string()) {
|
2020-04-26 20:46:21 +00:00
|
|
|
// CompleteMultipartUpload call
|
2020-04-26 20:39:32 +00:00
|
|
|
let upload_id = params.get("uploadid").unwrap();
|
2020-04-28 10:18:14 +00:00
|
|
|
Ok(
|
|
|
|
handle_complete_multipart_upload(garage, req, &bucket, &key, upload_id)
|
|
|
|
.await?,
|
|
|
|
)
|
2020-04-26 20:39:32 +00:00
|
|
|
} else {
|
|
|
|
Err(Error::BadRequest(format!(
|
|
|
|
"Not a CreateMultipartUpload call, what is it?"
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 17:46:52 +00:00
|
|
|
_ => Err(Error::BadRequest(format!("Invalid method"))),
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
2020-04-24 17:46:52 +00:00
|
|
|
} else {
|
2020-04-24 18:47:11 +00:00
|
|
|
match req.method() {
|
2020-05-01 14:30:50 +00:00
|
|
|
&Method::PUT => {
|
|
|
|
// CreateBucket
|
2020-04-26 16:22:33 +00:00
|
|
|
// If we're here, the bucket already exists, so just answer ok
|
2020-05-01 14:30:50 +00:00
|
|
|
println!(
|
|
|
|
"Body: {}",
|
|
|
|
std::str::from_utf8(&hyper::body::to_bytes(req.into_body()).await?)
|
|
|
|
.unwrap_or("<invalid utf8>")
|
|
|
|
);
|
2020-04-26 16:22:33 +00:00
|
|
|
let empty_body: BodyType = Box::new(BytesBody::from(vec![]));
|
|
|
|
let response = Response::builder()
|
|
|
|
.header("Location", format!("/{}", bucket))
|
|
|
|
.body(empty_body)
|
|
|
|
.unwrap();
|
|
|
|
Ok(response)
|
2020-04-26 18:55:13 +00:00
|
|
|
}
|
2020-05-01 14:30:50 +00:00
|
|
|
&Method::HEAD => {
|
|
|
|
// HeadBucket
|
|
|
|
let empty_body: BodyType = Box::new(BytesBody::from(vec![]));
|
|
|
|
let response = Response::builder().body(empty_body).unwrap();
|
|
|
|
Ok(response)
|
|
|
|
}
|
2020-04-26 20:39:32 +00:00
|
|
|
&Method::DELETE => {
|
|
|
|
// DeleteBucket query
|
|
|
|
Err(Error::Forbidden(
|
|
|
|
"Cannot delete buckets using S3 api, please talk to Garage directly".into(),
|
|
|
|
))
|
|
|
|
}
|
2020-04-24 18:47:11 +00:00
|
|
|
&Method::GET => {
|
2020-05-01 14:30:50 +00:00
|
|
|
// ListObjects query
|
|
|
|
let delimiter = params.get("delimiter").map(|x| x.as_str()).unwrap_or(&"");
|
|
|
|
let max_keys = params
|
|
|
|
.get("max-keys")
|
|
|
|
.map(|x| {
|
|
|
|
x.parse::<usize>().map_err(|e| {
|
|
|
|
Error::BadRequest(format!("Invalid value for max-keys: {}", e))
|
2020-04-24 18:47:11 +00:00
|
|
|
})
|
2020-05-01 14:30:50 +00:00
|
|
|
})
|
|
|
|
.unwrap_or(Ok(1000))?;
|
|
|
|
let prefix = params.get("prefix").map(|x| x.as_str()).unwrap_or(&"");
|
|
|
|
let urlencode_resp = params
|
|
|
|
.get("encoding-type")
|
|
|
|
.map(|x| x == "url")
|
|
|
|
.unwrap_or(false);
|
|
|
|
let marker = params.get("marker").map(String::as_str);
|
|
|
|
Ok(handle_list(
|
|
|
|
garage,
|
|
|
|
bucket,
|
|
|
|
delimiter,
|
|
|
|
max_keys,
|
|
|
|
prefix,
|
|
|
|
marker,
|
|
|
|
urlencode_resp,
|
|
|
|
)
|
|
|
|
.await?)
|
2020-04-21 14:33:12 +00:00
|
|
|
}
|
2020-05-04 13:09:23 +00:00
|
|
|
&Method::POST => {
|
|
|
|
if params.contains_key(&"delete".to_string()) {
|
|
|
|
// DeleteObjects
|
|
|
|
Ok(handle_delete_objects(garage, bucket, req).await?)
|
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"Body: {}",
|
|
|
|
std::str::from_utf8(&hyper::body::to_bytes(req.into_body()).await?)
|
|
|
|
.unwrap_or("<invalid utf8>")
|
|
|
|
);
|
|
|
|
Err(Error::BadRequest(format!("Unsupported call")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Err(Error::BadRequest(format!("Invalid method"))),
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 10:18:14 +00:00
|
|
|
|
|
|
|
fn parse_bucket_key(path: &str) -> Result<(&str, Option<&str>), Error> {
|
2020-04-28 10:35:04 +00:00
|
|
|
let path = path.trim_start_matches('/');
|
2020-04-28 10:18:14 +00:00
|
|
|
|
|
|
|
match path.find('/') {
|
2020-05-01 14:30:50 +00:00
|
|
|
Some(i) => {
|
|
|
|
let key = &path[i + 1..];
|
|
|
|
if key.len() > 0 {
|
|
|
|
Ok((&path[..i], Some(key)))
|
|
|
|
} else {
|
|
|
|
Ok((&path[..i], None))
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 10:18:14 +00:00
|
|
|
None => Ok((path, None)),
|
|
|
|
}
|
|
|
|
}
|