-
Notifications
You must be signed in to change notification settings - Fork 314
Expose SSPI context provider as public #2494
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0"?> | ||
<docs> | ||
<members name="SspiAuthenticationParameters"> | ||
<SspiAuthenticationParameters> | ||
<summary>Provides parameters used during SSPI authentication.</summary> | ||
</SspiAuthenticationParameters> | ||
<ctor> | ||
<summary>Creates an instance of the SspiAuthenticationParameters.</summary> | ||
<param name="serverName">The name of the server.</param> | ||
<param name="resource">The resource (often the server service principal name).</param> | ||
</ctor> | ||
<Resource> | ||
<summary>Gets the resource (often the server service principal name).</summary> | ||
</Resource> | ||
<ServerName> | ||
<summary>Gets the server name.</summary> | ||
</ServerName> | ||
<UserId> | ||
<summary>Gets or sets the user id if available.</summary> | ||
</UserId> | ||
<DatabaseName> | ||
<summary>Gets or sets the database name if available.</summary> | ||
</DatabaseName> | ||
<Password> | ||
<summary>Gets or sets the password if available.</summary> | ||
</Password> | ||
</members> | ||
</docs> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0"?> | ||
<docs> | ||
<members name="SspiContextProvider"> | ||
<SspiContextProvider> | ||
<summary>Provides the ability to customize SSPI context generation.</summary> | ||
</SspiContextProvider> | ||
<ctor> | ||
<summary>Creates an instance of the SSPIContextProvider.</summary> | ||
</ctor> | ||
<GenerateContext> | ||
twsouthwick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<summary>Generates an SSPI outgoing blob given the incoming blob.</summary> | ||
<param name="incomingBlob">Incoming blob</param> | ||
<param name="outgoingBlobWriter">Outgoing blob</param> | ||
<param name="authParams">Gets the authentication parameters associated with this connection.</param> | ||
<returns> | ||
<c>true</c> if the context was generated, otherwise <c>false</c>. | ||
</returns> | ||
</GenerateContext> | ||
</members> | ||
</docs> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Buffers; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.SqlClient.ManualTesting.Tests | ||
|
@@ -56,12 +58,63 @@ public static void IntegratedAuthenticationTest_ServerSPN() | |
TryOpenConnectionWithIntegratedAuthentication(builder.ConnectionString); | ||
} | ||
|
||
[ConditionalFact(nameof(IsIntegratedSecurityEnvironmentSet), nameof(AreConnectionStringsSetup))] | ||
public static void CustomSspiContextGeneratorTest() | ||
{ | ||
SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionString); | ||
builder.IntegratedSecurity = true; | ||
Assert.True(DataTestUtility.ParseDataSource(builder.DataSource, out string hostname, out int port, out string instanceName)); | ||
// Build the SPN for the server we are connecting to | ||
builder.ServerSPN = $"MSSQLSvc/{DataTestUtility.GetMachineFQDN(hostname)}"; | ||
if (!string.IsNullOrWhiteSpace(instanceName)) | ||
{ | ||
builder.ServerSPN += ":" + instanceName; | ||
} | ||
|
||
using SqlConnection conn = new(builder.ConnectionString) | ||
{ | ||
SspiContextProvider = new TestSspiContextProvider(), | ||
}; | ||
|
||
try | ||
{ | ||
conn.Open(); | ||
|
||
Assert.Fail("Expected to use custom SSPI context provider"); | ||
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. It would be nice to find a way to test the positive case, so that we know we can successfully connect via a custom context provider. 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. Yeah, I struggled to figure out how to test it even for this. The main problem with a positive case is that the implementation is wildly different between frameworks so testing it starts being difficult. My thought here was to ensure it gets plumbed through, and the existing tests ensure that once it's being called that it works correctly. If you have ideas here, please let me know how to proceed. |
||
} | ||
catch (SspiTestException sspi) | ||
{ | ||
Assert.Equal(sspi.AuthParams.ServerName, builder.DataSource); | ||
Assert.Equal(sspi.AuthParams.DatabaseName, builder.InitialCatalog); | ||
Assert.Equal(sspi.AuthParams.UserId, builder.UserID); | ||
Assert.Equal(sspi.AuthParams.Password, builder.Password); | ||
} | ||
} | ||
|
||
private static void TryOpenConnectionWithIntegratedAuthentication(string connectionString) | ||
{ | ||
using (SqlConnection connection = new SqlConnection(connectionString)) | ||
{ | ||
connection.Open(); | ||
} | ||
} | ||
|
||
private sealed class TestSspiContextProvider : SspiContextProvider | ||
{ | ||
protected override bool GenerateContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, SspiAuthenticationParameters authParams) | ||
{ | ||
throw new SspiTestException(authParams); | ||
} | ||
} | ||
|
||
private sealed class SspiTestException : Exception | ||
{ | ||
public SspiTestException(SspiAuthenticationParameters authParams) | ||
{ | ||
AuthParams = authParams; | ||
} | ||
|
||
public SspiAuthenticationParameters AuthParams { get; } | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.