From b1d1f4288fae218bc6b8c368cd4902c6e0375d41 Mon Sep 17 00:00:00 2001 From: "Brandon Waterloo [MSFT]" <36966225+bwateratmsft@users.noreply.github.com> Date: Fri, 9 Jun 2023 09:08:42 -0400 Subject: [PATCH 1/3] auth: Use setting names defined in VSCode builtin extension --- auth/package-lock.json | 4 +- auth/package.json | 2 +- auth/src/utils/configuredAzureEnv.ts | 66 ++++++++++++++-------------- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/auth/package-lock.json b/auth/package-lock.json index 4fca4d1f88..92b9453aa0 100644 --- a/auth/package-lock.json +++ b/auth/package-lock.json @@ -1,12 +1,12 @@ { "name": "@microsoft/vscode-azext-azureauth", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@microsoft/vscode-azext-azureauth", - "version": "1.0.0", + "version": "1.1.0", "license": "MIT", "dependencies": { "@azure/arm-subscriptions": "^5.1.0", diff --git a/auth/package.json b/auth/package.json index 2ab8bfa4a9..bae10df438 100644 --- a/auth/package.json +++ b/auth/package.json @@ -1,7 +1,7 @@ { "name": "@microsoft/vscode-azext-azureauth", "author": "Microsoft Corporation", - "version": "1.0.0", + "version": "1.1.0", "description": "Azure authentication helpers for Visual Studio Code", "tags": [ "azure", diff --git a/auth/src/utils/configuredAzureEnv.ts b/auth/src/utils/configuredAzureEnv.ts index 6f38e9b169..94c276f3ab 100644 --- a/auth/src/utils/configuredAzureEnv.ts +++ b/auth/src/utils/configuredAzureEnv.ts @@ -6,42 +6,36 @@ import * as azureEnv from '@azure/ms-rest-azure-env'; // This package is so small that it's not worth lazy loading import * as vscode from 'vscode'; -const AzureCloudName = azureEnv.Environment.AzureCloud.name; -const AzureChinaCloudName = azureEnv.Environment.ChinaCloud.name; -const AzureUSGovernmentCloudName = azureEnv.Environment.USGovernment.name; +// These strings come from https://github.com/microsoft/vscode/blob/eac16e9b63a11885b538db3e0b533a02a2fb8143/extensions/microsoft-authentication/package.json#L40-L99 +const CustomCloudConfigurationSection = 'microsoft-sovereign-cloud'; +const CloudEnvironmentSettingName = 'environment'; +const CustomEnvironmentSettingName = 'customEnvironment'; -const CloudNameToEndpointSettingValue: { [cloudName: string]: string | undefined } = {}; -CloudNameToEndpointSettingValue[AzureCloudName] = undefined; -CloudNameToEndpointSettingValue[AzureChinaCloudName] = 'Azure China'; -CloudNameToEndpointSettingValue[AzureUSGovernmentCloudName] = 'Azure US Government'; +const ChinaCloudSettingValue = 'ChinaCloud'; +const USGovernmentSettingValue = 'USGovernment'; +const CustomCloudSettingValue = 'custom'; /** * Gets the configured Azure environment. * - * @returns The configured Azure environment from the `microsoft-sovereign-cloud.endpoint` setting. + * @returns The configured Azure environment from the settings in the built-in authentication provider extension */ export function getConfiguredAzureEnv(): azureEnv.Environment & { isCustomCloud: boolean } { - const authProviderConfig = vscode.workspace.getConfiguration('microsoft-sovereign-cloud'); - const endpointSettingValue = authProviderConfig.get('endpoint')?.toLowerCase(); + const authProviderConfig = vscode.workspace.getConfiguration(CustomCloudConfigurationSection); + const environmentSettingValue = authProviderConfig.get(CloudEnvironmentSettingName); - // The endpoint setting will accept either the environment name (either 'Azure China' or 'Azure US Government'), - // or an endpoint URL. Since the user could configure the same environment either way, we need to check both. - // We'll also throw to lowercase just to maximize the chance of success. - - /* eslint-disable @typescript-eslint/no-non-null-assertion */ - if (endpointSettingValue === CloudNameToEndpointSettingValue[AzureChinaCloudName]!.toLowerCase() || endpointSettingValue === azureEnv.Environment.ChinaCloud.activeDirectoryEndpointUrl.toLowerCase()) { + if (environmentSettingValue === ChinaCloudSettingValue) { return { ...azureEnv.Environment.get(azureEnv.Environment.ChinaCloud.name), isCustomCloud: false, }; - } else if (endpointSettingValue === CloudNameToEndpointSettingValue[AzureUSGovernmentCloudName]!.toLowerCase() || endpointSettingValue === azureEnv.Environment.USGovernment.activeDirectoryEndpointUrl.toLowerCase()) { + } else if (environmentSettingValue === USGovernmentSettingValue) { return { ...azureEnv.Environment.get(azureEnv.Environment.USGovernment.name), isCustomCloud: false, }; - } else if (endpointSettingValue) { - const rgConfig = vscode.workspace.getConfiguration('azureResourceGroups'); - const customCloud = rgConfig.get('customCloud'); // TODO: final setting name + } else if (environmentSettingValue === CustomCloudSettingValue) { + const customCloud = authProviderConfig.get(CustomEnvironmentSettingName); if (customCloud) { return { @@ -50,9 +44,8 @@ export function getConfiguredAzureEnv(): azureEnv.Environment & { isCustomCloud: }; } - throw new Error(vscode.l10n.t('The custom cloud choice is not configured. Please configure the setting `azureResourceGroups.customCloud`.')); // TODO: final setting name + throw new Error(vscode.l10n.t('The custom cloud choice is not configured. Please configure the setting `{0}.{1}`.', CustomCloudConfigurationSection, CustomEnvironmentSettingName)); } - /* eslint-enable @typescript-eslint/no-non-null-assertion */ return { ...azureEnv.Environment.get(azureEnv.Environment.AzureCloud.name), @@ -63,22 +56,27 @@ export function getConfiguredAzureEnv(): azureEnv.Environment & { isCustomCloud: /** * Sets the configured Azure cloud. * - * @param cloud Use `'AzureCloud'` for public Azure cloud, `'AzureChinaCloud'` for Azure China, or `'AzureUSGovernment'` for Azure US Government. + * @param cloud Use `'AzureCloud'` or `undefined` for public Azure cloud, `'ChinaCloud'` for Azure China, or `'USGovernment'` for Azure US Government. * These are the same values as the cloud names in `@azure/ms-rest-azure-env`. For a custom cloud, use an instance of the `@azure/ms-rest-azure-env` `EnvironmentParameters`. * * @param target (Optional) The configuration target to use, by default {@link vscode.ConfigurationTarget.Global}. */ -export async function setConfiguredAzureEnv(cloud: string | azureEnv.EnvironmentParameters, target: vscode.ConfigurationTarget = vscode.ConfigurationTarget.Global): Promise { - const authProviderConfig = vscode.workspace.getConfiguration('microsoft-sovereign-cloud'); - - if (typeof cloud === 'string' && cloud in CloudNameToEndpointSettingValue) { - await authProviderConfig.update('endpoint', CloudNameToEndpointSettingValue[cloud], target); - } else if (typeof cloud === 'object' && 'activeDirectoryEndpointUrl' in cloud) { - await authProviderConfig.update('endpoint', cloud.activeDirectoryEndpointUrl, target); - - const rgConfig = vscode.workspace.getConfiguration('azureResourceGroups'); - await rgConfig.update('customCloud', cloud, target); // TODO: final setting name +export async function setConfiguredAzureEnv(cloud: 'AzureCloud' | 'ChinaCloud' | 'USGovernment' | undefined | azureEnv.EnvironmentParameters, target: vscode.ConfigurationTarget = vscode.ConfigurationTarget.Global): Promise { + const authProviderConfig = vscode.workspace.getConfiguration(CustomCloudConfigurationSection); + if (typeof cloud === 'undefined' || !cloud) { + // Use public cloud implicitly--set `environment` setting to `undefined` + await authProviderConfig.update(CloudEnvironmentSettingName, undefined, target); + } else if (typeof cloud === 'string' && cloud.toLowerCase() === 'AzureCloud'.toLowerCase()) { + // Use public cloud explicitly--set `environment` setting to `undefined` + await authProviderConfig.update(CloudEnvironmentSettingName, undefined, target); + } else if (typeof cloud === 'string') { + // Use a sovereign cloud--set the `environment` setting to the specified value + await authProviderConfig.update(CloudEnvironmentSettingName, cloud, target); + } else if (typeof cloud === 'object') { + // use a custom cloud--set the `environment` setting to `custom` and the `customEnvironment` setting to the specified value + await authProviderConfig.update(CloudEnvironmentSettingName, CustomCloudSettingValue, target); + await authProviderConfig.update(CustomEnvironmentSettingName, cloud, target); } else { throw new Error(`Invalid cloud value: ${JSON.stringify(cloud)}`); } @@ -89,5 +87,5 @@ export async function setConfiguredAzureEnv(cloud: string | azureEnv.Environment * @returns The provider ID to use, either `'microsoft'` or `'microsoft-sovereign-cloud'` */ export function getConfiguredAuthProviderId(): string { - return getConfiguredAzureEnv().name === AzureCloudName ? 'microsoft' : 'microsoft-sovereign-cloud'; + return getConfiguredAzureEnv().name === azureEnv.Environment.AzureCloud.name ? 'microsoft' : 'microsoft-sovereign-cloud'; } From 7dc2e2b15d979154c6569e272008faba76092351 Mon Sep 17 00:00:00 2001 From: "Brandon Waterloo [MSFT]" <36966225+bwateratmsft@users.noreply.github.com> Date: Fri, 9 Jun 2023 09:12:42 -0400 Subject: [PATCH 2/3] Just be case sensitive --- auth/src/utils/configuredAzureEnv.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auth/src/utils/configuredAzureEnv.ts b/auth/src/utils/configuredAzureEnv.ts index 94c276f3ab..f8c683cbc5 100644 --- a/auth/src/utils/configuredAzureEnv.ts +++ b/auth/src/utils/configuredAzureEnv.ts @@ -67,7 +67,7 @@ export async function setConfiguredAzureEnv(cloud: 'AzureCloud' | 'ChinaCloud' | if (typeof cloud === 'undefined' || !cloud) { // Use public cloud implicitly--set `environment` setting to `undefined` await authProviderConfig.update(CloudEnvironmentSettingName, undefined, target); - } else if (typeof cloud === 'string' && cloud.toLowerCase() === 'AzureCloud'.toLowerCase()) { + } else if (typeof cloud === 'string' && cloud === 'AzureCloud') { // Use public cloud explicitly--set `environment` setting to `undefined` await authProviderConfig.update(CloudEnvironmentSettingName, undefined, target); } else if (typeof cloud === 'string') { From e41f779839aff333ba039bc85caa6f4501f88b13 Mon Sep 17 00:00:00 2001 From: "Brandon Waterloo [MSFT]" <36966225+bwateratmsft@users.noreply.github.com> Date: Mon, 12 Jun 2023 09:31:19 -0400 Subject: [PATCH 3/3] Alex's feedback --- auth/src/utils/configuredAzureEnv.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/auth/src/utils/configuredAzureEnv.ts b/auth/src/utils/configuredAzureEnv.ts index f8c683cbc5..1944a173c8 100644 --- a/auth/src/utils/configuredAzureEnv.ts +++ b/auth/src/utils/configuredAzureEnv.ts @@ -11,9 +11,11 @@ const CustomCloudConfigurationSection = 'microsoft-sovereign-cloud'; const CloudEnvironmentSettingName = 'environment'; const CustomEnvironmentSettingName = 'customEnvironment'; -const ChinaCloudSettingValue = 'ChinaCloud'; -const USGovernmentSettingValue = 'USGovernment'; -const CustomCloudSettingValue = 'custom'; +enum CloudEnvironmentSettingValue { + ChinaCloud = 'ChinaCloud', + USGovernment = 'USGovernment', + Custom = 'custom', +} /** * Gets the configured Azure environment. @@ -24,17 +26,17 @@ export function getConfiguredAzureEnv(): azureEnv.Environment & { isCustomCloud: const authProviderConfig = vscode.workspace.getConfiguration(CustomCloudConfigurationSection); const environmentSettingValue = authProviderConfig.get(CloudEnvironmentSettingName); - if (environmentSettingValue === ChinaCloudSettingValue) { + if (environmentSettingValue === CloudEnvironmentSettingValue.ChinaCloud) { return { ...azureEnv.Environment.get(azureEnv.Environment.ChinaCloud.name), isCustomCloud: false, }; - } else if (environmentSettingValue === USGovernmentSettingValue) { + } else if (environmentSettingValue === CloudEnvironmentSettingValue.USGovernment) { return { ...azureEnv.Environment.get(azureEnv.Environment.USGovernment.name), isCustomCloud: false, }; - } else if (environmentSettingValue === CustomCloudSettingValue) { + } else if (environmentSettingValue === CloudEnvironmentSettingValue.Custom) { const customCloud = authProviderConfig.get(CustomEnvironmentSettingName); if (customCloud) { @@ -57,7 +59,7 @@ export function getConfiguredAzureEnv(): azureEnv.Environment & { isCustomCloud: * Sets the configured Azure cloud. * * @param cloud Use `'AzureCloud'` or `undefined` for public Azure cloud, `'ChinaCloud'` for Azure China, or `'USGovernment'` for Azure US Government. - * These are the same values as the cloud names in `@azure/ms-rest-azure-env`. For a custom cloud, use an instance of the `@azure/ms-rest-azure-env` `EnvironmentParameters`. + * These are the same values as the cloud names in `@azure/ms-rest-azure-env`. For a custom cloud, use an instance of the `@azure/ms-rest-azure-env` {@link azureEnv.EnvironmentParameters}. * * @param target (Optional) The configuration target to use, by default {@link vscode.ConfigurationTarget.Global}. */ @@ -75,7 +77,7 @@ export async function setConfiguredAzureEnv(cloud: 'AzureCloud' | 'ChinaCloud' | await authProviderConfig.update(CloudEnvironmentSettingName, cloud, target); } else if (typeof cloud === 'object') { // use a custom cloud--set the `environment` setting to `custom` and the `customEnvironment` setting to the specified value - await authProviderConfig.update(CloudEnvironmentSettingName, CustomCloudSettingValue, target); + await authProviderConfig.update(CloudEnvironmentSettingName, CloudEnvironmentSettingValue.Custom, target); await authProviderConfig.update(CustomEnvironmentSettingName, cloud, target); } else { throw new Error(`Invalid cloud value: ${JSON.stringify(cloud)}`);