Skip to content

Commit 45eee55

Browse files
authored
feat: emit bearer business metric (#1348)
1 parent 2be96f0 commit 45eee55

File tree

5 files changed

+67
-14
lines changed

5 files changed

+67
-14
lines changed

runtime/auth/http-auth/api/http-auth.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public abstract interface class aws/smithy/kotlin/runtime/http/auth/CloseableBea
6262
public final class aws/smithy/kotlin/runtime/http/auth/EnvironmentBearerTokenProvider : aws/smithy/kotlin/runtime/http/auth/BearerTokenProvider {
6363
public fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;)V
6464
public synthetic fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
65+
public fun <init> (Ljava/lang/String;Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;)V
66+
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
6567
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
6668
}
6769

runtime/auth/http-auth/common/src/aws/smithy/kotlin/runtime/http/auth/EnvironmentBearerTokenProvider.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,37 @@
44
*/
55
package aws.smithy.kotlin.runtime.http.auth
66

7+
import aws.smithy.kotlin.runtime.businessmetrics.SmithyBusinessMetric
8+
import aws.smithy.kotlin.runtime.businessmetrics.emitBusinessMetric
79
import aws.smithy.kotlin.runtime.collections.Attributes
8-
import aws.smithy.kotlin.runtime.collections.emptyAttributes
10+
import aws.smithy.kotlin.runtime.collections.mutableAttributes
911
import aws.smithy.kotlin.runtime.time.Instant
1012
import aws.smithy.kotlin.runtime.util.PlatformProvider
1113

1214
/**
13-
* A [BearerTokenProvider] that extracts the bearer token from the target environment variable.
15+
* A [BearerTokenProvider] that extracts the bearer token from JVM system properties or environment variables.
1416
*/
1517
public class EnvironmentBearerTokenProvider(
16-
private val key: String,
18+
private val sysPropKey: String,
19+
private val envKey: String,
1720
private val platform: PlatformProvider = PlatformProvider.System,
1821
) : BearerTokenProvider {
22+
@Deprecated("This constructor does not support a parameter for a system property key and will be removed in version 1.6.x")
23+
public constructor(
24+
envKey: String,
25+
platform: PlatformProvider = PlatformProvider.System,
26+
) : this("", envKey, platform)
27+
1928
override suspend fun resolve(attributes: Attributes): BearerToken {
20-
val bearerToken = platform.getenv(key)
21-
?: error("$key environment variable is not set")
29+
val bearerToken = sysPropKey.takeUnless(String::isBlank)?.let(platform::getProperty)
30+
?: platform.getenv(envKey)
31+
if (bearerToken.isNullOrBlank()) throw IllegalStateException("""Missing values for system property "$sysPropKey" and environment variable "$envKey"""")
2232

2333
return object : BearerToken {
2434
override val token: String = bearerToken
25-
override val attributes: Attributes = emptyAttributes()
35+
override val attributes: Attributes = mutableAttributes().apply {
36+
emitBusinessMetric(SmithyBusinessMetric.BEARER_SERVICE_ENV_VARS)
37+
}
2638
override val expiration: Instant? = null
2739
}
2840
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package aws.smithy.kotlin.runtime.http.auth
22

3-
import aws.smithy.kotlin.runtime.collections.emptyAttributes
43
import aws.smithy.kotlin.runtime.util.TestPlatformProvider
54
import kotlinx.coroutines.test.runTest
65
import kotlin.test.Test
@@ -9,27 +8,65 @@ import kotlin.test.assertFailsWith
98

109
class EnvironmentBearerTokenProviderTest {
1110
@Test
12-
fun testResolveWithValidToken() = runTest {
11+
fun testResolveFromEnvVar() = runTest {
1312
val provider = EnvironmentBearerTokenProvider(
14-
"TEST_TOKEN",
15-
TestPlatformProvider(mutableMapOf("TEST_TOKEN" to "test-bearer-token")),
13+
"TEST_SYS_PROPS_TOKEN",
14+
"TEST_ENV_TOKEN",
15+
TestPlatformProvider(
16+
env = mapOf("TEST_ENV_TOKEN" to "test-env-bearer-token"),
17+
),
1618
)
1719

1820
val token = provider.resolve()
1921

20-
assertEquals("test-bearer-token", token.token)
22+
assertEquals("test-env-bearer-token", token.token)
23+
}
24+
25+
@Test
26+
fun testResolveFromSysProps() = runTest {
27+
val provider = EnvironmentBearerTokenProvider(
28+
"TEST_SYS_PROPS_TOKEN",
29+
"TEST_ENV_TOKEN",
30+
TestPlatformProvider(
31+
props = mapOf("TEST_SYS_PROPS_TOKEN" to "test-sys-props-bearer-token"),
32+
),
33+
)
34+
35+
val token = provider.resolve()
36+
37+
assertEquals("test-sys-props-bearer-token", token.token)
38+
}
39+
40+
@Test
41+
fun testResolutionOrder() = runTest {
42+
val provider = EnvironmentBearerTokenProvider(
43+
"TEST_SYS_PROPS_TOKEN",
44+
"TEST_ENV_TOKEN",
45+
TestPlatformProvider(
46+
props = mapOf("TEST_SYS_PROPS_TOKEN" to "test-sys-props-bearer-token"),
47+
env = mapOf("TEST_ENV_TOKEN" to "test-env-bearer-token"),
48+
),
49+
)
50+
51+
val token = provider.resolve()
52+
53+
assertEquals("test-sys-props-bearer-token", token.token)
2154
}
2255

2356
@Test
2457
fun testResolveWithMissingToken() = runTest {
2558
val provider = EnvironmentBearerTokenProvider(
2659
"MISSING_TEST_TOKEN",
27-
TestPlatformProvider(mutableMapOf()),
60+
"MISSING_TEST_TOKEN",
61+
TestPlatformProvider(
62+
env = mapOf("TEST_TOKEN" to "test-env-bearer-token"),
63+
props = mapOf("TEST_TOKEN" to "test-sys-props-bearer-token"),
64+
),
2865
)
2966

3067
val exception = assertFailsWith<IllegalStateException> {
31-
provider.resolve(emptyAttributes())
68+
provider.resolve()
3269
}
33-
assertEquals("MISSING_TEST_TOKEN environment variable is not set", exception.message)
70+
assertEquals("Missing values for system property \"MISSING_TEST_TOKEN\" and environment variable \"MISSING_TEST_TOKEN\"", exception.message)
3471
}
3572
}

runtime/runtime-core/api/runtime-core.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public final class aws/smithy/kotlin/runtime/businessmetrics/SmithyBusinessMetri
107107
public static final field ACCOUNT_ID_MODE_DISABLED Laws/smithy/kotlin/runtime/businessmetrics/SmithyBusinessMetric;
108108
public static final field ACCOUNT_ID_MODE_PREFERRED Laws/smithy/kotlin/runtime/businessmetrics/SmithyBusinessMetric;
109109
public static final field ACCOUNT_ID_MODE_REQUIRED Laws/smithy/kotlin/runtime/businessmetrics/SmithyBusinessMetric;
110+
public static final field BEARER_SERVICE_ENV_VARS Laws/smithy/kotlin/runtime/businessmetrics/SmithyBusinessMetric;
110111
public static final field FLEXIBLE_CHECKSUMS_REQ_CRC32 Laws/smithy/kotlin/runtime/businessmetrics/SmithyBusinessMetric;
111112
public static final field FLEXIBLE_CHECKSUMS_REQ_CRC32C Laws/smithy/kotlin/runtime/businessmetrics/SmithyBusinessMetric;
112113
public static final field FLEXIBLE_CHECKSUMS_REQ_SHA1 Laws/smithy/kotlin/runtime/businessmetrics/SmithyBusinessMetric;

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public enum class SmithyBusinessMetric(public override val identifier: String) :
102102
FLEXIBLE_CHECKSUMS_REQ_WHEN_REQUIRED("a"),
103103
FLEXIBLE_CHECKSUMS_RES_WHEN_SUPPORTED("b"),
104104
FLEXIBLE_CHECKSUMS_RES_WHEN_REQUIRED("c"),
105+
BEARER_SERVICE_ENV_VARS("3"),
105106
;
106107

107108
override fun toString(): String = identifier

0 commit comments

Comments
 (0)