-
Notifications
You must be signed in to change notification settings - Fork 47
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
Merged
Merged
Changes from 30 commits
Commits
Show all changes
44 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 6174219
Merge branch 'main' of https://github.com/microsoft/vscode-azureresou…
MicroFish91 6543194
Merge branch 'main' of https://github.com/microsoft/vscode-azureresou…
MicroFish91 4aca3ac
Remove constructor
MicroFish91 0b8d659
Feedback
MicroFish91 7730e58
Update maskValue
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,85 @@ | ||
| # Azure Resources Authentication and API Retrieval | ||
|
|
||
| This guide covers the Azure Resources authentication handshake required for API retrieval by client extensions. It also provides information on the tools available to help client extensions quickly onboard to the new flow. | ||
|
|
||
| ## The Authentication Handshake | ||
|
|
||
| ### Overview | ||
|
|
||
| Azure Resources APIs are protected behind the new v4 authentication layer. This layer exposes two methods that client extensions must use to gain access: `createAzureResourcesApiSession` and `getAzureResourcesApis`. During activation, client extensions are expected to export an API including a receiver method called `receiveAzureResourcesApiSession` before initiating the API request handshake. | ||
|
|
||
| ### Steps | ||
|
|
||
| 1. On activation, the client extension should export its API and initiate the handshake by calling `createAzureResourcesApiSession`. The client extension should provide its own verification credential as part of this request (more on this later). | ||
|
|
||
| 1. The Azure Resources host extension verifies that the requesting extension is on its approved list. If approved, Azure Resources does not respond directly. Instead, it retrieves the extension's API from VS Code directly using the approved extension ID, then delivers the session credential via the `receiveAzureResourcesApiSession` receiver method. This ensures the credential reaches the approved recipient, even if a malicious actor tried to initiate the request. Azure Resources also returns the original client credential so the client extension can verify that it is communicating with the genuine Azure Resources extension. | ||
|
|
||
| 1. The client extension should then use the Azure Resources credential to retrieve the Azure Resources APIs by calling `getAzureResourcesApis`. | ||
|
|
||
| ### Diagram | ||
|  | ||
|
|
||
| ## Automating the Handshake | ||
|
|
||
| To simplify the handshake process, the following tools are made available and outlined below. | ||
|
|
||
| ### The API Request | ||
|
|
||
| Create your extension's API (`AzureExtensionApi`) and pass it along with the requisite request context (`AzureResourcesApiRequestContext`). We'll explore how to populate this context in the section that follows. | ||
|
|
||
| The `prepareAzureResourcesApiRequest` tool that we provide performs two key operations: | ||
|
|
||
| 1. **Prepares client extension API** - Returns your modified client extension API with the required `receiveAzureResourcesApiSession` receiver method added. | ||
| 2. **Provides handshake initializer** - Returns a function that initiates the Resources API request handshake when called. Call this before exporting your API during extension activation. | ||
|
|
||
| ```ts | ||
| const containerAppsApi: AzureExtensionApi = { | ||
| apiVersion: '1.0.0', | ||
| }; | ||
|
|
||
| const { clientApi, requestResourcesApis } = prepareAzureResourcesApiRequest(context, containerAppsApi); | ||
| requestResourcesApis(); | ||
| return createApiProvider([clientApi]); | ||
| ``` | ||
|
|
||
| ### The API Request Context | ||
|
|
||
| The following example shows how to configure the context when preparing for an Azure Resources API handshake request. | ||
|
|
||
| ```ts | ||
| const v2: string = '^2.0.0'; | ||
|
|
||
| const context: AzureResourcesApiRequestContext = { | ||
| azureResourcesApiVersions: [v2], | ||
| clientExtensionId: 'ms-azuretools.vscode-azurecontainerapps', | ||
|
|
||
| // Successful retrieval of Azure Resources APIs will be returned here | ||
| onDidReceiveAzureResourcesApis: (azureResourcesApis: (AzureResourcesExtensionApi | undefined)[]) => { | ||
| const [rgApiV2] = azureResourcesApis; | ||
| if (!rgApiV2) { | ||
| throw new Error(l10n.t('Failed to find a matching Azure Resources API for version "{0}".', v2)); | ||
| } | ||
| ext.rgApiV2 = rgApiV2; | ||
| ext.rgApiV2.resources.registerAzureResourceBranchDataProvider(AzExtResourceType.ContainerAppsEnvironment, ext.branchDataProvider); | ||
| }, | ||
|
|
||
| // OPTIONAL: Can use for special error handling & telemetry | ||
| // NOTE: Errors thrown during execution of this callback may be part of a separate process and may not bubble up to users. | ||
| // If you wish to surface specific errors to users, please consider logging them or using the VS Code API to display them through UI. | ||
| onApiRequestError: async (error: AzureResourcesApiRequestError) => { | ||
| switch (true) { | ||
| case error.code === AzureResourcesApiRequestErrorCode.ClientFailedCreateCredential: | ||
| case error.code === AzureResourcesApiRequestErrorCode.HostCreateSessionFailed: | ||
| case error.code === AzureResourcesApiRequestErrorCode.ClientReceivedInsufficientCredentials: | ||
| case error.code === AzureResourcesApiRequestErrorCode.ClientCredentialFailedVerification: | ||
| case error.code === AzureResourcesApiRequestErrorCode.HostApiProvisioningFailed: | ||
| default: | ||
| } | ||
| }, | ||
|
|
||
| }; | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| [Back to README](https://github.com/microsoft/vscode-azureresourcegroups/blob/main/api/README.md) | ||
32 changes: 32 additions & 0 deletions
32
api/src/auth/apiRequest/AzureResourcesApiRequestContext.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,32 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { AzureResourcesExtensionApi } from "../../extensionApi"; | ||
| import { AzureExtensionApi } from "../../utils/apiUtils"; | ||
| import { AzureResourcesApiRequestError } from "./apiRequestErrors"; | ||
|
|
||
| export interface AzureResourcesApiRequestContext { | ||
| clientExtensionId: string; | ||
| azureResourcesApiVersions: string[]; | ||
|
|
||
| /** | ||
| * Callback invoked when Azure Resource APIs are successfully obtained through the authentication handshake. | ||
| * | ||
| * @param azureResourcesApis - Array of APIs corresponding to the requested versions. APIs are returned in the same | ||
| * order as provided in this request context. If a requested version is not | ||
| * available or does not match, `undefined` will be returned at that position. | ||
| */ | ||
| onDidReceiveAzureResourcesApis: (azureResourcesApis: (AzureResourcesExtensionApi | AzureExtensionApi | undefined)[]) => void | Promise<void>; | ||
|
|
||
| /** | ||
| * Optional callback invoked when an error occurs during the Azure Resources API handshake process. | ||
| * | ||
| * @remarks Errors thrown during execution of this callback may be part of a separate process and may not bubble up to users. | ||
| * If you wish to surface specific errors to users, please consider logging them or using the VS Code API to display them through UI. | ||
| * | ||
| * @param error - The error that occurred during the handshake, containing an error code and message. | ||
| */ | ||
| onApiRequestError?: (error: AzureResourcesApiRequestError) => void | Promise<void>; | ||
| } |
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.