-
Notifications
You must be signed in to change notification settings - Fork 196
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
Fix presigning bug with content-length
and content-type
in S3
#1216
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a3a6bc
Fix presigning bug with `content-length` and `content-type` in S3
jdisanti 89156a6
Fix presigning test in `aws-sigv4`
jdisanti ef95fd2
Update changelog
jdisanti 3f7c0b5
Clean when generating code for diff preview
jdisanti d44c112
Incorporate feedback
jdisanti c6d40c3
Merge remote-tracking branch 'upstream/main' into jdisanti-fix-presig…
jdisanti c3f7f09
Fix server codegen
jdisanti b98800a
Merge branch 'main' into jdisanti-fix-presign-default-headers
Velfi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,39 +4,49 @@ | |
*/ | ||
|
||
use aws_sdk_s3 as s3; | ||
use aws_sdk_s3::presigning::request::PresignedRequest; | ||
use http::header::{CONTENT_LENGTH, CONTENT_TYPE}; | ||
use http::{HeaderMap, HeaderValue}; | ||
use s3::presigning::config::PresigningConfig; | ||
use std::error::Error; | ||
use std::time::{Duration, SystemTime}; | ||
|
||
/// Generates a `PresignedRequest` from the given input. | ||
/// Assumes that that input has a `presigned` method on it. | ||
macro_rules! presign_input { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment on test macros is often helpful |
||
($input:expr) => {{ | ||
let creds = s3::Credentials::new( | ||
"ANOTREAL", | ||
"notrealrnrELgWzOk3IfjzDKtFBhDby", | ||
Some("notarealsessiontoken".to_string()), | ||
None, | ||
"test", | ||
); | ||
let config = s3::Config::builder() | ||
.credentials_provider(creds) | ||
.region(s3::Region::new("us-east-1")) | ||
.build(); | ||
|
||
let req: PresignedRequest = $input | ||
.presigned( | ||
&config, | ||
PresigningConfig::builder() | ||
.start_time(SystemTime::UNIX_EPOCH + Duration::from_secs(1234567891)) | ||
.expires_in(Duration::from_secs(30)) | ||
.build() | ||
.unwrap(), | ||
) | ||
.await?; | ||
req | ||
}}; | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_presigning() -> Result<(), Box<dyn Error>> { | ||
let creds = s3::Credentials::new( | ||
"ANOTREAL", | ||
"notrealrnrELgWzOk3IfjzDKtFBhDby", | ||
Some("notarealsessiontoken".to_string()), | ||
None, | ||
"test", | ||
); | ||
let config = s3::Config::builder() | ||
.credentials_provider(creds) | ||
.region(s3::Region::new("us-east-1")) | ||
.build(); | ||
|
||
let input = s3::input::GetObjectInput::builder() | ||
let presigned = presign_input!(s3::input::GetObjectInput::builder() | ||
.bucket("test-bucket") | ||
.key("test-key") | ||
.build()?; | ||
|
||
let presigned = input | ||
.presigned( | ||
&config, | ||
PresigningConfig::builder() | ||
.start_time(SystemTime::UNIX_EPOCH + Duration::from_secs(1234567891)) | ||
.expires_in(Duration::from_secs(30)) | ||
.build() | ||
.unwrap(), | ||
) | ||
.await?; | ||
.build()?); | ||
|
||
let pq = presigned.uri().path_and_query().unwrap(); | ||
let path = pq.path(); | ||
|
@@ -63,3 +73,42 @@ async fn test_presigning() -> Result<(), Box<dyn Error>> { | |
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_presigning_with_payload_headers() -> Result<(), Box<dyn Error>> { | ||
let presigned = presign_input!(s3::input::PutObjectInput::builder() | ||
.bucket("test-bucket") | ||
.key("test-key") | ||
.content_length(12345) | ||
.content_type("application/x-test") | ||
.build()?); | ||
|
||
let pq = presigned.uri().path_and_query().unwrap(); | ||
let path = pq.path(); | ||
let query = pq.query().unwrap(); | ||
let mut query_params: Vec<&str> = query.split('&').collect(); | ||
query_params.sort(); | ||
|
||
assert_eq!("PUT", presigned.method().as_str()); | ||
assert_eq!("/test-bucket/test-key", path); | ||
assert_eq!( | ||
&[ | ||
"X-Amz-Algorithm=AWS4-HMAC-SHA256", | ||
"X-Amz-Credential=ANOTREAL%2F20090213%2Fus-east-1%2Fs3%2Faws4_request", | ||
"X-Amz-Date=20090213T233131Z", | ||
"X-Amz-Expires=30", | ||
"X-Amz-Security-Token=notarealsessiontoken", | ||
"X-Amz-Signature=6a22b8bf422d17fe25e7d9fcbd26df31397ca5e3ad07d1cec95326ffdbe4a0a2", | ||
"X-Amz-SignedHeaders=content-length%3Bcontent-type%3Bhost", | ||
"x-id=PutObject" | ||
][..], | ||
&query_params | ||
); | ||
|
||
let mut expected_headers = HeaderMap::new(); | ||
expected_headers.insert(CONTENT_LENGTH, HeaderValue::from_static("12345")); | ||
expected_headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/x-test")); | ||
assert_eq!(&expected_headers, presigned.headers()); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this is hard...this is probably hard...ideally we just wouldn't hit the UA middleware
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could potentially add something to the property bag that tells the UA middleware to no-op, but I don't think it would be easy to customize the middleware in the presigning function as it currently is.