Skip to content

Commit 32c112f

Browse files
authored
Merge pull request #14 from evochriso/main
Update Azure SQL Authentication to use Azure.Identity
2 parents c6f95f9 + b88e44a commit 32c112f

File tree

5 files changed

+33
-52
lines changed

5 files changed

+33
-52
lines changed

src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
[assembly: System.CLSCompliantAttribute(true)]
1+
[assembly: System.CLSCompliantAttribute(false)]
22
[assembly: System.Runtime.InteropServices.ComVisibleAttribute(false)]
33
[assembly: System.Runtime.InteropServices.GuidAttribute("8190b40b-ac5b-414f-8a00-9b6a2c12b010")]
44

55
public static class AzureSqlServerExtensions
66
{
7-
public static DbUp.Builder.UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this DbUp.Builder.SupportedDatabases supported, string connectionString, string schema) { }
8-
public static DbUp.Builder.UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this DbUp.Builder.SupportedDatabases supported, string connectionString, string schema, string resource) { }
9-
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/") { }
7+
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) { }
108
}
119
public static class SqlServerExtensions
1210
{
@@ -40,9 +38,7 @@ public enum AzureDatabaseEdition : int
4038
}
4139
public class AzureSqlConnectionManager : DbUp.Engine.Transactions.DatabaseConnectionManager, DbUp.Engine.Transactions.IConnectionManager
4240
{
43-
public AzureSqlConnectionManager(string connectionString) { }
44-
public AzureSqlConnectionManager(string connectionString, string resource) { }
45-
public AzureSqlConnectionManager(string connectionString, string resource, string tenantId, string azureAdInstance = "https://login.microsoftonline.com/") { }
41+
public AzureSqlConnectionManager(string connectionString, Azure.Core.TokenCredential tokenCredential, string resource = "https://database.windows.net/", string tenantId = null) { }
4642
public override System.Collections.Generic.IEnumerable<string> SplitScriptIntoCommands(string scriptContents) { }
4743
}
4844
public class SqlConnectionManager : DbUp.Engine.Transactions.DatabaseConnectionManager, DbUp.Engine.Transactions.IConnectionManager

src/dbup-sqlserver/AzureSqlConnectionManager.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
11
using System.Collections.Generic;
2-
2+
using System.Threading;
33
using Microsoft.Data.SqlClient;
44
using DbUp.Engine.Transactions;
55
using DbUp.Support;
6-
7-
using Microsoft.Azure.Services.AppAuthentication;
6+
using Azure.Core;
7+
using Azure.Identity;
88

99
namespace DbUp.SqlServer;
1010

1111
/// <summary>Manages an Azure Sql Server database connection.</summary>
1212
public class AzureSqlConnectionManager : DatabaseConnectionManager
1313
{
14-
public AzureSqlConnectionManager(string connectionString)
15-
: this(connectionString, "https://database.windows.net/", null)
16-
{ }
17-
18-
public AzureSqlConnectionManager(string connectionString, string resource)
19-
: this(connectionString, resource, null)
20-
{ }
21-
22-
public AzureSqlConnectionManager(string connectionString, string resource, string tenantId, string azureAdInstance = "https://login.microsoftonline.com/")
14+
public AzureSqlConnectionManager(
15+
string connectionString,
16+
TokenCredential tokenCredential,
17+
string resource = "https://database.windows.net/",
18+
string tenantId = null
19+
)
2320
: base(new DelegateConnectionFactory((log, dbManager) =>
2421
{
22+
var tokenContext =
23+
new TokenRequestContext(scopes: new string[] { resource + "/.default" }, tenantId: tenantId);
2524
var conn = new SqlConnection(connectionString)
2625
{
27-
AccessToken = new AzureServiceTokenProvider(azureAdInstance: azureAdInstance).GetAccessTokenAsync(resource, tenantId)
28-
.ConfigureAwait(false)
29-
.GetAwaiter()
30-
.GetResult()
26+
AccessToken = tokenCredential.GetToken(tokenContext, CancellationToken.None).Token
3127
};
3228

3329
if (dbManager.IsScriptOutputLogged)
3430
conn.InfoMessage += (sender, e) => log.LogInformation($"{{0}}", e.Message);
3531

3632
return conn;
3733
}))
38-
{ }
34+
{
35+
}
3936

4037
public override IEnumerable<string> SplitScriptIntoCommands(string scriptContents)
4138
{
Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Azure.Core;
2+
using Azure.Identity;
23
using DbUp.Builder;
34
using DbUp.SqlServer;
45

@@ -13,33 +14,21 @@ public static class AzureSqlServerExtensions
1314
/// <param name="supported">Fluent helper type.</param>
1415
/// <param name="connectionString">The connection string.</param>
1516
/// <param name="schema">The SQL schema name to use. Defaults to 'dbo' if <see langword="null" />.</param>
16-
/// <returns>A builder for a database upgrader designed for Azure SQL Server databases.</returns>
17-
public static UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this SupportedDatabases supported, string connectionString, string schema)
18-
{
19-
return supported.SqlDatabase(new AzureSqlConnectionManager(connectionString), schema);
20-
}
21-
22-
/// <summary>Creates an upgrader for Azure SQL Databases using Azure AD Integrated Security.</summary>
23-
/// <param name="supported">Fluent helper type.</param>
24-
/// <param name="connectionString">The connection string.</param>
25-
/// <param name="schema">The SQL schema name to use. Defaults to 'dbo' if <see langword="null" />.</param>
26-
/// <returns>A builder for a database upgrader designed for Azure SQL Server databases.</returns>
27-
public static UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this SupportedDatabases supported, string connectionString, string schema, string resource)
28-
{
29-
return AzureSqlDatabaseWithIntegratedSecurity(supported, connectionString, schema, resource, null);
30-
}
31-
32-
/// <summary>Creates an upgrader for Azure SQL Databases using Azure AD Integrated Security.</summary>
33-
/// <param name="supported">Fluent helper type.</param>
34-
/// <param name="connectionString">The connection string.</param>
35-
/// <param name="schema">The SQL schema name to use. Defaults to 'dbo' if <see langword="null" />.</param>
36-
/// <param name="resource">Resource to access. e.g. https://management.azure.com/.</param>
17+
/// <param name="tokenCredential">The credentials used. If null, 'DefaultAzureCredential' is used.</param>
18+
/// <param name="resource">Resource to access. e.g. https://database.windows.net/.</param>
3719
/// <param name="tenantId">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.</param>
38-
/// <param name="azureAdInstance">Specify a value for clouds other than the Public Cloud.</param>
3920
/// <returns>A builder for a database upgrader designed for Azure SQL Server databases.</returns>
40-
public static UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(this SupportedDatabases supported, string connectionString, string schema, string resource, string tenantId, string azureAdInstance = "https://login.microsoftonline.com/")
21+
public static UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSecurity(
22+
this SupportedDatabases supported,
23+
string connectionString,
24+
string schema = null,
25+
TokenCredential tokenCredential = null,
26+
string resource = "https://database.windows.net/",
27+
string tenantId = null
28+
)
4129
{
42-
return supported.SqlDatabase(new AzureSqlConnectionManager(connectionString, resource, tenantId, azureAdInstance), schema);
30+
return supported.SqlDatabase(
31+
new AzureSqlConnectionManager(connectionString, tokenCredential ?? new DefaultAzureCredential(), resource, tenantId), schema);
4332
}
4433
}
4534
#pragma warning restore CA1050 // Declare types in namespaces

src/dbup-sqlserver/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Runtime.InteropServices;
33

44
[assembly: ComVisible(false)]
5-
[assembly: CLSCompliant(true)]
5+
[assembly: CLSCompliant(false)]
66

77
// The following GUID is for the ID of the typelib if this project is exposed to COM
88
[assembly: Guid("8190b40b-ac5b-414f-8a00-9b6a2c12b010")]

src/dbup-sqlserver/dbup-sqlserver.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
<ItemGroup>
2626
<PackageReference Include="dbup-core" Version="6.0.0-beta.146"/>
2727
<PackageReference Include="System.Net.Security" Version="4.3.2" />
28-
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.1" />
29-
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />
28+
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
3029
</ItemGroup>
3130

3231
<ItemGroup>

0 commit comments

Comments
 (0)