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

refactor!: replace default HTTP client engine #554

Merged
merged 5 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion aws-runtime/aws-config/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ kotlin {
implementation("aws.smithy.kotlin:logging:$smithyKotlinVersion")
implementation("aws.smithy.kotlin:http:$smithyKotlinVersion")
implementation("aws.smithy.kotlin:utils:$smithyKotlinVersion")
implementation("aws.smithy.kotlin:http-client-engine-crt:$smithyKotlinVersion")
implementation("aws.smithy.kotlin:http-client-engine-default:$smithyKotlinVersion")
implementation(project(":aws-runtime:aws-http"))

// parsing common JSON credentials responses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package aws.sdk.kotlin.runtime.auth.credentials

import aws.sdk.kotlin.runtime.config.AwsSdkSetting
import aws.sdk.kotlin.runtime.config.imds.ImdsClient
import aws.smithy.kotlin.runtime.http.engine.DefaultHttpEngine
import aws.smithy.kotlin.runtime.http.engine.HttpClientEngine
import aws.smithy.kotlin.runtime.http.engine.crt.CrtHttpEngine
import aws.smithy.kotlin.runtime.io.Closeable
import aws.smithy.kotlin.runtime.util.Platform
import aws.smithy.kotlin.runtime.util.PlatformProvider
Expand Down Expand Up @@ -42,7 +42,7 @@ public class DefaultChainCredentialsProvider constructor(
) : CredentialsProvider, Closeable {

private val manageEngine = httpClientEngine == null
private val httpClientEngine = httpClientEngine ?: CrtHttpEngine()
private val httpClientEngine = httpClientEngine ?: DefaultHttpEngine()

private val chain = CredentialsProviderChain(
EnvironmentCredentialsProvider(platformProvider::getenv),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import aws.sdk.kotlin.runtime.config.resolve
import aws.smithy.kotlin.runtime.ServiceException
import aws.smithy.kotlin.runtime.client.ExecutionContext
import aws.smithy.kotlin.runtime.http.*
import aws.smithy.kotlin.runtime.http.engine.DefaultHttpEngine
import aws.smithy.kotlin.runtime.http.engine.HttpClientEngine
import aws.smithy.kotlin.runtime.http.engine.crt.CrtHttpEngine
import aws.smithy.kotlin.runtime.http.middleware.ResolveEndpoint
import aws.smithy.kotlin.runtime.http.middleware.Retry
import aws.smithy.kotlin.runtime.http.operation.*
Expand Down Expand Up @@ -66,7 +66,7 @@ public class EcsCredentialsProvider internal constructor(
public constructor() : this(Platform)

private val manageEngine = httpClientEngine == null
private val httpClientEngine = httpClientEngine ?: CrtHttpEngine()
private val httpClientEngine = httpClientEngine ?: DefaultHttpEngine()

private val retryMiddleware = run {
val tokenBucket = StandardRetryTokenBucket(StandardRetryTokenBucketOptions.Default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import aws.smithy.kotlin.runtime.client.ExecutionContext
import aws.smithy.kotlin.runtime.client.SdkClientOption
import aws.smithy.kotlin.runtime.client.SdkLogMode
import aws.smithy.kotlin.runtime.http.*
import aws.smithy.kotlin.runtime.http.engine.DefaultHttpEngine
import aws.smithy.kotlin.runtime.http.engine.HttpClientEngine
import aws.smithy.kotlin.runtime.http.engine.crt.CrtHttpEngine
import aws.smithy.kotlin.runtime.http.middleware.ResolveEndpoint
import aws.smithy.kotlin.runtime.http.middleware.Retry
import aws.smithy.kotlin.runtime.http.operation.*
Expand Down Expand Up @@ -73,7 +73,7 @@ public class ImdsClient private constructor(builder: Builder) : InstanceMetadata

init {
require(maxRetries > 0) { "maxRetries must be greater than zero" }
val engine = builder.engine ?: CrtHttpEngine {
val engine = builder.engine ?: DefaultHttpEngine {
connectTimeout = 1.seconds
socketReadTimeout = 1.seconds
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class ImdsClientTest {
connection.assertRequests()
}

@IgnoreWindows("DNS fails faster on windows and results in a different error")
@IgnoreWindows("DNS fails faster on windows and results in a different error: `socket connect failure, no route to host`")
@Test
fun testHttpConnectTimeouts() = runBlocking {
// end-to-end real client times out after 1-second
Expand All @@ -227,8 +227,9 @@ class ImdsClientTest {
client.get("/latest/metadata")
}
}
// on windows DNS fails faster with message `socket connect failure, no route to host.
assertTrue(ex.message!!.lowercase().contains("timed out"), "message `${ex.message}`")

// this message is asserted against whatever the default HTTP client engine throws for an exception...
assertTrue(ex.message!!.lowercase().contains("timeout has expired"), "message `${ex.message}`")
val elapsed = Instant.now().epochMilliseconds - start.epochMilliseconds
assertTrue(elapsed >= 1000, "expected elapsed ms to be greater than 1000; actual = $elapsed")
assertTrue(elapsed < 2000, "expected elapsed ms to be less than 2000; actual = $elapsed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import aws.sdk.kotlin.codegen.protocols.middleware.AwsSignatureVersion4
import aws.sdk.kotlin.codegen.sdkId
import software.amazon.smithy.aws.traits.auth.UnsignedPayloadTrait
import software.amazon.smithy.codegen.core.CodegenException
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.kotlin.codegen.core.*
import software.amazon.smithy.kotlin.codegen.model.buildSymbol
import software.amazon.smithy.kotlin.codegen.model.hasIdempotentTokenMember
Expand All @@ -32,12 +31,6 @@ open class AwsHttpProtocolClientGenerator(
httpBindingResolver: HttpBindingResolver
) : HttpProtocolClientGenerator(ctx, middlewares, httpBindingResolver) {

override val defaultHttpClientEngineSymbol: Symbol
get() = buildSymbol {
name = "CrtHttpEngine"
namespace(KotlinDependency.AWS_CRT_HTTP_ENGINE)
}

override fun render(writer: KotlinWriter) {
writer.write("\n\n")
writer.write("const val ServiceId: String = #S", ctx.service.sdkId)
Expand Down
1 change: 1 addition & 0 deletions services/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ subprojects {
implementation(kotlin("test"))
implementation(kotlin("test-junit5"))
implementation(project(":aws-runtime:testing"))
implementation(project(":tests:e2e-test-util"))
}
}
kotlinOptions {
Expand Down
6 changes: 3 additions & 3 deletions services/polly/e2eTest/PollyPresignerTest.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package aws.sdk.kotlin.services.polly

import aws.sdk.kotlin.runtime.http.engine.crt.CrtHttpEngine
import aws.sdk.kotlin.services.polly.model.OutputFormat
import aws.sdk.kotlin.services.polly.model.SynthesizeSpeechRequest
import aws.sdk.kotlin.services.polly.model.VoiceId
import aws.sdk.kotlin.services.polly.presigners.presign
import aws.sdk.kotlin.testing.withAllEngines
import aws.smithy.kotlin.runtime.http.response.complete
import aws.smithy.kotlin.runtime.http.sdkHttpClient
import kotlinx.coroutines.runBlocking
Expand All @@ -30,13 +30,13 @@ class PollyPresignerTest {
val client = PollyClient { region = "us-east-1" }
val presignedRequest = request.presign(client.config, 10.seconds)

CrtHttpEngine().use { engine ->
withAllEngines { engine ->
val httpClient = sdkHttpClient(engine)

val call = httpClient.call(presignedRequest)
call.complete()

assertEquals(200, call.response.status.value)
assertEquals(200, call.response.status.value, "presigned polly request failed for engine: $engine")
}
}
}
67 changes: 61 additions & 6 deletions services/s3/e2eTest/S3IntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package aws.sdk.kotlin.e2etest

import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.model.GetObjectRequest
import aws.sdk.kotlin.testing.PRINTABLE_CHARS
import aws.sdk.kotlin.testing.withAllEngines
import aws.smithy.kotlin.runtime.content.ByteStream
import aws.smithy.kotlin.runtime.content.decodeToString
import aws.smithy.kotlin.runtime.content.fromFile
Expand Down Expand Up @@ -100,15 +102,68 @@ class S3BucketOpsIntegrationTest {
}

@Test
fun testListObjectsWithDelimiter(): Unit = runBlocking {
fun testQueryParameterEncoding(): Unit = runBlocking {
// see: https://github.com/awslabs/aws-sdk-kotlin/issues/448

client.listObjects {
bucket = testBucket
delimiter = "/"
prefix = null
// this is mostly a stress test of signing w.r.t query parameter encoding (since
// delimiter is bound via @httpQuery) and the ability of an HTTP engine to keep
// the same encoding going out on the wire (e.g. not double percent encoding)

s3WithAllEngines { s3 ->
s3.listObjects {
bucket = testBucket
delimiter = PRINTABLE_CHARS
prefix = null
}
// only care that request is accepted, not the results
}
}

@Test
fun testPathEncoding(): Unit = runBlocking {
// this is mostly a stress test of signing w.r.t path encoding (since key is bound
// via @httpLabel) and the ability of an HTTP engine to keep the same encoding going
// out on the wire (e.g. not double percent encoding)

// NOTE: S3 provides guidance on choosing object key names: https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
// This test includes all printable chars (including ones S3 recommends avoiding). Users should
// strive to fall within the guidelines given by S3 though

s3WithAllEngines { s3 ->
val objKey = "foo$PRINTABLE_CHARS"
val content = "hello rfc3986"

s3.putObject {
bucket = testBucket
key = objKey
body = ByteStream.fromString(content)
}

val req = GetObjectRequest {
bucket = testBucket
key = objKey
}

s3.getObject(req) { resp ->
val actual = resp.body!!.decodeToString()
assertEquals(content, actual)
}
}
}
}

// only care that request is accepted, not the results
internal suspend fun s3WithAllEngines(block: suspend (S3Client) -> Unit) {
withAllEngines { engine ->
S3Client {
region = S3BucketOpsIntegrationTest.DEFAULT_REGION
httpClientEngine = engine
}.use {
try {
block(it)
} catch (ex: Exception) {
println("test failed for engine $engine")
throw ex
}
}
}
}
Loading