aboutsummaryrefslogtreecommitdiff
path: root/src/api/s3_bucket.rs
blob: d1a4425a736b14c9f639e0e785b5136cac2071cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::fmt::Write;
use std::sync::Arc;

use hyper::{Body, Response};
use quick_xml::se::to_string;
use serde::Serialize;

use garage_model::garage::Garage;
use garage_model::key_table::Key;
use garage_util::time::*;

use crate::error::*;

#[derive(Debug, Serialize, PartialEq)]
struct CreationDate {
	#[serde(rename = "$value")]
	pub body: String,
}
#[derive(Debug, Serialize, PartialEq)]
struct Name {
	#[serde(rename = "$value")]
	pub body: String,
}
#[derive(Debug, Serialize, PartialEq)]
struct Bucket {
	#[serde(rename = "CreationDate")]
	pub creation_date: CreationDate,
	#[serde(rename = "Name")]
	pub name: Name,
}
#[derive(Debug, Serialize, PartialEq)]
struct DisplayName {
	#[serde(rename = "$value")]
	pub body: String,
}
#[derive(Debug, Serialize, PartialEq)]
struct ID {
	#[serde(rename = "$value")]
	pub body: String,
}
#[derive(Debug, Serialize, PartialEq)]
struct Owner {
	#[serde(rename = "DisplayName")]
	display_name: DisplayName,
	#[serde(rename = "ID")]
	id: ID,
}
#[derive(Debug, Serialize, PartialEq)]
struct BucketList {
	#[serde(rename = "Bucket")]
	pub entries: Vec<Bucket>,
}
#[derive(Debug, Serialize, PartialEq)]
struct ListAllMyBucketsResult {
	#[serde(rename = "Buckets")]
	buckets: BucketList,
	#[serde(rename = "Owner")]
	owner: Owner,
}

pub fn handle_get_bucket_location(garage: Arc<Garage>) -> Result<Response<Body>, Error> {
	let mut xml = String::new();

	writeln!(&mut xml, r#"<?xml version="1.0" encoding="UTF-8"?>"#).unwrap();
	writeln!(
		&mut xml,
		r#"<LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">{}</LocationConstraint>"#,
		garage.config.s3_api.s3_region
	)
	.unwrap();

	Ok(Response::builder()
		.header("Content-Type", "application/xml")
		.body(Body::from(xml.into_bytes()))?)
}

pub fn handle_list_buckets(api_key: &Key) -> Result<Response<Body>, Error> {
	let list_buckets = ListAllMyBucketsResult {
		owner: Owner {
			display_name: DisplayName {
				body: api_key.name.get().to_string(),
			},
			id: ID {
				body: api_key.key_id.to_string(),
			},
		},
		buckets: BucketList {
			entries: api_key
				.authorized_buckets
				.items()
				.iter()
				.map(|(name, ts, _)| Bucket {
					creation_date: CreationDate {
						body: msec_to_rfc3339(*ts),
					},
					name: Name {
						body: name.to_string(),
					},
				})
				.collect(),
		},
	};

	let mut xml = r#"<?xml version="1.0" encoding="UTF-8"?>"#.to_string();
	xml.push_str(&to_string(&list_buckets)?);
	trace!("xml: {}", xml);

	Ok(Response::builder()
		.header("Content-Type", "application/xml")
		.body(Body::from(xml))?)
}