Skip to content

Commit

Permalink
Add support for converting PresignedRequest to http 1.x request (#3696)
Browse files Browse the repository at this point in the history
## Checklist
<!--- If a checkbox below is not applicable, then please DELETE it
rather than leaving it unchecked -->
- [ ] I have updated `CHANGELOG.next.toml` if I made changes to the
smithy-rs codegen or runtime crates
- [ ] I have updated `CHANGELOG.next.toml` if I made changes to the AWS
SDK, generated SDK code, or SDK runtime crates

----

_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
Velfi authored Jun 17, 2024
1 parent d759349 commit 9009d1b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 21 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ message = "Add documentation on the default configuration to `from_env`, `load_f
references = ["aws-sdk-rust#1162"]
meta = { "breaking" = false, "tada" = false, "bug" = false }
author = "ysaito1001"

[[aws-sdk-rust]]
message = """
Pre-signed requests may now be converted to Http v1.x requests. This requires enabling the `http-1x` feature for the SDK in question.
Then, call `PresignedRequest::make_http_1x_request` or `PresignedRequest::into_http_1x_request`.
"""
references = ["smithy-rs#3696"]
meta = { "breaking" = false, "tada" = true, "bug" = false }
author = "Velfi"
5 changes: 5 additions & 0 deletions aws/rust-runtime/aws-inlineable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ license = "Apache-2.0"
publish = false
repository = "https://github.com/smithy-lang/smithy-rs"

[features]
http_1x = ["dep:http-1x", "dep:http-body-1x", "aws-smithy-runtime-api/http-1x"]

[dependencies]
# Used by lru, and this forces it to be a later version that avoids
# https://github.com/tkaitchuck/aHash/issues/200
Expand All @@ -30,6 +33,8 @@ fastrand = "2.0.0"
hex = "0.4.3"
http = "0.2.9"
http-body = "0.4.5"
http-1x = { package = "http", version = "1", optional = true }
http-body-1x = { package = "http-body", version = "1", optional = true }
hmac = "0.12"
lru = "0.12.2"
ring = "0.17.5"
Expand Down
15 changes: 15 additions & 0 deletions aws/rust-runtime/aws-inlineable/src/presigning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ impl PresignedRequest {
.expect("constructor validated convertibility")
.map(|_req| body)
}

#[cfg(feature = "http-1x")]
/// Given a body, produce an `http_1x::Request` from this `PresignedRequest`
pub fn make_http_1x_request<B>(&self, body: B) -> http_1x::Request<B> {
self.clone().into_http_1x_request(body)
}

#[cfg(feature = "http-1x")]
/// Converts this `PresignedRequest` directly into an `http_1x` request.
pub fn into_http_1x_request<B>(self, body: B) -> http_1x::Request<B> {
self.http_request
.try_into_http1x()
.expect("constructor validated convertibility")
.map(|_req| body)
}
}

impl fmt::Debug for PresignedRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.generators.client.Inter
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.fluentBuilderType
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.RequestSerializerGenerator
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.rustlang.Feature
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.docs
Expand All @@ -36,14 +37,14 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.preludeScope
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
import software.amazon.smithy.rust.codegen.core.smithy.contextName
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization
import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization
import software.amazon.smithy.rust.codegen.core.util.cloneOperation
import software.amazon.smithy.rust.codegen.core.util.expectTrait
import software.amazon.smithy.rust.codegen.core.util.thenSingletonListOf
import software.amazon.smithy.rustsdk.traits.PresignableTrait
import kotlin.streams.toList

private val presigningTypes: Array<Pair<String, Any>> =
arrayOf(
Expand Down Expand Up @@ -101,12 +102,6 @@ class AwsPresigningDecorator internal constructor(
override val name: String = "AwsPresigning"
override val order: Byte = ORDER

private val codegenScope =
arrayOf(
*presigningTypes,
*preludeScope,
)

/**
* Adds presignable trait to known presignable operations and creates synthetic presignable shapes for codegen
*/
Expand Down Expand Up @@ -160,13 +155,29 @@ class AwsPresigningDecorator internal constructor(
self.execute(move |sender, conf|sender.presign(conf, presigning_config)).await
}
""",
*codegenScope,
"CustomizablePresigned" to CustomizablePresigned,
*preludeScope,
*presigningTypes,
"CustomizablePresigned" to customizablePresigned,
)
}
}

private val CustomizablePresigned =
override fun extras(
codegenContext: ClientCodegenContext,
rustCrate: RustCrate,
) {
if (anyPresignedShapes(codegenContext)) {
rustCrate.mergeFeature(
Feature(
"http-1x",
default = false,
listOf("dep:http-1x", "dep:http-body-1x", "aws-smithy-runtime-api/http-1x"),
),
)
}
}

private val customizablePresigned =
RuntimeType.forInlineFun("CustomizablePresigned", InternalTraitsModule) {
rustTemplate(
"""
Expand All @@ -175,7 +186,8 @@ class AwsPresigningDecorator internal constructor(
}
""",
*codegenScope,
*preludeScope,
*presigningTypes,
)
}
}
Expand All @@ -186,8 +198,8 @@ class AwsPresignedFluentBuilderMethod(
private val runtimeConfig = codegenContext.runtimeConfig
private val codegenScope =
arrayOf(
*presigningTypes,
*preludeScope,
*presigningTypes,
"Error" to AwsRuntimeType.presigning().resolve("config::Error"),
"SdkError" to RuntimeType.sdkError(runtimeConfig),
)
Expand Down Expand Up @@ -246,8 +258,7 @@ class AwsPresignedFluentBuilderMethod(
}
}
""",
*preludeScope,
*presigningTypes,
*codegenScope,
"OperationError" to section.operationErrorType,
"SdkError" to RuntimeType.sdkError(runtimeConfig),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ fun RuntimeConfig.awsRoot(): RuntimeCrateLocation {

object AwsRuntimeType {
fun presigning(): RuntimeType =
RuntimeType.forInlineDependency(InlineAwsDependency.forRustFile("presigning", visibility = Visibility.PUBLIC))
RuntimeType.forInlineDependency(
InlineAwsDependency.forRustFile(
"presigning", visibility = Visibility.PUBLIC,
CargoDependency.Http1x,
CargoDependency.HttpBody1x,
),
)

fun presigningInterceptor(runtimeConfig: RuntimeConfig): RuntimeType =
RuntimeType.forInlineDependency(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ class InlineDependency(
CargoDependency.smithyTypes(runtimeConfig),
)

fun constrained(): InlineDependency =
InlineDependency.forRustFile(ConstrainedModule, "/inlineable/src/constrained.rs")
fun constrained(): InlineDependency = forRustFile(ConstrainedModule, "/inlineable/src/constrained.rs")
}
}

Expand All @@ -181,6 +180,7 @@ data class CargoDependency(
val features: Set<String> = emptySet(),
val defaultFeatures: Boolean = true,
val rustName: String = name.replace("-", "_"),
val `package`: String? = null,
) : RustDependency(name) {
val key: Triple<String, DependencyLocation, DependencyScope> get() = Triple(name, location, scope)

Expand Down Expand Up @@ -225,6 +225,9 @@ data class CargoDependency(
if (!defaultFeatures) {
attribs["default-features"] = false
}
if (`package`.isNotNullOrEmpty()) {
attribs["package"] = `package`.toString()
}
return attribs
}

Expand Down Expand Up @@ -264,10 +267,6 @@ data class CargoDependency(
val Flate2: CargoDependency = CargoDependency("flate2", CratesIo("1.0.30"))
val Hex: CargoDependency = CargoDependency("hex", CratesIo("0.4.3"))
val Hmac: CargoDependency = CargoDependency("hmac", CratesIo("0.12"))
val Http: CargoDependency = CargoDependency("http", CratesIo("0.2.9"))
val HttpBody: CargoDependency = CargoDependency("http-body", CratesIo("0.4.4"))
val Hyper: CargoDependency = CargoDependency("hyper", CratesIo("0.14.26"))
val HyperWithStream: CargoDependency = Hyper.withFeature("stream")
val LazyStatic: CargoDependency = CargoDependency("lazy_static", CratesIo("1.4.0"))
val Lru: CargoDependency = CargoDependency("lru", CratesIo("0.12.2"))
val Md5: CargoDependency = CargoDependency("md-5", CratesIo("0.10.0"), rustName = "md5")
Expand Down Expand Up @@ -325,6 +324,17 @@ data class CargoDependency(
features = setOf("no-env-filter"),
)

// Hyper 0.x types
val Http: CargoDependency = CargoDependency("http", CratesIo("0.2.9"))
val HttpBody: CargoDependency = CargoDependency("http-body", CratesIo("0.4.4"))
val Hyper: CargoDependency = CargoDependency("hyper", CratesIo("0.14.26"))
val HyperWithStream: CargoDependency = Hyper.withFeature("stream")

// Hyper 1.x types
val Http1x: CargoDependency = CargoDependency("http-1x", CratesIo("1"), `package` = "http", optional = true)
val HttpBody1x: CargoDependency =
CargoDependency("http-body-1x", CratesIo("1"), `package` = "http-body", optional = true)

fun smithyAsync(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-async")

fun smithyChecksums(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-checksums")
Expand Down Expand Up @@ -370,3 +380,5 @@ data class CargoDependency(
CargoDependency("serde", CratesIo("1.0"), features = setOf("derive"), scope = DependencyScope.CfgUnstable)
}
}

private fun String?.isNotNullOrEmpty(): Boolean = !this.isNullOrEmpty()

0 comments on commit 9009d1b

Please sign in to comment.