-
Couldn't load subscription status.
- Fork 316
Prelogin handlers for connectivity #2601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prelogin handlers for connectivity #2601
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realise this is still a draft PR, but it's the first set of handlers which cover real-world TDS traffic so I thought it best to comment early. There's probably quite a bit of overlap with your own testing!
| SqlClientEventSource.Log.TryTraceEvent("<sc|{0}|{1}|{2}>{3}", | ||
| nameof(PreloginHandler), | ||
| nameof(AuthenticateClientInternal), | ||
| SqlClientLogger.LogLevel.Warning, | ||
| warningMessage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At present, logging is a little inconsistent between SqlClientEventSource and request.ConnectionContext.Logger. It'd be a good idea to try to try and unify these (perhaps making all new logs use request.ConnectionContext.Logger, which would leave an integration point for the OpenTelemetry integration.)
In the meantime, we can make a simple change to interpolate this string at compile time rather than force the runtime to parse a format string. The same applies to other instances.
| SqlClientEventSource.Log.TryTraceEvent("<sc|{0}|{1}|{2}>{3}", | |
| nameof(PreloginHandler), | |
| nameof(AuthenticateClientInternal), | |
| SqlClientLogger.LogLevel.Warning, | |
| warningMessage); | |
| SqlClientEventSource.Log.TryTraceEvent($"<sc|{nameof(PreloginHandler)}|{nameof(AuthenticateClientInternal)}|{SqlClientLogger.LogLevel.Warning}>{warningMessage}"); |
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIHandle.cs
Outdated
Show resolved
Hide resolved
...ore/src/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/TlsBeginHandler.cs
Outdated
Show resolved
Hide resolved
...ore/src/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/TlsBeginHandler.cs
Outdated
Show resolved
Hide resolved
...tcore/src/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/TlsEndHandler.cs
Outdated
Show resolved
Hide resolved
...tcore/src/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/TlsEndHandler.cs
Outdated
Show resolved
Hide resolved
...ft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Handlers/ConnectionHandlerContext.cs
Show resolved
Hide resolved
.../Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginHandlerContext.cs
Outdated
Show resolved
Hide resolved
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Show resolved
Hide resolved
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Outdated
Show resolved
Hide resolved
...core/src/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/BaseTlsHandler.cs
Outdated
Show resolved
Hide resolved
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Show resolved
Hide resolved
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Outdated
Show resolved
Hide resolved
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Outdated
Show resolved
Hide resolved
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Outdated
Show resolved
Hide resolved
...core/src/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/BaseTlsHandler.cs
Outdated
Show resolved
Hide resolved
....Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginHandler.cs
Outdated
Show resolved
Hide resolved
|
@VladimirReshetnikov I have taken care of all the review comments. |
| return (InternalEncryptionOption == EncryptionOptions.ON && !TrustServerCertificate) | ||
| || (ConnectionContext.AccessTokenInBytes != null && !TrustServerCertificate); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return (InternalEncryptionOption == EncryptionOptions.ON && !TrustServerCertificate) | |
| || (ConnectionContext.AccessTokenInBytes != null && !TrustServerCertificate); | |
| return !TrustServerCertificate && (InternalEncryptionOption == EncryptionOptions.ON | |
| || ConnectionContext.AccessTokenInBytes); |
Nit: readability suggestion.
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Show resolved
Hide resolved
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Outdated
Show resolved
Hide resolved
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Show resolved
Hide resolved
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Show resolved
Hide resolved
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Show resolved
Hide resolved
...ft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Handlers/ConnectionHandlerContext.cs
Show resolved
Hide resolved
...c/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/PreloginPacketHandler.cs
Show resolved
Hide resolved
...re/src/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/TlsAuthenticator.cs
Show resolved
Hide resolved
...ft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Handlers/ConnectionHandlerContext.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Handlers/ErrorExtensions.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Handlers/ErrorExtensions.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Handlers/ErrorExtensions.cs
Show resolved
Hide resolved
...re/src/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/TlsAuthenticator.cs
Show resolved
Hide resolved
...re/src/Microsoft/Data/SqlClientX/Handlers/Connection/PreloginSubHandlers/TlsAuthenticator.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Handlers/ErrorExtensions.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Handlers/HandlerRequest.cs
Outdated
Show resolved
Hide resolved
|
@cheenamalhotra addressed all comments. |
|
|
||
| SqlClientEventSource.Log.TrySNITraceEvent(nameof(PreloginHandler), EventType.INFO, "Connection Id {0}, Certificate will be validated for Target Server name", args0: connectionId); | ||
|
|
||
| return SNICommon.ValidateSslServerCertificate(connectionId, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not critical at the moment, but these dependencies on Managed SNI can make things complicated for both SqlClient and SqlClientX, so I would recommend adding a comment in such API docs that they are shared APIs, and should be handled carefully.
Adding Prelogin handler
There is a Handler called PreloginHandler, which is composed of two subhandlers depending on the Encryption option of the Connection String.
Encrypt = Strict signifies TDS8 and anything else means TDS7
For TDS8, the chain in prelogin looks like Tds8TlsHandler -> PreloginPacketHandler
Else PreloginPacketHandler -> Tds74TlsHandler.
The creation of SslStreams, and their ordering is done in the PreloginHandler itself.
Tds8TlsHandler and Tds74TlsHandlers, take care of TLS before or after prelogin exchange depending on the protocol.
The code in this PR hasn't changed anything from the existing code, except removing checks for unsupported SQL Server versions.
Tests have been added.
I also tested end to end (not checkedin) by creating the DataSourceParsingHandler>TransportCreationHandler > PreloginHandler in a chain.
To test TDS8, I used Azure SQL DB and for TDS74, I used a local server.
How to review
Start from the
PreloginHandlerclass which is the handler entrypoint for Prelogin. All the Tds8 specific handling is inTds8TlsHandlerand Tds74 handling is inTds74TlsHandler.The transmission and reading Prelogin packet is available in
PreloginPacketHandlerTests
Unit tests have been added.
For sanity integration test I used the following in a Unit test to validate connectivity. However this snippet is not checked in