admin api: make all handlers impls of a single trait
This commit is contained in:
parent
c1eb1610ba
commit
831f2b0207
5 changed files with 781 additions and 522 deletions
|
@ -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);
|
||||||
|
|
|
@ -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(
|
||||||
|
endpoint: Endpoint,
|
||||||
|
req: Request<IncomingBody>,
|
||||||
|
) -> Result<AdminApiRequest, Error> {
|
||||||
|
match endpoint {
|
||||||
|
Endpoint::GetClusterStatus => {
|
||||||
|
Ok(AdminApiRequest::GetClusterStatus(GetClusterStatusRequest))
|
||||||
|
}
|
||||||
|
Endpoint::GetClusterHealth => {
|
||||||
|
Ok(AdminApiRequest::GetClusterHealth(GetClusterHealthRequest))
|
||||||
|
}
|
||||||
|
Endpoint::ConnectClusterNodes => {
|
||||||
|
let req = parse_json_body::<ConnectClusterNodesRequest, _, Error>(req).await?;
|
||||||
|
Ok(AdminApiRequest::ConnectClusterNodes(req))
|
||||||
|
}
|
||||||
// Layout
|
// Layout
|
||||||
Endpoint::GetClusterLayout => handle_get_cluster_layout(&self.garage).await,
|
Endpoint::GetClusterLayout => {
|
||||||
Endpoint::UpdateClusterLayout => handle_update_cluster_layout(&self.garage, req).await,
|
Ok(AdminApiRequest::GetClusterLayout(GetClusterLayoutRequest))
|
||||||
Endpoint::ApplyClusterLayout => handle_apply_cluster_layout(&self.garage, req).await,
|
}
|
||||||
Endpoint::RevertClusterLayout => handle_revert_cluster_layout(&self.garage).await,
|
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
|
// Keys
|
||||||
Endpoint::ListKeys => handle_list_keys(&self.garage).await,
|
Endpoint::ListKeys => Ok(AdminApiRequest::ListKeys(ListKeysRequest)),
|
||||||
Endpoint::GetKeyInfo {
|
Endpoint::GetKeyInfo {
|
||||||
id,
|
id,
|
||||||
search,
|
search,
|
||||||
show_secret_key,
|
show_secret_key,
|
||||||
} => {
|
} => {
|
||||||
let show_secret_key = show_secret_key.map(|x| x == "true").unwrap_or(false);
|
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
|
Ok(AdminApiRequest::GetKeyInfo(GetKeyInfoRequest {
|
||||||
|
id,
|
||||||
|
search,
|
||||||
|
show_secret_key,
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
Endpoint::CreateKey => handle_create_key(&self.garage, req).await,
|
Endpoint::CreateKey => {
|
||||||
Endpoint::ImportKey => handle_import_key(&self.garage, req).await,
|
let req = parse_json_body::<CreateKeyRequest, _, Error>(req).await?;
|
||||||
Endpoint::UpdateKey { id } => handle_update_key(&self.garage, id, req).await,
|
Ok(AdminApiRequest::CreateKey(req))
|
||||||
Endpoint::DeleteKey { id } => handle_delete_key(&self.garage, id).await,
|
}
|
||||||
|
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
|
// Buckets
|
||||||
Endpoint::ListBuckets => handle_list_buckets(&self.garage).await,
|
Endpoint::ListBuckets => Ok(AdminApiRequest::ListBuckets(ListBucketsRequest)),
|
||||||
Endpoint::GetBucketInfo { id, global_alias } => {
|
Endpoint::GetBucketInfo { id, global_alias } => {
|
||||||
handle_get_bucket_info(&self.garage, id, global_alias).await
|
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,
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
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
|
// Bucket-key permissions
|
||||||
Endpoint::BucketAllowKey => {
|
Endpoint::BucketAllowKey => {
|
||||||
handle_bucket_change_key_perm(&self.garage, req, true).await
|
let req = parse_json_body::<BucketKeyPermChangeRequest, _, Error>(req).await?;
|
||||||
|
Ok(AdminApiRequest::BucketAllowKey(BucketAllowKeyRequest(req)))
|
||||||
}
|
}
|
||||||
Endpoint::BucketDenyKey => {
|
Endpoint::BucketDenyKey => {
|
||||||
handle_bucket_change_key_perm(&self.garage, req, false).await
|
let req = parse_json_body::<BucketKeyPermChangeRequest, _, Error>(req).await?;
|
||||||
|
Ok(AdminApiRequest::BucketDenyKey(BucketDenyKeyRequest(req)))
|
||||||
}
|
}
|
||||||
// Bucket aliasing
|
// Bucket aliasing
|
||||||
Endpoint::GlobalAliasBucket { id, alias } => {
|
Endpoint::GlobalAliasBucket { id, alias } => Ok(AdminApiRequest::GlobalAliasBucket(
|
||||||
handle_global_alias_bucket(&self.garage, id, alias).await
|
GlobalAliasBucketRequest { id, alias },
|
||||||
}
|
)),
|
||||||
Endpoint::GlobalUnaliasBucket { id, alias } => {
|
Endpoint::GlobalUnaliasBucket { id, alias } => Ok(AdminApiRequest::GlobalUnaliasBucket(
|
||||||
handle_global_unalias_bucket(&self.garage, id, alias).await
|
GlobalUnaliasBucketRequest { id, alias },
|
||||||
}
|
)),
|
||||||
Endpoint::LocalAliasBucket {
|
Endpoint::LocalAliasBucket {
|
||||||
id,
|
id,
|
||||||
access_key_id,
|
access_key_id,
|
||||||
alias,
|
alias,
|
||||||
} => handle_local_alias_bucket(&self.garage, id, access_key_id, alias).await,
|
} => Ok(AdminApiRequest::LocalAliasBucket(LocalAliasBucketRequest {
|
||||||
|
access_key_id,
|
||||||
|
id,
|
||||||
|
alias,
|
||||||
|
})),
|
||||||
Endpoint::LocalUnaliasBucket {
|
Endpoint::LocalUnaliasBucket {
|
||||||
id,
|
id,
|
||||||
access_key_id,
|
access_key_id,
|
||||||
alias,
|
alias,
|
||||||
} => handle_local_unalias_bucket(&self.garage, id, access_key_id, alias).await,
|
} => Ok(AdminApiRequest::LocalUnaliasBucket(
|
||||||
}
|
LocalUnaliasBucketRequest {
|
||||||
|
access_key_id,
|
||||||
|
id,
|
||||||
|
alias,
|
||||||
|
},
|
||||||
|
)),
|
||||||
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,16 +18,24 @@ 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]
|
||||||
|
impl EndpointHandler for ListBucketsRequest {
|
||||||
|
type Response = ListBucketsResponse;
|
||||||
|
|
||||||
|
async fn handle(self, garage: &Arc<Garage>) -> Result<ListBucketsResponse, Error> {
|
||||||
let buckets = garage
|
let buckets = garage
|
||||||
.bucket_table
|
.bucket_table
|
||||||
.get_range(
|
.get_range(
|
||||||
|
@ -66,15 +74,16 @@ pub async fn handle_list_buckets(garage: &Arc<Garage>) -> Result<Response<ResBod
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
Ok(json_ok_response(&res)?)
|
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> {
|
async fn handle(self, garage: &Arc<Garage>) -> Result<GetBucketInfoResponse, Error> {
|
||||||
let bucket_id = match (id, global_alias) {
|
let bucket_id = match (self.id, self.global_alias) {
|
||||||
(Some(id), None) => parse_bucket_id(&id)?,
|
(Some(id), None) => parse_bucket_id(&id)?,
|
||||||
(None, Some(ga)) => garage
|
(None, Some(ga)) => garage
|
||||||
.bucket_helper()
|
.bucket_helper()
|
||||||
|
@ -90,11 +99,12 @@ pub async fn handle_get_bucket_info(
|
||||||
|
|
||||||
bucket_info_results(garage, bucket_id).await
|
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,18 +221,17 @@ 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?;
|
|
||||||
|
|
||||||
|
async fn handle(self, garage: &Arc<Garage>) -> Result<CreateBucketResponse, Error> {
|
||||||
let helper = garage.locked_helper().await;
|
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!(
|
||||||
"{}: {}",
|
"{}: {}",
|
||||||
|
@ -237,7 +246,7 @@ pub async fn handle_create_bucket(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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!(
|
||||||
"{}: {}",
|
"{}: {}",
|
||||||
|
@ -255,11 +264,11 @@ pub async fn handle_create_bucket(
|
||||||
let bucket = Bucket::new();
|
let bucket = Bucket::new();
|
||||||
garage.bucket_table.insert(&bucket).await?;
|
garage.bucket_table.insert(&bucket).await?;
|
||||||
|
|
||||||
if let Some(ga) = &req.global_alias {
|
if let Some(ga) = &self.global_alias {
|
||||||
helper.set_global_bucket_alias(bucket.id, ga).await?;
|
helper.set_global_bucket_alias(bucket.id, ga).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(la) = &req.local_alias {
|
if let Some(la) = &self.local_alias {
|
||||||
helper
|
helper
|
||||||
.set_local_bucket_alias(bucket.id, &la.access_key_id, &la.alias)
|
.set_local_bucket_alias(bucket.id, &la.access_key_id, &la.alias)
|
||||||
.await?;
|
.await?;
|
||||||
|
@ -280,16 +289,20 @@ pub async fn handle_create_bucket(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bucket_info_results(garage, bucket.id).await
|
Ok(CreateBucketResponse(
|
||||||
|
bucket_info_results(garage, bucket.id).await?,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_delete_bucket(
|
#[async_trait]
|
||||||
garage: &Arc<Garage>,
|
impl EndpointHandler for DeleteBucketRequest {
|
||||||
id: String,
|
type Response = DeleteBucketResponse;
|
||||||
) -> Result<Response<ResBody>, Error> {
|
|
||||||
|
async fn handle(self, garage: &Arc<Garage>) -> Result<DeleteBucketResponse, Error> {
|
||||||
let helper = garage.locked_helper().await;
|
let helper = garage.locked_helper().await;
|
||||||
|
|
||||||
let bucket_id = parse_bucket_id(&id)?;
|
let bucket_id = parse_bucket_id(&self.id)?;
|
||||||
|
|
||||||
let mut bucket = helper.bucket().get_existing_bucket(bucket_id).await?;
|
let mut bucket = helper.bucket().get_existing_bucket(bucket_id).await?;
|
||||||
let state = bucket.state.as_option().unwrap();
|
let state = bucket.state.as_option().unwrap();
|
||||||
|
@ -327,18 +340,16 @@ pub async fn handle_delete_bucket(
|
||||||
bucket.state = Deletable::delete();
|
bucket.state = Deletable::delete();
|
||||||
garage.bucket_table.insert(&bucket).await?;
|
garage.bucket_table.insert(&bucket).await?;
|
||||||
|
|
||||||
Ok(Response::builder()
|
Ok(DeleteBucketResponse)
|
||||||
.status(StatusCode::NO_CONTENT)
|
}
|
||||||
.body(empty_body())?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_update_bucket(
|
#[async_trait]
|
||||||
garage: &Arc<Garage>,
|
impl EndpointHandler for UpdateBucketRequest {
|
||||||
id: String,
|
type Response = UpdateBucketResponse;
|
||||||
req: Request<IncomingBody>,
|
|
||||||
) -> Result<Response<ResBody>, Error> {
|
async fn handle(self, garage: &Arc<Garage>) -> Result<UpdateBucketResponse, Error> {
|
||||||
let req = parse_json_body::<UpdateBucketRequest, _, Error>(req).await?;
|
let bucket_id = parse_bucket_id(&self.id)?;
|
||||||
let bucket_id = parse_bucket_id(&id)?;
|
|
||||||
|
|
||||||
let mut bucket = garage
|
let mut bucket = garage
|
||||||
.bucket_helper()
|
.bucket_helper()
|
||||||
|
@ -347,7 +358,7 @@ pub async fn handle_update_bucket(
|
||||||
|
|
||||||
let state = bucket.state.as_option_mut().unwrap();
|
let state = bucket.state.as_option_mut().unwrap();
|
||||||
|
|
||||||
if let Some(wa) = req.website_access {
|
if let Some(wa) = self.params.website_access {
|
||||||
if wa.enabled {
|
if wa.enabled {
|
||||||
state.website_config.update(Some(WebsiteConfig {
|
state.website_config.update(Some(WebsiteConfig {
|
||||||
index_document: wa.index_document.ok_or_bad_request(
|
index_document: wa.index_document.ok_or_bad_request(
|
||||||
|
@ -365,7 +376,7 @@ pub async fn handle_update_bucket(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(q) = req.quotas {
|
if let Some(q) = self.params.quotas {
|
||||||
state.quotas.update(BucketQuotas {
|
state.quotas.update(BucketQuotas {
|
||||||
max_size: q.max_size,
|
max_size: q.max_size,
|
||||||
max_objects: q.max_objects,
|
max_objects: q.max_objects,
|
||||||
|
@ -374,18 +385,39 @@ pub async fn handle_update_bucket(
|
||||||
|
|
||||||
garage.bucket_table.insert(&bucket).await?;
|
garage.bucket_table.insert(&bucket).await?;
|
||||||
|
|
||||||
bucket_info_results(garage, bucket_id).await
|
Ok(UpdateBucketResponse(
|
||||||
|
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?;
|
|
||||||
|
|
||||||
bucket_info_results(garage, bucket_id).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn handle_global_unalias_bucket(
|
|
||||||
garage: &Arc<Garage>,
|
|
||||||
bucket_id: String,
|
|
||||||
alias: String,
|
|
||||||
) -> Result<Response<ResBody>, Error> {
|
|
||||||
let bucket_id = parse_bucket_id(&bucket_id)?;
|
|
||||||
|
|
||||||
let helper = garage.locked_helper().await;
|
|
||||||
|
|
||||||
helper.unset_global_bucket_alias(bucket_id, &alias).await?;
|
|
||||||
|
|
||||||
bucket_info_results(garage, bucket_id).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn handle_local_alias_bucket(
|
|
||||||
garage: &Arc<Garage>,
|
|
||||||
bucket_id: String,
|
|
||||||
access_key_id: String,
|
|
||||||
alias: String,
|
|
||||||
) -> Result<Response<ResBody>, Error> {
|
|
||||||
let bucket_id = parse_bucket_id(&bucket_id)?;
|
|
||||||
|
|
||||||
let helper = garage.locked_helper().await;
|
let helper = garage.locked_helper().await;
|
||||||
|
|
||||||
helper
|
helper
|
||||||
.set_local_bucket_alias(bucket_id, &access_key_id, &alias)
|
.set_global_bucket_alias(bucket_id, &self.alias)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
bucket_info_results(garage, bucket_id).await
|
Ok(GlobalAliasBucketResponse(
|
||||||
|
bucket_info_results(garage, bucket_id).await?,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_local_unalias_bucket(
|
#[async_trait]
|
||||||
garage: &Arc<Garage>,
|
impl EndpointHandler for GlobalUnaliasBucketRequest {
|
||||||
bucket_id: String,
|
type Response = GlobalUnaliasBucketResponse;
|
||||||
access_key_id: String,
|
|
||||||
alias: String,
|
async fn handle(self, garage: &Arc<Garage>) -> Result<GlobalUnaliasBucketResponse, Error> {
|
||||||
) -> Result<Response<ResBody>, Error> {
|
let bucket_id = parse_bucket_id(&self.id)?;
|
||||||
let bucket_id = parse_bucket_id(&bucket_id)?;
|
|
||||||
|
|
||||||
let helper = garage.locked_helper().await;
|
let helper = garage.locked_helper().await;
|
||||||
|
|
||||||
helper
|
helper
|
||||||
.unset_local_bucket_alias(bucket_id, &access_key_id, &alias)
|
.unset_global_bucket_alias(bucket_id, &self.alias)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
bucket_info_results(garage, bucket_id).await
|
Ok(GlobalUnaliasBucketResponse(
|
||||||
|
bucket_info_results(garage, bucket_id).await?,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl EndpointHandler for LocalAliasBucketRequest {
|
||||||
|
type Response = LocalAliasBucketResponse;
|
||||||
|
|
||||||
|
async fn handle(self, garage: &Arc<Garage>) -> Result<LocalAliasBucketResponse, Error> {
|
||||||
|
let bucket_id = parse_bucket_id(&self.id)?;
|
||||||
|
|
||||||
|
let helper = garage.locked_helper().await;
|
||||||
|
|
||||||
|
helper
|
||||||
|
.set_local_bucket_alias(bucket_id, &self.access_key_id, &self.alias)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(LocalAliasBucketResponse(
|
||||||
|
bucket_info_results(garage, bucket_id).await?,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl EndpointHandler for LocalUnaliasBucketRequest {
|
||||||
|
type Response = LocalUnaliasBucketResponse;
|
||||||
|
|
||||||
|
async fn handle(self, garage: &Arc<Garage>) -> Result<LocalUnaliasBucketResponse, Error> {
|
||||||
|
let bucket_id = parse_bucket_id(&self.id)?;
|
||||||
|
|
||||||
|
let helper = garage.locked_helper().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 ----
|
||||||
|
|
|
@ -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,18 +230,17 @@ 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?;
|
|
||||||
|
|
||||||
|
async fn handle(self, garage: &Arc<Garage>) -> Result<UpdateClusterLayoutResponse, Error> {
|
||||||
let mut layout = garage.system.cluster_layout().inner().clone();
|
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")?;
|
||||||
|
|
||||||
|
@ -281,17 +272,17 @@ pub async fn handle_update_cluster_layout(
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let res = format_cluster_layout(&layout);
|
let res = format_cluster_layout(&layout);
|
||||||
Ok(json_ok_response(&res)?)
|
Ok(UpdateClusterLayoutResponse(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?;
|
|
||||||
|
|
||||||
|
async fn handle(self, garage: &Arc<Garage>) -> Result<ApplyClusterLayoutResponse, Error> {
|
||||||
let layout = garage.system.cluster_layout().inner().clone();
|
let layout = garage.system.cluster_layout().inner().clone();
|
||||||
let (layout, msg) = layout.apply_staged_changes(Some(param.version))?;
|
let (layout, msg) = layout.apply_staged_changes(Some(self.version))?;
|
||||||
|
|
||||||
garage
|
garage
|
||||||
.system
|
.system
|
||||||
|
@ -299,16 +290,18 @@ pub async fn handle_apply_cluster_layout(
|
||||||
.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;
|
||||||
|
|
||||||
|
async fn handle(self, garage: &Arc<Garage>) -> Result<RevertClusterLayoutResponse, Error> {
|
||||||
let layout = garage.system.cluster_layout().inner().clone();
|
let layout = garage.system.cluster_layout().inner().clone();
|
||||||
let layout = layout.revert_staged_changes()?;
|
let layout = layout.revert_staged_changes()?;
|
||||||
garage
|
garage
|
||||||
|
@ -318,5 +311,6 @@ pub async fn handle_revert_cluster_layout(
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let res = format_cluster_layout(&layout);
|
let res = format_cluster_layout(&layout);
|
||||||
Ok(json_ok_response(&res)?)
|
Ok(RevertClusterLayoutResponse(res))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,14 +9,19 @@ 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]
|
||||||
|
impl EndpointHandler for ListKeysRequest {
|
||||||
|
type Response = ListKeysResponse;
|
||||||
|
|
||||||
|
async fn handle(self, garage: &Arc<Garage>) -> Result<ListKeysResponse, Error> {
|
||||||
let res = garage
|
let res = garage
|
||||||
.key_table
|
.key_table
|
||||||
.get_range(
|
.get_range(
|
||||||
|
@ -34,18 +39,18 @@ pub async fn handle_list_keys(garage: &Arc<Garage>) -> Result<Response<ResBody>,
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
Ok(json_ok_response(&res)?)
|
Ok(ListKeysResponse(res))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_get_key_info(
|
#[async_trait]
|
||||||
garage: &Arc<Garage>,
|
impl EndpointHandler for GetKeyInfoRequest {
|
||||||
id: Option<String>,
|
type Response = GetKeyInfoResponse;
|
||||||
search: Option<String>,
|
|
||||||
show_secret_key: bool,
|
async fn handle(self, garage: &Arc<Garage>) -> Result<GetKeyInfoResponse, Error> {
|
||||||
) -> Result<Response<ResBody>, Error> {
|
let key = if let Some(id) = self.id {
|
||||||
let key = if let Some(id) = id {
|
|
||||||
garage.key_helper().get_existing_key(&id).await?
|
garage.key_helper().get_existing_key(&id).await?
|
||||||
} else if let Some(search) = search {
|
} else if let Some(search) = self.search {
|
||||||
garage
|
garage
|
||||||
.key_helper()
|
.key_helper()
|
||||||
.get_existing_matching_key(&search)
|
.get_existing_matching_key(&search)
|
||||||
|
@ -54,63 +59,66 @@ pub async fn handle_get_key_info(
|
||||||
unreachable!();
|
unreachable!();
|
||||||
};
|
};
|
||||||
|
|
||||||
key_info_results(garage, key, show_secret_key).await
|
Ok(key_info_results(garage, key, self.show_secret_key).await?)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_create_key(
|
#[async_trait]
|
||||||
garage: &Arc<Garage>,
|
impl EndpointHandler for CreateKeyRequest {
|
||||||
req: Request<IncomingBody>,
|
type Response = CreateKeyResponse;
|
||||||
) -> 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"));
|
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?;
|
garage.key_table.insert(&key).await?;
|
||||||
|
|
||||||
key_info_results(garage, key, true).await
|
Ok(CreateKeyResponse(
|
||||||
|
key_info_results(garage, key, true).await?,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_import_key(
|
#[async_trait]
|
||||||
garage: &Arc<Garage>,
|
impl EndpointHandler for ImportKeyRequest {
|
||||||
req: Request<IncomingBody>,
|
type Response = ImportKeyResponse;
|
||||||
) -> 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?;
|
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() {
|
if prev_key.is_some() {
|
||||||
return Err(Error::KeyAlreadyExists(req.access_key_id.to_string()));
|
return Err(Error::KeyAlreadyExists(self.access_key_id.to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let imported_key = Key::import(
|
let imported_key = Key::import(
|
||||||
&req.access_key_id,
|
&self.access_key_id,
|
||||||
&req.secret_access_key,
|
&self.secret_access_key,
|
||||||
req.name.as_deref().unwrap_or("Imported key"),
|
self.name.as_deref().unwrap_or("Imported key"),
|
||||||
)
|
)
|
||||||
.ok_or_bad_request("Invalid key format")?;
|
.ok_or_bad_request("Invalid key format")?;
|
||||||
garage.key_table.insert(&imported_key).await?;
|
garage.key_table.insert(&imported_key).await?;
|
||||||
|
|
||||||
key_info_results(garage, imported_key, false).await
|
Ok(ImportKeyResponse(
|
||||||
|
key_info_results(garage, imported_key, false).await?,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_update_key(
|
#[async_trait]
|
||||||
garage: &Arc<Garage>,
|
impl EndpointHandler for UpdateKeyRequest {
|
||||||
id: String,
|
type Response = UpdateKeyResponse;
|
||||||
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<UpdateKeyResponse, Error> {
|
||||||
|
let mut key = garage.key_helper().get_existing_key(&self.id).await?;
|
||||||
|
|
||||||
let key_state = key.state.as_option_mut().unwrap();
|
let key_state = key.state.as_option_mut().unwrap();
|
||||||
|
|
||||||
if let Some(new_name) = req.name {
|
if let Some(new_name) = self.params.name {
|
||||||
key_state.name.update(new_name);
|
key_state.name.update(new_name);
|
||||||
}
|
}
|
||||||
if let Some(allow) = req.allow {
|
if let Some(allow) = self.params.allow {
|
||||||
if allow.create_bucket {
|
if allow.create_bucket {
|
||||||
key_state.allow_create_bucket.update(true);
|
key_state.allow_create_bucket.update(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(deny) = req.deny {
|
if let Some(deny) = self.params.deny {
|
||||||
if deny.create_bucket {
|
if deny.create_bucket {
|
||||||
key_state.allow_create_bucket.update(false);
|
key_state.allow_create_bucket.update(false);
|
||||||
}
|
}
|
||||||
|
@ -118,29 +126,32 @@ pub async fn handle_update_key(
|
||||||
|
|
||||||
garage.key_table.insert(&key).await?;
|
garage.key_table.insert(&key).await?;
|
||||||
|
|
||||||
key_info_results(garage, key, false).await
|
Ok(UpdateKeyResponse(
|
||||||
|
key_info_results(garage, key, false).await?,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_delete_key(
|
#[async_trait]
|
||||||
garage: &Arc<Garage>,
|
impl EndpointHandler for DeleteKeyRequest {
|
||||||
id: String,
|
type Response = DeleteKeyResponse;
|
||||||
) -> Result<Response<ResBody>, Error> {
|
|
||||||
|
async fn handle(self, garage: &Arc<Garage>) -> Result<DeleteKeyResponse, Error> {
|
||||||
let helper = garage.locked_helper().await;
|
let helper = garage.locked_helper().await;
|
||||||
|
|
||||||
let mut key = helper.key().get_existing_key(&id).await?;
|
let mut key = helper.key().get_existing_key(&self.id).await?;
|
||||||
|
|
||||||
helper.delete_key(&mut key).await?;
|
helper.delete_key(&mut key).await?;
|
||||||
|
|
||||||
Ok(Response::builder()
|
Ok(DeleteKeyResponse)
|
||||||
.status(StatusCode::NO_CONTENT)
|
}
|
||||||
.body(empty_body())?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue