Skip to content

feat: add support for bedrock api key auth #1317

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ object RuntimeTypes {
val BearerTokenAuthScheme = symbol("BearerTokenAuthScheme")
val BearerTokenProviderConfig = symbol("BearerTokenProviderConfig")
val BearerTokenProvider = symbol("BearerTokenProvider")
val BearerToken = symbol("BearerToken")
val EnvironmentBearerTokenProvider = symbol("EnvironmentBearerTokenProvider")

val reprioritizeAuthOptions = symbol("reprioritizeAuthOptions")
}
Expand Down
6 changes: 6 additions & 0 deletions runtime/auth/http-auth/api/http-auth.api
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public final class aws/smithy/kotlin/runtime/http/auth/BearerTokenSigner : aws/s
public abstract interface class aws/smithy/kotlin/runtime/http/auth/CloseableBearerTokenProvider : aws/smithy/kotlin/runtime/http/auth/BearerTokenProvider, java/io/Closeable {
}

public final class aws/smithy/kotlin/runtime/http/auth/EnvironmentBearerTokenProvider : aws/smithy/kotlin/runtime/http/auth/BearerTokenProvider {
public fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;)V
public synthetic fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class aws/smithy/kotlin/runtime/http/auth/ReprioritizeAuthOptionsKt {
public static final fun reprioritizeAuthOptions (Ljava/util/List;Ljava/util/List;)Ljava/util/List;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.smithy.kotlin.runtime.http.auth

import aws.smithy.kotlin.runtime.collections.Attributes
import aws.smithy.kotlin.runtime.collections.emptyAttributes
import aws.smithy.kotlin.runtime.time.Instant
import aws.smithy.kotlin.runtime.util.PlatformProvider

/**
* A [BearerTokenProvider] that extracts the bearer token from the target environment variable.
*/
public class EnvironmentBearerTokenProvider(
private val key: String,
private val platform: PlatformProvider = PlatformProvider.System,
) : BearerTokenProvider {
override suspend fun resolve(attributes: Attributes): BearerToken {
val bearerToken = platform.getenv(key)
?: error("$key environment variable is not set")

return object : BearerToken {
override val token: String = bearerToken
override val attributes: Attributes = emptyAttributes()
override val expiration: Instant? = null
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package aws.smithy.kotlin.runtime.http.auth

import aws.smithy.kotlin.runtime.collections.emptyAttributes
import aws.smithy.kotlin.runtime.util.TestPlatformProvider
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class EnvironmentBearerTokenProviderTest {
@Test
fun testResolveWithValidToken() = runTest {
val provider = EnvironmentBearerTokenProvider(
"TEST_TOKEN",
TestPlatformProvider(mutableMapOf("TEST_TOKEN" to "test-bearer-token")),
)

val token = provider.resolve()

assertEquals("test-bearer-token", token.token)
}

@Test
fun testResolveWithMissingToken() = runTest {
val provider = EnvironmentBearerTokenProvider(
"MISSING_TEST_TOKEN",
TestPlatformProvider(mutableMapOf()),
)

val exception = assertFailsWith<IllegalStateException> {
provider.resolve(emptyAttributes())
}
assertEquals("MISSING_TEST_TOKEN environment variable is not set", exception.message)
}
}
Loading