diff --git a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs index d0df0a5a..720bf4e8 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Data; using System.Data.Common; using Microsoft.Data.SqlClient; +using Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider; using System.Globalization; using System.Linq; using System.Threading; @@ -105,6 +106,15 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection } } + static ConnectionService() + { + SqlColumnEncryptionAzureKeyVaultProvider sqlColumnEncryptionAzureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider(AzureActiveDirectoryAuthenticationCallback); + SqlConnection.RegisterColumnEncryptionKeyStoreProviders(customProviders: new Dictionary(capacity: 1, comparer: StringComparer.OrdinalIgnoreCase) + { + { SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, sqlColumnEncryptionAzureKeyVaultProvider } + }); + } + /// /// Default constructor should be private since it's a singleton class, but we need a constructor @@ -117,6 +127,21 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection this.LockedDatabaseManager.ConnectionService = this; } + public static async Task AzureActiveDirectoryAuthenticationCallback(string authority, string resource, string scope) + { + RequestSecurityTokenParams message = new RequestSecurityTokenParams() + { + Authority = authority, + Provider = "Azure", + Resource = resource, + Scope = scope + }; + + RequestSecurityTokenResponse response = await Instance.ServiceHost.SendRequest(SecurityTokenRequest.Type, message, true); + + return response.Token; + } + /// /// Returns a connection queue for given type /// diff --git a/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/SecurityTokenRequest.cs b/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/SecurityTokenRequest.cs new file mode 100644 index 00000000..7798f95a --- /dev/null +++ b/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/SecurityTokenRequest.cs @@ -0,0 +1,50 @@ +using Microsoft.SqlTools.Hosting.Protocol.Contracts; + +namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts +{ + class RequestSecurityTokenParams + { + /// + /// Gets or sets the address of the authority to issue token. + /// + public string Authority { get; set; } + + /// + /// Gets or sets the provider that indicates the type of linked account to query. + /// + public string Provider { get; set; } + + /// + /// Gets or sets the identifier of the target resource that is the recipient of the requested token. + /// + public string Resource { get; set; } + + /// + /// Gets or sets the scope of the authentication request. + /// + public string Scope { get; set; } + } + + class RequestSecurityTokenResponse + { + /// + /// Gets or sets the key that uniquely identifies a particular linked account. + /// + public string AccountKey { get; set; } + + /// + /// Gets or sets the access token. + /// + public string Token { get; set; } + } + + /// + /// SecurityToken Request mapping entry + /// + class SecurityTokenRequest + { + public static readonly + RequestType Type = + RequestType.Create("account/securityTokenRequest"); + } +} diff --git a/src/Microsoft.SqlTools.ServiceLayer/Microsoft.SqlTools.ServiceLayer.csproj b/src/Microsoft.SqlTools.ServiceLayer/Microsoft.SqlTools.ServiceLayer.csproj index fb9a291a..0f49ab0c 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Microsoft.SqlTools.ServiceLayer.csproj +++ b/src/Microsoft.SqlTools.ServiceLayer/Microsoft.SqlTools.ServiceLayer.csproj @@ -20,6 +20,7 @@ + diff --git a/test/Microsoft.SqlTools.ManagedBatchParser.IntegrationTests/Microsoft.SqlTools.ManagedBatchParser.IntegrationTests.csproj b/test/Microsoft.SqlTools.ManagedBatchParser.IntegrationTests/Microsoft.SqlTools.ManagedBatchParser.IntegrationTests.csproj index cca0bd9c..aad5d6f7 100644 --- a/test/Microsoft.SqlTools.ManagedBatchParser.IntegrationTests/Microsoft.SqlTools.ManagedBatchParser.IntegrationTests.csproj +++ b/test/Microsoft.SqlTools.ManagedBatchParser.IntegrationTests/Microsoft.SqlTools.ManagedBatchParser.IntegrationTests.csproj @@ -28,7 +28,7 @@ - + diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Microsoft.SqlTools.ServiceLayer.IntegrationTests.csproj b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Microsoft.SqlTools.ServiceLayer.IntegrationTests.csproj index 1f29922e..e83ab2a3 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Microsoft.SqlTools.ServiceLayer.IntegrationTests.csproj +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Microsoft.SqlTools.ServiceLayer.IntegrationTests.csproj @@ -29,7 +29,7 @@ - +