|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { type StringDictionary } from "@azure/arm-appservice"; |
| 7 | +import { isSettingConvertible } from '@microsoft/vscode-azext-azureappsettings'; |
| 8 | +import { AzExtFsExtra, type IActionContext } from "@microsoft/vscode-azext-utils"; |
| 9 | +import { localEventHubsEmulatorConnectionRegExp, localStorageEmulatorConnectionString } from "../../constants"; |
| 10 | +import { type ILocalSettingsJson } from "../../funcConfig/local.settings"; |
| 11 | +import { localize } from "../../localize"; |
| 12 | +import { type SlotTreeItem } from "../../tree/SlotTreeItem"; |
| 13 | +import { tryGetLocalSettingsFileNoPrompt } from "../appSettings/localSettings/getLocalSettingsFile"; |
| 14 | + |
| 15 | +type ConnectionSetting = { name: string, value: string, type: 'ConnectionString' | 'ManagedIdentity' | 'Emulator' }; |
| 16 | + |
| 17 | +export async function getWarningsForConnectionSettings(context: IActionContext, |
| 18 | + options: { |
| 19 | + appSettings: StringDictionary, |
| 20 | + node: SlotTreeItem, |
| 21 | + projectPath: string | undefined |
| 22 | + }): Promise<string | undefined> { |
| 23 | + |
| 24 | + const localSettingsPath = await tryGetLocalSettingsFileNoPrompt(context, options.projectPath); |
| 25 | + const localSettings: ILocalSettingsJson = localSettingsPath ? await AzExtFsExtra.readJSON(localSettingsPath) : { Values: {} }; |
| 26 | + const localConnectionSettings = await getConnectionSettings(localSettings.Values ?? {}); |
| 27 | + const remoteConnectionSettings = await getConnectionSettings(options.appSettings?.properties ?? {}); |
| 28 | + |
| 29 | + if (localConnectionSettings.some(setting => setting.type === 'ManagedIdentity')) { |
| 30 | + if (!options.node.site.rawSite.identity || |
| 31 | + options.node.site.rawSite.identity.type === 'None') { |
| 32 | + // if they have nothing in remote, warn them to connect a managed identity |
| 33 | + return localize('configureManagedIdentityWarning', |
| 34 | + 'Your app is not connected to a managed identity. To ensure access, please configure a managed identity. Without it, your application may encounter authorization issues.'); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (localConnectionSettings.some(setting => setting.type === 'ConnectionString') || remoteConnectionSettings.some(setting => setting.type === 'ConnectionString')) { |
| 39 | + // if they have connection strings, warn them about insecure connections but don't try to convert them |
| 40 | + return localize('connectionStringWarning', |
| 41 | + 'Your app may be using connection strings for authentication. This may expose sensitive credentials and lead to security vulnerabilities. Consider using managed identities to enhance security.') |
| 42 | + } |
| 43 | + |
| 44 | + return; |
| 45 | +} |
| 46 | + |
| 47 | +function checkForConnectionSettings(property: { [propertyName: string]: string }): ConnectionSetting | undefined { |
| 48 | + if (isSettingConvertible(property.propertyName, property.value)) { |
| 49 | + // if the setting is convertible, we can assume it's a connection string |
| 50 | + return { |
| 51 | + name: property.propertyName, |
| 52 | + value: property.value, |
| 53 | + type: 'ConnectionString' |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + return undefined; |
| 58 | +} |
| 59 | +function checkForManagedIdentitySettings(property: { [propertyName: string]: string }): ConnectionSetting | undefined { |
| 60 | + |
| 61 | + if (property.propertyName.includes('__accountName') || property.propertyName.includes('__blobServiceUri') || |
| 62 | + property.propertyName.includes('__queueServiceUri') || property.propertyName.includes('__tableServiceUri') || |
| 63 | + property.propertyName.includes('__accountEndpoint') || property.propertyName.includes('__fullyQualifiedNamespace')) { |
| 64 | + return { |
| 65 | + name: property.propertyName, |
| 66 | + value: property.value, |
| 67 | + type: 'ManagedIdentity' |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + return undefined; |
| 72 | +} |
| 73 | + |
| 74 | +function checkForEmulatorSettings(property: { [propertyName: string]: string }): ConnectionSetting | undefined { |
| 75 | + if (property.value.includes(localStorageEmulatorConnectionString) || |
| 76 | + localEventHubsEmulatorConnectionRegExp.test(property.value)) { |
| 77 | + return { |
| 78 | + name: property.propertyName, |
| 79 | + value: property.value, |
| 80 | + type: 'Emulator' |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + return undefined; |
| 85 | +} |
| 86 | + |
| 87 | +async function getConnectionSettings(properties: StringDictionary): Promise<ConnectionSetting[]> { |
| 88 | + const settings: ConnectionSetting[] = []; |
| 89 | + for (const [key, value] of Object.entries(properties)) { |
| 90 | + const property = { propertyName: key, value: value as string }; |
| 91 | + const connectionSetting = checkForManagedIdentitySettings(property) ?? checkForConnectionSettings(property) ?? checkForEmulatorSettings(property); |
| 92 | + if (connectionSetting) { |
| 93 | + settings.push(connectionSetting); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return settings; |
| 98 | +} |
0 commit comments