-
Notifications
You must be signed in to change notification settings - Fork 48
Add an Azure Resources API (v4) authentication layer #1284
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
39
commits into
main
Choose a base branch
from
mwf/v4
base: main
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.
+345
−17
Open
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
09b7250
Add changes for resources-api
MicroFish91 37147d3
Add changes to Azure Resources
MicroFish91 9920f41
Add todo
MicroFish91 611ab51
Some nits
MicroFish91 2f5d146
Misc changes
MicroFish91 bb212ab
Update error messages and readme
MicroFish91 a8d2e09
More updates to readme
MicroFish91 39e1b69
Update getAzureResourcesApi function signature
MicroFish91 18dce26
Void
MicroFish91 7d1dfdc
Clean up names some more
MicroFish91 f45cf0d
More updates to documentation
MicroFish91 3e80d80
Add logic for allowed extension ids
MicroFish91 183e4dc
Add important note
MicroFish91 79b7421
Use a set
MicroFish91 7dc9819
Simplify auth readme
MicroFish91 e1caffc
Revert package.json and package-lock.json
MicroFish91 1f5b406
Update api type def file
MicroFish91 b9af84e
Add azure tools publisher constant
MicroFish91 8079638
Update types, errors, and comments
MicroFish91 dbba10e
Update typings
MicroFish91 29e7976
Update comments
MicroFish91 fa07ab1
Use an enum for error codes
MicroFish91 4e9e686
Update readme and comments
MicroFish91 cb7b973
Update a comment
MicroFish91 03da414
Update enum formatting
MicroFish91 11d1ffa
Fix a typo
MicroFish91 4140cbf
Change comment casing
MicroFish91 d178910
Update file structure, add capability for custom dependency injection
MicroFish91 a816d08
Remove a comment
MicroFish91 9ab914a
Fix some variable names
MicroFish91 0e438fa
semicolons
MicroFish91 1c6db3b
Move client tools changes
MicroFish91 5a1ad77
Add back in some missing files that were moved
MicroFish91 bf36371
Merge with main
MicroFish91 2580b09
Remove a var
MicroFish91 c9ce482
Semicolon
MicroFish91 54299f9
Preserve merge formatting
MicroFish91 16ba61a
Fix lint warning
MicroFish91 08b7d94
Revert unnecessary formats
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| export interface AzExtCredentialManager { | ||
| createCredential(extensionId: string): string | Promise<string>; | ||
| verifyCredential(credential: string, extensionId?: string): boolean | Promise<boolean>; | ||
|
|
||
| /** | ||
| * Masks sensitive information from a given string to ensure private credential management keys from the manager are not exposed. | ||
| * @param data - The string to be processed. | ||
| * @returns The same string stripped of any sensitive credentials. | ||
| */ | ||
| maskCredentials(data: string): string; | ||
| } |
39 changes: 39 additions & 0 deletions
39
api/src/auth/credentialManager/AzExtUUIDCredentialManager.ts
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,39 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as crypto from "crypto"; | ||
| import { maskValue } from "../../utils/maskValue"; | ||
| import { AzExtCredentialManager } from "./AzExtCredentialManager"; | ||
|
|
||
| /** | ||
| * A simple, light-weight credential manager that issues and tracks randomly generated UUIDs for extension verification. | ||
| */ | ||
| export class AzExtUUIDCredentialManager implements AzExtCredentialManager { | ||
| #uuidMap: Map<string, string>; | ||
|
|
||
| constructor() { | ||
| this.#uuidMap = new Map(); | ||
| } | ||
|
|
||
| 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
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,15 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| // Sourced from @microsoft/vscode-azext-utils | ||
| export function maskValue(data: string, valueToMask: string | undefined): string { | ||
| if (valueToMask) { | ||
| const formsOfValue: string[] = [valueToMask, encodeURIComponent(valueToMask)]; | ||
| for (const v of formsOfValue) { | ||
| data = data.replace(new RegExp(escape(v), 'gi'), '---'); | ||
| } | ||
| } | ||
| return data; | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/api/auth/createApiSession/CreateApiSessionInternalContext.ts
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,23 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { IActionContext } from "@microsoft/vscode-azext-utils"; | ||
| import { AzExtCredentialManager } from "api/src/auth/credentialManager/AzExtCredentialManager"; | ||
| import { AzureExtensionApi } from "api/src/utils/apiUtils"; | ||
|
|
||
| export interface CreateApiSessionInternalContext extends IActionContext { | ||
| credentialManager: AzExtCredentialManager; | ||
| clientExtensionId: string; | ||
| clientExtensionVersion: string; | ||
| clientExtensionCredential: string; | ||
|
|
||
| /** | ||
| * An optional API provider to be used in lieu of the VS Code API `vscode.extension.getExtension()`. | ||
| * This should _NOT_ be defined in production environments. | ||
| */ | ||
| clientApiProvider?: CreateApiSessionExtensionProvider; | ||
| } | ||
|
|
||
| export type CreateApiSessionExtensionProvider = { getApi(clientExtensionId: string, clientExtensionVersion: string): AzureExtensionApi }; |
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,43 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { IParsedError, maskUserInfo, parseError } from "@microsoft/vscode-azext-utils"; | ||
| import { apiUtils } from "../../../../api/src/utils/apiUtils"; | ||
| import { azureExtensions } from "../../../azureExtensions"; | ||
| import { ext } from '../../../extensionVariables'; | ||
| import { localize } from "../../../utils/localize"; | ||
| import { CreateApiSessionInternalContext } from './CreateApiSessionInternalContext'; | ||
|
|
||
| const allowedExtensionIds = new Set(azureExtensions.map(extension => `${extension.publisher.toLowerCase()}.${extension.name.toLowerCase()}`)); | ||
|
|
||
| export async function createApiSessionInternal(context: CreateApiSessionInternalContext): Promise<void> { | ||
| context.telemetry.properties.clientExtensionId = context.clientExtensionId; | ||
| context.telemetry.properties.clientExtensionVersion = context.clientExtensionVersion; | ||
|
|
||
| if (!allowedExtensionIds.has(context.clientExtensionId)) { | ||
| context.telemetry.properties.allowedExtension = 'false'; | ||
| ext.outputChannel.warn(localize('createResourcesApiSession.denied', 'Azure Resources API session denied for extension "{0}".', context.clientExtensionId)); | ||
| throw new Error('🧙 No, thank you! We don\'t want any more visitors, well-wishers, or distant relations! 🧝🦶'); | ||
| } | ||
|
|
||
| context.telemetry.properties.allowedExtension = 'true'; | ||
|
|
||
| try { | ||
| const clientApi = context.clientApiProvider?.getApi(context.clientExtensionId, context.clientExtensionVersion) ?? | ||
| await apiUtils.getAzureExtensionApi(ext.context, context.clientExtensionId, context.clientExtensionVersion); | ||
|
|
||
| const azureResourcesCredential: string = await context.credentialManager.createCredential(context.clientExtensionId); | ||
| await clientApi.receiveAzureResourcesApiSession?.(azureResourcesCredential, context.clientExtensionCredential); | ||
| } catch (err) { | ||
| const failed: string = localize('createResourcesApiSession.failed', 'Failed to create Azure Resources API session for extension "{0}".', context.clientExtensionId); | ||
| ext.outputChannel.error(failed); | ||
|
|
||
| const perr: IParsedError = parseError(err); | ||
| const perrMessage: string = context.credentialManager.maskCredentials(perr.message); | ||
| context.telemetry.properties.createResourcesApiSessionError = maskUserInfo(perrMessage, []); | ||
| ext.outputChannel.error(perrMessage); | ||
| throw new Error(failed); | ||
| } | ||
| } |
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,93 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { apiUtils, AzureExtensionApiFactory, callWithTelemetryAndErrorHandling, GetApiOptions, IActionContext } from '@microsoft/vscode-azext-utils'; | ||
| import { AzExtCredentialManager } from '../../../api/src/auth/credentialManager/AzExtCredentialManager'; | ||
| import { AzExtUUIDCredentialManager } from '../../../api/src/auth/credentialManager/AzExtUUIDCredentialManager'; | ||
| import { AzureResourcesAuthApiInternal } from '../../hostapi.v4.internal'; | ||
| import { createApiSessionInternal } from './createApiSession/createApiSessionInternal'; | ||
| import { CreateApiSessionExtensionProvider, CreateApiSessionInternalContext } from './createApiSession/CreateApiSessionInternalContext'; | ||
| import { getApiVerifyError, verifyApiSessionInternal } from './verifyApiSession/verifyApiSessionInternal'; | ||
| import { VerifyApiSessionInternalContext } from './verifyApiSession/VerifyApiSessionInternalContext'; | ||
|
|
||
| const v4: string = '4.0.0'; | ||
|
|
||
| export type AuthApiFactoryDependencies = { | ||
| /** | ||
| * An optional credential manager used for issuing and verifying Azure Resources API credentials. If none are supplied, a simple UUID credential manager will be used. | ||
| * @test Use this to more easily mock and inspect the behavior of the underlying credential manager. | ||
| */ | ||
| credentialManager?: AzExtCredentialManager; | ||
| /** | ||
| * An optional API provider to be used in lieu of the VS Code extension provider `vscode.extension.getExtension()`. | ||
| * This should _NOT_ be defined in production environments. | ||
| * @test Use this to more easily mock and inject custom client extension API exports. | ||
| */ | ||
| clientApiProvider?: CreateApiSessionExtensionProvider; | ||
| } | ||
|
|
||
| export function createAuthApiFactory(coreApiProvider: apiUtils.AzureExtensionApiProvider, customDependencies?: AuthApiFactoryDependencies): AzureExtensionApiFactory<AzureResourcesAuthApiInternal> { | ||
| const credentialManager = customDependencies?.credentialManager ?? new AzExtUUIDCredentialManager(); | ||
| const clientApiProvider = customDependencies?.clientApiProvider; | ||
|
|
||
| return { | ||
| apiVersion: v4, | ||
| createApi: (options?: GetApiOptions) => { | ||
| return { | ||
| apiVersion: v4, | ||
|
|
||
| getAzureResourcesApis: async (clientExtensionId: string, azureResourcesCredential: string, azureResourcesApiVersions: string[]) => { | ||
| return await callWithTelemetryAndErrorHandling('api.getAzureResourcesApis', async (context: IActionContext) => { | ||
| setTelemetryAndErrorHandling(context, options?.extensionId); | ||
|
|
||
| const verified: boolean = await verifyApiSessionInternal(Object.assign(context, { | ||
| credentialManager, | ||
| clientExtensionId: clientExtensionId?.toLowerCase(), | ||
| azureResourcesCredential, | ||
| }) satisfies VerifyApiSessionInternalContext); | ||
|
|
||
| if (!verified) { | ||
| throw new Error(getApiVerifyError(clientExtensionId)); | ||
| } | ||
|
|
||
| return azureResourcesApiVersions | ||
| .map((apiVersion) => { | ||
| try { | ||
| return coreApiProvider.getApi(apiVersion, options); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| }); | ||
|
|
||
| }) ?? []; | ||
| }, | ||
|
|
||
| createAzureResourcesApiSession: async (clientExtensionId: string, clientExtensionVersion: string, clientExtensionCredential: string) => { | ||
| return await callWithTelemetryAndErrorHandling('api.createAzureResourcesApiSession', async (context: IActionContext) => { | ||
| setTelemetryAndErrorHandling(context, options?.extensionId); | ||
|
|
||
| return await createApiSessionInternal(Object.assign(context, { | ||
| credentialManager, | ||
| clientExtensionId: clientExtensionId?.toLowerCase(), | ||
| clientExtensionVersion, | ||
| clientExtensionCredential, | ||
| clientApiProvider, | ||
| }) satisfies CreateApiSessionInternalContext); | ||
| }); | ||
| }, | ||
|
|
||
| }; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function setTelemetryAndErrorHandling(context: IActionContext, extensionId?: string): void { | ||
| context.telemetry.properties.callingExtensionId = extensionId; | ||
| context.telemetry.properties.isActivationEvent = 'true'; | ||
| context.telemetry.properties.apiVersion = v4; | ||
| context.errorHandling.rethrow = true; | ||
| context.errorHandling.suppressDisplay = true; | ||
| context.errorHandling.suppressReportIssue = true; | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/api/auth/verifyApiSession/VerifyApiSessionInternalContext.ts
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,13 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { IActionContext } from "@microsoft/vscode-azext-utils"; | ||
| import { AzExtCredentialManager } from "api/src/auth/credentialManager/AzExtCredentialManager"; | ||
|
|
||
| export interface VerifyApiSessionInternalContext extends IActionContext { | ||
| credentialManager: AzExtCredentialManager; | ||
| clientExtensionId: string; | ||
| azureResourcesCredential: string; | ||
| } |
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,34 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { l10n } from 'vscode'; | ||
| import { ext } from '../../../extensionVariables'; | ||
| import { VerifyApiSessionInternalContext } from './VerifyApiSessionInternalContext'; | ||
|
|
||
| export const getApiVerifyError = (clientExtensionId?: string) => `${clientExtensionId || 'Unknown Extension'} - 🧙 YOU SHALL NOT PASS! 🔥`; | ||
|
|
||
| export async function verifyApiSessionInternal(context: VerifyApiSessionInternalContext): Promise<boolean> { | ||
| const apiVerifyError: string = getApiVerifyError(context.clientExtensionId); | ||
|
|
||
| if (!context.clientExtensionId || !context.azureResourcesCredential) { | ||
| context.telemetry.properties.deniedReason = 'missingDetails'; | ||
| throw new Error(apiVerifyError); | ||
| } | ||
|
|
||
| context.telemetry.properties.clientExtensionId = context.clientExtensionId; | ||
|
|
||
| let verified: boolean | undefined; | ||
| try { | ||
| verified = await context.credentialManager.verifyCredential(context.azureResourcesCredential, context.clientExtensionId); | ||
| } catch { /** Skip; handle below */ } | ||
|
|
||
| if (!verified) { | ||
| context.telemetry.properties.deniedReason = 'failedVerification'; | ||
| ext.outputChannel.warn(l10n.t('Extension claiming to be "{0}" provided an access token that failed verification.', context.clientExtensionId)); | ||
| throw new Error(apiVerifyError); | ||
| } | ||
|
|
||
| return verified; | ||
| } |
Oops, something went wrong.
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.
Added this type in preparation to leverage dependency injection during tests. It probably belongs in the follow-up test PR, but I'm hoping to not have to extract and re-add it again 😅