2020-05-04 13:09:23 +00:00
|
|
|
use std::sync::Arc;
|
2020-05-01 15:52:35 +00:00
|
|
|
|
2024-02-05 17:49:54 +00:00
|
|
|
use http_body_util::BodyExt;
|
|
|
|
use hyper::{Request, Response, StatusCode};
|
2020-04-28 10:18:14 +00:00
|
|
|
|
|
|
|
use garage_util::data::*;
|
|
|
|
|
2020-07-07 11:59:22 +00:00
|
|
|
use garage_model::garage::Garage;
|
2022-05-10 11:16:57 +00:00
|
|
|
use garage_model::s3::object_table::*;
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2024-02-05 17:49:54 +00:00
|
|
|
use crate::helpers::*;
|
|
|
|
use crate::s3::api_server::{ReqBody, ResBody};
|
2022-05-24 10:16:39 +00:00
|
|
|
use crate::s3::error::*;
|
2023-10-20 11:55:34 +00:00
|
|
|
use crate::s3::put::next_timestamp;
|
2022-05-10 11:16:57 +00:00
|
|
|
use crate::s3::xml as s3_xml;
|
2021-02-19 23:13:07 +00:00
|
|
|
use crate::signature::verify_signed_content;
|
2020-05-01 15:52:35 +00:00
|
|
|
|
2020-05-04 13:09:23 +00:00
|
|
|
async fn handle_delete_internal(
|
|
|
|
garage: &Garage,
|
2021-12-14 12:55:11 +00:00
|
|
|
bucket_id: Uuid,
|
2020-05-04 13:09:23 +00:00
|
|
|
key: &str,
|
2021-05-02 21:13:08 +00:00
|
|
|
) -> Result<(Uuid, Uuid), Error> {
|
2020-11-11 15:12:42 +00:00
|
|
|
let object = garage
|
2020-04-28 10:18:14 +00:00
|
|
|
.object_table
|
2021-12-14 12:55:11 +00:00
|
|
|
.get(&bucket_id, &key.to_string())
|
2020-04-28 10:18:14 +00:00
|
|
|
.await?
|
2022-01-05 16:07:36 +00:00
|
|
|
.ok_or(Error::NoSuchKey)?; // No need to delete
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2023-10-20 11:55:34 +00:00
|
|
|
let del_timestamp = next_timestamp(Some(&object));
|
|
|
|
let del_uuid = gen_uuid();
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2023-10-20 11:55:34 +00:00
|
|
|
let deleted_version = object
|
|
|
|
.versions()
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.find(|v| !matches!(&v.state, ObjectVersionState::Aborted))
|
|
|
|
.or_else(|| object.versions().iter().rev().next());
|
|
|
|
let deleted_version = match deleted_version {
|
|
|
|
Some(dv) => dv.uuid,
|
|
|
|
None => {
|
|
|
|
warn!("Object has no versions: {:?}", object);
|
|
|
|
Uuid::from([0u8; 32])
|
2020-05-04 13:09:23 +00:00
|
|
|
}
|
2023-10-20 11:55:34 +00:00
|
|
|
};
|
2020-04-28 10:18:14 +00:00
|
|
|
|
|
|
|
let object = Object::new(
|
2021-12-14 12:55:11 +00:00
|
|
|
bucket_id,
|
2020-04-28 10:18:14 +00:00
|
|
|
key.into(),
|
|
|
|
vec![ObjectVersion {
|
2023-10-20 11:55:34 +00:00
|
|
|
uuid: del_uuid,
|
|
|
|
timestamp: del_timestamp,
|
2020-07-08 15:33:24 +00:00
|
|
|
state: ObjectVersionState::Complete(ObjectVersionData::DeleteMarker),
|
2020-04-28 10:18:14 +00:00
|
|
|
}],
|
|
|
|
);
|
|
|
|
|
|
|
|
garage.object_table.insert(&object).await?;
|
2021-10-26 08:20:05 +00:00
|
|
|
|
2023-10-20 11:55:34 +00:00
|
|
|
Ok((deleted_version, del_uuid))
|
2020-05-01 15:52:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:09:23 +00:00
|
|
|
pub async fn handle_delete(
|
|
|
|
garage: Arc<Garage>,
|
2021-12-14 12:55:11 +00:00
|
|
|
bucket_id: Uuid,
|
2020-05-04 13:09:23 +00:00
|
|
|
key: &str,
|
2024-02-05 17:49:54 +00:00
|
|
|
) -> Result<Response<ResBody>, Error> {
|
2022-09-14 15:07:55 +00:00
|
|
|
match handle_delete_internal(&garage, bucket_id, key).await {
|
|
|
|
Ok(_) | Err(Error::NoSuchKey) => Ok(Response::builder()
|
|
|
|
.status(StatusCode::NO_CONTENT)
|
2024-02-05 17:49:54 +00:00
|
|
|
.body(empty_body())
|
2022-09-14 15:07:55 +00:00
|
|
|
.unwrap()),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
2020-05-01 15:52:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:09:23 +00:00
|
|
|
pub async fn handle_delete_objects(
|
|
|
|
garage: Arc<Garage>,
|
2021-12-14 12:55:11 +00:00
|
|
|
bucket_id: Uuid,
|
2024-02-05 17:49:54 +00:00
|
|
|
req: Request<ReqBody>,
|
2021-02-19 23:13:07 +00:00
|
|
|
content_sha256: Option<Hash>,
|
2024-02-05 17:49:54 +00:00
|
|
|
) -> Result<Response<ResBody>, Error> {
|
|
|
|
let body = BodyExt::collect(req.into_body()).await?.to_bytes();
|
2022-01-17 09:55:31 +00:00
|
|
|
|
|
|
|
if let Some(content_sha256) = content_sha256 {
|
|
|
|
verify_signed_content(content_sha256, &body[..])?;
|
|
|
|
}
|
2021-02-19 23:13:07 +00:00
|
|
|
|
2021-10-26 08:20:05 +00:00
|
|
|
let cmd_xml = roxmltree::Document::parse(std::str::from_utf8(&body)?)?;
|
2020-11-08 14:04:30 +00:00
|
|
|
let cmd = parse_delete_objects_xml(&cmd_xml).ok_or_bad_request("Invalid delete XML query")?;
|
2020-05-04 13:09:23 +00:00
|
|
|
|
2021-05-03 20:45:42 +00:00
|
|
|
let mut ret_deleted = Vec::new();
|
|
|
|
let mut ret_errors = Vec::new();
|
2020-05-01 15:52:35 +00:00
|
|
|
|
2020-05-04 13:09:23 +00:00
|
|
|
for obj in cmd.objects.iter() {
|
2021-12-14 12:55:11 +00:00
|
|
|
match handle_delete_internal(&garage, bucket_id, &obj.key).await {
|
2020-05-04 13:09:23 +00:00
|
|
|
Ok((deleted_version, delete_marker_version)) => {
|
2021-04-27 23:05:40 +00:00
|
|
|
if cmd.quiet {
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-03 20:45:42 +00:00
|
|
|
ret_deleted.push(s3_xml::Deleted {
|
|
|
|
key: s3_xml::Value(obj.key.clone()),
|
|
|
|
version_id: s3_xml::Value(hex::encode(deleted_version)),
|
|
|
|
delete_marker_version_id: s3_xml::Value(hex::encode(delete_marker_version)),
|
|
|
|
});
|
2020-05-04 13:09:23 +00:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-05-03 20:45:42 +00:00
|
|
|
ret_errors.push(s3_xml::DeleteError {
|
|
|
|
code: s3_xml::Value(e.aws_code().to_string()),
|
|
|
|
key: Some(s3_xml::Value(obj.key.clone())),
|
|
|
|
message: s3_xml::Value(format!("{}", e)),
|
|
|
|
version_id: None,
|
|
|
|
});
|
2020-05-04 13:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 15:52:35 +00:00
|
|
|
|
2021-05-03 20:45:42 +00:00
|
|
|
let xml = s3_xml::to_xml_with_header(&s3_xml::DeleteResult {
|
|
|
|
xmlns: (),
|
|
|
|
deleted: ret_deleted,
|
|
|
|
errors: ret_errors,
|
|
|
|
})?;
|
2020-05-01 15:52:35 +00:00
|
|
|
|
2021-02-19 22:40:18 +00:00
|
|
|
Ok(Response::builder()
|
2021-02-23 17:46:25 +00:00
|
|
|
.header("Content-Type", "application/xml")
|
2024-02-05 17:49:54 +00:00
|
|
|
.body(string_body(xml))?)
|
2020-05-01 15:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DeleteRequest {
|
2021-04-27 23:05:40 +00:00
|
|
|
quiet: bool,
|
2020-05-04 13:09:23 +00:00
|
|
|
objects: Vec<DeleteObject>,
|
2020-04-28 10:18:14 +00:00
|
|
|
}
|
2020-05-01 15:52:35 +00:00
|
|
|
|
|
|
|
struct DeleteObject {
|
2020-05-04 13:09:23 +00:00
|
|
|
key: String,
|
2020-05-01 15:52:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 23:13:07 +00:00
|
|
|
fn parse_delete_objects_xml(xml: &roxmltree::Document) -> Option<DeleteRequest> {
|
2021-04-27 23:05:40 +00:00
|
|
|
let mut ret = DeleteRequest {
|
|
|
|
quiet: false,
|
|
|
|
objects: vec![],
|
|
|
|
};
|
2020-05-04 13:09:23 +00:00
|
|
|
|
|
|
|
let root = xml.root();
|
2021-02-19 23:13:07 +00:00
|
|
|
let delete = root.first_child()?;
|
2020-11-11 15:12:42 +00:00
|
|
|
|
2020-05-04 13:09:23 +00:00
|
|
|
if !delete.has_tag_name("Delete") {
|
2021-02-19 23:13:07 +00:00
|
|
|
return None;
|
2020-05-04 13:09:23 +00:00
|
|
|
}
|
2020-05-01 15:52:35 +00:00
|
|
|
|
2020-05-04 13:09:23 +00:00
|
|
|
for item in delete.children() {
|
|
|
|
if item.has_tag_name("Object") {
|
2021-02-19 23:13:07 +00:00
|
|
|
let key = item.children().find(|e| e.has_tag_name("Key"))?;
|
|
|
|
let key_str = key.text()?;
|
|
|
|
ret.objects.push(DeleteObject {
|
|
|
|
key: key_str.to_string(),
|
|
|
|
});
|
2021-04-27 23:05:40 +00:00
|
|
|
} else if item.has_tag_name("Quiet") {
|
|
|
|
if item.text()? == "true" {
|
|
|
|
ret.quiet = true;
|
|
|
|
} else {
|
|
|
|
ret.quiet = false;
|
|
|
|
}
|
2020-05-04 13:09:23 +00:00
|
|
|
} else {
|
2021-02-19 23:13:07 +00:00
|
|
|
return None;
|
2020-05-04 13:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:13:07 +00:00
|
|
|
Some(ret)
|
2020-05-04 13:09:23 +00:00
|
|
|
}
|