From cec08a23af38e387e8f7c7f81ae32f25b4aab86c Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 21 Apr 2022 17:03:29 +0200 Subject: [PATCH] Fix signatures and add basic code that makes a request (and it crashes yeah yeah yeah) --- k2v_test.py | 22 ++++++++++++++++++++++ src/api/generic_server.rs | 12 ++++++------ src/api/k2v/api_server.rs | 20 ++++++++++++-------- src/api/signature/payload.rs | 2 +- src/garage/server.rs | 10 ++++++++++ src/util/config.rs | 14 ++++++++++++-- 6 files changed, 63 insertions(+), 17 deletions(-) create mode 100755 k2v_test.py diff --git a/k2v_test.py b/k2v_test.py new file mode 100755 index 00000000..653c7489 --- /dev/null +++ b/k2v_test.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +import requests + +# let's talk to our AWS Elasticsearch cluster +#from requests_aws4auth import AWS4Auth +#auth = AWS4Auth('GK31c2f218a2e44f485b94239e', +# 'b892c0665f0ada8a4755dae98baa3b133590e11dae3bcc1f9d769d67f16c3835', +# 'us-east-1', +# 's3') + +from aws_requests_auth.aws_auth import AWSRequestsAuth +auth = AWSRequestsAuth(aws_access_key='GK31c2f218a2e44f485b94239e', + aws_secret_access_key='b892c0665f0ada8a4755dae98baa3b133590e11dae3bcc1f9d769d67f16c3835', + aws_host='localhost:3812', + aws_region='us-east-1', + aws_service='k2v') + + +response = requests.get('http://localhost:3812/alex', + auth=auth) +print(response.content) diff --git a/src/api/generic_server.rs b/src/api/generic_server.rs index 1d00e681..d263a94f 100644 --- a/src/api/generic_server.rs +++ b/src/api/generic_server.rs @@ -42,7 +42,7 @@ pub(crate) trait ApiHandler: Send + Sync + 'static { } pub(crate) struct ApiServer { - s3_region: String, + region: String, api_handler: A, // Metrics @@ -52,10 +52,10 @@ pub(crate) struct ApiServer { } impl ApiServer { - pub fn new(s3_region: String, api_handler: A) -> Arc { + pub fn new(region: String, api_handler: A) -> Arc { let meter = global::meter("garage/api"); Arc::new(Self { - s3_region, + region, api_handler, request_counter: meter .u64_counter(format!("api.{}.request_counter", A::API_NAME)) @@ -102,7 +102,7 @@ impl ApiServer { let server = Server::bind(&bind_addr).serve(service); let graceful = server.with_graceful_shutdown(shutdown_signal); - info!("API server listening on http://{}", bind_addr); + info!("{} API server listening on http://{}", A::API_NAME_DISPLAY, bind_addr); graceful.await?; Ok(()) @@ -119,7 +119,7 @@ impl ApiServer { let tracer = opentelemetry::global::tracer("garage"); let span = tracer - .span_builder("S3 API call (unknown)") + .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())), @@ -138,7 +138,7 @@ impl ApiServer { Ok(x) } Err(e) => { - let body: Body = Body::from(e.aws_xml(&self.s3_region, uri.path())); + let body: Body = Body::from(e.aws_xml(&self.region, uri.path())); let mut http_error_builder = Response::builder() .status(e.http_status_code()) .header("Content-Type", "application/xml"); diff --git a/src/api/k2v/api_server.rs b/src/api/k2v/api_server.rs index 7ee85bd9..0de04957 100644 --- a/src/api/k2v/api_server.rs +++ b/src/api/k2v/api_server.rs @@ -38,14 +38,18 @@ impl K2VApiServer { garage: Arc, shutdown_signal: impl Future, ) -> Result<(), GarageError> { - let addr = garage.config.s3_api.api_bind_addr; + if let Some(cfg) = &garage.config.k2v_api { + let bind_addr = cfg.api_bind_addr; - ApiServer::new( - garage.config.s3_api.s3_region.clone(), - K2VApiServer { garage }, - ) - .run_server(addr, shutdown_signal) - .await + ApiServer::new( + garage.config.s3_api.s3_region.clone(), + K2VApiServer { garage }, + ) + .run_server(bind_addr, shutdown_signal) + .await + } else { + Ok(()) + } } } @@ -91,7 +95,7 @@ impl ApiHandler for K2VApiServer { req, &mut content_sha256, &garage.config.s3_api.s3_region, - "s3", + "k2v", )?; let bucket_id = resolve_bucket(&garage, &bucket_name, &api_key).await?; diff --git a/src/api/signature/payload.rs b/src/api/signature/payload.rs index 2c7f2c01..9137dd2d 100644 --- a/src/api/signature/payload.rs +++ b/src/api/signature/payload.rs @@ -308,7 +308,7 @@ pub async fn verify_v4( date, &key_p.secret_key, &garage.config.s3_api.s3_region, - "s3", + service, ) .ok_or_internal_error("Unable to build signing HMAC")?; hmac.update(payload); diff --git a/src/garage/server.rs b/src/garage/server.rs index 647fade6..fd8374dd 100644 --- a/src/garage/server.rs +++ b/src/garage/server.rs @@ -9,6 +9,7 @@ use garage_util::error::Error; use garage_admin::metrics::*; use garage_admin::tracing_setup::*; use garage_api::s3::api_server::S3ApiServer; +use garage_api::k2v::api_server::K2VApiServer; use garage_model::garage::Garage; use garage_web::run_web_server; @@ -62,6 +63,12 @@ pub async fn run_server(config_file: PathBuf) -> Result<(), Error> { wait_from(watch_cancel.clone()), )); + info!("Initializing K2V API server..."); + let k2v_api_server = tokio::spawn(K2VApiServer::run( + garage.clone(), + wait_from(watch_cancel.clone()), + )); + info!("Initializing web server..."); let web_server = tokio::spawn(run_web_server( garage.clone(), @@ -83,6 +90,9 @@ pub async fn run_server(config_file: PathBuf) -> Result<(), Error> { if let Err(e) = s3_api_server.await? { warn!("S3 API server exited with error: {}", e); } + if let Err(e) = k2v_api_server.await? { + warn!("K2V API server exited with error: {}", e); + } if let Err(e) = web_server.await? { warn!("Web server exited with error: {}", e); } diff --git a/src/util/config.rs b/src/util/config.rs index e4d96476..9de0bddb 100644 --- a/src/util/config.rs +++ b/src/util/config.rs @@ -73,7 +73,10 @@ pub struct Config { pub sled_flush_every_ms: u64, /// Configuration for S3 api - pub s3_api: ApiConfig, + pub s3_api: S3ApiConfig, + + /// Configuration for K2V api + pub k2v_api: Option, /// Configuration for serving files as normal web server pub s3_web: WebConfig, @@ -85,7 +88,7 @@ pub struct Config { /// Configuration for S3 api #[derive(Deserialize, Debug, Clone)] -pub struct ApiConfig { +pub struct S3ApiConfig { /// Address and port to bind for api serving pub api_bind_addr: SocketAddr, /// S3 region to use @@ -95,6 +98,13 @@ pub struct ApiConfig { pub root_domain: Option, } +/// Configuration for K2V api +#[derive(Deserialize, Debug, Clone)] +pub struct K2VApiConfig { + /// Address and port to bind for api serving + pub api_bind_addr: SocketAddr, +} + /// Configuration for serving files as normal web server #[derive(Deserialize, Debug, Clone)] pub struct WebConfig {