Skip to content

Commit 44ec779

Browse files
committed
Add try-catch blocks for refresh steps.
1 parent 5c97bcc commit 44ec779

File tree

1 file changed

+60
-25
lines changed

1 file changed

+60
-25
lines changed

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

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import software.aws.toolkits.telemetry.AuthType
3030
import software.aws.toolkits.telemetry.AwsTelemetry
3131
import software.aws.toolkits.telemetry.CredentialSourceId
3232
import software.aws.toolkits.telemetry.Result
33+
import java.io.FileNotFoundException
34+
import java.io.IOException
3335
import java.time.Clock
3436
import java.time.Duration
3537
import java.time.Instant
@@ -361,6 +363,7 @@ class SsoAccessTokenProvider(
361363
}
362364
}
363365

366+
// error being thrown while loading registration, add try-catch blocks.
364367
fun refreshToken(currentToken: AccessToken): AccessToken {
365368
if (currentToken.refreshToken == null) {
366369
val message = "Requested token refresh, but refresh token was null"
@@ -373,16 +376,29 @@ class SsoAccessTokenProvider(
373376
throw InvalidRequestException.builder().message(message).build()
374377
}
375378

376-
val registration = when (currentToken) {
377-
is DeviceAuthorizationGrantToken -> loadDagClientRegistration()
378-
is PKCEAuthorizationGrantToken -> loadPkceClientRegistration()
379+
var registration: ClientRegistration? = null
380+
try {
381+
registration = when (currentToken) {
382+
is DeviceAuthorizationGrantToken -> loadDagClientRegistration()
383+
is PKCEAuthorizationGrantToken -> loadPkceClientRegistration()
384+
}
385+
} catch (e: Exception) {
386+
val message = "Error loading client registration: ${e.message}"
387+
sendRefreshCredentialsMetric(
388+
currentToken,
389+
reason = "Failed to load client registration",
390+
reasonDesc = "Step: Load Registration - $message",
391+
result = Result.Failed
392+
)
393+
throw InvalidClientException.builder().message(message).cause(e).build()
379394
}
395+
380396
if (registration == null) {
381397
val message = "Unable to load client registration from cache"
382398
sendRefreshCredentialsMetric(
383399
currentToken,
384400
reason = "Null client registration",
385-
reasonDesc = message,
401+
reasonDesc = "Step: Load Registration - $message",
386402
result = Result.Failed
387403
)
388404
throw InvalidClientException.builder().message(message).build()
@@ -420,15 +436,13 @@ class SsoAccessTokenProvider(
420436
else -> null
421437
}
422438

423-
// AwsServiceException#message will automatically pull in AwsServiceException#awsErrorDetails
424-
// we expect messages for SsoOidcException to be populated in e.message using execution executor added in
425-
// https://github.com/aws/aws-toolkit-jetbrains/commit/cc9ed87fa9391dd39ac05cbf99b4437112fa3d10
426439
val message = e.message ?: "$stageName: ${e::class.java.name}"
440+
val reasonDesc = "Step: $stageName - $message"
427441

428442
sendRefreshCredentialsMetric(
429443
currentToken,
430444
reason = "Refresh access token request failed: $stageName",
431-
reasonDesc = message,
445+
reasonDesc = reasonDesc,
432446
requestId = requestId,
433447
result = Result.Failed
434448
)
@@ -442,40 +456,61 @@ class SsoAccessTokenProvider(
442456
SAVE_TOKEN,
443457
}
444458

459+
// these throw fileNotFoundException if file doesnt exist.
445460
private fun loadDagClientRegistration(): ClientRegistration? =
446-
cache.loadClientRegistration(dagClientRegistrationCacheKey)?.let {
447-
return it
461+
try {
462+
cache.loadClientRegistration(dagClientRegistrationCacheKey)?.let {
463+
return it
464+
}
465+
} catch (e: FileNotFoundException) {
466+
throw e
448467
}
449468

450469
private fun loadPkceClientRegistration(): PKCEClientRegistration? =
451-
cache.loadClientRegistration(pkceClientRegistrationCacheKey)?.let {
452-
return it as PKCEClientRegistration
470+
try {
471+
cache.loadClientRegistration(pkceClientRegistrationCacheKey)?.let {
472+
return it as PKCEClientRegistration
473+
}
474+
} catch (e: FileNotFoundException) {
475+
throw e
453476
}
454477

455478
private fun saveClientRegistration(registration: ClientRegistration) {
456-
when (registration) {
457-
is DeviceAuthorizationClientRegistration -> {
458-
cache.saveClientRegistration(dagClientRegistrationCacheKey, registration)
459-
}
479+
try {
480+
when (registration) {
481+
is DeviceAuthorizationClientRegistration -> {
482+
cache.saveClientRegistration(dagClientRegistrationCacheKey, registration)
483+
}
460484

461-
is PKCEClientRegistration -> {
462-
cache.saveClientRegistration(pkceClientRegistrationCacheKey, registration)
485+
is PKCEClientRegistration -> {
486+
cache.saveClientRegistration(pkceClientRegistrationCacheKey, registration)
487+
}
463488
}
489+
} catch (e: Exception) {
490+
throw e
464491
}
465492
}
466493

467494
private fun invalidateClientRegistration() {
468-
cache.invalidateClientRegistration(dagClientRegistrationCacheKey)
469-
cache.invalidateClientRegistration(pkceClientRegistrationCacheKey)
495+
try {
496+
cache.invalidateClientRegistration(dagClientRegistrationCacheKey)
497+
cache.invalidateClientRegistration(pkceClientRegistrationCacheKey)
498+
} catch (e: Exception) {
499+
throw e
500+
}
470501
}
471502

472503
private fun saveAccessToken(token: AccessToken) {
473-
when (token) {
474-
is DeviceAuthorizationGrantToken -> {
475-
cache.saveAccessToken(dagAccessTokenCacheKey, token)
476-
}
504+
try {
505+
when (token) {
506+
is DeviceAuthorizationGrantToken -> {
507+
cache.saveAccessToken(dagAccessTokenCacheKey, token)
508+
}
477509

478-
is PKCEAuthorizationGrantToken -> cache.saveAccessToken(pkceAccessTokenCacheKey, token)
510+
is PKCEAuthorizationGrantToken -> cache.saveAccessToken(pkceAccessTokenCacheKey, token)
511+
}
512+
} catch (e:Exception){
513+
throw e
479514
}
480515
}
481516

0 commit comments

Comments
 (0)