From a40b7d3581d04372566fbeac0c59e80367624925 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra <13396919+cheenamalhotra@users.noreply.github.com> Date: Wed, 8 Mar 2023 14:58:30 -0800 Subject: [PATCH] Get Application Path from command options (#1916) --- .../Sql/AuthenticationProvider.cs | 17 ++++++++++++++--- .../Utility/Utils.cs | 2 +- .../Utility/CommandOptions.cs | 8 ++++++++ .../Connection/ConnectionService.cs | 2 +- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.SqlTools.Authentication/Sql/AuthenticationProvider.cs b/src/Microsoft.SqlTools.Authentication/Sql/AuthenticationProvider.cs index 74dbbcbe..1f57ed33 100644 --- a/src/Microsoft.SqlTools.Authentication/Sql/AuthenticationProvider.cs +++ b/src/Microsoft.SqlTools.Authentication/Sql/AuthenticationProvider.cs @@ -5,6 +5,7 @@ using Microsoft.Data.SqlClient; using Microsoft.SqlTools.Authentication.Utility; +using Microsoft.SqlTools.Utility; namespace Microsoft.SqlTools.Authentication.Sql { @@ -27,13 +28,23 @@ namespace Microsoft.SqlTools.Authentication.Sql /// Instantiates AuthenticationProvider to be used for AAD authentication with MSAL.NET and MSAL.js co-ordinated. /// /// Application Name that identifies user folder path location for reading/writing to shared cache. + /// Application Path directory where application cache folder is present. /// Callback that handles AAD authentication when user interaction is needed. - public AuthenticationProvider(string applicationName) + public AuthenticationProvider(string applicationName, string applicationPath) { - if(string.IsNullOrEmpty(applicationName)) { + if (string.IsNullOrEmpty(applicationName)) + { applicationName = nameof(SqlTools); + Logger.Warning($"Application Name not received with command options, using default application name as: {applicationName}"); } - var cachePath = Path.Combine(Utils.BuildAppDirectoryPath(), applicationName, AzureTokenFolder); + + if (string.IsNullOrEmpty(applicationPath)) + { + applicationPath = Utils.BuildAppDirectoryPath(); + Logger.Warning($"Application Path not received with command options, using default application path as: {applicationPath}"); + } + + var cachePath = Path.Combine(applicationPath, applicationName, AzureTokenFolder); this.authenticator = new Authenticator(ApplicationClientId, applicationName, cachePath, MsalCacheName); } diff --git a/src/Microsoft.SqlTools.Authentication/Utility/Utils.cs b/src/Microsoft.SqlTools.Authentication/Utility/Utils.cs index bc3b6712..939e36da 100644 --- a/src/Microsoft.SqlTools.Authentication/Utility/Utils.cs +++ b/src/Microsoft.SqlTools.Authentication/Utility/Utils.cs @@ -37,7 +37,7 @@ namespace Microsoft.SqlTools.Authentication.Utility /// When called on unsupported platform. public static string BuildAppDirectoryPath() { - var homedir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + var homedir = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // Windows if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) diff --git a/src/Microsoft.SqlTools.Hosting/Utility/CommandOptions.cs b/src/Microsoft.SqlTools.Hosting/Utility/CommandOptions.cs index b56c084b..7a901e8d 100644 --- a/src/Microsoft.SqlTools.Hosting/Utility/CommandOptions.cs +++ b/src/Microsoft.SqlTools.Hosting/Utility/CommandOptions.cs @@ -46,6 +46,9 @@ namespace Microsoft.SqlTools.Utility case "-application-name": ApplicationName = args[++i]; break; + case "-data-path": + ApplicationPath = args[++i]; + break; case "-autoflush-log": AutoFlushLog = true; break; @@ -110,6 +113,11 @@ namespace Microsoft.SqlTools.Utility /// public string ApplicationName { get; private set; } + /// + /// Path of application home directory + /// + public string ApplicationPath { get; private set; } + /// /// Contains any error messages during execution /// diff --git a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs index 6485ef65..c44ea349 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs @@ -1076,7 +1076,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection if (commandOptions != null && commandOptions.EnableSqlAuthenticationProvider) { // Register SqlAuthenticationProvider with SqlConnection for AAD Interactive (MFA) authentication. - var provider = new AuthenticationProvider(commandOptions.ApplicationName); + var provider = new AuthenticationProvider(commandOptions.ApplicationName, commandOptions.ApplicationPath); SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive, provider); this.EnableSqlAuthenticationProvider = true;