-
Notifications
You must be signed in to change notification settings - Fork 47
Azure Resources API (v4) internal tests #1288
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
Open
MicroFish91
wants to merge
31
commits into
mwf/v4-client-tools
Choose a base branch
from
mwf/v4-tests
base: mwf/v4-client-tools
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
0b31112
WIP
MicroFish91 71f4f14
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 f188eca
Add auth tests and supporting logic
MicroFish91 e71785c
Update with better comments
MicroFish91 3f4cb3a
Revert launch change
MicroFish91 34d2f1a
Use extensionId var
MicroFish91 c34d952
Add note
MicroFish91 5b0d57a
Merge with parent branch
MicroFish91 4c49c09
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 0b1d03e
Update mock credential manager
MicroFish91 570ecf4
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 b446718
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 0a7b447
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 3cf4fa3
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 69051c0
Remove part of a comment
MicroFish91 e433035
Merge with main + some new changes
MicroFish91 1718244
Fix formatting
MicroFish91 06e2b99
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 ef7ad23
Match to equal
MicroFish91 97b1239
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 eda6ce6
Escape the symbol
MicroFish91 b5eb5f9
Merge branch 'mwf/v4-client-tools' of https://github.com/microsoft/vs…
MicroFish91 81ce320
Merge with new eng package changes
MicroFish91 5c11b97
Inject more extension vars
MicroFish91 ff9d912
Merge branch 'mwf/v4-client-tools' of https://github.com/microsoft/vs…
MicroFish91 1e9269d
Merge with main + add getUI
MicroFish91 8edafe9
Add type
MicroFish91 0d63c1c
Add an extra test
MicroFish91 56385e1
Remove env var
MicroFish91 1039782
Upgrade utils package
MicroFish91 0aa6f1c
Remove ui extension vars; leverage testGlobalSetup
MicroFish91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { AzExtCredentialManager, maskValue } from "../../../extension.bundle"; | ||
|
|
||
| /** | ||
| * A mock credential manager with the same implementation as `AzExtUUIDCredentialManager`, | ||
| * but with a public getter to inspect the UUIDs during test. | ||
| */ | ||
| export class MockUUIDCredentialManager implements AzExtCredentialManager { | ||
| #uuidMap: Map<string, string> = new Map(); | ||
|
|
||
| get uuidMap() { | ||
| return this.#uuidMap; | ||
| } | ||
|
|
||
| createCredential(extensionId: string): string { | ||
| const uuid: string = crypto.randomUUID(); | ||
| this.#uuidMap.set(extensionId, uuid); | ||
| return uuid; | ||
| } | ||
|
|
||
| verifyCredential(credential: string, extensionId: string): boolean { | ||
| if (!credential || !extensionId) { | ||
| return false; | ||
| } | ||
| return credential === this.#uuidMap.get(extensionId); | ||
| } | ||
|
|
||
| maskCredentials(data: string): string { | ||
| for (const uuid of this.#uuidMap.values()) { | ||
| data = maskValue(data, uuid); | ||
| } | ||
| return data; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { apiUtils, AzureExtensionApi, AzureExtensionApiFactory, createApiProvider, GetApiOptions } from "../../../extension.bundle"; | ||
|
|
||
| /** | ||
| * Creates a mock API provider with API factories matching the versions provided. | ||
| * Only the values required by the interface will be implemented. | ||
| */ | ||
| export function createMockApiProvider(versions: string[]): apiUtils.AzureExtensionApiProvider { | ||
| const apiFactories: AzureExtensionApiFactory<AzureExtensionApi>[] = versions.map(version => { | ||
| return { | ||
| apiVersion: version, | ||
| createApi: (_options?: GetApiOptions) => { | ||
| return { | ||
| apiVersion: version, | ||
| }; | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| return createApiProvider(apiFactories); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as assert from "assert"; | ||
| import { apiUtils, AzExtCredentialManager, AzureResourcesExtensionAuthApi, createAzureResourcesAuthApiFactory, nonNullValue, parseError } from "../../../extension.bundle"; | ||
| import { assertThrowsAsync } from "../../wrapFunctionsInTelemetry.test"; | ||
| import { MockUUIDCredentialManager } from "./MockUUIDCredentialManager"; | ||
| import { createMockApiProvider } from "./mockApiProvider"; | ||
|
|
||
| const extensionId: string = 'ms-azuretools.vscode-azureresourcegroups'; | ||
| const extensionVersion: string = '^4.0.0'; | ||
| const coreApiVersions: string[] = ['0.0.1', '2.0.0', '3.0.0']; | ||
|
|
||
| suite('v4 API auth tests', async () => { | ||
| test('v4 API should be defined', async () => { | ||
| const apiProvider = await apiUtils.getExtensionExports<apiUtils.AzureExtensionApiProvider>(extensionId); | ||
| assert.ok(apiProvider, 'API provider is undefined'); | ||
|
|
||
| const v4Api = apiProvider.getApi(extensionVersion, { extensionId: 'ms-azuretools.vscode-azureresourcegroups-tests' }); | ||
| assert.ok(v4Api); | ||
| }); | ||
|
|
||
| // NOTE: `createAzureResourcesApiSession` is not normally intended to be called directly by Azure Resources itself; however, I've found that it | ||
| // kind of still works for testing. It will basically run everything exactly the same except at the end - the exported API for Azure Resources will be missing | ||
| // the receiver method so the credential has no way to be passed back to the extension through its API. | ||
| // Since we inject and hold a copy of the credential manager during tests, we can simply grab the generated credential from the manager. | ||
| // Client side handshake testing should be done separately to ensure that the receiver method is being called and passed the correct credential. | ||
|
|
||
| test('createAzureResourcesApiSession should provide a credential but not return it directly', async () => { | ||
| const credentialManager = new MockUUIDCredentialManager(); | ||
| const authApi: AzureResourcesExtensionAuthApi = createAuthApi(credentialManager, coreApiVersions); | ||
|
|
||
| const apiSession = await authApi.createAzureResourcesApiSession(extensionId, extensionVersion, crypto.randomUUID()); | ||
| assert.equal(apiSession, undefined); | ||
| assert.ok(credentialManager.uuidMap.get(extensionId)); | ||
| }); | ||
|
|
||
| test('createAzureResourcesApiSession should throw if an unallowed extension id is provided', async () => { | ||
| const credentialManager = new MockUUIDCredentialManager(); | ||
| const authApi: AzureResourcesExtensionAuthApi = createAuthApi(credentialManager, coreApiVersions); | ||
| assertThrowsAsync(async () => await authApi.createAzureResourcesApiSession('extension1', extensionVersion, crypto.randomUUID())) | ||
| }); | ||
|
|
||
| test('createAzureResourcesApiSession should not spill sensitive extension credentials in errors', async () => { | ||
| const credentialManager = new MockUUIDCredentialManager(); | ||
| credentialManager.createCredential('extension1'); | ||
| credentialManager.createCredential = () => { | ||
| throw new Error(credentialManager.uuidMap.get('extension1')); | ||
| } | ||
|
|
||
| const authApi: AzureResourcesExtensionAuthApi = createAuthApi(credentialManager, coreApiVersions); | ||
|
|
||
| try { | ||
| await authApi.createAzureResourcesApiSession(extensionId, extensionVersion, crypto.randomUUID()); | ||
| assert.fail('We expect the credential manager to throw in this test.'); | ||
| } catch (err) { | ||
| const perr = parseError(err); | ||
| assert.doesNotMatch(perr.message, new RegExp(nonNullValue(credentialManager.uuidMap.get('extension1')), 'i')); | ||
| } | ||
| }); | ||
|
|
||
| test('getAzureResourcesApis should return matching APIs if provided a valid credential', async () => { | ||
| const credentialManager = new MockUUIDCredentialManager(); | ||
|
|
||
| const authApi: AzureResourcesExtensionAuthApi = createAuthApi(credentialManager, coreApiVersions); | ||
| await authApi.createAzureResourcesApiSession(extensionId, extensionVersion, crypto.randomUUID()); | ||
|
|
||
| const resourcesApis = await authApi.getAzureResourcesApis(extensionId, nonNullValue(credentialManager.uuidMap.get(extensionId)), ['0.0.1', '^2.0.0']); | ||
| assert.match(resourcesApis[0]?.apiVersion ?? '', /^0.0.1$/); | ||
| assert.match(resourcesApis[1]?.apiVersion ?? '', /^2./); | ||
| }); | ||
|
|
||
| test('getAzureResourcesApis should throw if provided an invalid credential', async () => { | ||
| const credentialManager = new MockUUIDCredentialManager(); | ||
| const coreApiVersions: string[] = ['0.0.1', '2.0.0', '3.0.0']; | ||
| const authApi: AzureResourcesExtensionAuthApi = createAuthApi(credentialManager, coreApiVersions); | ||
| assertThrowsAsync(async () => await authApi.getAzureResourcesApis(extensionId, crypto.randomUUID(), ['^2.0.0'])); | ||
| }); | ||
|
|
||
| test('getAzureResourcesApis should not spill sensitive extension credentials in errors', async () => { | ||
| const credentialManager = new MockUUIDCredentialManager(); | ||
| const authApi: AzureResourcesExtensionAuthApi = createAuthApi(credentialManager, coreApiVersions); | ||
|
|
||
| credentialManager.createCredential('extension1'); | ||
| credentialManager.createCredential('extension2'); | ||
| credentialManager.createCredential('extension3'); | ||
|
|
||
| try { | ||
| await authApi.getAzureResourcesApis(extensionId, crypto.randomUUID(), ['^2.0.0']); | ||
| assert.fail('Should throw if requesting Azure Resources APIs without a valid credential.'); | ||
| } catch (err) { | ||
| const perr = parseError(err); | ||
| for (const credential of credentialManager.uuidMap.values()) { | ||
| assert.doesNotMatch(perr.message, new RegExp(credential, 'i')); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * Use to quickly bootstrap a testable auth API with core API factories matching the provided versions. | ||
| */ | ||
| function createAuthApi(credentialManager: AzExtCredentialManager, coreApiVersions: string[]): AzureResourcesExtensionAuthApi { | ||
| const coreApiProvider = createMockApiProvider(coreApiVersions); | ||
| const authApiProvider = createAzureResourcesAuthApiFactory(credentialManager, coreApiProvider); | ||
| return authApiProvider.createApi({ extensionId: 'ms-azuretools.vscode-azureresourcegroups-tests' }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A less copy and paste mock would be possible if I extended from the
AzExtUUIDCredentialManagerdirectly, but the map would need to be protected not private for me to be able to access it.