@@ -7,6 +7,7 @@ import com.intellij.openapi.Disposable
7
7
import com.intellij.openapi.components.service
8
8
import com.intellij.openapi.extensions.ExtensionPointName
9
9
import com.intellij.openapi.project.Project
10
+ import migration.software.aws.toolkits.jetbrains.services.telemetry.TelemetryService
10
11
import software.amazon.awssdk.services.ssooidc.model.SsoOidcException
11
12
import software.aws.toolkits.core.ClientConnectionSettings
12
13
import software.aws.toolkits.core.ConnectionSettings
@@ -25,11 +26,10 @@ import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.BearerTokenPr
25
26
import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.InteractiveBearerTokenProvider
26
27
import software.aws.toolkits.jetbrains.utils.runUnderProgressIfNeeded
27
28
import software.aws.toolkits.resources.message
28
- import software.aws.toolkits.telemetry.AuthTelemetry
29
- import software.aws.toolkits.telemetry.AwsTelemetry
30
29
import software.aws.toolkits.telemetry.CredentialSourceId
31
30
import software.aws.toolkits.telemetry.CredentialType
32
31
import software.aws.toolkits.telemetry.Result
32
+ import java.time.Instant
33
33
34
34
sealed interface ToolkitConnection {
35
35
val id: String
@@ -118,12 +118,18 @@ fun loginSso(
118
118
onPendingToken : (InteractiveBearerTokenProvider ) -> Unit = {},
119
119
onError : (Exception ) -> Unit = {},
120
120
onSuccess : () -> Unit = {},
121
+ metadata : ConnectionMetadata ? = null
121
122
): AwsBearerTokenConnection ? {
122
123
fun createAndAuthNewConnection (profile : AuthProfile ): AwsBearerTokenConnection ? {
123
124
val authManager = ToolkitAuthManager .getInstance()
124
125
val connection = try {
125
126
authManager.tryCreateTransientSsoConnection(profile) { transientConnection ->
126
- reauthConnectionIfNeeded(project, transientConnection, onPendingToken)
127
+ reauthConnectionIfNeeded(
128
+ project = project,
129
+ connection = transientConnection,
130
+ onPendingToken = onPendingToken,
131
+ metadata = metadata
132
+ )
127
133
}
128
134
} catch (e: Exception ) {
129
135
onError(e)
@@ -170,7 +176,11 @@ fun loginSso(
170
176
}
171
177
172
178
// For the case when the existing connection is in invalid state, we need to re-auth
173
- reauthConnectionIfNeeded(project, connection)
179
+ reauthConnectionIfNeeded(
180
+ project = project,
181
+ connection = connection,
182
+ metadata = metadata
183
+ )
174
184
return connection
175
185
} ? : run {
176
186
// No existing connection, start from scratch
@@ -221,58 +231,61 @@ fun AwsBearerTokenConnection.lazyIsUnauthedBearerConnection(): Boolean {
221
231
fun reauthConnectionIfNeeded (
222
232
project : Project ? ,
223
233
connection : ToolkitConnection ,
224
- onPendingToken : (InteractiveBearerTokenProvider ) -> Unit = {}
234
+ onPendingToken : (InteractiveBearerTokenProvider ) -> Unit = {},
235
+ metadata : ConnectionMetadata ? = null
225
236
): BearerTokenProvider {
226
237
val tokenProvider = (connection.getConnectionSettings() as TokenConnectionSettings ).tokenProvider.delegate as BearerTokenProvider
227
238
if (tokenProvider is InteractiveBearerTokenProvider ) {
228
239
onPendingToken(tokenProvider)
229
240
}
230
- return reauthProviderIfNeeded(project, tokenProvider, connection)
241
+ return reauthProviderIfNeeded(
242
+ project = project,
243
+ tokenProvider = tokenProvider,
244
+ connection = connection,
245
+ metadata = metadata ? : ConnectionMetadata (
246
+ sourceId = CredentialSourceId .AwsId .toString()
247
+ )
248
+ )
231
249
}
232
250
233
251
private fun reauthProviderIfNeeded (
234
252
project : Project ? ,
235
253
tokenProvider : BearerTokenProvider ,
236
- connection : ToolkitConnection
254
+ connection : ToolkitConnection ,
255
+ metadata : ConnectionMetadata
237
256
): BearerTokenProvider {
238
257
maybeReauthProviderIfNeeded(project, tokenProvider) {
239
258
runUnderProgressIfNeeded(project, message(" credentials.pending.title" ), true ) {
240
259
try {
241
260
tokenProvider.reauthenticate()
242
261
243
262
if (connection is AwsBearerTokenConnection ) {
244
- AwsTelemetry .loginWithBrowser(
245
- project = null ,
246
- result = Result .Succeeded ,
247
- isReAuth = true ,
248
- credentialType = CredentialType .BearerToken ,
263
+ recordLoginWithBrowser(
249
264
credentialStartUrl = connection.startUrl,
250
- credentialSourceId = CredentialSourceId .AwsId
265
+ credentialSourceId = metadata.sourceId,
266
+ isReAuth = true ,
267
+ result = Result .Succeeded
251
268
)
252
269
}
253
- AuthTelemetry .addConnection(
254
- project = null ,
255
- result = Result .Succeeded ,
270
+ recordAddConnection(
271
+ credentialSourceId = metadata.sourceId,
256
272
isReAuth = true ,
257
- credentialSourceId = CredentialSourceId . AwsId
273
+ result = Result . Failed
258
274
)
259
275
} catch (e: Exception ) {
260
276
if (connection is AwsBearerTokenConnection ) {
261
- AwsTelemetry .loginWithBrowser(
262
- project = null ,
263
- result = Result .Failed ,
264
- isReAuth = true ,
265
- reason = e.message,
266
- credentialType = CredentialType .BearerToken ,
277
+ recordLoginWithBrowser(
267
278
credentialStartUrl = connection.startUrl,
268
- credentialSourceId = CredentialSourceId .AwsId
279
+ credentialSourceId = metadata.sourceId,
280
+ isReAuth = true ,
281
+ result = Result .Failed ,
282
+ reason = e.message
269
283
)
270
284
}
271
- AuthTelemetry .addConnection(
272
- project = null ,
273
- result = Result .Succeeded ,
285
+ recordAddConnection(
286
+ credentialSourceId = metadata.sourceId,
274
287
isReAuth = true ,
275
- credentialSourceId = CredentialSourceId . AwsId ,
288
+ result = Result . Failed ,
276
289
reason = e.message
277
290
)
278
291
@@ -330,3 +343,50 @@ private fun getSsoSessionProfileNameFromCredentials(connection: CredentialIdenti
330
343
connection as ProfileCredentialsIdentifierSso
331
344
return connection.ssoSessionName
332
345
}
346
+
347
+ private fun recordLoginWithBrowser (
348
+ credentialStartUrl : String? = null,
349
+ credentialSourceId : String? = null,
350
+ reason : String? = null,
351
+ isReAuth : Boolean ,
352
+ result : Result
353
+ ) {
354
+ TelemetryService .getInstance().record(null as Project ? ) {
355
+ datum(" aws_loginWithBrowser" ) {
356
+ createTime(Instant .now())
357
+ unit(software.amazon.awssdk.services.toolkittelemetry.model.Unit .NONE )
358
+ value(1.0 )
359
+ passive(false )
360
+ credentialSourceId?.let { metadata(" credentialSourceId" , it) }
361
+ credentialStartUrl?.let { metadata(" credentialStartUrl" , it) }
362
+ metadata(" credentialType" , CredentialType .BearerToken .toString())
363
+ metadata(" isReAuth" , isReAuth.toString())
364
+ reason?.let { metadata(" reason" , it) }
365
+ metadata(" result" , result.toString())
366
+ }
367
+ }
368
+ }
369
+
370
+ private fun recordAddConnection (
371
+ credentialSourceId : String? = null,
372
+ reason : String? = null,
373
+ isReAuth : Boolean ,
374
+ result : Result
375
+ ) {
376
+ TelemetryService .getInstance().record(null as Project ? ) {
377
+ datum(" auth_addConnection" ) {
378
+ createTime(Instant .now())
379
+ unit(software.amazon.awssdk.services.toolkittelemetry.model.Unit .NONE )
380
+ value(1.0 )
381
+ passive(false )
382
+ credentialSourceId?.let { metadata(" credentialSourceId" , it) }
383
+ metadata(" isReAuth" , isReAuth.toString())
384
+ reason?.let { metadata(" reason" , it) }
385
+ metadata(" result" , result.toString())
386
+ }
387
+ }
388
+ }
389
+
390
+ data class ConnectionMetadata (
391
+ val sourceId : String? = null
392
+ )
0 commit comments