Skip to content

Commit c77546f

Browse files
committed
feedback
1 parent 7f285c5 commit c77546f

File tree

6 files changed

+42
-50
lines changed

6 files changed

+42
-50
lines changed

aws-runtime/aws-config/api/aws-config.api

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,17 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/InvalidSsoTokenExcept
102102
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
103103
}
104104

105-
public final class aws/sdk/kotlin/runtime/auth/credentials/LoginCredentialsProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider {
105+
public final class aws/sdk/kotlin/runtime/auth/credentials/LoginCredentialsProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CloseableCredentialsProvider {
106106
public fun <init> (Ljava/lang/String;Ljava/lang/String;Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/time/Clock;)V
107107
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/time/Clock;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
108+
public fun close ()V
108109
public final fun getHttpClient ()Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;
109110
public final fun getLoginSession ()Ljava/lang/String;
110111
public final fun getPlatformProvider ()Laws/smithy/kotlin/runtime/util/PlatformProvider;
111112
public final fun getRegion ()Ljava/lang/String;
112113
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
113114
}
114115

115-
public final class aws/sdk/kotlin/runtime/auth/credentials/LoginTokenProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider {
116-
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;JLaws/smithy/kotlin/runtime/http/engine/HttpClientEngine;Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/time/Clock;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
117-
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;JLaws/smithy/kotlin/runtime/http/engine/HttpClientEngine;Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/time/Clock;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
118-
public final fun getHttpClient ()Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;
119-
public final fun getLoginSessionName ()Ljava/lang/String;
120-
public final fun getPlatformProvider ()Laws/smithy/kotlin/runtime/util/PlatformProvider;
121-
public final fun getRefreshBufferWindow-UwyO8pc ()J
122-
public final fun getRegion ()Ljava/lang/String;
123-
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
124-
}
125-
126116
public final class aws/sdk/kotlin/runtime/auth/credentials/ProcessCredentialsProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider {
127117
public fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;JJ)V
128118
public synthetic fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;JJILkotlin/jvm/internal/DefaultConstructorMarker;)V

aws-runtime/aws-config/build.gradle.kts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ kotlin {
7171
fun awsModelFile(name: String): String =
7272
rootProject.file("codegen/sdk/aws-models/$name").relativeTo(project.layout.buildDirectory.get().asFile).toString()
7373

74-
fun awsShapeFile(name: String): String =
75-
rootProject.file("codegen/sdk/aws-shapes/$name").relativeTo(project.layout.buildDirectory.get().asFile).toString()
76-
7774
val codegen by configurations.getting
7875
dependencies {
7976
codegen(project(":codegen:aws-sdk-codegen"))
8077
codegen(libs.smithy.cli)
8178
codegen(libs.smithy.model)
79+
codegen(libs.smithy.aws.smoke.test.model)
8280
}
8381

8482
smithyBuild {
@@ -195,7 +193,6 @@ smithyBuild {
195193
create("signin-credentials-provider") {
196194
imports = listOf(
197195
awsModelFile("sign-in.json"),
198-
awsShapeFile("shapes.json"),
199196
)
200197

201198
val serviceShape = "com.amazonaws.signin#Signin"

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/auth/credentials/LoginCredentialsProvider.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55

66
package aws.sdk.kotlin.runtime.auth.credentials
77

8+
import aws.sdk.kotlin.runtime.auth.credentials.internal.signin.SigninClient
89
import aws.sdk.kotlin.runtime.http.interceptors.businessmetrics.AwsBusinessMetric
910
import aws.sdk.kotlin.runtime.http.interceptors.businessmetrics.withBusinessMetric
11+
import aws.smithy.kotlin.runtime.auth.awscredentials.CloseableCredentialsProvider
1012
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
1113
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider
1214
import aws.smithy.kotlin.runtime.collections.Attributes
1315
import aws.smithy.kotlin.runtime.http.engine.HttpClientEngine
1416
import aws.smithy.kotlin.runtime.telemetry.logging.logger
1517
import aws.smithy.kotlin.runtime.time.Clock
1618
import aws.smithy.kotlin.runtime.util.PlatformProvider
19+
import kotlinx.coroutines.runBlocking
1720
import kotlin.coroutines.coroutineContext
1821

1922
/**
@@ -54,7 +57,10 @@ public class LoginCredentialsProvider public constructor(
5457
public val httpClient: HttpClientEngine? = null,
5558
public val platformProvider: PlatformProvider = PlatformProvider.System,
5659
private val clock: Clock = Clock.System,
57-
) : CredentialsProvider {
60+
) : CloseableCredentialsProvider {
61+
private val cacheDirectory = resolveCacheDir(platformProvider)
62+
private val client = runBlocking { signinClient(region, httpClient) }
63+
5864
override suspend fun resolve(attributes: Attributes): Credentials {
5965
val logger = coroutineContext.logger<LoginCredentialsProvider>()
6066

@@ -65,11 +71,26 @@ public class LoginCredentialsProvider public constructor(
6571
httpClient = httpClient,
6672
platformProvider = platformProvider,
6773
clock = clock,
74+
cacheDirectory = cacheDirectory,
75+
client = client,
6876
)
6977

7078
logger.trace { "Attempting to load token using token provider for login-session: `$loginSession`" }
7179
val creds = loginTokenProvider.resolve(attributes)
7280

7381
return creds.withBusinessMetric(AwsBusinessMetric.Credentials.CREDENTIALS_LOGIN)
7482
}
83+
84+
override fun close() {
85+
client.close()
86+
}
7587
}
88+
89+
internal fun resolveCacheDir(platformProvider: PlatformProvider) =
90+
platformProvider.getenv("AWS_LOGIN_IN_CACHE_DIRECTORY") ?: platformProvider.filepath("~", ".aws", "login", "cache")
91+
92+
internal suspend fun signinClient(providedRegion: String? = null, providedHttpClient: HttpClientEngine? = null) =
93+
SigninClient.fromEnvironment {
94+
region = providedRegion
95+
httpClient = providedHttpClient
96+
}

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/auth/credentials/LoginTokenProvider.kt

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import aws.sdk.kotlin.runtime.auth.credentials.internal.signin.SigninClient
1111
import aws.sdk.kotlin.runtime.auth.credentials.internal.signin.createOAuth2Token
1212
import aws.sdk.kotlin.runtime.auth.credentials.internal.signin.model.AccessDeniedException
1313
import aws.sdk.kotlin.runtime.auth.credentials.internal.signin.model.OAuth2ErrorCode
14+
import aws.sdk.kotlin.runtime.auth.credentials.internal.signin.withConfig
1415
import aws.sdk.kotlin.runtime.config.profile.normalizePath
1516
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
1617
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider
@@ -30,7 +31,6 @@ import aws.smithy.kotlin.runtime.serde.json.jsonStreamWriter
3031
import aws.smithy.kotlin.runtime.serde.json.nextTokenOf
3132
import aws.smithy.kotlin.runtime.telemetry.logging.debug
3233
import aws.smithy.kotlin.runtime.telemetry.logging.error
33-
import aws.smithy.kotlin.runtime.telemetry.telemetryProvider
3434
import aws.smithy.kotlin.runtime.text.encoding.decodeBase64Bytes
3535
import aws.smithy.kotlin.runtime.text.encoding.encodeToHex
3636
import aws.smithy.kotlin.runtime.time.Clock
@@ -85,13 +85,15 @@ private class DpopInterceptor(private val dpopKeyPem: String) : HttpInterceptor
8585
* @param platformProvider the platform provider to use
8686
* @param clock the source of time for the provider
8787
*/
88-
public class LoginTokenProvider(
89-
public val loginSessionName: String,
90-
public val region: String? = null,
91-
public val refreshBufferWindow: Duration = DEFAULT_SIGNIN_TOKEN_REFRESH_BUFFER_SECONDS.seconds,
92-
public val httpClient: HttpClientEngine? = null,
93-
public val platformProvider: PlatformProvider = PlatformProvider.System,
94-
private val clock: Clock = Clock.System,
88+
internal class LoginTokenProvider(
89+
val loginSessionName: String,
90+
val region: String? = null,
91+
val refreshBufferWindow: Duration = DEFAULT_SIGNIN_TOKEN_REFRESH_BUFFER_SECONDS.seconds,
92+
val httpClient: HttpClientEngine? = null,
93+
val platformProvider: PlatformProvider = PlatformProvider.System,
94+
val clock: Clock = Clock.System,
95+
val cacheDirectory: String,
96+
val client: SigninClient,
9597
) : CredentialsProvider {
9698

9799
// debounce concurrent requests for a token
@@ -111,7 +113,7 @@ public class LoginTokenProvider(
111113
}
112114

113115
private suspend fun getToken(attributes: Attributes): LoginToken {
114-
val token = readLoginTokenFromCache(loginSessionName, platformProvider)
116+
val token = readLoginTokenFromCache(loginSessionName, platformProvider, cacheDirectory)
115117

116118
if (clock.now() < (token.expiresAt - refreshBufferWindow)) {
117119
coroutineContext.debug<LoginTokenProvider> { "using cached token for login-session: $loginSessionName" }
@@ -144,8 +146,7 @@ public class LoginTokenProvider(
144146

145147
private suspend fun writeToken(refreshed: LoginToken) {
146148
val cacheKey = getLoginCacheFilename(loginSessionName)
147-
val directory = resolveCacheDir(platformProvider)
148-
val filepath = normalizePath(platformProvider.filepath(directory, cacheKey), platformProvider)
149+
val filepath = normalizePath(platformProvider.filepath(cacheDirectory, cacheKey), platformProvider)
149150
val contents = serializeLoginToken(refreshed)
150151
try {
151152
platformProvider.writeFile(filepath, contents)
@@ -159,12 +160,7 @@ public class LoginTokenProvider(
159160
throw InvalidLoginTokenException(message ?: "Login token for login-session: $loginSessionName is expired", cause)
160161

161162
private suspend fun refreshToken(oldToken: LoginToken): LoginToken {
162-
val telemetry = coroutineContext.telemetryProvider
163-
164-
SigninClient.fromEnvironment {
165-
region = this@LoginTokenProvider.region
166-
httpClient = this@LoginTokenProvider.httpClient
167-
telemetryProvider = telemetry
163+
client.withConfig {
168164
interceptors += DpopInterceptor(oldToken.dpopKey)
169165
}.use { client ->
170166
return try {
@@ -331,20 +327,15 @@ private fun generateDpopProof(
331327
return "$message.${ base64UrlNoPadding.encode(signature) }"
332328
}
333329

334-
internal suspend fun readLoginTokenFromCache(cacheKey: String, platformProvider: PlatformProvider): LoginToken {
330+
internal suspend fun readLoginTokenFromCache(cacheKey: String, platformProvider: PlatformProvider, cacheDirectory: String): LoginToken {
335331
val key = getLoginCacheFilename(cacheKey)
336332
val bytes = with(platformProvider) {
337-
val directory = resolveCacheDir(this)
338-
val defaultCacheLocation = normalizePath(directory, this)
333+
val defaultCacheLocation = normalizePath(cacheDirectory, this)
339334
readFileOrNull(filepath(defaultCacheLocation, key))
340335
} ?: throw ProviderConfigurationException("Invalid or missing login session cache. Run `aws login` to initiate a new session")
341336
return deserializeLoginToken(bytes)
342337
}
343338

344-
private fun resolveCacheDir(platformProvider: PlatformProvider) =
345-
platformProvider.getenv("AWS_LOGIN_IN_CACHE_DIRECTORY")
346-
?: platformProvider.filepath("~", ".aws", "login", "cache")
347-
348339
internal fun getLoginCacheFilename(cacheKey: String): String {
349340
val sha256HexDigest = cacheKey.trim().encodeToByteArray().sha256().encodeToHex()
350341
return "$sha256HexDigest.json"

aws-runtime/aws-config/common/test/aws/sdk/kotlin/runtime/auth/credentials/LoginTokenProviderTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ class LoginTokenProviderTest {
152152
httpClient = httpClient,
153153
platformProvider = testPlatform,
154154
clock = testClock,
155+
cacheDirectory = resolveCacheDir(testPlatform),
156+
client = signinClient(providedHttpClient = httpClient),
155157
)
156158

157159
testCase.outcomes.forEach { expectedOutcome ->

codegen/sdk/aws-shapes/shapes.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)