Skip to content

Improve AAD token acquisition error handling - #3061

Open
Divang Sharma (divang) wants to merge 6 commits into
mainfrom
user/divang/improve-aad-token-error-handling
Open

Divang Sharma (divang) wants to merge 6 commits into
mainfrom
user/divang/improve-aad-token-error-handling

Conversation

@divang

Copy link
Copy Markdown
Contributor

Description

Improves error handling for AAD token acquisition through Managed Identity and DefaultAzureCredential.

Token acquisition previously surfaced timeout and interruption failures as generic Managed Identity token acquisition errors. This change introduces explicit handling for these cases while preserving the underlying exception cause for diagnostics.

Related to #3038

Changes

  • Centralize credential token conversion and error handling for:
    • ManagedIdentityCredential
    • DefaultAzureCredential
  • Report token-acquisition timeouts with a dedicated error message.
  • Report interrupted token acquisition with a dedicated error message.
  • Restore the thread interrupt flag when token acquisition is interrupted.
  • Preserve the original credential failure as the SQLServerException cause.
  • Preserve the credential-cache eviction behavior introduced for transient failures.
  • Continue using the existing error when credential acquisition completes without returning a token.
  • Add focused unit tests for:
    • Successful token conversion
    • Credential failure propagation
    • Token-acquisition timeout
    • Empty token response
    • Thread interruption and interrupt-flag restoration
  • Update the existing Managed Identity timeout test to verify the timeout-specific error.

Testing

mvn -Pjre25 "-Dtest=SQLServerSecurityUtilityTest,SQLServerConnectionUnitTest" test

@github-project-automation github-project-automation Bot moved this to In progress in MSSQL JDBC Sep 29, 2026
@divang Divang Sharma (divang) added the Enhancement An enhancement to the driver. Lower priority than bugs. label Sep 29, 2026
@divang Divang Sharma (divang) added this to the 13.7.0 milestone Sep 29, 2026
@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.53846% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 61.68%. Comparing base (644188c) to head (5908a5b).
⚠️ Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
...microsoft/sqlserver/jdbc/SQLServerMSAL4JUtils.java 75.00% 7 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3061      +/-   ##
============================================
+ Coverage     60.84%   61.68%   +0.84%     
- Complexity     5287     5347      +60     
============================================
  Files           154      154              
  Lines         36734    36872     +138     
  Branches       6742     6796      +54     
============================================
+ Hits          22351    22745     +394     
+ Misses        10598    10270     -328     
- Partials       3785     3857      +72     

☔ 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.

Copilot AI balanced review requested due to automatic review settings October 5, 2026 06: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.

Copilot review overview

🟢 Approval recommended

The focused implementation preserves existing behavior while adding correctly tested diagnostics.

Review effort: Balanced
Findings: None

What changed in this PR

Improves AAD credential failure diagnostics while preserving credential-cache eviction behavior.

Changes:

  • Adds timeout- and interruption-specific error mapping.
  • Restores interrupt status and preserves underlying causes.
  • Adds focused tests and updates timeout assertions.
File Description
SQLServerSecurityUtility.java Centralizes token exception mapping and conversion.
SQLServerResource.java Adds timeout and interruption messages.
SQLServerSecurityUtilityTest.java Tests exception mapping and interrupt restoration.
SQLServerConnectionUnitTest.java Verifies timeout-specific behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI balanced review requested due to automatic review settings October 6, 2026 06:39

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.

Copilot review overview

🟡 Changes recommended

Exception formatting can mask failures, and nested worker interruptions incorrectly interrupt the caller thread.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)

Comment thread src/main/java/com/microsoft/sqlserver/jdbc/SQLServerMSAL4JUtils.java Outdated
Copilot AI balanced review requested due to automatic review settings October 6, 2026 07:45

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.

Copilot review overview

🟡 Changes recommended

Three existing timeout tests still expect the replaced R_connectionTimedOut message.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Resolved since last review (2)

Copilot AI balanced review requested due to automatic review settings October 6, 2026 08:15

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.

Copilot review overview

🔵 Needs a closer look

Worker-thread interruptions can incorrectly set the login thread’s interrupt flag.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Avoid restoring caller interrupt flag for worker-thread interruptions

src/​main/​java/​com/​microsoft/​sqlserver/​jdbc/​SQLServerSecurityUtility.java:500

Only restore the interrupt flag when the current login thread was interrupted. This scans an arbitrary asynchronous credential failure and always interrupts the caller, so an InterruptedException raised by an Azure Identity/Reactor worker will spuriously leave the unrelated login thread interrupted. The MSAL mapper in this PR already avoids this for ExecutionException-wrapped worker interruptions; this path needs the same distinction (for example, identify Reactor's blockOptional interruption wrapper/marker rather than treating every nested interruption as caller interruption).

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.

Copilot review overview

🟢 Approval recommended

The behavior is well tested, with only minor formatter cleanup requested.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
Resolved since last review (1)

Comment thread src/test/java/com/microsoft/sqlserver/jdbc/SQLServerConnectionTest.java Outdated
Refactor assertion for clarity in exception handling.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 6, 2026 10:36

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.

Copilot review overview

🔵 Needs a closer look

Worker-side interruptions can incorrectly mark the calling login thread as interrupted.

Review effort: Balanced
Findings: None

Resolved since last review (1)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Distinguish caller interruption from worker-propagated interruption

src/​main/​java/​com/​microsoft/​sqlserver/​jdbc/​SQLServerSecurityUtility.java:499

This unconditionally interrupts the login thread whenever any nested cause is an InterruptedException. Unlike the direct Future.get path below, this reactive chain may also deliver an interruption raised on an Azure/Reactor worker; in that case the caller was never interrupted, but it is returned to the pool with its interrupt flag set. The MSAL mapper explicitly distinguishes direct caller interruption from an ExecutionException carrying worker interruption (and tests that distinction). Please likewise distinguish an interruption produced by this thread's blockOptional() wait from one propagated by the credential pipeline before restoring the caller flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement An enhancement to the driver. Lower priority than bugs.

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

2 participants