Skip to content

Commit d968e0f

Browse files
committed
add modifyCredentials + load stages to LoadClientRegistration flow
1 parent 2731e60 commit d968e0f

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

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

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,17 @@ class DiskCache(
104104
override fun loadClientRegistration(cacheKey: ClientRegistrationCacheKey): ClientRegistration? {
105105
LOG.debug { "loadClientRegistration for $cacheKey" }
106106
val inputStream = clientRegistrationCache(cacheKey).tryInputStreamIfExists()
107-
?: return null
108-
107+
if (inputStream == null) {
108+
val stage = LoadCredentialStage.ACCESS_FILE
109+
AwsTelemetry.modifyCredentials(
110+
credentialModification = CredentialModification.Unknown,
111+
result = Result.Failed,
112+
reason = "Failed to load ClientRegistration",
113+
reasonDesc = "Load Step:$stage failed: previous error writing to file or deleted",
114+
source = "loadClientRegistration"
115+
)
116+
return null
117+
}
109118
return loadClientRegistration(inputStream)
110119
}
111120

@@ -127,15 +136,10 @@ class DiskCache(
127136
result = Result.Failed,
128137
reason = "Failed to invalidate ClientRegistration",
129138
reasonDesc = e.message,
130-
source = "DiskCache.invalidateClientRegistration"
139+
source = "invalidateClientRegistration"
131140
)
132141
throw e
133142
}
134-
AwsTelemetry.modifyCredentials(
135-
credentialModification = CredentialModification.Delete,
136-
result = Result.Succeeded,
137-
source = "DiskCache.invalidateClientRegistration"
138-
)
139143
}
140144

141145
override fun invalidateAccessToken(ssoUrl: String) {
@@ -152,11 +156,6 @@ class DiskCache(
152156
)
153157
throw e
154158
}
155-
AwsTelemetry.modifyCredentials(
156-
credentialModification = CredentialModification.Delete,
157-
result = Result.Succeeded,
158-
source = "DiskCache.invalidateAccessToken"
159-
)
160159
}
161160

162161
override fun loadAccessToken(cacheKey: AccessTokenCacheKey): AccessToken? {
@@ -191,11 +190,6 @@ class DiskCache(
191190
)
192191
throw e
193192
}
194-
AwsTelemetry.modifyCredentials(
195-
credentialModification = CredentialModification.Delete,
196-
result = Result.Succeeded,
197-
source = "DiskCache.invalidateAccessToken"
198-
)
199193
}
200194

201195
private fun clientRegistrationCache(ssoRegion: String): Path = cacheDir.resolve("aws-toolkit-jetbrains-client-id-$ssoRegion.json")
@@ -222,15 +216,34 @@ class DiskCache(
222216
return cacheDir.resolve(fileName)
223217
}
224218

225-
private fun loadClientRegistration(inputStream: InputStream) =
226-
tryOrNull {
219+
private fun loadClientRegistration(inputStream: InputStream): ClientRegistration? {
220+
var stage = LoadCredentialStage.VALIDATE_CREDENTIALS
221+
try{
227222
val clientRegistration = objectMapper.readValue<ClientRegistration>(inputStream)
223+
stage = LoadCredentialStage.CHECK_EXPIRATION
228224
if (clientRegistration.expiresAt.isNotExpired()) {
229-
clientRegistration
225+
return clientRegistration
230226
} else {
231-
null
227+
AwsTelemetry.modifyCredentials(
228+
credentialModification = CredentialModification.Unknown,
229+
result = Result.Failed,
230+
reason = "Failed to load ClientRegistration",
231+
reasonDesc = "Load Step:$stage failed: ClientRegistration is expired",
232+
source = "loadClientRegistration"
233+
)
234+
return null
232235
}
236+
} catch (e: Exception) {
237+
AwsTelemetry.modifyCredentials(
238+
credentialModification = CredentialModification.Unknown,
239+
result = Result.Failed,
240+
reason = "Failed to load ClientRegistration",
241+
reasonDesc = "Load Step:$stage failed: ClientRegistration file is invalid",
242+
source = "loadClientRegistration"
243+
)
244+
return null
233245
}
246+
}
234247

235248
private fun loadAccessToken(inputStream: InputStream) = tryOrNull {
236249
val accessToken = objectMapper.readValue<AccessToken>(inputStream)
@@ -294,6 +307,12 @@ class DiskCache(
294307
}
295308
}
296309

310+
private enum class LoadCredentialStage {
311+
ACCESS_FILE,
312+
VALIDATE_CREDENTIALS,
313+
CHECK_EXPIRATION,
314+
}
315+
297316
companion object {
298317
val EXPIRATION_THRESHOLD = Duration.ofMinutes(15)
299318
private val LOG = getLogger<DiskCache>()

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -432,16 +432,8 @@ class SsoAccessTokenProvider(
432432

433433
stageName = RefreshCredentialStage.VALIDATE_REGISTRATION
434434
if (registration == null) {
435-
val (reason, message) = when {
436-
currentToken.expiresAt.isBefore(Instant.now(clock)) -> Pair(
437-
"Reauth Required: $stageName",
438-
"Expired client registration"
439-
)
440-
else -> Pair(
441-
"Unable to load client registration from cache: $stageName",
442-
"Null client registration"
443-
)
444-
}
435+
val reason = "Unable to load client registration from cache: $stageName"
436+
val message ="Null client registration: invalid or expired"
445437
sendRefreshCredentialsMetric(
446438
currentToken,
447439
reason = reason,

0 commit comments

Comments
 (0)