Skip to content

Commit 384d287

Browse files
telemetry(auth): report sessionDuration on reauth aws_loginWithBrowser (#6013)
Depends on aws/aws-toolkit-common#914 ## Problem On the condition of: - SSO session is BuilderID or Internal Amazon IdC - Subsequent login for same SSO session happened earlier than 90 days (the expected session expiration) We need to know on the client side to be able to report this information so that CloudWatch alarms can consume this. ## Solution By adding the existing sessionDuration field, which is `currentTime - whenThePreviousSessionWasCreated`, to `aws_loginWithBrowser` we will have all the information we need to alarm on. --- <!--- REMINDER: Ensure that your PR meets the guidelines in CONTRIBUTING.md --> License: I confirm that my contribution is made under the terms of the Apache 2.0 license. --------- Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 127a7ff commit 384d287

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"generateNonCodeFiles": "npm run generateNonCodeFiles -w packages/ --if-present"
4040
},
4141
"devDependencies": {
42-
"@aws-toolkits/telemetry": "^1.0.274",
42+
"@aws-toolkits/telemetry": "^1.0.282",
4343
"@playwright/browser-chromium": "^1.43.1",
4444
"@types/he": "^1.2.3",
4545
"@types/vscode": "^1.68.0",
@@ -71,7 +71,6 @@
7171
},
7272
"dependencies": {
7373
"@types/node": "^22.7.5",
74-
"@aws-toolkits/telemetry": "^1.0.242",
7574
"vscode-nls": "^5.2.0",
7675
"vscode-nls-dev": "^4.0.4"
7776
}

packages/core/src/auth/sso/ssoAccessTokenProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ export abstract class SsoAccessTokenProvider {
256256
awsRegion: this.profile.region,
257257
ssoRegistrationExpiresAt: args?.registrationExpiresAt,
258258
ssoRegistrationClientId: args?.registrationClientId,
259+
sessionDuration: getSessionDuration(this.tokenCacheKey),
259260
})
260261

261262
// Reset source in case there is a case where browser login was called but we forgot to set the source.
@@ -396,7 +397,7 @@ async function pollForTokenWithProgress<T extends { requestId?: string }>(
396397
*/
397398
function getSessionDuration(id: string) {
398399
const creationDate = globals.globalState.getSsoSessionCreationDate(id)
399-
return creationDate !== undefined ? Date.now() - creationDate : undefined
400+
return creationDate !== undefined ? globals.clock.Date.now() - creationDate : undefined
400401
}
401402

402403
/**

packages/core/src/test/credentials/sso/ssoAccessTokenProvider.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,50 @@ describe('SsoAccessTokenProvider', function () {
265265
assert.notDeepStrictEqual(await sut.getToken(), cachedToken)
266266
})
267267

268+
it(`emits session duration between logins of the same startUrl`, async function () {
269+
setupFlow()
270+
stubOpen()
271+
272+
await sut.createToken()
273+
clock.tick(5000)
274+
await sut.createToken()
275+
clock.tick(10_000)
276+
await sut.createToken()
277+
278+
// Mimic when we sign out then in again with the same region+startUrl. The ID is the only thing different.
279+
sut = SsoAccessTokenProvider.create(
280+
{ region, startUrl, identifier: 'bbb' },
281+
cache,
282+
oidcClient,
283+
reAuthState,
284+
() => true
285+
)
286+
await sut.createToken()
287+
288+
assertTelemetry('aws_loginWithBrowser', [
289+
{
290+
credentialStartUrl: startUrl,
291+
awsRegion: region,
292+
sessionDuration: undefined, // A new login.
293+
},
294+
{
295+
credentialStartUrl: startUrl,
296+
awsRegion: region,
297+
sessionDuration: 5000, // A reauth. 5000 - 0, is the diff between this and previous login
298+
},
299+
{
300+
credentialStartUrl: startUrl,
301+
awsRegion: region,
302+
sessionDuration: 10000, // A reauth. 15_000 - 5000 is the diff between this and previous login
303+
},
304+
{
305+
credentialStartUrl: startUrl,
306+
awsRegion: region,
307+
sessionDuration: undefined, // A new login, since we signed out of the last.
308+
},
309+
])
310+
})
311+
268312
it('respects the device authorization expiration time', async function () {
269313
// XXX: Don't know how to fix this "unhandled rejection" caused by this test:
270314
// rejected promise not handled within 1 second: Error: Timed-out waiting for browser login flow to complete

0 commit comments

Comments
 (0)