Skip to content

Commit c92a166

Browse files
authored
Take AWS error message only if details exists in refreshToken failure (#4611)
1 parent 6febd0a commit c92a166

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "bugfix",
3+
"description" : "Fix refresh token failure due to null aws error details"
4+
}

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/sso/SsoAccessTokenProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ class SsoAccessTokenProvider(
406406
else -> null
407407
}
408408
val message = when (e) {
409-
is AwsServiceException -> e.awsErrorDetails().errorMessage()
409+
is AwsServiceException -> e.awsErrorDetails()?.errorMessage() ?: "Unknown error"
410410
else -> e.message ?: "Unknown error"
411411
}
412412
sendFailedRefreshCredentialsMetricIfNeeded(

plugins/core/jetbrains-community/tst/software/aws/toolkits/jetbrains/core/credentials/sso/SsoAccessTokenProviderTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.mockito.kotlin.mock
2424
import org.mockito.kotlin.stub
2525
import org.mockito.kotlin.times
2626
import org.mockito.kotlin.verify
27+
import software.amazon.awssdk.awscore.exception.AwsServiceException
2728
import software.amazon.awssdk.services.ssooidc.SsoOidcClient
2829
import software.amazon.awssdk.services.ssooidc.model.AuthorizationPendingException
2930
import software.amazon.awssdk.services.ssooidc.model.CreateTokenRequest
@@ -300,6 +301,31 @@ class SsoAccessTokenProviderTest {
300301
verify(ssoCache).saveAccessToken(argThat<DeviceGrantAccessTokenCacheKey> { startUrl == ssoUrl }, eq(refreshedToken))
301302
}
302303

304+
@Test
305+
fun `refresh access token error handling does not fail if AWS error details are missing`() {
306+
val expirationClientRegistration = clock.instant().plusSeconds(120)
307+
setupCacheStub(expirationClientRegistration)
308+
309+
val accessToken = DeviceAuthorizationGrantToken(ssoUrl, ssoRegion, "dummyToken", "refreshToken", clock.instant())
310+
ssoCache.stub {
311+
on(
312+
ssoCache.loadAccessToken(argThat<DeviceGrantAccessTokenCacheKey> { startUrl == ssoUrl })
313+
).thenReturn(
314+
accessToken
315+
)
316+
}
317+
318+
ssoOidcClient.stub {
319+
on(
320+
ssoOidcClient.createToken(refreshTokenRequest())
321+
)
322+
.thenThrow(AwsServiceException.builder().build())
323+
}
324+
325+
assertThatThrownBy { runBlocking { sut.refreshToken(sut.accessToken()) } }
326+
.isInstanceOf(AwsServiceException::class.java)
327+
}
328+
303329
@Test
304330
fun `PKCE refresh access token saves PKCE token`() {
305331
setPkceTrue()

0 commit comments

Comments
 (0)