Skip to content

Commit ef7639c

Browse files
leigaoljustinmk3
andauthored
fix(cwspr): add more log info for auth code path (#4526)
* add more log info for auth code path * more logs * reformat logs * log prefix * add log at event listener * Update packages/core/src/auth/secondaryAuth.ts Co-authored-by: Justin M. Keyes <[email protected]> * Update packages/core/src/auth/sso/ssoAccessTokenProvider.ts Co-authored-by: Justin M. Keyes <[email protected]> * add one more log --------- Co-authored-by: Justin M. Keyes <[email protected]>
1 parent 46e1837 commit ef7639c

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

packages/core/src/auth/auth.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export class Auth implements AuthService, ConnectionManager {
188188
if (profile === undefined) {
189189
throw new Error(`Connection does not exist: ${id}`)
190190
}
191-
191+
getLogger().info(`auth: validating connection before using it: ${id}`)
192192
const validated = await this.validateConnection(id, profile)
193193
const conn =
194194
validated.type === 'sso' ? this.getSsoConnection(id, validated) : this.getIamConnection(id, validated)
@@ -204,7 +204,7 @@ export class Auth implements AuthService, ConnectionManager {
204204
if (this.activeConnection === undefined) {
205205
return
206206
}
207-
207+
getLogger().info(`auth: logout`)
208208
await this.store.setCurrentProfileId(undefined)
209209
await this.invalidateConnection(this.activeConnection.id)
210210
this.#activeConnection = undefined
@@ -339,6 +339,7 @@ export class Auth implements AuthService, ConnectionManager {
339339
* Put the SSO connection in to an expired state
340340
*/
341341
public async expireConnection(conn: Pick<SsoConnection, 'id'>): Promise<void> {
342+
getLogger().info(`auth: Expiring connection ${conn.id}`)
342343
const profile = this.store.getProfileOrThrow(conn.id)
343344
if (profile.type === 'iam') {
344345
throw new ToolkitError('Auth: Cannot force expire an IAM connection')
@@ -374,6 +375,7 @@ export class Auth implements AuthService, ConnectionManager {
374375

375376
public async updateConnection(connection: Pick<SsoConnection, 'id'>, profile: SsoProfile): Promise<SsoConnection>
376377
public async updateConnection(connection: Pick<Connection, 'id'>, profile: Profile): Promise<Connection> {
378+
getLogger().info(`auth: Updating connection ${connection.id}`)
377379
if (profile.type === 'iam') {
378380
throw new Error('Updating IAM connections is not supported')
379381
}
@@ -454,6 +456,7 @@ export class Auth implements AuthService, ConnectionManager {
454456
* before the local token(s) are cleared as they are needed in the request.
455457
*/
456458
private async invalidateConnection(id: Connection['id'], opt?: { skipGlobalLogout?: boolean }) {
459+
getLogger().info(`auth: Invalidating connection: ${id}`)
457460
const profile = this.store.getProfileOrThrow(id)
458461

459462
if (profile.type === 'sso') {
@@ -479,6 +482,7 @@ export class Auth implements AuthService, ConnectionManager {
479482
}
480483

481484
private async updateConnectionState(id: Connection['id'], connectionState: ProfileMetadata['connectionState']) {
485+
getLogger().info(`auth: Updating connection state of ${id} to ${connectionState}`)
482486
const oldProfile = this.store.getProfileOrThrow(id)
483487
if (oldProfile.metadata.connectionState === connectionState) {
484488
return oldProfile
@@ -504,8 +508,10 @@ export class Auth implements AuthService, ConnectionManager {
504508
if (profile.type === 'sso') {
505509
const provider = this.getTokenProvider(id, profile)
506510
if ((await provider.getToken()) === undefined) {
511+
getLogger().info(`auth: Connection is not valid: ${id} `)
507512
return this.updateConnectionState(id, 'invalid')
508513
} else {
514+
getLogger().info(`auth: Connection is valid: ${id} `)
509515
return this.updateConnectionState(id, 'valid')
510516
}
511517
} else {
@@ -539,7 +545,7 @@ export class Auth implements AuthService, ConnectionManager {
539545

540546
private async handleValidationError(id: Connection['id'], err: unknown) {
541547
this.#validationErrors.set(id, UnknownError.cast(err))
542-
548+
getLogger().info(`auth: Handling validation error of connection: ${id}`)
543549
return this.updateConnectionState(id, 'invalid')
544550
}
545551

@@ -736,6 +742,7 @@ export class Auth implements AuthService, ConnectionManager {
736742
}
737743

738744
private async handleInvalidCredentials<T>(id: Connection['id'], refresh: () => Promise<T>): Promise<T> {
745+
getLogger().info(`auth: Handling invalid credentials of connection: ${id}`)
739746
const profile = this.store.getProfile(id)
740747
const previousState = profile?.metadata.connectionState
741748
await this.updateConnectionState(id, 'invalid')

packages/core/src/auth/secondaryAuth.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Auth } from './auth'
1212
import { once } from '../shared/utilities/functionUtils'
1313
import { isNonNullable } from '../shared/utilities/tsUtils'
1414
import { Connection, SsoConnection, StatefulConnection } from './connection'
15+
import { indent } from '../shared/utilities/textUtilities'
1516

1617
let currentConn: Auth['activeConnection']
1718
const auths = new Map<string, SecondaryAuth>()
@@ -156,6 +157,13 @@ export class SecondaryAuth<T extends Connection = Connection> {
156157
}
157158

158159
public get isConnectionExpired() {
160+
if (this.activeConnection) {
161+
getLogger().info(
162+
indent(`secondaryAuth connection id = ${this.activeConnection.id}
163+
secondaryAuth connection status = ${this.auth.getConnectionState(this.activeConnection)}`,
164+
4, true)
165+
)
166+
}
159167
return !!this.activeConnection && this.auth.getConnectionState(this.activeConnection) === 'invalid'
160168
}
161169

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import { getLogger } from '../../shared/logger'
2727
import { AwsRefreshCredentials, telemetry } from '../../shared/telemetry/telemetry'
2828
import { getIdeProperties, isCloud9 } from '../../shared/extensionUtilities'
29+
import { indent } from '../../shared/utilities/textUtilities'
2930

3031
const clientRegistrationType = 'public'
3132
const deviceGrantType = 'urn:ietf:params:oauth:grant-type:device_code'
@@ -76,6 +77,7 @@ export class SsoAccessTokenProvider {
7677

7778
public async invalidate(): Promise<void> {
7879
// Use allSettled() instead of all() to ensure all clear() calls are resolved.
80+
getLogger().info(`SsoAccessTokenProvider invalidate token and registration`)
7981
await Promise.allSettled([
8082
this.cache.token.clear(this.tokenCacheKey, 'SsoAccessTokenProvider.invalidate()'),
8183
this.cache.registration.clear(this.registrationCacheKey, 'SsoAccessTokenProvider.invalidate()'),
@@ -84,7 +86,11 @@ export class SsoAccessTokenProvider {
8486

8587
public async getToken(): Promise<SsoToken | undefined> {
8688
const data = await this.cache.token.load(this.tokenCacheKey)
87-
89+
getLogger().info(
90+
indent(`current client registration id=${data?.registration?.clientId},
91+
expires at ${data?.registration?.expiresAt},
92+
key = ${this.tokenCacheKey}`, 4, true)
93+
)
8894
if (!data || !isExpired(data.token)) {
8995
return data?.token
9096
}

packages/core/src/codewhisperer/service/featureConfigProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class FeatureConfigProvider {
5454
)
5555
})
5656
} catch (e) {
57-
getLogger().debug('CodeWhisperer: Error when fetching feature configs', e)
57+
getLogger().error(`CodeWhisperer: Error when fetching feature configs ${e}`, e)
5858
}
5959
getLogger().debug(`CodeWhisperer: Current feature configs: ${this.getFeatureConfigsTelemetry()}`)
6060
}

packages/core/src/codewhisperer/util/authUtil.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export class AuthUtil {
112112

113113
public constructor(public readonly auth = Auth.instance) {
114114
this.auth.onDidChangeConnectionState(async e => {
115+
getLogger().info(`codewhisperer: connection changed to ${e.state}: ${e.id}`)
115116
if (e.state !== 'authenticating') {
116117
await this.refreshCodeWhisperer()
117118
}
@@ -120,6 +121,7 @@ export class AuthUtil {
120121
})
121122

122123
this.secondaryAuth.onDidChangeActiveConnection(async () => {
124+
getLogger().info(`codewhisperer: active connection changed`)
123125
if (this.isValidEnterpriseSsoInUse()) {
124126
void vscode.commands.executeCommand('aws.codeWhisperer.notifyNewCustomizations')
125127
}
@@ -295,11 +297,11 @@ export class AuthUtil {
295297
this.secondaryAuth.isConnectionExpired &&
296298
this.conn !== undefined &&
297299
isValidCodeWhispererCoreConnection(this.conn)
298-
getLogger().debug(`codewhisperer: Connection expired = ${connectionExpired},
300+
getLogger().info(`codewhisperer: Connection expired = ${connectionExpired},
299301
secondaryAuth connection expired = ${this.secondaryAuth.isConnectionExpired},
300302
connection is undefined = ${this.conn === undefined}`)
301303
if (this.conn) {
302-
getLogger().debug(
304+
getLogger().info(
303305
`codewhisperer: isValidCodeWhispererConnection = ${isValidCodeWhispererCoreConnection(this.conn)}`
304306
)
305307
}

packages/core/src/shared/utilities/cacheUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export function createDiskCache<T, K>(
112112
log('read failed (file not found)', key)
113113
return
114114
}
115-
115+
log(`read failed ${error}`, key)
116116
throw ToolkitError.chain(error, `Failed to read from "${target}"`, {
117117
code: 'FSReadFailed',
118118
details: { key },

0 commit comments

Comments
 (0)