Skip to content

Commit a78bf85

Browse files
committed
Passed TokenCredential in for Azure connections
1 parent b6d4b73 commit a78bf85

File tree

5 files changed

+32
-51
lines changed

5 files changed

+32
-51
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: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
using System.Collections.Generic;
2+
using System.Threading;
23
using Microsoft.Data.SqlClient;
34
using DbUp.Engine.Transactions;
45
using DbUp.Support;
5-
using Azure.Identity;
66
using Azure.Core;
7+
using Azure.Identity;
78

89
namespace DbUp.SqlServer;
910

1011
/// <summary>Manages an Azure Sql Server database connection.</summary>
1112
public class AzureSqlConnectionManager : DatabaseConnectionManager
1213
{
13-
public AzureSqlConnectionManager(string connectionString)
14-
: this(connectionString, "https://database.windows.net/", null)
15-
{ }
16-
17-
public AzureSqlConnectionManager(string connectionString, string resource)
18-
: this(connectionString, resource, null)
19-
{ }
20-
21-
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+
)
2220
: base(new DelegateConnectionFactory((log, dbManager) =>
2321
{
24-
var tokenProvider = new DefaultAzureCredential();
25-
var tokenContext = new TokenRequestContext(scopes: new string[] { resource + "/.default" }, tenantId: tenantId);
22+
var tokenContext =
23+
new TokenRequestContext(scopes: new string[] { resource + "/.default" }, tenantId: tenantId);
2624
var conn = new SqlConnection(connectionString)
2725
{
28-
AccessToken = tokenProvider.GetToken(tokenContext).Token
26+
AccessToken = tokenCredential.GetToken(tokenContext, CancellationToken.None).Token
2927
};
3028

3129
if (dbManager.IsScriptOutputLogged)
3230
conn.InfoMessage += (sender, e) => log.LogInformation($"{{0}}", e.Message);
3331

3432
return conn;
3533
}))
36-
{ }
34+
{
35+
}
3736

3837
public override IEnumerable<string> SplitScriptIntoCommands(string scriptContents)
3938
{
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@
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.Azure.Services.AppAuthentication" Version="1.6.2" />
29-
30-
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />
3128
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.4" />
32-
<PackageReference Include="Azure.Identity" Version="1.10.4" />
29+
<PackageReference Include="Azure.Identity" Version="1.12.0" />
3330
</ItemGroup>
3431

3532
<ItemGroup>

0 commit comments

Comments
 (0)