Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions source/Halibut.Tests/ClientServerLifecycleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Copyright 2012-2013 Octopus Deploy Pty. Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Halibut.Diagnostics;
using Halibut.ServiceModel;
using Halibut.Tests.Support;
using NUnit.Framework;

namespace Halibut.Tests
{
public class ClientServerLifecycleTests : BaseTest
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a slimmed down variant of what the DotMemory test does. This test exercises the code paths that were problematic with .NET4.8 and will also run on .net8.0

{
[Test]
public async Task ListeningConfiguration()
{
await using var server = RunServer(out var serverPort);

await using var runtime = CreateRuntimeForListener();
var client = CreateClient(runtime, serverPort);
var result = await client.AddAsync(2, 2);
result.Should().Be(4);
}

[Test]
public async Task PollingConfiguration()
{
await using var server = RunServer(out var serverPort);
await using var runtime = CreateRuntimeForPoller(server, out var client);
var result = await client.AddAsync(2, 2);
result.Should().Be(4);
}

[Test]
public async Task ListeningThenPollingConfiguration()
{
// On NET4.8 with SslProtocols.None this will result in SSPI errors
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running each of the above tests independently succeeded on .NET4.8 with SslProtocols.None, but when run in sequence will fail.

This added test exists to provide a straightforward entrypoint into the problem that can be run independently.

await ListeningConfiguration();
await PollingConfiguration();
}

HalibutRuntime CreateRuntimeForListener()
{
var runtime = new HalibutRuntimeBuilder()
.WithServerCertificate(Certificates.TentacleListening)
.WithLogFactory(new TestLogFactory(HalibutLog))
.Build();
return runtime;
}

HalibutRuntime CreateRuntimeForPoller(HalibutRuntime serverRuntime, out IAsyncClientCalculatorService client)
{
var runtime = new HalibutRuntimeBuilder()
.WithServerCertificate(Certificates.TentaclePolling)
.WithLogFactory(new TestLogFactory(HalibutLog))
.Build();
var port = runtime.Listen();
runtime.Trust(Certificates.OctopusPublicThumbprint);

var pollEndpoint = new ServiceEndPoint(
baseUri: new Uri($"https://localhost:{port}/"),
remoteThumbprint: Certificates.TentaclePollingPublicThumbprint,
halibutTimeoutsAndLimits: runtime.TimeoutsAndLimits
)
{
TcpClientConnectTimeout = TimeSpan.FromSeconds(5)
};
var pollingUri = new Uri("poll://TEST-POLL");
serverRuntime.Poll(pollingUri, pollEndpoint, CancellationToken);
var clientEndpoint = new ServiceEndPoint(
baseUri: pollingUri,
remoteThumbprint: Certificates.OctopusPublicThumbprint,
halibutTimeoutsAndLimits: runtime.TimeoutsAndLimits
);
client = runtime.CreateAsyncClient<ICalculatorService, IAsyncClientCalculatorService>(clientEndpoint);

return runtime;
}

static IAsyncClientCalculatorService CreateClient(HalibutRuntime runtime, int port)
{
var endpoint = new ServiceEndPoint(
baseUri: $"https://localhost:{port}",
remoteThumbprint: Certificates.OctopusPublicThumbprint,
halibutTimeoutsAndLimits: runtime.TimeoutsAndLimits
);
var client = runtime
.CreateAsyncClient<ICalculatorService, IAsyncClientCalculatorService>(endpoint);
return client;
}

static IServiceFactory CreateServiceFactory()
{
var services = new DelegateServiceFactory();
services.Register<ICalculatorService, IAsyncCalculatorService>(() => new AsyncCalculatorService());
return services;
}

HalibutRuntime RunServer(out int port)
{
var services = CreateServiceFactory();

var runtime = new HalibutRuntimeBuilder()
.WithServerCertificate(Certificates.Octopus)
.WithServiceFactory(services)
.WithLogFactory(new TestLogFactory(HalibutLog))
.Build();

runtime.Trust(Certificates.TentacleListeningPublicThumbprint);
runtime.Trust(Certificates.TentaclePollingPublicThumbprint);
port = runtime.Listen();

return runtime;
}

public class TestLogFactory : ILogFactory
{
readonly ILog _log;
public TestLogFactory(ILog log)
{
_log = log;
}

public ILog ForEndpoint(Uri endpoint) => _log;

public ILog ForPrefix(string endPoint) => _log;
}

public interface ICalculatorService
{
long Add(long a, long b);
}

public interface IAsyncCalculatorService
{
Task<long> AddAsync(long a, long b, CancellationToken cancellationToken);
}

public interface IAsyncClientCalculatorService
{
Task<long> AddAsync(long a, long b);
}

public class AsyncCalculatorService : IAsyncCalculatorService
{
public async Task<long> AddAsync(long a, long b, CancellationToken cancellationToken)
{
await Task.CompletedTask;
return a + b;
}
}
}
}
8 changes: 7 additions & 1 deletion source/Halibut.Tests/Transport/SecureClientFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ public async Task SecureClientClearsPoolWhenAllConnectionsCorrupt()
Params = new object[] { "Fred" }
};

var tcpConnectionFactory = new TcpConnectionFactory(Certificates.Octopus, halibutTimeoutsAndLimits, new StreamFactory(), NoOpSecureConnectionObserver.Instance);
var tcpConnectionFactory = new TcpConnectionFactory(
Certificates.Octopus,
halibutTimeoutsAndLimits,
new StreamFactory(),
NoOpSecureConnectionObserver.Instance,
SslConfiguration.Default
);
var secureClient = new SecureListeningClient(GetProtocol, endpoint, Certificates.Octopus, log, connectionManager, tcpConnectionFactory);
ResponseMessage response = null!;

Expand Down
3 changes: 2 additions & 1 deletion source/Halibut.Tests/Transport/SecureListenerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public async Task SecureListenerDoesNotCreateHundredsOfIoEventsPerSecondOnWindow
timeoutsAndLimits,
new StreamFactory(),
NoOpConnectionsObserver.Instance,
NoOpSecureConnectionObserver.Instance
NoOpSecureConnectionObserver.Instance,
SslConfiguration.Default
);

var idleAverage = CollectCounterValues(opsPerSec)
Expand Down
12 changes: 8 additions & 4 deletions source/Halibut/HalibutRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class HalibutRuntime : IHalibutRuntime
readonly ISecureConnectionObserver secureConnectionObserver;
readonly IActiveTcpConnectionsLimiter activeTcpConnectionsLimiter;
readonly IControlMessageObserver controlMessageObserver;
readonly ISslConfigurationProvider sslConfigurationProvider;

internal HalibutRuntime(
IServiceFactory serviceFactory,
Expand All @@ -61,7 +62,8 @@ internal HalibutRuntime(
IRpcObserver rpcObserver,
IConnectionsObserver connectionsObserver,
IControlMessageObserver controlMessageObserver,
ISecureConnectionObserver secureConnectionObserver
ISecureConnectionObserver secureConnectionObserver,
ISslConfigurationProvider sslConfigurationProvider
)
{
this.serverCertificate = serverCertificate;
Expand All @@ -78,9 +80,10 @@ ISecureConnectionObserver secureConnectionObserver
this.connectionsObserver = connectionsObserver;
this.secureConnectionObserver = secureConnectionObserver;
this.controlMessageObserver = controlMessageObserver;
this.sslConfigurationProvider = sslConfigurationProvider;

connectionManager = new ConnectionManagerAsync();
this.tcpConnectionFactory = new TcpConnectionFactory(serverCertificate, TimeoutsAndLimits, streamFactory, secureConnectionObserver);
tcpConnectionFactory = new TcpConnectionFactory(serverCertificate, TimeoutsAndLimits, streamFactory, secureConnectionObserver, sslConfigurationProvider);
activeTcpConnectionsLimiter = new ActiveTcpConnectionsLimiter(TimeoutsAndLimits);
}

Expand Down Expand Up @@ -135,7 +138,8 @@ public int Listen(IPEndPoint endpoint)
TimeoutsAndLimits,
streamFactory,
connectionsObserver,
secureConnectionObserver
secureConnectionObserver,
sslConfigurationProvider
);

listeners.DoWithExclusiveAccess(l =>
Expand Down Expand Up @@ -202,7 +206,7 @@ public async Task<ServiceEndPoint> DiscoverAsync(Uri uri, CancellationToken canc

public async Task<ServiceEndPoint> DiscoverAsync(ServiceEndPoint endpoint, CancellationToken cancellationToken)
{
var client = new DiscoveryClient(streamFactory);
var client = new DiscoveryClient(streamFactory, sslConfigurationProvider);
return await client.DiscoverAsync(endpoint, TimeoutsAndLimits, cancellationToken);
}

Expand Down
12 changes: 11 additions & 1 deletion source/Halibut/HalibutRuntimeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Halibut.Queue;
using Halibut.Queue.MessageStreamWrapping;
using Halibut.ServiceModel;
using Halibut.Transport;
using Halibut.Transport.Observability;
using Halibut.Transport.Protocol;
using Halibut.Transport.Streams;
Expand All @@ -31,6 +32,7 @@ public class HalibutRuntimeBuilder
ISecureConnectionObserver? secureConnectionObserver;
IControlMessageObserver? controlMessageObserver;
MessageStreamWrappers queueMessageStreamWrappers = new();
ISslConfigurationProvider? sslConfigurationProvider;

public HalibutRuntimeBuilder WithQueueMessageStreamWrappers(MessageStreamWrappers queueMessageStreamWrappers)
{
Expand All @@ -50,6 +52,12 @@ public HalibutRuntimeBuilder WithSecureConnectionObserver(ISecureConnectionObser
return this;
}

public HalibutRuntimeBuilder WithSslConfigurationProvider(ISslConfigurationProvider sslConfigurationProvider)
{
this.sslConfigurationProvider = sslConfigurationProvider;
return this;
}

internal HalibutRuntimeBuilder WithStreamFactory(IStreamFactory streamFactory)
{
this.streamFactory = streamFactory;
Expand Down Expand Up @@ -185,6 +193,7 @@ public HalibutRuntime Build()
var secureConnectionObserver = this.secureConnectionObserver ?? NoOpSecureConnectionObserver.Instance;
var rpcObserver = this.rpcObserver ?? new NoRpcObserver();
var controlMessageObserver = this.controlMessageObserver ?? new NoOpControlMessageObserver();
var sslConfigurationProvider = this.sslConfigurationProvider ?? SslConfiguration.Default;

var halibutRuntime = new HalibutRuntime(
serviceFactory,
Expand All @@ -200,7 +209,8 @@ public HalibutRuntime Build()
rpcObserver,
connectionsObserver,
controlMessageObserver,
secureConnectionObserver
secureConnectionObserver,
sslConfigurationProvider
);

if (onUnauthorizedClientConnect is not null)
Expand Down
27 changes: 27 additions & 0 deletions source/Halibut/Transport/DefaultSslConfigurationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2012-2013 Octopus Deploy Pty. Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Security.Authentication;

namespace Halibut.Transport
{
/// <summary>
/// Provides a default implementation of ISslConfigurationProvider that uses the system defaults.
/// </summary>
public class DefaultSslConfigurationProvider : ISslConfigurationProvider
{
public SslProtocols SupportedProtocols => SslProtocols.None;
}
}
16 changes: 14 additions & 2 deletions source/Halibut/Transport/DiscoveryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ public class DiscoveryClient
readonly LogFactory logs = new ();

readonly IStreamFactory streamFactory;
readonly ISslConfigurationProvider sslConfigurationProvider;

public DiscoveryClient(IStreamFactory streamFactory)
: this(streamFactory, SslConfiguration.Default)
{
}

public DiscoveryClient(IStreamFactory streamFactory, ISslConfigurationProvider sslConfigurationProvider)
{
this.streamFactory = streamFactory;
this.sslConfigurationProvider = sslConfigurationProvider;
}

public async Task<ServiceEndPoint> DiscoverAsync(ServiceEndPoint serviceEndpoint, HalibutTimeoutsAndLimits halibutTimeoutsAndLimits, CancellationToken cancellationToken)
Expand All @@ -45,10 +52,15 @@ public async Task<ServiceEndPoint> DiscoverAsync(ServiceEndPoint serviceEndpoint
await ssl.AuthenticateAsClientAsync(
serviceEndpoint.BaseUri.Host,
new X509Certificate2Collection(),
SslConfiguration.SupportedProtocols,
sslConfigurationProvider.SupportedProtocols,
false);
#else
await ssl.AuthenticateAsClientEnforcingTimeout(serviceEndpoint, new X509Certificate2Collection(), cancellationToken);
await ssl.AuthenticateAsClientEnforcingTimeout(
serviceEndpoint,
new X509Certificate2Collection(),
sslConfigurationProvider,
cancellationToken
);
#endif
await ssl.WriteAsync(HelloLine, 0, HelloLine.Length, cancellationToken);
await ssl.FlushAsync(cancellationToken);
Expand Down
23 changes: 23 additions & 0 deletions source/Halibut/Transport/ISslConfigurationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2012-2013 Octopus Deploy Pty. Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Security.Authentication;

namespace Halibut.Transport
{
public interface ISslConfigurationProvider
{
public SslProtocols SupportedProtocols { get; }
}
}
Loading