From 3ac2cfc8ad4927702a556a1f651c150e23cc7d07 Mon Sep 17 00:00:00 2001 From: Aayush Attri <31190113+attriaayush@users.noreply.github.com> Date: Fri, 29 Apr 2022 16:43:24 +0100 Subject: [PATCH] fix: token error (#216) * fix: token retrieval error handling * fix: release stable --- .github/workflows/release-stable.yaml | 3 +++ .../common/configuration/configuration.ts | 25 +++++++++++++------ src/test/unit/common/configuration.test.ts | 16 ++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/.github/workflows/release-stable.yaml b/.github/workflows/release-stable.yaml index a2ecaa389..130df4b4b 100644 --- a/.github/workflows/release-stable.yaml +++ b/.github/workflows/release-stable.yaml @@ -46,6 +46,9 @@ jobs: environment: Stable name: Release steps: + - name: Fetch sources + uses: actions/checkout@v2 + - name: Setup VSCE run: sudo npm install -g vsce@latest diff --git a/src/snyk/common/configuration/configuration.ts b/src/snyk/common/configuration/configuration.ts index 34ece2fc4..efc08b678 100644 --- a/src/snyk/common/configuration/configuration.ts +++ b/src/snyk/common/configuration/configuration.ts @@ -167,12 +167,16 @@ export class Configuration implements IConfiguration { } async getToken(): Promise { - try { - return SecretStorageAdapter.instance.get(SNYK_TOKEN_KEY); - } catch (e) { - // if the token cannot be parsed then clear the token - await this.clearToken(); - } + return new Promise(resolve => { + SecretStorageAdapter.instance + .get(SNYK_TOKEN_KEY) + .then(token => resolve(token)) + .catch(async _ => { + // clear the token and return empty string + await this.clearToken(); + resolve(''); + }); + }); } get snykCodeToken(): Promise { @@ -188,7 +192,14 @@ export class Configuration implements IConfiguration { } async clearToken(): Promise { - return await SecretStorageAdapter.instance.delete(SNYK_TOKEN_KEY); + return new Promise((resolve, reject) => { + SecretStorageAdapter.instance + .delete(SNYK_TOKEN_KEY) + .then(() => resolve()) + .catch(error => { + reject(error); + }); + }); } static get source(): string { diff --git a/src/test/unit/common/configuration.test.ts b/src/test/unit/common/configuration.test.ts index 5aa78192c..85a9e062a 100644 --- a/src/test/unit/common/configuration.test.ts +++ b/src/test/unit/common/configuration.test.ts @@ -23,6 +23,7 @@ suite('Configuration', () => { secrets: { store: (_key: string, _value: string) => Promise.resolve(), get: () => Promise.resolve(), + delete: () => Promise.resolve(), }, } as unknown as ExtensionContext; SecretStorageAdapter.init(extensionContext); @@ -124,6 +125,21 @@ suite('Configuration', () => { strictEqual(secretStorageGetStub.calledOnce, true); }); + test('Snyk Code: token should be cleared if the retrieval method throws', async () => { + const token = 'snyk-token'; + + sinon.stub(extensionContext.secrets, 'store').resolves(); + const secretStorageDeleteStub = sinon.stub(extensionContext.secrets, 'delete').resolves(); + const secretStorageGetStub = sinon.stub(extensionContext.secrets, 'get').rejects('cannot get token'); + + const configuration = new Configuration(process.env, workspaceStub); + await configuration.setToken(token); + + strictEqual(await configuration.snykCodeToken, ''); + strictEqual(secretStorageGetStub.calledOnce, true); + strictEqual(secretStorageDeleteStub.calledOnce, true); + }); + test('Snyk Code: token returns Snyk Code token when in development', async () => { const token = 'test-token'; const snykCodeToken = 'snykCode-token';