Skip to content

Commit 23d94d3

Browse files
authored
Support for additional metadata with loginSso calls (#4659)
1 parent af12f75 commit 23d94d3

File tree

4 files changed

+202
-78
lines changed

4 files changed

+202
-78
lines changed

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

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.intellij.openapi.Disposable
77
import com.intellij.openapi.components.service
88
import com.intellij.openapi.extensions.ExtensionPointName
99
import com.intellij.openapi.project.Project
10+
import migration.software.aws.toolkits.jetbrains.services.telemetry.TelemetryService
1011
import software.amazon.awssdk.services.ssooidc.model.SsoOidcException
1112
import software.aws.toolkits.core.ClientConnectionSettings
1213
import software.aws.toolkits.core.ConnectionSettings
@@ -25,11 +26,10 @@ import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.BearerTokenPr
2526
import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.InteractiveBearerTokenProvider
2627
import software.aws.toolkits.jetbrains.utils.runUnderProgressIfNeeded
2728
import software.aws.toolkits.resources.message
28-
import software.aws.toolkits.telemetry.AuthTelemetry
29-
import software.aws.toolkits.telemetry.AwsTelemetry
3029
import software.aws.toolkits.telemetry.CredentialSourceId
3130
import software.aws.toolkits.telemetry.CredentialType
3231
import software.aws.toolkits.telemetry.Result
32+
import java.time.Instant
3333

3434
sealed interface ToolkitConnection {
3535
val id: String
@@ -118,12 +118,18 @@ fun loginSso(
118118
onPendingToken: (InteractiveBearerTokenProvider) -> Unit = {},
119119
onError: (Exception) -> Unit = {},
120120
onSuccess: () -> Unit = {},
121+
metadata: ConnectionMetadata? = null
121122
): AwsBearerTokenConnection? {
122123
fun createAndAuthNewConnection(profile: AuthProfile): AwsBearerTokenConnection? {
123124
val authManager = ToolkitAuthManager.getInstance()
124125
val connection = try {
125126
authManager.tryCreateTransientSsoConnection(profile) { transientConnection ->
126-
reauthConnectionIfNeeded(project, transientConnection, onPendingToken)
127+
reauthConnectionIfNeeded(
128+
project = project,
129+
connection = transientConnection,
130+
onPendingToken = onPendingToken,
131+
metadata = metadata
132+
)
127133
}
128134
} catch (e: Exception) {
129135
onError(e)
@@ -170,7 +176,11 @@ fun loginSso(
170176
}
171177

172178
// 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+
)
174184
return connection
175185
} ?: run {
176186
// No existing connection, start from scratch
@@ -221,58 +231,61 @@ fun AwsBearerTokenConnection.lazyIsUnauthedBearerConnection(): Boolean {
221231
fun reauthConnectionIfNeeded(
222232
project: Project?,
223233
connection: ToolkitConnection,
224-
onPendingToken: (InteractiveBearerTokenProvider) -> Unit = {}
234+
onPendingToken: (InteractiveBearerTokenProvider) -> Unit = {},
235+
metadata: ConnectionMetadata? = null
225236
): BearerTokenProvider {
226237
val tokenProvider = (connection.getConnectionSettings() as TokenConnectionSettings).tokenProvider.delegate as BearerTokenProvider
227238
if (tokenProvider is InteractiveBearerTokenProvider) {
228239
onPendingToken(tokenProvider)
229240
}
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+
)
231249
}
232250

233251
private fun reauthProviderIfNeeded(
234252
project: Project?,
235253
tokenProvider: BearerTokenProvider,
236-
connection: ToolkitConnection
254+
connection: ToolkitConnection,
255+
metadata: ConnectionMetadata
237256
): BearerTokenProvider {
238257
maybeReauthProviderIfNeeded(project, tokenProvider) {
239258
runUnderProgressIfNeeded(project, message("credentials.pending.title"), true) {
240259
try {
241260
tokenProvider.reauthenticate()
242261

243262
if (connection is AwsBearerTokenConnection) {
244-
AwsTelemetry.loginWithBrowser(
245-
project = null,
246-
result = Result.Succeeded,
247-
isReAuth = true,
248-
credentialType = CredentialType.BearerToken,
263+
recordLoginWithBrowser(
249264
credentialStartUrl = connection.startUrl,
250-
credentialSourceId = CredentialSourceId.AwsId
265+
credentialSourceId = metadata.sourceId,
266+
isReAuth = true,
267+
result = Result.Succeeded
251268
)
252269
}
253-
AuthTelemetry.addConnection(
254-
project = null,
255-
result = Result.Succeeded,
270+
recordAddConnection(
271+
credentialSourceId = metadata.sourceId,
256272
isReAuth = true,
257-
credentialSourceId = CredentialSourceId.AwsId
273+
result = Result.Failed
258274
)
259275
} catch (e: Exception) {
260276
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(
267278
credentialStartUrl = connection.startUrl,
268-
credentialSourceId = CredentialSourceId.AwsId
279+
credentialSourceId = metadata.sourceId,
280+
isReAuth = true,
281+
result = Result.Failed,
282+
reason = e.message
269283
)
270284
}
271-
AuthTelemetry.addConnection(
272-
project = null,
273-
result = Result.Succeeded,
285+
recordAddConnection(
286+
credentialSourceId = metadata.sourceId,
274287
isReAuth = true,
275-
credentialSourceId = CredentialSourceId.AwsId,
288+
result = Result.Failed,
276289
reason = e.message
277290
)
278291

@@ -330,3 +343,50 @@ private fun getSsoSessionProfileNameFromCredentials(connection: CredentialIdenti
330343
connection as ProfileCredentialsIdentifierSso
331344
return connection.ssoSessionName
332345
}
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

Comments
 (0)