diff options
author | Alex Auvolat <lx@deuxfleurs.fr> | 2025-01-28 18:15:36 +0100 |
---|---|---|
committer | Alex Auvolat <lx@deuxfleurs.fr> | 2025-01-29 19:26:16 +0100 |
commit | 4cb45bd398afd7966cec5d4dfa4dd325c114f93c (patch) | |
tree | 26373ab1007c21ff6fe810b64f74559710e115ad /src/api/admin | |
parent | d5ad797ad762dee4fc1244ad15fbee208ae58480 (diff) | |
download | garage-4cb45bd398afd7966cec5d4dfa4dd325c114f93c.tar.gz garage-4cb45bd398afd7966cec5d4dfa4dd325c114f93c.zip |
admin api: fix CORS to work in browser
Diffstat (limited to 'src/api/admin')
-rw-r--r-- | src/api/admin/api_server.rs | 9 | ||||
-rw-r--r-- | src/api/admin/router_v2.rs | 1 | ||||
-rw-r--r-- | src/api/admin/special.rs | 11 |
3 files changed, 15 insertions, 6 deletions
diff --git a/src/api/admin/api_server.rs b/src/api/admin/api_server.rs index 82337b7e..92da3245 100644 --- a/src/api/admin/api_server.rs +++ b/src/api/admin/api_server.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use argon2::password_hash::PasswordHash; use async_trait::async_trait; -use http::header::AUTHORIZATION; +use http::header::{HeaderValue, ACCESS_CONTROL_ALLOW_ORIGIN, AUTHORIZATION}; use hyper::{body::Incoming as IncomingBody, Request, Response, StatusCode}; use tokio::sync::watch; @@ -134,6 +134,8 @@ impl ApiHandler for AdminApiServer { Endpoint::New(_) => AdminApiRequest::from_request(req).await?, }; + info!("Admin request: {}", request.name()); + let required_auth_hash = match request.authorization_type() { Authorization::None => None, @@ -162,7 +164,10 @@ impl ApiHandler for AdminApiServer { AdminApiRequest::Metrics(_req) => self.handle_metrics(), req => { let res = req.handle(&self.garage).await?; - json_ok_response(&res) + let mut res = json_ok_response(&res)?; + res.headers_mut() + .insert(ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static("*")); + Ok(res) } } } diff --git a/src/api/admin/router_v2.rs b/src/api/admin/router_v2.rs index dacf6793..c7a5e316 100644 --- a/src/api/admin/router_v2.rs +++ b/src/api/admin/router_v2.rs @@ -219,6 +219,7 @@ impl AdminApiRequest { /// Get the kind of authorization which is required to perform the operation. pub fn authorization_type(&self) -> Authorization { match self { + Self::Options(_) => Authorization::None, Self::Health(_) => Authorization::None, Self::CheckDomain(_) => Authorization::None, Self::Metrics(_) => Authorization::MetricsToken, diff --git a/src/api/admin/special.rs b/src/api/admin/special.rs index 0239021a..da3764d9 100644 --- a/src/api/admin/special.rs +++ b/src/api/admin/special.rs @@ -2,7 +2,9 @@ use std::sync::Arc; use async_trait::async_trait; -use http::header::{ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN, ALLOW}; +use http::header::{ + ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN, ALLOW, +}; use hyper::{Response, StatusCode}; use garage_model::garage::Garage; @@ -20,9 +22,10 @@ impl EndpointHandler for OptionsRequest { async fn handle(self, _garage: &Arc<Garage>) -> Result<Response<ResBody>, Error> { Ok(Response::builder() - .status(StatusCode::NO_CONTENT) - .header(ALLOW, "OPTIONS, GET, POST") - .header(ACCESS_CONTROL_ALLOW_METHODS, "OPTIONS, GET, POST") + .status(StatusCode::OK) + .header(ALLOW, "OPTIONS,GET,POST") + .header(ACCESS_CONTROL_ALLOW_METHODS, "OPTIONS,GET,POST") + .header(ACCESS_CONTROL_ALLOW_HEADERS, "authorization,content-type") .header(ACCESS_CONTROL_ALLOW_ORIGIN, "*") .body(empty_body())?) } |