aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/api/Cargo.toml6
-rw-r--r--src/api/api_server.rs140
-rw-r--r--src/api/lib.rs1
-rw-r--r--src/api/signature.rs307
-rw-r--r--src/util/config.rs9
-rw-r--r--src/util/error.rs4
6 files changed, 431 insertions, 36 deletions
diff --git a/src/api/Cargo.toml b/src/api/Cargo.toml
index 3606c1df..0da02338 100644
--- a/src/api/Cargo.toml
+++ b/src/api/Cargo.toml
@@ -17,6 +17,10 @@ garage_core = { path = "../core" }
bytes = "0.4"
hex = "0.3"
log = "0.4"
+chrono = "0.4"
+sha2 = "0.8"
+hmac = "0.7"
+crypto-mac = "0.7"
futures = "0.3"
futures-util = "0.3"
@@ -24,5 +28,7 @@ tokio = { version = "0.2", default-features = false, features = ["rt-core", "rt-
http = "0.2"
hyper = "0.13"
+url = "2.1"
+httpdate = "0.3"
diff --git a/src/api/api_server.rs b/src/api/api_server.rs
index de79ffc2..97cc6b93 100644
--- a/src/api/api_server.rs
+++ b/src/api/api_server.rs
@@ -1,6 +1,7 @@
use std::collections::VecDeque;
use std::net::SocketAddr;
use std::sync::Arc;
+use std::time::{Duration, UNIX_EPOCH};
use futures::future::Future;
use futures::stream::*;
@@ -21,6 +22,7 @@ use garage_core::object_table::*;
use garage_core::version_table::*;
use crate::http_util::*;
+use crate::signature::check_signature;
type BodyType = Box<dyn HttpBody<Data = Bytes, Error = Error> + Send + Unpin>;
@@ -28,7 +30,7 @@ pub async fn run_api_server(
garage: Arc<Garage>,
shutdown_signal: impl Future<Output = ()>,
) -> Result<(), Error> {
- let addr = &garage.config.api_bind_addr;
+ let addr = &garage.config.s3_api.api_bind_addr;
let service = make_service_fn(|conn: &AddrStream| {
let garage = garage.clone();
@@ -55,12 +57,18 @@ async fn handler(
req: Request<Body>,
addr: SocketAddr,
) -> Result<Response<BodyType>, Error> {
- match handler_inner(garage, req, addr).await {
- Ok(x) => Ok(x),
+ info!("{} {} {}", addr, req.method(), req.uri());
+ debug!("{:?}", req);
+ match handler_inner(garage, req).await {
+ Ok(x) => {
+ debug!("{} {:?}", x.status(), x.headers());
+ Ok(x)
+ }
Err(e) => {
let body: BodyType = Box::new(BytesBody::from(format!("{}\n", e)));
let mut http_error = Response::new(body);
*http_error.status_mut() = e.http_status_code();
+ warn!("Response: error {}, {}", e.http_status_code(), e);
Ok(http_error)
}
}
@@ -69,38 +77,59 @@ async fn handler(
async fn handler_inner(
garage: Arc<Garage>,
req: Request<Body>,
- addr: SocketAddr,
) -> Result<Response<BodyType>, Error> {
- info!("{} {} {}", addr, req.method(), req.uri());
-
- let bucket = req
- .headers()
- .get(hyper::header::HOST)
- .map(|x| x.to_str().map_err(Error::from))
- .unwrap_or(Err(Error::BadRequest(format!("Host: header missing"))))?
- .to_lowercase();
- let key = req.uri().path().to_string();
-
- match req.method() {
- &Method::GET => Ok(handle_get(garage, &bucket, &key).await?),
- &Method::PUT => {
- let mime_type = req
- .headers()
- .get(hyper::header::CONTENT_TYPE)
- .map(|x| x.to_str())
- .unwrap_or(Ok("blob"))?
- .to_string();
- let version_uuid =
- handle_put(garage, &mime_type, &bucket, &key, req.into_body()).await?;
- let response = format!("{}\n", hex::encode(version_uuid,));
- Ok(Response::new(Box::new(BytesBody::from(response))))
+ let path = req.uri().path().to_string();
+ let path = path.trim_start_matches('/');
+ let (bucket, key) = match path.find('/') {
+ Some(i) => {
+ let (bucket, key) = path.split_at(i);
+ (bucket, Some(key))
}
- &Method::DELETE => {
- let version_uuid = handle_delete(garage, &bucket, &key).await?;
- let response = format!("{}\n", hex::encode(version_uuid,));
- Ok(Response::new(Box::new(BytesBody::from(response))))
+ None => (path, None),
+ };
+ 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."
+ )));
+ }
+
+ if let Some(key) = key {
+ match req.method() {
+ &Method::HEAD => Ok(handle_head(garage, &bucket, &key).await?),
+ &Method::GET => Ok(handle_get(garage, &bucket, &key).await?),
+ &Method::PUT => {
+ let mime_type = req
+ .headers()
+ .get(hyper::header::CONTENT_TYPE)
+ .map(|x| x.to_str())
+ .unwrap_or(Ok("blob"))?
+ .to_string();
+ let version_uuid =
+ handle_put(garage, &mime_type, &bucket, &key, req.into_body()).await?;
+ let response = format!("{}\n", hex::encode(version_uuid,));
+ Ok(Response::new(Box::new(BytesBody::from(response))))
+ }
+ &Method::DELETE => {
+ let version_uuid = handle_delete(garage, &bucket, &key).await?;
+ let response = format!("{}\n", hex::encode(version_uuid,));
+ Ok(Response::new(Box::new(BytesBody::from(response))))
+ }
+ _ => Err(Error::BadRequest(format!("Invalid method"))),
}
- _ => Err(Error::BadRequest(format!("Invalid method"))),
+ } else {
+ // TODO: listing buckets
+ Err(Error::Forbidden("Unimplemented".into()))
}
}
@@ -280,6 +309,49 @@ async fn handle_delete(garage: Arc<Garage>, bucket: &str, key: &str) -> Result<U
return Ok(version_uuid);
}
+fn object_headers(version: &ObjectVersion) -> http::response::Builder {
+ let date = UNIX_EPOCH + Duration::from_millis(version.timestamp);
+ let date_str = httpdate::fmt_http_date(date);
+
+ Response::builder()
+ .header("Content-Type", version.mime_type.to_string())
+ .header("Content-Length", format!("{}", version.size))
+ .header("Last-Modified", date_str)
+}
+
+async fn handle_head(
+ garage: Arc<Garage>,
+ bucket: &str,
+ key: &str,
+) -> Result<Response<BodyType>, Error> {
+ let object = match garage
+ .object_table
+ .get(&bucket.to_string(), &key.to_string())
+ .await?
+ {
+ None => return Err(Error::NotFound),
+ Some(o) => o,
+ };
+
+ let version = match object
+ .versions()
+ .iter()
+ .rev()
+ .filter(|v| v.is_complete && v.data != ObjectVersionData::DeleteMarker)
+ .next()
+ {
+ Some(v) => v,
+ None => return Err(Error::NotFound),
+ };
+
+ let body: BodyType = Box::new(BytesBody::from(vec![]));
+ let response = object_headers(&version)
+ .status(StatusCode::OK)
+ .body(body)
+ .unwrap();
+ Ok(response)
+}
+
async fn handle_get(
garage: Arc<Garage>,
bucket: &str,
@@ -305,9 +377,7 @@ async fn handle_get(
None => return Err(Error::NotFound),
};
- let resp_builder = Response::builder()
- .header("Content-Type", last_v.mime_type.to_string())
- .status(StatusCode::OK);
+ let resp_builder = object_headers(&last_v).status(StatusCode::OK);
match &last_v.data {
ObjectVersionData::DeleteMarker => Err(Error::NotFound),
diff --git a/src/api/lib.rs b/src/api/lib.rs
index b313d45d..3bcd02f1 100644
--- a/src/api/lib.rs
+++ b/src/api/lib.rs
@@ -3,3 +3,4 @@ extern crate log;
pub mod api_server;
pub mod http_util;
+pub mod signature;
diff --git a/src/api/signature.rs b/src/api/signature.rs
new file mode 100644
index 00000000..2e82269c
--- /dev/null
+++ b/src/api/signature.rs
@@ -0,0 +1,307 @@
+use std::collections::HashMap;
+
+use chrono::{DateTime, Duration, NaiveDateTime, Utc};
+use hmac::{Hmac, Mac};
+use hyper::{Body, Method, Request};
+use sha2::{Digest, Sha256};
+
+use garage_table::*;
+use garage_util::error::Error;
+
+use garage_core::garage::Garage;
+use garage_core::key_table::*;
+
+const SHORT_DATE: &str = "%Y%m%d";
+const LONG_DATETIME: &str = "%Y%m%dT%H%M%SZ";
+
+type HmacSha256 = Hmac<Sha256>;
+
+pub async fn check_signature(garage: &Garage, request: &Request<Body>) -> Result<Key, Error> {
+ let mut headers = HashMap::new();
+ for (key, val) in request.headers() {
+ headers.insert(key.to_string(), val.to_str()?.to_string());
+ }
+ if let Some(query) = request.uri().query() {
+ let query_pairs = url::form_urlencoded::parse(query.as_bytes());
+ for (key, val) in query_pairs {
+ headers.insert(key.to_lowercase(), val.to_string());
+ }
+ }
+
+ let authorization = if let Some(authorization) = headers.get("authorization") {
+ parse_authorization(authorization, &headers)?
+ } else {
+ parse_query_authorization(&headers)?
+ };
+
+ let date = headers
+ .get("x-amz-date")
+ .ok_or(Error::BadRequest("Missing X-Amz-Date field".into()))?;
+ let date: NaiveDateTime = NaiveDateTime::parse_from_str(date, LONG_DATETIME)
+ .map_err(|e| Error::BadRequest(format!("Invalid date: {}", e)))?
+ .into();
+ let date: DateTime<Utc> = DateTime::from_utc(date, Utc);
+
+ if Utc::now() - date > Duration::hours(24) {
+ return Err(Error::BadRequest(format!("Date is too old")));
+ }
+
+ let scope = format!(
+ "{}/{}/s3/aws4_request",
+ date.format(SHORT_DATE),
+ garage.config.s3_api.s3_region
+ );
+ if authorization.scope != scope {
+ return Err(Error::BadRequest(format!(
+ "Invalid scope in authorization field, expected: {}",
+ scope
+ )));
+ }
+
+ let key = garage
+ .key_table
+ .get(&EmptyKey, &authorization.key_id)
+ .await?
+ .filter(|k| !k.deleted)
+ .ok_or(Error::Forbidden(format!(
+ "No such key: {}",
+ authorization.key_id
+ )))?;
+
+ let canonical_request = canonical_request(
+ request.method(),
+ &request.uri().path().to_string(),
+ &canonical_query_string(&request.uri()),
+ &headers,
+ &authorization.signed_headers,
+ &authorization.content_sha256,
+ );
+ let string_to_sign = string_to_sign(&date, &scope, &canonical_request);
+
+ let mut hmac = signing_hmac(
+ &date,
+ &key.secret_key,
+ &garage.config.s3_api.s3_region,
+ "s3",
+ )
+ .map_err(|e| Error::Message(format!("Unable to build signing HMAC: {}", e)))?;
+ hmac.input(string_to_sign.as_bytes());
+ let signature = hex::encode(hmac.result().code());
+
+ if authorization.signature != signature {
+ return Err(Error::Forbidden(format!("Invalid signature")));
+ }
+
+ Ok(key)
+}
+
+struct Authorization {
+ key_id: String,
+ scope: String,
+ signed_headers: String,
+ signature: String,
+ content_sha256: String,
+}
+
+fn parse_authorization(
+ authorization: &str,
+ headers: &HashMap<String, String>,
+) -> Result<Authorization, Error> {
+ let first_space = authorization
+ .find(' ')
+ .ok_or(Error::BadRequest("Authorization field too short".into()))?;
+ let (auth_kind, rest) = authorization.split_at(first_space);
+
+ if auth_kind != "AWS4-HMAC-SHA256" {
+ return Err(Error::BadRequest("Unsupported authorization method".into()));
+ }
+
+ let mut auth_params = HashMap::new();
+ for auth_part in rest.split(',') {
+ let auth_part = auth_part.trim();
+ let eq = auth_part.find('=').ok_or(Error::BadRequest(format!(
+ "Missing =value in authorization field {}",
+ auth_part
+ )))?;
+ let (key, value) = auth_part.split_at(eq);
+ auth_params.insert(key.to_string(), value.trim_start_matches('=').to_string());
+ }
+
+ let cred = auth_params
+ .get("Credential")
+ .ok_or(Error::BadRequest(format!(
+ "Could not find Credential in Authorization field"
+ )))?;
+ let (key_id, scope) = parse_credential(cred)?;
+
+ let content_sha256 = headers
+ .get("x-amz-content-sha256")
+ .ok_or(Error::BadRequest(
+ "Missing X-Amz-Content-Sha256 field".into(),
+ ))?;
+
+ let auth = Authorization {
+ key_id,
+ scope,
+ signed_headers: auth_params
+ .get("SignedHeaders")
+ .ok_or(Error::BadRequest(format!(
+ "Could not find SignedHeaders in Authorization field"
+ )))?
+ .to_string(),
+ signature: auth_params
+ .get("Signature")
+ .ok_or(Error::BadRequest(format!(
+ "Could not find Signature in Authorization field"
+ )))?
+ .to_string(),
+ content_sha256: content_sha256.to_string(),
+ };
+ Ok(auth)
+}
+
+fn parse_query_authorization(headers: &HashMap<String, String>) -> Result<Authorization, Error> {
+ let algo = headers
+ .get("x-amz-algorithm")
+ .ok_or(Error::BadRequest(format!(
+ "X-Amz-Algorithm not found in query parameters"
+ )))?;
+ if algo != "AWS4-HMAC-SHA256" {
+ return Err(Error::BadRequest(format!(
+ "Unsupported authorization method"
+ )));
+ }
+
+ let cred = headers
+ .get("x-amz-credential")
+ .ok_or(Error::BadRequest(format!(
+ "X-Amz-Credential not found in query parameters"
+ )))?;
+ let (key_id, scope) = parse_credential(cred)?;
+ let signed_headers = headers
+ .get("x-amz-signedheaders")
+ .ok_or(Error::BadRequest(format!(
+ "X-Amz-SignedHeaders not found in query parameters"
+ )))?;
+ let signature = headers
+ .get("x-amz-signature")
+ .ok_or(Error::BadRequest(format!(
+ "X-Amz-Signature not found in query parameters"
+ )))?;
+
+ Ok(Authorization {
+ key_id,
+ scope,
+ signed_headers: signed_headers.to_string(),
+ signature: signature.to_string(),
+ content_sha256: "UNSIGNED-PAYLOAD".to_string(),
+ })
+}
+
+fn parse_credential(cred: &str) -> Result<(String, String), Error> {
+ let first_slash = cred.find('/').ok_or(Error::BadRequest(format!(
+ "Credentials does not contain / in authorization field"
+ )))?;
+ let (key_id, scope) = cred.split_at(first_slash);
+ Ok((
+ key_id.to_string(),
+ scope.trim_start_matches('/').to_string(),
+ ))
+}
+
+fn string_to_sign(datetime: &DateTime<Utc>, scope_string: &str, canonical_req: &str) -> String {
+ let mut hasher = Sha256::default();
+ hasher.input(canonical_req.as_bytes());
+ [
+ "AWS4-HMAC-SHA256",
+ &datetime.format(LONG_DATETIME).to_string(),
+ scope_string,
+ &hex::encode(hasher.result().as_slice()),
+ ]
+ .join("\n")
+}
+
+fn signing_hmac(
+ datetime: &DateTime<Utc>,
+ secret_key: &str,
+ region: &str,
+ service: &str,
+) -> Result<HmacSha256, crypto_mac::InvalidKeyLength> {
+ let secret = String::from("AWS4") + secret_key;
+ let mut date_hmac = HmacSha256::new_varkey(secret.as_bytes())?;
+ date_hmac.input(datetime.format(SHORT_DATE).to_string().as_bytes());
+ let mut region_hmac = HmacSha256::new_varkey(&date_hmac.result().code())?;
+ region_hmac.input(region.as_bytes());
+ let mut service_hmac = HmacSha256::new_varkey(&region_hmac.result().code())?;
+ service_hmac.input(service.as_bytes());
+ let mut signing_hmac = HmacSha256::new_varkey(&service_hmac.result().code())?;
+ signing_hmac.input(b"aws4_request");
+ let hmac = HmacSha256::new_varkey(&signing_hmac.result().code())?;
+ Ok(hmac)
+}
+
+fn canonical_request(
+ method: &Method,
+ url_path: &str,
+ canonical_query_string: &str,
+ headers: &HashMap<String, String>,
+ signed_headers: &str,
+ content_sha256: &str,
+) -> String {
+ [
+ method.as_str(),
+ url_path,
+ canonical_query_string,
+ &canonical_header_string(&headers, signed_headers),
+ "",
+ signed_headers,
+ content_sha256,
+ ]
+ .join("\n")
+}
+
+fn canonical_header_string(headers: &HashMap<String, String>, signed_headers: &str) -> String {
+ let signed_headers_vec = signed_headers.split(';').collect::<Vec<_>>();
+ let mut items = headers
+ .iter()
+ .filter(|(key, _)| signed_headers_vec.contains(&key.as_str()))
+ .map(|(key, value)| key.to_lowercase() + ":" + value.trim())
+ .collect::<Vec<_>>();
+ items.sort();
+ items.join("\n")
+}
+
+fn canonical_query_string(uri: &hyper::Uri) -> String {
+ if let Some(query) = uri.query() {
+ let query_pairs = url::form_urlencoded::parse(query.as_bytes());
+ let mut items = query_pairs
+ .filter(|(key, _)| key != "X-Amz-Signature")
+ .map(|(key, value)| uri_encode(&key, true) + "=" + &uri_encode(&value, true))
+ .collect::<Vec<_>>();
+ items.sort();
+ items.join("&")
+ } else {
+ "".to_string()
+ }
+}
+
+fn uri_encode(string: &str, encode_slash: bool) -> String {
+ let mut result = String::with_capacity(string.len() * 2);
+ for c in string.chars() {
+ match c {
+ 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '-' | '~' | '.' => result.push(c),
+ '/' if encode_slash => result.push_str("%2F"),
+ '/' if !encode_slash => result.push('/'),
+ _ => {
+ result.push('%');
+ result.push_str(
+ &format!("{}", c)
+ .bytes()
+ .map(|b| format!("{:02X}", b))
+ .collect::<String>(),
+ );
+ }
+ }
+ }
+ result
+}
diff --git a/src/util/config.rs b/src/util/config.rs
index cb871562..28349530 100644
--- a/src/util/config.rs
+++ b/src/util/config.rs
@@ -11,7 +11,6 @@ pub struct Config {
pub metadata_dir: PathBuf,
pub data_dir: PathBuf,
- pub api_bind_addr: SocketAddr,
pub rpc_bind_addr: SocketAddr,
pub bootstrap_peers: Vec<SocketAddr>,
@@ -32,6 +31,8 @@ pub struct Config {
pub data_replication_factor: usize,
pub rpc_tls: Option<TlsConfig>,
+
+ pub s3_api: ApiConfig,
}
#[derive(Deserialize, Debug, Clone)]
@@ -41,6 +42,12 @@ pub struct TlsConfig {
pub node_key: String,
}
+#[derive(Deserialize, Debug, Clone)]
+pub struct ApiConfig {
+ pub api_bind_addr: SocketAddr,
+ pub s3_region: String,
+}
+
fn default_max_concurrent_rpc_requests() -> usize {
12
}
diff --git a/src/util/error.rs b/src/util/error.rs
index f73d6915..cb99cbbc 100644
--- a/src/util/error.rs
+++ b/src/util/error.rs
@@ -69,6 +69,9 @@ pub enum Error {
#[error(display = "Bad request: {}", _0)]
BadRequest(String),
+ #[error(display = "Forbidden: {}", _0)]
+ Forbidden(String),
+
#[error(display = "Not found")]
NotFound,
@@ -84,6 +87,7 @@ impl Error {
match self {
Error::BadRequest(_) => StatusCode::BAD_REQUEST,
Error::NotFound => StatusCode::NOT_FOUND,
+ Error::Forbidden(_) => StatusCode::FORBIDDEN,
Error::RPC(_) => StatusCode::SERVICE_UNAVAILABLE,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}