@@ -32,6 +32,8 @@ import software.aws.toolkits.core.utils.touch
32
32
import software.aws.toolkits.core.utils.tryDirOp
33
33
import software.aws.toolkits.core.utils.tryFileOp
34
34
import software.aws.toolkits.core.utils.tryOrNull
35
+ import software.aws.toolkits.telemetry.AuthTelemetry
36
+ import software.aws.toolkits.telemetry.Result
35
37
import java.io.InputStream
36
38
import java.io.OutputStream
37
39
import java.nio.file.Path
@@ -101,7 +103,18 @@ class DiskCache(
101
103
override fun loadClientRegistration (cacheKey : ClientRegistrationCacheKey ): ClientRegistration ? {
102
104
LOG .debug { " loadClientRegistration for $cacheKey " }
103
105
val inputStream = clientRegistrationCache(cacheKey).tryInputStreamIfExists()
104
- ? : return null
106
+ if (inputStream == null ) {
107
+ val stage = LoadCredentialStage .ACCESS_FILE
108
+ LOG .warn(" Failed to load Client Registration: cache file does not exist" )
109
+ AuthTelemetry .modifyConnection(
110
+ action = " Load cache file" ,
111
+ source = " loadClientRegistration" ,
112
+ result = Result .Failed ,
113
+ reason = " Failed to load Client Registration" ,
114
+ reasonDesc = " Load Step:$stage failed. Unable to load file"
115
+ )
116
+ return null
117
+ }
105
118
return loadClientRegistration(inputStream)
106
119
}
107
120
@@ -115,12 +128,34 @@ class DiskCache(
115
128
116
129
override fun invalidateClientRegistration (cacheKey : ClientRegistrationCacheKey ) {
117
130
LOG .debug { " invalidateClientRegistration for $cacheKey " }
118
- clientRegistrationCache(cacheKey).tryDeleteIfExists()
131
+ try {
132
+ clientRegistrationCache(cacheKey).tryDeleteIfExists()
133
+ } catch (e: Exception ) {
134
+ AuthTelemetry .modifyConnection(
135
+ action = " Delete cache file" ,
136
+ source = " invalidateClientRegistration" ,
137
+ result = Result .Failed ,
138
+ reason = " Failed to invalidate Client Registration" ,
139
+ reasonDesc = e.message ? : e::class .java.name
140
+ )
141
+ throw e
142
+ }
119
143
}
120
144
121
145
override fun invalidateAccessToken (ssoUrl : String ) {
122
146
LOG .debug { " invalidateAccessToken for $ssoUrl " }
123
- accessTokenCache(ssoUrl).tryDeleteIfExists()
147
+ try {
148
+ accessTokenCache(ssoUrl).tryDeleteIfExists()
149
+ } catch (e: Exception ) {
150
+ AuthTelemetry .modifyConnection(
151
+ action = " Delete cache file" ,
152
+ source = " invalidateAccessToken" ,
153
+ result = Result .Failed ,
154
+ reason = " Failed to invalidate Access Token" ,
155
+ reasonDesc = e.message ? : e::class .java.name
156
+ )
157
+ throw e
158
+ }
124
159
}
125
160
126
161
override fun loadAccessToken (cacheKey : AccessTokenCacheKey ): AccessToken ? {
@@ -143,7 +178,18 @@ class DiskCache(
143
178
144
179
override fun invalidateAccessToken (cacheKey : AccessTokenCacheKey ) {
145
180
LOG .debug { " invalidateAccessToken for $cacheKey " }
146
- accessTokenCache(cacheKey).tryDeleteIfExists()
181
+ try {
182
+ accessTokenCache(cacheKey).tryDeleteIfExists()
183
+ } catch (e: Exception ) {
184
+ AuthTelemetry .modifyConnection(
185
+ action = " Delete cache file" ,
186
+ source = " invalidateAccessToken" ,
187
+ result = Result .Failed ,
188
+ reason = " Failed to invalidate Access Token" ,
189
+ reasonDesc = e.message ? : e::class .java.name
190
+ )
191
+ throw e
192
+ }
147
193
}
148
194
149
195
private fun clientRegistrationCache (ssoRegion : String ): Path = cacheDir.resolve(" aws-toolkit-jetbrains-client-id-$ssoRegion .json" )
@@ -170,15 +216,36 @@ class DiskCache(
170
216
return cacheDir.resolve(fileName)
171
217
}
172
218
173
- private fun loadClientRegistration (inputStream : InputStream ) =
174
- tryOrNull {
219
+ private fun loadClientRegistration (inputStream : InputStream ): ClientRegistration ? {
220
+ var stage = LoadCredentialStage .VALIDATE_CREDENTIALS
221
+ try {
175
222
val clientRegistration = objectMapper.readValue<ClientRegistration >(inputStream)
223
+ stage = LoadCredentialStage .CHECK_EXPIRATION
176
224
if (clientRegistration.expiresAt.isNotExpired()) {
177
- clientRegistration
225
+ return clientRegistration
178
226
} else {
179
- null
227
+ LOG .warn(" Client Registration is expired" )
228
+ AuthTelemetry .modifyConnection(
229
+ action = " Validate Credentials" ,
230
+ source = " loadClientRegistration" ,
231
+ result = Result .Failed ,
232
+ reason = " Failed to load Client Registration" ,
233
+ reasonDesc = " Load Step:$stage failed: Client Registration is expired"
234
+ )
235
+ return null
180
236
}
237
+ } catch (e: Exception ) {
238
+ LOG .warn(" Client Registration could not be read" )
239
+ AuthTelemetry .modifyConnection(
240
+ action = " Validate Credentials" ,
241
+ source = " loadClientRegistration" ,
242
+ result = Result .Failed ,
243
+ reason = " Failed to load Client Registration" ,
244
+ reasonDesc = " Load Step:$stage failed: File could not be read"
245
+ )
246
+ return null
181
247
}
248
+ }
182
249
183
250
private fun loadAccessToken (inputStream : InputStream ) = tryOrNull {
184
251
val accessToken = objectMapper.readValue<AccessToken >(inputStream)
@@ -202,11 +269,22 @@ class DiskCache(
202
269
203
270
private fun writeKey (path : Path , consumer : (OutputStream ) -> Unit ) {
204
271
LOG .debug { " writing to $path " }
205
- path.tryDirOp(LOG ) { createParentDirectories() }
272
+ try {
273
+ path.tryDirOp(LOG ) { createParentDirectories() }
206
274
207
- path.tryFileOp(LOG ) {
208
- touch(restrictToOwner = true )
209
- outputStream().use(consumer)
275
+ path.tryFileOp(LOG ) {
276
+ touch(restrictToOwner = true )
277
+ outputStream().use(consumer)
278
+ }
279
+ } catch (e: Exception ) {
280
+ AuthTelemetry .modifyConnection(
281
+ action = " Write file" ,
282
+ source = " writeKey" ,
283
+ result = Result .Failed ,
284
+ reason = " Failed to write to cache" ,
285
+ reasonDesc = e.message ? : e::class .java.name
286
+ )
287
+ throw e
210
288
}
211
289
}
212
290
@@ -230,6 +308,12 @@ class DiskCache(
230
308
}
231
309
}
232
310
311
+ private enum class LoadCredentialStage {
312
+ ACCESS_FILE ,
313
+ VALIDATE_CREDENTIALS ,
314
+ CHECK_EXPIRATION ,
315
+ }
316
+
233
317
companion object {
234
318
val EXPIRATION_THRESHOLD = Duration .ofMinutes(15 )
235
319
private val LOG = getLogger<DiskCache >()
0 commit comments