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

Improve AAD fallback if key authentication is disabled #2290

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Changes from 1 commit
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
35 changes: 21 additions & 14 deletions src/tree/SubscriptionTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ILocationWizardContext, LocationListStep, ResourceGroupListStep, Subscr
import { AzExtParentTreeItem, AzExtTreeItem, AzureWizard, AzureWizardPromptStep, IActionContext } from '@microsoft/vscode-azext-utils';
import * as vscode from 'vscode';
import { API, Experience, getExperienceLabel, tryGetExperience } from '../AzureDBExperiences';
import { CosmosDBCredential } from '../docdb/getCosmosClient';
import { CosmosDBCredential, CosmosDBKeyCredential } from '../docdb/getCosmosClient';
import { DocDBAccountTreeItem } from "../docdb/tree/DocDBAccountTreeItem";
import { ext } from '../extensionVariables';
import { tryGetGremlinEndpointFromAzure } from '../graph/gremlinEndpoints';
Expand Down Expand Up @@ -134,21 +134,28 @@ export class SubscriptionTreeItem extends SubscriptionTreeItemBase {
// Use the default connection string
return new MongoAccountTreeItem(parent, id, label, connectionString.toString(), isEmulator, databaseAccount);
} else {
let keyResult: DatabaseAccountListKeysResult | undefined;
try {
keyResult = await client.databaseAccounts.listKeys(resourceGroup, name);
} catch (error) {
// If the client failed to list keys, proceed without using keys
let keyCred: CosmosDBKeyCredential | undefined = undefined;

const forceOAuth = vscode.workspace.getConfiguration().get<boolean>("azureDatabases.useCosmosOAuth");
// disable key auth if the user has opted in to OAuth (AAD/Entra ID)
if (!forceOAuth) {
let keyResult: DatabaseAccountListKeysResult | undefined;
try {
const acc = await client.databaseAccounts.get(resourceGroup, name);
// If the account has local auth disabled, don't even try to use key auth
if (!acc.disableLocalAuth) {
keyResult = await client.databaseAccounts.listKeys(resourceGroup, name);
keyCred = keyResult?.primaryMasterKey ? {
type: "key",
key: keyResult.primaryMasterKey
} : undefined;
}
} catch (error) {
// If the client failed to list keys, proceed without using keys
}
}

let keyCred = keyResult?.primaryMasterKey ? {
type: "key",
key: keyResult.primaryMasterKey
} : undefined;
const testCosmosAuth = vscode.workspace.getConfiguration().get<boolean>("azureDatabases.useCosmosOAuth");
if (testCosmosAuth) {
keyCred = undefined;
}
// OAuth is always enabled for Cosmos DB and will be used as a fall back if key auth is unavailable
const authCred = { type: "auth" };
const credentials = [keyCred, authCred].filter((cred): cred is CosmosDBCredential => cred !== undefined);
switch (experience && experience.api) {
Expand Down
Loading