From f66a203c017c179f080d646c848373710df5c28d Mon Sep 17 00:00:00 2001 From: Cheena Malhotra <13396919+cheenamalhotra@users.noreply.github.com> Date: Fri, 28 Apr 2023 13:39:33 -0700 Subject: [PATCH] Fix application names to respect connection's appname property (#2034) --- .../Hosting/Protocol/Constants.cs | 1 + .../Connection/ConnectionService.cs | 28 +++++++++---------- .../ObjectExplorer/ObjectExplorerService.cs | 2 +- .../ObjectManagementService.cs | 2 +- .../TableDesigner/TableDesignerService.cs | 5 ++-- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.SqlTools.Hosting/Hosting/Protocol/Constants.cs b/src/Microsoft.SqlTools.Hosting/Hosting/Protocol/Constants.cs index c84278a0..ff899150 100644 --- a/src/Microsoft.SqlTools.Hosting/Hosting/Protocol/Constants.cs +++ b/src/Microsoft.SqlTools.Hosting/Hosting/Protocol/Constants.cs @@ -13,6 +13,7 @@ namespace Microsoft.SqlTools.Hosting.Protocol public const string ContentLengthFormatString = "Content-Length: {0}\r\n\r\n"; public static readonly JsonSerializerSettings JsonSerializerSettings; + public static readonly string DefaultApplicationName = "azdata"; public static readonly string SqlLoginAuthenticationType = "SqlLogin"; static Constants() diff --git a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs index abd7eb90..20bf15ec 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs @@ -29,6 +29,7 @@ using Microsoft.SqlTools.Authentication.Sql; using Microsoft.SqlTools.Authentication; using System.IO; using Microsoft.SqlTools.Hosting.Utility; +using Constants = Microsoft.SqlTools.Hosting.Protocol.Constants; namespace Microsoft.SqlTools.ServiceLayer.Connection { @@ -470,19 +471,17 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection } } - private static string GetApplicationNameWithFeature(string applicationName, string featureName) + internal static string GetApplicationNameWithFeature(string applicationName, string featureName) { string appNameWithFeature = applicationName; - if (!string.IsNullOrWhiteSpace(applicationName) && !string.IsNullOrWhiteSpace(featureName)) + if (!string.IsNullOrWhiteSpace(applicationName) && !string.IsNullOrWhiteSpace(featureName) && !applicationName.EndsWith(featureName)) { - int index = applicationName.IndexOf('-'); - string appName = applicationName; - if (index > 0) - { - appName = applicationName.Substring(0, index); - } - appNameWithFeature = $"{appName}-{featureName}"; + int azdataStartIndex = applicationName.IndexOf(Constants.DefaultApplicationName); + string originalAppName = azdataStartIndex != -1 + ? applicationName.Substring(0, azdataStartIndex + Constants.DefaultApplicationName.Length) + : applicationName; // Reset to default if azdata not found. + appNameWithFeature = $"{originalAppName}-{featureName}"; } return appNameWithFeature; @@ -1162,7 +1161,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection } var cachePath = Path.Combine(applicationPath, applicationName, AzureTokenFolder); - return new Authenticator(new (ApplicationClientId, applicationName, cachePath, MsalCacheName), () => this.encryptionKeys); + return new Authenticator(new(ApplicationClientId, applicationName, cachePath, MsalCacheName), () => this.encryptionKeys); } private Task HandleEncryptionKeysNotificationEvent(EncryptionKeysChangeParams @params, EventContext context) @@ -1377,7 +1376,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection break; case SqlLogin: connectionBuilder.UserID = connectionDetails.UserName; - connectionBuilder.Password = string.IsNullOrEmpty(connectionDetails.Password) + connectionBuilder.Password = string.IsNullOrEmpty(connectionDetails.Password) ? string.Empty // Support empty password for accounts without password : connectionDetails.Password; connectionBuilder.Authentication = SqlAuthenticationMethod.SqlPassword; @@ -1596,9 +1595,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection ConnectionInfo info; SqlConnectionStringBuilder connStringBuilder; // set connection string using connection uri if connection details are undefined - if (connStringParams.ConnectionDetails == null) + if (connStringParams.ConnectionDetails == null && TryFindConnection(connStringParams.OwnerUri, out info)) { - TryFindConnection(connStringParams.OwnerUri, out info); connStringBuilder = CreateConnectionStringBuilder(info.ConnectionDetails); } // set connection string using connection details @@ -1611,9 +1609,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection connStringBuilder.Password = ConnectionService.PasswordPlaceholder; } // default connection string application name to always be included unless set to false - if (!connStringParams.IncludeApplicationName.HasValue || connStringParams.IncludeApplicationName.Value == true) + if (connStringBuilder.ApplicationName == null && (!connStringParams.IncludeApplicationName.HasValue || connStringParams.IncludeApplicationName.Value == true)) { - connStringBuilder.ApplicationName = "sqlops-connection-string"; + connStringBuilder.ApplicationName = Constants.DefaultApplicationName; } connectionString = connStringBuilder.ConnectionString; diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs index 11ef6da6..ca45a8fe 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs @@ -399,7 +399,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer { var builder = ConnectionService.CreateConnectionStringBuilder(session.ConnectionInfo.ConnectionDetails); builder.InitialCatalog = node.NodeValue; - builder.ApplicationName = TableDesignerService.TableDesignerApplicationName; + builder.ApplicationName = ConnectionService.GetApplicationNameWithFeature(builder.ApplicationName, TableDesignerService.TableDesignerApplicationNameSuffix); // Set Access Token only when authentication mode is not specified. var azureToken = builder.Authentication == SqlAuthenticationMethod.NotSpecified ? session.ConnectionInfo.ConnectionDetails.AzureAccountToken : null; diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectManagementService.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectManagementService.cs index c4ed6e20..1a573828 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectManagementService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectManagementService.cs @@ -19,7 +19,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement /// public class ObjectManagementService { - public const string ApplicationName = "azdata-object-management"; + public const string ApplicationName = "object-management"; private static Lazy objectManagementServiceInstance = new Lazy(() => new ObjectManagementService()); public static ObjectManagementService Instance => objectManagementServiceInstance.Value; public static ConnectionService connectionService; diff --git a/src/Microsoft.SqlTools.ServiceLayer/TableDesigner/TableDesignerService.cs b/src/Microsoft.SqlTools.ServiceLayer/TableDesigner/TableDesignerService.cs index bf49dfc7..36a4e5ae 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/TableDesigner/TableDesignerService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/TableDesigner/TableDesignerService.cs @@ -17,6 +17,7 @@ using Microsoft.SqlTools.ServiceLayer.TableDesigner.Contracts; using Dac = Microsoft.Data.Tools.Sql.DesignServices.TableDesigner; using STSHost = Microsoft.SqlTools.ServiceLayer.Hosting.ServiceHost; using Microsoft.SqlTools.ServiceLayer.SqlContext; +using Microsoft.SqlTools.ServiceLayer.Connection; namespace Microsoft.SqlTools.ServiceLayer.TableDesigner { @@ -25,7 +26,7 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner /// public sealed class TableDesignerService : IDisposable { - public const string TableDesignerApplicationName = "azdata-table-designer"; + public const string TableDesignerApplicationNameSuffix = "TableDesigner"; private Dictionary idTableMap = new Dictionary(); private bool disposed = false; @@ -1798,7 +1799,7 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner { var connectionStringBuilder = new SqlConnectionStringBuilder(tableInfo.ConnectionString); connectionStringBuilder.InitialCatalog = tableInfo.Database; - connectionStringBuilder.ApplicationName = TableDesignerService.TableDesignerApplicationName; + connectionStringBuilder.ApplicationName = ConnectionService.GetApplicationNameWithFeature(connectionStringBuilder.ApplicationName, TableDesignerService.TableDesignerApplicationNameSuffix); var connectionString = connectionStringBuilder.ToString(); // Set Access Token only when authentication mode is not specified.