Skip to content

Commit

Permalink
Refactor access to insecure setting
Browse files Browse the repository at this point in the history
  • Loading branch information
pvanbuijtene committed Dec 19, 2022
1 parent 102c7f3 commit fdc4d63
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 22 deletions.
5 changes: 2 additions & 3 deletions src/EventStore.Client/ChannelCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ public ChannelCache(EventStoreClientSettings settings) {
DnsEndPointEqualityComparer.Instance);
}

public TChannel GetChannelInfo(DnsEndPoint endPoint, bool? https = null) {
public TChannel GetChannelInfo(DnsEndPoint endPoint) {
lock (_lock) {
ThrowIfDisposed();

if (!_channels.TryGetValue(endPoint, out var channel)) {
channel = ChannelFactory.CreateChannel(
settings: _settings,
endPoint: endPoint,
https: https ?? !_settings.ConnectivitySettings.Insecure);
endPoint: endPoint);
_channels[endPoint] = channel;
}

Expand Down
9 changes: 3 additions & 6 deletions src/EventStore.Client/ChannelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ namespace EventStore.Client {
internal static class ChannelFactory {
private const int MaxReceiveMessageLength = 17 * 1024 * 1024;

public static TChannel CreateChannel(EventStoreClientSettings settings, EndPoint endPoint, bool https) =>
CreateChannel(settings, endPoint.ToUri(https));
public static TChannel CreateChannel(EventStoreClientSettings settings, EndPoint endPoint) {
var address = endPoint.ToUri(!settings.ConnectivitySettings.Insecure);

public static TChannel CreateChannel(EventStoreClientSettings settings, Uri? address) {
address ??= settings.ConnectivitySettings.Address;

if (address.Scheme == Uri.UriSchemeHttp ||settings.ConnectivitySettings.Insecure) {
if (settings.ConnectivitySettings.Insecure) {
//this must be switched on before creation of the HttpMessageHandler
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
}
Expand Down
36 changes: 29 additions & 7 deletions src/EventStore.Client/EventStoreClientConnectivitySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@ namespace EventStore.Client {
/// A class used to describe how to connect to an instance of EventStoreDB.
/// </summary>
public class EventStoreClientConnectivitySettings {
/// <summary>
/// The <see cref="Uri"/> of the EventStoreDB. Use this when connecting to a single node.
/// </summary>
public Uri Address { get; set; } = new UriBuilder {
private const int DefaultPort = 2113;
private bool _insecure;
private Uri? _address;
private Uri _defaultAddress = new UriBuilder {
Scheme = Uri.UriSchemeHttps,
Port = 2113
Port = DefaultPort
}.Uri;

/// <summary>
/// The <see cref="Uri"/> of the EventStoreDB. Use this when connecting to a single node.
/// </summary>
public Uri Address {
get { return _address == null ? _defaultAddress : _address; }
set { _address = value; }
}

/// <summary>
/// The maximum number of times to attempt <see cref="EndPoint"/> discovery.
/// </summary>
Expand Down Expand Up @@ -74,11 +82,25 @@ public class EventStoreClientConnectivitySettings {
/// True if pointing to a single EventStoreDB node.
/// </summary>
public bool IsSingleNode => GossipSeeds.Length == 0;

/// <summary>
/// True if communicating over an insecure channel; otherwise false.
/// </summary>
public bool Insecure { get; set; }
public bool Insecure {
get {
return IsSingleNode
? string.Compare(Address.Scheme, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) != 0
: _insecure;
}
set {
_insecure = value;

_defaultAddress = new UriBuilder {
Scheme = value ? Uri.UriSchemeHttp : Uri.UriSchemeHttps,
Port = DefaultPort
}.Uri;
}
}

/// <summary>
/// True if certificates will be validated; otherwise false.
Expand Down
6 changes: 2 additions & 4 deletions src/EventStore.Client/SingleNodeChannelSelector.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -11,7 +10,6 @@ internal class SingleNodeChannelSelector : IChannelSelector {
private readonly ILogger _log;
private readonly ChannelCache _channelCache;
private readonly DnsEndPoint _endPoint;
private readonly bool _https;

public SingleNodeChannelSelector(
EventStoreClientSettings settings,
Expand All @@ -21,9 +19,9 @@ public SingleNodeChannelSelector(
new NullLogger<SingleNodeChannelSelector>();

_channelCache = channelCache;

var uri = settings.ConnectivitySettings.Address;
_endPoint = new DnsEndPoint(host: uri.Host, port: uri.Port);
_https = string.Compare(uri.Scheme, Uri.UriSchemeHttps, ignoreCase: true) == 0;
}

public Task<ChannelBase> SelectChannelAsync(CancellationToken cancellationToken) =>
Expand All @@ -32,7 +30,7 @@ public Task<ChannelBase> SelectChannelAsync(CancellationToken cancellationToken)
public ChannelBase SelectChannel(DnsEndPoint endPoint) {
_log.LogInformation("Selected {endPoint}.", endPoint);

return _channelCache.GetChannelInfo(endPoint, _https);
return _channelCache.GetChannelInfo(endPoint);
}
}
}
47 changes: 47 additions & 0 deletions test/EventStore.Client.Tests/ConnectionStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,53 @@ public void with_default_settings() {
Assert.Equal(EventStoreClientConnectivitySettings.Default.KeepAliveTimeout,
settings.ConnectivitySettings.KeepAliveTimeout);
}

[Theory,
InlineData("esdb://localhost", true),
InlineData("esdb://localhost/?tls=false", false),
InlineData("esdb://localhost/?tls=true", true),
InlineData("esdb://localhost1,localhost2,localhost3", true),
InlineData("esdb://localhost1,localhost2,localhost3/?tls=false", false),
InlineData("esdb://localhost1,localhost2,localhost3/?tls=true", true)]
public void use_tls(string connectionString, bool useTls) {
var result = EventStoreClientSettings.Create(connectionString);

Assert.NotEqual(useTls, result.ConnectivitySettings.Insecure);
Assert.Equal(useTls ? "https":"http", result.ConnectivitySettings.Address.Scheme);
}

[Theory,
InlineData("esdb://localhost", null, false),
InlineData("esdb://localhost", true, true),
InlineData("esdb://localhost", false, false),
InlineData("esdb://localhost/?tls=true", null, false),
InlineData("esdb://localhost/?tls=true", true, true),
InlineData("esdb://localhost/?tls=true", false, false),
InlineData("esdb://localhost/?tls=false", null, true),
InlineData("esdb://localhost/?tls=false", true, true),
InlineData("esdb://localhost/?tls=false", false, false),
InlineData("esdb://localhost1,localhost2,localhost3", null, false),
InlineData("esdb://localhost1,localhost2,localhost3", true, false),
InlineData("esdb://localhost1,localhost2,localhost3", false, false),
InlineData("esdb://localhost1,localhost2,localhost3/?tls=true", null, false),
InlineData("esdb://localhost1,localhost2,localhost3/?tls=true", true, false),
InlineData("esdb://localhost1,localhost2,localhost3/?tls=true", false, false),
InlineData("esdb://localhost1,localhost2,localhost3/?tls=false", null, true),
InlineData("esdb://localhost1,localhost2,localhost3/?tls=false", true, true),
InlineData("esdb://localhost1,localhost2,localhost3/?tls=false", false, true),
]
public void override_tls_for_single_node(string connectionString, bool? insecureOverride, bool expected) {
var result = EventStoreClientSettings.Create(connectionString);
var settings = result.ConnectivitySettings;

if (insecureOverride.HasValue) {
settings.Address = new UriBuilder {
Scheme = insecureOverride.Value ? "hTTp" : "HttpS",
}.Uri;
}

Assert.Equal(expected, settings.Insecure);
}

private static string GetConnectionString(EventStoreClientSettings settings,
Func<string, string>? getKey = default)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using EventStore.Client.ServerFeatures;
using Grpc.Core;
Expand Down Expand Up @@ -69,7 +69,7 @@ await sut.GetAsync(
new EventStoreClientSettings {
CreateHttpMessageHandler = kestrel.CreateHandler
},
new UriBuilder().Uri)
new DnsEndPoint("localhost", 80))
.CreateCallInvoker(),
cancellationToken: default);

Expand Down

0 comments on commit fdc4d63

Please sign in to comment.