2023-09-29 16:41:00 +00:00
use std ::fs ::{ self , Permissions } ;
use std ::os ::unix ::prelude ::PermissionsExt ;
use std ::{ convert ::Infallible , sync ::Arc } ;
2020-11-02 14:48:39 +00:00
use futures ::future ::Future ;
2020-11-21 14:15:25 +00:00
use hyper ::{
2024-02-05 17:49:54 +00:00
body ::Incoming as IncomingBody ,
2022-01-06 11:55:49 +00:00
header ::{ HeaderValue , HOST } ,
2024-02-05 17:49:54 +00:00
Method , Request , Response , StatusCode ,
2020-11-21 16:50:19 +00:00
} ;
2020-11-02 14:48:39 +00:00
2024-02-05 17:49:54 +00:00
use tokio ::net ::{ TcpListener , UnixListener } ;
2023-09-29 16:41:00 +00:00
2022-02-22 14:21:06 +00:00
use opentelemetry ::{
global ,
metrics ::{ Counter , ValueRecorder } ,
trace ::{ FutureExt , TraceContextExt , Tracer } ,
Context , KeyValue ,
} ;
2020-11-21 16:50:19 +00:00
use crate ::error ::* ;
2021-12-16 10:47:58 +00:00
2024-02-07 13:33:07 +00:00
use garage_api ::generic_server ::{ server_loop , UnixListenerOn } ;
2024-02-05 17:49:54 +00:00
use garage_api ::helpers ::* ;
2022-05-10 11:16:57 +00:00
use garage_api ::s3 ::cors ::{ add_cors_headers , find_matching_cors_rule , handle_options_for_bucket } ;
2022-05-24 10:16:39 +00:00
use garage_api ::s3 ::error ::{
CommonErrorDerivative , Error as ApiError , OkOrBadRequest , OkOrInternalError ,
} ;
2022-05-10 11:16:57 +00:00
use garage_api ::s3 ::get ::{ handle_get , handle_head } ;
2021-12-16 10:47:58 +00:00
2020-11-02 14:48:39 +00:00
use garage_model ::garage ::Garage ;
2021-12-16 10:47:58 +00:00
2020-12-17 21:51:44 +00:00
use garage_table ::* ;
2023-08-08 13:10:33 +00:00
use garage_util ::data ::Uuid ;
2020-11-19 13:56:00 +00:00
use garage_util ::error ::Error as GarageError ;
2023-02-09 13:50:04 +00:00
use garage_util ::forwarded_headers ;
2022-02-22 14:21:06 +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-02-22 14:21:06 +00:00
struct WebMetrics {
request_counter : Counter < u64 > ,
error_counter : Counter < u64 > ,
request_duration : ValueRecorder < f64 > ,
}
impl WebMetrics {
fn new ( ) -> Self {
let meter = global ::meter ( " garage/web " ) ;
Self {
request_counter : meter
. u64_counter ( " web.request_counter " )
. with_description ( " Number of requests to the web endpoint " )
. init ( ) ,
error_counter : meter
. u64_counter ( " web.error_counter " )
. with_description ( " Number of requests to the web endpoint resulting in errors " )
. init ( ) ,
request_duration : meter
. f64_value_recorder ( " web.request_duration " )
. with_description ( " Duration of requests to the web endpoint " )
. init ( ) ,
}
}
}
2020-11-02 14:48:39 +00:00
2022-09-07 15:54:16 +00:00
pub struct WebServer {
2020-11-02 14:48:39 +00:00
garage : Arc < Garage > ,
2022-09-07 15:54:16 +00:00
metrics : Arc < WebMetrics > ,
root_domain : String ,
}
2020-11-02 14:48:39 +00:00
2022-09-07 15:54:16 +00:00
impl WebServer {
/// Run a web server
2024-02-07 13:33:07 +00:00
pub fn new ( garage : Arc < Garage > , root_domain : String ) -> Arc < Self > {
2022-09-07 15:54:16 +00:00
let metrics = Arc ::new ( WebMetrics ::new ( ) ) ;
2024-02-07 13:33:07 +00:00
Arc ::new ( WebServer {
2022-09-07 15:54:16 +00:00
garage ,
metrics ,
root_domain ,
2024-02-07 13:33:07 +00:00
} )
}
2022-09-07 15:54:16 +00:00
2024-02-07 13:33:07 +00:00
pub async fn run (
self : Arc < Self > ,
bind_addr : UnixOrTCPSocketAddress ,
shutdown_signal : impl Future < Output = ( ) > ,
) -> Result < ( ) , GarageError > {
2024-02-05 17:49:54 +00:00
info! ( " Web server listening on {} " , bind_addr ) ;
2023-09-29 16:41:00 +00:00
2024-02-05 17:49:54 +00:00
match bind_addr {
UnixOrTCPSocketAddress ::TCPSocket ( addr ) = > {
let listener = TcpListener ::bind ( addr ) . await ? ;
2022-02-22 14:21:06 +00:00
2024-02-07 13:33:07 +00:00
let handler =
move | stream , socketaddr | self . clone ( ) . handle_request ( stream , socketaddr ) ;
server_loop ( listener , handler , shutdown_signal ) . await
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 ) ? ;
2024-02-07 13:33:07 +00:00
let listener = UnixListenerOn ( listener , path . display ( ) . to_string ( ) ) ;
2023-09-29 16:41:00 +00:00
fs ::set_permissions ( path , Permissions ::from_mode ( 0o222 ) ) ? ;
2024-02-07 13:33:07 +00:00
let handler =
move | stream , socketaddr | self . clone ( ) . handle_request ( stream , socketaddr ) ;
server_loop ( listener , handler , shutdown_signal ) . await
2024-02-05 17:49:54 +00:00
}
2024-02-07 13:33:07 +00:00
}
2024-02-05 17:49:54 +00:00
}
2022-09-07 15:54:16 +00:00
async fn handle_request (
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-07 13:33:07 +00:00
) -> Result < Response < BoxBody < Error > > , http ::Error > {
2023-02-09 13:50:04 +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:50:04 +00:00
{
info! (
" {} (via {}) {} {} " ,
forwarded_for_ip_addr ,
addr ,
req . method ( ) ,
req . uri ( )
) ;
} else {
info! ( " {} {} {} " , addr , req . method ( ) , req . uri ( ) ) ;
}
2022-09-07 15:54:16 +00:00
// Lots of instrumentation
let tracer = opentelemetry ::global ::tracer ( " garage " ) ;
let span = tracer
. span_builder ( format! ( " Web {} request " , req . method ( ) ) )
. 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 metrics_tags = & [ KeyValue ::new ( " method " , req . method ( ) . to_string ( ) ) ] ;
// The actual handler
let res = self
. serve_file ( & req )
. with_context ( Context ::current_with_span ( span ) )
. record_duration ( & self . metrics . request_duration , & metrics_tags [ .. ] )
. await ;
// More instrumentation
self . metrics . request_counter . add ( 1 , & metrics_tags [ .. ] ) ;
// Returning the result
match res {
Ok ( res ) = > {
debug! ( " {} {} {} " , req . method ( ) , res . status ( ) , req . uri ( ) ) ;
2024-02-05 17:49:54 +00:00
Ok ( res
. map ( | body | BoxBody ::new ( http_body_util ::BodyExt ::map_err ( body , Error ::from ) ) ) )
2022-09-07 15:54:16 +00:00
}
Err ( error ) = > {
info! (
" {} {} {} {} " ,
req . method ( ) ,
error . http_status_code ( ) ,
req . uri ( ) ,
error
) ;
self . metrics . error_counter . add (
1 ,
& [
metrics_tags [ 0 ] . clone ( ) ,
KeyValue ::new ( " status_code " , error . http_status_code ( ) . to_string ( ) ) ,
] ,
) ;
Ok ( error_to_res ( error ) )
}
2020-11-02 14:48:39 +00:00
}
2022-09-07 15:54:16 +00:00
}
2020-11-02 14:48:39 +00:00
2023-08-28 10:05:14 +00:00
async fn check_key_exists ( self : & Arc < Self > , bucket_id : Uuid , key : & str ) -> Result < bool , Error > {
let exists = self
. garage
2023-08-08 13:10:33 +00:00
. object_table
. get ( & bucket_id , & key . to_string ( ) )
2023-08-28 10:05:14 +00:00
. await ?
. map ( | object | object . versions ( ) . iter ( ) . any ( | v | v . is_data ( ) ) )
. unwrap_or ( false ) ;
Ok ( exists )
2023-08-08 13:10:33 +00:00
}
2024-02-05 17:49:54 +00:00
async fn serve_file (
self : & Arc < Self > ,
req : & Request < IncomingBody > ,
) -> Result < Response < BoxBody < ApiError > > , Error > {
2022-09-07 15:54:16 +00:00
// Get http authority string (eg. [::1]:3902 or garage.tld:80)
let authority = req
. headers ( )
. get ( HOST )
. ok_or_bad_request ( " HOST header required " ) ?
. to_str ( ) ? ;
// Get bucket
let host = authority_to_host ( authority ) ? ;
let bucket_name = host_to_bucket ( & host , & self . root_domain ) . unwrap_or ( & host ) ;
let bucket_id = self
. garage
. bucket_alias_table
. get ( & EmptyKey , & bucket_name . to_string ( ) )
. await ?
. and_then ( | x | x . state . take ( ) )
. ok_or ( Error ::NotFound ) ? ;
// Check bucket isn't deleted and has website access enabled
let bucket = self
. garage
. bucket_table
. get ( & EmptyKey , & bucket_id )
. await ?
. ok_or ( Error ::NotFound ) ? ;
let website_config = bucket
. params ( )
. ok_or ( Error ::NotFound ) ?
. website_config
. get ( )
. as_ref ( )
. ok_or ( Error ::NotFound ) ? ;
// Get path
let path = req . uri ( ) . path ( ) . to_string ( ) ;
let index = & website_config . index_document ;
2023-08-08 13:10:33 +00:00
let ( key , may_redirect ) = path_to_keys ( & path , index ) ? ;
2022-09-07 15:54:16 +00:00
debug! (
2023-08-08 13:10:33 +00:00
" Selected bucket: \" {} \" {:?}, target key: \" {} \" , may redirect to: {:?} " ,
bucket_name , bucket_id , key , may_redirect
2022-09-07 15:54:16 +00:00
) ;
let ret_doc = match * req . method ( ) {
2024-02-05 18:27:12 +00:00
Method ::OPTIONS = > handle_options_for_bucket ( req , & bucket )
. map_err ( ApiError ::from )
. map ( | res | res . map ( | _empty_body : EmptyBody | empty_body ( ) ) ) ,
2024-02-05 17:49:54 +00:00
Method ::HEAD = > handle_head ( self . garage . clone ( ) , & req , bucket_id , & key , None ) . await ,
Method ::GET = > handle_get ( self . garage . clone ( ) , & req , bucket_id , & key , None ) . await ,
2022-09-07 15:54:16 +00:00
_ = > Err ( ApiError ::bad_request ( " HTTP method not supported " ) ) ,
2023-08-08 13:10:33 +00:00
} ;
// Try implicit redirect on error
let ret_doc_with_redir = match ( & ret_doc , may_redirect ) {
( Err ( ApiError ::NoSuchKey ) , ImplicitRedirect ::To { key , url } )
2023-08-28 10:05:14 +00:00
if self . check_key_exists ( bucket_id , key . as_str ( ) ) . await ? = >
2023-08-08 13:10:33 +00:00
{
Ok ( Response ::builder ( )
. status ( StatusCode ::FOUND )
. header ( " Location " , url )
2024-02-05 17:49:54 +00:00
. body ( empty_body ( ) )
2023-08-08 13:10:33 +00:00
. unwrap ( ) )
}
_ = > ret_doc ,
} ;
2022-09-07 15:54:16 +00:00
2023-08-08 13:10:33 +00:00
match ret_doc_with_redir . map_err ( Error ::from ) {
2022-09-07 15:54:16 +00:00
Err ( error ) = > {
// For a HEAD or OPTIONS method, and for non-4xx errors,
// we don't return the error document as content,
// we return above and just return the error message
// by relying on err_to_res that is called when we return an Err.
if * req . method ( ) = = Method ::HEAD
| | * req . method ( ) = = Method ::OPTIONS
| | ! error . http_status_code ( ) . is_client_error ( )
{
return Err ( error ) ;
}
2020-11-02 14:48:39 +00:00
2022-09-07 15:54:16 +00:00
// If no error document is set: just return the error directly
let error_document = match & website_config . error_document {
Some ( ed ) = > ed . trim_start_matches ( '/' ) . to_owned ( ) ,
None = > return Err ( error ) ,
} ;
// We want to return the error document
// Create a fake HTTP request with path = the error document
let req2 = Request ::builder ( )
. uri ( format! ( " http:// {} / {} " , host , & error_document ) )
2024-02-05 17:49:54 +00:00
. body ( empty_body ::< Infallible > ( ) )
2022-09-07 15:54:16 +00:00
. unwrap ( ) ;
match handle_get ( self . garage . clone ( ) , & req2 , bucket_id , & error_document , None ) . await
{
Ok ( mut error_doc ) = > {
// The error won't be logged back in handle_request,
// so log it here
info! (
" {} {} {} {} " ,
req . method ( ) ,
req . uri ( ) ,
error . http_status_code ( ) ,
error
) ;
* error_doc . status_mut ( ) = error . http_status_code ( ) ;
// Preserve error message in a special header
for error_line in error . to_string ( ) . split ( '\n' ) {
if let Ok ( v ) = HeaderValue ::from_bytes ( error_line . as_bytes ( ) ) {
error_doc . headers_mut ( ) . append ( " X-Garage-Error " , v ) ;
}
}
2020-11-08 14:47:25 +00:00
2022-09-07 15:54:16 +00:00
Ok ( error_doc )
}
Err ( error_doc_error ) = > {
warn! (
" Couldn't get error document {} for bucket {:?}: {} " ,
error_document , bucket_id , error_doc_error
) ;
Err ( error )
}
}
}
Ok ( mut resp ) = > {
// Maybe add CORS headers
if let Some ( rule ) = find_matching_cors_rule ( & bucket , req ) ? {
add_cors_headers ( & mut resp , rule )
. ok_or_internal_error ( " Invalid bucket CORS configuration " ) ? ;
}
Ok ( resp )
}
2022-01-06 11:55:49 +00:00
}
2020-11-11 20:17:34 +00:00
}
}
2024-02-05 17:49:54 +00:00
fn error_to_res ( e : Error ) -> Response < BoxBody < Error > > {
2022-01-06 11:55:49 +00:00
// If we are here, it is either that:
// - there was an error before trying to get the requested URL
// from the bucket (e.g. bucket not found)
// - there was an error processing the request and (the request
// was a HEAD request or we couldn't get the error document)
// We do NOT enter this code path when returning the bucket's
// error document (this is handled in serve_file)
2024-02-05 17:49:54 +00:00
let body = string_body ( format! ( " {} \n " , e ) ) ;
2020-11-11 20:17:34 +00:00
let mut http_error = Response ::new ( body ) ;
* http_error . status_mut ( ) = e . http_status_code ( ) ;
2021-11-29 10:52:42 +00:00
e . add_headers ( http_error . headers_mut ( ) ) ;
2020-11-11 20:17:34 +00:00
http_error
}
2023-08-08 13:10:33 +00:00
#[ derive(Debug, PartialEq) ]
enum ImplicitRedirect {
No ,
To { key : String , url : String } ,
}
2020-11-11 18:48:01 +00:00
/// Path to key
///
/// Convert the provided path to the internal key
/// When a path ends with "/", we append the index name to match traditional web server behavior
/// which is also AWS S3 behavior.
2023-08-08 13:10:33 +00:00
///
/// Check: https://docs.aws.amazon.com/AmazonS3/latest/userguide/IndexDocumentSupport.html
fn path_to_keys < ' a > ( path : & ' a str , index : & str ) -> Result < ( String , ImplicitRedirect ) , Error > {
2021-10-26 08:20:05 +00:00
let path_utf8 = percent_encoding ::percent_decode_str ( path ) . decode_utf8 ( ) ? ;
2020-11-21 16:50:19 +00:00
2023-08-28 10:05:14 +00:00
let base_key = match path_utf8 . strip_prefix ( " / " ) {
Some ( bk ) = > bk ,
None = > return Err ( Error ::BadRequest ( " Path must start with a / (slash) " . into ( ) ) ) ,
} ;
2023-08-08 13:10:33 +00:00
let is_bucket_root = base_key . len ( ) = = 0 ;
2023-08-28 10:05:14 +00:00
let is_trailing_slash = path_utf8 . ends_with ( " / " ) ;
2023-08-08 13:10:33 +00:00
match ( is_bucket_root , is_trailing_slash ) {
// It is not possible to store something at the root of the bucket (ie. empty key),
// the only option is to fetch the index
( true , _ ) = > Ok ( ( index . to_string ( ) , ImplicitRedirect ::No ) ) ,
// "If you create a folder structure in your bucket, you must have an index document at each level. In each folder, the index document must have the same name, for example, index.html. When a user specifies a URL that resembles a folder lookup, the presence or absence of a trailing slash determines the behavior of the website. For example, the following URL, with a trailing slash, returns the photos/index.html index document."
( false , true ) = > Ok ( ( format! ( " {base_key} {index} " ) , ImplicitRedirect ::No ) ) ,
// "However, if you exclude the trailing slash from the preceding URL, Amazon S3 first looks for an object photos in the bucket. If the photos object is not found, it searches for an index document, photos/index.html. If that document is found, Amazon S3 returns a 302 Found message and points to the photos/ key. For subsequent requests to photos/, Amazon S3 returns photos/index.html. If the index document is not found, Amazon S3 returns an error."
( false , false ) = > Ok ( (
base_key . to_string ( ) ,
ImplicitRedirect ::To {
key : format ! ( " {base_key}/{index} " ) ,
url : format ! ( " {path}/ " ) ,
} ,
) ) ,
2020-11-11 18:48:01 +00:00
}
}
2020-11-08 14:47:25 +00:00
#[ cfg(test) ]
mod tests {
use super ::* ;
2020-11-11 18:48:01 +00:00
#[ test ]
2023-08-08 13:10:33 +00:00
fn path_to_keys_test ( ) -> Result < ( ) , Error > {
assert_eq! (
path_to_keys ( " /file%20.jpg " , " index.html " ) ? ,
(
" file .jpg " . to_string ( ) ,
ImplicitRedirect ::To {
key : " file .jpg/index.html " . to_string ( ) ,
url : " /file%20.jpg/ " . to_string ( )
}
)
) ;
assert_eq! (
path_to_keys ( " /%20t/ " , " index.html " ) ? ,
( " t/index.html " . to_string ( ) , ImplicitRedirect ::No )
) ;
assert_eq! (
path_to_keys ( " / " , " index.html " ) ? ,
( " index.html " . to_string ( ) , ImplicitRedirect ::No )
) ;
assert_eq! (
path_to_keys ( " /hello " , " index.html " ) ? ,
(
" hello " . to_string ( ) ,
ImplicitRedirect ::To {
key : " hello/index.html " . to_string ( ) ,
url : " /hello/ " . to_string ( )
}
)
) ;
assert! ( path_to_keys ( " " , " index.html " ) . is_err ( ) ) ;
assert! ( path_to_keys ( " i/am/relative " , " index.html " ) . is_err ( ) ) ;
2020-11-11 18:48:01 +00:00
Ok ( ( ) )
}
2020-11-08 14:47:25 +00:00
}