Skip to content

Commit 79ccfa6

Browse files
authored
[Identity] Update VisualStudioCodeCredential to be broker-based (#41822)
Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
1 parent d76b8ba commit 79ccfa6

File tree

15 files changed

+431
-1167
lines changed

15 files changed

+431
-1167
lines changed

sdk/identity/azure-identity/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
- Expanded the set of acceptable values for environment variable `AZURE_TOKEN_CREDENTIALS` to allow for selection of a specific credential in the `DefaultAzureCredential` chain. At runtime, only the specified credential will be used when acquiring tokens with `DefaultAzureCredential`. For example, setting `AZURE_TOKEN_CREDENTIALS=WorkloadIdentityCredential` will make `DefaultAzureCredential` use only `WorkloadIdentityCredential`.
88
- Valid values are `EnvironmentCredential`, `WorkloadIdentityCredential`, `ManagedIdentityCredential`, `AzureCliCredential`, `AzurePowershellCredential`, `AzureDeveloperCliCredential`, and `InteractiveBrowserCredential`. ([#41709](https://github.com/Azure/azure-sdk-for-python/pull/41709))
9+
- Re-enabled `VisualStudioCodeCredential` - Previously deprecated `VisualStudioCodeCredential` has been re-implemented to work with the VS Code Azure Resources extension instead of the deprecated Azure Account extension. This requires the `azure-identity-broker` package to be installed for authentication. ([#41822](https://github.com/Azure/azure-sdk-for-python/pull/41822))
10+
- `VisualStudioCodeCredential` is now included in the `DefaultAzureCredential` token chain by default.
11+
912

1013
### Breaking Changes
1114

sdk/identity/azure-identity/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ DefaultAzureCredential(managed_identity_client_id=client_id)
108108

109109
Alternatively, set the environment variable `AZURE_CLIENT_ID` to the identity's client ID.
110110

111+
#### Authenticate Using Visual Studio Code with `DefaultAzureCredential`
112+
113+
To authenticate using Visual Studio Code, ensure you have signed in through the **Azure Resources** extension. The signed-in user is then picked up automatically by `DefaultAzureCredential`. Currently, this is only supported on Windows and WSL. To use this method of authentication, ensure the following prerequisites are met:
114+
115+
- [Azure Resources Extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azureresourcegroups) is installed in Visual Studio Code.
116+
- You are signed in using the `Azure: Sign In` command in VS Code.
117+
- You have the [`azure-identity-broker`][azure_identity_broker] package installed.
118+
111119
### Define a custom authentication flow with `ChainedTokenCredential`
112120

113121
While `DefaultAzureCredential` is generally the quickest way to authenticate apps for Azure, you can create a customized chain of credentials to be considered. `ChainedTokenCredential` enables users to combine multiple credential instances to define a customized chain of credentials. For more information, see [ChainedTokenCredential overview][ctc_overview].

sdk/identity/azure-identity/TROUBLESHOOTING.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This troubleshooting guide covers failure investigation techniques, common error
1919
- [Azure Virtual Machine managed identity](#azure-virtual-machine-managed-identity)
2020
- [Azure App Service and Azure Functions managed identity](#azure-app-service-and-azure-functions-managed-identity)
2121
- [Azure Kubernetes Service managed identity](#azure-kubernetes-service-managed-identity)
22+
- [Troubleshoot VisualStudioCodeCredential authentication issues](#troubleshoot-visualstudiocodecredential-authentication-issues)
2223
- [Troubleshoot AzureCliCredential authentication issues](#troubleshoot-azureclicredential-authentication-issues)
2324
- [Troubleshoot AzureDeveloperCliCredential authentication issues](#troubleshoot-azuredeveloperclicredential-authentication-issues)
2425
- [Troubleshoot AzurePowerShellCredential authentication issues](#troubleshoot-azurepowershellcredential-authentication-issues)
@@ -182,6 +183,26 @@ curl 'http://169.254.169.254/metadata/identity/oauth2/token?resource=https://man
182183
|---|---|---|
183184
|No managed identity endpoint found|The application attempted to authenticate before an identity was assigned to its pod|Verify the pod is labeled correctly. This also occurs when a correctly labeled pod authenticates before the identity is ready. To prevent initialization races, configure NMI to set the Retry-After header in its responses (see [Pod Identity documentation](https://azure.github.io/aad-pod-identity/docs/configure/feature_flags/#set-retry-after-header-in-nmi-response)).
184185

186+
## Troubleshoot `VisualStudioCodeCredential` authentication issues
187+
188+
> **Applies to:** Version 1.24.0b1 and later
189+
190+
As of version 1.24.0b1, `VisualStudioCodeCredential` uses brokered authentication to sign in using the Azure Resources extension in Visual Studio Code. This approach requires the `azure-identity-broker` dependency and currently only works on Windows or WSL.
191+
192+
### Platform support
193+
194+
**Note:** `VisualStudioCodeCredential` with brokered authentication is currently only supported on Windows and WSL. Linux and macOS are not yet supported.
195+
196+
### Common errors
197+
198+
`CredentialUnavailableError`
199+
200+
| Error Message | Description | Mitigation |
201+
|---|---|---|
202+
| VisualStudioCodeCredential requires the 'azure-identity-broker' package to be installed. You must also ensure you have the Azure Resources extension installed and have signed in to Azure via Visual Studio Code.` | Brokered authentication is not available, which may be due to missing dependencies, not being signed in to Azure in VS Code, or the Azure Resources extension not being installed. | <ul><li>Ensure your project includes the <code>azure-identity-broker</code> dependency.</li><li>In Visual Studio Code, install the <a href="https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azureresourcegroups">Azure Resources extension</a>.</li><li>Sign in to Azure using the "Azure: Sign In" command in VS Code.</li><li>Restart your application after signing in.</li></ul> |
203+
204+
> VisualStudioCodeCredential is intended for local development scenarios and is not recommended for production environments.
205+
185206
## Troubleshoot `AzureCliCredential` authentication issues
186207

187208
`CredentialUnavailableError`

sdk/identity/azure-identity/azure/identity/_credentials/default.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ class DefaultAzureCredential(ChainedTokenCredential):
8080
:keyword str shared_cache_tenant_id: Preferred tenant for :class:`~azure.identity.SharedTokenCacheCredential`.
8181
Defaults to the value of environment variable AZURE_TENANT_ID, if any.
8282
:keyword str visual_studio_code_tenant_id: Tenant ID to use when authenticating with
83-
:class:`~azure.identity.VisualStudioCodeCredential`. Defaults to the "Azure: Tenant" setting in VS Code's user
84-
settings or, when that setting has no value, the "organizations" tenant, which supports only Azure Active
85-
Directory work or school accounts.
83+
:class:`~azure.identity.VisualStudioCodeCredential`. Defaults to the tenant specified in the authentication
84+
record file used by the Azure Resources extension.
8685
:keyword int process_timeout: The timeout in seconds to use for developer credentials that run
8786
subprocesses (e.g. AzureCliCredential, AzurePowerShellCredential). Defaults to **10** seconds.
8887
@@ -101,18 +100,10 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=too-many-statement
101100
raise TypeError("'tenant_id' is not supported in DefaultAzureCredential.")
102101

103102
authority = kwargs.pop("authority", None)
104-
105-
vscode_tenant_id = kwargs.pop(
106-
"visual_studio_code_tenant_id", os.environ.get(EnvironmentVariables.AZURE_TENANT_ID)
107-
)
108-
vscode_args = dict(kwargs)
109-
if authority:
110-
vscode_args["authority"] = authority
111-
if vscode_tenant_id:
112-
vscode_args["tenant_id"] = vscode_tenant_id
113-
114103
authority = normalize_authority(authority) if authority else get_default_authority()
115104

105+
vscode_tenant_id = kwargs.pop("visual_studio_code_tenant_id", None)
106+
116107
interactive_browser_tenant_id = kwargs.pop(
117108
"interactive_browser_tenant_id", os.environ.get(EnvironmentVariables.AZURE_TENANT_ID)
118109
)
@@ -156,7 +147,7 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=too-many-statement
156147
},
157148
"visual_studio_code": {
158149
"exclude_param": "exclude_visual_studio_code_credential",
159-
"default_exclude": True,
150+
"default_exclude": False,
160151
},
161152
"cli": {
162153
"exclude_param": "exclude_cli_credential",
@@ -235,7 +226,7 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=too-many-statement
235226
except Exception as ex: # pylint:disable=broad-except
236227
_LOGGER.info("Shared token cache is unavailable: '%s'", ex)
237228
if not exclude_visual_studio_code_credential:
238-
credentials.append(VisualStudioCodeCredential(**vscode_args))
229+
credentials.append(VisualStudioCodeCredential(tenant_id=vscode_tenant_id))
239230
if not exclude_cli_credential:
240231
credentials.append(AzureCliCredential(process_timeout=process_timeout))
241232
if not exclude_powershell_credential:

0 commit comments

Comments
 (0)