Skip to content

Test | Fix Kerberos SSPI authentication failure when using TCP proxy in manual tests - #4146

Merged
cheenamalhotra merged 1 commit into
dotnet:mainfrom
shreyarao4:fix
May 5, 2026
Merged

Test | Fix Kerberos SSPI authentication failure when using TCP proxy in manual tests#4146
cheenamalhotra merged 1 commit into
dotnet:mainfrom
shreyarao4:fix

Conversation

@shreyarao4

Copy link
Copy Markdown
Contributor

Problem:
CreateAndStartProxy used in manual tests rewrites the connection string address to 127.0.0.1, causing kerberos to construct the wrong SPN (MSSQLSvc/localhost) instead of the real server name. Since localhost is not registered in Active Directory, SSPI authentication fails with
Microsoft.Data.SqlClient.SqlException: The target principal name is incorrect. Cannot generate SSPI context.

Kerberos builds the SPN from the resolved connection address. When the proxy rewrites the address to 127.0.0.1, the SPN is constructed from localhost instead of the actual server hostname, breaking AD lookup.

Fix:
Capture the original server name before the proxy rewrites the address. After rewriting, explicitly set ServerSPN in the connection string to the real server's SPN using DataTestUtility.GetMachineFQDN, but only if ServerSPN is not already set by the caller.
This fix only applies if ServerSPN is not set already.

Tests Affected:
SqlCommandCancelTest.TimeOutDuringRead_Tcp— previously failing with SSPI error, now passing

@shreyarao4
shreyarao4 requested a review from a team as a code owner April 7, 2026 05:58
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Apr 7, 2026
@paulmedynski paulmedynski added this to the 7.1.0-preview1 milestone Apr 7, 2026
@paulmedynski paulmedynski added the Area\Tests Issues that are targeted to tests or test projects label Apr 7, 2026
@paulmedynski paulmedynski self-assigned this Apr 7, 2026
@paulmedynski paulmedynski moved this from To triage to In review in SqlClient Board Apr 7, 2026
@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@codecov

codecov Bot commented Apr 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 64.39%. Comparing base (2a6280a) to head (641c828).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4146      +/-   ##
==========================================
- Coverage   65.92%   64.39%   -1.53%     
==========================================
  Files         276      272       -4     
  Lines       42962    65784   +22822     
==========================================
+ Hits        28322    42363   +14041     
- Misses      14640    23421    +8781     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 64.39% <ø> (?)

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

☔ View full report in Codecov by Sentry.
📢 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.

@cheenamalhotra cheenamalhotra changed the title Fix Kerberos SSPI authentication failure when using TCP proxy in manual tests Test | Fix Kerberos SSPI authentication failure when using TCP proxy in manual tests Apr 8, 2026
@cheenamalhotra
cheenamalhotra enabled auto-merge (squash) April 8, 2026 00:45
@shreyarao4

Copy link
Copy Markdown
Contributor Author

Can this be merged?

@cheenamalhotra

Copy link
Copy Markdown
Member

There are failures preventing this merge, it's strange why they are happening only on this branch. Can you update this branch with changes from 'main'?

@cheenamalhotra
cheenamalhotra disabled auto-merge April 24, 2026 08:09
@cheenamalhotra cheenamalhotra moved this from In review to Waiting for customer in SqlClient Board Apr 24, 2026
…al tests

Problem:
CreateAndStartProxy used in manual tests rewrites the connection string address to 127.0.0.1, causing kerberos to construct the wrong SPN (MSSQLSvc/localhost) instead of the real server name.
Since localhost is not registered in Active Directory, SSPI authentication fails with
Microsoft.Data.SqlClient.SqlException: The target principal name is incorrect. Cannot generate SSPI context.

Fix:
Capture the original server name before the proxy rewrites the address. After rewriting, explicitly set ServerSPN in the connection string to the real server's SPN using DataTestUtility.GetMachineFQDN,
but only if ServerSPN is not already set by the caller.

Tests Affected:
SqlCommandCancelTest.TimeOutDuringRead_Tcp— previously failing with SSPI error, now passing
@shreyarao4

Copy link
Copy Markdown
Contributor Author

@cheenamalhotra I have updated the branch

@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@shreyarao4

Copy link
Copy Markdown
Contributor Author

@cheenamalhotra Can this be merged?

@cheenamalhotra
cheenamalhotra enabled auto-merge (squash) May 5, 2026 05:07
@cheenamalhotra
cheenamalhotra merged commit 34b400e into dotnet:main May 5, 2026
301 checks passed
@github-project-automation github-project-automation Bot moved this from Waiting for customer to Done in SqlClient Board May 5, 2026
paulmedynski pushed a commit that referenced this pull request May 7, 2026
…al tests (#4146)

Problem:
CreateAndStartProxy used in manual tests rewrites the connection string address to 127.0.0.1, causing kerberos to construct the wrong SPN (MSSQLSvc/localhost) instead of the real server name.
Since localhost is not registered in Active Directory, SSPI authentication fails with
Microsoft.Data.SqlClient.SqlException: The target principal name is incorrect. Cannot generate SSPI context.

Fix:
Capture the original server name before the proxy rewrites the address. After rewriting, explicitly set ServerSPN in the connection string to the real server's SPN using DataTestUtility.GetMachineFQDN,
but only if ServerSPN is not already set by the caller.

Tests Affected:
SqlCommandCancelTest.TimeOutDuringRead_Tcp— previously failing with SSPI error, now passing
cheenamalhotra pushed a commit that referenced this pull request Jul 14, 2026
…al tests (#4146)

Problem:
CreateAndStartProxy used in manual tests rewrites the connection string address to 127.0.0.1, causing kerberos to construct the wrong SPN (MSSQLSvc/localhost) instead of the real server name.
Since localhost is not registered in Active Directory, SSPI authentication fails with
Microsoft.Data.SqlClient.SqlException: The target principal name is incorrect. Cannot generate SSPI context.

Fix:
Capture the original server name before the proxy rewrites the address. After rewriting, explicitly set ServerSPN in the connection string to the real server's SPN using DataTestUtility.GetMachineFQDN,
but only if ServerSPN is not already set by the caller.

Tests Affected:
SqlCommandCancelTest.TimeOutDuringRead_Tcp— previously failing with SSPI error, now passing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area\Tests Issues that are targeted to tests or test projects

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants