Skip to content

Commit 806b059

Browse files
committed
only return the required resolved spn
1 parent 99335ca commit 806b059

File tree

5 files changed

+22
-23
lines changed

5 files changed

+22
-23
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal class SNIProxy
3434
/// <param name="fullServerName">Full server name from connection string</param>
3535
/// <param name="timeout">Timer expiration</param>
3636
/// <param name="instanceName">Instance name</param>
37-
/// <param name="spns">SPNs</param>
37+
/// <param name="resolvedSpn">SPN</param>
3838
/// <param name="serverSPN">pre-defined SPN</param>
3939
/// <param name="flushCache">Flush packet cache</param>
4040
/// <param name="async">Asynchronous connection</param>
@@ -51,7 +51,7 @@ internal static SNIHandle CreateConnectionHandle(
5151
string fullServerName,
5252
TimeoutTimer timeout,
5353
out byte[] instanceName,
54-
ref string[] spns,
54+
out string resolvedSpn,
5555
string serverSPN,
5656
bool flushCache,
5757
bool async,
@@ -65,6 +65,7 @@ internal static SNIHandle CreateConnectionHandle(
6565
string serverCertificateFilename)
6666
{
6767
instanceName = new byte[1];
68+
resolvedSpn = default;
6869

6970
bool errorWithLocalDBProcessing;
7071
string localDBDataSource = GetLocalDBDataSource(fullServerName, out errorWithLocalDBProcessing);
@@ -103,7 +104,7 @@ internal static SNIHandle CreateConnectionHandle(
103104
{
104105
try
105106
{
106-
spns = GetSqlServerSPNs(details, serverSPN);
107+
resolvedSpn = GetSqlServerSPNs(details, serverSPN);
107108
}
108109
catch (Exception e)
109110
{
@@ -115,12 +116,12 @@ internal static SNIHandle CreateConnectionHandle(
115116
return sniHandle;
116117
}
117118

118-
private static string[] GetSqlServerSPNs(DataSource dataSource, string serverSPN)
119+
private static string GetSqlServerSPNs(DataSource dataSource, string serverSPN)
119120
{
120121
Debug.Assert(!string.IsNullOrWhiteSpace(dataSource.ServerName));
121122
if (!string.IsNullOrWhiteSpace(serverSPN))
122123
{
123-
return new[] { serverSPN };
124+
return serverSPN;
124125
}
125126

126127
string hostName = dataSource.ServerName;
@@ -138,7 +139,7 @@ private static string[] GetSqlServerSPNs(DataSource dataSource, string serverSPN
138139
return GetSqlServerSPNs(hostName, postfix, dataSource.ResolvedProtocol);
139140
}
140141

141-
private static string[] GetSqlServerSPNs(string hostNameOrAddress, string portOrInstanceName, DataSource.Protocol protocol)
142+
private static string GetSqlServerSPNs(string hostNameOrAddress, string portOrInstanceName, DataSource.Protocol protocol)
142143
{
143144
Debug.Assert(!string.IsNullOrWhiteSpace(hostNameOrAddress));
144145
IPHostEntry hostEntry = null;
@@ -169,12 +170,12 @@ private static string[] GetSqlServerSPNs(string hostNameOrAddress, string portOr
169170
string serverSpnWithDefaultPort = serverSpn + $":{DefaultSqlServerPort}";
170171
// Set both SPNs with and without Port as Port is optional for default instance
171172
SqlClientEventSource.Log.TryAdvancedTraceEvent("SNIProxy.GetSqlServerSPN | Info | ServerSPNs {0} and {1}", serverSpn, serverSpnWithDefaultPort);
172-
return new[] { serverSpn, serverSpnWithDefaultPort };
173+
return serverSpnWithDefaultPort;
173174
}
174175
// else Named Pipes do not need to valid port
175176

176177
SqlClientEventSource.Log.TryAdvancedTraceEvent("SNIProxy.GetSqlServerSPN | Info | ServerSPN {0}", serverSpn);
177-
return new[] { serverSpn };
178+
return serverSpn;
178179
}
179180

180181
/// <summary>

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -437,14 +437,12 @@ internal void Connect(ServerInfo serverInfo,
437437

438438
_connHandler.pendingSQLDNSObject = null;
439439

440-
string[] serverSpns = null;
441-
442440
// AD Integrated behaves like Windows integrated when connecting to a non-fedAuth server
443441
_physicalStateObj.CreatePhysicalSNIHandle(
444442
serverInfo.ExtendedServerName,
445443
timeout,
446444
out instanceName,
447-
ref serverSpns,
445+
out var serverSpn,
448446
false,
449447
true,
450448
fParallel,
@@ -542,7 +540,7 @@ internal void Connect(ServerInfo serverInfo,
542540
serverInfo.ExtendedServerName,
543541
timeout,
544542
out instanceName,
545-
ref serverSpns,
543+
out serverSpn,
546544
true,
547545
true,
548546
fParallel,
@@ -593,10 +591,10 @@ internal void Connect(ServerInfo serverInfo,
593591
}
594592
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Prelogin handshake successful");
595593

596-
// We need to initialize the authentication provider with the server SPN
597-
// This array will either be a single entry with the SPN or two entries with the second
598-
// one being including a default port.
599-
_authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this, serverSpns[^1]);
594+
if (_authenticationProvider is { } && serverSpn is { })
595+
{
596+
_authenticationProvider.Initialize(serverInfo, _physicalStateObj, this, serverSpn);
597+
}
600598

601599
if (_fMARS && marsCapable)
602600
{

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal TdsParserStateObject(TdsParser parser, TdsParserStateObject physicalCon
6262
AddError(parser.ProcessSNIError(this));
6363
ThrowExceptionAndWarning();
6464
}
65-
65+
6666
// we post a callback that represents the call to dispose; once the
6767
// object is disposed, the next callback will cause the GC Handle to
6868
// be released.
@@ -181,7 +181,7 @@ internal abstract void CreatePhysicalSNIHandle(
181181
string serverName,
182182
TimeoutTimer timeout,
183183
out byte[] instanceName,
184-
ref string[] spns,
184+
out string resolvedSpn,
185185
bool flushCache,
186186
bool async,
187187
bool fParallel,

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ internal override void CreatePhysicalSNIHandle(
8181
string serverName,
8282
TimeoutTimer timeout,
8383
out byte[] instanceName,
84-
ref string[] spns,
84+
out string resolvedSpn,
8585
bool flushCache,
8686
bool async,
8787
bool parallel,
@@ -94,7 +94,7 @@ internal override void CreatePhysicalSNIHandle(
9494
string hostNameInCertificate,
9595
string serverCertificateFilename)
9696
{
97-
SNIHandle? sessionHandle = SNIProxy.CreateConnectionHandle(serverName, timeout, out instanceName, ref spns, serverSPN,
97+
SNIHandle? sessionHandle = SNIProxy.CreateConnectionHandle(serverName, timeout, out instanceName, out resolvedSpn, serverSPN,
9898
flushCache, async, parallel, isIntegratedSecurity, iPAddressPreference, cachedFQDN, ref pendingDNSInfo, tlsFirst,
9999
hostNameInCertificate, serverCertificateFilename);
100100

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ internal override void CreatePhysicalSNIHandle(
144144
string serverName,
145145
TimeoutTimer timeout,
146146
out byte[] instanceName,
147-
ref string[] spns,
147+
out string resolvedSpn,
148148
bool flushCache,
149149
bool async,
150150
bool fParallel,
@@ -178,7 +178,7 @@ internal override void CreatePhysicalSNIHandle(
178178

179179
_sessionHandle = new SNIHandle(myInfo, serverName, ref serverSPN, timeout.MillisecondsRemainingInt, out instanceName,
180180
flushCache, !async, fParallel, ipPreference, cachedDNSInfo, hostNameInCertificate);
181-
spns = new[] { serverSPN.TrimEnd() };
181+
resolvedSpn = serverSPN.TrimEnd();
182182
}
183183

184184
protected override uint SniPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize)
@@ -423,7 +423,7 @@ internal override uint WaitForSSLHandShakeToComplete(out int protocolVersion)
423423
}
424424
else if (nativeProtocol.HasFlag(NativeProtocols.SP_PROT_SSL3_CLIENT) || nativeProtocol.HasFlag(NativeProtocols.SP_PROT_SSL3_SERVER))
425425
{
426-
// SSL 2.0 and 3.0 are only referenced to log a warning, not explicitly used for connections
426+
// SSL 2.0 and 3.0 are only referenced to log a warning, not explicitly used for connections
427427
#pragma warning disable CS0618, CA5397
428428
protocolVersion = (int)SslProtocols.Ssl3;
429429
}

0 commit comments

Comments
 (0)