2021-12-14 12:55:11 +00:00
|
|
|
use std::collections::HashMap;
|
2021-04-27 23:05:40 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use hyper::{Body, Response};
|
|
|
|
|
|
|
|
use garage_model::garage::Garage;
|
2021-05-02 20:30:56 +00:00
|
|
|
use garage_model::key_table::Key;
|
2021-12-14 12:55:11 +00:00
|
|
|
use garage_table::util::EmptyKey;
|
|
|
|
use garage_util::crdt::*;
|
2021-05-02 20:30:56 +00:00
|
|
|
use garage_util::time::*;
|
2021-04-27 23:05:40 +00:00
|
|
|
|
|
|
|
use crate::error::*;
|
2021-05-03 20:45:42 +00:00
|
|
|
use crate::s3_xml;
|
2021-05-02 20:30:56 +00:00
|
|
|
|
2021-04-27 23:05:40 +00:00
|
|
|
pub fn handle_get_bucket_location(garage: Arc<Garage>) -> Result<Response<Body>, Error> {
|
2021-05-03 20:45:42 +00:00
|
|
|
let loc = s3_xml::LocationConstraint {
|
|
|
|
xmlns: (),
|
|
|
|
region: garage.config.s3_api.s3_region.to_string(),
|
|
|
|
};
|
|
|
|
let xml = s3_xml::to_xml_with_header(&loc)?;
|
2021-04-27 23:05:40 +00:00
|
|
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
.header("Content-Type", "application/xml")
|
|
|
|
.body(Body::from(xml.into_bytes()))?)
|
|
|
|
}
|
2021-05-02 20:30:56 +00:00
|
|
|
|
2021-05-14 20:33:26 +00:00
|
|
|
pub fn handle_get_bucket_versioning() -> Result<Response<Body>, Error> {
|
|
|
|
let versioning = s3_xml::VersioningConfiguration {
|
|
|
|
xmlns: (),
|
|
|
|
status: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let xml = s3_xml::to_xml_with_header(&versioning)?;
|
|
|
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
.header("Content-Type", "application/xml")
|
|
|
|
.body(Body::from(xml.into_bytes()))?)
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:55:11 +00:00
|
|
|
pub async fn handle_list_buckets(garage: &Garage, api_key: &Key) -> Result<Response<Body>, Error> {
|
|
|
|
let key_state = api_key.state.as_option().ok_or_internal_error(
|
|
|
|
"Key should not be in deleted state at this point (internal error)",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Collect buckets user has access to
|
|
|
|
let ids = api_key
|
|
|
|
.state
|
|
|
|
.as_option()
|
|
|
|
.unwrap()
|
|
|
|
.authorized_buckets
|
|
|
|
.items()
|
|
|
|
.iter()
|
2021-12-22 08:39:37 +00:00
|
|
|
.filter(|(_, perms)| perms.allow_read || perms.allow_write || perms.allow_owner)
|
2021-12-14 12:55:11 +00:00
|
|
|
.map(|(id, _)| *id)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let mut buckets_by_id = HashMap::new();
|
|
|
|
let mut aliases = HashMap::new();
|
|
|
|
|
|
|
|
for bucket_id in ids.iter() {
|
|
|
|
let bucket = garage.bucket_table.get(bucket_id, &EmptyKey).await?;
|
|
|
|
if let Some(bucket) = bucket {
|
|
|
|
if let Deletable::Present(param) = bucket.state {
|
|
|
|
for (alias, _, active) in param.aliases.items() {
|
|
|
|
if *active {
|
|
|
|
let alias_ent = garage.bucket_alias_table.get(&EmptyKey, alias).await?;
|
|
|
|
if let Some(alias_ent) = alias_ent {
|
2022-01-03 17:32:15 +00:00
|
|
|
if let Some(alias_bucket) = alias_ent.state.get() {
|
|
|
|
if alias_bucket == bucket_id {
|
2021-12-17 10:53:13 +00:00
|
|
|
aliases.insert(alias_ent.name().to_string(), *bucket_id);
|
2021-12-14 12:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buckets_by_id.insert(bucket_id, param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:32:15 +00:00
|
|
|
for (alias, _, id_opt) in key_state.local_aliases.items() {
|
|
|
|
if let Some(id) = id_opt {
|
2021-12-14 12:55:11 +00:00
|
|
|
aliases.insert(alias.clone(), *id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate response
|
2021-05-03 20:45:42 +00:00
|
|
|
let list_buckets = s3_xml::ListAllMyBucketsResult {
|
|
|
|
owner: s3_xml::Owner {
|
|
|
|
display_name: s3_xml::Value(api_key.name.get().to_string()),
|
|
|
|
id: s3_xml::Value(api_key.key_id.to_string()),
|
2021-05-02 20:30:56 +00:00
|
|
|
},
|
2021-05-03 20:45:42 +00:00
|
|
|
buckets: s3_xml::BucketList {
|
2021-12-14 12:55:11 +00:00
|
|
|
entries: aliases
|
2021-05-02 20:30:56 +00:00
|
|
|
.iter()
|
2021-12-14 12:55:11 +00:00
|
|
|
.filter_map(|(name, id)| buckets_by_id.get(id).map(|p| (name, id, p)))
|
|
|
|
.map(|(name, _id, param)| s3_xml::Bucket {
|
|
|
|
creation_date: s3_xml::Value(msec_to_rfc3339(param.creation_date)),
|
2021-05-03 20:45:42 +00:00
|
|
|
name: s3_xml::Value(name.to_string()),
|
2021-05-02 20:30:56 +00:00
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-05-03 20:45:42 +00:00
|
|
|
let xml = s3_xml::to_xml_with_header(&list_buckets)?;
|
2021-05-02 20:30:56 +00:00
|
|
|
trace!("xml: {}", xml);
|
|
|
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
.header("Content-Type", "application/xml")
|
|
|
|
.body(Body::from(xml))?)
|
|
|
|
}
|