-
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
AWS Endpoint Middleware #188
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion
2
aws/rust-runtime/auth/Cargo.toml → aws/rust-runtime/aws-auth/Cargo.toml
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "auth" | ||
name = "aws-auth" | ||
version = "0.1.0" | ||
authors = ["Russell Cohen <[email protected]>"] | ||
edition = "2018" | ||
|
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 |
---|---|---|
|
@@ -3,13 +3,13 @@ | |
* SPDX-License-Identifier: Apache-2.0. | ||
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. formatting only |
||
*/ | ||
|
||
use crate::{ProvideCredentials, Credentials, CredentialsError}; | ||
use std::env::VarError; | ||
use crate::{Credentials, CredentialsError, ProvideCredentials}; | ||
use std::collections::HashMap; | ||
use std::env::VarError; | ||
|
||
/// Load Credentials from Environment Variables | ||
pub struct EnvironmentVariableCredentialsProvider { | ||
env: Box<dyn Fn(&str) -> Result<String, VarError> + Send + Sync> | ||
env: Box<dyn Fn(&str) -> Result<String, VarError> + Send + Sync>, | ||
} | ||
|
||
impl EnvironmentVariableCredentialsProvider { | ||
|
@@ -21,8 +21,10 @@ impl EnvironmentVariableCredentialsProvider { | |
fn for_map(env: HashMap<String, String>) -> Self { | ||
EnvironmentVariableCredentialsProvider { | ||
env: Box::new(move |key: &str| { | ||
env.get(key).ok_or(VarError::NotPresent).map(|k|k.to_string()) | ||
}) | ||
env.get(key) | ||
.ok_or(VarError::NotPresent) | ||
.map(|k| k.to_string()) | ||
}), | ||
} | ||
} | ||
} | ||
|
@@ -36,31 +38,32 @@ const ENV_PROVIDER: &'static str = "EnvironmentVariable"; | |
impl ProvideCredentials for EnvironmentVariableCredentialsProvider { | ||
fn credentials(&self) -> Result<Credentials, CredentialsError> { | ||
let access_key = (self.env)("AWS_ACCESS_KEY_ID").map_err(to_cred_error)?; | ||
let secret_key = | ||
(self.env)("AWS_SECRET_ACCESS_KEY").or_else(|_|(self.env)("SECRET_ACCESS_KEY")).map_err(to_cred_error)?; | ||
let secret_key = (self.env)("AWS_SECRET_ACCESS_KEY") | ||
.or_else(|_| (self.env)("SECRET_ACCESS_KEY")) | ||
.map_err(to_cred_error)?; | ||
let session_token = (self.env)("AWS_SESSION_TOKEN").ok(); | ||
Ok(Credentials { | ||
access_key_id: access_key, | ||
secret_access_key: secret_key, | ||
session_token, | ||
expires_after: None, | ||
provider_name: ENV_PROVIDER | ||
provider_name: ENV_PROVIDER, | ||
}) | ||
} | ||
} | ||
|
||
fn to_cred_error(err: VarError) -> CredentialsError { | ||
match err { | ||
VarError::NotPresent => CredentialsError::CredentialsNotLoaded, | ||
e @ VarError::NotUnicode(_) => CredentialsError::Unhandled(Box::new(e)) | ||
e @ VarError::NotUnicode(_) => CredentialsError::Unhandled(Box::new(e)), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::provider::EnvironmentVariableCredentialsProvider; | ||
use crate::{CredentialsError, ProvideCredentials}; | ||
use std::collections::HashMap; | ||
use crate::{ProvideCredentials, CredentialsError}; | ||
|
||
#[test] | ||
fn valid_no_token() { | ||
|
@@ -101,7 +104,6 @@ mod test { | |
assert_eq!(creds.session_token.unwrap(), "token"); | ||
assert_eq!(creds.access_key_id, "access"); | ||
assert_eq!(creds.secret_access_key, "secret"); | ||
|
||
} | ||
|
||
#[test] | ||
|
@@ -110,8 +112,8 @@ mod test { | |
let provider = EnvironmentVariableCredentialsProvider::for_map(env); | ||
let err = provider.credentials().expect_err("no credentials defined"); | ||
match err { | ||
CredentialsError::Unhandled(_ ) => panic!("wrong error type"), | ||
_ => () | ||
CredentialsError::Unhandled(_) => panic!("wrong error type"), | ||
_ => (), | ||
}; | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "aws-endpoint" | ||
version = "0.1.0" | ||
authors = ["Russell Cohen <[email protected]>"] | ||
edition = "2018" | ||
description = "AWS Endpoint Support" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
smithy-http = { path = "../../../rust-runtime/smithy-http"} | ||
aws-types = { path = "../aws-types" } | ||
http = "0.2.3" |
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 |
---|---|---|
@@ -0,0 +1,202 @@ | ||
use aws_types::{Region, SigningRegion, SigningService}; | ||
use http::Uri; | ||
use smithy_http::endpoint::{Endpoint, EndpointPrefix}; | ||
use smithy_http::middleware::MapRequest; | ||
use smithy_http::operation::Request; | ||
use smithy_http::property_bag::PropertyBag; | ||
use std::error::Error; | ||
use std::fmt; | ||
use std::fmt::{Debug, Display, Formatter}; | ||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
|
||
/// Endpoint to connect to an AWS Service | ||
/// | ||
/// An `AwsEndpoint` captures all necessary information needed to connect to an AWS service, including: | ||
/// - The URI of the endpoint (needed to actually send the request) | ||
/// - The name of the service (needed downstream for signing) | ||
/// - The signing region (which may differ from the actual region) | ||
#[derive(Clone)] | ||
pub struct AwsEndpoint { | ||
endpoint: Endpoint, | ||
signing_service: Option<SigningService>, | ||
signing_region: Option<SigningRegion>, | ||
} | ||
|
||
pub type BoxError = Box<dyn Error + Send + Sync + 'static>; | ||
|
||
/// Resolve the AWS Endpoint for a given region | ||
/// | ||
/// To provide a static endpoint, [`Endpoint`](smithy_http::endpoint::Endpoint) implements this trait. | ||
/// Example usage: | ||
/// ```rust | ||
/// # mod dynamodb { | ||
/// # use aws_endpoint::ResolveAwsEndpoint; | ||
/// # pub struct ConfigBuilder; | ||
/// # impl ConfigBuilder { | ||
/// # pub fn endpoint(&mut self, resolver: impl ResolveAwsEndpoint + 'static) { | ||
/// # // ... | ||
/// # } | ||
/// # } | ||
/// # pub struct Config; | ||
/// # impl Config { | ||
/// # pub fn builder() -> ConfigBuilder { | ||
/// # ConfigBuilder | ||
/// # } | ||
/// # } | ||
/// # } | ||
/// use smithy_http::endpoint::Endpoint; | ||
/// use http::Uri; | ||
/// let config = dynamodb::Config::builder() | ||
/// .endpoint( | ||
/// Endpoint::immutable(Uri::from_static("http://localhost:8080")) | ||
/// ); | ||
/// ``` | ||
/// In the future, each AWS service will generate their own implementation of `ResolveAwsEndpoint`. This implementation | ||
/// may use endpoint discovery. The list of supported regions for a given service | ||
/// will be codegenerated from `endpoints.json`. | ||
pub trait ResolveAwsEndpoint: Send + Sync { | ||
// TODO: consider if we want modeled error variants here | ||
fn endpoint(&self, region: &Region) -> Result<AwsEndpoint, BoxError>; | ||
} | ||
|
||
/// Default AWS Endpoint Implementation | ||
/// | ||
/// This is used as a temporary stub. Prior to GA, this will be replaced with specifically generated endpoint | ||
/// resolvers for each service that model the endpoints for each service correctly. Some services differ | ||
/// from the standard endpoint pattern. | ||
pub struct DefaultAwsEndpointResolver { | ||
service: &'static str, | ||
} | ||
|
||
impl DefaultAwsEndpointResolver { | ||
pub fn for_service(service: &'static str) -> Self { | ||
Self { service } | ||
} | ||
} | ||
|
||
/// An `Endpoint` can be its own resolver to support static endpoints | ||
impl ResolveAwsEndpoint for Endpoint { | ||
fn endpoint(&self, _region: &Region) -> Result<AwsEndpoint, BoxError> { | ||
Ok(AwsEndpoint { | ||
endpoint: self.clone(), | ||
signing_service: None, | ||
signing_region: None, | ||
}) | ||
} | ||
} | ||
|
||
impl ResolveAwsEndpoint for DefaultAwsEndpointResolver { | ||
fn endpoint(&self, region: &Region) -> Result<AwsEndpoint, BoxError> { | ||
let uri = Uri::from_str(&format!( | ||
"https://{}.{}.amazonaws.com", | ||
region.as_ref(), | ||
self.service | ||
))?; | ||
Ok(AwsEndpoint { | ||
endpoint: Endpoint::mutable(uri), | ||
signing_region: Some(region.clone().into()), | ||
signing_service: Some(SigningService::from_static(self.service)), | ||
}) | ||
} | ||
} | ||
|
||
type AwsEndpointResolver = Arc<dyn ResolveAwsEndpoint>; | ||
fn get_endpoint_resolver(config: &PropertyBag) -> Option<&AwsEndpointResolver> { | ||
config.get() | ||
} | ||
|
||
pub fn set_endpoint_resolver(provider: AwsEndpointResolver, config: &mut PropertyBag) { | ||
config.insert(provider); | ||
} | ||
|
||
/// Middleware Stage to Add an Endpoint to a Request | ||
/// | ||
/// AwsEndpointStage implements [`MapRequest`](smithy_http::middleware::MapRequest). It will: | ||
/// 1. Load an endpoint provider from the property bag. | ||
/// 2. Load an endpoint given the [`Region`](aws_types::Region) in the property bag. | ||
/// 3. Apply the endpoint to the URI in the request | ||
/// 4. Set the `SigningRegion` and `SigningService` in the property bag to drive downstream | ||
/// signing middleware. | ||
pub struct AwsEndpointStage; | ||
|
||
#[derive(Debug)] | ||
pub enum AwsEndpointStageError { | ||
NoEndpointResolver, | ||
NoRegion, | ||
EndpointResolutionError(BoxError), | ||
} | ||
|
||
impl Display for AwsEndpointStageError { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
Debug::fmt(self, f) | ||
} | ||
} | ||
impl Error for AwsEndpointStageError {} | ||
|
||
impl MapRequest for AwsEndpointStage { | ||
type Error = AwsEndpointStageError; | ||
|
||
fn apply(&self, request: Request) -> Result<Request, Self::Error> { | ||
request.augment(|mut http_req, config| { | ||
let provider = | ||
get_endpoint_resolver(config).ok_or(AwsEndpointStageError::NoEndpointResolver)?; | ||
let region = config | ||
.get::<Region>() | ||
.ok_or(AwsEndpointStageError::NoRegion)?; | ||
let endpoint = provider | ||
.endpoint(region) | ||
.map_err(AwsEndpointStageError::EndpointResolutionError)?; | ||
let signing_region = endpoint | ||
.signing_region | ||
.unwrap_or_else(|| region.clone().into()); | ||
config.insert::<SigningRegion>(signing_region); | ||
if let Some(signing_service) = endpoint.signing_service { | ||
config.insert::<SigningService>(signing_service); | ||
} | ||
endpoint | ||
.endpoint | ||
.set_endpoint(http_req.uri_mut(), config.get::<EndpointPrefix>()); | ||
Ok(http_req) | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::{set_endpoint_resolver, AwsEndpointStage, DefaultAwsEndpointResolver}; | ||
use aws_types::{Region, SigningRegion, SigningService}; | ||
use http::Uri; | ||
use smithy_http::body::SdkBody; | ||
use smithy_http::middleware::MapRequest; | ||
use smithy_http::operation; | ||
use std::sync::Arc; | ||
|
||
#[test] | ||
fn default_endpoint_updates_request() { | ||
let provider = Arc::new(DefaultAwsEndpointResolver::for_service("kinesis")); | ||
let req = http::Request::new(SdkBody::from("")); | ||
let region = Region::new("us-east-1"); | ||
let mut req = operation::Request::new(req); | ||
{ | ||
let mut conf = req.config_mut(); | ||
conf.insert(region.clone()); | ||
set_endpoint_resolver(provider, &mut conf); | ||
}; | ||
let req = AwsEndpointStage.apply(req).expect("should succeed"); | ||
assert_eq!( | ||
req.config().get(), | ||
Some(&SigningRegion::from(region.clone())) | ||
); | ||
assert_eq!( | ||
req.config().get(), | ||
Some(&SigningService::from_static("kinesis")) | ||
); | ||
|
||
let (req, _conf) = req.into_parts(); | ||
assert_eq!( | ||
req.uri(), | ||
&Uri::from_static("https://us-east-1.kinesis.amazonaws.com") | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "aws-types" | ||
version = "0.1.0" | ||
authors = ["Russell Cohen <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
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.
formatting only. the CI format gate isn't set up for the aws runtime yet.