Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move 'sign_with_time' method to 'S3Action' #28

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +4 to +5
Copy link
Owner

@paolobarbolini paolobarbolini Oct 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this @guerinoni?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your repo - your rules. I can remove it if is a big problem.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it's ok, maybe for who use this IDE can be handy

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 let's leave it then

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)
}
Comment on lines -145 to -148
Copy link
Owner

@paolobarbolini paolobarbolini Oct 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...you can remove all of these sign implementations which are all the same?

Basically if a trait has a default implementation for a certain function it'll use it when you do impl Trait for T unless you overwrite it with something else
https://doc.rust-lang.org/book/ch10-02-traits.html#default-implementations

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course. I'm familiar with the concept. I just did the brainless work of C&P :D


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;
Comment on lines +54 to +56
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refers to line 38 but I can't seem to be able to comment a line which hasn't been changed so I'll comment this one instead:

Could you add a default implementation of sign:

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

so that... (continues in the other comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely 👍

}
Loading