Skip to content
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

add: tests to ensure all interceptors are run if an error occurs in one #2664

Merged
merged 16 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust-runtime/aws-smithy-runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository = "https://github.com/awslabs/smithy-rs"
[features]
default = []
http-auth = ["dep:zeroize"]
anonymous-auth = []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, the anonymous auth stuff was always available. I added some more stuff related to anonymous auth and feature-gated it since we were already doing so for http-auth.


[dependencies]
aws-smithy-async = { path = "../aws-smithy-async" }
Expand Down
12 changes: 6 additions & 6 deletions rust-runtime/aws-smithy-runtime-api/src/client/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ impl AuthOptionResolverParams {
}

pub trait AuthOptionResolver: Send + Sync + fmt::Debug {
fn resolve_auth_options<'a>(
&'a self,
fn resolve_auth_options(
&self,
params: &AuthOptionResolverParams,
) -> Result<Cow<'a, [AuthSchemeId]>, BoxError>;
) -> Result<Cow<'_, [AuthSchemeId]>, BoxError>;
}

impl AuthOptionResolver for Box<dyn AuthOptionResolver> {
fn resolve_auth_options<'a>(
&'a self,
fn resolve_auth_options(
&self,
params: &AuthOptionResolverParams,
) -> Result<Cow<'a, [AuthSchemeId]>, BoxError> {
Comment on lines -51 to -61
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter was complaining about an unnecessary explicit lifetime here and in the auth option resolver so I switched it to an anonymous lifetime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I take it to mean that the compiler deterministically derives the anomalous lifeitme in this case from the first &.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's what happens AFAICT

) -> Result<Cow<'_, [AuthSchemeId]>, BoxError> {
(**self).resolve_auth_options(params)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ impl StaticAuthOptionResolver {
}

impl AuthOptionResolver for StaticAuthOptionResolver {
fn resolve_auth_options<'a>(
&'a self,
fn resolve_auth_options(
&self,
_params: &AuthOptionResolverParams,
) -> Result<Cow<'a, [AuthSchemeId]>, BoxError> {
) -> Result<Cow<'_, [AuthSchemeId]>, BoxError> {
Ok(Cow::Borrowed(&self.auth_options))
}
}
Expand All @@ -36,8 +36,14 @@ impl AuthOptionResolver for StaticAuthOptionResolver {
pub struct StaticAuthOptionResolverParams;

impl StaticAuthOptionResolverParams {
/// Creates new `StaticAuthOptionResolverParams`.
/// Creates a new `StaticAuthOptionResolverParams`.
pub fn new() -> Self {
Self
}
}

impl From<StaticAuthOptionResolverParams> for AuthOptionResolverParams {
fn from(params: StaticAuthOptionResolverParams) -> Self {
AuthOptionResolverParams::new(params)
}
}
27 changes: 3 additions & 24 deletions rust-runtime/aws-smithy-runtime-api/src/client/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use std::time::SystemTime;
#[cfg(feature = "http-auth")]
pub mod http;

#[cfg(feature = "anonymous-auth")]
pub mod anonymous;

pub trait IdentityResolver: Send + Sync + Debug {
fn resolve_identity(&self, config_bag: &ConfigBag) -> Future<Identity>;
}
Expand Down Expand Up @@ -65,30 +68,6 @@ impl Identity {
}
}

#[derive(Debug)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to aws_smithy_runtime_api::client::identity::anonymous

pub struct AnonymousIdentity;

impl AnonymousIdentity {
pub fn new() -> Self {
Self
}
}

#[derive(Debug)]
pub struct AnonymousIdentityResolver;

impl AnonymousIdentityResolver {
pub fn new() -> Self {
AnonymousIdentityResolver
}
}

impl IdentityResolver for AnonymousIdentityResolver {
fn resolve_identity(&self, _: &ConfigBag) -> Future<Identity> {
Future::ready(Ok(Identity::new(AnonymousIdentity::new(), None)))
}
}

pub mod builders {
use super::*;
use crate::client::auth::AuthSchemeId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

use crate::client::identity::{Identity, IdentityResolver};
use crate::client::orchestrator::Future;
use crate::config_bag::ConfigBag;

#[derive(Debug)]
pub struct AnonymousIdentity;

impl AnonymousIdentity {
pub fn new() -> Self {
Self
}
}

#[derive(Debug)]
pub struct AnonymousIdentityResolver;

impl AnonymousIdentityResolver {
pub fn new() -> Self {
AnonymousIdentityResolver
}
}

impl IdentityResolver for AnonymousIdentityResolver {
fn resolve_identity(&self, _: &ConfigBag) -> Future<Identity> {
Future::ready(Ok(Identity::new(AnonymousIdentity::new(), None)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use crate::client::interceptors::Interceptors;
use crate::config_bag::ConfigBag;

#[cfg(feature = "anonymous-auth")]
pub mod anonymous_auth;

pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;

pub trait RuntimePlugin {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

//! The [AnonymousAuthRuntimePlugin] and supporting code.

use super::RuntimePlugin;
use crate::client::auth::option_resolver::{
StaticAuthOptionResolver, StaticAuthOptionResolverParams,
};
use crate::client::auth::{AuthSchemeId, HttpAuthScheme, HttpAuthSchemes, HttpRequestSigner};
use crate::client::identity::anonymous::AnonymousIdentityResolver;
use crate::client::identity::{Identity, IdentityResolver, IdentityResolvers};
use crate::client::interceptors::Interceptors;
use crate::client::orchestrator::{ConfigBagAccessors, HttpRequest};
use crate::client::runtime_plugin::BoxError;
use crate::config_bag::ConfigBag;

const ANONYMOUS_AUTH_SCHEME_ID: AuthSchemeId = AuthSchemeId::new("anonymous");

/// A [RuntimePlugin] to provide anonymous authentication. This runtime plugin sets its own:
/// - [AuthOptionResolver]
/// - [AuthOptionResolverParams]
/// - [IdentityResolvers]
/// - [HttpAuthSchemes]
///
/// **The above components will replace any existing ones!** As such, don't use this plugin unless:
/// - You only need to make anonymous requests, such as when interacting with [Open Data](https://aws.amazon.com/opendata/).
/// - You're writing orchestrator tests and don't care about authentication.
pub struct AnonymousAuthRuntimePlugin;
Velfi marked this conversation as resolved.
Show resolved Hide resolved

impl RuntimePlugin for AnonymousAuthRuntimePlugin {
fn configure(
&self,
cfg: &mut ConfigBag,
_interceptors: &mut Interceptors,
) -> Result<(), BoxError> {
cfg.set_auth_option_resolver_params(StaticAuthOptionResolverParams::new().into());
cfg.set_auth_option_resolver(StaticAuthOptionResolver::new(vec![
ANONYMOUS_AUTH_SCHEME_ID,
]));
cfg.set_identity_resolvers(
IdentityResolvers::builder()
.identity_resolver(ANONYMOUS_AUTH_SCHEME_ID, AnonymousIdentityResolver::new())
.build(),
);
cfg.set_http_auth_schemes(
HttpAuthSchemes::builder()
.auth_scheme(ANONYMOUS_AUTH_SCHEME_ID, AnonymousAuthScheme::new())
.build(),
);

Ok(())
}
}

#[derive(Debug, Default)]
pub struct AnonymousAuthScheme {
signer: AnonymousSigner,
}

impl AnonymousAuthScheme {
pub fn new() -> Self {
Self::default()
}
}

#[derive(Debug, Default)]
struct AnonymousSigner;

impl HttpRequestSigner for AnonymousSigner {
fn sign_request(
&self,
_request: &mut HttpRequest,
_identity: &Identity,
_config_bag: &ConfigBag,
) -> Result<(), BoxError> {
Ok(())
}
}

impl HttpAuthScheme for AnonymousAuthScheme {
fn scheme_id(&self) -> AuthSchemeId {
ANONYMOUS_AUTH_SCHEME_ID
}

fn identity_resolver<'a>(
&self,
identity_resolvers: &'a IdentityResolvers,
) -> Option<&'a dyn IdentityResolver> {
identity_resolvers.identity_resolver(ANONYMOUS_AUTH_SCHEME_ID)
}

fn request_signer(&self) -> &dyn HttpRequestSigner {
&self.signer
}
}
3 changes: 3 additions & 0 deletions rust-runtime/aws-smithy-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository = "https://github.com/awslabs/smithy-rs"

[features]
http-auth = ["aws-smithy-runtime-api/http-auth"]
anonymous-auth = ["aws-smithy-runtime-api/anonymous-auth"]
test-util = ["dep:aws-smithy-protocol-test"]

[dependencies]
Expand All @@ -31,6 +32,8 @@ tracing = "0.1"
[dev-dependencies]
aws-smithy-async = { path = "../aws-smithy-async", features = ["rt-tokio"] }
tokio = { version = "1.25", features = ["macros", "rt", "test-util"] }
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
tracing-test = "0.2.1"

[package.metadata.docs.rs]
all-features = true
Expand Down
6 changes: 6 additions & 0 deletions rust-runtime/aws-smithy-runtime/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ pub mod connections;
/// used to limit the rate at which requests are sent.
pub mod retries;

/// Utilities for testing orchestrators. An orchestrator missing required components will panic when
/// run. This module contains stub components that can be used when you only care about testing some
/// specific aspect of the orchestrator.
#[cfg(feature = "test-util")]
pub mod test_util;

mod timeout;
Loading