Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: token error #216

Merged
merged 2 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/release-stable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm install is missing here

Expand Down
25 changes: 18 additions & 7 deletions src/snyk/common/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,16 @@ export class Configuration implements IConfiguration {
}

async getToken(): Promise<string | undefined> {
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<string | undefined> {
Expand All @@ -188,7 +192,14 @@ export class Configuration implements IConfiguration {
}

async clearToken(): Promise<void> {
return await SecretStorageAdapter.instance.delete(SNYK_TOKEN_KEY);
return new Promise<void>((resolve, reject) => {
SecretStorageAdapter.instance
.delete(SNYK_TOKEN_KEY)
.then(() => resolve())
.catch(error => {
reject(error);
});
});
}

static get source(): string {
Expand Down
16 changes: 16 additions & 0 deletions src/test/unit/common/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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';
Expand Down