|
| 1 | +package aws.sdk.kotlin.services.bedrock |
| 2 | + |
| 3 | +import aws.sdk.kotlin.services.bedrock.auth.finalizeBearerTokenConfig |
| 4 | +import aws.smithy.kotlin.runtime.auth.AuthSchemeId |
| 5 | +import aws.smithy.kotlin.runtime.collections.Attributes |
| 6 | +import aws.smithy.kotlin.runtime.collections.emptyAttributes |
| 7 | +import aws.smithy.kotlin.runtime.http.Headers |
| 8 | +import aws.smithy.kotlin.runtime.http.HttpBody |
| 9 | +import aws.smithy.kotlin.runtime.http.HttpCall |
| 10 | +import aws.smithy.kotlin.runtime.http.HttpStatusCode |
| 11 | +import aws.smithy.kotlin.runtime.http.auth.BearerToken |
| 12 | +import aws.smithy.kotlin.runtime.http.auth.BearerTokenProvider |
| 13 | +import aws.smithy.kotlin.runtime.http.engine.HttpClientEngine |
| 14 | +import aws.smithy.kotlin.runtime.http.engine.HttpClientEngineBase |
| 15 | +import aws.smithy.kotlin.runtime.http.engine.HttpClientEngineConfig |
| 16 | +import aws.smithy.kotlin.runtime.http.request.HttpRequest |
| 17 | +import aws.smithy.kotlin.runtime.http.response.HttpResponse |
| 18 | +import aws.smithy.kotlin.runtime.io.use |
| 19 | +import aws.smithy.kotlin.runtime.operation.ExecutionContext |
| 20 | +import aws.smithy.kotlin.runtime.time.Instant |
| 21 | +import aws.smithy.kotlin.runtime.util.TestPlatformProvider |
| 22 | +import kotlinx.coroutines.test.runTest |
| 23 | +import kotlin.test.assertEquals |
| 24 | +import kotlin.test.assertNotNull |
| 25 | + |
| 26 | +class BedrockEnvironmentBearerTokenTest { |
| 27 | + private fun mockHttpClient(handler: (HttpRequest) -> HttpResponse): HttpClientEngine { |
| 28 | + return object : HttpClientEngineBase("test engine") { |
| 29 | + override val config: HttpClientEngineConfig = HttpClientEngineConfig.Default |
| 30 | + |
| 31 | + override suspend fun roundTrip(context: ExecutionContext, request: HttpRequest): HttpCall { |
| 32 | + val response = handler(request) |
| 33 | + return HttpCall(request, response, Instant.now(), Instant.now()) |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private val mockPlatformProvider = TestPlatformProvider( |
| 39 | + env = mapOf("AWS_BEARER_TOKEN_BEDROCK" to "bedrock-token"), |
| 40 | + ) |
| 41 | + |
| 42 | + fun testAuthSchemePreferenceConfigured() = runTest { |
| 43 | + val builder = BedrockClient.Builder() |
| 44 | + val expectedAuthSchemePreference = listOf(AuthSchemeId.HttpBearer) |
| 45 | + finalizeBearerTokenConfig(builder, mockPlatformProvider) |
| 46 | + assertEquals(expectedAuthSchemePreference, builder.config.authSchemePreference) |
| 47 | + } |
| 48 | + |
| 49 | + fun testBearerAuthSchemePromotedToFirst() = runTest { |
| 50 | + val builder = BedrockClient.Builder() |
| 51 | + builder.config.authSchemePreference = listOf(AuthSchemeId.AwsSigV4) |
| 52 | + finalizeBearerTokenConfig(builder, mockPlatformProvider) |
| 53 | + val expectedAuthSchemePreference = listOf(AuthSchemeId.HttpBearer, AuthSchemeId.AwsSigV4) |
| 54 | + assertEquals(expectedAuthSchemePreference, builder.config.authSchemePreference) |
| 55 | + |
| 56 | + builder.config.authSchemePreference = listOf(AuthSchemeId.AwsSigV4, AuthSchemeId.HttpBearer) |
| 57 | + assertEquals(expectedAuthSchemePreference, builder.config.authSchemePreference) |
| 58 | + } |
| 59 | + |
| 60 | + fun testBearerTokenProviderConfigured() = runTest { |
| 61 | + val builder = BedrockClient.Builder() |
| 62 | + finalizeBearerTokenConfig(builder, mockPlatformProvider) |
| 63 | + assertNotNull(builder.config.bearerTokenProvider) |
| 64 | + val token = builder.config.bearerTokenProvider!!.resolve() |
| 65 | + assertNotNull(token) |
| 66 | + assertEquals("bedrock-token", token.token) |
| 67 | + } |
| 68 | + |
| 69 | + fun testExplicitProviderTakesPrecedence() = runTest { |
| 70 | + val builder = BedrockClient.Builder() |
| 71 | + builder.config.bearerTokenProvider = object : BearerTokenProvider { |
| 72 | + override suspend fun resolve(attributes: Attributes): BearerToken = object : BearerToken { |
| 73 | + override val token: String = "different-bedrock-token" |
| 74 | + override val attributes: Attributes = emptyAttributes() |
| 75 | + override val expiration: Instant? = null |
| 76 | + } |
| 77 | + } |
| 78 | + finalizeBearerTokenConfig(builder, mockPlatformProvider) |
| 79 | + assertNotNull(builder.config.bearerTokenProvider) |
| 80 | + val token = builder.config.bearerTokenProvider!!.resolve() |
| 81 | + assertNotNull(token) |
| 82 | + assertEquals("different-bedrock-token", token.token) |
| 83 | + } |
| 84 | + |
| 85 | + fun testBearerTokenProviderFunctionality() = runTest { |
| 86 | + var capturedAuthHeader: String? = null |
| 87 | + |
| 88 | + BedrockClient.fromEnvironment { |
| 89 | + region = "us-west-2" |
| 90 | + httpClient = mockHttpClient { request -> |
| 91 | + // Capture the Authorization header |
| 92 | + capturedAuthHeader = request.headers["Authorization"] |
| 93 | + HttpResponse( |
| 94 | + status = HttpStatusCode.OK, |
| 95 | + headers = Headers.Empty, |
| 96 | + body = HttpBody.Empty, |
| 97 | + ) |
| 98 | + } |
| 99 | + }.use { client -> |
| 100 | + // Make an api call to capture Authorization header |
| 101 | + client.listFoundationModels() |
| 102 | + |
| 103 | + assertNotNull(capturedAuthHeader) |
| 104 | + assertEquals("Bearer bedrock-token", capturedAuthHeader) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments