Skip to content

Add authentication diagnostics to Azure extension (MSAL + Azure.Identity) - #4872

Merged
cheenamalhotra merged 21 commits into
mainfrom
dev/cheena/dev/automation/azure-msal-logging
Oct 29, 2026
Merged

cheenamalhotra merged 21 commits into
mainfrom
dev/cheena/dev/automation/azure-msal-logging

Conversation

@cheenamalhotra

@cheenamalhotra cheenamalhotra commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Description

Add authentication diagnostics to the Azure extension through SqlClient's EventSource. No callbacks, no PII.

  • Add the AuthenticationTrace keyword (0x2000) to Microsoft.Data.SqlClient.EventSource. Auth events (IDs 22–27) use only this keyword, so they can be collected on their own or together with ordinary Trace (0x2002).
  • Forward MSAL logs (PII disabled), an allowlisted subset of Azure.Identity events, and SqlClient token start/success/failure events. Each event has message, source (SqlClient, MSAL, Azure-Identity) and connectionId.
  • Azure.Identity events are forwarded only during extension-owned token requests, so the app's own Azure SDK traffic is not captured.
  • Add dotnet-trace and in-process EventListener samples; document usage, privacy and known limitations in the package README.

Collect with:

dotnet-trace collect --process-id <pid> --providers Microsoft.Data.SqlClient.EventSource:0x2000:5 --rundown false

Suggested release note: Azure extension authentication diagnostics (MSAL, Azure.Identity and token acquisition) can be collected with the new AuthenticationTrace EventSource keyword.

  • Tests added or updated
  • Public API changes documented
  • Verified with live Azure SQL captures
  • No breaking public API changes identified

Issues

Fixes #1846.

Testing

Executed on macOS with .NET 10:

dotnet test src/Microsoft.Data.SqlClient.Extensions/Azure/test/Azure.Test.csproj -f net10.0 -p:ReferenceType=Project --no-restore

Passed: 104; skipped: 12; failed: 0.

dotnet build src/Microsoft.Data.SqlClient.Extensions/Azure/test/Azure.Test.csproj -f net462 -p:ReferenceType=Project --no-restore -warnaserror
dotnet build src/Microsoft.Data.SqlClient.Extensions/Azure/src/Azure.csproj -p:ReferenceType=Project --no-restore -warnaserror
dotnet build doc/samples/Microsoft.Data.SqlClient.Samples.csproj
git diff --check

All passed. net462 tests compiled, not executed.

Live Azure SQL captures with dotnet-trace (0x2000:5):

  • ActiveDirectoryDefault: connected; 7 auth-only events (start, skipped chain credentials, AzureCliCredential selected, success), one connection ID, no ordinary Trace events, none lost.
  • ActiveDirectoryManagedIdentity (no MI on host): expected failure; 41 events including MSAL MI flow, the Azure.Identity failure and SqlClient Token acquisition failed., one connection ID.
  • ActiveDirectoryInteractive (earlier run): connected; MSAL and SqlClient events correlated by connection ID.

Not run: Windows/.NET Framework runtime, WAM broker, and performance measurements.

Guidelines

Please review the contribution guidelines before submitting a pull request:

Adds an authentication logging callback and MSAL PII opt-in to
ActiveDirectoryAuthenticationProviderOptions, with EventSource tracing
for MSAL and Azure Identity diagnostics.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 9881423d-614d-429e-ba7d-612fbfbcb52e
@cheenamalhotra
cheenamalhotra requested a review from a team as a code owner October 19, 2026 23:13
Copilot AI balanced review requested due to automatic review settings October 19, 2026 23:13
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Oct 19, 2026
@cheenamalhotra cheenamalhotra added this to the 8.0.0 milestone Oct 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Privacy-sensitive event forwarding and shared token-cache synchronization warrant human review, particularly for unexecuted Windows, .NET Framework, and WAM runtime paths.

No open issues 🎉

What changed in this PR

Adds Azure extension authentication diagnostics through independent EventSource tracing and optional application callbacks, addressing issue #1846.

Changes:

  • Introduces SqlClient-owned logging contracts and callback-only MSAL PII opt-in.
  • Adds scoped SDK logging and coordinated token caches across compatible provider replacements.
  • Adds regression tests, API documentation, and a usage sample.
File Description
src/​Microsoft.Data.SqlClient.Internal/​Logging/​src/​SqlClientEventSource.cs Adds severity-specific authentication events.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​test/​WamBrokerTests.cs Updates callback-state reflection assertions.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​test/​AuthenticationLoggingTests.cs Tests privacy, concurrency, caching, and callback isolation.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​src/​SqlAuthenticationLogLevel.cs Defines owned diagnostic severities.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​src/​SqlAuthenticationLogEntry.cs Defines immutable diagnostic entries.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​src/​PackageReadme.md Documents logging configuration and privacy boundaries.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​src/​AzureAuthenticationEventListener.cs Bridges selected Azure Identity events.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​src/​AuthenticationLogger.cs Routes scoped diagnostics to independent outputs.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​src/​ActiveDirectoryAuthenticationProviderOptions.cs Adds callback and PII options.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​src/​ActiveDirectoryAuthenticationProvider.Windows.cs Separates window callback state from logging ownership.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​src/​ActiveDirectoryAuthenticationProvider.cs Integrates logging and shared token-cache coordination.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​doc/​SqlAuthenticationLogLevel.xml Documents severity semantics.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​doc/​SqlAuthenticationLogEntry.xml Documents entry lifetime and sensitive-data handling.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​doc/​ActiveDirectoryAuthenticationProviderOptions.xml Documents callback requirements and PII restrictions.
src/​Microsoft.Data.SqlClient.Extensions/​Azure/​doc/​ActiveDirectoryAuthenticationProvider.xml Documents logging and provider replacement behavior.
doc/​samples/​SqlAuthenticationLogging_Example.cs Demonstrates tracing and optional callbacks.

Review effort: Balanced

@cheenamalhotra cheenamalhotra added the Public API 🆕 Issues/PRs that introduce new APIs to the driver. label Oct 19, 2026
@cheenamalhotra cheenamalhotra modified the milestones: 8.0.0, 8.0.0-preview1 Oct 19, 2026
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 9881423d-614d-429e-ba7d-612fbfbcb52e
Copilot AI balanced review requested due to automatic review settings October 19, 2026 23:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Privacy-sensitive forwarding and shared token-cache synchronization need human review, particularly with Windows, .NET Framework, and WAM runtime paths unverified.

No open issues 🎉

Review effort: Balanced

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 9881423d-614d-429e-ba7d-612fbfbcb52e
Copilot AI balanced review requested due to automatic review settings October 19, 2026 23:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Privacy-sensitive forwarding and token-cache synchronization need human review, particularly with Windows, .NET Framework, and WAM runtime paths unverified.

No open issues 🎉

Review effort: Balanced

@cheenamalhotra cheenamalhotra moved this from To triage to In review in SqlClient Board Oct 20, 2026
@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.13%. Comparing base (7b1184b) to head (0847eeb).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4872      +/-   ##
==========================================
- Coverage   67.94%   66.13%   -1.82%     
==========================================
  Files         311      305       -6     
  Lines       45399    68294   +22895     
==========================================
+ Hits        30845    45164   +14319     
- Misses      14554    23130    +8576     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 66.13% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@paulmedynski
paulmedynski self-requested a review October 21, 2026 17:32

@benrr101 benrr101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a huge change and I'm really concerned we're building something from scratch that we either already have or can leverage an external package to provide. Let's consider this a bit more in-depth before we rush to get it merged

@github-project-automation github-project-automation Bot moved this from In review to Waiting for customer in SqlClient Board Oct 21, 2026
Add braces, use auto-property, drop wrapper property, use named
arguments and ternaries, and add Arrange/Act/Assert headers.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 9881423d-614d-429e-ba7d-612fbfbcb52e
Copilot AI balanced review requested due to automatic review settings October 21, 2026 17:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unrestricted SDK forwarding can expose sensitive application diagnostics, and unguarded collector dispatch can cause recursive authentication logging.

2 open issues

Review effort: Balanced

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 9881423d-614d-429e-ba7d-612fbfbcb52e
Copilot AI balanced review requested due to automatic review settings October 23, 2026 05:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Subscription synchronization remains unresolved, and process-wide forwarding needs human sign-off with Windows/.NET Framework runtime behavior unverified.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Prevent stale initial subscription from overriding collector updates

src/​Microsoft.Data.SqlClient.Extensions/​Azure/​src/​AzureAuthenticationEventListener.cs:139

[P2] Coordinate the initial subscription refresh with collector commands. The constructor calls ApplySubscriptions() outside EventSource's subscription lock. For an existing Azure-Identity source, it can set state.Level to Informational here and pause before EnableEvents. A concurrent collector command can then set and apply Verbose, after which the constructor applies its stale Informational subscription. The version check retries, but ApplySource skips the repair because the cached level is already Verbose. Requested Verbose SDK events are then dropped until a later subscription update repairs the mismatch. Coordinate initialization with command-driven updates, or reconcile stale subscription calls without trusting the cached level. Preserve reentrant command handling and add a concurrent first-use/collector-enable regression test.

2 resolved since last review

Review effort: Balanced

benrr101
benrr101 previously approved these changes Oct 23, 2026
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 9881423d-614d-429e-ba7d-612fbfbcb52e
Copilot AI balanced review requested due to automatic review settings October 26, 2026 17:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A startup subscription race can suppress requested diagnostics, and the missing-window WAM path omits its terminal failure event.

2 open issues

Review effort: Balanced

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 9881423d-614d-429e-ba7d-612fbfbcb52e
Copilot AI balanced review requested due to automatic review settings October 26, 2026 18:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Process-wide diagnostic forwarding and subscription synchronization need human sign-off, with Windows, .NET Framework, and WAM runtime validation still outstanding.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 9881423d-614d-429e-ba7d-612fbfbcb52e
Copilot AI balanced review requested due to automatic review settings October 26, 2026 19:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Subscription concurrency and accepted process-wide privacy trade-offs warrant human sign-off, with Windows/.NET Framework and WAM runtime validation still outstanding.

No open issues 🎉

Review effort: Balanced

@cheenamalhotra
cheenamalhotra dismissed paulmedynski’s stale review October 26, 2026 21:06

Stale review, requested review again

@paulmedynski paulmedynski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making changes related to my previous feedback. I think this updated approach is simpler. The Azure-Identity listener is still quite complicated though, and could use expanded comments/docs. I'll continue my review once that is added.

- Add class, field, and method-body docs to AzureAuthenticationEventListener.
- Expand listener initialization comment in the provider.
- Include the authentication method in SqlClient token events.
- Parse MSAL correlation IDs with TryParseExact("D").

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 9881423d-614d-429e-ba7d-612fbfbcb52e
Copilot AI balanced review requested due to automatic review settings October 27, 2026 15:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Process-wide forwarding and reentrant subscription management warrant human review, with Windows/.NET Framework and WAM runtime validation still outstanding.

No open issues 🎉

Review effort: Balanced

@cheenamalhotra
cheenamalhotra merged commit b368afd into main Oct 29, 2026
123 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Public API 🆕 Issues/PRs that introduce new APIs to the driver.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Expose MSAL logs to the application

5 participants