-
Notifications
You must be signed in to change notification settings - Fork 837
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
object_store: Add support for requester pays buckets #6768
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d055e6
Add support for requester pays buckets
kylebarron a3f2f58
Add tests
kylebarron eb94f21
fix rustdoc
kylebarron 844d983
Merge branch 'master' into kyle/request-payer
kylebarron ec8c816
Merge branch 'master' into kyle/request-payer
kylebarron 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,11 +101,14 @@ pub struct AwsAuthorizer<'a> { | |
region: &'a str, | ||
token_header: Option<HeaderName>, | ||
sign_payload: bool, | ||
request_payer: bool, | ||
} | ||
|
||
static DATE_HEADER: HeaderName = HeaderName::from_static("x-amz-date"); | ||
static HASH_HEADER: HeaderName = HeaderName::from_static("x-amz-content-sha256"); | ||
static TOKEN_HEADER: HeaderName = HeaderName::from_static("x-amz-security-token"); | ||
static REQUEST_PAYER_HEADER: HeaderName = HeaderName::from_static("x-amz-request-payer"); | ||
static REQUEST_PAYER_HEADER_VALUE: HeaderValue = HeaderValue::from_static("requester"); | ||
const ALGORITHM: &str = "AWS4-HMAC-SHA256"; | ||
|
||
impl<'a> AwsAuthorizer<'a> { | ||
|
@@ -118,6 +121,7 @@ impl<'a> AwsAuthorizer<'a> { | |
date: None, | ||
sign_payload: true, | ||
token_header: None, | ||
request_payer: false, | ||
} | ||
} | ||
|
||
|
@@ -134,6 +138,14 @@ impl<'a> AwsAuthorizer<'a> { | |
self | ||
} | ||
|
||
/// Set whether to include requester pays headers | ||
/// | ||
/// <https://docs.aws.amazon.com/AmazonS3/latest/userguide/ObjectsinRequesterPaysBuckets.html> | ||
pub fn with_request_payer(mut self, request_payer: bool) -> Self { | ||
self.request_payer = request_payer; | ||
self | ||
} | ||
|
||
/// Authorize `request` with an optional pre-calculated SHA256 digest by attaching | ||
/// the relevant [AWS SigV4] headers | ||
/// | ||
|
@@ -180,6 +192,15 @@ impl<'a> AwsAuthorizer<'a> { | |
let header_digest = HeaderValue::from_str(&digest).unwrap(); | ||
request.headers_mut().insert(&HASH_HEADER, header_digest); | ||
|
||
if self.request_payer { | ||
// For DELETE, GET, HEAD, POST, and PUT requests, include x-amz-request-payer : | ||
// requester in the header | ||
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/ObjectsinRequesterPaysBuckets.html | ||
request | ||
.headers_mut() | ||
.insert(&REQUEST_PAYER_HEADER, REQUEST_PAYER_HEADER_VALUE.clone()); | ||
} | ||
|
||
let (signed_headers, canonical_headers) = canonicalize_headers(request.headers()); | ||
|
||
let scope = self.scope(date); | ||
|
@@ -226,6 +247,13 @@ impl<'a> AwsAuthorizer<'a> { | |
.append_pair("X-Amz-Expires", &expires_in.as_secs().to_string()) | ||
.append_pair("X-Amz-SignedHeaders", "host"); | ||
|
||
if self.request_payer { | ||
// For signed URLs, include x-amz-request-payer=requester in the request | ||
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/ObjectsinRequesterPaysBuckets.html | ||
url.query_pairs_mut() | ||
.append_pair("x-amz-request-payer", "requester"); | ||
} | ||
|
||
// For S3, you must include the X-Amz-Security-Token query parameter in the URL if | ||
// using credentials sourced from the STS service. | ||
if let Some(ref token) = self.credential.token { | ||
|
@@ -763,12 +791,53 @@ mod tests { | |
region: "us-east-1", | ||
sign_payload: true, | ||
token_header: None, | ||
request_payer: false, | ||
}; | ||
|
||
signer.authorize(&mut request, None); | ||
assert_eq!(request.headers().get(&AUTHORIZATION).unwrap(), "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20220806/us-east-1/ec2/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=a3c787a7ed37f7fdfbfd2d7056a3d7c9d85e6d52a2bfbec73793c0be6e7862d4") | ||
} | ||
|
||
#[test] | ||
fn test_sign_with_signed_payload_request_payer() { | ||
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. 👍 |
||
let client = Client::new(); | ||
|
||
// Test credentials from https://docs.aws.amazon.com/AmazonS3/latest/userguide/RESTAuthentication.html | ||
let credential = AwsCredential { | ||
key_id: "AKIAIOSFODNN7EXAMPLE".to_string(), | ||
secret_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY".to_string(), | ||
token: None, | ||
}; | ||
|
||
// method = 'GET' | ||
// service = 'ec2' | ||
// host = 'ec2.amazonaws.com' | ||
// region = 'us-east-1' | ||
// endpoint = 'https://ec2.amazonaws.com' | ||
// request_parameters = '' | ||
let date = DateTime::parse_from_rfc3339("2022-08-06T18:01:34Z") | ||
.unwrap() | ||
.with_timezone(&Utc); | ||
|
||
let mut request = client | ||
.request(Method::GET, "https://ec2.amazon.com/") | ||
.build() | ||
.unwrap(); | ||
|
||
let signer = AwsAuthorizer { | ||
date: Some(date), | ||
credential: &credential, | ||
service: "ec2", | ||
region: "us-east-1", | ||
sign_payload: true, | ||
token_header: None, | ||
request_payer: true, | ||
}; | ||
|
||
signer.authorize(&mut request, None); | ||
assert_eq!(request.headers().get(&AUTHORIZATION).unwrap(), "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20220806/us-east-1/ec2/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-request-payer, Signature=7030625a9e9b57ed2a40e63d749f4a4b7714b6e15004cab026152f870dd8565d") | ||
} | ||
|
||
#[test] | ||
fn test_sign_with_unsigned_payload() { | ||
let client = Client::new(); | ||
|
@@ -802,6 +871,7 @@ mod tests { | |
region: "us-east-1", | ||
token_header: None, | ||
sign_payload: false, | ||
request_payer: false, | ||
}; | ||
|
||
authorizer.authorize(&mut request, None); | ||
|
@@ -828,6 +898,7 @@ mod tests { | |
region: "us-east-1", | ||
token_header: None, | ||
sign_payload: false, | ||
request_payer: false, | ||
}; | ||
|
||
let mut url = Url::parse("https://examplebucket.s3.amazonaws.com/test.txt").unwrap(); | ||
|
@@ -848,6 +919,48 @@ mod tests { | |
); | ||
} | ||
|
||
#[test] | ||
fn signed_get_url_request_payer() { | ||
// Values from https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html | ||
let credential = AwsCredential { | ||
key_id: "AKIAIOSFODNN7EXAMPLE".to_string(), | ||
secret_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY".to_string(), | ||
token: None, | ||
}; | ||
|
||
let date = DateTime::parse_from_rfc3339("2013-05-24T00:00:00Z") | ||
.unwrap() | ||
.with_timezone(&Utc); | ||
|
||
let authorizer = AwsAuthorizer { | ||
date: Some(date), | ||
credential: &credential, | ||
service: "s3", | ||
region: "us-east-1", | ||
token_header: None, | ||
sign_payload: false, | ||
request_payer: true, | ||
}; | ||
|
||
let mut url = Url::parse("https://examplebucket.s3.amazonaws.com/test.txt").unwrap(); | ||
authorizer.sign(Method::GET, &mut url, Duration::from_secs(86400)); | ||
|
||
assert_eq!( | ||
url, | ||
Url::parse( | ||
"https://examplebucket.s3.amazonaws.com/test.txt?\ | ||
X-Amz-Algorithm=AWS4-HMAC-SHA256&\ | ||
X-Amz-Credential=AKIAIOSFODNN7EXAMPLE%2F20130524%2Fus-east-1%2Fs3%2Faws4_request&\ | ||
X-Amz-Date=20130524T000000Z&\ | ||
X-Amz-Expires=86400&\ | ||
X-Amz-SignedHeaders=host&\ | ||
x-amz-request-payer=requester&\ | ||
X-Amz-Signature=9ad7c781cc30121f199b47d35ed3528473e4375b63c5d91cd87c927803e4e00a" | ||
) | ||
.unwrap() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_sign_port() { | ||
let client = Client::new(); | ||
|
@@ -880,6 +993,7 @@ mod tests { | |
region: "us-east-1", | ||
token_header: None, | ||
sign_payload: true, | ||
request_payer: false, | ||
}; | ||
|
||
authorizer.authorize(&mut request, None); | ||
|
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.
Are there other values for this header? Wondering if it should be an enumeration instead of a boolean?
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.
No, this is the only value for the header: https://docs.aws.amazon.com/AmazonS3/latest/userguide/ObjectsinRequesterPaysBuckets.html