From c6223067a272ae1aedbc22cf9e2b081939263b62 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 10:00:36 -0500 Subject: [PATCH 01/44] isolate test --- .../core/src/test/credentials/auth.test.ts | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 022e2c5c6e7..921b76f408c 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -505,34 +505,35 @@ describe('Auth', function () { sinon.restore() await fs.delete(tmpDir, { recursive: true }) }) + for (const _ of Array.from({ length: 1000 }, (i) => i)) { + it('does not cache if the credentials file changes', async function () { + const initialCreds = { + profileName: 'default', + accessKey: 'x', + secretKey: 'x', + } + + await UserCredentialsUtils.generateCredentialsFile(initialCreds) + + const conn = await auth.getConnection({ id: 'profile:default' }) + assert.ok(conn?.type === 'iam', 'Expected an IAM connection') + assert.deepStrictEqual(await conn.getCredentials(), { + accessKeyId: initialCreds.accessKey, + secretAccessKey: initialCreds.secretKey, + sessionToken: undefined, + }) - it('does not cache if the credentials file changes', async function () { - const initialCreds = { - profileName: 'default', - accessKey: 'x', - secretKey: 'x', - } - - await UserCredentialsUtils.generateCredentialsFile(initialCreds) - - const conn = await auth.getConnection({ id: 'profile:default' }) - assert.ok(conn?.type === 'iam', 'Expected an IAM connection') - assert.deepStrictEqual(await conn.getCredentials(), { - accessKeyId: initialCreds.accessKey, - secretAccessKey: initialCreds.secretKey, - sessionToken: undefined, - }) - - await fs.delete(getCredentialsFilename()) + await fs.delete(getCredentialsFilename()) - const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } - await UserCredentialsUtils.generateCredentialsFile(newCreds) - assert.deepStrictEqual(await conn.getCredentials(), { - accessKeyId: newCreds.accessKey, - secretAccessKey: newCreds.secretKey, - sessionToken: undefined, + const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } + await UserCredentialsUtils.generateCredentialsFile(newCreds) + assert.deepStrictEqual(await conn.getCredentials(), { + accessKeyId: newCreds.accessKey, + secretAccessKey: newCreds.secretKey, + sessionToken: undefined, + }) }) - }) + } }) describe('AuthNode', function () { From fc8a1c66600cd93bdce3a9ff4ee8bad313be205d Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 10:01:21 -0500 Subject: [PATCH 02/44] add exception for eslint --- packages/core/src/test/credentials/auth.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 921b76f408c..0e972d3e058 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -490,8 +490,8 @@ describe('Auth', function () { }) }) }) - - describe('Shared ini files', function () { + // eslint-disable-next-line aws-toolkits/no-only-in-tests + describe.only('Shared ini files', function () { let tmpDir: string beforeEach(async function () { From 0f92bfe6778f7c471d4343be222b6117f162d84e Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 10:19:06 -0500 Subject: [PATCH 03/44] implement waitUntil on windows --- .../core/src/shared/credentials/userCredentialsUtils.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/core/src/shared/credentials/userCredentialsUtils.ts b/packages/core/src/shared/credentials/userCredentialsUtils.ts index 03c1316a3e4..fa5257c08a0 100644 --- a/packages/core/src/shared/credentials/userCredentialsUtils.ts +++ b/packages/core/src/shared/credentials/userCredentialsUtils.ts @@ -9,6 +9,8 @@ import { fileExists } from '../filesystemUtilities' import { isNonNullable } from '../utilities/tsUtils' import { getConfigFilename, getCredentialsFilename } from '../../auth/credentials/sharedCredentialsFile' import { fs } from '../../shared/fs/fs' +import { waitUntil } from '../utilities/timeoutUtils' +import { isWin } from '../vscode/env' const header = ` # AWS credentials file used by AWS CLI, SDKs, and tools. @@ -98,5 +100,9 @@ export class UserCredentialsUtils { } await fs.writeFile(dest, contents.join('\n')) + // Windows Race condition with writing files. + if (isWin()) { + await waitUntil(() => fs.exists(dest), { timeout: 5000, interval: 100 }) + } } } From c29a589ef3c4f3a9de4a1f1ab6031e72a41976fc Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 10:47:04 -0500 Subject: [PATCH 04/44] bake timeout into the fs module --- .../src/shared/credentials/userCredentialsUtils.ts | 6 ------ packages/core/src/shared/fs/fs.ts | 10 +++++++++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/core/src/shared/credentials/userCredentialsUtils.ts b/packages/core/src/shared/credentials/userCredentialsUtils.ts index fa5257c08a0..03c1316a3e4 100644 --- a/packages/core/src/shared/credentials/userCredentialsUtils.ts +++ b/packages/core/src/shared/credentials/userCredentialsUtils.ts @@ -9,8 +9,6 @@ import { fileExists } from '../filesystemUtilities' import { isNonNullable } from '../utilities/tsUtils' import { getConfigFilename, getCredentialsFilename } from '../../auth/credentials/sharedCredentialsFile' import { fs } from '../../shared/fs/fs' -import { waitUntil } from '../utilities/timeoutUtils' -import { isWin } from '../vscode/env' const header = ` # AWS credentials file used by AWS CLI, SDKs, and tools. @@ -100,9 +98,5 @@ export class UserCredentialsUtils { } await fs.writeFile(dest, contents.join('\n')) - // Windows Race condition with writing files. - if (isWin()) { - await waitUntil(() => fs.exists(dest), { timeout: 5000, interval: 100 }) - } } } diff --git a/packages/core/src/shared/fs/fs.ts b/packages/core/src/shared/fs/fs.ts index 477b16c12ea..b79a922a56c 100644 --- a/packages/core/src/shared/fs/fs.ts +++ b/packages/core/src/shared/fs/fs.ts @@ -304,6 +304,9 @@ export class FileSystem { } await write(uri) + if (isWin()) { + await waitUntil(async () => await fs.exists(uri), { timeout: 5000, truthy: true }) + } } /** @@ -428,7 +431,7 @@ export class FileSystem { }) } - return vfs.delete(uri, opt).then(undefined, async (err) => { + vfs.delete(uri, opt).then(undefined, async (err) => { const notFound = isFileNotFoundError(err) if (notFound && opt.force) { @@ -461,6 +464,11 @@ export class FileSystem { throw err }) + + // Windows race condition + if (isWin()) { + await waitUntil(async () => !(await fs.exists(fileOrDir)), { timeout: 5000, truthy: true }) + } } async readdir(uri: vscode.Uri | string): Promise<[string, vscode.FileType][]> { From b2a4734418db3b1cf0cd6a99b986f229656fa837 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 11:02:11 -0500 Subject: [PATCH 05/44] add errors if waitUntil fails --- packages/core/src/shared/fs/fs.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/core/src/shared/fs/fs.ts b/packages/core/src/shared/fs/fs.ts index b79a922a56c..f8412d3c667 100644 --- a/packages/core/src/shared/fs/fs.ts +++ b/packages/core/src/shared/fs/fs.ts @@ -305,7 +305,10 @@ export class FileSystem { await write(uri) if (isWin()) { - await waitUntil(async () => await fs.exists(uri), { timeout: 5000, truthy: true }) + const success = await waitUntil(async () => await fs.exists(uri), { timeout: 5000, truthy: true }) + if (!success) { + throw new ToolkitError(`Failed to write file ${uri.toString()}`) + } } } @@ -467,7 +470,10 @@ export class FileSystem { // Windows race condition if (isWin()) { - await waitUntil(async () => !(await fs.exists(fileOrDir)), { timeout: 5000, truthy: true }) + const success = await waitUntil(async () => !(await fs.exists(fileOrDir)), { timeout: 5000, truthy: true }) + if (!success) { + throw new ToolkitError(`Failed to delete file ${uri.toString()}`) + } } } From f4e2736f500cb0a0c74367abb93b9dc367bf91e3 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 11:11:11 -0500 Subject: [PATCH 06/44] undo fs changes --- packages/core/src/shared/fs/fs.ts | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/packages/core/src/shared/fs/fs.ts b/packages/core/src/shared/fs/fs.ts index f8412d3c667..477b16c12ea 100644 --- a/packages/core/src/shared/fs/fs.ts +++ b/packages/core/src/shared/fs/fs.ts @@ -304,12 +304,6 @@ export class FileSystem { } await write(uri) - if (isWin()) { - const success = await waitUntil(async () => await fs.exists(uri), { timeout: 5000, truthy: true }) - if (!success) { - throw new ToolkitError(`Failed to write file ${uri.toString()}`) - } - } } /** @@ -434,7 +428,7 @@ export class FileSystem { }) } - vfs.delete(uri, opt).then(undefined, async (err) => { + return vfs.delete(uri, opt).then(undefined, async (err) => { const notFound = isFileNotFoundError(err) if (notFound && opt.force) { @@ -467,14 +461,6 @@ export class FileSystem { throw err }) - - // Windows race condition - if (isWin()) { - const success = await waitUntil(async () => !(await fs.exists(fileOrDir)), { timeout: 5000, truthy: true }) - if (!success) { - throw new ToolkitError(`Failed to delete file ${uri.toString()}`) - } - } } async readdir(uri: vscode.Uri | string): Promise<[string, vscode.FileType][]> { From 1860d22f959e4c78db8688827ec967702cd3efb0 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 11:17:39 -0500 Subject: [PATCH 07/44] add console.log statements --- packages/core/src/test/credentials/auth.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 0e972d3e058..ae670c3dcd5 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -524,9 +524,10 @@ describe('Auth', function () { }) await fs.delete(getCredentialsFilename()) - + console.log('file exists after delete=%O', await fs.exists(getCredentialsFilename())) const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } await UserCredentialsUtils.generateCredentialsFile(newCreds) + console.log('file exists after generating=%O', await fs.exists(getCredentialsFilename())) assert.deepStrictEqual(await conn.getCredentials(), { accessKeyId: newCreds.accessKey, secretAccessKey: newCreds.secretKey, From 176ae5f8ff429dc85b371fd2881bb859de39b1c9 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 11:51:41 -0500 Subject: [PATCH 08/44] track code path --- packages/core/src/shared/credentials/userCredentialsUtils.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/core/src/shared/credentials/userCredentialsUtils.ts b/packages/core/src/shared/credentials/userCredentialsUtils.ts index 03c1316a3e4..d3784baa6c6 100644 --- a/packages/core/src/shared/credentials/userCredentialsUtils.ts +++ b/packages/core/src/shared/credentials/userCredentialsUtils.ts @@ -92,8 +92,12 @@ export class UserCredentialsUtils { const contents = credentialsContext ? ['', createNewCredentialsFile(credentialsContext)] : [] if (await fs.exists(dest)) { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('hit case where dest exists') contents.unshift(await fs.readFileText(dest)) } else { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('hit case where dest exists') contents.unshift(header) } From 2be7f2f1492bbd6af71277433e976063c1dc3b00 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 12:10:01 -0500 Subject: [PATCH 09/44] add more logging --- packages/core/src/shared/credentials/userCredentialsUtils.ts | 3 ++- packages/core/src/test/credentials/auth.test.ts | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core/src/shared/credentials/userCredentialsUtils.ts b/packages/core/src/shared/credentials/userCredentialsUtils.ts index d3784baa6c6..84ac3951ef8 100644 --- a/packages/core/src/shared/credentials/userCredentialsUtils.ts +++ b/packages/core/src/shared/credentials/userCredentialsUtils.ts @@ -100,7 +100,8 @@ export class UserCredentialsUtils { console.log('hit case where dest exists') contents.unshift(header) } - + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('contents=%O', contents) await fs.writeFile(dest, contents.join('\n')) } } diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index ae670c3dcd5..18594b966ff 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -528,6 +528,7 @@ describe('Auth', function () { const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } await UserCredentialsUtils.generateCredentialsFile(newCreds) console.log('file exists after generating=%O', await fs.exists(getCredentialsFilename())) + console.log('file contents = %O', await fs.readFileText(getCredentialsFilename())) assert.deepStrictEqual(await conn.getCredentials(), { accessKeyId: newCreds.accessKey, secretAccessKey: newCreds.secretKey, From f2c17b8e78352e164713dc2b0dff778dcdb88a08 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 12:34:13 -0500 Subject: [PATCH 10/44] check if cache is being returned --- packages/core/src/auth/auth.ts | 2 ++ .../core/src/shared/credentials/userCredentialsUtils.ts | 6 ------ packages/core/src/test/credentials/auth.test.ts | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/core/src/auth/auth.ts b/packages/core/src/auth/auth.ts index d7c2a5d58b7..a973ea96277 100644 --- a/packages/core/src/auth/auth.ts +++ b/packages/core/src/auth/auth.ts @@ -903,6 +903,8 @@ export class Auth implements AuthService, ConnectionManager { private async _getCredentials(id: Connection['id'], provider: CredentialsProvider): Promise { const credentials = await this.getCachedCredentials(provider) if (credentials !== undefined) { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('returning cached credentials') return credentials } else if ((await provider.canAutoConnect()) === true) { return this.createCachedCredentials(provider) diff --git a/packages/core/src/shared/credentials/userCredentialsUtils.ts b/packages/core/src/shared/credentials/userCredentialsUtils.ts index 84ac3951ef8..3d6e0948568 100644 --- a/packages/core/src/shared/credentials/userCredentialsUtils.ts +++ b/packages/core/src/shared/credentials/userCredentialsUtils.ts @@ -92,16 +92,10 @@ export class UserCredentialsUtils { const contents = credentialsContext ? ['', createNewCredentialsFile(credentialsContext)] : [] if (await fs.exists(dest)) { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('hit case where dest exists') contents.unshift(await fs.readFileText(dest)) } else { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('hit case where dest exists') contents.unshift(header) } - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('contents=%O', contents) await fs.writeFile(dest, contents.join('\n')) } } diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 18594b966ff..60bb74acc16 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -528,7 +528,7 @@ describe('Auth', function () { const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } await UserCredentialsUtils.generateCredentialsFile(newCreds) console.log('file exists after generating=%O', await fs.exists(getCredentialsFilename())) - console.log('file contents = %O', await fs.readFileText(getCredentialsFilename())) + assert.deepStrictEqual(await conn.getCredentials(), { accessKeyId: newCreds.accessKey, secretAccessKey: newCreds.secretKey, From 75b77f56e1f673ece6d7b4f333bec354dff3be06 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 12:57:23 -0500 Subject: [PATCH 11/44] add more logging --- packages/core/src/auth/credentials/store.ts | 4 ++++ packages/core/src/auth/secondaryAuth.ts | 2 ++ 2 files changed, 6 insertions(+) diff --git a/packages/core/src/auth/credentials/store.ts b/packages/core/src/auth/credentials/store.ts index 2fd2d29b18b..1e442c27ada 100644 --- a/packages/core/src/auth/credentials/store.ts +++ b/packages/core/src/auth/credentials/store.ts @@ -31,6 +31,8 @@ export class CredentialsStore { */ public isValid(key: string): boolean { if (this.credentialsCache[key]) { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('credentialsCache=%O', this.credentialsCache[key]) const expiration = this.credentialsCache[key].credentials.expiration return expiration !== undefined ? expiration >= new globals.clock.Date() : true } @@ -42,6 +44,8 @@ export class CredentialsStore { * Returns undefined if the specified credentials are expired or not found. */ public async getCredentials(credentials: CredentialsId): Promise { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('credentials.isValid=%O', this.isValid(asString(credentials))) if (this.isValid(asString(credentials))) { return this.credentialsCache[asString(credentials)] } else { diff --git a/packages/core/src/auth/secondaryAuth.ts b/packages/core/src/auth/secondaryAuth.ts index add91765833..5b8eb2b9a58 100644 --- a/packages/core/src/auth/secondaryAuth.ts +++ b/packages/core/src/auth/secondaryAuth.ts @@ -148,6 +148,8 @@ export class SecondaryAuth { }) const refreshConn = (event: string) => { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('refreshing connection with event=%s', event) getLogger().debug(`secondaryAuth: detected ${event} event in sso cache, refreshing auth.`) globals.clock.setTimeout( telemetry.function_call.run( From 5e316a1e242baa54a272c0474cefb6248d81fa58 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 12:57:45 -0500 Subject: [PATCH 12/44] reduce repeat --- packages/core/src/test/credentials/auth.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 60bb74acc16..8230e22f8b3 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -505,7 +505,7 @@ describe('Auth', function () { sinon.restore() await fs.delete(tmpDir, { recursive: true }) }) - for (const _ of Array.from({ length: 1000 }, (i) => i)) { + for (const _ of Array.from({ length: 50 }, (i) => i)) { it('does not cache if the credentials file changes', async function () { const initialCreds = { profileName: 'default', From ce858935f5b59413a8f3e67fa310d425e276fd7f Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 12:59:03 -0500 Subject: [PATCH 13/44] add one more logging statement --- packages/core/src/auth/secondaryAuth.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/core/src/auth/secondaryAuth.ts b/packages/core/src/auth/secondaryAuth.ts index 5b8eb2b9a58..6badfb2eceb 100644 --- a/packages/core/src/auth/secondaryAuth.ts +++ b/packages/core/src/auth/secondaryAuth.ts @@ -160,8 +160,12 @@ export class SecondaryAuth { getLogger().debug( `secondaryAuth: cache event did not update global state, no refresh is needed.` ) + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('secondaryAuth: cache event did not update global state, no refresh is needed.') return } + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('secondaryAuth: restorePreviousSession/restoreConnecton') await this.auth.restorePreviousSession() await this.restoreConnection(true) }, From f805cb8382b65c5621a526372c7c22642792bae0 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 13:14:30 -0500 Subject: [PATCH 14/44] rebump to 1000 --- packages/core/src/test/credentials/auth.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 8230e22f8b3..60bb74acc16 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -505,7 +505,7 @@ describe('Auth', function () { sinon.restore() await fs.delete(tmpDir, { recursive: true }) }) - for (const _ of Array.from({ length: 50 }, (i) => i)) { + for (const _ of Array.from({ length: 1000 }, (i) => i)) { it('does not cache if the credentials file changes', async function () { const initialCreds = { profileName: 'default', From 2bddbfe50db04ab9880655699ec9a1bec665540b Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 15:15:28 -0500 Subject: [PATCH 15/44] change logging statements --- packages/core/src/auth/auth.ts | 4 ++++ packages/core/src/auth/credentials/store.ts | 2 -- packages/core/src/auth/secondaryAuth.ts | 6 ------ 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/core/src/auth/auth.ts b/packages/core/src/auth/auth.ts index a973ea96277..0084dea7c7f 100644 --- a/packages/core/src/auth/auth.ts +++ b/packages/core/src/auth/auth.ts @@ -857,6 +857,8 @@ export class Auth implements AuthService, ConnectionManager { } private async getCachedCredentials(provider: CredentialsProvider) { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('getCachedCredentials called') const creds = await globals.loginManager.store.getCredentials(provider.getCredentialsId()) if (creds !== undefined && creds.credentialsHashCode === provider.getHashCode()) { return creds.credentials @@ -901,6 +903,8 @@ export class Auth implements AuthService, ConnectionManager { private readonly getCredentials = keyedDebounce(this._getCredentials.bind(this)) private async _getCredentials(id: Connection['id'], provider: CredentialsProvider): Promise { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('_getCredentials called') const credentials = await this.getCachedCredentials(provider) if (credentials !== undefined) { //eslint-disable-next-line aws-toolkits/no-console-log diff --git a/packages/core/src/auth/credentials/store.ts b/packages/core/src/auth/credentials/store.ts index 1e442c27ada..7a0ff83a0ab 100644 --- a/packages/core/src/auth/credentials/store.ts +++ b/packages/core/src/auth/credentials/store.ts @@ -31,8 +31,6 @@ export class CredentialsStore { */ public isValid(key: string): boolean { if (this.credentialsCache[key]) { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('credentialsCache=%O', this.credentialsCache[key]) const expiration = this.credentialsCache[key].credentials.expiration return expiration !== undefined ? expiration >= new globals.clock.Date() : true } diff --git a/packages/core/src/auth/secondaryAuth.ts b/packages/core/src/auth/secondaryAuth.ts index 6badfb2eceb..add91765833 100644 --- a/packages/core/src/auth/secondaryAuth.ts +++ b/packages/core/src/auth/secondaryAuth.ts @@ -148,8 +148,6 @@ export class SecondaryAuth { }) const refreshConn = (event: string) => { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('refreshing connection with event=%s', event) getLogger().debug(`secondaryAuth: detected ${event} event in sso cache, refreshing auth.`) globals.clock.setTimeout( telemetry.function_call.run( @@ -160,12 +158,8 @@ export class SecondaryAuth { getLogger().debug( `secondaryAuth: cache event did not update global state, no refresh is needed.` ) - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('secondaryAuth: cache event did not update global state, no refresh is needed.') return } - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('secondaryAuth: restorePreviousSession/restoreConnecton') await this.auth.restorePreviousSession() await this.restoreConnection(true) }, From cb1c3b61879f50d97dbc3298a51d5ae3655ef0e0 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 15:40:21 -0500 Subject: [PATCH 16/44] add more debugging --- packages/core/src/auth/auth.ts | 2 ++ packages/core/src/auth/credentials/store.ts | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/packages/core/src/auth/auth.ts b/packages/core/src/auth/auth.ts index 0084dea7c7f..4a1cf703612 100644 --- a/packages/core/src/auth/auth.ts +++ b/packages/core/src/auth/auth.ts @@ -848,6 +848,8 @@ export class Auth implements AuthService, ConnectionManager { } private async createCachedCredentials(provider: CredentialsProvider) { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('createCachedCredentials called') const providerId = provider.getCredentialsId() globals.loginManager.store.invalidateCredentials(providerId) const { credentials } = await globals.loginManager.store.upsertCredentials(providerId, provider) diff --git a/packages/core/src/auth/credentials/store.ts b/packages/core/src/auth/credentials/store.ts index 7a0ff83a0ab..690f9236d8c 100644 --- a/packages/core/src/auth/credentials/store.ts +++ b/packages/core/src/auth/credentials/store.ts @@ -60,6 +60,8 @@ export class CredentialsStore { credentialsId: CredentialsId, credentialsProvider: CredentialsProvider ): Promise { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('upsertCredentials called') let credentials = await this.getCredentials(credentialsId) if (!credentials) { @@ -109,6 +111,8 @@ export async function getCredentialsFromStore( credentialsId: CredentialsId, credentialsStore: CredentialsStore ): Promise { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('getCredentialsFromStore called') const provider = await CredentialsProviderManager.getInstance().getCredentialsProvider(credentialsId) if (!provider) { credentialsStore.invalidateCredentials(credentialsId) From 2cfe87e3709fec8741b48bf1e306f549a7bd762f Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 15:42:48 -0500 Subject: [PATCH 17/44] another log --- packages/core/src/auth/auth.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/core/src/auth/auth.ts b/packages/core/src/auth/auth.ts index 4a1cf703612..475c25b39c3 100644 --- a/packages/core/src/auth/auth.ts +++ b/packages/core/src/auth/auth.ts @@ -870,6 +870,8 @@ export class Auth implements AuthService, ConnectionManager { private readonly getToken = keyedDebounce(this._getToken.bind(this)) @withTelemetryContext({ name: '_getToken', class: authClassName }) private async _getToken(id: Connection['id'], provider: SsoAccessTokenProvider): Promise { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('_getToken called') const token = await provider.getToken().catch((err) => { this.throwOnRecoverableError(err) @@ -922,7 +924,8 @@ export class Auth implements AuthService, ConnectionManager { @withTelemetryContext({ name: 'handleInvalidCredentials', class: authClassName }) private async handleInvalidCredentials(id: Connection['id'], refresh: () => Promise): Promise { getLogger().info(`auth: Handling invalid credentials of connection: ${id}`) - + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('handleInvalidCredentials called') let profile: StoredProfile try { profile = this.store.getProfileOrThrow(id) From 3c4311531104f9db35ac459989934c508fca4d2e Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 16:04:06 -0500 Subject: [PATCH 18/44] add more logging --- packages/core/src/auth/auth.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/core/src/auth/auth.ts b/packages/core/src/auth/auth.ts index 475c25b39c3..fe8030adf5b 100644 --- a/packages/core/src/auth/auth.ts +++ b/packages/core/src/auth/auth.ts @@ -218,6 +218,8 @@ export class Auth implements AuthService, ConnectionManager { public async reauthenticate({ id }: Pick, invalidate?: boolean): Promise @withTelemetryContext({ name: 'reauthenticate', class: authClassName }) public async reauthenticate({ id }: Pick, invalidate?: boolean): Promise { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('reauthenticate is called') const shouldInvalidate = invalidate ?? true const profile = this.store.getProfileOrThrow(id) if (profile.type === 'sso') { @@ -452,6 +454,8 @@ export class Auth implements AuthService, ConnectionManager { */ @withTelemetryContext({ name: 'refreshConnectionState', class: authClassName }) public async refreshConnectionState(connection?: Pick): Promise { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('refreshConnectionState called') if (connection === undefined) { return } @@ -646,7 +650,11 @@ export class Auth implements AuthService, ConnectionManager { */ @withTelemetryContext({ name: 'validateConnection', class: authClassName }) private async validateConnection(id: Connection['id'], profile: StoredProfile) { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('validateConnection is called') const runCheck = async () => { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('runCheck is called') if (profile.type === 'sso') { const provider = this.getSsoTokenProvider(id, profile) if ((await provider.getToken()) === undefined) { From fa8c1038ef2c188389ade22eff9e3901d730f605 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 16:31:26 -0500 Subject: [PATCH 19/44] add more logging --- packages/core/src/auth/auth.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/core/src/auth/auth.ts b/packages/core/src/auth/auth.ts index fe8030adf5b..6e217505bde 100644 --- a/packages/core/src/auth/auth.ts +++ b/packages/core/src/auth/auth.ts @@ -870,7 +870,14 @@ export class Auth implements AuthService, ConnectionManager { //eslint-disable-next-line aws-toolkits/no-console-log console.log('getCachedCredentials called') const creds = await globals.loginManager.store.getCredentials(provider.getCredentialsId()) + //eslint-disable-next-line aws-toolkits/no-console-log + console.log( + 'provider hash is same as credentials has: %O', + creds?.credentialsHashCode === provider.getHashCode() + ) if (creds !== undefined && creds.credentialsHashCode === provider.getHashCode()) { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('returning cached credentials') return creds.credentials } } @@ -918,6 +925,8 @@ export class Auth implements AuthService, ConnectionManager { //eslint-disable-next-line aws-toolkits/no-console-log console.log('_getCredentials called') const credentials = await this.getCachedCredentials(provider) + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('credentials is undefined: %O', credentials === undefined) if (credentials !== undefined) { //eslint-disable-next-line aws-toolkits/no-console-log console.log('returning cached credentials') @@ -925,6 +934,8 @@ export class Auth implements AuthService, ConnectionManager { } else if ((await provider.canAutoConnect()) === true) { return this.createCachedCredentials(provider) } else { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('hit case with handleInvalidCredentials') return this.handleInvalidCredentials(id, () => this.createCachedCredentials(provider)) } } From e0eae0c1506d0468bf9b0ddbb7ecd8e963f0fa66 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 16:51:38 -0500 Subject: [PATCH 20/44] try adding a waitUntil to check if file content updated --- packages/core/src/test/credentials/auth.test.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 60bb74acc16..71da1b3b483 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -19,6 +19,7 @@ import { UserCredentialsUtils } from '../../shared/credentials/userCredentialsUt import { getCredentialsFilename } from '../../auth/credentials/sharedCredentialsFile' import { Connection, isIamConnection, isSsoConnection, scopesSsoAccountAccess } from '../../auth/connection' import { AuthNode, createDeleteConnectionButton, promptForConnection } from '../../auth/utils' +import { waitUntil } from '../../shared' const ssoProfile = createSsoProfile() const scopedSsoProfile = createSsoProfile({ scopes: ['foo'] }) @@ -522,12 +523,19 @@ describe('Auth', function () { secretAccessKey: initialCreds.secretKey, sessionToken: undefined, }) - + const contentBefore = await fs.readFileText(getCredentialsFilename()) await fs.delete(getCredentialsFilename()) - console.log('file exists after delete=%O', await fs.exists(getCredentialsFilename())) const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } await UserCredentialsUtils.generateCredentialsFile(newCreds) - console.log('file exists after generating=%O', await fs.exists(getCredentialsFilename())) + const didUpdate = await waitUntil( + async () => (await fs.readFileText(getCredentialsFilename())) !== contentBefore, + { + timeout: 5000, + truthy: true, + } + ) + + assert.ok(didUpdate, 'Expected the credentials file to be updated') assert.deepStrictEqual(await conn.getCredentials(), { accessKeyId: newCreds.accessKey, From a42f2647c03e375c90d2c82f4fd596f2e59f00d0 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 12 Nov 2024 17:15:50 -0500 Subject: [PATCH 21/44] implement a retry mechanism --- packages/core/src/test/credentials/auth.test.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 71da1b3b483..9b0167ebb3b 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -19,7 +19,7 @@ import { UserCredentialsUtils } from '../../shared/credentials/userCredentialsUt import { getCredentialsFilename } from '../../auth/credentials/sharedCredentialsFile' import { Connection, isIamConnection, isSsoConnection, scopesSsoAccountAccess } from '../../auth/connection' import { AuthNode, createDeleteConnectionButton, promptForConnection } from '../../auth/utils' -import { waitUntil } from '../../shared' +import { isWin } from '../../shared/vscode/env' const ssoProfile = createSsoProfile() const scopedSsoProfile = createSsoProfile({ scopes: ['foo'] }) @@ -508,6 +508,9 @@ describe('Auth', function () { }) for (const _ of Array.from({ length: 1000 }, (i) => i)) { it('does not cache if the credentials file changes', async function () { + if (isWin()) { + this.retries(5) + } const initialCreds = { profileName: 'default', accessKey: 'x', @@ -527,15 +530,9 @@ describe('Auth', function () { await fs.delete(getCredentialsFilename()) const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } await UserCredentialsUtils.generateCredentialsFile(newCreds) - const didUpdate = await waitUntil( - async () => (await fs.readFileText(getCredentialsFilename())) !== contentBefore, - { - timeout: 5000, - truthy: true, - } - ) + const contentAfter = await fs.readFileText(getCredentialsFilename()) - assert.ok(didUpdate, 'Expected the credentials file to be updated') + assert.ok(contentBefore !== contentAfter) assert.deepStrictEqual(await conn.getCredentials(), { accessKeyId: newCreds.accessKey, From 9b167acd120020a40893d089e43dbde317b3263b Mon Sep 17 00:00:00 2001 From: hkobew Date: Wed, 13 Nov 2024 09:55:01 -0500 Subject: [PATCH 22/44] adjust logging, set timeout manually --- packages/core/src/auth/auth.ts | 12 ++++-------- packages/core/src/auth/credentials/store.ts | 2 -- .../src/auth/providers/sharedCredentialsProvider.ts | 7 ++++++- packages/core/src/test/credentials/auth.test.ts | 2 ++ 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/core/src/auth/auth.ts b/packages/core/src/auth/auth.ts index 6e217505bde..a174caaf1bb 100644 --- a/packages/core/src/auth/auth.ts +++ b/packages/core/src/auth/auth.ts @@ -867,14 +867,14 @@ export class Auth implements AuthService, ConnectionManager { } private async getCachedCredentials(provider: CredentialsProvider) { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('getCachedCredentials called') const creds = await globals.loginManager.store.getCredentials(provider.getCredentialsId()) //eslint-disable-next-line aws-toolkits/no-console-log console.log( 'provider hash is same as credentials has: %O', creds?.credentialsHashCode === provider.getHashCode() ) + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('creds is undefined: %O', creds === undefined) if (creds !== undefined && creds.credentialsHashCode === provider.getHashCode()) { //eslint-disable-next-line aws-toolkits/no-console-log console.log('returning cached credentials') @@ -885,8 +885,6 @@ export class Auth implements AuthService, ConnectionManager { private readonly getToken = keyedDebounce(this._getToken.bind(this)) @withTelemetryContext({ name: '_getToken', class: authClassName }) private async _getToken(id: Connection['id'], provider: SsoAccessTokenProvider): Promise { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('_getToken called') const token = await provider.getToken().catch((err) => { this.throwOnRecoverableError(err) @@ -922,20 +920,18 @@ export class Auth implements AuthService, ConnectionManager { private readonly getCredentials = keyedDebounce(this._getCredentials.bind(this)) private async _getCredentials(id: Connection['id'], provider: CredentialsProvider): Promise { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('_getCredentials called') const credentials = await this.getCachedCredentials(provider) //eslint-disable-next-line aws-toolkits/no-console-log console.log('credentials is undefined: %O', credentials === undefined) if (credentials !== undefined) { //eslint-disable-next-line aws-toolkits/no-console-log - console.log('returning cached credentials') + console.log('hit sad path (cache passed)') return credentials } else if ((await provider.canAutoConnect()) === true) { return this.createCachedCredentials(provider) } else { //eslint-disable-next-line aws-toolkits/no-console-log - console.log('hit case with handleInvalidCredentials') + console.log('hit happy path (cache failed)') return this.handleInvalidCredentials(id, () => this.createCachedCredentials(provider)) } } diff --git a/packages/core/src/auth/credentials/store.ts b/packages/core/src/auth/credentials/store.ts index 690f9236d8c..ece675b5399 100644 --- a/packages/core/src/auth/credentials/store.ts +++ b/packages/core/src/auth/credentials/store.ts @@ -42,8 +42,6 @@ export class CredentialsStore { * Returns undefined if the specified credentials are expired or not found. */ public async getCredentials(credentials: CredentialsId): Promise { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('credentials.isValid=%O', this.isValid(asString(credentials))) if (this.isValid(asString(credentials))) { return this.credentialsCache[asString(credentials)] } else { diff --git a/packages/core/src/auth/providers/sharedCredentialsProvider.ts b/packages/core/src/auth/providers/sharedCredentialsProvider.ts index 10167c38daa..6bbc354e8ef 100644 --- a/packages/core/src/auth/providers/sharedCredentialsProvider.ts +++ b/packages/core/src/auth/providers/sharedCredentialsProvider.ts @@ -67,7 +67,10 @@ export class SharedCredentialsProvider implements CredentialsProvider { public constructor( private readonly profileName: string, private readonly sections: Section[] - ) {} + ) { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('init new SharedCredentialsProvider') + } public getCredentialsId(): CredentialsId { return { @@ -98,6 +101,8 @@ export class SharedCredentialsProvider implements CredentialsProvider { } public getHashCode(): string { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('returning hash: %O', getStringHash(JSON.stringify(this.profile))) return getStringHash(JSON.stringify(this.profile)) } diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 9b0167ebb3b..a5aa20a27c9 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -510,7 +510,9 @@ describe('Auth', function () { it('does not cache if the credentials file changes', async function () { if (isWin()) { this.retries(5) + this.timeout(30000) } + const initialCreds = { profileName: 'default', accessKey: 'x', From 5db69dc88f89763eeedf4ca36489f5c0988d5a92 Mon Sep 17 00:00:00 2001 From: hkobew Date: Wed, 13 Nov 2024 10:06:24 -0500 Subject: [PATCH 23/44] avoid retry as it causes hang --- packages/core/src/test/credentials/auth.test.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index a5aa20a27c9..0b9a0d36c97 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -19,7 +19,6 @@ import { UserCredentialsUtils } from '../../shared/credentials/userCredentialsUt import { getCredentialsFilename } from '../../auth/credentials/sharedCredentialsFile' import { Connection, isIamConnection, isSsoConnection, scopesSsoAccountAccess } from '../../auth/connection' import { AuthNode, createDeleteConnectionButton, promptForConnection } from '../../auth/utils' -import { isWin } from '../../shared/vscode/env' const ssoProfile = createSsoProfile() const scopedSsoProfile = createSsoProfile({ scopes: ['foo'] }) @@ -508,10 +507,10 @@ describe('Auth', function () { }) for (const _ of Array.from({ length: 1000 }, (i) => i)) { it('does not cache if the credentials file changes', async function () { - if (isWin()) { - this.retries(5) - this.timeout(30000) - } + // if (isWin()) { + // this.retries(5) + // this.timeout(30000) + // } const initialCreds = { profileName: 'default', From 143fa3483f052d0ed95f58ba30c544bde3615384 Mon Sep 17 00:00:00 2001 From: hkobew Date: Wed, 13 Nov 2024 10:31:24 -0500 Subject: [PATCH 24/44] add logging to refresh logic --- .../sharedCredentialsProviderFactory.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts index 41cc3465516..aa24c362537 100644 --- a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts +++ b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts @@ -18,7 +18,11 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac private loadedConfigModificationMillis?: number public async refresh(): Promise { + // eslint-disable-next-line aws-toolkits/no-console-log + console.log('refresh called') if (await this.needsRefresh()) { + // eslint-disable-next-line aws-toolkits/no-console-log + console.log('needs refresh passed') await this.loadSharedCredentialsProviders() } } @@ -37,7 +41,16 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac private async needsRefresh(): Promise { const credentialsLastModMillis = await this.getLastModifiedMillis(getCredentialsFilename()) const configLastModMillis = await this.getLastModifiedMillis(getConfigFilename()) - + // eslint-disable-next-line aws-toolkits/no-console-log + console.log('in needs refresh') + // eslint-disable-next-line aws-toolkits/no-console-log + console.log('\tcredentialsLastModMillis: %O', credentialsLastModMillis) + // eslint-disable-next-line aws-toolkits/no-console-log + console.log('\tconfigLastModMillis: %O', configLastModMillis) + // eslint-disable-next-line aws-toolkits/no-console-log + console.log('\tthis.loadedCredentialsModificationMillis: %O', this.loadedCredentialsModificationMillis) + // eslint-disable-next-line aws-toolkits/no-console-log + console.log('\tthis.loadedConfigModificationMillis: %O', this.loadedConfigModificationMillis) return ( this.loadedCredentialsModificationMillis !== credentialsLastModMillis || this.loadedConfigModificationMillis !== configLastModMillis @@ -45,6 +58,8 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac } private async loadSharedCredentialsProviders(): Promise { + //eslint-disable-next-line aws-toolkits/no-console-log + console.log('loadSharedCredentialsProviders called') this.resetProviders() const result = await loadSharedCredentialsSections() From e65fe5019bbc1775c5bcc9e175112b3726d26116 Mon Sep 17 00:00:00 2001 From: hkobew Date: Wed, 13 Nov 2024 14:09:34 -0500 Subject: [PATCH 25/44] adjust test and its logging --- packages/core/src/auth/credentials/store.ts | 4 ---- .../sharedCredentialsProviderFactory.ts | 22 +++++++++---------- .../credentials/userCredentialsUtils.ts | 1 + .../core/src/test/credentials/auth.test.ts | 3 +++ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/core/src/auth/credentials/store.ts b/packages/core/src/auth/credentials/store.ts index ece675b5399..2fd2d29b18b 100644 --- a/packages/core/src/auth/credentials/store.ts +++ b/packages/core/src/auth/credentials/store.ts @@ -58,8 +58,6 @@ export class CredentialsStore { credentialsId: CredentialsId, credentialsProvider: CredentialsProvider ): Promise { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('upsertCredentials called') let credentials = await this.getCredentials(credentialsId) if (!credentials) { @@ -109,8 +107,6 @@ export async function getCredentialsFromStore( credentialsId: CredentialsId, credentialsStore: CredentialsStore ): Promise { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('getCredentialsFromStore called') const provider = await CredentialsProviderManager.getInstance().getCredentialsProvider(credentialsId) if (!provider) { credentialsStore.invalidateCredentials(credentialsId) diff --git a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts index aa24c362537..6b1bd115bf7 100644 --- a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts +++ b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts @@ -38,19 +38,19 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac super.resetProviders() } - private async needsRefresh(): Promise { + public async needsRefresh(): Promise { const credentialsLastModMillis = await this.getLastModifiedMillis(getCredentialsFilename()) const configLastModMillis = await this.getLastModifiedMillis(getConfigFilename()) - // eslint-disable-next-line aws-toolkits/no-console-log - console.log('in needs refresh') - // eslint-disable-next-line aws-toolkits/no-console-log - console.log('\tcredentialsLastModMillis: %O', credentialsLastModMillis) - // eslint-disable-next-line aws-toolkits/no-console-log - console.log('\tconfigLastModMillis: %O', configLastModMillis) - // eslint-disable-next-line aws-toolkits/no-console-log - console.log('\tthis.loadedCredentialsModificationMillis: %O', this.loadedCredentialsModificationMillis) - // eslint-disable-next-line aws-toolkits/no-console-log - console.log('\tthis.loadedConfigModificationMillis: %O', this.loadedConfigModificationMillis) + // // eslint-disable-next-line aws-toolkits/no-console-log + // console.log('in needs refresh') + // // eslint-disable-next-line aws-toolkits/no-console-log + // console.log('\tcredentialsLastModMillis: %O', credentialsLastModMillis) + // // eslint-disable-next-line aws-toolkits/no-console-log + // console.log('\tconfigLastModMillis: %O', configLastModMillis) + // // eslint-disable-next-line aws-toolkits/no-console-log + // console.log('\tthis.loadedCredentialsModificationMillis: %O', this.loadedCredentialsModificationMillis) + // // eslint-disable-next-line aws-toolkits/no-console-log + // console.log('\tthis.loadedConfigModificationMillis: %O', this.loadedConfigModificationMillis) return ( this.loadedCredentialsModificationMillis !== credentialsLastModMillis || this.loadedConfigModificationMillis !== configLastModMillis diff --git a/packages/core/src/shared/credentials/userCredentialsUtils.ts b/packages/core/src/shared/credentials/userCredentialsUtils.ts index 3d6e0948568..03c1316a3e4 100644 --- a/packages/core/src/shared/credentials/userCredentialsUtils.ts +++ b/packages/core/src/shared/credentials/userCredentialsUtils.ts @@ -96,6 +96,7 @@ export class UserCredentialsUtils { } else { contents.unshift(header) } + await fs.writeFile(dest, contents.join('\n')) } } diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 0b9a0d36c97..e760a0d18f3 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -528,12 +528,15 @@ describe('Auth', function () { sessionToken: undefined, }) const contentBefore = await fs.readFileText(getCredentialsFilename()) + const statBefore = await fs.stat(getCredentialsFilename()) await fs.delete(getCredentialsFilename()) const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } await UserCredentialsUtils.generateCredentialsFile(newCreds) const contentAfter = await fs.readFileText(getCredentialsFilename()) + const statAfter = await fs.stat(getCredentialsFilename()) assert.ok(contentBefore !== contentAfter) + assert.notDeepStrictEqual(statAfter, statBefore) assert.deepStrictEqual(await conn.getCredentials(), { accessKeyId: newCreds.accessKey, From 46ac1527cdd8352ff9a0d1d8d7a66e779e18f448 Mon Sep 17 00:00:00 2001 From: hkobew Date: Wed, 13 Nov 2024 14:23:01 -0500 Subject: [PATCH 26/44] remove all logging --- packages/core/src/auth/auth.ts | 28 +------------------ .../providers/sharedCredentialsProvider.ts | 7 +---- .../sharedCredentialsProviderFactory.ts | 19 ++----------- 3 files changed, 4 insertions(+), 50 deletions(-) diff --git a/packages/core/src/auth/auth.ts b/packages/core/src/auth/auth.ts index a174caaf1bb..d7c2a5d58b7 100644 --- a/packages/core/src/auth/auth.ts +++ b/packages/core/src/auth/auth.ts @@ -218,8 +218,6 @@ export class Auth implements AuthService, ConnectionManager { public async reauthenticate({ id }: Pick, invalidate?: boolean): Promise @withTelemetryContext({ name: 'reauthenticate', class: authClassName }) public async reauthenticate({ id }: Pick, invalidate?: boolean): Promise { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('reauthenticate is called') const shouldInvalidate = invalidate ?? true const profile = this.store.getProfileOrThrow(id) if (profile.type === 'sso') { @@ -454,8 +452,6 @@ export class Auth implements AuthService, ConnectionManager { */ @withTelemetryContext({ name: 'refreshConnectionState', class: authClassName }) public async refreshConnectionState(connection?: Pick): Promise { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('refreshConnectionState called') if (connection === undefined) { return } @@ -650,11 +646,7 @@ export class Auth implements AuthService, ConnectionManager { */ @withTelemetryContext({ name: 'validateConnection', class: authClassName }) private async validateConnection(id: Connection['id'], profile: StoredProfile) { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('validateConnection is called') const runCheck = async () => { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('runCheck is called') if (profile.type === 'sso') { const provider = this.getSsoTokenProvider(id, profile) if ((await provider.getToken()) === undefined) { @@ -856,8 +848,6 @@ export class Auth implements AuthService, ConnectionManager { } private async createCachedCredentials(provider: CredentialsProvider) { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('createCachedCredentials called') const providerId = provider.getCredentialsId() globals.loginManager.store.invalidateCredentials(providerId) const { credentials } = await globals.loginManager.store.upsertCredentials(providerId, provider) @@ -868,16 +858,7 @@ export class Auth implements AuthService, ConnectionManager { private async getCachedCredentials(provider: CredentialsProvider) { const creds = await globals.loginManager.store.getCredentials(provider.getCredentialsId()) - //eslint-disable-next-line aws-toolkits/no-console-log - console.log( - 'provider hash is same as credentials has: %O', - creds?.credentialsHashCode === provider.getHashCode() - ) - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('creds is undefined: %O', creds === undefined) if (creds !== undefined && creds.credentialsHashCode === provider.getHashCode()) { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('returning cached credentials') return creds.credentials } } @@ -921,17 +902,11 @@ export class Auth implements AuthService, ConnectionManager { private readonly getCredentials = keyedDebounce(this._getCredentials.bind(this)) private async _getCredentials(id: Connection['id'], provider: CredentialsProvider): Promise { const credentials = await this.getCachedCredentials(provider) - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('credentials is undefined: %O', credentials === undefined) if (credentials !== undefined) { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('hit sad path (cache passed)') return credentials } else if ((await provider.canAutoConnect()) === true) { return this.createCachedCredentials(provider) } else { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('hit happy path (cache failed)') return this.handleInvalidCredentials(id, () => this.createCachedCredentials(provider)) } } @@ -939,8 +914,7 @@ export class Auth implements AuthService, ConnectionManager { @withTelemetryContext({ name: 'handleInvalidCredentials', class: authClassName }) private async handleInvalidCredentials(id: Connection['id'], refresh: () => Promise): Promise { getLogger().info(`auth: Handling invalid credentials of connection: ${id}`) - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('handleInvalidCredentials called') + let profile: StoredProfile try { profile = this.store.getProfileOrThrow(id) diff --git a/packages/core/src/auth/providers/sharedCredentialsProvider.ts b/packages/core/src/auth/providers/sharedCredentialsProvider.ts index 6bbc354e8ef..10167c38daa 100644 --- a/packages/core/src/auth/providers/sharedCredentialsProvider.ts +++ b/packages/core/src/auth/providers/sharedCredentialsProvider.ts @@ -67,10 +67,7 @@ export class SharedCredentialsProvider implements CredentialsProvider { public constructor( private readonly profileName: string, private readonly sections: Section[] - ) { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('init new SharedCredentialsProvider') - } + ) {} public getCredentialsId(): CredentialsId { return { @@ -101,8 +98,6 @@ export class SharedCredentialsProvider implements CredentialsProvider { } public getHashCode(): string { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('returning hash: %O', getStringHash(JSON.stringify(this.profile))) return getStringHash(JSON.stringify(this.profile)) } diff --git a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts index 6b1bd115bf7..41cc3465516 100644 --- a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts +++ b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts @@ -18,11 +18,7 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac private loadedConfigModificationMillis?: number public async refresh(): Promise { - // eslint-disable-next-line aws-toolkits/no-console-log - console.log('refresh called') if (await this.needsRefresh()) { - // eslint-disable-next-line aws-toolkits/no-console-log - console.log('needs refresh passed') await this.loadSharedCredentialsProviders() } } @@ -38,19 +34,10 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac super.resetProviders() } - public async needsRefresh(): Promise { + private async needsRefresh(): Promise { const credentialsLastModMillis = await this.getLastModifiedMillis(getCredentialsFilename()) const configLastModMillis = await this.getLastModifiedMillis(getConfigFilename()) - // // eslint-disable-next-line aws-toolkits/no-console-log - // console.log('in needs refresh') - // // eslint-disable-next-line aws-toolkits/no-console-log - // console.log('\tcredentialsLastModMillis: %O', credentialsLastModMillis) - // // eslint-disable-next-line aws-toolkits/no-console-log - // console.log('\tconfigLastModMillis: %O', configLastModMillis) - // // eslint-disable-next-line aws-toolkits/no-console-log - // console.log('\tthis.loadedCredentialsModificationMillis: %O', this.loadedCredentialsModificationMillis) - // // eslint-disable-next-line aws-toolkits/no-console-log - // console.log('\tthis.loadedConfigModificationMillis: %O', this.loadedConfigModificationMillis) + return ( this.loadedCredentialsModificationMillis !== credentialsLastModMillis || this.loadedConfigModificationMillis !== configLastModMillis @@ -58,8 +45,6 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac } private async loadSharedCredentialsProviders(): Promise { - //eslint-disable-next-line aws-toolkits/no-console-log - console.log('loadSharedCredentialsProviders called') this.resetProviders() const result = await loadSharedCredentialsSections() From 1161e548af13cceb4089d3ecb0e403be0605a7ec Mon Sep 17 00:00:00 2001 From: hkobew Date: Wed, 13 Nov 2024 14:45:35 -0500 Subject: [PATCH 27/44] add waitUntil for stat changes --- packages/core/src/test/credentials/auth.test.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index e760a0d18f3..00edff50128 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -19,6 +19,7 @@ import { UserCredentialsUtils } from '../../shared/credentials/userCredentialsUt import { getCredentialsFilename } from '../../auth/credentials/sharedCredentialsFile' import { Connection, isIamConnection, isSsoConnection, scopesSsoAccountAccess } from '../../auth/connection' import { AuthNode, createDeleteConnectionButton, promptForConnection } from '../../auth/utils' +import { waitUntil } from '../../shared' const ssoProfile = createSsoProfile() const scopedSsoProfile = createSsoProfile({ scopes: ['foo'] }) @@ -507,11 +508,6 @@ describe('Auth', function () { }) for (const _ of Array.from({ length: 1000 }, (i) => i)) { it('does not cache if the credentials file changes', async function () { - // if (isWin()) { - // this.retries(5) - // this.timeout(30000) - // } - const initialCreds = { profileName: 'default', accessKey: 'x', @@ -536,7 +532,9 @@ describe('Auth', function () { const statAfter = await fs.stat(getCredentialsFilename()) assert.ok(contentBefore !== contentAfter) - assert.notDeepStrictEqual(statAfter, statBefore) + + await waitUntil(async () => statBefore !== (await fs.stat(getCredentialsFilename())), { timeout: 5000 }) + assert.notDeepStrictEqual(statBefore, statAfter, 'Credentials file update failed') assert.deepStrictEqual(await conn.getCredentials(), { accessKeyId: newCreds.accessKey, From a5225f9e64391a574055ec9a39f07101a1c23ac8 Mon Sep 17 00:00:00 2001 From: hkobew Date: Wed, 13 Nov 2024 15:00:04 -0500 Subject: [PATCH 28/44] try a long interval --- packages/core/src/test/credentials/auth.test.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 00edff50128..f39b0bf6b72 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -529,12 +529,15 @@ describe('Auth', function () { const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } await UserCredentialsUtils.generateCredentialsFile(newCreds) const contentAfter = await fs.readFileText(getCredentialsFilename()) - const statAfter = await fs.stat(getCredentialsFilename()) - assert.ok(contentBefore !== contentAfter) + assert.notDeepStrictEqual(contentBefore, contentAfter) - await waitUntil(async () => statBefore !== (await fs.stat(getCredentialsFilename())), { timeout: 5000 }) - assert.notDeepStrictEqual(statBefore, statAfter, 'Credentials file update failed') + const updated = await waitUntil(async () => statBefore !== (await fs.stat(getCredentialsFilename())), { + timeout: 20000, + interval: 1000, + truthy: true, + }) + assert.ok(updated, 'Credentials file update failed') assert.deepStrictEqual(await conn.getCredentials(), { accessKeyId: newCreds.accessKey, From 354164ac5510068960a4257b29510ee92c2a849a Mon Sep 17 00:00:00 2001 From: hkobew Date: Wed, 13 Nov 2024 16:56:01 -0500 Subject: [PATCH 29/44] wrap entire check in a waitUntil --- packages/core/src/test/credentials/auth.test.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index f39b0bf6b72..3dc6a63129c 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -20,6 +20,7 @@ import { getCredentialsFilename } from '../../auth/credentials/sharedCredentials import { Connection, isIamConnection, isSsoConnection, scopesSsoAccountAccess } from '../../auth/connection' import { AuthNode, createDeleteConnectionButton, promptForConnection } from '../../auth/utils' import { waitUntil } from '../../shared' +import { Credentials } from '@aws-sdk/types' const ssoProfile = createSsoProfile() const scopedSsoProfile = createSsoProfile({ scopes: ['foo'] }) @@ -524,7 +525,6 @@ describe('Auth', function () { sessionToken: undefined, }) const contentBefore = await fs.readFileText(getCredentialsFilename()) - const statBefore = await fs.stat(getCredentialsFilename()) await fs.delete(getCredentialsFilename()) const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } await UserCredentialsUtils.generateCredentialsFile(newCreds) @@ -532,18 +532,16 @@ describe('Auth', function () { assert.notDeepStrictEqual(contentBefore, contentAfter) - const updated = await waitUntil(async () => statBefore !== (await fs.stat(getCredentialsFilename())), { + const areCredsEqual = (creds: Credentials) => { + return creds.accessKeyId === newCreds.accessKey && creds.secretAccessKey === newCreds.secretKey + } + + const credsUpdated = await waitUntil(async () => areCredsEqual(await conn.getCredentials()), { timeout: 20000, interval: 1000, truthy: true, }) - assert.ok(updated, 'Credentials file update failed') - - assert.deepStrictEqual(await conn.getCredentials(), { - accessKeyId: newCreds.accessKey, - secretAccessKey: newCreds.secretKey, - sessionToken: undefined, - }) + assert.ok(credsUpdated, 'Credentials file update failed') }) } }) From 10051bcac9c0edbf1a9af95b2208f4171453427d Mon Sep 17 00:00:00 2001 From: hkobew Date: Thu, 14 Nov 2024 14:41:10 -0500 Subject: [PATCH 30/44] simplify test code --- .../core/src/test/credentials/auth.test.ts | 74 +++++++++---------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 3dc6a63129c..3224de9eade 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -19,8 +19,8 @@ import { UserCredentialsUtils } from '../../shared/credentials/userCredentialsUt import { getCredentialsFilename } from '../../auth/credentials/sharedCredentialsFile' import { Connection, isIamConnection, isSsoConnection, scopesSsoAccountAccess } from '../../auth/connection' import { AuthNode, createDeleteConnectionButton, promptForConnection } from '../../auth/utils' -import { waitUntil } from '../../shared' import { Credentials } from '@aws-sdk/types' +import { waitUntil } from '../../shared' const ssoProfile = createSsoProfile() const scopedSsoProfile = createSsoProfile({ scopes: ['foo'] }) @@ -492,8 +492,8 @@ describe('Auth', function () { }) }) }) - // eslint-disable-next-line aws-toolkits/no-only-in-tests - describe.only('Shared ini files', function () { + + describe('Shared ini files', function () { let tmpDir: string beforeEach(async function () { @@ -507,43 +507,39 @@ describe('Auth', function () { sinon.restore() await fs.delete(tmpDir, { recursive: true }) }) - for (const _ of Array.from({ length: 1000 }, (i) => i)) { - it('does not cache if the credentials file changes', async function () { - const initialCreds = { - profileName: 'default', - accessKey: 'x', - secretKey: 'x', - } - - await UserCredentialsUtils.generateCredentialsFile(initialCreds) - - const conn = await auth.getConnection({ id: 'profile:default' }) - assert.ok(conn?.type === 'iam', 'Expected an IAM connection') - assert.deepStrictEqual(await conn.getCredentials(), { - accessKeyId: initialCreds.accessKey, - secretAccessKey: initialCreds.secretKey, - sessionToken: undefined, - }) - const contentBefore = await fs.readFileText(getCredentialsFilename()) - await fs.delete(getCredentialsFilename()) - const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } - await UserCredentialsUtils.generateCredentialsFile(newCreds) - const contentAfter = await fs.readFileText(getCredentialsFilename()) - - assert.notDeepStrictEqual(contentBefore, contentAfter) - - const areCredsEqual = (creds: Credentials) => { - return creds.accessKeyId === newCreds.accessKey && creds.secretAccessKey === newCreds.secretKey - } - - const credsUpdated = await waitUntil(async () => areCredsEqual(await conn.getCredentials()), { - timeout: 20000, - interval: 1000, - truthy: true, - }) - assert.ok(credsUpdated, 'Credentials file update failed') + + it('does not cache if the credentials file changes', async function () { + const initialCreds = { + profileName: 'default', + accessKey: 'x', + secretKey: 'x', + } + + await UserCredentialsUtils.generateCredentialsFile(initialCreds) + + const conn = await auth.getConnection({ id: 'profile:default' }) + assert.ok(conn?.type === 'iam', 'Expected an IAM connection') + assert.deepStrictEqual(await conn.getCredentials(), { + accessKeyId: initialCreds.accessKey, + secretAccessKey: initialCreds.secretKey, + sessionToken: undefined, }) - } + + await fs.delete(getCredentialsFilename()) + + const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } + await UserCredentialsUtils.generateCredentialsFile(newCreds) + + const credsAreUpdated = (creds: Credentials) => { + return creds.accessKeyId === newCreds.accessKey && creds.secretAccessKey === newCreds.secretKey + } + + const credsUpdated = await waitUntil(async () => credsAreUpdated(await conn.getCredentials()), { + timeout: 5000, + truthy: true, + }) + assert.ok(credsUpdated, 'Expected credentials to be updated') + }) }) describe('AuthNode', function () { From 0ca894383289cb52ddb92471f5c3f0ffaa67b3da Mon Sep 17 00:00:00 2001 From: hkobew Date: Thu, 14 Nov 2024 14:54:55 -0500 Subject: [PATCH 31/44] add back test isolation --- .../core/src/test/credentials/auth.test.ts | 57 ++++++++++--------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 3224de9eade..9d4fbc30627 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -508,38 +508,41 @@ describe('Auth', function () { await fs.delete(tmpDir, { recursive: true }) }) - it('does not cache if the credentials file changes', async function () { - const initialCreds = { - profileName: 'default', - accessKey: 'x', - secretKey: 'x', - } - - await UserCredentialsUtils.generateCredentialsFile(initialCreds) - - const conn = await auth.getConnection({ id: 'profile:default' }) - assert.ok(conn?.type === 'iam', 'Expected an IAM connection') - assert.deepStrictEqual(await conn.getCredentials(), { - accessKeyId: initialCreds.accessKey, - secretAccessKey: initialCreds.secretKey, - sessionToken: undefined, - }) + for (const _ of Array.from({ length: 1000 }, (i) => i)) { + it('does not cache if the credentials file changes', async function () { + const initialCreds = { + profileName: 'default', + accessKey: 'x', + secretKey: 'x', + } + + await UserCredentialsUtils.generateCredentialsFile(initialCreds) + + const conn = await auth.getConnection({ id: 'profile:default' }) + assert.ok(conn?.type === 'iam', 'Expected an IAM connection') + assert.deepStrictEqual(await conn.getCredentials(), { + accessKeyId: initialCreds.accessKey, + secretAccessKey: initialCreds.secretKey, + sessionToken: undefined, + }) - await fs.delete(getCredentialsFilename()) + await fs.delete(getCredentialsFilename()) - const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } - await UserCredentialsUtils.generateCredentialsFile(newCreds) + const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } + await UserCredentialsUtils.generateCredentialsFile(newCreds) - const credsAreUpdated = (creds: Credentials) => { - return creds.accessKeyId === newCreds.accessKey && creds.secretAccessKey === newCreds.secretKey - } + const credsAreUpdated = (creds: Credentials) => { + return creds.accessKeyId === newCreds.accessKey && creds.secretAccessKey === newCreds.secretKey + } - const credsUpdated = await waitUntil(async () => credsAreUpdated(await conn.getCredentials()), { - timeout: 5000, - truthy: true, + const credsUpdated = await waitUntil(async () => credsAreUpdated(await conn.getCredentials()), { + timeout: 5000, + interval: 100, + truthy: true, + }) + assert.ok(credsUpdated, 'Expected credentials to be updated') }) - assert.ok(credsUpdated, 'Expected credentials to be updated') - }) + } }) describe('AuthNode', function () { From 9291ebb424be24e09c0692af65a88f828ec912c0 Mon Sep 17 00:00:00 2001 From: hkobew Date: Thu, 14 Nov 2024 15:33:20 -0500 Subject: [PATCH 32/44] add check for mtime --- packages/core/src/test/credentials/auth.test.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 9d4fbc30627..18839edffe8 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -508,7 +508,7 @@ describe('Auth', function () { await fs.delete(tmpDir, { recursive: true }) }) - for (const _ of Array.from({ length: 1000 }, (i) => i)) { + for (const _ of Array.from({ length: 100 }, (i) => i)) { it('does not cache if the credentials file changes', async function () { const initialCreds = { profileName: 'default', @@ -525,16 +525,18 @@ describe('Auth', function () { secretAccessKey: initialCreds.secretKey, sessionToken: undefined, }) + const lastModifiedBefore = (await fs.stat(getCredentialsFilename())).mtime await fs.delete(getCredentialsFilename()) const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } await UserCredentialsUtils.generateCredentialsFile(newCreds) - + const lastModifiedAfter = (await fs.stat(getCredentialsFilename())).mtime const credsAreUpdated = (creds: Credentials) => { return creds.accessKeyId === newCreds.accessKey && creds.secretAccessKey === newCreds.secretKey } - + console.log(lastModifiedBefore, lastModifiedAfter) + assert.notStrictEqual(lastModifiedBefore, lastModifiedAfter, 'Expected credentials file to be updated') const credsUpdated = await waitUntil(async () => credsAreUpdated(await conn.getCredentials()), { timeout: 5000, interval: 100, From 8c314424503a9e228c547151ac1bc3c314a7eb77 Mon Sep 17 00:00:00 2001 From: hkobew Date: Thu, 14 Nov 2024 15:59:13 -0500 Subject: [PATCH 33/44] add logging statements --- packages/core/src/test/credentials/auth.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 18839edffe8..a87d0eae0a6 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -535,7 +535,7 @@ describe('Auth', function () { const credsAreUpdated = (creds: Credentials) => { return creds.accessKeyId === newCreds.accessKey && creds.secretAccessKey === newCreds.secretKey } - console.log(lastModifiedBefore, lastModifiedAfter) + console.log('before: %O, after: %O', lastModifiedBefore, lastModifiedAfter) assert.notStrictEqual(lastModifiedBefore, lastModifiedAfter, 'Expected credentials file to be updated') const credsUpdated = await waitUntil(async () => credsAreUpdated(await conn.getCredentials()), { timeout: 5000, From c28425414677725c3ae21c4ea65caaccc52291ec Mon Sep 17 00:00:00 2001 From: hkobew Date: Thu, 14 Nov 2024 17:36:26 -0500 Subject: [PATCH 34/44] adjust logging and timeout --- .../sharedCredentialsProviderFactory.ts | 1 + packages/core/src/test/credentials/auth.test.ts | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts index 41cc3465516..77a1eab2d77 100644 --- a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts +++ b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts @@ -35,6 +35,7 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac } private async needsRefresh(): Promise { + console.log('needs refreshCalled') const credentialsLastModMillis = await this.getLastModifiedMillis(getCredentialsFilename()) const configLastModMillis = await this.getLastModifiedMillis(getConfigFilename()) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index a87d0eae0a6..5f7a70c824e 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -508,8 +508,8 @@ describe('Auth', function () { await fs.delete(tmpDir, { recursive: true }) }) - for (const _ of Array.from({ length: 100 }, (i) => i)) { - it('does not cache if the credentials file changes', async function () { + for (const _ of Array.from({ length: 1 }, (i) => i)) { + it.only('does not cache if the credentials file changes', async function () { const initialCreds = { profileName: 'default', accessKey: 'x', @@ -525,20 +525,22 @@ describe('Auth', function () { secretAccessKey: initialCreds.secretKey, sessionToken: undefined, }) - const lastModifiedBefore = (await fs.stat(getCredentialsFilename())).mtime + const statBefore = await fs.stat(getCredentialsFilename()) await fs.delete(getCredentialsFilename()) + console.log('file deleted') const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } await UserCredentialsUtils.generateCredentialsFile(newCreds) - const lastModifiedAfter = (await fs.stat(getCredentialsFilename())).mtime + console.log('file written') + const statAfter = await fs.stat(getCredentialsFilename()) const credsAreUpdated = (creds: Credentials) => { return creds.accessKeyId === newCreds.accessKey && creds.secretAccessKey === newCreds.secretKey } - console.log('before: %O, after: %O', lastModifiedBefore, lastModifiedAfter) - assert.notStrictEqual(lastModifiedBefore, lastModifiedAfter, 'Expected credentials file to be updated') + console.log('before: %O, after: %O', statBefore, statAfter) + assert.notStrictEqual(statBefore, statAfter, 'Expected credentials file to be updated') const credsUpdated = await waitUntil(async () => credsAreUpdated(await conn.getCredentials()), { - timeout: 5000, + timeout: 1000, interval: 100, truthy: true, }) From 53fc20e13991a7e3a69025c7186bad14c0068d43 Mon Sep 17 00:00:00 2001 From: hkobew Date: Thu, 14 Nov 2024 17:38:47 -0500 Subject: [PATCH 35/44] adjust logging --- packages/core/src/test/credentials/auth.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 5f7a70c824e..40ad5c489c3 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -544,6 +544,8 @@ describe('Auth', function () { interval: 100, truthy: true, }) + const statLater = await fs.stat(getCredentialsFilename()) + console.log('before: %O, after: %O, later: %O', statBefore, statAfter, statLater) assert.ok(credsUpdated, 'Expected credentials to be updated') }) } From fbb215d9fdc98357be03a5193e8de4ee44be2042 Mon Sep 17 00:00:00 2001 From: hkobew Date: Thu, 14 Nov 2024 17:48:15 -0500 Subject: [PATCH 36/44] rerun test a bunch --- packages/core/src/test/credentials/auth.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 40ad5c489c3..6390f6f0492 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -508,8 +508,8 @@ describe('Auth', function () { await fs.delete(tmpDir, { recursive: true }) }) - for (const _ of Array.from({ length: 1 }, (i) => i)) { - it.only('does not cache if the credentials file changes', async function () { + for (const _ of Array.from({ length: 100 }, (i) => i)) { + it('does not cache if the credentials file changes', async function () { const initialCreds = { profileName: 'default', accessKey: 'x', From 9dff77c1ae40e9a605281b46df82368ce8013c79 Mon Sep 17 00:00:00 2001 From: hkobew Date: Fri, 15 Nov 2024 09:55:53 -0500 Subject: [PATCH 37/44] add really explicit logging statements --- .../sharedCredentialsProviderFactory.ts | 1 - .../core/src/test/credentials/auth.test.ts | 49 ++++++++++++++----- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts index 77a1eab2d77..41cc3465516 100644 --- a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts +++ b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts @@ -35,7 +35,6 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac } private async needsRefresh(): Promise { - console.log('needs refreshCalled') const credentialsLastModMillis = await this.getLastModifiedMillis(getCredentialsFilename()) const configLastModMillis = await this.getLastModifiedMillis(getConfigFilename()) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 6390f6f0492..d1550733c28 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -515,9 +515,12 @@ describe('Auth', function () { accessKey: 'x', secretKey: 'x', } - + console.log('generating file for the first time') await UserCredentialsUtils.generateCredentialsFile(initialCreds) + const initialStats = await fs.stat(getCredentialsFilename()) + const intialContent = await fs.readFileText(getCredentialsFilename()) + const conn = await auth.getConnection({ id: 'profile:default' }) assert.ok(conn?.type === 'iam', 'Expected an IAM connection') assert.deepStrictEqual(await conn.getCredentials(), { @@ -525,27 +528,51 @@ describe('Auth', function () { secretAccessKey: initialCreds.secretKey, sessionToken: undefined, }) - const statBefore = await fs.stat(getCredentialsFilename()) - + const contentBeforeDeleting = await fs.readFileText(getCredentialsFilename()) + const statBeforeDeleting = await fs.stat(getCredentialsFilename()) + console.log('Deleting the file') await fs.delete(getCredentialsFilename()) - console.log('file deleted') const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } + console.log('regenerating the same file') await UserCredentialsUtils.generateCredentialsFile(newCreds) - console.log('file written') - const statAfter = await fs.stat(getCredentialsFilename()) + + const statsAfterRegen = await fs.stat(getCredentialsFilename()) + const contentAfterRegen = await fs.readFileText(getCredentialsFilename()) const credsAreUpdated = (creds: Credentials) => { return creds.accessKeyId === newCreds.accessKey && creds.secretAccessKey === newCreds.secretKey } - console.log('before: %O, after: %O', statBefore, statAfter) - assert.notStrictEqual(statBefore, statAfter, 'Expected credentials file to be updated') + console.log( + 'initial: %O, beforeDelete: %O, afterRegen: %O', + initialStats, + statBeforeDeleting, + statsAfterRegen + ) + + console.log('Before wait until') + const start = Date.now() const credsUpdated = await waitUntil(async () => credsAreUpdated(await conn.getCredentials()), { - timeout: 1000, + timeout: 10000, interval: 100, truthy: true, }) - const statLater = await fs.stat(getCredentialsFilename()) - console.log('before: %O, after: %O, later: %O', statBefore, statAfter, statLater) + console.log('After wait until: %O seconds later', (Date.now() - start) / 1000) + const statAfterWait = await fs.stat(getCredentialsFilename()) + const contentAfterWait = await fs.readFileText(getCredentialsFilename()) + console.log( + 'stats: initial: %O, beforeDelete: %O, afterRegen: %O, afterWait: %O', + initialStats, + statBeforeDeleting, + statsAfterRegen, + statAfterWait + ) + console.log( + 'content: initial: %O, beforeDelete: %O, afterRegen: %O, afterWait: %O', + intialContent, + contentBeforeDeleting, + contentAfterRegen, + contentAfterWait + ) assert.ok(credsUpdated, 'Expected credentials to be updated') }) } From d7499fde1c6d5cb9b5ec2ca75f9d30edd7cb2472 Mon Sep 17 00:00:00 2001 From: hkobew Date: Fri, 15 Nov 2024 10:21:35 -0500 Subject: [PATCH 38/44] reformat logs --- .../core/src/test/credentials/auth.test.ts | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index d1550733c28..494dd3324af 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -508,14 +508,14 @@ describe('Auth', function () { await fs.delete(tmpDir, { recursive: true }) }) - for (const _ of Array.from({ length: 100 }, (i) => i)) { + for (const _ of Array.from({ length: 1000 }, (i) => i)) { it('does not cache if the credentials file changes', async function () { const initialCreds = { profileName: 'default', accessKey: 'x', secretKey: 'x', } - console.log('generating file for the first time') + console.log('Generating credentials file for the first time') await UserCredentialsUtils.generateCredentialsFile(initialCreds) const initialStats = await fs.stat(getCredentialsFilename()) @@ -530,11 +530,11 @@ describe('Auth', function () { }) const contentBeforeDeleting = await fs.readFileText(getCredentialsFilename()) const statBeforeDeleting = await fs.stat(getCredentialsFilename()) - console.log('Deleting the file') + console.log('Deleting the credentials file') await fs.delete(getCredentialsFilename()) const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } - console.log('regenerating the same file') + console.log('Regenerating credentials file') await UserCredentialsUtils.generateCredentialsFile(newCreds) const statsAfterRegen = await fs.stat(getCredentialsFilename()) @@ -542,14 +542,8 @@ describe('Auth', function () { const credsAreUpdated = (creds: Credentials) => { return creds.accessKeyId === newCreds.accessKey && creds.secretAccessKey === newCreds.secretKey } - console.log( - 'initial: %O, beforeDelete: %O, afterRegen: %O', - initialStats, - statBeforeDeleting, - statsAfterRegen - ) - console.log('Before wait until') + console.log('Before waitUntil block') const start = Date.now() const credsUpdated = await waitUntil(async () => credsAreUpdated(await conn.getCredentials()), { timeout: 10000, @@ -560,19 +554,23 @@ describe('Auth', function () { const statAfterWait = await fs.stat(getCredentialsFilename()) const contentAfterWait = await fs.readFileText(getCredentialsFilename()) console.log( - 'stats: initial: %O, beforeDelete: %O, afterRegen: %O, afterWait: %O', + 'stats:\n --initial: %O \n, --beforeDelete: %O \n, --afterRegen: %O \n, --afterWait: %O \n', initialStats, statBeforeDeleting, statsAfterRegen, statAfterWait ) console.log( - 'content: initial: %O, beforeDelete: %O, afterRegen: %O, afterWait: %O', + 'content:\n --initial: %O \n, --beforeDelete: %O \n, --afterRegen: %O \n, --afterWait: %O \n', intialContent, contentBeforeDeleting, contentAfterRegen, contentAfterWait ) + if (!credsUpdated) { + console.log('creds failed to update!') + console.log('creds: %O', await conn.getCredentials()) + } assert.ok(credsUpdated, 'Expected credentials to be updated') }) } From ce87909798a8c458477c477bb70d6540b3b224fa Mon Sep 17 00:00:00 2001 From: hkobew Date: Fri, 15 Nov 2024 13:23:38 -0500 Subject: [PATCH 39/44] try sleeping in between regen --- packages/core/src/test/credentials/auth.test.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 494dd3324af..8e41d76972a 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -20,7 +20,8 @@ import { getCredentialsFilename } from '../../auth/credentials/sharedCredentials import { Connection, isIamConnection, isSsoConnection, scopesSsoAccountAccess } from '../../auth/connection' import { AuthNode, createDeleteConnectionButton, promptForConnection } from '../../auth/utils' import { Credentials } from '@aws-sdk/types' -import { waitUntil } from '../../shared' +import { sleep, waitUntil } from '../../shared' +import { isWin } from '../../shared/vscode/env' const ssoProfile = createSsoProfile() const scopedSsoProfile = createSsoProfile({ scopes: ['foo'] }) @@ -533,10 +534,20 @@ describe('Auth', function () { console.log('Deleting the credentials file') await fs.delete(getCredentialsFilename()) + console.log('sleeping first time for a second') + if (isWin()) { + await sleep(1000) + } + const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } console.log('Regenerating credentials file') await UserCredentialsUtils.generateCredentialsFile(newCreds) + console.log('sleeping second time for a second') + if (isWin()) { + await sleep(1000) + } + const statsAfterRegen = await fs.stat(getCredentialsFilename()) const contentAfterRegen = await fs.readFileText(getCredentialsFilename()) const credsAreUpdated = (creds: Credentials) => { From 3cbf39a0bd24ed814b222d483f7c22a59eb531f8 Mon Sep 17 00:00:00 2001 From: hkobew Date: Fri, 15 Nov 2024 14:07:09 -0500 Subject: [PATCH 40/44] remove logging --- .../sharedCredentialsProviderFactory.ts | 38 +------ .../core/src/test/credentials/auth.test.ts | 104 +++++------------- 2 files changed, 27 insertions(+), 115 deletions(-) diff --git a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts index 41cc3465516..4864a332db0 100644 --- a/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts +++ b/packages/core/src/auth/providers/sharedCredentialsProviderFactory.ts @@ -3,47 +3,23 @@ * SPDX-License-Identifier: Apache-2.0 */ -import fs from '../../shared/fs/fs' import { getLogger, Logger } from '../../shared/logger' import { loadSharedCredentialsSections, updateAwsSdkLoadConfigEnvVar } from '../credentials/sharedCredentials' import { CredentialsProviderType } from './credentials' import { BaseCredentialsProviderFactory } from './credentialsProviderFactory' import { SharedCredentialsProvider } from './sharedCredentialsProvider' -import { getCredentialsFilename, getConfigFilename } from '../credentials/sharedCredentialsFile' export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFactory { private readonly logger: Logger = getLogger() - private loadedCredentialsModificationMillis?: number - private loadedConfigModificationMillis?: number - public async refresh(): Promise { - if (await this.needsRefresh()) { - await this.loadSharedCredentialsProviders() - } + await this.loadSharedCredentialsProviders() } public override getProviderType(): CredentialsProviderType | undefined { return SharedCredentialsProvider.getProviderType() } - protected override resetProviders() { - this.loadedCredentialsModificationMillis = undefined - this.loadedConfigModificationMillis = undefined - - super.resetProviders() - } - - private async needsRefresh(): Promise { - const credentialsLastModMillis = await this.getLastModifiedMillis(getCredentialsFilename()) - const configLastModMillis = await this.getLastModifiedMillis(getConfigFilename()) - - return ( - this.loadedCredentialsModificationMillis !== credentialsLastModMillis || - this.loadedConfigModificationMillis !== configLastModMillis - ) - } - private async loadSharedCredentialsProviders(): Promise { this.resetProviders() @@ -52,9 +28,6 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac const errors = result.errors.map((e) => e.message).join('\t\n') getLogger().warn(`credentials: errors while parsing:\n%s`, errors) } - - this.loadedCredentialsModificationMillis = await this.getLastModifiedMillis(getCredentialsFilename()) - this.loadedConfigModificationMillis = await this.getLastModifiedMillis(getConfigFilename()) await updateAwsSdkLoadConfigEnvVar() getLogger().verbose( @@ -79,13 +52,4 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac this.addProvider(provider) } } - - private async getLastModifiedMillis(filepath: string): Promise { - try { - const stat = await fs.stat(filepath) - return stat.mtime - } catch (err) { - return undefined - } - } } diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 8e41d76972a..022e2c5c6e7 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -19,9 +19,6 @@ import { UserCredentialsUtils } from '../../shared/credentials/userCredentialsUt import { getCredentialsFilename } from '../../auth/credentials/sharedCredentialsFile' import { Connection, isIamConnection, isSsoConnection, scopesSsoAccountAccess } from '../../auth/connection' import { AuthNode, createDeleteConnectionButton, promptForConnection } from '../../auth/utils' -import { Credentials } from '@aws-sdk/types' -import { sleep, waitUntil } from '../../shared' -import { isWin } from '../../shared/vscode/env' const ssoProfile = createSsoProfile() const scopedSsoProfile = createSsoProfile({ scopes: ['foo'] }) @@ -509,82 +506,33 @@ describe('Auth', function () { await fs.delete(tmpDir, { recursive: true }) }) - for (const _ of Array.from({ length: 1000 }, (i) => i)) { - it('does not cache if the credentials file changes', async function () { - const initialCreds = { - profileName: 'default', - accessKey: 'x', - secretKey: 'x', - } - console.log('Generating credentials file for the first time') - await UserCredentialsUtils.generateCredentialsFile(initialCreds) - - const initialStats = await fs.stat(getCredentialsFilename()) - const intialContent = await fs.readFileText(getCredentialsFilename()) - - const conn = await auth.getConnection({ id: 'profile:default' }) - assert.ok(conn?.type === 'iam', 'Expected an IAM connection') - assert.deepStrictEqual(await conn.getCredentials(), { - accessKeyId: initialCreds.accessKey, - secretAccessKey: initialCreds.secretKey, - sessionToken: undefined, - }) - const contentBeforeDeleting = await fs.readFileText(getCredentialsFilename()) - const statBeforeDeleting = await fs.stat(getCredentialsFilename()) - console.log('Deleting the credentials file') - await fs.delete(getCredentialsFilename()) - - console.log('sleeping first time for a second') - if (isWin()) { - await sleep(1000) - } - - const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } - console.log('Regenerating credentials file') - await UserCredentialsUtils.generateCredentialsFile(newCreds) - - console.log('sleeping second time for a second') - if (isWin()) { - await sleep(1000) - } - - const statsAfterRegen = await fs.stat(getCredentialsFilename()) - const contentAfterRegen = await fs.readFileText(getCredentialsFilename()) - const credsAreUpdated = (creds: Credentials) => { - return creds.accessKeyId === newCreds.accessKey && creds.secretAccessKey === newCreds.secretKey - } - - console.log('Before waitUntil block') - const start = Date.now() - const credsUpdated = await waitUntil(async () => credsAreUpdated(await conn.getCredentials()), { - timeout: 10000, - interval: 100, - truthy: true, - }) - console.log('After wait until: %O seconds later', (Date.now() - start) / 1000) - const statAfterWait = await fs.stat(getCredentialsFilename()) - const contentAfterWait = await fs.readFileText(getCredentialsFilename()) - console.log( - 'stats:\n --initial: %O \n, --beforeDelete: %O \n, --afterRegen: %O \n, --afterWait: %O \n', - initialStats, - statBeforeDeleting, - statsAfterRegen, - statAfterWait - ) - console.log( - 'content:\n --initial: %O \n, --beforeDelete: %O \n, --afterRegen: %O \n, --afterWait: %O \n', - intialContent, - contentBeforeDeleting, - contentAfterRegen, - contentAfterWait - ) - if (!credsUpdated) { - console.log('creds failed to update!') - console.log('creds: %O', await conn.getCredentials()) - } - assert.ok(credsUpdated, 'Expected credentials to be updated') + it('does not cache if the credentials file changes', async function () { + const initialCreds = { + profileName: 'default', + accessKey: 'x', + secretKey: 'x', + } + + await UserCredentialsUtils.generateCredentialsFile(initialCreds) + + const conn = await auth.getConnection({ id: 'profile:default' }) + assert.ok(conn?.type === 'iam', 'Expected an IAM connection') + assert.deepStrictEqual(await conn.getCredentials(), { + accessKeyId: initialCreds.accessKey, + secretAccessKey: initialCreds.secretKey, + sessionToken: undefined, }) - } + + await fs.delete(getCredentialsFilename()) + + const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } + await UserCredentialsUtils.generateCredentialsFile(newCreds) + assert.deepStrictEqual(await conn.getCredentials(), { + accessKeyId: newCreds.accessKey, + secretAccessKey: newCreds.secretKey, + sessionToken: undefined, + }) + }) }) describe('AuthNode', function () { From 87fbc01bdd96c389c8f4f2e3237437b432590a1e Mon Sep 17 00:00:00 2001 From: hkobew Date: Fri, 15 Nov 2024 14:09:10 -0500 Subject: [PATCH 41/44] focus on individual test --- .../core/src/test/credentials/auth.test.ts | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 022e2c5c6e7..921b76f408c 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -505,34 +505,35 @@ describe('Auth', function () { sinon.restore() await fs.delete(tmpDir, { recursive: true }) }) + for (const _ of Array.from({ length: 1000 }, (i) => i)) { + it('does not cache if the credentials file changes', async function () { + const initialCreds = { + profileName: 'default', + accessKey: 'x', + secretKey: 'x', + } + + await UserCredentialsUtils.generateCredentialsFile(initialCreds) + + const conn = await auth.getConnection({ id: 'profile:default' }) + assert.ok(conn?.type === 'iam', 'Expected an IAM connection') + assert.deepStrictEqual(await conn.getCredentials(), { + accessKeyId: initialCreds.accessKey, + secretAccessKey: initialCreds.secretKey, + sessionToken: undefined, + }) - it('does not cache if the credentials file changes', async function () { - const initialCreds = { - profileName: 'default', - accessKey: 'x', - secretKey: 'x', - } - - await UserCredentialsUtils.generateCredentialsFile(initialCreds) - - const conn = await auth.getConnection({ id: 'profile:default' }) - assert.ok(conn?.type === 'iam', 'Expected an IAM connection') - assert.deepStrictEqual(await conn.getCredentials(), { - accessKeyId: initialCreds.accessKey, - secretAccessKey: initialCreds.secretKey, - sessionToken: undefined, - }) - - await fs.delete(getCredentialsFilename()) + await fs.delete(getCredentialsFilename()) - const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } - await UserCredentialsUtils.generateCredentialsFile(newCreds) - assert.deepStrictEqual(await conn.getCredentials(), { - accessKeyId: newCreds.accessKey, - secretAccessKey: newCreds.secretKey, - sessionToken: undefined, + const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } + await UserCredentialsUtils.generateCredentialsFile(newCreds) + assert.deepStrictEqual(await conn.getCredentials(), { + accessKeyId: newCreds.accessKey, + secretAccessKey: newCreds.secretKey, + sessionToken: undefined, + }) }) - }) + } }) describe('AuthNode', function () { From 92c81382d516d6c421f48af0104b6ed90ecdc578 Mon Sep 17 00:00:00 2001 From: hkobew Date: Fri, 15 Nov 2024 14:19:59 -0500 Subject: [PATCH 42/44] adapt other test --- .../provider/sharedCredentialsProviderFactory.test.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/core/src/test/credentials/provider/sharedCredentialsProviderFactory.test.ts b/packages/core/src/test/credentials/provider/sharedCredentialsProviderFactory.test.ts index 09d210f18b2..c9e96fccc0d 100644 --- a/packages/core/src/test/credentials/provider/sharedCredentialsProviderFactory.test.ts +++ b/packages/core/src/test/credentials/provider/sharedCredentialsProviderFactory.test.ts @@ -120,7 +120,7 @@ describe('SharedCredentialsProviderFactory', async function () { ) }) - it('refresh does not reload from file if the file has not changed', async function () { + it('refresh does reload from file even if the file has changed', async function () { const sut = new SharedCredentialsProviderFactory() // First load @@ -129,10 +129,7 @@ describe('SharedCredentialsProviderFactory', async function () { // Expect: No reload await sut.refresh() - assert.ok( - loadSharedCredentialsSectionsStub.calledOnce, - 'Credentials should have only been loaded from disk once' - ) + assert.ok(loadSharedCredentialsSectionsStub.calledTwice, 'Credentials should have loaded from disk twice') }) it('refresh reloads from file if the file has changed', async function () { From dc8a7de1371da0773738ed2fb8ec667f30ce57db Mon Sep 17 00:00:00 2001 From: hkobew Date: Fri, 15 Nov 2024 14:36:32 -0500 Subject: [PATCH 43/44] change test wording --- .../provider/sharedCredentialsProviderFactory.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/test/credentials/provider/sharedCredentialsProviderFactory.test.ts b/packages/core/src/test/credentials/provider/sharedCredentialsProviderFactory.test.ts index c9e96fccc0d..84f2d378e46 100644 --- a/packages/core/src/test/credentials/provider/sharedCredentialsProviderFactory.test.ts +++ b/packages/core/src/test/credentials/provider/sharedCredentialsProviderFactory.test.ts @@ -120,7 +120,7 @@ describe('SharedCredentialsProviderFactory', async function () { ) }) - it('refresh does reload from file even if the file has changed', async function () { + it('refresh always reloads from file', async function () { const sut = new SharedCredentialsProviderFactory() // First load From 29b31a2d728fee32128d1459b5041c2ab99afcd3 Mon Sep 17 00:00:00 2001 From: hkobew Date: Fri, 15 Nov 2024 14:37:23 -0500 Subject: [PATCH 44/44] remove loop on test --- .../core/src/test/credentials/auth.test.ts | 51 +++++++++---------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/packages/core/src/test/credentials/auth.test.ts b/packages/core/src/test/credentials/auth.test.ts index 921b76f408c..022e2c5c6e7 100644 --- a/packages/core/src/test/credentials/auth.test.ts +++ b/packages/core/src/test/credentials/auth.test.ts @@ -505,35 +505,34 @@ describe('Auth', function () { sinon.restore() await fs.delete(tmpDir, { recursive: true }) }) - for (const _ of Array.from({ length: 1000 }, (i) => i)) { - it('does not cache if the credentials file changes', async function () { - const initialCreds = { - profileName: 'default', - accessKey: 'x', - secretKey: 'x', - } - - await UserCredentialsUtils.generateCredentialsFile(initialCreds) - - const conn = await auth.getConnection({ id: 'profile:default' }) - assert.ok(conn?.type === 'iam', 'Expected an IAM connection') - assert.deepStrictEqual(await conn.getCredentials(), { - accessKeyId: initialCreds.accessKey, - secretAccessKey: initialCreds.secretKey, - sessionToken: undefined, - }) - await fs.delete(getCredentialsFilename()) + it('does not cache if the credentials file changes', async function () { + const initialCreds = { + profileName: 'default', + accessKey: 'x', + secretKey: 'x', + } + + await UserCredentialsUtils.generateCredentialsFile(initialCreds) + + const conn = await auth.getConnection({ id: 'profile:default' }) + assert.ok(conn?.type === 'iam', 'Expected an IAM connection') + assert.deepStrictEqual(await conn.getCredentials(), { + accessKeyId: initialCreds.accessKey, + secretAccessKey: initialCreds.secretKey, + sessionToken: undefined, + }) - const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } - await UserCredentialsUtils.generateCredentialsFile(newCreds) - assert.deepStrictEqual(await conn.getCredentials(), { - accessKeyId: newCreds.accessKey, - secretAccessKey: newCreds.secretKey, - sessionToken: undefined, - }) + await fs.delete(getCredentialsFilename()) + + const newCreds = { ...initialCreds, accessKey: 'y', secretKey: 'y' } + await UserCredentialsUtils.generateCredentialsFile(newCreds) + assert.deepStrictEqual(await conn.getCredentials(), { + accessKeyId: newCreds.accessKey, + secretAccessKey: newCreds.secretKey, + sessionToken: undefined, }) - } + }) }) describe('AuthNode', function () {