Skip to content

Commit

Permalink
Move 'sign_with_time' method to 'S3Action'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Zafirov committed Oct 21, 2021
1 parent 85b30e6 commit 94903a6
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 247 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/target
Cargo.lock
cobertura.xml

.idea
29 changes: 12 additions & 17 deletions src/actions/create_bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ impl<'a> CreateBucket<'a> {
headers: Map::new(),
}
}
}

impl<'a> S3Action<'a> for CreateBucket<'a> {
const METHOD: Method = Method::Put;

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}

fn sign_with_time(&self, expires_in: Duration, time: &OffsetDateTime) -> Url {
let url = self.bucket.base_url().clone();
Expand All @@ -51,23 +63,6 @@ impl<'a> CreateBucket<'a> {
}
}

impl<'a> S3Action<'a> for CreateBucket<'a> {
const METHOD: Method = Method::Put;

fn sign(&self, expires_in: Duration) -> Url {
let now = OffsetDateTime::now_utc();
self.sign_with_time(expires_in, &now)
}

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}
}

#[cfg(test)]
mod tests {
use time::OffsetDateTime;
Expand Down
29 changes: 12 additions & 17 deletions src/actions/delete_bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ impl<'a> DeleteBucket<'a> {
headers: Map::new(),
}
}
}

impl<'a> S3Action<'a> for DeleteBucket<'a> {
const METHOD: Method = Method::Delete;

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}

fn sign_with_time(&self, expires_in: Duration, time: &OffsetDateTime) -> Url {
let url = self.bucket.base_url().clone();
Expand All @@ -53,23 +65,6 @@ impl<'a> DeleteBucket<'a> {
}
}

impl<'a> S3Action<'a> for DeleteBucket<'a> {
const METHOD: Method = Method::Delete;

fn sign(&self, expires_in: Duration) -> Url {
let now = OffsetDateTime::now_utc();
self.sign_with_time(expires_in, &now)
}

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}
}

#[cfg(test)]
mod tests {
use time::OffsetDateTime;
Expand Down
29 changes: 12 additions & 17 deletions src/actions/delete_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ impl<'a> DeleteObject<'a> {
headers: Map::new(),
}
}
}

impl<'a> S3Action<'a> for DeleteObject<'a> {
const METHOD: Method = Method::Delete;

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}

fn sign_with_time(&self, expires_in: Duration, time: &OffsetDateTime) -> Url {
let url = self.bucket.object_url(self.object).unwrap();
Expand All @@ -57,23 +69,6 @@ impl<'a> DeleteObject<'a> {
}
}

impl<'a> S3Action<'a> for DeleteObject<'a> {
const METHOD: Method = Method::Delete;

fn sign(&self, expires_in: Duration) -> Url {
let now = OffsetDateTime::now_utc();
self.sign_with_time(expires_in, &now)
}

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}
}

#[cfg(test)]
mod tests {
use time::OffsetDateTime;
Expand Down
49 changes: 22 additions & 27 deletions src/actions/delete_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,6 @@ impl<'a, I> DeleteObjects<'a, I>
where
I: Iterator<Item = &'a ObjectIdentifier>,
{
fn sign_with_time(&self, expires_in: Duration, time: &OffsetDateTime) -> Url {
let url = self.bucket.base_url().clone();
let query = SortingIterator::new(iter::once(("delete", "1")), self.query.iter());

match self.credentials {
Some(credentials) => sign(
time,
Method::Post,
url,
credentials.key(),
credentials.secret(),
credentials.token(),
self.bucket.region(),
expires_in.as_secs(),
query,
self.headers.iter(),
),
None => crate::signing::util::add_query_params(url, query),
}
}

pub fn body_with_md5(self) -> (String, String) {
#[derive(Serialize)]
#[serde(rename = "Delete")]
Expand All @@ -98,7 +77,6 @@ where
#[serde(rename = "Quiet")]
quiet: Option<bool>,
}

#[derive(Serialize)]
#[serde(rename = "Delete")]
struct Object<'a> {
Expand Down Expand Up @@ -129,6 +107,7 @@ where
};

let body = quick_xml::se::to_string(&req).unwrap();

let content_md5 = base64::encode(md5::compute(body.as_bytes()).as_ref());
(body, content_md5)
}
Expand All @@ -140,18 +119,34 @@ where
{
const METHOD: Method = Method::Post;

fn sign(&self, expires_in: Duration) -> Url {
let now = OffsetDateTime::now_utc();
self.sign_with_time(expires_in, &now)
}

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}

fn sign_with_time(&self, expires_in: Duration, time: &OffsetDateTime) -> Url {
let url = self.bucket.base_url().clone();
let query = SortingIterator::new(iter::once(("delete", "1")), self.query.iter());

match self.credentials {
Some(credentials) => sign(
time,
Method::Post,
url,
credentials.key(),
credentials.secret(),
credentials.token(),
self.bucket.region(),
expires_in.as_secs(),
query,
self.headers.iter(),
),
None => crate::signing::util::add_query_params(url, query),
}
}
}

#[cfg(test)]
Expand Down
29 changes: 12 additions & 17 deletions src/actions/get_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ impl<'a> GetObject<'a> {
headers: Map::new(),
}
}
}

impl<'a> S3Action<'a> for GetObject<'a> {
const METHOD: Method = Method::Get;

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}

fn sign_with_time(&self, expires_in: Duration, time: &OffsetDateTime) -> Url {
let url = self.bucket.object_url(self.object).unwrap();
Expand All @@ -57,23 +69,6 @@ impl<'a> GetObject<'a> {
}
}

impl<'a> S3Action<'a> for GetObject<'a> {
const METHOD: Method = Method::Get;

fn sign(&self, expires_in: Duration) -> Url {
let now = OffsetDateTime::now_utc();
self.sign_with_time(expires_in, &now)
}

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}
}

#[cfg(test)]
mod tests {
use time::OffsetDateTime;
Expand Down
29 changes: 12 additions & 17 deletions src/actions/head_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ impl<'a> HeadObject<'a> {
headers: Map::new(),
}
}
}

impl<'a> S3Action<'a> for HeadObject<'a> {
const METHOD: Method = Method::Head;

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}

fn sign_with_time(&self, expires_in: Duration, time: &OffsetDateTime) -> Url {
let url = self.bucket.object_url(self.object).unwrap();
Expand All @@ -57,23 +69,6 @@ impl<'a> HeadObject<'a> {
}
}

impl<'a> S3Action<'a> for HeadObject<'a> {
const METHOD: Method = Method::Head;

fn sign(&self, expires_in: Duration) -> Url {
let now = OffsetDateTime::now_utc();
self.sign_with_time(expires_in, &now)
}

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}
}

#[cfg(test)]
mod tests {
use time::OffsetDateTime;
Expand Down
29 changes: 12 additions & 17 deletions src/actions/list_objects_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ impl<'a> ListObjectsV2<'a> {

Ok(parsed)
}
}

impl<'a> S3Action<'a> for ListObjectsV2<'a> {
const METHOD: Method = Method::Get;

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}

fn sign_with_time(&self, expires_in: Duration, time: &OffsetDateTime) -> Url {
let url = self.bucket.base_url().clone();
Expand All @@ -139,23 +151,6 @@ impl<'a> ListObjectsV2<'a> {
}
}

impl<'a> S3Action<'a> for ListObjectsV2<'a> {
const METHOD: Method = Method::Get;

fn sign(&self, expires_in: Duration) -> Url {
let now = OffsetDateTime::now_utc();
self.sign_with_time(expires_in, &now)
}

fn query_mut(&mut self) -> &mut Map<'a> {
&mut self.query
}

fn headers_mut(&mut self) -> &mut Map<'a> {
&mut self.headers
}
}

#[cfg(test)]
mod tests {
use time::OffsetDateTime;
Expand Down
11 changes: 10 additions & 1 deletion src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ pub mod list_objects_v2;
mod multipart_upload;
mod put_object;

use time::OffsetDateTime;

/// A request which can be signed
pub trait S3Action<'a> {
const METHOD: Method;

/// Sign a request for this action, using `METHOD` for the [`Method`]
fn sign(&self, expires_in: Duration) -> Url;
fn sign(&self, expires_in: Duration) -> Url {
let now = OffsetDateTime::now_utc();
self.sign_with_time(expires_in, &now)
}

/// Get a mutable reference to the query string of this action
fn query_mut(&mut self) -> &mut Map<'a>;
Expand All @@ -45,4 +50,8 @@ pub trait S3Action<'a> {
/// Headers specified here must also be present in the final request,
/// with the same value specified, otherwise the S3 API will return an error.
fn headers_mut(&mut self) -> &mut Map<'a>;

/// Takes the time at which the URL should be signed
/// Used for testing purposes
fn sign_with_time(&self, expires_in: Duration, time: &OffsetDateTime) -> Url;
}

0 comments on commit 94903a6

Please sign in to comment.