Add readOnlyKeys call for accounts with Reader role

This commit is contained in:
Senthamil Sindhu 2024-07-19 07:28:39 -07:00
parent 29a1a819c3
commit 40283ff7f1
2 changed files with 43 additions and 25 deletions

View File

@ -27,7 +27,7 @@ import {
} from "Shared/StorageUtility"; } from "Shared/StorageUtility";
import * as StringUtility from "Shared/StringUtility"; import * as StringUtility from "Shared/StringUtility";
import { updateUserContext, userContext } from "UserContext"; import { updateUserContext, userContext } from "UserContext";
import { logConsoleInfo } from "Utils/NotificationConsoleUtils"; import { logConsoleError, logConsoleInfo } from "Utils/NotificationConsoleUtils";
import * as PriorityBasedExecutionUtils from "Utils/PriorityBasedExecutionUtils"; import * as PriorityBasedExecutionUtils from "Utils/PriorityBasedExecutionUtils";
import { useQueryCopilot } from "hooks/useQueryCopilot"; import { useQueryCopilot } from "hooks/useQueryCopilot";
import { useSidePanel } from "hooks/useSidePanel"; import { useSidePanel } from "hooks/useSidePanel";
@ -36,8 +36,7 @@ import Explorer from "../../Explorer";
import { RightPaneForm, RightPaneFormProps } from "../RightPaneForm/RightPaneForm"; import { RightPaneForm, RightPaneFormProps } from "../RightPaneForm/RightPaneForm";
import { AuthType } from "AuthType"; import { AuthType } from "AuthType";
import create, { UseStore } from "zustand"; import create, { UseStore } from "zustand";
import { DatabaseAccountListKeysResult } from "@azure/arm-cosmosdb/esm/models"; import { getReadOnlyKeys, listKeys } from "Utils/arm/generatedClients/cosmos/databaseAccounts";
import { listKeys } from "Utils/arm/generatedClients/cosmos/databaseAccounts";
export interface DataPlaneRbacState { export interface DataPlaneRbacState {
dataPlaneRbacEnabled: boolean; dataPlaneRbacEnabled: boolean;
@ -171,14 +170,26 @@ export const SettingsPane: FunctionComponent<{ explorer: Explorer }> = ({
hasDataPlaneRbacSettingChanged: true, hasDataPlaneRbacSettingChanged: true,
}); });
const { databaseAccount: account, subscriptionId, resourceGroup } = userContext; const { databaseAccount: account, subscriptionId, resourceGroup } = userContext;
if (!userContext.features.enableAadDataPlane) { if (!userContext.features.enableAadDataPlane && !userContext.masterKey) {
const keys: DatabaseAccountListKeysResult = await listKeys(subscriptionId, resourceGroup, account.name); let keys;
try {
if (keys.primaryMasterKey) { keys = await listKeys(subscriptionId, resourceGroup, account.name);
updateUserContext({ masterKey: keys.primaryMasterKey }); updateUserContext({
masterKey: keys.primaryMasterKey,
useDataPlaneRbac.setState({ dataPlaneRbacEnabled: false }); });
} catch (error) {
// if listKeys fail because of permissions issue, then make call to get ReadOnlyKeys
if (error.code === "AuthorizationFailed") {
keys = await getReadOnlyKeys(subscriptionId, resourceGroup, account.name);
updateUserContext({
masterKey: keys.primaryReadonlyMasterKey,
});
} else {
logConsoleError(`Error occurred fetching keys for the account." ${error.message}`);
throw error;
}
} }
useDataPlaneRbac.setState({ dataPlaneRbacEnabled: false });
} }
} }

View File

@ -40,7 +40,7 @@ import { DefaultExperienceUtility } from "../Shared/DefaultExperienceUtility";
import { Node, PortalEnv, updateUserContext, userContext } from "../UserContext"; import { Node, PortalEnv, updateUserContext, userContext } from "../UserContext";
import { acquireTokenWithMsal, getAuthorizationHeader, getMsalInstance } from "../Utils/AuthorizationUtils"; import { acquireTokenWithMsal, getAuthorizationHeader, getMsalInstance } from "../Utils/AuthorizationUtils";
import { isInvalidParentFrameOrigin, shouldProcessMessage } from "../Utils/MessageValidation"; import { isInvalidParentFrameOrigin, shouldProcessMessage } from "../Utils/MessageValidation";
import { listKeys } from "../Utils/arm/generatedClients/cosmos/databaseAccounts"; import { getReadOnlyKeys, listKeys } from "../Utils/arm/generatedClients/cosmos/databaseAccounts";
import { applyExplorerBindings } from "../applyExplorerBindings"; import { applyExplorerBindings } from "../applyExplorerBindings";
import { useDataPlaneRbac } from "Explorer/Panes/SettingsPane/SettingsPane"; import { useDataPlaneRbac } from "Explorer/Panes/SettingsPane/SettingsPane";
import * as Logger from "../Common/Logger"; import * as Logger from "../Common/Logger";
@ -465,26 +465,33 @@ function configureEmulator(): Explorer {
return explorer; return explorer;
} }
async function fetchAndUpdateKeys(subscriptionId: string, resourceGroup: string, account: string) { export async function fetchAndUpdateKeys(subscriptionId: string, resourceGroup: string, account: string) {
Logger.logInfo(`Fetching keys for ${userContext.apiType} account ${account}`, "Explorer/fetchAndUpdateKeys");
let keys;
try { try {
Logger.logInfo(`Fetching keys for ${userContext.apiType} account ${account}`, "Explorer/fetchAndUpdateKeys"); keys = await listKeys(subscriptionId, resourceGroup, account);
const keys = await listKeys(subscriptionId, resourceGroup, account);
Logger.logInfo(`Keys fetched for ${userContext.apiType} account ${account}`, "Explorer/fetchAndUpdateKeys"); Logger.logInfo(`Keys fetched for ${userContext.apiType} account ${account}`, "Explorer/fetchAndUpdateKeys");
updateUserContext({ updateUserContext({
masterKey: keys.primaryMasterKey, masterKey: keys.primaryMasterKey,
}); });
Logger.logInfo(
`User context updated with Master key for ${userContext.apiType} account ${account}`,
"Explorer/fetchAndUpdateKeys",
);
} catch (error) { } catch (error) {
console.error("Error during fetching keys or updating user context:", error); if (error.code === "AuthorizationFailed") {
Logger.logError( keys = await getReadOnlyKeys(subscriptionId, resourceGroup, account);
`Error during fetching keys or updating user context: ${error} for ${userContext.apiType} account ${account}`, Logger.logInfo(
"Explorer/fetchAndUpdateKeys", `Read only Keys fetched for ${userContext.apiType} account ${account}`,
); "Explorer/fetchAndUpdateKeys",
throw error; );
updateUserContext({
masterKey: keys.primaryReadonlyMasterKey,
});
} else {
logConsoleError(`Error occurred fetching keys for the account." ${error.message}`);
Logger.logError(
`Error during fetching keys or updating user context: ${error} for ${userContext.apiType} account ${account}`,
"Explorer/fetchAndUpdateKeys",
);
throw error;
}
} }
} }