Skip to content

Commit

Permalink
Use reqwest build_split (#4039)
Browse files Browse the repository at this point in the history
* Use reqwest build_split

* Fix typo
  • Loading branch information
tustvold committed Apr 9, 2023
1 parent ff670c5 commit 2e69848
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
21 changes: 4 additions & 17 deletions object_store/src/aws/credential.rs
Expand Up @@ -81,8 +81,6 @@ const HASH_HEADER: &str = "x-amz-content-sha256";
const TOKEN_HEADER: &str = "x-amz-security-token";
const AUTH_HEADER: &str = "authorization";

const ALL_HEADERS: &[&str; 4] = &[DATE_HEADER, HASH_HEADER, TOKEN_HEADER, AUTH_HEADER];

impl<'a> RequestSigner<'a> {
fn sign(&self, request: &mut Request, pre_calculated_digest: Option<Vec<u8>>) {
if let Some(ref token) = self.credential.token {
Expand Down Expand Up @@ -175,20 +173,15 @@ pub trait CredentialExt {

impl CredentialExt for RequestBuilder {
fn with_aws_sigv4(
mut self,
self,
credential: &AwsCredential,
region: &str,
service: &str,
sign_payload: bool,
payload_sha256: Option<Vec<u8>>,
) -> Self {
// Hack around lack of access to underlying request
// https://github.com/seanmonstar/reqwest/issues/1212
let mut request = self
.try_clone()
.expect("not stream")
.build()
.expect("request valid");
let (client, request) = self.build_split();
let mut request = request.expect("request valid");

let date = Utc::now();
let signer = RequestSigner {
Expand All @@ -200,13 +193,7 @@ impl CredentialExt for RequestBuilder {
};

signer.sign(&mut request, payload_sha256);

for header in ALL_HEADERS {
if let Some(val) = request.headers_mut().remove(*header) {
self = self.header(*header, val)
}
}
self
Self::from_parts(client, request)
}
}

Expand Down
36 changes: 15 additions & 21 deletions object_store/src/azure/credential.rs
Expand Up @@ -124,39 +124,33 @@ impl CredentialExt for RequestBuilder {
.header(DATE, &date_val)
.header(&VERSION, &AZURE_VERSION);

// Hack around lack of access to underlying request
// https://github.com/seanmonstar/reqwest/issues/1212
let request = self
.try_clone()
.expect("not stream")
.build()
.expect("request valid");

match credential {
AzureCredential::AccessKey(key) => {
let (client, request) = self.build_split();
let mut request = request.expect("request valid");

let signature = generate_authorization(
request.headers(),
request.url(),
request.method(),
account,
key.as_str(),
);
self = self
// "signature" is a base 64 encoded string so it should never contain illegal characters.
.header(
AUTHORIZATION,
HeaderValue::from_str(signature.as_str()).unwrap(),
);

// "signature" is a base 64 encoded string so it should never
// contain illegal characters
request.headers_mut().append(
AUTHORIZATION,
HeaderValue::from_str(signature.as_str()).unwrap(),
);

Self::from_parts(client, request)
}
AzureCredential::AuthorizationToken(token) => {
self = self.header(AUTHORIZATION, token);
self.header(AUTHORIZATION, token)
}
AzureCredential::SASToken(query_pairs) => {
self = self.query(&query_pairs);
}
};

self
AzureCredential::SASToken(query_pairs) => self.query(&query_pairs),
}
}
}

Expand Down

0 comments on commit 2e69848

Please sign in to comment.