From 6b18efce7fca4c092f56cee1bbc97696dd641091 Mon Sep 17 00:00:00 2001 From: evochriso Date: Sun, 11 Feb 2024 20:55:14 -0500 Subject: [PATCH 1/5] Replace deprecated package Microsoft.Azure.Services.AppAuthentication with Azure.Identity --- src/dbup-sqlserver/AzureSqlConnectionManager.cs | 10 +++++----- src/dbup-sqlserver/dbup-sqlserver.csproj | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/dbup-sqlserver/AzureSqlConnectionManager.cs b/src/dbup-sqlserver/AzureSqlConnectionManager.cs index 008cba9..2d5619e 100644 --- a/src/dbup-sqlserver/AzureSqlConnectionManager.cs +++ b/src/dbup-sqlserver/AzureSqlConnectionManager.cs @@ -10,7 +10,8 @@ using DbUp.Support; #if SUPPORTS_AZURE_AD -using Microsoft.Azure.Services.AppAuthentication; +using Azure.Identity; +using Azure.Core; namespace DbUp.SqlServer { @@ -28,12 +29,11 @@ public AzureSqlConnectionManager(string connectionString, string resource) public AzureSqlConnectionManager(string connectionString, string resource, string tenantId, string azureAdInstance = "https://login.microsoftonline.com/") : base(new DelegateConnectionFactory((log, dbManager) => { + var tokenProvider = new DefaultAzureCredential(); + var tokenContext = new TokenRequestContext(scopes: new string[] { resource + "/.default" }, tenantId: tenantId); var conn = new SqlConnection(connectionString) { - AccessToken = new AzureServiceTokenProvider(azureAdInstance: azureAdInstance).GetAccessTokenAsync(resource, tenantId) - .ConfigureAwait(false) - .GetAwaiter() - .GetResult() + AccessToken = tokenProvider.GetToken(tokenContext).Token }; if (dbManager.IsScriptOutputLogged) diff --git a/src/dbup-sqlserver/dbup-sqlserver.csproj b/src/dbup-sqlserver/dbup-sqlserver.csproj index 8e6c3da..fc19816 100644 --- a/src/dbup-sqlserver/dbup-sqlserver.csproj +++ b/src/dbup-sqlserver/dbup-sqlserver.csproj @@ -24,21 +24,21 @@ - + - + - + - - + + From a78bf8578964bb4f68e836a8d7dcd812b44cc10b Mon Sep 17 00:00:00 2001 From: Robert Wagner Date: Fri, 26 Jul 2024 17:34:12 +1000 Subject: [PATCH 2/5] Passed `TokenCredential` in for Azure connections --- .../NoPublicApiChanges.Run.approved.cs | 10 ++--- .../AzureSqlConnectionManager.cs | 27 +++++++------ .../AzureSqlServerExtensions.cs | 39 +++++++------------ src/dbup-sqlserver/Properties/AssemblyInfo.cs | 2 +- src/dbup-sqlserver/dbup-sqlserver.csproj | 5 +-- 5 files changed, 32 insertions(+), 51 deletions(-) diff --git a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs index 57a0780..5c750d7 100644 --- a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs +++ b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs @@ -1,12 +1,10 @@ -[assembly: System.CLSCompliantAttribute(true)] +[assembly: System.CLSCompliantAttribute(false)] [assembly: System.Runtime.InteropServices.ComVisibleAttribute(false)] [assembly: System.Runtime.InteropServices.GuidAttribute("8190b40b-ac5b-414f-8a00-9b6a2c12b010")] public static class AzureSqlServerExtensions { - public static DbUp.Builder.UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this DbUp.Builder.SupportedDatabases supported, string connectionString, string schema) { } - public static DbUp.Builder.UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this DbUp.Builder.SupportedDatabases supported, string connectionString, string schema, string resource) { } - public static DbUp.Builder.UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this DbUp.Builder.SupportedDatabases supported, string connectionString, string schema, string resource, string tenantId, string azureAdInstance = "https://login.microsoftonline.com/") { } + public static DbUp.Builder.UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this DbUp.Builder.SupportedDatabases supported, string connectionString, string schema = null, Azure.Core.TokenCredential tokenCredential = null, string resource = "https://database.windows.net/", string tenantId = null) { } } public static class SqlServerExtensions { @@ -40,9 +38,7 @@ public enum AzureDatabaseEdition : int } public class AzureSqlConnectionManager : DbUp.Engine.Transactions.DatabaseConnectionManager, DbUp.Engine.Transactions.IConnectionManager { - public AzureSqlConnectionManager(string connectionString) { } - public AzureSqlConnectionManager(string connectionString, string resource) { } - public AzureSqlConnectionManager(string connectionString, string resource, string tenantId, string azureAdInstance = "https://login.microsoftonline.com/") { } + public AzureSqlConnectionManager(string connectionString, Azure.Core.TokenCredential tokenCredential, string resource = "https://database.windows.net/", string tenantId = null) { } public override System.Collections.Generic.IEnumerable SplitScriptIntoCommands(string scriptContents) { } } public class SqlConnectionManager : DbUp.Engine.Transactions.DatabaseConnectionManager, DbUp.Engine.Transactions.IConnectionManager diff --git a/src/dbup-sqlserver/AzureSqlConnectionManager.cs b/src/dbup-sqlserver/AzureSqlConnectionManager.cs index 49805be..3008b01 100644 --- a/src/dbup-sqlserver/AzureSqlConnectionManager.cs +++ b/src/dbup-sqlserver/AzureSqlConnectionManager.cs @@ -1,31 +1,29 @@ using System.Collections.Generic; +using System.Threading; using Microsoft.Data.SqlClient; using DbUp.Engine.Transactions; using DbUp.Support; -using Azure.Identity; using Azure.Core; +using Azure.Identity; namespace DbUp.SqlServer; /// Manages an Azure Sql Server database connection. public class AzureSqlConnectionManager : DatabaseConnectionManager { - public AzureSqlConnectionManager(string connectionString) - : this(connectionString, "https://database.windows.net/", null) - { } - - public AzureSqlConnectionManager(string connectionString, string resource) - : this(connectionString, resource, null) - { } - - public AzureSqlConnectionManager(string connectionString, string resource, string tenantId, string azureAdInstance = "https://login.microsoftonline.com/") + public AzureSqlConnectionManager( + string connectionString, + TokenCredential tokenCredential, + string resource = "https://database.windows.net/", + string tenantId = null + ) : base(new DelegateConnectionFactory((log, dbManager) => { - var tokenProvider = new DefaultAzureCredential(); - var tokenContext = new TokenRequestContext(scopes: new string[] { resource + "/.default" }, tenantId: tenantId); + var tokenContext = + new TokenRequestContext(scopes: new string[] { resource + "/.default" }, tenantId: tenantId); var conn = new SqlConnection(connectionString) { - AccessToken = tokenProvider.GetToken(tokenContext).Token + AccessToken = tokenCredential.GetToken(tokenContext, CancellationToken.None).Token }; if (dbManager.IsScriptOutputLogged) @@ -33,7 +31,8 @@ public AzureSqlConnectionManager(string connectionString, string resource, strin return conn; })) - { } + { + } public override IEnumerable SplitScriptIntoCommands(string scriptContents) { diff --git a/src/dbup-sqlserver/AzureSqlServerExtensions.cs b/src/dbup-sqlserver/AzureSqlServerExtensions.cs index de7aa57..c93c4e8 100644 --- a/src/dbup-sqlserver/AzureSqlServerExtensions.cs +++ b/src/dbup-sqlserver/AzureSqlServerExtensions.cs @@ -1,4 +1,5 @@ -using System; +using Azure.Core; +using Azure.Identity; using DbUp.Builder; using DbUp.SqlServer; @@ -13,33 +14,21 @@ public static class AzureSqlServerExtensions /// Fluent helper type. /// The connection string. /// The SQL schema name to use. Defaults to 'dbo' if . - /// A builder for a database upgrader designed for Azure SQL Server databases. - public static UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this SupportedDatabases supported, string connectionString, string schema) - { - return supported.SqlDatabase(new AzureSqlConnectionManager(connectionString), schema); - } - - /// Creates an upgrader for Azure SQL Databases using Azure AD Integrated Security. - /// Fluent helper type. - /// The connection string. - /// The SQL schema name to use. Defaults to 'dbo' if . - /// A builder for a database upgrader designed for Azure SQL Server databases. - public static UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this SupportedDatabases supported, string connectionString, string schema, string resource) - { - return AzureSqlDatabaseWithIntegratedSecurity(supported, connectionString, schema, resource, null); - } - - /// Creates an upgrader for Azure SQL Databases using Azure AD Integrated Security. - /// Fluent helper type. - /// The connection string. - /// The SQL schema name to use. Defaults to 'dbo' if . - /// Resource to access. e.g. https://management.azure.com/. + /// The credentials used. If null, 'DefaultAzureCredential' is used. + /// Resource to access. e.g. https://database.windows.net/. /// If not specified, default tenant is used. Managed Service Identity REST protocols do not accept tenantId, so this can only be used with certificate and client secret based authentication. - /// Specify a value for clouds other than the Public Cloud. /// A builder for a database upgrader designed for Azure SQL Server databases. - public static UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this SupportedDatabases supported, string connectionString, string schema, string resource, string tenantId, string azureAdInstance = "https://login.microsoftonline.com/") + public static UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity( + this SupportedDatabases supported, + string connectionString, + string schema = null, + TokenCredential tokenCredential = null, + string resource = "https://database.windows.net/", + string tenantId = null + ) { - return supported.SqlDatabase(new AzureSqlConnectionManager(connectionString, resource, tenantId, azureAdInstance), schema); + return supported.SqlDatabase( + new AzureSqlConnectionManager(connectionString, tokenCredential ?? new DefaultAzureCredential(), resource, tenantId), schema); } } #pragma warning restore CA1050 // Declare types in namespaces diff --git a/src/dbup-sqlserver/Properties/AssemblyInfo.cs b/src/dbup-sqlserver/Properties/AssemblyInfo.cs index 2cb2224..57508d6 100644 --- a/src/dbup-sqlserver/Properties/AssemblyInfo.cs +++ b/src/dbup-sqlserver/Properties/AssemblyInfo.cs @@ -2,7 +2,7 @@ using System.Runtime.InteropServices; [assembly: ComVisible(false)] -[assembly: CLSCompliant(true)] +[assembly: CLSCompliant(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("8190b40b-ac5b-414f-8a00-9b6a2c12b010")] diff --git a/src/dbup-sqlserver/dbup-sqlserver.csproj b/src/dbup-sqlserver/dbup-sqlserver.csproj index 249f0df..14714a7 100644 --- a/src/dbup-sqlserver/dbup-sqlserver.csproj +++ b/src/dbup-sqlserver/dbup-sqlserver.csproj @@ -25,11 +25,8 @@ - - - - + From 0cbdb1f4158848793c6b6e86c35fff4fc9ca8952 Mon Sep 17 00:00:00 2001 From: Robert Wagner Date: Fri, 26 Jul 2024 17:35:04 +1000 Subject: [PATCH 3/5] Undid accidental downgrade --- src/dbup-sqlserver/dbup-sqlserver.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dbup-sqlserver/dbup-sqlserver.csproj b/src/dbup-sqlserver/dbup-sqlserver.csproj index 14714a7..f47007e 100644 --- a/src/dbup-sqlserver/dbup-sqlserver.csproj +++ b/src/dbup-sqlserver/dbup-sqlserver.csproj @@ -25,7 +25,7 @@ - + From 373de57547f38d988495ae61069403ee6d2ed139 Mon Sep 17 00:00:00 2001 From: Robert Wagner Date: Sat, 27 Jul 2024 15:14:16 +1000 Subject: [PATCH 4/5] Removed unneeded reference --- src/dbup-sqlserver/dbup-sqlserver.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dbup-sqlserver/dbup-sqlserver.csproj b/src/dbup-sqlserver/dbup-sqlserver.csproj index f47007e..ad90858 100644 --- a/src/dbup-sqlserver/dbup-sqlserver.csproj +++ b/src/dbup-sqlserver/dbup-sqlserver.csproj @@ -26,7 +26,6 @@ - From b88e44aa4d73a6762157beff20ba0316607670d4 Mon Sep 17 00:00:00 2001 From: Robert Wagner Date: Mon, 30 Sep 2024 17:18:43 +1000 Subject: [PATCH 5/5] Upgraded to SqlClient 5.2.2 --- src/dbup-sqlserver/dbup-sqlserver.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dbup-sqlserver/dbup-sqlserver.csproj b/src/dbup-sqlserver/dbup-sqlserver.csproj index ad90858..d3fb7da 100644 --- a/src/dbup-sqlserver/dbup-sqlserver.csproj +++ b/src/dbup-sqlserver/dbup-sqlserver.csproj @@ -25,7 +25,7 @@ - +