admin API refactoring (step 1) #939

Merged
lx merged 19 commits from refactor-admin into next-v2 2025-01-29 20:42:57 +00:00
5 changed files with 781 additions and 522 deletions
Showing only changes of commit 831f2b0207 - Show all commits

View file

@ -1,7 +1,13 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc;
use async_trait::async_trait;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use garage_model::garage::Garage;
use crate::admin::error::Error;
use crate::admin::EndpointHandler;
use crate::helpers::is_default; use crate::helpers::is_default;
pub enum AdminApiRequest { pub enum AdminApiRequest {
@ -13,8 +19,35 @@ pub enum AdminApiRequest {
UpdateClusterLayout(UpdateClusterLayoutRequest), UpdateClusterLayout(UpdateClusterLayoutRequest),
ApplyClusterLayout(ApplyClusterLayoutRequest), ApplyClusterLayout(ApplyClusterLayoutRequest),
RevertClusterLayout(RevertClusterLayoutRequest), RevertClusterLayout(RevertClusterLayoutRequest),
// Access key operations
ListKeys(ListKeysRequest),
GetKeyInfo(GetKeyInfoRequest),
CreateKey(CreateKeyRequest),
ImportKey(ImportKeyRequest),
UpdateKey(UpdateKeyRequest),
DeleteKey(DeleteKeyRequest),
// Bucket operations
ListBuckets(ListBucketsRequest),
GetBucketInfo(GetBucketInfoRequest),
CreateBucket(CreateBucketRequest),
UpdateBucket(UpdateBucketRequest),
DeleteBucket(DeleteBucketRequest),
// Operations on permissions for keys on buckets
BucketAllowKey(BucketAllowKeyRequest),
BucketDenyKey(BucketDenyKeyRequest),
// Operations on bucket aliases
GlobalAliasBucket(GlobalAliasBucketRequest),
GlobalUnaliasBucket(GlobalUnaliasBucketRequest),
LocalAliasBucket(LocalAliasBucketRequest),
LocalUnaliasBucket(LocalUnaliasBucketRequest),
} }
#[derive(Serialize)]
#[serde(untagged)]
pub enum AdminApiResponse { pub enum AdminApiResponse {
// Cluster operations // Cluster operations
GetClusterStatus(GetClusterStatusResponse), GetClusterStatus(GetClusterStatusResponse),
@ -24,6 +57,98 @@ pub enum AdminApiResponse {
UpdateClusterLayout(UpdateClusterLayoutResponse), UpdateClusterLayout(UpdateClusterLayoutResponse),
ApplyClusterLayout(ApplyClusterLayoutResponse), ApplyClusterLayout(ApplyClusterLayoutResponse),
RevertClusterLayout(RevertClusterLayoutResponse), RevertClusterLayout(RevertClusterLayoutResponse),
// Access key operations
ListKeys(ListKeysResponse),
GetKeyInfo(GetKeyInfoResponse),
CreateKey(CreateKeyResponse),
ImportKey(ImportKeyResponse),
UpdateKey(UpdateKeyResponse),
DeleteKey(DeleteKeyResponse),
// Bucket operations
ListBuckets(ListBucketsResponse),
GetBucketInfo(GetBucketInfoResponse),
CreateBucket(CreateBucketResponse),
UpdateBucket(UpdateBucketResponse),
DeleteBucket(DeleteBucketResponse),
// Operations on permissions for keys on buckets
BucketAllowKey(BucketAllowKeyResponse),
BucketDenyKey(BucketDenyKeyResponse),
// Operations on bucket aliases
GlobalAliasBucket(GlobalAliasBucketResponse),
GlobalUnaliasBucket(GlobalUnaliasBucketResponse),
LocalAliasBucket(LocalAliasBucketResponse),
LocalUnaliasBucket(LocalUnaliasBucketResponse),
}
#[async_trait]
impl EndpointHandler for AdminApiRequest {
type Response = AdminApiResponse;
async fn handle(self, garage: &Arc<Garage>) -> Result<AdminApiResponse, Error> {
Ok(match self {
// Cluster operations
Self::GetClusterStatus(req) => {
AdminApiResponse::GetClusterStatus(req.handle(garage).await?)
}
Self::GetClusterHealth(req) => {
AdminApiResponse::GetClusterHealth(req.handle(garage).await?)
}
Self::ConnectClusterNodes(req) => {
AdminApiResponse::ConnectClusterNodes(req.handle(garage).await?)
}
Self::GetClusterLayout(req) => {
AdminApiResponse::GetClusterLayout(req.handle(garage).await?)
}
Self::UpdateClusterLayout(req) => {
AdminApiResponse::UpdateClusterLayout(req.handle(garage).await?)
}
Self::ApplyClusterLayout(req) => {
AdminApiResponse::ApplyClusterLayout(req.handle(garage).await?)
}
Self::RevertClusterLayout(req) => {
AdminApiResponse::RevertClusterLayout(req.handle(garage).await?)
}
// Access key operations
Self::ListKeys(req) => AdminApiResponse::ListKeys(req.handle(garage).await?),
Self::GetKeyInfo(req) => AdminApiResponse::GetKeyInfo(req.handle(garage).await?),
Self::CreateKey(req) => AdminApiResponse::CreateKey(req.handle(garage).await?),
Self::ImportKey(req) => AdminApiResponse::ImportKey(req.handle(garage).await?),
Self::UpdateKey(req) => AdminApiResponse::UpdateKey(req.handle(garage).await?),
Self::DeleteKey(req) => AdminApiResponse::DeleteKey(req.handle(garage).await?),
// Bucket operations
Self::ListBuckets(req) => AdminApiResponse::ListBuckets(req.handle(garage).await?),
Self::GetBucketInfo(req) => AdminApiResponse::GetBucketInfo(req.handle(garage).await?),
Self::CreateBucket(req) => AdminApiResponse::CreateBucket(req.handle(garage).await?),
Self::UpdateBucket(req) => AdminApiResponse::UpdateBucket(req.handle(garage).await?),
Self::DeleteBucket(req) => AdminApiResponse::DeleteBucket(req.handle(garage).await?),
// Operations on permissions for keys on buckets
Self::BucketAllowKey(req) => {
AdminApiResponse::BucketAllowKey(req.handle(garage).await?)
}
Self::BucketDenyKey(req) => AdminApiResponse::BucketDenyKey(req.handle(garage).await?),
// Operations on bucket aliases
Self::GlobalAliasBucket(req) => {
AdminApiResponse::GlobalAliasBucket(req.handle(garage).await?)
}
Self::GlobalUnaliasBucket(req) => {
AdminApiResponse::GlobalUnaliasBucket(req.handle(garage).await?)
}
Self::LocalAliasBucket(req) => {
AdminApiResponse::LocalAliasBucket(req.handle(garage).await?)
}
Self::LocalUnaliasBucket(req) => {
AdminApiResponse::LocalUnaliasBucket(req.handle(garage).await?)
}
})
}
} }
// ********************************************** // **********************************************
@ -277,24 +402,30 @@ pub struct ImportKeyResponse(pub GetKeyInfoResponse);
// ---- UpdateKey ---- // ---- UpdateKey ----
pub struct UpdateKeyRequest {
pub id: String,
pub params: UpdateKeyRequestParams,
}
#[derive(Serialize)]
pub struct UpdateKeyResponse(pub GetKeyInfoResponse);
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct UpdateKeyRequest { pub struct UpdateKeyRequestParams {
// TODO: id (get parameter) goes here // TODO: id (get parameter) goes here
pub name: Option<String>, pub name: Option<String>,
pub allow: Option<KeyPerm>, pub allow: Option<KeyPerm>,
pub deny: Option<KeyPerm>, pub deny: Option<KeyPerm>,
} }
#[derive(Serialize)]
pub struct UpdateKeyResponse(pub GetKeyInfoResponse);
// ---- DeleteKey ---- // ---- DeleteKey ----
pub struct DeleteKeyRequest { pub struct DeleteKeyRequest {
pub id: String, pub id: String,
} }
#[derive(Serialize)]
pub struct DeleteKeyResponse; pub struct DeleteKeyResponse;
// ********************************************** // **********************************************
@ -305,6 +436,7 @@ pub struct DeleteKeyResponse;
pub struct ListBucketsRequest; pub struct ListBucketsRequest;
#[derive(Serialize)]
pub struct ListBucketsResponse(pub Vec<ListBucketsResponseItem>); pub struct ListBucketsResponse(pub Vec<ListBucketsResponseItem>);
#[derive(Serialize)] #[derive(Serialize)]
@ -380,7 +512,7 @@ pub struct CreateBucketRequest {
} }
#[derive(Serialize)] #[derive(Serialize)]
pub struct CreateBucketResponse(GetBucketInfoResponse); pub struct CreateBucketResponse(pub GetBucketInfoResponse);
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -393,15 +525,20 @@ pub struct CreateBucketLocalAlias {
// ---- UpdateBucket ---- // ---- UpdateBucket ----
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateBucketRequest { pub struct UpdateBucketRequest {
pub website_access: Option<UpdateBucketWebsiteAccess>, pub id: String,
pub quotas: Option<ApiBucketQuotas>, pub params: UpdateBucketRequestParams,
} }
#[derive(Serialize)] #[derive(Serialize)]
pub struct UpdateBucketResponse(GetBucketInfoResponse); pub struct UpdateBucketResponse(pub GetBucketInfoResponse);
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateBucketRequestParams {
pub website_access: Option<UpdateBucketWebsiteAccess>,
pub quotas: Option<ApiBucketQuotas>,
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -417,6 +554,7 @@ pub struct DeleteBucketRequest {
pub id: String, pub id: String,
} }
#[derive(Serialize)]
pub struct DeleteBucketResponse; pub struct DeleteBucketResponse;
// ********************************************** // **********************************************
@ -427,7 +565,8 @@ pub struct DeleteBucketResponse;
pub struct BucketAllowKeyRequest(pub BucketKeyPermChangeRequest); pub struct BucketAllowKeyRequest(pub BucketKeyPermChangeRequest);
pub struct BucketAllowKeyResponse; #[derive(Serialize)]
pub struct BucketAllowKeyResponse(pub GetBucketInfoResponse);
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -441,7 +580,8 @@ pub struct BucketKeyPermChangeRequest {
pub struct BucketDenyKeyRequest(pub BucketKeyPermChangeRequest); pub struct BucketDenyKeyRequest(pub BucketKeyPermChangeRequest);
pub struct BucketDenyKeyResponse; #[derive(Serialize)]
pub struct BucketDenyKeyResponse(pub GetBucketInfoResponse);
// ********************************************** // **********************************************
// Operations on bucket aliases // Operations on bucket aliases
@ -454,7 +594,8 @@ pub struct GlobalAliasBucketRequest {
pub alias: String, pub alias: String,
} }
pub struct GlobalAliasBucketReponse; #[derive(Serialize)]
pub struct GlobalAliasBucketResponse(pub GetBucketInfoResponse);
// ---- GlobalUnaliasBucket ---- // ---- GlobalUnaliasBucket ----
@ -463,7 +604,8 @@ pub struct GlobalUnaliasBucketRequest {
pub alias: String, pub alias: String,
} }
pub struct GlobalUnaliasBucketReponse; #[derive(Serialize)]
pub struct GlobalUnaliasBucketResponse(pub GetBucketInfoResponse);
// ---- LocalAliasBucket ---- // ---- LocalAliasBucket ----
@ -473,7 +615,8 @@ pub struct LocalAliasBucketRequest {
pub alias: String, pub alias: String,
} }
pub struct LocalAliasBucketReponse; #[derive(Serialize)]
pub struct LocalAliasBucketResponse(pub GetBucketInfoResponse);
// ---- LocalUnaliasBucket ---- // ---- LocalUnaliasBucket ----
@ -483,4 +626,5 @@ pub struct LocalUnaliasBucketRequest {
pub alias: String, pub alias: String,
} }
pub struct LocalUnaliasBucketReponse; #[derive(Serialize)]
pub struct LocalUnaliasBucketResponse(pub GetBucketInfoResponse);

View file

@ -23,10 +23,7 @@ use garage_util::socket_address::UnixOrTCPSocketAddress;
use crate::generic_server::*; use crate::generic_server::*;
use crate::admin::api::*; use crate::admin::api::*;
use crate::admin::bucket::*;
use crate::admin::cluster::*;
use crate::admin::error::*; use crate::admin::error::*;
use crate::admin::key::*;
use crate::admin::router_v0; use crate::admin::router_v0;
use crate::admin::router_v1::{Authorization, Endpoint}; use crate::admin::router_v1::{Authorization, Endpoint};
use crate::admin::EndpointHandler; use crate::admin::EndpointHandler;
@ -271,67 +268,134 @@ impl ApiHandler for AdminApiServer {
Endpoint::CheckDomain => self.handle_check_domain(req).await, Endpoint::CheckDomain => self.handle_check_domain(req).await,
Endpoint::Health => self.handle_health(), Endpoint::Health => self.handle_health(),
Endpoint::Metrics => self.handle_metrics(), Endpoint::Metrics => self.handle_metrics(),
Endpoint::GetClusterStatus => GetClusterStatusRequest e => {
.handle(&self.garage) async {
let body = parse_request_body(e, req).await?;
let res = body.handle(&self.garage).await?;
json_ok_response(&res)
}
.await .await
.and_then(|x| json_ok_response(&x)), }
Endpoint::GetClusterHealth => GetClusterHealthRequest }
.handle(&self.garage) }
.await }
.and_then(|x| json_ok_response(&x)),
Endpoint::ConnectClusterNodes => handle_connect_cluster_nodes(&self.garage, req).await, async fn parse_request_body(
// Layout endpoint: Endpoint,
Endpoint::GetClusterLayout => handle_get_cluster_layout(&self.garage).await, req: Request<IncomingBody>,
Endpoint::UpdateClusterLayout => handle_update_cluster_layout(&self.garage, req).await, ) -> Result<AdminApiRequest, Error> {
Endpoint::ApplyClusterLayout => handle_apply_cluster_layout(&self.garage, req).await, match endpoint {
Endpoint::RevertClusterLayout => handle_revert_cluster_layout(&self.garage).await, Endpoint::GetClusterStatus => {
// Keys Ok(AdminApiRequest::GetClusterStatus(GetClusterStatusRequest))
Endpoint::ListKeys => handle_list_keys(&self.garage).await, }
Endpoint::GetKeyInfo { Endpoint::GetClusterHealth => {
Ok(AdminApiRequest::GetClusterHealth(GetClusterHealthRequest))
}
Endpoint::ConnectClusterNodes => {
let req = parse_json_body::<ConnectClusterNodesRequest, _, Error>(req).await?;
Ok(AdminApiRequest::ConnectClusterNodes(req))
}
// Layout
Endpoint::GetClusterLayout => {
Ok(AdminApiRequest::GetClusterLayout(GetClusterLayoutRequest))
}
Endpoint::UpdateClusterLayout => {
let updates = parse_json_body::<UpdateClusterLayoutRequest, _, Error>(req).await?;
Ok(AdminApiRequest::UpdateClusterLayout(updates))
}
Endpoint::ApplyClusterLayout => {
let param = parse_json_body::<ApplyClusterLayoutRequest, _, Error>(req).await?;
Ok(AdminApiRequest::ApplyClusterLayout(param))
}
Endpoint::RevertClusterLayout => Ok(AdminApiRequest::RevertClusterLayout(
RevertClusterLayoutRequest,
)),
// Keys
Endpoint::ListKeys => Ok(AdminApiRequest::ListKeys(ListKeysRequest)),
Endpoint::GetKeyInfo {
id,
search,
show_secret_key,
} => {
let show_secret_key = show_secret_key.map(|x| x == "true").unwrap_or(false);
Ok(AdminApiRequest::GetKeyInfo(GetKeyInfoRequest {
id, id,
search, search,
show_secret_key, show_secret_key,
} => { }))
let show_secret_key = show_secret_key.map(|x| x == "true").unwrap_or(false);
handle_get_key_info(&self.garage, id, search, show_secret_key).await
}
Endpoint::CreateKey => handle_create_key(&self.garage, req).await,
Endpoint::ImportKey => handle_import_key(&self.garage, req).await,
Endpoint::UpdateKey { id } => handle_update_key(&self.garage, id, req).await,
Endpoint::DeleteKey { id } => handle_delete_key(&self.garage, id).await,
// Buckets
Endpoint::ListBuckets => handle_list_buckets(&self.garage).await,
Endpoint::GetBucketInfo { id, global_alias } => {
handle_get_bucket_info(&self.garage, id, global_alias).await
}
Endpoint::CreateBucket => handle_create_bucket(&self.garage, req).await,
Endpoint::DeleteBucket { id } => handle_delete_bucket(&self.garage, id).await,
Endpoint::UpdateBucket { id } => handle_update_bucket(&self.garage, id, req).await,
// Bucket-key permissions
Endpoint::BucketAllowKey => {
handle_bucket_change_key_perm(&self.garage, req, true).await
}
Endpoint::BucketDenyKey => {
handle_bucket_change_key_perm(&self.garage, req, false).await
}
// Bucket aliasing
Endpoint::GlobalAliasBucket { id, alias } => {
handle_global_alias_bucket(&self.garage, id, alias).await
}
Endpoint::GlobalUnaliasBucket { id, alias } => {
handle_global_unalias_bucket(&self.garage, id, alias).await
}
Endpoint::LocalAliasBucket {
id,
access_key_id,
alias,
} => handle_local_alias_bucket(&self.garage, id, access_key_id, alias).await,
Endpoint::LocalUnaliasBucket {
id,
access_key_id,
alias,
} => handle_local_unalias_bucket(&self.garage, id, access_key_id, alias).await,
} }
Endpoint::CreateKey => {
let req = parse_json_body::<CreateKeyRequest, _, Error>(req).await?;
Ok(AdminApiRequest::CreateKey(req))
}
Endpoint::ImportKey => {
let req = parse_json_body::<ImportKeyRequest, _, Error>(req).await?;
Ok(AdminApiRequest::ImportKey(req))
}
Endpoint::UpdateKey { id } => {
let params = parse_json_body::<UpdateKeyRequestParams, _, Error>(req).await?;
Ok(AdminApiRequest::UpdateKey(UpdateKeyRequest { id, params }))
}
Endpoint::DeleteKey { id } => Ok(AdminApiRequest::DeleteKey(DeleteKeyRequest { id })),
// Buckets
Endpoint::ListBuckets => Ok(AdminApiRequest::ListBuckets(ListBucketsRequest)),
Endpoint::GetBucketInfo { id, global_alias } => {
Ok(AdminApiRequest::GetBucketInfo(GetBucketInfoRequest {
id,
global_alias,
}))
}
Endpoint::CreateBucket => {
let req = parse_json_body::<CreateBucketRequest, _, Error>(req).await?;
Ok(AdminApiRequest::CreateBucket(req))
}
Endpoint::DeleteBucket { id } => {
Ok(AdminApiRequest::DeleteBucket(DeleteBucketRequest { id }))
}
Endpoint::UpdateBucket { id } => {
let params = parse_json_body::<UpdateBucketRequestParams, _, Error>(req).await?;
Ok(AdminApiRequest::UpdateBucket(UpdateBucketRequest {
id,
params,
}))
}
// Bucket-key permissions
Endpoint::BucketAllowKey => {
let req = parse_json_body::<BucketKeyPermChangeRequest, _, Error>(req).await?;
Ok(AdminApiRequest::BucketAllowKey(BucketAllowKeyRequest(req)))
}
Endpoint::BucketDenyKey => {
let req = parse_json_body::<BucketKeyPermChangeRequest, _, Error>(req).await?;
Ok(AdminApiRequest::BucketDenyKey(BucketDenyKeyRequest(req)))
}
// Bucket aliasing
Endpoint::GlobalAliasBucket { id, alias } => Ok(AdminApiRequest::GlobalAliasBucket(
GlobalAliasBucketRequest { id, alias },
)),
Endpoint::GlobalUnaliasBucket { id, alias } => Ok(AdminApiRequest::GlobalUnaliasBucket(
GlobalUnaliasBucketRequest { id, alias },
)),
Endpoint::LocalAliasBucket {
id,
access_key_id,
alias,
} => Ok(AdminApiRequest::LocalAliasBucket(LocalAliasBucketRequest {
access_key_id,
id,
alias,
})),
Endpoint::LocalUnaliasBucket {
id,
access_key_id,
alias,
} => Ok(AdminApiRequest::LocalUnaliasBucket(
LocalUnaliasBucketRequest {
access_key_id,
id,
alias,
},
)),
_ => unreachable!(),
} }
} }

View file

@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use hyper::{body::Incoming as IncomingBody, Request, Response, StatusCode}; use async_trait::async_trait;
use garage_util::crdt::*; use garage_util::crdt::*;
use garage_util::data::*; use garage_util::data::*;
@ -18,83 +18,93 @@ use garage_model::s3::object_table::*;
use crate::admin::api::ApiBucketKeyPerm; use crate::admin::api::ApiBucketKeyPerm;
use crate::admin::api::{ use crate::admin::api::{
ApiBucketQuotas, BucketKeyPermChangeRequest, BucketLocalAlias, CreateBucketRequest, ApiBucketQuotas, BucketAllowKeyRequest, BucketAllowKeyResponse, BucketDenyKeyRequest,
GetBucketInfoKey, GetBucketInfoResponse, GetBucketInfoWebsiteResponse, ListBucketsResponseItem, BucketDenyKeyResponse, BucketKeyPermChangeRequest, BucketLocalAlias, CreateBucketRequest,
UpdateBucketRequest, CreateBucketResponse, DeleteBucketRequest, DeleteBucketResponse, GetBucketInfoKey,
GetBucketInfoRequest, GetBucketInfoResponse, GetBucketInfoWebsiteResponse,
GlobalAliasBucketRequest, GlobalAliasBucketResponse, GlobalUnaliasBucketRequest,
GlobalUnaliasBucketResponse, ListBucketsRequest, ListBucketsResponse, ListBucketsResponseItem,
LocalAliasBucketRequest, LocalAliasBucketResponse, LocalUnaliasBucketRequest,
LocalUnaliasBucketResponse, UpdateBucketRequest, UpdateBucketResponse,
}; };
use crate::admin::api_server::ResBody;
use crate::admin::error::*; use crate::admin::error::*;
use crate::admin::EndpointHandler;
use crate::common_error::CommonError; use crate::common_error::CommonError;
use crate::helpers::*;
pub async fn handle_list_buckets(garage: &Arc<Garage>) -> Result<Response<ResBody>, Error> { #[async_trait]
let buckets = garage impl EndpointHandler for ListBucketsRequest {
.bucket_table type Response = ListBucketsResponse;
.get_range(
&EmptyKey,
None,
Some(DeletedFilter::NotDeleted),
10000,
EnumerationOrder::Forward,
)
.await?;
let res = buckets async fn handle(self, garage: &Arc<Garage>) -> Result<ListBucketsResponse, Error> {
.into_iter() let buckets = garage
.map(|b| { .bucket_table
let state = b.state.as_option().unwrap(); .get_range(
ListBucketsResponseItem { &EmptyKey,
id: hex::encode(b.id), None,
global_aliases: state Some(DeletedFilter::NotDeleted),
.aliases 10000,
.items() EnumerationOrder::Forward,
.iter() )
.filter(|(_, _, a)| *a) .await?;
.map(|(n, _, _)| n.to_string())
.collect::<Vec<_>>(),
local_aliases: state
.local_aliases
.items()
.iter()
.filter(|(_, _, a)| *a)
.map(|((k, n), _, _)| BucketLocalAlias {
access_key_id: k.to_string(),
alias: n.to_string(),
})
.collect::<Vec<_>>(),
}
})
.collect::<Vec<_>>();
Ok(json_ok_response(&res)?) let res = buckets
.into_iter()
.map(|b| {
let state = b.state.as_option().unwrap();
ListBucketsResponseItem {
id: hex::encode(b.id),
global_aliases: state
.aliases
.items()
.iter()
.filter(|(_, _, a)| *a)
.map(|(n, _, _)| n.to_string())
.collect::<Vec<_>>(),
local_aliases: state
.local_aliases
.items()
.iter()
.filter(|(_, _, a)| *a)
.map(|((k, n), _, _)| BucketLocalAlias {
access_key_id: k.to_string(),
alias: n.to_string(),
})
.collect::<Vec<_>>(),
}
})
.collect::<Vec<_>>();
Ok(ListBucketsResponse(res))
}
} }
pub async fn handle_get_bucket_info( #[async_trait]
garage: &Arc<Garage>, impl EndpointHandler for GetBucketInfoRequest {
id: Option<String>, type Response = GetBucketInfoResponse;
global_alias: Option<String>,
) -> Result<Response<ResBody>, Error> {
let bucket_id = match (id, global_alias) {
(Some(id), None) => parse_bucket_id(&id)?,
(None, Some(ga)) => garage
.bucket_helper()
.resolve_global_bucket_name(&ga)
.await?
.ok_or_else(|| HelperError::NoSuchBucket(ga.to_string()))?,
_ => {
return Err(Error::bad_request(
"Either id or globalAlias must be provided (but not both)",
));
}
};
bucket_info_results(garage, bucket_id).await async fn handle(self, garage: &Arc<Garage>) -> Result<GetBucketInfoResponse, Error> {
let bucket_id = match (self.id, self.global_alias) {
(Some(id), None) => parse_bucket_id(&id)?,
(None, Some(ga)) => garage
.bucket_helper()
.resolve_global_bucket_name(&ga)
.await?
.ok_or_else(|| HelperError::NoSuchBucket(ga.to_string()))?,
_ => {
return Err(Error::bad_request(
"Either id or globalAlias must be provided (but not both)",
));
}
};
bucket_info_results(garage, bucket_id).await
}
} }
async fn bucket_info_results( async fn bucket_info_results(
garage: &Arc<Garage>, garage: &Arc<Garage>,
bucket_id: Uuid, bucket_id: Uuid,
) -> Result<Response<ResBody>, Error> { ) -> Result<GetBucketInfoResponse, Error> {
let bucket = garage let bucket = garage
.bucket_helper() .bucket_helper()
.get_existing_bucket(bucket_id) .get_existing_bucket(bucket_id)
@ -211,181 +221,203 @@ async fn bucket_info_results(
}, },
}; };
Ok(json_ok_response(&res)?) Ok(res)
} }
pub async fn handle_create_bucket( #[async_trait]
garage: &Arc<Garage>, impl EndpointHandler for CreateBucketRequest {
req: Request<IncomingBody>, type Response = CreateBucketResponse;
) -> Result<Response<ResBody>, Error> {
let req = parse_json_body::<CreateBucketRequest, _, Error>(req).await?;
let helper = garage.locked_helper().await; async fn handle(self, garage: &Arc<Garage>) -> Result<CreateBucketResponse, Error> {
let helper = garage.locked_helper().await;
if let Some(ga) = &req.global_alias { if let Some(ga) = &self.global_alias {
if !is_valid_bucket_name(ga) { if !is_valid_bucket_name(ga) {
return Err(Error::bad_request(format!( return Err(Error::bad_request(format!(
"{}: {}", "{}: {}",
ga, INVALID_BUCKET_NAME_MESSAGE ga, INVALID_BUCKET_NAME_MESSAGE
))); )));
} }
if let Some(alias) = garage.bucket_alias_table.get(&EmptyKey, ga).await? { if let Some(alias) = garage.bucket_alias_table.get(&EmptyKey, ga).await? {
if alias.state.get().is_some() { if alias.state.get().is_some() {
return Err(CommonError::BucketAlreadyExists.into()); return Err(CommonError::BucketAlreadyExists.into());
}
} }
} }
}
if let Some(la) = &req.local_alias { if let Some(la) = &self.local_alias {
if !is_valid_bucket_name(&la.alias) { if !is_valid_bucket_name(&la.alias) {
return Err(Error::bad_request(format!( return Err(Error::bad_request(format!(
"{}: {}", "{}: {}",
la.alias, INVALID_BUCKET_NAME_MESSAGE la.alias, INVALID_BUCKET_NAME_MESSAGE
))); )));
}
let key = helper.key().get_existing_key(&la.access_key_id).await?;
let state = key.state.as_option().unwrap();
if matches!(state.local_aliases.get(&la.alias), Some(_)) {
return Err(Error::bad_request("Local alias already exists"));
}
} }
let key = helper.key().get_existing_key(&la.access_key_id).await?; let bucket = Bucket::new();
let state = key.state.as_option().unwrap(); garage.bucket_table.insert(&bucket).await?;
if matches!(state.local_aliases.get(&la.alias), Some(_)) {
return Err(Error::bad_request("Local alias already exists")); if let Some(ga) = &self.global_alias {
helper.set_global_bucket_alias(bucket.id, ga).await?;
} }
if let Some(la) = &self.local_alias {
helper
.set_local_bucket_alias(bucket.id, &la.access_key_id, &la.alias)
.await?;
if la.allow.read || la.allow.write || la.allow.owner {
helper
.set_bucket_key_permissions(
bucket.id,
&la.access_key_id,
BucketKeyPerm {
timestamp: now_msec(),
allow_read: la.allow.read,
allow_write: la.allow.write,
allow_owner: la.allow.owner,
},
)
.await?;
}
}
Ok(CreateBucketResponse(
bucket_info_results(garage, bucket.id).await?,
))
} }
}
let bucket = Bucket::new(); #[async_trait]
garage.bucket_table.insert(&bucket).await?; impl EndpointHandler for DeleteBucketRequest {
type Response = DeleteBucketResponse;
if let Some(ga) = &req.global_alias { async fn handle(self, garage: &Arc<Garage>) -> Result<DeleteBucketResponse, Error> {
helper.set_global_bucket_alias(bucket.id, ga).await?; let helper = garage.locked_helper().await;
let bucket_id = parse_bucket_id(&self.id)?;
let mut bucket = helper.bucket().get_existing_bucket(bucket_id).await?;
let state = bucket.state.as_option().unwrap();
// Check bucket is empty
if !helper.bucket().is_bucket_empty(bucket_id).await? {
return Err(CommonError::BucketNotEmpty.into());
}
// --- done checking, now commit ---
// 1. delete authorization from keys that had access
for (key_id, perm) in bucket.authorized_keys() {
if perm.is_any() {
helper
.set_bucket_key_permissions(bucket.id, key_id, BucketKeyPerm::NO_PERMISSIONS)
.await?;
}
}
// 2. delete all local aliases
for ((key_id, alias), _, active) in state.local_aliases.items().iter() {
if *active {
helper
.unset_local_bucket_alias(bucket.id, key_id, alias)
.await?;
}
}
// 3. delete all global aliases
for (alias, _, active) in state.aliases.items().iter() {
if *active {
helper.purge_global_bucket_alias(bucket.id, alias).await?;
}
}
// 4. delete bucket
bucket.state = Deletable::delete();
garage.bucket_table.insert(&bucket).await?;
Ok(DeleteBucketResponse)
} }
}
if let Some(la) = &req.local_alias { #[async_trait]
helper impl EndpointHandler for UpdateBucketRequest {
.set_local_bucket_alias(bucket.id, &la.access_key_id, &la.alias) type Response = UpdateBucketResponse;
async fn handle(self, garage: &Arc<Garage>) -> Result<UpdateBucketResponse, Error> {
let bucket_id = parse_bucket_id(&self.id)?;
let mut bucket = garage
.bucket_helper()
.get_existing_bucket(bucket_id)
.await?; .await?;
if la.allow.read || la.allow.write || la.allow.owner { let state = bucket.state.as_option_mut().unwrap();
helper
.set_bucket_key_permissions(
bucket.id,
&la.access_key_id,
BucketKeyPerm {
timestamp: now_msec(),
allow_read: la.allow.read,
allow_write: la.allow.write,
allow_owner: la.allow.owner,
},
)
.await?;
}
}
bucket_info_results(garage, bucket.id).await if let Some(wa) = self.params.website_access {
} if wa.enabled {
state.website_config.update(Some(WebsiteConfig {
pub async fn handle_delete_bucket( index_document: wa.index_document.ok_or_bad_request(
garage: &Arc<Garage>, "Please specify indexDocument when enabling website access.",
id: String, )?,
) -> Result<Response<ResBody>, Error> { error_document: wa.error_document,
let helper = garage.locked_helper().await; }));
} else {
let bucket_id = parse_bucket_id(&id)?; if wa.index_document.is_some() || wa.error_document.is_some() {
return Err(Error::bad_request(
let mut bucket = helper.bucket().get_existing_bucket(bucket_id).await?; "Cannot specify indexDocument or errorDocument when disabling website access.",
let state = bucket.state.as_option().unwrap(); ));
}
// Check bucket is empty state.website_config.update(None);
if !helper.bucket().is_bucket_empty(bucket_id).await? {
return Err(CommonError::BucketNotEmpty.into());
}
// --- done checking, now commit ---
// 1. delete authorization from keys that had access
for (key_id, perm) in bucket.authorized_keys() {
if perm.is_any() {
helper
.set_bucket_key_permissions(bucket.id, key_id, BucketKeyPerm::NO_PERMISSIONS)
.await?;
}
}
// 2. delete all local aliases
for ((key_id, alias), _, active) in state.local_aliases.items().iter() {
if *active {
helper
.unset_local_bucket_alias(bucket.id, key_id, alias)
.await?;
}
}
// 3. delete all global aliases
for (alias, _, active) in state.aliases.items().iter() {
if *active {
helper.purge_global_bucket_alias(bucket.id, alias).await?;
}
}
// 4. delete bucket
bucket.state = Deletable::delete();
garage.bucket_table.insert(&bucket).await?;
Ok(Response::builder()
.status(StatusCode::NO_CONTENT)
.body(empty_body())?)
}
pub async fn handle_update_bucket(
garage: &Arc<Garage>,
id: String,
req: Request<IncomingBody>,
) -> Result<Response<ResBody>, Error> {
let req = parse_json_body::<UpdateBucketRequest, _, Error>(req).await?;
let bucket_id = parse_bucket_id(&id)?;
let mut bucket = garage
.bucket_helper()
.get_existing_bucket(bucket_id)
.await?;
let state = bucket.state.as_option_mut().unwrap();
if let Some(wa) = req.website_access {
if wa.enabled {
state.website_config.update(Some(WebsiteConfig {
index_document: wa.index_document.ok_or_bad_request(
"Please specify indexDocument when enabling website access.",
)?,
error_document: wa.error_document,
}));
} else {
if wa.index_document.is_some() || wa.error_document.is_some() {
return Err(Error::bad_request(
"Cannot specify indexDocument or errorDocument when disabling website access.",
));
} }
state.website_config.update(None);
} }
if let Some(q) = self.params.quotas {
state.quotas.update(BucketQuotas {
max_size: q.max_size,
max_objects: q.max_objects,
});
}
garage.bucket_table.insert(&bucket).await?;
Ok(UpdateBucketResponse(
bucket_info_results(garage, bucket_id).await?,
))
} }
if let Some(q) = req.quotas {
state.quotas.update(BucketQuotas {
max_size: q.max_size,
max_objects: q.max_objects,
});
}
garage.bucket_table.insert(&bucket).await?;
bucket_info_results(garage, bucket_id).await
} }
// ---- BUCKET/KEY PERMISSIONS ---- // ---- BUCKET/KEY PERMISSIONS ----
#[async_trait]
impl EndpointHandler for BucketAllowKeyRequest {
type Response = BucketAllowKeyResponse;
async fn handle(self, garage: &Arc<Garage>) -> Result<BucketAllowKeyResponse, Error> {
let res = handle_bucket_change_key_perm(garage, self.0, true).await?;
Ok(BucketAllowKeyResponse(res))
}
}
#[async_trait]
impl EndpointHandler for BucketDenyKeyRequest {
type Response = BucketDenyKeyResponse;
async fn handle(self, garage: &Arc<Garage>) -> Result<BucketDenyKeyResponse, Error> {
let res = handle_bucket_change_key_perm(garage, self.0, false).await?;
Ok(BucketDenyKeyResponse(res))
}
}
pub async fn handle_bucket_change_key_perm( pub async fn handle_bucket_change_key_perm(
garage: &Arc<Garage>, garage: &Arc<Garage>,
req: Request<IncomingBody>, req: BucketKeyPermChangeRequest,
new_perm_flag: bool, new_perm_flag: bool,
) -> Result<Response<ResBody>, Error> { ) -> Result<GetBucketInfoResponse, Error> {
let req = parse_json_body::<BucketKeyPermChangeRequest, _, Error>(req).await?;
let helper = garage.locked_helper().await; let helper = garage.locked_helper().await;
let bucket_id = parse_bucket_id(&req.bucket_id)?; let bucket_id = parse_bucket_id(&req.bucket_id)?;
@ -420,66 +452,80 @@ pub async fn handle_bucket_change_key_perm(
// ---- BUCKET ALIASES ---- // ---- BUCKET ALIASES ----
pub async fn handle_global_alias_bucket( #[async_trait]
garage: &Arc<Garage>, impl EndpointHandler for GlobalAliasBucketRequest {
bucket_id: String, type Response = GlobalAliasBucketResponse;
alias: String,
) -> Result<Response<ResBody>, Error> {
let bucket_id = parse_bucket_id(&bucket_id)?;
let helper = garage.locked_helper().await; async fn handle(self, garage: &Arc<Garage>) -> Result<GlobalAliasBucketResponse, Error> {
let bucket_id = parse_bucket_id(&self.id)?;
helper.set_global_bucket_alias(bucket_id, &alias).await?; let helper = garage.locked_helper().await;
bucket_info_results(garage, bucket_id).await helper
.set_global_bucket_alias(bucket_id, &self.alias)
.await?;
Ok(GlobalAliasBucketResponse(
bucket_info_results(garage, bucket_id).await?,
))
}
} }
pub async fn handle_global_unalias_bucket( #[async_trait]
garage: &Arc<Garage>, impl EndpointHandler for GlobalUnaliasBucketRequest {
bucket_id: String, type Response = GlobalUnaliasBucketResponse;
alias: String,
) -> Result<Response<ResBody>, Error> {
let bucket_id = parse_bucket_id(&bucket_id)?;
let helper = garage.locked_helper().await; async fn handle(self, garage: &Arc<Garage>) -> Result<GlobalUnaliasBucketResponse, Error> {
let bucket_id = parse_bucket_id(&self.id)?;
helper.unset_global_bucket_alias(bucket_id, &alias).await?; let helper = garage.locked_helper().await;
bucket_info_results(garage, bucket_id).await helper
.unset_global_bucket_alias(bucket_id, &self.alias)
.await?;
Ok(GlobalUnaliasBucketResponse(
bucket_info_results(garage, bucket_id).await?,
))
}
} }
pub async fn handle_local_alias_bucket( #[async_trait]
garage: &Arc<Garage>, impl EndpointHandler for LocalAliasBucketRequest {
bucket_id: String, type Response = LocalAliasBucketResponse;
access_key_id: String,
alias: String,
) -> Result<Response<ResBody>, Error> {
let bucket_id = parse_bucket_id(&bucket_id)?;
let helper = garage.locked_helper().await; async fn handle(self, garage: &Arc<Garage>) -> Result<LocalAliasBucketResponse, Error> {
let bucket_id = parse_bucket_id(&self.id)?;
helper let helper = garage.locked_helper().await;
.set_local_bucket_alias(bucket_id, &access_key_id, &alias)
.await?;
bucket_info_results(garage, bucket_id).await helper
.set_local_bucket_alias(bucket_id, &self.access_key_id, &self.alias)
.await?;
Ok(LocalAliasBucketResponse(
bucket_info_results(garage, bucket_id).await?,
))
}
} }
pub async fn handle_local_unalias_bucket( #[async_trait]
garage: &Arc<Garage>, impl EndpointHandler for LocalUnaliasBucketRequest {
bucket_id: String, type Response = LocalUnaliasBucketResponse;
access_key_id: String,
alias: String,
) -> Result<Response<ResBody>, Error> {
let bucket_id = parse_bucket_id(&bucket_id)?;
let helper = garage.locked_helper().await; async fn handle(self, garage: &Arc<Garage>) -> Result<LocalUnaliasBucketResponse, Error> {
let bucket_id = parse_bucket_id(&self.id)?;
helper let helper = garage.locked_helper().await;
.unset_local_bucket_alias(bucket_id, &access_key_id, &alias)
.await?;
bucket_info_results(garage, bucket_id).await helper
.unset_local_bucket_alias(bucket_id, &self.access_key_id, &self.alias)
.await?;
Ok(LocalUnaliasBucketResponse(
bucket_info_results(garage, bucket_id).await?,
))
}
} }
// ---- HELPER ---- // ---- HELPER ----

View file

@ -2,7 +2,6 @@ use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use hyper::{body::Incoming as IncomingBody, Request, Response};
use garage_util::crdt::*; use garage_util::crdt::*;
use garage_util::data::*; use garage_util::data::*;
@ -14,14 +13,13 @@ use garage_model::garage::Garage;
use crate::admin::api::{ use crate::admin::api::{
ApplyClusterLayoutRequest, ApplyClusterLayoutResponse, ConnectClusterNodeResponse, ApplyClusterLayoutRequest, ApplyClusterLayoutResponse, ConnectClusterNodeResponse,
ConnectClusterNodesRequest, ConnectClusterNodesResponse, FreeSpaceResp, ConnectClusterNodesRequest, ConnectClusterNodesResponse, FreeSpaceResp,
GetClusterHealthRequest, GetClusterHealthResponse, GetClusterLayoutResponse, GetClusterHealthRequest, GetClusterHealthResponse, GetClusterLayoutRequest,
GetClusterStatusRequest, GetClusterStatusResponse, NodeResp, NodeRoleChange, GetClusterLayoutResponse, GetClusterStatusRequest, GetClusterStatusResponse, NodeResp,
NodeRoleChangeEnum, NodeRoleResp, UpdateClusterLayoutRequest, NodeRoleChange, NodeRoleChangeEnum, NodeRoleResp, RevertClusterLayoutRequest,
RevertClusterLayoutResponse, UpdateClusterLayoutRequest, UpdateClusterLayoutResponse,
}; };
use crate::admin::api_server::ResBody;
use crate::admin::error::*; use crate::admin::error::*;
use crate::admin::EndpointHandler; use crate::admin::EndpointHandler;
use crate::helpers::{json_ok_response, parse_json_body};
#[async_trait] #[async_trait]
impl EndpointHandler for GetClusterStatusRequest { impl EndpointHandler for GetClusterStatusRequest {
@ -149,17 +147,6 @@ impl EndpointHandler for GetClusterHealthRequest {
} }
} }
pub async fn handle_connect_cluster_nodes(
garage: &Arc<Garage>,
req: Request<IncomingBody>,
) -> Result<Response<ResBody>, Error> {
let req = parse_json_body::<ConnectClusterNodesRequest, _, Error>(req).await?;
let res = req.handle(garage).await?;
Ok(json_ok_response(&res)?)
}
#[async_trait] #[async_trait]
impl EndpointHandler for ConnectClusterNodesRequest { impl EndpointHandler for ConnectClusterNodesRequest {
type Response = ConnectClusterNodesResponse; type Response = ConnectClusterNodesResponse;
@ -183,10 +170,15 @@ impl EndpointHandler for ConnectClusterNodesRequest {
} }
} }
pub async fn handle_get_cluster_layout(garage: &Arc<Garage>) -> Result<Response<ResBody>, Error> { #[async_trait]
let res = format_cluster_layout(garage.system.cluster_layout().inner()); impl EndpointHandler for GetClusterLayoutRequest {
type Response = GetClusterLayoutResponse;
Ok(json_ok_response(&res)?) async fn handle(self, garage: &Arc<Garage>) -> Result<GetClusterLayoutResponse, Error> {
Ok(format_cluster_layout(
garage.system.cluster_layout().inner(),
))
}
} }
fn format_cluster_layout(layout: &layout::LayoutHistory) -> GetClusterLayoutResponse { fn format_cluster_layout(layout: &layout::LayoutHistory) -> GetClusterLayoutResponse {
@ -238,85 +230,87 @@ fn format_cluster_layout(layout: &layout::LayoutHistory) -> GetClusterLayoutResp
// ---- update functions ---- // ---- update functions ----
pub async fn handle_update_cluster_layout( #[async_trait]
garage: &Arc<Garage>, impl EndpointHandler for UpdateClusterLayoutRequest {
req: Request<IncomingBody>, type Response = UpdateClusterLayoutResponse;
) -> Result<Response<ResBody>, Error> {
let updates = parse_json_body::<UpdateClusterLayoutRequest, _, Error>(req).await?;
let mut layout = garage.system.cluster_layout().inner().clone(); async fn handle(self, garage: &Arc<Garage>) -> Result<UpdateClusterLayoutResponse, Error> {
let mut layout = garage.system.cluster_layout().inner().clone();
let mut roles = layout.current().roles.clone(); let mut roles = layout.current().roles.clone();
roles.merge(&layout.staging.get().roles); roles.merge(&layout.staging.get().roles);
for change in updates.0 { for change in self.0 {
let node = hex::decode(&change.id).ok_or_bad_request("Invalid node identifier")?; let node = hex::decode(&change.id).ok_or_bad_request("Invalid node identifier")?;
let node = Uuid::try_from(&node).ok_or_bad_request("Invalid node identifier")?; let node = Uuid::try_from(&node).ok_or_bad_request("Invalid node identifier")?;
let new_role = match change.action { let new_role = match change.action {
NodeRoleChangeEnum::Remove { remove: true } => None, NodeRoleChangeEnum::Remove { remove: true } => None,
NodeRoleChangeEnum::Update { NodeRoleChangeEnum::Update {
zone, zone,
capacity, capacity,
tags, tags,
} => Some(layout::NodeRole { } => Some(layout::NodeRole {
zone, zone,
capacity, capacity,
tags, tags,
}), }),
_ => return Err(Error::bad_request("Invalid layout change")), _ => return Err(Error::bad_request("Invalid layout change")),
}; };
layout layout
.staging .staging
.get_mut() .get_mut()
.roles .roles
.merge(&roles.update_mutator(node, layout::NodeRoleV(new_role))); .merge(&roles.update_mutator(node, layout::NodeRoleV(new_role)));
}
garage
.system
.layout_manager
.update_cluster_layout(&layout)
.await?;
let res = format_cluster_layout(&layout);
Ok(UpdateClusterLayoutResponse(res))
} }
garage
.system
.layout_manager
.update_cluster_layout(&layout)
.await?;
let res = format_cluster_layout(&layout);
Ok(json_ok_response(&res)?)
} }
pub async fn handle_apply_cluster_layout( #[async_trait]
garage: &Arc<Garage>, impl EndpointHandler for ApplyClusterLayoutRequest {
req: Request<IncomingBody>, type Response = ApplyClusterLayoutResponse;
) -> Result<Response<ResBody>, Error> {
let param = parse_json_body::<ApplyClusterLayoutRequest, _, Error>(req).await?;
let layout = garage.system.cluster_layout().inner().clone(); async fn handle(self, garage: &Arc<Garage>) -> Result<ApplyClusterLayoutResponse, Error> {
let (layout, msg) = layout.apply_staged_changes(Some(param.version))?; let layout = garage.system.cluster_layout().inner().clone();
let (layout, msg) = layout.apply_staged_changes(Some(self.version))?;
garage garage
.system .system
.layout_manager .layout_manager
.update_cluster_layout(&layout) .update_cluster_layout(&layout)
.await?; .await?;
let res = ApplyClusterLayoutResponse { Ok(ApplyClusterLayoutResponse {
message: msg, message: msg,
layout: format_cluster_layout(&layout), layout: format_cluster_layout(&layout),
}; })
Ok(json_ok_response(&res)?) }
} }
pub async fn handle_revert_cluster_layout( #[async_trait]
garage: &Arc<Garage>, impl EndpointHandler for RevertClusterLayoutRequest {
) -> Result<Response<ResBody>, Error> { type Response = RevertClusterLayoutResponse;
let layout = garage.system.cluster_layout().inner().clone();
let layout = layout.revert_staged_changes()?;
garage
.system
.layout_manager
.update_cluster_layout(&layout)
.await?;
let res = format_cluster_layout(&layout); async fn handle(self, garage: &Arc<Garage>) -> Result<RevertClusterLayoutResponse, Error> {
Ok(json_ok_response(&res)?) let layout = garage.system.cluster_layout().inner().clone();
let layout = layout.revert_staged_changes()?;
garage
.system
.layout_manager
.update_cluster_layout(&layout)
.await?;
let res = format_cluster_layout(&layout);
Ok(RevertClusterLayoutResponse(res))
}
} }

View file

@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use hyper::{body::Incoming as IncomingBody, Request, Response, StatusCode}; use async_trait::async_trait;
use garage_table::*; use garage_table::*;
@ -9,138 +9,149 @@ use garage_model::garage::Garage;
use garage_model::key_table::*; use garage_model::key_table::*;
use crate::admin::api::{ use crate::admin::api::{
ApiBucketKeyPerm, CreateKeyRequest, GetKeyInfoResponse, ImportKeyRequest, ApiBucketKeyPerm, CreateKeyRequest, CreateKeyResponse, DeleteKeyRequest, DeleteKeyResponse,
KeyInfoBucketResponse, KeyPerm, ListKeysResponseItem, UpdateKeyRequest, GetKeyInfoRequest, GetKeyInfoResponse, ImportKeyRequest, ImportKeyResponse,
KeyInfoBucketResponse, KeyPerm, ListKeysRequest, ListKeysResponse, ListKeysResponseItem,
UpdateKeyRequest, UpdateKeyResponse,
}; };
use crate::admin::api_server::ResBody;
use crate::admin::error::*; use crate::admin::error::*;
use crate::helpers::*; use crate::admin::EndpointHandler;
pub async fn handle_list_keys(garage: &Arc<Garage>) -> Result<Response<ResBody>, Error> { #[async_trait]
let res = garage impl EndpointHandler for ListKeysRequest {
.key_table type Response = ListKeysResponse;
.get_range(
&EmptyKey,
None,
Some(KeyFilter::Deleted(DeletedFilter::NotDeleted)),
10000,
EnumerationOrder::Forward,
)
.await?
.iter()
.map(|k| ListKeysResponseItem {
id: k.key_id.to_string(),
name: k.params().unwrap().name.get().clone(),
})
.collect::<Vec<_>>();
Ok(json_ok_response(&res)?) async fn handle(self, garage: &Arc<Garage>) -> Result<ListKeysResponse, Error> {
} let res = garage
.key_table
pub async fn handle_get_key_info( .get_range(
garage: &Arc<Garage>, &EmptyKey,
id: Option<String>, None,
search: Option<String>, Some(KeyFilter::Deleted(DeletedFilter::NotDeleted)),
show_secret_key: bool, 10000,
) -> Result<Response<ResBody>, Error> { EnumerationOrder::Forward,
let key = if let Some(id) = id { )
garage.key_helper().get_existing_key(&id).await?
} else if let Some(search) = search {
garage
.key_helper()
.get_existing_matching_key(&search)
.await? .await?
} else { .iter()
unreachable!(); .map(|k| ListKeysResponseItem {
}; id: k.key_id.to_string(),
name: k.params().unwrap().name.get().clone(),
})
.collect::<Vec<_>>();
key_info_results(garage, key, show_secret_key).await Ok(ListKeysResponse(res))
}
pub async fn handle_create_key(
garage: &Arc<Garage>,
req: Request<IncomingBody>,
) -> Result<Response<ResBody>, Error> {
let req = parse_json_body::<CreateKeyRequest, _, Error>(req).await?;
let key = Key::new(req.name.as_deref().unwrap_or("Unnamed key"));
garage.key_table.insert(&key).await?;
key_info_results(garage, key, true).await
}
pub async fn handle_import_key(
garage: &Arc<Garage>,
req: Request<IncomingBody>,
) -> Result<Response<ResBody>, Error> {
let req = parse_json_body::<ImportKeyRequest, _, Error>(req).await?;
let prev_key = garage.key_table.get(&EmptyKey, &req.access_key_id).await?;
if prev_key.is_some() {
return Err(Error::KeyAlreadyExists(req.access_key_id.to_string()));
} }
let imported_key = Key::import(
&req.access_key_id,
&req.secret_access_key,
req.name.as_deref().unwrap_or("Imported key"),
)
.ok_or_bad_request("Invalid key format")?;
garage.key_table.insert(&imported_key).await?;
key_info_results(garage, imported_key, false).await
} }
pub async fn handle_update_key( #[async_trait]
garage: &Arc<Garage>, impl EndpointHandler for GetKeyInfoRequest {
id: String, type Response = GetKeyInfoResponse;
req: Request<IncomingBody>,
) -> Result<Response<ResBody>, Error> {
let req = parse_json_body::<UpdateKeyRequest, _, Error>(req).await?;
let mut key = garage.key_helper().get_existing_key(&id).await?; async fn handle(self, garage: &Arc<Garage>) -> Result<GetKeyInfoResponse, Error> {
let key = if let Some(id) = self.id {
garage.key_helper().get_existing_key(&id).await?
} else if let Some(search) = self.search {
garage
.key_helper()
.get_existing_matching_key(&search)
.await?
} else {
unreachable!();
};
let key_state = key.state.as_option_mut().unwrap(); Ok(key_info_results(garage, key, self.show_secret_key).await?)
if let Some(new_name) = req.name {
key_state.name.update(new_name);
} }
if let Some(allow) = req.allow { }
if allow.create_bucket {
key_state.allow_create_bucket.update(true); #[async_trait]
impl EndpointHandler for CreateKeyRequest {
type Response = CreateKeyResponse;
async fn handle(self, garage: &Arc<Garage>) -> Result<CreateKeyResponse, Error> {
let key = Key::new(self.name.as_deref().unwrap_or("Unnamed key"));
garage.key_table.insert(&key).await?;
Ok(CreateKeyResponse(
key_info_results(garage, key, true).await?,
))
}
}
#[async_trait]
impl EndpointHandler for ImportKeyRequest {
type Response = ImportKeyResponse;
async fn handle(self, garage: &Arc<Garage>) -> Result<ImportKeyResponse, Error> {
let prev_key = garage.key_table.get(&EmptyKey, &self.access_key_id).await?;
if prev_key.is_some() {
return Err(Error::KeyAlreadyExists(self.access_key_id.to_string()));
} }
}
if let Some(deny) = req.deny {
if deny.create_bucket {
key_state.allow_create_bucket.update(false);
}
}
garage.key_table.insert(&key).await?; let imported_key = Key::import(
&self.access_key_id,
&self.secret_access_key,
self.name.as_deref().unwrap_or("Imported key"),
)
.ok_or_bad_request("Invalid key format")?;
garage.key_table.insert(&imported_key).await?;
key_info_results(garage, key, false).await Ok(ImportKeyResponse(
key_info_results(garage, imported_key, false).await?,
))
}
} }
pub async fn handle_delete_key( #[async_trait]
garage: &Arc<Garage>, impl EndpointHandler for UpdateKeyRequest {
id: String, type Response = UpdateKeyResponse;
) -> Result<Response<ResBody>, Error> {
let helper = garage.locked_helper().await;
let mut key = helper.key().get_existing_key(&id).await?; async fn handle(self, garage: &Arc<Garage>) -> Result<UpdateKeyResponse, Error> {
let mut key = garage.key_helper().get_existing_key(&self.id).await?;
helper.delete_key(&mut key).await?; let key_state = key.state.as_option_mut().unwrap();
Ok(Response::builder() if let Some(new_name) = self.params.name {
.status(StatusCode::NO_CONTENT) key_state.name.update(new_name);
.body(empty_body())?) }
if let Some(allow) = self.params.allow {
if allow.create_bucket {
key_state.allow_create_bucket.update(true);
}
}
if let Some(deny) = self.params.deny {
if deny.create_bucket {
key_state.allow_create_bucket.update(false);
}
}
garage.key_table.insert(&key).await?;
Ok(UpdateKeyResponse(
key_info_results(garage, key, false).await?,
))
}
}
#[async_trait]
impl EndpointHandler for DeleteKeyRequest {
type Response = DeleteKeyResponse;
async fn handle(self, garage: &Arc<Garage>) -> Result<DeleteKeyResponse, Error> {
let helper = garage.locked_helper().await;
let mut key = helper.key().get_existing_key(&self.id).await?;
helper.delete_key(&mut key).await?;
Ok(DeleteKeyResponse)
}
} }
async fn key_info_results( async fn key_info_results(
garage: &Arc<Garage>, garage: &Arc<Garage>,
key: Key, key: Key,
show_secret: bool, show_secret: bool,
) -> Result<Response<ResBody>, Error> { ) -> Result<GetKeyInfoResponse, Error> {
let mut relevant_buckets = HashMap::new(); let mut relevant_buckets = HashMap::new();
let key_state = key.state.as_option().unwrap(); let key_state = key.state.as_option().unwrap();
@ -211,5 +222,5 @@ async fn key_info_results(
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
}; };
Ok(json_ok_response(&res)?) Ok(res)
} }