2020-05-01 15:52:35 +00:00
|
|
|
use std::fmt::Write;
|
2020-05-04 13:09:23 +00:00
|
|
|
use std::sync::Arc;
|
2020-05-01 15:52:35 +00:00
|
|
|
|
|
|
|
use hyper::{Body, Request, Response};
|
2020-04-28 10:18:14 +00:00
|
|
|
|
|
|
|
use garage_util::data::*;
|
2021-03-15 15:21:41 +00:00
|
|
|
use garage_util::time::*;
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2020-07-07 11:59:22 +00:00
|
|
|
use garage_model::garage::Garage;
|
|
|
|
use garage_model::object_table::*;
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2020-05-01 15:52:35 +00:00
|
|
|
use crate::encoding::*;
|
2020-11-08 14:04:30 +00:00
|
|
|
use crate::error::*;
|
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,
|
|
|
|
bucket: &str,
|
|
|
|
key: &str,
|
|
|
|
) -> 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
|
|
|
|
.get(&bucket.to_string(), &key.to_string())
|
|
|
|
.await?
|
2020-11-11 15:12:42 +00:00
|
|
|
.ok_or(Error::NotFound)?; // No need to delete
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2020-07-08 15:34:37 +00:00
|
|
|
let interesting_versions = object.versions().iter().filter(|v| match v.state {
|
|
|
|
ObjectVersionState::Aborted => false,
|
|
|
|
ObjectVersionState::Complete(ObjectVersionData::DeleteMarker) => false,
|
|
|
|
_ => true,
|
2020-04-28 10:18:14 +00:00
|
|
|
});
|
|
|
|
|
2021-03-15 14:26:29 +00:00
|
|
|
let mut version_to_delete = None;
|
2020-04-28 10:18:14 +00:00
|
|
|
let mut timestamp = now_msec();
|
|
|
|
for v in interesting_versions {
|
2021-03-15 14:26:29 +00:00
|
|
|
if v.timestamp + 1 > timestamp || version_to_delete.is_none() {
|
|
|
|
version_to_delete = Some(v.uuid);
|
2020-05-04 13:09:23 +00:00
|
|
|
}
|
2020-04-28 10:18:14 +00:00
|
|
|
timestamp = std::cmp::max(timestamp, v.timestamp + 1);
|
|
|
|
}
|
|
|
|
|
2021-03-15 14:26:29 +00:00
|
|
|
let deleted_version = version_to_delete.ok_or(Error::NotFound)?;
|
2020-04-28 10:18:14 +00:00
|
|
|
|
|
|
|
let version_uuid = gen_uuid();
|
|
|
|
|
|
|
|
let object = Object::new(
|
|
|
|
bucket.into(),
|
|
|
|
key.into(),
|
|
|
|
vec![ObjectVersion {
|
|
|
|
uuid: version_uuid,
|
|
|
|
timestamp: now_msec(),
|
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?;
|
2020-05-01 15:52:35 +00:00
|
|
|
return Ok((deleted_version, version_uuid));
|
|
|
|
}
|
|
|
|
|
2020-05-04 13:09:23 +00:00
|
|
|
pub async fn handle_delete(
|
|
|
|
garage: Arc<Garage>,
|
|
|
|
bucket: &str,
|
|
|
|
key: &str,
|
2020-07-07 15:15:53 +00:00
|
|
|
) -> Result<Response<Body>, Error> {
|
2020-05-04 13:09:23 +00:00
|
|
|
let (_deleted_version, delete_marker_version) =
|
|
|
|
handle_delete_internal(&garage, bucket, key).await?;
|
2020-05-01 15:52:35 +00:00
|
|
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
.header("x-amz-version-id", hex::encode(delete_marker_version))
|
2020-07-07 15:15:53 +00:00
|
|
|
.body(Body::from(vec![]))
|
2020-05-01 15:52:35 +00:00
|
|
|
.unwrap())
|
|
|
|
}
|
|
|
|
|
2020-05-04 13:09:23 +00:00
|
|
|
pub async fn handle_delete_objects(
|
|
|
|
garage: Arc<Garage>,
|
|
|
|
bucket: &str,
|
|
|
|
req: Request<Body>,
|
2021-02-19 23:13:07 +00:00
|
|
|
content_sha256: Option<Hash>,
|
2020-07-07 15:15:53 +00:00
|
|
|
) -> Result<Response<Body>, Error> {
|
2020-05-04 13:09:23 +00:00
|
|
|
let body = hyper::body::to_bytes(req.into_body()).await?;
|
2021-02-19 23:13:07 +00:00
|
|
|
verify_signed_content(content_sha256, &body[..])?;
|
|
|
|
|
2020-05-04 13:09:23 +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
|
|
|
|
|
|
|
let mut retxml = String::new();
|
2020-05-01 15:52:35 +00:00
|
|
|
writeln!(&mut retxml, r#"<?xml version="1.0" encoding="UTF-8"?>"#).unwrap();
|
|
|
|
writeln!(&mut retxml, "<DeleteObjectsOutput>").unwrap();
|
|
|
|
|
2020-05-04 13:09:23 +00:00
|
|
|
for obj in cmd.objects.iter() {
|
|
|
|
match handle_delete_internal(&garage, bucket, &obj.key).await {
|
|
|
|
Ok((deleted_version, delete_marker_version)) => {
|
|
|
|
writeln!(&mut retxml, "\t<Deleted>").unwrap();
|
2021-02-19 22:40:18 +00:00
|
|
|
writeln!(&mut retxml, "\t\t<Key>{}</Key>", xml_escape(&obj.key)).unwrap();
|
2020-05-04 13:09:23 +00:00
|
|
|
writeln!(
|
|
|
|
&mut retxml,
|
|
|
|
"\t\t<VersionId>{}</VersionId>",
|
|
|
|
hex::encode(deleted_version)
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
writeln!(
|
|
|
|
&mut retxml,
|
|
|
|
"\t\t<DeleteMarkerVersionId>{}</DeleteMarkerVersionId>",
|
|
|
|
hex::encode(delete_marker_version)
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
writeln!(&mut retxml, "\t</Deleted>").unwrap();
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
writeln!(&mut retxml, "\t<Error>").unwrap();
|
|
|
|
writeln!(&mut retxml, "\t\t<Code>{}</Code>", e.http_status_code()).unwrap();
|
2021-02-19 22:40:18 +00:00
|
|
|
writeln!(&mut retxml, "\t\t<Key>{}</Key>", xml_escape(&obj.key)).unwrap();
|
2020-05-04 13:09:23 +00:00
|
|
|
writeln!(
|
|
|
|
&mut retxml,
|
|
|
|
"\t\t<Message>{}</Message>",
|
|
|
|
xml_escape(&format!("{}", e))
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
writeln!(&mut retxml, "\t</Error>").unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 15:52:35 +00:00
|
|
|
|
|
|
|
writeln!(&mut retxml, "</DeleteObjectsOutput>").unwrap();
|
|
|
|
|
2021-02-19 22:40:18 +00:00
|
|
|
Ok(Response::builder()
|
2021-02-23 17:46:25 +00:00
|
|
|
.header("Content-Type", "application/xml")
|
|
|
|
.body(Body::from(retxml.into_bytes()))?)
|
2020-05-01 15:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DeleteRequest {
|
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> {
|
2020-05-04 13:09:23 +00:00
|
|
|
let mut ret = DeleteRequest { objects: vec![] };
|
|
|
|
|
|
|
|
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(),
|
|
|
|
});
|
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
|
|
|
}
|