Skip to content

Commit

Permalink
Fix subtle break of endpoint prefixes due to semver (#3318)
Browse files Browse the repository at this point in the history
When we released smithy-rs release-2023-12-08, we introduced a silent
failure for endpoint prefixes, and not in the newly released version,
but in the previous releases. There is a subtle issue with semver that
causes this. This PR addresses the endpoint prefix part of this problem.
Other PRs will fix other parts that are broken by this semver issue.

The issue is that unstable (0.x) runtime crates are declaring types that
get placed into the `ConfigBag`, and these types are referenced in the
`ConfigBag` across crate boundaries. This by itself isn't a problem, but
because our stable 1.x crates depend on the unstable crates, it becomes
a problem. By releasing 1.1.0 that depends on 0.61, consumers of 1.x
pull in both 0.60 and 0.61. The generated code pulls in 0.60, and the
1.1.x crates pull in 0.61. This is fine since two semver-incompatible
crate versions can be in the dependency closure. Thus, the generated
code which is using 0.60 is placing a 0.60 type in the `ConfigBag`, and
the runtime crates that pull the type out of the `ConfigBag` are
expecting a 0.61 version of it. This leads to the type not existing upon
lookup, which then causes the silent break.

This PR fixes this by moving the `EndpointPrefix` type and its
associated methods into `aws-smithy-runtime-api`, a stable crate. The
`aws-smithy-http` unstable crate is updated to point to the new stable
version so that a patch release of that crate will solve the issue for
old versions going forward as well.

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
  • Loading branch information
jdisanti authored Jan 12, 2024
1 parent e3f0de4 commit 30a801a
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 48 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
# meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client | server | all"}
# author = "rcoh"

[[aws-sdk-rust]]
message = "`EndpointPrefix` and `apply_endpoint` moved from aws-smithy-http to aws-smithy-runtime-api so that is in a stable (1.x) crate. A deprecated type alias was left in place with a note showing the new location."
references = ["smithy-rs#3318"]
meta = { "breaking" = false, "tada" = false, "bug" = false }
author = "jdisanti"

[[smithy-rs]]
message = "`EndpointPrefix` and `apply_endpoint` moved from aws-smithy-http to aws-smithy-runtime-api so that is in a stable (1.x) crate. A deprecated type alias was left in place with a note showing the new location."
references = ["smithy-rs#3318"]
meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client"}
author = "jdisanti"

[[smithy-rs]]
message = "The `Metadata` storable was moved from aws_smithy_http into aws_smithy_runtime_api. A deprecated type alias was left in place with a note showing where the new location is."
references = ["smithy-rs#3325"]
Expand Down
2 changes: 1 addition & 1 deletion aws/rust-runtime/aws-config/src/ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
use crate::http_credential_provider::HttpCredentialProvider;
use crate::provider_config::ProviderConfig;
use aws_credential_types::provider::{self, error::CredentialsError, future, ProvideCredentials};
use aws_smithy_http::endpoint::apply_endpoint;
use aws_smithy_runtime::client::endpoint::apply_endpoint;
use aws_smithy_runtime_api::client::dns::{ResolveDns, ResolveDnsError, SharedDnsResolver};
use aws_smithy_runtime_api::client::http::HttpConnectorSettings;
use aws_smithy_runtime_api::shared::IntoShared;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use http::HeaderValue;

/// Interceptor that adds an Accept header to API Gateway requests.
#[derive(Debug, Default)]
pub(crate) struct AcceptHeaderInterceptor;
pub(crate) struct AcceptHeaderInterceptor {
_priv: (),
}

impl Intercept for AcceptHeaderInterceptor {
fn name(&self) -> &'static str {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class EndpointTraitBindings(
private val endpointTrait: EndpointTrait,
) {
private val inputShape = operationShape.inputShape(model)
private val endpointPrefix = RuntimeType.smithyHttp(runtimeConfig).resolve("endpoint::EndpointPrefix")
private val endpointPrefix = RuntimeType.smithyRuntimeApiClient(runtimeConfig).resolve("client::endpoint::EndpointPrefix")

/**
* Render the `EndpointPrefix` struct. [input] refers to the symbol referring to the input of this operation.
Expand Down Expand Up @@ -82,8 +82,8 @@ class EndpointTraitBindings(
rustTemplate(
contents,
"InvalidEndpointError" to
RuntimeType.smithyHttp(runtimeConfig)
.resolve("endpoint::error::InvalidEndpointError"),
RuntimeType.smithyRuntimeApiClient(runtimeConfig)
.resolve("client::endpoint::error::InvalidEndpointError"),
)
}
"${label.content} = $field"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ internal class EndpointTraitBindingsTest {
)
implBlock(symbolProvider.toSymbol(model.lookup("test#GetStatusInput"))) {
rustBlockTemplate(
"fn endpoint_prefix(&self) -> std::result::Result<#{endpoint}::EndpointPrefix, #{endpoint}::error::InvalidEndpointError>",
"endpoint" to RuntimeType.smithyHttp(TestRuntimeConfig).resolve("endpoint"),
"fn endpoint_prefix(&self) -> std::result::Result<#{EndpointPrefix}, #{InvalidEndpointError}>",
"EndpointPrefix" to RuntimeType.smithyRuntimeApiClient(TestRuntimeConfig).resolve("client::endpoint::EndpointPrefix"),
"InvalidEndpointError" to RuntimeType.smithyRuntimeApiClient(TestRuntimeConfig).resolve("client::endpoint::error::InvalidEndpointError"),
) {
endpointBindingGenerator.render(this, "self")
}
Expand Down Expand Up @@ -162,8 +163,8 @@ internal class EndpointTraitBindingsTest {
"""
async fn test_endpoint_prefix() {
use #{capture_request};
use aws_smithy_http::endpoint::EndpointPrefix;
use aws_smithy_runtime_api::box_error::BoxError;
use aws_smithy_runtime_api::client::endpoint::EndpointPrefix;
use aws_smithy_runtime_api::client::runtime_components::RuntimeComponents;
use aws_smithy_types::body::SdkBody;
use aws_smithy_types::config_bag::ConfigBag;
Expand Down
49 changes: 16 additions & 33 deletions rust-runtime/aws-smithy-http/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,41 @@

//! Code for resolving an endpoint (URI) that a request should be sent to
#![allow(deprecated)]

use crate::endpoint::error::InvalidEndpointError;
use http::uri::{Authority, Uri};
use std::borrow::Cow;
use std::fmt::Debug;
use std::result::Result as StdResult;
use std::str::FromStr;

use http::uri::{Authority, Uri};

use aws_smithy_types::config_bag::{Storable, StoreReplace};
pub use error::ResolveEndpointError;

use crate::endpoint::error::InvalidEndpointError;

pub mod error;
pub use error::ResolveEndpointError;

/// An endpoint-resolution-specific Result. Contains either an [`Endpoint`](aws_smithy_types::endpoint::Endpoint) or a [`ResolveEndpointError`].
#[deprecated(since = "0.60.1", note = "Was never used.")]
pub type Result = std::result::Result<aws_smithy_types::endpoint::Endpoint, ResolveEndpointError>;

/// A special type that adds support for services that have special URL-prefixing rules.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EndpointPrefix(String);
impl EndpointPrefix {
/// Create a new endpoint prefix from an `impl Into<String>`. If the prefix argument is invalid,
/// a [`InvalidEndpointError`] will be returned.
pub fn new(prefix: impl Into<String>) -> StdResult<Self, InvalidEndpointError> {
let prefix = prefix.into();
match Authority::from_str(&prefix) {
Ok(_) => Ok(EndpointPrefix(prefix)),
Err(err) => Err(InvalidEndpointError::failed_to_construct_authority(
prefix, err,
)),
}
}

/// Get the `str` representation of this `EndpointPrefix`.
pub fn as_str(&self) -> &str {
&self.0
}
}

impl Storable for EndpointPrefix {
type Storer = StoreReplace<Self>;
}
#[deprecated(
since = "0.60.1",
note = "Use aws_smithy_runtime_api::client::endpoint::EndpointPrefix instead."
)]
pub type EndpointPrefix = aws_smithy_runtime_api::client::endpoint::EndpointPrefix;

/// Apply `endpoint` to `uri`
///
/// This method mutates `uri` by setting the `endpoint` on it
#[deprecated(
since = "0.60.1",
note = "Use aws_smithy_runtime::client::endpoint::apply_endpoint instead."
)]
pub fn apply_endpoint(
uri: &mut Uri,
endpoint: &Uri,
prefix: Option<&EndpointPrefix>,
) -> StdResult<(), InvalidEndpointError> {
let prefix = prefix.map(|p| p.0.as_str()).unwrap_or("");
let prefix = prefix.map(EndpointPrefix::as_str).unwrap_or("");
let authority = endpoint
.authority()
.as_ref()
Expand Down
156 changes: 156 additions & 0 deletions rust-runtime/aws-smithy-runtime-api/src/client/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use crate::impl_shared_conversions;
use aws_smithy_types::config_bag::{Storable, StoreReplace};
use aws_smithy_types::endpoint::Endpoint;
use aws_smithy_types::type_erasure::TypeErasedBox;
use error::InvalidEndpointError;
use http::uri::Authority;
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;

new_type_future! {
Expand Down Expand Up @@ -71,3 +74,156 @@ impl ResolveEndpoint for SharedEndpointResolver {
impl ValidateConfig for SharedEndpointResolver {}

impl_shared_conversions!(convert SharedEndpointResolver from ResolveEndpoint using SharedEndpointResolver::new);

/// A special type that adds support for services that have special URL-prefixing rules.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EndpointPrefix(String);
impl EndpointPrefix {
/// Create a new endpoint prefix from an `impl Into<String>`. If the prefix argument is invalid,
/// a [`InvalidEndpointError`] will be returned.
pub fn new(prefix: impl Into<String>) -> Result<Self, InvalidEndpointError> {
let prefix = prefix.into();
match Authority::from_str(&prefix) {
Ok(_) => Ok(EndpointPrefix(prefix)),
Err(err) => Err(InvalidEndpointError::failed_to_construct_authority(
prefix, err,
)),
}
}

/// Get the `str` representation of this `EndpointPrefix`.
pub fn as_str(&self) -> &str {
&self.0
}
}

impl Storable for EndpointPrefix {
type Storer = StoreReplace<Self>;
}

/// Errors related to endpoint resolution and validation
pub mod error {
use crate::box_error::BoxError;
use std::error::Error as StdError;
use std::fmt;

/// Endpoint resolution failed
#[derive(Debug)]
pub struct ResolveEndpointError {
message: String,
source: Option<BoxError>,
}

impl ResolveEndpointError {
/// Create an [`ResolveEndpointError`] with a message
pub fn message(message: impl Into<String>) -> Self {
Self {
message: message.into(),
source: None,
}
}

/// Add a source to the error
pub fn with_source(self, source: Option<BoxError>) -> Self {
Self { source, ..self }
}

/// Create a [`ResolveEndpointError`] from a message and a source
pub fn from_source(message: impl Into<String>, source: impl Into<BoxError>) -> Self {
Self::message(message).with_source(Some(source.into()))
}
}

impl fmt::Display for ResolveEndpointError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}

impl StdError for ResolveEndpointError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.source.as_ref().map(|err| err.as_ref() as _)
}
}

#[derive(Debug)]
pub(super) enum InvalidEndpointErrorKind {
EndpointMustHaveScheme,
FailedToConstructAuthority { authority: String, source: BoxError },
FailedToConstructUri { source: BoxError },
}

/// An error that occurs when an endpoint is found to be invalid. This usually occurs due to an
/// incomplete URI.
#[derive(Debug)]
pub struct InvalidEndpointError {
pub(super) kind: InvalidEndpointErrorKind,
}

impl InvalidEndpointError {
/// Construct a build error for a missing scheme
pub fn endpoint_must_have_scheme() -> Self {
Self {
kind: InvalidEndpointErrorKind::EndpointMustHaveScheme,
}
}

/// Construct a build error for an invalid authority
pub fn failed_to_construct_authority(
authority: impl Into<String>,
source: impl Into<Box<dyn StdError + Send + Sync + 'static>>,
) -> Self {
Self {
kind: InvalidEndpointErrorKind::FailedToConstructAuthority {
authority: authority.into(),
source: source.into(),
},
}
}

/// Construct a build error for an invalid URI
pub fn failed_to_construct_uri(
source: impl Into<Box<dyn StdError + Send + Sync + 'static>>,
) -> Self {
Self {
kind: InvalidEndpointErrorKind::FailedToConstructUri {
source: source.into(),
},
}
}
}

impl From<InvalidEndpointErrorKind> for InvalidEndpointError {
fn from(kind: InvalidEndpointErrorKind) -> Self {
Self { kind }
}
}

impl fmt::Display for InvalidEndpointError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use InvalidEndpointErrorKind as ErrorKind;
match &self.kind {
ErrorKind::EndpointMustHaveScheme => write!(f, "endpoint must contain a valid scheme"),
ErrorKind::FailedToConstructAuthority { authority, source: _ } => write!(
f,
"endpoint must contain a valid authority when combined with endpoint prefix: {authority}"
),
ErrorKind::FailedToConstructUri { .. } => write!(f, "failed to construct URI"),
}
}
}

impl StdError for InvalidEndpointError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
use InvalidEndpointErrorKind as ErrorKind;
match &self.kind {
ErrorKind::FailedToConstructUri { source } => Some(source.as_ref()),
ErrorKind::FailedToConstructAuthority {
authority: _,
source,
} => Some(source.as_ref()),
ErrorKind::EndpointMustHaveScheme => None,
}
}
}
}
5 changes: 5 additions & 0 deletions rust-runtime/aws-smithy-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ rustdoc-args = ["--cfg", "docsrs"]

[package.metadata.smithy-rs-release-tooling]
stable = true

# aws-smithy-http is used by the http-auth feature, which is not turned on by the SDK at all.
# Without ignoring it, the `check-aws-sdk-smoketest-docs-clippy-udeps` CI script fails.
[package.metadata.cargo-udeps.ignore]
normal = ["aws-smithy-http"]
2 changes: 2 additions & 0 deletions rust-runtime/aws-smithy-runtime/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub mod defaults;

pub mod dns;

pub mod endpoint;

/// Built-in Smithy HTTP clients and connectors.
///
/// See the [module docs in `aws-smithy-runtime-api`](aws_smithy_runtime_api::client::http)
Expand Down
Loading

0 comments on commit 30a801a

Please sign in to comment.