-
Notifications
You must be signed in to change notification settings - Fork 193
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
Revamp errors for aws-sigv4
#1937
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
use http::header::{InvalidHeaderName, InvalidHeaderValue}; | ||
use std::error::Error; | ||
use std::fmt; | ||
use std::str::Utf8Error; | ||
|
||
#[derive(Debug)] | ||
enum SigningErrorKind { | ||
FailedToCreateCanonicalRequest { source: CanonicalRequestError }, | ||
} | ||
|
||
/// Error signing request | ||
#[derive(Debug)] | ||
pub struct SigningError { | ||
kind: SigningErrorKind, | ||
} | ||
|
||
impl fmt::Display for SigningError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self.kind { | ||
SigningErrorKind::FailedToCreateCanonicalRequest { .. } => { | ||
write!(f, "failed to create canonical request") | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Error for SigningError { | ||
fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
match &self.kind { | ||
SigningErrorKind::FailedToCreateCanonicalRequest { source } => Some(source), | ||
} | ||
} | ||
} | ||
Comment on lines
+22
to
+38
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. I believe this follows #1345? 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. Yes. The source doesn't get written in the |
||
|
||
impl From<CanonicalRequestError> for SigningError { | ||
fn from(source: CanonicalRequestError) -> Self { | ||
Self { | ||
kind: SigningErrorKind::FailedToCreateCanonicalRequest { source }, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
enum CanonicalRequestErrorKind { | ||
InvalidHeaderName { source: InvalidHeaderName }, | ||
InvalidHeaderValue { source: InvalidHeaderValue }, | ||
InvalidUtf8InHeaderValue { source: Utf8Error }, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct CanonicalRequestError { | ||
kind: CanonicalRequestErrorKind, | ||
} | ||
|
||
impl fmt::Display for CanonicalRequestError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
use CanonicalRequestErrorKind::*; | ||
match self.kind { | ||
InvalidHeaderName { .. } => write!(f, "invalid header name"), | ||
InvalidHeaderValue { .. } => write!(f, "invalid header value"), | ||
InvalidUtf8InHeaderValue { .. } => write!(f, "invalid UTF-8 in header value"), | ||
} | ||
} | ||
} | ||
|
||
impl Error for CanonicalRequestError { | ||
fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
use CanonicalRequestErrorKind::*; | ||
match &self.kind { | ||
InvalidHeaderName { source } => Some(source), | ||
InvalidHeaderValue { source } => Some(source), | ||
InvalidUtf8InHeaderValue { source } => Some(source), | ||
} | ||
} | ||
} | ||
|
||
impl CanonicalRequestError { | ||
pub(crate) fn invalid_utf8_in_header_value(source: Utf8Error) -> Self { | ||
Self { | ||
kind: CanonicalRequestErrorKind::InvalidUtf8InHeaderValue { source }, | ||
} | ||
} | ||
} | ||
|
||
impl From<InvalidHeaderName> for CanonicalRequestError { | ||
fn from(source: InvalidHeaderName) -> Self { | ||
Self { | ||
kind: CanonicalRequestErrorKind::InvalidHeaderName { source }, | ||
} | ||
} | ||
} | ||
|
||
impl From<InvalidHeaderValue> for CanonicalRequestError { | ||
fn from(source: InvalidHeaderValue) -> Self { | ||
Self { | ||
kind: CanonicalRequestErrorKind::InvalidHeaderValue { source }, | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
use super::error::SigningError; | ||
use super::{PayloadChecksumKind, SignatureLocation}; | ||
use crate::http_request::canonical_request::header; | ||
use crate::http_request::canonical_request::param; | ||
|
@@ -15,12 +16,8 @@ use http::header::HeaderValue; | |
use http::{HeaderMap, Method, Uri}; | ||
use std::borrow::Cow; | ||
use std::convert::TryFrom; | ||
use std::error::Error as StdError; | ||
use std::str; | ||
|
||
/// Signing error type | ||
pub type Error = Box<dyn StdError + Send + Sync + 'static>; | ||
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. 🎉 |
||
|
||
/// Represents all of the information necessary to sign an HTTP request. | ||
#[derive(Debug)] | ||
#[non_exhaustive] | ||
|
@@ -155,7 +152,7 @@ impl SigningInstructions { | |
pub fn sign<'a>( | ||
request: SignableRequest<'a>, | ||
params: &'a SigningParams<'a>, | ||
) -> Result<SigningOutput<SigningInstructions>, Error> { | ||
) -> Result<SigningOutput<SigningInstructions>, SigningError> { | ||
tracing::trace!(request = ?request, params = ?params, "signing request"); | ||
match params.settings.signature_location { | ||
SignatureLocation::Headers => { | ||
|
@@ -181,7 +178,7 @@ type CalculatedParams = Vec<(&'static str, Cow<'static, str>)>; | |
fn calculate_signing_params<'a>( | ||
request: &'a SignableRequest<'a>, | ||
params: &'a SigningParams<'a>, | ||
) -> Result<(CalculatedParams, String), Error> { | ||
) -> Result<(CalculatedParams, String), SigningError> { | ||
let creq = CanonicalRequest::from(request, params)?; | ||
tracing::trace!(canonical_request = %creq); | ||
|
||
|
@@ -230,7 +227,7 @@ fn calculate_signing_params<'a>( | |
fn calculate_signing_headers<'a>( | ||
request: &'a SignableRequest<'a>, | ||
params: &'a SigningParams<'a>, | ||
) -> Result<SigningOutput<HeaderMap<HeaderValue>>, Error> { | ||
) -> Result<SigningOutput<HeaderMap<HeaderValue>>, SigningError> { | ||
// Step 1: https://docs.aws.amazon.com/en_pv/general/latest/gr/sigv4-create-canonical-request.html. | ||
let creq = CanonicalRequest::from(request, params)?; | ||
tracing::trace!(canonical_request = %creq); | ||
|
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.
🎉