Skip to content
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

auth: Use setting names defined in VSCode builtin extension #1491

Merged
merged 3 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions auth/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion auth/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
70 changes: 35 additions & 35 deletions auth/src/utils/configuredAzureEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,38 @@
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';
enum CloudEnvironmentSettingValue {
ChinaCloud = 'ChinaCloud',
USGovernment = 'USGovernment',
Custom = '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<string | undefined>('endpoint')?.toLowerCase();

// 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.
const authProviderConfig = vscode.workspace.getConfiguration(CustomCloudConfigurationSection);
const environmentSettingValue = authProviderConfig.get<string | undefined>(CloudEnvironmentSettingName);

/* eslint-disable @typescript-eslint/no-non-null-assertion */
if (endpointSettingValue === CloudNameToEndpointSettingValue[AzureChinaCloudName]!.toLowerCase() || endpointSettingValue === azureEnv.Environment.ChinaCloud.activeDirectoryEndpointUrl.toLowerCase()) {
if (environmentSettingValue === CloudEnvironmentSettingValue.ChinaCloud) {
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 === CloudEnvironmentSettingValue.USGovernment) {
return {
...azureEnv.Environment.get(azureEnv.Environment.USGovernment.name),
isCustomCloud: false,
};
} else if (endpointSettingValue) {
const rgConfig = vscode.workspace.getConfiguration('azureResourceGroups');
const customCloud = rgConfig.get<azureEnv.EnvironmentParameters | undefined>('customCloud'); // TODO: final setting name
} else if (environmentSettingValue === CloudEnvironmentSettingValue.Custom) {
const customCloud = authProviderConfig.get<azureEnv.EnvironmentParameters | undefined>(CustomEnvironmentSettingName);

if (customCloud) {
return {
Expand All @@ -50,9 +46,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),
Expand All @@ -63,22 +58,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.
* 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 cloud Use `'AzureCloud'` or `undefined` for public Azure cloud, `'ChinaCloud'` for Azure China, or `'USGovernment'` for Azure US Government.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically a breaking change but I'm not going to rev the major version for it at this point.

* 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}.
*/
export async function setConfiguredAzureEnv(cloud: string | azureEnv.EnvironmentParameters, target: vscode.ConfigurationTarget = vscode.ConfigurationTarget.Global): Promise<void> {
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<void> {
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 === 'AzureCloud') {
// 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, CloudEnvironmentSettingValue.Custom, target);
await authProviderConfig.update(CustomEnvironmentSettingName, cloud, target);
} else {
throw new Error(`Invalid cloud value: ${JSON.stringify(cloud)}`);
}
Expand All @@ -89,5 +89,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';
}