Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/design/application-name-telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ dab_oss_2.0.0+XXSX|L1AC0M|110000M1M000MMMMMWMM|100?111001110?+

It is opt-out (`DAB_TELEMETRY_APPNAME_OPT_OUT=1`), carries no secrets or identifiers, and is purely additive to the existing `Application Name` value.

## SqlClient registered application identity

SQL Server and DWSQL connections also set
`SqlConnection.RegisteredApplication = RegisteredApplication.DataApiBuilder` before opening,
including metadata discovery and CLI autoentity discovery. This requires SqlClient 7.1 or later;
the Azure authentication extension remains included to preserve Entra ID authentication support.

This fixed product identifier is independent of the application-name token and its opt-out.
It contains no configuration or customer identifiers, is not part of the connection-pool key,
and must not be used for authorization. SqlClient reports it during physical connection login;
reused pooled connections retain the original identity, and background connections created for
`Min Pool Size` report `Unknown`.

## Motivation

DAB ships as an open-source container that customers run anywhere. We have very little visibility into how it is configured or which features are exercised. The connection's `Application Name` is a standard, low-cost signal that the database side already records, so encoding a small feature fingerprint there gives us aggregate usage insight with:
Expand Down
2 changes: 1 addition & 1 deletion scripts/notice-generation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Invoke-WebRequest $chiliCreamLicenseMetadataURL -UseBasicParsing |
Out-File $chiliCreamLicenseSavePath

# Define the path to the license file in your repository and Read the content of the license file
$sqlClientSNILicenseFilePath = "$BuildSourcesDir/external_licenses/Microsoft.Data.SqlClient.SNI.6.0.2.License.txt"
$sqlClientSNILicenseFilePath = "$BuildSourcesDir/external_licenses/Microsoft.Data.SqlClient.SNI.7.1.2.License.txt"
$sqlClientSNILicense = Get-Content -Path $sqlClientSNILicenseFilePath -Raw

# Replace erroneous copyright, using [System.IO.File] for better performance than Get-Content and Set-Content
Expand Down
5 changes: 4 additions & 1 deletion src/Cli/ConfigGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4000,7 +4000,10 @@ public static bool TrySimulateAutoentities(AutoConfigSimulateOptions options, Fi

try
{
using SqlConnection connection = new(connectionString);
using SqlConnection connection = new(connectionString)
{
RegisteredApplication = RegisteredApplication.DataApiBuilder
};
connection.Open();

foreach ((string filterName, Autoentity autoentity) in runtimeConfig.Autoentities.Autoentities)
Expand Down
1 change: 1 addition & 0 deletions src/Config/Azure.DataApiBuilder.Config.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageReference Include="System.IO.Abstractions" />
<PackageReference Include="System.Drawing.Common" />
<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="Microsoft.Data.SqlClient.Extensions.Azure" />
<!-- Override the vulnerable Pkcs pulled in by Microsoft.Data.SqlClient (see Directory.Packages.props). -->
<PackageReference Include="System.Security.Cryptography.Pkcs" />
<PackageReference Include="MySqlConnector" />
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Telemetry/ApplicationNameTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,9 @@ private static char EncodeManagedIdentity(EncodeInputs inputs)
SqlAuthenticationMethod.ActiveDirectoryDefault or SqlAuthenticationMethod.ActiveDirectoryWorkloadIdentity => MISSING,
SqlAuthenticationMethod.NotSpecified => sql.IntegratedSecurity
|| !string.IsNullOrEmpty(sql.UserID) || !string.IsNullOrEmpty(sql.Password) ? '0' : MISSING,
#pragma warning disable CS0618 // Classify legacy authentication without enabling it.
SqlAuthenticationMethod.SqlPassword or SqlAuthenticationMethod.ActiveDirectoryPassword
#pragma warning restore CS0618
or SqlAuthenticationMethod.ActiveDirectoryIntegrated or SqlAuthenticationMethod.ActiveDirectoryInteractive
or SqlAuthenticationMethod.ActiveDirectoryServicePrincipal or SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow => '0',
_ => MISSING,
Expand Down
1 change: 1 addition & 0 deletions src/Core/Azure.DataApiBuilder.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" />
<PackageReference Include="Microsoft.Azure.Cosmos" />
<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="Microsoft.Data.SqlClient.Extensions.Azure" />
<!-- Repeated from Config so the packed Core library still depends on the patched Pkcs when project references are privatized. -->
<PackageReference Include="System.Security.Cryptography.Pkcs" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
Expand Down
1 change: 1 addition & 0 deletions src/Core/Resolvers/MsSqlQueryExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public override SqlConnection CreateConnection(string dataSourceName)
SqlConnection conn = new()
{
ConnectionString = connectionString,
RegisteredApplication = RegisteredApplication.DataApiBuilder,
};

// Extract info message from SQLConnection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public override string GetDefaultSchemaName()
return "dbo";
}

/// <inheritdoc/>
protected override SqlConnection CreateConnection() => new()
{
RegisteredApplication = RegisteredApplication.DataApiBuilder
};

/// <summary>
/// Takes a string version of an SQL Server data type (also applies to Azure SQL DB)
/// and returns its .NET common language runtime (CLR) counterpart
Expand Down Expand Up @@ -186,7 +192,7 @@ protected override async Task FillSchemaForStoredProcedureAsync(
StoredProcedureDefinition storedProcedureDefinition,
CancellationToken cancellationToken)
{
using DbConnection conn = new SqlConnection();
using DbConnection conn = CreateConnection();
conn.ConnectionString = ConnectionString;
await QueryExecutor.SetManagedIdentityAccessTokenIfAnyAsync(
conn,
Expand Down
14 changes: 10 additions & 4 deletions src/Core/Services/MetadataProviders/SqlMetadataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ protected virtual async Task FillSchemaForStoredProcedureAsync(
StoredProcedureDefinition storedProcedureDefinition,
CancellationToken cancellationToken)
{
using ConnectionT conn = new();
using ConnectionT conn = CreateConnection();
conn.ConnectionString = ConnectionString;
DataTable procedureMetadata;
string[] procedureRestrictions = new string[NUMBER_OF_RESTRICTIONS];
Expand Down Expand Up @@ -1876,6 +1876,12 @@ private async Task<DataTable> GetTableWithSchemaFromDataSetAsync(
return dataTable!;
}

/// <summary>
/// Creates an unopened connection for metadata discovery and connection validation.
/// </summary>
/// <returns>A new database connection.</returns>
protected virtual ConnectionT CreateConnection() => new();

/// <summary>
/// This method attempts to open a database connection using the provided connection string.
/// If the connection fails to open, it catches the exception and throws a DataApiBuilderException.
Expand All @@ -1885,7 +1891,7 @@ private async Task<DataTable> GetTableWithSchemaFromDataSetAsync(
private async Task ValidateDatabaseConnection(
CancellationToken cancellationToken)
{
using ConnectionT conn = new();
using ConnectionT conn = CreateConnection();
conn.ConnectionString = ConnectionString;
await QueryExecutor.SetManagedIdentityAccessTokenIfAnyAsync(
conn,
Expand Down Expand Up @@ -1921,7 +1927,7 @@ private async Task<DataTable> FillSchemaForTableAsync(
string tableName,
CancellationToken cancellationToken)
{
using ConnectionT conn = new();
using ConnectionT conn = CreateConnection();
// If connection string is set to empty string
// we throw here to avoid having to sort out
// complicated db specific exception messages.
Expand Down Expand Up @@ -2407,7 +2413,7 @@ protected virtual async Task<DataTable> GetColumnsAsync(
string tableName,
CancellationToken cancellationToken)
{
using ConnectionT conn = new();
using ConnectionT conn = CreateConnection();
conn.ConnectionString = ConnectionString;
await QueryExecutor.SetManagedIdentityAccessTokenIfAnyAsync(
conn,
Expand Down
15 changes: 8 additions & 7 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0" />
<!--When updating Microsoft.Data.SqlClient, update license URL in scripts/notice-generation.ps1-->
<PackageVersion Include="Microsoft.Data.SqlClient" Version="6.1.5" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="7.1.2" />
<PackageVersion Include="Microsoft.Data.SqlClient.Extensions.Azure" Version="7.1.2" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.22.0" />
<PackageVersion Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.1.2" />
<PackageVersion Include="Microsoft.IdentityModel.OpenIdConnect" Version="8.1.2" />
<PackageVersion Include="Microsoft.IdentityModel.Protocols" Version="8.1.2" />
<PackageVersion Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.1.2" />
<PackageVersion Include="Microsoft.IdentityModel.Validators" Version="8.1.2" />
<PackageVersion Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.17.0" />
<PackageVersion Include="Microsoft.IdentityModel.OpenIdConnect" Version="8.17.0" />
<PackageVersion Include="Microsoft.IdentityModel.Protocols" Version="8.17.0" />
<PackageVersion Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.17.0" />
<PackageVersion Include="Microsoft.IdentityModel.Validators" Version="8.17.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="Microsoft.OData.Edm" Version="7.20.0" />
<PackageVersion Include="Microsoft.OData.Core" Version="7.20.0" />
Expand All @@ -80,7 +81,7 @@
<PackageVersion Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="System.Drawing.Common" Version="8.0.3" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="8.1.2" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="8.17.0" />
<PackageVersion Include="System.IO.Abstractions" Version="21.0.2" />
<PackageVersion Include="System.IO.Abstractions.TestingHelpers" Version="21.0.2" />
<!-- Pinned to override the vulnerable Pkcs 9.0.11/9.0.4 pulled in transitively by Microsoft.Data.SqlClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ public void Review_OboMetadataAndRequestNamesRespectClientLimit()
MsSqlQueryExecutor executor = new(provider, parser.Object, NullLogger<IQueryExecutor>.Instance, accessor);

using SqlConnection metadata = executor.CreateConnection(config.DefaultDataSourceName);
Assert.AreEqual(RegisteredApplication.DataApiBuilder, metadata.RegisteredApplication);
Assert.IsTrue(new SqlConnectionStringBuilder(metadata.ConnectionString).ApplicationName.StartsWith(customName, StringComparison.Ordinal));
Assert.AreEqual('M', GeneralFromConnectionString(metadata.ConnectionString)[5]);

Expand All @@ -530,6 +531,7 @@ public void Review_OboMetadataAndRequestNamesRespectClientLimit()
}, "test")),
};
using SqlConnection request = executor.CreateConnection(config.DefaultDataSourceName);
Assert.AreEqual(RegisteredApplication.DataApiBuilder, request.RegisteredApplication);
string name = new SqlConnectionStringBuilder(request.ConnectionString).ApplicationName;
Assert.IsTrue(name.Length <= 128);
Assert.AreEqual(22, name.IndexOf('|'), "Per-user isolation hash must not be truncated.");
Expand Down
16 changes: 16 additions & 0 deletions src/Service.Tests/UnitTests/SqlMetadataProviderHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ namespace Azure.DataApiBuilder.Service.Tests.UnitTests
[TestClass, TestCategory(TestCategory.MSSQL)]
public class SqlMetadataProviderHelperTests
{
/// <summary>
/// Metadata discovery connections identify DAB before they are opened.
/// </summary>
[TestMethod]
public void CreateConnection_SetsRegisteredApplication()
{
MsSqlMetadataProvider provider = CreateProvider();
MethodInfo method = typeof(MsSqlMetadataProvider).GetMethod(
"CreateConnection", BindingFlags.Instance | BindingFlags.NonPublic)!;

using SqlConnection connection = (SqlConnection)method.Invoke(provider, null)!;

Assert.AreEqual(RegisteredApplication.DataApiBuilder, connection.RegisteredApplication);
Assert.AreEqual(ConnectionState.Closed, connection.State);
}

[TestMethod]
public void InferredDatabaseObjectAccessors_ReturnConfiguredValues()
{
Expand Down
38 changes: 38 additions & 0 deletions src/Service.Tests/UnitTests/SqlQueryExecutorUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,44 @@ public class SqlQueryExecutorUnitTests
// The key of the item stored in http context
private const string TOTAL_DB_EXECUTION_TIME = "TotalDbExecutionTime";

/// <summary>
/// SQL Server and DWSQL connections identify DAB before login without changing the connection string.
/// </summary>
[DataTestMethod]
[DataRow(DatabaseType.MSSQL)]
[DataRow(DatabaseType.DWSQL)]
public void CreateConnection_SetsRegisteredApplication(DatabaseType databaseType)
{
const string connectionString = "Server=localhost;Database=telemetry;Integrated Security=true;Application Name=custom;";
RuntimeConfig config = new(
Schema: "",
DataSource: new(databaseType, connectionString, new()),
Entities: new(new Dictionary<string, Entity>()));
using FileSystemRuntimeConfigLoader loader = new(new MockFileSystem()) { RuntimeConfig = config };
using RuntimeConfigProvider provider = new(loader);
Mock<DbExceptionParser> parser = new(provider);
MsSqlQueryExecutor executor = new(
provider, parser.Object, new Mock<ILogger<IQueryExecutor>>().Object, new HttpContextAccessor());

using SqlConnection connection = executor.CreateConnection(config.DefaultDataSourceName);

Assert.AreEqual(RegisteredApplication.DataApiBuilder, connection.RegisteredApplication);
Assert.AreEqual(ConnectionState.Closed, connection.State);
Assert.AreEqual(executor.ConnectionStringBuilders[config.DefaultDataSourceName].ConnectionString, connection.ConnectionString);
}

/// <summary>
/// Entra ID authentication remains available after upgrading SqlClient.
/// </summary>
[DataTestMethod]
[DataRow(SqlAuthenticationMethod.ActiveDirectoryDefault)]
[DataRow(SqlAuthenticationMethod.ActiveDirectoryManagedIdentity)]
[DataRow(SqlAuthenticationMethod.ActiveDirectoryServicePrincipal)]
public void SqlClient_RegistersAzureAuthenticationProvider(SqlAuthenticationMethod authenticationMethod)
{
Assert.IsNotNull(SqlAuthenticationProvider.GetProvider(authenticationMethod));
}

/// <summary>
/// Validates managed identity token issued ONLY when connection string does not specify
/// User, Password, and Authentication method.
Expand Down
Loading