Implemented website hosting authorization endpoint.

Fixes: #468
This commit is contained in:
Jonathan Davies 2023-01-12 13:35:14 +00:00
parent 94d723f27c
commit 100b01e859
2 changed files with 51 additions and 0 deletions

View File

@ -77,6 +77,53 @@ impl AdminApiServer {
.body(Body::empty())?) .body(Body::empty())?)
} }
async fn handle_check_website_enabled(
&self,
req: Request<Body>,
) -> Result<Response<Body>, Error> {
let has_domain_header = req.headers().contains_key("domain");
if !has_domain_header {
return Err(Error::bad_request("No domain header found"));
}
let domain = &req
.headers()
.get("domain")
.ok_or_internal_error("Could not parse domain header")?;
let domain_string = String::from(
domain
.to_str()
.ok_or_bad_request("Invalid characters found in domain header")?,
);
let bucket_id = self
.garage
.bucket_helper()
.resolve_global_bucket_name(&domain_string)
.await?
.ok_or_else(|| HelperError::NoSuchBucket(domain_string))?;
let bucket = self
.garage
.bucket_helper()
.get_existing_bucket(bucket_id)
.await?;
let bucket_state = bucket.state.as_option().unwrap();
let bucket_website_config = bucket_state.website_config.get();
match bucket_website_config {
Some(_v) => Ok(Response::builder()
.status(StatusCode::OK)
.body(Body::from("Bucket authorized for website hosting"))?),
None => Err(Error::bad_request(
"Bucket is not authorized for website hosting",
)),
}
}
fn handle_health(&self) -> Result<Response<Body>, Error> { fn handle_health(&self) -> Result<Response<Body>, Error> {
let health = self.garage.system.health(); let health = self.garage.system.health();
@ -174,6 +221,7 @@ impl ApiHandler for AdminApiServer {
match endpoint { match endpoint {
Endpoint::Options => self.handle_options(&req), Endpoint::Options => self.handle_options(&req),
Endpoint::CheckWebsiteEnabled => self.handle_check_website_enabled(req).await,
Endpoint::Health => self.handle_health(), Endpoint::Health => self.handle_health(),
Endpoint::Metrics => self.handle_metrics(), Endpoint::Metrics => self.handle_metrics(),
Endpoint::GetClusterStatus => handle_get_cluster_status(&self.garage).await, Endpoint::GetClusterStatus => handle_get_cluster_status(&self.garage).await,

View File

@ -17,6 +17,7 @@ router_match! {@func
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Endpoint { pub enum Endpoint {
Options, Options,
CheckWebsiteEnabled,
Health, Health,
Metrics, Metrics,
GetClusterStatus, GetClusterStatus,
@ -91,6 +92,7 @@ impl Endpoint {
let res = router_match!(@gen_path_parser (req.method(), path, query) [ let res = router_match!(@gen_path_parser (req.method(), path, query) [
OPTIONS _ => Options, OPTIONS _ => Options,
GET "/check" => CheckWebsiteEnabled,
GET "/health" => Health, GET "/health" => Health,
GET "/metrics" => Metrics, GET "/metrics" => Metrics,
GET "/v0/status" => GetClusterStatus, GET "/v0/status" => GetClusterStatus,
@ -136,6 +138,7 @@ impl Endpoint {
pub fn authorization_type(&self) -> Authorization { pub fn authorization_type(&self) -> Authorization {
match self { match self {
Self::Health => Authorization::None, Self::Health => Authorization::None,
Self::CheckWebsiteEnabled => Authorization::None,
Self::Metrics => Authorization::MetricsToken, Self::Metrics => Authorization::MetricsToken,
_ => Authorization::AdminToken, _ => Authorization::AdminToken,
} }