-
Notifications
You must be signed in to change notification settings - Fork 311
Expose SspiAuthenticationParameters on SspiContextProvider #2454
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
Changes from 2 commits
8adfef4
bf59162
72f1c99
7f1f800
e9c04ad
a0f5c7a
7c9b0f3
454da94
4e07c89
9536950
a813d22
ca8fbea
f7c90b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
twsouthwick marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -26,24 +26,78 @@ private protected virtual void Initialize() | |||
{ | ||||
} | ||||
|
||||
protected abstract void GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, ReadOnlySpan<string> serverSpns); | ||||
protected abstract bool GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, SqlAuthenticationParameters authParams); | ||||
|
||||
internal void SSPIData(ReadOnlySpan<byte> receivedBuff, IBufferWriter<byte> outgoingBlobWriter, string serverSpn) | ||||
=> SSPIData(receivedBuff, outgoingBlobWriter, new[] { serverSpn }); | ||||
{ | ||||
using var _ = TrySNIEventScope.Create(nameof(SSPIContextProvider)); | ||||
|
||||
internal void SSPIData(ReadOnlySpan<byte> receivedBuff, IBufferWriter<byte> outgoingBlobWriter, string[] serverSpns) | ||||
if (!RunGenerateSspiClientContext(receivedBuff, outgoingBlobWriter, serverSpn)) | ||||
{ | ||||
// If we've hit here, the SSPI context provider implementation failed to generate the SSPI context. | ||||
SSPIError(SQLMessage.SSPIGenerateError(), TdsEnums.GEN_CLIENT_CONTEXT); | ||||
} | ||||
} | ||||
|
||||
internal void SSPIData(ReadOnlySpan<byte> receivedBuff, IBufferWriter<byte> outgoingBlobWriter, ReadOnlySpan<string> serverSpns) | ||||
{ | ||||
using (TrySNIEventScope.Create(nameof(SSPIContextProvider))) | ||||
using var _ = TrySNIEventScope.Create(nameof(SSPIContextProvider)); | ||||
|
||||
foreach (var serverSpn in serverSpns) | ||||
{ | ||||
try | ||||
{ | ||||
GenerateSspiClientContext(receivedBuff, outgoingBlobWriter, serverSpns); | ||||
} | ||||
catch (Exception e) | ||||
if (RunGenerateSspiClientContext(receivedBuff, outgoingBlobWriter, serverSpn)) | ||||
{ | ||||
SSPIError(e.Message + Environment.NewLine + e.StackTrace, TdsEnums.GEN_CLIENT_CONTEXT); | ||||
return; | ||||
} | ||||
} | ||||
|
||||
// If we've hit here, the SSPI context provider implementation failed to generate the SSPI context. | ||||
SSPIError(SQLMessage.SSPIGenerateError(), TdsEnums.GEN_CLIENT_CONTEXT); | ||||
} | ||||
|
||||
private bool RunGenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, string serverSpn) | ||||
{ | ||||
var authParams = CreateSqlAuthParams(_parser.Connection, serverSpn); | ||||
|
||||
try | ||||
{ | ||||
#if NET8_0_OR_GREATER | ||||
SqlClientEventSource.Log.TryTraceEvent("{0}.{1} | Info | Session Id {2}, SPN={3}", GetType().FullName, | ||||
twsouthwick marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
nameof(GenerateSspiClientContext), _physicalStateObj.SessionId, serverSpn); | ||||
#else | ||||
SqlClientEventSource.Log.TryTraceEvent("{0}.{1} | Info | SPN={1}", GetType().FullName, | ||||
nameof(GenerateSspiClientContext), serverSpn); | ||||
#endif | ||||
|
||||
return GenerateSspiClientContext(incomingBlob, outgoingBlobWriter, authParams); | ||||
} | ||||
catch (Exception e) | ||||
{ | ||||
SSPIError(e.Message + Environment.NewLine + e.StackTrace, TdsEnums.GEN_CLIENT_CONTEXT); | ||||
return false; | ||||
} | ||||
} | ||||
|
||||
private static SqlAuthenticationParameters CreateSqlAuthParams(SqlInternalConnectionTds connection, string serverSpn) | ||||
{ | ||||
var auth = new SqlAuthenticationParameters.Builder( | ||||
twsouthwick marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
authenticationMethod: connection.ConnectionOptions.Authentication, | ||||
resource: null, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to think of the best way to populate this. In other usage, I see serverSpn getting set as the resource and dataSource getting set as the serverName. I think it makes sense to continue that pattern here given that serverName and server SPN may be different. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 2437 in e0fa87b
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also not sure how to feel about authority. In federated auth, it's assumed that authority will be present, but here we don't set a value. Let me chat with the team to see if we'd prefer to create a new type to better represent this set of credentials. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. I refactored the builder so that the same pattern can be had wherever it's used if we want to use the existing |
||||
authority: null, | ||||
serverName: serverSpn, | ||||
connection.ConnectionOptions.InitialCatalog); | ||||
|
||||
if (connection.ConnectionOptions.UserID is { } userId) | ||||
{ | ||||
auth.WithUserId(userId); | ||||
} | ||||
|
||||
if (connection.ConnectionOptions.Password is { } password) | ||||
{ | ||||
auth.WithPassword(password); | ||||
} | ||||
|
||||
return auth; | ||||
twsouthwick marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
} | ||||
|
||||
protected void SSPIError(string error, string procedure) | ||||
|
Uh oh!
There was an error while loading. Please reload this page.