Skip to content

Commit 86562e1

Browse files
wfurtstephentoub
andauthored
make use of ports in SPN optional (#57159)
* make port optional in SPN * fix tests * feedback from review * Update src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/AuthenticationHelper.NtAuth.cs Co-authored-by: Stephen Toub <stoub@microsoft.com> * fix build Co-authored-by: Stephen Toub <stoub@microsoft.com>
1 parent 4f60ea9 commit 86562e1

File tree

7 files changed

+67
-13
lines changed

7 files changed

+67
-13
lines changed

src/libraries/Common/tests/System/Net/EnterpriseTests/EnterpriseTestConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public static class EnterpriseTestConfiguration
77
{
88
public const string Realm = "LINUX.CONTOSO.COM";
99
public const string NegotiateAuthWebServer = "http://apacheweb.linux.contoso.com/auth/kerberos/";
10+
public const string NegotiateAuthWebServerNotDefaultPort = "http://apacheweb.linux.contoso.com:8081/auth/kerberos/";
1011
public const string AlternativeService = "http://altweb.linux.contoso.com:8080/auth/kerberos/";
1112
public const string NtlmAuthWebServer = "http://apacheweb.linux.contoso.com:8080/auth/ntlm/";
1213
public const string DigestAuthWebServer = "http://apacheweb.linux.contoso.com/auth/digest/";

src/libraries/Common/tests/System/Net/EnterpriseTests/setup/apacheweb/apache2.conf

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Listen 8080
5454
</IfDefine>
5555
<IfDefine !ALTPORT>
5656
Listen 80
57+
Listen 8081
5758
</IfDefine>
5859

5960
#
@@ -238,7 +239,7 @@ Group daemon
238239
# e-mailed. This address appears on some server-generated pages, such
239240
# as error documents. e.g. admin@your-domain.com
240241
#
241-
ServerAdmin you@example.com
242+
ServerAdmin webmaster@contoso.com
242243

243244
#
244245
# ServerName gives the name and port that the server uses to identify itself.
@@ -583,11 +584,18 @@ SSLRandomSeed startup builtin
583584
SSLRandomSeed connect builtin
584585
</IfModule>
585586

587+
<IfDefine ALTPORT>
586588
<VirtualHost *:8080>
587-
ServerAdmin webmaster@contoso.com
588589
DocumentRoot "/setup/altdocs"
589590
ServerName altservice.contoso.com:8080
590591
</VirtualHost>
592+
</IfDefine>
593+
594+
<IfDefine !ALTSPN>
595+
<VirtualHost *:8081>
596+
DocumentRoot "/setup/htdocs"
597+
</VirtualHost>
598+
</IfDefine>
591599

592600

593601
<IFDefine NTLM>

src/libraries/Common/tests/System/Net/EnterpriseTests/setup/apacheweb/run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if [ "$1" == "-debug" ]; then
1111
fi
1212

1313
if [ "$1" == "-DNTLM" ]; then
14+
# NTLM/Winbind is aggressive and eats Negotiate so it cannot be combined with Kerberos
1415
./setup-pdc.sh
1516
/usr/sbin/apache2 -DALTPORT "$@"
1617
shift

src/libraries/Common/tests/System/Net/EnterpriseTests/setup/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ services:
4141
hostname: altweb
4242
domainname: linux.contoso.com
4343
dns_search: linux.contoso.com
44-
command: -DALTPORT
44+
command: "-DALTPORT -DALTSPN"
4545
volumes:
4646
- shared-volume:/SHARED
4747
networks:

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/AuthenticationHelper.NtAuth.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,38 @@ namespace System.Net.Http
1313
{
1414
internal static partial class AuthenticationHelper
1515
{
16+
private const string UsePortInSpnCtxSwitch = "System.Net.Http.UsePortInSpn";
17+
private const string UsePortInSpnEnvironmentVariable = "DOTNET_SYSTEM_NET_HTTP_USEPORTINSPN";
18+
19+
private static volatile int s_usePortInSpn = -1;
20+
21+
private static bool UsePortInSpn
22+
{
23+
get
24+
{
25+
int usePortInSpn = s_usePortInSpn;
26+
if (usePortInSpn != -1)
27+
{
28+
return usePortInSpn != 0;
29+
}
30+
31+
// First check for the AppContext switch, giving it priority over the environment variable.
32+
if (AppContext.TryGetSwitch(UsePortInSpnCtxSwitch, out bool value))
33+
{
34+
s_usePortInSpn = value ? 1 : 0;
35+
}
36+
else
37+
{
38+
// AppContext switch wasn't used. Check the environment variable.
39+
s_usePortInSpn =
40+
Environment.GetEnvironmentVariable(UsePortInSpnEnvironmentVariable) is string envVar &&
41+
(envVar == "1" || envVar.Equals("true", StringComparison.OrdinalIgnoreCase)) ? 1 : 0;
42+
}
43+
44+
return s_usePortInSpn != 0;
45+
}
46+
}
47+
1648
private static Task<HttpResponseMessage> InnerSendAsync(HttpRequestMessage request, bool async, bool isProxyAuth, HttpConnectionPool pool, HttpConnection connection, CancellationToken cancellationToken)
1749
{
1850
return isProxyAuth ?
@@ -110,7 +142,7 @@ private static async Task<HttpResponseMessage> SendWithNtAuthAsync(HttpRequestMe
110142
hostName = result.HostName;
111143
}
112144

113-
if (!isProxyAuth && !authUri.IsDefaultPort)
145+
if (!isProxyAuth && !authUri.IsDefaultPort && UsePortInSpn)
114146
{
115147
hostName = string.Create(null, stackalloc char[128], $"{hostName}:{authUri.Port}");
116148
}

src/libraries/System.Net.Http/tests/EnterpriseTests/HttpClientAuthenticationTest.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,39 @@
33

44
using System.Net.Test.Common;
55
using System.Threading.Tasks;
6-
6+
using Microsoft.DotNet.RemoteExecutor;
77
using Xunit;
88

99
namespace System.Net.Http.Enterprise.Tests
1010
{
1111
[ConditionalClass(typeof(EnterpriseTestConfiguration), nameof(EnterpriseTestConfiguration.Enabled))]
1212
public class HttpClientAuthenticationTest
1313
{
14+
private const string AppContextSettingName = "System.Net.Http.UsePortInSpn";
15+
1416
[Theory]
1517
[InlineData(EnterpriseTestConfiguration.NegotiateAuthWebServer, false)]
16-
[InlineData(EnterpriseTestConfiguration.AlternativeService, false)]
18+
[InlineData(EnterpriseTestConfiguration.NegotiateAuthWebServerNotDefaultPort, false)]
19+
[InlineData(EnterpriseTestConfiguration.AlternativeService, false, true)]
1720
[InlineData(EnterpriseTestConfiguration.DigestAuthWebServer, true)]
1821
[InlineData(EnterpriseTestConfiguration.DigestAuthWebServer, false)]
1922
[InlineData(EnterpriseTestConfiguration.NtlmAuthWebServer, true)]
20-
public async Task HttpClient_ValidAuthentication_Success(string url, bool useDomain)
23+
public void HttpClient_ValidAuthentication_Success(string url, bool useDomain, bool useAltPort = false)
2124
{
22-
using var handler = new HttpClientHandler();
23-
handler.Credentials = useDomain ? EnterpriseTestConfiguration.ValidDomainNetworkCredentials : EnterpriseTestConfiguration.ValidNetworkCredentials;
24-
using var client = new HttpClient(handler);
25+
RemoteExecutor.Invoke((url, useAltPort, useDomain) =>
26+
{
27+
// This is safe as we have no parallel tests
28+
if (!string.IsNullOrEmpty(useAltPort))
29+
{
30+
AppContext.SetSwitch(AppContextSettingName, true);
31+
}
32+
using var handler = new HttpClientHandler();
33+
handler.Credentials = string.IsNullOrEmpty(useDomain) ? EnterpriseTestConfiguration.ValidNetworkCredentials : EnterpriseTestConfiguration.ValidDomainNetworkCredentials;
34+
using var client = new HttpClient(handler);
2535

26-
using HttpResponseMessage response = await client.GetAsync(url);
27-
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
36+
using HttpResponseMessage response = client.GetAsync(url).GetAwaiter().GetResult();
37+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
38+
}, url, useAltPort ? "true" : "" , useDomain ? "true" : "").Dispose();
2839
}
2940

3041
[ActiveIssue("https://github.com/dotnet/runtime/issues/416")]
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
4+
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
45
</PropertyGroup>
56
<ItemGroup>
67
<Compile Include="HttpClientAuthenticationTest.cs" />
78

89
<Compile Include="$(CommonTestPath)System\Net\EnterpriseTests\EnterpriseTestConfiguration.cs"
910
Link="Common\System\Net\EnterpriseTests\EnterpriseTestConfiguration.cs" />
1011
</ItemGroup>
11-
</Project>
12+
</Project>

0 commit comments

Comments
 (0)