-
Notifications
You must be signed in to change notification settings - Fork 214
Support selectable auth schemes #4203
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
842ee41
Add ability to map boxed future in `new_type_future`
ysaito1001 eab28d4
Use auth scheme preference in default auth scheme resolver
ysaito1001 08f09d4
Make auth scheme preference configurable
ysaito1001 b6608c2
Add tests for `resolve_identity`
ysaito1001 6626ce8
Add test for auth scheme preference priority
ysaito1001 a562638
Update lockfiles
ysaito1001 2d483a0
Add changelog entry
ysaito1001 e5b48cb
Revert "Add ability to map boxed future in `new_type_future`"
ysaito1001 d07260e
Use auth scheme preference in orchestrator
ysaito1001 27f5e8b
Merge branch 'main' into ysaito/selectable-auth-schemes
ysaito1001 fca5226
Align `AuthSchemeId` values with shape ID substrings
ysaito1001 dff3160
Merge branch 'main' into ysaito/selectable-auth-schemes
ysaito1001 37e88bf
Merge branch 'main' into ysaito/selectable-auth-schemes
ysaito1001 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 hidden or 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,37 @@ | ||
--- | ||
applies_to: | ||
- aws-sdk-rust | ||
- client | ||
authors: | ||
- ysaito1001 | ||
references: | ||
- smithy-rs#4203 | ||
breaking: false | ||
new_feature: true | ||
bug_fix: false | ||
--- | ||
Add support for configuring auth schemes manually using an auth scheme preference list. | ||
The preference list allows customers to reprioritize the order of auth schemes originally | ||
determined by the auth scheme resolver. | ||
Customers can configure the auth scheme preference at the following locations, listed in order of precedence: | ||
1. Service Client Configuration | ||
```rust | ||
use aws_runtime::auth::sigv4; | ||
use aws_smithy_runtime_api::client::auth::AuthSchemeId; | ||
use aws_smithy_runtime_api::client::auth::http::HTTP_BEARER_AUTH_SCHEME_ID; | ||
|
||
let config = aws_sdk_s3::Config::builder() | ||
.auth_scheme_preference([AuthSchemeId::from("scheme1"), sigv4::SCHEME_ID, HTTP_BEARER_AUTH_SCHEME_ID]) | ||
// ... | ||
.build(); | ||
``` | ||
2. Environment Variable | ||
``` | ||
AWS_AUTH_SCHEME_PREFERENCE=scheme1, sigv4, httpBearerAuth | ||
``` | ||
3. Configuration File | ||
``` | ||
auth_scheme_preference=scheme1, sigv4, httpBearerAuth | ||
``` | ||
With this configuration, the auth scheme resolver will prefer to select them in the specified order, | ||
if they are supported. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
201 changes: 201 additions & 0 deletions
201
aws/rust-runtime/aws-config/src/default_provider/auth_scheme_preference.rs
This file contains hidden or 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,201 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
use crate::provider_config::ProviderConfig; | ||
use aws_runtime::env_config::EnvConfigValue; | ||
use aws_smithy_runtime_api::client::auth::{AuthSchemeId, AuthSchemePreference}; | ||
use aws_smithy_types::error::display::DisplayErrorContext; | ||
use std::borrow::Cow; | ||
use std::fmt; | ||
|
||
mod env { | ||
pub(super) const AUTH_SCHEME_PREFERENCE: &str = "AWS_AUTH_SCHEME_PREFERENCE"; | ||
} | ||
|
||
mod profile_key { | ||
pub(super) const AUTH_SCHEME_PREFERENCE: &str = "auth_scheme_preference"; | ||
} | ||
|
||
/// Load the value for the auth scheme preference | ||
/// | ||
/// This checks the following sources: | ||
/// 1. The environment variable `AWS_AUTH_SCHEME_PREFERENCE=scheme1,scheme2,scheme3` | ||
/// 2. The profile key `auth_scheme_preference=scheme1,scheme2,scheme3` | ||
/// | ||
/// A scheme name can be either a fully qualified name or a shorthand with the namespace prefix trimmed. | ||
/// For example, valid scheme names include "aws.auth#sigv4", "smithy.api#httpBasicAuth", "sigv4", and "httpBasicAuth". | ||
/// Whitespace (spaces or tabs), including leading, trailing, and between names, is ignored. | ||
/// | ||
/// Returns `None` if a parsed string component is empty when creating an `AuthSchemeId`. | ||
pub(crate) async fn auth_scheme_preference_provider( | ||
provider_config: &ProviderConfig, | ||
) -> Option<AuthSchemePreference> { | ||
let env = provider_config.env(); | ||
let profiles = provider_config.profile().await; | ||
|
||
EnvConfigValue::new() | ||
.env(env::AUTH_SCHEME_PREFERENCE) | ||
.profile(profile_key::AUTH_SCHEME_PREFERENCE) | ||
.validate(&env, profiles, parse_auth_scheme_names) | ||
.map_err(|err| tracing::warn!(err = %DisplayErrorContext(&err), "invalid value for `AuthSchemePreference`")) | ||
.unwrap_or(None) | ||
} | ||
|
||
fn parse_auth_scheme_names(csv: &str) -> Result<AuthSchemePreference, InvalidAuthSchemeNamesCsv> { | ||
csv.split(',') | ||
.map(|s| { | ||
let trimmed = s.trim().replace([' ', '\t'], ""); | ||
if trimmed.is_empty() { | ||
return Err(InvalidAuthSchemeNamesCsv { | ||
value: format!("Empty name found in `{csv}`."), | ||
}); | ||
} | ||
let scheme_name = trimmed.split('#').next_back().unwrap_or(&trimmed); | ||
Ok(runtime_api_auth_scheme_id(scheme_name)) | ||
}) | ||
.collect::<Result<Vec<_>, _>>() | ||
.map(AuthSchemePreference::from) | ||
} | ||
|
||
// This function is not automatically extensible. When a new scheme is introduced, | ||
// it must be manually updated for the scheme to be recognized in the environment configuration. | ||
// | ||
// Furthermore, this function does not return a predefined `AuthSchemeId` like `HTTP_BASIC_AUTH_SCHEME_ID`. | ||
// The predefined `SchemeId`s are gated behind the `http-auth` feature, but this function returns an `AuthSchemeId` | ||
// that wraps the parsed string regardless of feature flags. If the feature is disabled, the returned | ||
// auth scheme preference is simply ignored during auth scheme resolution. | ||
fn runtime_api_auth_scheme_id(auth_scheme_name: &str) -> AuthSchemeId { | ||
let runtime_api_auth_scheme_str = match auth_scheme_name { | ||
"httpBasicAuth" => "http-basic-auth", | ||
"httpDigestAuth" => "http-digest-auth", | ||
"httpBearerAuth" => "http-bearer-auth", | ||
"httpApiKeyAuth" => "http-api-key-auth", | ||
"noAuth" => "no_auth", | ||
otherwise => otherwise, | ||
}; | ||
AuthSchemeId::from(Cow::Owned(runtime_api_auth_scheme_str.to_owned())) | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct InvalidAuthSchemeNamesCsv { | ||
value: String, | ||
} | ||
|
||
impl fmt::Display for InvalidAuthSchemeNamesCsv { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!( | ||
f, | ||
"Not a valid comma-separated auth scheme names: {}", | ||
self.value | ||
) | ||
} | ||
} | ||
|
||
impl std::error::Error for InvalidAuthSchemeNamesCsv {} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::env; | ||
use crate::{ | ||
default_provider::auth_scheme_preference::auth_scheme_preference_provider, | ||
provider_config::ProviderConfig, | ||
}; | ||
use aws_types::os_shim_internal::Env; | ||
use tracing_test::traced_test; | ||
|
||
#[tokio::test] | ||
#[traced_test] | ||
async fn log_error_on_invalid_value() { | ||
let conf = ProviderConfig::empty().with_env(Env::from_slice(&[( | ||
env::AUTH_SCHEME_PREFERENCE, | ||
"scheme1, , \tscheme2", | ||
)])); | ||
assert_eq!(None, auth_scheme_preference_provider(&conf).await); | ||
assert!(logs_contain( | ||
"Not a valid comma-separated auth scheme names: Empty name found" | ||
)); | ||
} | ||
|
||
#[cfg(feature = "sso")] // for aws-smithy-runtime-api/http-auth | ||
mod http_auth_tests { | ||
use super::env; | ||
#[allow(deprecated)] | ||
use crate::profile::profile_file::{ProfileFileKind, ProfileFiles}; | ||
use crate::{ | ||
default_provider::auth_scheme_preference::auth_scheme_preference_provider, | ||
provider_config::ProviderConfig, | ||
}; | ||
use aws_smithy_runtime_api::client::auth::AuthSchemePreference; | ||
use aws_types::os_shim_internal::{Env, Fs}; | ||
|
||
#[tokio::test] | ||
async fn environment_priority() { | ||
let conf = ProviderConfig::empty() | ||
.with_env(Env::from_slice(&[( | ||
env::AUTH_SCHEME_PREFERENCE, | ||
"aws.auth#sigv4, smithy.api#httpBasicAuth, smithy.api#httpDigestAuth, smithy.api#httpBearerAuth, smithy.api#httpApiKeyAuth", | ||
)])) | ||
.with_profile_config( | ||
Some( | ||
#[allow(deprecated)] | ||
ProfileFiles::builder() | ||
.with_file( | ||
#[allow(deprecated)] | ||
ProfileFileKind::Config, | ||
"conf", | ||
) | ||
.build(), | ||
), | ||
None, | ||
) | ||
.with_fs(Fs::from_slice(&[( | ||
"conf", | ||
"[default]\nauth_scheme_preference = scheme1, scheme2 , \tscheme3 \t", | ||
)])); | ||
assert_eq!( | ||
AuthSchemePreference::from([ | ||
aws_runtime::auth::sigv4::SCHEME_ID, | ||
aws_smithy_runtime_api::client::auth::http::HTTP_BASIC_AUTH_SCHEME_ID, | ||
aws_smithy_runtime_api::client::auth::http::HTTP_DIGEST_AUTH_SCHEME_ID, | ||
aws_smithy_runtime_api::client::auth::http::HTTP_BEARER_AUTH_SCHEME_ID, | ||
aws_smithy_runtime_api::client::auth::http::HTTP_API_KEY_AUTH_SCHEME_ID, | ||
]), | ||
auth_scheme_preference_provider(&conf).await.unwrap() | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn load_from_profile() { | ||
let conf = ProviderConfig::empty() | ||
.with_profile_config( | ||
Some( | ||
#[allow(deprecated)] | ||
ProfileFiles::builder() | ||
.with_file( | ||
#[allow(deprecated)] | ||
ProfileFileKind::Config, | ||
"conf", | ||
) | ||
.build(), | ||
), | ||
None, | ||
) | ||
.with_fs(Fs::from_slice(&[( | ||
"conf", | ||
"[default]\nauth_scheme_preference = sigv4, httpBasicAuth, httpDigestAuth, \thttpBearerAuth \t, httpApiKeyAuth ", | ||
)])); | ||
assert_eq!( | ||
AuthSchemePreference::from([ | ||
aws_runtime::auth::sigv4::SCHEME_ID, | ||
aws_smithy_runtime_api::client::auth::http::HTTP_BASIC_AUTH_SCHEME_ID, | ||
aws_smithy_runtime_api::client::auth::http::HTTP_DIGEST_AUTH_SCHEME_ID, | ||
aws_smithy_runtime_api::client::auth::http::HTTP_BEARER_AUTH_SCHEME_ID, | ||
aws_smithy_runtime_api::client::auth::http::HTTP_API_KEY_AUTH_SCHEME_ID, | ||
]), | ||
auth_scheme_preference_provider(&conf).await.unwrap() | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.