|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import aws.sdk.kotlin.services.sesv2.SesV2Client |
| 7 | +import aws.sdk.kotlin.services.sesv2.sendEmail |
| 8 | +import aws.smithy.kotlin.runtime.auth.awssigning.crt.CrtAwsSigner |
| 9 | +import aws.smithy.kotlin.runtime.client.ProtocolRequestInterceptorContext |
| 10 | +import aws.smithy.kotlin.runtime.http.HttpException |
| 11 | +import aws.smithy.kotlin.runtime.http.auth.SigV4AsymmetricAuthScheme |
| 12 | +import aws.smithy.kotlin.runtime.http.interceptors.HttpInterceptor |
| 13 | +import aws.smithy.kotlin.runtime.http.request.HttpRequest |
| 14 | +import kotlinx.coroutines.runBlocking |
| 15 | +import kotlin.test.Ignore |
| 16 | +import kotlin.test.Test |
| 17 | +import kotlin.test.assertContains |
| 18 | +import kotlin.test.assertEquals |
| 19 | +import kotlin.test.assertFailsWith |
| 20 | +import kotlin.test.assertNotNull |
| 21 | + |
| 22 | +class Sigv4aTest { |
| 23 | + @Test |
| 24 | + @Ignore // TODO enable once SESv2 model adds endpointId and Sigv4a |
| 25 | + |
| 26 | + fun testSigv4a() = runBlocking { |
| 27 | + val interceptor = RequestCapturingInterceptor() |
| 28 | + |
| 29 | + SesV2Client.fromEnvironment { |
| 30 | + retryStrategy { |
| 31 | + maxAttempts = 1 // The call is intended to fail, no sense trying more than once |
| 32 | + } |
| 33 | + |
| 34 | + interceptors += interceptor |
| 35 | + |
| 36 | + authSchemes = listOf(SigV4AsymmetricAuthScheme(CrtAwsSigner, "ses")) |
| 37 | + }.use { ses -> |
| 38 | + assertFailsWith<HttpException> { |
| 39 | + ses.sendEmail { |
| 40 | + // endpointId = "bdm3x3zl.n5x" // TODO uncomment |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + assertEquals(1, interceptor.requests.size) |
| 46 | + val request = interceptor.requests.single() |
| 47 | + |
| 48 | + assertContains("bdm3x3zl.n5x.endpoints.email.amazonaws.com", request.url.host.toString()) // Correct endpoint? |
| 49 | + |
| 50 | + val authHeader = assertNotNull( |
| 51 | + request.headers["Authorization"], |
| 52 | + "Missing Authorization header, found: ${request.headers.entries().map { it.key }}", |
| 53 | + ) |
| 54 | + assertContains(authHeader, "AWS4-ECDSA-P256-SHA256") // Verify that request was signed with Sigv4a |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +private class RequestCapturingInterceptor : HttpInterceptor { |
| 59 | + val requests = mutableListOf<HttpRequest>() |
| 60 | + |
| 61 | + override fun readBeforeTransmit(context: ProtocolRequestInterceptorContext<Any, HttpRequest>) { |
| 62 | + requests += context.protocolRequest |
| 63 | + } |
| 64 | +} |
0 commit comments